Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob96e68805b00a4f8893a61cbfb47d55b920e92180
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 : Node_Id;
158 Component_List : List_Id;
159 Loc : Source_Ptr) return Node_Id;
160 -- Compute number of entries for concurrent object. This is a count of
161 -- simple entries, followed by an expression that computes the length
162 -- of the range of each entry family. A single array with that size is
163 -- allocated for each concurrent object of the type.
165 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
166 -- Build the function that translates the entry index in the call
167 -- (which depends on the size of entry families) into an index into the
168 -- Entry_Bodies_Array, to determine the body and barrier function used
169 -- in a protected entry call. A pointer to this function appears in every
170 -- protected object.
172 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
173 -- Build subprogram declaration for previous one
175 function Build_Lock_Free_Protected_Subprogram_Body
176 (N : Node_Id;
177 Prot_Typ : Node_Id;
178 Unprot_Spec : Node_Id) return Node_Id;
179 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
180 -- the subprogram specification of the unprotected version of N. Transform
181 -- N such that it invokes the unprotected version of the body.
183 function Build_Lock_Free_Unprotected_Subprogram_Body
184 (N : Node_Id;
185 Prot_Typ : Node_Id) return Node_Id;
186 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
187 -- of N where the original statements of N are synchronized through atomic
188 -- actions such as compare and exchange. Prior to invoking this routine, it
189 -- has been established that N can be implemented in a lock-free fashion.
191 function Build_Parameter_Block
192 (Loc : Source_Ptr;
193 Actuals : List_Id;
194 Formals : List_Id;
195 Decls : List_Id) return Entity_Id;
196 -- Generate an access type for each actual parameter in the list Actuals.
197 -- Create an encapsulating record that contains all the actuals and return
198 -- its type. Generate:
199 -- type Ann1 is access all <actual1-type>
200 -- ...
201 -- type AnnN is access all <actualN-type>
202 -- type Pnn is record
203 -- <formal1> : Ann1;
204 -- ...
205 -- <formalN> : AnnN;
206 -- end record;
208 function Build_Protected_Entry
209 (N : Node_Id;
210 Ent : Entity_Id;
211 Pid : Node_Id) return Node_Id;
212 -- Build the procedure implementing the statement sequence of the specified
213 -- entry body.
215 function Build_Protected_Entry_Specification
216 (Loc : Source_Ptr;
217 Def_Id : Entity_Id;
218 Ent_Id : Entity_Id) return Node_Id;
219 -- Build a specification for the procedure implementing the statements of
220 -- the specified entry body. Add attributes associating it with the entry
221 -- defining identifier Ent_Id.
223 function Build_Protected_Spec
224 (N : Node_Id;
225 Obj_Type : Entity_Id;
226 Ident : Entity_Id;
227 Unprotected : Boolean := False) return List_Id;
228 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
229 -- Subprogram_Type. Builds signature of protected subprogram, adding the
230 -- formal that corresponds to the object itself. For an access to protected
231 -- subprogram, there is no object type to specify, so the parameter has
232 -- type Address and mode In. An indirect call through such a pointer will
233 -- convert the address to a reference to the actual object. The object is
234 -- a limited record and therefore a by_reference type.
236 function Build_Protected_Subprogram_Body
237 (N : Node_Id;
238 Pid : Node_Id;
239 N_Op_Spec : Node_Id) return Node_Id;
240 -- This function is used to construct the protected version of a protected
241 -- subprogram. Its statement sequence first defers abort, then locks the
242 -- associated protected object, and then enters a block that contains a
243 -- call to the unprotected version of the subprogram (for details, see
244 -- Build_Unprotected_Subprogram_Body). This block statement requires a
245 -- cleanup handler that unlocks the object in all cases. For details,
246 -- see Exp_Ch7.Expand_Cleanup_Actions.
248 function Build_Renamed_Formal_Declaration
249 (New_F : Entity_Id;
250 Formal : Entity_Id;
251 Comp : Entity_Id;
252 Renamed_Formal : Node_Id) return Node_Id;
253 -- Create a renaming declaration for a formal, within a protected entry
254 -- body or an accept body. The renamed object is a component of the
255 -- parameter block that is a parameter in the entry call.
257 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
258 -- does not dereference the corresponding component to prevent an illegal
259 -- use of the incomplete type (AI05-0151).
261 function Build_Selected_Name
262 (Prefix : Entity_Id;
263 Selector : Entity_Id;
264 Append_Char : Character := ' ') return Name_Id;
265 -- Build a name in the form of Prefix__Selector, with an optional character
266 -- appended. This is used for internal subprograms generated for operations
267 -- of protected types, including barrier functions. For the subprograms
268 -- generated for entry bodies and entry barriers, the generated name
269 -- includes a sequence number that makes names unique in the presence of
270 -- entry overloading. This is necessary because entry body procedures and
271 -- barrier functions all have the same signature.
273 procedure Build_Simple_Entry_Call
274 (N : Node_Id;
275 Concval : Node_Id;
276 Ename : Node_Id;
277 Index : Node_Id);
278 -- Build the call corresponding to the task entry call. N is the task entry
279 -- call, Concval is the concurrent object, Ename is the entry name and
280 -- Index is the entry family index.
281 -- Note that N might be expanded into an N_Block_Statement if it gets
282 -- inlined.
284 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
285 -- This routine constructs a specification for the procedure that we will
286 -- build for the task body for task type T. The spec has the form:
288 -- procedure tnameB (_Task : access tnameV);
290 -- where name is the character name taken from the task type entity that
291 -- is passed as the argument to the procedure, and tnameV is the task
292 -- value type that is associated with the task type.
294 function Build_Unprotected_Subprogram_Body
295 (N : Node_Id;
296 Pid : Node_Id) return Node_Id;
297 -- This routine constructs the unprotected version of a protected
298 -- subprogram body, which contains all of the code in the original,
299 -- unexpanded body. This is the version of the protected subprogram that is
300 -- called from all protected operations on the same object, including the
301 -- protected version of the same subprogram.
303 procedure Build_Wrapper_Bodies
304 (Loc : Source_Ptr;
305 Typ : Entity_Id;
306 N : Node_Id);
307 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
308 -- record of a concurrent type. N is the insertion node where all bodies
309 -- will be placed. This routine builds the bodies of the subprograms which
310 -- serve as an indirection mechanism to overriding primitives of concurrent
311 -- types, entries and protected procedures. Any new body is analyzed.
313 procedure Build_Wrapper_Specs
314 (Loc : Source_Ptr;
315 Typ : Entity_Id;
316 N : in out Node_Id);
317 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
318 -- record of a concurrent type. N is the insertion node where all specs
319 -- will be placed. This routine builds the specs of the subprograms which
320 -- serve as an indirection mechanism to overriding primitives of concurrent
321 -- types, entries and protected procedures. Any new spec is analyzed.
323 procedure Collect_Entry_Families
324 (Loc : Source_Ptr;
325 Cdecls : List_Id;
326 Current_Node : in out Node_Id;
327 Conctyp : Entity_Id);
328 -- For each entry family in a concurrent type, create an anonymous array
329 -- type of the right size, and add a component to the corresponding_record.
331 function Concurrent_Object
332 (Spec_Id : Entity_Id;
333 Conc_Typ : Entity_Id) return Entity_Id;
334 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
335 -- the entity associated with the concurrent object in the Protected_Body_
336 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
337 -- denotes formal parameter _O, _object or _task.
339 function Copy_Result_Type (Res : Node_Id) return Node_Id;
340 -- Copy the result type of a function specification, when building the
341 -- internal operation corresponding to a protected function, or when
342 -- expanding an access to protected function. If the result is an anonymous
343 -- access to subprogram itself, we need to create a new signature with the
344 -- same parameter names and the same resolved types, but with new entities
345 -- for the formals.
347 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean;
348 -- Return whether a secondary stack for the task T should be created by the
349 -- expander. The secondary stack for a task will be created by the expander
350 -- if the size of the stack has been specified by the Secondary_Stack_Size
351 -- representation aspect and either the No_Implicit_Heap_Allocations or
352 -- No_Implicit_Task_Allocations restrictions are in effect and the
353 -- No_Secondary_Stack restriction is not.
355 procedure Debug_Private_Data_Declarations (Decls : List_Id);
356 -- Decls is a list which may contain the declarations created by Install_
357 -- Private_Data_Declarations. All generated entities are marked as needing
358 -- debug info and debug nodes are manually generation where necessary. This
359 -- step of the expansion must to be done after private data has been moved
360 -- to its final resting scope to ensure proper visibility of debug objects.
362 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
363 -- If control flow optimizations are suppressed, and Alt is an accept,
364 -- delay, or entry call alternative with no trailing statements, insert
365 -- a null trailing statement with the given Loc (which is the sloc of
366 -- the accept, delay, or entry call statement). There might not be any
367 -- generated code for the accept, delay, or entry call itself (the effect
368 -- of these statements is part of the general processing done for the
369 -- enclosing selective accept, timed entry call, or asynchronous select),
370 -- and the null statement is there to carry the sloc of that statement to
371 -- the back-end for trace-based coverage analysis purposes.
373 procedure Extract_Dispatching_Call
374 (N : Node_Id;
375 Call_Ent : out Entity_Id;
376 Object : out Entity_Id;
377 Actuals : out List_Id;
378 Formals : out List_Id);
379 -- Given a dispatching call, extract the entity of the name of the call,
380 -- its actual dispatching object, its actual parameters and the formal
381 -- parameters of the overridden interface-level version. If the type of
382 -- the dispatching object is an access type then an explicit dereference
383 -- is returned in Object.
385 procedure Extract_Entry
386 (N : Node_Id;
387 Concval : out Node_Id;
388 Ename : out Node_Id;
389 Index : out Node_Id);
390 -- Given an entry call, returns the associated concurrent object, the entry
391 -- name, and the entry family index.
393 function Family_Offset
394 (Loc : Source_Ptr;
395 Hi : Node_Id;
396 Lo : Node_Id;
397 Ttyp : Entity_Id;
398 Cap : Boolean) return Node_Id;
399 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
400 -- accept statement, or the upper bound in the discrete subtype of an entry
401 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
402 -- type of the entry. If Cap is true, the result is capped according to
403 -- Entry_Family_Bound.
405 function Family_Size
406 (Loc : Source_Ptr;
407 Hi : Node_Id;
408 Lo : Node_Id;
409 Ttyp : Entity_Id;
410 Cap : Boolean) return Node_Id;
411 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
412 -- family, and handle properly the superflat case. This is equivalent to
413 -- the use of 'Length on the index type, but must use Family_Offset to
414 -- handle properly the case of bounds that depend on discriminants. If
415 -- Cap is true, the result is capped according to Entry_Family_Bound.
417 procedure Find_Enclosing_Context
418 (N : Node_Id;
419 Context : out Node_Id;
420 Context_Id : out Entity_Id;
421 Context_Decls : out List_Id);
422 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
423 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
424 -- nearest enclosing body, block, package, or return statement and return
425 -- its constituents. Context is the enclosing construct, Context_Id is
426 -- the scope of Context_Id and Context_Decls is the declarative list of
427 -- Context.
429 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
430 -- Given a subprogram identifier, return the entity which is associated
431 -- with the protection entry index in the Protected_Body_Subprogram or
432 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
433 -- parameter _E.
435 function Is_Potentially_Large_Family
436 (Base_Index : Entity_Id;
437 Conctyp : Entity_Id;
438 Lo : Node_Id;
439 Hi : Node_Id) return Boolean;
440 -- Determine whether an entry family is potentially large because one of
441 -- its bounds denotes a discrminant.
443 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
444 -- Determine whether Id is a function or a procedure and is marked as a
445 -- private primitive.
447 function Null_Statements (Stats : List_Id) return Boolean;
448 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
449 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
450 -- to still count as null. Returns True for a null sequence. The argument
451 -- is the list of statements from the DO-END sequence.
453 function Parameter_Block_Pack
454 (Loc : Source_Ptr;
455 Blk_Typ : Entity_Id;
456 Actuals : List_Id;
457 Formals : List_Id;
458 Decls : List_Id;
459 Stmts : List_Id) return Entity_Id;
460 -- Set the components of the generated parameter block with the values
461 -- of the actual parameters. Generate aliased temporaries to capture the
462 -- values for types that are passed by copy. Otherwise generate a reference
463 -- to the actual's value. Return the address of the aggregate block.
464 -- Generate:
465 -- Jnn1 : alias <formal-type1>;
466 -- Jnn1 := <actual1>;
467 -- ...
468 -- P : Blk_Typ := (
469 -- Jnn1'unchecked_access;
470 -- <actual2>'reference;
471 -- ...);
473 function Parameter_Block_Unpack
474 (Loc : Source_Ptr;
475 P : Entity_Id;
476 Actuals : List_Id;
477 Formals : List_Id) return List_Id;
478 -- Retrieve the values of the components from the parameter block and
479 -- assign then to the original actual parameters. Generate:
480 -- <actual1> := P.<formal1>;
481 -- ...
482 -- <actualN> := P.<formalN>;
484 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id);
485 -- Reset the scope of declarations and blocks at the top level of Bod to
486 -- be E. Bod is either a block or a subprogram body. Used after expanding
487 -- various kinds of entry bodies into their corresponding constructs. This
488 -- is needed during unnesting to determine whether a body generated for an
489 -- entry or an accept alternative includes uplevel references.
491 function Trivial_Accept_OK return Boolean;
492 -- If there is no DO-END block for an accept, or if the DO-END block has
493 -- only null statements, then it is possible to do the Rendezvous with much
494 -- less overhead using the Accept_Trivial routine in the run-time library.
495 -- However, this is not always a valid optimization. Whether it is valid or
496 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
497 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
498 -- a rescheduling is required, so this optimization is not allowed. This
499 -- function returns True if the optimization is permitted.
501 -----------------------------
502 -- Actual_Index_Expression --
503 -----------------------------
505 function Actual_Index_Expression
506 (Sloc : Source_Ptr;
507 Ent : Entity_Id;
508 Index : Node_Id;
509 Tsk : Entity_Id) return Node_Id
511 Ttyp : constant Entity_Id := Etype (Tsk);
512 Expr : Node_Id;
513 Num : Node_Id;
514 Lo : Node_Id;
515 Hi : Node_Id;
516 Prev : Entity_Id;
517 S : Node_Id;
519 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
520 -- Compute difference between bounds of entry family
522 --------------------------
523 -- Actual_Family_Offset --
524 --------------------------
526 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
528 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
529 -- Replace a reference to a discriminant with a selected component
530 -- denoting the discriminant of the target task.
532 -----------------------------
533 -- Actual_Discriminant_Ref --
534 -----------------------------
536 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
537 Typ : constant Entity_Id := Etype (Bound);
538 B : Node_Id;
540 begin
541 if not Is_Entity_Name (Bound)
542 or else Ekind (Entity (Bound)) /= E_Discriminant
543 then
544 if Nkind (Bound) = N_Attribute_Reference then
545 return Bound;
546 else
547 B := New_Copy_Tree (Bound);
548 end if;
550 else
551 B :=
552 Make_Selected_Component (Sloc,
553 Prefix => New_Copy_Tree (Tsk),
554 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
556 Analyze_And_Resolve (B, Typ);
557 end if;
559 return
560 Make_Attribute_Reference (Sloc,
561 Attribute_Name => Name_Pos,
562 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
563 Expressions => New_List (B));
564 end Actual_Discriminant_Ref;
566 -- Start of processing for Actual_Family_Offset
568 begin
569 return
570 Make_Op_Subtract (Sloc,
571 Left_Opnd => Actual_Discriminant_Ref (Hi),
572 Right_Opnd => Actual_Discriminant_Ref (Lo));
573 end Actual_Family_Offset;
575 -- Start of processing for Actual_Index_Expression
577 begin
578 -- The queues of entries and entry families appear in textual order in
579 -- the associated record. The entry index is computed as the sum of the
580 -- number of queues for all entries that precede the designated one, to
581 -- which is added the index expression, if this expression denotes a
582 -- member of a family.
584 -- The following is a place holder for the count of simple entries
586 Num := Make_Integer_Literal (Sloc, 1);
588 -- We construct an expression which is a series of addition operations.
589 -- See comments in Entry_Index_Expression, which is identical in
590 -- structure.
592 if Present (Index) then
593 S := Entry_Index_Type (Ent);
595 -- First make sure the index is in range if requested. The index type
596 -- has been directly set on the prefix, see Resolve_Entry.
598 if Do_Range_Check (Index) then
599 Generate_Range_Check
600 (Index, Etype (Prefix (Parent (Index))), CE_Range_Check_Failed);
601 end if;
603 Expr :=
604 Make_Op_Add (Sloc,
605 Left_Opnd => Num,
606 Right_Opnd =>
607 Actual_Family_Offset (
608 Make_Attribute_Reference (Sloc,
609 Attribute_Name => Name_Pos,
610 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
611 Expressions => New_List (Relocate_Node (Index))),
612 Type_Low_Bound (S)));
613 else
614 Expr := Num;
615 end if;
617 -- Now add lengths of preceding entries and entry families
619 Prev := First_Entity (Ttyp);
620 while Chars (Prev) /= Chars (Ent)
621 or else (Ekind (Prev) /= Ekind (Ent))
622 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
623 loop
624 if Ekind (Prev) = E_Entry then
625 Set_Intval (Num, Intval (Num) + 1);
627 elsif Ekind (Prev) = E_Entry_Family then
628 S := Entry_Index_Type (Prev);
630 -- The need for the following full view retrieval stems from this
631 -- complex case of nested generics and tasking:
633 -- generic
634 -- type Formal_Index is range <>;
635 -- ...
636 -- package Outer is
637 -- type Index is private;
638 -- generic
639 -- ...
640 -- package Inner is
641 -- procedure P;
642 -- end Inner;
643 -- private
644 -- type Index is new Formal_Index range 1 .. 10;
645 -- end Outer;
647 -- package body Outer is
648 -- task type T is
649 -- entry Fam (Index); -- (2)
650 -- entry E;
651 -- end T;
652 -- package body Inner is -- (3)
653 -- procedure P is
654 -- begin
655 -- T.E; -- (1)
656 -- end P;
657 -- end Inner;
658 -- ...
660 -- We are currently building the index expression for the entry
661 -- call "T.E" (1). Part of the expansion must mention the range
662 -- of the discrete type "Index" (2) of entry family "Fam".
664 -- However only the private view of type "Index" is available to
665 -- the inner generic (3) because there was no prior mention of
666 -- the type inside "Inner". This visibility requirement is
667 -- implicit and cannot be detected during the construction of
668 -- the generic trees and needs special handling.
670 if In_Instance_Body
671 and then Is_Private_Type (S)
672 and then Present (Full_View (S))
673 then
674 S := Full_View (S);
675 end if;
677 Lo := Type_Low_Bound (S);
678 Hi := Type_High_Bound (S);
680 Expr :=
681 Make_Op_Add (Sloc,
682 Left_Opnd => Expr,
683 Right_Opnd =>
684 Make_Op_Add (Sloc,
685 Left_Opnd => Actual_Family_Offset (Hi, Lo),
686 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
688 -- Other components are anonymous types to be ignored
690 else
691 null;
692 end if;
694 Next_Entity (Prev);
695 end loop;
697 return Expr;
698 end Actual_Index_Expression;
700 --------------------------
701 -- Add_Formal_Renamings --
702 --------------------------
704 procedure Add_Formal_Renamings
705 (Spec : Node_Id;
706 Decls : List_Id;
707 Ent : Entity_Id;
708 Loc : Source_Ptr)
710 Ptr : constant Entity_Id :=
711 Defining_Identifier
712 (Next (First (Parameter_Specifications (Spec))));
713 -- The name of the formal that holds the address of the parameter block
714 -- for the call.
716 Comp : Entity_Id;
717 Decl : Node_Id;
718 Formal : Entity_Id;
719 New_F : Entity_Id;
720 Renamed_Formal : Node_Id;
722 begin
723 Formal := First_Formal (Ent);
724 while Present (Formal) loop
725 Comp := Entry_Component (Formal);
726 New_F :=
727 Make_Defining_Identifier (Sloc (Formal),
728 Chars => Chars (Formal));
729 Set_Etype (New_F, Etype (Formal));
730 Set_Scope (New_F, Ent);
732 -- Now we set debug info needed on New_F even though it does not come
733 -- from source, so that the debugger will get the right information
734 -- for these generated names.
736 Set_Debug_Info_Needed (New_F);
738 if Ekind (Formal) = E_In_Parameter then
739 Mutate_Ekind (New_F, E_Constant);
740 else
741 Mutate_Ekind (New_F, E_Variable);
742 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
743 end if;
745 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
747 Renamed_Formal :=
748 Make_Selected_Component (Loc,
749 Prefix =>
750 Make_Explicit_Dereference (Loc,
751 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
752 Make_Identifier (Loc, Chars (Ptr)))),
753 Selector_Name => New_Occurrence_Of (Comp, Loc));
755 Decl :=
756 Build_Renamed_Formal_Declaration
757 (New_F, Formal, Comp, Renamed_Formal);
759 Append (Decl, Decls);
760 Set_Renamed_Object (Formal, New_F);
761 Next_Formal (Formal);
762 end loop;
763 end Add_Formal_Renamings;
765 ------------------------
766 -- Add_Object_Pointer --
767 ------------------------
769 procedure Add_Object_Pointer
770 (Loc : Source_Ptr;
771 Conc_Typ : Entity_Id;
772 Decls : List_Id)
774 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
775 Decl : Node_Id;
776 Obj_Ptr : Node_Id;
778 begin
779 -- Create the renaming declaration for the Protection object of a
780 -- protected type. _Object is used by Complete_Entry_Body.
781 -- ??? An attempt to make this a renaming was unsuccessful.
783 -- Build the entity for the access type
785 Obj_Ptr :=
786 Make_Defining_Identifier (Loc,
787 New_External_Name (Chars (Rec_Typ), 'P'));
789 -- Generate:
790 -- _object : poVP := poVP!O;
792 Decl :=
793 Make_Object_Declaration (Loc,
794 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
795 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
796 Expression =>
797 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
798 Set_Debug_Info_Needed (Defining_Identifier (Decl));
799 Prepend_To (Decls, Decl);
801 -- Generate:
802 -- type poVP is access poV;
804 Decl :=
805 Make_Full_Type_Declaration (Loc,
806 Defining_Identifier =>
807 Obj_Ptr,
808 Type_Definition =>
809 Make_Access_To_Object_Definition (Loc,
810 Subtype_Indication =>
811 New_Occurrence_Of (Rec_Typ, Loc)));
812 Set_Debug_Info_Needed (Defining_Identifier (Decl));
813 Prepend_To (Decls, Decl);
814 end Add_Object_Pointer;
816 -----------------------
817 -- Build_Accept_Body --
818 -----------------------
820 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
821 Loc : constant Source_Ptr := Sloc (Astat);
822 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
823 New_S : Node_Id;
824 Hand : Node_Id;
825 Call : Node_Id;
826 Ohandle : Node_Id;
828 begin
829 -- At the end of the statement sequence, Complete_Rendezvous is called.
830 -- A label skipping the Complete_Rendezvous, and all other accept
831 -- processing, has already been added for the expansion of requeue
832 -- statements. The Sloc is copied from the last statement since it
833 -- is really part of this last statement.
835 Call :=
836 Build_Runtime_Call
837 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
838 Insert_Before (Last (Statements (Stats)), Call);
839 Analyze (Call);
841 -- Ada 2022 (AI12-0279)
843 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
844 and then RTE_Available (RE_Yield)
845 then
846 Insert_Action_After (Call,
847 Make_Procedure_Call_Statement (Loc,
848 New_Occurrence_Of (RTE (RE_Yield), Loc)));
849 end if;
851 -- If exception handlers are present, then append Complete_Rendezvous
852 -- calls to the handlers, and construct the required outer block. As
853 -- above, the Sloc is copied from the last statement in the sequence.
855 if Present (Exception_Handlers (Stats)) then
856 Hand := First (Exception_Handlers (Stats));
857 while Present (Hand) loop
858 Call :=
859 Build_Runtime_Call
860 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
861 Append (Call, Statements (Hand));
862 Analyze (Call);
864 -- Ada 2022 (AI12-0279)
866 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
867 and then RTE_Available (RE_Yield)
868 then
869 Insert_Action_After (Call,
870 Make_Procedure_Call_Statement (Loc,
871 New_Occurrence_Of (RTE (RE_Yield), Loc)));
872 end if;
874 Next (Hand);
875 end loop;
877 New_S :=
878 Make_Handled_Sequence_Of_Statements (Loc,
879 Statements => New_List (
880 Make_Block_Statement (Loc,
881 Handled_Statement_Sequence => Stats)));
883 else
884 New_S := Stats;
885 end if;
887 -- At this stage we know that the new statement sequence does
888 -- not have an exception handler part, so we supply one to call
889 -- Exceptional_Complete_Rendezvous. This handler is
891 -- when all others =>
892 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
894 -- We handle Abort_Signal to make sure that we properly catch the abort
895 -- case and wake up the caller.
897 Call :=
898 Make_Procedure_Call_Statement (Sloc (Stats),
899 Name => New_Occurrence_Of (
900 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
901 Parameter_Associations => New_List (
902 Make_Function_Call (Sloc (Stats),
903 Name =>
904 New_Occurrence_Of
905 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))));
907 Ohandle := Make_Others_Choice (Loc);
908 Set_All_Others (Ohandle);
910 Set_Exception_Handlers (New_S,
911 New_List (
912 Make_Implicit_Exception_Handler (Loc,
913 Exception_Choices => New_List (Ohandle),
915 Statements => New_List (Call))));
917 -- Ada 2022 (AI12-0279)
919 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
920 and then RTE_Available (RE_Yield)
921 then
922 Insert_Action_After (Call,
923 Make_Procedure_Call_Statement (Loc,
924 New_Occurrence_Of (RTE (RE_Yield), Loc)));
925 end if;
927 Set_Parent (New_S, Astat); -- temp parent for Analyze call
928 Analyze_Exception_Handlers (Exception_Handlers (New_S));
929 Expand_Exception_Handlers (New_S);
931 -- Exceptional_Complete_Rendezvous must be called with abort still
932 -- deferred, which is the case for a "when all others" handler.
934 return New_S;
935 end Build_Accept_Body;
937 -----------------------------------
938 -- Build_Activation_Chain_Entity --
939 -----------------------------------
941 procedure Build_Activation_Chain_Entity (N : Node_Id) is
942 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
943 -- Determine whether an extended return statement has activation chain
945 --------------------------
946 -- Has_Activation_Chain --
947 --------------------------
949 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
950 Decl : Node_Id;
952 begin
953 Decl := First (Return_Object_Declarations (Stmt));
954 while Present (Decl) loop
955 if Nkind (Decl) = N_Object_Declaration
956 and then Chars (Defining_Identifier (Decl)) = Name_uChain
957 then
958 return True;
959 end if;
961 Next (Decl);
962 end loop;
964 return False;
965 end Has_Activation_Chain;
967 -- Local variables
969 Context : Node_Id;
970 Context_Id : Entity_Id;
971 Decls : List_Id;
973 -- Start of processing for Build_Activation_Chain_Entity
975 begin
976 -- No action needed if the run-time has no tasking support
978 if Global_No_Tasking then
979 return;
980 end if;
982 -- Activation chain is never used for sequential elaboration policy, see
983 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
985 if Partition_Elaboration_Policy = 'S' then
986 return;
987 end if;
989 Find_Enclosing_Context (N, Context, Context_Id, Decls);
991 -- If activation chain entity has not been declared already, create one
993 if Nkind (Context) = N_Extended_Return_Statement
994 or else No (Activation_Chain_Entity (Context))
995 then
996 -- Since extended return statements do not store the entity of the
997 -- chain, examine the return object declarations to avoid creating
998 -- a duplicate.
1000 if Nkind (Context) = N_Extended_Return_Statement
1001 and then Has_Activation_Chain (Context)
1002 then
1003 return;
1004 end if;
1006 declare
1007 Loc : constant Source_Ptr := Sloc (Context);
1008 Chain : Entity_Id;
1009 Decl : Node_Id;
1011 begin
1012 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
1014 -- Note: An extended return statement is not really a task
1015 -- activator, but it does have an activation chain on which to
1016 -- store the tasks temporarily. On successful return, the tasks
1017 -- on this chain are moved to the chain passed in by the caller.
1018 -- We do not build an Activation_Chain_Entity for an extended
1019 -- return statement, because we do not want to build a call to
1020 -- Activate_Tasks. Task activation is the responsibility of the
1021 -- caller.
1023 if Nkind (Context) /= N_Extended_Return_Statement then
1024 Set_Activation_Chain_Entity (Context, Chain);
1025 end if;
1027 Decl :=
1028 Make_Object_Declaration (Loc,
1029 Defining_Identifier => Chain,
1030 Aliased_Present => True,
1031 Object_Definition =>
1032 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
1034 Prepend_To (Decls, Decl);
1036 -- Ensure that _chain appears in the proper scope of the context
1038 if Context_Id /= Current_Scope then
1039 Push_Scope (Context_Id);
1040 Analyze (Decl);
1041 Pop_Scope;
1042 else
1043 Analyze (Decl);
1044 end if;
1045 end;
1046 end if;
1047 end Build_Activation_Chain_Entity;
1049 ----------------------------
1050 -- Build_Barrier_Function --
1051 ----------------------------
1053 function Build_Barrier_Function
1054 (N : Node_Id;
1055 Ent : Entity_Id;
1056 Pid : Entity_Id) return Node_Id
1058 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
1059 Cond : constant Node_Id := Condition (Ent_Formals);
1060 Loc : constant Source_Ptr := Sloc (Cond);
1061 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1062 Op_Decls : constant List_Id := New_List;
1063 Stmt : Node_Id;
1064 Func_Body : Node_Id;
1066 begin
1067 -- Add a declaration for the Protection object, renaming declarations
1068 -- for the discriminals and privals and finally a declaration for the
1069 -- entry family index (if applicable).
1071 Install_Private_Data_Declarations (Sloc (N),
1072 Spec_Id => Func_Id,
1073 Conc_Typ => Pid,
1074 Body_Nod => N,
1075 Decls => Op_Decls,
1076 Barrier => True,
1077 Family => Ekind (Ent) = E_Entry_Family);
1079 -- If compiling with -fpreserve-control-flow, make sure we insert an
1080 -- IF statement so that the back-end knows to generate a conditional
1081 -- branch instruction, even if the condition is just the name of a
1082 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1083 -- such redundant IF statements under -fpreserve-control-flow
1084 -- (whether coming from this routine, or directly from source).
1086 if Opt.Suppress_Control_Flow_Optimizations then
1087 Stmt :=
1088 Make_Implicit_If_Statement (Cond,
1089 Condition => Cond,
1090 Then_Statements => New_List (
1091 Make_Simple_Return_Statement (Loc,
1092 New_Occurrence_Of (Standard_True, Loc))),
1094 Else_Statements => New_List (
1095 Make_Simple_Return_Statement (Loc,
1096 New_Occurrence_Of (Standard_False, Loc))));
1098 else
1099 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1100 end if;
1102 -- Note: the condition in the barrier function needs to be properly
1103 -- processed for the C/Fortran boolean possibility, but this happens
1104 -- automatically since the return statement does this normalization.
1106 Func_Body :=
1107 Make_Subprogram_Body (Loc,
1108 Specification =>
1109 Build_Barrier_Function_Specification (Loc,
1110 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1111 Declarations => Op_Decls,
1112 Handled_Statement_Sequence =>
1113 Make_Handled_Sequence_Of_Statements (Loc,
1114 Statements => New_List (Stmt)));
1115 Set_Is_Entry_Barrier_Function (Func_Body);
1117 return Func_Body;
1118 end Build_Barrier_Function;
1120 ------------------------------------------
1121 -- Build_Barrier_Function_Specification --
1122 ------------------------------------------
1124 function Build_Barrier_Function_Specification
1125 (Loc : Source_Ptr;
1126 Def_Id : Entity_Id) return Node_Id
1128 begin
1129 Set_Debug_Info_Needed (Def_Id);
1131 return
1132 Make_Function_Specification (Loc,
1133 Defining_Unit_Name => Def_Id,
1134 Parameter_Specifications => New_List (
1135 Make_Parameter_Specification (Loc,
1136 Defining_Identifier =>
1137 Make_Defining_Identifier (Loc, Name_uO),
1138 Parameter_Type =>
1139 New_Occurrence_Of (RTE (RE_Address), Loc)),
1141 Make_Parameter_Specification (Loc,
1142 Defining_Identifier =>
1143 Make_Defining_Identifier (Loc, Name_uE),
1144 Parameter_Type =>
1145 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1147 Result_Definition =>
1148 New_Occurrence_Of (Standard_Boolean, Loc));
1149 end Build_Barrier_Function_Specification;
1151 --------------------------
1152 -- Build_Call_With_Task --
1153 --------------------------
1155 function Build_Call_With_Task
1156 (N : Node_Id;
1157 E : Entity_Id) return Node_Id
1159 Loc : constant Source_Ptr := Sloc (N);
1160 begin
1161 return
1162 Make_Function_Call (Loc,
1163 Name => New_Occurrence_Of (E, Loc),
1164 Parameter_Associations => New_List (Concurrent_Ref (N)));
1165 end Build_Call_With_Task;
1167 -----------------------------
1168 -- Build_Class_Wide_Master --
1169 -----------------------------
1171 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1172 Loc : constant Source_Ptr := Sloc (Typ);
1173 Master_Decl : Node_Id;
1174 Master_Id : Entity_Id;
1175 Master_Scope : Entity_Id;
1176 Name_Id : Node_Id;
1177 Related_Node : Node_Id;
1178 Ren_Decl : Node_Id;
1180 begin
1181 -- No action needed if the run-time has no tasking support
1183 if Global_No_Tasking then
1184 return;
1185 end if;
1187 -- Find the declaration that created the access type, which is either a
1188 -- type declaration, or an object declaration with an access definition,
1189 -- in which case the type is anonymous.
1191 if Is_Itype (Typ) then
1192 Related_Node := Associated_Node_For_Itype (Typ);
1193 else
1194 Related_Node := Parent (Typ);
1195 end if;
1197 Master_Scope := Find_Master_Scope (Typ);
1199 -- Nothing to do if the master scope already contains a _master entity.
1200 -- The only exception to this is the following scenario:
1202 -- Source_Scope
1203 -- Transient_Scope_1
1204 -- _master
1206 -- Transient_Scope_2
1207 -- use of master
1209 -- In this case the source scope is marked as having the master entity
1210 -- even though the actual declaration appears inside an inner scope. If
1211 -- the second transient scope requires a _master, it cannot use the one
1212 -- already declared because the entity is not visible.
1214 Name_Id := Make_Identifier (Loc, Name_uMaster);
1215 Master_Decl := Empty;
1217 if not Has_Master_Entity (Master_Scope)
1218 or else No (Current_Entity_In_Scope (Name_Id))
1219 then
1220 declare
1221 Ins_Nod : Node_Id;
1223 begin
1224 Set_Has_Master_Entity (Master_Scope);
1225 Master_Decl := Build_Master_Declaration (Loc);
1227 -- Ensure that the master declaration is placed before its use
1229 Ins_Nod := Find_Hook_Context (Related_Node);
1230 while not Is_List_Member (Ins_Nod) loop
1231 Ins_Nod := Parent (Ins_Nod);
1232 end loop;
1234 Insert_Before (First (List_Containing (Ins_Nod)), Master_Decl);
1235 Analyze (Master_Decl);
1237 -- Mark the containing scope as a task master. Masters associated
1238 -- with return statements are already marked at this stage (see
1239 -- Analyze_Subprogram_Body).
1241 if Ekind (Current_Scope) /= E_Return_Statement then
1242 declare
1243 Par : Node_Id := Related_Node;
1245 begin
1246 while Nkind (Par) /= N_Compilation_Unit loop
1247 Par := Parent (Par);
1249 -- If we fall off the top, we are at the outer level,
1250 -- and the environment task is our effective master,
1251 -- so nothing to mark.
1253 if Nkind (Par) in
1254 N_Block_Statement | N_Subprogram_Body | N_Task_Body
1255 then
1256 Set_Is_Task_Master (Par);
1257 exit;
1258 end if;
1259 end loop;
1260 end;
1261 end if;
1262 end;
1263 end if;
1265 Master_Id :=
1266 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1268 -- Generate:
1269 -- typeMnn renames _master;
1271 Ren_Decl :=
1272 Make_Object_Renaming_Declaration (Loc,
1273 Defining_Identifier => Master_Id,
1274 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1275 Name => Name_Id);
1277 -- If the master is declared locally, add the renaming declaration
1278 -- immediately after it, to prevent access-before-elaboration in the
1279 -- back-end.
1281 if Present (Master_Decl) then
1282 Insert_After (Master_Decl, Ren_Decl);
1283 Analyze (Ren_Decl);
1285 else
1286 Insert_Action (Related_Node, Ren_Decl);
1287 end if;
1289 Set_Master_Id (Typ, Master_Id);
1290 end Build_Class_Wide_Master;
1292 --------------------------------
1293 -- Build_Corresponding_Record --
1294 --------------------------------
1296 function Build_Corresponding_Record
1297 (N : Node_Id;
1298 Ctyp : Entity_Id;
1299 Loc : Source_Ptr) return Node_Id
1301 Rec_Ent : constant Entity_Id :=
1302 Make_Defining_Identifier
1303 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1304 Disc : Entity_Id;
1305 Dlist : List_Id;
1306 New_Disc : Entity_Id;
1307 Cdecls : List_Id;
1309 begin
1310 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1311 Mutate_Ekind (Rec_Ent, E_Record_Type);
1312 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1313 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1314 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1315 Set_Stored_Constraint (Rec_Ent, No_Elist);
1316 Cdecls := New_List;
1318 -- Use discriminals to create list of discriminants for record, and
1319 -- create new discriminals for use in default expressions, etc. It is
1320 -- worth noting that a task discriminant gives rise to 5 entities;
1322 -- a) The original discriminant.
1323 -- b) The discriminal for use in the task.
1324 -- c) The discriminant of the corresponding record.
1325 -- d) The discriminal for the init proc of the corresponding record.
1326 -- e) The local variable that renames the discriminant in the procedure
1327 -- for the task body.
1329 -- In fact the discriminals b) are used in the renaming declarations
1330 -- for e). See details in einfo (Handling of Discriminants).
1332 if Present (Discriminant_Specifications (N)) then
1333 Dlist := New_List;
1334 Disc := First_Discriminant (Ctyp);
1336 while Present (Disc) loop
1337 New_Disc := CR_Discriminant (Disc);
1339 Append_To (Dlist,
1340 Make_Discriminant_Specification (Loc,
1341 Defining_Identifier => New_Disc,
1342 Discriminant_Type =>
1343 New_Occurrence_Of (Etype (Disc), Loc),
1344 Expression =>
1345 New_Copy (Discriminant_Default_Value (Disc))));
1347 Next_Discriminant (Disc);
1348 end loop;
1350 else
1351 Dlist := No_List;
1352 end if;
1354 -- Now we can construct the record type declaration. Note that this
1355 -- record is "limited tagged". It is "limited" to reflect the underlying
1356 -- limitedness of the task or protected object that it represents, and
1357 -- ensuring for example that it is properly passed by reference. It is
1358 -- "tagged" to give support to dispatching calls through interfaces. We
1359 -- propagate here the list of interfaces covered by the concurrent type
1360 -- (Ada 2005: AI-345).
1362 return
1363 Make_Full_Type_Declaration (Loc,
1364 Defining_Identifier => Rec_Ent,
1365 Discriminant_Specifications => Dlist,
1366 Type_Definition =>
1367 Make_Record_Definition (Loc,
1368 Component_List =>
1369 Make_Component_List (Loc, Component_Items => Cdecls),
1370 Tagged_Present =>
1371 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1372 Interface_List => Interface_List (N),
1373 Limited_Present => True));
1374 end Build_Corresponding_Record;
1376 ---------------------------------
1377 -- Build_Dispatching_Tag_Check --
1378 ---------------------------------
1380 function Build_Dispatching_Tag_Check
1381 (K : Entity_Id;
1382 N : Node_Id) return Node_Id
1384 Loc : constant Source_Ptr := Sloc (N);
1386 begin
1387 return
1388 Make_Op_Or (Loc,
1389 Make_Op_Eq (Loc,
1390 Left_Opnd =>
1391 New_Occurrence_Of (K, Loc),
1392 Right_Opnd =>
1393 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1395 Make_Op_Eq (Loc,
1396 Left_Opnd =>
1397 New_Occurrence_Of (K, Loc),
1398 Right_Opnd =>
1399 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1400 end Build_Dispatching_Tag_Check;
1402 ----------------------------------
1403 -- Build_Entry_Count_Expression --
1404 ----------------------------------
1406 function Build_Entry_Count_Expression
1407 (Concurrent_Type : Node_Id;
1408 Component_List : List_Id;
1409 Loc : Source_Ptr) return Node_Id
1411 Eindx : Nat;
1412 Ent : Entity_Id;
1413 Ecount : Node_Id;
1414 Comp : Node_Id;
1415 Lo : Node_Id;
1416 Hi : Node_Id;
1417 Typ : Entity_Id;
1418 Large : Boolean;
1420 begin
1421 -- Count number of non-family entries
1423 Eindx := 0;
1424 Ent := First_Entity (Concurrent_Type);
1425 while Present (Ent) loop
1426 if Ekind (Ent) = E_Entry then
1427 Eindx := Eindx + 1;
1428 end if;
1430 Next_Entity (Ent);
1431 end loop;
1433 Ecount := Make_Integer_Literal (Loc, Eindx);
1435 -- Loop through entry families building the addition nodes
1437 Ent := First_Entity (Concurrent_Type);
1438 Comp := First (Component_List);
1439 while Present (Ent) loop
1440 if Ekind (Ent) = E_Entry_Family then
1441 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1442 Next (Comp);
1443 end loop;
1445 Typ := Entry_Index_Type (Ent);
1446 Hi := Type_High_Bound (Typ);
1447 Lo := Type_Low_Bound (Typ);
1448 Large := Is_Potentially_Large_Family
1449 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1450 Ecount :=
1451 Make_Op_Add (Loc,
1452 Left_Opnd => Ecount,
1453 Right_Opnd =>
1454 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1455 end if;
1457 Next_Entity (Ent);
1458 end loop;
1460 return Ecount;
1461 end Build_Entry_Count_Expression;
1463 ------------------------------
1464 -- Build_Master_Declaration --
1465 ------------------------------
1467 function Build_Master_Declaration (Loc : Source_Ptr) return Node_Id is
1468 Master_Decl : Node_Id;
1470 begin
1471 -- Generate a dummy master if tasks or tasking hierarchies are
1472 -- prohibited.
1474 -- _Master : constant Integer := Library_Task_Level;
1476 if not Tasking_Allowed
1477 or else Restrictions.Set (No_Task_Hierarchy)
1478 or else not RTE_Available (RE_Current_Master)
1479 then
1480 Master_Decl :=
1481 Make_Object_Declaration (Loc,
1482 Defining_Identifier =>
1483 Make_Defining_Identifier (Loc, Name_uMaster),
1484 Constant_Present => True,
1485 Object_Definition =>
1486 New_Occurrence_Of (Standard_Integer, Loc),
1487 Expression =>
1488 Make_Integer_Literal (Loc, Library_Task_Level));
1490 -- Generate:
1491 -- _master : constant Integer := Current_Master.all;
1493 else
1494 Master_Decl :=
1495 Make_Object_Declaration (Loc,
1496 Defining_Identifier =>
1497 Make_Defining_Identifier (Loc, Name_uMaster),
1498 Constant_Present => True,
1499 Object_Definition =>
1500 New_Occurrence_Of (Standard_Integer, Loc),
1501 Expression =>
1502 Make_Explicit_Dereference (Loc,
1503 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1504 end if;
1506 return Master_Decl;
1507 end Build_Master_Declaration;
1509 ---------------------------
1510 -- Build_Parameter_Block --
1511 ---------------------------
1513 function Build_Parameter_Block
1514 (Loc : Source_Ptr;
1515 Actuals : List_Id;
1516 Formals : List_Id;
1517 Decls : List_Id) return Entity_Id
1519 Actual : Entity_Id;
1520 Comp_Nam : Node_Id;
1521 Comps : List_Id;
1522 Formal : Entity_Id;
1523 Has_Comp : Boolean := False;
1524 Rec_Nam : Node_Id;
1526 begin
1527 Actual := First (Actuals);
1528 Comps := New_List;
1529 Formal := Defining_Identifier (First (Formals));
1531 while Present (Actual) loop
1532 if not Is_Controlling_Actual (Actual) then
1534 -- Generate:
1535 -- type Ann is access all <actual-type>
1537 Comp_Nam := Make_Temporary (Loc, 'A');
1538 Set_Is_Param_Block_Component_Type (Comp_Nam);
1540 Append_To (Decls,
1541 Make_Full_Type_Declaration (Loc,
1542 Defining_Identifier => Comp_Nam,
1543 Type_Definition =>
1544 Make_Access_To_Object_Definition (Loc,
1545 All_Present => True,
1546 Constant_Present => Ekind (Formal) = E_In_Parameter,
1547 Subtype_Indication =>
1548 New_Occurrence_Of (Etype (Actual), Loc))));
1550 -- Generate:
1551 -- Param : Ann;
1553 Append_To (Comps,
1554 Make_Component_Declaration (Loc,
1555 Defining_Identifier =>
1556 Make_Defining_Identifier (Loc, Chars (Formal)),
1557 Component_Definition =>
1558 Make_Component_Definition (Loc,
1559 Aliased_Present =>
1560 False,
1561 Subtype_Indication =>
1562 New_Occurrence_Of (Comp_Nam, Loc))));
1564 Has_Comp := True;
1565 end if;
1567 Next_Actual (Actual);
1568 Next_Formal_With_Extras (Formal);
1569 end loop;
1571 Rec_Nam := Make_Temporary (Loc, 'P');
1573 if Has_Comp then
1575 -- Generate:
1576 -- type Pnn is record
1577 -- Param1 : Ann1;
1578 -- ...
1579 -- ParamN : AnnN;
1581 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1582 -- the original parameter names and Ann1 .. AnnN are the access to
1583 -- actual types.
1585 Append_To (Decls,
1586 Make_Full_Type_Declaration (Loc,
1587 Defining_Identifier =>
1588 Rec_Nam,
1589 Type_Definition =>
1590 Make_Record_Definition (Loc,
1591 Component_List =>
1592 Make_Component_List (Loc, Comps))));
1593 else
1594 -- Generate:
1595 -- type Pnn is null record;
1597 Append_To (Decls,
1598 Make_Full_Type_Declaration (Loc,
1599 Defining_Identifier =>
1600 Rec_Nam,
1601 Type_Definition =>
1602 Make_Record_Definition (Loc,
1603 Null_Present => True,
1604 Component_List => Empty)));
1605 end if;
1607 return Rec_Nam;
1608 end Build_Parameter_Block;
1610 --------------------------------------
1611 -- Build_Renamed_Formal_Declaration --
1612 --------------------------------------
1614 function Build_Renamed_Formal_Declaration
1615 (New_F : Entity_Id;
1616 Formal : Entity_Id;
1617 Comp : Entity_Id;
1618 Renamed_Formal : Node_Id) return Node_Id
1620 Loc : constant Source_Ptr := Sloc (New_F);
1621 Decl : Node_Id;
1623 begin
1624 -- If the formal is a tagged incomplete type, it is already passed
1625 -- by reference, so it is sufficient to rename the pointer component
1626 -- that corresponds to the actual. Otherwise we need to dereference
1627 -- the pointer component to obtain the actual.
1629 if Is_Incomplete_Type (Etype (Formal))
1630 and then Is_Tagged_Type (Etype (Formal))
1631 then
1632 Decl :=
1633 Make_Object_Renaming_Declaration (Loc,
1634 Defining_Identifier => New_F,
1635 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1636 Name => Renamed_Formal);
1638 else
1639 Decl :=
1640 Make_Object_Renaming_Declaration (Loc,
1641 Defining_Identifier => New_F,
1642 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1643 Name =>
1644 Make_Explicit_Dereference (Loc, Renamed_Formal));
1645 end if;
1647 return Decl;
1648 end Build_Renamed_Formal_Declaration;
1650 --------------------------
1651 -- Build_Wrapper_Bodies --
1652 --------------------------
1654 procedure Build_Wrapper_Bodies
1655 (Loc : Source_Ptr;
1656 Typ : Entity_Id;
1657 N : Node_Id)
1659 Rec_Typ : Entity_Id;
1661 function Build_Wrapper_Body
1662 (Loc : Source_Ptr;
1663 Subp_Id : Entity_Id;
1664 Obj_Typ : Entity_Id;
1665 Formals : List_Id) return Node_Id;
1666 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1667 -- associated with a protected or task type. Subp_Id is the subprogram
1668 -- name which will be wrapped. Obj_Typ is the type of the new formal
1669 -- parameter which handles dispatching and object notation. Formals are
1670 -- the original formals of Subp_Id which will be explicitly replicated.
1672 ------------------------
1673 -- Build_Wrapper_Body --
1674 ------------------------
1676 function Build_Wrapper_Body
1677 (Loc : Source_Ptr;
1678 Subp_Id : Entity_Id;
1679 Obj_Typ : Entity_Id;
1680 Formals : List_Id) return Node_Id
1682 Body_Spec : Node_Id;
1684 begin
1685 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1687 -- The subprogram is not overriding or is not a primitive declared
1688 -- between two views.
1690 if No (Body_Spec) then
1691 return Empty;
1692 end if;
1694 declare
1695 Actuals : List_Id := No_List;
1696 Conv_Id : Node_Id;
1697 First_Form : Node_Id;
1698 Formal : Node_Id;
1699 Nam : Node_Id;
1701 begin
1702 -- Map formals to actuals. Use the list built for the wrapper
1703 -- spec, skipping the object notation parameter.
1705 First_Form := First (Parameter_Specifications (Body_Spec));
1707 Formal := First_Form;
1708 Next (Formal);
1710 if Present (Formal) then
1711 Actuals := New_List;
1712 while Present (Formal) loop
1713 Append_To (Actuals,
1714 Make_Identifier (Loc,
1715 Chars => Chars (Defining_Identifier (Formal))));
1716 Next (Formal);
1717 end loop;
1718 end if;
1720 -- Special processing for primitives declared between a private
1721 -- type and its completion: the wrapper needs a properly typed
1722 -- parameter if the wrapped operation has a controlling first
1723 -- parameter. Note that this might not be the case for a function
1724 -- with a controlling result.
1726 if Is_Private_Primitive_Subprogram (Subp_Id) then
1727 if No (Actuals) then
1728 Actuals := New_List;
1729 end if;
1731 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1732 Prepend_To (Actuals,
1733 Unchecked_Convert_To
1734 (Corresponding_Concurrent_Type (Obj_Typ),
1735 Make_Identifier (Loc, Name_uO)));
1737 else
1738 Prepend_To (Actuals,
1739 Make_Identifier (Loc,
1740 Chars => Chars (Defining_Identifier (First_Form))));
1741 end if;
1743 Nam := New_Occurrence_Of (Subp_Id, Loc);
1744 else
1745 -- An access-to-variable object parameter requires an explicit
1746 -- dereference in the unchecked conversion. This case occurs
1747 -- when a protected entry wrapper must override an interface
1748 -- level procedure with interface access as first parameter.
1750 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1752 if Nkind (Parameter_Type (First_Form)) =
1753 N_Access_Definition
1754 then
1755 Conv_Id :=
1756 Make_Explicit_Dereference (Loc,
1757 Prefix => Make_Identifier (Loc, Name_uO));
1758 else
1759 Conv_Id := Make_Identifier (Loc, Name_uO);
1760 end if;
1762 Nam :=
1763 Make_Selected_Component (Loc,
1764 Prefix =>
1765 Unchecked_Convert_To
1766 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1767 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
1768 end if;
1770 -- Create the subprogram body. For a function, the call to the
1771 -- actual subprogram has to be converted to the corresponding
1772 -- record if it is a controlling result.
1774 if Ekind (Subp_Id) = E_Function then
1775 declare
1776 Res : Node_Id;
1778 begin
1779 Res :=
1780 Make_Function_Call (Loc,
1781 Name => Nam,
1782 Parameter_Associations => Actuals);
1784 if Has_Controlling_Result (Subp_Id) then
1785 Res :=
1786 Unchecked_Convert_To
1787 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1788 end if;
1790 return
1791 Make_Subprogram_Body (Loc,
1792 Specification => Body_Spec,
1793 Declarations => Empty_List,
1794 Handled_Statement_Sequence =>
1795 Make_Handled_Sequence_Of_Statements (Loc,
1796 Statements => New_List (
1797 Make_Simple_Return_Statement (Loc, Res))));
1798 end;
1800 else
1801 return
1802 Make_Subprogram_Body (Loc,
1803 Specification => Body_Spec,
1804 Declarations => Empty_List,
1805 Handled_Statement_Sequence =>
1806 Make_Handled_Sequence_Of_Statements (Loc,
1807 Statements => New_List (
1808 Make_Procedure_Call_Statement (Loc,
1809 Name => Nam,
1810 Parameter_Associations => Actuals))));
1811 end if;
1812 end;
1813 end Build_Wrapper_Body;
1815 -- Start of processing for Build_Wrapper_Bodies
1817 begin
1818 if Is_Concurrent_Type (Typ) then
1819 Rec_Typ := Corresponding_Record_Type (Typ);
1820 else
1821 Rec_Typ := Typ;
1822 end if;
1824 -- Generate wrapper bodies for a concurrent type which implements an
1825 -- interface.
1827 if Present (Interfaces (Rec_Typ)) then
1828 declare
1829 Insert_Nod : Node_Id;
1830 Prim : Entity_Id;
1831 Prim_Elmt : Elmt_Id;
1832 Prim_Decl : Node_Id;
1833 Subp : Entity_Id;
1834 Wrap_Body : Node_Id;
1835 Wrap_Id : Entity_Id;
1837 begin
1838 Insert_Nod := N;
1840 -- Examine all primitive operations of the corresponding record
1841 -- type, looking for wrapper specs. Generate bodies in order to
1842 -- complete them.
1844 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
1845 while Present (Prim_Elmt) loop
1846 Prim := Node (Prim_Elmt);
1848 if (Ekind (Prim) = E_Function
1849 or else Ekind (Prim) = E_Procedure)
1850 and then Is_Primitive_Wrapper (Prim)
1851 then
1852 Subp := Wrapped_Entity (Prim);
1853 Prim_Decl := Parent (Parent (Prim));
1855 Wrap_Body :=
1856 Build_Wrapper_Body (Loc,
1857 Subp_Id => Subp,
1858 Obj_Typ => Rec_Typ,
1859 Formals => Parameter_Specifications (Parent (Subp)));
1860 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
1862 Set_Corresponding_Spec (Wrap_Body, Prim);
1863 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
1865 Insert_After (Insert_Nod, Wrap_Body);
1866 Insert_Nod := Wrap_Body;
1868 Analyze (Wrap_Body);
1869 end if;
1871 Next_Elmt (Prim_Elmt);
1872 end loop;
1873 end;
1874 end if;
1875 end Build_Wrapper_Bodies;
1877 ------------------------
1878 -- Build_Wrapper_Spec --
1879 ------------------------
1881 function Build_Wrapper_Spec
1882 (Subp_Id : Entity_Id;
1883 Obj_Typ : Entity_Id;
1884 Formals : List_Id) return Node_Id
1886 function Overriding_Possible
1887 (Iface_Op : Entity_Id;
1888 Wrapper : Entity_Id) return Boolean;
1889 -- Determine whether a primitive operation can be overridden by Wrapper.
1890 -- Iface_Op is the candidate primitive operation of an interface type,
1891 -- Wrapper is the generated entry wrapper.
1893 function Replicate_Formals
1894 (Loc : Source_Ptr;
1895 Formals : List_Id) return List_Id;
1896 -- An explicit parameter replication is required due to the Is_Entry_
1897 -- Formal flag being set for all the formals of an entry. The explicit
1898 -- replication removes the flag that would otherwise cause a different
1899 -- path of analysis.
1901 -------------------------
1902 -- Overriding_Possible --
1903 -------------------------
1905 function Overriding_Possible
1906 (Iface_Op : Entity_Id;
1907 Wrapper : Entity_Id) return Boolean
1909 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
1910 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
1912 function Type_Conformant_Parameters
1913 (Iface_Op_Params : List_Id;
1914 Wrapper_Params : List_Id) return Boolean;
1915 -- Determine whether the parameters of the generated entry wrapper
1916 -- and those of a primitive operation are type conformant. During
1917 -- this check, the first parameter of the primitive operation is
1918 -- skipped if it is a controlling argument: protected functions
1919 -- may have a controlling result.
1921 --------------------------------
1922 -- Type_Conformant_Parameters --
1923 --------------------------------
1925 function Type_Conformant_Parameters
1926 (Iface_Op_Params : List_Id;
1927 Wrapper_Params : List_Id) return Boolean
1929 Iface_Op_Param : Node_Id;
1930 Iface_Op_Typ : Entity_Id;
1931 Wrapper_Param : Node_Id;
1932 Wrapper_Typ : Entity_Id;
1934 begin
1935 -- Skip the first (controlling) parameter of primitive operation
1937 Iface_Op_Param := First (Iface_Op_Params);
1939 if Present (First_Formal (Iface_Op))
1940 and then Is_Controlling_Formal (First_Formal (Iface_Op))
1941 then
1942 Next (Iface_Op_Param);
1943 end if;
1945 Wrapper_Param := First (Wrapper_Params);
1946 while Present (Iface_Op_Param)
1947 and then Present (Wrapper_Param)
1948 loop
1949 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
1950 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
1952 -- The two parameters must be mode conformant
1954 if not Conforming_Types
1955 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
1956 then
1957 return False;
1958 end if;
1960 Next (Iface_Op_Param);
1961 Next (Wrapper_Param);
1962 end loop;
1964 -- One of the lists is longer than the other
1966 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
1967 return False;
1968 end if;
1970 return True;
1971 end Type_Conformant_Parameters;
1973 -- Start of processing for Overriding_Possible
1975 begin
1976 if Chars (Iface_Op) /= Chars (Wrapper) then
1977 return False;
1978 end if;
1980 -- If an inherited subprogram is implemented by a protected procedure
1981 -- or an entry, then the first parameter of the inherited subprogram
1982 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
1984 if Ekind (Iface_Op) = E_Procedure
1985 and then Present (Parameter_Specifications (Iface_Op_Spec))
1986 then
1987 declare
1988 Obj_Param : constant Node_Id :=
1989 First (Parameter_Specifications (Iface_Op_Spec));
1990 begin
1991 if not Out_Present (Obj_Param)
1992 and then Nkind (Parameter_Type (Obj_Param)) /=
1993 N_Access_Definition
1994 then
1995 return False;
1996 end if;
1997 end;
1998 end if;
2000 return
2001 Type_Conformant_Parameters
2002 (Parameter_Specifications (Iface_Op_Spec),
2003 Parameter_Specifications (Wrapper_Spec));
2004 end Overriding_Possible;
2006 -----------------------
2007 -- Replicate_Formals --
2008 -----------------------
2010 function Replicate_Formals
2011 (Loc : Source_Ptr;
2012 Formals : List_Id) return List_Id
2014 New_Formals : constant List_Id := New_List;
2015 Formal : Node_Id;
2016 Param_Type : Node_Id;
2018 begin
2019 Formal := First (Formals);
2021 -- Skip the object parameter when dealing with primitives declared
2022 -- between two views.
2024 if Is_Private_Primitive_Subprogram (Subp_Id)
2025 and then not Has_Controlling_Result (Subp_Id)
2026 then
2027 Next (Formal);
2028 end if;
2030 while Present (Formal) loop
2032 -- Create an explicit copy of the entry parameter
2034 -- When creating the wrapper subprogram for a primitive operation
2035 -- of a protected interface we must construct an equivalent
2036 -- signature to that of the overriding operation. For regular
2037 -- parameters we can just use the type of the formal, but for
2038 -- access to subprogram parameters we need to reanalyze the
2039 -- parameter type to create local entities for the signature of
2040 -- the subprogram type. Using the entities of the overriding
2041 -- subprogram will result in out-of-scope errors in the back-end.
2043 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2044 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2045 else
2046 Param_Type :=
2047 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2048 end if;
2050 Append_To (New_Formals,
2051 Make_Parameter_Specification (Loc,
2052 Defining_Identifier =>
2053 Make_Defining_Identifier (Loc,
2054 Chars => Chars (Defining_Identifier (Formal))),
2055 In_Present => In_Present (Formal),
2056 Out_Present => Out_Present (Formal),
2057 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2058 Parameter_Type => Param_Type));
2060 Next (Formal);
2061 end loop;
2063 return New_Formals;
2064 end Replicate_Formals;
2066 -- Local variables
2068 Loc : constant Source_Ptr := Sloc (Subp_Id);
2069 First_Param : Node_Id := Empty;
2070 Iface : Entity_Id;
2071 Iface_Elmt : Elmt_Id;
2072 Iface_Op : Entity_Id;
2073 Iface_Op_Elmt : Elmt_Id;
2074 Overridden_Subp : Entity_Id;
2076 -- Start of processing for Build_Wrapper_Spec
2078 begin
2079 -- No point in building wrappers for untagged concurrent types
2081 pragma Assert (Is_Tagged_Type (Obj_Typ));
2083 -- Check if this subprogram has a profile that matches some interface
2084 -- primitive.
2086 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2088 if Present (Overridden_Subp) then
2089 First_Param :=
2090 First (Parameter_Specifications (Parent (Overridden_Subp)));
2092 -- An entry or a protected procedure can override a routine where the
2093 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2094 -- type. Since the wrapper must have the exact same signature as that of
2095 -- the overridden subprogram, we try to find the overriding candidate
2096 -- and use its controlling formal.
2098 -- Check every implemented interface
2100 elsif Present (Interfaces (Obj_Typ)) then
2101 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2102 Search : while Present (Iface_Elmt) loop
2103 Iface := Node (Iface_Elmt);
2105 -- Check every interface primitive
2107 if Present (Primitive_Operations (Iface)) then
2108 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2109 while Present (Iface_Op_Elmt) loop
2110 Iface_Op := Node (Iface_Op_Elmt);
2112 -- Ignore predefined primitives
2114 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2115 Iface_Op := Ultimate_Alias (Iface_Op);
2117 -- The current primitive operation can be overridden by
2118 -- the generated entry wrapper.
2120 if Overriding_Possible (Iface_Op, Subp_Id) then
2121 First_Param :=
2122 First (Parameter_Specifications (Parent (Iface_Op)));
2124 exit Search;
2125 end if;
2126 end if;
2128 Next_Elmt (Iface_Op_Elmt);
2129 end loop;
2130 end if;
2132 Next_Elmt (Iface_Elmt);
2133 end loop Search;
2134 end if;
2136 -- Do not generate the wrapper if no interface primitive is covered by
2137 -- the subprogram and it is not a primitive declared between two views
2138 -- (see Process_Full_View).
2140 if No (First_Param)
2141 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2142 then
2143 return Empty;
2144 end if;
2146 declare
2147 Wrapper_Id : constant Entity_Id :=
2148 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2149 New_Formals : List_Id;
2150 Obj_Param : Node_Id;
2151 Obj_Param_Typ : Entity_Id;
2153 begin
2154 -- Minimum decoration is needed to catch the entity in
2155 -- Sem_Ch6.Override_Dispatching_Operation.
2157 if Ekind (Subp_Id) = E_Function then
2158 Mutate_Ekind (Wrapper_Id, E_Function);
2159 else
2160 Mutate_Ekind (Wrapper_Id, E_Procedure);
2161 end if;
2163 Set_Is_Primitive_Wrapper (Wrapper_Id);
2164 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2165 Set_Is_Private_Primitive (Wrapper_Id,
2166 Is_Private_Primitive_Subprogram (Subp_Id));
2168 -- Process the formals
2170 New_Formals := Replicate_Formals (Loc, Formals);
2172 -- A function with a controlling result and no first controlling
2173 -- formal needs no additional parameter.
2175 if Has_Controlling_Result (Subp_Id)
2176 and then
2177 (No (First_Formal (Subp_Id))
2178 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2179 then
2180 null;
2182 -- Routine Subp_Id has been found to override an interface primitive.
2183 -- If the interface operation has an access parameter, create a copy
2184 -- of it, with the same null exclusion indicator if present.
2186 elsif Present (First_Param) then
2187 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2188 Obj_Param_Typ :=
2189 Make_Access_Definition (Loc,
2190 Subtype_Mark =>
2191 New_Occurrence_Of (Obj_Typ, Loc),
2192 Null_Exclusion_Present =>
2193 Null_Exclusion_Present (Parameter_Type (First_Param)),
2194 Constant_Present =>
2195 Constant_Present (Parameter_Type (First_Param)));
2196 else
2197 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2198 end if;
2200 Obj_Param :=
2201 Make_Parameter_Specification (Loc,
2202 Defining_Identifier =>
2203 Make_Defining_Identifier (Loc,
2204 Chars => Name_uO),
2205 In_Present => In_Present (First_Param),
2206 Out_Present => Out_Present (First_Param),
2207 Parameter_Type => Obj_Param_Typ);
2209 Prepend_To (New_Formals, Obj_Param);
2211 -- If we are dealing with a primitive declared between two views,
2212 -- implemented by a synchronized operation, we need to create
2213 -- a default parameter. The mode of the parameter must match that
2214 -- of the primitive operation.
2216 else
2217 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2219 Obj_Param :=
2220 Make_Parameter_Specification (Loc,
2221 Defining_Identifier =>
2222 Make_Defining_Identifier (Loc, Name_uO),
2223 In_Present =>
2224 In_Present (Parent (First_Entity (Subp_Id))),
2225 Out_Present => Ekind (Subp_Id) /= E_Function,
2226 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2228 Prepend_To (New_Formals, Obj_Param);
2229 end if;
2231 -- Build the final spec. If it is a function with a controlling
2232 -- result, it is a primitive operation of the corresponding
2233 -- record type, so mark the spec accordingly.
2235 if Ekind (Subp_Id) = E_Function then
2236 declare
2237 Res_Def : Node_Id;
2239 begin
2240 if Has_Controlling_Result (Subp_Id) then
2241 Res_Def :=
2242 New_Occurrence_Of
2243 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2244 else
2245 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2246 end if;
2248 return
2249 Make_Function_Specification (Loc,
2250 Defining_Unit_Name => Wrapper_Id,
2251 Parameter_Specifications => New_Formals,
2252 Result_Definition => Res_Def);
2253 end;
2254 else
2255 return
2256 Make_Procedure_Specification (Loc,
2257 Defining_Unit_Name => Wrapper_Id,
2258 Parameter_Specifications => New_Formals);
2259 end if;
2260 end;
2261 end Build_Wrapper_Spec;
2263 -------------------------
2264 -- Build_Wrapper_Specs --
2265 -------------------------
2267 procedure Build_Wrapper_Specs
2268 (Loc : Source_Ptr;
2269 Typ : Entity_Id;
2270 N : in out Node_Id)
2272 Def : Node_Id;
2273 Rec_Typ : Entity_Id;
2274 procedure Scan_Declarations (L : List_Id);
2275 -- Common processing for visible and private declarations
2276 -- of a protected type.
2278 procedure Scan_Declarations (L : List_Id) is
2279 Decl : Node_Id;
2280 Wrap_Decl : Node_Id;
2281 Wrap_Spec : Node_Id;
2283 begin
2284 if No (L) then
2285 return;
2286 end if;
2288 Decl := First (L);
2289 while Present (Decl) loop
2290 Wrap_Spec := Empty;
2292 if Nkind (Decl) = N_Entry_Declaration
2293 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2294 then
2295 Wrap_Spec :=
2296 Build_Wrapper_Spec
2297 (Subp_Id => Defining_Identifier (Decl),
2298 Obj_Typ => Rec_Typ,
2299 Formals => Parameter_Specifications (Decl));
2301 elsif Nkind (Decl) = N_Subprogram_Declaration then
2302 Wrap_Spec :=
2303 Build_Wrapper_Spec
2304 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2305 Obj_Typ => Rec_Typ,
2306 Formals =>
2307 Parameter_Specifications (Specification (Decl)));
2308 end if;
2310 if Present (Wrap_Spec) then
2311 Wrap_Decl :=
2312 Make_Subprogram_Declaration (Loc,
2313 Specification => Wrap_Spec);
2315 Insert_After (N, Wrap_Decl);
2316 N := Wrap_Decl;
2318 Analyze (Wrap_Decl);
2319 end if;
2321 Next (Decl);
2322 end loop;
2323 end Scan_Declarations;
2325 -- start of processing for Build_Wrapper_Specs
2327 begin
2328 if Is_Protected_Type (Typ) then
2329 Def := Protected_Definition (Parent (Typ));
2330 else pragma Assert (Is_Task_Type (Typ));
2331 Def := Task_Definition (Parent (Typ));
2332 end if;
2334 Rec_Typ := Corresponding_Record_Type (Typ);
2336 -- Generate wrapper specs for a concurrent type which implements an
2337 -- interface. Operations in both the visible and private parts may
2338 -- implement progenitor operations.
2340 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2341 Scan_Declarations (Visible_Declarations (Def));
2342 Scan_Declarations (Private_Declarations (Def));
2343 end if;
2344 end Build_Wrapper_Specs;
2346 ---------------------------
2347 -- Build_Find_Body_Index --
2348 ---------------------------
2350 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2351 Loc : constant Source_Ptr := Sloc (Typ);
2352 Ent : Entity_Id;
2353 E_Typ : Entity_Id;
2354 Has_F : Boolean := False;
2355 Index : Nat;
2356 If_St : Node_Id := Empty;
2357 Lo : Node_Id;
2358 Hi : Node_Id;
2359 Decls : List_Id := New_List;
2360 Ret : Node_Id := Empty;
2361 Spec : Node_Id;
2362 Siz : Node_Id := Empty;
2364 procedure Add_If_Clause (Expr : Node_Id);
2365 -- Add test for range of current entry
2367 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2368 -- If a bound of an entry is given by a discriminant, retrieve the
2369 -- actual value of the discriminant from the enclosing object.
2371 -------------------
2372 -- Add_If_Clause --
2373 -------------------
2375 procedure Add_If_Clause (Expr : Node_Id) is
2376 Cond : Node_Id;
2377 Stats : constant List_Id :=
2378 New_List (
2379 Make_Simple_Return_Statement (Loc,
2380 Expression => Make_Integer_Literal (Loc, Index + 1)));
2382 begin
2383 -- Index for current entry body
2385 Index := Index + 1;
2387 -- Compute total length of entry queues so far
2389 if No (Siz) then
2390 Siz := Expr;
2391 else
2392 Siz :=
2393 Make_Op_Add (Loc,
2394 Left_Opnd => Siz,
2395 Right_Opnd => Expr);
2396 end if;
2398 Cond :=
2399 Make_Op_Le (Loc,
2400 Left_Opnd => Make_Identifier (Loc, Name_uE),
2401 Right_Opnd => Siz);
2403 -- Map entry queue indexes in the range of the current family
2404 -- into the current index, that designates the entry body.
2406 if No (If_St) then
2407 If_St :=
2408 Make_Implicit_If_Statement (Typ,
2409 Condition => Cond,
2410 Then_Statements => Stats,
2411 Elsif_Parts => New_List);
2412 Ret := If_St;
2414 else
2415 Append_To (Elsif_Parts (If_St),
2416 Make_Elsif_Part (Loc,
2417 Condition => Cond,
2418 Then_Statements => Stats));
2419 end if;
2420 end Add_If_Clause;
2422 ------------------------------
2423 -- Convert_Discriminant_Ref --
2424 ------------------------------
2426 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2427 B : Node_Id;
2429 begin
2430 if Is_Entity_Name (Bound)
2431 and then Ekind (Entity (Bound)) = E_Discriminant
2432 then
2433 B :=
2434 Make_Selected_Component (Loc,
2435 Prefix =>
2436 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2437 Make_Explicit_Dereference (Loc,
2438 Make_Identifier (Loc, Name_uObject))),
2439 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2440 Set_Etype (B, Etype (Entity (Bound)));
2441 else
2442 B := New_Copy_Tree (Bound);
2443 end if;
2445 return B;
2446 end Convert_Discriminant_Ref;
2448 -- Start of processing for Build_Find_Body_Index
2450 begin
2451 Spec := Build_Find_Body_Index_Spec (Typ);
2453 Ent := First_Entity (Typ);
2454 while Present (Ent) loop
2455 if Ekind (Ent) = E_Entry_Family then
2456 Has_F := True;
2457 exit;
2458 end if;
2460 Next_Entity (Ent);
2461 end loop;
2463 if not Has_F then
2465 -- If the protected type has no entry families, there is a one-one
2466 -- correspondence between entry queue and entry body.
2468 Ret :=
2469 Make_Simple_Return_Statement (Loc,
2470 Expression => Make_Identifier (Loc, Name_uE));
2472 else
2473 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2474 -- the following:
2476 -- if E <= l1 then return 1;
2477 -- elsif E <= l1 + l2 then return 2;
2478 -- ...
2480 Index := 0;
2481 Siz := Empty;
2482 Ent := First_Entity (Typ);
2484 Add_Object_Pointer (Loc, Typ, Decls);
2486 while Present (Ent) loop
2487 if Ekind (Ent) = E_Entry then
2488 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2490 elsif Ekind (Ent) = E_Entry_Family then
2491 E_Typ := Entry_Index_Type (Ent);
2492 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2493 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2494 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2495 end if;
2497 Next_Entity (Ent);
2498 end loop;
2500 if Index = 1 then
2501 Decls := New_List;
2502 Ret :=
2503 Make_Simple_Return_Statement (Loc,
2504 Expression => Make_Integer_Literal (Loc, 1));
2506 else
2507 -- Ranges are in increasing order, so last one doesn't need guard
2509 declare
2510 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2511 begin
2512 Remove (Nod);
2513 Set_Else_Statements (Ret, Then_Statements (Nod));
2515 -- If Elsif_Parts becomes empty then remove it entirely, as
2516 -- otherwise we would violate the invariant of If_Statement
2517 -- node described in Sinfo.
2519 if Is_Empty_List (Elsif_Parts (Ret)) then
2520 pragma Assert (Elsif_Parts (Ret) /= No_List);
2521 Set_Elsif_Parts (Ret, No_List);
2522 end if;
2523 end;
2524 end if;
2525 end if;
2527 return
2528 Make_Subprogram_Body (Loc,
2529 Specification => Spec,
2530 Declarations => Decls,
2531 Handled_Statement_Sequence =>
2532 Make_Handled_Sequence_Of_Statements (Loc,
2533 Statements => New_List (Ret)));
2534 end Build_Find_Body_Index;
2536 --------------------------------
2537 -- Build_Find_Body_Index_Spec --
2538 --------------------------------
2540 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2541 Loc : constant Source_Ptr := Sloc (Typ);
2542 Id : constant Entity_Id :=
2543 Make_Defining_Identifier (Loc,
2544 Chars => New_External_Name (Chars (Typ), 'F'));
2545 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2546 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2548 begin
2549 return
2550 Make_Function_Specification (Loc,
2551 Defining_Unit_Name => Id,
2552 Parameter_Specifications => New_List (
2553 Make_Parameter_Specification (Loc,
2554 Defining_Identifier => Parm1,
2555 Parameter_Type =>
2556 New_Occurrence_Of (RTE (RE_Address), Loc)),
2558 Make_Parameter_Specification (Loc,
2559 Defining_Identifier => Parm2,
2560 Parameter_Type =>
2561 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
2563 Result_Definition => New_Occurrence_Of (
2564 RTE (RE_Protected_Entry_Index), Loc));
2565 end Build_Find_Body_Index_Spec;
2567 -----------------------------------------------
2568 -- Build_Lock_Free_Protected_Subprogram_Body --
2569 -----------------------------------------------
2571 function Build_Lock_Free_Protected_Subprogram_Body
2572 (N : Node_Id;
2573 Prot_Typ : Node_Id;
2574 Unprot_Spec : Node_Id) return Node_Id
2576 Actuals : constant List_Id := New_List;
2577 Loc : constant Source_Ptr := Sloc (N);
2578 Spec : constant Node_Id := Specification (N);
2579 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2580 Formal : Node_Id;
2581 Prot_Spec : Node_Id;
2582 Stmt : Node_Id;
2584 begin
2585 -- Create the protected version of the body
2587 Prot_Spec :=
2588 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2590 -- Build the actual parameters which appear in the call to the
2591 -- unprotected version of the body.
2593 Formal := First (Parameter_Specifications (Prot_Spec));
2594 while Present (Formal) loop
2595 Append_To (Actuals,
2596 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2598 Next (Formal);
2599 end loop;
2601 -- Function case, generate:
2602 -- return <Unprot_Func_Call>;
2604 if Nkind (Spec) = N_Function_Specification then
2605 Stmt :=
2606 Make_Simple_Return_Statement (Loc,
2607 Expression =>
2608 Make_Function_Call (Loc,
2609 Name =>
2610 Make_Identifier (Loc, Chars (Unprot_Id)),
2611 Parameter_Associations => Actuals));
2613 -- Procedure case, call the unprotected version
2615 else
2616 Stmt :=
2617 Make_Procedure_Call_Statement (Loc,
2618 Name =>
2619 Make_Identifier (Loc, Chars (Unprot_Id)),
2620 Parameter_Associations => Actuals);
2621 end if;
2623 return
2624 Make_Subprogram_Body (Loc,
2625 Declarations => Empty_List,
2626 Specification => Prot_Spec,
2627 Handled_Statement_Sequence =>
2628 Make_Handled_Sequence_Of_Statements (Loc,
2629 Statements => New_List (Stmt)));
2630 end Build_Lock_Free_Protected_Subprogram_Body;
2632 -------------------------------------------------
2633 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2634 -------------------------------------------------
2636 -- Procedures which meet the lock-free implementation requirements and
2637 -- reference a unique scalar component Comp are expanded in the following
2638 -- manner:
2640 -- procedure P (...) is
2641 -- Expected_Comp : constant Comp_Type :=
2642 -- Comp_Type
2643 -- (System.Atomic_Primitives.Lock_Free_Read_N
2644 -- (_Object.Comp'Address));
2645 -- begin
2646 -- loop
2647 -- declare
2648 -- <original declarations before the object renaming declaration
2649 -- of Comp>
2651 -- Desired_Comp : Comp_Type := Expected_Comp;
2652 -- Comp : Comp_Type renames Desired_Comp;
2654 -- <original declarations after the object renaming declaration
2655 -- of Comp>
2657 -- begin
2658 -- <original statements>
2659 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
2660 -- (_Object.Comp'Address,
2661 -- Interfaces.Unsigned_N (Expected_Comp),
2662 -- Interfaces.Unsigned_N (Desired_Comp));
2663 -- end;
2664 -- end loop;
2665 -- end P;
2667 -- Each return and raise statement of P is transformed into an atomic
2668 -- status check:
2670 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2671 -- (_Object.Comp'Address,
2672 -- Interfaces.Unsigned_N (Expected_Comp),
2673 -- Interfaces.Unsigned_N (Desired_Comp));
2674 -- then
2675 -- <original statement>
2676 -- else
2677 -- goto L0;
2678 -- end if;
2680 -- Functions which meet the lock-free implementation requirements and
2681 -- reference a unique scalar component Comp are expanded in the following
2682 -- manner:
2684 -- function F (...) return ... is
2685 -- <original declarations before the object renaming declaration
2686 -- of Comp>
2688 -- Expected_Comp : constant Comp_Type :=
2689 -- Comp_Type
2690 -- (System.Atomic_Primitives.Lock_Free_Read_N
2691 -- (_Object.Comp'Address));
2692 -- Comp : Comp_Type renames Expected_Comp;
2694 -- <original declarations after the object renaming declaration of
2695 -- Comp>
2697 -- begin
2698 -- <original statements>
2699 -- end F;
2701 function Build_Lock_Free_Unprotected_Subprogram_Body
2702 (N : Node_Id;
2703 Prot_Typ : Node_Id) return Node_Id
2705 function Referenced_Component (N : Node_Id) return Entity_Id;
2706 -- Subprograms which meet the lock-free implementation criteria are
2707 -- allowed to reference only one unique component. Return the prival
2708 -- of the said component.
2710 --------------------------
2711 -- Referenced_Component --
2712 --------------------------
2714 function Referenced_Component (N : Node_Id) return Entity_Id is
2715 Comp : Entity_Id;
2716 Decl : Node_Id;
2717 Source_Comp : Entity_Id := Empty;
2719 begin
2720 -- Find the unique source component which N references in its
2721 -- statements.
2723 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
2724 declare
2725 Element : Lock_Free_Subprogram renames
2726 Lock_Free_Subprogram_Table.Table (Index);
2727 begin
2728 if Element.Sub_Body = N then
2729 Source_Comp := Element.Comp_Id;
2730 exit;
2731 end if;
2732 end;
2733 end loop;
2735 if No (Source_Comp) then
2736 return Empty;
2737 end if;
2739 -- Find the prival which corresponds to the source component within
2740 -- the declarations of N.
2742 Decl := First (Declarations (N));
2743 while Present (Decl) loop
2745 -- Privals appear as object renamings
2747 if Nkind (Decl) = N_Object_Renaming_Declaration then
2748 Comp := Defining_Identifier (Decl);
2750 if Present (Prival_Link (Comp))
2751 and then Prival_Link (Comp) = Source_Comp
2752 then
2753 return Comp;
2754 end if;
2755 end if;
2757 Next (Decl);
2758 end loop;
2760 return Empty;
2761 end Referenced_Component;
2763 -- Local variables
2765 Comp : constant Entity_Id := Referenced_Component (N);
2766 Loc : constant Source_Ptr := Sloc (N);
2767 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
2768 Decls : List_Id := Declarations (N);
2770 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
2772 begin
2773 -- Add renamings for the protection object, discriminals, privals, and
2774 -- the entry index constant for use by debugger.
2776 Debug_Private_Data_Declarations (Decls);
2778 -- Perform the lock-free expansion when the subprogram references a
2779 -- protected component.
2781 if Present (Comp) then
2782 Protected_Component_Ref : declare
2783 Comp_Decl : constant Node_Id := Parent (Comp);
2784 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
2785 Comp_Type : constant Entity_Id := Etype (Comp);
2787 Is_Procedure : constant Boolean :=
2788 Ekind (Corresponding_Spec (N)) = E_Procedure;
2789 -- Indicates if N is a protected procedure body
2791 Block_Decls : List_Id := No_List;
2792 Try_Write : Entity_Id;
2793 Desired_Comp : Entity_Id;
2794 Decl : Node_Id;
2795 Label : Node_Id;
2796 Label_Id : Entity_Id := Empty;
2797 Read : Entity_Id;
2798 Expected_Comp : Entity_Id;
2799 Stmt : Node_Id;
2800 Stmts : List_Id :=
2801 New_Copy_List_Tree (Statements (Hand_Stmt_Seq));
2802 Typ_Size : Int;
2803 Unsigned : Entity_Id;
2805 function Process_Node (N : Node_Id) return Traverse_Result;
2806 -- Transform a single node if it is a return statement, a raise
2807 -- statement or a reference to Comp.
2809 procedure Process_Stmts (Stmts : List_Id);
2810 -- Given a statement sequence Stmts, wrap any return or raise
2811 -- statements in the following manner:
2813 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2814 -- (_Object.Comp'Address,
2815 -- Interfaces.Unsigned_N (Expected_Comp),
2816 -- Interfaces.Unsigned_N (Desired_Comp))
2817 -- then
2818 -- <Stmt>;
2819 -- else
2820 -- goto L0;
2821 -- end if;
2823 ------------------
2824 -- Process_Node --
2825 ------------------
2827 function Process_Node (N : Node_Id) return Traverse_Result is
2829 procedure Wrap_Statement (Stmt : Node_Id);
2830 -- Wrap an arbitrary statement inside an if statement where the
2831 -- condition does an atomic check on the state of the object.
2833 --------------------
2834 -- Wrap_Statement --
2835 --------------------
2837 procedure Wrap_Statement (Stmt : Node_Id) is
2838 begin
2839 -- The first time through, create the declaration of a label
2840 -- which is used to skip the remainder of source statements
2841 -- if the state of the object has changed.
2843 if No (Label_Id) then
2844 Label_Id :=
2845 Make_Identifier (Loc, New_External_Name ('L', 0));
2846 Set_Entity (Label_Id,
2847 Make_Defining_Identifier (Loc, Chars (Label_Id)));
2848 end if;
2850 -- Generate:
2851 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2852 -- (_Object.Comp'Address,
2853 -- Interfaces.Unsigned_N (Expected_Comp),
2854 -- Interfaces.Unsigned_N (Desired_Comp))
2855 -- then
2856 -- <Stmt>;
2857 -- else
2858 -- goto L0;
2859 -- end if;
2861 Rewrite (Stmt,
2862 Make_Implicit_If_Statement (N,
2863 Condition =>
2864 Make_Function_Call (Loc,
2865 Name =>
2866 New_Occurrence_Of (Try_Write, Loc),
2867 Parameter_Associations => New_List (
2868 Make_Attribute_Reference (Loc,
2869 Prefix => Relocate_Node (Comp_Sel_Nam),
2870 Attribute_Name => Name_Address),
2872 Unchecked_Convert_To (Unsigned,
2873 New_Occurrence_Of (Expected_Comp, Loc)),
2875 Unchecked_Convert_To (Unsigned,
2876 New_Occurrence_Of (Desired_Comp, Loc)))),
2878 Then_Statements => New_List (Relocate_Node (Stmt)),
2880 Else_Statements => New_List (
2881 Make_Goto_Statement (Loc,
2882 Name =>
2883 New_Occurrence_Of (Entity (Label_Id), Loc)))));
2884 end Wrap_Statement;
2886 -- Start of processing for Process_Node
2888 begin
2889 -- Wrap each return and raise statement that appear inside a
2890 -- procedure. Skip the last return statement which is added by
2891 -- default since it is transformed into an exit statement.
2893 if Is_Procedure
2894 and then ((Nkind (N) = N_Simple_Return_Statement
2895 and then N /= Last (Stmts))
2896 or else Nkind (N) = N_Extended_Return_Statement
2897 or else (Nkind (N) in
2898 N_Raise_xxx_Error | N_Raise_Statement
2899 and then Comes_From_Source (N)))
2900 then
2901 Wrap_Statement (N);
2902 return Skip;
2903 end if;
2905 -- Force reanalysis
2907 Set_Analyzed (N, False);
2909 return OK;
2910 end Process_Node;
2912 procedure Process_Nodes is new Traverse_Proc (Process_Node);
2914 -------------------
2915 -- Process_Stmts --
2916 -------------------
2918 procedure Process_Stmts (Stmts : List_Id) is
2919 Stmt : Node_Id;
2920 begin
2921 Stmt := First (Stmts);
2922 while Present (Stmt) loop
2923 Process_Nodes (Stmt);
2924 Next (Stmt);
2925 end loop;
2926 end Process_Stmts;
2928 -- Start of processing for Protected_Component_Ref
2930 begin
2931 -- Get the type size
2933 if Known_Static_Esize (Comp_Type) then
2934 Typ_Size := UI_To_Int (Esize (Comp_Type));
2936 -- If the Esize (Object_Size) is unknown at compile time, look at
2937 -- the RM_Size (Value_Size) since it may have been set by an
2938 -- explicit representation clause.
2940 elsif Known_Static_RM_Size (Comp_Type) then
2941 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
2943 -- Should not happen since this has already been checked in
2944 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
2946 else
2947 raise Program_Error;
2948 end if;
2950 -- Retrieve all relevant atomic routines and types
2952 case Typ_Size is
2953 when 8 =>
2954 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
2955 Read := RTE (RE_Lock_Free_Read_8);
2956 Unsigned := RTE (RE_Uint8);
2958 when 16 =>
2959 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
2960 Read := RTE (RE_Lock_Free_Read_16);
2961 Unsigned := RTE (RE_Uint16);
2963 when 32 =>
2964 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
2965 Read := RTE (RE_Lock_Free_Read_32);
2966 Unsigned := RTE (RE_Uint32);
2968 when 64 =>
2969 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
2970 Read := RTE (RE_Lock_Free_Read_64);
2971 Unsigned := RTE (RE_Uint64);
2973 when others =>
2974 raise Program_Error;
2975 end case;
2977 -- Generate:
2978 -- Expected_Comp : constant Comp_Type :=
2979 -- Comp_Type
2980 -- (System.Atomic_Primitives.Lock_Free_Read_N
2981 -- (_Object.Comp'Address));
2983 Expected_Comp :=
2984 Make_Defining_Identifier (Loc,
2985 New_External_Name (Chars (Comp), Suffix => "_saved"));
2987 Decl :=
2988 Make_Object_Declaration (Loc,
2989 Defining_Identifier => Expected_Comp,
2990 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
2991 Constant_Present => True,
2992 Expression =>
2993 Unchecked_Convert_To (Comp_Type,
2994 Make_Function_Call (Loc,
2995 Name => New_Occurrence_Of (Read, Loc),
2996 Parameter_Associations => New_List (
2997 Make_Attribute_Reference (Loc,
2998 Prefix => Relocate_Node (Comp_Sel_Nam),
2999 Attribute_Name => Name_Address)))));
3001 -- Protected procedures
3003 if Is_Procedure then
3004 -- Move the original declarations inside the generated block
3006 Block_Decls := Decls;
3008 -- Reset the declarations list of the protected procedure to
3009 -- contain only Decl.
3011 Decls := New_List (Decl);
3013 -- Generate:
3014 -- Desired_Comp : Comp_Type := Expected_Comp;
3016 Desired_Comp :=
3017 Make_Defining_Identifier (Loc,
3018 New_External_Name (Chars (Comp), Suffix => "_current"));
3020 -- Insert the declarations of Expected_Comp and Desired_Comp in
3021 -- the block declarations right before the renaming of the
3022 -- protected component.
3024 Insert_Before (Comp_Decl,
3025 Make_Object_Declaration (Loc,
3026 Defining_Identifier => Desired_Comp,
3027 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3028 Expression =>
3029 New_Occurrence_Of (Expected_Comp, Loc)));
3031 -- Protected function
3033 else
3034 Desired_Comp := Expected_Comp;
3036 -- Insert the declaration of Expected_Comp in the function
3037 -- declarations right before the renaming of the protected
3038 -- component.
3040 Insert_Before (Comp_Decl, Decl);
3041 end if;
3043 -- Rewrite the protected component renaming declaration to be a
3044 -- renaming of Desired_Comp.
3046 -- Generate:
3047 -- Comp : Comp_Type renames Desired_Comp;
3049 Rewrite (Comp_Decl,
3050 Make_Object_Renaming_Declaration (Loc,
3051 Defining_Identifier =>
3052 Defining_Identifier (Comp_Decl),
3053 Subtype_Mark =>
3054 New_Occurrence_Of (Comp_Type, Loc),
3055 Name =>
3056 New_Occurrence_Of (Desired_Comp, Loc)));
3058 -- Wrap any return or raise statements in Stmts in same the manner
3059 -- described in Process_Stmts.
3061 Process_Stmts (Stmts);
3063 -- Generate:
3064 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3065 -- (_Object.Comp'Address,
3066 -- Interfaces.Unsigned_N (Expected_Comp),
3067 -- Interfaces.Unsigned_N (Desired_Comp))
3069 if Is_Procedure then
3070 Stmt :=
3071 Make_Exit_Statement (Loc,
3072 Condition =>
3073 Make_Function_Call (Loc,
3074 Name =>
3075 New_Occurrence_Of (Try_Write, Loc),
3076 Parameter_Associations => New_List (
3077 Make_Attribute_Reference (Loc,
3078 Prefix => Relocate_Node (Comp_Sel_Nam),
3079 Attribute_Name => Name_Address),
3081 Unchecked_Convert_To (Unsigned,
3082 New_Occurrence_Of (Expected_Comp, Loc)),
3084 Unchecked_Convert_To (Unsigned,
3085 New_Occurrence_Of (Desired_Comp, Loc)))));
3087 -- Small optimization: transform the default return statement
3088 -- of a procedure into the atomic exit statement.
3090 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3091 Rewrite (Last (Stmts), Stmt);
3092 else
3093 Append_To (Stmts, Stmt);
3094 end if;
3095 end if;
3097 -- Create the declaration of the label used to skip the rest of
3098 -- the source statements when the object state changes.
3100 if Present (Label_Id) then
3101 Label := Make_Label (Loc, Label_Id);
3102 Append_To (Decls,
3103 Make_Implicit_Label_Declaration (Loc,
3104 Defining_Identifier => Entity (Label_Id),
3105 Label_Construct => Label));
3106 Append_To (Stmts, Label);
3107 end if;
3109 -- Generate:
3110 -- loop
3111 -- declare
3112 -- <Decls>
3113 -- begin
3114 -- <Stmts>
3115 -- end;
3116 -- end loop;
3118 if Is_Procedure then
3119 Stmts :=
3120 New_List (
3121 Make_Loop_Statement (Loc,
3122 Statements => New_List (
3123 Make_Block_Statement (Loc,
3124 Declarations => Block_Decls,
3125 Handled_Statement_Sequence =>
3126 Make_Handled_Sequence_Of_Statements (Loc,
3127 Statements => Stmts))),
3128 End_Label => Empty));
3129 end if;
3131 Hand_Stmt_Seq :=
3132 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3133 end Protected_Component_Ref;
3134 end if;
3136 -- Make an unprotected version of the subprogram for use within the same
3137 -- object, with new name and extra parameter representing the object.
3139 return
3140 Make_Subprogram_Body (Loc,
3141 Specification =>
3142 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3143 Declarations => Decls,
3144 Handled_Statement_Sequence => Hand_Stmt_Seq);
3145 end Build_Lock_Free_Unprotected_Subprogram_Body;
3147 -------------------------
3148 -- Build_Master_Entity --
3149 -------------------------
3151 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3152 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3153 Context : Node_Id;
3154 Context_Id : Entity_Id;
3155 Decl : Node_Id;
3156 Decls : List_Id;
3157 Par : Node_Id;
3159 begin
3160 -- No action needed if the run-time has no tasking support
3162 if Global_No_Tasking then
3163 return;
3164 end if;
3166 if Is_Itype (Obj_Or_Typ) then
3167 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3168 else
3169 Par := Parent (Obj_Or_Typ);
3170 end if;
3172 -- For transient scopes check if the master entity is already defined
3174 if Is_Type (Obj_Or_Typ)
3175 and then Ekind (Scope (Obj_Or_Typ)) = E_Block
3176 and then Is_Internal (Scope (Obj_Or_Typ))
3177 then
3178 declare
3179 Master_Scope : constant Entity_Id :=
3180 Find_Master_Scope (Obj_Or_Typ);
3181 begin
3182 if Has_Master_Entity (Master_Scope)
3183 or else Is_Finalizer (Master_Scope)
3184 then
3185 return;
3186 end if;
3188 if Present (Current_Entity_In_Scope (Name_uMaster)) then
3189 return;
3190 end if;
3191 end;
3192 end if;
3194 -- When creating a master for a record component which is either a task
3195 -- or access-to-task, the enclosing record is the master scope and the
3196 -- proper insertion point is the component list.
3198 if Is_Record_Type (Current_Scope) then
3199 Context := Par;
3200 Context_Id := Current_Scope;
3201 Decls := List_Containing (Context);
3203 -- Default case for object declarations and access types. Note that the
3204 -- context is updated to the nearest enclosing body, block, package, or
3205 -- return statement.
3207 else
3208 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3209 end if;
3211 -- When the enclosing context is a BIP function whose result type has
3212 -- tasks, the function has an extra formal that is the master of the
3213 -- tasks to be created by its returned object (that is, when its
3214 -- enclosing context is a return statement). However, if the body of
3215 -- the function creates tasks before its return statements, such tasks
3216 -- need their own master.
3218 if Has_Master_Entity (Context_Id)
3219 and then Ekind (Context_Id) = E_Function
3220 and then Is_Build_In_Place_Function (Context_Id)
3221 and then Needs_BIP_Task_Actuals (Context_Id)
3222 then
3223 -- No need to add it again if previously added
3225 declare
3226 Master_Present : Boolean;
3228 begin
3229 -- Handle transient scopes
3231 if Context_Id /= Current_Scope then
3232 Push_Scope (Context_Id);
3233 Master_Present :=
3234 Present (Current_Entity_In_Scope (Name_uMaster));
3235 Pop_Scope;
3236 else
3237 Master_Present :=
3238 Present (Current_Entity_In_Scope (Name_uMaster));
3239 end if;
3241 if Master_Present then
3242 return;
3243 end if;
3244 end;
3246 -- Nothing to do if the context already has a master; internally built
3247 -- finalizers don't need a master.
3249 elsif Has_Master_Entity (Context_Id)
3250 or else Is_Finalizer (Context_Id)
3251 then
3252 return;
3253 end if;
3255 Decl := Build_Master_Declaration (Loc);
3257 -- The master is inserted at the start of the declarative list of the
3258 -- context.
3260 Prepend_To (Decls, Decl);
3262 -- In certain cases where transient scopes are involved, the immediate
3263 -- scope is not always the proper master scope. Ensure that the master
3264 -- declaration and entity appear in the same context.
3266 if Context_Id /= Current_Scope then
3267 Push_Scope (Context_Id);
3268 Analyze (Decl);
3269 Pop_Scope;
3270 else
3271 Analyze (Decl);
3272 end if;
3274 -- Mark the enclosing scope and its associated construct as being task
3275 -- masters.
3277 Set_Has_Master_Entity (Context_Id);
3279 while Present (Context)
3280 and then Nkind (Context) /= N_Compilation_Unit
3281 loop
3282 if Nkind (Context) in
3283 N_Block_Statement | N_Subprogram_Body | N_Task_Body
3284 then
3285 Set_Is_Task_Master (Context);
3286 exit;
3288 elsif Nkind (Parent (Context)) = N_Subunit then
3289 Context := Corresponding_Stub (Parent (Context));
3290 end if;
3292 Context := Parent (Context);
3293 end loop;
3294 end Build_Master_Entity;
3296 ---------------------------
3297 -- Build_Master_Renaming --
3298 ---------------------------
3300 procedure Build_Master_Renaming
3301 (Ptr_Typ : Entity_Id;
3302 Ins_Nod : Node_Id := Empty)
3304 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3305 Context : Node_Id;
3306 Master_Decl : Node_Id;
3307 Master_Id : Entity_Id;
3309 begin
3310 -- No action needed if the run-time has no tasking support
3312 if Global_No_Tasking then
3313 return;
3314 end if;
3316 -- Determine the proper context to insert the master renaming
3318 if Present (Ins_Nod) then
3319 Context := Ins_Nod;
3321 elsif Is_Itype (Ptr_Typ) then
3322 Context := Associated_Node_For_Itype (Ptr_Typ);
3324 -- When the context references a discriminant or a component of a
3325 -- private type and we are processing declarations in the private
3326 -- part of the enclosing package, we must insert the master renaming
3327 -- before the full declaration of the private type; otherwise the
3328 -- master renaming would be inserted in the public part of the
3329 -- package (and hence before the declaration of _master).
3331 if In_Private_Part (Current_Scope) then
3332 declare
3333 Ctx : Node_Id := Context;
3335 begin
3336 if Nkind (Context) = N_Discriminant_Specification then
3337 Ctx := Parent (Ctx);
3338 else
3339 while Nkind (Ctx) in
3340 N_Component_Declaration | N_Component_List
3341 loop
3342 Ctx := Parent (Ctx);
3343 end loop;
3344 end if;
3346 if Nkind (Ctx) in N_Private_Type_Declaration
3347 | N_Private_Extension_Declaration
3348 then
3349 Context := Parent (Full_View (Defining_Identifier (Ctx)));
3350 end if;
3351 end;
3352 end if;
3354 else
3355 Context := Parent (Ptr_Typ);
3356 end if;
3358 -- Generate:
3359 -- <Ptr_Typ>M : Master_Id renames _Master;
3360 -- and add a numeric suffix to the name to ensure that it is
3361 -- unique in case other access types in nested constructs
3362 -- are homonyms of this one.
3364 Master_Id :=
3365 Make_Defining_Identifier (Loc,
3366 New_External_Name (Chars (Ptr_Typ), 'M', -1));
3368 Master_Decl :=
3369 Make_Object_Renaming_Declaration (Loc,
3370 Defining_Identifier => Master_Id,
3371 Subtype_Mark =>
3372 New_Occurrence_Of (Standard_Integer, Loc),
3373 Name => Make_Identifier (Loc, Name_uMaster));
3375 Insert_Action (Context, Master_Decl);
3377 -- The renamed master now services the access type
3379 Set_Master_Id (Ptr_Typ, Master_Id);
3380 end Build_Master_Renaming;
3382 ---------------------------
3383 -- Build_Protected_Entry --
3384 ---------------------------
3386 function Build_Protected_Entry
3387 (N : Node_Id;
3388 Ent : Entity_Id;
3389 Pid : Node_Id) return Node_Id
3391 Bod_Decls : constant List_Id := New_List;
3392 Decls : constant List_Id := Declarations (N);
3393 End_Lab : constant Node_Id :=
3394 End_Label (Handled_Statement_Sequence (N));
3395 End_Loc : constant Source_Ptr :=
3396 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3397 -- Used for the generated call to Complete_Entry_Body
3399 Loc : constant Source_Ptr := Sloc (N);
3401 Bod_Id : Entity_Id;
3402 Bod_Spec : Node_Id;
3403 Bod_Stmts : List_Id;
3404 Complete : Node_Id;
3405 Ohandle : Node_Id;
3406 Proc_Body : Node_Id;
3408 EH_Loc : Source_Ptr;
3409 -- Used for the exception handler, inserted at end of the body
3411 begin
3412 -- Set the source location on the exception handler only when debugging
3413 -- the expanded code (see Make_Implicit_Exception_Handler).
3415 if Debug_Generated_Code then
3416 EH_Loc := End_Loc;
3418 -- Otherwise the inserted code should not be visible to the debugger
3420 else
3421 EH_Loc := No_Location;
3422 end if;
3424 Bod_Id :=
3425 Make_Defining_Identifier (Loc,
3426 Chars => Chars (Protected_Body_Subprogram (Ent)));
3427 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3429 -- Add the following declarations:
3431 -- type poVP is access poV;
3432 -- _object : poVP := poVP (_O);
3434 -- where _O is the formal parameter associated with the concurrent
3435 -- object. These declarations are needed for Complete_Entry_Body.
3437 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3439 -- Add renamings for all formals, the Protection object, discriminals,
3440 -- privals and the entry index constant for use by debugger.
3442 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3443 Debug_Private_Data_Declarations (Decls);
3445 -- Put the declarations and the statements from the entry
3447 Bod_Stmts :=
3448 New_List (
3449 Make_Block_Statement (Loc,
3450 Declarations => Decls,
3451 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3453 -- Analyze now and reset scopes for declarations so that Scope fields
3454 -- currently denoting the entry will now denote the block scope, and
3455 -- the block's scope will be set to the new procedure entity.
3457 Analyze_Statements (Bod_Stmts);
3459 Set_Scope (Entity (Identifier (First (Bod_Stmts))),
3460 Protected_Body_Subprogram (Ent));
3462 Reset_Scopes_To
3463 (First (Bod_Stmts), Entity (Identifier (First (Bod_Stmts))));
3465 case Corresponding_Runtime_Package (Pid) is
3466 when System_Tasking_Protected_Objects_Entries =>
3467 Append_To (Bod_Stmts,
3468 Make_Procedure_Call_Statement (End_Loc,
3469 Name =>
3470 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3471 Parameter_Associations => New_List (
3472 Make_Attribute_Reference (End_Loc,
3473 Prefix =>
3474 Make_Selected_Component (End_Loc,
3475 Prefix =>
3476 Make_Identifier (End_Loc, Name_uObject),
3477 Selector_Name =>
3478 Make_Identifier (End_Loc, Name_uObject)),
3479 Attribute_Name => Name_Unchecked_Access))));
3481 when System_Tasking_Protected_Objects_Single_Entry =>
3483 -- Historically, a call to Complete_Single_Entry_Body was
3484 -- inserted, but it was a null procedure.
3486 null;
3488 when others =>
3489 raise Program_Error;
3490 end case;
3492 -- When exceptions cannot be propagated, we never need to call
3493 -- Exception_Complete_Entry_Body.
3495 if No_Exception_Handlers_Set then
3496 return
3497 Make_Subprogram_Body (Loc,
3498 Specification => Bod_Spec,
3499 Declarations => Bod_Decls,
3500 Handled_Statement_Sequence =>
3501 Make_Handled_Sequence_Of_Statements (Loc,
3502 Statements => Bod_Stmts,
3503 End_Label => End_Lab));
3505 else
3506 Ohandle := Make_Others_Choice (Loc);
3507 Set_All_Others (Ohandle);
3509 case Corresponding_Runtime_Package (Pid) is
3510 when System_Tasking_Protected_Objects_Entries =>
3511 Complete :=
3512 New_Occurrence_Of
3513 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3515 when System_Tasking_Protected_Objects_Single_Entry =>
3516 Complete :=
3517 New_Occurrence_Of
3518 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3520 when others =>
3521 raise Program_Error;
3522 end case;
3524 -- Create body of entry procedure. The renaming declarations are
3525 -- placed ahead of the block that contains the actual entry body.
3527 Proc_Body :=
3528 Make_Subprogram_Body (Loc,
3529 Specification => Bod_Spec,
3530 Declarations => Bod_Decls,
3531 Handled_Statement_Sequence =>
3532 Make_Handled_Sequence_Of_Statements (Loc,
3533 Statements => Bod_Stmts,
3534 End_Label => End_Lab,
3535 Exception_Handlers => New_List (
3536 Make_Implicit_Exception_Handler (EH_Loc,
3537 Exception_Choices => New_List (Ohandle),
3539 Statements => New_List (
3540 Make_Procedure_Call_Statement (EH_Loc,
3541 Name => Complete,
3542 Parameter_Associations => New_List (
3543 Make_Attribute_Reference (EH_Loc,
3544 Prefix =>
3545 Make_Selected_Component (EH_Loc,
3546 Prefix =>
3547 Make_Identifier (EH_Loc, Name_uObject),
3548 Selector_Name =>
3549 Make_Identifier (EH_Loc, Name_uObject)),
3550 Attribute_Name => Name_Unchecked_Access),
3552 Make_Function_Call (EH_Loc,
3553 Name =>
3554 New_Occurrence_Of
3555 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
3557 -- Establish link between subprogram body and source entry body
3559 Set_Corresponding_Entry_Body (Proc_Body, N);
3560 Set_At_End_Proc (Proc_Body, At_End_Proc (N));
3562 Reset_Scopes_To (Proc_Body, Protected_Body_Subprogram (Ent));
3563 return Proc_Body;
3564 end if;
3565 end Build_Protected_Entry;
3567 -----------------------------------------
3568 -- Build_Protected_Entry_Specification --
3569 -----------------------------------------
3571 function Build_Protected_Entry_Specification
3572 (Loc : Source_Ptr;
3573 Def_Id : Entity_Id;
3574 Ent_Id : Entity_Id) return Node_Id
3576 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3578 begin
3579 Set_Debug_Info_Needed (Def_Id);
3581 if Present (Ent_Id) then
3582 Append_Elmt (P, Accept_Address (Ent_Id));
3583 end if;
3585 return
3586 Make_Procedure_Specification (Loc,
3587 Defining_Unit_Name => Def_Id,
3588 Parameter_Specifications => New_List (
3589 Make_Parameter_Specification (Loc,
3590 Defining_Identifier =>
3591 Make_Defining_Identifier (Loc, Name_uO),
3592 Parameter_Type =>
3593 New_Occurrence_Of (RTE (RE_Address), Loc)),
3595 Make_Parameter_Specification (Loc,
3596 Defining_Identifier => P,
3597 Parameter_Type =>
3598 New_Occurrence_Of (RTE (RE_Address), Loc)),
3600 Make_Parameter_Specification (Loc,
3601 Defining_Identifier =>
3602 Make_Defining_Identifier (Loc, Name_uE),
3603 Parameter_Type =>
3604 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
3605 end Build_Protected_Entry_Specification;
3607 --------------------------
3608 -- Build_Protected_Spec --
3609 --------------------------
3611 function Build_Protected_Spec
3612 (N : Node_Id;
3613 Obj_Type : Entity_Id;
3614 Ident : Entity_Id;
3615 Unprotected : Boolean := False) return List_Id
3617 Loc : constant Source_Ptr := Sloc (N);
3619 Decl : Node_Id;
3620 Formal : Entity_Id;
3621 New_Formal : Entity_Id;
3622 New_Plist : List_Id;
3624 begin
3625 New_Plist := New_List;
3627 Formal := First_Formal (Ident);
3628 while Present (Formal) loop
3629 New_Formal :=
3630 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
3631 Set_Comes_From_Source (New_Formal, Comes_From_Source (Formal));
3633 if Unprotected then
3634 Mutate_Ekind (New_Formal, Ekind (Formal));
3635 Set_Protected_Formal (Formal, New_Formal);
3636 end if;
3638 Append_To (New_Plist,
3639 Make_Parameter_Specification (Loc,
3640 Defining_Identifier => New_Formal,
3641 Aliased_Present => Aliased_Present (Parent (Formal)),
3642 In_Present => In_Present (Parent (Formal)),
3643 Out_Present => Out_Present (Parent (Formal)),
3644 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc)));
3646 Next_Formal (Formal);
3647 end loop;
3649 -- If the subprogram is a procedure and the context is not an access
3650 -- to protected subprogram, the parameter is in-out. Otherwise it is
3651 -- an in parameter.
3653 Decl :=
3654 Make_Parameter_Specification (Loc,
3655 Defining_Identifier =>
3656 Make_Defining_Identifier (Loc, Name_uObject),
3657 In_Present => True,
3658 Out_Present =>
3659 (Etype (Ident) = Standard_Void_Type
3660 and then not Is_RTE (Obj_Type, RE_Address)),
3661 Parameter_Type =>
3662 New_Occurrence_Of (Obj_Type, Loc));
3663 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3664 Prepend_To (New_Plist, Decl);
3666 return New_Plist;
3667 end Build_Protected_Spec;
3669 ---------------------------------------
3670 -- Build_Protected_Sub_Specification --
3671 ---------------------------------------
3673 function Build_Protected_Sub_Specification
3674 (N : Node_Id;
3675 Prot_Typ : Entity_Id;
3676 Mode : Subprogram_Protection_Mode) return Node_Id
3678 Loc : constant Source_Ptr := Sloc (N);
3679 Decl : Node_Id;
3680 Def_Id : Entity_Id;
3681 New_Id : Entity_Id;
3682 New_Plist : List_Id;
3683 New_Spec : Node_Id;
3685 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3686 (Dispatching_Mode => ' ',
3687 Protected_Mode => 'P',
3688 Unprotected_Mode => 'N');
3690 begin
3691 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
3692 then
3693 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3694 else
3695 Decl := N;
3696 end if;
3698 Def_Id := Defining_Unit_Name (Specification (Decl));
3700 New_Plist :=
3701 Build_Protected_Spec
3702 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3703 Mode = Unprotected_Mode);
3704 New_Id :=
3705 Make_Defining_Identifier (Loc,
3706 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3708 -- Reference the original nondispatching subprogram since the analysis
3709 -- of the object.operation notation may need its original name (see
3710 -- Sem_Ch4.Names_Match).
3712 if Mode = Dispatching_Mode then
3713 Mutate_Ekind (New_Id, Ekind (Def_Id));
3714 Set_Original_Protected_Subprogram (New_Id, Def_Id);
3715 end if;
3717 -- Link the protected or unprotected version to the original subprogram
3718 -- it emulates.
3720 Mutate_Ekind (New_Id, Ekind (Def_Id));
3721 Set_Protected_Subprogram (New_Id, Def_Id);
3723 -- The unprotected operation carries the user code, and debugging
3724 -- information must be generated for it, even though this spec does
3725 -- not come from source. It is also convenient to allow gdb to step
3726 -- into the protected operation, even though it only contains lock/
3727 -- unlock calls.
3729 Set_Debug_Info_Needed (New_Id);
3731 -- If a pragma Eliminate applies to the source entity, the internal
3732 -- subprograms will be eliminated as well.
3734 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3736 -- It seems we should set Has_Nested_Subprogram here, but instead we
3737 -- currently set it in Expand_N_Protected_Body, because the entity
3738 -- created here isn't the one that Corresponding_Spec of the body
3739 -- will later be set to, and that's the entity where it's needed. ???
3741 Set_Has_Nested_Subprogram (New_Id, Has_Nested_Subprogram (Def_Id));
3743 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3744 New_Spec :=
3745 Make_Procedure_Specification (Loc,
3746 Defining_Unit_Name => New_Id,
3747 Parameter_Specifications => New_Plist);
3749 -- Create a new specification for the anonymous subprogram type
3751 else
3752 New_Spec :=
3753 Make_Function_Specification (Loc,
3754 Defining_Unit_Name => New_Id,
3755 Parameter_Specifications => New_Plist,
3756 Result_Definition =>
3757 Copy_Result_Type (Result_Definition (Specification (Decl))));
3759 Set_Return_Present (Defining_Unit_Name (New_Spec));
3760 end if;
3762 return New_Spec;
3763 end Build_Protected_Sub_Specification;
3765 -------------------------------------
3766 -- Build_Protected_Subprogram_Body --
3767 -------------------------------------
3769 function Build_Protected_Subprogram_Body
3770 (N : Node_Id;
3771 Pid : Node_Id;
3772 N_Op_Spec : Node_Id) return Node_Id
3774 Might_Raise : constant Boolean := Sem_Util.Might_Raise (N);
3776 Loc : constant Source_Ptr := Sloc (N);
3777 Op_Spec : constant Node_Id := Specification (N);
3778 P_Op_Spec : constant Node_Id :=
3779 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3781 Lock_Kind : RE_Id;
3782 Lock_Name : Node_Id;
3783 Lock_Stmt : Node_Id;
3784 Object_Parm : Node_Id;
3785 Pformal : Node_Id;
3786 R : Node_Id;
3787 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3788 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3789 Stmts : List_Id;
3790 Sub_Body : Node_Id;
3791 Uactuals : List_Id;
3792 Unprot_Call : Node_Id;
3794 begin
3795 -- Build a list of the formal parameters of the protected version of
3796 -- the subprogram to use as the actual parameters of the unprotected
3797 -- version.
3799 Uactuals := New_List;
3800 Pformal := First (Parameter_Specifications (P_Op_Spec));
3801 while Present (Pformal) loop
3802 Append_To (Uactuals,
3803 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3804 Next (Pformal);
3805 end loop;
3807 -- Make a call to the unprotected version of the subprogram built above
3808 -- for use by the protected version built below.
3810 if Nkind (Op_Spec) = N_Function_Specification then
3811 if Might_Raise then
3812 Unprot_Call :=
3813 Make_Simple_Return_Statement (Loc,
3814 Expression =>
3815 Make_Function_Call (Loc,
3816 Name =>
3817 Make_Identifier (Loc,
3818 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3819 Parameter_Associations => Uactuals));
3821 else
3822 R := Make_Temporary (Loc, 'R');
3824 Unprot_Call :=
3825 Make_Object_Declaration (Loc,
3826 Defining_Identifier => R,
3827 Constant_Present => True,
3828 Object_Definition =>
3829 New_Copy (Result_Definition (N_Op_Spec)),
3830 Expression =>
3831 Make_Function_Call (Loc,
3832 Name =>
3833 Make_Identifier (Loc,
3834 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3835 Parameter_Associations => Uactuals));
3837 Return_Stmt :=
3838 Make_Simple_Return_Statement (Loc,
3839 Expression => New_Occurrence_Of (R, Loc));
3840 end if;
3842 if Has_Aspect (Pid, Aspect_Exclusive_Functions)
3843 and then
3844 (No (Find_Value_Of_Aspect (Pid, Aspect_Exclusive_Functions))
3845 or else
3846 Is_True (Static_Boolean (Find_Value_Of_Aspect
3847 (Pid, Aspect_Exclusive_Functions))))
3848 then
3849 Lock_Kind := RE_Lock;
3850 else
3851 Lock_Kind := RE_Lock_Read_Only;
3852 end if;
3853 else
3854 Unprot_Call :=
3855 Make_Procedure_Call_Statement (Loc,
3856 Name =>
3857 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3858 Parameter_Associations => Uactuals);
3860 Lock_Kind := RE_Lock;
3861 end if;
3863 -- Wrap call in block that will be covered by an at_end handler
3865 if Might_Raise then
3866 Unprot_Call :=
3867 Make_Block_Statement (Loc,
3868 Handled_Statement_Sequence =>
3869 Make_Handled_Sequence_Of_Statements (Loc,
3870 Statements => New_List (Unprot_Call)));
3871 end if;
3873 -- Make the protected subprogram body. This locks the protected
3874 -- object and calls the unprotected version of the subprogram.
3876 case Corresponding_Runtime_Package (Pid) is
3877 when System_Tasking_Protected_Objects_Entries =>
3878 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
3880 when System_Tasking_Protected_Objects_Single_Entry =>
3881 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
3883 when System_Tasking_Protected_Objects =>
3884 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
3886 when others =>
3887 raise Program_Error;
3888 end case;
3890 Object_Parm :=
3891 Make_Attribute_Reference (Loc,
3892 Prefix =>
3893 Make_Selected_Component (Loc,
3894 Prefix => Make_Identifier (Loc, Name_uObject),
3895 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3896 Attribute_Name => Name_Unchecked_Access);
3898 Lock_Stmt :=
3899 Make_Procedure_Call_Statement (Loc,
3900 Name => Lock_Name,
3901 Parameter_Associations => New_List (Object_Parm));
3903 if Abort_Allowed then
3904 Stmts := New_List (
3905 Build_Runtime_Call (Loc, RE_Abort_Defer),
3906 Lock_Stmt);
3908 else
3909 Stmts := New_List (Lock_Stmt);
3910 end if;
3912 if Might_Raise then
3913 Append (Unprot_Call, Stmts);
3914 else
3915 if Nkind (Op_Spec) = N_Function_Specification then
3916 Pre_Stmts := Stmts;
3917 Stmts := Empty_List;
3918 else
3919 Append (Unprot_Call, Stmts);
3920 end if;
3922 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
3924 if Nkind (Op_Spec) = N_Function_Specification then
3925 Append_To (Stmts, Return_Stmt);
3926 Append_To (Pre_Stmts,
3927 Make_Block_Statement (Loc,
3928 Declarations => New_List (Unprot_Call),
3929 Handled_Statement_Sequence =>
3930 Make_Handled_Sequence_Of_Statements (Loc,
3931 Statements => Stmts)));
3932 Stmts := Pre_Stmts;
3933 end if;
3934 end if;
3936 Sub_Body :=
3937 Make_Subprogram_Body (Loc,
3938 Declarations => Empty_List,
3939 Specification => P_Op_Spec,
3940 Handled_Statement_Sequence =>
3941 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3943 -- Mark this subprogram as a protected subprogram body so that the
3944 -- cleanup will be inserted. This is done only in the Might_Raise
3945 -- case because otherwise the cleanup has already been inserted.
3947 if Might_Raise then
3948 Set_Is_Protected_Subprogram_Body (Sub_Body);
3949 end if;
3951 return Sub_Body;
3952 end Build_Protected_Subprogram_Body;
3954 -------------------------------------
3955 -- Build_Protected_Subprogram_Call --
3956 -------------------------------------
3958 procedure Build_Protected_Subprogram_Call
3959 (N : Node_Id;
3960 Name : Node_Id;
3961 Rec : Node_Id;
3962 External : Boolean := True)
3964 Loc : constant Source_Ptr := Sloc (N);
3965 Sub : constant Entity_Id := Entity (Name);
3966 New_Sub : Node_Id;
3967 Params : List_Id;
3969 begin
3970 if External then
3971 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
3972 else
3973 New_Sub :=
3974 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
3975 end if;
3977 if Present (Parameter_Associations (N)) then
3978 Params := New_Copy_List_Tree (Parameter_Associations (N));
3979 else
3980 Params := New_List;
3981 end if;
3983 -- If the type is an untagged derived type, convert to the root type,
3984 -- which is the one on which the operations are defined.
3986 if Nkind (Rec) = N_Unchecked_Type_Conversion
3987 and then not Is_Tagged_Type (Etype (Rec))
3988 and then Is_Derived_Type (Etype (Rec))
3989 then
3990 Set_Etype (Rec, Root_Type (Etype (Rec)));
3991 Set_Subtype_Mark (Rec,
3992 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
3993 end if;
3995 Prepend (Rec, Params);
3997 if Ekind (Sub) = E_Procedure then
3998 Rewrite (N,
3999 Make_Procedure_Call_Statement (Loc,
4000 Name => New_Sub,
4001 Parameter_Associations => Params));
4003 else
4004 pragma Assert (Ekind (Sub) = E_Function);
4005 Rewrite (N,
4006 Make_Function_Call (Loc,
4007 Name => New_Sub,
4008 Parameter_Associations => Params));
4010 -- Preserve type of call for subsequent processing (required for
4011 -- call to Wrap_Transient_Expression in the case of a shared passive
4012 -- protected).
4014 Set_Etype (N, Etype (New_Sub));
4015 end if;
4017 if External
4018 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4019 and then Is_Entity_Name (Expression (Rec))
4020 and then Is_Shared_Passive (Entity (Expression (Rec)))
4021 then
4022 Add_Shared_Var_Lock_Procs (N);
4023 end if;
4024 end Build_Protected_Subprogram_Call;
4026 ---------------------------------------------
4027 -- Build_Protected_Subprogram_Call_Cleanup --
4028 ---------------------------------------------
4030 procedure Build_Protected_Subprogram_Call_Cleanup
4031 (Op_Spec : Node_Id;
4032 Conc_Typ : Node_Id;
4033 Loc : Source_Ptr;
4034 Stmts : List_Id)
4036 Nam : Node_Id;
4038 begin
4039 -- If the associated protected object has entries, a protected
4040 -- procedure has to service entry queues. In this case generate:
4042 -- Service_Entries (_object._object'Access);
4044 if Nkind (Op_Spec) = N_Procedure_Specification
4045 and then Has_Entries (Conc_Typ)
4046 then
4047 case Corresponding_Runtime_Package (Conc_Typ) is
4048 when System_Tasking_Protected_Objects_Entries =>
4049 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4051 when System_Tasking_Protected_Objects_Single_Entry =>
4052 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4054 when others =>
4055 raise Program_Error;
4056 end case;
4058 Append_To (Stmts,
4059 Make_Procedure_Call_Statement (Loc,
4060 Name => Nam,
4061 Parameter_Associations => New_List (
4062 Make_Attribute_Reference (Loc,
4063 Prefix =>
4064 Make_Selected_Component (Loc,
4065 Prefix => Make_Identifier (Loc, Name_uObject),
4066 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4067 Attribute_Name => Name_Unchecked_Access))));
4069 else
4070 -- Generate:
4071 -- Unlock (_object._object'Access);
4073 case Corresponding_Runtime_Package (Conc_Typ) is
4074 when System_Tasking_Protected_Objects_Entries =>
4075 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4077 when System_Tasking_Protected_Objects_Single_Entry =>
4078 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4080 when System_Tasking_Protected_Objects =>
4081 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4083 when others =>
4084 raise Program_Error;
4085 end case;
4087 Append_To (Stmts,
4088 Make_Procedure_Call_Statement (Loc,
4089 Name => Nam,
4090 Parameter_Associations => New_List (
4091 Make_Attribute_Reference (Loc,
4092 Prefix =>
4093 Make_Selected_Component (Loc,
4094 Prefix => Make_Identifier (Loc, Name_uObject),
4095 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4096 Attribute_Name => Name_Unchecked_Access))));
4097 end if;
4099 -- Generate:
4100 -- Abort_Undefer;
4102 if Abort_Allowed then
4103 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4104 end if;
4105 end Build_Protected_Subprogram_Call_Cleanup;
4107 -------------------------
4108 -- Build_Selected_Name --
4109 -------------------------
4111 function Build_Selected_Name
4112 (Prefix : Entity_Id;
4113 Selector : Entity_Id;
4114 Append_Char : Character := ' ') return Name_Id
4116 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4117 Select_Len : Natural;
4119 begin
4120 Get_Name_String (Chars (Selector));
4121 Select_Len := Name_Len;
4122 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4123 Get_Name_String (Chars (Prefix));
4125 -- If scope is anonymous type, discard suffix to recover name of
4126 -- single protected object. Otherwise use protected type name.
4128 if Name_Buffer (Name_Len) = 'T' then
4129 Name_Len := Name_Len - 1;
4130 end if;
4132 Add_Str_To_Name_Buffer ("__");
4133 for J in 1 .. Select_Len loop
4134 Add_Char_To_Name_Buffer (Select_Buffer (J));
4135 end loop;
4137 -- Now add the Append_Char if specified. The encoding to follow
4138 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4139 -- then the entity is associated to a protected type subprogram.
4140 -- Otherwise, it is a protected type entry. For each case, the
4141 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4143 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4145 if Append_Char /= ' ' then
4146 if Append_Char in 'P' | 'N' then
4147 Add_Char_To_Name_Buffer (Append_Char);
4148 return Name_Find;
4149 else
4150 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4151 return New_External_Name (Name_Find, ' ', -1);
4152 end if;
4153 else
4154 return Name_Find;
4155 end if;
4156 end Build_Selected_Name;
4158 -----------------------------
4159 -- Build_Simple_Entry_Call --
4160 -----------------------------
4162 -- A task entry call is converted to a call to Call_Simple
4164 -- declare
4165 -- P : parms := (parm, parm, parm);
4166 -- begin
4167 -- Call_Simple (acceptor-task, entry-index, P'Address);
4168 -- parm := P.param;
4169 -- parm := P.param;
4170 -- ...
4171 -- end;
4173 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4174 -- the parameters, and the constructed aggregate value contains either the
4175 -- parameters or, in the case of non-elementary types, references to these
4176 -- parameters. Then the address of this aggregate is passed to the runtime
4177 -- routine, along with the task id value and the task entry index value.
4178 -- Pnn is only required if parameters are present.
4180 -- The assignments after the call are present only in the case of in-out
4181 -- or out parameters for elementary types, and are used to assign back the
4182 -- resulting values of such parameters.
4184 -- Note: the reason that we insert a block here is that in the context
4185 -- of selects, conditional entry calls etc. the entry call statement
4186 -- appears on its own, not as an element of a list.
4188 -- A protected entry call is converted to a Protected_Entry_Call:
4190 -- declare
4191 -- P : E1_Params := (param, param, param);
4192 -- Pnn : Boolean;
4193 -- Bnn : Communications_Block;
4195 -- declare
4196 -- P : E1_Params := (param, param, param);
4197 -- Bnn : Communications_Block;
4199 -- begin
4200 -- Protected_Entry_Call (
4201 -- Object => po._object'Access,
4202 -- E => <entry index>;
4203 -- Uninterpreted_Data => P'Address;
4204 -- Mode => Simple_Call;
4205 -- Block => Bnn);
4206 -- parm := P.param;
4207 -- parm := P.param;
4208 -- ...
4209 -- end;
4211 procedure Build_Simple_Entry_Call
4212 (N : Node_Id;
4213 Concval : Node_Id;
4214 Ename : Node_Id;
4215 Index : Node_Id)
4217 begin
4218 Expand_Call (N);
4220 -- If call has been inlined, nothing left to do
4222 if Nkind (N) = N_Block_Statement then
4223 return;
4224 end if;
4226 -- Convert entry call to Call_Simple call
4228 declare
4229 Loc : constant Source_Ptr := Sloc (N);
4230 Parms : constant List_Id := Parameter_Associations (N);
4231 Stats : constant List_Id := New_List;
4232 Actual : Node_Id;
4233 Call : Node_Id;
4234 Comm_Name : Entity_Id;
4235 Conctyp : Node_Id;
4236 Decls : List_Id;
4237 Ent : Entity_Id;
4238 Ent_Acc : Entity_Id;
4239 Formal : Node_Id;
4240 Iface_Tag : Entity_Id;
4241 Iface_Typ : Entity_Id;
4242 N_Node : Node_Id;
4243 N_Var : Node_Id;
4244 P : Entity_Id;
4245 Parm1 : Node_Id;
4246 Parm2 : Node_Id;
4247 Parm3 : Node_Id;
4248 Pdecl : Node_Id;
4249 Plist : List_Id;
4250 X : Entity_Id;
4251 Xdecl : Node_Id;
4253 begin
4254 -- Simple entry and entry family cases merge here
4256 Ent := Entity (Ename);
4257 Ent_Acc := Entry_Parameters_Type (Ent);
4258 Conctyp := Etype (Concval);
4260 -- Special case for protected subprogram calls
4262 if Is_Protected_Type (Conctyp)
4263 and then Is_Subprogram (Entity (Ename))
4264 then
4265 if not Is_Eliminated (Entity (Ename)) then
4266 Build_Protected_Subprogram_Call
4267 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4268 Analyze (N);
4269 end if;
4271 return;
4272 end if;
4274 -- First parameter is the Task_Id value from the task value or the
4275 -- Object from the protected object value, obtained by selecting
4276 -- the _Task_Id or _Object from the result of doing an unchecked
4277 -- conversion to convert the value to the corresponding record type.
4279 if Nkind (Concval) = N_Function_Call
4280 and then Is_Task_Type (Conctyp)
4281 and then Ada_Version >= Ada_2005
4282 then
4283 declare
4284 ExpR : constant Node_Id := Relocate_Node (Concval);
4285 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4286 Decl : Node_Id;
4288 begin
4289 Decl :=
4290 Make_Object_Declaration (Loc,
4291 Defining_Identifier => Obj,
4292 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4293 Expression => ExpR);
4294 Set_Etype (Obj, Conctyp);
4295 Decls := New_List (Decl);
4296 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4297 end;
4299 else
4300 Decls := New_List;
4301 end if;
4303 Parm1 := Concurrent_Ref (Concval);
4305 -- Second parameter is the entry index, computed by the routine
4306 -- provided for this purpose. The value of this expression is
4307 -- assigned to an intermediate variable to assure that any entry
4308 -- family index expressions are evaluated before the entry
4309 -- parameters.
4311 if not Is_Protected_Type (Conctyp)
4312 or else
4313 Corresponding_Runtime_Package (Conctyp) =
4314 System_Tasking_Protected_Objects_Entries
4315 then
4316 X := Make_Defining_Identifier (Loc, Name_uX);
4318 Xdecl :=
4319 Make_Object_Declaration (Loc,
4320 Defining_Identifier => X,
4321 Object_Definition =>
4322 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4323 Expression => Actual_Index_Expression (
4324 Loc, Entity (Ename), Index, Concval));
4326 Append_To (Decls, Xdecl);
4327 Parm2 := New_Occurrence_Of (X, Loc);
4329 else
4330 Xdecl := Empty;
4331 Parm2 := Empty;
4332 end if;
4334 -- The third parameter is the packaged parameters. If there are
4335 -- none, then it is just the null address, since nothing is passed.
4337 if No (Parms) then
4338 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4339 P := Empty;
4341 -- Case of parameters present, where third argument is the address
4342 -- of a packaged record containing the required parameter values.
4344 else
4345 -- First build a list of parameter values, which are references to
4346 -- objects of the parameter types.
4348 Plist := New_List;
4350 Actual := First_Actual (N);
4351 Formal := First_Formal (Ent);
4352 while Present (Actual) loop
4354 -- If it is a by-copy type, copy it to a new variable. The
4355 -- packaged record has a field that points to this variable.
4357 if Is_By_Copy_Type (Etype (Actual)) then
4358 N_Node :=
4359 Make_Object_Declaration (Loc,
4360 Defining_Identifier => Make_Temporary (Loc, 'J'),
4361 Aliased_Present => True,
4362 Object_Definition =>
4363 New_Occurrence_Of (Etype (Formal), Loc));
4365 -- Mark the object as not needing initialization since the
4366 -- initialization is performed separately, avoiding errors
4367 -- on cases such as formals of null-excluding access types.
4369 Set_No_Initialization (N_Node);
4371 -- We must make a separate assignment statement for the
4372 -- case of limited types. We cannot assign it unless the
4373 -- Assignment_OK flag is set first. An out formal of an
4374 -- access type or whose type has a Default_Value must also
4375 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4376 -- but no constraint, predicate, or null-exclusion check is
4377 -- applied before the call.
4379 if Ekind (Formal) /= E_Out_Parameter
4380 or else Is_Access_Type (Etype (Formal))
4381 or else
4382 (Is_Scalar_Type (Etype (Formal))
4383 and then
4384 Present (Default_Aspect_Value (Etype (Formal))))
4385 then
4386 N_Var :=
4387 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4388 Set_Assignment_OK (N_Var);
4389 Append_To (Stats,
4390 Make_Assignment_Statement (Loc,
4391 Name => N_Var,
4392 Expression => Relocate_Node (Actual)));
4394 -- Mark the object as internal, so we don't later reset
4395 -- No_Initialization flag in Default_Initialize_Object,
4396 -- which would lead to needless default initialization.
4397 -- We don't set this outside the if statement, because
4398 -- out scalar parameters without Default_Value do require
4399 -- default initialization if Initialize_Scalars applies.
4401 Set_Is_Internal (Defining_Identifier (N_Node));
4403 -- If actual is an out parameter of a null-excluding
4404 -- access type, there is access check on entry, so set
4405 -- Suppress_Assignment_Checks on the generated statement
4406 -- that assigns the actual to the parameter block.
4408 Set_Suppress_Assignment_Checks (Last (Stats));
4409 end if;
4411 Append (N_Node, Decls);
4413 Append_To (Plist,
4414 Make_Attribute_Reference (Loc,
4415 Attribute_Name => Name_Unchecked_Access,
4416 Prefix =>
4417 New_Occurrence_Of
4418 (Defining_Identifier (N_Node), Loc)));
4420 else
4421 -- Interface class-wide formal
4423 if Ada_Version >= Ada_2005
4424 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4425 and then Is_Interface (Etype (Formal))
4426 then
4427 Iface_Typ := Etype (Etype (Formal));
4429 -- Generate:
4430 -- formal_iface_type! (actual.iface_tag)'reference
4432 Iface_Tag :=
4433 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4434 pragma Assert (Present (Iface_Tag));
4436 Append_To (Plist,
4437 Make_Reference (Loc,
4438 Unchecked_Convert_To (Iface_Typ,
4439 Make_Selected_Component (Loc,
4440 Prefix =>
4441 Relocate_Node (Actual),
4442 Selector_Name =>
4443 New_Occurrence_Of (Iface_Tag, Loc)))));
4444 else
4445 -- Generate:
4446 -- actual'reference
4448 Append_To (Plist,
4449 Make_Reference (Loc, Relocate_Node (Actual)));
4450 end if;
4451 end if;
4453 Next_Actual (Actual);
4454 Next_Formal_With_Extras (Formal);
4455 end loop;
4457 -- Now build the declaration of parameters initialized with the
4458 -- aggregate containing this constructed parameter list.
4460 P := Make_Defining_Identifier (Loc, Name_uP);
4462 Pdecl :=
4463 Make_Object_Declaration (Loc,
4464 Defining_Identifier => P,
4465 Object_Definition =>
4466 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4467 Expression =>
4468 Make_Aggregate (Loc, Expressions => Plist));
4470 Parm3 :=
4471 Make_Attribute_Reference (Loc,
4472 Prefix => New_Occurrence_Of (P, Loc),
4473 Attribute_Name => Name_Address);
4475 Append (Pdecl, Decls);
4476 end if;
4478 -- Now we can create the call, case of protected type
4480 if Is_Protected_Type (Conctyp) then
4481 case Corresponding_Runtime_Package (Conctyp) is
4482 when System_Tasking_Protected_Objects_Entries =>
4484 -- Change the type of the index declaration
4486 Set_Object_Definition (Xdecl,
4487 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4489 -- Some additional declarations for protected entry calls
4491 if No (Decls) then
4492 Decls := New_List;
4493 end if;
4495 -- Bnn : Communications_Block;
4497 Comm_Name := Make_Temporary (Loc, 'B');
4499 Append_To (Decls,
4500 Make_Object_Declaration (Loc,
4501 Defining_Identifier => Comm_Name,
4502 Object_Definition =>
4503 New_Occurrence_Of
4504 (RTE (RE_Communication_Block), Loc)));
4506 -- Some additional statements for protected entry calls
4508 -- Protected_Entry_Call
4509 -- (Object => po._object'Access,
4510 -- E => <entry index>;
4511 -- Uninterpreted_Data => P'Address;
4512 -- Mode => Simple_Call;
4513 -- Block => Bnn);
4515 Call :=
4516 Make_Procedure_Call_Statement (Loc,
4517 Name =>
4518 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4520 Parameter_Associations => New_List (
4521 Make_Attribute_Reference (Loc,
4522 Attribute_Name => Name_Unchecked_Access,
4523 Prefix => Parm1),
4524 Parm2,
4525 Parm3,
4526 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4527 New_Occurrence_Of (Comm_Name, Loc)));
4529 when System_Tasking_Protected_Objects_Single_Entry =>
4531 -- Protected_Single_Entry_Call
4532 -- (Object => po._object'Access,
4533 -- Uninterpreted_Data => P'Address);
4535 Call :=
4536 Make_Procedure_Call_Statement (Loc,
4537 Name =>
4538 New_Occurrence_Of
4539 (RTE (RE_Protected_Single_Entry_Call), Loc),
4541 Parameter_Associations => New_List (
4542 Make_Attribute_Reference (Loc,
4543 Attribute_Name => Name_Unchecked_Access,
4544 Prefix => Parm1),
4545 Parm3));
4547 when others =>
4548 raise Program_Error;
4549 end case;
4551 -- Case of task type
4553 else
4554 Call :=
4555 Make_Procedure_Call_Statement (Loc,
4556 Name =>
4557 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4558 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4560 end if;
4562 Append_To (Stats, Call);
4564 -- If there are out or in/out parameters by copy add assignment
4565 -- statements for the result values.
4567 if Present (Parms) then
4568 Actual := First_Actual (N);
4569 Formal := First_Formal (Ent);
4571 Set_Assignment_OK (Actual);
4572 while Present (Actual) loop
4573 if Is_By_Copy_Type (Etype (Actual))
4574 and then Ekind (Formal) /= E_In_Parameter
4575 then
4576 N_Node :=
4577 Make_Assignment_Statement (Loc,
4578 Name => New_Copy (Actual),
4579 Expression =>
4580 Make_Explicit_Dereference (Loc,
4581 Make_Selected_Component (Loc,
4582 Prefix => New_Occurrence_Of (P, Loc),
4583 Selector_Name =>
4584 Make_Identifier (Loc, Chars (Formal)))));
4586 -- In all cases (including limited private types) we want
4587 -- the assignment to be valid.
4589 Set_Assignment_OK (Name (N_Node));
4591 -- If the call is the triggering alternative in an
4592 -- asynchronous select, or the entry_call alternative of a
4593 -- conditional entry call, the assignments for in-out
4594 -- parameters are incorporated into the statement list that
4595 -- follows, so that there are executed only if the entry
4596 -- call succeeds.
4598 if (Nkind (Parent (N)) = N_Triggering_Alternative
4599 and then N = Triggering_Statement (Parent (N)))
4600 or else
4601 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4602 and then N = Entry_Call_Statement (Parent (N)))
4603 then
4604 if No (Statements (Parent (N))) then
4605 Set_Statements (Parent (N), New_List);
4606 end if;
4608 Prepend (N_Node, Statements (Parent (N)));
4610 else
4611 Insert_After (Call, N_Node);
4612 end if;
4613 end if;
4615 Next_Actual (Actual);
4616 Next_Formal_With_Extras (Formal);
4617 end loop;
4618 end if;
4620 -- Finally, create block and analyze it
4622 Rewrite (N,
4623 Make_Block_Statement (Loc,
4624 Declarations => Decls,
4625 Handled_Statement_Sequence =>
4626 Make_Handled_Sequence_Of_Statements (Loc,
4627 Statements => Stats)));
4629 Analyze (N);
4630 end;
4631 end Build_Simple_Entry_Call;
4633 --------------------------------
4634 -- Build_Task_Activation_Call --
4635 --------------------------------
4637 procedure Build_Task_Activation_Call (N : Node_Id) is
4638 function Activation_Call_Loc return Source_Ptr;
4639 -- Find a suitable source location for the activation call
4641 -------------------------
4642 -- Activation_Call_Loc --
4643 -------------------------
4645 function Activation_Call_Loc return Source_Ptr is
4646 begin
4647 -- The activation call must carry the location of the "end" keyword
4648 -- when the context is a package declaration.
4650 if Nkind (N) = N_Package_Declaration then
4651 return End_Keyword_Location (N);
4653 -- Otherwise the activation call must carry the location of the
4654 -- "begin" keyword.
4656 else
4657 return Begin_Keyword_Location (N);
4658 end if;
4659 end Activation_Call_Loc;
4661 -- Local variables
4663 Chain : Entity_Id;
4664 Call : Node_Id;
4665 Loc : Source_Ptr;
4666 Name : Node_Id;
4667 Owner : Node_Id;
4668 Stmt : Node_Id;
4670 -- Start of processing for Build_Task_Activation_Call
4672 begin
4673 -- For sequential elaboration policy, all the tasks will be activated at
4674 -- the end of the elaboration.
4676 if Partition_Elaboration_Policy = 'S' then
4677 return;
4679 -- Do not create an activation call for a package spec if the package
4680 -- has a completing body. The activation call will be inserted after
4681 -- the "begin" of the body.
4683 elsif Nkind (N) = N_Package_Declaration
4684 and then Present (Corresponding_Body (N))
4685 then
4686 return;
4687 end if;
4689 -- Obtain the activation chain entity. Block statements, entry bodies,
4690 -- subprogram bodies, and task bodies keep the entity in their nodes.
4691 -- Package bodies on the other hand store it in the declaration of the
4692 -- corresponding package spec.
4694 Owner := N;
4696 if Nkind (Owner) = N_Package_Body then
4697 Owner := Unit_Declaration_Node (Corresponding_Spec (Owner));
4698 end if;
4700 Chain := Activation_Chain_Entity (Owner);
4702 -- Nothing to do when there are no tasks to activate. This is indicated
4703 -- by a missing activation chain entity; also skip generating it when
4704 -- it is a ghost entity.
4706 if No (Chain) or else Is_Ignored_Ghost_Entity (Chain) then
4707 return;
4709 -- The availability of the activation chain entity does not ensure
4710 -- that we have tasks to activate because it may have been declared
4711 -- by the frontend to pass a required extra formal to a build-in-place
4712 -- subprogram call. If we are within the scope of a protected type and
4713 -- pragma Detect_Blocking is active we can assume that no tasks will be
4714 -- activated; if tasks are created in a protected object and this pragma
4715 -- is active then the frontend emits a warning and Program_Error is
4716 -- raised at runtime.
4718 elsif Detect_Blocking and then Within_Protected_Type (Current_Scope) then
4719 return;
4720 end if;
4722 -- The location of the activation call must be as close as possible to
4723 -- the intended semantic location of the activation because the ABE
4724 -- mechanism relies heavily on accurate locations.
4726 Loc := Activation_Call_Loc;
4728 if Restricted_Profile then
4729 Name := New_Occurrence_Of (RTE (RE_Activate_Restricted_Tasks), Loc);
4730 else
4731 Name := New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc);
4732 end if;
4734 Call :=
4735 Make_Procedure_Call_Statement (Loc,
4736 Name => Name,
4737 Parameter_Associations =>
4738 New_List (Make_Attribute_Reference (Loc,
4739 Prefix => New_Occurrence_Of (Chain, Loc),
4740 Attribute_Name => Name_Unchecked_Access)));
4742 if Nkind (N) = N_Package_Declaration then
4743 if Present (Private_Declarations (Specification (N))) then
4744 Append (Call, Private_Declarations (Specification (N)));
4745 else
4746 Append (Call, Visible_Declarations (Specification (N)));
4747 end if;
4749 else
4750 -- The call goes at the start of the statement sequence after the
4751 -- start of exception range label if one is present.
4753 if Present (Handled_Statement_Sequence (N)) then
4754 Stmt := First (Statements (Handled_Statement_Sequence (N)));
4756 -- A special case, skip exception range label if one is present
4757 -- (from front end zcx processing).
4759 if Nkind (Stmt) = N_Label and then Exception_Junk (Stmt) then
4760 Next (Stmt);
4761 end if;
4763 -- Another special case, if the first statement is a block from
4764 -- optimization of a local raise to a goto, then the call goes
4765 -- inside this block.
4767 if Nkind (Stmt) = N_Block_Statement
4768 and then Exception_Junk (Stmt)
4769 then
4770 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
4771 end if;
4773 -- Insertion point is after any exception label pushes, since we
4774 -- want it covered by any local handlers.
4776 while Nkind (Stmt) in N_Push_xxx_Label loop
4777 Next (Stmt);
4778 end loop;
4780 -- Now we have the proper insertion point
4782 Insert_Before (Stmt, Call);
4784 else
4785 Set_Handled_Statement_Sequence (N,
4786 Make_Handled_Sequence_Of_Statements (Loc,
4787 Statements => New_List (Call)));
4788 end if;
4789 end if;
4791 Analyze (Call);
4793 if Legacy_Elaboration_Checks then
4794 Check_Task_Activation (N);
4795 end if;
4796 end Build_Task_Activation_Call;
4798 -------------------------------
4799 -- Build_Task_Allocate_Block --
4800 -------------------------------
4802 procedure Build_Task_Allocate_Block
4803 (Actions : List_Id;
4804 N : Node_Id;
4805 Args : List_Id)
4807 T : constant Entity_Id := Entity (Expression (N));
4808 Init : constant Entity_Id := Base_Init_Proc (T);
4809 Loc : constant Source_Ptr := Sloc (N);
4810 Chain : constant Entity_Id :=
4811 Make_Defining_Identifier (Loc, Name_uChain);
4812 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4813 Block : Node_Id;
4815 begin
4816 Block :=
4817 Make_Block_Statement (Loc,
4818 Identifier => New_Occurrence_Of (Blkent, Loc),
4819 Declarations => New_List (
4821 -- _Chain : Activation_Chain;
4823 Make_Object_Declaration (Loc,
4824 Defining_Identifier => Chain,
4825 Aliased_Present => True,
4826 Object_Definition =>
4827 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4829 Handled_Statement_Sequence =>
4830 Make_Handled_Sequence_Of_Statements (Loc,
4832 Statements => New_List (
4834 -- Init (Args);
4836 Make_Procedure_Call_Statement (Loc,
4837 Name => New_Occurrence_Of (Init, Loc),
4838 Parameter_Associations => Args),
4840 -- Activate_Tasks (_Chain);
4842 Make_Procedure_Call_Statement (Loc,
4843 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4844 Parameter_Associations => New_List (
4845 Make_Attribute_Reference (Loc,
4846 Prefix => New_Occurrence_Of (Chain, Loc),
4847 Attribute_Name => Name_Unchecked_Access))))),
4849 Has_Created_Identifier => True,
4850 Is_Task_Allocation_Block => True);
4852 Append_To (Actions,
4853 Make_Implicit_Label_Declaration (Loc,
4854 Defining_Identifier => Blkent,
4855 Label_Construct => Block));
4857 Append_To (Actions, Block);
4859 Set_Activation_Chain_Entity (Block, Chain);
4860 end Build_Task_Allocate_Block;
4862 -----------------------------------------------
4863 -- Build_Task_Allocate_Block_With_Init_Stmts --
4864 -----------------------------------------------
4866 procedure Build_Task_Allocate_Block_With_Init_Stmts
4867 (Actions : List_Id;
4868 N : Node_Id;
4869 Init_Stmts : List_Id)
4871 Loc : constant Source_Ptr := Sloc (N);
4872 Chain : constant Entity_Id :=
4873 Make_Defining_Identifier (Loc, Name_uChain);
4874 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4875 Block : Node_Id;
4877 begin
4878 Append_To (Init_Stmts,
4879 Make_Procedure_Call_Statement (Loc,
4880 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4881 Parameter_Associations => New_List (
4882 Make_Attribute_Reference (Loc,
4883 Prefix => New_Occurrence_Of (Chain, Loc),
4884 Attribute_Name => Name_Unchecked_Access))));
4886 Block :=
4887 Make_Block_Statement (Loc,
4888 Identifier => New_Occurrence_Of (Blkent, Loc),
4889 Declarations => New_List (
4891 -- _Chain : Activation_Chain;
4893 Make_Object_Declaration (Loc,
4894 Defining_Identifier => Chain,
4895 Aliased_Present => True,
4896 Object_Definition =>
4897 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4899 Handled_Statement_Sequence =>
4900 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4902 Has_Created_Identifier => True,
4903 Is_Task_Allocation_Block => True);
4905 Append_To (Actions,
4906 Make_Implicit_Label_Declaration (Loc,
4907 Defining_Identifier => Blkent,
4908 Label_Construct => Block));
4910 Append_To (Actions, Block);
4912 Set_Activation_Chain_Entity (Block, Chain);
4913 end Build_Task_Allocate_Block_With_Init_Stmts;
4915 -----------------------------------
4916 -- Build_Task_Proc_Specification --
4917 -----------------------------------
4919 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4920 Loc : constant Source_Ptr := Sloc (T);
4921 Spec_Id : Entity_Id;
4923 begin
4924 -- Case of explicit task type, suffix TB
4926 if Comes_From_Source (T) then
4927 Spec_Id :=
4928 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
4930 -- Case of anonymous task type, suffix B
4932 else
4933 Spec_Id :=
4934 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
4935 end if;
4937 Set_Is_Internal (Spec_Id);
4939 -- Associate the procedure with the task, if this is the declaration
4940 -- (and not the body) of the procedure.
4942 if No (Task_Body_Procedure (T)) then
4943 Set_Task_Body_Procedure (T, Spec_Id);
4944 end if;
4946 return
4947 Make_Procedure_Specification (Loc,
4948 Defining_Unit_Name => Spec_Id,
4949 Parameter_Specifications => New_List (
4950 Make_Parameter_Specification (Loc,
4951 Defining_Identifier =>
4952 Make_Defining_Identifier (Loc, Name_uTask),
4953 Parameter_Type =>
4954 Make_Access_Definition (Loc,
4955 Subtype_Mark =>
4956 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
4957 end Build_Task_Proc_Specification;
4959 ---------------------------------------
4960 -- Build_Unprotected_Subprogram_Body --
4961 ---------------------------------------
4963 function Build_Unprotected_Subprogram_Body
4964 (N : Node_Id;
4965 Pid : Node_Id) return Node_Id
4967 Decls : constant List_Id := Declarations (N);
4969 begin
4970 -- Add renamings for the Protection object, discriminals, privals, and
4971 -- the entry index constant for use by debugger.
4973 Debug_Private_Data_Declarations (Decls);
4975 -- Make an unprotected version of the subprogram for use within the same
4976 -- object, with a new name and an additional parameter representing the
4977 -- object.
4979 return
4980 Make_Subprogram_Body (Sloc (N),
4981 Specification =>
4982 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
4983 Declarations => Decls,
4984 Handled_Statement_Sequence => Handled_Statement_Sequence (N),
4985 At_End_Proc => At_End_Proc (N));
4986 end Build_Unprotected_Subprogram_Body;
4988 ----------------------------
4989 -- Collect_Entry_Families --
4990 ----------------------------
4992 procedure Collect_Entry_Families
4993 (Loc : Source_Ptr;
4994 Cdecls : List_Id;
4995 Current_Node : in out Node_Id;
4996 Conctyp : Entity_Id)
4998 Efam : Entity_Id;
4999 Efam_Decl : Node_Id;
5000 Efam_Type : Entity_Id;
5002 begin
5003 Efam := First_Entity (Conctyp);
5004 while Present (Efam) loop
5005 if Ekind (Efam) = E_Entry_Family then
5006 Efam_Type := Make_Temporary (Loc, 'F');
5008 declare
5009 Eityp : constant Entity_Id := Entry_Index_Type (Efam);
5010 Lo : constant Node_Id := Type_Low_Bound (Eityp);
5011 Hi : constant Node_Id := Type_High_Bound (Eityp);
5012 Bdecl : Node_Id;
5013 Bityp : Entity_Id;
5015 begin
5016 Bityp := Base_Type (Eityp);
5018 if Is_Potentially_Large_Family (Bityp, Conctyp, Lo, Hi) then
5019 Bityp := Make_Temporary (Loc, 'B');
5021 Bdecl :=
5022 Make_Subtype_Declaration (Loc,
5023 Defining_Identifier => Bityp,
5024 Subtype_Indication =>
5025 Make_Subtype_Indication (Loc,
5026 Subtype_Mark =>
5027 New_Occurrence_Of (Standard_Integer, Loc),
5028 Constraint =>
5029 Make_Range_Constraint (Loc,
5030 Range_Expression => Make_Range (Loc,
5031 Make_Integer_Literal
5032 (Loc, -Entry_Family_Bound),
5033 Make_Integer_Literal
5034 (Loc, Entry_Family_Bound - 1)))));
5036 Insert_After (Current_Node, Bdecl);
5037 Current_Node := Bdecl;
5038 Analyze (Bdecl);
5039 end if;
5041 Efam_Decl :=
5042 Make_Full_Type_Declaration (Loc,
5043 Defining_Identifier => Efam_Type,
5044 Type_Definition =>
5045 Make_Unconstrained_Array_Definition (Loc,
5046 Subtype_Marks =>
5047 (New_List (New_Occurrence_Of (Bityp, Loc))),
5049 Component_Definition =>
5050 Make_Component_Definition (Loc,
5051 Aliased_Present => False,
5052 Subtype_Indication =>
5053 New_Occurrence_Of (Standard_Character, Loc))));
5054 end;
5056 Insert_After (Current_Node, Efam_Decl);
5057 Current_Node := Efam_Decl;
5058 Analyze (Efam_Decl);
5060 Append_To (Cdecls,
5061 Make_Component_Declaration (Loc,
5062 Defining_Identifier =>
5063 Make_Defining_Identifier (Loc, Chars (Efam)),
5065 Component_Definition =>
5066 Make_Component_Definition (Loc,
5067 Aliased_Present => False,
5068 Subtype_Indication =>
5069 Make_Subtype_Indication (Loc,
5070 Subtype_Mark =>
5071 New_Occurrence_Of (Efam_Type, Loc),
5073 Constraint =>
5074 Make_Index_Or_Discriminant_Constraint (Loc,
5075 Constraints => New_List (
5076 New_Occurrence_Of (Entry_Index_Type (Efam),
5077 Loc)))))));
5078 end if;
5080 Next_Entity (Efam);
5081 end loop;
5082 end Collect_Entry_Families;
5084 -----------------------
5085 -- Concurrent_Object --
5086 -----------------------
5088 function Concurrent_Object
5089 (Spec_Id : Entity_Id;
5090 Conc_Typ : Entity_Id) return Entity_Id
5092 begin
5093 -- Parameter _O or _object
5095 if Is_Protected_Type (Conc_Typ) then
5096 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5098 -- Parameter _task
5100 else
5101 pragma Assert (Is_Task_Type (Conc_Typ));
5102 return First_Formal (Task_Body_Procedure (Conc_Typ));
5103 end if;
5104 end Concurrent_Object;
5106 ----------------------
5107 -- Copy_Result_Type --
5108 ----------------------
5110 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5111 New_Res : constant Node_Id := New_Copy_Tree (Res);
5112 Par_Spec : Node_Id;
5113 Formal : Entity_Id;
5115 begin
5116 -- If the result type is an access_to_subprogram, we must create new
5117 -- entities for its spec.
5119 if Nkind (New_Res) = N_Access_Definition
5120 and then Present (Access_To_Subprogram_Definition (New_Res))
5121 then
5122 -- Provide new entities for the formals
5124 Par_Spec := First (Parameter_Specifications
5125 (Access_To_Subprogram_Definition (New_Res)));
5126 while Present (Par_Spec) loop
5127 Formal := Defining_Identifier (Par_Spec);
5128 Set_Defining_Identifier (Par_Spec,
5129 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5130 Next (Par_Spec);
5131 end loop;
5132 end if;
5134 return New_Res;
5135 end Copy_Result_Type;
5137 --------------------
5138 -- Concurrent_Ref --
5139 --------------------
5141 -- The expression returned for a reference to a concurrent object has the
5142 -- form:
5144 -- taskV!(name)._Task_Id
5146 -- for a task, and
5148 -- objectV!(name)._Object
5150 -- for a protected object. For the case of an access to a concurrent
5151 -- object, there is an extra explicit dereference:
5153 -- taskV!(name.all)._Task_Id
5154 -- objectV!(name.all)._Object
5156 -- here taskV and objectV are the types for the associated records, which
5157 -- contain the required _Task_Id and _Object fields for tasks and protected
5158 -- objects, respectively.
5160 -- For the case of a task type name, the expression is
5162 -- Self;
5164 -- i.e. a call to the Self function which returns precisely this Task_Id
5166 -- For the case of a protected type name, the expression is
5168 -- objectR
5170 -- which is a renaming of the _object field of the current object
5171 -- record, passed into protected operations as a parameter.
5173 function Concurrent_Ref (N : Node_Id) return Node_Id is
5174 Loc : constant Source_Ptr := Sloc (N);
5175 Ntyp : constant Entity_Id := Etype (N);
5176 Dtyp : Entity_Id;
5177 Sel : Name_Id;
5179 function Is_Current_Task (T : Entity_Id) return Boolean;
5180 -- Check whether the reference is to the immediately enclosing task
5181 -- type, or to an outer one (rare but legal).
5183 ---------------------
5184 -- Is_Current_Task --
5185 ---------------------
5187 function Is_Current_Task (T : Entity_Id) return Boolean is
5188 Scop : Entity_Id;
5190 begin
5191 Scop := Current_Scope;
5192 while Present (Scop) and then Scop /= Standard_Standard loop
5193 if Scop = T then
5194 return True;
5196 elsif Is_Task_Type (Scop) then
5197 return False;
5199 -- If this is a procedure nested within the task type, we must
5200 -- assume that it can be called from an inner task, and therefore
5201 -- cannot treat it as a local reference.
5203 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5204 return False;
5206 else
5207 Scop := Scope (Scop);
5208 end if;
5209 end loop;
5211 -- We know that we are within the task body, so should have found it
5212 -- in scope.
5214 raise Program_Error;
5215 end Is_Current_Task;
5217 -- Start of processing for Concurrent_Ref
5219 begin
5220 if Is_Access_Type (Ntyp) then
5221 Dtyp := Designated_Type (Ntyp);
5223 if Is_Protected_Type (Dtyp) then
5224 Sel := Name_uObject;
5225 else
5226 Sel := Name_uTask_Id;
5227 end if;
5229 return
5230 Make_Selected_Component (Loc,
5231 Prefix =>
5232 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5233 Make_Explicit_Dereference (Loc, N)),
5234 Selector_Name => Make_Identifier (Loc, Sel));
5236 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5237 if Is_Task_Type (Entity (N)) then
5239 if Is_Current_Task (Entity (N)) then
5240 return
5241 Make_Function_Call (Loc,
5242 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5244 else
5245 declare
5246 Decl : Node_Id;
5247 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5248 T_Body : constant Node_Id :=
5249 Parent (Corresponding_Body (Parent (Entity (N))));
5251 begin
5252 Decl :=
5253 Make_Object_Declaration (Loc,
5254 Defining_Identifier => T_Self,
5255 Object_Definition =>
5256 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5257 Expression =>
5258 Make_Function_Call (Loc,
5259 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5260 Prepend (Decl, Declarations (T_Body));
5261 Analyze (Decl);
5262 Set_Scope (T_Self, Entity (N));
5263 return New_Occurrence_Of (T_Self, Loc);
5264 end;
5265 end if;
5267 else
5268 pragma Assert (Is_Protected_Type (Entity (N)));
5270 return
5271 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5272 end if;
5274 else
5275 if Is_Protected_Type (Ntyp) then
5276 Sel := Name_uObject;
5277 elsif Is_Task_Type (Ntyp) then
5278 Sel := Name_uTask_Id;
5279 else
5280 raise Program_Error;
5281 end if;
5283 return
5284 Make_Selected_Component (Loc,
5285 Prefix =>
5286 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5287 New_Copy_Tree (N)),
5288 Selector_Name => Make_Identifier (Loc, Sel));
5289 end if;
5290 end Concurrent_Ref;
5292 ------------------------
5293 -- Convert_Concurrent --
5294 ------------------------
5296 function Convert_Concurrent
5297 (N : Node_Id;
5298 Typ : Entity_Id) return Node_Id
5300 begin
5301 if not Is_Concurrent_Type (Typ) then
5302 return N;
5303 else
5304 return
5305 Unchecked_Convert_To
5306 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5307 end if;
5308 end Convert_Concurrent;
5310 -------------------------------------
5311 -- Create_Secondary_Stack_For_Task --
5312 -------------------------------------
5314 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean is
5315 begin
5316 return
5317 (Restriction_Active (No_Implicit_Heap_Allocations)
5318 or else Restriction_Active (No_Implicit_Task_Allocations))
5319 and then not Restriction_Active (No_Secondary_Stack)
5320 and then Has_Rep_Pragma
5321 (T, Name_Secondary_Stack_Size, Check_Parents => False);
5322 end Create_Secondary_Stack_For_Task;
5324 -------------------------------------
5325 -- Debug_Private_Data_Declarations --
5326 -------------------------------------
5328 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5329 Debug_Nod : Node_Id;
5330 Decl : Node_Id;
5332 begin
5333 Decl := First (Decls);
5334 while Present (Decl) and then not Comes_From_Source (Decl) loop
5336 -- Declaration for concurrent entity _object and its access type,
5337 -- along with the entry index subtype:
5338 -- type prot_typVP is access prot_typV;
5339 -- _object : prot_typVP := prot_typV (_O);
5340 -- subtype Jnn is <Type of Index> range Low .. High;
5342 if Nkind (Decl) in N_Full_Type_Declaration | N_Object_Declaration then
5343 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5345 -- Declaration for the Protection object, discriminals, privals, and
5346 -- entry index constant:
5347 -- conc_typR : protection_typ renames _object._object;
5348 -- discr_nameD : discr_typ renames _object.discr_name;
5349 -- discr_nameD : discr_typ renames _task.discr_name;
5350 -- prival_name : comp_typ renames _object.comp_name;
5351 -- J : constant Jnn :=
5352 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5354 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5355 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5356 Debug_Nod := Debug_Renaming_Declaration (Decl);
5358 if Present (Debug_Nod) then
5359 Insert_After (Decl, Debug_Nod);
5360 end if;
5361 end if;
5363 Next (Decl);
5364 end loop;
5365 end Debug_Private_Data_Declarations;
5367 ------------------------------
5368 -- Ensure_Statement_Present --
5369 ------------------------------
5371 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5372 Stmt : Node_Id;
5374 begin
5375 if Opt.Suppress_Control_Flow_Optimizations
5376 and then Is_Empty_List (Statements (Alt))
5377 then
5378 Stmt := Make_Null_Statement (Loc);
5380 -- Mark NULL statement as coming from source so that it is not
5381 -- eliminated by GIGI.
5383 -- Another covert channel. If this is a requirement, it must be
5384 -- documented in sinfo/einfo ???
5386 Set_Comes_From_Source (Stmt, True);
5388 Set_Statements (Alt, New_List (Stmt));
5389 end if;
5390 end Ensure_Statement_Present;
5392 ----------------------------
5393 -- Entry_Index_Expression --
5394 ----------------------------
5396 function Entry_Index_Expression
5397 (Sloc : Source_Ptr;
5398 Ent : Entity_Id;
5399 Index : Node_Id;
5400 Ttyp : Entity_Id) return Node_Id
5402 Expr : Node_Id;
5403 Num : Node_Id;
5404 Lo : Node_Id;
5405 Hi : Node_Id;
5406 Prev : Entity_Id;
5407 S : Node_Id;
5409 begin
5410 -- The queues of entries and entry families appear in textual order in
5411 -- the associated record. The entry index is computed as the sum of the
5412 -- number of queues for all entries that precede the designated one, to
5413 -- which is added the index expression, if this expression denotes a
5414 -- member of a family.
5416 -- The following is a place holder for the count of simple entries
5418 Num := Make_Integer_Literal (Sloc, 1);
5420 -- We construct an expression which is a series of addition operations.
5421 -- The first operand is the number of single entries that precede this
5422 -- one, the second operand is the index value relative to the start of
5423 -- the referenced family, and the remaining operands are the lengths of
5424 -- the entry families that precede this entry, i.e. the constructed
5425 -- expression is:
5427 -- number_simple_entries +
5428 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5429 -- family'length + ...
5431 -- where index-value is the given index value, and s is the index
5432 -- subtype (we have to use pos because the subtype might be an
5433 -- enumeration type preventing direct subtraction). Note that the task
5434 -- entry array is one-indexed.
5436 -- The upper bound of the entry family may be a discriminant, so we
5437 -- retrieve the lower bound explicitly to compute offset, rather than
5438 -- using the index subtype which may mention a discriminant.
5440 if Present (Index) then
5441 S := Entry_Index_Type (Ent);
5443 -- First make sure the index is in range if requested. The index type
5444 -- is the pristine Entry_Index_Type of the entry.
5446 if Do_Range_Check (Index) then
5447 Generate_Range_Check (Index, S, CE_Range_Check_Failed);
5448 end if;
5450 Expr :=
5451 Make_Op_Add (Sloc,
5452 Left_Opnd => Num,
5453 Right_Opnd =>
5454 Family_Offset
5455 (Sloc,
5456 Make_Attribute_Reference (Sloc,
5457 Attribute_Name => Name_Pos,
5458 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5459 Expressions => New_List (Relocate_Node (Index))),
5460 Type_Low_Bound (S),
5461 Ttyp,
5462 False));
5463 else
5464 Expr := Num;
5465 end if;
5467 -- Now add lengths of preceding entries and entry families
5469 Prev := First_Entity (Ttyp);
5470 while Chars (Prev) /= Chars (Ent)
5471 or else (Ekind (Prev) /= Ekind (Ent))
5472 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5473 loop
5474 if Ekind (Prev) = E_Entry then
5475 Set_Intval (Num, Intval (Num) + 1);
5477 elsif Ekind (Prev) = E_Entry_Family then
5478 S := Entry_Index_Type (Prev);
5479 Lo := Type_Low_Bound (S);
5480 Hi := Type_High_Bound (S);
5482 Expr :=
5483 Make_Op_Add (Sloc,
5484 Left_Opnd => Expr,
5485 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5487 -- Other components are anonymous types to be ignored
5489 else
5490 null;
5491 end if;
5493 Next_Entity (Prev);
5494 end loop;
5496 return Expr;
5497 end Entry_Index_Expression;
5499 ---------------------------
5500 -- Establish_Task_Master --
5501 ---------------------------
5503 procedure Establish_Task_Master (N : Node_Id) is
5504 Call : Node_Id;
5506 begin
5507 if Restriction_Active (No_Task_Hierarchy) = False then
5508 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5510 -- The block may have no declarations (and nevertheless be a task
5511 -- master) if it contains a call that may return an object that
5512 -- contains tasks.
5514 if No (Declarations (N)) then
5515 Set_Declarations (N, New_List (Call));
5516 else
5517 Prepend_To (Declarations (N), Call);
5518 end if;
5520 Analyze (Call);
5521 end if;
5522 end Establish_Task_Master;
5524 --------------------------------
5525 -- Expand_Accept_Declarations --
5526 --------------------------------
5528 -- Part of the expansion of an accept statement involves the creation of
5529 -- a declaration that can be referenced from the statement sequence of
5530 -- the accept:
5532 -- Ann : Address;
5534 -- This declaration is inserted immediately before the accept statement
5535 -- and it is important that it be inserted before the statements of the
5536 -- statement sequence are analyzed. Thus it would be too late to create
5537 -- this declaration in the Expand_N_Accept_Statement routine, which is
5538 -- why there is a separate procedure to be called directly from Sem_Ch9.
5540 -- Ann is used to hold the address of the record containing the parameters
5541 -- (see Expand_N_Entry_Call for more details on how this record is built).
5542 -- References to the parameters do an unchecked conversion of this address
5543 -- to a pointer to the required record type, and then access the field that
5544 -- holds the value of the required parameter. The entity for the address
5545 -- variable is held as the top stack element (i.e. the last element) of the
5546 -- Accept_Address stack in the corresponding entry entity, and this element
5547 -- must be set in place before the statements are processed.
5549 -- The above description applies to the case of a stand alone accept
5550 -- statement, i.e. one not appearing as part of a select alternative.
5552 -- For the case of an accept that appears as part of a select alternative
5553 -- of a selective accept, we must still create the declaration right away,
5554 -- since Ann is needed immediately, but there is an important difference:
5556 -- The declaration is inserted before the selective accept, not before
5557 -- the accept statement (which is not part of a list anyway, and so would
5558 -- not accommodate inserted declarations)
5560 -- We only need one address variable for the entire selective accept. So
5561 -- the Ann declaration is created only for the first accept alternative,
5562 -- and subsequent accept alternatives reference the same Ann variable.
5564 -- We can distinguish the two cases by seeing whether the accept statement
5565 -- is part of a list. If not, then it must be in an accept alternative.
5567 -- To expand the requeue statement, a label is provided at the end of the
5568 -- accept statement or alternative of which it is a part, so that the
5569 -- statement can be skipped after the requeue is complete. This label is
5570 -- created here rather than during the expansion of the accept statement,
5571 -- because it will be needed by any requeue statements within the accept,
5572 -- which are expanded before the accept.
5574 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5575 Loc : constant Source_Ptr := Sloc (N);
5576 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5577 Ann : Entity_Id := Empty;
5578 Adecl : Node_Id;
5579 Lab : Node_Id;
5580 Ldecl : Node_Id;
5581 Ldecl2 : Node_Id;
5583 begin
5584 if Expander_Active then
5586 -- If we have no handled statement sequence, we may need to build
5587 -- a dummy sequence consisting of a null statement. This can be
5588 -- skipped if the trivial accept optimization is permitted.
5590 if not Trivial_Accept_OK
5591 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5592 then
5593 Set_Handled_Statement_Sequence (N,
5594 Make_Handled_Sequence_Of_Statements (Loc,
5595 Statements => New_List (Make_Null_Statement (Loc))));
5596 end if;
5598 -- Create and declare two labels to be placed at the end of the
5599 -- accept statement. The first label is used to allow requeues to
5600 -- skip the remainder of entry processing. The second label is used
5601 -- to skip the remainder of entry processing if the rendezvous
5602 -- completes in the middle of the accept body.
5604 if Present (Handled_Statement_Sequence (N)) then
5605 declare
5606 Ent : Entity_Id;
5608 begin
5609 Ent := Make_Temporary (Loc, 'L');
5610 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5611 Ldecl :=
5612 Make_Implicit_Label_Declaration (Loc,
5613 Defining_Identifier => Ent,
5614 Label_Construct => Lab);
5615 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5617 Ent := Make_Temporary (Loc, 'L');
5618 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5619 Ldecl2 :=
5620 Make_Implicit_Label_Declaration (Loc,
5621 Defining_Identifier => Ent,
5622 Label_Construct => Lab);
5623 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5624 end;
5626 else
5627 Ldecl := Empty;
5628 Ldecl2 := Empty;
5629 end if;
5631 -- Case of stand alone accept statement
5633 if Is_List_Member (N) then
5635 if Present (Handled_Statement_Sequence (N)) then
5636 Ann := Make_Temporary (Loc, 'A');
5638 Adecl :=
5639 Make_Object_Declaration (Loc,
5640 Defining_Identifier => Ann,
5641 Object_Definition =>
5642 New_Occurrence_Of (RTE (RE_Address), Loc));
5644 Insert_Before_And_Analyze (N, Adecl);
5645 Insert_Before_And_Analyze (N, Ldecl);
5646 Insert_Before_And_Analyze (N, Ldecl2);
5647 end if;
5649 -- Case of accept statement which is in an accept alternative
5651 else
5652 declare
5653 Acc_Alt : constant Node_Id := Parent (N);
5654 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5655 Alt : Node_Id;
5657 begin
5658 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5659 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5661 -- ??? Consider a single label for select statements
5663 if Present (Handled_Statement_Sequence (N)) then
5664 Prepend (Ldecl2,
5665 Statements (Handled_Statement_Sequence (N)));
5666 Analyze (Ldecl2);
5668 Prepend (Ldecl,
5669 Statements (Handled_Statement_Sequence (N)));
5670 Analyze (Ldecl);
5671 end if;
5673 -- Find first accept alternative of the selective accept. A
5674 -- valid selective accept must have at least one accept in it.
5676 Alt := First (Select_Alternatives (Sel_Acc));
5678 while Nkind (Alt) /= N_Accept_Alternative loop
5679 Next (Alt);
5680 end loop;
5682 -- If this is the first accept statement, then we have to
5683 -- create the Ann variable, as for the stand alone case, except
5684 -- that it is inserted before the selective accept. Similarly,
5685 -- a label for requeue expansion must be declared.
5687 if N = Accept_Statement (Alt) then
5688 Ann := Make_Temporary (Loc, 'A');
5689 Adecl :=
5690 Make_Object_Declaration (Loc,
5691 Defining_Identifier => Ann,
5692 Object_Definition =>
5693 New_Occurrence_Of (RTE (RE_Address), Loc));
5695 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5697 -- If this is not the first accept statement, then find the Ann
5698 -- variable allocated by the first accept and use it.
5700 else
5701 Ann :=
5702 Node (Last_Elmt (Accept_Address
5703 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5704 end if;
5705 end;
5706 end if;
5708 -- Merge here with Ann either created or referenced, and Adecl
5709 -- pointing to the corresponding declaration. Remaining processing
5710 -- is the same for the two cases.
5712 if Present (Ann) then
5713 Append_Elmt (Ann, Accept_Address (Ent));
5714 Set_Debug_Info_Needed (Ann);
5715 end if;
5717 -- Create renaming declarations for the entry formals. Each reference
5718 -- to a formal becomes a dereference of a component of the parameter
5719 -- block, whose address is held in Ann. These declarations are
5720 -- eventually inserted into the accept block, and analyzed there so
5721 -- that they have the proper scope for gdb and do not conflict with
5722 -- other declarations.
5724 if Present (Parameter_Specifications (N))
5725 and then Present (Handled_Statement_Sequence (N))
5726 then
5727 declare
5728 Comp : Entity_Id;
5729 Decl : Node_Id;
5730 Formal : Entity_Id;
5731 New_F : Entity_Id;
5732 Renamed_Formal : Node_Id;
5734 begin
5735 Push_Scope (Ent);
5736 Formal := First_Formal (Ent);
5738 while Present (Formal) loop
5739 Comp := Entry_Component (Formal);
5740 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5742 Set_Etype (New_F, Etype (Formal));
5743 Set_Scope (New_F, Ent);
5745 -- Now we set debug info needed on New_F even though it does
5746 -- not come from source, so that the debugger will get the
5747 -- right information for these generated names.
5749 Set_Debug_Info_Needed (New_F);
5751 if Ekind (Formal) = E_In_Parameter then
5752 Mutate_Ekind (New_F, E_Constant);
5753 else
5754 Mutate_Ekind (New_F, E_Variable);
5755 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5756 end if;
5758 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5760 Renamed_Formal :=
5761 Make_Selected_Component (Loc,
5762 Prefix =>
5763 Make_Explicit_Dereference (Loc,
5764 Unchecked_Convert_To (
5765 Entry_Parameters_Type (Ent),
5766 New_Occurrence_Of (Ann, Loc))),
5767 Selector_Name =>
5768 New_Occurrence_Of (Comp, Loc));
5770 Decl :=
5771 Build_Renamed_Formal_Declaration
5772 (New_F, Formal, Comp, Renamed_Formal);
5774 if No (Declarations (N)) then
5775 Set_Declarations (N, New_List);
5776 end if;
5778 Append (Decl, Declarations (N));
5779 Set_Renamed_Object (Formal, New_F);
5780 Next_Formal (Formal);
5781 end loop;
5783 End_Scope;
5784 end;
5785 end if;
5786 end if;
5787 end Expand_Accept_Declarations;
5789 ---------------------------------------------
5790 -- Expand_Access_Protected_Subprogram_Type --
5791 ---------------------------------------------
5793 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5794 Loc : constant Source_Ptr := Sloc (N);
5795 T : constant Entity_Id := Defining_Identifier (N);
5796 D_T : constant Entity_Id := Designated_Type (T);
5797 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5798 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5799 P_List : constant List_Id :=
5800 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
5802 Comps : List_Id;
5803 Decl1 : Node_Id;
5804 Decl2 : Node_Id;
5805 Def1 : Node_Id;
5807 begin
5808 -- Create access to subprogram with full signature
5810 if Etype (D_T) /= Standard_Void_Type then
5811 Def1 :=
5812 Make_Access_Function_Definition (Loc,
5813 Parameter_Specifications => P_List,
5814 Result_Definition =>
5815 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5817 else
5818 Def1 :=
5819 Make_Access_Procedure_Definition (Loc,
5820 Parameter_Specifications => P_List);
5821 end if;
5823 Decl1 :=
5824 Make_Full_Type_Declaration (Loc,
5825 Defining_Identifier => D_T2,
5826 Type_Definition => Def1);
5828 -- Declare the new types before the original one since the latter will
5829 -- refer to them through the Equivalent_Type slot.
5831 Insert_Before_And_Analyze (N, Decl1);
5833 -- Associate the access to subprogram with its original access to
5834 -- protected subprogram type. Needed by the backend to know that this
5835 -- type corresponds with an access to protected subprogram type.
5837 Set_Original_Access_Type (D_T2, T);
5839 -- Create Equivalent_Type, a record with two components for an access to
5840 -- object and an access to subprogram.
5842 Comps := New_List (
5843 Make_Component_Declaration (Loc,
5844 Defining_Identifier => Make_Temporary (Loc, 'P'),
5845 Component_Definition =>
5846 Make_Component_Definition (Loc,
5847 Aliased_Present => False,
5848 Subtype_Indication =>
5849 New_Occurrence_Of (RTE (RE_Address), Loc))),
5851 Make_Component_Declaration (Loc,
5852 Defining_Identifier => Make_Temporary (Loc, 'S'),
5853 Component_Definition =>
5854 Make_Component_Definition (Loc,
5855 Aliased_Present => False,
5856 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5858 Decl2 :=
5859 Make_Full_Type_Declaration (Loc,
5860 Defining_Identifier => E_T,
5861 Type_Definition =>
5862 Make_Record_Definition (Loc,
5863 Component_List =>
5864 Make_Component_List (Loc, Component_Items => Comps)));
5866 Insert_Before_And_Analyze (N, Decl2);
5867 Set_Equivalent_Type (T, E_T);
5868 end Expand_Access_Protected_Subprogram_Type;
5870 --------------------------
5871 -- Expand_Entry_Barrier --
5872 --------------------------
5874 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5875 Cond : constant Node_Id := Condition (Entry_Body_Formal_Part (N));
5876 Prot : constant Entity_Id := Scope (Ent);
5877 Spec_Decl : constant Node_Id := Parent (Prot);
5879 Func_Id : Entity_Id := Empty;
5880 -- The entity of the barrier function
5882 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
5883 -- Check whether entity in Barrier is external to protected type.
5884 -- If so, barrier may not be properly synchronized.
5886 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
5887 -- Check whether N meets the Pure_Barriers restriction. Return OK if
5888 -- so.
5890 function Is_Simple_Barrier (N : Node_Id) return Boolean;
5891 -- Check whether N meets the Simple_Barriers restriction. Return OK if
5892 -- so.
5894 ----------------------
5895 -- Is_Global_Entity --
5896 ----------------------
5898 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
5899 E : Entity_Id;
5900 S : Entity_Id;
5902 begin
5903 if Is_Entity_Name (N) and then Present (Entity (N)) then
5904 E := Entity (N);
5905 S := Scope (E);
5907 if Ekind (E) = E_Variable then
5909 -- If the variable is local to the barrier function generated
5910 -- during expansion, it is ok. If expansion is not performed,
5911 -- then Func is Empty so this test cannot succeed.
5913 if Scope (E) = Func_Id then
5914 null;
5916 -- A protected call from a barrier to another object is ok
5918 elsif Ekind (Etype (E)) = E_Protected_Type then
5919 null;
5921 -- If the variable is within the package body we consider
5922 -- this safe. This is a common (if dubious) idiom.
5924 elsif S = Scope (Prot)
5925 and then Is_Package_Or_Generic_Package (S)
5926 and then Nkind (Parent (E)) = N_Object_Declaration
5927 and then Nkind (Parent (Parent (E))) = N_Package_Body
5928 then
5929 null;
5931 else
5932 Error_Msg_N ("potentially unsynchronized barrier??", N);
5933 Error_Msg_N ("\& should be private component of type??", N);
5934 end if;
5935 end if;
5936 end if;
5938 return OK;
5939 end Is_Global_Entity;
5941 procedure Check_Unprotected_Barrier is
5942 new Traverse_Proc (Is_Global_Entity);
5944 -----------------------
5945 -- Is_Simple_Barrier --
5946 -----------------------
5948 function Is_Simple_Barrier (N : Node_Id) return Boolean is
5949 Renamed : Node_Id;
5951 begin
5952 if Is_Static_Expression (N) then
5953 return True;
5954 elsif Ada_Version >= Ada_2022
5955 and then Nkind (N) in N_Selected_Component | N_Indexed_Component
5956 and then Statically_Names_Object (N)
5957 then
5958 -- Restriction relaxed in Ada 2022 to allow statically named
5959 -- subcomponents.
5960 return Is_Simple_Barrier (Prefix (N));
5961 end if;
5963 -- Check if the name is a component of the protected object. If
5964 -- the expander is active, the component has been transformed into a
5965 -- renaming of _object.all.component. Original_Node is needed in case
5966 -- validity checking is enabled, in which case the simple object
5967 -- reference will have been rewritten.
5969 if Expander_Active then
5971 -- The expanded name may have been constant folded in which case
5972 -- the original node is not necessarily an entity name (e.g. an
5973 -- indexed component).
5975 if not Is_Entity_Name (Original_Node (N)) then
5976 return False;
5977 end if;
5979 Renamed := Renamed_Object (Entity (Original_Node (N)));
5981 return
5982 Present (Renamed)
5983 and then Nkind (Renamed) = N_Selected_Component
5984 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
5985 elsif not Is_Entity_Name (N) then
5986 return False;
5987 else
5988 return Is_Protected_Component (Entity (N));
5989 end if;
5990 end Is_Simple_Barrier;
5992 ---------------------
5993 -- Is_Pure_Barrier --
5994 ---------------------
5996 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
5997 begin
5998 case Nkind (N) is
5999 when N_Expanded_Name
6000 | N_Identifier
6003 -- Because of N_Expanded_Name case, return Skip instead of OK.
6005 if No (Entity (N)) then
6006 return Abandon;
6008 elsif Is_Numeric_Type (Entity (N)) then
6009 return Skip;
6010 end if;
6012 case Ekind (Entity (N)) is
6013 when E_Constant
6014 | E_Discriminant
6016 return Skip;
6018 when E_Enumeration_Literal
6019 | E_Named_Integer
6020 | E_Named_Real
6022 if not Is_OK_Static_Expression (N) then
6023 return Abandon;
6024 end if;
6025 return Skip;
6027 when E_Component =>
6028 return Skip;
6030 when E_Variable =>
6031 if Is_Simple_Barrier (N) then
6032 return Skip;
6033 end if;
6035 when E_Function =>
6037 -- The count attribute has been transformed into run-time
6038 -- calls.
6040 if Is_RTE (Entity (N), RE_Protected_Count)
6041 or else Is_RTE (Entity (N), RE_Protected_Count_Entry)
6042 then
6043 return Skip;
6044 end if;
6046 when others =>
6047 null;
6048 end case;
6050 when N_Function_Call =>
6052 -- Function call checks are carried out as part of the analysis
6053 -- of the function call name.
6055 return OK;
6057 when N_Character_Literal
6058 | N_Integer_Literal
6059 | N_Real_Literal
6061 return OK;
6063 when N_Op_Boolean
6064 | N_Op_Not
6066 if Ekind (Entity (N)) = E_Operator then
6067 return OK;
6068 end if;
6070 when N_Short_Circuit
6071 | N_If_Expression
6072 | N_Case_Expression
6074 return OK;
6076 when N_Indexed_Component | N_Selected_Component =>
6077 if Statically_Names_Object (N) then
6078 return Is_Pure_Barrier (Prefix (N));
6079 else
6080 return Abandon;
6081 end if;
6083 when N_Case_Expression_Alternative =>
6084 -- do not traverse Discrete_Choices subtree
6085 if Is_Pure_Barrier (Expression (N)) /= Abandon then
6086 return Skip;
6087 end if;
6089 when N_Expression_With_Actions =>
6090 -- this may occur in the case of a Count attribute reference
6091 if Is_Rewrite_Substitution (N)
6092 and then Is_Pure_Barrier (Original_Node (N)) /= Abandon
6093 then
6094 return Skip;
6095 end if;
6097 when N_Membership_Test =>
6098 if Is_Pure_Barrier (Left_Opnd (N)) /= Abandon
6099 and then All_Membership_Choices_Static (N)
6100 then
6101 return Skip;
6102 end if;
6104 when N_Type_Conversion =>
6106 -- Conversions to Universal_Integer do not raise constraint
6107 -- errors. Likewise if the expression's type is statically
6108 -- compatible with the target's type.
6110 if Etype (N) = Universal_Integer
6111 or else Subtypes_Statically_Compatible
6112 (Etype (Expression (N)), Etype (N))
6113 then
6114 return OK;
6115 end if;
6117 when N_Unchecked_Type_Conversion =>
6118 return OK;
6120 when others =>
6121 null;
6122 end case;
6124 return Abandon;
6125 end Is_Pure_Barrier;
6127 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6129 -- Local variables
6131 Cond_Id : Entity_Id;
6132 Entry_Body : Node_Id;
6133 Func_Body : Node_Id := Empty;
6135 -- Start of processing for Expand_Entry_Barrier
6137 begin
6138 if No_Run_Time_Mode then
6139 Error_Msg_CRT ("entry barrier", N);
6140 return;
6141 end if;
6143 -- Prevent cascaded errors
6145 if Nkind (Cond) = N_Error then
6146 return;
6147 end if;
6149 -- The body of the entry barrier must be analyzed in the context of the
6150 -- protected object, but its scope is external to it, just as any other
6151 -- unprotected version of a protected operation. The specification has
6152 -- been produced when the protected type declaration was elaborated. We
6153 -- build the body, insert it in the enclosing scope, but analyze it in
6154 -- the current context. A more uniform approach would be to treat the
6155 -- barrier just as a protected function, and discard the protected
6156 -- version of it because it is never called.
6158 if Expander_Active then
6159 Func_Body := Build_Barrier_Function (N, Ent, Prot);
6160 Func_Id := Barrier_Function (Ent);
6161 Set_Corresponding_Spec (Func_Body, Func_Id);
6163 Entry_Body := Parent (Corresponding_Body (Spec_Decl));
6165 if Nkind (Parent (Entry_Body)) = N_Subunit then
6166 Entry_Body := Corresponding_Stub (Parent (Entry_Body));
6167 end if;
6169 Insert_Before_And_Analyze (Entry_Body, Func_Body);
6171 Set_Discriminals (Spec_Decl);
6172 Set_Scope (Func_Id, Scope (Prot));
6174 else
6175 Analyze_And_Resolve (Cond, Any_Boolean);
6176 end if;
6178 -- Check Simple_Barriers and Pure_Barriers restrictions.
6179 -- Note that it is safe to be calling Check_Restriction from here, even
6180 -- though this is part of the expander, since Expand_Entry_Barrier is
6181 -- called from Sem_Ch9 even in -gnatc mode.
6183 if not Is_Simple_Barrier (Cond) then
6184 -- flag restriction violation
6185 Check_Restriction (Simple_Barriers, Cond);
6186 end if;
6188 if Check_Pure_Barriers (Cond) = Abandon then
6189 -- flag restriction violation
6190 Check_Restriction (Pure_Barriers, Cond);
6192 -- Emit warning if barrier contains global entities and is thus
6193 -- potentially unsynchronized (if Pure_Barriers restrictions
6194 -- are met then no need to check for this).
6195 Check_Unprotected_Barrier (Cond);
6196 end if;
6198 if Is_Entity_Name (Cond) then
6199 Cond_Id := Entity (Cond);
6201 -- Perform a small optimization of simple barrier functions. If the
6202 -- scope of the condition's entity is not the barrier function, then
6203 -- the condition does not depend on any of the generated renamings.
6204 -- If this is the case, eliminate the renamings as they are useless.
6205 -- This optimization is not performed when the condition was folded
6206 -- and validity checks are in effect because the original condition
6207 -- may have produced at least one check that depends on the generated
6208 -- renamings.
6210 if Expander_Active
6211 and then Scope (Cond_Id) /= Func_Id
6212 and then not Validity_Check_Operands
6213 then
6214 Set_Declarations (Func_Body, Empty_List);
6215 end if;
6217 -- Note that after analysis variables in this context will be
6218 -- replaced by the corresponding prival, that is to say a renaming
6219 -- of a selected component of the form _Object.Var. If expansion is
6220 -- disabled, as within a generic, we check that the entity appears in
6221 -- the current scope.
6222 end if;
6223 end Expand_Entry_Barrier;
6225 ------------------------------
6226 -- Expand_N_Abort_Statement --
6227 ------------------------------
6229 -- Expand abort T1, T2, .. Tn; into:
6230 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6232 procedure Expand_N_Abort_Statement (N : Node_Id) is
6233 Loc : constant Source_Ptr := Sloc (N);
6234 Tlist : constant List_Id := Names (N);
6235 Count : Nat;
6236 Aggr : Node_Id;
6237 Tasknm : Node_Id;
6239 begin
6240 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6241 Count := 0;
6243 Tasknm := First (Tlist);
6245 while Present (Tasknm) loop
6246 Count := Count + 1;
6248 -- A task interface class-wide type object is being aborted. Retrieve
6249 -- its _task_id by calling a dispatching routine.
6251 if Ada_Version >= Ada_2005
6252 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6253 and then Is_Interface (Etype (Tasknm))
6254 and then Is_Task_Interface (Etype (Tasknm))
6255 then
6256 Append_To (Component_Associations (Aggr),
6257 Make_Component_Association (Loc,
6258 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6259 Expression =>
6261 -- Task_Id (Tasknm._disp_get_task_id)
6263 Unchecked_Convert_To
6264 (RTE (RO_ST_Task_Id),
6265 Make_Selected_Component (Loc,
6266 Prefix => New_Copy_Tree (Tasknm),
6267 Selector_Name =>
6268 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6270 else
6271 Append_To (Component_Associations (Aggr),
6272 Make_Component_Association (Loc,
6273 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6274 Expression => Concurrent_Ref (Tasknm)));
6275 end if;
6277 Next (Tasknm);
6278 end loop;
6280 Rewrite (N,
6281 Make_Procedure_Call_Statement (Loc,
6282 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6283 Parameter_Associations => New_List (
6284 Make_Qualified_Expression (Loc,
6285 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6286 Expression => Aggr))));
6288 Analyze (N);
6289 end Expand_N_Abort_Statement;
6291 -------------------------------
6292 -- Expand_N_Accept_Statement --
6293 -------------------------------
6295 -- This procedure handles expansion of accept statements that stand alone,
6296 -- i.e. they are not part of an accept alternative. The expansion of
6297 -- accept statement in accept alternatives is handled by the routines
6298 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6299 -- following description applies only to stand alone accept statements.
6301 -- If there is no handled statement sequence, or only null statements, then
6302 -- this is called a trivial accept, and the expansion is:
6304 -- Accept_Trivial (entry-index)
6306 -- If there is a handled statement sequence, then the expansion is:
6308 -- Ann : Address;
6309 -- {Lnn : Label}
6311 -- begin
6312 -- begin
6313 -- Accept_Call (entry-index, Ann);
6314 -- Renaming_Declarations for formals
6315 -- <statement sequence from N_Accept_Statement node>
6316 -- Complete_Rendezvous;
6317 -- <<Lnn>>
6319 -- exception
6320 -- when ... =>
6321 -- <exception handler from N_Accept_Statement node>
6322 -- Complete_Rendezvous;
6323 -- when ... =>
6324 -- <exception handler from N_Accept_Statement node>
6325 -- Complete_Rendezvous;
6326 -- ...
6327 -- end;
6329 -- exception
6330 -- when all others =>
6331 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6332 -- end;
6334 -- The first three declarations were already inserted ahead of the accept
6335 -- statement by the Expand_Accept_Declarations procedure, which was called
6336 -- directly from the semantics during analysis of the accept statement,
6337 -- before analyzing its contained statements.
6339 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6340 -- from possible expansion activity (the original source of course does
6341 -- not have any declarations associated with the accept statement, since
6342 -- an accept statement has no declarative part). In particular, if the
6343 -- expander is active, the first such declaration is the declaration of
6344 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6346 -- The two blocks are merged into a single block if the inner block has
6347 -- no exception handlers, but otherwise two blocks are required, since
6348 -- exceptions might be raised in the exception handlers of the inner
6349 -- block, and Exceptional_Complete_Rendezvous must be called.
6351 procedure Expand_N_Accept_Statement (N : Node_Id) is
6352 Loc : constant Source_Ptr := Sloc (N);
6353 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6354 Ename : constant Node_Id := Entry_Direct_Name (N);
6355 Eindx : constant Node_Id := Entry_Index (N);
6356 Eent : constant Entity_Id := Entity (Ename);
6357 Acstack : constant Elist_Id := Accept_Address (Eent);
6358 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6359 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6360 Blkent : Entity_Id;
6361 Call : Node_Id;
6362 Block : Node_Id;
6364 begin
6365 -- If the accept statement is not part of a list, then its parent must
6366 -- be an accept alternative, and, as described above, we do not do any
6367 -- expansion for such accept statements at this level.
6369 if not Is_List_Member (N) then
6370 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6371 return;
6373 -- Trivial accept case (no statement sequence, or null statements).
6374 -- If the accept statement has declarations, then just insert them
6375 -- before the procedure call.
6377 elsif Trivial_Accept_OK
6378 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6379 then
6380 -- Remove declarations for renamings, because the parameter block
6381 -- will not be assigned.
6383 declare
6384 D : Node_Id;
6385 Next_D : Node_Id;
6387 begin
6388 D := First (Declarations (N));
6389 while Present (D) loop
6390 Next_D := Next (D);
6391 if Nkind (D) = N_Object_Renaming_Declaration then
6392 Remove (D);
6393 end if;
6395 D := Next_D;
6396 end loop;
6397 end;
6399 if Present (Declarations (N)) then
6400 Insert_Actions (N, Declarations (N));
6401 end if;
6403 Rewrite (N,
6404 Make_Procedure_Call_Statement (Loc,
6405 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6406 Parameter_Associations => New_List (
6407 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6409 Analyze (N);
6411 -- Ada 2022 (AI12-0279)
6413 if Has_Yield_Aspect (Eent)
6414 and then RTE_Available (RE_Yield)
6415 then
6416 Insert_Action_After (N,
6417 Make_Procedure_Call_Statement (Loc,
6418 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6419 end if;
6421 -- Discard Entry_Address that was created for it, so it will not be
6422 -- emitted if this accept statement is in the statement part of a
6423 -- delay alternative.
6425 if Present (Stats) then
6426 Remove_Last_Elmt (Acstack);
6427 end if;
6429 -- Case of statement sequence present
6431 else
6432 -- Construct the block, using the declarations from the accept
6433 -- statement if any to initialize the declarations of the block.
6435 Blkent := Make_Temporary (Loc, 'A');
6436 Mutate_Ekind (Blkent, E_Block);
6437 Set_Etype (Blkent, Standard_Void_Type);
6438 Set_Scope (Blkent, Current_Scope);
6440 Block :=
6441 Make_Block_Statement (Loc,
6442 Identifier => New_Occurrence_Of (Blkent, Loc),
6443 Declarations => Declarations (N),
6444 Handled_Statement_Sequence => Build_Accept_Body (N));
6446 -- Reset the Scope of local entities associated with the accept
6447 -- statement (that currently reference the entry scope) to the
6448 -- block scope, to avoid having references to the locals treated
6449 -- as up-level references.
6451 Reset_Scopes_To (Block, Blkent);
6453 -- For the analysis of the generated declarations, the parent node
6454 -- must be properly set.
6456 Set_Parent (Block, Parent (N));
6457 Set_Parent (Blkent, Block);
6459 -- Prepend call to Accept_Call to main statement sequence If the
6460 -- accept has exception handlers, the statement sequence is wrapped
6461 -- in a block. Insert call and renaming declarations in the
6462 -- declarations of the block, so they are elaborated before the
6463 -- handlers.
6465 Call :=
6466 Make_Procedure_Call_Statement (Loc,
6467 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6468 Parameter_Associations => New_List (
6469 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6470 New_Occurrence_Of (Ann, Loc)));
6472 if Parent (Stats) = N then
6473 Prepend (Call, Statements (Stats));
6474 else
6475 Set_Declarations (Parent (Stats), New_List (Call));
6476 end if;
6478 Analyze (Call);
6480 Push_Scope (Blkent);
6482 declare
6483 D : Node_Id;
6484 Next_D : Node_Id;
6485 Typ : Entity_Id;
6487 begin
6488 D := First (Declarations (N));
6489 while Present (D) loop
6490 Next_D := Next (D);
6492 if Nkind (D) = N_Object_Renaming_Declaration then
6494 -- The renaming declarations for the formals were created
6495 -- during analysis of the accept statement, and attached to
6496 -- the list of declarations. Place them now in the context
6497 -- of the accept block or subprogram.
6499 Remove (D);
6500 Typ := Entity (Subtype_Mark (D));
6501 Insert_After (Call, D);
6502 Analyze (D);
6504 -- If the formal is class_wide, it does not have an actual
6505 -- subtype. The analysis of the renaming declaration creates
6506 -- one, but we need to retain the class-wide nature of the
6507 -- entity.
6509 if Is_Class_Wide_Type (Typ) then
6510 Set_Etype (Defining_Identifier (D), Typ);
6511 end if;
6513 end if;
6515 D := Next_D;
6516 end loop;
6517 end;
6519 End_Scope;
6521 -- Replace the accept statement by the new block
6523 Rewrite (N, Block);
6524 Analyze (N);
6526 -- Last step is to unstack the Accept_Address value
6528 Remove_Last_Elmt (Acstack);
6529 end if;
6530 end Expand_N_Accept_Statement;
6532 ----------------------------------
6533 -- Expand_N_Asynchronous_Select --
6534 ----------------------------------
6536 -- This procedure assumes that the trigger statement is an entry call or
6537 -- a dispatching procedure call. A delay alternative should already have
6538 -- been expanded into an entry call to the appropriate delay object Wait
6539 -- entry.
6541 -- If the trigger is a task entry call, the select is implemented with
6542 -- a Task_Entry_Call:
6544 -- declare
6545 -- B : Boolean;
6546 -- C : Boolean;
6547 -- P : parms := (parm, parm, parm);
6549 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6551 -- procedure _clean is
6552 -- begin
6553 -- ...
6554 -- Cancel_Task_Entry_Call (C);
6555 -- ...
6556 -- end _clean;
6558 -- begin
6559 -- Abort_Defer;
6560 -- Task_Entry_Call
6561 -- (<acceptor-task>, -- Acceptor
6562 -- <entry-index>, -- E
6563 -- P'Address, -- Uninterpreted_Data
6564 -- Asynchronous_Call, -- Mode
6565 -- B); -- Rendezvous_Successful
6567 -- begin
6568 -- begin
6569 -- Abort_Undefer;
6570 -- <abortable-part>
6571 -- at end
6572 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6573 -- end;
6574 -- exception
6575 -- when Abort_Signal => Abort_Undefer;
6576 -- end;
6578 -- parm := P.param;
6579 -- parm := P.param;
6580 -- ...
6581 -- if not C then
6582 -- <triggered-statements>
6583 -- end if;
6584 -- end;
6586 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6587 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6588 -- as follows:
6590 -- declare
6591 -- P : parms := (parm, parm, parm);
6592 -- begin
6593 -- Call_Simple (acceptor-task, entry-index, P'Address);
6594 -- parm := P.param;
6595 -- parm := P.param;
6596 -- ...
6597 -- end;
6599 -- so the task at hand is to convert the latter expansion into the former
6601 -- If the trigger is a protected entry call, the select is implemented
6602 -- with Protected_Entry_Call:
6604 -- declare
6605 -- P : E1_Params := (param, param, param);
6606 -- Bnn : Communications_Block;
6608 -- begin
6609 -- declare
6611 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6613 -- procedure _clean is
6614 -- begin
6615 -- ...
6616 -- if Enqueued (Bnn) then
6617 -- Cancel_Protected_Entry_Call (Bnn);
6618 -- end if;
6619 -- ...
6620 -- end _clean;
6622 -- begin
6623 -- begin
6624 -- Protected_Entry_Call
6625 -- (po._object'Access, -- Object
6626 -- <entry index>, -- E
6627 -- P'Address, -- Uninterpreted_Data
6628 -- Asynchronous_Call, -- Mode
6629 -- Bnn); -- Block
6631 -- if Enqueued (Bnn) then
6632 -- <abortable-part>
6633 -- end if;
6634 -- at end
6635 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6636 -- end;
6637 -- exception
6638 -- when Abort_Signal => Abort_Undefer;
6639 -- end;
6641 -- if not Cancelled (Bnn) then
6642 -- <triggered-statements>
6643 -- end if;
6644 -- end;
6646 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6647 -- entry call:
6649 -- declare
6650 -- P : E1_Params := (param, param, param);
6651 -- Bnn : Communications_Block;
6653 -- begin
6654 -- Protected_Entry_Call
6655 -- (po._object'Access, -- Object
6656 -- <entry index>, -- E
6657 -- P'Address, -- Uninterpreted_Data
6658 -- Simple_Call, -- Mode
6659 -- Bnn); -- Block
6660 -- parm := P.param;
6661 -- parm := P.param;
6662 -- ...
6663 -- end;
6665 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6666 -- expanded into:
6668 -- declare
6669 -- B : Boolean := False;
6670 -- Bnn : Communication_Block;
6671 -- C : Ada.Tags.Prim_Op_Kind;
6672 -- D : System.Storage_Elements.Dummy_Communication_Block;
6673 -- K : Ada.Tags.Tagged_Kind :=
6674 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6675 -- P : Parameters := (Param1 .. ParamN);
6676 -- S : Integer;
6677 -- U : Boolean;
6679 -- begin
6680 -- if K = Ada.Tags.TK_Limited_Tagged
6681 -- or else K = Ada.Tags.TK_Tagged
6682 -- then
6683 -- <dispatching-call>;
6684 -- <triggering-statements>;
6686 -- else
6687 -- S :=
6688 -- Ada.Tags.Get_Offset_Index
6689 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6691 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6693 -- if C = POK_Protected_Entry then
6694 -- declare
6695 -- procedure _clean is
6696 -- begin
6697 -- if Enqueued (Bnn) then
6698 -- Cancel_Protected_Entry_Call (Bnn);
6699 -- end if;
6700 -- end _clean;
6702 -- begin
6703 -- begin
6704 -- _Disp_Asynchronous_Select
6705 -- (<object>, S, P'Address, D, B);
6706 -- Bnn := Communication_Block (D);
6708 -- Param1 := P.Param1;
6709 -- ...
6710 -- ParamN := P.ParamN;
6712 -- if Enqueued (Bnn) then
6713 -- <abortable-statements>
6714 -- end if;
6715 -- at end
6716 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6717 -- end;
6718 -- exception
6719 -- when Abort_Signal => Abort_Undefer;
6720 -- end;
6722 -- if not Cancelled (Bnn) then
6723 -- <triggering-statements>
6724 -- end if;
6726 -- elsif C = POK_Task_Entry then
6727 -- declare
6728 -- procedure _clean is
6729 -- begin
6730 -- Cancel_Task_Entry_Call (U);
6731 -- end _clean;
6733 -- begin
6734 -- Abort_Defer;
6736 -- _Disp_Asynchronous_Select
6737 -- (<object>, S, P'Address, D, B);
6738 -- Bnn := Communication_Bloc (D);
6740 -- Param1 := P.Param1;
6741 -- ...
6742 -- ParamN := P.ParamN;
6744 -- begin
6745 -- begin
6746 -- Abort_Undefer;
6747 -- <abortable-statements>
6748 -- at end
6749 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6750 -- end;
6751 -- exception
6752 -- when Abort_Signal => Abort_Undefer;
6753 -- end;
6755 -- if not U then
6756 -- <triggering-statements>
6757 -- end if;
6758 -- end;
6760 -- else
6761 -- <dispatching-call>;
6762 -- <triggering-statements>
6763 -- end if;
6764 -- end if;
6765 -- end;
6767 -- The job is to convert this to the asynchronous form
6769 -- If the trigger is a delay statement, it will have been expanded into
6770 -- a call to one of the GNARL delay procedures. This routine will convert
6771 -- this into a protected entry call on a delay object and then continue
6772 -- processing as for a protected entry call trigger. This requires
6773 -- declaring a Delay_Block object and adding a pointer to this object to
6774 -- the parameter list of the delay procedure to form the parameter list of
6775 -- the entry call. This object is used by the runtime to queue the delay
6776 -- request.
6778 -- For a description of the use of P and the assignments after the call,
6779 -- see Expand_N_Entry_Call_Statement.
6781 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6782 Loc : constant Source_Ptr := Sloc (N);
6783 Abrt : constant Node_Id := Abortable_Part (N);
6784 Trig : constant Node_Id := Triggering_Alternative (N);
6786 Abort_Block_Ent : Entity_Id;
6787 Abortable_Block : Node_Id;
6788 Actuals : List_Id;
6789 Astats : List_Id;
6790 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6791 Blk_Typ : Entity_Id;
6792 Call : Node_Id;
6793 Call_Ent : Entity_Id;
6794 Cancel_Param : Entity_Id;
6795 Cleanup_Block : Node_Id;
6796 Cleanup_Block_Ent : Entity_Id;
6797 Cleanup_Stmts : List_Id;
6798 Conc_Typ_Stmts : List_Id;
6799 Concval : Node_Id;
6800 Dblock_Ent : Entity_Id;
6801 Decl : Node_Id;
6802 Decls : List_Id;
6803 Ecall : Node_Id;
6804 Ename : Node_Id;
6805 Enqueue_Call : Node_Id;
6806 Formals : List_Id;
6807 Hdle : List_Id;
6808 Index : Node_Id;
6809 Lim_Typ_Stmts : List_Id;
6810 N_Orig : Node_Id;
6811 Obj : Entity_Id;
6812 Param : Node_Id;
6813 Params : List_Id;
6814 Pdef : Entity_Id;
6815 ProtE_Stmts : List_Id;
6816 ProtP_Stmts : List_Id;
6817 Stmt : Node_Id;
6818 Stmts : List_Id;
6819 TaskE_Stmts : List_Id;
6820 Tstats : List_Id;
6822 B : Entity_Id; -- Call status flag
6823 Bnn : Entity_Id; -- Communication block
6824 C : Entity_Id; -- Call kind
6825 K : Entity_Id; -- Tagged kind
6826 P : Entity_Id; -- Parameter block
6827 S : Entity_Id; -- Primitive operation slot
6828 T : Entity_Id; -- Additional status flag
6830 procedure Rewrite_Abortable_Part;
6831 -- If the trigger is a dispatching call, the expansion inserts multiple
6832 -- copies of the abortable part. This is both inefficient, and may lead
6833 -- to duplicate definitions that the back-end will reject, when the
6834 -- abortable part includes loops. This procedure rewrites the abortable
6835 -- part into a call to a generated procedure.
6837 ----------------------------
6838 -- Rewrite_Abortable_Part --
6839 ----------------------------
6841 procedure Rewrite_Abortable_Part is
6842 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6843 Decl : Node_Id;
6845 begin
6846 Decl :=
6847 Make_Subprogram_Body (Loc,
6848 Specification =>
6849 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6850 Declarations => New_List,
6851 Handled_Statement_Sequence =>
6852 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6853 Insert_Before (N, Decl);
6854 Analyze (Decl);
6856 -- Rewrite abortable part into a call to this procedure
6858 Astats :=
6859 New_List (
6860 Make_Procedure_Call_Statement (Loc,
6861 Name => New_Occurrence_Of (Proc, Loc)));
6862 end Rewrite_Abortable_Part;
6864 -- Start of processing for Expand_N_Asynchronous_Select
6866 begin
6867 -- Asynchronous select is not supported on restricted runtimes. Don't
6868 -- try to expand.
6870 if Restricted_Profile then
6871 return;
6872 end if;
6874 Process_Statements_For_Controlled_Objects (Trig);
6875 Process_Statements_For_Controlled_Objects (Abrt);
6877 Ecall := Triggering_Statement (Trig);
6879 Ensure_Statement_Present (Sloc (Ecall), Trig);
6881 -- Retrieve Astats and Tstats now because the finalization machinery may
6882 -- wrap them in blocks.
6884 Astats := Statements (Abrt);
6885 Tstats := Statements (Trig);
6887 -- The arguments in the call may require dynamic allocation, and the
6888 -- call statement may have been transformed into a block. The block
6889 -- may contain additional declarations for internal entities, and the
6890 -- original call is found by sequential search.
6892 if Nkind (Ecall) = N_Block_Statement then
6893 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6894 while Nkind (Ecall) not in
6895 N_Procedure_Call_Statement | N_Entry_Call_Statement
6896 loop
6897 Next (Ecall);
6898 end loop;
6899 end if;
6901 -- This is either a dispatching call or a delay statement used as a
6902 -- trigger which was expanded into a procedure call.
6904 if Nkind (Ecall) = N_Procedure_Call_Statement then
6905 if Ada_Version >= Ada_2005
6906 and then
6907 (No (Original_Node (Ecall))
6908 or else Nkind (Original_Node (Ecall)) not in N_Delay_Statement)
6909 then
6910 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6912 Rewrite_Abortable_Part;
6913 Decls := New_List;
6914 Stmts := New_List;
6916 -- Call status flag processing, generate:
6917 -- B : Boolean := False;
6919 B := Build_B (Loc, Decls);
6921 -- Communication block processing, generate:
6922 -- Bnn : Communication_Block;
6924 Bnn := Make_Temporary (Loc, 'B');
6925 Append_To (Decls,
6926 Make_Object_Declaration (Loc,
6927 Defining_Identifier => Bnn,
6928 Object_Definition =>
6929 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6931 -- Call kind processing, generate:
6932 -- C : Ada.Tags.Prim_Op_Kind;
6934 C := Build_C (Loc, Decls);
6936 -- Tagged kind processing, generate:
6937 -- K : Ada.Tags.Tagged_Kind :=
6938 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6940 -- Dummy communication block, generate:
6941 -- D : Dummy_Communication_Block;
6943 Append_To (Decls,
6944 Make_Object_Declaration (Loc,
6945 Defining_Identifier =>
6946 Make_Defining_Identifier (Loc, Name_uD),
6947 Object_Definition =>
6948 New_Occurrence_Of
6949 (RTE (RE_Dummy_Communication_Block), Loc)));
6951 K := Build_K (Loc, Decls, Obj);
6953 -- Parameter block processing
6955 Blk_Typ := Build_Parameter_Block
6956 (Loc, Actuals, Formals, Decls);
6957 P := Parameter_Block_Pack
6958 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6960 -- Dispatch table slot processing, generate:
6961 -- S : Integer;
6963 S := Build_S (Loc, Decls);
6965 -- Additional status flag processing, generate:
6966 -- Tnn : Boolean;
6968 T := Make_Temporary (Loc, 'T');
6969 Append_To (Decls,
6970 Make_Object_Declaration (Loc,
6971 Defining_Identifier => T,
6972 Object_Definition =>
6973 New_Occurrence_Of (Standard_Boolean, Loc)));
6975 ------------------------------
6976 -- Protected entry handling --
6977 ------------------------------
6979 -- Generate:
6980 -- Param1 := P.Param1;
6981 -- ...
6982 -- ParamN := P.ParamN;
6984 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6986 -- Generate:
6987 -- Bnn := Communication_Block (D);
6989 Prepend_To (Cleanup_Stmts,
6990 Make_Assignment_Statement (Loc,
6991 Name => New_Occurrence_Of (Bnn, Loc),
6992 Expression =>
6993 Unchecked_Convert_To
6994 (RTE (RE_Communication_Block),
6995 Make_Identifier (Loc, Name_uD))));
6997 -- Generate:
6998 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7000 Prepend_To (Cleanup_Stmts,
7001 Make_Procedure_Call_Statement (Loc,
7002 Name =>
7003 New_Occurrence_Of
7004 (Find_Prim_Op
7005 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7006 Loc),
7007 Parameter_Associations =>
7008 New_List (
7009 New_Copy_Tree (Obj), -- <object>
7010 New_Occurrence_Of (S, Loc), -- S
7011 Make_Attribute_Reference (Loc, -- P'Address
7012 Prefix => New_Occurrence_Of (P, Loc),
7013 Attribute_Name => Name_Address),
7014 Make_Identifier (Loc, Name_uD), -- D
7015 New_Occurrence_Of (B, Loc)))); -- B
7017 -- Generate:
7018 -- if Enqueued (Bnn) then
7019 -- <abortable-statements>
7020 -- end if;
7022 Append_To (Cleanup_Stmts,
7023 Make_Implicit_If_Statement (N,
7024 Condition =>
7025 Make_Function_Call (Loc,
7026 Name =>
7027 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7028 Parameter_Associations =>
7029 New_List (New_Occurrence_Of (Bnn, Loc))),
7031 Then_Statements =>
7032 New_Copy_List_Tree (Astats)));
7034 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7035 -- will then generate a _clean for the communication block Bnn.
7037 -- Generate:
7038 -- declare
7039 -- procedure _clean is
7040 -- begin
7041 -- if Enqueued (Bnn) then
7042 -- Cancel_Protected_Entry_Call (Bnn);
7043 -- end if;
7044 -- end _clean;
7045 -- begin
7046 -- Cleanup_Stmts
7047 -- at end
7048 -- _clean;
7049 -- end;
7051 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7052 Cleanup_Block :=
7053 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7055 -- Wrap the cleanup block in an exception handling block
7057 -- Generate:
7058 -- begin
7059 -- Cleanup_Block
7060 -- exception
7061 -- when Abort_Signal => Abort_Undefer;
7062 -- end;
7064 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7065 ProtE_Stmts :=
7066 New_List (
7067 Make_Implicit_Label_Declaration (Loc,
7068 Defining_Identifier => Abort_Block_Ent),
7070 Build_Abort_Block
7071 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7073 -- Generate:
7074 -- if not Cancelled (Bnn) then
7075 -- <triggering-statements>
7076 -- end if;
7078 Append_To (ProtE_Stmts,
7079 Make_Implicit_If_Statement (N,
7080 Condition =>
7081 Make_Op_Not (Loc,
7082 Right_Opnd =>
7083 Make_Function_Call (Loc,
7084 Name =>
7085 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7086 Parameter_Associations =>
7087 New_List (New_Occurrence_Of (Bnn, Loc)))),
7089 Then_Statements =>
7090 New_Copy_List_Tree (Tstats)));
7092 -------------------------
7093 -- Task entry handling --
7094 -------------------------
7096 -- Generate:
7097 -- Param1 := P.Param1;
7098 -- ...
7099 -- ParamN := P.ParamN;
7101 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7103 -- Generate:
7104 -- Bnn := Communication_Block (D);
7106 Append_To (TaskE_Stmts,
7107 Make_Assignment_Statement (Loc,
7108 Name =>
7109 New_Occurrence_Of (Bnn, Loc),
7110 Expression =>
7111 Unchecked_Convert_To
7112 (RTE (RE_Communication_Block),
7113 Make_Identifier (Loc, Name_uD))));
7115 -- Generate:
7116 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7118 Prepend_To (TaskE_Stmts,
7119 Make_Procedure_Call_Statement (Loc,
7120 Name =>
7121 New_Occurrence_Of (
7122 Find_Prim_Op (Etype (Etype (Obj)),
7123 Name_uDisp_Asynchronous_Select),
7124 Loc),
7126 Parameter_Associations => New_List (
7127 New_Copy_Tree (Obj), -- <object>
7128 New_Occurrence_Of (S, Loc), -- S
7129 Make_Attribute_Reference (Loc, -- P'Address
7130 Prefix => New_Occurrence_Of (P, Loc),
7131 Attribute_Name => Name_Address),
7132 Make_Identifier (Loc, Name_uD), -- D
7133 New_Occurrence_Of (B, Loc)))); -- B
7135 -- Generate:
7136 -- Abort_Defer;
7138 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7140 -- Generate:
7141 -- Abort_Undefer;
7142 -- <abortable-statements>
7144 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7146 Prepend_To
7147 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7149 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7150 -- will generate a _clean for the additional status flag.
7152 -- Generate:
7153 -- declare
7154 -- procedure _clean is
7155 -- begin
7156 -- Cancel_Task_Entry_Call (U);
7157 -- end _clean;
7158 -- begin
7159 -- Cleanup_Stmts
7160 -- at end
7161 -- _clean;
7162 -- end;
7164 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7165 Cleanup_Block :=
7166 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7168 -- Wrap the cleanup block in an exception handling block
7170 -- Generate:
7171 -- begin
7172 -- Cleanup_Block
7173 -- exception
7174 -- when Abort_Signal => Abort_Undefer;
7175 -- end;
7177 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7179 Append_To (TaskE_Stmts,
7180 Make_Implicit_Label_Declaration (Loc,
7181 Defining_Identifier => Abort_Block_Ent));
7183 Append_To (TaskE_Stmts,
7184 Build_Abort_Block
7185 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7187 -- Generate:
7188 -- if not T then
7189 -- <triggering-statements>
7190 -- end if;
7192 Append_To (TaskE_Stmts,
7193 Make_Implicit_If_Statement (N,
7194 Condition =>
7195 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7197 Then_Statements =>
7198 New_Copy_List_Tree (Tstats)));
7200 ----------------------------------
7201 -- Protected procedure handling --
7202 ----------------------------------
7204 -- Generate:
7205 -- <dispatching-call>;
7206 -- <triggering-statements>
7208 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7209 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7211 -- Generate:
7212 -- S := Ada.Tags.Get_Offset_Index
7213 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7215 Conc_Typ_Stmts :=
7216 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7218 -- Generate:
7219 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7221 Append_To (Conc_Typ_Stmts,
7222 Make_Procedure_Call_Statement (Loc,
7223 Name =>
7224 New_Occurrence_Of
7225 (Find_Prim_Op (Etype (Etype (Obj)),
7226 Name_uDisp_Get_Prim_Op_Kind),
7227 Loc),
7228 Parameter_Associations =>
7229 New_List (
7230 New_Copy_Tree (Obj),
7231 New_Occurrence_Of (S, Loc),
7232 New_Occurrence_Of (C, Loc))));
7234 -- Generate:
7235 -- if C = POK_Procedure_Entry then
7236 -- ProtE_Stmts
7237 -- elsif C = POK_Task_Entry then
7238 -- TaskE_Stmts
7239 -- else
7240 -- ProtP_Stmts
7241 -- end if;
7243 Append_To (Conc_Typ_Stmts,
7244 Make_Implicit_If_Statement (N,
7245 Condition =>
7246 Make_Op_Eq (Loc,
7247 Left_Opnd =>
7248 New_Occurrence_Of (C, Loc),
7249 Right_Opnd =>
7250 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7252 Then_Statements =>
7253 ProtE_Stmts,
7255 Elsif_Parts =>
7256 New_List (
7257 Make_Elsif_Part (Loc,
7258 Condition =>
7259 Make_Op_Eq (Loc,
7260 Left_Opnd =>
7261 New_Occurrence_Of (C, Loc),
7262 Right_Opnd =>
7263 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7265 Then_Statements =>
7266 TaskE_Stmts)),
7268 Else_Statements =>
7269 ProtP_Stmts));
7271 -- Generate:
7272 -- <dispatching-call>;
7273 -- <triggering-statements>
7275 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7276 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7278 -- Generate:
7279 -- if K = Ada.Tags.TK_Limited_Tagged
7280 -- or else K = Ada.Tags.TK_Tagged
7281 -- then
7282 -- Lim_Typ_Stmts
7283 -- else
7284 -- Conc_Typ_Stmts
7285 -- end if;
7287 Append_To (Stmts,
7288 Make_Implicit_If_Statement (N,
7289 Condition => Build_Dispatching_Tag_Check (K, N),
7290 Then_Statements => Lim_Typ_Stmts,
7291 Else_Statements => Conc_Typ_Stmts));
7293 Rewrite (N,
7294 Make_Block_Statement (Loc,
7295 Declarations =>
7296 Decls,
7297 Handled_Statement_Sequence =>
7298 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7300 Analyze (N);
7301 return;
7303 -- Delay triggering statement processing
7305 else
7306 -- Add a Delay_Block object to the parameter list of the delay
7307 -- procedure to form the parameter list of the Wait entry call.
7309 Dblock_Ent := Make_Temporary (Loc, 'D');
7311 Pdef := Entity (Name (Ecall));
7313 if Is_RTE (Pdef, RO_CA_Delay_For) then
7314 Enqueue_Call :=
7315 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7317 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7318 Enqueue_Call :=
7319 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7321 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7322 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7323 end if;
7325 Append_To (Parameter_Associations (Ecall),
7326 Make_Attribute_Reference (Loc,
7327 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7328 Attribute_Name => Name_Unchecked_Access));
7330 -- Create the inner block to protect the abortable part
7332 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7334 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7336 Abortable_Block :=
7337 Make_Block_Statement (Loc,
7338 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7339 Handled_Statement_Sequence =>
7340 Make_Handled_Sequence_Of_Statements (Loc,
7341 Statements => Astats),
7342 Has_Created_Identifier => True,
7343 Is_Asynchronous_Call_Block => True);
7345 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7347 Rewrite (Ecall,
7348 Make_Implicit_If_Statement (N,
7349 Condition =>
7350 Make_Function_Call (Loc,
7351 Name => Enqueue_Call,
7352 Parameter_Associations => Parameter_Associations (Ecall)),
7353 Then_Statements =>
7354 New_List (Make_Block_Statement (Loc,
7355 Handled_Statement_Sequence =>
7356 Make_Handled_Sequence_Of_Statements (Loc,
7357 Statements => New_List (
7358 Make_Implicit_Label_Declaration (Loc,
7359 Defining_Identifier => Blk_Ent,
7360 Label_Construct => Abortable_Block),
7361 Abortable_Block),
7362 Exception_Handlers => Hdle)))));
7364 Stmts := New_List (Ecall);
7366 -- Construct statement sequence for new block
7368 Append_To (Stmts,
7369 Make_Implicit_If_Statement (N,
7370 Condition =>
7371 Make_Function_Call (Loc,
7372 Name => New_Occurrence_Of (
7373 RTE (RE_Timed_Out), Loc),
7374 Parameter_Associations => New_List (
7375 Make_Attribute_Reference (Loc,
7376 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7377 Attribute_Name => Name_Unchecked_Access))),
7378 Then_Statements => Tstats));
7380 -- The result is the new block
7382 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7384 Rewrite (N,
7385 Make_Block_Statement (Loc,
7386 Declarations => New_List (
7387 Make_Object_Declaration (Loc,
7388 Defining_Identifier => Dblock_Ent,
7389 Aliased_Present => True,
7390 Object_Definition =>
7391 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7393 Handled_Statement_Sequence =>
7394 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7396 Analyze (N);
7397 return;
7398 end if;
7400 else
7401 N_Orig := N;
7402 end if;
7404 Extract_Entry (Ecall, Concval, Ename, Index);
7405 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7407 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7408 Decls := Declarations (Ecall);
7410 if Is_Protected_Type (Etype (Concval)) then
7412 -- Get the declarations of the block expanded from the entry call
7414 Decl := First (Decls);
7415 while Present (Decl)
7416 and then (Nkind (Decl) /= N_Object_Declaration
7417 or else not Is_RTE (Etype (Object_Definition (Decl)),
7418 RE_Communication_Block))
7419 loop
7420 Next (Decl);
7421 end loop;
7423 pragma Assert (Present (Decl));
7424 Cancel_Param := Defining_Identifier (Decl);
7426 -- Change the mode of the Protected_Entry_Call call
7428 -- Protected_Entry_Call (
7429 -- Object => po._object'Access,
7430 -- E => <entry index>;
7431 -- Uninterpreted_Data => P'Address;
7432 -- Mode => Asynchronous_Call;
7433 -- Block => Bnn);
7435 -- Skip assignments to temporaries created for in-out parameters
7437 -- This makes unwarranted assumptions about the shape of the expanded
7438 -- tree for the call, and should be cleaned up ???
7440 Stmt := First (Stmts);
7441 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7442 Next (Stmt);
7443 end loop;
7445 Call := Stmt;
7447 Param := First (Parameter_Associations (Call));
7448 while Present (Param)
7449 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7450 loop
7451 Next (Param);
7452 end loop;
7454 pragma Assert (Present (Param));
7455 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7456 Analyze (Param);
7458 -- Append an if statement to execute the abortable part
7460 -- Generate:
7461 -- if Enqueued (Bnn) then
7463 Append_To (Stmts,
7464 Make_Implicit_If_Statement (N,
7465 Condition =>
7466 Make_Function_Call (Loc,
7467 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7468 Parameter_Associations => New_List (
7469 New_Occurrence_Of (Cancel_Param, Loc))),
7470 Then_Statements => Astats));
7472 Abortable_Block :=
7473 Make_Block_Statement (Loc,
7474 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7475 Handled_Statement_Sequence =>
7476 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7477 Has_Created_Identifier => True,
7478 Is_Asynchronous_Call_Block => True);
7480 Stmts := New_List (
7481 Make_Block_Statement (Loc,
7482 Handled_Statement_Sequence =>
7483 Make_Handled_Sequence_Of_Statements (Loc,
7484 Statements => New_List (
7485 Make_Implicit_Label_Declaration (Loc,
7486 Defining_Identifier => Blk_Ent,
7487 Label_Construct => Abortable_Block),
7488 Abortable_Block),
7490 -- exception
7492 Exception_Handlers => New_List (
7493 Make_Implicit_Exception_Handler (Loc,
7495 -- when Abort_Signal =>
7496 -- null;
7498 Exception_Choices =>
7499 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7500 Statements => New_List (Make_Null_Statement (Loc)))))),
7502 -- if not Cancelled (Bnn) then
7503 -- triggered statements
7504 -- end if;
7506 Make_Implicit_If_Statement (N,
7507 Condition => Make_Op_Not (Loc,
7508 Right_Opnd =>
7509 Make_Function_Call (Loc,
7510 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7511 Parameter_Associations => New_List (
7512 New_Occurrence_Of (Cancel_Param, Loc)))),
7513 Then_Statements => Tstats));
7515 -- Asynchronous task entry call
7517 else
7518 if No (Decls) then
7519 Decls := New_List;
7520 end if;
7522 B := Make_Defining_Identifier (Loc, Name_uB);
7524 -- Insert declaration of B in declarations of existing block
7526 Prepend_To (Decls,
7527 Make_Object_Declaration (Loc,
7528 Defining_Identifier => B,
7529 Object_Definition =>
7530 New_Occurrence_Of (Standard_Boolean, Loc)));
7532 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7534 -- Insert the declaration of C in the declarations of the existing
7535 -- block. The variable is initialized to something (True or False,
7536 -- does not matter) to prevent CodePeer from complaining about a
7537 -- possible read of an uninitialized variable.
7539 Prepend_To (Decls,
7540 Make_Object_Declaration (Loc,
7541 Defining_Identifier => Cancel_Param,
7542 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7543 Expression => New_Occurrence_Of (Standard_False, Loc),
7544 Has_Init_Expression => True));
7546 -- Remove and save the call to Call_Simple
7548 Stmt := First (Stmts);
7550 -- Skip assignments to temporaries created for in-out parameters.
7551 -- This makes unwarranted assumptions about the shape of the expanded
7552 -- tree for the call, and should be cleaned up ???
7554 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7555 Next (Stmt);
7556 end loop;
7558 Call := Stmt;
7560 -- Create the inner block to protect the abortable part
7562 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7564 if Abort_Allowed then
7565 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7566 end if;
7568 Abortable_Block :=
7569 Make_Block_Statement (Loc,
7570 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7571 Handled_Statement_Sequence =>
7572 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7573 Has_Created_Identifier => True,
7574 Is_Asynchronous_Call_Block => True);
7576 Insert_After (Call,
7577 Make_Block_Statement (Loc,
7578 Handled_Statement_Sequence =>
7579 Make_Handled_Sequence_Of_Statements (Loc,
7580 Statements => New_List (
7581 Make_Implicit_Label_Declaration (Loc,
7582 Defining_Identifier => Blk_Ent,
7583 Label_Construct => Abortable_Block),
7584 Abortable_Block),
7585 Exception_Handlers => Hdle)));
7587 -- Create new call statement
7589 Params := Parameter_Associations (Call);
7591 Append_To (Params,
7592 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7593 Append_To (Params, New_Occurrence_Of (B, Loc));
7595 Rewrite (Call,
7596 Make_Procedure_Call_Statement (Loc,
7597 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7598 Parameter_Associations => Params));
7600 -- Construct statement sequence for new block
7602 Append_To (Stmts,
7603 Make_Implicit_If_Statement (N,
7604 Condition =>
7605 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7606 Then_Statements => Tstats));
7608 -- Protected the call against abort
7610 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7611 end if;
7613 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7615 -- The result is the new block
7617 Rewrite (N_Orig,
7618 Make_Block_Statement (Loc,
7619 Declarations => Decls,
7620 Handled_Statement_Sequence =>
7621 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7623 Analyze (N_Orig);
7624 end Expand_N_Asynchronous_Select;
7626 -------------------------------------
7627 -- Expand_N_Conditional_Entry_Call --
7628 -------------------------------------
7630 -- The conditional task entry call is converted to a call to
7631 -- Task_Entry_Call:
7633 -- declare
7634 -- B : Boolean;
7635 -- P : parms := (parm, parm, parm);
7637 -- begin
7638 -- Task_Entry_Call
7639 -- (<acceptor-task>, -- Acceptor
7640 -- <entry-index>, -- E
7641 -- P'Address, -- Uninterpreted_Data
7642 -- Conditional_Call, -- Mode
7643 -- B); -- Rendezvous_Successful
7644 -- parm := P.param;
7645 -- parm := P.param;
7646 -- ...
7647 -- if B then
7648 -- normal-statements
7649 -- else
7650 -- else-statements
7651 -- end if;
7652 -- end;
7654 -- For a description of the use of P and the assignments after the call,
7655 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7656 -- conditional entry call has already been expanded (by the Expand_N_Entry
7657 -- _Call_Statement procedure) as follows:
7659 -- declare
7660 -- P : parms := (parm, parm, parm);
7661 -- begin
7662 -- ... info for in-out parameters
7663 -- Call_Simple (acceptor-task, entry-index, P'Address);
7664 -- parm := P.param;
7665 -- parm := P.param;
7666 -- ...
7667 -- end;
7669 -- so the task at hand is to convert the latter expansion into the former
7671 -- The conditional protected entry call is converted to a call to
7672 -- Protected_Entry_Call:
7674 -- declare
7675 -- P : parms := (parm, parm, parm);
7676 -- Bnn : Communications_Block;
7678 -- begin
7679 -- Protected_Entry_Call
7680 -- (po._object'Access, -- Object
7681 -- <entry index>, -- E
7682 -- P'Address, -- Uninterpreted_Data
7683 -- Conditional_Call, -- Mode
7684 -- Bnn); -- Block
7685 -- parm := P.param;
7686 -- parm := P.param;
7687 -- ...
7688 -- if Cancelled (Bnn) then
7689 -- else-statements
7690 -- else
7691 -- normal-statements
7692 -- end if;
7693 -- end;
7695 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7696 -- into:
7698 -- declare
7699 -- B : Boolean := False;
7700 -- C : Ada.Tags.Prim_Op_Kind;
7701 -- K : Ada.Tags.Tagged_Kind :=
7702 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7703 -- P : Parameters := (Param1 .. ParamN);
7704 -- S : Integer;
7706 -- begin
7707 -- if K = Ada.Tags.TK_Limited_Tagged
7708 -- or else K = Ada.Tags.TK_Tagged
7709 -- then
7710 -- <dispatching-call>;
7711 -- <triggering-statements>
7713 -- else
7714 -- S :=
7715 -- Ada.Tags.Get_Offset_Index
7716 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7718 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7720 -- if C = POK_Protected_Entry
7721 -- or else C = POK_Task_Entry
7722 -- then
7723 -- Param1 := P.Param1;
7724 -- ...
7725 -- ParamN := P.ParamN;
7726 -- end if;
7728 -- if B then
7729 -- if C = POK_Procedure
7730 -- or else C = POK_Protected_Procedure
7731 -- or else C = POK_Task_Procedure
7732 -- then
7733 -- <dispatching-call>;
7734 -- end if;
7736 -- <triggering-statements>
7737 -- else
7738 -- <else-statements>
7739 -- end if;
7740 -- end if;
7741 -- end;
7743 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7744 Loc : constant Source_Ptr := Sloc (N);
7745 Alt : constant Node_Id := Entry_Call_Alternative (N);
7746 Blk : Node_Id := Entry_Call_Statement (Alt);
7748 Actuals : List_Id;
7749 Blk_Typ : Entity_Id;
7750 Call : Node_Id;
7751 Call_Ent : Entity_Id;
7752 Conc_Typ_Stmts : List_Id;
7753 Decl : Node_Id;
7754 Decls : List_Id;
7755 Formals : List_Id;
7756 Lim_Typ_Stmts : List_Id;
7757 N_Stats : List_Id;
7758 Obj : Entity_Id;
7759 Param : Node_Id;
7760 Params : List_Id;
7761 Stmt : Node_Id;
7762 Stmts : List_Id;
7763 Transient_Blk : Node_Id;
7764 Unpack : List_Id;
7766 B : Entity_Id; -- Call status flag
7767 C : Entity_Id; -- Call kind
7768 K : Entity_Id; -- Tagged kind
7769 P : Entity_Id; -- Parameter block
7770 S : Entity_Id; -- Primitive operation slot
7772 begin
7773 Process_Statements_For_Controlled_Objects (N);
7775 if Ada_Version >= Ada_2005
7776 and then Nkind (Blk) = N_Procedure_Call_Statement
7777 then
7778 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7780 Decls := New_List;
7781 Stmts := New_List;
7783 -- Call status flag processing, generate:
7784 -- B : Boolean := False;
7786 B := Build_B (Loc, Decls);
7788 -- Call kind processing, generate:
7789 -- C : Ada.Tags.Prim_Op_Kind;
7791 C := Build_C (Loc, Decls);
7793 -- Tagged kind processing, generate:
7794 -- K : Ada.Tags.Tagged_Kind :=
7795 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7797 K := Build_K (Loc, Decls, Obj);
7799 -- Parameter block processing
7801 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7802 P := Parameter_Block_Pack
7803 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7805 -- Dispatch table slot processing, generate:
7806 -- S : Integer;
7808 S := Build_S (Loc, Decls);
7810 -- Generate:
7811 -- S := Ada.Tags.Get_Offset_Index
7812 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7814 Conc_Typ_Stmts :=
7815 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7817 -- Generate:
7818 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7820 Append_To (Conc_Typ_Stmts,
7821 Make_Procedure_Call_Statement (Loc,
7822 Name =>
7823 New_Occurrence_Of (
7824 Find_Prim_Op (Etype (Etype (Obj)),
7825 Name_uDisp_Conditional_Select),
7826 Loc),
7827 Parameter_Associations =>
7828 New_List (
7829 New_Copy_Tree (Obj), -- <object>
7830 New_Occurrence_Of (S, Loc), -- S
7831 Make_Attribute_Reference (Loc, -- P'Address
7832 Prefix => New_Occurrence_Of (P, Loc),
7833 Attribute_Name => Name_Address),
7834 New_Occurrence_Of (C, Loc), -- C
7835 New_Occurrence_Of (B, Loc)))); -- B
7837 -- Generate:
7838 -- if C = POK_Protected_Entry
7839 -- or else C = POK_Task_Entry
7840 -- then
7841 -- Param1 := P.Param1;
7842 -- ...
7843 -- ParamN := P.ParamN;
7844 -- end if;
7846 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7848 -- Generate the if statement only when the packed parameters need
7849 -- explicit assignments to their corresponding actuals.
7851 if Present (Unpack) then
7852 Append_To (Conc_Typ_Stmts,
7853 Make_Implicit_If_Statement (N,
7854 Condition =>
7855 Make_Or_Else (Loc,
7856 Left_Opnd =>
7857 Make_Op_Eq (Loc,
7858 Left_Opnd =>
7859 New_Occurrence_Of (C, Loc),
7860 Right_Opnd =>
7861 New_Occurrence_Of (RTE (
7862 RE_POK_Protected_Entry), Loc)),
7864 Right_Opnd =>
7865 Make_Op_Eq (Loc,
7866 Left_Opnd =>
7867 New_Occurrence_Of (C, Loc),
7868 Right_Opnd =>
7869 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7871 Then_Statements => Unpack));
7872 end if;
7874 -- Generate:
7875 -- if B then
7876 -- if C = POK_Procedure
7877 -- or else C = POK_Protected_Procedure
7878 -- or else C = POK_Task_Procedure
7879 -- then
7880 -- <dispatching-call>
7881 -- end if;
7882 -- <normal-statements>
7883 -- else
7884 -- <else-statements>
7885 -- end if;
7887 N_Stats := New_Copy_Separate_List (Statements (Alt));
7889 Prepend_To (N_Stats,
7890 Make_Implicit_If_Statement (N,
7891 Condition =>
7892 Make_Or_Else (Loc,
7893 Left_Opnd =>
7894 Make_Op_Eq (Loc,
7895 Left_Opnd =>
7896 New_Occurrence_Of (C, Loc),
7897 Right_Opnd =>
7898 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7900 Right_Opnd =>
7901 Make_Or_Else (Loc,
7902 Left_Opnd =>
7903 Make_Op_Eq (Loc,
7904 Left_Opnd =>
7905 New_Occurrence_Of (C, Loc),
7906 Right_Opnd =>
7907 New_Occurrence_Of (RTE (
7908 RE_POK_Protected_Procedure), Loc)),
7910 Right_Opnd =>
7911 Make_Op_Eq (Loc,
7912 Left_Opnd =>
7913 New_Occurrence_Of (C, Loc),
7914 Right_Opnd =>
7915 New_Occurrence_Of (RTE (
7916 RE_POK_Task_Procedure), Loc)))),
7918 Then_Statements =>
7919 New_List (Blk)));
7921 Append_To (Conc_Typ_Stmts,
7922 Make_Implicit_If_Statement (N,
7923 Condition => New_Occurrence_Of (B, Loc),
7924 Then_Statements => N_Stats,
7925 Else_Statements => Else_Statements (N)));
7927 -- Generate:
7928 -- <dispatching-call>;
7929 -- <triggering-statements>
7931 Lim_Typ_Stmts := New_Copy_Separate_List (Statements (Alt));
7932 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7934 -- Generate:
7935 -- if K = Ada.Tags.TK_Limited_Tagged
7936 -- or else K = Ada.Tags.TK_Tagged
7937 -- then
7938 -- Lim_Typ_Stmts
7939 -- else
7940 -- Conc_Typ_Stmts
7941 -- end if;
7943 Append_To (Stmts,
7944 Make_Implicit_If_Statement (N,
7945 Condition => Build_Dispatching_Tag_Check (K, N),
7946 Then_Statements => Lim_Typ_Stmts,
7947 Else_Statements => Conc_Typ_Stmts));
7949 Rewrite (N,
7950 Make_Block_Statement (Loc,
7951 Declarations =>
7952 Decls,
7953 Handled_Statement_Sequence =>
7954 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7956 -- As described above, the entry alternative is transformed into a
7957 -- block that contains the gnulli call, and possibly assignment
7958 -- statements for in-out parameters. The gnulli call may itself be
7959 -- rewritten into a transient block if some unconstrained parameters
7960 -- require it. We need to retrieve the call to complete its parameter
7961 -- list.
7963 else
7964 Transient_Blk :=
7965 First (Statements (Handled_Statement_Sequence (Blk)));
7967 if Present (Transient_Blk)
7968 and then Nkind (Transient_Blk) = N_Block_Statement
7969 then
7970 Blk := Transient_Blk;
7971 end if;
7973 Stmts := Statements (Handled_Statement_Sequence (Blk));
7974 Stmt := First (Stmts);
7975 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7976 Next (Stmt);
7977 end loop;
7979 Call := Stmt;
7980 Params := Parameter_Associations (Call);
7982 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7984 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7986 Param := First (Params);
7987 while Present (Param)
7988 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7989 loop
7990 Next (Param);
7991 end loop;
7993 pragma Assert (Present (Param));
7994 Rewrite (Param,
7995 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
7997 Analyze (Param);
7999 -- Find the Communication_Block parameter for the call to the
8000 -- Cancelled function.
8002 Decl := First (Declarations (Blk));
8003 while Present (Decl)
8004 and then not Is_RTE (Etype (Object_Definition (Decl)),
8005 RE_Communication_Block)
8006 loop
8007 Next (Decl);
8008 end loop;
8010 -- Add an if statement to execute the else part if the call
8011 -- does not succeed (as indicated by the Cancelled predicate).
8013 Append_To (Stmts,
8014 Make_Implicit_If_Statement (N,
8015 Condition => Make_Function_Call (Loc,
8016 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8017 Parameter_Associations => New_List (
8018 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8019 Then_Statements => Else_Statements (N),
8020 Else_Statements => Statements (Alt)));
8022 else
8023 B := Make_Defining_Identifier (Loc, Name_uB);
8025 -- Insert declaration of B in declarations of existing block
8027 if No (Declarations (Blk)) then
8028 Set_Declarations (Blk, New_List);
8029 end if;
8031 Prepend_To (Declarations (Blk),
8032 Make_Object_Declaration (Loc,
8033 Defining_Identifier => B,
8034 Object_Definition =>
8035 New_Occurrence_Of (Standard_Boolean, Loc)));
8037 -- Create new call statement
8039 Append_To (Params,
8040 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8041 Append_To (Params, New_Occurrence_Of (B, Loc));
8043 Rewrite (Call,
8044 Make_Procedure_Call_Statement (Loc,
8045 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8046 Parameter_Associations => Params));
8048 -- Construct statement sequence for new block
8050 Append_To (Stmts,
8051 Make_Implicit_If_Statement (N,
8052 Condition => New_Occurrence_Of (B, Loc),
8053 Then_Statements => Statements (Alt),
8054 Else_Statements => Else_Statements (N)));
8055 end if;
8057 -- The result is the new block
8059 Rewrite (N,
8060 Make_Block_Statement (Loc,
8061 Declarations => Declarations (Blk),
8062 Handled_Statement_Sequence =>
8063 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8064 end if;
8066 Analyze (N);
8068 Reset_Scopes_To (N, Entity (Identifier (N)));
8069 end Expand_N_Conditional_Entry_Call;
8071 ---------------------------------------
8072 -- Expand_N_Delay_Relative_Statement --
8073 ---------------------------------------
8075 -- Delay statement is implemented as a procedure call to Delay_For
8076 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8077 -- simple delays imposed by the use of Protected Objects.
8079 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8080 Loc : constant Source_Ptr := Sloc (N);
8081 Proc : Entity_Id;
8083 begin
8084 -- Try to use Ada.Calendar.Delays.Delay_For if available.
8086 if RTE_Available (RO_CA_Delay_For) then
8087 Proc := RTE (RO_CA_Delay_For);
8089 -- Otherwise, use System.Relative_Delays.Delay_For and emit an error
8090 -- message if not available. This is the implementation used on
8091 -- restricted platforms when Ada.Calendar is not available.
8093 else
8094 Proc := RTE (RO_RD_Delay_For);
8095 end if;
8097 Rewrite (N,
8098 Make_Procedure_Call_Statement (Loc,
8099 Name => New_Occurrence_Of (Proc, Loc),
8100 Parameter_Associations => New_List (Expression (N))));
8101 Analyze (N);
8102 end Expand_N_Delay_Relative_Statement;
8104 ------------------------------------
8105 -- Expand_N_Delay_Until_Statement --
8106 ------------------------------------
8108 -- Delay Until statement is implemented as a procedure call to
8109 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8111 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8112 Loc : constant Source_Ptr := Sloc (N);
8113 Typ : Entity_Id;
8115 begin
8116 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8117 Typ := RTE (RO_CA_Delay_Until);
8118 else
8119 Typ := RTE (RO_RT_Delay_Until);
8120 end if;
8122 Rewrite (N,
8123 Make_Procedure_Call_Statement (Loc,
8124 Name => New_Occurrence_Of (Typ, Loc),
8125 Parameter_Associations => New_List (Expression (N))));
8127 Analyze (N);
8128 end Expand_N_Delay_Until_Statement;
8130 -------------------------
8131 -- Expand_N_Entry_Body --
8132 -------------------------
8134 procedure Expand_N_Entry_Body (N : Node_Id) is
8135 begin
8136 -- Associate discriminals with the next protected operation body to be
8137 -- expanded.
8139 if Present (Next_Protected_Operation (N)) then
8140 Set_Discriminals (Parent (Current_Scope));
8141 end if;
8142 end Expand_N_Entry_Body;
8144 -----------------------------------
8145 -- Expand_N_Entry_Call_Statement --
8146 -----------------------------------
8148 -- An entry call is expanded into GNARLI calls to implement a simple entry
8149 -- call (see Build_Simple_Entry_Call).
8151 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8152 Concval : Node_Id;
8153 Ename : Node_Id;
8154 Index : Node_Id;
8156 begin
8157 if No_Run_Time_Mode then
8158 Error_Msg_CRT ("entry call", N);
8159 return;
8160 end if;
8162 -- If this entry call is part of an asynchronous select, don't expand it
8163 -- here; it will be expanded with the select statement. Don't expand
8164 -- timed entry calls either, as they are translated into asynchronous
8165 -- entry calls.
8167 -- ??? This whole approach is questionable; it may be better to go back
8168 -- to allowing the expansion to take place and then attempting to fix it
8169 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8170 -- whether the expanded call is on a task or protected entry.
8172 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8173 or else N /= Triggering_Statement (Parent (N)))
8174 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8175 or else N /= Entry_Call_Statement (Parent (N))
8176 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8177 then
8178 Extract_Entry (N, Concval, Ename, Index);
8179 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8180 end if;
8181 end Expand_N_Entry_Call_Statement;
8183 --------------------------------
8184 -- Expand_N_Entry_Declaration --
8185 --------------------------------
8187 -- If there are parameters, then first, each of the formals is marked by
8188 -- setting Is_Entry_Formal. Next a record type is built which is used to
8189 -- hold the parameter values. The name of this record type is entryP where
8190 -- entry is the name of the entry, with an additional corresponding access
8191 -- type called entryPA. The record type has matching components for each
8192 -- formal (the component names are the same as the formal names). For
8193 -- elementary types, the component type matches the formal type. For
8194 -- composite types, an access type is declared (with the name formalA)
8195 -- which designates the formal type, and the type of the component is this
8196 -- access type. Finally the Entry_Component of each formal is set to
8197 -- reference the corresponding record component.
8199 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8200 Loc : constant Source_Ptr := Sloc (N);
8201 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8202 Components : List_Id;
8203 Formal : Node_Id;
8204 Ftype : Entity_Id;
8205 Last_Decl : Node_Id;
8206 Component : Entity_Id;
8207 Ctype : Entity_Id;
8208 Decl : Node_Id;
8209 Rec_Ent : Entity_Id;
8210 Acc_Ent : Entity_Id;
8212 begin
8213 Formal := First_Formal (Entry_Ent);
8214 Last_Decl := N;
8216 -- Most processing is done only if parameters are present
8218 if Present (Formal) then
8219 Components := New_List;
8221 -- Loop through formals
8223 while Present (Formal) loop
8224 Set_Is_Entry_Formal (Formal);
8225 Component :=
8226 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8227 Set_Entry_Component (Formal, Component);
8228 Set_Entry_Formal (Component, Formal);
8229 Ftype := Etype (Formal);
8231 -- Declare new access type and then append
8233 Ctype := Make_Temporary (Loc, 'A');
8234 Set_Is_Param_Block_Component_Type (Ctype);
8236 Decl :=
8237 Make_Full_Type_Declaration (Loc,
8238 Defining_Identifier => Ctype,
8239 Type_Definition =>
8240 Make_Access_To_Object_Definition (Loc,
8241 All_Present => True,
8242 Constant_Present => Ekind (Formal) = E_In_Parameter,
8243 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8245 Insert_After (Last_Decl, Decl);
8246 Last_Decl := Decl;
8248 Append_To (Components,
8249 Make_Component_Declaration (Loc,
8250 Defining_Identifier => Component,
8251 Component_Definition =>
8252 Make_Component_Definition (Loc,
8253 Aliased_Present => False,
8254 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8256 Next_Formal_With_Extras (Formal);
8257 end loop;
8259 -- Create the Entry_Parameter_Record declaration
8261 Rec_Ent := Make_Temporary (Loc, 'P');
8263 Decl :=
8264 Make_Full_Type_Declaration (Loc,
8265 Defining_Identifier => Rec_Ent,
8266 Type_Definition =>
8267 Make_Record_Definition (Loc,
8268 Component_List =>
8269 Make_Component_List (Loc,
8270 Component_Items => Components)));
8272 Insert_After (Last_Decl, Decl);
8273 Last_Decl := Decl;
8275 -- Construct and link in the corresponding access type
8277 Acc_Ent := Make_Temporary (Loc, 'A');
8279 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8281 Decl :=
8282 Make_Full_Type_Declaration (Loc,
8283 Defining_Identifier => Acc_Ent,
8284 Type_Definition =>
8285 Make_Access_To_Object_Definition (Loc,
8286 All_Present => True,
8287 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8289 Insert_After (Last_Decl, Decl);
8290 end if;
8291 end Expand_N_Entry_Declaration;
8293 -----------------------------
8294 -- Expand_N_Protected_Body --
8295 -----------------------------
8297 -- Protected bodies are expanded to the completion of the subprograms
8298 -- created for the corresponding protected type. These are a protected and
8299 -- unprotected version of each protected subprogram in the object, a
8300 -- function to calculate each entry barrier, and a procedure to execute the
8301 -- sequence of statements of each protected entry body. For example, for
8302 -- protected type ptype:
8304 -- function entB
8305 -- (O : System.Address;
8306 -- E : Protected_Entry_Index)
8307 -- return Boolean
8308 -- is
8309 -- <discriminant renamings>
8310 -- <private object renamings>
8311 -- begin
8312 -- return <barrier expression>;
8313 -- end entB;
8315 -- procedure pprocN (_object : in out poV;...) is
8316 -- <discriminant renamings>
8317 -- <private object renamings>
8318 -- begin
8319 -- <sequence of statements>
8320 -- end pprocN;
8322 -- procedure pprocP (_object : in out poV;...) is
8323 -- procedure _clean is
8324 -- Pn : Boolean;
8325 -- begin
8326 -- ptypeS (_object, Pn);
8327 -- Unlock (_object._object'Access);
8328 -- Abort_Undefer.all;
8329 -- end _clean;
8331 -- begin
8332 -- Abort_Defer.all;
8333 -- Lock (_object._object'Access);
8334 -- pprocN (_object;...);
8335 -- at end
8336 -- _clean;
8337 -- end pproc;
8339 -- function pfuncN (_object : poV;...) return Return_Type is
8340 -- <discriminant renamings>
8341 -- <private object renamings>
8342 -- begin
8343 -- <sequence of statements>
8344 -- end pfuncN;
8346 -- function pfuncP (_object : poV) return Return_Type is
8347 -- procedure _clean is
8348 -- begin
8349 -- Unlock (_object._object'Access);
8350 -- Abort_Undefer.all;
8351 -- end _clean;
8353 -- begin
8354 -- Abort_Defer.all;
8355 -- Lock (_object._object'Access);
8356 -- return pfuncN (_object);
8358 -- at end
8359 -- _clean;
8360 -- end pfunc;
8362 -- procedure entE
8363 -- (O : System.Address;
8364 -- P : System.Address;
8365 -- E : Protected_Entry_Index)
8366 -- is
8367 -- <discriminant renamings>
8368 -- <private object renamings>
8369 -- type poVP is access poV;
8370 -- _Object : ptVP := ptVP!(O);
8372 -- begin
8373 -- begin
8374 -- <statement sequence>
8375 -- Complete_Entry_Body (_Object._Object);
8376 -- exception
8377 -- when all others =>
8378 -- Exceptional_Complete_Entry_Body (
8379 -- _Object._Object, Get_GNAT_Exception);
8380 -- end;
8381 -- end entE;
8383 -- The type poV is the record created for the protected type to hold
8384 -- the state of the protected object.
8386 procedure Expand_N_Protected_Body (N : Node_Id) is
8387 Loc : constant Source_Ptr := Sloc (N);
8388 Pid : constant Entity_Id := Corresponding_Spec (N);
8390 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8391 -- This flag indicates whether the lock free implementation is active
8393 Current_Node : Node_Id;
8394 Disp_Op_Body : Node_Id;
8395 New_Op_Body : Node_Id;
8396 Op_Body : Node_Id;
8397 Op_Decl : Node_Id;
8398 Op_Id : Entity_Id;
8400 function Build_Dispatching_Subprogram_Body
8401 (N : Node_Id;
8402 Pid : Node_Id;
8403 Prot_Bod : Node_Id) return Node_Id;
8404 -- Build a dispatching version of the protected subprogram body. The
8405 -- newly generated subprogram contains a call to the original protected
8406 -- body. The following code is generated:
8408 -- function <protected-function-name> (Param1 .. ParamN) return
8409 -- <return-type> is
8410 -- begin
8411 -- return <protected-function-name>P (Param1 .. ParamN);
8412 -- end <protected-function-name>;
8414 -- or
8416 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8417 -- begin
8418 -- <protected-procedure-name>P (Param1 .. ParamN);
8419 -- end <protected-procedure-name>
8421 ---------------------------------------
8422 -- Build_Dispatching_Subprogram_Body --
8423 ---------------------------------------
8425 function Build_Dispatching_Subprogram_Body
8426 (N : Node_Id;
8427 Pid : Node_Id;
8428 Prot_Bod : Node_Id) return Node_Id
8430 Loc : constant Source_Ptr := Sloc (N);
8431 Actuals : List_Id;
8432 Formal : Node_Id;
8433 Spec : Node_Id;
8434 Stmts : List_Id;
8436 begin
8437 -- Generate a specification without a letter suffix in order to
8438 -- override an interface function or procedure.
8440 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8442 -- The formal parameters become the actuals of the protected function
8443 -- or procedure call.
8445 Actuals := New_List;
8446 Formal := First (Parameter_Specifications (Spec));
8447 while Present (Formal) loop
8448 Append_To (Actuals,
8449 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8450 Next (Formal);
8451 end loop;
8453 if Nkind (Spec) = N_Procedure_Specification then
8454 Stmts :=
8455 New_List (
8456 Make_Procedure_Call_Statement (Loc,
8457 Name =>
8458 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8459 Parameter_Associations => Actuals));
8461 else
8462 pragma Assert (Nkind (Spec) = N_Function_Specification);
8464 Stmts :=
8465 New_List (
8466 Make_Simple_Return_Statement (Loc,
8467 Expression =>
8468 Make_Function_Call (Loc,
8469 Name =>
8470 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8471 Parameter_Associations => Actuals)));
8472 end if;
8474 return
8475 Make_Subprogram_Body (Loc,
8476 Declarations => Empty_List,
8477 Specification => Spec,
8478 Handled_Statement_Sequence =>
8479 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8480 end Build_Dispatching_Subprogram_Body;
8482 -- Start of processing for Expand_N_Protected_Body
8484 begin
8485 if No_Run_Time_Mode then
8486 Error_Msg_CRT ("protected body", N);
8487 return;
8488 end if;
8490 -- This is the proper body corresponding to a stub. The declarations
8491 -- must be inserted at the point of the stub, which in turn is in the
8492 -- declarative part of the parent unit.
8494 if Nkind (Parent (N)) = N_Subunit then
8495 Current_Node := Corresponding_Stub (Parent (N));
8496 else
8497 Current_Node := N;
8498 end if;
8500 Op_Body := First (Declarations (N));
8502 -- The protected body is replaced with the bodies of its protected
8503 -- operations, and the declarations for internal objects that may
8504 -- have been created for entry family bounds.
8506 Rewrite (N, Make_Null_Statement (Sloc (N)));
8507 Analyze (N);
8509 while Present (Op_Body) loop
8510 case Nkind (Op_Body) is
8511 when N_Subprogram_Declaration =>
8512 null;
8514 when N_Subprogram_Body =>
8516 -- Do not create bodies for eliminated operations
8518 if not Is_Eliminated (Defining_Entity (Op_Body))
8519 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8520 then
8521 if Lock_Free_Active then
8522 New_Op_Body :=
8523 Build_Lock_Free_Unprotected_Subprogram_Body
8524 (Op_Body, Pid);
8525 else
8526 New_Op_Body :=
8527 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8528 end if;
8530 Insert_After (Current_Node, New_Op_Body);
8531 Current_Node := New_Op_Body;
8532 Analyze (New_Op_Body);
8534 -- When the original protected body has nested subprograms,
8535 -- the new body also has them, so set the flag accordingly
8536 -- and reset the scopes of the top-level nested subprograms
8537 -- and other declaration entities so that they now refer to
8538 -- the new body's entity. (It would preferable to do this
8539 -- within Build_Protected_Sub_Specification, which is called
8540 -- from Build_Unprotected_Subprogram_Body, but the needed
8541 -- subprogram entity isn't available via Corresponding_Spec
8542 -- until after the above Analyze call.)
8544 if Has_Nested_Subprogram (Corresponding_Spec (Op_Body)) then
8545 Set_Has_Nested_Subprogram
8546 (Corresponding_Spec (New_Op_Body));
8548 Reset_Scopes_To
8549 (New_Op_Body, Corresponding_Spec (New_Op_Body));
8550 end if;
8552 -- Build the corresponding protected operation. This is
8553 -- needed only if this is a public or private operation of
8554 -- the type.
8556 -- Why do we need to test for Corresponding_Spec being
8557 -- present here when it's assumed to be set further above
8558 -- in the Is_Eliminated test???
8560 if Present (Corresponding_Spec (Op_Body)) then
8561 Op_Decl :=
8562 Unit_Declaration_Node (Corresponding_Spec (Op_Body));
8564 if Nkind (Parent (Op_Decl)) = N_Protected_Definition then
8565 if Lock_Free_Active then
8566 New_Op_Body :=
8567 Build_Lock_Free_Protected_Subprogram_Body
8568 (Op_Body, Pid, Specification (New_Op_Body));
8569 else
8570 New_Op_Body :=
8571 Build_Protected_Subprogram_Body (
8572 Op_Body, Pid, Specification (New_Op_Body));
8573 end if;
8575 Insert_After (Current_Node, New_Op_Body);
8576 Analyze (New_Op_Body);
8577 Current_Node := New_Op_Body;
8579 -- Generate an overriding primitive operation body for
8580 -- this subprogram if the protected type implements
8581 -- an interface.
8583 if Ada_Version >= Ada_2005
8584 and then Present (Interfaces (
8585 Corresponding_Record_Type (Pid)))
8586 then
8587 Disp_Op_Body :=
8588 Build_Dispatching_Subprogram_Body (
8589 Op_Body, Pid, New_Op_Body);
8591 Insert_After (Current_Node, Disp_Op_Body);
8592 Analyze (Disp_Op_Body);
8594 Current_Node := Disp_Op_Body;
8595 end if;
8596 end if;
8597 end if;
8598 end if;
8600 when N_Entry_Body =>
8601 Op_Id := Defining_Identifier (Op_Body);
8602 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8604 Insert_After (Current_Node, New_Op_Body);
8605 Current_Node := New_Op_Body;
8606 Analyze (New_Op_Body);
8608 when N_Implicit_Label_Declaration =>
8609 null;
8611 when N_Call_Marker
8612 | N_Itype_Reference
8614 New_Op_Body := New_Copy (Op_Body);
8615 Insert_After (Current_Node, New_Op_Body);
8616 Current_Node := New_Op_Body;
8618 when N_Freeze_Entity =>
8619 New_Op_Body := New_Copy (Op_Body);
8621 if Present (Entity (Op_Body))
8622 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8623 then
8624 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8625 end if;
8627 Insert_After (Current_Node, New_Op_Body);
8628 Current_Node := New_Op_Body;
8629 Analyze (New_Op_Body);
8631 when N_Pragma =>
8632 New_Op_Body := New_Copy (Op_Body);
8633 Insert_After (Current_Node, New_Op_Body);
8634 Current_Node := New_Op_Body;
8635 Analyze (New_Op_Body);
8637 when N_Object_Declaration =>
8638 pragma Assert (not Comes_From_Source (Op_Body));
8639 New_Op_Body := New_Copy (Op_Body);
8640 Insert_After (Current_Node, New_Op_Body);
8641 Current_Node := New_Op_Body;
8642 Analyze (New_Op_Body);
8644 when others =>
8645 raise Program_Error;
8646 end case;
8648 Next (Op_Body);
8649 end loop;
8651 -- Finally, create the body of the function that maps an entry index
8652 -- into the corresponding body index, except when there is no entry, or
8653 -- in a Ravenscar-like profile.
8655 if Corresponding_Runtime_Package (Pid) =
8656 System_Tasking_Protected_Objects_Entries
8657 then
8658 New_Op_Body := Build_Find_Body_Index (Pid);
8659 Insert_After (Current_Node, New_Op_Body);
8660 Current_Node := New_Op_Body;
8661 Analyze (New_Op_Body);
8662 end if;
8664 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8665 -- protected body. At this point all wrapper specs have been created,
8666 -- frozen and included in the dispatch table for the protected type.
8668 if Ada_Version >= Ada_2005 then
8669 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8670 end if;
8671 end Expand_N_Protected_Body;
8673 -----------------------------------------
8674 -- Expand_N_Protected_Type_Declaration --
8675 -----------------------------------------
8677 -- First we create a corresponding record type declaration used to
8678 -- represent values of this protected type.
8679 -- The general form of this type declaration is
8681 -- type poV (discriminants) is record
8682 -- _Object : aliased <kind>Protection
8683 -- [(<entry count> [, <handler count>])];
8684 -- [entry_family : array (bounds) of Void;]
8685 -- <private data fields>
8686 -- end record;
8688 -- The discriminants are present only if the corresponding protected type
8689 -- has discriminants, and they exactly mirror the protected type
8690 -- discriminants. The private data fields similarly mirror the private
8691 -- declarations of the protected type.
8693 -- The Object field is always present. It contains RTS specific data used
8694 -- to control the protected object. It is declared as Aliased so that it
8695 -- can be passed as a pointer to the RTS. This allows the protected record
8696 -- to be referenced within RTS data structures. An appropriate Protection
8697 -- type and discriminant are generated.
8699 -- The Service field is present for protected objects with entries. It
8700 -- contains sufficient information to allow the entry service procedure for
8701 -- this object to be called when the object is not known till runtime.
8703 -- One entry_family component is present for each entry family in the
8704 -- task definition (see Expand_N_Task_Type_Declaration).
8706 -- When a protected object is declared, an instance of the protected type
8707 -- value record is created. The elaboration of this declaration creates the
8708 -- correct bounds for the entry families, and also evaluates the priority
8709 -- expression if needed. The initialization routine for the protected type
8710 -- itself then calls Initialize_Protection with appropriate parameters to
8711 -- initialize the value of the Task_Id field. Install_Handlers may be also
8712 -- called if a pragma Attach_Handler applies.
8714 -- Note: this record is passed to the subprograms created by the expansion
8715 -- of protected subprograms and entries. It is an in parameter to protected
8716 -- functions and an in out parameter to procedures and entry bodies. The
8717 -- Entity_Id for this created record type is placed in the
8718 -- Corresponding_Record_Type field of the associated protected type entity.
8720 -- Next we create a procedure specifications for protected subprograms and
8721 -- entry bodies. For each protected subprograms two subprograms are
8722 -- created, an unprotected and a protected version. The unprotected version
8723 -- is called from within other operations of the same protected object.
8725 -- We also build the call to register the procedure if a pragma
8726 -- Interrupt_Handler applies.
8728 -- A single subprogram is created to service all entry bodies; it has an
8729 -- additional boolean out parameter indicating that the previous entry call
8730 -- made by the current task was serviced immediately, i.e. not by proxy.
8731 -- The O parameter contains a pointer to a record object of the type
8732 -- described above. An untyped interface is used here to allow this
8733 -- procedure to be called in places where the type of the object to be
8734 -- serviced is not known. This must be done, for example, when a call that
8735 -- may have been requeued is cancelled; the corresponding object must be
8736 -- serviced, but which object that is not known till runtime.
8738 -- procedure ptypeS
8739 -- (O : System.Address; P : out Boolean);
8740 -- procedure pprocN (_object : in out poV);
8741 -- procedure pproc (_object : in out poV);
8742 -- function pfuncN (_object : poV);
8743 -- function pfunc (_object : poV);
8744 -- ...
8746 -- Note that this must come after the record type declaration, since
8747 -- the specs refer to this type.
8749 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8750 Discr_Map : constant Elist_Id := New_Elmt_List;
8751 Loc : constant Source_Ptr := Sloc (N);
8752 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8754 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8755 -- This flag indicates whether the lock free implementation is active
8757 Pdef : constant Node_Id := Protected_Definition (N);
8758 -- This contains two lists; one for visible and one for private decls
8760 Current_Node : Node_Id := N;
8761 E_Count : Int;
8762 Entries_Aggr : Node_Id;
8763 Rec_Decl : Node_Id;
8764 Rec_Id : Entity_Id;
8766 procedure Check_Inlining (Subp : Entity_Id);
8767 -- If the original operation has a pragma Inline, propagate the flag
8768 -- to the internal body, for possible inlining later on. The source
8769 -- operation is invisible to the back-end and is never actually called.
8771 procedure Expand_Entry_Declaration (Decl : Node_Id);
8772 -- Create the entry barrier and the procedure body for entry declaration
8773 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8775 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8776 -- When compiling under the Ravenscar profile, private components must
8777 -- have a static size, or else a protected object will require heap
8778 -- allocation, violating the corresponding restriction. It is preferable
8779 -- to make this check here, because it provides a better error message
8780 -- than the back-end, which refers to the object as a whole.
8782 procedure Register_Handler;
8783 -- For a protected operation that is an interrupt handler, add the
8784 -- freeze action that will register it as such.
8786 procedure Replace_Access_Definition (Comp : Node_Id);
8787 -- If a private component of the type is an access to itself, this
8788 -- is not a reference to the current instance, but an access type out
8789 -- of which one might construct a list. If such a component exists, we
8790 -- create an incomplete type for the equivalent record type, and
8791 -- a named access type for it, that replaces the access definition
8792 -- of the original component. This is similar to what is done for
8793 -- records in Check_Anonymous_Access_Components, but simpler, because
8794 -- the corresponding record type has no previous declaration.
8795 -- This needs to be done only once, even if there are several such
8796 -- access components. The following entity stores the constructed
8797 -- access type.
8799 Acc_T : Entity_Id := Empty;
8801 --------------------
8802 -- Check_Inlining --
8803 --------------------
8805 procedure Check_Inlining (Subp : Entity_Id) is
8806 begin
8807 if Is_Inlined (Subp) then
8808 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8809 Set_Is_Inlined (Subp, False);
8810 end if;
8812 if Has_Pragma_No_Inline (Subp) then
8813 Set_Has_Pragma_No_Inline (Protected_Body_Subprogram (Subp));
8814 end if;
8815 end Check_Inlining;
8817 ---------------------------
8818 -- Static_Component_Size --
8819 ---------------------------
8821 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8822 Typ : constant Entity_Id := Etype (Comp);
8823 C : Entity_Id;
8825 begin
8826 if Is_Scalar_Type (Typ) then
8827 return True;
8829 elsif Is_Array_Type (Typ) then
8830 return Compile_Time_Known_Bounds (Typ);
8832 elsif Is_Record_Type (Typ) then
8833 C := First_Component (Typ);
8834 while Present (C) loop
8835 if not Static_Component_Size (C) then
8836 return False;
8837 end if;
8839 Next_Component (C);
8840 end loop;
8842 return True;
8844 -- Any other type will be checked by the back-end
8846 else
8847 return True;
8848 end if;
8849 end Static_Component_Size;
8851 ------------------------------
8852 -- Expand_Entry_Declaration --
8853 ------------------------------
8855 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8856 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8857 Bar_Id : Entity_Id;
8858 Bod_Id : Entity_Id;
8859 Subp : Node_Id;
8861 begin
8862 E_Count := E_Count + 1;
8864 -- Create the protected body subprogram
8866 Bod_Id :=
8867 Make_Defining_Identifier (Loc,
8868 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8869 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8871 Subp :=
8872 Make_Subprogram_Declaration (Loc,
8873 Specification =>
8874 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8876 Insert_After (Current_Node, Subp);
8877 Current_Node := Subp;
8879 Analyze (Subp);
8881 -- Build a wrapper procedure to handle contract cases, preconditions,
8882 -- and postconditions.
8884 Build_Entry_Contract_Wrapper (Ent_Id, N);
8886 -- Create the barrier function
8888 Bar_Id :=
8889 Make_Defining_Identifier (Loc,
8890 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8891 Set_Barrier_Function (Ent_Id, Bar_Id);
8893 Subp :=
8894 Make_Subprogram_Declaration (Loc,
8895 Specification =>
8896 Build_Barrier_Function_Specification (Loc, Bar_Id));
8897 Set_Is_Entry_Barrier_Function (Subp);
8899 Insert_After (Current_Node, Subp);
8900 Current_Node := Subp;
8902 Analyze (Subp);
8904 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8905 Set_Scope (Bar_Id, Scope (Ent_Id));
8907 -- Collect pointers to the protected subprogram and the barrier
8908 -- of the current entry, for insertion into Entry_Bodies_Array.
8910 Append_To (Expressions (Entries_Aggr),
8911 Make_Aggregate (Loc,
8912 Expressions => New_List (
8913 Make_Attribute_Reference (Loc,
8914 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8915 Attribute_Name => Name_Unrestricted_Access),
8916 Make_Attribute_Reference (Loc,
8917 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8918 Attribute_Name => Name_Unrestricted_Access))));
8919 end Expand_Entry_Declaration;
8921 ----------------------
8922 -- Register_Handler --
8923 ----------------------
8925 procedure Register_Handler is
8927 -- All semantic checks already done in Sem_Prag
8929 Prot_Proc : constant Entity_Id :=
8930 Defining_Unit_Name (Specification (Current_Node));
8932 Proc_Address : constant Node_Id :=
8933 Make_Attribute_Reference (Loc,
8934 Prefix =>
8935 New_Occurrence_Of (Prot_Proc, Loc),
8936 Attribute_Name => Name_Address);
8938 RTS_Call : constant Entity_Id :=
8939 Make_Procedure_Call_Statement (Loc,
8940 Name =>
8941 New_Occurrence_Of
8942 (RTE (RE_Register_Interrupt_Handler), Loc),
8943 Parameter_Associations => New_List (Proc_Address));
8944 begin
8945 Append_Freeze_Action (Prot_Proc, RTS_Call);
8946 end Register_Handler;
8948 -------------------------------
8949 -- Replace_Access_Definition --
8950 -------------------------------
8952 procedure Replace_Access_Definition (Comp : Node_Id) is
8953 Loc : constant Source_Ptr := Sloc (Comp);
8954 Inc_T : Node_Id;
8955 Inc_D : Node_Id;
8956 Acc_Def : Node_Id;
8957 Acc_D : Node_Id;
8959 begin
8960 if No (Acc_T) then
8961 Inc_T := Make_Defining_Identifier (Loc, Chars (Rec_Id));
8962 Inc_D := Make_Incomplete_Type_Declaration (Loc, Inc_T);
8963 Acc_T := Make_Temporary (Loc, 'S');
8964 Acc_Def :=
8965 Make_Access_To_Object_Definition (Loc,
8966 Subtype_Indication => New_Occurrence_Of (Inc_T, Loc));
8967 Acc_D :=
8968 Make_Full_Type_Declaration (Loc,
8969 Defining_Identifier => Acc_T,
8970 Type_Definition => Acc_Def);
8972 Insert_Before (Rec_Decl, Inc_D);
8973 Analyze (Inc_D);
8975 Insert_Before (Rec_Decl, Acc_D);
8976 Analyze (Acc_D);
8977 end if;
8979 Set_Access_Definition (Comp, Empty);
8980 Set_Subtype_Indication (Comp, New_Occurrence_Of (Acc_T, Loc));
8981 end Replace_Access_Definition;
8983 -- Local variables
8985 Body_Arr : Node_Id;
8986 Body_Id : Entity_Id;
8987 Cdecls : List_Id;
8988 Comp : Node_Id;
8989 Expr : Node_Id;
8990 New_Priv : Node_Id;
8991 Obj_Def : Node_Id;
8992 Object_Comp : Node_Id;
8993 Priv : Node_Id;
8994 Sub : Node_Id;
8996 -- Start of processing for Expand_N_Protected_Type_Declaration
8998 begin
8999 if Present (Corresponding_Record_Type (Prot_Typ)) then
9000 return;
9001 else
9002 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9003 Rec_Id := Defining_Identifier (Rec_Decl);
9004 end if;
9006 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9008 Qualify_Entity_Names (N);
9010 -- If the type has discriminants, their occurrences in the declaration
9011 -- have been replaced by the corresponding discriminals. For components
9012 -- that are constrained by discriminants, their homologues in the
9013 -- corresponding record type must refer to the discriminants of that
9014 -- record, so we must apply a new renaming to subtypes_indications:
9016 -- protected discriminant => discriminal => record discriminant
9018 -- This replacement is not applied to default expressions, for which
9019 -- the discriminal is correct.
9021 if Has_Discriminants (Prot_Typ) then
9022 declare
9023 Disc : Entity_Id;
9024 Decl : Node_Id;
9026 begin
9027 Disc := First_Discriminant (Prot_Typ);
9028 Decl := First (Discriminant_Specifications (Rec_Decl));
9029 while Present (Disc) loop
9030 Append_Elmt (Discriminal (Disc), Discr_Map);
9031 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9032 Next_Discriminant (Disc);
9033 Next (Decl);
9034 end loop;
9035 end;
9036 end if;
9038 -- Fill in the component declarations
9040 -- Add components for entry families. For each entry family, create an
9041 -- anonymous type declaration with the same size, and analyze the type.
9043 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9045 pragma Assert (Present (Pdef));
9047 Insert_After (Current_Node, Rec_Decl);
9048 Current_Node := Rec_Decl;
9050 -- Add private field components
9052 Priv := First (Private_Declarations (Pdef));
9053 while Present (Priv) loop
9054 if Nkind (Priv) = N_Component_Declaration then
9055 if not Static_Component_Size (Defining_Identifier (Priv)) then
9057 -- When compiling for a restricted profile, the private
9058 -- components must have a static size. If not, this is an error
9059 -- for a single protected declaration, and rates a warning on a
9060 -- protected type declaration.
9062 if not Comes_From_Source (Prot_Typ) then
9064 -- It's ok to be checking this restriction at expansion
9065 -- time, because this is only for the restricted profile,
9066 -- which is not subject to strict RM conformance, so it
9067 -- is OK to miss this check in -gnatc mode.
9069 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9070 Check_Restriction
9071 (No_Implicit_Protected_Object_Allocations, Priv);
9073 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9074 if not Discriminated_Size (Defining_Identifier (Priv))
9075 then
9076 -- Any object of the type will be non-static
9078 Error_Msg_N ("component has non-static size??", Priv);
9079 Error_Msg_NE
9080 ("\creation of protected object of type& will "
9081 & "violate restriction "
9082 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9083 else
9084 -- Object will be non-static if discriminants are
9086 Error_Msg_NE
9087 ("creation of protected object of type& with "
9088 & "non-static discriminants will violate "
9089 & "restriction No_Implicit_Heap_Allocations??",
9090 Priv, Prot_Typ);
9091 end if;
9093 -- Likewise for No_Implicit_Protected_Object_Allocations
9095 elsif Restriction_Active
9096 (No_Implicit_Protected_Object_Allocations)
9097 then
9098 if not Discriminated_Size (Defining_Identifier (Priv)) then
9099 -- Any object of the type will be non-static
9101 Error_Msg_N ("component has non-static size??", Priv);
9102 Error_Msg_NE
9103 ("\creation of protected object of type& will violate "
9104 & "restriction "
9105 & "No_Implicit_Protected_Object_Allocations??",
9106 Priv, Prot_Typ);
9107 else
9108 -- Object will be non-static if discriminants are
9110 Error_Msg_NE
9111 ("creation of protected object of type& with "
9112 & "non-static discriminants will violate restriction "
9113 & "No_Implicit_Protected_Object_Allocations??",
9114 Priv, Prot_Typ);
9115 end if;
9116 end if;
9117 end if;
9119 -- The component definition consists of a subtype indication, or
9120 -- (in Ada 2005) an access definition. Make a copy of the proper
9121 -- definition.
9123 declare
9124 Old_Comp : constant Node_Id := Component_Definition (Priv);
9125 Oent : constant Entity_Id := Defining_Identifier (Priv);
9126 Nent : constant Entity_Id :=
9127 Make_Defining_Identifier (Sloc (Oent),
9128 Chars => Chars (Oent));
9129 New_Comp : Node_Id;
9131 begin
9132 if Present (Subtype_Indication (Old_Comp)) then
9133 New_Comp :=
9134 Make_Component_Definition (Sloc (Oent),
9135 Aliased_Present => False,
9136 Subtype_Indication =>
9137 New_Copy_Tree
9138 (Subtype_Indication (Old_Comp), Discr_Map));
9139 else
9140 New_Comp :=
9141 Make_Component_Definition (Sloc (Oent),
9142 Aliased_Present => False,
9143 Access_Definition =>
9144 New_Copy_Tree
9145 (Access_Definition (Old_Comp), Discr_Map));
9147 -- A self-reference in the private part becomes a
9148 -- self-reference to the corresponding record.
9150 if Entity (Subtype_Mark (Access_Definition (New_Comp)))
9151 = Prot_Typ
9152 then
9153 Replace_Access_Definition (New_Comp);
9154 end if;
9155 end if;
9157 New_Priv :=
9158 Make_Component_Declaration (Loc,
9159 Defining_Identifier => Nent,
9160 Component_Definition => New_Comp,
9161 Expression => Expression (Priv));
9163 Set_Has_Per_Object_Constraint (Nent,
9164 Has_Per_Object_Constraint (Oent));
9166 Append_To (Cdecls, New_Priv);
9167 end;
9169 elsif Nkind (Priv) = N_Subprogram_Declaration then
9171 -- Make the unprotected version of the subprogram available for
9172 -- expansion of intra object calls. There is need for a protected
9173 -- version only if the subprogram is an interrupt handler,
9174 -- otherwise this operation can only be called from within the
9175 -- body.
9177 Sub :=
9178 Make_Subprogram_Declaration (Loc,
9179 Specification =>
9180 Build_Protected_Sub_Specification
9181 (Priv, Prot_Typ, Unprotected_Mode));
9183 Insert_After (Current_Node, Sub);
9184 Analyze (Sub);
9186 Set_Protected_Body_Subprogram
9187 (Defining_Unit_Name (Specification (Priv)),
9188 Defining_Unit_Name (Specification (Sub)));
9189 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9190 Current_Node := Sub;
9192 Sub :=
9193 Make_Subprogram_Declaration (Loc,
9194 Specification =>
9195 Build_Protected_Sub_Specification
9196 (Priv, Prot_Typ, Protected_Mode));
9198 Insert_After (Current_Node, Sub);
9199 Analyze (Sub);
9200 Current_Node := Sub;
9202 if Is_Interrupt_Handler
9203 (Defining_Unit_Name (Specification (Priv)))
9204 then
9205 if not Restricted_Profile then
9206 Register_Handler;
9207 end if;
9208 end if;
9209 end if;
9211 Next (Priv);
9212 end loop;
9214 -- Except for the lock-free implementation, append the _Object field
9215 -- with the right type to the component list. We need to compute the
9216 -- number of entries, and in some cases the number of Attach_Handler
9217 -- pragmas.
9219 if not Lock_Free_Active then
9220 declare
9221 Entry_Count_Expr : constant Node_Id :=
9222 Build_Entry_Count_Expression
9223 (Prot_Typ, Cdecls, Loc);
9224 Num_Attach_Handler : Nat := 0;
9225 Protection_Subtype : Node_Id;
9226 Ritem : Node_Id;
9228 begin
9229 if Has_Attach_Handler (Prot_Typ) then
9230 Ritem := First_Rep_Item (Prot_Typ);
9231 while Present (Ritem) loop
9232 if Nkind (Ritem) = N_Pragma
9233 and then Pragma_Name (Ritem) = Name_Attach_Handler
9234 then
9235 Num_Attach_Handler := Num_Attach_Handler + 1;
9236 end if;
9238 Next_Rep_Item (Ritem);
9239 end loop;
9240 end if;
9242 -- Determine the proper protection type. There are two special
9243 -- cases: 1) when the protected type has dynamic interrupt
9244 -- handlers, and 2) when it has static handlers and we use a
9245 -- restricted profile.
9247 if Has_Attach_Handler (Prot_Typ)
9248 and then not Restricted_Profile
9249 then
9250 Protection_Subtype :=
9251 Make_Subtype_Indication (Loc,
9252 Subtype_Mark =>
9253 New_Occurrence_Of
9254 (RTE (RE_Static_Interrupt_Protection), Loc),
9255 Constraint =>
9256 Make_Index_Or_Discriminant_Constraint (Loc,
9257 Constraints => New_List (
9258 Entry_Count_Expr,
9259 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9261 elsif Has_Interrupt_Handler (Prot_Typ)
9262 and then not Restriction_Active (No_Dynamic_Attachment)
9263 then
9264 Protection_Subtype :=
9265 Make_Subtype_Indication (Loc,
9266 Subtype_Mark =>
9267 New_Occurrence_Of
9268 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9269 Constraint =>
9270 Make_Index_Or_Discriminant_Constraint (Loc,
9271 Constraints => New_List (Entry_Count_Expr)));
9273 else
9274 case Corresponding_Runtime_Package (Prot_Typ) is
9275 when System_Tasking_Protected_Objects_Entries =>
9276 Protection_Subtype :=
9277 Make_Subtype_Indication (Loc,
9278 Subtype_Mark =>
9279 New_Occurrence_Of
9280 (RTE (RE_Protection_Entries), Loc),
9281 Constraint =>
9282 Make_Index_Or_Discriminant_Constraint (Loc,
9283 Constraints => New_List (Entry_Count_Expr)));
9285 when System_Tasking_Protected_Objects_Single_Entry =>
9286 Protection_Subtype :=
9287 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9289 when System_Tasking_Protected_Objects =>
9290 Protection_Subtype :=
9291 New_Occurrence_Of (RTE (RE_Protection), Loc);
9293 when others =>
9294 raise Program_Error;
9295 end case;
9296 end if;
9298 Object_Comp :=
9299 Make_Component_Declaration (Loc,
9300 Defining_Identifier =>
9301 Make_Defining_Identifier (Loc, Name_uObject),
9302 Component_Definition =>
9303 Make_Component_Definition (Loc,
9304 Aliased_Present => True,
9305 Subtype_Indication => Protection_Subtype));
9306 end;
9308 -- Put the _Object component after the private component so that it
9309 -- be finalized early as required by 9.4 (20)
9311 Append_To (Cdecls, Object_Comp);
9312 end if;
9314 -- Analyze the record declaration immediately after construction,
9315 -- because the initialization procedure is needed for single object
9316 -- declarations before the next entity is analyzed (the freeze call
9317 -- that generates this initialization procedure is found below).
9319 Analyze (Rec_Decl, Suppress => All_Checks);
9321 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9322 -- the corresponding record is frozen. If any wrappers are generated,
9323 -- Current_Node is updated accordingly.
9325 if Ada_Version >= Ada_2005 then
9326 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9327 end if;
9329 -- Collect pointers to entry bodies and their barriers, to be placed
9330 -- in the Entry_Bodies_Array for the type. For each entry/family we
9331 -- add an expression to the aggregate which is the initial value of
9332 -- this array. The array is declared after all protected subprograms.
9334 if Has_Entries (Prot_Typ) then
9335 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9336 else
9337 Entries_Aggr := Empty;
9338 end if;
9340 -- Build two new procedure specifications for each protected subprogram;
9341 -- one to call from outside the object and one to call from inside.
9342 -- Build a barrier function and an entry body action procedure
9343 -- specification for each protected entry. Initialize the entry body
9344 -- array. If subprogram is flagged as eliminated, do not generate any
9345 -- internal operations.
9347 E_Count := 0;
9348 Comp := First (Visible_Declarations (Pdef));
9349 while Present (Comp) loop
9350 if Nkind (Comp) = N_Subprogram_Declaration then
9351 Sub :=
9352 Make_Subprogram_Declaration (Loc,
9353 Specification =>
9354 Build_Protected_Sub_Specification
9355 (Comp, Prot_Typ, Unprotected_Mode));
9357 Insert_After (Current_Node, Sub);
9358 Analyze (Sub);
9360 Set_Protected_Body_Subprogram
9361 (Defining_Unit_Name (Specification (Comp)),
9362 Defining_Unit_Name (Specification (Sub)));
9363 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9365 -- Make the protected version of the subprogram available for
9366 -- expansion of external calls.
9368 Current_Node := Sub;
9370 Sub :=
9371 Make_Subprogram_Declaration (Loc,
9372 Specification =>
9373 Build_Protected_Sub_Specification
9374 (Comp, Prot_Typ, Protected_Mode));
9376 Insert_After (Current_Node, Sub);
9377 Analyze (Sub);
9379 Current_Node := Sub;
9381 -- Generate an overriding primitive operation specification for
9382 -- this subprogram if the protected type implements an interface
9383 -- and Build_Wrapper_Spec did not generate its wrapper.
9385 if Ada_Version >= Ada_2005
9386 and then
9387 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9388 then
9389 declare
9390 Found : Boolean := False;
9391 Prim_Elmt : Elmt_Id;
9392 Prim_Op : Node_Id;
9394 begin
9395 Prim_Elmt :=
9396 First_Elmt
9397 (Primitive_Operations
9398 (Corresponding_Record_Type (Prot_Typ)));
9400 while Present (Prim_Elmt) loop
9401 Prim_Op := Node (Prim_Elmt);
9403 if Is_Primitive_Wrapper (Prim_Op)
9404 and then Wrapped_Entity (Prim_Op) =
9405 Defining_Entity (Specification (Comp))
9406 then
9407 Found := True;
9408 exit;
9409 end if;
9411 Next_Elmt (Prim_Elmt);
9412 end loop;
9414 if not Found then
9415 Sub :=
9416 Make_Subprogram_Declaration (Loc,
9417 Specification =>
9418 Build_Protected_Sub_Specification
9419 (Comp, Prot_Typ, Dispatching_Mode));
9421 Insert_After (Current_Node, Sub);
9422 Analyze (Sub);
9424 Current_Node := Sub;
9425 end if;
9426 end;
9427 end if;
9429 -- If a pragma Interrupt_Handler applies, build and add a call to
9430 -- Register_Interrupt_Handler to the freezing actions of the
9431 -- protected version (Current_Node) of the subprogram:
9433 -- system.interrupts.register_interrupt_handler
9434 -- (prot_procP'address);
9436 if not Restricted_Profile
9437 and then Is_Interrupt_Handler
9438 (Defining_Unit_Name (Specification (Comp)))
9439 then
9440 Register_Handler;
9441 end if;
9443 elsif Nkind (Comp) = N_Entry_Declaration then
9444 Expand_Entry_Declaration (Comp);
9445 end if;
9447 Next (Comp);
9448 end loop;
9450 -- If there are some private entry declarations, expand it as if they
9451 -- were visible entries.
9453 Comp := First (Private_Declarations (Pdef));
9454 while Present (Comp) loop
9455 if Nkind (Comp) = N_Entry_Declaration then
9456 Expand_Entry_Declaration (Comp);
9457 end if;
9459 Next (Comp);
9460 end loop;
9462 -- Create the declaration of an array object which contains the values
9463 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9464 -- type. This object is later passed to the appropriate protected object
9465 -- initialization routine.
9467 if Has_Entries (Prot_Typ)
9468 and then Corresponding_Runtime_Package (Prot_Typ) =
9469 System_Tasking_Protected_Objects_Entries
9470 then
9471 declare
9472 Count : Int;
9473 Item : Entity_Id;
9474 Max_Vals : Node_Id;
9475 Maxes : List_Id;
9476 Maxes_Id : Entity_Id;
9477 Need_Array : Boolean := False;
9479 begin
9480 -- First check if there is any Max_Queue_Length pragma
9482 Item := First_Entity (Prot_Typ);
9483 while Present (Item) loop
9484 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9485 Need_Array := True;
9486 exit;
9487 end if;
9489 Next_Entity (Item);
9490 end loop;
9492 -- Gather the Max_Queue_Length values of all entries in a list. A
9493 -- value of zero indicates that the entry has no limitation on its
9494 -- queue length.
9496 if Need_Array then
9497 Count := 0;
9498 Item := First_Entity (Prot_Typ);
9499 Maxes := New_List;
9500 while Present (Item) loop
9501 if Is_Entry (Item) then
9502 Count := Count + 1;
9503 Append_To (Maxes,
9504 Make_Integer_Literal
9505 (Loc, Get_Max_Queue_Length (Item)));
9506 end if;
9508 Next_Entity (Item);
9509 end loop;
9511 -- Create the declaration of the array object. Generate:
9513 -- Maxes_Id : aliased constant
9514 -- Protected_Entry_Queue_Max_Array
9515 -- (1 .. Count) := (..., ...);
9517 Maxes_Id :=
9518 Make_Defining_Identifier (Loc,
9519 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9521 Max_Vals :=
9522 Make_Object_Declaration (Loc,
9523 Defining_Identifier => Maxes_Id,
9524 Aliased_Present => True,
9525 Constant_Present => True,
9526 Object_Definition =>
9527 Make_Subtype_Indication (Loc,
9528 Subtype_Mark =>
9529 New_Occurrence_Of
9530 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9531 Constraint =>
9532 Make_Index_Or_Discriminant_Constraint (Loc,
9533 Constraints => New_List (
9534 Make_Range (Loc,
9535 Make_Integer_Literal (Loc, 1),
9536 Make_Integer_Literal (Loc, Count))))),
9537 Expression => Make_Aggregate (Loc, Maxes));
9539 -- A pointer to this array will be placed in the corresponding
9540 -- record by its initialization procedure so this needs to be
9541 -- analyzed here.
9543 Insert_After (Current_Node, Max_Vals);
9544 Current_Node := Max_Vals;
9545 Analyze (Max_Vals);
9547 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9548 end if;
9549 end;
9550 end if;
9552 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9553 -- all protected subprograms have been collected.
9555 if Has_Entries (Prot_Typ) then
9556 Body_Id :=
9557 Make_Defining_Identifier (Sloc (Prot_Typ),
9558 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9560 case Corresponding_Runtime_Package (Prot_Typ) is
9561 when System_Tasking_Protected_Objects_Entries =>
9562 Expr := Entries_Aggr;
9563 Obj_Def :=
9564 Make_Subtype_Indication (Loc,
9565 Subtype_Mark =>
9566 New_Occurrence_Of
9567 (RTE (RE_Protected_Entry_Body_Array), Loc),
9568 Constraint =>
9569 Make_Index_Or_Discriminant_Constraint (Loc,
9570 Constraints => New_List (
9571 Make_Range (Loc,
9572 Make_Integer_Literal (Loc, 1),
9573 Make_Integer_Literal (Loc, E_Count)))));
9575 when System_Tasking_Protected_Objects_Single_Entry =>
9576 Expr := Remove_Head (Expressions (Entries_Aggr));
9577 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9579 when others =>
9580 raise Program_Error;
9581 end case;
9583 Body_Arr :=
9584 Make_Object_Declaration (Loc,
9585 Defining_Identifier => Body_Id,
9586 Aliased_Present => True,
9587 Constant_Present => True,
9588 Object_Definition => Obj_Def,
9589 Expression => Expr);
9591 -- A pointer to this array will be placed in the corresponding record
9592 -- by its initialization procedure so this needs to be analyzed here.
9594 Insert_After (Current_Node, Body_Arr);
9595 Current_Node := Body_Arr;
9596 Analyze (Body_Arr);
9598 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9600 -- Finally, build the function that maps an entry index into the
9601 -- corresponding body. A pointer to this function is placed in each
9602 -- object of the type. Except for a ravenscar-like profile (no abort,
9603 -- no entry queue, 1 entry)
9605 if Corresponding_Runtime_Package (Prot_Typ) =
9606 System_Tasking_Protected_Objects_Entries
9607 then
9608 Sub :=
9609 Make_Subprogram_Declaration (Loc,
9610 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9612 Insert_After (Current_Node, Sub);
9613 Analyze (Sub);
9614 end if;
9615 end if;
9616 end Expand_N_Protected_Type_Declaration;
9618 --------------------------------
9619 -- Expand_N_Requeue_Statement --
9620 --------------------------------
9622 -- A nondispatching requeue statement is expanded into one of four GNARLI
9623 -- operations, depending on the source and destination (task or protected
9624 -- object). A dispatching requeue statement is expanded into a call to the
9625 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9626 -- jump around the remainder of processing for the original entry and, if
9627 -- the destination is (different) protected object, to attempt to service
9628 -- it. The following illustrates the various cases:
9630 -- procedure entE
9631 -- (O : System.Address;
9632 -- P : System.Address;
9633 -- E : Protected_Entry_Index)
9634 -- is
9635 -- <discriminant renamings>
9636 -- <private object renamings>
9637 -- type poVP is access poV;
9638 -- _object : ptVP := ptVP!(O);
9640 -- begin
9641 -- begin
9642 -- <start of statement sequence for entry>
9644 -- -- Requeue from one protected entry body to another protected
9645 -- -- entry.
9647 -- Requeue_Protected_Entry (
9648 -- _object._object'Access,
9649 -- new._object'Access,
9650 -- E,
9651 -- Abort_Present);
9652 -- return;
9654 -- <some more of the statement sequence for entry>
9656 -- -- Requeue from an entry body to a task entry
9658 -- Requeue_Protected_To_Task_Entry (
9659 -- New._task_id,
9660 -- E,
9661 -- Abort_Present);
9662 -- return;
9664 -- <rest of statement sequence for entry>
9665 -- Complete_Entry_Body (_object._object);
9667 -- exception
9668 -- when all others =>
9669 -- Exceptional_Complete_Entry_Body (
9670 -- _object._object, Get_GNAT_Exception);
9671 -- end;
9672 -- end entE;
9674 -- Requeue of a task entry call to a task entry
9676 -- Accept_Call (E, Ann);
9677 -- <start of statement sequence for accept statement>
9678 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9679 -- goto Lnn;
9680 -- <rest of statement sequence for accept statement>
9681 -- <<Lnn>>
9682 -- Complete_Rendezvous;
9684 -- exception
9685 -- when all others =>
9686 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9688 -- Requeue of a task entry call to a protected entry
9690 -- Accept_Call (E, Ann);
9691 -- <start of statement sequence for accept statement>
9692 -- Requeue_Task_To_Protected_Entry (
9693 -- new._object'Access,
9694 -- E,
9695 -- Abort_Present);
9696 -- newS (new, Pnn);
9697 -- goto Lnn;
9698 -- <rest of statement sequence for accept statement>
9699 -- <<Lnn>>
9700 -- Complete_Rendezvous;
9702 -- exception
9703 -- when all others =>
9704 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9706 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9707 -- marked by pragma Implemented (XXX, By_Entry).
9709 -- The requeue is inside a protected entry:
9711 -- procedure entE
9712 -- (O : System.Address;
9713 -- P : System.Address;
9714 -- E : Protected_Entry_Index)
9715 -- is
9716 -- <discriminant renamings>
9717 -- <private object renamings>
9718 -- type poVP is access poV;
9719 -- _object : ptVP := ptVP!(O);
9721 -- begin
9722 -- begin
9723 -- <start of statement sequence for entry>
9725 -- _Disp_Requeue
9726 -- (<interface class-wide object>,
9727 -- True,
9728 -- _object'Address,
9729 -- Ada.Tags.Get_Offset_Index
9730 -- (Tag (_object),
9731 -- <interface dispatch table index of target entry>),
9732 -- Abort_Present);
9733 -- return;
9735 -- <rest of statement sequence for entry>
9736 -- Complete_Entry_Body (_object._object);
9738 -- exception
9739 -- when all others =>
9740 -- Exceptional_Complete_Entry_Body (
9741 -- _object._object, Get_GNAT_Exception);
9742 -- end;
9743 -- end entE;
9745 -- The requeue is inside a task entry:
9747 -- Accept_Call (E, Ann);
9748 -- <start of statement sequence for accept statement>
9749 -- _Disp_Requeue
9750 -- (<interface class-wide object>,
9751 -- False,
9752 -- null,
9753 -- Ada.Tags.Get_Offset_Index
9754 -- (Tag (_object),
9755 -- <interface dispatch table index of target entrt>),
9756 -- Abort_Present);
9757 -- newS (new, Pnn);
9758 -- goto Lnn;
9759 -- <rest of statement sequence for accept statement>
9760 -- <<Lnn>>
9761 -- Complete_Rendezvous;
9763 -- exception
9764 -- when all others =>
9765 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9767 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9768 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9769 -- statement is replaced by a dispatching call with actual parameters taken
9770 -- from the inner-most accept statement or entry body.
9772 -- Target.Primitive (Param1, ..., ParamN);
9774 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9775 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9776 -- at all.
9778 -- declare
9779 -- S : constant Offset_Index :=
9780 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9781 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9783 -- begin
9784 -- if C = POK_Protected_Entry
9785 -- or else C = POK_Task_Entry
9786 -- then
9787 -- <statements for dispatching requeue>
9789 -- elsif C = POK_Protected_Procedure then
9790 -- <dispatching call equivalent>
9792 -- else
9793 -- raise Program_Error;
9794 -- end if;
9795 -- end;
9797 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9798 Loc : constant Source_Ptr := Sloc (N);
9799 Conc_Typ : Entity_Id;
9800 Concval : Node_Id;
9801 Ename : Node_Id;
9802 Enc_Subp : Entity_Id;
9803 Index : Node_Id;
9804 Old_Typ : Entity_Id;
9806 function Build_Dispatching_Call_Equivalent return Node_Id;
9807 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9808 -- the form Concval.Ename. It is statically known that Ename is allowed
9809 -- to be implemented by a protected procedure. Create a dispatching call
9810 -- equivalent of Concval.Ename taking the actual parameters from the
9811 -- inner-most accept statement or entry body.
9813 function Build_Dispatching_Requeue return Node_Id;
9814 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9815 -- the form Concval.Ename. It is statically known that Ename is allowed
9816 -- to be implemented by a protected or a task entry. Create a call to
9817 -- primitive _Disp_Requeue which handles the low-level actions.
9819 function Build_Dispatching_Requeue_To_Any return Node_Id;
9820 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9821 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9822 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9823 -- determines at runtime whether Ename denotes an entry or a procedure
9824 -- and perform the appropriate kind of dispatching select.
9826 function Build_Normal_Requeue return Node_Id;
9827 -- N denotes a nondispatching requeue statement to either a task or a
9828 -- protected entry. Build the appropriate runtime call to perform the
9829 -- action.
9831 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9832 -- For a protected entry, create a return statement to skip the rest of
9833 -- the entry body. Otherwise, create a goto statement to skip the rest
9834 -- of a task accept statement. The lookup for the enclosing entry body
9835 -- or accept statement starts from Search.
9837 ---------------------------------------
9838 -- Build_Dispatching_Call_Equivalent --
9839 ---------------------------------------
9841 function Build_Dispatching_Call_Equivalent return Node_Id is
9842 Call_Ent : constant Entity_Id := Entity (Ename);
9843 Obj : constant Node_Id := Original_Node (Concval);
9844 Acc_Ent : Node_Id;
9845 Actuals : List_Id;
9846 Formal : Node_Id;
9847 Formals : List_Id;
9849 begin
9850 -- Climb the parent chain looking for the inner-most entry body or
9851 -- accept statement.
9853 Acc_Ent := N;
9854 while Present (Acc_Ent)
9855 and then Nkind (Acc_Ent) not in N_Accept_Statement | N_Entry_Body
9856 loop
9857 Acc_Ent := Parent (Acc_Ent);
9858 end loop;
9860 -- A requeue statement should be housed inside an entry body or an
9861 -- accept statement at some level. If this is not the case, then the
9862 -- tree is malformed.
9864 pragma Assert (Present (Acc_Ent));
9866 -- Recover the list of formal parameters
9868 if Nkind (Acc_Ent) = N_Entry_Body then
9869 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9870 end if;
9872 Formals := Parameter_Specifications (Acc_Ent);
9874 -- Create the actual parameters for the dispatching call. These are
9875 -- simply copies of the entry body or accept statement formals in the
9876 -- same order as they appear.
9878 Actuals := No_List;
9880 if Present (Formals) then
9881 Actuals := New_List;
9882 Formal := First (Formals);
9883 while Present (Formal) loop
9884 Append_To (Actuals,
9885 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9886 Next (Formal);
9887 end loop;
9888 end if;
9890 -- Generate:
9891 -- Obj.Call_Ent (Actuals);
9893 return
9894 Make_Procedure_Call_Statement (Loc,
9895 Name =>
9896 Make_Selected_Component (Loc,
9897 Prefix => Make_Identifier (Loc, Chars (Obj)),
9898 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9900 Parameter_Associations => Actuals);
9901 end Build_Dispatching_Call_Equivalent;
9903 -------------------------------
9904 -- Build_Dispatching_Requeue --
9905 -------------------------------
9907 function Build_Dispatching_Requeue return Node_Id is
9908 Params : constant List_Id := New_List;
9910 begin
9911 -- Process the "with abort" parameter
9913 Prepend_To (Params,
9914 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9916 -- Process the entry wrapper's position in the primary dispatch
9917 -- table parameter. Generate:
9919 -- Ada.Tags.Get_Entry_Index
9920 -- (T => To_Tag_Ptr (Obj'Address).all,
9921 -- Position =>
9922 -- Ada.Tags.Get_Offset_Index
9923 -- (Ada.Tags.Tag (Concval),
9924 -- <interface dispatch table position of Ename>));
9926 -- Note that Obj'Address is recursively expanded into a call to
9927 -- Base_Address (Obj).
9929 if Tagged_Type_Expansion then
9930 Prepend_To (Params,
9931 Make_Function_Call (Loc,
9932 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9933 Parameter_Associations => New_List (
9935 Make_Explicit_Dereference (Loc,
9936 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9937 Make_Attribute_Reference (Loc,
9938 Prefix => New_Copy_Tree (Concval),
9939 Attribute_Name => Name_Address))),
9941 Make_Function_Call (Loc,
9942 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9943 Parameter_Associations => New_List (
9944 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9945 Make_Integer_Literal (Loc,
9946 DT_Position (Entity (Ename))))))));
9948 -- VM targets
9950 else
9951 Prepend_To (Params,
9952 Make_Function_Call (Loc,
9953 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9954 Parameter_Associations => New_List (
9956 Make_Attribute_Reference (Loc,
9957 Prefix => Concval,
9958 Attribute_Name => Name_Tag),
9960 Make_Function_Call (Loc,
9961 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9963 Parameter_Associations => New_List (
9965 -- Obj_Tag
9967 Make_Attribute_Reference (Loc,
9968 Prefix => Concval,
9969 Attribute_Name => Name_Tag),
9971 -- Tag_Typ
9973 Make_Attribute_Reference (Loc,
9974 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9975 Attribute_Name => Name_Tag),
9977 -- Position
9979 Make_Integer_Literal (Loc,
9980 DT_Position (Entity (Ename))))))));
9981 end if;
9983 -- Specific actuals for protected to XXX requeue
9985 if Is_Protected_Type (Old_Typ) then
9986 Prepend_To (Params,
9987 Make_Attribute_Reference (Loc, -- _object'Address
9988 Prefix =>
9989 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9990 Attribute_Name => Name_Address));
9992 Prepend_To (Params, -- True
9993 New_Occurrence_Of (Standard_True, Loc));
9995 -- Specific actuals for task to XXX requeue
9997 else
9998 pragma Assert (Is_Task_Type (Old_Typ));
10000 Prepend_To (Params, -- null
10001 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10003 Prepend_To (Params, -- False
10004 New_Occurrence_Of (Standard_False, Loc));
10005 end if;
10007 -- Add the object parameter
10009 Prepend_To (Params, New_Copy_Tree (Concval));
10011 -- Generate:
10012 -- _Disp_Requeue (<Params>);
10014 -- Find entity for Disp_Requeue operation, which belongs to
10015 -- the type and may not be directly visible.
10017 declare
10018 Elmt : Elmt_Id;
10019 Op : Entity_Id := Empty;
10021 begin
10022 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10023 while Present (Elmt) loop
10024 Op := Node (Elmt);
10025 exit when Chars (Op) = Name_uDisp_Requeue;
10026 Next_Elmt (Elmt);
10027 end loop;
10029 pragma Assert (Present (Op));
10031 return
10032 Make_Procedure_Call_Statement (Loc,
10033 Name => New_Occurrence_Of (Op, Loc),
10034 Parameter_Associations => Params);
10035 end;
10036 end Build_Dispatching_Requeue;
10038 --------------------------------------
10039 -- Build_Dispatching_Requeue_To_Any --
10040 --------------------------------------
10042 function Build_Dispatching_Requeue_To_Any return Node_Id is
10043 Call_Ent : constant Entity_Id := Entity (Ename);
10044 Obj : constant Node_Id := Original_Node (Concval);
10045 Skip : constant Node_Id := Build_Skip_Statement (N);
10046 C : Entity_Id;
10047 Decls : List_Id;
10048 S : Entity_Id;
10049 Stmts : List_Id;
10051 begin
10052 Decls := New_List;
10053 Stmts := New_List;
10055 -- Dispatch table slot processing, generate:
10056 -- S : Integer;
10058 S := Build_S (Loc, Decls);
10060 -- Call kind processing, generate:
10061 -- C : Ada.Tags.Prim_Op_Kind;
10063 C := Build_C (Loc, Decls);
10065 -- Generate:
10066 -- S := Ada.Tags.Get_Offset_Index
10067 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10069 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10071 -- Generate:
10072 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10074 Append_To (Stmts,
10075 Make_Procedure_Call_Statement (Loc,
10076 Name =>
10077 New_Occurrence_Of (
10078 Find_Prim_Op (Etype (Etype (Obj)),
10079 Name_uDisp_Get_Prim_Op_Kind),
10080 Loc),
10081 Parameter_Associations => New_List (
10082 New_Copy_Tree (Obj),
10083 New_Occurrence_Of (S, Loc),
10084 New_Occurrence_Of (C, Loc))));
10086 Append_To (Stmts,
10088 -- if C = POK_Protected_Entry
10089 -- or else C = POK_Task_Entry
10090 -- then
10092 Make_Implicit_If_Statement (N,
10093 Condition =>
10094 Make_Op_Or (Loc,
10095 Left_Opnd =>
10096 Make_Op_Eq (Loc,
10097 Left_Opnd =>
10098 New_Occurrence_Of (C, Loc),
10099 Right_Opnd =>
10100 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10102 Right_Opnd =>
10103 Make_Op_Eq (Loc,
10104 Left_Opnd =>
10105 New_Occurrence_Of (C, Loc),
10106 Right_Opnd =>
10107 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10109 -- Dispatching requeue equivalent
10111 Then_Statements => New_List (
10112 Build_Dispatching_Requeue,
10113 Skip),
10115 -- elsif C = POK_Protected_Procedure then
10117 Elsif_Parts => New_List (
10118 Make_Elsif_Part (Loc,
10119 Condition =>
10120 Make_Op_Eq (Loc,
10121 Left_Opnd =>
10122 New_Occurrence_Of (C, Loc),
10123 Right_Opnd =>
10124 New_Occurrence_Of (
10125 RTE (RE_POK_Protected_Procedure), Loc)),
10127 -- Dispatching call equivalent
10129 Then_Statements => New_List (
10130 Build_Dispatching_Call_Equivalent))),
10132 -- else
10133 -- raise Program_Error;
10134 -- end if;
10136 Else_Statements => New_List (
10137 Make_Raise_Program_Error (Loc,
10138 Reason => PE_Explicit_Raise))));
10140 -- Wrap everything into a block
10142 return
10143 Make_Block_Statement (Loc,
10144 Declarations => Decls,
10145 Handled_Statement_Sequence =>
10146 Make_Handled_Sequence_Of_Statements (Loc,
10147 Statements => Stmts));
10148 end Build_Dispatching_Requeue_To_Any;
10150 --------------------------
10151 -- Build_Normal_Requeue --
10152 --------------------------
10154 function Build_Normal_Requeue return Node_Id is
10155 Params : constant List_Id := New_List;
10156 Param : Node_Id;
10157 RT_Call : Node_Id;
10159 begin
10160 -- Process the "with abort" parameter
10162 Prepend_To (Params,
10163 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10165 -- Add the index expression to the parameters. It is common among all
10166 -- four cases.
10168 Prepend_To (Params,
10169 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10171 if Is_Protected_Type (Old_Typ) then
10172 declare
10173 Self_Param : Node_Id;
10175 begin
10176 Self_Param :=
10177 Make_Attribute_Reference (Loc,
10178 Prefix =>
10179 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10180 Attribute_Name =>
10181 Name_Unchecked_Access);
10183 -- Protected to protected requeue
10185 if Is_Protected_Type (Conc_Typ) then
10186 RT_Call :=
10187 New_Occurrence_Of (
10188 RTE (RE_Requeue_Protected_Entry), Loc);
10190 Param :=
10191 Make_Attribute_Reference (Loc,
10192 Prefix =>
10193 Concurrent_Ref (Concval),
10194 Attribute_Name =>
10195 Name_Unchecked_Access);
10197 -- Protected to task requeue
10199 else pragma Assert (Is_Task_Type (Conc_Typ));
10200 RT_Call :=
10201 New_Occurrence_Of (
10202 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10204 Param := Concurrent_Ref (Concval);
10205 end if;
10207 Prepend_To (Params, Param);
10208 Prepend_To (Params, Self_Param);
10209 end;
10211 else pragma Assert (Is_Task_Type (Old_Typ));
10213 -- Task to protected requeue
10215 if Is_Protected_Type (Conc_Typ) then
10216 RT_Call :=
10217 New_Occurrence_Of (
10218 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10220 Param :=
10221 Make_Attribute_Reference (Loc,
10222 Prefix =>
10223 Concurrent_Ref (Concval),
10224 Attribute_Name =>
10225 Name_Unchecked_Access);
10227 -- Task to task requeue
10229 else pragma Assert (Is_Task_Type (Conc_Typ));
10230 RT_Call :=
10231 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10233 Param := Concurrent_Ref (Concval);
10234 end if;
10236 Prepend_To (Params, Param);
10237 end if;
10239 return
10240 Make_Procedure_Call_Statement (Loc,
10241 Name => RT_Call,
10242 Parameter_Associations => Params);
10243 end Build_Normal_Requeue;
10245 --------------------------
10246 -- Build_Skip_Statement --
10247 --------------------------
10249 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10250 Skip_Stmt : Node_Id;
10252 begin
10253 -- Build a return statement to skip the rest of the entire body
10255 if Is_Protected_Type (Old_Typ) then
10256 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10258 -- If the requeue is within a task, find the end label of the
10259 -- enclosing accept statement and create a goto statement to it.
10261 else
10262 declare
10263 Acc : Node_Id;
10264 Label : Node_Id;
10266 begin
10267 -- Climb the parent chain looking for the enclosing accept
10268 -- statement.
10270 Acc := Parent (Search);
10271 while Present (Acc)
10272 and then Nkind (Acc) /= N_Accept_Statement
10273 loop
10274 Acc := Parent (Acc);
10275 end loop;
10277 -- The last statement is the second label used for completing
10278 -- the rendezvous the usual way. The label we are looking for
10279 -- is right before it.
10281 Label :=
10282 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10284 pragma Assert (Nkind (Label) = N_Label);
10286 -- Generate a goto statement to skip the rest of the accept
10288 Skip_Stmt :=
10289 Make_Goto_Statement (Loc,
10290 Name =>
10291 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10292 end;
10293 end if;
10295 Set_Analyzed (Skip_Stmt);
10297 return Skip_Stmt;
10298 end Build_Skip_Statement;
10300 -- Start of processing for Expand_N_Requeue_Statement
10302 begin
10303 -- Extract the components of the entry call
10305 Extract_Entry (N, Concval, Ename, Index);
10306 Conc_Typ := Etype (Concval);
10308 -- Examine the scope stack in order to find nearest enclosing concurrent
10309 -- type. This will constitute our invocation source.
10311 Old_Typ := Current_Scope;
10312 while Present (Old_Typ)
10313 and then not Is_Concurrent_Type (Old_Typ)
10314 loop
10315 Old_Typ := Scope (Old_Typ);
10316 end loop;
10318 -- Obtain the innermost enclosing callable construct for use in
10319 -- generating a dynamic accessibility check.
10321 Enc_Subp := Current_Scope;
10323 if Ekind (Enc_Subp) not in Entry_Kind | Subprogram_Kind then
10324 Enc_Subp := Enclosing_Subprogram (Enc_Subp);
10325 end if;
10327 -- Generate a dynamic accessibility check on the target object
10329 Insert_Before_And_Analyze (N,
10330 Make_Raise_Program_Error (Loc,
10331 Condition =>
10332 Make_Op_Gt (Loc,
10333 Left_Opnd => Accessibility_Level (Name (N), Dynamic_Level),
10334 Right_Opnd => Make_Integer_Literal (Loc,
10335 Scope_Depth (Enc_Subp))),
10336 Reason => PE_Accessibility_Check_Failed));
10338 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10339 -- Concval.Ename where the type of Concval is class-wide concurrent
10340 -- interface.
10342 if Ada_Version >= Ada_2012
10343 and then Present (Concval)
10344 and then Is_Class_Wide_Type (Conc_Typ)
10345 and then Is_Concurrent_Interface (Conc_Typ)
10346 then
10347 declare
10348 Has_Impl : Boolean := False;
10349 Impl_Kind : Name_Id := No_Name;
10351 begin
10352 -- Check whether the Ename is flagged by pragma Implemented
10354 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10355 Has_Impl := True;
10356 Impl_Kind := Implementation_Kind (Entity (Ename));
10357 end if;
10359 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10360 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10362 if Has_Impl and then Impl_Kind = Name_By_Entry then
10363 Rewrite (N, Build_Dispatching_Requeue);
10364 Analyze (N);
10365 Insert_After (N, Build_Skip_Statement (N));
10367 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10368 -- a protected procedure. In this case the requeue is transformed
10369 -- into a dispatching call.
10371 elsif Has_Impl
10372 and then Impl_Kind = Name_By_Protected_Procedure
10373 then
10374 Rewrite (N, Build_Dispatching_Call_Equivalent);
10375 Analyze (N);
10377 -- The procedure_or_entry_NAME's implementation kind is either
10378 -- By_Any, Optional, or pragma Implemented was not applied at all.
10379 -- In this case a runtime test determines whether Ename denotes an
10380 -- entry or a protected procedure and performs the appropriate
10381 -- call.
10383 else
10384 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10385 Analyze (N);
10386 end if;
10387 end;
10389 -- Processing for regular (nondispatching) requeues
10391 else
10392 Rewrite (N, Build_Normal_Requeue);
10393 Analyze (N);
10394 Insert_After (N, Build_Skip_Statement (N));
10395 end if;
10396 end Expand_N_Requeue_Statement;
10398 -------------------------------
10399 -- Expand_N_Selective_Accept --
10400 -------------------------------
10402 procedure Expand_N_Selective_Accept (N : Node_Id) is
10403 Loc : constant Source_Ptr := Sloc (N);
10404 Alts : constant List_Id := Select_Alternatives (N);
10406 -- Note: in the below declarations a lot of new lists are allocated
10407 -- unconditionally which may well not end up being used. That's not
10408 -- a good idea since it wastes space gratuitously ???
10410 Accept_Case : List_Id;
10411 Accept_List : constant List_Id := New_List;
10413 Alt : Node_Id;
10414 Alt_List : constant List_Id := New_List;
10415 Alt_Stats : List_Id;
10416 Ann : Entity_Id := Empty;
10418 Check_Guard : Boolean := True;
10420 Decls : constant List_Id := New_List;
10421 Stats : constant List_Id := New_List;
10422 Body_List : constant List_Id := New_List;
10423 Trailing_List : constant List_Id := New_List;
10425 Choices : List_Id;
10426 Else_Present : Boolean := False;
10427 Terminate_Alt : Node_Id := Empty;
10428 Select_Mode : Node_Id;
10430 Delay_Case : List_Id;
10431 Delay_Count : Integer := 0;
10432 Delay_Val : Entity_Id;
10433 Delay_Index : Entity_Id;
10434 Delay_Min : Entity_Id;
10435 Delay_Num : Pos := 1;
10436 Delay_Alt_List : List_Id := New_List;
10437 Delay_List : constant List_Id := New_List;
10438 D : Entity_Id;
10439 M : Entity_Id;
10441 First_Delay : Boolean := True;
10442 Guard_Open : Entity_Id;
10444 End_Lab : Node_Id;
10445 Index : Pos := 1;
10446 Lab : Node_Id;
10447 Num_Alts : Nat;
10448 Num_Accept : Nat := 0;
10449 Proc : Node_Id;
10450 Time_Type : Entity_Id := Empty;
10451 Select_Call : Node_Id;
10453 Qnam : constant Entity_Id :=
10454 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10456 Xnam : constant Entity_Id :=
10457 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10459 -----------------------
10460 -- Local subprograms --
10461 -----------------------
10463 function Accept_Or_Raise return List_Id;
10464 -- For the rare case where delay alternatives all have guards, and
10465 -- all of them are closed, it is still possible that there were open
10466 -- accept alternatives with no callers. We must reexamine the
10467 -- Accept_List, and execute a selective wait with no else if some
10468 -- accept is open. If none, we raise program_error.
10470 procedure Add_Accept (Alt : Node_Id);
10471 -- Process a single accept statement in a select alternative. Build
10472 -- procedure for body of accept, and add entry to dispatch table with
10473 -- expression for guard, in preparation for call to run time select.
10475 function Make_And_Declare_Label (Num : Int) return Node_Id;
10476 -- Manufacture a label using Num as a serial number and declare it.
10477 -- The declaration is appended to Decls. The label marks the trailing
10478 -- statements of an accept or delay alternative.
10480 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10481 -- Build call to Selective_Wait runtime routine
10483 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10484 -- Add code to compare value of delay with previous values, and
10485 -- generate case entry for trailing statements.
10487 procedure Process_Accept_Alternative
10488 (Alt : Node_Id;
10489 Index : Int;
10490 Proc : Node_Id);
10491 -- Add code to call corresponding procedure, and branch to
10492 -- trailing statements, if any.
10494 ---------------------
10495 -- Accept_Or_Raise --
10496 ---------------------
10498 function Accept_Or_Raise return List_Id is
10499 Cond : Node_Id;
10500 Stats : List_Id;
10501 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10503 begin
10504 -- We generate the following:
10506 -- for J in q'range loop
10507 -- if q(J).S /=null_task_entry then
10508 -- selective_wait (simple_mode,...);
10509 -- done := True;
10510 -- exit;
10511 -- end if;
10512 -- end loop;
10514 -- if no rendez_vous then
10515 -- raise program_error;
10516 -- end if;
10518 -- Note that the code needs to know that the selector name
10519 -- in an Accept_Alternative is named S.
10521 Cond := Make_Op_Ne (Loc,
10522 Left_Opnd =>
10523 Make_Selected_Component (Loc,
10524 Prefix =>
10525 Make_Indexed_Component (Loc,
10526 Prefix => New_Occurrence_Of (Qnam, Loc),
10527 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10528 Selector_Name => Make_Identifier (Loc, Name_S)),
10529 Right_Opnd =>
10530 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10532 Stats := New_List (
10533 Make_Implicit_Loop_Statement (N,
10534 Iteration_Scheme =>
10535 Make_Iteration_Scheme (Loc,
10536 Loop_Parameter_Specification =>
10537 Make_Loop_Parameter_Specification (Loc,
10538 Defining_Identifier => J,
10539 Discrete_Subtype_Definition =>
10540 Make_Attribute_Reference (Loc,
10541 Prefix => New_Occurrence_Of (Qnam, Loc),
10542 Attribute_Name => Name_Range,
10543 Expressions => New_List (
10544 Make_Integer_Literal (Loc, 1))))),
10546 Statements => New_List (
10547 Make_Implicit_If_Statement (N,
10548 Condition => Cond,
10549 Then_Statements => New_List (
10550 Make_Select_Call (
10551 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10552 Make_Exit_Statement (Loc))))));
10554 Append_To (Stats,
10555 Make_Raise_Program_Error (Loc,
10556 Condition => Make_Op_Eq (Loc,
10557 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10558 Right_Opnd =>
10559 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10560 Reason => PE_All_Guards_Closed));
10562 return Stats;
10563 end Accept_Or_Raise;
10565 ----------------
10566 -- Add_Accept --
10567 ----------------
10569 procedure Add_Accept (Alt : Node_Id) is
10570 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10571 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10572 Eloc : constant Source_Ptr := Sloc (Ename);
10573 Eent : constant Entity_Id := Entity (Ename);
10574 Index : constant Node_Id := Entry_Index (Acc_Stm);
10576 Call : Node_Id;
10577 Expr : Node_Id;
10578 Null_Body : Node_Id;
10579 PB_Ent : Entity_Id;
10580 Proc_Body : Node_Id;
10582 -- Start of processing for Add_Accept
10584 begin
10585 if No (Ann) then
10586 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10587 end if;
10589 if Present (Condition (Alt)) then
10590 Expr :=
10591 Make_If_Expression (Eloc, New_List (
10592 Condition (Alt),
10593 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10594 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10595 else
10596 Expr := Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent));
10597 end if;
10599 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10600 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10602 -- Always add call to Abort_Undefer when generating code, since
10603 -- this is what the runtime expects (abort deferred in
10604 -- Selective_Wait). In CodePeer mode this only confuses the
10605 -- analysis with unknown calls, so don't do it.
10607 if not CodePeer_Mode then
10608 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10609 Insert_Before
10610 (First (Statements (Handled_Statement_Sequence
10611 (Accept_Statement (Alt)))),
10612 Call);
10613 Analyze (Call);
10614 end if;
10616 PB_Ent :=
10617 Make_Defining_Identifier (Eloc,
10618 New_External_Name (Chars (Ename), 'A', Num_Accept));
10620 -- Link the acceptor to the original receiving entry
10622 Mutate_Ekind (PB_Ent, E_Procedure);
10623 Set_Receiving_Entry (PB_Ent, Eent);
10625 if Comes_From_Source (Alt) then
10626 Set_Debug_Info_Needed (PB_Ent);
10627 end if;
10629 Proc_Body :=
10630 Make_Subprogram_Body (Eloc,
10631 Specification =>
10632 Make_Procedure_Specification (Eloc,
10633 Defining_Unit_Name => PB_Ent),
10634 Declarations => Declarations (Acc_Stm),
10635 Handled_Statement_Sequence =>
10636 Build_Accept_Body (Accept_Statement (Alt)));
10638 Reset_Scopes_To (Proc_Body, PB_Ent);
10640 -- During the analysis of the body of the accept statement, any
10641 -- zero cost exception handler records were collected in the
10642 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10643 -- This is where we move them to where they belong, namely the
10644 -- newly created procedure.
10646 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10647 Append (Proc_Body, Body_List);
10649 else
10650 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10652 -- if accept statement has declarations, insert above, given that
10653 -- we are not creating a body for the accept.
10655 if Present (Declarations (Acc_Stm)) then
10656 Insert_Actions (N, Declarations (Acc_Stm));
10657 end if;
10658 end if;
10660 Append_To (Accept_List,
10661 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10663 Num_Accept := Num_Accept + 1;
10664 end Add_Accept;
10666 ----------------------------
10667 -- Make_And_Declare_Label --
10668 ----------------------------
10670 function Make_And_Declare_Label (Num : Int) return Node_Id is
10671 Lab_Id : Node_Id;
10673 begin
10674 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10675 Lab :=
10676 Make_Label (Loc, Lab_Id);
10678 Append_To (Decls,
10679 Make_Implicit_Label_Declaration (Loc,
10680 Defining_Identifier =>
10681 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10682 Label_Construct => Lab));
10684 return Lab;
10685 end Make_And_Declare_Label;
10687 ----------------------
10688 -- Make_Select_Call --
10689 ----------------------
10691 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10692 Params : constant List_Id := New_List;
10694 begin
10695 Append_To (Params,
10696 Make_Attribute_Reference (Loc,
10697 Prefix => New_Occurrence_Of (Qnam, Loc),
10698 Attribute_Name => Name_Unchecked_Access));
10699 Append_To (Params, Select_Mode);
10700 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10701 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10703 return
10704 Make_Procedure_Call_Statement (Loc,
10705 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10706 Parameter_Associations => Params);
10707 end Make_Select_Call;
10709 --------------------------------
10710 -- Process_Accept_Alternative --
10711 --------------------------------
10713 procedure Process_Accept_Alternative
10714 (Alt : Node_Id;
10715 Index : Int;
10716 Proc : Node_Id)
10718 Astmt : constant Node_Id := Accept_Statement (Alt);
10719 Alt_Stats : List_Id;
10721 begin
10722 Adjust_Condition (Condition (Alt));
10724 -- Accept with body
10726 if Present (Handled_Statement_Sequence (Astmt)) then
10727 Alt_Stats :=
10728 New_List (
10729 Make_Procedure_Call_Statement (Sloc (Proc),
10730 Name =>
10731 New_Occurrence_Of
10732 (Defining_Unit_Name (Specification (Proc)),
10733 Sloc (Proc))));
10735 -- Accept with no body (followed by trailing statements)
10737 else
10738 declare
10739 Entry_Id : constant Entity_Id :=
10740 Entity (Entry_Direct_Name (Accept_Statement (Alt)));
10741 begin
10742 -- Ada 2022 (AI12-0279)
10744 if Has_Yield_Aspect (Entry_Id)
10745 and then RTE_Available (RE_Yield)
10746 then
10747 Alt_Stats :=
10748 New_List (
10749 Make_Procedure_Call_Statement (Sloc (Proc),
10750 New_Occurrence_Of (RTE (RE_Yield), Sloc (Proc))));
10751 else
10752 Alt_Stats := Empty_List;
10753 end if;
10754 end;
10755 end if;
10757 Ensure_Statement_Present (Sloc (Astmt), Alt);
10759 -- After the call, if any, branch to trailing statements, if any.
10760 -- We create a label for each, as well as the corresponding label
10761 -- declaration.
10763 if not Is_Empty_List (Statements (Alt)) then
10764 Lab := Make_And_Declare_Label (Index);
10765 Append (Lab, Trailing_List);
10766 Append_List (Statements (Alt), Trailing_List);
10767 Append_To (Trailing_List,
10768 Make_Goto_Statement (Loc,
10769 Name => New_Copy (Identifier (End_Lab))));
10771 else
10772 Lab := End_Lab;
10773 end if;
10775 Append_To (Alt_Stats,
10776 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10778 Append_To (Alt_List,
10779 Make_Case_Statement_Alternative (Loc,
10780 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10781 Statements => Alt_Stats));
10782 end Process_Accept_Alternative;
10784 -------------------------------
10785 -- Process_Delay_Alternative --
10786 -------------------------------
10788 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10789 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10790 Cond : Node_Id;
10791 Delay_Alt : List_Id;
10793 begin
10794 -- Deal with C/Fortran boolean as delay condition
10796 Adjust_Condition (Condition (Alt));
10798 -- Determine the smallest specified delay
10800 -- for each delay alternative generate:
10802 -- if guard-expression then
10803 -- Delay_Val := delay-expression;
10804 -- Guard_Open := True;
10805 -- if Delay_Val < Delay_Min then
10806 -- Delay_Min := Delay_Val;
10807 -- Delay_Index := Index;
10808 -- end if;
10809 -- end if;
10811 -- The enclosing if-statement is omitted if there is no guard
10813 if Delay_Count = 1 or else First_Delay then
10814 First_Delay := False;
10816 Delay_Alt := New_List (
10817 Make_Assignment_Statement (Loc,
10818 Name => New_Occurrence_Of (Delay_Min, Loc),
10819 Expression => Expression (Delay_Statement (Alt))));
10821 if Delay_Count > 1 then
10822 Append_To (Delay_Alt,
10823 Make_Assignment_Statement (Loc,
10824 Name => New_Occurrence_Of (Delay_Index, Loc),
10825 Expression => Make_Integer_Literal (Loc, Index)));
10826 end if;
10828 else
10829 Delay_Alt := New_List (
10830 Make_Assignment_Statement (Loc,
10831 Name => New_Occurrence_Of (Delay_Val, Loc),
10832 Expression => Expression (Delay_Statement (Alt))));
10834 if Time_Type = Standard_Duration then
10835 Cond :=
10836 Make_Op_Lt (Loc,
10837 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10838 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10840 else
10841 -- The scope of the time type must define a comparison
10842 -- operator. The scope itself may not be visible, so we
10843 -- construct a node with entity information to insure that
10844 -- semantic analysis can find the proper operator.
10846 Cond :=
10847 Make_Function_Call (Loc,
10848 Name => Make_Selected_Component (Loc,
10849 Prefix =>
10850 New_Occurrence_Of (Scope (Time_Type), Loc),
10851 Selector_Name =>
10852 Make_Operator_Symbol (Loc,
10853 Chars => Name_Op_Lt,
10854 Strval => No_String)),
10855 Parameter_Associations =>
10856 New_List (
10857 New_Occurrence_Of (Delay_Val, Loc),
10858 New_Occurrence_Of (Delay_Min, Loc)));
10860 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10861 end if;
10863 Append_To (Delay_Alt,
10864 Make_Implicit_If_Statement (N,
10865 Condition => Cond,
10866 Then_Statements => New_List (
10867 Make_Assignment_Statement (Loc,
10868 Name => New_Occurrence_Of (Delay_Min, Loc),
10869 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10871 Make_Assignment_Statement (Loc,
10872 Name => New_Occurrence_Of (Delay_Index, Loc),
10873 Expression => Make_Integer_Literal (Loc, Index)))));
10874 end if;
10876 if Check_Guard then
10877 Append_To (Delay_Alt,
10878 Make_Assignment_Statement (Loc,
10879 Name => New_Occurrence_Of (Guard_Open, Loc),
10880 Expression => New_Occurrence_Of (Standard_True, Loc)));
10881 end if;
10883 if Present (Condition (Alt)) then
10884 Delay_Alt := New_List (
10885 Make_Implicit_If_Statement (N,
10886 Condition => Condition (Alt),
10887 Then_Statements => Delay_Alt));
10888 end if;
10890 Append_List (Delay_Alt, Delay_List);
10892 Ensure_Statement_Present (Dloc, Alt);
10894 -- If the delay alternative has a statement part, add choice to the
10895 -- case statements for delays.
10897 if not Is_Empty_List (Statements (Alt)) then
10899 if Delay_Count = 1 then
10900 Append_List (Statements (Alt), Delay_Alt_List);
10902 else
10903 Append_To (Delay_Alt_List,
10904 Make_Case_Statement_Alternative (Loc,
10905 Discrete_Choices => New_List (
10906 Make_Integer_Literal (Loc, Index)),
10907 Statements => Statements (Alt)));
10908 end if;
10910 elsif Delay_Count = 1 then
10912 -- If the single delay has no trailing statements, add a branch
10913 -- to the exit label to the selective wait.
10915 Delay_Alt_List := New_List (
10916 Make_Goto_Statement (Loc,
10917 Name => New_Copy (Identifier (End_Lab))));
10919 end if;
10920 end Process_Delay_Alternative;
10922 -- Start of processing for Expand_N_Selective_Accept
10924 begin
10925 Process_Statements_For_Controlled_Objects (N);
10927 -- First insert some declarations before the select. The first is:
10929 -- Ann : Address
10931 -- This variable holds the parameters passed to the accept body. This
10932 -- declaration has already been inserted by the time we get here by
10933 -- a call to Expand_Accept_Declarations made from the semantics when
10934 -- processing the first accept statement contained in the select. We
10935 -- can find this entity as Accept_Address (E), where E is any of the
10936 -- entries references by contained accept statements.
10938 -- The first step is to scan the list of Selective_Accept_Statements
10939 -- to find this entity, and also count the number of accepts, and
10940 -- determine if terminated, delay or else is present:
10942 Num_Alts := 0;
10944 Alt := First (Alts);
10945 while Present (Alt) loop
10946 Process_Statements_For_Controlled_Objects (Alt);
10948 if Nkind (Alt) = N_Accept_Alternative then
10949 Add_Accept (Alt);
10951 elsif Nkind (Alt) = N_Delay_Alternative then
10952 Delay_Count := Delay_Count + 1;
10954 -- If the delays are relative delays, the delay expressions have
10955 -- type Standard_Duration. Otherwise they must have some time type
10956 -- recognized by GNAT.
10958 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10959 Time_Type := Standard_Duration;
10960 else
10961 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10963 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10964 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10965 then
10966 null;
10967 else
10968 -- Move this check to sem???
10969 Error_Msg_NE (
10970 "& is not a time type (RM 9.6(6))",
10971 Expression (Delay_Statement (Alt)), Time_Type);
10972 Time_Type := Standard_Duration;
10973 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10974 end if;
10975 end if;
10977 if No (Condition (Alt)) then
10979 -- This guard will always be open
10981 Check_Guard := False;
10982 end if;
10984 elsif Nkind (Alt) = N_Terminate_Alternative then
10985 Adjust_Condition (Condition (Alt));
10986 Terminate_Alt := Alt;
10987 end if;
10989 Num_Alts := Num_Alts + 1;
10990 Next (Alt);
10991 end loop;
10993 Else_Present := Present (Else_Statements (N));
10995 -- At the same time (see procedure Add_Accept) we build the accept list:
10997 -- Qnn : Accept_List (1 .. num-select) := (
10998 -- (null-body, entry-index),
10999 -- (null-body, entry-index),
11000 -- ..
11001 -- (null_body, entry-index));
11003 -- In the above declaration, null-body is True if the corresponding
11004 -- accept has no body, and false otherwise. The entry is either the
11005 -- entry index expression if there is no guard, or if a guard is
11006 -- present, then an if expression of the form:
11008 -- (if guard then entry-index else Null_Task_Entry)
11010 -- If a guard is statically known to be false, the entry can simply
11011 -- be omitted from the accept list.
11013 Append_To (Decls,
11014 Make_Object_Declaration (Loc,
11015 Defining_Identifier => Qnam,
11016 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11017 Aliased_Present => True,
11018 Expression =>
11019 Make_Qualified_Expression (Loc,
11020 Subtype_Mark =>
11021 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11022 Expression =>
11023 Make_Aggregate (Loc, Expressions => Accept_List))));
11025 -- Then we declare the variable that holds the index for the accept
11026 -- that will be selected for service:
11028 -- Xnn : Select_Index;
11030 Append_To (Decls,
11031 Make_Object_Declaration (Loc,
11032 Defining_Identifier => Xnam,
11033 Object_Definition =>
11034 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11035 Expression =>
11036 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11038 -- After this follow procedure declarations for each accept body
11040 -- procedure Pnn is
11041 -- begin
11042 -- ...
11043 -- end;
11045 -- where the ... are statements from the corresponding procedure body.
11046 -- No parameters are involved, since the parameters are passed via Ann
11047 -- and the parameter references have already been expanded to be direct
11048 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11049 -- any embedded tasking statements (which would normally be illegal in
11050 -- procedures), have been converted to calls to the tasking runtime so
11051 -- there is no problem in putting them into procedures.
11053 -- The original accept statement has been expanded into a block in
11054 -- the same fashion as for simple accepts (see Build_Accept_Body).
11056 -- Note: we don't really need to build these procedures for the case
11057 -- where no delay statement is present, but it is just as easy to
11058 -- build them unconditionally, and not significantly inefficient,
11059 -- since if they are short they will be inlined anyway.
11061 -- The procedure declarations have been assembled in Body_List
11063 -- If delays are present, we must compute the required delay.
11064 -- We first generate the declarations:
11066 -- Delay_Index : Boolean := 0;
11067 -- Delay_Min : Some_Time_Type.Time;
11068 -- Delay_Val : Some_Time_Type.Time;
11070 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11071 -- active delay that is actually chosen as the basis for the possible
11072 -- delay if an immediate rendez-vous is not possible.
11074 -- In the most common case there is a single delay statement, and this
11075 -- is handled specially.
11077 if Delay_Count > 0 then
11079 -- Generate the required declarations
11081 Delay_Val :=
11082 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11083 Delay_Index :=
11084 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11085 Delay_Min :=
11086 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11088 pragma Assert (Present (Time_Type));
11090 Append_To (Decls,
11091 Make_Object_Declaration (Loc,
11092 Defining_Identifier => Delay_Val,
11093 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11095 Append_To (Decls,
11096 Make_Object_Declaration (Loc,
11097 Defining_Identifier => Delay_Index,
11098 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11099 Expression => Make_Integer_Literal (Loc, 0)));
11101 Append_To (Decls,
11102 Make_Object_Declaration (Loc,
11103 Defining_Identifier => Delay_Min,
11104 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11105 Expression =>
11106 Unchecked_Convert_To (Time_Type,
11107 Make_Attribute_Reference (Loc,
11108 Prefix =>
11109 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11110 Attribute_Name => Name_Last))));
11112 -- Create Duration and Delay_Mode objects used for passing a delay
11113 -- value to RTS
11115 D := Make_Temporary (Loc, 'D');
11116 M := Make_Temporary (Loc, 'M');
11118 declare
11119 Discr : Entity_Id;
11121 begin
11122 -- Note that these values are defined in s-osprim.ads and must
11123 -- be kept in sync:
11125 -- Relative : constant := 0;
11126 -- Absolute_Calendar : constant := 1;
11127 -- Absolute_RT : constant := 2;
11129 if Time_Type = Standard_Duration then
11130 Discr := Make_Integer_Literal (Loc, 0);
11132 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11133 Discr := Make_Integer_Literal (Loc, 1);
11135 else
11136 pragma Assert
11137 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11138 Discr := Make_Integer_Literal (Loc, 2);
11139 end if;
11141 Append_To (Decls,
11142 Make_Object_Declaration (Loc,
11143 Defining_Identifier => D,
11144 Object_Definition =>
11145 New_Occurrence_Of (Standard_Duration, Loc)));
11147 Append_To (Decls,
11148 Make_Object_Declaration (Loc,
11149 Defining_Identifier => M,
11150 Object_Definition =>
11151 New_Occurrence_Of (Standard_Integer, Loc),
11152 Expression => Discr));
11153 end;
11155 if Check_Guard then
11156 Guard_Open :=
11157 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11159 Append_To (Decls,
11160 Make_Object_Declaration (Loc,
11161 Defining_Identifier => Guard_Open,
11162 Object_Definition =>
11163 New_Occurrence_Of (Standard_Boolean, Loc),
11164 Expression =>
11165 New_Occurrence_Of (Standard_False, Loc)));
11166 end if;
11168 -- Delay_Count is zero, don't need M and D set (suppress warning)
11170 else
11171 M := Empty;
11172 D := Empty;
11173 end if;
11175 if Present (Terminate_Alt) then
11177 -- If the terminate alternative guard is False, use
11178 -- Simple_Mode; otherwise use Terminate_Mode.
11180 if Present (Condition (Terminate_Alt)) then
11181 Select_Mode := Make_If_Expression (Loc,
11182 New_List (Condition (Terminate_Alt),
11183 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11184 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11185 else
11186 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11187 end if;
11189 elsif Else_Present or Delay_Count > 0 then
11190 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11192 else
11193 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11194 end if;
11196 Select_Call := Make_Select_Call (Select_Mode);
11197 Append (Select_Call, Stats);
11199 -- Now generate code to act on the result. There is an entry
11200 -- in this case for each accept statement with a non-null body,
11201 -- followed by a branch to the statements that follow the Accept.
11202 -- In the absence of delay alternatives, we generate:
11204 -- case X is
11205 -- when No_Rendezvous => -- omitted if simple mode
11206 -- goto Lab0;
11208 -- when 1 =>
11209 -- P1n;
11210 -- goto Lab1;
11212 -- when 2 =>
11213 -- P2n;
11214 -- goto Lab2;
11216 -- when others =>
11217 -- goto Exit;
11218 -- end case;
11220 -- Lab0: Else_Statements;
11221 -- goto exit;
11223 -- Lab1: Trailing_Statements1;
11224 -- goto Exit;
11226 -- Lab2: Trailing_Statements2;
11227 -- goto Exit;
11228 -- ...
11229 -- Exit:
11231 -- Generate label for common exit
11233 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11235 -- First entry is the default case, when no rendezvous is possible
11237 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11239 if Else_Present then
11241 -- If no rendezvous is possible, the else part is executed
11243 Lab := Make_And_Declare_Label (0);
11244 Alt_Stats := New_List (
11245 Make_Goto_Statement (Loc,
11246 Name => New_Copy (Identifier (Lab))));
11248 Append (Lab, Trailing_List);
11249 Append_List (Else_Statements (N), Trailing_List);
11250 Append_To (Trailing_List,
11251 Make_Goto_Statement (Loc,
11252 Name => New_Copy (Identifier (End_Lab))));
11253 else
11254 Alt_Stats := New_List (
11255 Make_Goto_Statement (Loc,
11256 Name => New_Copy (Identifier (End_Lab))));
11257 end if;
11259 Append_To (Alt_List,
11260 Make_Case_Statement_Alternative (Loc,
11261 Discrete_Choices => Choices,
11262 Statements => Alt_Stats));
11264 -- We make use of the fact that Accept_Index is an integer type, and
11265 -- generate successive literals for entries for each accept. Only those
11266 -- for which there is a body or trailing statements get a case entry.
11268 Alt := First (Select_Alternatives (N));
11269 Proc := First (Body_List);
11270 while Present (Alt) loop
11272 if Nkind (Alt) = N_Accept_Alternative then
11273 Process_Accept_Alternative (Alt, Index, Proc);
11274 Index := Index + 1;
11276 if Present
11277 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11278 then
11279 Next (Proc);
11280 end if;
11282 elsif Nkind (Alt) = N_Delay_Alternative then
11283 Process_Delay_Alternative (Alt, Delay_Num);
11284 Delay_Num := Delay_Num + 1;
11285 end if;
11287 Next (Alt);
11288 end loop;
11290 -- An others choice is always added to the main case, as well
11291 -- as the delay case (to satisfy the compiler).
11293 Append_To (Alt_List,
11294 Make_Case_Statement_Alternative (Loc,
11295 Discrete_Choices =>
11296 New_List (Make_Others_Choice (Loc)),
11297 Statements =>
11298 New_List (Make_Goto_Statement (Loc,
11299 Name => New_Copy (Identifier (End_Lab))))));
11301 Accept_Case := New_List (
11302 Make_Case_Statement (Loc,
11303 Expression => New_Occurrence_Of (Xnam, Loc),
11304 Alternatives => Alt_List));
11306 Append_List (Trailing_List, Accept_Case);
11307 Append_List (Body_List, Decls);
11309 -- Construct case statement for trailing statements of delay
11310 -- alternatives, if there are several of them.
11312 if Delay_Count > 1 then
11313 Append_To (Delay_Alt_List,
11314 Make_Case_Statement_Alternative (Loc,
11315 Discrete_Choices =>
11316 New_List (Make_Others_Choice (Loc)),
11317 Statements =>
11318 New_List (Make_Null_Statement (Loc))));
11320 Delay_Case := New_List (
11321 Make_Case_Statement (Loc,
11322 Expression => New_Occurrence_Of (Delay_Index, Loc),
11323 Alternatives => Delay_Alt_List));
11324 else
11325 Delay_Case := Delay_Alt_List;
11326 end if;
11328 -- If there are no delay alternatives, we append the case statement
11329 -- to the statement list.
11331 if Delay_Count = 0 then
11332 Append_List (Accept_Case, Stats);
11334 -- Delay alternatives present
11336 else
11337 -- If delay alternatives are present we generate:
11339 -- find minimum delay.
11340 -- DX := minimum delay;
11341 -- M := <delay mode>;
11342 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11343 -- DX, MX, X);
11345 -- if X = No_Rendezvous then
11346 -- case statement for delay statements.
11347 -- else
11348 -- case statement for accept alternatives.
11349 -- end if;
11351 declare
11352 Cases : Node_Id;
11353 Stmt : Node_Id;
11354 Parms : List_Id;
11355 Parm : Node_Id;
11356 Conv : Node_Id;
11358 begin
11359 -- The type of the delay expression is known to be legal
11361 if Time_Type = Standard_Duration then
11362 Conv := New_Occurrence_Of (Delay_Min, Loc);
11364 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11365 Conv := Make_Function_Call (Loc,
11366 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11367 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11369 else
11370 pragma Assert
11371 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11373 Conv := Make_Function_Call (Loc,
11374 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11375 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11376 end if;
11378 Stmt := Make_Assignment_Statement (Loc,
11379 Name => New_Occurrence_Of (D, Loc),
11380 Expression => Conv);
11382 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11384 Parms := Parameter_Associations (Select_Call);
11386 Parm := First (Parms);
11387 while Present (Parm) and then Parm /= Select_Mode loop
11388 Next (Parm);
11389 end loop;
11391 pragma Assert (Present (Parm));
11392 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11393 Analyze (Parm);
11395 -- Prepare two new parameters of Duration and Delay_Mode type
11396 -- which represent the value and the mode of the minimum delay.
11398 Next (Parm);
11399 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11400 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11402 -- Create a call to RTS
11404 Rewrite (Select_Call,
11405 Make_Procedure_Call_Statement (Loc,
11406 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11407 Parameter_Associations => Parms));
11409 -- This new call should follow the calculation of the minimum
11410 -- delay.
11412 Insert_List_Before (Select_Call, Delay_List);
11414 if Check_Guard then
11415 Stmt :=
11416 Make_Implicit_If_Statement (N,
11417 Condition => New_Occurrence_Of (Guard_Open, Loc),
11418 Then_Statements => New_List (
11419 New_Copy_Tree (Stmt),
11420 New_Copy_Tree (Select_Call)),
11421 Else_Statements => Accept_Or_Raise);
11422 Rewrite (Select_Call, Stmt);
11423 else
11424 Insert_Before (Select_Call, Stmt);
11425 end if;
11427 Cases :=
11428 Make_Implicit_If_Statement (N,
11429 Condition => Make_Op_Eq (Loc,
11430 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11431 Right_Opnd =>
11432 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11434 Then_Statements => Delay_Case,
11435 Else_Statements => Accept_Case);
11437 Append (Cases, Stats);
11438 end;
11439 end if;
11441 Append (End_Lab, Stats);
11443 -- Replace accept statement with appropriate block
11445 Rewrite (N,
11446 Make_Block_Statement (Loc,
11447 Declarations => Decls,
11448 Handled_Statement_Sequence =>
11449 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11450 Analyze (N);
11452 -- Note: have to worry more about abort deferral in above code ???
11454 -- Final step is to unstack the Accept_Address entries for all accept
11455 -- statements appearing in accept alternatives in the select statement
11457 Alt := First (Alts);
11458 while Present (Alt) loop
11459 if Nkind (Alt) = N_Accept_Alternative then
11460 Remove_Last_Elmt (Accept_Address
11461 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11462 end if;
11464 Next (Alt);
11465 end loop;
11466 end Expand_N_Selective_Accept;
11468 -------------------------------------------
11469 -- Expand_N_Single_Protected_Declaration --
11470 -------------------------------------------
11472 -- A single protected declaration should never be present after semantic
11473 -- analysis because it is transformed into a protected type declaration
11474 -- and an accompanying anonymous object. This routine ensures that the
11475 -- transformation takes place.
11477 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11478 begin
11479 raise Program_Error;
11480 end Expand_N_Single_Protected_Declaration;
11482 --------------------------------------
11483 -- Expand_N_Single_Task_Declaration --
11484 --------------------------------------
11486 -- A single task declaration should never be present after semantic
11487 -- analysis because it is transformed into a task type declaration and
11488 -- an accompanying anonymous object. This routine ensures that the
11489 -- transformation takes place.
11491 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11492 begin
11493 raise Program_Error;
11494 end Expand_N_Single_Task_Declaration;
11496 ------------------------
11497 -- Expand_N_Task_Body --
11498 ------------------------
11500 -- Given a task body
11502 -- task body tname is
11503 -- <declarations>
11504 -- begin
11505 -- <statements>
11506 -- end x;
11508 -- This expansion routine converts it into a procedure and sets the
11509 -- elaboration flag for the procedure to true, to represent the fact
11510 -- that the task body is now elaborated:
11512 -- procedure tnameB (_Task : access tnameV) is
11513 -- discriminal : dtype renames _Task.discriminant;
11515 -- procedure _clean is
11516 -- begin
11517 -- Abort_Defer.all;
11518 -- Complete_Task;
11519 -- Abort_Undefer.all;
11520 -- return;
11521 -- end _clean;
11523 -- begin
11524 -- Abort_Undefer.all;
11525 -- <declarations>
11526 -- System.Task_Stages.Complete_Activation;
11527 -- <statements>
11528 -- at end
11529 -- _clean;
11530 -- end tnameB;
11532 -- tnameE := True;
11534 -- In addition, if the task body is an activator, then a call to activate
11535 -- tasks is added at the start of the statements, before the call to
11536 -- Complete_Activation, and if in addition the task is a master then it
11537 -- must be established as a master. These calls are inserted and analyzed
11538 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11539 -- expanded.
11541 -- There is one discriminal declaration line generated for each
11542 -- discriminant that is present to provide an easy reference point for
11543 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11545 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11546 -- task body procedures have a profile (Arg : System.Address). That is
11547 -- needed because GNARLI has to use the same access-to-subprogram type
11548 -- for all task types. We depend here on knowing that in GNAT, passing
11549 -- an address argument by value is identical to passing a record value
11550 -- by access (in either case a single pointer is passed), so even though
11551 -- this procedure has the wrong profile. In fact it's all OK, since the
11552 -- callings sequence is identical.
11554 procedure Expand_N_Task_Body (N : Node_Id) is
11555 Loc : constant Source_Ptr := Sloc (N);
11556 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11557 Call : Node_Id;
11558 New_N : Node_Id;
11560 Insert_Nod : Node_Id;
11561 -- Used to determine the proper location of wrapper body insertions
11563 begin
11564 -- if no task body procedure, means we had an error in configurable
11565 -- run-time mode, and there is no point in proceeding further.
11567 if No (Task_Body_Procedure (Ttyp)) then
11568 return;
11569 end if;
11571 -- Add renaming declarations for discriminals and a declaration for the
11572 -- entry family index (if applicable).
11574 Install_Private_Data_Declarations
11575 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11577 -- Add a call to Abort_Undefer at the very beginning of the task
11578 -- body since this body is called with abort still deferred.
11580 if Abort_Allowed then
11581 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11582 Prepend (Call, Declarations (N));
11583 Analyze (Call);
11584 end if;
11586 -- Place call to Complete_Activation at the head of the statement list.
11588 if Restricted_Profile then
11589 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11590 else
11591 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11592 end if;
11594 Insert_Before
11595 (First (Statements (Handled_Statement_Sequence (N))), Call);
11596 Analyze (Call);
11598 New_N :=
11599 Make_Subprogram_Body (Loc,
11600 Specification => Build_Task_Proc_Specification (Ttyp),
11601 Declarations => Declarations (N),
11602 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11603 Set_Is_Task_Body_Procedure (New_N);
11604 Set_At_End_Proc (New_N, At_End_Proc (N));
11606 -- If the task contains generic instantiations, cleanup actions are
11607 -- delayed until after instantiation. Transfer the activation chain to
11608 -- the subprogram, to insure that the activation call is properly
11609 -- generated. It the task body contains inner tasks, indicate that the
11610 -- subprogram is a task master.
11612 if Delay_Cleanups (Ttyp) then
11613 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11614 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11615 end if;
11617 Rewrite (N, New_N);
11618 Analyze (N);
11620 -- Set elaboration flag immediately after task body. If the body is a
11621 -- subunit, the flag is set in the declarative part containing the stub.
11623 if Nkind (Parent (N)) /= N_Subunit then
11624 Insert_After (N,
11625 Make_Assignment_Statement (Loc,
11626 Name =>
11627 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11628 Expression => New_Occurrence_Of (Standard_True, Loc)));
11629 end if;
11631 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11632 -- the task body. At this point all wrapper specs have been created,
11633 -- frozen and included in the dispatch table for the task type.
11635 if Ada_Version >= Ada_2005 then
11636 if Nkind (Parent (N)) = N_Subunit then
11637 Insert_Nod := Corresponding_Stub (Parent (N));
11638 else
11639 Insert_Nod := N;
11640 end if;
11642 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11643 end if;
11644 end Expand_N_Task_Body;
11646 ------------------------------------
11647 -- Expand_N_Task_Type_Declaration --
11648 ------------------------------------
11650 -- We have several things to do. First we must create a Boolean flag used
11651 -- to mark if the body is elaborated yet. This variable gets set to True
11652 -- when the body of the task is elaborated (we can't rely on the normal
11653 -- ABE mechanism for the task body, since we need to pass an access to
11654 -- this elaboration boolean to the runtime routines).
11656 -- taskE : aliased Boolean := False;
11658 -- Next a variable is declared to hold the task stack size (either the
11659 -- default : Unspecified_Size, or a value that is set by a pragma
11660 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11661 -- the variable is initialized with this value:
11663 -- taskZ : Size_Type := Unspecified_Size;
11664 -- or
11665 -- taskZ : Size_Type := Size_Type (size_expression);
11667 -- Note: No variable is needed to hold the task relative deadline since
11668 -- its value would never be static because the parameter is of a private
11669 -- type (Ada.Real_Time.Time_Span).
11671 -- Next we create a corresponding record type declaration used to represent
11672 -- values of this task. The general form of this type declaration is
11674 -- type taskV (discriminants) is record
11675 -- _Task_Id : Task_Id;
11676 -- entry_family : array (bounds) of Void;
11677 -- _Priority : Integer := priority_expression;
11678 -- _Size : Size_Type := size_expression;
11679 -- _Secondary_Stack_Size : Size_Type := size_expression;
11680 -- _Task_Info : Task_Info_Type := task_info_expression;
11681 -- _CPU : Integer := cpu_range_expression;
11682 -- _Relative_Deadline : Time_Span := time_span_expression;
11683 -- _Domain : Dispatching_Domain := dd_expression;
11684 -- end record;
11686 -- The discriminants are present only if the corresponding task type has
11687 -- discriminants, and they exactly mirror the task type discriminants.
11689 -- The Id field is always present. It contains the Task_Id value, as set by
11690 -- the call to Create_Task. Note that although the task is limited, the
11691 -- task value record type is not limited, so there is no problem in passing
11692 -- this field as an out parameter to Create_Task.
11694 -- One entry_family component is present for each entry family in the task
11695 -- definition. The bounds correspond to the bounds of the entry family
11696 -- (which may depend on discriminants). The element type is void, since we
11697 -- only need the bounds information for determining the entry index. Note
11698 -- that the use of an anonymous array would normally be illegal in this
11699 -- context, but this is a parser check, and the semantics is quite prepared
11700 -- to handle such a case.
11702 -- The _Size field is present only if a Storage_Size pragma appears in the
11703 -- task definition. The expression captures the argument that was present
11704 -- in the pragma, and is used to override the task stack size otherwise
11705 -- associated with the task type.
11707 -- The _Secondary_Stack_Size field is present only the task entity has a
11708 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11709 -- when the record init proc is built, to capture the expression of the
11710 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11711 -- be filled here since aspect evaluations are delayed till the freeze
11712 -- point.
11714 -- The _Priority field is present only if the task entity has a Priority or
11715 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11716 -- definition clause). It will be filled at the freeze point, when the
11717 -- record init proc is built, to capture the expression of the rep item
11718 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11719 -- here since aspect evaluations are delayed till the freeze point.
11721 -- The _Task_Info field is present only if a Task_Info pragma appears in
11722 -- the task definition. The expression captures the argument that was
11723 -- present in the pragma, and is used to provide the Task_Image parameter
11724 -- to the call to Create_Task.
11726 -- The _CPU field is present only if the task entity has a CPU rep item
11727 -- (pragma, aspect specification or attribute definition clause). It will
11728 -- be filled at the freeze point, when the record init proc is built, to
11729 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11730 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11731 -- are delayed till the freeze point.
11733 -- The _Relative_Deadline field is present only if a Relative_Deadline
11734 -- pragma appears in the task definition. The expression captures the
11735 -- argument that was present in the pragma, and is used to provide the
11736 -- Relative_Deadline parameter to the call to Create_Task.
11738 -- The _Domain field is present only if the task entity has a
11739 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11740 -- definition clause). It will be filled at the freeze point, when the
11741 -- record init proc is built, to capture the expression of the rep item
11742 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11743 -- here since aspect evaluations are delayed till the freeze point.
11745 -- When a task is declared, an instance of the task value record is
11746 -- created. The elaboration of this declaration creates the correct bounds
11747 -- for the entry families, and also evaluates the size, priority, and
11748 -- task_Info expressions if needed. The initialization routine for the task
11749 -- type itself then calls Create_Task with appropriate parameters to
11750 -- initialize the value of the Task_Id field.
11752 -- Note: the address of this record is passed as the "Discriminants"
11753 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11754 -- body procedure, it does not matter that it does not quite match the
11755 -- GNARLI model of what is being passed (the record contains more than just
11756 -- the discriminants, but the discriminants can be found from the record
11757 -- value).
11759 -- The Entity_Id for this created record type is placed in the
11760 -- Corresponding_Record_Type field of the associated task type entity.
11762 -- Next we create a procedure specification for the task body procedure:
11764 -- procedure taskB (_Task : access taskV);
11766 -- Note that this must come after the record type declaration, since
11767 -- the spec refers to this type. It turns out that the initialization
11768 -- procedure for the value type references the task body spec, but that's
11769 -- fine, since it won't be generated till the freeze point for the type,
11770 -- which is certainly after the task body spec declaration.
11772 -- Finally, we set the task index value field of the entry attribute in
11773 -- the case of a simple entry.
11775 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11776 Loc : constant Source_Ptr := Sloc (N);
11777 TaskId : constant Entity_Id := Defining_Identifier (N);
11778 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11779 Tasknm : constant Name_Id := Chars (Tasktyp);
11780 Taskdef : constant Node_Id := Task_Definition (N);
11782 Body_Decl : Node_Id;
11783 Cdecls : List_Id;
11784 Decl_Stack : Node_Id;
11785 Decl_SS : Node_Id;
11786 Elab_Decl : Node_Id;
11787 Ent_Stack : Entity_Id;
11788 Proc_Spec : Node_Id;
11789 Rec_Decl : Node_Id;
11790 Rec_Ent : Entity_Id;
11791 Size_Decl : Entity_Id;
11792 Task_Size : Node_Id;
11794 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11795 -- Searches the task definition T for the first occurrence of the pragma
11796 -- Relative Deadline. The caller has ensured that the pragma is present
11797 -- in the task definition. Note that this routine cannot be implemented
11798 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11799 -- not chained because their expansion into a procedure call statement
11800 -- would cause a break in the chain.
11802 ----------------------------------
11803 -- Get_Relative_Deadline_Pragma --
11804 ----------------------------------
11806 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11807 N : Node_Id;
11809 begin
11810 N := First (Visible_Declarations (T));
11811 while Present (N) loop
11812 if Nkind (N) = N_Pragma
11813 and then Pragma_Name (N) = Name_Relative_Deadline
11814 then
11815 return N;
11816 end if;
11818 Next (N);
11819 end loop;
11821 N := First (Private_Declarations (T));
11822 while Present (N) loop
11823 if Nkind (N) = N_Pragma
11824 and then Pragma_Name (N) = Name_Relative_Deadline
11825 then
11826 return N;
11827 end if;
11829 Next (N);
11830 end loop;
11832 raise Program_Error;
11833 end Get_Relative_Deadline_Pragma;
11835 -- Start of processing for Expand_N_Task_Type_Declaration
11837 begin
11838 -- If already expanded, nothing to do
11840 if Present (Corresponding_Record_Type (Tasktyp)) then
11841 return;
11842 end if;
11844 -- Here we will do the expansion
11846 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11848 Rec_Ent := Defining_Identifier (Rec_Decl);
11849 Cdecls := Component_Items (Component_List
11850 (Type_Definition (Rec_Decl)));
11852 Qualify_Entity_Names (N);
11854 -- First create the elaboration variable
11856 Elab_Decl :=
11857 Make_Object_Declaration (Loc,
11858 Defining_Identifier =>
11859 Make_Defining_Identifier (Sloc (Tasktyp),
11860 Chars => New_External_Name (Tasknm, 'E')),
11861 Aliased_Present => True,
11862 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11863 Expression => New_Occurrence_Of (Standard_False, Loc));
11865 Insert_After (N, Elab_Decl);
11867 -- Next create the declaration of the size variable (tasknmZ)
11869 Set_Storage_Size_Variable (Tasktyp,
11870 Make_Defining_Identifier (Sloc (Tasktyp),
11871 Chars => New_External_Name (Tasknm, 'Z')));
11873 if Present (Taskdef)
11874 and then Has_Storage_Size_Pragma (Taskdef)
11875 and then
11876 Is_OK_Static_Expression
11877 (Expression
11878 (First (Pragma_Argument_Associations
11879 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11880 then
11881 Size_Decl :=
11882 Make_Object_Declaration (Loc,
11883 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11884 Object_Definition =>
11885 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11886 Expression =>
11887 Convert_To (RTE (RE_Size_Type),
11888 Relocate_Node
11889 (Expression (First (Pragma_Argument_Associations
11890 (Get_Rep_Pragma
11891 (TaskId, Name_Storage_Size)))))));
11893 else
11894 Size_Decl :=
11895 Make_Object_Declaration (Loc,
11896 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11897 Object_Definition =>
11898 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11899 Expression =>
11900 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11901 end if;
11903 Insert_After (Elab_Decl, Size_Decl);
11905 -- Next build the rest of the corresponding record declaration. This is
11906 -- done last, since the corresponding record initialization procedure
11907 -- will reference the previously created entities.
11909 -- Fill in the component declarations -- first the _Task_Id field
11911 Append_To (Cdecls,
11912 Make_Component_Declaration (Loc,
11913 Defining_Identifier =>
11914 Make_Defining_Identifier (Loc, Name_uTask_Id),
11915 Component_Definition =>
11916 Make_Component_Definition (Loc,
11917 Aliased_Present => False,
11918 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11919 Loc))));
11921 -- Declare static ATCB (that is, created by the expander) if we are
11922 -- using the Restricted run time.
11924 if Restricted_Profile then
11925 Append_To (Cdecls,
11926 Make_Component_Declaration (Loc,
11927 Defining_Identifier =>
11928 Make_Defining_Identifier (Loc, Name_uATCB),
11930 Component_Definition =>
11931 Make_Component_Definition (Loc,
11932 Aliased_Present => True,
11933 Subtype_Indication => Make_Subtype_Indication (Loc,
11934 Subtype_Mark =>
11935 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11937 Constraint =>
11938 Make_Index_Or_Discriminant_Constraint (Loc,
11939 Constraints =>
11940 New_List (Make_Integer_Literal (Loc, 0)))))));
11942 end if;
11944 -- Declare static stack (that is, created by the expander) if we are
11945 -- using the Restricted run time on a bare board configuration.
11947 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11949 -- First we need to extract the appropriate stack size
11951 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11953 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11954 declare
11955 Expr_N : constant Node_Id :=
11956 Expression (First (
11957 Pragma_Argument_Associations (
11958 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11959 Etyp : constant Entity_Id := Etype (Expr_N);
11960 P : constant Node_Id := Parent (Expr_N);
11962 begin
11963 -- The stack is defined inside the corresponding record.
11964 -- Therefore if the size of the stack is set by means of
11965 -- a discriminant, we must reference the discriminant of the
11966 -- corresponding record type.
11968 if Nkind (Expr_N) in N_Has_Entity
11969 and then Present (Discriminal_Link (Entity (Expr_N)))
11970 then
11971 Task_Size :=
11972 New_Occurrence_Of
11973 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11974 Loc);
11975 Set_Parent (Task_Size, P);
11976 Set_Etype (Task_Size, Etyp);
11977 Set_Analyzed (Task_Size);
11979 else
11980 Task_Size := New_Copy_Tree (Expr_N);
11981 end if;
11982 end;
11984 else
11985 Task_Size :=
11986 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11987 end if;
11989 Decl_Stack := Make_Component_Declaration (Loc,
11990 Defining_Identifier => Ent_Stack,
11992 Component_Definition =>
11993 Make_Component_Definition (Loc,
11994 Aliased_Present => True,
11995 Subtype_Indication => Make_Subtype_Indication (Loc,
11996 Subtype_Mark =>
11997 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11999 Constraint =>
12000 Make_Index_Or_Discriminant_Constraint (Loc,
12001 Constraints => New_List (Make_Range (Loc,
12002 Low_Bound => Make_Integer_Literal (Loc, 1),
12003 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12004 Task_Size)))))));
12006 Append_To (Cdecls, Decl_Stack);
12008 -- The appropriate alignment for the stack is ensured by the run-time
12009 -- code in charge of task creation.
12011 end if;
12013 -- Declare a static secondary stack if the conditions for a statically
12014 -- generated stack are met.
12016 if Create_Secondary_Stack_For_Task (TaskId) then
12017 declare
12018 Size_Expr : constant Node_Id :=
12019 Expression (First (
12020 Pragma_Argument_Associations (
12021 Get_Rep_Pragma (TaskId,
12022 Name_Secondary_Stack_Size))));
12024 Stack_Size : Node_Id;
12026 begin
12027 -- The secondary stack is defined inside the corresponding
12028 -- record. Therefore if the size of the stack is set by means
12029 -- of a discriminant, we must reference the discriminant of the
12030 -- corresponding record type.
12032 if Nkind (Size_Expr) in N_Has_Entity
12033 and then Present (Discriminal_Link (Entity (Size_Expr)))
12034 then
12035 Stack_Size :=
12036 New_Occurrence_Of
12037 (CR_Discriminant (Discriminal_Link (Entity (Size_Expr))),
12038 Loc);
12039 Set_Parent (Stack_Size, Parent (Size_Expr));
12040 Set_Etype (Stack_Size, Etype (Size_Expr));
12041 Set_Analyzed (Stack_Size);
12043 else
12044 Stack_Size := New_Copy_Tree (Size_Expr);
12045 end if;
12047 -- Create the secondary stack for the task
12049 Decl_SS :=
12050 Make_Component_Declaration (Loc,
12051 Defining_Identifier =>
12052 Make_Defining_Identifier (Loc, Name_uSecondary_Stack),
12053 Component_Definition =>
12054 Make_Component_Definition (Loc,
12055 Aliased_Present => True,
12056 Subtype_Indication =>
12057 Make_Subtype_Indication (Loc,
12058 Subtype_Mark =>
12059 New_Occurrence_Of (RTE (RE_SS_Stack), Loc),
12060 Constraint =>
12061 Make_Index_Or_Discriminant_Constraint (Loc,
12062 Constraints => New_List (
12063 Convert_To (RTE (RE_Size_Type),
12064 Stack_Size))))));
12066 Append_To (Cdecls, Decl_SS);
12067 end;
12068 end if;
12070 -- Add components for entry families
12072 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12074 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12075 -- item is present.
12077 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12078 Append_To (Cdecls,
12079 Make_Component_Declaration (Loc,
12080 Defining_Identifier =>
12081 Make_Defining_Identifier (Loc, Name_uPriority),
12082 Component_Definition =>
12083 Make_Component_Definition (Loc,
12084 Aliased_Present => False,
12085 Subtype_Indication =>
12086 New_Occurrence_Of (Standard_Integer, Loc))));
12087 end if;
12089 -- Add the _Size component if a Storage_Size pragma is present
12091 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12092 Append_To (Cdecls,
12093 Make_Component_Declaration (Loc,
12094 Defining_Identifier =>
12095 Make_Defining_Identifier (Loc, Name_uSize),
12097 Component_Definition =>
12098 Make_Component_Definition (Loc,
12099 Aliased_Present => False,
12100 Subtype_Indication =>
12101 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12103 Expression =>
12104 Convert_To (RTE (RE_Size_Type),
12105 New_Copy_Tree (
12106 Expression (First (
12107 Pragma_Argument_Associations (
12108 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12109 end if;
12111 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
12112 -- pragma is present.
12114 if Has_Rep_Pragma
12115 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
12116 then
12117 Append_To (Cdecls,
12118 Make_Component_Declaration (Loc,
12119 Defining_Identifier =>
12120 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
12122 Component_Definition =>
12123 Make_Component_Definition (Loc,
12124 Aliased_Present => False,
12125 Subtype_Indication =>
12126 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
12127 end if;
12129 -- Add the _Task_Info component if a Task_Info pragma is present
12131 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12132 Append_To (Cdecls,
12133 Make_Component_Declaration (Loc,
12134 Defining_Identifier =>
12135 Make_Defining_Identifier (Loc, Name_uTask_Info),
12137 Component_Definition =>
12138 Make_Component_Definition (Loc,
12139 Aliased_Present => False,
12140 Subtype_Indication =>
12141 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12143 Expression => New_Copy (
12144 Expression (First (
12145 Pragma_Argument_Associations (
12146 Get_Rep_Pragma
12147 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12148 end if;
12150 -- Add the _CPU component if a CPU rep item is present
12152 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12153 Append_To (Cdecls,
12154 Make_Component_Declaration (Loc,
12155 Defining_Identifier =>
12156 Make_Defining_Identifier (Loc, Name_uCPU),
12158 Component_Definition =>
12159 Make_Component_Definition (Loc,
12160 Aliased_Present => False,
12161 Subtype_Indication =>
12162 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12163 end if;
12165 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12166 -- present. If we are using a restricted run time this component will
12167 -- not be added (deadlines are not allowed by the Ravenscar profile),
12168 -- unless the task dispatching policy is EDF (for GNAT_Ravenscar_EDF
12169 -- profile).
12171 if (not Restricted_Profile or else Task_Dispatching_Policy = 'E')
12172 and then Present (Taskdef)
12173 and then Has_Relative_Deadline_Pragma (Taskdef)
12174 then
12175 Append_To (Cdecls,
12176 Make_Component_Declaration (Loc,
12177 Defining_Identifier =>
12178 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12180 Component_Definition =>
12181 Make_Component_Definition (Loc,
12182 Aliased_Present => False,
12183 Subtype_Indication =>
12184 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12186 Expression =>
12187 Convert_To (RTE (RE_Time_Span),
12188 New_Copy_Tree (
12189 Expression (First (
12190 Pragma_Argument_Associations (
12191 Get_Relative_Deadline_Pragma (Taskdef))))))));
12192 end if;
12194 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12195 -- item is present. If we are using a restricted run time this component
12196 -- will not be added (dispatching domains are not allowed by the
12197 -- Ravenscar profile).
12199 if not Restricted_Profile
12200 and then
12201 Has_Rep_Item
12202 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12203 then
12204 Append_To (Cdecls,
12205 Make_Component_Declaration (Loc,
12206 Defining_Identifier =>
12207 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12209 Component_Definition =>
12210 Make_Component_Definition (Loc,
12211 Aliased_Present => False,
12212 Subtype_Indication =>
12213 New_Occurrence_Of
12214 (RTE (RE_Dispatching_Domain_Access), Loc))));
12215 end if;
12217 Insert_After (Size_Decl, Rec_Decl);
12219 -- Analyze the record declaration immediately after construction,
12220 -- because the initialization procedure is needed for single task
12221 -- declarations before the next entity is analyzed.
12223 Analyze (Rec_Decl);
12225 -- Create the declaration of the task body procedure
12227 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12228 Body_Decl :=
12229 Make_Subprogram_Declaration (Loc,
12230 Specification => Proc_Spec);
12231 Set_Is_Task_Body_Procedure (Body_Decl);
12233 Insert_After (Rec_Decl, Body_Decl);
12235 -- The subprogram does not comes from source, so we have to indicate the
12236 -- need for debugging information explicitly.
12238 if Comes_From_Source (Original_Node (N)) then
12239 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12240 end if;
12242 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12243 -- the corresponding record has been frozen.
12245 if Ada_Version >= Ada_2005 then
12246 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12247 end if;
12249 -- Ada 2005 (AI-345): We must defer freezing to allow further
12250 -- declaration of primitive subprograms covering task interfaces
12252 if Ada_Version <= Ada_95 then
12254 -- Now we can freeze the corresponding record. This needs manually
12255 -- freezing, since it is really part of the task type, and the task
12256 -- type is frozen at this stage. We of course need the initialization
12257 -- procedure for this corresponding record type and we won't get it
12258 -- in time if we don't freeze now.
12260 Insert_List_After (Body_Decl, List => Freeze_Entity (Rec_Ent, N));
12261 end if;
12263 -- Complete the expansion of access types to the current task type, if
12264 -- any were declared.
12266 Expand_Previous_Access_Type (Tasktyp);
12268 -- Create wrappers for entries that have contract cases, preconditions
12269 -- and postconditions.
12271 declare
12272 Ent : Entity_Id;
12274 begin
12275 Ent := First_Entity (Tasktyp);
12276 while Present (Ent) loop
12277 if Ekind (Ent) in E_Entry | E_Entry_Family then
12278 Build_Entry_Contract_Wrapper (Ent, N);
12279 end if;
12281 Next_Entity (Ent);
12282 end loop;
12283 end;
12284 end Expand_N_Task_Type_Declaration;
12286 -------------------------------
12287 -- Expand_N_Timed_Entry_Call --
12288 -------------------------------
12290 -- A timed entry call in normal case is not implemented using ATC mechanism
12291 -- anymore for efficiency reason.
12293 -- select
12294 -- T.E;
12295 -- S1;
12296 -- or
12297 -- delay D;
12298 -- S2;
12299 -- end select;
12301 -- is expanded as follows:
12303 -- 1) When T.E is a task entry_call;
12305 -- declare
12306 -- B : Boolean;
12307 -- X : Task_Entry_Index := <entry index>;
12308 -- DX : Duration := To_Duration (D);
12309 -- M : Delay_Mode := <discriminant>;
12310 -- P : parms := (parm, parm, parm);
12312 -- begin
12313 -- Timed_Protected_Entry_Call
12314 -- (<acceptor-task>, X, P'Address, DX, M, B);
12315 -- if B then
12316 -- S1;
12317 -- else
12318 -- S2;
12319 -- end if;
12320 -- end;
12322 -- 2) When T.E is a protected entry_call;
12324 -- declare
12325 -- B : Boolean;
12326 -- X : Protected_Entry_Index := <entry index>;
12327 -- DX : Duration := To_Duration (D);
12328 -- M : Delay_Mode := <discriminant>;
12329 -- P : parms := (parm, parm, parm);
12331 -- begin
12332 -- Timed_Protected_Entry_Call
12333 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12334 -- if B then
12335 -- S1;
12336 -- else
12337 -- S2;
12338 -- end if;
12339 -- end;
12341 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12342 -- is no delay and the triggering statements are executed. We first
12343 -- determine the kind of the triggering call and then execute a
12344 -- synchronized operation or a direct call.
12346 -- declare
12347 -- B : Boolean := False;
12348 -- C : Ada.Tags.Prim_Op_Kind;
12349 -- DX : Duration := To_Duration (D)
12350 -- K : Ada.Tags.Tagged_Kind :=
12351 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12352 -- M : Integer :=...;
12353 -- P : Parameters := (Param1 .. ParamN);
12354 -- S : Integer;
12356 -- begin
12357 -- if K = Ada.Tags.TK_Limited_Tagged
12358 -- or else K = Ada.Tags.TK_Tagged
12359 -- then
12360 -- <dispatching-call>;
12361 -- B := True;
12363 -- else
12364 -- S :=
12365 -- Ada.Tags.Get_Offset_Index
12366 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12368 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12370 -- if C = POK_Protected_Entry
12371 -- or else C = POK_Task_Entry
12372 -- then
12373 -- Param1 := P.Param1;
12374 -- ...
12375 -- ParamN := P.ParamN;
12376 -- end if;
12378 -- if B then
12379 -- if C = POK_Procedure
12380 -- or else C = POK_Protected_Procedure
12381 -- or else C = POK_Task_Procedure
12382 -- then
12383 -- <dispatching-call>;
12384 -- end if;
12385 -- end if;
12386 -- end if;
12388 -- if B then
12389 -- <triggering-statements>
12390 -- else
12391 -- <timed-statements>
12392 -- end if;
12393 -- end;
12395 -- The triggering statement and the sequence of timed statements have not
12396 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12397 -- global references if within an instantiation.
12399 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12400 Actuals : List_Id;
12401 Blk_Typ : Entity_Id;
12402 Call : Node_Id;
12403 Call_Ent : Entity_Id;
12404 Conc_Typ_Stmts : List_Id;
12405 Concval : Node_Id := Empty; -- init to avoid warning
12406 D_Alt : constant Node_Id := Delay_Alternative (N);
12407 D_Conv : Node_Id;
12408 D_Disc : Node_Id;
12409 D_Stat : Node_Id := Delay_Statement (D_Alt);
12410 D_Stats : List_Id;
12411 D_Type : Entity_Id;
12412 Decls : List_Id;
12413 Dummy : Node_Id;
12414 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12415 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12416 E_Stats : List_Id;
12417 Ename : Node_Id;
12418 Formals : List_Id;
12419 Index : Node_Id;
12420 Is_Disp_Select : Boolean;
12421 Lim_Typ_Stmts : List_Id;
12422 Loc : constant Source_Ptr := Sloc (D_Stat);
12423 N_Stats : List_Id;
12424 Obj : Entity_Id;
12425 Param : Node_Id;
12426 Params : List_Id;
12427 Stmt : Node_Id;
12428 Stmts : List_Id;
12429 Unpack : List_Id;
12431 B : Entity_Id; -- Call status flag
12432 C : Entity_Id; -- Call kind
12433 D : Entity_Id; -- Delay
12434 K : Entity_Id; -- Tagged kind
12435 M : Entity_Id; -- Delay mode
12436 P : Entity_Id; -- Parameter block
12437 S : Entity_Id; -- Primitive operation slot
12439 -- Start of processing for Expand_N_Timed_Entry_Call
12441 begin
12442 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12443 -- was already reported on spec, so do not attempt to expand the call.
12445 if Restriction_Active (No_Select_Statements) then
12446 return;
12447 end if;
12449 Process_Statements_For_Controlled_Objects (E_Alt);
12450 Process_Statements_For_Controlled_Objects (D_Alt);
12452 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12454 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12455 -- may wrap them in blocks.
12457 E_Stats := Statements (E_Alt);
12458 D_Stats := Statements (D_Alt);
12460 -- The arguments in the call may require dynamic allocation, and the
12461 -- call statement may have been transformed into a block. The block
12462 -- may contain additional declarations for internal entities, and the
12463 -- original call is found by sequential search.
12465 if Nkind (E_Call) = N_Block_Statement then
12466 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12467 while Nkind (E_Call) not in
12468 N_Procedure_Call_Statement | N_Entry_Call_Statement
12469 loop
12470 Next (E_Call);
12471 end loop;
12472 end if;
12474 Is_Disp_Select :=
12475 Ada_Version >= Ada_2005
12476 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12478 if Is_Disp_Select then
12479 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12480 Decls := New_List;
12482 Stmts := New_List;
12484 -- Generate:
12485 -- B : Boolean := False;
12487 B := Build_B (Loc, Decls);
12489 -- Generate:
12490 -- C : Ada.Tags.Prim_Op_Kind;
12492 C := Build_C (Loc, Decls);
12494 -- Because the analysis of all statements was disabled, manually
12495 -- analyze the delay statement.
12497 Analyze (D_Stat);
12498 D_Stat := Original_Node (D_Stat);
12500 else
12501 -- Build an entry call using Simple_Entry_Call
12503 Extract_Entry (E_Call, Concval, Ename, Index);
12504 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12506 Decls := Declarations (E_Call);
12507 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12509 if No (Decls) then
12510 Decls := New_List;
12511 end if;
12513 -- Generate:
12514 -- B : Boolean;
12516 B := Make_Defining_Identifier (Loc, Name_uB);
12518 Prepend_To (Decls,
12519 Make_Object_Declaration (Loc,
12520 Defining_Identifier => B,
12521 Object_Definition =>
12522 New_Occurrence_Of (Standard_Boolean, Loc)));
12523 end if;
12525 -- Duration and mode processing
12527 D_Type := Base_Type (Etype (Expression (D_Stat)));
12529 -- Use the type of the delay expression (Calendar or Real_Time) to
12530 -- generate the appropriate conversion.
12532 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12533 D_Disc := Make_Integer_Literal (Loc, 0);
12534 D_Conv := Relocate_Node (Expression (D_Stat));
12536 elsif Is_RTE (D_Type, RO_CA_Time) then
12537 D_Disc := Make_Integer_Literal (Loc, 1);
12538 D_Conv :=
12539 Make_Function_Call (Loc,
12540 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12541 Parameter_Associations =>
12542 New_List (New_Copy (Expression (D_Stat))));
12544 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12545 D_Disc := Make_Integer_Literal (Loc, 2);
12546 D_Conv :=
12547 Make_Function_Call (Loc,
12548 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12549 Parameter_Associations =>
12550 New_List (New_Copy (Expression (D_Stat))));
12551 end if;
12553 D := Make_Temporary (Loc, 'D');
12555 -- Generate:
12556 -- D : Duration;
12558 Append_To (Decls,
12559 Make_Object_Declaration (Loc,
12560 Defining_Identifier => D,
12561 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12563 M := Make_Temporary (Loc, 'M');
12565 -- Generate:
12566 -- M : Integer := (0 | 1 | 2);
12568 Append_To (Decls,
12569 Make_Object_Declaration (Loc,
12570 Defining_Identifier => M,
12571 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12572 Expression => D_Disc));
12574 -- Parameter block processing
12576 -- Manually create the parameter block for dispatching calls. In the
12577 -- case of entries, the block has already been created during the call
12578 -- to Build_Simple_Entry_Call.
12580 if Is_Disp_Select then
12582 -- Compute the delay at this stage because the evaluation of its
12583 -- expression must not occur earlier (see ACVC C97302A).
12585 Append_To (Stmts,
12586 Make_Assignment_Statement (Loc,
12587 Name => New_Occurrence_Of (D, Loc),
12588 Expression => D_Conv));
12590 -- Tagged kind processing, generate:
12591 -- K : Ada.Tags.Tagged_Kind :=
12592 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12594 K := Build_K (Loc, Decls, Obj);
12596 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12597 P :=
12598 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12600 -- Dispatch table slot processing, generate:
12601 -- S : Integer;
12603 S := Build_S (Loc, Decls);
12605 -- Generate:
12606 -- S := Ada.Tags.Get_Offset_Index
12607 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12609 Conc_Typ_Stmts :=
12610 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12612 -- Generate:
12613 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12615 -- where Obj is the controlling formal parameter, S is the dispatch
12616 -- table slot number of the dispatching operation, P is the wrapped
12617 -- parameter block, D is the duration, M is the duration mode, C is
12618 -- the call kind and B is the call status.
12620 Params := New_List;
12622 Append_To (Params, New_Copy_Tree (Obj));
12623 Append_To (Params, New_Occurrence_Of (S, Loc));
12624 Append_To (Params,
12625 Make_Attribute_Reference (Loc,
12626 Prefix => New_Occurrence_Of (P, Loc),
12627 Attribute_Name => Name_Address));
12628 Append_To (Params, New_Occurrence_Of (D, Loc));
12629 Append_To (Params, New_Occurrence_Of (M, Loc));
12630 Append_To (Params, New_Occurrence_Of (C, Loc));
12631 Append_To (Params, New_Occurrence_Of (B, Loc));
12633 Append_To (Conc_Typ_Stmts,
12634 Make_Procedure_Call_Statement (Loc,
12635 Name =>
12636 New_Occurrence_Of
12637 (Find_Prim_Op
12638 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12639 Parameter_Associations => Params));
12641 -- Generate:
12642 -- if C = POK_Protected_Entry
12643 -- or else C = POK_Task_Entry
12644 -- then
12645 -- Param1 := P.Param1;
12646 -- ...
12647 -- ParamN := P.ParamN;
12648 -- end if;
12650 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12652 -- Generate the if statement only when the packed parameters need
12653 -- explicit assignments to their corresponding actuals.
12655 if Present (Unpack) then
12656 Append_To (Conc_Typ_Stmts,
12657 Make_Implicit_If_Statement (N,
12659 Condition =>
12660 Make_Or_Else (Loc,
12661 Left_Opnd =>
12662 Make_Op_Eq (Loc,
12663 Left_Opnd => New_Occurrence_Of (C, Loc),
12664 Right_Opnd =>
12665 New_Occurrence_Of
12666 (RTE (RE_POK_Protected_Entry), Loc)),
12668 Right_Opnd =>
12669 Make_Op_Eq (Loc,
12670 Left_Opnd => New_Occurrence_Of (C, Loc),
12671 Right_Opnd =>
12672 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12674 Then_Statements => Unpack));
12675 end if;
12677 -- Generate:
12679 -- if B then
12680 -- if C = POK_Procedure
12681 -- or else C = POK_Protected_Procedure
12682 -- or else C = POK_Task_Procedure
12683 -- then
12684 -- <dispatching-call>
12685 -- end if;
12686 -- end if;
12688 N_Stats := New_List (
12689 Make_Implicit_If_Statement (N,
12690 Condition =>
12691 Make_Or_Else (Loc,
12692 Left_Opnd =>
12693 Make_Op_Eq (Loc,
12694 Left_Opnd => New_Occurrence_Of (C, Loc),
12695 Right_Opnd =>
12696 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12698 Right_Opnd =>
12699 Make_Or_Else (Loc,
12700 Left_Opnd =>
12701 Make_Op_Eq (Loc,
12702 Left_Opnd => New_Occurrence_Of (C, Loc),
12703 Right_Opnd =>
12704 New_Occurrence_Of (RTE (
12705 RE_POK_Protected_Procedure), Loc)),
12706 Right_Opnd =>
12707 Make_Op_Eq (Loc,
12708 Left_Opnd => New_Occurrence_Of (C, Loc),
12709 Right_Opnd =>
12710 New_Occurrence_Of
12711 (RTE (RE_POK_Task_Procedure), Loc)))),
12713 Then_Statements => New_List (E_Call)));
12715 Append_To (Conc_Typ_Stmts,
12716 Make_Implicit_If_Statement (N,
12717 Condition => New_Occurrence_Of (B, Loc),
12718 Then_Statements => N_Stats));
12720 -- Generate:
12721 -- <dispatching-call>;
12722 -- B := True;
12724 Lim_Typ_Stmts :=
12725 New_List (New_Copy_Tree (E_Call),
12726 Make_Assignment_Statement (Loc,
12727 Name => New_Occurrence_Of (B, Loc),
12728 Expression => New_Occurrence_Of (Standard_True, Loc)));
12730 -- Generate:
12731 -- if K = Ada.Tags.TK_Limited_Tagged
12732 -- or else K = Ada.Tags.TK_Tagged
12733 -- then
12734 -- Lim_Typ_Stmts
12735 -- else
12736 -- Conc_Typ_Stmts
12737 -- end if;
12739 Append_To (Stmts,
12740 Make_Implicit_If_Statement (N,
12741 Condition => Build_Dispatching_Tag_Check (K, N),
12742 Then_Statements => Lim_Typ_Stmts,
12743 Else_Statements => Conc_Typ_Stmts));
12745 -- Generate:
12747 -- if B then
12748 -- <triggering-statements>
12749 -- else
12750 -- <timed-statements>
12751 -- end if;
12753 Append_To (Stmts,
12754 Make_Implicit_If_Statement (N,
12755 Condition => New_Occurrence_Of (B, Loc),
12756 Then_Statements => E_Stats,
12757 Else_Statements => D_Stats));
12759 else
12760 -- Simple case of a nondispatching trigger. Skip assignments to
12761 -- temporaries created for in-out parameters.
12763 -- This makes unwarranted assumptions about the shape of the expanded
12764 -- tree for the call, and should be cleaned up ???
12766 Stmt := First (Stmts);
12767 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12768 Next (Stmt);
12769 end loop;
12771 -- Compute the delay at this stage because the evaluation of
12772 -- its expression must not occur earlier (see ACVC C97302A).
12774 Insert_Before (Stmt,
12775 Make_Assignment_Statement (Loc,
12776 Name => New_Occurrence_Of (D, Loc),
12777 Expression => D_Conv));
12779 Call := Stmt;
12780 Params := Parameter_Associations (Call);
12782 -- For a protected type, we build a Timed_Protected_Entry_Call
12784 if Is_Protected_Type (Etype (Concval)) then
12786 -- Create a new call statement
12788 Param := First (Params);
12789 while Present (Param)
12790 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12791 loop
12792 Next (Param);
12793 end loop;
12795 Dummy := Remove_Next (Next (Param));
12797 -- Remove garbage is following the Cancel_Param if present
12799 Dummy := Next (Param);
12801 -- Remove the mode of the Protected_Entry_Call call, then remove
12802 -- the Communication_Block of the Protected_Entry_Call call, and
12803 -- finally add Duration and a Delay_Mode parameter
12805 pragma Assert (Present (Param));
12806 Rewrite (Param, New_Occurrence_Of (D, Loc));
12808 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12810 -- Add a Boolean flag for successful entry call
12812 Append_To (Params, New_Occurrence_Of (B, Loc));
12814 case Corresponding_Runtime_Package (Etype (Concval)) is
12815 when System_Tasking_Protected_Objects_Entries =>
12816 Rewrite (Call,
12817 Make_Procedure_Call_Statement (Loc,
12818 Name =>
12819 New_Occurrence_Of
12820 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12821 Parameter_Associations => Params));
12823 when others =>
12824 raise Program_Error;
12825 end case;
12827 -- For the task case, build a Timed_Task_Entry_Call
12829 else
12830 -- Create a new call statement
12832 Append_To (Params, New_Occurrence_Of (D, Loc));
12833 Append_To (Params, New_Occurrence_Of (M, Loc));
12834 Append_To (Params, New_Occurrence_Of (B, Loc));
12836 Rewrite (Call,
12837 Make_Procedure_Call_Statement (Loc,
12838 Name =>
12839 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12840 Parameter_Associations => Params));
12841 end if;
12843 Append_To (Stmts,
12844 Make_Implicit_If_Statement (N,
12845 Condition => New_Occurrence_Of (B, Loc),
12846 Then_Statements => E_Stats,
12847 Else_Statements => D_Stats));
12848 end if;
12850 Rewrite (N,
12851 Make_Block_Statement (Loc,
12852 Declarations => Decls,
12853 Handled_Statement_Sequence =>
12854 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12856 Analyze (N);
12858 -- Some items in Decls used to be in the N_Block in E_Call that is
12859 -- constructed in Expand_Entry_Call, and are now in the new Block
12860 -- into which N has been rewritten. Adjust their scopes to reflect that.
12862 if Nkind (E_Call) = N_Block_Statement then
12863 Obj := First_Entity (Entity (Identifier (E_Call)));
12864 while Present (Obj) loop
12865 Set_Scope (Obj, Entity (Identifier (N)));
12866 Next_Entity (Obj);
12867 end loop;
12868 end if;
12870 Reset_Scopes_To (N, Entity (Identifier (N)));
12871 end Expand_N_Timed_Entry_Call;
12873 ----------------------------------------
12874 -- Expand_Protected_Body_Declarations --
12875 ----------------------------------------
12877 procedure Expand_Protected_Body_Declarations
12878 (N : Node_Id;
12879 Spec_Id : Entity_Id)
12881 begin
12882 if No_Run_Time_Mode then
12883 Error_Msg_CRT ("protected body", N);
12884 return;
12886 elsif Expander_Active then
12888 -- Associate discriminals with the first subprogram or entry body to
12889 -- be expanded.
12891 if Present (First_Protected_Operation (Declarations (N))) then
12892 Set_Discriminals (Parent (Spec_Id));
12893 end if;
12894 end if;
12895 end Expand_Protected_Body_Declarations;
12897 -------------------------
12898 -- External_Subprogram --
12899 -------------------------
12901 function External_Subprogram (E : Entity_Id) return Entity_Id is
12902 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12904 begin
12905 -- The internal and external subprograms follow each other on the entity
12906 -- chain. Note that previously private operations had no separate
12907 -- external subprogram. We now create one in all cases, because a
12908 -- private operation may actually appear in an external call, through
12909 -- a 'Access reference used for a callback.
12911 -- If the operation is a function that returns an anonymous access type,
12912 -- the corresponding itype appears before the operation, and must be
12913 -- skipped.
12915 -- This mechanism is fragile, there should be a real link between the
12916 -- two versions of the operation, but there is no place to put it ???
12918 if Is_Access_Type (Next_Entity (Subp)) then
12919 return Next_Entity (Next_Entity (Subp));
12920 else
12921 return Next_Entity (Subp);
12922 end if;
12923 end External_Subprogram;
12925 ------------------------------
12926 -- Extract_Dispatching_Call --
12927 ------------------------------
12929 procedure Extract_Dispatching_Call
12930 (N : Node_Id;
12931 Call_Ent : out Entity_Id;
12932 Object : out Entity_Id;
12933 Actuals : out List_Id;
12934 Formals : out List_Id)
12936 Call_Nam : Node_Id;
12938 begin
12939 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12941 if Present (Original_Node (N)) then
12942 Call_Nam := Name (Original_Node (N));
12943 else
12944 Call_Nam := Name (N);
12945 end if;
12947 -- Retrieve the name of the dispatching procedure. It contains the
12948 -- dispatch table slot number.
12950 loop
12951 case Nkind (Call_Nam) is
12952 when N_Identifier =>
12953 exit;
12955 when N_Selected_Component =>
12956 Call_Nam := Selector_Name (Call_Nam);
12958 when others =>
12959 raise Program_Error;
12960 end case;
12961 end loop;
12963 Actuals := Parameter_Associations (N);
12964 Call_Ent := Entity (Call_Nam);
12965 Formals := Parameter_Specifications (Parent (Call_Ent));
12966 Object := First (Actuals);
12968 if Present (Original_Node (Object)) then
12969 Object := Original_Node (Object);
12970 end if;
12972 -- If the type of the dispatching object is an access type then return
12973 -- an explicit dereference of a copy of the object, and note that this
12974 -- is the controlling actual of the call.
12976 if Is_Access_Type (Etype (Object)) then
12977 Object :=
12978 Make_Explicit_Dereference (Sloc (N), New_Copy_Tree (Object));
12979 Analyze (Object);
12980 Set_Is_Controlling_Actual (Object);
12981 end if;
12982 end Extract_Dispatching_Call;
12984 -------------------
12985 -- Extract_Entry --
12986 -------------------
12988 procedure Extract_Entry
12989 (N : Node_Id;
12990 Concval : out Node_Id;
12991 Ename : out Node_Id;
12992 Index : out Node_Id)
12994 Nam : constant Node_Id := Name (N);
12996 begin
12997 -- For a simple entry, the name is a selected component, with the
12998 -- prefix being the task value, and the selector being the entry.
13000 if Nkind (Nam) = N_Selected_Component then
13001 Concval := Prefix (Nam);
13002 Ename := Selector_Name (Nam);
13003 Index := Empty;
13005 -- For a member of an entry family, the name is an indexed component
13006 -- where the prefix is a selected component, whose prefix in turn is
13007 -- the task value, and whose selector is the entry family. The single
13008 -- expression in the expressions list of the indexed component is the
13009 -- subscript for the family.
13011 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13012 Concval := Prefix (Prefix (Nam));
13013 Ename := Selector_Name (Prefix (Nam));
13014 Index := First (Expressions (Nam));
13015 end if;
13017 -- Through indirection, the type may actually be a limited view of a
13018 -- concurrent type. When compiling a call, the non-limited view of the
13019 -- type is visible.
13021 if From_Limited_With (Etype (Concval)) then
13022 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13023 end if;
13024 end Extract_Entry;
13026 -------------------
13027 -- Family_Offset --
13028 -------------------
13030 function Family_Offset
13031 (Loc : Source_Ptr;
13032 Hi : Node_Id;
13033 Lo : Node_Id;
13034 Ttyp : Entity_Id;
13035 Cap : Boolean) return Node_Id
13037 Ityp : Entity_Id;
13038 Real_Hi : Node_Id;
13039 Real_Lo : Node_Id;
13041 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13042 -- If one of the bounds is a reference to a discriminant, replace with
13043 -- corresponding discriminal of type. Within the body of a task retrieve
13044 -- the renamed discriminant by simple visibility, using its generated
13045 -- name. Within a protected object, find the original discriminant and
13046 -- replace it with the discriminal of the current protected operation.
13048 ------------------------------
13049 -- Convert_Discriminant_Ref --
13050 ------------------------------
13052 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13053 Loc : constant Source_Ptr := Sloc (Bound);
13054 B : Node_Id;
13055 D : Entity_Id;
13057 begin
13058 if Is_Entity_Name (Bound)
13059 and then Ekind (Entity (Bound)) = E_Discriminant
13060 then
13061 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13062 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13063 Find_Direct_Name (B);
13065 elsif Is_Protected_Type (Ttyp) then
13066 D := First_Discriminant (Ttyp);
13067 while Chars (D) /= Chars (Entity (Bound)) loop
13068 Next_Discriminant (D);
13069 end loop;
13071 B := New_Occurrence_Of (Discriminal (D), Loc);
13073 else
13074 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13075 end if;
13077 elsif Nkind (Bound) = N_Attribute_Reference then
13078 return Bound;
13080 else
13081 B := New_Copy_Tree (Bound);
13082 end if;
13084 return
13085 Make_Attribute_Reference (Loc,
13086 Attribute_Name => Name_Pos,
13087 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13088 Expressions => New_List (B));
13089 end Convert_Discriminant_Ref;
13091 -- Start of processing for Family_Offset
13093 begin
13094 Real_Hi := Convert_Discriminant_Ref (Hi);
13095 Real_Lo := Convert_Discriminant_Ref (Lo);
13097 if Cap then
13098 if Is_Task_Type (Ttyp) then
13099 Ityp := RTE (RE_Task_Entry_Index);
13100 else
13101 Ityp := RTE (RE_Protected_Entry_Index);
13102 end if;
13104 Real_Hi :=
13105 Make_Attribute_Reference (Loc,
13106 Prefix => New_Occurrence_Of (Ityp, Loc),
13107 Attribute_Name => Name_Min,
13108 Expressions => New_List (
13109 Real_Hi,
13110 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13112 Real_Lo :=
13113 Make_Attribute_Reference (Loc,
13114 Prefix => New_Occurrence_Of (Ityp, Loc),
13115 Attribute_Name => Name_Max,
13116 Expressions => New_List (
13117 Real_Lo,
13118 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13119 end if;
13121 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13122 end Family_Offset;
13124 -----------------
13125 -- Family_Size --
13126 -----------------
13128 function Family_Size
13129 (Loc : Source_Ptr;
13130 Hi : Node_Id;
13131 Lo : Node_Id;
13132 Ttyp : Entity_Id;
13133 Cap : Boolean) return Node_Id
13135 Ityp : Entity_Id;
13137 begin
13138 if Is_Task_Type (Ttyp) then
13139 Ityp := RTE (RE_Task_Entry_Index);
13140 else
13141 Ityp := RTE (RE_Protected_Entry_Index);
13142 end if;
13144 return
13145 Make_Attribute_Reference (Loc,
13146 Prefix => New_Occurrence_Of (Ityp, Loc),
13147 Attribute_Name => Name_Max,
13148 Expressions => New_List (
13149 Make_Op_Add (Loc,
13150 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13151 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13152 Make_Integer_Literal (Loc, 0)));
13153 end Family_Size;
13155 ----------------------------
13156 -- Find_Enclosing_Context --
13157 ----------------------------
13159 procedure Find_Enclosing_Context
13160 (N : Node_Id;
13161 Context : out Node_Id;
13162 Context_Id : out Entity_Id;
13163 Context_Decls : out List_Id)
13165 begin
13166 -- Traverse the parent chain looking for an enclosing body, block,
13167 -- package or return statement.
13169 Context := Parent (N);
13170 while Present (Context) loop
13171 if Nkind (Context) in N_Entry_Body
13172 | N_Extended_Return_Statement
13173 | N_Package_Body
13174 | N_Package_Declaration
13175 | N_Subprogram_Body
13176 | N_Task_Body
13177 then
13178 exit;
13180 -- Do not consider block created to protect a list of statements with
13181 -- an Abort_Defer / Abort_Undefer_Direct pair.
13183 elsif Nkind (Context) = N_Block_Statement
13184 and then not Is_Abort_Block (Context)
13185 then
13186 exit;
13187 end if;
13189 Context := Parent (Context);
13190 end loop;
13192 pragma Assert (Present (Context));
13194 -- Extract the constituents of the context
13196 if Nkind (Context) = N_Extended_Return_Statement then
13197 Context_Decls := Return_Object_Declarations (Context);
13198 Context_Id := Return_Statement_Entity (Context);
13200 -- Package declarations and bodies use a common library-level activation
13201 -- chain or task master, therefore return the package declaration as the
13202 -- proper carrier for the appropriate flag.
13204 elsif Nkind (Context) = N_Package_Body then
13205 Context_Decls := Declarations (Context);
13206 Context_Id := Corresponding_Spec (Context);
13207 Context := Parent (Context_Id);
13209 if Nkind (Context) = N_Defining_Program_Unit_Name then
13210 Context := Parent (Parent (Context));
13211 else
13212 Context := Parent (Context);
13213 end if;
13215 elsif Nkind (Context) = N_Package_Declaration then
13216 Context_Decls := Visible_Declarations (Specification (Context));
13217 Context_Id := Defining_Unit_Name (Specification (Context));
13219 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13220 Context_Id := Defining_Identifier (Context_Id);
13221 end if;
13223 else
13224 if Nkind (Context) = N_Block_Statement then
13225 Context_Id := Entity (Identifier (Context));
13227 if No (Declarations (Context)) then
13228 Set_Declarations (Context, New_List);
13229 end if;
13231 elsif Nkind (Context) = N_Entry_Body then
13232 Context_Id := Defining_Identifier (Context);
13234 elsif Nkind (Context) = N_Subprogram_Body then
13235 if Present (Corresponding_Spec (Context)) then
13236 Context_Id := Corresponding_Spec (Context);
13237 else
13238 Context_Id := Defining_Unit_Name (Specification (Context));
13240 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13241 Context_Id := Defining_Identifier (Context_Id);
13242 end if;
13243 end if;
13245 elsif Nkind (Context) = N_Task_Body then
13246 Context_Id := Corresponding_Spec (Context);
13248 else
13249 raise Program_Error;
13250 end if;
13252 Context_Decls := Declarations (Context);
13253 end if;
13255 pragma Assert (Present (Context_Id));
13256 pragma Assert (Present (Context_Decls));
13257 end Find_Enclosing_Context;
13259 -----------------------
13260 -- Find_Master_Scope --
13261 -----------------------
13263 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13264 S : Entity_Id;
13266 begin
13267 -- In Ada 2005, the master is the innermost enclosing scope that is not
13268 -- transient. If the enclosing block is the rewriting of a call or the
13269 -- scope is an extended return statement this is valid master. The
13270 -- master in an extended return is only used within the return, and is
13271 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13272 -- now before that overwriting occurs.
13274 S := Scope (E);
13276 if Ada_Version >= Ada_2005 then
13277 while Is_Internal (S) loop
13278 if Nkind (Parent (S)) = N_Block_Statement
13279 and then Has_Master_Entity (S)
13280 then
13281 exit;
13283 elsif Ekind (S) = E_Return_Statement then
13284 exit;
13286 else
13287 S := Scope (S);
13288 end if;
13289 end loop;
13290 end if;
13292 return S;
13293 end Find_Master_Scope;
13295 -------------------------------
13296 -- First_Protected_Operation --
13297 -------------------------------
13299 function First_Protected_Operation (D : List_Id) return Node_Id is
13300 First_Op : Node_Id;
13302 begin
13303 First_Op := First (D);
13304 while Present (First_Op)
13305 and then Nkind (First_Op) not in N_Subprogram_Body | N_Entry_Body
13306 loop
13307 Next (First_Op);
13308 end loop;
13310 return First_Op;
13311 end First_Protected_Operation;
13313 ---------------------------------------
13314 -- Install_Private_Data_Declarations --
13315 ---------------------------------------
13317 procedure Install_Private_Data_Declarations
13318 (Loc : Source_Ptr;
13319 Spec_Id : Entity_Id;
13320 Conc_Typ : Entity_Id;
13321 Body_Nod : Node_Id;
13322 Decls : List_Id;
13323 Barrier : Boolean := False;
13324 Family : Boolean := False)
13326 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13327 Decl : Node_Id;
13328 Def : Node_Id;
13329 Insert_Node : Node_Id := Empty;
13330 Obj_Ent : Entity_Id;
13332 procedure Add (Decl : Node_Id);
13333 -- Add a single declaration after Insert_Node. If this is the first
13334 -- addition, Decl is added to the front of Decls and it becomes the
13335 -- insertion node.
13337 function Replace_Bound (Bound : Node_Id) return Node_Id;
13338 -- The bounds of an entry index may depend on discriminants, create a
13339 -- reference to the corresponding prival. Otherwise return a duplicate
13340 -- of the original bound.
13342 ---------
13343 -- Add --
13344 ---------
13346 procedure Add (Decl : Node_Id) is
13347 begin
13348 if No (Insert_Node) then
13349 Prepend_To (Decls, Decl);
13350 else
13351 Insert_After (Insert_Node, Decl);
13352 end if;
13354 Insert_Node := Decl;
13355 end Add;
13357 -------------------
13358 -- Replace_Bound --
13359 -------------------
13361 function Replace_Bound (Bound : Node_Id) return Node_Id is
13362 begin
13363 if Nkind (Bound) = N_Identifier
13364 and then Is_Discriminal (Entity (Bound))
13365 then
13366 return Make_Identifier (Loc, Chars (Entity (Bound)));
13367 else
13368 return Duplicate_Subexpr (Bound);
13369 end if;
13370 end Replace_Bound;
13372 -- Start of processing for Install_Private_Data_Declarations
13374 begin
13375 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13376 -- formal parameter _O, _object or _task depending on the context.
13378 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13380 -- Special processing of _O for barrier functions, protected entries
13381 -- and families.
13383 if Barrier
13384 or else
13385 (Is_Protected
13386 and then
13387 (Ekind (Spec_Id) = E_Entry
13388 or else Ekind (Spec_Id) = E_Entry_Family))
13389 then
13390 declare
13391 Conc_Rec : constant Entity_Id :=
13392 Corresponding_Record_Type (Conc_Typ);
13393 Typ_Id : constant Entity_Id :=
13394 Make_Defining_Identifier (Loc,
13395 New_External_Name (Chars (Conc_Rec), 'P'));
13396 begin
13397 -- Generate:
13398 -- type prot_typVP is access prot_typV;
13400 Decl :=
13401 Make_Full_Type_Declaration (Loc,
13402 Defining_Identifier => Typ_Id,
13403 Type_Definition =>
13404 Make_Access_To_Object_Definition (Loc,
13405 Subtype_Indication =>
13406 New_Occurrence_Of (Conc_Rec, Loc)));
13407 Add (Decl);
13409 -- Generate:
13410 -- _object : prot_typVP := prot_typV (_O);
13412 Decl :=
13413 Make_Object_Declaration (Loc,
13414 Defining_Identifier =>
13415 Make_Defining_Identifier (Loc, Name_uObject),
13416 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13417 Expression =>
13418 Unchecked_Convert_To (Typ_Id,
13419 New_Occurrence_Of (Obj_Ent, Loc)));
13420 Add (Decl);
13422 -- Set the reference to the concurrent object
13424 Obj_Ent := Defining_Identifier (Decl);
13425 end;
13426 end if;
13428 -- Step 2: Create the Protection object and build its declaration for
13429 -- any protected entry (family) of subprogram. Note for the lock-free
13430 -- implementation, the Protection object is not needed anymore.
13432 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13433 declare
13434 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13435 Prot_Typ : RE_Id;
13437 begin
13438 Set_Protection_Object (Spec_Id, Prot_Ent);
13440 -- Determine the proper protection type
13442 if Has_Attach_Handler (Conc_Typ)
13443 and then not Restricted_Profile
13444 then
13445 Prot_Typ := RE_Static_Interrupt_Protection;
13447 elsif Has_Interrupt_Handler (Conc_Typ)
13448 and then not Restriction_Active (No_Dynamic_Attachment)
13449 then
13450 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13452 else
13453 case Corresponding_Runtime_Package (Conc_Typ) is
13454 when System_Tasking_Protected_Objects_Entries =>
13455 Prot_Typ := RE_Protection_Entries;
13457 when System_Tasking_Protected_Objects_Single_Entry =>
13458 Prot_Typ := RE_Protection_Entry;
13460 when System_Tasking_Protected_Objects =>
13461 Prot_Typ := RE_Protection;
13463 when others =>
13464 raise Program_Error;
13465 end case;
13466 end if;
13468 -- Generate:
13469 -- conc_typR : protection_typ renames _object._object;
13471 Decl :=
13472 Make_Object_Renaming_Declaration (Loc,
13473 Defining_Identifier => Prot_Ent,
13474 Subtype_Mark =>
13475 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13476 Name =>
13477 Make_Selected_Component (Loc,
13478 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13479 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13481 Add (Decl);
13482 end;
13483 end if;
13485 -- Step 3: Add discriminant renamings (if any)
13487 if Has_Discriminants (Conc_Typ) then
13488 declare
13489 D : Entity_Id;
13491 begin
13492 D := First_Discriminant (Conc_Typ);
13493 while Present (D) loop
13495 -- Adjust the source location
13497 Set_Sloc (Discriminal (D), Loc);
13499 -- Generate:
13500 -- discr_name : discr_typ renames _object.discr_name;
13501 -- or
13502 -- discr_name : discr_typ renames _task.discr_name;
13504 Decl :=
13505 Make_Object_Renaming_Declaration (Loc,
13506 Defining_Identifier => Discriminal (D),
13507 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13508 Name =>
13509 Make_Selected_Component (Loc,
13510 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13511 Selector_Name => Make_Identifier (Loc, Chars (D))));
13513 Add (Decl);
13515 -- Set debug info needed on this renaming declaration even
13516 -- though it does not come from source, so that the debugger
13517 -- will get the right information for these generated names.
13519 Set_Debug_Info_Needed (Discriminal (D));
13521 Next_Discriminant (D);
13522 end loop;
13523 end;
13524 end if;
13526 -- Step 4: Add private component renamings (if any)
13528 if Is_Protected then
13529 Def := Protected_Definition (Parent (Conc_Typ));
13531 if Present (Private_Declarations (Def)) then
13532 declare
13533 Comp : Node_Id;
13534 Comp_Id : Entity_Id;
13535 Decl_Id : Entity_Id;
13536 Nam : Name_Id;
13538 begin
13539 Comp := First (Private_Declarations (Def));
13540 while Present (Comp) loop
13541 if Nkind (Comp) = N_Component_Declaration then
13542 Comp_Id := Defining_Identifier (Comp);
13543 Nam := Chars (Comp_Id);
13544 Decl_Id := Make_Defining_Identifier (Sloc (Comp_Id), Nam);
13546 -- Minimal decoration
13548 if Ekind (Spec_Id) = E_Function then
13549 Mutate_Ekind (Decl_Id, E_Constant);
13550 else
13551 Mutate_Ekind (Decl_Id, E_Variable);
13552 end if;
13554 Set_Prival (Comp_Id, Decl_Id);
13555 Set_Prival_Link (Decl_Id, Comp_Id);
13556 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13557 Set_Is_Independent (Decl_Id, Is_Independent (Comp_Id));
13559 -- Copy the Comes_From_Source flag of the component, as
13560 -- the renaming may be the only entity directly seen by
13561 -- the user in the context, but do not warn for it.
13563 Set_Comes_From_Source
13564 (Decl_Id, Comes_From_Source (Comp_Id));
13565 Set_Warnings_Off (Decl_Id);
13567 -- Generate:
13568 -- comp_name : comp_typ renames _object.comp_name;
13570 Decl :=
13571 Make_Object_Renaming_Declaration (Loc,
13572 Defining_Identifier => Decl_Id,
13573 Subtype_Mark =>
13574 New_Occurrence_Of (Etype (Comp_Id), Loc),
13575 Name =>
13576 Make_Selected_Component (Loc,
13577 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13578 Selector_Name => Make_Identifier (Loc, Nam)));
13580 Add (Decl);
13581 end if;
13583 Next (Comp);
13584 end loop;
13585 end;
13586 end if;
13587 end if;
13589 -- Step 5: Add the declaration of the entry index and the associated
13590 -- type for barrier functions and entry families.
13592 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13593 declare
13594 E : constant Entity_Id := Index_Object (Spec_Id);
13595 Index : constant Entity_Id :=
13596 Defining_Identifier
13597 (Entry_Index_Specification
13598 (Entry_Body_Formal_Part (Body_Nod)));
13599 Index_Con : constant Entity_Id :=
13600 Make_Defining_Identifier (Loc, Chars (Index));
13601 High : Node_Id;
13602 Index_Typ : Entity_Id;
13603 Low : Node_Id;
13605 begin
13606 -- Minimal decoration
13608 Mutate_Ekind (Index_Con, E_Constant);
13609 Set_Entry_Index_Constant (Index, Index_Con);
13610 Set_Discriminal_Link (Index_Con, Index);
13612 -- Retrieve the bounds of the entry family
13614 High := Type_High_Bound (Etype (Index));
13615 Low := Type_Low_Bound (Etype (Index));
13617 -- In the simple case the entry family is given by a subtype mark
13618 -- and the index constant has the same type.
13620 if Is_Entity_Name (Original_Node (
13621 Discrete_Subtype_Definition (Parent (Index))))
13622 then
13623 Index_Typ := Etype (Index);
13625 -- Otherwise a new subtype declaration is required
13627 else
13628 High := Replace_Bound (High);
13629 Low := Replace_Bound (Low);
13631 Index_Typ := Make_Temporary (Loc, 'J');
13633 -- Generate:
13634 -- subtype Jnn is <Etype of Index> range Low .. High;
13636 Decl :=
13637 Make_Subtype_Declaration (Loc,
13638 Defining_Identifier => Index_Typ,
13639 Subtype_Indication =>
13640 Make_Subtype_Indication (Loc,
13641 Subtype_Mark =>
13642 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13643 Constraint =>
13644 Make_Range_Constraint (Loc,
13645 Range_Expression =>
13646 Make_Range (Loc, Low, High))));
13647 Add (Decl);
13648 end if;
13650 Set_Etype (Index_Con, Index_Typ);
13652 -- Create the object which designates the index:
13653 -- J : constant Jnn :=
13654 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13656 -- where Jnn is the subtype created above or the original type of
13657 -- the index, _E is a formal of the protected body subprogram and
13658 -- <index expr> is the index of the first family member.
13660 Decl :=
13661 Make_Object_Declaration (Loc,
13662 Defining_Identifier => Index_Con,
13663 Constant_Present => True,
13664 Object_Definition =>
13665 New_Occurrence_Of (Index_Typ, Loc),
13667 Expression =>
13668 Make_Attribute_Reference (Loc,
13669 Prefix =>
13670 New_Occurrence_Of (Index_Typ, Loc),
13671 Attribute_Name => Name_Val,
13673 Expressions => New_List (
13675 Make_Op_Add (Loc,
13676 Left_Opnd =>
13677 Make_Op_Subtract (Loc,
13678 Left_Opnd => New_Occurrence_Of (E, Loc),
13679 Right_Opnd =>
13680 Entry_Index_Expression (Loc,
13681 Defining_Identifier (Body_Nod),
13682 Empty, Conc_Typ)),
13684 Right_Opnd =>
13685 Make_Attribute_Reference (Loc,
13686 Prefix =>
13687 New_Occurrence_Of (Index_Typ, Loc),
13688 Attribute_Name => Name_Pos,
13689 Expressions => New_List (
13690 Make_Attribute_Reference (Loc,
13691 Prefix =>
13692 New_Occurrence_Of (Index_Typ, Loc),
13693 Attribute_Name => Name_First)))))));
13694 Add (Decl);
13695 end;
13696 end if;
13697 end Install_Private_Data_Declarations;
13699 ---------------------------------
13700 -- Is_Potentially_Large_Family --
13701 ---------------------------------
13703 function Is_Potentially_Large_Family
13704 (Base_Index : Entity_Id;
13705 Conctyp : Entity_Id;
13706 Lo : Node_Id;
13707 Hi : Node_Id) return Boolean
13709 begin
13710 return Scope (Base_Index) = Standard_Standard
13711 and then Base_Index = Base_Type (Standard_Integer)
13712 and then Has_Defaulted_Discriminants (Conctyp)
13713 and then
13714 (Denotes_Discriminant (Lo, True)
13715 or else
13716 Denotes_Discriminant (Hi, True));
13717 end Is_Potentially_Large_Family;
13719 -------------------------------------
13720 -- Is_Private_Primitive_Subprogram --
13721 -------------------------------------
13723 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13724 begin
13725 return
13726 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13727 and then Is_Private_Primitive (Id);
13728 end Is_Private_Primitive_Subprogram;
13730 ------------------
13731 -- Index_Object --
13732 ------------------
13734 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13735 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13736 Formal : Entity_Id;
13738 begin
13739 Formal := First_Formal (Bod_Subp);
13740 while Present (Formal) loop
13742 -- Look for formal parameter _E
13744 if Chars (Formal) = Name_uE then
13745 return Formal;
13746 end if;
13748 Next_Formal (Formal);
13749 end loop;
13751 -- A protected body subprogram should always have the parameter in
13752 -- question.
13754 raise Program_Error;
13755 end Index_Object;
13757 --------------------------------
13758 -- Make_Initialize_Protection --
13759 --------------------------------
13761 function Make_Initialize_Protection
13762 (Protect_Rec : Entity_Id) return List_Id
13764 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13765 P_Arr : Entity_Id;
13766 Pdec : Node_Id;
13767 Ptyp : constant Node_Id :=
13768 Corresponding_Concurrent_Type (Protect_Rec);
13769 Args : List_Id;
13770 L : constant List_Id := New_List;
13771 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13772 Prio_Type : Entity_Id;
13773 Prio_Var : Entity_Id := Empty;
13774 Restricted : constant Boolean := Restricted_Profile;
13776 begin
13777 -- We may need two calls to properly initialize the object, one to
13778 -- Initialize_Protection, and possibly one to Install_Handlers if we
13779 -- have a pragma Attach_Handler.
13781 -- Get protected declaration. In the case of a task type declaration,
13782 -- this is simply the parent of the protected type entity. In the single
13783 -- protected object declaration, this parent will be the implicit type,
13784 -- and we can find the corresponding single protected object declaration
13785 -- by searching forward in the declaration list in the tree.
13787 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13788 -- of this type should have been removed during semantic analysis.
13790 Pdec := Parent (Ptyp);
13791 while Nkind (Pdec) not in
13792 N_Protected_Type_Declaration | N_Single_Protected_Declaration
13793 loop
13794 Next (Pdec);
13795 end loop;
13797 -- Build the parameter list for the call. Note that _Init is the name
13798 -- of the formal for the object to be initialized, which is the task
13799 -- value record itself.
13801 Args := New_List;
13803 -- For lock-free implementation, skip initializations of the Protection
13804 -- object.
13806 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13808 -- Object parameter. This is a pointer to the object of type
13809 -- Protection used by the GNARL to control the protected object.
13811 Append_To (Args,
13812 Make_Attribute_Reference (Loc,
13813 Prefix =>
13814 Make_Selected_Component (Loc,
13815 Prefix => Make_Identifier (Loc, Name_uInit),
13816 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13817 Attribute_Name => Name_Unchecked_Access));
13819 -- Priority parameter. Set to Unspecified_Priority unless there is a
13820 -- Priority rep item, in which case we take the value from the pragma
13821 -- or attribute definition clause, or there is an Interrupt_Priority
13822 -- rep item and no Priority rep item, and we set the ceiling to
13823 -- Interrupt_Priority'Last, an implementation-defined value, see
13824 -- (RM D.3(10)).
13826 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13827 declare
13828 Prio_Clause : constant Node_Id :=
13829 Get_Rep_Item
13830 (Ptyp, Name_Priority, Check_Parents => False);
13832 Prio : Node_Id;
13834 begin
13835 -- Pragma Priority
13837 if Nkind (Prio_Clause) = N_Pragma then
13838 Prio :=
13839 Expression
13840 (First (Pragma_Argument_Associations (Prio_Clause)));
13842 -- Get_Rep_Item returns either priority pragma
13844 if Pragma_Name (Prio_Clause) = Name_Priority then
13845 Prio_Type := RTE (RE_Any_Priority);
13846 else
13847 Prio_Type := RTE (RE_Interrupt_Priority);
13848 end if;
13850 -- Attribute definition clause Priority
13852 else
13853 if Chars (Prio_Clause) = Name_Priority then
13854 Prio_Type := RTE (RE_Any_Priority);
13855 else
13856 Prio_Type := RTE (RE_Interrupt_Priority);
13857 end if;
13859 Prio := Expression (Prio_Clause);
13860 end if;
13862 -- Always create a locale variable to capture the priority.
13863 -- The priority is also passed to Install_Restriced_Handlers.
13864 -- Note that it is really necessary to create this variable
13865 -- explicitly. It might be thought that removing side effects
13866 -- would the appropriate approach, but that could generate
13867 -- declarations improperly placed in the enclosing scope.
13869 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13870 Append_To (L,
13871 Make_Object_Declaration (Loc,
13872 Defining_Identifier => Prio_Var,
13873 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13874 Expression => Relocate_Node (Prio)));
13876 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13877 end;
13879 -- When no priority is specified but an xx_Handler pragma is, we
13880 -- default to System.Interrupts.Default_Interrupt_Priority, see
13881 -- D.3(10).
13883 elsif Has_Attach_Handler (Ptyp)
13884 or else Has_Interrupt_Handler (Ptyp)
13885 then
13886 Append_To (Args,
13887 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13889 -- Normal case, no priority or xx_Handler specified, default priority
13891 else
13892 Append_To (Args,
13893 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13894 end if;
13896 -- Deadline_Floor parameter for GNAT_Ravenscar_EDF runtimes
13898 if Restricted_Profile and Task_Dispatching_Policy = 'E' then
13899 Deadline_Floor : declare
13900 Item : constant Node_Id :=
13901 Get_Rep_Item
13902 (Ptyp, Name_Deadline_Floor, Check_Parents => False);
13904 Deadline : Node_Id;
13906 begin
13907 if Present (Item) then
13909 -- Pragma Deadline_Floor
13911 if Nkind (Item) = N_Pragma then
13912 Deadline :=
13913 Expression
13914 (First (Pragma_Argument_Associations (Item)));
13916 -- Attribute definition clause Deadline_Floor
13918 else
13919 pragma Assert
13920 (Nkind (Item) = N_Attribute_Definition_Clause);
13922 Deadline := Expression (Item);
13923 end if;
13925 Append_To (Args, Deadline);
13927 -- Unusual case: default deadline
13929 else
13930 Append_To (Args,
13931 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
13932 end if;
13933 end Deadline_Floor;
13934 end if;
13936 -- Test for Compiler_Info parameter. This parameter allows entry body
13937 -- procedures and barrier functions to be called from the runtime. It
13938 -- is a pointer to the record generated by the compiler to represent
13939 -- the protected object.
13941 -- A protected type without entries that covers an interface and
13942 -- overrides the abstract routines with protected procedures is
13943 -- considered equivalent to a protected type with entries in the
13944 -- context of dispatching select statements.
13946 -- Protected types with interrupt handlers (when not using a
13947 -- restricted profile) are also considered equivalent to protected
13948 -- types with entries.
13950 -- The types which are used (Static_Interrupt_Protection and
13951 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13953 declare
13954 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13956 Called_Subp : RE_Id;
13958 begin
13959 case Pkg_Id is
13960 when System_Tasking_Protected_Objects_Entries =>
13961 Called_Subp := RE_Initialize_Protection_Entries;
13963 -- Argument Compiler_Info
13965 Append_To (Args,
13966 Make_Attribute_Reference (Loc,
13967 Prefix => Make_Identifier (Loc, Name_uInit),
13968 Attribute_Name => Name_Address));
13970 when System_Tasking_Protected_Objects_Single_Entry =>
13971 Called_Subp := RE_Initialize_Protection_Entry;
13973 -- Argument Compiler_Info
13975 Append_To (Args,
13976 Make_Attribute_Reference (Loc,
13977 Prefix => Make_Identifier (Loc, Name_uInit),
13978 Attribute_Name => Name_Address));
13980 when System_Tasking_Protected_Objects =>
13981 Called_Subp := RE_Initialize_Protection;
13983 when others =>
13984 raise Program_Error;
13985 end case;
13987 -- Entry_Queue_Maxes parameter. This is an access to an array of
13988 -- naturals representing the entry queue maximums for each entry
13989 -- in the protected type. Zero represents no max. The access is
13990 -- null if there is no limit for all entries (usual case).
13992 if Has_Entry
13993 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
13994 then
13995 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
13996 Append_To (Args,
13997 Make_Attribute_Reference (Loc,
13998 Prefix =>
13999 New_Occurrence_Of
14000 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
14001 Attribute_Name => Name_Unrestricted_Access));
14002 else
14003 Append_To (Args, Make_Null (Loc));
14004 end if;
14006 -- Edge cases exist where entry initialization functions are
14007 -- called, but no entries exist, so null is appended.
14009 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14010 Append_To (Args, Make_Null (Loc));
14011 end if;
14013 -- Entry_Bodies parameter. This is a pointer to an array of
14014 -- pointers to the entry body procedures and barrier functions of
14015 -- the object. If the protected type has no entries this object
14016 -- will not exist, in this case, pass a null (it can happen when
14017 -- there are protected interrupt handlers or interfaces).
14019 if Has_Entry then
14020 P_Arr := Entry_Bodies_Array (Ptyp);
14022 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14023 -- multiple entries).
14025 Append_To (Args,
14026 Make_Attribute_Reference (Loc,
14027 Prefix => New_Occurrence_Of (P_Arr, Loc),
14028 Attribute_Name => Name_Unrestricted_Access));
14030 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14032 -- Find index mapping function (clumsy but ok for now)
14034 while Ekind (P_Arr) /= E_Function loop
14035 Next_Entity (P_Arr);
14036 end loop;
14038 Append_To (Args,
14039 Make_Attribute_Reference (Loc,
14040 Prefix => New_Occurrence_Of (P_Arr, Loc),
14041 Attribute_Name => Name_Unrestricted_Access));
14042 end if;
14044 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14046 -- This is the case where we have a protected object with
14047 -- interfaces and no entries, and the single entry restriction
14048 -- is in effect. We pass a null pointer for the entry
14049 -- parameter because there is no actual entry.
14051 Append_To (Args, Make_Null (Loc));
14053 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14055 -- This is the case where we have a protected object with no
14056 -- entries and:
14057 -- - either interrupt handlers with non restricted profile,
14058 -- - or interfaces
14059 -- Note that the types which are used for interrupt handlers
14060 -- (Static/Dynamic_Interrupt_Protection) are derived from
14061 -- Protection_Entries. We pass two null pointers because there
14062 -- is no actual entry, and the initialization procedure needs
14063 -- both Entry_Bodies and Find_Body_Index.
14065 Append_To (Args, Make_Null (Loc));
14066 Append_To (Args, Make_Null (Loc));
14067 end if;
14069 Append_To (L,
14070 Make_Procedure_Call_Statement (Loc,
14071 Name =>
14072 New_Occurrence_Of (RTE (Called_Subp), Loc),
14073 Parameter_Associations => Args));
14074 end;
14075 end if;
14077 if Has_Attach_Handler (Ptyp) then
14079 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14080 -- make the following call:
14082 -- Install_Handlers (_object,
14083 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14085 -- or, in the case of Ravenscar:
14087 -- Install_Restricted_Handlers
14088 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14090 declare
14091 Args : constant List_Id := New_List;
14092 Table : constant List_Id := New_List;
14093 Ritem : Node_Id := First_Rep_Item (Ptyp);
14095 begin
14096 -- Build the Priority parameter (only for ravenscar)
14098 if Restricted then
14100 -- Priority comes from a pragma
14102 if Present (Prio_Var) then
14103 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14105 -- Priority is the default one
14107 else
14108 Append_To (Args,
14109 New_Occurrence_Of
14110 (RTE (RE_Default_Interrupt_Priority), Loc));
14111 end if;
14112 end if;
14114 -- Build the Attach_Handler table argument
14116 while Present (Ritem) loop
14117 if Nkind (Ritem) = N_Pragma
14118 and then Pragma_Name (Ritem) = Name_Attach_Handler
14119 then
14120 declare
14121 Handler : constant Node_Id :=
14122 First (Pragma_Argument_Associations (Ritem));
14124 Interrupt : constant Node_Id := Next (Handler);
14125 Expr : constant Node_Id := Expression (Interrupt);
14127 begin
14128 Append_To (Table,
14129 Make_Aggregate (Loc, Expressions => New_List (
14130 Unchecked_Convert_To
14131 (RTE (RE_System_Interrupt_Id), Expr),
14132 Make_Attribute_Reference (Loc,
14133 Prefix =>
14134 Make_Selected_Component (Loc,
14135 Prefix =>
14136 Make_Identifier (Loc, Name_uInit),
14137 Selector_Name =>
14138 Duplicate_Subexpr_No_Checks
14139 (Expression (Handler))),
14140 Attribute_Name => Name_Access))));
14141 end;
14142 end if;
14144 Next_Rep_Item (Ritem);
14145 end loop;
14147 -- Append the table argument we just built
14149 Append_To (Args, Make_Aggregate (Loc, Table));
14151 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14152 -- call to the statements.
14154 if Restricted then
14155 -- Call a simplified version of Install_Handlers to be used
14156 -- when the Ravenscar restrictions are in effect
14157 -- (Install_Restricted_Handlers).
14159 Append_To (L,
14160 Make_Procedure_Call_Statement (Loc,
14161 Name =>
14162 New_Occurrence_Of
14163 (RTE (RE_Install_Restricted_Handlers), Loc),
14164 Parameter_Associations => Args));
14166 else
14167 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14169 -- First, prepends the _object argument
14171 Prepend_To (Args,
14172 Make_Attribute_Reference (Loc,
14173 Prefix =>
14174 Make_Selected_Component (Loc,
14175 Prefix => Make_Identifier (Loc, Name_uInit),
14176 Selector_Name =>
14177 Make_Identifier (Loc, Name_uObject)),
14178 Attribute_Name => Name_Unchecked_Access));
14179 end if;
14181 -- Then, insert call to Install_Handlers
14183 Append_To (L,
14184 Make_Procedure_Call_Statement (Loc,
14185 Name =>
14186 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14187 Parameter_Associations => Args));
14188 end if;
14189 end;
14190 end if;
14192 return L;
14193 end Make_Initialize_Protection;
14195 ---------------------------
14196 -- Make_Task_Create_Call --
14197 ---------------------------
14199 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14200 Loc : constant Source_Ptr := Sloc (Task_Rec);
14201 Args : List_Id;
14202 Ecount : Node_Id;
14203 Name : Node_Id;
14204 Tdec : Node_Id;
14205 Tdef : Node_Id;
14206 Tnam : Name_Id;
14207 Ttyp : Node_Id;
14209 begin
14210 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14211 Tnam := Chars (Ttyp);
14213 -- Get task declaration. In the case of a task type declaration, this is
14214 -- simply the parent of the task type entity. In the single task
14215 -- declaration, this parent will be the implicit type, and we can find
14216 -- the corresponding single task declaration by searching forward in the
14217 -- declaration list in the tree.
14219 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14220 -- this type should have been removed during semantic analysis.
14222 Tdec := Parent (Ttyp);
14223 while Nkind (Tdec) not in
14224 N_Task_Type_Declaration | N_Single_Task_Declaration
14225 loop
14226 Next (Tdec);
14227 end loop;
14229 -- Now we can find the task definition from this declaration
14231 Tdef := Task_Definition (Tdec);
14233 -- Build the parameter list for the call. Note that _Init is the name
14234 -- of the formal for the object to be initialized, which is the task
14235 -- value record itself.
14237 Args := New_List;
14239 -- Priority parameter. Set to Unspecified_Priority unless there is a
14240 -- Priority rep item, in which case we take the value from the rep item.
14241 -- Not used on Ravenscar_EDF profile.
14243 if not (Restricted_Profile and then Task_Dispatching_Policy = 'E') then
14244 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14245 Append_To (Args,
14246 Make_Selected_Component (Loc,
14247 Prefix => Make_Identifier (Loc, Name_uInit),
14248 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14249 else
14250 Append_To (Args,
14251 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14252 end if;
14253 end if;
14255 -- Optional Stack parameter
14257 if Restricted_Profile then
14259 -- If the stack has been preallocated by the expander then
14260 -- pass its address. Otherwise, pass a null address.
14262 if Preallocated_Stacks_On_Target then
14263 Append_To (Args,
14264 Make_Attribute_Reference (Loc,
14265 Prefix =>
14266 Make_Selected_Component (Loc,
14267 Prefix => Make_Identifier (Loc, Name_uInit),
14268 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14269 Attribute_Name => Name_Address));
14271 else
14272 Append_To (Args,
14273 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14274 end if;
14275 end if;
14277 -- Size parameter. If no Storage_Size pragma is present, then
14278 -- the size is taken from the taskZ variable for the type, which
14279 -- is either Unspecified_Size, or has been reset by the use of
14280 -- a Storage_Size attribute definition clause. If a pragma is
14281 -- present, then the size is taken from the _Size field of the
14282 -- task value record, which was set from the pragma value.
14284 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14285 Append_To (Args,
14286 Make_Selected_Component (Loc,
14287 Prefix => Make_Identifier (Loc, Name_uInit),
14288 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14290 else
14291 Append_To (Args,
14292 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14293 end if;
14295 -- Secondary_Stack parameter used for restricted profiles
14297 if Restricted_Profile then
14299 -- If the secondary stack has been allocated by the expander then
14300 -- pass its access pointer. Otherwise, pass null.
14302 if Create_Secondary_Stack_For_Task (Ttyp) then
14303 Append_To (Args,
14304 Make_Attribute_Reference (Loc,
14305 Prefix =>
14306 Make_Selected_Component (Loc,
14307 Prefix => Make_Identifier (Loc, Name_uInit),
14308 Selector_Name =>
14309 Make_Identifier (Loc, Name_uSecondary_Stack)),
14310 Attribute_Name => Name_Unrestricted_Access));
14312 else
14313 Append_To (Args, Make_Null (Loc));
14314 end if;
14315 end if;
14317 -- Secondary_Stack_Size parameter. Set RE_Unspecified_Size unless there
14318 -- is a Secondary_Stack_Size pragma, in which case take the value from
14319 -- the pragma. If the restriction No_Secondary_Stack is active then a
14320 -- size of 0 is passed regardless to prevent the allocation of the
14321 -- unused stack.
14323 if Restriction_Active (No_Secondary_Stack) then
14324 Append_To (Args, Make_Integer_Literal (Loc, 0));
14326 elsif Has_Rep_Pragma
14327 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14328 then
14329 Append_To (Args,
14330 Make_Selected_Component (Loc,
14331 Prefix => Make_Identifier (Loc, Name_uInit),
14332 Selector_Name =>
14333 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14335 else
14336 Append_To (Args,
14337 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14338 end if;
14340 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14341 -- Task_Info pragma, in which case we take the value from the pragma.
14343 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14344 Append_To (Args,
14345 Make_Selected_Component (Loc,
14346 Prefix => Make_Identifier (Loc, Name_uInit),
14347 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14349 else
14350 Append_To (Args,
14351 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14352 end if;
14354 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14355 -- in which case we take the value from the rep item. The parameter is
14356 -- passed as an Integer because in the case of unspecified CPU the
14357 -- value is not in the range of CPU_Range.
14359 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14360 Append_To (Args,
14361 Convert_To (Standard_Integer,
14362 Make_Selected_Component (Loc,
14363 Prefix => Make_Identifier (Loc, Name_uInit),
14364 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14365 else
14366 Append_To (Args,
14367 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14368 end if;
14370 if not Restricted_Profile or else Task_Dispatching_Policy = 'E' then
14372 -- Deadline parameter. If no Relative_Deadline pragma is present,
14373 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14374 -- the deadline is taken from the _Relative_Deadline field of the
14375 -- task value record, which was set from the pragma value. Note that
14376 -- this parameter must not be generated for the restricted profiles
14377 -- since Ravenscar does not allow deadlines.
14379 -- Case where pragma Relative_Deadline applies: use given value
14381 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14382 Append_To (Args,
14383 Make_Selected_Component (Loc,
14384 Prefix => Make_Identifier (Loc, Name_uInit),
14385 Selector_Name =>
14386 Make_Identifier (Loc, Name_uRelative_Deadline)));
14388 -- No pragma Relative_Deadline apply to the task
14390 else
14391 Append_To (Args,
14392 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14393 end if;
14394 end if;
14396 if not Restricted_Profile then
14398 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14399 -- present, then the dispatching domain is null. If a rep item is
14400 -- present, then the dispatching domain is taken from the
14401 -- _Dispatching_Domain field of the task value record, which was set
14402 -- from the rep item value.
14404 -- Case where Dispatching_Domain rep item applies: use given value
14406 if Has_Rep_Item
14407 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14408 then
14409 Append_To (Args,
14410 Make_Selected_Component (Loc,
14411 Prefix =>
14412 Make_Identifier (Loc, Name_uInit),
14413 Selector_Name =>
14414 Make_Identifier (Loc, Name_uDispatching_Domain)));
14416 -- No pragma or aspect Dispatching_Domain applies to the task
14418 else
14419 Append_To (Args, Make_Null (Loc));
14420 end if;
14422 -- Number of entries. This is an expression of the form:
14424 -- n + _Init.a'Length + _Init.a'B'Length + ...
14426 -- where a,b... are the entry family names for the task definition
14428 Ecount :=
14429 Build_Entry_Count_Expression
14430 (Ttyp,
14431 Component_Items
14432 (Component_List
14433 (Type_Definition
14434 (Parent (Corresponding_Record_Type (Ttyp))))),
14435 Loc);
14436 Append_To (Args, Ecount);
14438 -- Master parameter. This is a reference to the _Master parameter of
14439 -- the initialization procedure, except in the case of the pragma
14440 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14441 -- System.Tasking.Library_Task_Level.
14443 if Restriction_Active (No_Task_Hierarchy) = False then
14444 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14445 else
14446 Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level));
14447 end if;
14448 end if;
14450 -- State parameter. This is a pointer to the task body procedure. The
14451 -- required value is obtained by taking 'Unrestricted_Access of the task
14452 -- body procedure and converting it (with an unchecked conversion) to
14453 -- the type required by the task kernel. For further details, see the
14454 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14455 -- than 'Address in order to avoid creating trampolines.
14457 declare
14458 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14459 Subp_Ptr_Typ : constant Node_Id :=
14460 Create_Itype (E_Access_Subprogram_Type, Tdec);
14461 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14463 begin
14464 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14465 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14467 -- Be sure to freeze a reference to the access-to-subprogram type,
14468 -- otherwise gigi will complain that it's in the wrong scope, because
14469 -- it's actually inside the init procedure for the record type that
14470 -- corresponds to the task type.
14472 Set_Itype (Ref, Subp_Ptr_Typ);
14473 Append_Freeze_Action (Task_Rec, Ref);
14475 Append_To (Args,
14476 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14477 Make_Qualified_Expression (Loc,
14478 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14479 Expression =>
14480 Make_Attribute_Reference (Loc,
14481 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14482 Attribute_Name => Name_Unrestricted_Access))));
14483 end;
14485 -- Discriminants parameter. This is just the address of the task
14486 -- value record itself (which contains the discriminant values
14488 Append_To (Args,
14489 Make_Attribute_Reference (Loc,
14490 Prefix => Make_Identifier (Loc, Name_uInit),
14491 Attribute_Name => Name_Address));
14493 -- Elaborated parameter. This is an access to the elaboration Boolean
14495 Append_To (Args,
14496 Make_Attribute_Reference (Loc,
14497 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14498 Attribute_Name => Name_Unchecked_Access));
14500 -- Add Chain parameter (not done for sequential elaboration policy, see
14501 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14503 if Partition_Elaboration_Policy /= 'S' then
14504 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14505 end if;
14507 -- Task name parameter. Take this from the _Task_Id parameter to the
14508 -- init call unless there is a Task_Name pragma, in which case we take
14509 -- the value from the pragma.
14511 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14512 -- Copy expression in full, because it may be dynamic and have
14513 -- side effects.
14515 Append_To (Args,
14516 New_Copy_Tree
14517 (Expression
14518 (First
14519 (Pragma_Argument_Associations
14520 (Get_Rep_Pragma
14521 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14523 else
14524 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14525 end if;
14527 -- Created_Task parameter. This is the _Task_Id field of the task
14528 -- record value
14530 Append_To (Args,
14531 Make_Selected_Component (Loc,
14532 Prefix => Make_Identifier (Loc, Name_uInit),
14533 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14535 declare
14536 Create_RE : RE_Id;
14538 begin
14539 if Restricted_Profile then
14540 if Partition_Elaboration_Policy = 'S' then
14541 Create_RE := RE_Create_Restricted_Task_Sequential;
14542 else
14543 Create_RE := RE_Create_Restricted_Task;
14544 end if;
14545 else
14546 Create_RE := RE_Create_Task;
14547 end if;
14549 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14550 end;
14552 return
14553 Make_Procedure_Call_Statement (Loc,
14554 Name => Name,
14555 Parameter_Associations => Args);
14556 end Make_Task_Create_Call;
14558 ------------------------------
14559 -- Next_Protected_Operation --
14560 ------------------------------
14562 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14563 Next_Op : Node_Id;
14565 begin
14566 -- Check whether there is a subsequent body for a protected operation
14567 -- in the current protected body. In Ada2012 that includes expression
14568 -- functions that are completions.
14570 Next_Op := Next (N);
14571 while Present (Next_Op)
14572 and then Nkind (Next_Op) not in
14573 N_Subprogram_Body | N_Entry_Body | N_Expression_Function
14574 loop
14575 Next (Next_Op);
14576 end loop;
14578 return Next_Op;
14579 end Next_Protected_Operation;
14581 ---------------------
14582 -- Null_Statements --
14583 ---------------------
14585 function Null_Statements (Stats : List_Id) return Boolean is
14586 Stmt : Node_Id;
14588 begin
14589 Stmt := First (Stats);
14590 while Nkind (Stmt) /= N_Empty
14591 and then (Nkind (Stmt) in N_Null_Statement | N_Label
14592 or else
14593 (Nkind (Stmt) = N_Pragma
14594 and then
14595 Pragma_Name_Unmapped (Stmt) in Name_Unreferenced
14596 | Name_Unmodified
14597 | Name_Warnings))
14598 loop
14599 Next (Stmt);
14600 end loop;
14602 return Nkind (Stmt) = N_Empty;
14603 end Null_Statements;
14605 --------------------------
14606 -- Parameter_Block_Pack --
14607 --------------------------
14609 function Parameter_Block_Pack
14610 (Loc : Source_Ptr;
14611 Blk_Typ : Entity_Id;
14612 Actuals : List_Id;
14613 Formals : List_Id;
14614 Decls : List_Id;
14615 Stmts : List_Id) return Entity_Id
14617 Actual : Entity_Id;
14618 Expr : Node_Id := Empty;
14619 Formal : Entity_Id;
14620 Has_Param : Boolean := False;
14621 P : Entity_Id;
14622 Params : List_Id;
14623 Temp_Asn : Node_Id;
14624 Temp_Nam : Node_Id;
14626 begin
14627 Actual := First (Actuals);
14628 Formal := Defining_Identifier (First (Formals));
14629 Params := New_List;
14630 while Present (Actual) loop
14631 if Is_By_Copy_Type (Etype (Actual)) then
14632 -- Generate:
14633 -- Jnn : aliased <formal-type>
14635 Temp_Nam := Make_Temporary (Loc, 'J');
14637 Append_To (Decls,
14638 Make_Object_Declaration (Loc,
14639 Aliased_Present => True,
14640 Defining_Identifier => Temp_Nam,
14641 Object_Definition =>
14642 New_Occurrence_Of (Etype (Formal), Loc)));
14644 -- The object is initialized with an explicit assignment
14645 -- later. Indicate that it does not need an initialization
14646 -- to prevent spurious warnings if the type excludes null.
14648 Set_No_Initialization (Last (Decls));
14650 if Ekind (Formal) /= E_Out_Parameter then
14652 -- Generate:
14653 -- Jnn := <actual>
14655 Temp_Asn :=
14656 New_Occurrence_Of (Temp_Nam, Loc);
14658 Set_Assignment_OK (Temp_Asn);
14660 Append_To (Stmts,
14661 Make_Assignment_Statement (Loc,
14662 Name => Temp_Asn,
14663 Expression => New_Copy_Tree (Actual)));
14664 end if;
14666 -- If the actual is not controlling, generate:
14668 -- Jnn'unchecked_access
14670 -- and add it to aggegate for access to formals. Note that the
14671 -- actual may be by-copy but still be a controlling actual if it
14672 -- is an access to class-wide interface.
14674 if not Is_Controlling_Actual (Actual) then
14675 Append_To (Params,
14676 Make_Attribute_Reference (Loc,
14677 Attribute_Name => Name_Unchecked_Access,
14678 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14680 Has_Param := True;
14681 end if;
14683 -- The controlling parameter is omitted
14685 else
14686 if not Is_Controlling_Actual (Actual) then
14687 Append_To (Params,
14688 Make_Reference (Loc, New_Copy_Tree (Actual)));
14690 Has_Param := True;
14691 end if;
14692 end if;
14694 Next_Actual (Actual);
14695 Next_Formal_With_Extras (Formal);
14696 end loop;
14698 if Has_Param then
14699 Expr := Make_Aggregate (Loc, Params);
14700 end if;
14702 -- Generate:
14703 -- P : Ann := (
14704 -- J1'unchecked_access;
14705 -- <actual2>'reference;
14706 -- ...);
14708 P := Make_Temporary (Loc, 'P');
14710 Append_To (Decls,
14711 Make_Object_Declaration (Loc,
14712 Defining_Identifier => P,
14713 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14714 Expression => Expr));
14716 return P;
14717 end Parameter_Block_Pack;
14719 ----------------------------
14720 -- Parameter_Block_Unpack --
14721 ----------------------------
14723 function Parameter_Block_Unpack
14724 (Loc : Source_Ptr;
14725 P : Entity_Id;
14726 Actuals : List_Id;
14727 Formals : List_Id) return List_Id
14729 Actual : Entity_Id;
14730 Asnmt : Node_Id;
14731 Formal : Entity_Id;
14732 Has_Asnmt : Boolean := False;
14733 Result : constant List_Id := New_List;
14735 begin
14736 Actual := First (Actuals);
14737 Formal := Defining_Identifier (First (Formals));
14738 while Present (Actual) loop
14739 if Is_By_Copy_Type (Etype (Actual))
14740 and then Ekind (Formal) /= E_In_Parameter
14741 then
14742 -- Generate:
14743 -- <actual> := P.<formal>;
14745 Asnmt :=
14746 Make_Assignment_Statement (Loc,
14747 Name =>
14748 New_Copy (Actual),
14749 Expression =>
14750 Make_Explicit_Dereference (Loc,
14751 Make_Selected_Component (Loc,
14752 Prefix =>
14753 New_Occurrence_Of (P, Loc),
14754 Selector_Name =>
14755 Make_Identifier (Loc, Chars (Formal)))));
14757 Set_Assignment_OK (Name (Asnmt));
14758 Append_To (Result, Asnmt);
14760 Has_Asnmt := True;
14761 end if;
14763 Next_Actual (Actual);
14764 Next_Formal_With_Extras (Formal);
14765 end loop;
14767 if Has_Asnmt then
14768 return Result;
14769 else
14770 return New_List (Make_Null_Statement (Loc));
14771 end if;
14772 end Parameter_Block_Unpack;
14774 ---------------------
14775 -- Reset_Scopes_To --
14776 ---------------------
14778 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id) is
14779 function Reset_Scope (N : Node_Id) return Traverse_Result;
14780 -- Temporaries may have been declared during expansion of the procedure
14781 -- created for an entry body or an accept alternative. Indicate that
14782 -- their scope is the new body, to ensure proper generation of uplevel
14783 -- references where needed during unnesting.
14785 procedure Reset_Scopes is new Traverse_Proc (Reset_Scope);
14787 -----------------
14788 -- Reset_Scope --
14789 -----------------
14791 function Reset_Scope (N : Node_Id) return Traverse_Result is
14792 Decl : Node_Id;
14794 begin
14795 -- If this is a block statement with an Identifier, it forms a scope,
14796 -- so we want to reset its scope but not look inside.
14798 if N /= Bod
14799 and then Nkind (N) = N_Block_Statement
14800 and then Present (Identifier (N))
14801 then
14802 Set_Scope (Entity (Identifier (N)), E);
14803 return Skip;
14805 -- Ditto for a package declaration or a full type declaration, etc.
14807 elsif (Nkind (N) = N_Package_Declaration
14808 and then N /= Specification (N))
14809 or else Nkind (N) in N_Declaration
14810 or else Nkind (N) in N_Renaming_Declaration
14811 then
14812 Set_Scope (Defining_Entity (N), E);
14813 return Skip;
14815 elsif N = Bod then
14817 -- Scan declarations in new body. Declarations in the statement
14818 -- part will be handled during later traversal.
14820 Decl := First (Declarations (N));
14821 while Present (Decl) loop
14822 Reset_Scopes (Decl);
14823 Next (Decl);
14824 end loop;
14826 elsif Nkind (N) = N_Freeze_Entity then
14828 -- Scan the actions associated with a freeze node, which may
14829 -- actually be declarations with entities that need to have
14830 -- their scopes reset.
14832 Decl := First (Actions (N));
14833 while Present (Decl) loop
14834 Reset_Scopes (Decl);
14835 Next (Decl);
14836 end loop;
14838 elsif N /= Bod and then Nkind (N) in N_Proper_Body then
14840 -- A subprogram without a separate declaration may be encountered,
14841 -- and we need to reset the subprogram's entity's scope.
14843 if Nkind (N) = N_Subprogram_Body then
14844 Set_Scope (Defining_Entity (Specification (N)), E);
14845 end if;
14847 return Skip;
14848 end if;
14850 return OK;
14851 end Reset_Scope;
14853 -- Start of processing for Reset_Scopes_To
14855 begin
14856 Reset_Scopes (Bod);
14857 end Reset_Scopes_To;
14859 ----------------------
14860 -- Set_Discriminals --
14861 ----------------------
14863 procedure Set_Discriminals (Dec : Node_Id) is
14864 D : Entity_Id;
14865 Pdef : Entity_Id;
14866 D_Minal : Entity_Id;
14868 begin
14869 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14870 Pdef := Defining_Identifier (Dec);
14872 if Has_Discriminants (Pdef) then
14873 D := First_Discriminant (Pdef);
14874 while Present (D) loop
14875 D_Minal :=
14876 Make_Defining_Identifier (Sloc (D),
14877 Chars => New_External_Name (Chars (D), 'D'));
14879 Mutate_Ekind (D_Minal, E_Constant);
14880 Set_Etype (D_Minal, Etype (D));
14881 Set_Scope (D_Minal, Pdef);
14882 Set_Discriminal (D, D_Minal);
14883 Set_Discriminal_Link (D_Minal, D);
14885 Next_Discriminant (D);
14886 end loop;
14887 end if;
14888 end Set_Discriminals;
14890 -----------------------
14891 -- Trivial_Accept_OK --
14892 -----------------------
14894 function Trivial_Accept_OK return Boolean is
14895 begin
14896 case Opt.Task_Dispatching_Policy is
14898 -- If we have the default task dispatching policy in effect, we can
14899 -- definitely do the optimization (one way of looking at this is to
14900 -- think of the formal definition of the default policy being allowed
14901 -- to run any task it likes after a rendezvous, so even if notionally
14902 -- a full rescheduling occurs, we can say that our dispatching policy
14903 -- (i.e. the default dispatching policy) reorders the queue to be the
14904 -- same as just before the call.
14906 when ' ' =>
14907 return True;
14909 -- FIFO_Within_Priorities certainly does not permit this
14910 -- optimization since the Rendezvous is a scheduling action that may
14911 -- require some other task to be run.
14913 when 'F' =>
14914 return False;
14916 -- For now, disallow the optimization for all other policies. This
14917 -- may be over-conservative, but it is certainly not incorrect.
14919 when others =>
14920 return False;
14921 end case;
14922 end Trivial_Accept_OK;
14924 end Exp_Ch9;