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