2013-11-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob8db80bde74bb485568bbb401ca843fe17e7d0887
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-2013, 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 Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch3; use Exp_Ch3;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Dbug; use Exp_Dbug;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Sel; use Exp_Sel;
37 with Exp_Smem; use Exp_Smem;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
41 with Hostparm;
42 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch9; use Sem_Ch9;
55 with Sem_Ch11; use Sem_Ch11;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Exp_Ch9 is
70 -- The following constant establishes the upper bound for the index of
71 -- an entry family. It is used to limit the allocated size of protected
72 -- types with defaulted discriminant of an integer type, when the bound
73 -- of some entry family depends on a discriminant. The limitation to
74 -- entry families of 128K should be reasonable in all cases, and is a
75 -- documented implementation restriction.
77 Entry_Family_Bound : constant Int := 2**16;
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Actual_Index_Expression
84 (Sloc : Source_Ptr;
85 Ent : Entity_Id;
86 Index : Node_Id;
87 Tsk : Entity_Id) return Node_Id;
88 -- Compute the index position for an entry call. Tsk is the target task. If
89 -- the bounds of some entry family depend on discriminants, the expression
90 -- computed by this function uses the discriminants of the target task.
92 procedure Add_Object_Pointer
93 (Loc : Source_Ptr;
94 Conc_Typ : Entity_Id;
95 Decls : List_Id);
96 -- Prepend an object pointer declaration to the declaration list Decls.
97 -- This object pointer is initialized to a type conversion of the System.
98 -- Address pointer passed to entry barrier functions and entry body
99 -- procedures.
101 procedure Add_Formal_Renamings
102 (Spec : Node_Id;
103 Decls : List_Id;
104 Ent : Entity_Id;
105 Loc : Source_Ptr);
106 -- Create renaming declarations for the formals, inside the procedure that
107 -- implements an entry body. The renamings make the original names of the
108 -- formals accessible to gdb, and serve no other purpose.
109 -- Spec is the specification of the procedure being built.
110 -- Decls is the list of declarations to be enhanced.
111 -- Ent is the entity for the original entry body.
113 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
114 -- Transform accept statement into a block with added exception handler.
115 -- Used both for simple accept statements and for accept alternatives in
116 -- select statements. Astat is the accept statement.
118 function Build_Barrier_Function
119 (N : Node_Id;
120 Ent : Entity_Id;
121 Pid : Node_Id) return Node_Id;
122 -- Build the function body returning the value of the barrier expression
123 -- for the specified entry body.
125 function Build_Barrier_Function_Specification
126 (Loc : Source_Ptr;
127 Def_Id : Entity_Id) return Node_Id;
128 -- Build a specification for a function implementing the protected entry
129 -- barrier of the specified entry body.
131 function Build_Corresponding_Record
132 (N : Node_Id;
133 Ctyp : Node_Id;
134 Loc : Source_Ptr) return Node_Id;
135 -- Common to tasks and protected types. Copy discriminant specifications,
136 -- build record declaration. N is the type declaration, Ctyp is the
137 -- concurrent entity (task type or protected type).
139 function Build_Dispatching_Tag_Check
140 (K : Entity_Id;
141 N : Node_Id) return Node_Id;
142 -- Utility to create the tree to check whether the dispatching call in
143 -- a timed entry call, a conditional entry call, or an asynchronous
144 -- transfer of control is a call to a primitive of a non-synchronized type.
145 -- K is the temporary that holds the tagged kind of the target object, and
146 -- N is the enclosing construct.
148 function Build_Entry_Count_Expression
149 (Concurrent_Type : Node_Id;
150 Component_List : List_Id;
151 Loc : Source_Ptr) return Node_Id;
152 -- Compute number of entries for concurrent object. This is a count of
153 -- simple entries, followed by an expression that computes the length
154 -- of the range of each entry family. A single array with that size is
155 -- allocated for each concurrent object of the type.
157 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
158 -- Build the function that translates the entry index in the call
159 -- (which depends on the size of entry families) into an index into the
160 -- Entry_Bodies_Array, to determine the body and barrier function used
161 -- in a protected entry call. A pointer to this function appears in every
162 -- protected object.
164 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
165 -- Build subprogram declaration for previous one
167 function Build_Lock_Free_Protected_Subprogram_Body
168 (N : Node_Id;
169 Prot_Typ : Node_Id;
170 Unprot_Spec : Node_Id) return Node_Id;
171 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
172 -- the subprogram specification of the unprotected version of N. Transform
173 -- N such that it invokes the unprotected version of the body.
175 function Build_Lock_Free_Unprotected_Subprogram_Body
176 (N : Node_Id;
177 Prot_Typ : Node_Id) return Node_Id;
178 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
179 -- of N where the original statements of N are synchronized through atomic
180 -- actions such as compare and exchange. Prior to invoking this routine, it
181 -- has been established that N can be implemented in a lock-free fashion.
183 function Build_Parameter_Block
184 (Loc : Source_Ptr;
185 Actuals : List_Id;
186 Formals : List_Id;
187 Decls : List_Id) return Entity_Id;
188 -- Generate an access type for each actual parameter in the list Actuals.
189 -- Create an encapsulating record that contains all the actuals and return
190 -- its type. Generate:
191 -- type Ann1 is access all <actual1-type>
192 -- ...
193 -- type AnnN is access all <actualN-type>
194 -- type Pnn is record
195 -- <formal1> : Ann1;
196 -- ...
197 -- <formalN> : AnnN;
198 -- end record;
200 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
201 -- Build body of wrapper procedure for an entry or entry family that has
202 -- pre/postconditions. The body gathers the PPC's and expands them in the
203 -- usual way, and performs the entry call itself. This way preconditions
204 -- are evaluated before the call is queued. E is the entry in question,
205 -- and Decl is the enclosing synchronized type declaration at whose
206 -- freeze point the generated body is analyzed.
208 function Build_Protected_Entry
209 (N : Node_Id;
210 Ent : Entity_Id;
211 Pid : Node_Id) return Node_Id;
212 -- Build the procedure implementing the statement sequence of the specified
213 -- entry body.
215 function Build_Protected_Entry_Specification
216 (Loc : Source_Ptr;
217 Def_Id : Entity_Id;
218 Ent_Id : Entity_Id) return Node_Id;
219 -- Build a specification for the procedure implementing the statements of
220 -- the specified entry body. Add attributes associating it with the entry
221 -- defining identifier Ent_Id.
223 function Build_Protected_Spec
224 (N : Node_Id;
225 Obj_Type : Entity_Id;
226 Ident : Entity_Id;
227 Unprotected : Boolean := False) return List_Id;
228 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
229 -- Subprogram_Type. Builds signature of protected subprogram, adding the
230 -- formal that corresponds to the object itself. For an access to protected
231 -- subprogram, there is no object type to specify, so the parameter has
232 -- type Address and mode In. An indirect call through such a pointer will
233 -- convert the address to a reference to the actual object. The object is
234 -- a limited record and therefore a by_reference type.
236 function Build_Protected_Subprogram_Body
237 (N : Node_Id;
238 Pid : Node_Id;
239 N_Op_Spec : Node_Id) return Node_Id;
240 -- This function is used to construct the protected version of a protected
241 -- subprogram. Its statement sequence first defers abort, then locks
242 -- the associated protected object, and then enters a block that contains
243 -- a call to the unprotected version of the subprogram (for details, see
244 -- Build_Unprotected_Subprogram_Body). This block statement requires
245 -- a cleanup handler that unlocks the object in all cases.
246 -- (see Exp_Ch7.Expand_Cleanup_Actions).
248 function Build_Renamed_Formal_Declaration
249 (New_F : Entity_Id;
250 Formal : Entity_Id;
251 Comp : Entity_Id;
252 Renamed_Formal : Node_Id) return Node_Id;
253 -- Create a renaming declaration for a formal, within a protected entry
254 -- body or an accept body. The renamed object is a component of the
255 -- parameter block that is a parameter in the entry call.
257 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
258 -- does not dereference the corresponding component to prevent an illegal
259 -- use of the incomplete type (AI05-0151).
261 function Build_Selected_Name
262 (Prefix : Entity_Id;
263 Selector : Entity_Id;
264 Append_Char : Character := ' ') return Name_Id;
265 -- Build a name in the form of Prefix__Selector, with an optional
266 -- character appended. This is used for internal subprograms generated
267 -- for operations of protected types, including barrier functions.
268 -- For the subprograms generated for entry bodies and entry barriers,
269 -- the generated name includes a sequence number that makes names
270 -- unique in the presence of entry overloading. This is necessary
271 -- because entry body procedures and barrier functions all have the
272 -- same signature.
274 procedure Build_Simple_Entry_Call
275 (N : Node_Id;
276 Concval : Node_Id;
277 Ename : Node_Id;
278 Index : Node_Id);
279 -- Some comments here would be useful ???
281 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
282 -- This routine constructs a specification for the procedure that we will
283 -- build for the task body for task type T. The spec has the form:
285 -- procedure tnameB (_Task : access tnameV);
287 -- where name is the character name taken from the task type entity that
288 -- is passed as the argument to the procedure, and tnameV is the task
289 -- value type that is associated with the task type.
291 function Build_Unprotected_Subprogram_Body
292 (N : Node_Id;
293 Pid : Node_Id) return Node_Id;
294 -- This routine constructs the unprotected version of a protected
295 -- subprogram body, which is contains all of the code in the
296 -- original, unexpanded body. This is the version of the protected
297 -- subprogram that is called from all protected operations on the same
298 -- object, including the protected version of the same subprogram.
300 procedure Build_Wrapper_Bodies
301 (Loc : Source_Ptr;
302 Typ : Entity_Id;
303 N : Node_Id);
304 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
305 -- record of a concurrent type. N is the insertion node where all bodies
306 -- will be placed. This routine builds the bodies of the subprograms which
307 -- serve as an indirection mechanism to overriding primitives of concurrent
308 -- types, entries and protected procedures. Any new body is analyzed.
310 procedure Build_Wrapper_Specs
311 (Loc : Source_Ptr;
312 Typ : Entity_Id;
313 N : in out Node_Id);
314 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
315 -- record of a concurrent type. N is the insertion node where all specs
316 -- will be placed. This routine builds the specs of the subprograms which
317 -- serve as an indirection mechanism to overriding primitives of concurrent
318 -- types, entries and protected procedures. Any new spec is analyzed.
320 procedure Collect_Entry_Families
321 (Loc : Source_Ptr;
322 Cdecls : List_Id;
323 Current_Node : in out Node_Id;
324 Conctyp : Entity_Id);
325 -- For each entry family in a concurrent type, create an anonymous array
326 -- type of the right size, and add a component to the corresponding_record.
328 function Concurrent_Object
329 (Spec_Id : Entity_Id;
330 Conc_Typ : Entity_Id) return Entity_Id;
331 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
332 -- the entity associated with the concurrent object in the Protected_Body_
333 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
334 -- denotes formal parameter _O, _object or _task.
336 function Copy_Result_Type (Res : Node_Id) return Node_Id;
337 -- Copy the result type of a function specification, when building the
338 -- internal operation corresponding to a protected function, or when
339 -- expanding an access to protected function. If the result is an anonymous
340 -- access to subprogram itself, we need to create a new signature with the
341 -- same parameter names and the same resolved types, but with new entities
342 -- for the formals.
344 procedure Debug_Private_Data_Declarations (Decls : List_Id);
345 -- Decls is a list which may contain the declarations created by Install_
346 -- Private_Data_Declarations. All generated entities are marked as needing
347 -- debug info and debug nodes are manually generation where necessary. This
348 -- step of the expansion must to be done after private data has been moved
349 -- to its final resting scope to ensure proper visibility of debug objects.
351 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
352 -- If control flow optimizations are suppressed, and Alt is an accept,
353 -- delay, or entry call alternative with no trailing statements, insert a
354 -- null trailing statement with the given Loc (which is the sloc of the
355 -- accept, delay, or entry call statement). There might not be any
356 -- generated code for the accept, delay, or entry call itself (the
357 -- effect of these statements is part of the general processsing done
358 -- for the enclosing selective accept, timed entry call, or asynchronous
359 -- select), and the null statement is there to carry the sloc of that
360 -- statement to the back-end for trace-based coverage analysis purposes.
362 procedure Extract_Dispatching_Call
363 (N : Node_Id;
364 Call_Ent : out Entity_Id;
365 Object : out Entity_Id;
366 Actuals : out List_Id;
367 Formals : out List_Id);
368 -- Given a dispatching call, extract the entity of the name of the call,
369 -- its actual dispatching object, its actual parameters and the formal
370 -- parameters of the overridden interface-level version. If the type of
371 -- the dispatching object is an access type then an explicit dereference
372 -- is returned in Object.
374 procedure Extract_Entry
375 (N : Node_Id;
376 Concval : out Node_Id;
377 Ename : out Node_Id;
378 Index : out Node_Id);
379 -- Given an entry call, returns the associated concurrent object,
380 -- the entry name, and the entry family index.
382 function Family_Offset
383 (Loc : Source_Ptr;
384 Hi : Node_Id;
385 Lo : Node_Id;
386 Ttyp : Entity_Id;
387 Cap : Boolean) return Node_Id;
388 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in
389 -- an accept statement, or the upper bound in the discrete subtype of
390 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
391 -- the concurrent type of the entry. If Cap is true, the result is
392 -- capped according to Entry_Family_Bound.
394 function Family_Size
395 (Loc : Source_Ptr;
396 Hi : Node_Id;
397 Lo : Node_Id;
398 Ttyp : Entity_Id;
399 Cap : Boolean) return Node_Id;
400 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
401 -- a family, and handle properly the superflat case. This is equivalent
402 -- to the use of 'Length on the index type, but must use Family_Offset
403 -- to handle properly the case of bounds that depend on discriminants.
404 -- If Cap is true, the result is capped according to Entry_Family_Bound.
406 procedure Find_Enclosing_Context
407 (N : Node_Id;
408 Context : out Node_Id;
409 Context_Id : out Entity_Id;
410 Context_Decls : out List_Id);
411 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
412 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
413 -- nearest enclosing body, block, package or return statement and return
414 -- its constituents. Context is the enclosing construct, Context_Id is
415 -- the scope of Context_Id and Context_Decls is the declarative list of
416 -- Context.
418 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
419 -- Given a subprogram identifier, return the entity which is associated
420 -- with the protection entry index in the Protected_Body_Subprogram or the
421 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
422 -- parameter _E.
424 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
425 -- Tell whether a given subprogram cannot raise an exception
427 function Is_Potentially_Large_Family
428 (Base_Index : Entity_Id;
429 Conctyp : Entity_Id;
430 Lo : Node_Id;
431 Hi : Node_Id) return Boolean;
433 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
434 -- Determine whether Id is a function or a procedure and is marked as a
435 -- private primitive.
437 function Null_Statements (Stats : List_Id) return Boolean;
438 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
439 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
440 -- well to still count as null. Returns True for a null sequence. The
441 -- argument is the list of statements from the DO-END sequence.
443 function Parameter_Block_Pack
444 (Loc : Source_Ptr;
445 Blk_Typ : Entity_Id;
446 Actuals : List_Id;
447 Formals : List_Id;
448 Decls : List_Id;
449 Stmts : List_Id) return Entity_Id;
450 -- Set the components of the generated parameter block with the values of
451 -- the actual parameters. Generate aliased temporaries to capture the
452 -- values for types that are passed by copy. Otherwise generate a reference
453 -- to the actual's value. Return the address of the aggregate block.
454 -- Generate:
455 -- Jnn1 : alias <formal-type1>;
456 -- Jnn1 := <actual1>;
457 -- ...
458 -- P : Blk_Typ := (
459 -- Jnn1'unchecked_access;
460 -- <actual2>'reference;
461 -- ...);
463 function Parameter_Block_Unpack
464 (Loc : Source_Ptr;
465 P : Entity_Id;
466 Actuals : List_Id;
467 Formals : List_Id) return List_Id;
468 -- Retrieve the values of the components from the parameter block and
469 -- assign then to the original actual parameters. Generate:
470 -- <actual1> := P.<formal1>;
471 -- ...
472 -- <actualN> := P.<formalN>;
474 function Trivial_Accept_OK return Boolean;
475 -- If there is no DO-END block for an accept, or if the DO-END block has
476 -- only null statements, then it is possible to do the Rendezvous with much
477 -- less overhead using the Accept_Trivial routine in the run-time library.
478 -- However, this is not always a valid optimization. Whether it is valid or
479 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
480 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
481 -- a rescheduling is required, so this optimization is not allowed. This
482 -- function returns True if the optimization is permitted.
484 -----------------------------
485 -- Actual_Index_Expression --
486 -----------------------------
488 function Actual_Index_Expression
489 (Sloc : Source_Ptr;
490 Ent : Entity_Id;
491 Index : Node_Id;
492 Tsk : Entity_Id) return Node_Id
494 Ttyp : constant Entity_Id := Etype (Tsk);
495 Expr : Node_Id;
496 Num : Node_Id;
497 Lo : Node_Id;
498 Hi : Node_Id;
499 Prev : Entity_Id;
500 S : Node_Id;
502 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
503 -- Compute difference between bounds of entry family
505 --------------------------
506 -- Actual_Family_Offset --
507 --------------------------
509 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
511 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
512 -- Replace a reference to a discriminant with a selected component
513 -- denoting the discriminant of the target task.
515 -----------------------------
516 -- Actual_Discriminant_Ref --
517 -----------------------------
519 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
520 Typ : constant Entity_Id := Etype (Bound);
521 B : Node_Id;
523 begin
524 if not Is_Entity_Name (Bound)
525 or else Ekind (Entity (Bound)) /= E_Discriminant
526 then
527 if Nkind (Bound) = N_Attribute_Reference then
528 return Bound;
529 else
530 B := New_Copy_Tree (Bound);
531 end if;
533 else
534 B :=
535 Make_Selected_Component (Sloc,
536 Prefix => New_Copy_Tree (Tsk),
537 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
539 Analyze_And_Resolve (B, Typ);
540 end if;
542 return
543 Make_Attribute_Reference (Sloc,
544 Attribute_Name => Name_Pos,
545 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
546 Expressions => New_List (B));
547 end Actual_Discriminant_Ref;
549 -- Start of processing for Actual_Family_Offset
551 begin
552 return
553 Make_Op_Subtract (Sloc,
554 Left_Opnd => Actual_Discriminant_Ref (Hi),
555 Right_Opnd => Actual_Discriminant_Ref (Lo));
556 end Actual_Family_Offset;
558 -- Start of processing for Actual_Index_Expression
560 begin
561 -- The queues of entries and entry families appear in textual order in
562 -- the associated record. The entry index is computed as the sum of the
563 -- number of queues for all entries that precede the designated one, to
564 -- which is added the index expression, if this expression denotes a
565 -- member of a family.
567 -- The following is a place holder for the count of simple entries
569 Num := Make_Integer_Literal (Sloc, 1);
571 -- We construct an expression which is a series of addition operations.
572 -- See comments in Entry_Index_Expression, which is identical in
573 -- structure.
575 if Present (Index) then
576 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
578 Expr :=
579 Make_Op_Add (Sloc,
580 Left_Opnd => Num,
582 Right_Opnd =>
583 Actual_Family_Offset (
584 Make_Attribute_Reference (Sloc,
585 Attribute_Name => Name_Pos,
586 Prefix => New_Reference_To (Base_Type (S), Sloc),
587 Expressions => New_List (Relocate_Node (Index))),
588 Type_Low_Bound (S)));
589 else
590 Expr := Num;
591 end if;
593 -- Now add lengths of preceding entries and entry families
595 Prev := First_Entity (Ttyp);
597 while Chars (Prev) /= Chars (Ent)
598 or else (Ekind (Prev) /= Ekind (Ent))
599 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
600 loop
601 if Ekind (Prev) = E_Entry then
602 Set_Intval (Num, Intval (Num) + 1);
604 elsif Ekind (Prev) = E_Entry_Family then
605 S :=
606 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
608 -- The need for the following full view retrieval stems from
609 -- this complex case of nested generics and tasking:
611 -- generic
612 -- type Formal_Index is range <>;
613 -- ...
614 -- package Outer is
615 -- type Index is private;
616 -- generic
617 -- ...
618 -- package Inner is
619 -- procedure P;
620 -- end Inner;
621 -- private
622 -- type Index is new Formal_Index range 1 .. 10;
623 -- end Outer;
625 -- package body Outer is
626 -- task type T is
627 -- entry Fam (Index); -- (2)
628 -- entry E;
629 -- end T;
630 -- package body Inner is -- (3)
631 -- procedure P is
632 -- begin
633 -- T.E; -- (1)
634 -- end P;
635 -- end Inner;
636 -- ...
638 -- We are currently building the index expression for the entry
639 -- call "T.E" (1). Part of the expansion must mention the range
640 -- of the discrete type "Index" (2) of entry family "Fam".
641 -- However only the private view of type "Index" is available to
642 -- the inner generic (3) because there was no prior mention of
643 -- the type inside "Inner". This visibility requirement is
644 -- implicit and cannot be detected during the construction of
645 -- the generic trees and needs special handling.
647 if In_Instance_Body
648 and then Is_Private_Type (S)
649 and then Present (Full_View (S))
650 then
651 S := Full_View (S);
652 end if;
654 Lo := Type_Low_Bound (S);
655 Hi := Type_High_Bound (S);
657 Expr :=
658 Make_Op_Add (Sloc,
659 Left_Opnd => Expr,
660 Right_Opnd =>
661 Make_Op_Add (Sloc,
662 Left_Opnd =>
663 Actual_Family_Offset (Hi, Lo),
664 Right_Opnd =>
665 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
712 -- come from source, so that the debugger will get the right
713 -- information 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_Reference_To (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 =>
773 Make_Defining_Identifier (Loc, Name_uObject),
774 Object_Definition =>
775 New_Reference_To (Obj_Ptr, Loc),
776 Expression =>
777 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
778 Set_Debug_Info_Needed (Defining_Identifier (Decl));
779 Prepend_To (Decls, Decl);
781 -- Generate:
782 -- type poVP is access poV;
784 Decl :=
785 Make_Full_Type_Declaration (Loc,
786 Defining_Identifier =>
787 Obj_Ptr,
788 Type_Definition =>
789 Make_Access_To_Object_Definition (Loc,
790 Subtype_Indication =>
791 New_Reference_To (Rec_Typ, Loc)));
792 Set_Debug_Info_Needed (Defining_Identifier (Decl));
793 Prepend_To (Decls, Decl);
794 end Add_Object_Pointer;
796 -----------------------
797 -- Build_Accept_Body --
798 -----------------------
800 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
801 Loc : constant Source_Ptr := Sloc (Astat);
802 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
803 New_S : Node_Id;
804 Hand : Node_Id;
805 Call : Node_Id;
806 Ohandle : Node_Id;
808 begin
809 -- At the end of the statement sequence, Complete_Rendezvous is called.
810 -- A label skipping the Complete_Rendezvous, and all other accept
811 -- processing, has already been added for the expansion of requeue
812 -- statements. The Sloc is copied from the last statement since it
813 -- is really part of this last statement.
815 Call :=
816 Build_Runtime_Call
817 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
818 Insert_Before (Last (Statements (Stats)), Call);
819 Analyze (Call);
821 -- If exception handlers are present, then append Complete_Rendezvous
822 -- calls to the handlers, and construct the required outer block. As
823 -- above, the Sloc is copied from the last statement in the sequence.
825 if Present (Exception_Handlers (Stats)) then
826 Hand := First (Exception_Handlers (Stats));
827 while Present (Hand) loop
828 Call :=
829 Build_Runtime_Call
830 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
831 Append (Call, Statements (Hand));
832 Analyze (Call);
833 Next (Hand);
834 end loop;
836 New_S :=
837 Make_Handled_Sequence_Of_Statements (Loc,
838 Statements => New_List (
839 Make_Block_Statement (Loc,
840 Handled_Statement_Sequence => Stats)));
842 else
843 New_S := Stats;
844 end if;
846 -- At this stage we know that the new statement sequence does not
847 -- have an exception handler part, so we supply one to call
848 -- Exceptional_Complete_Rendezvous. This handler is
850 -- when all others =>
851 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
853 -- We handle Abort_Signal to make sure that we properly catch the abort
854 -- case and wake up the caller.
856 Ohandle := Make_Others_Choice (Loc);
857 Set_All_Others (Ohandle);
859 Set_Exception_Handlers (New_S,
860 New_List (
861 Make_Implicit_Exception_Handler (Loc,
862 Exception_Choices => New_List (Ohandle),
864 Statements => New_List (
865 Make_Procedure_Call_Statement (Sloc (Stats),
866 Name => New_Reference_To (
867 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
868 Parameter_Associations => New_List (
869 Make_Function_Call (Sloc (Stats),
870 Name => New_Reference_To (
871 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
873 Set_Parent (New_S, Astat); -- temp parent for Analyze call
874 Analyze_Exception_Handlers (Exception_Handlers (New_S));
875 Expand_Exception_Handlers (New_S);
877 -- Exceptional_Complete_Rendezvous must be called with abort
878 -- still deferred, which is the case for a "when all others" handler.
880 return New_S;
881 end Build_Accept_Body;
883 -----------------------------------
884 -- Build_Activation_Chain_Entity --
885 -----------------------------------
887 procedure Build_Activation_Chain_Entity (N : Node_Id) is
888 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
889 -- Determine whether an extended return statement has an activation
890 -- chain.
892 --------------------------
893 -- Has_Activation_Chain --
894 --------------------------
896 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
897 Decl : Node_Id;
899 begin
900 Decl := First (Return_Object_Declarations (Stmt));
901 while Present (Decl) loop
902 if Nkind (Decl) = N_Object_Declaration
903 and then Chars (Defining_Identifier (Decl)) = Name_uChain
904 then
905 return True;
906 end if;
908 Next (Decl);
909 end loop;
911 return False;
912 end Has_Activation_Chain;
914 -- Local variables
916 Context : Node_Id;
917 Context_Id : Entity_Id;
918 Decls : List_Id;
920 -- Start of processing for Build_Activation_Chain_Entity
922 begin
923 -- Activation chain is never used for sequential elaboration policy, see
924 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
926 if Partition_Elaboration_Policy = 'S' then
927 return;
928 end if;
930 Find_Enclosing_Context (N, Context, Context_Id, Decls);
932 -- If activation chain entity has not been declared already, create one
934 if Nkind (Context) = N_Extended_Return_Statement
935 or else No (Activation_Chain_Entity (Context))
936 then
937 -- Since extended return statements do not store the entity of the
938 -- chain, examine the return object declarations to avoid creating
939 -- a duplicate.
941 if Nkind (Context) = N_Extended_Return_Statement
942 and then Has_Activation_Chain (Context)
943 then
944 return;
945 end if;
947 declare
948 Loc : constant Source_Ptr := Sloc (Context);
949 Chain : Entity_Id;
950 Decl : Node_Id;
952 begin
953 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
955 -- Note: An extended return statement is not really a task
956 -- activator, but it does have an activation chain on which to
957 -- store the tasks temporarily. On successful return, the tasks
958 -- on this chain are moved to the chain passed in by the caller.
959 -- We do not build an Activation_Chain_Entity for an extended
960 -- return statement, because we do not want to build a call to
961 -- Activate_Tasks. Task activation is the responsibility of the
962 -- caller.
964 if Nkind (Context) /= N_Extended_Return_Statement then
965 Set_Activation_Chain_Entity (Context, Chain);
966 end if;
968 Decl :=
969 Make_Object_Declaration (Loc,
970 Defining_Identifier => Chain,
971 Aliased_Present => True,
972 Object_Definition =>
973 New_Reference_To (RTE (RE_Activation_Chain), Loc));
975 Prepend_To (Decls, Decl);
977 -- Ensure that the _chain appears in the proper scope of the
978 -- context.
980 if Context_Id /= Current_Scope then
981 Push_Scope (Context_Id);
982 Analyze (Decl);
983 Pop_Scope;
984 else
985 Analyze (Decl);
986 end if;
987 end;
988 end if;
989 end Build_Activation_Chain_Entity;
991 ----------------------------
992 -- Build_Barrier_Function --
993 ----------------------------
995 function Build_Barrier_Function
996 (N : Node_Id;
997 Ent : Entity_Id;
998 Pid : Node_Id) return Node_Id
1000 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
1001 Cond : constant Node_Id := Condition (Ent_Formals);
1002 Loc : constant Source_Ptr := Sloc (Cond);
1003 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1004 Op_Decls : constant List_Id := New_List;
1005 Stmt : Node_Id;
1006 Func_Body : Node_Id;
1008 begin
1009 -- Add a declaration for the Protection object, renaming declarations
1010 -- for the discriminals and privals and finally a declaration for the
1011 -- entry family index (if applicable).
1013 Install_Private_Data_Declarations (Sloc (N),
1014 Spec_Id => Func_Id,
1015 Conc_Typ => Pid,
1016 Body_Nod => N,
1017 Decls => Op_Decls,
1018 Barrier => True,
1019 Family => Ekind (Ent) = E_Entry_Family);
1021 -- If compiling with -fpreserve-control-flow, make sure we insert an
1022 -- IF statement so that the back-end knows to generate a conditional
1023 -- branch instruction, even if the condition is just the name of a
1024 -- boolean object.
1026 if Opt.Suppress_Control_Flow_Optimizations then
1027 Stmt := 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))),
1032 Else_Statements => New_List (
1033 Make_Simple_Return_Statement (Loc,
1034 New_Occurrence_Of (Standard_False, Loc))));
1036 else
1037 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1038 end if;
1040 -- Note: the condition in the barrier function needs to be properly
1041 -- processed for the C/Fortran boolean possibility, but this happens
1042 -- automatically since the return statement does this normalization.
1044 Func_Body :=
1045 Make_Subprogram_Body (Loc,
1046 Specification =>
1047 Build_Barrier_Function_Specification (Loc,
1048 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1049 Declarations => Op_Decls,
1050 Handled_Statement_Sequence =>
1051 Make_Handled_Sequence_Of_Statements (Loc,
1052 Statements => New_List (Stmt)));
1053 Set_Is_Entry_Barrier_Function (Func_Body);
1055 return Func_Body;
1056 end Build_Barrier_Function;
1058 ------------------------------------------
1059 -- Build_Barrier_Function_Specification --
1060 ------------------------------------------
1062 function Build_Barrier_Function_Specification
1063 (Loc : Source_Ptr;
1064 Def_Id : Entity_Id) return Node_Id
1066 begin
1067 Set_Debug_Info_Needed (Def_Id);
1069 return Make_Function_Specification (Loc,
1070 Defining_Unit_Name => Def_Id,
1071 Parameter_Specifications => New_List (
1072 Make_Parameter_Specification (Loc,
1073 Defining_Identifier =>
1074 Make_Defining_Identifier (Loc, Name_uO),
1075 Parameter_Type =>
1076 New_Reference_To (RTE (RE_Address), Loc)),
1078 Make_Parameter_Specification (Loc,
1079 Defining_Identifier =>
1080 Make_Defining_Identifier (Loc, Name_uE),
1081 Parameter_Type =>
1082 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1084 Result_Definition =>
1085 New_Reference_To (Standard_Boolean, Loc));
1086 end Build_Barrier_Function_Specification;
1088 --------------------------
1089 -- Build_Call_With_Task --
1090 --------------------------
1092 function Build_Call_With_Task
1093 (N : Node_Id;
1094 E : Entity_Id) return Node_Id
1096 Loc : constant Source_Ptr := Sloc (N);
1097 begin
1098 return
1099 Make_Function_Call (Loc,
1100 Name => New_Reference_To (E, Loc),
1101 Parameter_Associations => New_List (Concurrent_Ref (N)));
1102 end Build_Call_With_Task;
1104 -----------------------------
1105 -- Build_Class_Wide_Master --
1106 -----------------------------
1108 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1109 Loc : constant Source_Ptr := Sloc (Typ);
1110 Master_Id : Entity_Id;
1111 Master_Scope : Entity_Id;
1112 Name_Id : Node_Id;
1113 Related_Node : Node_Id;
1114 Ren_Decl : Node_Id;
1116 begin
1117 -- Nothing to do if there is no task hierarchy
1119 if Restriction_Active (No_Task_Hierarchy) then
1120 return;
1121 end if;
1123 -- Find the declaration that created the access type. It is either a
1124 -- type declaration, or an object declaration with an access definition,
1125 -- in which case the type is anonymous.
1127 if Is_Itype (Typ) then
1128 Related_Node := Associated_Node_For_Itype (Typ);
1129 else
1130 Related_Node := Parent (Typ);
1131 end if;
1133 Master_Scope := Find_Master_Scope (Typ);
1135 -- Nothing to do if the master scope already contains a _master entity.
1136 -- The only exception to this is the following scenario:
1138 -- Source_Scope
1139 -- Transient_Scope_1
1140 -- _master
1142 -- Transient_Scope_2
1143 -- use of master
1145 -- In this case the source scope is marked as having the master entity
1146 -- even though the actual declaration appears inside an inner scope. If
1147 -- the second transient scope requires a _master, it cannot use the one
1148 -- already declared because the entity is not visible.
1150 Name_Id := Make_Identifier (Loc, Name_uMaster);
1152 if not Has_Master_Entity (Master_Scope)
1153 or else No (Current_Entity_In_Scope (Name_Id))
1154 then
1155 declare
1156 Master_Decl : Node_Id;
1158 begin
1159 Set_Has_Master_Entity (Master_Scope);
1161 -- Generate:
1162 -- _master : constant Integer := Current_Master.all;
1164 Master_Decl :=
1165 Make_Object_Declaration (Loc,
1166 Defining_Identifier =>
1167 Make_Defining_Identifier (Loc, Name_uMaster),
1168 Constant_Present => True,
1169 Object_Definition =>
1170 New_Reference_To (Standard_Integer, Loc),
1171 Expression =>
1172 Make_Explicit_Dereference (Loc,
1173 New_Reference_To (RTE (RE_Current_Master), Loc)));
1175 Insert_Action (Related_Node, Master_Decl);
1176 Analyze (Master_Decl);
1178 -- Mark the containing scope as a task master. Masters associated
1179 -- with return statements are already marked at this stage (see
1180 -- Analyze_Subprogram_Body).
1182 if Ekind (Current_Scope) /= E_Return_Statement then
1183 declare
1184 Par : Node_Id := Related_Node;
1186 begin
1187 while Nkind (Par) /= N_Compilation_Unit loop
1188 Par := Parent (Par);
1190 -- If we fall off the top, we are at the outer level, and
1191 -- the environment task is our effective master, so
1192 -- nothing to mark.
1194 if Nkind_In (Par, N_Block_Statement,
1195 N_Subprogram_Body,
1196 N_Task_Body)
1197 then
1198 Set_Is_Task_Master (Par);
1199 exit;
1200 end if;
1201 end loop;
1202 end;
1203 end if;
1204 end;
1205 end if;
1207 Master_Id :=
1208 Make_Defining_Identifier (Loc,
1209 New_External_Name (Chars (Typ), 'M'));
1211 -- Generate:
1212 -- Mnn renames _master;
1214 Ren_Decl :=
1215 Make_Object_Renaming_Declaration (Loc,
1216 Defining_Identifier => Master_Id,
1217 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
1218 Name => Name_Id);
1220 Insert_Action (Related_Node, Ren_Decl);
1222 Set_Master_Id (Typ, Master_Id);
1223 end Build_Class_Wide_Master;
1225 --------------------------------
1226 -- Build_Corresponding_Record --
1227 --------------------------------
1229 function Build_Corresponding_Record
1230 (N : Node_Id;
1231 Ctyp : Entity_Id;
1232 Loc : Source_Ptr) return Node_Id
1234 Rec_Ent : constant Entity_Id :=
1235 Make_Defining_Identifier
1236 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1237 Disc : Entity_Id;
1238 Dlist : List_Id;
1239 New_Disc : Entity_Id;
1240 Cdecls : List_Id;
1242 begin
1243 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1244 Set_Ekind (Rec_Ent, E_Record_Type);
1245 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1246 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1247 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1248 Set_Stored_Constraint (Rec_Ent, No_Elist);
1249 Cdecls := New_List;
1251 -- Use discriminals to create list of discriminants for record, and
1252 -- create new discriminals for use in default expressions, etc. It is
1253 -- worth noting that a task discriminant gives rise to 5 entities;
1255 -- a) The original discriminant.
1256 -- b) The discriminal for use in the task.
1257 -- c) The discriminant of the corresponding record.
1258 -- d) The discriminal for the init proc of the corresponding record.
1259 -- e) The local variable that renames the discriminant in the procedure
1260 -- for the task body.
1262 -- In fact the discriminals b) are used in the renaming declarations
1263 -- for e). See details in einfo (Handling of Discriminants).
1265 if Present (Discriminant_Specifications (N)) then
1266 Dlist := New_List;
1267 Disc := First_Discriminant (Ctyp);
1269 while Present (Disc) loop
1270 New_Disc := CR_Discriminant (Disc);
1272 Append_To (Dlist,
1273 Make_Discriminant_Specification (Loc,
1274 Defining_Identifier => New_Disc,
1275 Discriminant_Type =>
1276 New_Occurrence_Of (Etype (Disc), Loc),
1277 Expression =>
1278 New_Copy (Discriminant_Default_Value (Disc))));
1280 Next_Discriminant (Disc);
1281 end loop;
1283 else
1284 Dlist := No_List;
1285 end if;
1287 -- Now we can construct the record type declaration. Note that this
1288 -- record is "limited tagged". It is "limited" to reflect the underlying
1289 -- limitedness of the task or protected object that it represents, and
1290 -- ensuring for example that it is properly passed by reference. It is
1291 -- "tagged" to give support to dispatching calls through interfaces. We
1292 -- propagate here the list of interfaces covered by the concurrent type
1293 -- (Ada 2005: AI-345).
1295 return
1296 Make_Full_Type_Declaration (Loc,
1297 Defining_Identifier => Rec_Ent,
1298 Discriminant_Specifications => Dlist,
1299 Type_Definition =>
1300 Make_Record_Definition (Loc,
1301 Component_List =>
1302 Make_Component_List (Loc,
1303 Component_Items => Cdecls),
1304 Tagged_Present =>
1305 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1306 Interface_List => Interface_List (N),
1307 Limited_Present => True));
1308 end Build_Corresponding_Record;
1310 ---------------------------------
1311 -- Build_Dispatching_Tag_Check --
1312 ---------------------------------
1314 function Build_Dispatching_Tag_Check
1315 (K : Entity_Id;
1316 N : Node_Id) return Node_Id
1318 Loc : constant Source_Ptr := Sloc (N);
1319 begin
1320 return
1321 Make_Op_Or (Loc,
1322 Make_Op_Eq (Loc,
1323 Left_Opnd => New_Reference_To (K, Loc),
1324 Right_Opnd => New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
1325 Make_Op_Eq (Loc,
1326 Left_Opnd => New_Reference_To (K, Loc),
1327 Right_Opnd => New_Reference_To (RTE (RE_TK_Tagged), Loc)));
1328 end Build_Dispatching_Tag_Check;
1330 ----------------------------------
1331 -- Build_Entry_Count_Expression --
1332 ----------------------------------
1334 function Build_Entry_Count_Expression
1335 (Concurrent_Type : Node_Id;
1336 Component_List : List_Id;
1337 Loc : Source_Ptr) return Node_Id
1339 Eindx : Nat;
1340 Ent : Entity_Id;
1341 Ecount : Node_Id;
1342 Comp : Node_Id;
1343 Lo : Node_Id;
1344 Hi : Node_Id;
1345 Typ : Entity_Id;
1346 Large : Boolean;
1348 begin
1349 -- Count number of non-family entries
1351 Eindx := 0;
1352 Ent := First_Entity (Concurrent_Type);
1353 while Present (Ent) loop
1354 if Ekind (Ent) = E_Entry then
1355 Eindx := Eindx + 1;
1356 end if;
1358 Next_Entity (Ent);
1359 end loop;
1361 Ecount := Make_Integer_Literal (Loc, Eindx);
1363 -- Loop through entry families building the addition nodes
1365 Ent := First_Entity (Concurrent_Type);
1366 Comp := First (Component_List);
1367 while Present (Ent) loop
1368 if Ekind (Ent) = E_Entry_Family then
1369 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1370 Next (Comp);
1371 end loop;
1373 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1374 Hi := Type_High_Bound (Typ);
1375 Lo := Type_Low_Bound (Typ);
1376 Large := Is_Potentially_Large_Family
1377 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1378 Ecount :=
1379 Make_Op_Add (Loc,
1380 Left_Opnd => Ecount,
1381 Right_Opnd => Family_Size
1382 (Loc, Hi, Lo, Concurrent_Type, Large));
1383 end if;
1385 Next_Entity (Ent);
1386 end loop;
1388 return Ecount;
1389 end Build_Entry_Count_Expression;
1391 -----------------------
1392 -- Build_Entry_Names --
1393 -----------------------
1395 procedure Build_Entry_Names
1396 (Obj_Ref : Node_Id;
1397 Obj_Typ : Entity_Id;
1398 Stmts : List_Id)
1400 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1401 Data : Entity_Id := Empty;
1402 Index : Entity_Id := Empty;
1403 Typ : Entity_Id := Obj_Typ;
1405 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1406 -- Given an entry [family], create a static string which denotes the
1407 -- name of Comp_Id and assign it to the underlying data structure which
1408 -- contains the entry names of a concurrent object.
1410 function Object_Reference return Node_Id;
1411 -- Return a reference to field _object or _task_id depending on the
1412 -- concurrent object being processed.
1414 ----------------------
1415 -- Build_Entry_Name --
1416 ----------------------
1418 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1419 function Build_Range (Def : Node_Id) return Node_Id;
1420 -- Given a discrete subtype definition of an entry family, generate a
1421 -- range node which covers the range of Def's type.
1423 procedure Create_Index_And_Data;
1424 -- Generate the declarations of variables Index and Data. Subsequent
1425 -- calls do nothing.
1427 function Increment_Index return Node_Id;
1428 -- Increment the index used in the assignment of string names to the
1429 -- Data array.
1431 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1432 -- Given the name of a temporary variable, create the following
1433 -- declaration for it:
1435 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1437 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1438 -- Given the name of a temporary variable, place it in the array of
1439 -- string names. Generate:
1441 -- Data (Index) := Def_Id'Unchecked_Access;
1443 -----------------
1444 -- Build_Range --
1445 -----------------
1447 function Build_Range (Def : Node_Id) return Node_Id is
1448 High : Node_Id := Type_High_Bound (Etype (Def));
1449 Low : Node_Id := Type_Low_Bound (Etype (Def));
1451 begin
1452 -- If a bound references a discriminant, generate an identifier
1453 -- with the same name. Resolution will map it to the formals of
1454 -- the init proc.
1456 if Is_Entity_Name (Low)
1457 and then Ekind (Entity (Low)) = E_Discriminant
1458 then
1459 Low :=
1460 Make_Selected_Component (Loc,
1461 Prefix => New_Copy_Tree (Obj_Ref),
1462 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1463 else
1464 Low := New_Copy_Tree (Low);
1465 end if;
1467 if Is_Entity_Name (High)
1468 and then Ekind (Entity (High)) = E_Discriminant
1469 then
1470 High :=
1471 Make_Selected_Component (Loc,
1472 Prefix => New_Copy_Tree (Obj_Ref),
1473 Selector_Name => Make_Identifier (Loc, Chars (High)));
1474 else
1475 High := New_Copy_Tree (High);
1476 end if;
1478 return
1479 Make_Range (Loc,
1480 Low_Bound => Low,
1481 High_Bound => High);
1482 end Build_Range;
1484 ---------------------------
1485 -- Create_Index_And_Data --
1486 ---------------------------
1488 procedure Create_Index_And_Data is
1489 begin
1490 if No (Index) and then No (Data) then
1491 declare
1492 Count : RE_Id;
1493 Data_Typ : RE_Id;
1494 Size : Entity_Id;
1496 begin
1497 if Is_Protected_Type (Typ) then
1498 Count := RO_PE_Number_Of_Entries;
1499 Data_Typ := RE_Protected_Entry_Names_Array;
1500 else
1501 Count := RO_ST_Number_Of_Entries;
1502 Data_Typ := RE_Task_Entry_Names_Array;
1503 end if;
1505 -- Step 1: Generate the declaration of the index variable:
1507 -- Index : Entry_Index := 1;
1509 Index := Make_Temporary (Loc, 'I');
1511 Append_To (Stmts,
1512 Make_Object_Declaration (Loc,
1513 Defining_Identifier => Index,
1514 Object_Definition =>
1515 New_Reference_To (RTE (RE_Entry_Index), Loc),
1516 Expression => Make_Integer_Literal (Loc, 1)));
1518 -- Step 2: Generate the declaration of an array to house all
1519 -- names:
1521 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1522 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1524 Size := Make_Temporary (Loc, 'S');
1526 Append_To (Stmts,
1527 Make_Object_Declaration (Loc,
1528 Defining_Identifier => Size,
1529 Constant_Present => True,
1530 Object_Definition =>
1531 New_Reference_To (RTE (RE_Entry_Index), Loc),
1532 Expression =>
1533 Make_Function_Call (Loc,
1534 Name =>
1535 New_Reference_To (RTE (Count), Loc),
1536 Parameter_Associations =>
1537 New_List (Object_Reference))));
1539 Data := Make_Temporary (Loc, 'A');
1541 Append_To (Stmts,
1542 Make_Object_Declaration (Loc,
1543 Defining_Identifier => Data,
1544 Aliased_Present => True,
1545 Object_Definition =>
1546 New_Reference_To (RTE (Data_Typ), Loc),
1547 Expression =>
1548 Make_Aggregate (Loc,
1549 Component_Associations => New_List (
1550 Make_Component_Association (Loc,
1551 Choices => New_List (
1552 Make_Range (Loc,
1553 Low_Bound => Make_Integer_Literal (Loc, 1),
1554 High_Bound => New_Reference_To (Size, Loc))),
1555 Expression => Make_Null (Loc))))));
1556 end;
1557 end if;
1558 end Create_Index_And_Data;
1560 ---------------------
1561 -- Increment_Index --
1562 ---------------------
1564 function Increment_Index return Node_Id is
1565 begin
1566 return
1567 Make_Assignment_Statement (Loc,
1568 Name => New_Reference_To (Index, Loc),
1569 Expression =>
1570 Make_Op_Add (Loc,
1571 Left_Opnd => New_Reference_To (Index, Loc),
1572 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1573 end Increment_Index;
1575 ----------------------
1576 -- Name_Declaration --
1577 ----------------------
1579 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1580 begin
1581 return
1582 Make_Object_Declaration (Loc,
1583 Defining_Identifier => Def_Id,
1584 Aliased_Present => True,
1585 Constant_Present => True,
1586 Object_Definition => New_Reference_To (Standard_String, Loc),
1587 Expression =>
1588 Make_String_Literal (Loc, String_From_Name_Buffer));
1589 end Name_Declaration;
1591 --------------------
1592 -- Set_Entry_Name --
1593 --------------------
1595 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1596 begin
1597 return
1598 Make_Assignment_Statement (Loc,
1599 Name =>
1600 Make_Indexed_Component (Loc,
1601 Prefix => New_Reference_To (Data, Loc),
1602 Expressions => New_List (New_Reference_To (Index, Loc))),
1604 Expression =>
1605 Make_Attribute_Reference (Loc,
1606 Prefix => New_Reference_To (Def_Id, Loc),
1607 Attribute_Name => Name_Unchecked_Access));
1608 end Set_Entry_Name;
1610 -- Local variables
1612 Temp_Id : Entity_Id;
1613 Subt_Def : Node_Id;
1615 -- Start of processing for Build_Entry_Name
1617 begin
1618 if Ekind (Comp_Id) = E_Entry_Family then
1619 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1621 Create_Index_And_Data;
1623 -- Step 1: Create the string name of the entry family.
1624 -- Generate:
1625 -- Temp : aliased constant String := "name ()";
1627 Temp_Id := Make_Temporary (Loc, 'S');
1628 Get_Name_String (Chars (Comp_Id));
1629 Add_Char_To_Name_Buffer (' ');
1630 Add_Char_To_Name_Buffer ('(');
1631 Add_Char_To_Name_Buffer (')');
1633 Append_To (Stmts, Name_Declaration (Temp_Id));
1635 -- Generate:
1636 -- for Member in Family_Low .. Family_High loop
1637 -- Set_Entry_Name (...);
1638 -- Index := Index + 1;
1639 -- end loop;
1641 Append_To (Stmts,
1642 Make_Loop_Statement (Loc,
1643 Iteration_Scheme =>
1644 Make_Iteration_Scheme (Loc,
1645 Loop_Parameter_Specification =>
1646 Make_Loop_Parameter_Specification (Loc,
1647 Defining_Identifier =>
1648 Make_Temporary (Loc, 'L'),
1649 Discrete_Subtype_Definition =>
1650 Build_Range (Subt_Def))),
1652 Statements => New_List (
1653 Set_Entry_Name (Temp_Id),
1654 Increment_Index),
1655 End_Label => Empty));
1657 -- Entry
1659 else
1660 Create_Index_And_Data;
1662 -- Step 1: Create the string name of the entry. Generate:
1663 -- Temp : aliased constant String := "name";
1665 Temp_Id := Make_Temporary (Loc, 'S');
1666 Get_Name_String (Chars (Comp_Id));
1668 Append_To (Stmts, Name_Declaration (Temp_Id));
1670 -- Step 2: Associate the string name with the underlying data
1671 -- structure.
1673 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1674 Append_To (Stmts, Increment_Index);
1675 end if;
1676 end Build_Entry_Name;
1678 ----------------------
1679 -- Object_Reference --
1680 ----------------------
1682 function Object_Reference return Node_Id is
1683 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1684 Field : Name_Id;
1685 Ref : Node_Id;
1687 begin
1688 if Is_Protected_Type (Typ) then
1689 Field := Name_uObject;
1690 else
1691 Field := Name_uTask_Id;
1692 end if;
1694 Ref :=
1695 Make_Selected_Component (Loc,
1696 Prefix =>
1697 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1698 Selector_Name => Make_Identifier (Loc, Field));
1700 if Is_Protected_Type (Typ) then
1701 Ref :=
1702 Make_Attribute_Reference (Loc,
1703 Prefix => Ref,
1704 Attribute_Name => Name_Unchecked_Access);
1705 end if;
1707 return Ref;
1708 end Object_Reference;
1710 -- Local variables
1712 Comp : Node_Id;
1713 Proc : RE_Id;
1715 -- Start of processing for Build_Entry_Names
1717 begin
1718 -- Retrieve the original concurrent type
1720 if Is_Concurrent_Record_Type (Typ) then
1721 Typ := Corresponding_Concurrent_Type (Typ);
1722 end if;
1724 pragma Assert (Is_Concurrent_Type (Typ));
1726 -- Nothing to do if the type has no entries
1728 if not Has_Entries (Typ) then
1729 return;
1730 end if;
1732 -- Avoid generating entry names for a protected type with only one entry
1734 if Is_Protected_Type (Typ)
1735 and then Find_Protection_Type (Base_Type (Typ)) /=
1736 RTE (RE_Protection_Entries)
1737 then
1738 return;
1739 end if;
1741 -- Step 1: Populate the array with statically generated strings denoting
1742 -- entries and entry family names.
1744 Comp := First_Entity (Typ);
1745 while Present (Comp) loop
1746 if Comes_From_Source (Comp)
1747 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
1748 then
1749 Build_Entry_Name (Comp);
1750 end if;
1752 Next_Entity (Comp);
1753 end loop;
1755 -- Step 2: Associate the array with the related concurrent object:
1757 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
1759 if Present (Data) then
1760 if Is_Protected_Type (Typ) then
1761 Proc := RO_PE_Set_Entry_Names;
1762 else
1763 Proc := RO_ST_Set_Entry_Names;
1764 end if;
1766 Append_To (Stmts,
1767 Make_Procedure_Call_Statement (Loc,
1768 Name => New_Reference_To (RTE (Proc), Loc),
1769 Parameter_Associations => New_List (
1770 Object_Reference,
1771 Make_Attribute_Reference (Loc,
1772 Prefix => New_Reference_To (Data, Loc),
1773 Attribute_Name => Name_Unchecked_Access))));
1774 end if;
1775 end Build_Entry_Names;
1777 ---------------------------
1778 -- Build_Parameter_Block --
1779 ---------------------------
1781 function Build_Parameter_Block
1782 (Loc : Source_Ptr;
1783 Actuals : List_Id;
1784 Formals : List_Id;
1785 Decls : List_Id) return Entity_Id
1787 Actual : Entity_Id;
1788 Comp_Nam : Node_Id;
1789 Comps : List_Id;
1790 Formal : Entity_Id;
1791 Has_Comp : Boolean := False;
1792 Rec_Nam : Node_Id;
1794 begin
1795 Actual := First (Actuals);
1796 Comps := New_List;
1797 Formal := Defining_Identifier (First (Formals));
1799 while Present (Actual) loop
1800 if not Is_Controlling_Actual (Actual) then
1802 -- Generate:
1803 -- type Ann is access all <actual-type>
1805 Comp_Nam := Make_Temporary (Loc, 'A');
1807 Append_To (Decls,
1808 Make_Full_Type_Declaration (Loc,
1809 Defining_Identifier => Comp_Nam,
1810 Type_Definition =>
1811 Make_Access_To_Object_Definition (Loc,
1812 All_Present => True,
1813 Constant_Present => Ekind (Formal) = E_In_Parameter,
1814 Subtype_Indication =>
1815 New_Reference_To (Etype (Actual), Loc))));
1817 -- Generate:
1818 -- Param : Ann;
1820 Append_To (Comps,
1821 Make_Component_Declaration (Loc,
1822 Defining_Identifier =>
1823 Make_Defining_Identifier (Loc, Chars (Formal)),
1824 Component_Definition =>
1825 Make_Component_Definition (Loc,
1826 Aliased_Present =>
1827 False,
1828 Subtype_Indication =>
1829 New_Reference_To (Comp_Nam, Loc))));
1831 Has_Comp := True;
1832 end if;
1834 Next_Actual (Actual);
1835 Next_Formal_With_Extras (Formal);
1836 end loop;
1838 Rec_Nam := Make_Temporary (Loc, 'P');
1840 if Has_Comp then
1842 -- Generate:
1843 -- type Pnn is record
1844 -- Param1 : Ann1;
1845 -- ...
1846 -- ParamN : AnnN;
1848 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1849 -- the original parameter names and Ann1 .. AnnN are the access to
1850 -- actual types.
1852 Append_To (Decls,
1853 Make_Full_Type_Declaration (Loc,
1854 Defining_Identifier =>
1855 Rec_Nam,
1856 Type_Definition =>
1857 Make_Record_Definition (Loc,
1858 Component_List =>
1859 Make_Component_List (Loc, Comps))));
1860 else
1861 -- Generate:
1862 -- type Pnn is null record;
1864 Append_To (Decls,
1865 Make_Full_Type_Declaration (Loc,
1866 Defining_Identifier =>
1867 Rec_Nam,
1868 Type_Definition =>
1869 Make_Record_Definition (Loc,
1870 Null_Present => True,
1871 Component_List => Empty)));
1872 end if;
1874 return Rec_Nam;
1875 end Build_Parameter_Block;
1877 --------------------------------------
1878 -- Build_Renamed_Formal_Declaration --
1879 --------------------------------------
1881 function Build_Renamed_Formal_Declaration
1882 (New_F : Entity_Id;
1883 Formal : Entity_Id;
1884 Comp : Entity_Id;
1885 Renamed_Formal : Node_Id) return Node_Id
1887 Loc : constant Source_Ptr := Sloc (New_F);
1888 Decl : Node_Id;
1890 begin
1891 -- If the formal is a tagged incomplete type, it is already passed
1892 -- by reference, so it is sufficient to rename the pointer component
1893 -- that corresponds to the actual. Otherwise we need to dereference
1894 -- the pointer component to obtain the actual.
1896 if Is_Incomplete_Type (Etype (Formal))
1897 and then Is_Tagged_Type (Etype (Formal))
1898 then
1899 Decl :=
1900 Make_Object_Renaming_Declaration (Loc,
1901 Defining_Identifier => New_F,
1902 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1903 Name => Renamed_Formal);
1905 else
1906 Decl :=
1907 Make_Object_Renaming_Declaration (Loc,
1908 Defining_Identifier => New_F,
1909 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1910 Name =>
1911 Make_Explicit_Dereference (Loc, Renamed_Formal));
1912 end if;
1914 return Decl;
1915 end Build_Renamed_Formal_Declaration;
1917 -----------------------
1918 -- Build_PPC_Wrapper --
1919 -----------------------
1921 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1922 Loc : constant Source_Ptr := Sloc (E);
1923 Synch_Type : constant Entity_Id := Scope (E);
1925 Wrapper_Id : constant Entity_Id :=
1926 Make_Defining_Identifier (Loc,
1927 Chars => New_External_Name (Chars (E), 'E'));
1928 -- the wrapper procedure name
1930 Wrapper_Body : Node_Id;
1932 Synch_Id : constant Entity_Id :=
1933 Make_Defining_Identifier (Loc,
1934 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1935 -- The parameter that designates the synchronized object in the call
1937 Actuals : constant List_Id := New_List;
1938 -- The actuals in the entry call
1940 Decls : constant List_Id := New_List;
1942 Entry_Call : Node_Id;
1943 Entry_Name : Node_Id;
1945 Specs : List_Id;
1946 -- The specification of the wrapper procedure
1948 begin
1950 -- Only build the wrapper if entry has pre/postconditions.
1951 -- Should this be done unconditionally instead ???
1953 declare
1954 P : Node_Id;
1956 begin
1957 P := Pre_Post_Conditions (Contract (E));
1959 if No (P) then
1960 return;
1961 end if;
1963 -- Transfer ppc pragmas to the declarations of the wrapper
1965 while Present (P) loop
1966 if Nam_In (Pragma_Name (P), Name_Precondition,
1967 Name_Postcondition)
1968 then
1969 Append (Relocate_Node (P), Decls);
1970 Set_Analyzed (Last (Decls), False);
1971 end if;
1973 P := Next_Pragma (P);
1974 end loop;
1975 end;
1977 -- First formal is synchronized object
1979 Specs := New_List (
1980 Make_Parameter_Specification (Loc,
1981 Defining_Identifier => Synch_Id,
1982 Out_Present => True,
1983 In_Present => True,
1984 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1986 Entry_Name :=
1987 Make_Selected_Component (Loc,
1988 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1989 Selector_Name => New_Occurrence_Of (E, Loc));
1991 -- If entity is entry family, second formal is the corresponding index,
1992 -- and entry name is an indexed component.
1994 if Ekind (E) = E_Entry_Family then
1995 declare
1996 Index : constant Entity_Id :=
1997 Make_Defining_Identifier (Loc, Name_I);
1998 begin
1999 Append_To (Specs,
2000 Make_Parameter_Specification (Loc,
2001 Defining_Identifier => Index,
2002 Parameter_Type =>
2003 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
2005 Entry_Name :=
2006 Make_Indexed_Component (Loc,
2007 Prefix => Entry_Name,
2008 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
2009 end;
2010 end if;
2012 Entry_Call :=
2013 Make_Procedure_Call_Statement (Loc,
2014 Name => Entry_Name,
2015 Parameter_Associations => Actuals);
2017 -- Now add formals that match those of the entry, and build actuals for
2018 -- the nested entry call.
2020 declare
2021 Form : Entity_Id;
2022 New_Form : Entity_Id;
2023 Parm_Spec : Node_Id;
2025 begin
2026 Form := First_Formal (E);
2027 while Present (Form) loop
2028 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
2029 Parm_Spec :=
2030 Make_Parameter_Specification (Loc,
2031 Defining_Identifier => New_Form,
2032 Out_Present => Out_Present (Parent (Form)),
2033 In_Present => In_Present (Parent (Form)),
2034 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
2036 Append (Parm_Spec, Specs);
2037 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
2038 Next_Formal (Form);
2039 end loop;
2040 end;
2042 -- Add renaming declarations for the discriminants of the enclosing
2043 -- type, which may be visible in the preconditions.
2045 if Has_Discriminants (Synch_Type) then
2046 declare
2047 D : Entity_Id;
2048 Decl : Node_Id;
2050 begin
2051 D := First_Discriminant (Synch_Type);
2052 while Present (D) loop
2053 Decl :=
2054 Make_Object_Renaming_Declaration (Loc,
2055 Defining_Identifier =>
2056 Make_Defining_Identifier (Loc, Chars (D)),
2057 Subtype_Mark => New_Reference_To (Etype (D), Loc),
2058 Name =>
2059 Make_Selected_Component (Loc,
2060 Prefix => New_Reference_To (Synch_Id, Loc),
2061 Selector_Name => Make_Identifier (Loc, Chars (D))));
2062 Prepend (Decl, Decls);
2063 Next_Discriminant (D);
2064 end loop;
2065 end;
2066 end if;
2068 Set_PPC_Wrapper (E, Wrapper_Id);
2069 Wrapper_Body :=
2070 Make_Subprogram_Body (Loc,
2071 Specification =>
2072 Make_Procedure_Specification (Loc,
2073 Defining_Unit_Name => Wrapper_Id,
2074 Parameter_Specifications => Specs),
2075 Declarations => Decls,
2076 Handled_Statement_Sequence =>
2077 Make_Handled_Sequence_Of_Statements (Loc,
2078 Statements => New_List (Entry_Call)));
2080 -- The wrapper body is analyzed when the enclosing type is frozen
2082 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
2083 end Build_PPC_Wrapper;
2085 --------------------------
2086 -- Build_Wrapper_Bodies --
2087 --------------------------
2089 procedure Build_Wrapper_Bodies
2090 (Loc : Source_Ptr;
2091 Typ : Entity_Id;
2092 N : Node_Id)
2094 Rec_Typ : Entity_Id;
2096 function Build_Wrapper_Body
2097 (Loc : Source_Ptr;
2098 Subp_Id : Entity_Id;
2099 Obj_Typ : Entity_Id;
2100 Formals : List_Id) return Node_Id;
2101 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2102 -- associated with a protected or task type. Subp_Id is the subprogram
2103 -- name which will be wrapped. Obj_Typ is the type of the new formal
2104 -- parameter which handles dispatching and object notation. Formals are
2105 -- the original formals of Subp_Id which will be explicitly replicated.
2107 ------------------------
2108 -- Build_Wrapper_Body --
2109 ------------------------
2111 function Build_Wrapper_Body
2112 (Loc : Source_Ptr;
2113 Subp_Id : Entity_Id;
2114 Obj_Typ : Entity_Id;
2115 Formals : List_Id) return Node_Id
2117 Body_Spec : Node_Id;
2119 begin
2120 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2122 -- The subprogram is not overriding or is not a primitive declared
2123 -- between two views.
2125 if No (Body_Spec) then
2126 return Empty;
2127 end if;
2129 declare
2130 Actuals : List_Id := No_List;
2131 Conv_Id : Node_Id;
2132 First_Form : Node_Id;
2133 Formal : Node_Id;
2134 Nam : Node_Id;
2136 begin
2137 -- Map formals to actuals. Use the list built for the wrapper
2138 -- spec, skipping the object notation parameter.
2140 First_Form := First (Parameter_Specifications (Body_Spec));
2142 Formal := First_Form;
2143 Next (Formal);
2145 if Present (Formal) then
2146 Actuals := New_List;
2147 while Present (Formal) loop
2148 Append_To (Actuals,
2149 Make_Identifier (Loc,
2150 Chars => Chars (Defining_Identifier (Formal))));
2151 Next (Formal);
2152 end loop;
2153 end if;
2155 -- Special processing for primitives declared between a private
2156 -- type and its completion: the wrapper needs a properly typed
2157 -- parameter if the wrapped operation has a controlling first
2158 -- parameter. Note that this might not be the case for a function
2159 -- with a controlling result.
2161 if Is_Private_Primitive_Subprogram (Subp_Id) then
2162 if No (Actuals) then
2163 Actuals := New_List;
2164 end if;
2166 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2167 Prepend_To (Actuals,
2168 Unchecked_Convert_To
2169 (Corresponding_Concurrent_Type (Obj_Typ),
2170 Make_Identifier (Loc, Name_uO)));
2172 else
2173 Prepend_To (Actuals,
2174 Make_Identifier (Loc,
2175 Chars => Chars (Defining_Identifier (First_Form))));
2176 end if;
2178 Nam := New_Reference_To (Subp_Id, Loc);
2179 else
2180 -- An access-to-variable object parameter requires an explicit
2181 -- dereference in the unchecked conversion. This case occurs
2182 -- when a protected entry wrapper must override an interface
2183 -- level procedure with interface access as first parameter.
2185 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2187 if Nkind (Parameter_Type (First_Form)) =
2188 N_Access_Definition
2189 then
2190 Conv_Id :=
2191 Make_Explicit_Dereference (Loc,
2192 Prefix => Make_Identifier (Loc, Name_uO));
2193 else
2194 Conv_Id := Make_Identifier (Loc, Name_uO);
2195 end if;
2197 Nam :=
2198 Make_Selected_Component (Loc,
2199 Prefix =>
2200 Unchecked_Convert_To
2201 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2202 Selector_Name => New_Reference_To (Subp_Id, Loc));
2203 end if;
2205 -- Create the subprogram body. For a function, the call to the
2206 -- actual subprogram has to be converted to the corresponding
2207 -- record if it is a controlling result.
2209 if Ekind (Subp_Id) = E_Function then
2210 declare
2211 Res : Node_Id;
2213 begin
2214 Res :=
2215 Make_Function_Call (Loc,
2216 Name => Nam,
2217 Parameter_Associations => Actuals);
2219 if Has_Controlling_Result (Subp_Id) then
2220 Res :=
2221 Unchecked_Convert_To
2222 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2223 end if;
2225 return
2226 Make_Subprogram_Body (Loc,
2227 Specification => Body_Spec,
2228 Declarations => Empty_List,
2229 Handled_Statement_Sequence =>
2230 Make_Handled_Sequence_Of_Statements (Loc,
2231 Statements => New_List (
2232 Make_Simple_Return_Statement (Loc, Res))));
2233 end;
2235 else
2236 return
2237 Make_Subprogram_Body (Loc,
2238 Specification => Body_Spec,
2239 Declarations => Empty_List,
2240 Handled_Statement_Sequence =>
2241 Make_Handled_Sequence_Of_Statements (Loc,
2242 Statements => New_List (
2243 Make_Procedure_Call_Statement (Loc,
2244 Name => Nam,
2245 Parameter_Associations => Actuals))));
2246 end if;
2247 end;
2248 end Build_Wrapper_Body;
2250 -- Start of processing for Build_Wrapper_Bodies
2252 begin
2253 if Is_Concurrent_Type (Typ) then
2254 Rec_Typ := Corresponding_Record_Type (Typ);
2255 else
2256 Rec_Typ := Typ;
2257 end if;
2259 -- Generate wrapper bodies for a concurrent type which implements an
2260 -- interface.
2262 if Present (Interfaces (Rec_Typ)) then
2263 declare
2264 Insert_Nod : Node_Id;
2265 Prim : Entity_Id;
2266 Prim_Elmt : Elmt_Id;
2267 Prim_Decl : Node_Id;
2268 Subp : Entity_Id;
2269 Wrap_Body : Node_Id;
2270 Wrap_Id : Entity_Id;
2272 begin
2273 Insert_Nod := N;
2275 -- Examine all primitive operations of the corresponding record
2276 -- type, looking for wrapper specs. Generate bodies in order to
2277 -- complete them.
2279 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2280 while Present (Prim_Elmt) loop
2281 Prim := Node (Prim_Elmt);
2283 if (Ekind (Prim) = E_Function
2284 or else Ekind (Prim) = E_Procedure)
2285 and then Is_Primitive_Wrapper (Prim)
2286 then
2287 Subp := Wrapped_Entity (Prim);
2288 Prim_Decl := Parent (Parent (Prim));
2290 Wrap_Body :=
2291 Build_Wrapper_Body (Loc,
2292 Subp_Id => Subp,
2293 Obj_Typ => Rec_Typ,
2294 Formals => Parameter_Specifications (Parent (Subp)));
2295 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2297 Set_Corresponding_Spec (Wrap_Body, Prim);
2298 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2300 Insert_After (Insert_Nod, Wrap_Body);
2301 Insert_Nod := Wrap_Body;
2303 Analyze (Wrap_Body);
2304 end if;
2306 Next_Elmt (Prim_Elmt);
2307 end loop;
2308 end;
2309 end if;
2310 end Build_Wrapper_Bodies;
2312 ------------------------
2313 -- Build_Wrapper_Spec --
2314 ------------------------
2316 function Build_Wrapper_Spec
2317 (Subp_Id : Entity_Id;
2318 Obj_Typ : Entity_Id;
2319 Formals : List_Id) return Node_Id
2321 Loc : constant Source_Ptr := Sloc (Subp_Id);
2322 First_Param : Node_Id;
2323 Iface : Entity_Id;
2324 Iface_Elmt : Elmt_Id;
2325 Iface_Op : Entity_Id;
2326 Iface_Op_Elmt : Elmt_Id;
2328 function Overriding_Possible
2329 (Iface_Op : Entity_Id;
2330 Wrapper : Entity_Id) return Boolean;
2331 -- Determine whether a primitive operation can be overridden by Wrapper.
2332 -- Iface_Op is the candidate primitive operation of an interface type,
2333 -- Wrapper is the generated entry wrapper.
2335 function Replicate_Formals
2336 (Loc : Source_Ptr;
2337 Formals : List_Id) return List_Id;
2338 -- An explicit parameter replication is required due to the Is_Entry_
2339 -- Formal flag being set for all the formals of an entry. The explicit
2340 -- replication removes the flag that would otherwise cause a different
2341 -- path of analysis.
2343 -------------------------
2344 -- Overriding_Possible --
2345 -------------------------
2347 function Overriding_Possible
2348 (Iface_Op : Entity_Id;
2349 Wrapper : Entity_Id) return Boolean
2351 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2352 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2354 function Type_Conformant_Parameters
2355 (Iface_Op_Params : List_Id;
2356 Wrapper_Params : List_Id) return Boolean;
2357 -- Determine whether the parameters of the generated entry wrapper
2358 -- and those of a primitive operation are type conformant. During
2359 -- this check, the first parameter of the primitive operation is
2360 -- skipped if it is a controlling argument: protected functions
2361 -- may have a controlling result.
2363 --------------------------------
2364 -- Type_Conformant_Parameters --
2365 --------------------------------
2367 function Type_Conformant_Parameters
2368 (Iface_Op_Params : List_Id;
2369 Wrapper_Params : List_Id) return Boolean
2371 Iface_Op_Param : Node_Id;
2372 Iface_Op_Typ : Entity_Id;
2373 Wrapper_Param : Node_Id;
2374 Wrapper_Typ : Entity_Id;
2376 begin
2377 -- Skip the first (controlling) parameter of primitive operation
2379 Iface_Op_Param := First (Iface_Op_Params);
2381 if Present (First_Formal (Iface_Op))
2382 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2383 then
2384 Iface_Op_Param := Next (Iface_Op_Param);
2385 end if;
2387 Wrapper_Param := First (Wrapper_Params);
2388 while Present (Iface_Op_Param)
2389 and then Present (Wrapper_Param)
2390 loop
2391 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2392 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2394 -- The two parameters must be mode conformant
2396 if not Conforming_Types
2397 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2398 then
2399 return False;
2400 end if;
2402 Next (Iface_Op_Param);
2403 Next (Wrapper_Param);
2404 end loop;
2406 -- One of the lists is longer than the other
2408 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2409 return False;
2410 end if;
2412 return True;
2413 end Type_Conformant_Parameters;
2415 -- Start of processing for Overriding_Possible
2417 begin
2418 if Chars (Iface_Op) /= Chars (Wrapper) then
2419 return False;
2420 end if;
2422 -- If an inherited subprogram is implemented by a protected procedure
2423 -- or an entry, then the first parameter of the inherited subprogram
2424 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2426 if Ekind (Iface_Op) = E_Procedure
2427 and then Present (Parameter_Specifications (Iface_Op_Spec))
2428 then
2429 declare
2430 Obj_Param : constant Node_Id :=
2431 First (Parameter_Specifications (Iface_Op_Spec));
2432 begin
2433 if not Out_Present (Obj_Param)
2434 and then Nkind (Parameter_Type (Obj_Param)) /=
2435 N_Access_Definition
2436 then
2437 return False;
2438 end if;
2439 end;
2440 end if;
2442 return
2443 Type_Conformant_Parameters (
2444 Parameter_Specifications (Iface_Op_Spec),
2445 Parameter_Specifications (Wrapper_Spec));
2446 end Overriding_Possible;
2448 -----------------------
2449 -- Replicate_Formals --
2450 -----------------------
2452 function Replicate_Formals
2453 (Loc : Source_Ptr;
2454 Formals : List_Id) return List_Id
2456 New_Formals : constant List_Id := New_List;
2457 Formal : Node_Id;
2458 Param_Type : Node_Id;
2460 begin
2461 Formal := First (Formals);
2463 -- Skip the object parameter when dealing with primitives declared
2464 -- between two views.
2466 if Is_Private_Primitive_Subprogram (Subp_Id)
2467 and then not Has_Controlling_Result (Subp_Id)
2468 then
2469 Formal := Next (Formal);
2470 end if;
2472 while Present (Formal) loop
2474 -- Create an explicit copy of the entry parameter
2476 -- When creating the wrapper subprogram for a primitive operation
2477 -- of a protected interface we must construct an equivalent
2478 -- signature to that of the overriding operation. For regular
2479 -- parameters we can just use the type of the formal, but for
2480 -- access to subprogram parameters we need to reanalyze the
2481 -- parameter type to create local entities for the signature of
2482 -- the subprogram type. Using the entities of the overriding
2483 -- subprogram will result in out-of-scope errors in the back-end.
2485 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2486 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2487 else
2488 Param_Type :=
2489 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2490 end if;
2492 Append_To (New_Formals,
2493 Make_Parameter_Specification (Loc,
2494 Defining_Identifier =>
2495 Make_Defining_Identifier (Loc,
2496 Chars => Chars (Defining_Identifier (Formal))),
2497 In_Present => In_Present (Formal),
2498 Out_Present => Out_Present (Formal),
2499 Parameter_Type => Param_Type));
2501 Next (Formal);
2502 end loop;
2504 return New_Formals;
2505 end Replicate_Formals;
2507 -- Start of processing for Build_Wrapper_Spec
2509 begin
2510 -- There is no point in building wrappers for non-tagged concurrent
2511 -- types.
2513 pragma Assert (Is_Tagged_Type (Obj_Typ));
2515 -- An entry or a protected procedure can override a routine where the
2516 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2517 -- type. Since the wrapper must have the exact same signature as that of
2518 -- the overridden subprogram, we try to find the overriding candidate
2519 -- and use its controlling formal.
2521 First_Param := Empty;
2523 -- Check every implemented interface
2525 if Present (Interfaces (Obj_Typ)) then
2526 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2527 Search : while Present (Iface_Elmt) loop
2528 Iface := Node (Iface_Elmt);
2530 -- Check every interface primitive
2532 if Present (Primitive_Operations (Iface)) then
2533 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2534 while Present (Iface_Op_Elmt) loop
2535 Iface_Op := Node (Iface_Op_Elmt);
2537 -- Ignore predefined primitives
2539 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2540 Iface_Op := Ultimate_Alias (Iface_Op);
2542 -- The current primitive operation can be overridden by
2543 -- the generated entry wrapper.
2545 if Overriding_Possible (Iface_Op, Subp_Id) then
2546 First_Param :=
2547 First (Parameter_Specifications (Parent (Iface_Op)));
2549 exit Search;
2550 end if;
2551 end if;
2553 Next_Elmt (Iface_Op_Elmt);
2554 end loop;
2555 end if;
2557 Next_Elmt (Iface_Elmt);
2558 end loop Search;
2559 end if;
2561 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2562 -- this subprogram and this is not a primitive declared between two
2563 -- views then force the generation of a wrapper. As an optimization,
2564 -- previous versions of the frontend avoid generating the wrapper;
2565 -- however, the wrapper facilitates locating and reporting an error
2566 -- when a duplicate declaration is found later. See example in
2567 -- AI05-0090-1.
2569 if No (First_Param)
2570 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2571 then
2572 if Is_Task_Type
2573 (Corresponding_Concurrent_Type (Obj_Typ))
2574 then
2575 First_Param :=
2576 Make_Parameter_Specification (Loc,
2577 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2578 In_Present => True,
2579 Out_Present => False,
2580 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2582 -- For entries and procedures of protected types the mode of
2583 -- the controlling argument must be in-out.
2585 else
2586 First_Param :=
2587 Make_Parameter_Specification (Loc,
2588 Defining_Identifier =>
2589 Make_Defining_Identifier (Loc,
2590 Chars => Name_uO),
2591 In_Present => True,
2592 Out_Present => (Ekind (Subp_Id) /= E_Function),
2593 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2594 end if;
2595 end if;
2597 declare
2598 Wrapper_Id : constant Entity_Id :=
2599 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2600 New_Formals : List_Id;
2601 Obj_Param : Node_Id;
2602 Obj_Param_Typ : Entity_Id;
2604 begin
2605 -- Minimum decoration is needed to catch the entity in
2606 -- Sem_Ch6.Override_Dispatching_Operation.
2608 if Ekind (Subp_Id) = E_Function then
2609 Set_Ekind (Wrapper_Id, E_Function);
2610 else
2611 Set_Ekind (Wrapper_Id, E_Procedure);
2612 end if;
2614 Set_Is_Primitive_Wrapper (Wrapper_Id);
2615 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2616 Set_Is_Private_Primitive (Wrapper_Id,
2617 Is_Private_Primitive_Subprogram (Subp_Id));
2619 -- Process the formals
2621 New_Formals := Replicate_Formals (Loc, Formals);
2623 -- A function with a controlling result and no first controlling
2624 -- formal needs no additional parameter.
2626 if Has_Controlling_Result (Subp_Id)
2627 and then
2628 (No (First_Formal (Subp_Id))
2629 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2630 then
2631 null;
2633 -- Routine Subp_Id has been found to override an interface primitive.
2634 -- If the interface operation has an access parameter, create a copy
2635 -- of it, with the same null exclusion indicator if present.
2637 elsif Present (First_Param) then
2638 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2639 Obj_Param_Typ :=
2640 Make_Access_Definition (Loc,
2641 Subtype_Mark =>
2642 New_Reference_To (Obj_Typ, Loc));
2643 Set_Null_Exclusion_Present (Obj_Param_Typ,
2644 Null_Exclusion_Present (Parameter_Type (First_Param)));
2646 else
2647 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2648 end if;
2650 Obj_Param :=
2651 Make_Parameter_Specification (Loc,
2652 Defining_Identifier =>
2653 Make_Defining_Identifier (Loc,
2654 Chars => Name_uO),
2655 In_Present => In_Present (First_Param),
2656 Out_Present => Out_Present (First_Param),
2657 Parameter_Type => Obj_Param_Typ);
2659 Prepend_To (New_Formals, Obj_Param);
2661 -- If we are dealing with a primitive declared between two views,
2662 -- implemented by a synchronized operation, we need to create
2663 -- a default parameter. The mode of the parameter must match that
2664 -- of the primitive operation.
2666 else
2667 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2668 Obj_Param :=
2669 Make_Parameter_Specification (Loc,
2670 Defining_Identifier =>
2671 Make_Defining_Identifier (Loc, Name_uO),
2672 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2673 Out_Present => Ekind (Subp_Id) /= E_Function,
2674 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2675 Prepend_To (New_Formals, Obj_Param);
2676 end if;
2678 -- Build the final spec. If it is a function with a controlling
2679 -- result, it is a primitive operation of the corresponding
2680 -- record type, so mark the spec accordingly.
2682 if Ekind (Subp_Id) = E_Function then
2683 declare
2684 Res_Def : Node_Id;
2686 begin
2687 if Has_Controlling_Result (Subp_Id) then
2688 Res_Def :=
2689 New_Occurrence_Of
2690 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2691 else
2692 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2693 end if;
2695 return
2696 Make_Function_Specification (Loc,
2697 Defining_Unit_Name => Wrapper_Id,
2698 Parameter_Specifications => New_Formals,
2699 Result_Definition => Res_Def);
2700 end;
2701 else
2702 return
2703 Make_Procedure_Specification (Loc,
2704 Defining_Unit_Name => Wrapper_Id,
2705 Parameter_Specifications => New_Formals);
2706 end if;
2707 end;
2708 end Build_Wrapper_Spec;
2710 -------------------------
2711 -- Build_Wrapper_Specs --
2712 -------------------------
2714 procedure Build_Wrapper_Specs
2715 (Loc : Source_Ptr;
2716 Typ : Entity_Id;
2717 N : in out Node_Id)
2719 Def : Node_Id;
2720 Rec_Typ : Entity_Id;
2721 procedure Scan_Declarations (L : List_Id);
2722 -- Common processing for visible and private declarations
2723 -- of a protected type.
2725 procedure Scan_Declarations (L : List_Id) is
2726 Decl : Node_Id;
2727 Wrap_Decl : Node_Id;
2728 Wrap_Spec : Node_Id;
2730 begin
2731 if No (L) then
2732 return;
2733 end if;
2735 Decl := First (L);
2736 while Present (Decl) loop
2737 Wrap_Spec := Empty;
2739 if Nkind (Decl) = N_Entry_Declaration
2740 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2741 then
2742 Wrap_Spec :=
2743 Build_Wrapper_Spec
2744 (Subp_Id => Defining_Identifier (Decl),
2745 Obj_Typ => Rec_Typ,
2746 Formals => Parameter_Specifications (Decl));
2748 elsif Nkind (Decl) = N_Subprogram_Declaration then
2749 Wrap_Spec :=
2750 Build_Wrapper_Spec
2751 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2752 Obj_Typ => Rec_Typ,
2753 Formals =>
2754 Parameter_Specifications (Specification (Decl)));
2755 end if;
2757 if Present (Wrap_Spec) then
2758 Wrap_Decl :=
2759 Make_Subprogram_Declaration (Loc,
2760 Specification => Wrap_Spec);
2762 Insert_After (N, Wrap_Decl);
2763 N := Wrap_Decl;
2765 Analyze (Wrap_Decl);
2766 end if;
2768 Next (Decl);
2769 end loop;
2770 end Scan_Declarations;
2772 -- start of processing for Build_Wrapper_Specs
2774 begin
2775 if Is_Protected_Type (Typ) then
2776 Def := Protected_Definition (Parent (Typ));
2777 else pragma Assert (Is_Task_Type (Typ));
2778 Def := Task_Definition (Parent (Typ));
2779 end if;
2781 Rec_Typ := Corresponding_Record_Type (Typ);
2783 -- Generate wrapper specs for a concurrent type which implements an
2784 -- interface. Operations in both the visible and private parts may
2785 -- implement progenitor operations.
2787 if Present (Interfaces (Rec_Typ))
2788 and then Present (Def)
2789 then
2790 Scan_Declarations (Visible_Declarations (Def));
2791 Scan_Declarations (Private_Declarations (Def));
2792 end if;
2793 end Build_Wrapper_Specs;
2795 ---------------------------
2796 -- Build_Find_Body_Index --
2797 ---------------------------
2799 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2800 Loc : constant Source_Ptr := Sloc (Typ);
2801 Ent : Entity_Id;
2802 E_Typ : Entity_Id;
2803 Has_F : Boolean := False;
2804 Index : Nat;
2805 If_St : Node_Id := Empty;
2806 Lo : Node_Id;
2807 Hi : Node_Id;
2808 Decls : List_Id := New_List;
2809 Ret : Node_Id;
2810 Spec : Node_Id;
2811 Siz : Node_Id := Empty;
2813 procedure Add_If_Clause (Expr : Node_Id);
2814 -- Add test for range of current entry
2816 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2817 -- If a bound of an entry is given by a discriminant, retrieve the
2818 -- actual value of the discriminant from the enclosing object.
2820 -------------------
2821 -- Add_If_Clause --
2822 -------------------
2824 procedure Add_If_Clause (Expr : Node_Id) is
2825 Cond : Node_Id;
2826 Stats : constant List_Id :=
2827 New_List (
2828 Make_Simple_Return_Statement (Loc,
2829 Expression => Make_Integer_Literal (Loc, Index + 1)));
2831 begin
2832 -- Index for current entry body
2834 Index := Index + 1;
2836 -- Compute total length of entry queues so far
2838 if No (Siz) then
2839 Siz := Expr;
2840 else
2841 Siz :=
2842 Make_Op_Add (Loc,
2843 Left_Opnd => Siz,
2844 Right_Opnd => Expr);
2845 end if;
2847 Cond :=
2848 Make_Op_Le (Loc,
2849 Left_Opnd => Make_Identifier (Loc, Name_uE),
2850 Right_Opnd => Siz);
2852 -- Map entry queue indexes in the range of the current family
2853 -- into the current index, that designates the entry body.
2855 if No (If_St) then
2856 If_St :=
2857 Make_Implicit_If_Statement (Typ,
2858 Condition => Cond,
2859 Then_Statements => Stats,
2860 Elsif_Parts => New_List);
2861 Ret := If_St;
2863 else
2864 Append_To (Elsif_Parts (If_St),
2865 Make_Elsif_Part (Loc,
2866 Condition => Cond,
2867 Then_Statements => Stats));
2868 end if;
2869 end Add_If_Clause;
2871 ------------------------------
2872 -- Convert_Discriminant_Ref --
2873 ------------------------------
2875 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2876 B : Node_Id;
2878 begin
2879 if Is_Entity_Name (Bound)
2880 and then Ekind (Entity (Bound)) = E_Discriminant
2881 then
2882 B :=
2883 Make_Selected_Component (Loc,
2884 Prefix =>
2885 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2886 Make_Explicit_Dereference (Loc,
2887 Make_Identifier (Loc, Name_uObject))),
2888 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2889 Set_Etype (B, Etype (Entity (Bound)));
2890 else
2891 B := New_Copy_Tree (Bound);
2892 end if;
2894 return B;
2895 end Convert_Discriminant_Ref;
2897 -- Start of processing for Build_Find_Body_Index
2899 begin
2900 Spec := Build_Find_Body_Index_Spec (Typ);
2902 Ent := First_Entity (Typ);
2903 while Present (Ent) loop
2904 if Ekind (Ent) = E_Entry_Family then
2905 Has_F := True;
2906 exit;
2907 end if;
2909 Next_Entity (Ent);
2910 end loop;
2912 if not Has_F then
2914 -- If the protected type has no entry families, there is a one-one
2915 -- correspondence between entry queue and entry body.
2917 Ret :=
2918 Make_Simple_Return_Statement (Loc,
2919 Expression => Make_Identifier (Loc, Name_uE));
2921 else
2922 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2923 -- the following:
2925 -- if E <= l1 then return 1;
2926 -- elsif E <= l1 + l2 then return 2;
2927 -- ...
2929 Index := 0;
2930 Siz := Empty;
2931 Ent := First_Entity (Typ);
2933 Add_Object_Pointer (Loc, Typ, Decls);
2935 while Present (Ent) loop
2936 if Ekind (Ent) = E_Entry then
2937 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2939 elsif Ekind (Ent) = E_Entry_Family then
2940 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2941 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2942 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2943 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2944 end if;
2946 Next_Entity (Ent);
2947 end loop;
2949 if Index = 1 then
2950 Decls := New_List;
2951 Ret :=
2952 Make_Simple_Return_Statement (Loc,
2953 Expression => Make_Integer_Literal (Loc, 1));
2955 elsif Nkind (Ret) = N_If_Statement then
2957 -- Ranges are in increasing order, so last one doesn't need guard
2959 declare
2960 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2961 begin
2962 Remove (Nod);
2963 Set_Else_Statements (Ret, Then_Statements (Nod));
2964 end;
2965 end if;
2966 end if;
2968 return
2969 Make_Subprogram_Body (Loc,
2970 Specification => Spec,
2971 Declarations => Decls,
2972 Handled_Statement_Sequence =>
2973 Make_Handled_Sequence_Of_Statements (Loc,
2974 Statements => New_List (Ret)));
2975 end Build_Find_Body_Index;
2977 --------------------------------
2978 -- Build_Find_Body_Index_Spec --
2979 --------------------------------
2981 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2982 Loc : constant Source_Ptr := Sloc (Typ);
2983 Id : constant Entity_Id :=
2984 Make_Defining_Identifier (Loc,
2985 Chars => New_External_Name (Chars (Typ), 'F'));
2986 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2987 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2989 begin
2990 return
2991 Make_Function_Specification (Loc,
2992 Defining_Unit_Name => Id,
2993 Parameter_Specifications => New_List (
2994 Make_Parameter_Specification (Loc,
2995 Defining_Identifier => Parm1,
2996 Parameter_Type =>
2997 New_Reference_To (RTE (RE_Address), Loc)),
2999 Make_Parameter_Specification (Loc,
3000 Defining_Identifier => Parm2,
3001 Parameter_Type =>
3002 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
3004 Result_Definition => New_Occurrence_Of (
3005 RTE (RE_Protected_Entry_Index), Loc));
3006 end Build_Find_Body_Index_Spec;
3008 -----------------------------------------------
3009 -- Build_Lock_Free_Protected_Subprogram_Body --
3010 -----------------------------------------------
3012 function Build_Lock_Free_Protected_Subprogram_Body
3013 (N : Node_Id;
3014 Prot_Typ : Node_Id;
3015 Unprot_Spec : Node_Id) return Node_Id
3017 Actuals : constant List_Id := New_List;
3018 Loc : constant Source_Ptr := Sloc (N);
3019 Spec : constant Node_Id := Specification (N);
3020 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3021 Formal : Node_Id;
3022 Prot_Spec : Node_Id;
3023 Stmt : Node_Id;
3025 begin
3026 -- Create the protected version of the body
3028 Prot_Spec :=
3029 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3031 -- Build the actual parameters which appear in the call to the
3032 -- unprotected version of the body.
3034 Formal := First (Parameter_Specifications (Prot_Spec));
3035 while Present (Formal) loop
3036 Append_To (Actuals,
3037 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3039 Next (Formal);
3040 end loop;
3042 -- Function case, generate:
3043 -- return <Unprot_Func_Call>;
3045 if Nkind (Spec) = N_Function_Specification then
3046 Stmt :=
3047 Make_Simple_Return_Statement (Loc,
3048 Expression =>
3049 Make_Function_Call (Loc,
3050 Name =>
3051 Make_Identifier (Loc, Chars (Unprot_Id)),
3052 Parameter_Associations => Actuals));
3054 -- Procedure case, call the unprotected version
3056 else
3057 Stmt :=
3058 Make_Procedure_Call_Statement (Loc,
3059 Name =>
3060 Make_Identifier (Loc, Chars (Unprot_Id)),
3061 Parameter_Associations => Actuals);
3062 end if;
3064 return
3065 Make_Subprogram_Body (Loc,
3066 Declarations => Empty_List,
3067 Specification => Prot_Spec,
3068 Handled_Statement_Sequence =>
3069 Make_Handled_Sequence_Of_Statements (Loc,
3070 Statements => New_List (Stmt)));
3071 end Build_Lock_Free_Protected_Subprogram_Body;
3073 -------------------------------------------------
3074 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3075 -------------------------------------------------
3077 -- Procedures which meet the lock-free implementation requirements and
3078 -- reference a unique scalar component Comp are expanded in the following
3079 -- manner:
3081 -- procedure P (...) is
3082 -- Expected_Comp : constant Comp_Type :=
3083 -- Comp_Type
3084 -- (System.Atomic_Primitives.Lock_Free_Read_N
3085 -- (_Object.Comp'Address));
3086 -- begin
3087 -- loop
3088 -- declare
3089 -- <original declarations before the object renaming declaration
3090 -- of Comp>
3092 -- Desired_Comp : Comp_Type := Expected_Comp;
3093 -- Comp : Comp_Type renames Desired_Comp;
3095 -- <original delarations after the object renaming declaration
3096 -- of Comp>
3098 -- begin
3099 -- <original statements>
3100 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3101 -- (_Object.Comp'Address,
3102 -- Interfaces.Unsigned_N (Expected_Comp),
3103 -- Interfaces.Unsigned_N (Desired_Comp));
3104 -- end;
3105 -- end loop;
3106 -- end P;
3108 -- Each return and raise statement of P is transformed into an atomic
3109 -- status check:
3111 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3112 -- (_Object.Comp'Address,
3113 -- Interfaces.Unsigned_N (Expected_Comp),
3114 -- Interfaces.Unsigned_N (Desired_Comp));
3115 -- then
3116 -- <original statement>
3117 -- else
3118 -- goto L0;
3119 -- end if;
3121 -- Functions which meet the lock-free implementation requirements and
3122 -- reference a unique scalar component Comp are expanded in the following
3123 -- manner:
3125 -- function F (...) return ... is
3126 -- <original declarations before the object renaming declaration
3127 -- of Comp>
3129 -- Expected_Comp : constant Comp_Type :=
3130 -- Comp_Type
3131 -- (System.Atomic_Primitives.Lock_Free_Read_N
3132 -- (_Object.Comp'Address));
3133 -- Comp : Comp_Type renames Expected_Comp;
3135 -- <original delarations after the object renaming declaration of
3136 -- Comp>
3138 -- begin
3139 -- <original statements>
3140 -- end F;
3142 function Build_Lock_Free_Unprotected_Subprogram_Body
3143 (N : Node_Id;
3144 Prot_Typ : Node_Id) return Node_Id
3146 function Referenced_Component (N : Node_Id) return Entity_Id;
3147 -- Subprograms which meet the lock-free implementation criteria are
3148 -- allowed to reference only one unique component. Return the prival
3149 -- of the said component.
3151 --------------------------
3152 -- Referenced_Component --
3153 --------------------------
3155 function Referenced_Component (N : Node_Id) return Entity_Id is
3156 Comp : Entity_Id;
3157 Decl : Node_Id;
3158 Source_Comp : Entity_Id := Empty;
3160 begin
3161 -- Find the unique source component which N references in its
3162 -- statements.
3164 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3165 declare
3166 Element : Lock_Free_Subprogram renames
3167 Lock_Free_Subprogram_Table.Table (Index);
3168 begin
3169 if Element.Sub_Body = N then
3170 Source_Comp := Element.Comp_Id;
3171 exit;
3172 end if;
3173 end;
3174 end loop;
3176 if No (Source_Comp) then
3177 return Empty;
3178 end if;
3180 -- Find the prival which corresponds to the source component within
3181 -- the declarations of N.
3183 Decl := First (Declarations (N));
3184 while Present (Decl) loop
3186 -- Privals appear as object renamings
3188 if Nkind (Decl) = N_Object_Renaming_Declaration then
3189 Comp := Defining_Identifier (Decl);
3191 if Present (Prival_Link (Comp))
3192 and then Prival_Link (Comp) = Source_Comp
3193 then
3194 return Comp;
3195 end if;
3196 end if;
3198 Next (Decl);
3199 end loop;
3201 return Empty;
3202 end Referenced_Component;
3204 -- Local variables
3206 Comp : constant Entity_Id := Referenced_Component (N);
3207 Loc : constant Source_Ptr := Sloc (N);
3208 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3209 Decls : List_Id := Declarations (N);
3211 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3213 begin
3214 -- Add renamings for the protection object, discriminals, privals and
3215 -- the entry index constant for use by debugger.
3217 Debug_Private_Data_Declarations (Decls);
3219 -- Perform the lock-free expansion when the subprogram references a
3220 -- protected component.
3222 if Present (Comp) then
3223 Protected_Component_Ref : declare
3224 Comp_Decl : constant Node_Id := Parent (Comp);
3225 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3226 Comp_Type : constant Entity_Id := Etype (Comp);
3228 Is_Procedure : constant Boolean :=
3229 Ekind (Corresponding_Spec (N)) = E_Procedure;
3230 -- Indicates if N is a protected procedure body
3232 Block_Decls : List_Id;
3233 Try_Write : Entity_Id;
3234 Desired_Comp : Entity_Id;
3235 Decl : Node_Id;
3236 Label : Node_Id;
3237 Label_Id : Entity_Id := Empty;
3238 Read : Entity_Id;
3239 Expected_Comp : Entity_Id;
3240 Stmt : Node_Id;
3241 Stmts : List_Id :=
3242 New_Copy_List (Statements (Hand_Stmt_Seq));
3243 Typ_Size : Int;
3244 Unsigned : Entity_Id;
3246 function Process_Node (N : Node_Id) return Traverse_Result;
3247 -- Transform a single node if it is a return statement, a raise
3248 -- statement or a reference to Comp.
3250 procedure Process_Stmts (Stmts : List_Id);
3251 -- Given a statement sequence Stmts, wrap any return or raise
3252 -- statements in the following manner:
3254 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3255 -- (_Object.Comp'Address,
3256 -- Interfaces.Unsigned_N (Expected_Comp),
3257 -- Interfaces.Unsigned_N (Desired_Comp))
3258 -- then
3259 -- <Stmt>;
3260 -- else
3261 -- goto L0;
3262 -- end if;
3264 ------------------
3265 -- Process_Node --
3266 ------------------
3268 function Process_Node (N : Node_Id) return Traverse_Result is
3270 procedure Wrap_Statement (Stmt : Node_Id);
3271 -- Wrap an arbitrary statement inside an if statement where the
3272 -- condition does an atomic check on the state of the object.
3274 --------------------
3275 -- Wrap_Statement --
3276 --------------------
3278 procedure Wrap_Statement (Stmt : Node_Id) is
3279 begin
3280 -- The first time through, create the declaration of a label
3281 -- which is used to skip the remainder of source statements
3282 -- if the state of the object has changed.
3284 if No (Label_Id) then
3285 Label_Id :=
3286 Make_Identifier (Loc, New_External_Name ('L', 0));
3287 Set_Entity (Label_Id,
3288 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3289 end if;
3291 -- Generate:
3292 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3293 -- (_Object.Comp'Address,
3294 -- Interfaces.Unsigned_N (Expected_Comp),
3295 -- Interfaces.Unsigned_N (Desired_Comp))
3296 -- then
3297 -- <Stmt>;
3298 -- else
3299 -- goto L0;
3300 -- end if;
3302 Rewrite (Stmt,
3303 Make_Implicit_If_Statement (N,
3304 Condition =>
3305 Make_Function_Call (Loc,
3306 Name =>
3307 New_Reference_To (Try_Write, Loc),
3308 Parameter_Associations => New_List (
3309 Make_Attribute_Reference (Loc,
3310 Prefix => Relocate_Node (Comp_Sel_Nam),
3311 Attribute_Name => Name_Address),
3313 Unchecked_Convert_To (Unsigned,
3314 New_Reference_To (Expected_Comp, Loc)),
3316 Unchecked_Convert_To (Unsigned,
3317 New_Reference_To (Desired_Comp, Loc)))),
3319 Then_Statements => New_List (Relocate_Node (Stmt)),
3321 Else_Statements => New_List (
3322 Make_Goto_Statement (Loc,
3323 Name =>
3324 New_Reference_To (Entity (Label_Id), Loc)))));
3325 end Wrap_Statement;
3327 -- Start of processing for Process_Node
3329 begin
3330 -- Wrap each return and raise statement that appear inside a
3331 -- procedure. Skip the last return statement which is added by
3332 -- default since it is transformed into an exit statement.
3334 if Is_Procedure
3335 and then ((Nkind (N) = N_Simple_Return_Statement
3336 and then N /= Last (Stmts))
3337 or else Nkind (N) = N_Extended_Return_Statement
3338 or else (Nkind_In (N, N_Raise_Constraint_Error,
3339 N_Raise_Program_Error,
3340 N_Raise_Statement,
3341 N_Raise_Storage_Error)
3342 and then Comes_From_Source (N)))
3343 then
3344 Wrap_Statement (N);
3345 return Skip;
3346 end if;
3348 -- Force reanalysis
3350 Set_Analyzed (N, False);
3352 return OK;
3353 end Process_Node;
3355 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3357 -------------------
3358 -- Process_Stmts --
3359 -------------------
3361 procedure Process_Stmts (Stmts : List_Id) is
3362 Stmt : Node_Id;
3363 begin
3364 Stmt := First (Stmts);
3365 while Present (Stmt) loop
3366 Process_Nodes (Stmt);
3367 Next (Stmt);
3368 end loop;
3369 end Process_Stmts;
3371 -- Start of processing for Protected_Component_Ref
3373 begin
3374 -- Get the type size
3376 if Known_Static_Esize (Comp_Type) then
3377 Typ_Size := UI_To_Int (Esize (Comp_Type));
3379 -- If the Esize (Object_Size) is unknown at compile time, look at
3380 -- the RM_Size (Value_Size) since it may have been set by an
3381 -- explicit representation clause.
3383 elsif Known_Static_RM_Size (Comp_Type) then
3384 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3386 -- Should not happen since this has already been checked in
3387 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3389 else
3390 raise Program_Error;
3391 end if;
3393 -- Retrieve all relevant atomic routines and types
3395 case Typ_Size is
3396 when 8 =>
3397 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3398 Read := RTE (RE_Lock_Free_Read_8);
3399 Unsigned := RTE (RE_Uint8);
3401 when 16 =>
3402 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3403 Read := RTE (RE_Lock_Free_Read_16);
3404 Unsigned := RTE (RE_Uint16);
3406 when 32 =>
3407 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3408 Read := RTE (RE_Lock_Free_Read_32);
3409 Unsigned := RTE (RE_Uint32);
3411 when 64 =>
3412 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3413 Read := RTE (RE_Lock_Free_Read_64);
3414 Unsigned := RTE (RE_Uint64);
3416 when others =>
3417 raise Program_Error;
3418 end case;
3420 -- Generate:
3421 -- Expected_Comp : constant Comp_Type :=
3422 -- Comp_Type
3423 -- (System.Atomic_Primitives.Lock_Free_Read_N
3424 -- (_Object.Comp'Address));
3426 Expected_Comp :=
3427 Make_Defining_Identifier (Loc,
3428 New_External_Name (Chars (Comp), Suffix => "_saved"));
3430 Decl :=
3431 Make_Object_Declaration (Loc,
3432 Defining_Identifier => Expected_Comp,
3433 Object_Definition => New_Reference_To (Comp_Type, Loc),
3434 Constant_Present => True,
3435 Expression =>
3436 Unchecked_Convert_To (Comp_Type,
3437 Make_Function_Call (Loc,
3438 Name => New_Reference_To (Read, Loc),
3439 Parameter_Associations => New_List (
3440 Make_Attribute_Reference (Loc,
3441 Prefix => Relocate_Node (Comp_Sel_Nam),
3442 Attribute_Name => Name_Address)))));
3444 -- Protected procedures
3446 if Is_Procedure then
3447 -- Move the original declarations inside the generated block
3449 Block_Decls := Decls;
3451 -- Reset the declarations list of the protected procedure to
3452 -- contain only Decl.
3454 Decls := New_List (Decl);
3456 -- Generate:
3457 -- Desired_Comp : Comp_Type := Expected_Comp;
3459 Desired_Comp :=
3460 Make_Defining_Identifier (Loc,
3461 New_External_Name (Chars (Comp), Suffix => "_current"));
3463 -- Insert the declarations of Expected_Comp and Desired_Comp in
3464 -- the block declarations right before the renaming of the
3465 -- protected component.
3467 Insert_Before (Comp_Decl,
3468 Make_Object_Declaration (Loc,
3469 Defining_Identifier => Desired_Comp,
3470 Object_Definition => New_Reference_To (Comp_Type, Loc),
3471 Expression =>
3472 New_Reference_To (Expected_Comp, Loc)));
3474 -- Protected function
3476 else
3477 Desired_Comp := Expected_Comp;
3479 -- Insert the declaration of Expected_Comp in the function
3480 -- declarations right before the renaming of the protected
3481 -- component.
3483 Insert_Before (Comp_Decl, Decl);
3484 end if;
3486 -- Rewrite the protected component renaming declaration to be a
3487 -- renaming of Desired_Comp.
3489 -- Generate:
3490 -- Comp : Comp_Type renames Desired_Comp;
3492 Rewrite (Comp_Decl,
3493 Make_Object_Renaming_Declaration (Loc,
3494 Defining_Identifier =>
3495 Defining_Identifier (Comp_Decl),
3496 Subtype_Mark =>
3497 New_Occurrence_Of (Comp_Type, Loc),
3498 Name =>
3499 New_Reference_To (Desired_Comp, Loc)));
3501 -- Wrap any return or raise statements in Stmts in same the manner
3502 -- described in Process_Stmts.
3504 Process_Stmts (Stmts);
3506 -- Generate:
3507 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3508 -- (_Object.Comp'Address,
3509 -- Interfaces.Unsigned_N (Expected_Comp),
3510 -- Interfaces.Unsigned_N (Desired_Comp))
3512 if Is_Procedure then
3513 Stmt :=
3514 Make_Exit_Statement (Loc,
3515 Condition =>
3516 Make_Function_Call (Loc,
3517 Name =>
3518 New_Reference_To (Try_Write, Loc),
3519 Parameter_Associations => New_List (
3520 Make_Attribute_Reference (Loc,
3521 Prefix => Relocate_Node (Comp_Sel_Nam),
3522 Attribute_Name => Name_Address),
3524 Unchecked_Convert_To (Unsigned,
3525 New_Reference_To (Expected_Comp, Loc)),
3527 Unchecked_Convert_To (Unsigned,
3528 New_Reference_To (Desired_Comp, Loc)))));
3530 -- Small optimization: transform the default return statement
3531 -- of a procedure into the atomic exit statement.
3533 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3534 Rewrite (Last (Stmts), Stmt);
3535 else
3536 Append_To (Stmts, Stmt);
3537 end if;
3538 end if;
3540 -- Create the declaration of the label used to skip the rest of
3541 -- the source statements when the object state changes.
3543 if Present (Label_Id) then
3544 Label := Make_Label (Loc, Label_Id);
3545 Append_To (Decls,
3546 Make_Implicit_Label_Declaration (Loc,
3547 Defining_Identifier => Entity (Label_Id),
3548 Label_Construct => Label));
3549 Append_To (Stmts, Label);
3550 end if;
3552 -- Generate:
3553 -- loop
3554 -- declare
3555 -- <Decls>
3556 -- begin
3557 -- <Stmts>
3558 -- end;
3559 -- end loop;
3561 if Is_Procedure then
3562 Stmts :=
3563 New_List (
3564 Make_Loop_Statement (Loc,
3565 Statements => New_List (
3566 Make_Block_Statement (Loc,
3567 Declarations => Block_Decls,
3568 Handled_Statement_Sequence =>
3569 Make_Handled_Sequence_Of_Statements (Loc,
3570 Statements => Stmts))),
3571 End_Label => Empty));
3572 end if;
3574 Hand_Stmt_Seq :=
3575 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3576 end Protected_Component_Ref;
3577 end if;
3579 -- Make an unprotected version of the subprogram for use within the same
3580 -- object, with new name and extra parameter representing the object.
3582 return
3583 Make_Subprogram_Body (Loc,
3584 Specification =>
3585 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3586 Declarations => Decls,
3587 Handled_Statement_Sequence => Hand_Stmt_Seq);
3588 end Build_Lock_Free_Unprotected_Subprogram_Body;
3590 -------------------------
3591 -- Build_Master_Entity --
3592 -------------------------
3594 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3595 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3596 Context : Node_Id;
3597 Context_Id : Entity_Id;
3598 Decl : Node_Id;
3599 Decls : List_Id;
3600 Par : Node_Id;
3602 begin
3603 if Is_Itype (Obj_Or_Typ) then
3604 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3605 else
3606 Par := Parent (Obj_Or_Typ);
3607 end if;
3609 -- When creating a master for a record component which is either a task
3610 -- or access-to-task, the enclosing record is the master scope and the
3611 -- proper insertion point is the component list.
3613 if Is_Record_Type (Current_Scope) then
3614 Context := Par;
3615 Context_Id := Current_Scope;
3616 Decls := List_Containing (Context);
3618 -- Default case for object declarations and access types. Note that the
3619 -- context is updated to the nearest enclosing body, block, package or
3620 -- return statement.
3622 else
3623 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3624 end if;
3626 -- Do not create a master if one already exists or there is no task
3627 -- hierarchy.
3629 if Has_Master_Entity (Context_Id)
3630 or else Restriction_Active (No_Task_Hierarchy)
3631 then
3632 return;
3633 end if;
3635 -- Create a master, generate:
3636 -- _Master : constant Master_Id := Current_Master.all;
3638 Decl :=
3639 Make_Object_Declaration (Loc,
3640 Defining_Identifier =>
3641 Make_Defining_Identifier (Loc, Name_uMaster),
3642 Constant_Present => True,
3643 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
3644 Expression =>
3645 Make_Explicit_Dereference (Loc,
3646 New_Reference_To (RTE (RE_Current_Master), Loc)));
3648 -- The master is inserted at the start of the declarative list of the
3649 -- context.
3651 Prepend_To (Decls, Decl);
3653 -- In certain cases where transient scopes are involved, the immediate
3654 -- scope is not always the proper master scope. Ensure that the master
3655 -- declaration and entity appear in the same context.
3657 if Context_Id /= Current_Scope then
3658 Push_Scope (Context_Id);
3659 Analyze (Decl);
3660 Pop_Scope;
3661 else
3662 Analyze (Decl);
3663 end if;
3665 -- Mark the enclosing scope and its associated construct as being task
3666 -- masters.
3668 Set_Has_Master_Entity (Context_Id);
3670 while Present (Context)
3671 and then Nkind (Context) /= N_Compilation_Unit
3672 loop
3673 if Nkind_In (Context, N_Block_Statement,
3674 N_Subprogram_Body,
3675 N_Task_Body)
3676 then
3677 Set_Is_Task_Master (Context);
3678 exit;
3680 elsif Nkind (Parent (Context)) = N_Subunit then
3681 Context := Corresponding_Stub (Parent (Context));
3682 end if;
3684 Context := Parent (Context);
3685 end loop;
3686 end Build_Master_Entity;
3688 ---------------------------
3689 -- Build_Master_Renaming --
3690 ---------------------------
3692 procedure Build_Master_Renaming
3693 (Ptr_Typ : Entity_Id;
3694 Ins_Nod : Node_Id := Empty)
3696 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3697 Context : Node_Id;
3698 Master_Decl : Node_Id;
3699 Master_Id : Entity_Id;
3701 begin
3702 -- Nothing to do if there is no task hierarchy
3704 if Restriction_Active (No_Task_Hierarchy) then
3705 return;
3706 end if;
3708 -- Determine the proper context to insert the master renaming
3710 if Present (Ins_Nod) then
3711 Context := Ins_Nod;
3712 elsif Is_Itype (Ptr_Typ) then
3713 Context := Associated_Node_For_Itype (Ptr_Typ);
3714 else
3715 Context := Parent (Ptr_Typ);
3716 end if;
3718 -- Generate:
3719 -- <Ptr_Typ>M : Master_Id renames _Master;
3721 Master_Id :=
3722 Make_Defining_Identifier (Loc,
3723 New_External_Name (Chars (Ptr_Typ), 'M'));
3725 Master_Decl :=
3726 Make_Object_Renaming_Declaration (Loc,
3727 Defining_Identifier => Master_Id,
3728 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
3729 Name => Make_Identifier (Loc, Name_uMaster));
3731 Insert_Action (Context, Master_Decl);
3733 -- The renamed master now services the access type
3735 Set_Master_Id (Ptr_Typ, Master_Id);
3736 end Build_Master_Renaming;
3738 -----------------------------------------
3739 -- Build_Private_Protected_Declaration --
3740 -----------------------------------------
3742 function Build_Private_Protected_Declaration
3743 (N : Node_Id) return Entity_Id
3745 Loc : constant Source_Ptr := Sloc (N);
3746 Body_Id : constant Entity_Id := Defining_Entity (N);
3747 Decl : Node_Id;
3748 Plist : List_Id;
3749 Formal : Entity_Id;
3750 New_Spec : Node_Id;
3751 Spec_Id : Entity_Id;
3753 begin
3754 Formal := First_Formal (Body_Id);
3756 -- The protected operation always has at least one formal, namely the
3757 -- object itself, but it is only placed in the parameter list if
3758 -- expansion is enabled.
3760 if Present (Formal) or else Expander_Active then
3761 Plist := Copy_Parameter_List (Body_Id);
3762 else
3763 Plist := No_List;
3764 end if;
3766 if Nkind (Specification (N)) = N_Procedure_Specification then
3767 New_Spec :=
3768 Make_Procedure_Specification (Loc,
3769 Defining_Unit_Name =>
3770 Make_Defining_Identifier (Sloc (Body_Id),
3771 Chars => Chars (Body_Id)),
3772 Parameter_Specifications =>
3773 Plist);
3774 else
3775 New_Spec :=
3776 Make_Function_Specification (Loc,
3777 Defining_Unit_Name =>
3778 Make_Defining_Identifier (Sloc (Body_Id),
3779 Chars => Chars (Body_Id)),
3780 Parameter_Specifications => Plist,
3781 Result_Definition =>
3782 New_Occurrence_Of (Etype (Body_Id), Loc));
3783 end if;
3785 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3786 Insert_Before (N, Decl);
3787 Spec_Id := Defining_Unit_Name (New_Spec);
3789 -- Indicate that the entity comes from source, to ensure that cross-
3790 -- reference information is properly generated. The body itself is
3791 -- rewritten during expansion, and the body entity will not appear in
3792 -- calls to the operation.
3794 Set_Comes_From_Source (Spec_Id, True);
3795 Analyze (Decl);
3796 Set_Has_Completion (Spec_Id);
3797 Set_Convention (Spec_Id, Convention_Protected);
3798 return Spec_Id;
3799 end Build_Private_Protected_Declaration;
3801 ---------------------------
3802 -- Build_Protected_Entry --
3803 ---------------------------
3805 function Build_Protected_Entry
3806 (N : Node_Id;
3807 Ent : Entity_Id;
3808 Pid : Node_Id) return Node_Id
3810 Loc : constant Source_Ptr := Sloc (N);
3812 Decls : constant List_Id := Declarations (N);
3813 End_Lab : constant Node_Id :=
3814 End_Label (Handled_Statement_Sequence (N));
3815 End_Loc : constant Source_Ptr :=
3816 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3817 -- Used for the generated call to Complete_Entry_Body
3819 Han_Loc : Source_Ptr;
3820 -- Used for the exception handler, inserted at end of the body
3822 Op_Decls : constant List_Id := New_List;
3823 Complete : Node_Id;
3824 Edef : Entity_Id;
3825 Espec : Node_Id;
3826 Ohandle : Node_Id;
3827 Op_Stats : List_Id;
3829 begin
3830 -- Set the source location on the exception handler only when debugging
3831 -- the expanded code (see Make_Implicit_Exception_Handler).
3833 if Debug_Generated_Code then
3834 Han_Loc := End_Loc;
3836 -- Otherwise the inserted code should not be visible to the debugger
3838 else
3839 Han_Loc := No_Location;
3840 end if;
3842 Edef :=
3843 Make_Defining_Identifier (Loc,
3844 Chars => Chars (Protected_Body_Subprogram (Ent)));
3845 Espec :=
3846 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3848 -- Add the following declarations:
3849 -- type poVP is access poV;
3850 -- _object : poVP := poVP (_O);
3852 -- where _O is the formal parameter associated with the concurrent
3853 -- object. These declarations are needed for Complete_Entry_Body.
3855 Add_Object_Pointer (Loc, Pid, Op_Decls);
3857 -- Add renamings for all formals, the Protection object, discriminals,
3858 -- privals and the entry index constant for use by debugger.
3860 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3861 Debug_Private_Data_Declarations (Decls);
3863 case Corresponding_Runtime_Package (Pid) is
3864 when System_Tasking_Protected_Objects_Entries =>
3865 Complete :=
3866 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
3868 when System_Tasking_Protected_Objects_Single_Entry =>
3869 Complete :=
3870 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
3872 when others =>
3873 raise Program_Error;
3874 end case;
3876 Op_Stats := New_List (
3877 Make_Block_Statement (Loc,
3878 Declarations => Decls,
3879 Handled_Statement_Sequence =>
3880 Handled_Statement_Sequence (N)),
3882 Make_Procedure_Call_Statement (End_Loc,
3883 Name => Complete,
3884 Parameter_Associations => New_List (
3885 Make_Attribute_Reference (End_Loc,
3886 Prefix =>
3887 Make_Selected_Component (End_Loc,
3888 Prefix => Make_Identifier (End_Loc, Name_uObject),
3889 Selector_Name => Make_Identifier (End_Loc, Name_uObject)),
3890 Attribute_Name => Name_Unchecked_Access))));
3892 -- When exceptions can not be propagated, we never need to call
3893 -- Exception_Complete_Entry_Body
3895 if No_Exception_Handlers_Set then
3896 return
3897 Make_Subprogram_Body (Loc,
3898 Specification => Espec,
3899 Declarations => Op_Decls,
3900 Handled_Statement_Sequence =>
3901 Make_Handled_Sequence_Of_Statements (Loc,
3902 Statements => Op_Stats,
3903 End_Label => End_Lab));
3905 else
3906 Ohandle := Make_Others_Choice (Loc);
3907 Set_All_Others (Ohandle);
3909 case Corresponding_Runtime_Package (Pid) is
3910 when System_Tasking_Protected_Objects_Entries =>
3911 Complete :=
3912 New_Reference_To
3913 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3915 when System_Tasking_Protected_Objects_Single_Entry =>
3916 Complete :=
3917 New_Reference_To
3918 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3920 when others =>
3921 raise Program_Error;
3922 end case;
3924 -- Establish link between subprogram body entity and source entry
3926 Set_Corresponding_Protected_Entry (Edef, Ent);
3928 -- Create body of entry procedure. The renaming declarations are
3929 -- placed ahead of the block that contains the actual entry body.
3931 return
3932 Make_Subprogram_Body (Loc,
3933 Specification => Espec,
3934 Declarations => Op_Decls,
3935 Handled_Statement_Sequence =>
3936 Make_Handled_Sequence_Of_Statements (Loc,
3937 Statements => Op_Stats,
3938 End_Label => End_Lab,
3939 Exception_Handlers => New_List (
3940 Make_Implicit_Exception_Handler (Han_Loc,
3941 Exception_Choices => New_List (Ohandle),
3943 Statements => New_List (
3944 Make_Procedure_Call_Statement (Han_Loc,
3945 Name => Complete,
3946 Parameter_Associations => New_List (
3947 Make_Attribute_Reference (Han_Loc,
3948 Prefix =>
3949 Make_Selected_Component (Han_Loc,
3950 Prefix =>
3951 Make_Identifier (Han_Loc, Name_uObject),
3952 Selector_Name =>
3953 Make_Identifier (Han_Loc, Name_uObject)),
3954 Attribute_Name => Name_Unchecked_Access),
3956 Make_Function_Call (Han_Loc,
3957 Name => New_Reference_To (
3958 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3959 end if;
3960 end Build_Protected_Entry;
3962 -----------------------------------------
3963 -- Build_Protected_Entry_Specification --
3964 -----------------------------------------
3966 function Build_Protected_Entry_Specification
3967 (Loc : Source_Ptr;
3968 Def_Id : Entity_Id;
3969 Ent_Id : Entity_Id) return Node_Id
3971 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3973 begin
3974 Set_Debug_Info_Needed (Def_Id);
3976 if Present (Ent_Id) then
3977 Append_Elmt (P, Accept_Address (Ent_Id));
3978 end if;
3980 return
3981 Make_Procedure_Specification (Loc,
3982 Defining_Unit_Name => Def_Id,
3983 Parameter_Specifications => New_List (
3984 Make_Parameter_Specification (Loc,
3985 Defining_Identifier =>
3986 Make_Defining_Identifier (Loc, Name_uO),
3987 Parameter_Type =>
3988 New_Reference_To (RTE (RE_Address), Loc)),
3990 Make_Parameter_Specification (Loc,
3991 Defining_Identifier => P,
3992 Parameter_Type =>
3993 New_Reference_To (RTE (RE_Address), Loc)),
3995 Make_Parameter_Specification (Loc,
3996 Defining_Identifier =>
3997 Make_Defining_Identifier (Loc, Name_uE),
3998 Parameter_Type =>
3999 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
4000 end Build_Protected_Entry_Specification;
4002 --------------------------
4003 -- Build_Protected_Spec --
4004 --------------------------
4006 function Build_Protected_Spec
4007 (N : Node_Id;
4008 Obj_Type : Entity_Id;
4009 Ident : Entity_Id;
4010 Unprotected : Boolean := False) return List_Id
4012 Loc : constant Source_Ptr := Sloc (N);
4013 Decl : Node_Id;
4014 Formal : Entity_Id;
4015 New_Plist : List_Id;
4016 New_Param : Node_Id;
4018 begin
4019 New_Plist := New_List;
4021 Formal := First_Formal (Ident);
4022 while Present (Formal) loop
4023 New_Param :=
4024 Make_Parameter_Specification (Loc,
4025 Defining_Identifier =>
4026 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4027 In_Present => In_Present (Parent (Formal)),
4028 Out_Present => Out_Present (Parent (Formal)),
4029 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
4031 if Unprotected then
4032 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4033 end if;
4035 Append (New_Param, New_Plist);
4036 Next_Formal (Formal);
4037 end loop;
4039 -- If the subprogram is a procedure and the context is not an access
4040 -- to protected subprogram, the parameter is in-out. Otherwise it is
4041 -- an in parameter.
4043 Decl :=
4044 Make_Parameter_Specification (Loc,
4045 Defining_Identifier =>
4046 Make_Defining_Identifier (Loc, Name_uObject),
4047 In_Present => True,
4048 Out_Present =>
4049 (Etype (Ident) = Standard_Void_Type
4050 and then not Is_RTE (Obj_Type, RE_Address)),
4051 Parameter_Type =>
4052 New_Reference_To (Obj_Type, Loc));
4053 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4054 Prepend_To (New_Plist, Decl);
4056 return New_Plist;
4057 end Build_Protected_Spec;
4059 ---------------------------------------
4060 -- Build_Protected_Sub_Specification --
4061 ---------------------------------------
4063 function Build_Protected_Sub_Specification
4064 (N : Node_Id;
4065 Prot_Typ : Entity_Id;
4066 Mode : Subprogram_Protection_Mode) return Node_Id
4068 Loc : constant Source_Ptr := Sloc (N);
4069 Decl : Node_Id;
4070 Def_Id : Entity_Id;
4071 New_Id : Entity_Id;
4072 New_Plist : List_Id;
4073 New_Spec : Node_Id;
4075 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4076 (Dispatching_Mode => ' ',
4077 Protected_Mode => 'P',
4078 Unprotected_Mode => 'N');
4080 begin
4081 if Ekind (Defining_Unit_Name (Specification (N))) =
4082 E_Subprogram_Body
4083 then
4084 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4085 else
4086 Decl := N;
4087 end if;
4089 Def_Id := Defining_Unit_Name (Specification (Decl));
4091 New_Plist :=
4092 Build_Protected_Spec
4093 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4094 Mode = Unprotected_Mode);
4095 New_Id :=
4096 Make_Defining_Identifier (Loc,
4097 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4099 -- The unprotected operation carries the user code, and debugging
4100 -- information must be generated for it, even though this spec does
4101 -- not come from source. It is also convenient to allow gdb to step
4102 -- into the protected operation, even though it only contains lock/
4103 -- unlock calls.
4105 Set_Debug_Info_Needed (New_Id);
4107 -- If a pragma Eliminate applies to the source entity, the internal
4108 -- subprograms will be eliminated as well.
4110 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4112 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4113 New_Spec :=
4114 Make_Procedure_Specification (Loc,
4115 Defining_Unit_Name => New_Id,
4116 Parameter_Specifications => New_Plist);
4118 -- Create a new specification for the anonymous subprogram type
4120 else
4121 New_Spec :=
4122 Make_Function_Specification (Loc,
4123 Defining_Unit_Name => New_Id,
4124 Parameter_Specifications => New_Plist,
4125 Result_Definition =>
4126 Copy_Result_Type (Result_Definition (Specification (Decl))));
4128 Set_Return_Present (Defining_Unit_Name (New_Spec));
4129 end if;
4131 return New_Spec;
4132 end Build_Protected_Sub_Specification;
4134 -------------------------------------
4135 -- Build_Protected_Subprogram_Body --
4136 -------------------------------------
4138 function Build_Protected_Subprogram_Body
4139 (N : Node_Id;
4140 Pid : Node_Id;
4141 N_Op_Spec : Node_Id) return Node_Id
4143 Loc : constant Source_Ptr := Sloc (N);
4144 Op_Spec : Node_Id;
4145 P_Op_Spec : Node_Id;
4146 Uactuals : List_Id;
4147 Pformal : Node_Id;
4148 Unprot_Call : Node_Id;
4149 Sub_Body : Node_Id;
4150 Lock_Name : Node_Id;
4151 Lock_Stmt : Node_Id;
4152 Service_Name : Node_Id;
4153 R : Node_Id;
4154 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4155 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4156 Stmts : List_Id;
4157 Object_Parm : Node_Id;
4158 Exc_Safe : Boolean;
4159 Lock_Kind : RE_Id;
4161 begin
4162 Op_Spec := Specification (N);
4163 Exc_Safe := Is_Exception_Safe (N);
4165 P_Op_Spec :=
4166 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4168 -- Build a list of the formal parameters of the protected version of
4169 -- the subprogram to use as the actual parameters of the unprotected
4170 -- version.
4172 Uactuals := New_List;
4173 Pformal := First (Parameter_Specifications (P_Op_Spec));
4174 while Present (Pformal) loop
4175 Append_To (Uactuals,
4176 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4177 Next (Pformal);
4178 end loop;
4180 -- Make a call to the unprotected version of the subprogram built above
4181 -- for use by the protected version built below.
4183 if Nkind (Op_Spec) = N_Function_Specification then
4184 if Exc_Safe then
4185 R := Make_Temporary (Loc, 'R');
4186 Unprot_Call :=
4187 Make_Object_Declaration (Loc,
4188 Defining_Identifier => R,
4189 Constant_Present => True,
4190 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4191 Expression =>
4192 Make_Function_Call (Loc,
4193 Name => Make_Identifier (Loc,
4194 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4195 Parameter_Associations => Uactuals));
4197 Return_Stmt :=
4198 Make_Simple_Return_Statement (Loc,
4199 Expression => New_Reference_To (R, Loc));
4201 else
4202 Unprot_Call := Make_Simple_Return_Statement (Loc,
4203 Expression => Make_Function_Call (Loc,
4204 Name =>
4205 Make_Identifier (Loc,
4206 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4207 Parameter_Associations => Uactuals));
4208 end if;
4210 Lock_Kind := RE_Lock_Read_Only;
4212 else
4213 Unprot_Call :=
4214 Make_Procedure_Call_Statement (Loc,
4215 Name =>
4216 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4217 Parameter_Associations => Uactuals);
4219 Lock_Kind := RE_Lock;
4220 end if;
4222 -- Wrap call in block that will be covered by an at_end handler
4224 if not Exc_Safe then
4225 Unprot_Call := Make_Block_Statement (Loc,
4226 Handled_Statement_Sequence =>
4227 Make_Handled_Sequence_Of_Statements (Loc,
4228 Statements => New_List (Unprot_Call)));
4229 end if;
4231 -- Make the protected subprogram body. This locks the protected
4232 -- object and calls the unprotected version of the subprogram.
4234 case Corresponding_Runtime_Package (Pid) is
4235 when System_Tasking_Protected_Objects_Entries =>
4236 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
4237 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
4239 when System_Tasking_Protected_Objects_Single_Entry =>
4240 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
4241 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
4243 when System_Tasking_Protected_Objects =>
4244 Lock_Name := New_Reference_To (RTE (Lock_Kind), Loc);
4245 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
4247 when others =>
4248 raise Program_Error;
4249 end case;
4251 Object_Parm :=
4252 Make_Attribute_Reference (Loc,
4253 Prefix =>
4254 Make_Selected_Component (Loc,
4255 Prefix => Make_Identifier (Loc, Name_uObject),
4256 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4257 Attribute_Name => Name_Unchecked_Access);
4259 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4260 Name => Lock_Name,
4261 Parameter_Associations => New_List (Object_Parm));
4263 if Abort_Allowed then
4264 Stmts := New_List (
4265 Make_Procedure_Call_Statement (Loc,
4266 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4267 Parameter_Associations => Empty_List),
4268 Lock_Stmt);
4270 else
4271 Stmts := New_List (Lock_Stmt);
4272 end if;
4274 if not Exc_Safe then
4275 Append (Unprot_Call, Stmts);
4276 else
4277 if Nkind (Op_Spec) = N_Function_Specification then
4278 Pre_Stmts := Stmts;
4279 Stmts := Empty_List;
4280 else
4281 Append (Unprot_Call, Stmts);
4282 end if;
4284 Append (
4285 Make_Procedure_Call_Statement (Loc,
4286 Name => Service_Name,
4287 Parameter_Associations =>
4288 New_List (New_Copy_Tree (Object_Parm))),
4289 Stmts);
4291 if Abort_Allowed then
4292 Append (
4293 Make_Procedure_Call_Statement (Loc,
4294 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
4295 Parameter_Associations => Empty_List),
4296 Stmts);
4297 end if;
4299 if Nkind (Op_Spec) = N_Function_Specification then
4300 Append (Return_Stmt, Stmts);
4301 Append (Make_Block_Statement (Loc,
4302 Declarations => New_List (Unprot_Call),
4303 Handled_Statement_Sequence =>
4304 Make_Handled_Sequence_Of_Statements (Loc,
4305 Statements => Stmts)), Pre_Stmts);
4306 Stmts := Pre_Stmts;
4307 end if;
4308 end if;
4310 Sub_Body :=
4311 Make_Subprogram_Body (Loc,
4312 Declarations => Empty_List,
4313 Specification => P_Op_Spec,
4314 Handled_Statement_Sequence =>
4315 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4317 if not Exc_Safe then
4318 Set_Is_Protected_Subprogram_Body (Sub_Body);
4319 end if;
4321 return Sub_Body;
4322 end Build_Protected_Subprogram_Body;
4324 -------------------------------------
4325 -- Build_Protected_Subprogram_Call --
4326 -------------------------------------
4328 procedure Build_Protected_Subprogram_Call
4329 (N : Node_Id;
4330 Name : Node_Id;
4331 Rec : Node_Id;
4332 External : Boolean := True)
4334 Loc : constant Source_Ptr := Sloc (N);
4335 Sub : constant Entity_Id := Entity (Name);
4336 New_Sub : Node_Id;
4337 Params : List_Id;
4339 begin
4340 if External then
4341 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4342 else
4343 New_Sub :=
4344 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4345 end if;
4347 if Present (Parameter_Associations (N)) then
4348 Params := New_Copy_List_Tree (Parameter_Associations (N));
4349 else
4350 Params := New_List;
4351 end if;
4353 -- If the type is an untagged derived type, convert to the root type,
4354 -- which is the one on which the operations are defined.
4356 if Nkind (Rec) = N_Unchecked_Type_Conversion
4357 and then not Is_Tagged_Type (Etype (Rec))
4358 and then Is_Derived_Type (Etype (Rec))
4359 then
4360 Set_Etype (Rec, Root_Type (Etype (Rec)));
4361 Set_Subtype_Mark (Rec,
4362 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4363 end if;
4365 Prepend (Rec, Params);
4367 if Ekind (Sub) = E_Procedure then
4368 Rewrite (N,
4369 Make_Procedure_Call_Statement (Loc,
4370 Name => New_Sub,
4371 Parameter_Associations => Params));
4373 else
4374 pragma Assert (Ekind (Sub) = E_Function);
4375 Rewrite (N,
4376 Make_Function_Call (Loc,
4377 Name => New_Sub,
4378 Parameter_Associations => Params));
4379 end if;
4381 if External
4382 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4383 and then Is_Entity_Name (Expression (Rec))
4384 and then Is_Shared_Passive (Entity (Expression (Rec)))
4385 then
4386 Add_Shared_Var_Lock_Procs (N);
4387 end if;
4388 end Build_Protected_Subprogram_Call;
4390 -------------------------
4391 -- Build_Selected_Name --
4392 -------------------------
4394 function Build_Selected_Name
4395 (Prefix : Entity_Id;
4396 Selector : Entity_Id;
4397 Append_Char : Character := ' ') return Name_Id
4399 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4400 Select_Len : Natural;
4402 begin
4403 Get_Name_String (Chars (Selector));
4404 Select_Len := Name_Len;
4405 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4406 Get_Name_String (Chars (Prefix));
4408 -- If scope is anonymous type, discard suffix to recover name of
4409 -- single protected object. Otherwise use protected type name.
4411 if Name_Buffer (Name_Len) = 'T' then
4412 Name_Len := Name_Len - 1;
4413 end if;
4415 Add_Str_To_Name_Buffer ("__");
4416 for J in 1 .. Select_Len loop
4417 Add_Char_To_Name_Buffer (Select_Buffer (J));
4418 end loop;
4420 -- Now add the Append_Char if specified. The encoding to follow
4421 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4422 -- then the entity is associated to a protected type subprogram.
4423 -- Otherwise, it is a protected type entry. For each case, the
4424 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4426 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4428 if Append_Char /= ' ' then
4429 if Append_Char = 'P' or Append_Char = 'N' then
4430 Add_Char_To_Name_Buffer (Append_Char);
4431 return Name_Find;
4432 else
4433 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4434 return New_External_Name (Name_Find, ' ', -1);
4435 end if;
4436 else
4437 return Name_Find;
4438 end if;
4439 end Build_Selected_Name;
4441 -----------------------------
4442 -- Build_Simple_Entry_Call --
4443 -----------------------------
4445 -- A task entry call is converted to a call to Call_Simple
4447 -- declare
4448 -- P : parms := (parm, parm, parm);
4449 -- begin
4450 -- Call_Simple (acceptor-task, entry-index, P'Address);
4451 -- parm := P.param;
4452 -- parm := P.param;
4453 -- ...
4454 -- end;
4456 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4457 -- the parameters, and the constructed aggregate value contains either the
4458 -- parameters or, in the case of non-elementary types, references to these
4459 -- parameters. Then the address of this aggregate is passed to the runtime
4460 -- routine, along with the task id value and the task entry index value.
4461 -- Pnn is only required if parameters are present.
4463 -- The assignments after the call are present only in the case of in-out
4464 -- or out parameters for elementary types, and are used to assign back the
4465 -- resulting values of such parameters.
4467 -- Note: the reason that we insert a block here is that in the context
4468 -- of selects, conditional entry calls etc. the entry call statement
4469 -- appears on its own, not as an element of a list.
4471 -- A protected entry call is converted to a Protected_Entry_Call:
4473 -- declare
4474 -- P : E1_Params := (param, param, param);
4475 -- Pnn : Boolean;
4476 -- Bnn : Communications_Block;
4478 -- declare
4479 -- P : E1_Params := (param, param, param);
4480 -- Bnn : Communications_Block;
4482 -- begin
4483 -- Protected_Entry_Call (
4484 -- Object => po._object'Access,
4485 -- E => <entry index>;
4486 -- Uninterpreted_Data => P'Address;
4487 -- Mode => Simple_Call;
4488 -- Block => Bnn);
4489 -- parm := P.param;
4490 -- parm := P.param;
4491 -- ...
4492 -- end;
4494 procedure Build_Simple_Entry_Call
4495 (N : Node_Id;
4496 Concval : Node_Id;
4497 Ename : Node_Id;
4498 Index : Node_Id)
4500 begin
4501 Expand_Call (N);
4503 -- If call has been inlined, nothing left to do
4505 if Nkind (N) = N_Block_Statement then
4506 return;
4507 end if;
4509 -- Convert entry call to Call_Simple call
4511 declare
4512 Loc : constant Source_Ptr := Sloc (N);
4513 Parms : constant List_Id := Parameter_Associations (N);
4514 Stats : constant List_Id := New_List;
4515 Actual : Node_Id;
4516 Call : Node_Id;
4517 Comm_Name : Entity_Id;
4518 Conctyp : Node_Id;
4519 Decls : List_Id;
4520 Ent : Entity_Id;
4521 Ent_Acc : Entity_Id;
4522 Formal : Node_Id;
4523 Iface_Tag : Entity_Id;
4524 Iface_Typ : Entity_Id;
4525 N_Node : Node_Id;
4526 N_Var : Node_Id;
4527 P : Entity_Id;
4528 Parm1 : Node_Id;
4529 Parm2 : Node_Id;
4530 Parm3 : Node_Id;
4531 Pdecl : Node_Id;
4532 Plist : List_Id;
4533 X : Entity_Id;
4534 Xdecl : Node_Id;
4536 begin
4537 -- Simple entry and entry family cases merge here
4539 Ent := Entity (Ename);
4540 Ent_Acc := Entry_Parameters_Type (Ent);
4541 Conctyp := Etype (Concval);
4543 -- If prefix is an access type, dereference to obtain the task type
4545 if Is_Access_Type (Conctyp) then
4546 Conctyp := Designated_Type (Conctyp);
4547 end if;
4549 -- Special case for protected subprogram calls
4551 if Is_Protected_Type (Conctyp)
4552 and then Is_Subprogram (Entity (Ename))
4553 then
4554 if not Is_Eliminated (Entity (Ename)) then
4555 Build_Protected_Subprogram_Call
4556 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4557 Analyze (N);
4558 end if;
4560 return;
4561 end if;
4563 -- First parameter is the Task_Id value from the task value or the
4564 -- Object from the protected object value, obtained by selecting
4565 -- the _Task_Id or _Object from the result of doing an unchecked
4566 -- conversion to convert the value to the corresponding record type.
4568 if Nkind (Concval) = N_Function_Call
4569 and then Is_Task_Type (Conctyp)
4570 and then Ada_Version >= Ada_2005
4571 then
4572 declare
4573 ExpR : constant Node_Id := Relocate_Node (Concval);
4574 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4575 Decl : Node_Id;
4577 begin
4578 Decl :=
4579 Make_Object_Declaration (Loc,
4580 Defining_Identifier => Obj,
4581 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4582 Expression => ExpR);
4583 Set_Etype (Obj, Conctyp);
4584 Decls := New_List (Decl);
4585 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4586 end;
4588 else
4589 Decls := New_List;
4590 end if;
4592 Parm1 := Concurrent_Ref (Concval);
4594 -- Second parameter is the entry index, computed by the routine
4595 -- provided for this purpose. The value of this expression is
4596 -- assigned to an intermediate variable to assure that any entry
4597 -- family index expressions are evaluated before the entry
4598 -- parameters.
4600 if Abort_Allowed
4601 or else Restriction_Active (No_Entry_Queue) = False
4602 or else not Is_Protected_Type (Conctyp)
4603 or else Number_Entries (Conctyp) > 1
4604 or else (Has_Attach_Handler (Conctyp)
4605 and then not Restricted_Profile)
4606 then
4607 X := Make_Defining_Identifier (Loc, Name_uX);
4609 Xdecl :=
4610 Make_Object_Declaration (Loc,
4611 Defining_Identifier => X,
4612 Object_Definition =>
4613 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
4614 Expression => Actual_Index_Expression (
4615 Loc, Entity (Ename), Index, Concval));
4617 Append_To (Decls, Xdecl);
4618 Parm2 := New_Reference_To (X, Loc);
4620 else
4621 Xdecl := Empty;
4622 Parm2 := Empty;
4623 end if;
4625 -- The third parameter is the packaged parameters. If there are
4626 -- none, then it is just the null address, since nothing is passed.
4628 if No (Parms) then
4629 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
4630 P := Empty;
4632 -- Case of parameters present, where third argument is the address
4633 -- of a packaged record containing the required parameter values.
4635 else
4636 -- First build a list of parameter values, which are references to
4637 -- objects of the parameter types.
4639 Plist := New_List;
4641 Actual := First_Actual (N);
4642 Formal := First_Formal (Ent);
4644 while Present (Actual) loop
4646 -- If it is a by_copy_type, copy it to a new variable. The
4647 -- packaged record has a field that points to this variable.
4649 if Is_By_Copy_Type (Etype (Actual)) then
4650 N_Node :=
4651 Make_Object_Declaration (Loc,
4652 Defining_Identifier => Make_Temporary (Loc, 'J'),
4653 Aliased_Present => True,
4654 Object_Definition =>
4655 New_Reference_To (Etype (Formal), Loc));
4657 -- Mark the object as not needing initialization since the
4658 -- initialization is performed separately, avoiding errors
4659 -- on cases such as formals of null-excluding access types.
4661 Set_No_Initialization (N_Node);
4663 -- We must make an assignment statement separate for the
4664 -- case of limited type. We cannot assign it unless the
4665 -- Assignment_OK flag is set first. An out formal of an
4666 -- access type must also be initialized from the actual,
4667 -- as stated in RM 6.4.1 (13).
4669 if Ekind (Formal) /= E_Out_Parameter
4670 or else Is_Access_Type (Etype (Formal))
4671 then
4672 N_Var :=
4673 New_Reference_To (Defining_Identifier (N_Node), Loc);
4674 Set_Assignment_OK (N_Var);
4675 Append_To (Stats,
4676 Make_Assignment_Statement (Loc,
4677 Name => N_Var,
4678 Expression => Relocate_Node (Actual)));
4679 end if;
4681 Append (N_Node, Decls);
4683 Append_To (Plist,
4684 Make_Attribute_Reference (Loc,
4685 Attribute_Name => Name_Unchecked_Access,
4686 Prefix =>
4687 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4689 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4691 elsif Is_VM_By_Copy_Actual (Actual) then
4692 N_Node :=
4693 Make_Object_Declaration (Loc,
4694 Defining_Identifier => Make_Temporary (Loc, 'J'),
4695 Aliased_Present => True,
4696 Object_Definition =>
4697 New_Reference_To (Etype (Formal), Loc),
4698 Expression => New_Copy_Tree (Actual));
4699 Set_Assignment_OK (N_Node);
4701 Append (N_Node, Decls);
4703 Append_To (Plist,
4704 Make_Attribute_Reference (Loc,
4705 Attribute_Name => Name_Unchecked_Access,
4706 Prefix =>
4707 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4709 else
4710 -- Interface class-wide formal
4712 if Ada_Version >= Ada_2005
4713 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4714 and then Is_Interface (Etype (Formal))
4715 then
4716 Iface_Typ := Etype (Etype (Formal));
4718 -- Generate:
4719 -- formal_iface_type! (actual.iface_tag)'reference
4721 Iface_Tag :=
4722 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4723 pragma Assert (Present (Iface_Tag));
4725 Append_To (Plist,
4726 Make_Reference (Loc,
4727 Unchecked_Convert_To (Iface_Typ,
4728 Make_Selected_Component (Loc,
4729 Prefix =>
4730 Relocate_Node (Actual),
4731 Selector_Name =>
4732 New_Reference_To (Iface_Tag, Loc)))));
4733 else
4734 -- Generate:
4735 -- actual'reference
4737 Append_To (Plist,
4738 Make_Reference (Loc, Relocate_Node (Actual)));
4739 end if;
4740 end if;
4742 Next_Actual (Actual);
4743 Next_Formal_With_Extras (Formal);
4744 end loop;
4746 -- Now build the declaration of parameters initialized with the
4747 -- aggregate containing this constructed parameter list.
4749 P := Make_Defining_Identifier (Loc, Name_uP);
4751 Pdecl :=
4752 Make_Object_Declaration (Loc,
4753 Defining_Identifier => P,
4754 Object_Definition =>
4755 New_Reference_To (Designated_Type (Ent_Acc), Loc),
4756 Expression =>
4757 Make_Aggregate (Loc, Expressions => Plist));
4759 Parm3 :=
4760 Make_Attribute_Reference (Loc,
4761 Prefix => New_Reference_To (P, Loc),
4762 Attribute_Name => Name_Address);
4764 Append (Pdecl, Decls);
4765 end if;
4767 -- Now we can create the call, case of protected type
4769 if Is_Protected_Type (Conctyp) then
4770 case Corresponding_Runtime_Package (Conctyp) is
4771 when System_Tasking_Protected_Objects_Entries =>
4773 -- Change the type of the index declaration
4775 Set_Object_Definition (Xdecl,
4776 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
4778 -- Some additional declarations for protected entry calls
4780 if No (Decls) then
4781 Decls := New_List;
4782 end if;
4784 -- Bnn : Communications_Block;
4786 Comm_Name := Make_Temporary (Loc, 'B');
4788 Append_To (Decls,
4789 Make_Object_Declaration (Loc,
4790 Defining_Identifier => Comm_Name,
4791 Object_Definition =>
4792 New_Reference_To (RTE (RE_Communication_Block), Loc)));
4794 -- Some additional statements for protected entry calls
4796 -- Protected_Entry_Call (
4797 -- Object => po._object'Access,
4798 -- E => <entry index>;
4799 -- Uninterpreted_Data => P'Address;
4800 -- Mode => Simple_Call;
4801 -- Block => Bnn);
4803 Call :=
4804 Make_Procedure_Call_Statement (Loc,
4805 Name =>
4806 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
4808 Parameter_Associations => New_List (
4809 Make_Attribute_Reference (Loc,
4810 Attribute_Name => Name_Unchecked_Access,
4811 Prefix => Parm1),
4812 Parm2,
4813 Parm3,
4814 New_Reference_To (RTE (RE_Simple_Call), Loc),
4815 New_Occurrence_Of (Comm_Name, Loc)));
4817 when System_Tasking_Protected_Objects_Single_Entry =>
4818 -- Protected_Single_Entry_Call (
4819 -- Object => po._object'Access,
4820 -- Uninterpreted_Data => P'Address;
4821 -- Mode => Simple_Call);
4823 Call :=
4824 Make_Procedure_Call_Statement (Loc,
4825 Name => New_Reference_To (
4826 RTE (RE_Protected_Single_Entry_Call), Loc),
4828 Parameter_Associations => New_List (
4829 Make_Attribute_Reference (Loc,
4830 Attribute_Name => Name_Unchecked_Access,
4831 Prefix => Parm1),
4832 Parm3,
4833 New_Reference_To (RTE (RE_Simple_Call), Loc)));
4835 when others =>
4836 raise Program_Error;
4837 end case;
4839 -- Case of task type
4841 else
4842 Call :=
4843 Make_Procedure_Call_Statement (Loc,
4844 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
4845 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4847 end if;
4849 Append_To (Stats, Call);
4851 -- If there are out or in/out parameters by copy add assignment
4852 -- statements for the result values.
4854 if Present (Parms) then
4855 Actual := First_Actual (N);
4856 Formal := First_Formal (Ent);
4858 Set_Assignment_OK (Actual);
4859 while Present (Actual) loop
4860 if (Is_By_Copy_Type (Etype (Actual))
4861 or else Is_VM_By_Copy_Actual (Actual))
4862 and then Ekind (Formal) /= E_In_Parameter
4863 then
4864 N_Node :=
4865 Make_Assignment_Statement (Loc,
4866 Name => New_Copy (Actual),
4867 Expression =>
4868 Make_Explicit_Dereference (Loc,
4869 Make_Selected_Component (Loc,
4870 Prefix => New_Reference_To (P, Loc),
4871 Selector_Name =>
4872 Make_Identifier (Loc, Chars (Formal)))));
4874 -- In all cases (including limited private types) we want
4875 -- the assignment to be valid.
4877 Set_Assignment_OK (Name (N_Node));
4879 -- If the call is the triggering alternative in an
4880 -- asynchronous select, or the entry_call alternative of a
4881 -- conditional entry call, the assignments for in-out
4882 -- parameters are incorporated into the statement list that
4883 -- follows, so that there are executed only if the entry
4884 -- call succeeds.
4886 if (Nkind (Parent (N)) = N_Triggering_Alternative
4887 and then N = Triggering_Statement (Parent (N)))
4888 or else
4889 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4890 and then N = Entry_Call_Statement (Parent (N)))
4891 then
4892 if No (Statements (Parent (N))) then
4893 Set_Statements (Parent (N), New_List);
4894 end if;
4896 Prepend (N_Node, Statements (Parent (N)));
4898 else
4899 Insert_After (Call, N_Node);
4900 end if;
4901 end if;
4903 Next_Actual (Actual);
4904 Next_Formal_With_Extras (Formal);
4905 end loop;
4906 end if;
4908 -- Finally, create block and analyze it
4910 Rewrite (N,
4911 Make_Block_Statement (Loc,
4912 Declarations => Decls,
4913 Handled_Statement_Sequence =>
4914 Make_Handled_Sequence_Of_Statements (Loc,
4915 Statements => Stats)));
4917 Analyze (N);
4918 end;
4919 end Build_Simple_Entry_Call;
4921 --------------------------------
4922 -- Build_Task_Activation_Call --
4923 --------------------------------
4925 procedure Build_Task_Activation_Call (N : Node_Id) is
4926 Loc : constant Source_Ptr := Sloc (N);
4927 Chain : Entity_Id;
4928 Call : Node_Id;
4929 Name : Node_Id;
4930 P : Node_Id;
4932 begin
4933 -- For sequential elaboration policy, all the tasks will be activated at
4934 -- the end of the elaboration.
4936 if Partition_Elaboration_Policy = 'S' then
4937 return;
4938 end if;
4940 -- Get the activation chain entity. Except in the case of a package
4941 -- body, this is in the node that was passed. For a package body, we
4942 -- have to find the corresponding package declaration node.
4944 if Nkind (N) = N_Package_Body then
4945 P := Corresponding_Spec (N);
4946 loop
4947 P := Parent (P);
4948 exit when Nkind (P) = N_Package_Declaration;
4949 end loop;
4951 Chain := Activation_Chain_Entity (P);
4953 else
4954 Chain := Activation_Chain_Entity (N);
4955 end if;
4957 if Present (Chain) then
4958 if Restricted_Profile then
4959 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
4960 else
4961 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
4962 end if;
4964 Call :=
4965 Make_Procedure_Call_Statement (Loc,
4966 Name => Name,
4967 Parameter_Associations =>
4968 New_List (Make_Attribute_Reference (Loc,
4969 Prefix => New_Occurrence_Of (Chain, Loc),
4970 Attribute_Name => Name_Unchecked_Access)));
4972 if Nkind (N) = N_Package_Declaration then
4973 if Present (Corresponding_Body (N)) then
4974 null;
4976 elsif Present (Private_Declarations (Specification (N))) then
4977 Append (Call, Private_Declarations (Specification (N)));
4979 else
4980 Append (Call, Visible_Declarations (Specification (N)));
4981 end if;
4983 else
4984 if Present (Handled_Statement_Sequence (N)) then
4986 -- The call goes at the start of the statement sequence
4987 -- after the start of exception range label if one is present.
4989 declare
4990 Stm : Node_Id;
4992 begin
4993 Stm := First (Statements (Handled_Statement_Sequence (N)));
4995 -- A special case, skip exception range label if one is
4996 -- present (from front end zcx processing).
4998 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4999 Next (Stm);
5000 end if;
5002 -- Another special case, if the first statement is a block
5003 -- from optimization of a local raise to a goto, then the
5004 -- call goes inside this block.
5006 if Nkind (Stm) = N_Block_Statement
5007 and then Exception_Junk (Stm)
5008 then
5009 Stm :=
5010 First (Statements (Handled_Statement_Sequence (Stm)));
5011 end if;
5013 -- Insertion point is after any exception label pushes,
5014 -- since we want it covered by any local handlers.
5016 while Nkind (Stm) in N_Push_xxx_Label loop
5017 Next (Stm);
5018 end loop;
5020 -- Now we have the proper insertion point
5022 Insert_Before (Stm, Call);
5023 end;
5025 else
5026 Set_Handled_Statement_Sequence (N,
5027 Make_Handled_Sequence_Of_Statements (Loc,
5028 Statements => New_List (Call)));
5029 end if;
5030 end if;
5032 Analyze (Call);
5033 Check_Task_Activation (N);
5034 end if;
5035 end Build_Task_Activation_Call;
5037 -------------------------------
5038 -- Build_Task_Allocate_Block --
5039 -------------------------------
5041 procedure Build_Task_Allocate_Block
5042 (Actions : List_Id;
5043 N : Node_Id;
5044 Args : List_Id)
5046 T : constant Entity_Id := Entity (Expression (N));
5047 Init : constant Entity_Id := Base_Init_Proc (T);
5048 Loc : constant Source_Ptr := Sloc (N);
5049 Chain : constant Entity_Id :=
5050 Make_Defining_Identifier (Loc, Name_uChain);
5051 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5052 Block : Node_Id;
5054 begin
5055 Block :=
5056 Make_Block_Statement (Loc,
5057 Identifier => New_Reference_To (Blkent, Loc),
5058 Declarations => New_List (
5060 -- _Chain : Activation_Chain;
5062 Make_Object_Declaration (Loc,
5063 Defining_Identifier => Chain,
5064 Aliased_Present => True,
5065 Object_Definition =>
5066 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5068 Handled_Statement_Sequence =>
5069 Make_Handled_Sequence_Of_Statements (Loc,
5071 Statements => New_List (
5073 -- Init (Args);
5075 Make_Procedure_Call_Statement (Loc,
5076 Name => New_Reference_To (Init, Loc),
5077 Parameter_Associations => Args),
5079 -- Activate_Tasks (_Chain);
5081 Make_Procedure_Call_Statement (Loc,
5082 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5083 Parameter_Associations => New_List (
5084 Make_Attribute_Reference (Loc,
5085 Prefix => New_Reference_To (Chain, Loc),
5086 Attribute_Name => Name_Unchecked_Access))))),
5088 Has_Created_Identifier => True,
5089 Is_Task_Allocation_Block => True);
5091 Append_To (Actions,
5092 Make_Implicit_Label_Declaration (Loc,
5093 Defining_Identifier => Blkent,
5094 Label_Construct => Block));
5096 Append_To (Actions, Block);
5098 Set_Activation_Chain_Entity (Block, Chain);
5099 end Build_Task_Allocate_Block;
5101 -----------------------------------------------
5102 -- Build_Task_Allocate_Block_With_Init_Stmts --
5103 -----------------------------------------------
5105 procedure Build_Task_Allocate_Block_With_Init_Stmts
5106 (Actions : List_Id;
5107 N : Node_Id;
5108 Init_Stmts : List_Id)
5110 Loc : constant Source_Ptr := Sloc (N);
5111 Chain : constant Entity_Id :=
5112 Make_Defining_Identifier (Loc, Name_uChain);
5113 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5114 Block : Node_Id;
5116 begin
5117 Append_To (Init_Stmts,
5118 Make_Procedure_Call_Statement (Loc,
5119 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5120 Parameter_Associations => New_List (
5121 Make_Attribute_Reference (Loc,
5122 Prefix => New_Reference_To (Chain, Loc),
5123 Attribute_Name => Name_Unchecked_Access))));
5125 Block :=
5126 Make_Block_Statement (Loc,
5127 Identifier => New_Reference_To (Blkent, Loc),
5128 Declarations => New_List (
5130 -- _Chain : Activation_Chain;
5132 Make_Object_Declaration (Loc,
5133 Defining_Identifier => Chain,
5134 Aliased_Present => True,
5135 Object_Definition =>
5136 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5138 Handled_Statement_Sequence =>
5139 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5141 Has_Created_Identifier => True,
5142 Is_Task_Allocation_Block => True);
5144 Append_To (Actions,
5145 Make_Implicit_Label_Declaration (Loc,
5146 Defining_Identifier => Blkent,
5147 Label_Construct => Block));
5149 Append_To (Actions, Block);
5151 Set_Activation_Chain_Entity (Block, Chain);
5152 end Build_Task_Allocate_Block_With_Init_Stmts;
5154 -----------------------------------
5155 -- Build_Task_Proc_Specification --
5156 -----------------------------------
5158 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5159 Loc : constant Source_Ptr := Sloc (T);
5160 Spec_Id : Entity_Id;
5162 begin
5163 -- Case of explicit task type, suffix TB
5165 if Comes_From_Source (T) then
5166 Spec_Id :=
5167 Make_Defining_Identifier (Loc,
5168 Chars => New_External_Name (Chars (T), "TB"));
5170 -- Case of anonymous task type, suffix B
5172 else
5173 Spec_Id :=
5174 Make_Defining_Identifier (Loc,
5175 Chars => New_External_Name (Chars (T), 'B'));
5176 end if;
5178 Set_Is_Internal (Spec_Id);
5180 -- Associate the procedure with the task, if this is the declaration
5181 -- (and not the body) of the procedure.
5183 if No (Task_Body_Procedure (T)) then
5184 Set_Task_Body_Procedure (T, Spec_Id);
5185 end if;
5187 return
5188 Make_Procedure_Specification (Loc,
5189 Defining_Unit_Name => Spec_Id,
5190 Parameter_Specifications => New_List (
5191 Make_Parameter_Specification (Loc,
5192 Defining_Identifier =>
5193 Make_Defining_Identifier (Loc, Name_uTask),
5194 Parameter_Type =>
5195 Make_Access_Definition (Loc,
5196 Subtype_Mark =>
5197 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
5198 end Build_Task_Proc_Specification;
5200 ---------------------------------------
5201 -- Build_Unprotected_Subprogram_Body --
5202 ---------------------------------------
5204 function Build_Unprotected_Subprogram_Body
5205 (N : Node_Id;
5206 Pid : Node_Id) return Node_Id
5208 Decls : constant List_Id := Declarations (N);
5210 begin
5211 -- Add renamings for the Protection object, discriminals, privals and
5212 -- the entry index constant for use by debugger.
5214 Debug_Private_Data_Declarations (Decls);
5216 -- Make an unprotected version of the subprogram for use within the same
5217 -- object, with a new name and an additional parameter representing the
5218 -- object.
5220 return
5221 Make_Subprogram_Body (Sloc (N),
5222 Specification =>
5223 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5224 Declarations => Decls,
5225 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5226 end Build_Unprotected_Subprogram_Body;
5228 ----------------------------
5229 -- Collect_Entry_Families --
5230 ----------------------------
5232 procedure Collect_Entry_Families
5233 (Loc : Source_Ptr;
5234 Cdecls : List_Id;
5235 Current_Node : in out Node_Id;
5236 Conctyp : Entity_Id)
5238 Efam : Entity_Id;
5239 Efam_Decl : Node_Id;
5240 Efam_Type : Entity_Id;
5242 begin
5243 Efam := First_Entity (Conctyp);
5244 while Present (Efam) loop
5245 if Ekind (Efam) = E_Entry_Family then
5246 Efam_Type := Make_Temporary (Loc, 'F');
5248 declare
5249 Bas : Entity_Id :=
5250 Base_Type
5251 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5253 Bas_Decl : Node_Id := Empty;
5254 Lo, Hi : Node_Id;
5256 begin
5257 Get_Index_Bounds
5258 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5260 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5261 Bas := Make_Temporary (Loc, 'B');
5263 Bas_Decl :=
5264 Make_Subtype_Declaration (Loc,
5265 Defining_Identifier => Bas,
5266 Subtype_Indication =>
5267 Make_Subtype_Indication (Loc,
5268 Subtype_Mark =>
5269 New_Occurrence_Of (Standard_Integer, Loc),
5270 Constraint =>
5271 Make_Range_Constraint (Loc,
5272 Range_Expression => Make_Range (Loc,
5273 Make_Integer_Literal
5274 (Loc, -Entry_Family_Bound),
5275 Make_Integer_Literal
5276 (Loc, Entry_Family_Bound - 1)))));
5278 Insert_After (Current_Node, Bas_Decl);
5279 Current_Node := Bas_Decl;
5280 Analyze (Bas_Decl);
5281 end if;
5283 Efam_Decl :=
5284 Make_Full_Type_Declaration (Loc,
5285 Defining_Identifier => Efam_Type,
5286 Type_Definition =>
5287 Make_Unconstrained_Array_Definition (Loc,
5288 Subtype_Marks =>
5289 (New_List (New_Occurrence_Of (Bas, Loc))),
5291 Component_Definition =>
5292 Make_Component_Definition (Loc,
5293 Aliased_Present => False,
5294 Subtype_Indication =>
5295 New_Reference_To (Standard_Character, Loc))));
5296 end;
5298 Insert_After (Current_Node, Efam_Decl);
5299 Current_Node := Efam_Decl;
5300 Analyze (Efam_Decl);
5302 Append_To (Cdecls,
5303 Make_Component_Declaration (Loc,
5304 Defining_Identifier =>
5305 Make_Defining_Identifier (Loc, Chars (Efam)),
5307 Component_Definition =>
5308 Make_Component_Definition (Loc,
5309 Aliased_Present => False,
5310 Subtype_Indication =>
5311 Make_Subtype_Indication (Loc,
5312 Subtype_Mark =>
5313 New_Occurrence_Of (Efam_Type, Loc),
5315 Constraint =>
5316 Make_Index_Or_Discriminant_Constraint (Loc,
5317 Constraints => New_List (
5318 New_Occurrence_Of
5319 (Etype (Discrete_Subtype_Definition
5320 (Parent (Efam))), Loc)))))));
5322 end if;
5324 Next_Entity (Efam);
5325 end loop;
5326 end Collect_Entry_Families;
5328 -----------------------
5329 -- Concurrent_Object --
5330 -----------------------
5332 function Concurrent_Object
5333 (Spec_Id : Entity_Id;
5334 Conc_Typ : Entity_Id) return Entity_Id
5336 begin
5337 -- Parameter _O or _object
5339 if Is_Protected_Type (Conc_Typ) then
5340 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5342 -- Parameter _task
5344 else
5345 pragma Assert (Is_Task_Type (Conc_Typ));
5346 return First_Formal (Task_Body_Procedure (Conc_Typ));
5347 end if;
5348 end Concurrent_Object;
5350 ----------------------
5351 -- Copy_Result_Type --
5352 ----------------------
5354 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5355 New_Res : constant Node_Id := New_Copy_Tree (Res);
5356 Par_Spec : Node_Id;
5357 Formal : Entity_Id;
5359 begin
5360 -- If the result type is an access_to_subprogram, we must create new
5361 -- entities for its spec.
5363 if Nkind (New_Res) = N_Access_Definition
5364 and then Present (Access_To_Subprogram_Definition (New_Res))
5365 then
5366 -- Provide new entities for the formals
5368 Par_Spec := First (Parameter_Specifications
5369 (Access_To_Subprogram_Definition (New_Res)));
5370 while Present (Par_Spec) loop
5371 Formal := Defining_Identifier (Par_Spec);
5372 Set_Defining_Identifier (Par_Spec,
5373 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5374 Next (Par_Spec);
5375 end loop;
5376 end if;
5378 return New_Res;
5379 end Copy_Result_Type;
5381 --------------------
5382 -- Concurrent_Ref --
5383 --------------------
5385 -- The expression returned for a reference to a concurrent object has the
5386 -- form:
5388 -- taskV!(name)._Task_Id
5390 -- for a task, and
5392 -- objectV!(name)._Object
5394 -- for a protected object. For the case of an access to a concurrent
5395 -- object, there is an extra explicit dereference:
5397 -- taskV!(name.all)._Task_Id
5398 -- objectV!(name.all)._Object
5400 -- here taskV and objectV are the types for the associated records, which
5401 -- contain the required _Task_Id and _Object fields for tasks and protected
5402 -- objects, respectively.
5404 -- For the case of a task type name, the expression is
5406 -- Self;
5408 -- i.e. a call to the Self function which returns precisely this Task_Id
5410 -- For the case of a protected type name, the expression is
5412 -- objectR
5414 -- which is a renaming of the _object field of the current object
5415 -- record, passed into protected operations as a parameter.
5417 function Concurrent_Ref (N : Node_Id) return Node_Id is
5418 Loc : constant Source_Ptr := Sloc (N);
5419 Ntyp : constant Entity_Id := Etype (N);
5420 Dtyp : Entity_Id;
5421 Sel : Name_Id;
5423 function Is_Current_Task (T : Entity_Id) return Boolean;
5424 -- Check whether the reference is to the immediately enclosing task
5425 -- type, or to an outer one (rare but legal).
5427 ---------------------
5428 -- Is_Current_Task --
5429 ---------------------
5431 function Is_Current_Task (T : Entity_Id) return Boolean is
5432 Scop : Entity_Id;
5434 begin
5435 Scop := Current_Scope;
5436 while Present (Scop)
5437 and then Scop /= Standard_Standard
5438 loop
5440 if Scop = T then
5441 return True;
5443 elsif Is_Task_Type (Scop) then
5444 return False;
5446 -- If this is a procedure nested within the task type, we must
5447 -- assume that it can be called from an inner task, and therefore
5448 -- cannot treat it as a local reference.
5450 elsif Is_Overloadable (Scop)
5451 and then In_Open_Scopes (T)
5452 then
5453 return False;
5455 else
5456 Scop := Scope (Scop);
5457 end if;
5458 end loop;
5460 -- We know that we are within the task body, so should have found it
5461 -- in scope.
5463 raise Program_Error;
5464 end Is_Current_Task;
5466 -- Start of processing for Concurrent_Ref
5468 begin
5469 if Is_Access_Type (Ntyp) then
5470 Dtyp := Designated_Type (Ntyp);
5472 if Is_Protected_Type (Dtyp) then
5473 Sel := Name_uObject;
5474 else
5475 Sel := Name_uTask_Id;
5476 end if;
5478 return
5479 Make_Selected_Component (Loc,
5480 Prefix =>
5481 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5482 Make_Explicit_Dereference (Loc, N)),
5483 Selector_Name => Make_Identifier (Loc, Sel));
5485 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5486 if Is_Task_Type (Entity (N)) then
5488 if Is_Current_Task (Entity (N)) then
5489 return
5490 Make_Function_Call (Loc,
5491 Name => New_Reference_To (RTE (RE_Self), Loc));
5493 else
5494 declare
5495 Decl : Node_Id;
5496 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5497 T_Body : constant Node_Id :=
5498 Parent (Corresponding_Body (Parent (Entity (N))));
5500 begin
5501 Decl :=
5502 Make_Object_Declaration (Loc,
5503 Defining_Identifier => T_Self,
5504 Object_Definition =>
5505 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5506 Expression =>
5507 Make_Function_Call (Loc,
5508 Name => New_Reference_To (RTE (RE_Self), Loc)));
5509 Prepend (Decl, Declarations (T_Body));
5510 Analyze (Decl);
5511 Set_Scope (T_Self, Entity (N));
5512 return New_Occurrence_Of (T_Self, Loc);
5513 end;
5514 end if;
5516 else
5517 pragma Assert (Is_Protected_Type (Entity (N)));
5519 return
5520 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
5521 end if;
5523 else
5524 if Is_Protected_Type (Ntyp) then
5525 Sel := Name_uObject;
5527 elsif Is_Task_Type (Ntyp) then
5528 Sel := Name_uTask_Id;
5530 else
5531 raise Program_Error;
5532 end if;
5534 return
5535 Make_Selected_Component (Loc,
5536 Prefix =>
5537 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5538 New_Copy_Tree (N)),
5539 Selector_Name => Make_Identifier (Loc, Sel));
5540 end if;
5541 end Concurrent_Ref;
5543 ------------------------
5544 -- Convert_Concurrent --
5545 ------------------------
5547 function Convert_Concurrent
5548 (N : Node_Id;
5549 Typ : Entity_Id) return Node_Id
5551 begin
5552 if not Is_Concurrent_Type (Typ) then
5553 return N;
5554 else
5555 return
5556 Unchecked_Convert_To
5557 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5558 end if;
5559 end Convert_Concurrent;
5561 -------------------------------------
5562 -- Debug_Private_Data_Declarations --
5563 -------------------------------------
5565 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5566 Debug_Nod : Node_Id;
5567 Decl : Node_Id;
5569 begin
5570 Decl := First (Decls);
5571 while Present (Decl) and then not Comes_From_Source (Decl) loop
5572 -- Declaration for concurrent entity _object and its access type,
5573 -- along with the entry index subtype:
5574 -- type prot_typVP is access prot_typV;
5575 -- _object : prot_typVP := prot_typV (_O);
5576 -- subtype Jnn is <Type of Index> range Low .. High;
5578 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5579 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5581 -- Declaration for the Protection object, discriminals, privals and
5582 -- entry index constant:
5583 -- conc_typR : protection_typ renames _object._object;
5584 -- discr_nameD : discr_typ renames _object.discr_name;
5585 -- discr_nameD : discr_typ renames _task.discr_name;
5586 -- prival_name : comp_typ renames _object.comp_name;
5587 -- J : constant Jnn :=
5588 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5590 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5591 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5592 Debug_Nod := Debug_Renaming_Declaration (Decl);
5594 if Present (Debug_Nod) then
5595 Insert_After (Decl, Debug_Nod);
5596 end if;
5597 end if;
5599 Next (Decl);
5600 end loop;
5601 end Debug_Private_Data_Declarations;
5603 ------------------------------
5604 -- Ensure_Statement_Present --
5605 ------------------------------
5607 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5608 Stmt : Node_Id;
5610 begin
5611 if Opt.Suppress_Control_Flow_Optimizations
5612 and then Is_Empty_List (Statements (Alt))
5613 then
5614 Stmt := Make_Null_Statement (Loc);
5616 -- Mark NULL statement as coming from source so that it is not
5617 -- eliminated by GIGI.
5619 -- Another covert channel! If this is a requirement, it must be
5620 -- documented in sinfo/einfo ???
5622 Set_Comes_From_Source (Stmt, True);
5624 Set_Statements (Alt, New_List (Stmt));
5625 end if;
5626 end Ensure_Statement_Present;
5628 ----------------------------
5629 -- Entry_Index_Expression --
5630 ----------------------------
5632 function Entry_Index_Expression
5633 (Sloc : Source_Ptr;
5634 Ent : Entity_Id;
5635 Index : Node_Id;
5636 Ttyp : Entity_Id) return Node_Id
5638 Expr : Node_Id;
5639 Num : Node_Id;
5640 Lo : Node_Id;
5641 Hi : Node_Id;
5642 Prev : Entity_Id;
5643 S : Node_Id;
5645 begin
5646 -- The queues of entries and entry families appear in textual order in
5647 -- the associated record. The entry index is computed as the sum of the
5648 -- number of queues for all entries that precede the designated one, to
5649 -- which is added the index expression, if this expression denotes a
5650 -- member of a family.
5652 -- The following is a place holder for the count of simple entries
5654 Num := Make_Integer_Literal (Sloc, 1);
5656 -- We construct an expression which is a series of addition operations.
5657 -- The first operand is the number of single entries that precede this
5658 -- one, the second operand is the index value relative to the start of
5659 -- the referenced family, and the remaining operands are the lengths of
5660 -- the entry families that precede this entry, i.e. the constructed
5661 -- expression is:
5663 -- number_simple_entries +
5664 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5665 -- family'length + ...
5667 -- where index-value is the given index value, and s is the index
5668 -- subtype (we have to use pos because the subtype might be an
5669 -- enumeration type preventing direct subtraction). Note that the task
5670 -- entry array is one-indexed.
5672 -- The upper bound of the entry family may be a discriminant, so we
5673 -- retrieve the lower bound explicitly to compute offset, rather than
5674 -- using the index subtype which may mention a discriminant.
5676 if Present (Index) then
5677 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5679 Expr :=
5680 Make_Op_Add (Sloc,
5681 Left_Opnd => Num,
5683 Right_Opnd =>
5684 Family_Offset (
5685 Sloc,
5686 Make_Attribute_Reference (Sloc,
5687 Attribute_Name => Name_Pos,
5688 Prefix => New_Reference_To (Base_Type (S), Sloc),
5689 Expressions => New_List (Relocate_Node (Index))),
5690 Type_Low_Bound (S),
5691 Ttyp,
5692 False));
5693 else
5694 Expr := Num;
5695 end if;
5697 -- Now add lengths of preceding entries and entry families
5699 Prev := First_Entity (Ttyp);
5701 while Chars (Prev) /= Chars (Ent)
5702 or else (Ekind (Prev) /= Ekind (Ent))
5703 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5704 loop
5705 if Ekind (Prev) = E_Entry then
5706 Set_Intval (Num, Intval (Num) + 1);
5708 elsif Ekind (Prev) = E_Entry_Family then
5709 S :=
5710 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5711 Lo := Type_Low_Bound (S);
5712 Hi := Type_High_Bound (S);
5714 Expr :=
5715 Make_Op_Add (Sloc,
5716 Left_Opnd => Expr,
5717 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5719 -- Other components are anonymous types to be ignored
5721 else
5722 null;
5723 end if;
5725 Next_Entity (Prev);
5726 end loop;
5728 return Expr;
5729 end Entry_Index_Expression;
5731 ---------------------------
5732 -- Establish_Task_Master --
5733 ---------------------------
5735 procedure Establish_Task_Master (N : Node_Id) is
5736 Call : Node_Id;
5738 begin
5739 if Restriction_Active (No_Task_Hierarchy) = False then
5740 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5742 -- The block may have no declarations, and nevertheless be a task
5743 -- master, if it contains a call that may return an object that
5744 -- contains tasks.
5746 if No (Declarations (N)) then
5747 Set_Declarations (N, New_List (Call));
5748 else
5749 Prepend_To (Declarations (N), Call);
5750 end if;
5752 Analyze (Call);
5753 end if;
5754 end Establish_Task_Master;
5756 --------------------------------
5757 -- Expand_Accept_Declarations --
5758 --------------------------------
5760 -- Part of the expansion of an accept statement involves the creation of
5761 -- a declaration that can be referenced from the statement sequence of
5762 -- the accept:
5764 -- Ann : Address;
5766 -- This declaration is inserted immediately before the accept statement
5767 -- and it is important that it be inserted before the statements of the
5768 -- statement sequence are analyzed. Thus it would be too late to create
5769 -- this declaration in the Expand_N_Accept_Statement routine, which is
5770 -- why there is a separate procedure to be called directly from Sem_Ch9.
5772 -- Ann is used to hold the address of the record containing the parameters
5773 -- (see Expand_N_Entry_Call for more details on how this record is built).
5774 -- References to the parameters do an unchecked conversion of this address
5775 -- to a pointer to the required record type, and then access the field that
5776 -- holds the value of the required parameter. The entity for the address
5777 -- variable is held as the top stack element (i.e. the last element) of the
5778 -- Accept_Address stack in the corresponding entry entity, and this element
5779 -- must be set in place before the statements are processed.
5781 -- The above description applies to the case of a stand alone accept
5782 -- statement, i.e. one not appearing as part of a select alternative.
5784 -- For the case of an accept that appears as part of a select alternative
5785 -- of a selective accept, we must still create the declaration right away,
5786 -- since Ann is needed immediately, but there is an important difference:
5788 -- The declaration is inserted before the selective accept, not before
5789 -- the accept statement (which is not part of a list anyway, and so would
5790 -- not accommodate inserted declarations)
5792 -- We only need one address variable for the entire selective accept. So
5793 -- the Ann declaration is created only for the first accept alternative,
5794 -- and subsequent accept alternatives reference the same Ann variable.
5796 -- We can distinguish the two cases by seeing whether the accept statement
5797 -- is part of a list. If not, then it must be in an accept alternative.
5799 -- To expand the requeue statement, a label is provided at the end of the
5800 -- accept statement or alternative of which it is a part, so that the
5801 -- statement can be skipped after the requeue is complete. This label is
5802 -- created here rather than during the expansion of the accept statement,
5803 -- because it will be needed by any requeue statements within the accept,
5804 -- which are expanded before the accept.
5806 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5807 Loc : constant Source_Ptr := Sloc (N);
5808 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5809 Ann : Entity_Id := Empty;
5810 Adecl : Node_Id;
5811 Lab : Node_Id;
5812 Ldecl : Node_Id;
5813 Ldecl2 : Node_Id;
5815 begin
5816 if Full_Expander_Active then
5818 -- If we have no handled statement sequence, we may need to build
5819 -- a dummy sequence consisting of a null statement. This can be
5820 -- skipped if the trivial accept optimization is permitted.
5822 if not Trivial_Accept_OK
5823 and then
5824 (No (Stats) or else Null_Statements (Statements (Stats)))
5825 then
5826 Set_Handled_Statement_Sequence (N,
5827 Make_Handled_Sequence_Of_Statements (Loc,
5828 Statements => New_List (Make_Null_Statement (Loc))));
5829 end if;
5831 -- Create and declare two labels to be placed at the end of the
5832 -- accept statement. The first label is used to allow requeues to
5833 -- skip the remainder of entry processing. The second label is used
5834 -- to skip the remainder of entry processing if the rendezvous
5835 -- completes in the middle of the accept body.
5837 if Present (Handled_Statement_Sequence (N)) then
5838 declare
5839 Ent : Entity_Id;
5841 begin
5842 Ent := Make_Temporary (Loc, 'L');
5843 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5844 Ldecl :=
5845 Make_Implicit_Label_Declaration (Loc,
5846 Defining_Identifier => Ent,
5847 Label_Construct => Lab);
5848 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5850 Ent := Make_Temporary (Loc, 'L');
5851 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5852 Ldecl2 :=
5853 Make_Implicit_Label_Declaration (Loc,
5854 Defining_Identifier => Ent,
5855 Label_Construct => Lab);
5856 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5857 end;
5859 else
5860 Ldecl := Empty;
5861 Ldecl2 := Empty;
5862 end if;
5864 -- Case of stand alone accept statement
5866 if Is_List_Member (N) then
5868 if Present (Handled_Statement_Sequence (N)) then
5869 Ann := Make_Temporary (Loc, 'A');
5871 Adecl :=
5872 Make_Object_Declaration (Loc,
5873 Defining_Identifier => Ann,
5874 Object_Definition =>
5875 New_Reference_To (RTE (RE_Address), Loc));
5877 Insert_Before_And_Analyze (N, Adecl);
5878 Insert_Before_And_Analyze (N, Ldecl);
5879 Insert_Before_And_Analyze (N, Ldecl2);
5880 end if;
5882 -- Case of accept statement which is in an accept alternative
5884 else
5885 declare
5886 Acc_Alt : constant Node_Id := Parent (N);
5887 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5888 Alt : Node_Id;
5890 begin
5891 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5892 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5894 -- ??? Consider a single label for select statements
5896 if Present (Handled_Statement_Sequence (N)) then
5897 Prepend (Ldecl2,
5898 Statements (Handled_Statement_Sequence (N)));
5899 Analyze (Ldecl2);
5901 Prepend (Ldecl,
5902 Statements (Handled_Statement_Sequence (N)));
5903 Analyze (Ldecl);
5904 end if;
5906 -- Find first accept alternative of the selective accept. A
5907 -- valid selective accept must have at least one accept in it.
5909 Alt := First (Select_Alternatives (Sel_Acc));
5911 while Nkind (Alt) /= N_Accept_Alternative loop
5912 Next (Alt);
5913 end loop;
5915 -- If we are the first accept statement, then we have to create
5916 -- the Ann variable, as for the stand alone case, except that
5917 -- it is inserted before the selective accept. Similarly, a
5918 -- label for requeue expansion must be declared.
5920 if N = Accept_Statement (Alt) then
5921 Ann := Make_Temporary (Loc, 'A');
5922 Adecl :=
5923 Make_Object_Declaration (Loc,
5924 Defining_Identifier => Ann,
5925 Object_Definition =>
5926 New_Reference_To (RTE (RE_Address), Loc));
5928 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5930 -- If we are not the first accept statement, then find the Ann
5931 -- variable allocated by the first accept and use it.
5933 else
5934 Ann :=
5935 Node (Last_Elmt (Accept_Address
5936 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5937 end if;
5938 end;
5939 end if;
5941 -- Merge here with Ann either created or referenced, and Adecl
5942 -- pointing to the corresponding declaration. Remaining processing
5943 -- is the same for the two cases.
5945 if Present (Ann) then
5946 Append_Elmt (Ann, Accept_Address (Ent));
5947 Set_Debug_Info_Needed (Ann);
5948 end if;
5950 -- Create renaming declarations for the entry formals. Each reference
5951 -- to a formal becomes a dereference of a component of the parameter
5952 -- block, whose address is held in Ann. These declarations are
5953 -- eventually inserted into the accept block, and analyzed there so
5954 -- that they have the proper scope for gdb and do not conflict with
5955 -- other declarations.
5957 if Present (Parameter_Specifications (N))
5958 and then Present (Handled_Statement_Sequence (N))
5959 then
5960 declare
5961 Comp : Entity_Id;
5962 Decl : Node_Id;
5963 Formal : Entity_Id;
5964 New_F : Entity_Id;
5965 Renamed_Formal : Node_Id;
5967 begin
5968 Push_Scope (Ent);
5969 Formal := First_Formal (Ent);
5971 while Present (Formal) loop
5972 Comp := Entry_Component (Formal);
5973 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5975 Set_Etype (New_F, Etype (Formal));
5976 Set_Scope (New_F, Ent);
5978 -- Now we set debug info needed on New_F even though it does
5979 -- not come from source, so that the debugger will get the
5980 -- right information for these generated names.
5982 Set_Debug_Info_Needed (New_F);
5984 if Ekind (Formal) = E_In_Parameter then
5985 Set_Ekind (New_F, E_Constant);
5986 else
5987 Set_Ekind (New_F, E_Variable);
5988 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5989 end if;
5991 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5993 Renamed_Formal :=
5994 Make_Selected_Component (Loc,
5995 Prefix =>
5996 Unchecked_Convert_To (
5997 Entry_Parameters_Type (Ent),
5998 New_Reference_To (Ann, Loc)),
5999 Selector_Name =>
6000 New_Reference_To (Comp, Loc));
6002 Decl :=
6003 Build_Renamed_Formal_Declaration
6004 (New_F, Formal, Comp, Renamed_Formal);
6006 if No (Declarations (N)) then
6007 Set_Declarations (N, New_List);
6008 end if;
6010 Append (Decl, Declarations (N));
6011 Set_Renamed_Object (Formal, New_F);
6012 Next_Formal (Formal);
6013 end loop;
6015 End_Scope;
6016 end;
6017 end if;
6018 end if;
6019 end Expand_Accept_Declarations;
6021 ---------------------------------------------
6022 -- Expand_Access_Protected_Subprogram_Type --
6023 ---------------------------------------------
6025 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6026 Loc : constant Source_Ptr := Sloc (N);
6027 Comps : List_Id;
6028 T : constant Entity_Id := Defining_Identifier (N);
6029 D_T : constant Entity_Id := Designated_Type (T);
6030 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6031 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6032 P_List : constant List_Id := Build_Protected_Spec
6033 (N, RTE (RE_Address), D_T, False);
6034 Decl1 : Node_Id;
6035 Decl2 : Node_Id;
6036 Def1 : Node_Id;
6038 begin
6039 -- Create access to subprogram with full signature
6041 if Etype (D_T) /= Standard_Void_Type then
6042 Def1 :=
6043 Make_Access_Function_Definition (Loc,
6044 Parameter_Specifications => P_List,
6045 Result_Definition =>
6046 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6048 else
6049 Def1 :=
6050 Make_Access_Procedure_Definition (Loc,
6051 Parameter_Specifications => P_List);
6052 end if;
6054 Decl1 :=
6055 Make_Full_Type_Declaration (Loc,
6056 Defining_Identifier => D_T2,
6057 Type_Definition => Def1);
6059 Insert_After_And_Analyze (N, Decl1);
6061 -- Associate the access to subprogram with its original access to
6062 -- protected subprogram type. Needed by the backend to know that this
6063 -- type corresponds with an access to protected subprogram type.
6065 Set_Original_Access_Type (D_T2, T);
6067 -- Create Equivalent_Type, a record with two components for an access to
6068 -- object and an access to subprogram.
6070 Comps := New_List (
6071 Make_Component_Declaration (Loc,
6072 Defining_Identifier => Make_Temporary (Loc, 'P'),
6073 Component_Definition =>
6074 Make_Component_Definition (Loc,
6075 Aliased_Present => False,
6076 Subtype_Indication =>
6077 New_Occurrence_Of (RTE (RE_Address), Loc))),
6079 Make_Component_Declaration (Loc,
6080 Defining_Identifier => Make_Temporary (Loc, 'S'),
6081 Component_Definition =>
6082 Make_Component_Definition (Loc,
6083 Aliased_Present => False,
6084 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6086 Decl2 :=
6087 Make_Full_Type_Declaration (Loc,
6088 Defining_Identifier => E_T,
6089 Type_Definition =>
6090 Make_Record_Definition (Loc,
6091 Component_List =>
6092 Make_Component_List (Loc, Component_Items => Comps)));
6094 Insert_After_And_Analyze (Decl1, Decl2);
6095 Set_Equivalent_Type (T, E_T);
6096 end Expand_Access_Protected_Subprogram_Type;
6098 --------------------------
6099 -- Expand_Entry_Barrier --
6100 --------------------------
6102 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6103 Cond : constant Node_Id :=
6104 Condition (Entry_Body_Formal_Part (N));
6105 Prot : constant Entity_Id := Scope (Ent);
6106 Spec_Decl : constant Node_Id := Parent (Prot);
6107 Func : Node_Id;
6108 B_F : Node_Id;
6109 Body_Decl : Node_Id;
6111 begin
6112 if No_Run_Time_Mode then
6113 Error_Msg_CRT ("entry barrier", N);
6114 return;
6115 end if;
6117 -- The body of the entry barrier must be analyzed in the context of the
6118 -- protected object, but its scope is external to it, just as any other
6119 -- unprotected version of a protected operation. The specification has
6120 -- been produced when the protected type declaration was elaborated. We
6121 -- build the body, insert it in the enclosing scope, but analyze it in
6122 -- the current context. A more uniform approach would be to treat the
6123 -- barrier just as a protected function, and discard the protected
6124 -- version of it because it is never called.
6126 if Full_Expander_Active then
6127 B_F := Build_Barrier_Function (N, Ent, Prot);
6128 Func := Barrier_Function (Ent);
6129 Set_Corresponding_Spec (B_F, Func);
6131 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6133 if Nkind (Parent (Body_Decl)) = N_Subunit then
6134 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6135 end if;
6137 Insert_Before_And_Analyze (Body_Decl, B_F);
6139 Set_Discriminals (Spec_Decl);
6140 Set_Scope (Func, Scope (Prot));
6142 else
6143 Analyze_And_Resolve (Cond, Any_Boolean);
6144 end if;
6146 -- The Ravenscar profile restricts barriers to simple variables declared
6147 -- within the protected object. We also allow Boolean constants, since
6148 -- these appear in several published examples and are also allowed by
6149 -- the Aonix compiler.
6151 -- Note that after analysis variables in this context will be replaced
6152 -- by the corresponding prival, that is to say a renaming of a selected
6153 -- component of the form _Object.Var. If expansion is disabled, as
6154 -- within a generic, we check that the entity appears in the current
6155 -- scope.
6157 if Is_Entity_Name (Cond) then
6159 -- A small optimization of useless renamings. If the scope of the
6160 -- entity of the condition is not the barrier function, then the
6161 -- condition does not reference any of the generated renamings
6162 -- within the function.
6164 if Full_Expander_Active and then Scope (Entity (Cond)) /= Func then
6165 Set_Declarations (B_F, Empty_List);
6166 end if;
6168 if Entity (Cond) = Standard_False
6169 or else
6170 Entity (Cond) = Standard_True
6171 then
6172 return;
6174 elsif not Expander_Active
6175 and then Scope (Entity (Cond)) = Current_Scope
6176 then
6177 return;
6179 -- Check for case of _object.all.field (note that the explicit
6180 -- dereference gets inserted by analyze/expand of _object.field)
6182 elsif Present (Renamed_Object (Entity (Cond)))
6183 and then
6184 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
6185 and then
6186 Chars
6187 (Prefix
6188 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
6189 then
6190 return;
6191 end if;
6192 end if;
6194 -- It is not a boolean variable or literal, so check the restriction
6196 Check_Restriction (Simple_Barriers, Cond);
6197 end Expand_Entry_Barrier;
6199 ------------------------------
6200 -- Expand_N_Abort_Statement --
6201 ------------------------------
6203 -- Expand abort T1, T2, .. Tn; into:
6204 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6206 procedure Expand_N_Abort_Statement (N : Node_Id) is
6207 Loc : constant Source_Ptr := Sloc (N);
6208 Tlist : constant List_Id := Names (N);
6209 Count : Nat;
6210 Aggr : Node_Id;
6211 Tasknm : Node_Id;
6213 begin
6214 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6215 Count := 0;
6217 Tasknm := First (Tlist);
6219 while Present (Tasknm) loop
6220 Count := Count + 1;
6222 -- A task interface class-wide type object is being aborted.
6223 -- Retrieve its _task_id by calling a dispatching routine.
6225 if Ada_Version >= Ada_2005
6226 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6227 and then Is_Interface (Etype (Tasknm))
6228 and then Is_Task_Interface (Etype (Tasknm))
6229 then
6230 Append_To (Component_Associations (Aggr),
6231 Make_Component_Association (Loc,
6232 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6233 Expression =>
6235 -- Task_Id (Tasknm._disp_get_task_id)
6237 Make_Unchecked_Type_Conversion (Loc,
6238 Subtype_Mark =>
6239 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
6240 Expression =>
6241 Make_Selected_Component (Loc,
6242 Prefix => New_Copy_Tree (Tasknm),
6243 Selector_Name =>
6244 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6246 else
6247 Append_To (Component_Associations (Aggr),
6248 Make_Component_Association (Loc,
6249 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6250 Expression => Concurrent_Ref (Tasknm)));
6251 end if;
6253 Next (Tasknm);
6254 end loop;
6256 Rewrite (N,
6257 Make_Procedure_Call_Statement (Loc,
6258 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
6259 Parameter_Associations => New_List (
6260 Make_Qualified_Expression (Loc,
6261 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
6262 Expression => Aggr))));
6264 Analyze (N);
6265 end Expand_N_Abort_Statement;
6267 -------------------------------
6268 -- Expand_N_Accept_Statement --
6269 -------------------------------
6271 -- This procedure handles expansion of accept statements that stand
6272 -- alone, i.e. they are not part of an accept alternative. The expansion
6273 -- of accept statement in accept alternatives is handled by the routines
6274 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6275 -- following description applies only to stand alone accept statements.
6277 -- If there is no handled statement sequence, or only null statements,
6278 -- then this is called a trivial accept, and the expansion is:
6280 -- Accept_Trivial (entry-index)
6282 -- If there is a handled statement sequence, then the expansion is:
6284 -- Ann : Address;
6285 -- {Lnn : Label}
6287 -- begin
6288 -- begin
6289 -- Accept_Call (entry-index, Ann);
6290 -- Renaming_Declarations for formals
6291 -- <statement sequence from N_Accept_Statement node>
6292 -- Complete_Rendezvous;
6293 -- <<Lnn>>
6295 -- exception
6296 -- when ... =>
6297 -- <exception handler from N_Accept_Statement node>
6298 -- Complete_Rendezvous;
6299 -- when ... =>
6300 -- <exception handler from N_Accept_Statement node>
6301 -- Complete_Rendezvous;
6302 -- ...
6303 -- end;
6305 -- exception
6306 -- when all others =>
6307 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6308 -- end;
6310 -- The first three declarations were already inserted ahead of the accept
6311 -- statement by the Expand_Accept_Declarations procedure, which was called
6312 -- directly from the semantics during analysis of the accept statement,
6313 -- before analyzing its contained statements.
6315 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6316 -- from possible expansion activity (the original source of course does
6317 -- not have any declarations associated with the accept statement, since
6318 -- an accept statement has no declarative part). In particular, if the
6319 -- expander is active, the first such declaration is the declaration of
6320 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6322 -- The two blocks are merged into a single block if the inner block has
6323 -- no exception handlers, but otherwise two blocks are required, since
6324 -- exceptions might be raised in the exception handlers of the inner
6325 -- block, and Exceptional_Complete_Rendezvous must be called.
6327 procedure Expand_N_Accept_Statement (N : Node_Id) is
6328 Loc : constant Source_Ptr := Sloc (N);
6329 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6330 Ename : constant Node_Id := Entry_Direct_Name (N);
6331 Eindx : constant Node_Id := Entry_Index (N);
6332 Eent : constant Entity_Id := Entity (Ename);
6333 Acstack : constant Elist_Id := Accept_Address (Eent);
6334 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6335 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6336 Blkent : Entity_Id;
6337 Call : Node_Id;
6338 Block : Node_Id;
6340 begin
6341 -- If the accept statement is not part of a list, then its parent must
6342 -- be an accept alternative, and, as described above, we do not do any
6343 -- expansion for such accept statements at this level.
6345 if not Is_List_Member (N) then
6346 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6347 return;
6349 -- Trivial accept case (no statement sequence, or null statements).
6350 -- If the accept statement has declarations, then just insert them
6351 -- before the procedure call.
6353 elsif Trivial_Accept_OK
6354 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6355 then
6356 -- Remove declarations for renamings, because the parameter block
6357 -- will not be assigned.
6359 declare
6360 D : Node_Id;
6361 Next_D : Node_Id;
6363 begin
6364 D := First (Declarations (N));
6366 while Present (D) loop
6367 Next_D := Next (D);
6368 if Nkind (D) = N_Object_Renaming_Declaration then
6369 Remove (D);
6370 end if;
6372 D := Next_D;
6373 end loop;
6374 end;
6376 if Present (Declarations (N)) then
6377 Insert_Actions (N, Declarations (N));
6378 end if;
6380 Rewrite (N,
6381 Make_Procedure_Call_Statement (Loc,
6382 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
6383 Parameter_Associations => New_List (
6384 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6386 Analyze (N);
6388 -- Discard Entry_Address that was created for it, so it will not be
6389 -- emitted if this accept statement is in the statement part of a
6390 -- delay alternative.
6392 if Present (Stats) then
6393 Remove_Last_Elmt (Acstack);
6394 end if;
6396 -- Case of statement sequence present
6398 else
6399 -- Construct the block, using the declarations from the accept
6400 -- statement if any to initialize the declarations of the block.
6402 Blkent := Make_Temporary (Loc, 'A');
6403 Set_Ekind (Blkent, E_Block);
6404 Set_Etype (Blkent, Standard_Void_Type);
6405 Set_Scope (Blkent, Current_Scope);
6407 Block :=
6408 Make_Block_Statement (Loc,
6409 Identifier => New_Reference_To (Blkent, Loc),
6410 Declarations => Declarations (N),
6411 Handled_Statement_Sequence => Build_Accept_Body (N));
6413 -- For the analysis of the generated declarations, the parent node
6414 -- must be properly set.
6416 Set_Parent (Block, Parent (N));
6418 -- Prepend call to Accept_Call to main statement sequence If the
6419 -- accept has exception handlers, the statement sequence is wrapped
6420 -- in a block. Insert call and renaming declarations in the
6421 -- declarations of the block, so they are elaborated before the
6422 -- handlers.
6424 Call :=
6425 Make_Procedure_Call_Statement (Loc,
6426 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
6427 Parameter_Associations => New_List (
6428 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6429 New_Reference_To (Ann, Loc)));
6431 if Parent (Stats) = N then
6432 Prepend (Call, Statements (Stats));
6433 else
6434 Set_Declarations (Parent (Stats), New_List (Call));
6435 end if;
6437 Analyze (Call);
6439 Push_Scope (Blkent);
6441 declare
6442 D : Node_Id;
6443 Next_D : Node_Id;
6444 Typ : Entity_Id;
6446 begin
6447 D := First (Declarations (N));
6448 while Present (D) loop
6449 Next_D := Next (D);
6451 if Nkind (D) = N_Object_Renaming_Declaration then
6453 -- The renaming declarations for the formals were created
6454 -- during analysis of the accept statement, and attached to
6455 -- the list of declarations. Place them now in the context
6456 -- of the accept block or subprogram.
6458 Remove (D);
6459 Typ := Entity (Subtype_Mark (D));
6460 Insert_After (Call, D);
6461 Analyze (D);
6463 -- If the formal is class_wide, it does not have an actual
6464 -- subtype. The analysis of the renaming declaration creates
6465 -- one, but we need to retain the class-wide nature of the
6466 -- entity.
6468 if Is_Class_Wide_Type (Typ) then
6469 Set_Etype (Defining_Identifier (D), Typ);
6470 end if;
6472 end if;
6474 D := Next_D;
6475 end loop;
6476 end;
6478 End_Scope;
6480 -- Replace the accept statement by the new block
6482 Rewrite (N, Block);
6483 Analyze (N);
6485 -- Last step is to unstack the Accept_Address value
6487 Remove_Last_Elmt (Acstack);
6488 end if;
6489 end Expand_N_Accept_Statement;
6491 ----------------------------------
6492 -- Expand_N_Asynchronous_Select --
6493 ----------------------------------
6495 -- This procedure assumes that the trigger statement is an entry call or
6496 -- a dispatching procedure call. A delay alternative should already have
6497 -- been expanded into an entry call to the appropriate delay object Wait
6498 -- entry.
6500 -- If the trigger is a task entry call, the select is implemented with
6501 -- a Task_Entry_Call:
6503 -- declare
6504 -- B : Boolean;
6505 -- C : Boolean;
6506 -- P : parms := (parm, parm, parm);
6508 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6510 -- procedure _clean is
6511 -- begin
6512 -- ...
6513 -- Cancel_Task_Entry_Call (C);
6514 -- ...
6515 -- end _clean;
6517 -- begin
6518 -- Abort_Defer;
6519 -- Task_Entry_Call
6520 -- (<acceptor-task>, -- Acceptor
6521 -- <entry-index>, -- E
6522 -- P'Address, -- Uninterpreted_Data
6523 -- Asynchronous_Call, -- Mode
6524 -- B); -- Rendezvous_Successful
6526 -- begin
6527 -- begin
6528 -- Abort_Undefer;
6529 -- <abortable-part>
6530 -- at end
6531 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6532 -- end;
6533 -- exception
6534 -- when Abort_Signal => Abort_Undefer;
6535 -- end;
6537 -- parm := P.param;
6538 -- parm := P.param;
6539 -- ...
6540 -- if not C then
6541 -- <triggered-statements>
6542 -- end if;
6543 -- end;
6545 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6546 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6547 -- as follows:
6549 -- declare
6550 -- P : parms := (parm, parm, parm);
6551 -- begin
6552 -- Call_Simple (acceptor-task, entry-index, P'Address);
6553 -- parm := P.param;
6554 -- parm := P.param;
6555 -- ...
6556 -- end;
6558 -- so the task at hand is to convert the latter expansion into the former
6560 -- If the trigger is a protected entry call, the select is implemented
6561 -- with Protected_Entry_Call:
6563 -- declare
6564 -- P : E1_Params := (param, param, param);
6565 -- Bnn : Communications_Block;
6567 -- begin
6568 -- declare
6570 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6572 -- procedure _clean is
6573 -- begin
6574 -- ...
6575 -- if Enqueued (Bnn) then
6576 -- Cancel_Protected_Entry_Call (Bnn);
6577 -- end if;
6578 -- ...
6579 -- end _clean;
6581 -- begin
6582 -- begin
6583 -- Protected_Entry_Call
6584 -- (po._object'Access, -- Object
6585 -- <entry index>, -- E
6586 -- P'Address, -- Uninterpreted_Data
6587 -- Asynchronous_Call, -- Mode
6588 -- Bnn); -- Block
6590 -- if Enqueued (Bnn) then
6591 -- <abortable-part>
6592 -- end if;
6593 -- at end
6594 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6595 -- end;
6596 -- exception
6597 -- when Abort_Signal => Abort_Undefer;
6598 -- end;
6600 -- if not Cancelled (Bnn) then
6601 -- <triggered-statements>
6602 -- end if;
6603 -- end;
6605 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6606 -- entry call:
6608 -- declare
6609 -- P : E1_Params := (param, param, param);
6610 -- Bnn : Communications_Block;
6612 -- begin
6613 -- Protected_Entry_Call
6614 -- (po._object'Access, -- Object
6615 -- <entry index>, -- E
6616 -- P'Address, -- Uninterpreted_Data
6617 -- Simple_Call, -- Mode
6618 -- Bnn); -- Block
6619 -- parm := P.param;
6620 -- parm := P.param;
6621 -- ...
6622 -- end;
6624 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6625 -- expanded into:
6627 -- declare
6628 -- B : Boolean := False;
6629 -- Bnn : Communication_Block;
6630 -- C : Ada.Tags.Prim_Op_Kind;
6631 -- D : System.Storage_Elements.Dummy_Communication_Block;
6632 -- K : Ada.Tags.Tagged_Kind :=
6633 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6634 -- P : Parameters := (Param1 .. ParamN);
6635 -- S : Integer;
6636 -- U : Boolean;
6638 -- begin
6639 -- if K = Ada.Tags.TK_Limited_Tagged
6640 -- or else K = Ada.Tags.TK_Tagged
6641 -- then
6642 -- <dispatching-call>;
6643 -- <triggering-statements>;
6645 -- else
6646 -- S :=
6647 -- Ada.Tags.Get_Offset_Index
6648 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6650 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6652 -- if C = POK_Protected_Entry then
6653 -- declare
6654 -- procedure _clean is
6655 -- begin
6656 -- if Enqueued (Bnn) then
6657 -- Cancel_Protected_Entry_Call (Bnn);
6658 -- end if;
6659 -- end _clean;
6661 -- begin
6662 -- begin
6663 -- _Disp_Asynchronous_Select
6664 -- (<object>, S, P'Address, D, B);
6665 -- Bnn := Communication_Block (D);
6667 -- Param1 := P.Param1;
6668 -- ...
6669 -- ParamN := P.ParamN;
6671 -- if Enqueued (Bnn) then
6672 -- <abortable-statements>
6673 -- end if;
6674 -- at end
6675 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6676 -- end;
6677 -- exception
6678 -- when Abort_Signal => Abort_Undefer;
6679 -- end;
6681 -- if not Cancelled (Bnn) then
6682 -- <triggering-statements>
6683 -- end if;
6685 -- elsif C = POK_Task_Entry then
6686 -- declare
6687 -- procedure _clean is
6688 -- begin
6689 -- Cancel_Task_Entry_Call (U);
6690 -- end _clean;
6692 -- begin
6693 -- Abort_Defer;
6695 -- _Disp_Asynchronous_Select
6696 -- (<object>, S, P'Address, D, B);
6697 -- Bnn := Communication_Bloc (D);
6699 -- Param1 := P.Param1;
6700 -- ...
6701 -- ParamN := P.ParamN;
6703 -- begin
6704 -- begin
6705 -- Abort_Undefer;
6706 -- <abortable-statements>
6707 -- at end
6708 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6709 -- end;
6710 -- exception
6711 -- when Abort_Signal => Abort_Undefer;
6712 -- end;
6714 -- if not U then
6715 -- <triggering-statements>
6716 -- end if;
6717 -- end;
6719 -- else
6720 -- <dispatching-call>;
6721 -- <triggering-statements>
6722 -- end if;
6723 -- end if;
6724 -- end;
6726 -- The job is to convert this to the asynchronous form
6728 -- If the trigger is a delay statement, it will have been expanded into a
6729 -- call to one of the GNARL delay procedures. This routine will convert
6730 -- this into a protected entry call on a delay object and then continue
6731 -- processing as for a protected entry call trigger. This requires
6732 -- declaring a Delay_Block object and adding a pointer to this object to
6733 -- the parameter list of the delay procedure to form the parameter list of
6734 -- the entry call. This object is used by the runtime to queue the delay
6735 -- request.
6737 -- For a description of the use of P and the assignments after the call,
6738 -- see Expand_N_Entry_Call_Statement.
6740 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6741 Loc : constant Source_Ptr := Sloc (N);
6742 Abrt : constant Node_Id := Abortable_Part (N);
6743 Trig : constant Node_Id := Triggering_Alternative (N);
6745 Abort_Block_Ent : Entity_Id;
6746 Abortable_Block : Node_Id;
6747 Actuals : List_Id;
6748 Astats : List_Id;
6749 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6750 Blk_Typ : Entity_Id;
6751 Call : Node_Id;
6752 Call_Ent : Entity_Id;
6753 Cancel_Param : Entity_Id;
6754 Cleanup_Block : Node_Id;
6755 Cleanup_Block_Ent : Entity_Id;
6756 Cleanup_Stmts : List_Id;
6757 Conc_Typ_Stmts : List_Id;
6758 Concval : Node_Id;
6759 Dblock_Ent : Entity_Id;
6760 Decl : Node_Id;
6761 Decls : List_Id;
6762 Ecall : Node_Id;
6763 Ename : Node_Id;
6764 Enqueue_Call : Node_Id;
6765 Formals : List_Id;
6766 Hdle : List_Id;
6767 Handler_Stmt : Node_Id;
6768 Index : Node_Id;
6769 Lim_Typ_Stmts : List_Id;
6770 N_Orig : Node_Id;
6771 Obj : Entity_Id;
6772 Param : Node_Id;
6773 Params : List_Id;
6774 Pdef : Entity_Id;
6775 ProtE_Stmts : List_Id;
6776 ProtP_Stmts : List_Id;
6777 Stmt : Node_Id;
6778 Stmts : List_Id;
6779 TaskE_Stmts : List_Id;
6780 Tstats : List_Id;
6782 B : Entity_Id; -- Call status flag
6783 Bnn : Entity_Id; -- Communication block
6784 C : Entity_Id; -- Call kind
6785 K : Entity_Id; -- Tagged kind
6786 P : Entity_Id; -- Parameter block
6787 S : Entity_Id; -- Primitive operation slot
6788 T : Entity_Id; -- Additional status flag
6790 procedure Rewrite_Abortable_Part;
6791 -- If the trigger is a dispatching call, the expansion inserts multiple
6792 -- copies of the abortable part. This is both inefficient, and may lead
6793 -- to duplicate definitions that the back-end will reject, when the
6794 -- abortable part includes loops. This procedure rewrites the abortable
6795 -- part into a call to a generated procedure.
6797 ----------------------------
6798 -- Rewrite_Abortable_Part --
6799 ----------------------------
6801 procedure Rewrite_Abortable_Part is
6802 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6803 Decl : Node_Id;
6805 begin
6806 Decl :=
6807 Make_Subprogram_Body (Loc,
6808 Specification =>
6809 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6810 Declarations => New_List,
6811 Handled_Statement_Sequence =>
6812 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6813 Insert_Before (N, Decl);
6814 Analyze (Decl);
6816 -- Rewrite abortable part into a call to this procedure.
6818 Astats :=
6819 New_List (
6820 Make_Procedure_Call_Statement (Loc,
6821 Name => New_Occurrence_Of (Proc, Loc)));
6822 end Rewrite_Abortable_Part;
6824 begin
6825 Process_Statements_For_Controlled_Objects (Trig);
6826 Process_Statements_For_Controlled_Objects (Abrt);
6828 Ecall := Triggering_Statement (Trig);
6830 Ensure_Statement_Present (Sloc (Ecall), Trig);
6832 -- Retrieve Astats and Tstats now because the finalization machinery may
6833 -- wrap them in blocks.
6835 Astats := Statements (Abrt);
6836 Tstats := Statements (Trig);
6838 -- The arguments in the call may require dynamic allocation, and the
6839 -- call statement may have been transformed into a block. The block
6840 -- may contain additional declarations for internal entities, and the
6841 -- original call is found by sequential search.
6843 if Nkind (Ecall) = N_Block_Statement then
6844 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6845 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6846 N_Entry_Call_Statement)
6847 loop
6848 Next (Ecall);
6849 end loop;
6850 end if;
6852 -- This is either a dispatching call or a delay statement used as a
6853 -- trigger which was expanded into a procedure call.
6855 if Nkind (Ecall) = N_Procedure_Call_Statement then
6856 if Ada_Version >= Ada_2005
6857 and then
6858 (No (Original_Node (Ecall))
6859 or else not Nkind_In (Original_Node (Ecall),
6860 N_Delay_Relative_Statement,
6861 N_Delay_Until_Statement))
6862 then
6863 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6865 Rewrite_Abortable_Part;
6866 Decls := New_List;
6867 Stmts := New_List;
6869 -- Call status flag processing, generate:
6870 -- B : Boolean := False;
6872 B := Build_B (Loc, Decls);
6874 -- Communication block processing, generate:
6875 -- Bnn : Communication_Block;
6877 Bnn := Make_Temporary (Loc, 'B');
6878 Append_To (Decls,
6879 Make_Object_Declaration (Loc,
6880 Defining_Identifier => Bnn,
6881 Object_Definition =>
6882 New_Reference_To (RTE (RE_Communication_Block), Loc)));
6884 -- Call kind processing, generate:
6885 -- C : Ada.Tags.Prim_Op_Kind;
6887 C := Build_C (Loc, Decls);
6889 -- Tagged kind processing, generate:
6890 -- K : Ada.Tags.Tagged_Kind :=
6891 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6893 -- Dummy communication block, generate:
6894 -- D : Dummy_Communication_Block;
6896 Append_To (Decls,
6897 Make_Object_Declaration (Loc,
6898 Defining_Identifier =>
6899 Make_Defining_Identifier (Loc, Name_uD),
6900 Object_Definition =>
6901 New_Reference_To
6902 (RTE (RE_Dummy_Communication_Block), Loc)));
6904 K := Build_K (Loc, Decls, Obj);
6906 -- Parameter block processing
6908 Blk_Typ := Build_Parameter_Block
6909 (Loc, Actuals, Formals, Decls);
6910 P := Parameter_Block_Pack
6911 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6913 -- Dispatch table slot processing, generate:
6914 -- S : Integer;
6916 S := Build_S (Loc, Decls);
6918 -- Additional status flag processing, generate:
6919 -- Tnn : Boolean;
6921 T := Make_Temporary (Loc, 'T');
6922 Append_To (Decls,
6923 Make_Object_Declaration (Loc,
6924 Defining_Identifier => T,
6925 Object_Definition =>
6926 New_Reference_To (Standard_Boolean, Loc)));
6928 ------------------------------
6929 -- Protected entry handling --
6930 ------------------------------
6932 -- Generate:
6933 -- Param1 := P.Param1;
6934 -- ...
6935 -- ParamN := P.ParamN;
6937 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6939 -- Generate:
6940 -- Bnn := Communication_Block (D);
6942 Prepend_To (Cleanup_Stmts,
6943 Make_Assignment_Statement (Loc,
6944 Name => New_Reference_To (Bnn, Loc),
6945 Expression =>
6946 Make_Unchecked_Type_Conversion (Loc,
6947 Subtype_Mark =>
6948 New_Reference_To (RTE (RE_Communication_Block), Loc),
6949 Expression => Make_Identifier (Loc, Name_uD))));
6951 -- Generate:
6952 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6954 Prepend_To (Cleanup_Stmts,
6955 Make_Procedure_Call_Statement (Loc,
6956 Name =>
6957 New_Reference_To
6958 (Find_Prim_Op
6959 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
6960 Loc),
6961 Parameter_Associations =>
6962 New_List (
6963 New_Copy_Tree (Obj), -- <object>
6964 New_Reference_To (S, Loc), -- S
6965 Make_Attribute_Reference (Loc, -- P'Address
6966 Prefix => New_Reference_To (P, Loc),
6967 Attribute_Name => Name_Address),
6968 Make_Identifier (Loc, Name_uD), -- D
6969 New_Reference_To (B, Loc)))); -- B
6971 -- Generate:
6972 -- if Enqueued (Bnn) then
6973 -- <abortable-statements>
6974 -- end if;
6976 Append_To (Cleanup_Stmts,
6977 Make_Implicit_If_Statement (N,
6978 Condition =>
6979 Make_Function_Call (Loc,
6980 Name =>
6981 New_Reference_To (RTE (RE_Enqueued), Loc),
6982 Parameter_Associations =>
6983 New_List (New_Reference_To (Bnn, Loc))),
6985 Then_Statements =>
6986 New_Copy_List_Tree (Astats)));
6988 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6989 -- will then generate a _clean for the communication block Bnn.
6991 -- Generate:
6992 -- declare
6993 -- procedure _clean is
6994 -- begin
6995 -- if Enqueued (Bnn) then
6996 -- Cancel_Protected_Entry_Call (Bnn);
6997 -- end if;
6998 -- end _clean;
6999 -- begin
7000 -- Cleanup_Stmts
7001 -- at end
7002 -- _clean;
7003 -- end;
7005 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7006 Cleanup_Block :=
7007 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7009 -- Wrap the cleanup block in an exception handling block
7011 -- Generate:
7012 -- begin
7013 -- Cleanup_Block
7014 -- exception
7015 -- when Abort_Signal => Abort_Undefer;
7016 -- end;
7018 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7019 ProtE_Stmts :=
7020 New_List (
7021 Make_Implicit_Label_Declaration (Loc,
7022 Defining_Identifier => Abort_Block_Ent),
7024 Build_Abort_Block
7025 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7027 -- Generate:
7028 -- if not Cancelled (Bnn) then
7029 -- <triggering-statements>
7030 -- end if;
7032 Append_To (ProtE_Stmts,
7033 Make_Implicit_If_Statement (N,
7034 Condition =>
7035 Make_Op_Not (Loc,
7036 Right_Opnd =>
7037 Make_Function_Call (Loc,
7038 Name =>
7039 New_Reference_To (RTE (RE_Cancelled), Loc),
7040 Parameter_Associations =>
7041 New_List (New_Reference_To (Bnn, Loc)))),
7043 Then_Statements =>
7044 New_Copy_List_Tree (Tstats)));
7046 -------------------------
7047 -- Task entry handling --
7048 -------------------------
7050 -- Generate:
7051 -- Param1 := P.Param1;
7052 -- ...
7053 -- ParamN := P.ParamN;
7055 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7057 -- Generate:
7058 -- Bnn := Communication_Block (D);
7060 Append_To (TaskE_Stmts,
7061 Make_Assignment_Statement (Loc,
7062 Name =>
7063 New_Reference_To (Bnn, Loc),
7064 Expression =>
7065 Make_Unchecked_Type_Conversion (Loc,
7066 Subtype_Mark =>
7067 New_Reference_To (RTE (RE_Communication_Block), Loc),
7068 Expression => Make_Identifier (Loc, Name_uD))));
7070 -- Generate:
7071 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7073 Prepend_To (TaskE_Stmts,
7074 Make_Procedure_Call_Statement (Loc,
7075 Name =>
7076 New_Reference_To (
7077 Find_Prim_Op (Etype (Etype (Obj)),
7078 Name_uDisp_Asynchronous_Select),
7079 Loc),
7081 Parameter_Associations =>
7082 New_List (
7083 New_Copy_Tree (Obj), -- <object>
7084 New_Reference_To (S, Loc), -- S
7085 Make_Attribute_Reference (Loc, -- P'Address
7086 Prefix => New_Reference_To (P, Loc),
7087 Attribute_Name => Name_Address),
7088 Make_Identifier (Loc, Name_uD), -- D
7089 New_Reference_To (B, Loc)))); -- B
7091 -- Generate:
7092 -- Abort_Defer;
7094 Prepend_To (TaskE_Stmts,
7095 Make_Procedure_Call_Statement (Loc,
7096 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7097 Parameter_Associations => No_List));
7099 -- Generate:
7100 -- Abort_Undefer;
7101 -- <abortable-statements>
7103 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7105 Prepend_To (Cleanup_Stmts,
7106 Make_Procedure_Call_Statement (Loc,
7107 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7108 Parameter_Associations => No_List));
7110 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7111 -- will generate a _clean for the additional status flag.
7113 -- Generate:
7114 -- declare
7115 -- procedure _clean is
7116 -- begin
7117 -- Cancel_Task_Entry_Call (U);
7118 -- end _clean;
7119 -- begin
7120 -- Cleanup_Stmts
7121 -- at end
7122 -- _clean;
7123 -- end;
7125 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7126 Cleanup_Block :=
7127 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7129 -- Wrap the cleanup block in an exception handling block
7131 -- Generate:
7132 -- begin
7133 -- Cleanup_Block
7134 -- exception
7135 -- when Abort_Signal => Abort_Undefer;
7136 -- end;
7138 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7140 Append_To (TaskE_Stmts,
7141 Make_Implicit_Label_Declaration (Loc,
7142 Defining_Identifier => Abort_Block_Ent));
7144 Append_To (TaskE_Stmts,
7145 Build_Abort_Block
7146 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7148 -- Generate:
7149 -- if not T then
7150 -- <triggering-statements>
7151 -- end if;
7153 Append_To (TaskE_Stmts,
7154 Make_Implicit_If_Statement (N,
7155 Condition =>
7156 Make_Op_Not (Loc, Right_Opnd => New_Reference_To (T, Loc)),
7158 Then_Statements =>
7159 New_Copy_List_Tree (Tstats)));
7161 ----------------------------------
7162 -- Protected procedure handling --
7163 ----------------------------------
7165 -- Generate:
7166 -- <dispatching-call>;
7167 -- <triggering-statements>
7169 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7170 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7172 -- Generate:
7173 -- S := Ada.Tags.Get_Offset_Index
7174 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7176 Conc_Typ_Stmts :=
7177 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7179 -- Generate:
7180 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7182 Append_To (Conc_Typ_Stmts,
7183 Make_Procedure_Call_Statement (Loc,
7184 Name =>
7185 New_Reference_To
7186 (Find_Prim_Op (Etype (Etype (Obj)),
7187 Name_uDisp_Get_Prim_Op_Kind),
7188 Loc),
7189 Parameter_Associations =>
7190 New_List (
7191 New_Copy_Tree (Obj),
7192 New_Reference_To (S, Loc),
7193 New_Reference_To (C, Loc))));
7195 -- Generate:
7196 -- if C = POK_Procedure_Entry then
7197 -- ProtE_Stmts
7198 -- elsif C = POK_Task_Entry then
7199 -- TaskE_Stmts
7200 -- else
7201 -- ProtP_Stmts
7202 -- end if;
7204 Append_To (Conc_Typ_Stmts,
7205 Make_Implicit_If_Statement (N,
7206 Condition =>
7207 Make_Op_Eq (Loc,
7208 Left_Opnd =>
7209 New_Reference_To (C, Loc),
7210 Right_Opnd =>
7211 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
7213 Then_Statements =>
7214 ProtE_Stmts,
7216 Elsif_Parts =>
7217 New_List (
7218 Make_Elsif_Part (Loc,
7219 Condition =>
7220 Make_Op_Eq (Loc,
7221 Left_Opnd =>
7222 New_Reference_To (C, Loc),
7223 Right_Opnd =>
7224 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
7226 Then_Statements =>
7227 TaskE_Stmts)),
7229 Else_Statements =>
7230 ProtP_Stmts));
7232 -- Generate:
7233 -- <dispatching-call>;
7234 -- <triggering-statements>
7236 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7237 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7239 -- Generate:
7240 -- if K = Ada.Tags.TK_Limited_Tagged
7241 -- or else K = Ada.Tags.TK_Tagged
7242 -- then
7243 -- Lim_Typ_Stmts
7244 -- else
7245 -- Conc_Typ_Stmts
7246 -- end if;
7248 Append_To (Stmts,
7249 Make_Implicit_If_Statement (N,
7250 Condition => Build_Dispatching_Tag_Check (K, N),
7251 Then_Statements => Lim_Typ_Stmts,
7252 Else_Statements => Conc_Typ_Stmts));
7254 Rewrite (N,
7255 Make_Block_Statement (Loc,
7256 Declarations =>
7257 Decls,
7258 Handled_Statement_Sequence =>
7259 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7261 Analyze (N);
7262 return;
7264 -- Delay triggering statement processing
7266 else
7267 -- Add a Delay_Block object to the parameter list of the delay
7268 -- procedure to form the parameter list of the Wait entry call.
7270 Dblock_Ent := Make_Temporary (Loc, 'D');
7272 Pdef := Entity (Name (Ecall));
7274 if Is_RTE (Pdef, RO_CA_Delay_For) then
7275 Enqueue_Call :=
7276 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
7278 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7279 Enqueue_Call :=
7280 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
7282 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7283 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
7284 end if;
7286 Append_To (Parameter_Associations (Ecall),
7287 Make_Attribute_Reference (Loc,
7288 Prefix => New_Reference_To (Dblock_Ent, Loc),
7289 Attribute_Name => Name_Unchecked_Access));
7291 -- Create the inner block to protect the abortable part
7293 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7295 Prepend_To (Astats,
7296 Make_Procedure_Call_Statement (Loc,
7297 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7299 Abortable_Block :=
7300 Make_Block_Statement (Loc,
7301 Identifier => New_Reference_To (Blk_Ent, Loc),
7302 Handled_Statement_Sequence =>
7303 Make_Handled_Sequence_Of_Statements (Loc,
7304 Statements => Astats),
7305 Has_Created_Identifier => True,
7306 Is_Asynchronous_Call_Block => True);
7308 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7310 Rewrite (Ecall,
7311 Make_Implicit_If_Statement (N,
7312 Condition =>
7313 Make_Function_Call (Loc,
7314 Name => Enqueue_Call,
7315 Parameter_Associations => Parameter_Associations (Ecall)),
7316 Then_Statements =>
7317 New_List (Make_Block_Statement (Loc,
7318 Handled_Statement_Sequence =>
7319 Make_Handled_Sequence_Of_Statements (Loc,
7320 Statements => New_List (
7321 Make_Implicit_Label_Declaration (Loc,
7322 Defining_Identifier => Blk_Ent,
7323 Label_Construct => Abortable_Block),
7324 Abortable_Block),
7325 Exception_Handlers => Hdle)))));
7327 Stmts := New_List (Ecall);
7329 -- Construct statement sequence for new block
7331 Append_To (Stmts,
7332 Make_Implicit_If_Statement (N,
7333 Condition =>
7334 Make_Function_Call (Loc,
7335 Name => New_Reference_To (
7336 RTE (RE_Timed_Out), Loc),
7337 Parameter_Associations => New_List (
7338 Make_Attribute_Reference (Loc,
7339 Prefix => New_Reference_To (Dblock_Ent, Loc),
7340 Attribute_Name => Name_Unchecked_Access))),
7341 Then_Statements => Tstats));
7343 -- The result is the new block
7345 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7347 Rewrite (N,
7348 Make_Block_Statement (Loc,
7349 Declarations => New_List (
7350 Make_Object_Declaration (Loc,
7351 Defining_Identifier => Dblock_Ent,
7352 Aliased_Present => True,
7353 Object_Definition =>
7354 New_Reference_To (RTE (RE_Delay_Block), Loc))),
7356 Handled_Statement_Sequence =>
7357 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7359 Analyze (N);
7360 return;
7361 end if;
7363 else
7364 N_Orig := N;
7365 end if;
7367 Extract_Entry (Ecall, Concval, Ename, Index);
7368 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7370 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7371 Decls := Declarations (Ecall);
7373 if Is_Protected_Type (Etype (Concval)) then
7375 -- Get the declarations of the block expanded from the entry call
7377 Decl := First (Decls);
7378 while Present (Decl)
7379 and then (Nkind (Decl) /= N_Object_Declaration
7380 or else not Is_RTE (Etype (Object_Definition (Decl)),
7381 RE_Communication_Block))
7382 loop
7383 Next (Decl);
7384 end loop;
7386 pragma Assert (Present (Decl));
7387 Cancel_Param := Defining_Identifier (Decl);
7389 -- Change the mode of the Protected_Entry_Call call
7391 -- Protected_Entry_Call (
7392 -- Object => po._object'Access,
7393 -- E => <entry index>;
7394 -- Uninterpreted_Data => P'Address;
7395 -- Mode => Asynchronous_Call;
7396 -- Block => Bnn);
7398 -- Skip assignments to temporaries created for in-out parameters
7400 -- This makes unwarranted assumptions about the shape of the expanded
7401 -- tree for the call, and should be cleaned up ???
7403 Stmt := First (Stmts);
7404 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7405 Next (Stmt);
7406 end loop;
7408 Call := Stmt;
7410 Param := First (Parameter_Associations (Call));
7411 while Present (Param)
7412 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7413 loop
7414 Next (Param);
7415 end loop;
7417 pragma Assert (Present (Param));
7418 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7419 Analyze (Param);
7421 -- Append an if statement to execute the abortable part
7423 -- Generate:
7424 -- if Enqueued (Bnn) then
7426 Append_To (Stmts,
7427 Make_Implicit_If_Statement (N,
7428 Condition =>
7429 Make_Function_Call (Loc,
7430 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
7431 Parameter_Associations => New_List (
7432 New_Reference_To (Cancel_Param, Loc))),
7433 Then_Statements => Astats));
7435 Abortable_Block :=
7436 Make_Block_Statement (Loc,
7437 Identifier => New_Reference_To (Blk_Ent, Loc),
7438 Handled_Statement_Sequence =>
7439 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7440 Has_Created_Identifier => True,
7441 Is_Asynchronous_Call_Block => True);
7443 -- For the VM call Update_Exception instead of Abort_Undefer.
7444 -- See 4jexcept.ads for an explanation.
7446 if VM_Target = No_VM then
7447 if Exception_Mechanism = Back_End_Exceptions then
7449 -- Aborts are not deferred at beginning of exception handlers
7450 -- in ZCX.
7452 Handler_Stmt := Make_Null_Statement (Loc);
7454 else
7455 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7456 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7457 Parameter_Associations => No_List);
7458 end if;
7459 else
7460 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7461 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
7462 Parameter_Associations => New_List (
7463 Make_Function_Call (Loc,
7464 Name => New_Occurrence_Of
7465 (RTE (RE_Current_Target_Exception), Loc))));
7466 end if;
7468 Stmts := New_List (
7469 Make_Block_Statement (Loc,
7470 Handled_Statement_Sequence =>
7471 Make_Handled_Sequence_Of_Statements (Loc,
7472 Statements => New_List (
7473 Make_Implicit_Label_Declaration (Loc,
7474 Defining_Identifier => Blk_Ent,
7475 Label_Construct => Abortable_Block),
7476 Abortable_Block),
7478 -- exception
7480 Exception_Handlers => New_List (
7481 Make_Implicit_Exception_Handler (Loc,
7483 -- when Abort_Signal =>
7484 -- Abort_Undefer.all;
7486 Exception_Choices =>
7487 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
7488 Statements => New_List (Handler_Stmt))))),
7490 -- if not Cancelled (Bnn) then
7491 -- triggered statements
7492 -- end if;
7494 Make_Implicit_If_Statement (N,
7495 Condition => Make_Op_Not (Loc,
7496 Right_Opnd =>
7497 Make_Function_Call (Loc,
7498 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7499 Parameter_Associations => New_List (
7500 New_Occurrence_Of (Cancel_Param, Loc)))),
7501 Then_Statements => Tstats));
7503 -- Asynchronous task entry call
7505 else
7506 if No (Decls) then
7507 Decls := New_List;
7508 end if;
7510 B := Make_Defining_Identifier (Loc, Name_uB);
7512 -- Insert declaration of B in declarations of existing block
7514 Prepend_To (Decls,
7515 Make_Object_Declaration (Loc,
7516 Defining_Identifier => B,
7517 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7519 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7521 -- Insert declaration of C in declarations of existing block
7523 Prepend_To (Decls,
7524 Make_Object_Declaration (Loc,
7525 Defining_Identifier => Cancel_Param,
7526 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7528 -- Remove and save the call to Call_Simple
7530 Stmt := First (Stmts);
7532 -- Skip assignments to temporaries created for in-out parameters.
7533 -- This makes unwarranted assumptions about the shape of the expanded
7534 -- tree for the call, and should be cleaned up ???
7536 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7537 Next (Stmt);
7538 end loop;
7540 Call := Stmt;
7542 -- Create the inner block to protect the abortable part
7544 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7546 Prepend_To (Astats,
7547 Make_Procedure_Call_Statement (Loc,
7548 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7550 Abortable_Block :=
7551 Make_Block_Statement (Loc,
7552 Identifier => New_Reference_To (Blk_Ent, Loc),
7553 Handled_Statement_Sequence =>
7554 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7555 Has_Created_Identifier => True,
7556 Is_Asynchronous_Call_Block => True);
7558 Insert_After (Call,
7559 Make_Block_Statement (Loc,
7560 Handled_Statement_Sequence =>
7561 Make_Handled_Sequence_Of_Statements (Loc,
7562 Statements => New_List (
7563 Make_Implicit_Label_Declaration (Loc,
7564 Defining_Identifier => Blk_Ent,
7565 Label_Construct => Abortable_Block),
7566 Abortable_Block),
7567 Exception_Handlers => Hdle)));
7569 -- Create new call statement
7571 Params := Parameter_Associations (Call);
7573 Append_To (Params,
7574 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7575 Append_To (Params, New_Reference_To (B, Loc));
7577 Rewrite (Call,
7578 Make_Procedure_Call_Statement (Loc,
7579 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7580 Parameter_Associations => Params));
7582 -- Construct statement sequence for new block
7584 Append_To (Stmts,
7585 Make_Implicit_If_Statement (N,
7586 Condition =>
7587 Make_Op_Not (Loc, New_Reference_To (Cancel_Param, Loc)),
7588 Then_Statements => Tstats));
7590 -- Protected the call against abort
7592 Prepend_To (Stmts,
7593 Make_Procedure_Call_Statement (Loc,
7594 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7595 Parameter_Associations => Empty_List));
7596 end if;
7598 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7600 -- The result is the new block
7602 Rewrite (N_Orig,
7603 Make_Block_Statement (Loc,
7604 Declarations => Decls,
7605 Handled_Statement_Sequence =>
7606 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7608 Analyze (N_Orig);
7609 end Expand_N_Asynchronous_Select;
7611 -------------------------------------
7612 -- Expand_N_Conditional_Entry_Call --
7613 -------------------------------------
7615 -- The conditional task entry call is converted to a call to
7616 -- Task_Entry_Call:
7618 -- declare
7619 -- B : Boolean;
7620 -- P : parms := (parm, parm, parm);
7622 -- begin
7623 -- Task_Entry_Call
7624 -- (<acceptor-task>, -- Acceptor
7625 -- <entry-index>, -- E
7626 -- P'Address, -- Uninterpreted_Data
7627 -- Conditional_Call, -- Mode
7628 -- B); -- Rendezvous_Successful
7629 -- parm := P.param;
7630 -- parm := P.param;
7631 -- ...
7632 -- if B then
7633 -- normal-statements
7634 -- else
7635 -- else-statements
7636 -- end if;
7637 -- end;
7639 -- For a description of the use of P and the assignments after the call,
7640 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7641 -- conditional entry call has already been expanded (by the Expand_N_Entry
7642 -- _Call_Statement procedure) as follows:
7644 -- declare
7645 -- P : parms := (parm, parm, parm);
7646 -- begin
7647 -- ... info for in-out parameters
7648 -- Call_Simple (acceptor-task, entry-index, P'Address);
7649 -- parm := P.param;
7650 -- parm := P.param;
7651 -- ...
7652 -- end;
7654 -- so the task at hand is to convert the latter expansion into the former
7656 -- The conditional protected entry call is converted to a call to
7657 -- Protected_Entry_Call:
7659 -- declare
7660 -- P : parms := (parm, parm, parm);
7661 -- Bnn : Communications_Block;
7663 -- begin
7664 -- Protected_Entry_Call
7665 -- (po._object'Access, -- Object
7666 -- <entry index>, -- E
7667 -- P'Address, -- Uninterpreted_Data
7668 -- Conditional_Call, -- Mode
7669 -- Bnn); -- Block
7670 -- parm := P.param;
7671 -- parm := P.param;
7672 -- ...
7673 -- if Cancelled (Bnn) then
7674 -- else-statements
7675 -- else
7676 -- normal-statements
7677 -- end if;
7678 -- end;
7680 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7681 -- into:
7683 -- declare
7684 -- B : Boolean := False;
7685 -- C : Ada.Tags.Prim_Op_Kind;
7686 -- K : Ada.Tags.Tagged_Kind :=
7687 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7688 -- P : Parameters := (Param1 .. ParamN);
7689 -- S : Integer;
7691 -- begin
7692 -- if K = Ada.Tags.TK_Limited_Tagged
7693 -- or else K = Ada.Tags.TK_Tagged
7694 -- then
7695 -- <dispatching-call>;
7696 -- <triggering-statements>
7698 -- else
7699 -- S :=
7700 -- Ada.Tags.Get_Offset_Index
7701 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7703 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7705 -- if C = POK_Protected_Entry
7706 -- or else C = POK_Task_Entry
7707 -- then
7708 -- Param1 := P.Param1;
7709 -- ...
7710 -- ParamN := P.ParamN;
7711 -- end if;
7713 -- if B then
7714 -- if C = POK_Procedure
7715 -- or else C = POK_Protected_Procedure
7716 -- or else C = POK_Task_Procedure
7717 -- then
7718 -- <dispatching-call>;
7719 -- end if;
7721 -- <triggering-statements>
7722 -- else
7723 -- <else-statements>
7724 -- end if;
7725 -- end if;
7726 -- end;
7728 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7729 Loc : constant Source_Ptr := Sloc (N);
7730 Alt : constant Node_Id := Entry_Call_Alternative (N);
7731 Blk : Node_Id := Entry_Call_Statement (Alt);
7733 Actuals : List_Id;
7734 Blk_Typ : Entity_Id;
7735 Call : Node_Id;
7736 Call_Ent : Entity_Id;
7737 Conc_Typ_Stmts : List_Id;
7738 Decl : Node_Id;
7739 Decls : List_Id;
7740 Formals : List_Id;
7741 Lim_Typ_Stmts : List_Id;
7742 N_Stats : List_Id;
7743 Obj : Entity_Id;
7744 Param : Node_Id;
7745 Params : List_Id;
7746 Stmt : Node_Id;
7747 Stmts : List_Id;
7748 Transient_Blk : Node_Id;
7749 Unpack : List_Id;
7751 B : Entity_Id; -- Call status flag
7752 C : Entity_Id; -- Call kind
7753 K : Entity_Id; -- Tagged kind
7754 P : Entity_Id; -- Parameter block
7755 S : Entity_Id; -- Primitive operation slot
7757 begin
7758 Process_Statements_For_Controlled_Objects (N);
7760 if Ada_Version >= Ada_2005
7761 and then Nkind (Blk) = N_Procedure_Call_Statement
7762 then
7763 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7765 Decls := New_List;
7766 Stmts := New_List;
7768 -- Call status flag processing, generate:
7769 -- B : Boolean := False;
7771 B := Build_B (Loc, Decls);
7773 -- Call kind processing, generate:
7774 -- C : Ada.Tags.Prim_Op_Kind;
7776 C := Build_C (Loc, Decls);
7778 -- Tagged kind processing, generate:
7779 -- K : Ada.Tags.Tagged_Kind :=
7780 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7782 K := Build_K (Loc, Decls, Obj);
7784 -- Parameter block processing
7786 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7787 P := Parameter_Block_Pack
7788 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7790 -- Dispatch table slot processing, generate:
7791 -- S : Integer;
7793 S := Build_S (Loc, Decls);
7795 -- Generate:
7796 -- S := Ada.Tags.Get_Offset_Index
7797 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7799 Conc_Typ_Stmts :=
7800 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7802 -- Generate:
7803 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7805 Append_To (Conc_Typ_Stmts,
7806 Make_Procedure_Call_Statement (Loc,
7807 Name =>
7808 New_Reference_To (
7809 Find_Prim_Op (Etype (Etype (Obj)),
7810 Name_uDisp_Conditional_Select),
7811 Loc),
7812 Parameter_Associations =>
7813 New_List (
7814 New_Copy_Tree (Obj), -- <object>
7815 New_Reference_To (S, Loc), -- S
7816 Make_Attribute_Reference (Loc, -- P'Address
7817 Prefix => New_Reference_To (P, Loc),
7818 Attribute_Name => Name_Address),
7819 New_Reference_To (C, Loc), -- C
7820 New_Reference_To (B, Loc)))); -- B
7822 -- Generate:
7823 -- if C = POK_Protected_Entry
7824 -- or else C = POK_Task_Entry
7825 -- then
7826 -- Param1 := P.Param1;
7827 -- ...
7828 -- ParamN := P.ParamN;
7829 -- end if;
7831 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7833 -- Generate the if statement only when the packed parameters need
7834 -- explicit assignments to their corresponding actuals.
7836 if Present (Unpack) then
7837 Append_To (Conc_Typ_Stmts,
7838 Make_Implicit_If_Statement (N,
7839 Condition =>
7840 Make_Or_Else (Loc,
7841 Left_Opnd =>
7842 Make_Op_Eq (Loc,
7843 Left_Opnd =>
7844 New_Reference_To (C, Loc),
7845 Right_Opnd =>
7846 New_Reference_To (RTE (
7847 RE_POK_Protected_Entry), Loc)),
7849 Right_Opnd =>
7850 Make_Op_Eq (Loc,
7851 Left_Opnd =>
7852 New_Reference_To (C, Loc),
7853 Right_Opnd =>
7854 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
7856 Then_Statements => Unpack));
7857 end if;
7859 -- Generate:
7860 -- if B then
7861 -- if C = POK_Procedure
7862 -- or else C = POK_Protected_Procedure
7863 -- or else C = POK_Task_Procedure
7864 -- then
7865 -- <dispatching-call>
7866 -- end if;
7867 -- <normal-statements>
7868 -- else
7869 -- <else-statements>
7870 -- end if;
7872 N_Stats := New_Copy_List_Tree (Statements (Alt));
7874 Prepend_To (N_Stats,
7875 Make_Implicit_If_Statement (N,
7876 Condition =>
7877 Make_Or_Else (Loc,
7878 Left_Opnd =>
7879 Make_Op_Eq (Loc,
7880 Left_Opnd =>
7881 New_Reference_To (C, Loc),
7882 Right_Opnd =>
7883 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
7885 Right_Opnd =>
7886 Make_Or_Else (Loc,
7887 Left_Opnd =>
7888 Make_Op_Eq (Loc,
7889 Left_Opnd =>
7890 New_Reference_To (C, Loc),
7891 Right_Opnd =>
7892 New_Reference_To (RTE (
7893 RE_POK_Protected_Procedure), Loc)),
7895 Right_Opnd =>
7896 Make_Op_Eq (Loc,
7897 Left_Opnd =>
7898 New_Reference_To (C, Loc),
7899 Right_Opnd =>
7900 New_Reference_To (RTE (
7901 RE_POK_Task_Procedure), Loc)))),
7903 Then_Statements =>
7904 New_List (Blk)));
7906 Append_To (Conc_Typ_Stmts,
7907 Make_Implicit_If_Statement (N,
7908 Condition => New_Reference_To (B, Loc),
7909 Then_Statements => N_Stats,
7910 Else_Statements => Else_Statements (N)));
7912 -- Generate:
7913 -- <dispatching-call>;
7914 -- <triggering-statements>
7916 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7917 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7919 -- Generate:
7920 -- if K = Ada.Tags.TK_Limited_Tagged
7921 -- or else K = Ada.Tags.TK_Tagged
7922 -- then
7923 -- Lim_Typ_Stmts
7924 -- else
7925 -- Conc_Typ_Stmts
7926 -- end if;
7928 Append_To (Stmts,
7929 Make_Implicit_If_Statement (N,
7930 Condition => Build_Dispatching_Tag_Check (K, N),
7931 Then_Statements => Lim_Typ_Stmts,
7932 Else_Statements => Conc_Typ_Stmts));
7934 Rewrite (N,
7935 Make_Block_Statement (Loc,
7936 Declarations =>
7937 Decls,
7938 Handled_Statement_Sequence =>
7939 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7941 -- As described above, the entry alternative is transformed into a
7942 -- block that contains the gnulli call, and possibly assignment
7943 -- statements for in-out parameters. The gnulli call may itself be
7944 -- rewritten into a transient block if some unconstrained parameters
7945 -- require it. We need to retrieve the call to complete its parameter
7946 -- list.
7948 else
7949 Transient_Blk :=
7950 First_Real_Statement (Handled_Statement_Sequence (Blk));
7952 if Present (Transient_Blk)
7953 and then Nkind (Transient_Blk) = N_Block_Statement
7954 then
7955 Blk := Transient_Blk;
7956 end if;
7958 Stmts := Statements (Handled_Statement_Sequence (Blk));
7959 Stmt := First (Stmts);
7960 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7961 Next (Stmt);
7962 end loop;
7964 Call := Stmt;
7965 Params := Parameter_Associations (Call);
7967 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7969 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7971 Param := First (Params);
7972 while Present (Param)
7973 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7974 loop
7975 Next (Param);
7976 end loop;
7978 pragma Assert (Present (Param));
7979 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
7981 Analyze (Param);
7983 -- Find the Communication_Block parameter for the call to the
7984 -- Cancelled function.
7986 Decl := First (Declarations (Blk));
7987 while Present (Decl)
7988 and then not Is_RTE (Etype (Object_Definition (Decl)),
7989 RE_Communication_Block)
7990 loop
7991 Next (Decl);
7992 end loop;
7994 -- Add an if statement to execute the else part if the call
7995 -- does not succeed (as indicated by the Cancelled predicate).
7997 Append_To (Stmts,
7998 Make_Implicit_If_Statement (N,
7999 Condition => Make_Function_Call (Loc,
8000 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
8001 Parameter_Associations => New_List (
8002 New_Reference_To (Defining_Identifier (Decl), Loc))),
8003 Then_Statements => Else_Statements (N),
8004 Else_Statements => Statements (Alt)));
8006 else
8007 B := Make_Defining_Identifier (Loc, Name_uB);
8009 -- Insert declaration of B in declarations of existing block
8011 if No (Declarations (Blk)) then
8012 Set_Declarations (Blk, New_List);
8013 end if;
8015 Prepend_To (Declarations (Blk),
8016 Make_Object_Declaration (Loc,
8017 Defining_Identifier => B,
8018 Object_Definition =>
8019 New_Reference_To (Standard_Boolean, Loc)));
8021 -- Create new call statement
8023 Append_To (Params,
8024 New_Reference_To (RTE (RE_Conditional_Call), Loc));
8025 Append_To (Params, New_Reference_To (B, Loc));
8027 Rewrite (Call,
8028 Make_Procedure_Call_Statement (Loc,
8029 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
8030 Parameter_Associations => Params));
8032 -- Construct statement sequence for new block
8034 Append_To (Stmts,
8035 Make_Implicit_If_Statement (N,
8036 Condition => New_Reference_To (B, Loc),
8037 Then_Statements => Statements (Alt),
8038 Else_Statements => Else_Statements (N)));
8039 end if;
8041 -- The result is the new block
8043 Rewrite (N,
8044 Make_Block_Statement (Loc,
8045 Declarations => Declarations (Blk),
8046 Handled_Statement_Sequence =>
8047 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8048 end if;
8050 Analyze (N);
8051 end Expand_N_Conditional_Entry_Call;
8053 ---------------------------------------
8054 -- Expand_N_Delay_Relative_Statement --
8055 ---------------------------------------
8057 -- Delay statement is implemented as a procedure call to Delay_For
8058 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8059 -- simple delays imposed by the use of Protected Objects.
8061 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8062 Loc : constant Source_Ptr := Sloc (N);
8063 begin
8064 Rewrite (N,
8065 Make_Procedure_Call_Statement (Loc,
8066 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
8067 Parameter_Associations => New_List (Expression (N))));
8068 Analyze (N);
8069 end Expand_N_Delay_Relative_Statement;
8071 ------------------------------------
8072 -- Expand_N_Delay_Until_Statement --
8073 ------------------------------------
8075 -- Delay Until statement is implemented as a procedure call to
8076 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8078 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8079 Loc : constant Source_Ptr := Sloc (N);
8080 Typ : Entity_Id;
8082 begin
8083 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8084 Typ := RTE (RO_CA_Delay_Until);
8085 else
8086 Typ := RTE (RO_RT_Delay_Until);
8087 end if;
8089 Rewrite (N,
8090 Make_Procedure_Call_Statement (Loc,
8091 Name => New_Reference_To (Typ, Loc),
8092 Parameter_Associations => New_List (Expression (N))));
8094 Analyze (N);
8095 end Expand_N_Delay_Until_Statement;
8097 -------------------------
8098 -- Expand_N_Entry_Body --
8099 -------------------------
8101 procedure Expand_N_Entry_Body (N : Node_Id) is
8102 begin
8103 -- Associate discriminals with the next protected operation body to be
8104 -- expanded.
8106 if Present (Next_Protected_Operation (N)) then
8107 Set_Discriminals (Parent (Current_Scope));
8108 end if;
8109 end Expand_N_Entry_Body;
8111 -----------------------------------
8112 -- Expand_N_Entry_Call_Statement --
8113 -----------------------------------
8115 -- An entry call is expanded into GNARLI calls to implement a simple entry
8116 -- call (see Build_Simple_Entry_Call).
8118 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8119 Concval : Node_Id;
8120 Ename : Node_Id;
8121 Index : Node_Id;
8123 begin
8124 if No_Run_Time_Mode then
8125 Error_Msg_CRT ("entry call", N);
8126 return;
8127 end if;
8129 -- If this entry call is part of an asynchronous select, don't expand it
8130 -- here; it will be expanded with the select statement. Don't expand
8131 -- timed entry calls either, as they are translated into asynchronous
8132 -- entry calls.
8134 -- ??? This whole approach is questionable; it may be better to go back
8135 -- to allowing the expansion to take place and then attempting to fix it
8136 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8137 -- whether the expanded call is on a task or protected entry.
8139 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8140 or else N /= Triggering_Statement (Parent (N)))
8141 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8142 or else N /= Entry_Call_Statement (Parent (N))
8143 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8144 then
8145 Extract_Entry (N, Concval, Ename, Index);
8146 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8147 end if;
8148 end Expand_N_Entry_Call_Statement;
8150 --------------------------------
8151 -- Expand_N_Entry_Declaration --
8152 --------------------------------
8154 -- If there are parameters, then first, each of the formals is marked by
8155 -- setting Is_Entry_Formal. Next a record type is built which is used to
8156 -- hold the parameter values. The name of this record type is entryP where
8157 -- entry is the name of the entry, with an additional corresponding access
8158 -- type called entryPA. The record type has matching components for each
8159 -- formal (the component names are the same as the formal names). For
8160 -- elementary types, the component type matches the formal type. For
8161 -- composite types, an access type is declared (with the name formalA)
8162 -- which designates the formal type, and the type of the component is this
8163 -- access type. Finally the Entry_Component of each formal is set to
8164 -- reference the corresponding record component.
8166 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8167 Loc : constant Source_Ptr := Sloc (N);
8168 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8169 Components : List_Id;
8170 Formal : Node_Id;
8171 Ftype : Entity_Id;
8172 Last_Decl : Node_Id;
8173 Component : Entity_Id;
8174 Ctype : Entity_Id;
8175 Decl : Node_Id;
8176 Rec_Ent : Entity_Id;
8177 Acc_Ent : Entity_Id;
8179 begin
8180 Formal := First_Formal (Entry_Ent);
8181 Last_Decl := N;
8183 -- Most processing is done only if parameters are present
8185 if Present (Formal) then
8186 Components := New_List;
8188 -- Loop through formals
8190 while Present (Formal) loop
8191 Set_Is_Entry_Formal (Formal);
8192 Component :=
8193 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8194 Set_Entry_Component (Formal, Component);
8195 Set_Entry_Formal (Component, Formal);
8196 Ftype := Etype (Formal);
8198 -- Declare new access type and then append
8200 Ctype := Make_Temporary (Loc, 'A');
8202 Decl :=
8203 Make_Full_Type_Declaration (Loc,
8204 Defining_Identifier => Ctype,
8205 Type_Definition =>
8206 Make_Access_To_Object_Definition (Loc,
8207 All_Present => True,
8208 Constant_Present => Ekind (Formal) = E_In_Parameter,
8209 Subtype_Indication => New_Reference_To (Ftype, Loc)));
8211 Insert_After (Last_Decl, Decl);
8212 Last_Decl := Decl;
8214 Append_To (Components,
8215 Make_Component_Declaration (Loc,
8216 Defining_Identifier => Component,
8217 Component_Definition =>
8218 Make_Component_Definition (Loc,
8219 Aliased_Present => False,
8220 Subtype_Indication => New_Reference_To (Ctype, Loc))));
8222 Next_Formal_With_Extras (Formal);
8223 end loop;
8225 -- Create the Entry_Parameter_Record declaration
8227 Rec_Ent := Make_Temporary (Loc, 'P');
8229 Decl :=
8230 Make_Full_Type_Declaration (Loc,
8231 Defining_Identifier => Rec_Ent,
8232 Type_Definition =>
8233 Make_Record_Definition (Loc,
8234 Component_List =>
8235 Make_Component_List (Loc,
8236 Component_Items => Components)));
8238 Insert_After (Last_Decl, Decl);
8239 Last_Decl := Decl;
8241 -- Construct and link in the corresponding access type
8243 Acc_Ent := Make_Temporary (Loc, 'A');
8245 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8247 Decl :=
8248 Make_Full_Type_Declaration (Loc,
8249 Defining_Identifier => Acc_Ent,
8250 Type_Definition =>
8251 Make_Access_To_Object_Definition (Loc,
8252 All_Present => True,
8253 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
8255 Insert_After (Last_Decl, Decl);
8256 end if;
8257 end Expand_N_Entry_Declaration;
8259 -----------------------------
8260 -- Expand_N_Protected_Body --
8261 -----------------------------
8263 -- Protected bodies are expanded to the completion of the subprograms
8264 -- created for the corresponding protected type. These are a protected and
8265 -- unprotected version of each protected subprogram in the object, a
8266 -- function to calculate each entry barrier, and a procedure to execute the
8267 -- sequence of statements of each protected entry body. For example, for
8268 -- protected type ptype:
8270 -- function entB
8271 -- (O : System.Address;
8272 -- E : Protected_Entry_Index)
8273 -- return Boolean
8274 -- is
8275 -- <discriminant renamings>
8276 -- <private object renamings>
8277 -- begin
8278 -- return <barrier expression>;
8279 -- end entB;
8281 -- procedure pprocN (_object : in out poV;...) is
8282 -- <discriminant renamings>
8283 -- <private object renamings>
8284 -- begin
8285 -- <sequence of statements>
8286 -- end pprocN;
8288 -- procedure pprocP (_object : in out poV;...) is
8289 -- procedure _clean is
8290 -- Pn : Boolean;
8291 -- begin
8292 -- ptypeS (_object, Pn);
8293 -- Unlock (_object._object'Access);
8294 -- Abort_Undefer.all;
8295 -- end _clean;
8297 -- begin
8298 -- Abort_Defer.all;
8299 -- Lock (_object._object'Access);
8300 -- pprocN (_object;...);
8301 -- at end
8302 -- _clean;
8303 -- end pproc;
8305 -- function pfuncN (_object : poV;...) return Return_Type is
8306 -- <discriminant renamings>
8307 -- <private object renamings>
8308 -- begin
8309 -- <sequence of statements>
8310 -- end pfuncN;
8312 -- function pfuncP (_object : poV) return Return_Type is
8313 -- procedure _clean is
8314 -- begin
8315 -- Unlock (_object._object'Access);
8316 -- Abort_Undefer.all;
8317 -- end _clean;
8319 -- begin
8320 -- Abort_Defer.all;
8321 -- Lock (_object._object'Access);
8322 -- return pfuncN (_object);
8324 -- at end
8325 -- _clean;
8326 -- end pfunc;
8328 -- procedure entE
8329 -- (O : System.Address;
8330 -- P : System.Address;
8331 -- E : Protected_Entry_Index)
8332 -- is
8333 -- <discriminant renamings>
8334 -- <private object renamings>
8335 -- type poVP is access poV;
8336 -- _Object : ptVP := ptVP!(O);
8338 -- begin
8339 -- begin
8340 -- <statement sequence>
8341 -- Complete_Entry_Body (_Object._Object);
8342 -- exception
8343 -- when all others =>
8344 -- Exceptional_Complete_Entry_Body (
8345 -- _Object._Object, Get_GNAT_Exception);
8346 -- end;
8347 -- end entE;
8349 -- The type poV is the record created for the protected type to hold
8350 -- the state of the protected object.
8352 procedure Expand_N_Protected_Body (N : Node_Id) is
8353 Loc : constant Source_Ptr := Sloc (N);
8354 Pid : constant Entity_Id := Corresponding_Spec (N);
8356 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8357 -- This flag indicates whether the lock free implementation is active
8359 Current_Node : Node_Id;
8360 Disp_Op_Body : Node_Id;
8361 New_Op_Body : Node_Id;
8362 Num_Entries : Natural := 0;
8363 Op_Body : Node_Id;
8364 Op_Id : Entity_Id;
8366 function Build_Dispatching_Subprogram_Body
8367 (N : Node_Id;
8368 Pid : Node_Id;
8369 Prot_Bod : Node_Id) return Node_Id;
8370 -- Build a dispatching version of the protected subprogram body. The
8371 -- newly generated subprogram contains a call to the original protected
8372 -- body. The following code is generated:
8374 -- function <protected-function-name> (Param1 .. ParamN) return
8375 -- <return-type> is
8376 -- begin
8377 -- return <protected-function-name>P (Param1 .. ParamN);
8378 -- end <protected-function-name>;
8380 -- or
8382 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8383 -- begin
8384 -- <protected-procedure-name>P (Param1 .. ParamN);
8385 -- end <protected-procedure-name>
8387 ---------------------------------------
8388 -- Build_Dispatching_Subprogram_Body --
8389 ---------------------------------------
8391 function Build_Dispatching_Subprogram_Body
8392 (N : Node_Id;
8393 Pid : Node_Id;
8394 Prot_Bod : Node_Id) return Node_Id
8396 Loc : constant Source_Ptr := Sloc (N);
8397 Actuals : List_Id;
8398 Formal : Node_Id;
8399 Spec : Node_Id;
8400 Stmts : List_Id;
8402 begin
8403 -- Generate a specification without a letter suffix in order to
8404 -- override an interface function or procedure.
8406 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8408 -- The formal parameters become the actuals of the protected function
8409 -- or procedure call.
8411 Actuals := New_List;
8412 Formal := First (Parameter_Specifications (Spec));
8413 while Present (Formal) loop
8414 Append_To (Actuals,
8415 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8416 Next (Formal);
8417 end loop;
8419 if Nkind (Spec) = N_Procedure_Specification then
8420 Stmts :=
8421 New_List (
8422 Make_Procedure_Call_Statement (Loc,
8423 Name =>
8424 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8425 Parameter_Associations => Actuals));
8427 else
8428 pragma Assert (Nkind (Spec) = N_Function_Specification);
8430 Stmts :=
8431 New_List (
8432 Make_Simple_Return_Statement (Loc,
8433 Expression =>
8434 Make_Function_Call (Loc,
8435 Name =>
8436 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8437 Parameter_Associations => Actuals)));
8438 end if;
8440 return
8441 Make_Subprogram_Body (Loc,
8442 Declarations => Empty_List,
8443 Specification => Spec,
8444 Handled_Statement_Sequence =>
8445 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8446 end Build_Dispatching_Subprogram_Body;
8448 -- Start of processing for Expand_N_Protected_Body
8450 begin
8451 if No_Run_Time_Mode then
8452 Error_Msg_CRT ("protected body", N);
8453 return;
8454 end if;
8456 -- This is the proper body corresponding to a stub. The declarations
8457 -- must be inserted at the point of the stub, which in turn is in the
8458 -- declarative part of the parent unit.
8460 if Nkind (Parent (N)) = N_Subunit then
8461 Current_Node := Corresponding_Stub (Parent (N));
8462 else
8463 Current_Node := N;
8464 end if;
8466 Op_Body := First (Declarations (N));
8468 -- The protected body is replaced with the bodies of its
8469 -- protected operations, and the declarations for internal objects
8470 -- that may have been created for entry family bounds.
8472 Rewrite (N, Make_Null_Statement (Sloc (N)));
8473 Analyze (N);
8475 while Present (Op_Body) loop
8476 case Nkind (Op_Body) is
8477 when N_Subprogram_Declaration =>
8478 null;
8480 when N_Subprogram_Body =>
8482 -- Do not create bodies for eliminated operations
8484 if not Is_Eliminated (Defining_Entity (Op_Body))
8485 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8486 then
8487 if Lock_Free_Active then
8488 New_Op_Body :=
8489 Build_Lock_Free_Unprotected_Subprogram_Body
8490 (Op_Body, Pid);
8491 else
8492 New_Op_Body :=
8493 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8494 end if;
8496 Insert_After (Current_Node, New_Op_Body);
8497 Current_Node := New_Op_Body;
8498 Analyze (New_Op_Body);
8500 -- Build the corresponding protected operation. It may
8501 -- appear that this is needed only if this is a visible
8502 -- operation of the type, or if it is an interrupt handler,
8503 -- and this was the strategy used previously in GNAT.
8505 -- However, the operation may be exported through a 'Access
8506 -- to an external caller. This is the common idiom in code
8507 -- that uses the Ada 2005 Timing_Events package. As a result
8508 -- we need to produce the protected body for both visible
8509 -- and private operations, as well as operations that only
8510 -- have a body in the source, and for which we create a
8511 -- declaration in the protected body itself.
8513 if Present (Corresponding_Spec (Op_Body)) then
8514 if Lock_Free_Active then
8515 New_Op_Body :=
8516 Build_Lock_Free_Protected_Subprogram_Body
8517 (Op_Body, Pid, Specification (New_Op_Body));
8518 else
8519 New_Op_Body :=
8520 Build_Protected_Subprogram_Body
8521 (Op_Body, Pid, Specification (New_Op_Body));
8522 end if;
8524 Insert_After (Current_Node, New_Op_Body);
8525 Analyze (New_Op_Body);
8527 Current_Node := New_Op_Body;
8529 -- Generate an overriding primitive operation body for
8530 -- this subprogram if the protected type implements an
8531 -- interface.
8533 if Ada_Version >= Ada_2005
8534 and then
8535 Present (Interfaces (Corresponding_Record_Type (Pid)))
8536 then
8537 Disp_Op_Body :=
8538 Build_Dispatching_Subprogram_Body
8539 (Op_Body, Pid, New_Op_Body);
8541 Insert_After (Current_Node, Disp_Op_Body);
8542 Analyze (Disp_Op_Body);
8544 Current_Node := Disp_Op_Body;
8545 end if;
8546 end if;
8547 end if;
8549 when N_Entry_Body =>
8550 Op_Id := Defining_Identifier (Op_Body);
8551 Num_Entries := Num_Entries + 1;
8553 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8555 Insert_After (Current_Node, New_Op_Body);
8556 Current_Node := New_Op_Body;
8557 Analyze (New_Op_Body);
8559 when N_Implicit_Label_Declaration =>
8560 null;
8562 when N_Itype_Reference =>
8563 Insert_After (Current_Node, New_Copy (Op_Body));
8565 when N_Freeze_Entity =>
8566 New_Op_Body := New_Copy (Op_Body);
8568 if Present (Entity (Op_Body))
8569 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8570 then
8571 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8572 end if;
8574 Insert_After (Current_Node, New_Op_Body);
8575 Current_Node := New_Op_Body;
8576 Analyze (New_Op_Body);
8578 when N_Pragma =>
8579 New_Op_Body := New_Copy (Op_Body);
8580 Insert_After (Current_Node, New_Op_Body);
8581 Current_Node := New_Op_Body;
8582 Analyze (New_Op_Body);
8584 when N_Object_Declaration =>
8585 pragma Assert (not Comes_From_Source (Op_Body));
8586 New_Op_Body := New_Copy (Op_Body);
8587 Insert_After (Current_Node, New_Op_Body);
8588 Current_Node := New_Op_Body;
8589 Analyze (New_Op_Body);
8591 when others =>
8592 raise Program_Error;
8594 end case;
8596 Next (Op_Body);
8597 end loop;
8599 -- Finally, create the body of the function that maps an entry index
8600 -- into the corresponding body index, except when there is no entry, or
8601 -- in a Ravenscar-like profile.
8603 if Corresponding_Runtime_Package (Pid) =
8604 System_Tasking_Protected_Objects_Entries
8605 then
8606 New_Op_Body := Build_Find_Body_Index (Pid);
8607 Insert_After (Current_Node, New_Op_Body);
8608 Current_Node := New_Op_Body;
8609 Analyze (New_Op_Body);
8610 end if;
8612 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8613 -- protected body. At this point all wrapper specs have been created,
8614 -- frozen and included in the dispatch table for the protected type.
8616 if Ada_Version >= Ada_2005 then
8617 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8618 end if;
8619 end Expand_N_Protected_Body;
8621 -----------------------------------------
8622 -- Expand_N_Protected_Type_Declaration --
8623 -----------------------------------------
8625 -- First we create a corresponding record type declaration used to
8626 -- represent values of this protected type.
8627 -- The general form of this type declaration is
8629 -- type poV (discriminants) is record
8630 -- _Object : aliased <kind>Protection
8631 -- [(<entry count> [, <handler count>])];
8632 -- [entry_family : array (bounds) of Void;]
8633 -- <private data fields>
8634 -- end record;
8636 -- The discriminants are present only if the corresponding protected type
8637 -- has discriminants, and they exactly mirror the protected type
8638 -- discriminants. The private data fields similarly mirror the private
8639 -- declarations of the protected type.
8641 -- The Object field is always present. It contains RTS specific data used
8642 -- to control the protected object. It is declared as Aliased so that it
8643 -- can be passed as a pointer to the RTS. This allows the protected record
8644 -- to be referenced within RTS data structures. An appropriate Protection
8645 -- type and discriminant are generated.
8647 -- The Service field is present for protected objects with entries. It
8648 -- contains sufficient information to allow the entry service procedure for
8649 -- this object to be called when the object is not known till runtime.
8651 -- One entry_family component is present for each entry family in the
8652 -- task definition (see Expand_N_Task_Type_Declaration).
8654 -- When a protected object is declared, an instance of the protected type
8655 -- value record is created. The elaboration of this declaration creates the
8656 -- correct bounds for the entry families, and also evaluates the priority
8657 -- expression if needed. The initialization routine for the protected type
8658 -- itself then calls Initialize_Protection with appropriate parameters to
8659 -- initialize the value of the Task_Id field. Install_Handlers may be also
8660 -- called if a pragma Attach_Handler applies.
8662 -- Note: this record is passed to the subprograms created by the expansion
8663 -- of protected subprograms and entries. It is an in parameter to protected
8664 -- functions and an in out parameter to procedures and entry bodies. The
8665 -- Entity_Id for this created record type is placed in the
8666 -- Corresponding_Record_Type field of the associated protected type entity.
8668 -- Next we create a procedure specifications for protected subprograms and
8669 -- entry bodies. For each protected subprograms two subprograms are
8670 -- created, an unprotected and a protected version. The unprotected version
8671 -- is called from within other operations of the same protected object.
8673 -- We also build the call to register the procedure if a pragma
8674 -- Interrupt_Handler applies.
8676 -- A single subprogram is created to service all entry bodies; it has an
8677 -- additional boolean out parameter indicating that the previous entry call
8678 -- made by the current task was serviced immediately, i.e. not by proxy.
8679 -- The O parameter contains a pointer to a record object of the type
8680 -- described above. An untyped interface is used here to allow this
8681 -- procedure to be called in places where the type of the object to be
8682 -- serviced is not known. This must be done, for example, when a call that
8683 -- may have been requeued is cancelled; the corresponding object must be
8684 -- serviced, but which object that is not known till runtime.
8686 -- procedure ptypeS
8687 -- (O : System.Address; P : out Boolean);
8688 -- procedure pprocN (_object : in out poV);
8689 -- procedure pproc (_object : in out poV);
8690 -- function pfuncN (_object : poV);
8691 -- function pfunc (_object : poV);
8692 -- ...
8694 -- Note that this must come after the record type declaration, since
8695 -- the specs refer to this type.
8697 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8698 Loc : constant Source_Ptr := Sloc (N);
8699 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8701 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8702 -- This flag indicates whether the lock free implementation is active
8704 Pdef : constant Node_Id := Protected_Definition (N);
8705 -- This contains two lists; one for visible and one for private decls
8707 Rec_Decl : Node_Id;
8708 Cdecls : List_Id;
8709 Discr_Map : constant Elist_Id := New_Elmt_List;
8710 Priv : Node_Id;
8711 New_Priv : Node_Id;
8712 Comp : Node_Id;
8713 Comp_Id : Entity_Id;
8714 Sub : Node_Id;
8715 Current_Node : Node_Id := N;
8716 Bdef : Entity_Id := Empty; -- avoid uninit warning
8717 Edef : Entity_Id := Empty; -- avoid uninit warning
8718 Entries_Aggr : Node_Id;
8719 Body_Id : Entity_Id;
8720 Body_Arr : Node_Id;
8721 E_Count : Int;
8722 Object_Comp : Node_Id;
8724 procedure Check_Inlining (Subp : Entity_Id);
8725 -- If the original operation has a pragma Inline, propagate the flag
8726 -- to the internal body, for possible inlining later on. The source
8727 -- operation is invisible to the back-end and is never actually called.
8729 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8730 -- When compiling under the Ravenscar profile, private components must
8731 -- have a static size, or else a protected object will require heap
8732 -- allocation, violating the corresponding restriction. It is preferable
8733 -- to make this check here, because it provides a better error message
8734 -- than the back-end, which refers to the object as a whole.
8736 procedure Register_Handler;
8737 -- For a protected operation that is an interrupt handler, add the
8738 -- freeze action that will register it as such.
8740 --------------------
8741 -- Check_Inlining --
8742 --------------------
8744 procedure Check_Inlining (Subp : Entity_Id) is
8745 begin
8746 if Is_Inlined (Subp) then
8747 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8748 Set_Is_Inlined (Subp, False);
8749 end if;
8750 end Check_Inlining;
8752 ---------------------------------
8753 -- Check_Static_Component_Size --
8754 ---------------------------------
8756 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8757 Typ : constant Entity_Id := Etype (Comp);
8758 C : Entity_Id;
8760 begin
8761 if Is_Scalar_Type (Typ) then
8762 return True;
8764 elsif Is_Array_Type (Typ) then
8765 return Compile_Time_Known_Bounds (Typ);
8767 elsif Is_Record_Type (Typ) then
8768 C := First_Component (Typ);
8769 while Present (C) loop
8770 if not Static_Component_Size (C) then
8771 return False;
8772 end if;
8774 Next_Component (C);
8775 end loop;
8777 return True;
8779 -- Any other type will be checked by the back-end
8781 else
8782 return True;
8783 end if;
8784 end Static_Component_Size;
8786 ----------------------
8787 -- Register_Handler --
8788 ----------------------
8790 procedure Register_Handler is
8792 -- All semantic checks already done in Sem_Prag
8794 Prot_Proc : constant Entity_Id :=
8795 Defining_Unit_Name (Specification (Current_Node));
8797 Proc_Address : constant Node_Id :=
8798 Make_Attribute_Reference (Loc,
8799 Prefix =>
8800 New_Reference_To (Prot_Proc, Loc),
8801 Attribute_Name => Name_Address);
8803 RTS_Call : constant Entity_Id :=
8804 Make_Procedure_Call_Statement (Loc,
8805 Name =>
8806 New_Reference_To
8807 (RTE (RE_Register_Interrupt_Handler), Loc),
8808 Parameter_Associations => New_List (Proc_Address));
8809 begin
8810 Append_Freeze_Action (Prot_Proc, RTS_Call);
8811 end Register_Handler;
8813 -- Start of processing for Expand_N_Protected_Type_Declaration
8815 begin
8816 if Present (Corresponding_Record_Type (Prot_Typ)) then
8817 return;
8818 else
8819 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8820 end if;
8822 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8824 Qualify_Entity_Names (N);
8826 -- If the type has discriminants, their occurrences in the declaration
8827 -- have been replaced by the corresponding discriminals. For components
8828 -- that are constrained by discriminants, their homologues in the
8829 -- corresponding record type must refer to the discriminants of that
8830 -- record, so we must apply a new renaming to subtypes_indications:
8832 -- protected discriminant => discriminal => record discriminant
8834 -- This replacement is not applied to default expressions, for which
8835 -- the discriminal is correct.
8837 if Has_Discriminants (Prot_Typ) then
8838 declare
8839 Disc : Entity_Id;
8840 Decl : Node_Id;
8842 begin
8843 Disc := First_Discriminant (Prot_Typ);
8844 Decl := First (Discriminant_Specifications (Rec_Decl));
8845 while Present (Disc) loop
8846 Append_Elmt (Discriminal (Disc), Discr_Map);
8847 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8848 Next_Discriminant (Disc);
8849 Next (Decl);
8850 end loop;
8851 end;
8852 end if;
8854 -- Fill in the component declarations
8856 -- Add components for entry families. For each entry family, create an
8857 -- anonymous type declaration with the same size, and analyze the type.
8859 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8861 pragma Assert (Present (Pdef));
8863 -- Add private field components
8865 if Present (Private_Declarations (Pdef)) then
8866 Priv := First (Private_Declarations (Pdef));
8867 while Present (Priv) loop
8868 if Nkind (Priv) = N_Component_Declaration then
8869 if not Static_Component_Size (Defining_Identifier (Priv)) then
8871 -- When compiling for a restricted profile, the private
8872 -- components must have a static size. If not, this is an
8873 -- error for a single protected declaration, and rates a
8874 -- warning on a protected type declaration.
8876 if not Comes_From_Source (Prot_Typ) then
8877 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8879 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8880 Error_Msg_N ("component has non-static size??", Priv);
8881 Error_Msg_NE
8882 ("\creation of protected object of type& will violate"
8883 & " restriction No_Implicit_Heap_Allocations??",
8884 Priv, Prot_Typ);
8885 end if;
8886 end if;
8888 -- The component definition consists of a subtype indication,
8889 -- or (in Ada 2005) an access definition. Make a copy of the
8890 -- proper definition.
8892 declare
8893 Old_Comp : constant Node_Id := Component_Definition (Priv);
8894 Oent : constant Entity_Id := Defining_Identifier (Priv);
8895 New_Comp : Node_Id;
8896 Nent : constant Entity_Id :=
8897 Make_Defining_Identifier (Sloc (Oent),
8898 Chars => Chars (Oent));
8900 begin
8901 if Present (Subtype_Indication (Old_Comp)) then
8902 New_Comp :=
8903 Make_Component_Definition (Sloc (Oent),
8904 Aliased_Present => False,
8905 Subtype_Indication =>
8906 New_Copy_Tree (Subtype_Indication (Old_Comp),
8907 Discr_Map));
8908 else
8909 New_Comp :=
8910 Make_Component_Definition (Sloc (Oent),
8911 Aliased_Present => False,
8912 Access_Definition =>
8913 New_Copy_Tree (Access_Definition (Old_Comp),
8914 Discr_Map));
8915 end if;
8917 New_Priv :=
8918 Make_Component_Declaration (Loc,
8919 Defining_Identifier => Nent,
8920 Component_Definition => New_Comp,
8921 Expression => Expression (Priv));
8923 Set_Has_Per_Object_Constraint (Nent,
8924 Has_Per_Object_Constraint (Oent));
8926 Append_To (Cdecls, New_Priv);
8927 end;
8929 elsif Nkind (Priv) = N_Subprogram_Declaration then
8931 -- Make the unprotected version of the subprogram available
8932 -- for expansion of intra object calls. There is need for
8933 -- a protected version only if the subprogram is an interrupt
8934 -- handler, otherwise this operation can only be called from
8935 -- within the body.
8937 Sub :=
8938 Make_Subprogram_Declaration (Loc,
8939 Specification =>
8940 Build_Protected_Sub_Specification
8941 (Priv, Prot_Typ, Unprotected_Mode));
8943 Insert_After (Current_Node, Sub);
8944 Analyze (Sub);
8946 Set_Protected_Body_Subprogram
8947 (Defining_Unit_Name (Specification (Priv)),
8948 Defining_Unit_Name (Specification (Sub)));
8949 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
8950 Current_Node := Sub;
8952 Sub :=
8953 Make_Subprogram_Declaration (Loc,
8954 Specification =>
8955 Build_Protected_Sub_Specification
8956 (Priv, Prot_Typ, Protected_Mode));
8958 Insert_After (Current_Node, Sub);
8959 Analyze (Sub);
8960 Current_Node := Sub;
8962 if Is_Interrupt_Handler
8963 (Defining_Unit_Name (Specification (Priv)))
8964 then
8965 if not Restricted_Profile then
8966 Register_Handler;
8967 end if;
8968 end if;
8969 end if;
8971 Next (Priv);
8972 end loop;
8973 end if;
8975 -- Except for the lock-free implementation, prepend the _Object field
8976 -- with the right type to the component list. We need to compute the
8977 -- number of entries, and in some cases the number of Attach_Handler
8978 -- pragmas.
8980 if not Lock_Free_Active then
8981 declare
8982 Ritem : Node_Id;
8983 Num_Attach_Handler : Int := 0;
8984 Protection_Subtype : Node_Id;
8985 Entry_Count_Expr : constant Node_Id :=
8986 Build_Entry_Count_Expression
8987 (Prot_Typ, Cdecls, Loc);
8989 begin
8990 if Has_Attach_Handler (Prot_Typ) then
8991 Ritem := First_Rep_Item (Prot_Typ);
8992 while Present (Ritem) loop
8993 if Nkind (Ritem) = N_Pragma
8994 and then Pragma_Name (Ritem) = Name_Attach_Handler
8995 then
8996 Num_Attach_Handler := Num_Attach_Handler + 1;
8997 end if;
8999 Next_Rep_Item (Ritem);
9000 end loop;
9001 end if;
9003 -- Determine the proper protection type. There are two special
9004 -- cases: 1) when the protected type has dynamic interrupt
9005 -- handlers, and 2) when it has static handlers and we use a
9006 -- restricted profile.
9008 if Has_Attach_Handler (Prot_Typ)
9009 and then not Restricted_Profile
9010 then
9011 Protection_Subtype :=
9012 Make_Subtype_Indication (Loc,
9013 Subtype_Mark =>
9014 New_Reference_To
9015 (RTE (RE_Static_Interrupt_Protection), Loc),
9016 Constraint =>
9017 Make_Index_Or_Discriminant_Constraint (Loc,
9018 Constraints => New_List (
9019 Entry_Count_Expr,
9020 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9022 elsif Has_Interrupt_Handler (Prot_Typ)
9023 and then not Restriction_Active (No_Dynamic_Attachment)
9024 then
9025 Protection_Subtype :=
9026 Make_Subtype_Indication (Loc,
9027 Subtype_Mark =>
9028 New_Reference_To
9029 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9030 Constraint =>
9031 Make_Index_Or_Discriminant_Constraint (Loc,
9032 Constraints => New_List (Entry_Count_Expr)));
9034 else
9035 case Corresponding_Runtime_Package (Prot_Typ) is
9036 when System_Tasking_Protected_Objects_Entries =>
9037 Protection_Subtype :=
9038 Make_Subtype_Indication (Loc,
9039 Subtype_Mark =>
9040 New_Reference_To
9041 (RTE (RE_Protection_Entries), Loc),
9042 Constraint =>
9043 Make_Index_Or_Discriminant_Constraint (Loc,
9044 Constraints => New_List (Entry_Count_Expr)));
9046 when System_Tasking_Protected_Objects_Single_Entry =>
9047 Protection_Subtype :=
9048 New_Reference_To (RTE (RE_Protection_Entry), Loc);
9050 when System_Tasking_Protected_Objects =>
9051 Protection_Subtype :=
9052 New_Reference_To (RTE (RE_Protection), Loc);
9054 when others =>
9055 raise Program_Error;
9056 end case;
9057 end if;
9059 Object_Comp :=
9060 Make_Component_Declaration (Loc,
9061 Defining_Identifier =>
9062 Make_Defining_Identifier (Loc, Name_uObject),
9063 Component_Definition =>
9064 Make_Component_Definition (Loc,
9065 Aliased_Present => True,
9066 Subtype_Indication => Protection_Subtype));
9067 end;
9069 -- Put the _Object component after the private component so that it
9070 -- be finalized early as required by 9.4 (20)
9072 Append_To (Cdecls, Object_Comp);
9073 end if;
9075 Insert_After (Current_Node, Rec_Decl);
9076 Current_Node := Rec_Decl;
9078 -- Analyze the record declaration immediately after construction,
9079 -- because the initialization procedure is needed for single object
9080 -- declarations before the next entity is analyzed (the freeze call
9081 -- that generates this initialization procedure is found below).
9083 Analyze (Rec_Decl, Suppress => All_Checks);
9085 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9086 -- the corresponding record is frozen. If any wrappers are generated,
9087 -- Current_Node is updated accordingly.
9089 if Ada_Version >= Ada_2005 then
9090 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9091 end if;
9093 -- Collect pointers to entry bodies and their barriers, to be placed
9094 -- in the Entry_Bodies_Array for the type. For each entry/family we
9095 -- add an expression to the aggregate which is the initial value of
9096 -- this array. The array is declared after all protected subprograms.
9098 if Has_Entries (Prot_Typ) then
9099 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9100 else
9101 Entries_Aggr := Empty;
9102 end if;
9104 -- Build two new procedure specifications for each protected subprogram;
9105 -- one to call from outside the object and one to call from inside.
9106 -- Build a barrier function and an entry body action procedure
9107 -- specification for each protected entry. Initialize the entry body
9108 -- array. If subprogram is flagged as eliminated, do not generate any
9109 -- internal operations.
9111 E_Count := 0;
9112 Comp := First (Visible_Declarations (Pdef));
9113 while Present (Comp) loop
9114 if Nkind (Comp) = N_Subprogram_Declaration then
9115 Sub :=
9116 Make_Subprogram_Declaration (Loc,
9117 Specification =>
9118 Build_Protected_Sub_Specification
9119 (Comp, Prot_Typ, Unprotected_Mode));
9121 Insert_After (Current_Node, Sub);
9122 Analyze (Sub);
9124 Set_Protected_Body_Subprogram
9125 (Defining_Unit_Name (Specification (Comp)),
9126 Defining_Unit_Name (Specification (Sub)));
9127 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9129 -- Make the protected version of the subprogram available for
9130 -- expansion of external calls.
9132 Current_Node := Sub;
9134 Sub :=
9135 Make_Subprogram_Declaration (Loc,
9136 Specification =>
9137 Build_Protected_Sub_Specification
9138 (Comp, Prot_Typ, Protected_Mode));
9140 Insert_After (Current_Node, Sub);
9141 Analyze (Sub);
9143 Current_Node := Sub;
9145 -- Generate an overriding primitive operation specification for
9146 -- this subprogram if the protected type implements an interface.
9148 if Ada_Version >= Ada_2005
9149 and then
9150 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9151 then
9152 Sub :=
9153 Make_Subprogram_Declaration (Loc,
9154 Specification =>
9155 Build_Protected_Sub_Specification
9156 (Comp, Prot_Typ, Dispatching_Mode));
9158 Insert_After (Current_Node, Sub);
9159 Analyze (Sub);
9161 Current_Node := Sub;
9162 end if;
9164 -- If a pragma Interrupt_Handler applies, build and add a call to
9165 -- Register_Interrupt_Handler to the freezing actions of the
9166 -- protected version (Current_Node) of the subprogram:
9168 -- system.interrupts.register_interrupt_handler
9169 -- (prot_procP'address);
9171 if not Restricted_Profile
9172 and then Is_Interrupt_Handler
9173 (Defining_Unit_Name (Specification (Comp)))
9174 then
9175 Register_Handler;
9176 end if;
9178 elsif Nkind (Comp) = N_Entry_Declaration then
9179 E_Count := E_Count + 1;
9180 Comp_Id := Defining_Identifier (Comp);
9182 Edef :=
9183 Make_Defining_Identifier (Loc,
9184 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9185 Sub :=
9186 Make_Subprogram_Declaration (Loc,
9187 Specification =>
9188 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9190 Insert_After (Current_Node, Sub);
9191 Analyze (Sub);
9193 -- Build wrapper procedure for pre/postconditions
9195 Build_PPC_Wrapper (Comp_Id, N);
9197 Set_Protected_Body_Subprogram
9198 (Defining_Identifier (Comp),
9199 Defining_Unit_Name (Specification (Sub)));
9201 Current_Node := Sub;
9203 Bdef :=
9204 Make_Defining_Identifier (Loc,
9205 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
9206 Sub :=
9207 Make_Subprogram_Declaration (Loc,
9208 Specification =>
9209 Build_Barrier_Function_Specification (Loc, Bdef));
9211 Insert_After (Current_Node, Sub);
9212 Analyze (Sub);
9213 Set_Protected_Body_Subprogram (Bdef, Bdef);
9214 Set_Barrier_Function (Comp_Id, Bdef);
9215 Set_Scope (Bdef, Scope (Comp_Id));
9216 Current_Node := Sub;
9218 -- Collect pointers to the protected subprogram and the barrier
9219 -- of the current entry, for insertion into Entry_Bodies_Array.
9221 Append_To (Expressions (Entries_Aggr),
9222 Make_Aggregate (Loc,
9223 Expressions => New_List (
9224 Make_Attribute_Reference (Loc,
9225 Prefix => New_Reference_To (Bdef, Loc),
9226 Attribute_Name => Name_Unrestricted_Access),
9227 Make_Attribute_Reference (Loc,
9228 Prefix => New_Reference_To (Edef, Loc),
9229 Attribute_Name => Name_Unrestricted_Access))));
9230 end if;
9232 Next (Comp);
9233 end loop;
9235 -- If there are some private entry declarations, expand it as if they
9236 -- were visible entries.
9238 if Present (Private_Declarations (Pdef)) then
9239 Comp := First (Private_Declarations (Pdef));
9240 while Present (Comp) loop
9241 if Nkind (Comp) = N_Entry_Declaration then
9242 E_Count := E_Count + 1;
9243 Comp_Id := Defining_Identifier (Comp);
9245 Edef :=
9246 Make_Defining_Identifier (Loc,
9247 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9248 Sub :=
9249 Make_Subprogram_Declaration (Loc,
9250 Specification =>
9251 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9253 Insert_After (Current_Node, Sub);
9254 Analyze (Sub);
9256 Set_Protected_Body_Subprogram
9257 (Defining_Identifier (Comp),
9258 Defining_Unit_Name (Specification (Sub)));
9260 Current_Node := Sub;
9262 Bdef :=
9263 Make_Defining_Identifier (Loc,
9264 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9266 Sub :=
9267 Make_Subprogram_Declaration (Loc,
9268 Specification =>
9269 Build_Barrier_Function_Specification (Loc, Bdef));
9271 Insert_After (Current_Node, Sub);
9272 Analyze (Sub);
9273 Set_Protected_Body_Subprogram (Bdef, Bdef);
9274 Set_Barrier_Function (Comp_Id, Bdef);
9275 Set_Scope (Bdef, Scope (Comp_Id));
9276 Current_Node := Sub;
9278 -- Collect pointers to the protected subprogram and the barrier
9279 -- of the current entry, for insertion into Entry_Bodies_Array.
9281 Append_To (Expressions (Entries_Aggr),
9282 Make_Aggregate (Loc,
9283 Expressions => New_List (
9284 Make_Attribute_Reference (Loc,
9285 Prefix => New_Reference_To (Bdef, Loc),
9286 Attribute_Name => Name_Unrestricted_Access),
9287 Make_Attribute_Reference (Loc,
9288 Prefix => New_Reference_To (Edef, Loc),
9289 Attribute_Name => Name_Unrestricted_Access))));
9290 end if;
9292 Next (Comp);
9293 end loop;
9294 end if;
9296 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9297 -- all protected subprograms have been collected.
9299 if Has_Entries (Prot_Typ) then
9300 Body_Id :=
9301 Make_Defining_Identifier (Sloc (Prot_Typ),
9302 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9304 case Corresponding_Runtime_Package (Prot_Typ) is
9305 when System_Tasking_Protected_Objects_Entries =>
9306 Body_Arr := Make_Object_Declaration (Loc,
9307 Defining_Identifier => Body_Id,
9308 Aliased_Present => True,
9309 Object_Definition =>
9310 Make_Subtype_Indication (Loc,
9311 Subtype_Mark => New_Reference_To (
9312 RTE (RE_Protected_Entry_Body_Array), Loc),
9313 Constraint =>
9314 Make_Index_Or_Discriminant_Constraint (Loc,
9315 Constraints => New_List (
9316 Make_Range (Loc,
9317 Make_Integer_Literal (Loc, 1),
9318 Make_Integer_Literal (Loc, E_Count))))),
9319 Expression => Entries_Aggr);
9321 when System_Tasking_Protected_Objects_Single_Entry =>
9322 Body_Arr := Make_Object_Declaration (Loc,
9323 Defining_Identifier => Body_Id,
9324 Aliased_Present => True,
9325 Object_Definition => New_Reference_To
9326 (RTE (RE_Entry_Body), Loc),
9327 Expression =>
9328 Make_Aggregate (Loc,
9329 Expressions => New_List (
9330 Make_Attribute_Reference (Loc,
9331 Prefix => New_Reference_To (Bdef, Loc),
9332 Attribute_Name => Name_Unrestricted_Access),
9333 Make_Attribute_Reference (Loc,
9334 Prefix => New_Reference_To (Edef, Loc),
9335 Attribute_Name => Name_Unrestricted_Access))));
9337 when others =>
9338 raise Program_Error;
9339 end case;
9341 -- A pointer to this array will be placed in the corresponding record
9342 -- by its initialization procedure so this needs to be analyzed here.
9344 Insert_After (Current_Node, Body_Arr);
9345 Current_Node := Body_Arr;
9346 Analyze (Body_Arr);
9348 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9350 -- Finally, build the function that maps an entry index into the
9351 -- corresponding body. A pointer to this function is placed in each
9352 -- object of the type. Except for a ravenscar-like profile (no abort,
9353 -- no entry queue, 1 entry)
9355 if Corresponding_Runtime_Package (Prot_Typ) =
9356 System_Tasking_Protected_Objects_Entries
9357 then
9358 Sub :=
9359 Make_Subprogram_Declaration (Loc,
9360 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9361 Insert_After (Current_Node, Sub);
9362 Analyze (Sub);
9363 end if;
9364 end if;
9365 end Expand_N_Protected_Type_Declaration;
9367 --------------------------------
9368 -- Expand_N_Requeue_Statement --
9369 --------------------------------
9371 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9372 -- operations, depending on the source and destination (task or protected
9373 -- object). A dispatching requeue statement is expanded into a call to the
9374 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9375 -- jump around the remainder of processing for the original entry and, if
9376 -- the destination is (different) protected object, to attempt to service
9377 -- it. The following illustrates the various cases:
9379 -- procedure entE
9380 -- (O : System.Address;
9381 -- P : System.Address;
9382 -- E : Protected_Entry_Index)
9383 -- is
9384 -- <discriminant renamings>
9385 -- <private object renamings>
9386 -- type poVP is access poV;
9387 -- _object : ptVP := ptVP!(O);
9389 -- begin
9390 -- begin
9391 -- <start of statement sequence for entry>
9393 -- -- Requeue from one protected entry body to another protected
9394 -- -- entry.
9396 -- Requeue_Protected_Entry (
9397 -- _object._object'Access,
9398 -- new._object'Access,
9399 -- E,
9400 -- Abort_Present);
9401 -- return;
9403 -- <some more of the statement sequence for entry>
9405 -- -- Requeue from an entry body to a task entry
9407 -- Requeue_Protected_To_Task_Entry (
9408 -- New._task_id,
9409 -- E,
9410 -- Abort_Present);
9411 -- return;
9413 -- <rest of statement sequence for entry>
9414 -- Complete_Entry_Body (_object._object);
9416 -- exception
9417 -- when all others =>
9418 -- Exceptional_Complete_Entry_Body (
9419 -- _object._object, Get_GNAT_Exception);
9420 -- end;
9421 -- end entE;
9423 -- Requeue of a task entry call to a task entry
9425 -- Accept_Call (E, Ann);
9426 -- <start of statement sequence for accept statement>
9427 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9428 -- goto Lnn;
9429 -- <rest of statement sequence for accept statement>
9430 -- <<Lnn>>
9431 -- Complete_Rendezvous;
9433 -- exception
9434 -- when all others =>
9435 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9437 -- Requeue of a task entry call to a protected entry
9439 -- Accept_Call (E, Ann);
9440 -- <start of statement sequence for accept statement>
9441 -- Requeue_Task_To_Protected_Entry (
9442 -- new._object'Access,
9443 -- E,
9444 -- Abort_Present);
9445 -- newS (new, Pnn);
9446 -- goto Lnn;
9447 -- <rest of statement sequence for accept statement>
9448 -- <<Lnn>>
9449 -- Complete_Rendezvous;
9451 -- exception
9452 -- when all others =>
9453 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9455 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9456 -- marked by pragma Implemented (XXX, By_Entry).
9458 -- The requeue is inside a protected entry:
9460 -- procedure entE
9461 -- (O : System.Address;
9462 -- P : System.Address;
9463 -- E : Protected_Entry_Index)
9464 -- is
9465 -- <discriminant renamings>
9466 -- <private object renamings>
9467 -- type poVP is access poV;
9468 -- _object : ptVP := ptVP!(O);
9470 -- begin
9471 -- begin
9472 -- <start of statement sequence for entry>
9474 -- _Disp_Requeue
9475 -- (<interface class-wide object>,
9476 -- True,
9477 -- _object'Address,
9478 -- Ada.Tags.Get_Offset_Index
9479 -- (Tag (_object),
9480 -- <interface dispatch table index of target entry>),
9481 -- Abort_Present);
9482 -- return;
9484 -- <rest of statement sequence for entry>
9485 -- Complete_Entry_Body (_object._object);
9487 -- exception
9488 -- when all others =>
9489 -- Exceptional_Complete_Entry_Body (
9490 -- _object._object, Get_GNAT_Exception);
9491 -- end;
9492 -- end entE;
9494 -- The requeue is inside a task entry:
9496 -- Accept_Call (E, Ann);
9497 -- <start of statement sequence for accept statement>
9498 -- _Disp_Requeue
9499 -- (<interface class-wide object>,
9500 -- False,
9501 -- null,
9502 -- Ada.Tags.Get_Offset_Index
9503 -- (Tag (_object),
9504 -- <interface dispatch table index of target entrt>),
9505 -- Abort_Present);
9506 -- newS (new, Pnn);
9507 -- goto Lnn;
9508 -- <rest of statement sequence for accept statement>
9509 -- <<Lnn>>
9510 -- Complete_Rendezvous;
9512 -- exception
9513 -- when all others =>
9514 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9516 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9517 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9518 -- statement is replaced by a dispatching call with actual parameters taken
9519 -- from the inner-most accept statement or entry body.
9521 -- Target.Primitive (Param1, ..., ParamN);
9523 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9524 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9525 -- at all.
9527 -- declare
9528 -- S : constant Offset_Index :=
9529 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9530 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9532 -- begin
9533 -- if C = POK_Protected_Entry
9534 -- or else C = POK_Task_Entry
9535 -- then
9536 -- <statements for dispatching requeue>
9538 -- elsif C = POK_Protected_Procedure then
9539 -- <dispatching call equivalent>
9541 -- else
9542 -- raise Program_Error;
9543 -- end if;
9544 -- end;
9546 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9547 Loc : constant Source_Ptr := Sloc (N);
9548 Conc_Typ : Entity_Id;
9549 Concval : Node_Id;
9550 Ename : Node_Id;
9551 Index : Node_Id;
9552 Old_Typ : Entity_Id;
9554 function Build_Dispatching_Call_Equivalent return Node_Id;
9555 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9556 -- the form Concval.Ename. It is statically known that Ename is allowed
9557 -- to be implemented by a protected procedure. Create a dispatching call
9558 -- equivalent of Concval.Ename taking the actual parameters from the
9559 -- inner-most accept statement or entry body.
9561 function Build_Dispatching_Requeue return Node_Id;
9562 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9563 -- the form Concval.Ename. It is statically known that Ename is allowed
9564 -- to be implemented by a protected or a task entry. Create a call to
9565 -- primitive _Disp_Requeue which handles the low-level actions.
9567 function Build_Dispatching_Requeue_To_Any return Node_Id;
9568 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9569 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9570 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9571 -- determines at runtime whether Ename denotes an entry or a procedure
9572 -- and perform the appropriate kind of dispatching select.
9574 function Build_Normal_Requeue return Node_Id;
9575 -- N denotes a non-dispatching requeue statement to either a task or a
9576 -- protected entry. Build the appropriate runtime call to perform the
9577 -- action.
9579 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9580 -- For a protected entry, create a return statement to skip the rest of
9581 -- the entry body. Otherwise, create a goto statement to skip the rest
9582 -- of a task accept statement. The lookup for the enclosing entry body
9583 -- or accept statement starts from Search.
9585 ---------------------------------------
9586 -- Build_Dispatching_Call_Equivalent --
9587 ---------------------------------------
9589 function Build_Dispatching_Call_Equivalent return Node_Id is
9590 Call_Ent : constant Entity_Id := Entity (Ename);
9591 Obj : constant Node_Id := Original_Node (Concval);
9592 Acc_Ent : Node_Id;
9593 Actuals : List_Id;
9594 Formal : Node_Id;
9595 Formals : List_Id;
9597 begin
9598 -- Climb the parent chain looking for the inner-most entry body or
9599 -- accept statement.
9601 Acc_Ent := N;
9602 while Present (Acc_Ent)
9603 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9604 N_Entry_Body)
9605 loop
9606 Acc_Ent := Parent (Acc_Ent);
9607 end loop;
9609 -- A requeue statement should be housed inside an entry body or an
9610 -- accept statement at some level. If this is not the case, then the
9611 -- tree is malformed.
9613 pragma Assert (Present (Acc_Ent));
9615 -- Recover the list of formal parameters
9617 if Nkind (Acc_Ent) = N_Entry_Body then
9618 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9619 end if;
9621 Formals := Parameter_Specifications (Acc_Ent);
9623 -- Create the actual parameters for the dispatching call. These are
9624 -- simply copies of the entry body or accept statement formals in the
9625 -- same order as they appear.
9627 Actuals := No_List;
9629 if Present (Formals) then
9630 Actuals := New_List;
9631 Formal := First (Formals);
9632 while Present (Formal) loop
9633 Append_To (Actuals,
9634 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9635 Next (Formal);
9636 end loop;
9637 end if;
9639 -- Generate:
9640 -- Obj.Call_Ent (Actuals);
9642 return
9643 Make_Procedure_Call_Statement (Loc,
9644 Name =>
9645 Make_Selected_Component (Loc,
9646 Prefix => Make_Identifier (Loc, Chars (Obj)),
9647 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9649 Parameter_Associations => Actuals);
9650 end Build_Dispatching_Call_Equivalent;
9652 -------------------------------
9653 -- Build_Dispatching_Requeue --
9654 -------------------------------
9656 function Build_Dispatching_Requeue return Node_Id is
9657 Params : constant List_Id := New_List;
9659 begin
9660 -- Process the "with abort" parameter
9662 Prepend_To (Params,
9663 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9665 -- Process the entry wrapper's position in the primary dispatch
9666 -- table parameter. Generate:
9668 -- Ada.Tags.Get_Entry_Index
9669 -- (T => To_Tag_Ptr (Obj'Address).all,
9670 -- Position =>
9671 -- Ada.Tags.Get_Offset_Index
9672 -- (Ada.Tags.Tag (Concval),
9673 -- <interface dispatch table position of Ename>));
9675 -- Note that Obj'Address is recursively expanded into a call to
9676 -- Base_Address (Obj).
9678 if Tagged_Type_Expansion then
9679 Prepend_To (Params,
9680 Make_Function_Call (Loc,
9681 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9682 Parameter_Associations => New_List (
9684 Make_Explicit_Dereference (Loc,
9685 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9686 Make_Attribute_Reference (Loc,
9687 Prefix => New_Copy_Tree (Concval),
9688 Attribute_Name => Name_Address))),
9690 Make_Function_Call (Loc,
9691 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9692 Parameter_Associations => New_List (
9693 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9694 Make_Integer_Literal (Loc,
9695 DT_Position (Entity (Ename))))))));
9697 -- VM targets
9699 else
9700 Prepend_To (Params,
9701 Make_Function_Call (Loc,
9702 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9703 Parameter_Associations => New_List (
9705 Make_Attribute_Reference (Loc,
9706 Prefix => Concval,
9707 Attribute_Name => Name_Tag),
9709 Make_Function_Call (Loc,
9710 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9712 Parameter_Associations => New_List (
9714 -- Obj_Tag
9716 Make_Attribute_Reference (Loc,
9717 Prefix => Concval,
9718 Attribute_Name => Name_Tag),
9720 -- Tag_Typ
9722 Make_Attribute_Reference (Loc,
9723 Prefix => New_Reference_To (Etype (Concval), Loc),
9724 Attribute_Name => Name_Tag),
9726 -- Position
9728 Make_Integer_Literal (Loc,
9729 DT_Position (Entity (Ename))))))));
9730 end if;
9732 -- Specific actuals for protected to XXX requeue
9734 if Is_Protected_Type (Old_Typ) then
9735 Prepend_To (Params,
9736 Make_Attribute_Reference (Loc, -- _object'Address
9737 Prefix =>
9738 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9739 Attribute_Name => Name_Address));
9741 Prepend_To (Params, -- True
9742 New_Reference_To (Standard_True, Loc));
9744 -- Specific actuals for task to XXX requeue
9746 else
9747 pragma Assert (Is_Task_Type (Old_Typ));
9749 Prepend_To (Params, -- null
9750 New_Reference_To (RTE (RE_Null_Address), Loc));
9752 Prepend_To (Params, -- False
9753 New_Reference_To (Standard_False, Loc));
9754 end if;
9756 -- Add the object parameter
9758 Prepend_To (Params, New_Copy_Tree (Concval));
9760 -- Generate:
9761 -- _Disp_Requeue (<Params>);
9763 -- Find entity for Disp_Requeue operation, which belongs to
9764 -- the type and may not be directly visible.
9766 declare
9767 Elmt : Elmt_Id;
9768 Op : Entity_Id;
9770 begin
9771 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9772 while Present (Elmt) loop
9773 Op := Node (Elmt);
9774 exit when Chars (Op) = Name_uDisp_Requeue;
9775 Next_Elmt (Elmt);
9776 end loop;
9778 return
9779 Make_Procedure_Call_Statement (Loc,
9780 Name => New_Occurrence_Of (Op, Loc),
9781 Parameter_Associations => Params);
9782 end;
9783 end Build_Dispatching_Requeue;
9785 --------------------------------------
9786 -- Build_Dispatching_Requeue_To_Any --
9787 --------------------------------------
9789 function Build_Dispatching_Requeue_To_Any return Node_Id is
9790 Call_Ent : constant Entity_Id := Entity (Ename);
9791 Obj : constant Node_Id := Original_Node (Concval);
9792 Skip : constant Node_Id := Build_Skip_Statement (N);
9793 C : Entity_Id;
9794 Decls : List_Id;
9795 S : Entity_Id;
9796 Stmts : List_Id;
9798 begin
9799 Decls := New_List;
9800 Stmts := New_List;
9802 -- Dispatch table slot processing, generate:
9803 -- S : Integer;
9805 S := Build_S (Loc, Decls);
9807 -- Call kind processing, generate:
9808 -- C : Ada.Tags.Prim_Op_Kind;
9810 C := Build_C (Loc, Decls);
9812 -- Generate:
9813 -- S := Ada.Tags.Get_Offset_Index
9814 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9816 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9818 -- Generate:
9819 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9821 Append_To (Stmts,
9822 Make_Procedure_Call_Statement (Loc,
9823 Name =>
9824 New_Reference_To (
9825 Find_Prim_Op (Etype (Etype (Obj)),
9826 Name_uDisp_Get_Prim_Op_Kind),
9827 Loc),
9828 Parameter_Associations => New_List (
9829 New_Copy_Tree (Obj),
9830 New_Reference_To (S, Loc),
9831 New_Reference_To (C, Loc))));
9833 Append_To (Stmts,
9835 -- if C = POK_Protected_Entry
9836 -- or else C = POK_Task_Entry
9837 -- then
9839 Make_Implicit_If_Statement (N,
9840 Condition =>
9841 Make_Op_Or (Loc,
9842 Left_Opnd =>
9843 Make_Op_Eq (Loc,
9844 Left_Opnd =>
9845 New_Reference_To (C, Loc),
9846 Right_Opnd =>
9847 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
9849 Right_Opnd =>
9850 Make_Op_Eq (Loc,
9851 Left_Opnd =>
9852 New_Reference_To (C, Loc),
9853 Right_Opnd =>
9854 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
9856 -- Dispatching requeue equivalent
9858 Then_Statements => New_List (
9859 Build_Dispatching_Requeue,
9860 Skip),
9862 -- elsif C = POK_Protected_Procedure then
9864 Elsif_Parts => New_List (
9865 Make_Elsif_Part (Loc,
9866 Condition =>
9867 Make_Op_Eq (Loc,
9868 Left_Opnd =>
9869 New_Reference_To (C, Loc),
9870 Right_Opnd =>
9871 New_Reference_To (
9872 RTE (RE_POK_Protected_Procedure), Loc)),
9874 -- Dispatching call equivalent
9876 Then_Statements => New_List (
9877 Build_Dispatching_Call_Equivalent))),
9879 -- else
9880 -- raise Program_Error;
9881 -- end if;
9883 Else_Statements => New_List (
9884 Make_Raise_Program_Error (Loc,
9885 Reason => PE_Explicit_Raise))));
9887 -- Wrap everything into a block
9889 return
9890 Make_Block_Statement (Loc,
9891 Declarations => Decls,
9892 Handled_Statement_Sequence =>
9893 Make_Handled_Sequence_Of_Statements (Loc,
9894 Statements => Stmts));
9895 end Build_Dispatching_Requeue_To_Any;
9897 --------------------------
9898 -- Build_Normal_Requeue --
9899 --------------------------
9901 function Build_Normal_Requeue return Node_Id is
9902 Params : constant List_Id := New_List;
9903 Param : Node_Id;
9904 RT_Call : Node_Id;
9906 begin
9907 -- Process the "with abort" parameter
9909 Prepend_To (Params,
9910 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9912 -- Add the index expression to the parameters. It is common among all
9913 -- four cases.
9915 Prepend_To (Params,
9916 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
9918 if Is_Protected_Type (Old_Typ) then
9919 declare
9920 Self_Param : Node_Id;
9922 begin
9923 Self_Param :=
9924 Make_Attribute_Reference (Loc,
9925 Prefix =>
9926 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9927 Attribute_Name =>
9928 Name_Unchecked_Access);
9930 -- Protected to protected requeue
9932 if Is_Protected_Type (Conc_Typ) then
9933 RT_Call :=
9934 New_Reference_To (
9935 RTE (RE_Requeue_Protected_Entry), Loc);
9937 Param :=
9938 Make_Attribute_Reference (Loc,
9939 Prefix =>
9940 Concurrent_Ref (Concval),
9941 Attribute_Name =>
9942 Name_Unchecked_Access);
9944 -- Protected to task requeue
9946 else pragma Assert (Is_Task_Type (Conc_Typ));
9947 RT_Call :=
9948 New_Reference_To (
9949 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9951 Param := Concurrent_Ref (Concval);
9952 end if;
9954 Prepend_To (Params, Param);
9955 Prepend_To (Params, Self_Param);
9956 end;
9958 else pragma Assert (Is_Task_Type (Old_Typ));
9960 -- Task to protected requeue
9962 if Is_Protected_Type (Conc_Typ) then
9963 RT_Call :=
9964 New_Reference_To (
9965 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
9967 Param :=
9968 Make_Attribute_Reference (Loc,
9969 Prefix =>
9970 Concurrent_Ref (Concval),
9971 Attribute_Name =>
9972 Name_Unchecked_Access);
9974 -- Task to task requeue
9976 else pragma Assert (Is_Task_Type (Conc_Typ));
9977 RT_Call :=
9978 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
9980 Param := Concurrent_Ref (Concval);
9981 end if;
9983 Prepend_To (Params, Param);
9984 end if;
9986 return
9987 Make_Procedure_Call_Statement (Loc,
9988 Name => RT_Call,
9989 Parameter_Associations => Params);
9990 end Build_Normal_Requeue;
9992 --------------------------
9993 -- Build_Skip_Statement --
9994 --------------------------
9996 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
9997 Skip_Stmt : Node_Id;
9999 begin
10000 -- Build a return statement to skip the rest of the entire body
10002 if Is_Protected_Type (Old_Typ) then
10003 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10005 -- If the requeue is within a task, find the end label of the
10006 -- enclosing accept statement and create a goto statement to it.
10008 else
10009 declare
10010 Acc : Node_Id;
10011 Label : Node_Id;
10013 begin
10014 -- Climb the parent chain looking for the enclosing accept
10015 -- statement.
10017 Acc := Parent (Search);
10018 while Present (Acc)
10019 and then Nkind (Acc) /= N_Accept_Statement
10020 loop
10021 Acc := Parent (Acc);
10022 end loop;
10024 -- The last statement is the second label used for completing
10025 -- the rendezvous the usual way. The label we are looking for
10026 -- is right before it.
10028 Label :=
10029 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10031 pragma Assert (Nkind (Label) = N_Label);
10033 -- Generate a goto statement to skip the rest of the accept
10035 Skip_Stmt :=
10036 Make_Goto_Statement (Loc,
10037 Name =>
10038 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10039 end;
10040 end if;
10042 Set_Analyzed (Skip_Stmt);
10044 return Skip_Stmt;
10045 end Build_Skip_Statement;
10047 -- Start of processing for Expand_N_Requeue_Statement
10049 begin
10050 -- Extract the components of the entry call
10052 Extract_Entry (N, Concval, Ename, Index);
10053 Conc_Typ := Etype (Concval);
10055 -- If the prefix is an access to class-wide type, dereference to get
10056 -- object and entry type.
10058 if Is_Access_Type (Conc_Typ) then
10059 Conc_Typ := Designated_Type (Conc_Typ);
10060 Rewrite (Concval,
10061 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10062 Analyze_And_Resolve (Concval, Conc_Typ);
10063 end if;
10065 -- Examine the scope stack in order to find nearest enclosing protected
10066 -- or task type. This will constitute our invocation source.
10068 Old_Typ := Current_Scope;
10069 while Present (Old_Typ)
10070 and then not Is_Protected_Type (Old_Typ)
10071 and then not Is_Task_Type (Old_Typ)
10072 loop
10073 Old_Typ := Scope (Old_Typ);
10074 end loop;
10076 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10077 -- Concval.Ename where the type of Concval is class-wide concurrent
10078 -- interface.
10080 if Ada_Version >= Ada_2012
10081 and then Present (Concval)
10082 and then Is_Class_Wide_Type (Conc_Typ)
10083 and then Is_Concurrent_Interface (Conc_Typ)
10084 then
10085 declare
10086 Has_Impl : Boolean := False;
10087 Impl_Kind : Name_Id := No_Name;
10089 begin
10090 -- Check whether the Ename is flagged by pragma Implemented
10092 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10093 Has_Impl := True;
10094 Impl_Kind := Implementation_Kind (Entity (Ename));
10095 end if;
10097 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10098 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10100 if Has_Impl
10101 and then Impl_Kind = Name_By_Entry
10102 then
10103 Rewrite (N, Build_Dispatching_Requeue);
10104 Analyze (N);
10105 Insert_After (N, Build_Skip_Statement (N));
10107 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10108 -- a protected procedure. In this case the requeue is transformed
10109 -- into a dispatching call.
10111 elsif Has_Impl
10112 and then Impl_Kind = Name_By_Protected_Procedure
10113 then
10114 Rewrite (N, Build_Dispatching_Call_Equivalent);
10115 Analyze (N);
10117 -- The procedure_or_entry_NAME's implementation kind is either
10118 -- By_Any, Optional, or pragma Implemented was not applied at all.
10119 -- In this case a runtime test determines whether Ename denotes an
10120 -- entry or a protected procedure and performs the appropriate
10121 -- call.
10123 else
10124 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10125 Analyze (N);
10126 end if;
10127 end;
10129 -- Processing for regular (non-dispatching) requeues
10131 else
10132 Rewrite (N, Build_Normal_Requeue);
10133 Analyze (N);
10134 Insert_After (N, Build_Skip_Statement (N));
10135 end if;
10136 end Expand_N_Requeue_Statement;
10138 -------------------------------
10139 -- Expand_N_Selective_Accept --
10140 -------------------------------
10142 procedure Expand_N_Selective_Accept (N : Node_Id) is
10143 Loc : constant Source_Ptr := Sloc (N);
10144 Alts : constant List_Id := Select_Alternatives (N);
10146 -- Note: in the below declarations a lot of new lists are allocated
10147 -- unconditionally which may well not end up being used. That's not
10148 -- a good idea since it wastes space gratuitously ???
10150 Accept_Case : List_Id;
10151 Accept_List : constant List_Id := New_List;
10153 Alt : Node_Id;
10154 Alt_List : constant List_Id := New_List;
10155 Alt_Stats : List_Id;
10156 Ann : Entity_Id := Empty;
10158 Check_Guard : Boolean := True;
10160 Decls : constant List_Id := New_List;
10161 Stats : constant List_Id := New_List;
10162 Body_List : constant List_Id := New_List;
10163 Trailing_List : constant List_Id := New_List;
10165 Choices : List_Id;
10166 Else_Present : Boolean := False;
10167 Terminate_Alt : Node_Id := Empty;
10168 Select_Mode : Node_Id;
10170 Delay_Case : List_Id;
10171 Delay_Count : Integer := 0;
10172 Delay_Val : Entity_Id;
10173 Delay_Index : Entity_Id;
10174 Delay_Min : Entity_Id;
10175 Delay_Num : Int := 1;
10176 Delay_Alt_List : List_Id := New_List;
10177 Delay_List : constant List_Id := New_List;
10178 D : Entity_Id;
10179 M : Entity_Id;
10181 First_Delay : Boolean := True;
10182 Guard_Open : Entity_Id;
10184 End_Lab : Node_Id;
10185 Index : Int := 1;
10186 Lab : Node_Id;
10187 Num_Alts : Int;
10188 Num_Accept : Nat := 0;
10189 Proc : Node_Id;
10190 Time_Type : Entity_Id;
10191 Select_Call : Node_Id;
10193 Qnam : constant Entity_Id :=
10194 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10196 Xnam : constant Entity_Id :=
10197 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10199 -----------------------
10200 -- Local subprograms --
10201 -----------------------
10203 function Accept_Or_Raise return List_Id;
10204 -- For the rare case where delay alternatives all have guards, and
10205 -- all of them are closed, it is still possible that there were open
10206 -- accept alternatives with no callers. We must reexamine the
10207 -- Accept_List, and execute a selective wait with no else if some
10208 -- accept is open. If none, we raise program_error.
10210 procedure Add_Accept (Alt : Node_Id);
10211 -- Process a single accept statement in a select alternative. Build
10212 -- procedure for body of accept, and add entry to dispatch table with
10213 -- expression for guard, in preparation for call to run time select.
10215 function Make_And_Declare_Label (Num : Int) return Node_Id;
10216 -- Manufacture a label using Num as a serial number and declare it.
10217 -- The declaration is appended to Decls. The label marks the trailing
10218 -- statements of an accept or delay alternative.
10220 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10221 -- Build call to Selective_Wait runtime routine
10223 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10224 -- Add code to compare value of delay with previous values, and
10225 -- generate case entry for trailing statements.
10227 procedure Process_Accept_Alternative
10228 (Alt : Node_Id;
10229 Index : Int;
10230 Proc : Node_Id);
10231 -- Add code to call corresponding procedure, and branch to
10232 -- trailing statements, if any.
10234 ---------------------
10235 -- Accept_Or_Raise --
10236 ---------------------
10238 function Accept_Or_Raise return List_Id is
10239 Cond : Node_Id;
10240 Stats : List_Id;
10241 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10243 begin
10244 -- We generate the following:
10246 -- for J in q'range loop
10247 -- if q(J).S /=null_task_entry then
10248 -- selective_wait (simple_mode,...);
10249 -- done := True;
10250 -- exit;
10251 -- end if;
10252 -- end loop;
10254 -- if no rendez_vous then
10255 -- raise program_error;
10256 -- end if;
10258 -- Note that the code needs to know that the selector name
10259 -- in an Accept_Alternative is named S.
10261 Cond := Make_Op_Ne (Loc,
10262 Left_Opnd =>
10263 Make_Selected_Component (Loc,
10264 Prefix =>
10265 Make_Indexed_Component (Loc,
10266 Prefix => New_Reference_To (Qnam, Loc),
10267 Expressions => New_List (New_Reference_To (J, Loc))),
10268 Selector_Name => Make_Identifier (Loc, Name_S)),
10269 Right_Opnd =>
10270 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
10272 Stats := New_List (
10273 Make_Implicit_Loop_Statement (N,
10274 Iteration_Scheme =>
10275 Make_Iteration_Scheme (Loc,
10276 Loop_Parameter_Specification =>
10277 Make_Loop_Parameter_Specification (Loc,
10278 Defining_Identifier => J,
10279 Discrete_Subtype_Definition =>
10280 Make_Attribute_Reference (Loc,
10281 Prefix => New_Reference_To (Qnam, Loc),
10282 Attribute_Name => Name_Range,
10283 Expressions => New_List (
10284 Make_Integer_Literal (Loc, 1))))),
10286 Statements => New_List (
10287 Make_Implicit_If_Statement (N,
10288 Condition => Cond,
10289 Then_Statements => New_List (
10290 Make_Select_Call (
10291 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
10292 Make_Exit_Statement (Loc))))));
10294 Append_To (Stats,
10295 Make_Raise_Program_Error (Loc,
10296 Condition => Make_Op_Eq (Loc,
10297 Left_Opnd => New_Reference_To (Xnam, Loc),
10298 Right_Opnd =>
10299 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10300 Reason => PE_All_Guards_Closed));
10302 return Stats;
10303 end Accept_Or_Raise;
10305 ----------------
10306 -- Add_Accept --
10307 ----------------
10309 procedure Add_Accept (Alt : Node_Id) is
10310 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10311 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10312 Eloc : constant Source_Ptr := Sloc (Ename);
10313 Eent : constant Entity_Id := Entity (Ename);
10314 Index : constant Node_Id := Entry_Index (Acc_Stm);
10315 Null_Body : Node_Id;
10316 Proc_Body : Node_Id;
10317 PB_Ent : Entity_Id;
10318 Expr : Node_Id;
10319 Call : Node_Id;
10321 begin
10322 if No (Ann) then
10323 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10324 end if;
10326 if Present (Condition (Alt)) then
10327 Expr :=
10328 Make_If_Expression (Eloc, New_List (
10329 Condition (Alt),
10330 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10331 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
10332 else
10333 Expr :=
10334 Entry_Index_Expression
10335 (Eloc, Eent, Index, Scope (Eent));
10336 end if;
10338 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10339 Null_Body := New_Reference_To (Standard_False, Eloc);
10341 if Abort_Allowed then
10342 Call := Make_Procedure_Call_Statement (Eloc,
10343 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
10344 Insert_Before (First (Statements (Handled_Statement_Sequence (
10345 Accept_Statement (Alt)))), Call);
10346 Analyze (Call);
10347 end if;
10349 PB_Ent :=
10350 Make_Defining_Identifier (Eloc,
10351 New_External_Name (Chars (Ename), 'A', Num_Accept));
10353 if Comes_From_Source (Alt) then
10354 Set_Debug_Info_Needed (PB_Ent);
10355 end if;
10357 Proc_Body :=
10358 Make_Subprogram_Body (Eloc,
10359 Specification =>
10360 Make_Procedure_Specification (Eloc,
10361 Defining_Unit_Name => PB_Ent),
10362 Declarations => Declarations (Acc_Stm),
10363 Handled_Statement_Sequence =>
10364 Build_Accept_Body (Accept_Statement (Alt)));
10366 -- During the analysis of the body of the accept statement, any
10367 -- zero cost exception handler records were collected in the
10368 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10369 -- This is where we move them to where they belong, namely the
10370 -- newly created procedure.
10372 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10373 Append (Proc_Body, Body_List);
10375 else
10376 Null_Body := New_Reference_To (Standard_True, Eloc);
10378 -- if accept statement has declarations, insert above, given that
10379 -- we are not creating a body for the accept.
10381 if Present (Declarations (Acc_Stm)) then
10382 Insert_Actions (N, Declarations (Acc_Stm));
10383 end if;
10384 end if;
10386 Append_To (Accept_List,
10387 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10389 Num_Accept := Num_Accept + 1;
10390 end Add_Accept;
10392 ----------------------------
10393 -- Make_And_Declare_Label --
10394 ----------------------------
10396 function Make_And_Declare_Label (Num : Int) return Node_Id is
10397 Lab_Id : Node_Id;
10399 begin
10400 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10401 Lab :=
10402 Make_Label (Loc, Lab_Id);
10404 Append_To (Decls,
10405 Make_Implicit_Label_Declaration (Loc,
10406 Defining_Identifier =>
10407 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10408 Label_Construct => Lab));
10410 return Lab;
10411 end Make_And_Declare_Label;
10413 ----------------------
10414 -- Make_Select_Call --
10415 ----------------------
10417 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10418 Params : constant List_Id := New_List;
10420 begin
10421 Append (
10422 Make_Attribute_Reference (Loc,
10423 Prefix => New_Reference_To (Qnam, Loc),
10424 Attribute_Name => Name_Unchecked_Access),
10425 Params);
10426 Append (Select_Mode, Params);
10427 Append (New_Reference_To (Ann, Loc), Params);
10428 Append (New_Reference_To (Xnam, Loc), Params);
10430 return
10431 Make_Procedure_Call_Statement (Loc,
10432 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
10433 Parameter_Associations => Params);
10434 end Make_Select_Call;
10436 --------------------------------
10437 -- Process_Accept_Alternative --
10438 --------------------------------
10440 procedure Process_Accept_Alternative
10441 (Alt : Node_Id;
10442 Index : Int;
10443 Proc : Node_Id)
10445 Astmt : constant Node_Id := Accept_Statement (Alt);
10446 Alt_Stats : List_Id;
10448 begin
10449 Adjust_Condition (Condition (Alt));
10451 -- Accept with body
10453 if Present (Handled_Statement_Sequence (Astmt)) then
10454 Alt_Stats :=
10455 New_List (
10456 Make_Procedure_Call_Statement (Sloc (Proc),
10457 Name =>
10458 New_Reference_To
10459 (Defining_Unit_Name (Specification (Proc)),
10460 Sloc (Proc))));
10462 -- Accept with no body (followed by trailing statements)
10464 else
10465 Alt_Stats := Empty_List;
10466 end if;
10468 Ensure_Statement_Present (Sloc (Astmt), Alt);
10470 -- After the call, if any, branch to trailing statements, if any.
10471 -- We create a label for each, as well as the corresponding label
10472 -- declaration.
10474 if not Is_Empty_List (Statements (Alt)) then
10475 Lab := Make_And_Declare_Label (Index);
10476 Append (Lab, Trailing_List);
10477 Append_List (Statements (Alt), Trailing_List);
10478 Append_To (Trailing_List,
10479 Make_Goto_Statement (Loc,
10480 Name => New_Copy (Identifier (End_Lab))));
10482 else
10483 Lab := End_Lab;
10484 end if;
10486 Append_To (Alt_Stats,
10487 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10489 Append_To (Alt_List,
10490 Make_Case_Statement_Alternative (Loc,
10491 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10492 Statements => Alt_Stats));
10493 end Process_Accept_Alternative;
10495 -------------------------------
10496 -- Process_Delay_Alternative --
10497 -------------------------------
10499 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10500 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10501 Cond : Node_Id;
10502 Delay_Alt : List_Id;
10504 begin
10505 -- Deal with C/Fortran boolean as delay condition
10507 Adjust_Condition (Condition (Alt));
10509 -- Determine the smallest specified delay
10511 -- for each delay alternative generate:
10513 -- if guard-expression then
10514 -- Delay_Val := delay-expression;
10515 -- Guard_Open := True;
10516 -- if Delay_Val < Delay_Min then
10517 -- Delay_Min := Delay_Val;
10518 -- Delay_Index := Index;
10519 -- end if;
10520 -- end if;
10522 -- The enclosing if-statement is omitted if there is no guard
10524 if Delay_Count = 1 or else First_Delay then
10525 First_Delay := False;
10527 Delay_Alt := New_List (
10528 Make_Assignment_Statement (Loc,
10529 Name => New_Reference_To (Delay_Min, Loc),
10530 Expression => Expression (Delay_Statement (Alt))));
10532 if Delay_Count > 1 then
10533 Append_To (Delay_Alt,
10534 Make_Assignment_Statement (Loc,
10535 Name => New_Reference_To (Delay_Index, Loc),
10536 Expression => Make_Integer_Literal (Loc, Index)));
10537 end if;
10539 else
10540 Delay_Alt := New_List (
10541 Make_Assignment_Statement (Loc,
10542 Name => New_Reference_To (Delay_Val, Loc),
10543 Expression => Expression (Delay_Statement (Alt))));
10545 if Time_Type = Standard_Duration then
10546 Cond :=
10547 Make_Op_Lt (Loc,
10548 Left_Opnd => New_Reference_To (Delay_Val, Loc),
10549 Right_Opnd => New_Reference_To (Delay_Min, Loc));
10551 else
10552 -- The scope of the time type must define a comparison
10553 -- operator. The scope itself may not be visible, so we
10554 -- construct a node with entity information to insure that
10555 -- semantic analysis can find the proper operator.
10557 Cond :=
10558 Make_Function_Call (Loc,
10559 Name => Make_Selected_Component (Loc,
10560 Prefix =>
10561 New_Reference_To (Scope (Time_Type), Loc),
10562 Selector_Name =>
10563 Make_Operator_Symbol (Loc,
10564 Chars => Name_Op_Lt,
10565 Strval => No_String)),
10566 Parameter_Associations =>
10567 New_List (
10568 New_Reference_To (Delay_Val, Loc),
10569 New_Reference_To (Delay_Min, Loc)));
10571 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10572 end if;
10574 Append_To (Delay_Alt,
10575 Make_Implicit_If_Statement (N,
10576 Condition => Cond,
10577 Then_Statements => New_List (
10578 Make_Assignment_Statement (Loc,
10579 Name => New_Reference_To (Delay_Min, Loc),
10580 Expression => New_Reference_To (Delay_Val, Loc)),
10582 Make_Assignment_Statement (Loc,
10583 Name => New_Reference_To (Delay_Index, Loc),
10584 Expression => Make_Integer_Literal (Loc, Index)))));
10585 end if;
10587 if Check_Guard then
10588 Append_To (Delay_Alt,
10589 Make_Assignment_Statement (Loc,
10590 Name => New_Reference_To (Guard_Open, Loc),
10591 Expression => New_Reference_To (Standard_True, Loc)));
10592 end if;
10594 if Present (Condition (Alt)) then
10595 Delay_Alt := New_List (
10596 Make_Implicit_If_Statement (N,
10597 Condition => Condition (Alt),
10598 Then_Statements => Delay_Alt));
10599 end if;
10601 Append_List (Delay_Alt, Delay_List);
10603 Ensure_Statement_Present (Dloc, Alt);
10605 -- If the delay alternative has a statement part, add choice to the
10606 -- case statements for delays.
10608 if not Is_Empty_List (Statements (Alt)) then
10610 if Delay_Count = 1 then
10611 Append_List (Statements (Alt), Delay_Alt_List);
10613 else
10614 Append_To (Delay_Alt_List,
10615 Make_Case_Statement_Alternative (Loc,
10616 Discrete_Choices => New_List (
10617 Make_Integer_Literal (Loc, Index)),
10618 Statements => Statements (Alt)));
10619 end if;
10621 elsif Delay_Count = 1 then
10623 -- If the single delay has no trailing statements, add a branch
10624 -- to the exit label to the selective wait.
10626 Delay_Alt_List := New_List (
10627 Make_Goto_Statement (Loc,
10628 Name => New_Copy (Identifier (End_Lab))));
10630 end if;
10631 end Process_Delay_Alternative;
10633 -- Start of processing for Expand_N_Selective_Accept
10635 begin
10636 Process_Statements_For_Controlled_Objects (N);
10638 -- First insert some declarations before the select. The first is:
10640 -- Ann : Address
10642 -- This variable holds the parameters passed to the accept body. This
10643 -- declaration has already been inserted by the time we get here by
10644 -- a call to Expand_Accept_Declarations made from the semantics when
10645 -- processing the first accept statement contained in the select. We
10646 -- can find this entity as Accept_Address (E), where E is any of the
10647 -- entries references by contained accept statements.
10649 -- The first step is to scan the list of Selective_Accept_Statements
10650 -- to find this entity, and also count the number of accepts, and
10651 -- determine if terminated, delay or else is present:
10653 Num_Alts := 0;
10655 Alt := First (Alts);
10656 while Present (Alt) loop
10657 Process_Statements_For_Controlled_Objects (Alt);
10659 if Nkind (Alt) = N_Accept_Alternative then
10660 Add_Accept (Alt);
10662 elsif Nkind (Alt) = N_Delay_Alternative then
10663 Delay_Count := Delay_Count + 1;
10665 -- If the delays are relative delays, the delay expressions have
10666 -- type Standard_Duration. Otherwise they must have some time type
10667 -- recognized by GNAT.
10669 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10670 Time_Type := Standard_Duration;
10671 else
10672 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10674 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10675 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10676 then
10677 null;
10678 else
10679 Error_Msg_NE (
10680 "& is not a time type (RM 9.6(6))",
10681 Expression (Delay_Statement (Alt)), Time_Type);
10682 Time_Type := Standard_Duration;
10683 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10684 end if;
10685 end if;
10687 if No (Condition (Alt)) then
10689 -- This guard will always be open
10691 Check_Guard := False;
10692 end if;
10694 elsif Nkind (Alt) = N_Terminate_Alternative then
10695 Adjust_Condition (Condition (Alt));
10696 Terminate_Alt := Alt;
10697 end if;
10699 Num_Alts := Num_Alts + 1;
10700 Next (Alt);
10701 end loop;
10703 Else_Present := Present (Else_Statements (N));
10705 -- At the same time (see procedure Add_Accept) we build the accept list:
10707 -- Qnn : Accept_List (1 .. num-select) := (
10708 -- (null-body, entry-index),
10709 -- (null-body, entry-index),
10710 -- ..
10711 -- (null_body, entry-index));
10713 -- In the above declaration, null-body is True if the corresponding
10714 -- accept has no body, and false otherwise. The entry is either the
10715 -- entry index expression if there is no guard, or if a guard is
10716 -- present, then an if expression of the form:
10718 -- (if guard then entry-index else Null_Task_Entry)
10720 -- If a guard is statically known to be false, the entry can simply
10721 -- be omitted from the accept list.
10723 Append_To (Decls,
10724 Make_Object_Declaration (Loc,
10725 Defining_Identifier => Qnam,
10726 Object_Definition => New_Reference_To (RTE (RE_Accept_List), Loc),
10727 Aliased_Present => True,
10728 Expression =>
10729 Make_Qualified_Expression (Loc,
10730 Subtype_Mark =>
10731 New_Reference_To (RTE (RE_Accept_List), Loc),
10732 Expression =>
10733 Make_Aggregate (Loc, Expressions => Accept_List))));
10735 -- Then we declare the variable that holds the index for the accept
10736 -- that will be selected for service:
10738 -- Xnn : Select_Index;
10740 Append_To (Decls,
10741 Make_Object_Declaration (Loc,
10742 Defining_Identifier => Xnam,
10743 Object_Definition =>
10744 New_Reference_To (RTE (RE_Select_Index), Loc),
10745 Expression =>
10746 New_Reference_To (RTE (RE_No_Rendezvous), Loc)));
10748 -- After this follow procedure declarations for each accept body
10750 -- procedure Pnn is
10751 -- begin
10752 -- ...
10753 -- end;
10755 -- where the ... are statements from the corresponding procedure body.
10756 -- No parameters are involved, since the parameters are passed via Ann
10757 -- and the parameter references have already been expanded to be direct
10758 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10759 -- any embedded tasking statements (which would normally be illegal in
10760 -- procedures), have been converted to calls to the tasking runtime so
10761 -- there is no problem in putting them into procedures.
10763 -- The original accept statement has been expanded into a block in
10764 -- the same fashion as for simple accepts (see Build_Accept_Body).
10766 -- Note: we don't really need to build these procedures for the case
10767 -- where no delay statement is present, but it is just as easy to
10768 -- build them unconditionally, and not significantly inefficient,
10769 -- since if they are short they will be inlined anyway.
10771 -- The procedure declarations have been assembled in Body_List
10773 -- If delays are present, we must compute the required delay.
10774 -- We first generate the declarations:
10776 -- Delay_Index : Boolean := 0;
10777 -- Delay_Min : Some_Time_Type.Time;
10778 -- Delay_Val : Some_Time_Type.Time;
10780 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10781 -- active delay that is actually chosen as the basis for the possible
10782 -- delay if an immediate rendez-vous is not possible.
10784 -- In the most common case there is a single delay statement, and this
10785 -- is handled specially.
10787 if Delay_Count > 0 then
10789 -- Generate the required declarations
10791 Delay_Val :=
10792 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10793 Delay_Index :=
10794 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10795 Delay_Min :=
10796 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10798 Append_To (Decls,
10799 Make_Object_Declaration (Loc,
10800 Defining_Identifier => Delay_Val,
10801 Object_Definition => New_Reference_To (Time_Type, Loc)));
10803 Append_To (Decls,
10804 Make_Object_Declaration (Loc,
10805 Defining_Identifier => Delay_Index,
10806 Object_Definition => New_Reference_To (Standard_Integer, Loc),
10807 Expression => Make_Integer_Literal (Loc, 0)));
10809 Append_To (Decls,
10810 Make_Object_Declaration (Loc,
10811 Defining_Identifier => Delay_Min,
10812 Object_Definition => New_Reference_To (Time_Type, Loc),
10813 Expression =>
10814 Unchecked_Convert_To (Time_Type,
10815 Make_Attribute_Reference (Loc,
10816 Prefix =>
10817 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10818 Attribute_Name => Name_Last))));
10820 -- Create Duration and Delay_Mode objects used for passing a delay
10821 -- value to RTS
10823 D := Make_Temporary (Loc, 'D');
10824 M := Make_Temporary (Loc, 'M');
10826 declare
10827 Discr : Entity_Id;
10829 begin
10830 -- Note that these values are defined in s-osprim.ads and must
10831 -- be kept in sync:
10833 -- Relative : constant := 0;
10834 -- Absolute_Calendar : constant := 1;
10835 -- Absolute_RT : constant := 2;
10837 if Time_Type = Standard_Duration then
10838 Discr := Make_Integer_Literal (Loc, 0);
10840 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10841 Discr := Make_Integer_Literal (Loc, 1);
10843 else
10844 pragma Assert
10845 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10846 Discr := Make_Integer_Literal (Loc, 2);
10847 end if;
10849 Append_To (Decls,
10850 Make_Object_Declaration (Loc,
10851 Defining_Identifier => D,
10852 Object_Definition =>
10853 New_Reference_To (Standard_Duration, Loc)));
10855 Append_To (Decls,
10856 Make_Object_Declaration (Loc,
10857 Defining_Identifier => M,
10858 Object_Definition =>
10859 New_Reference_To (Standard_Integer, Loc),
10860 Expression => Discr));
10861 end;
10863 if Check_Guard then
10864 Guard_Open :=
10865 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
10867 Append_To (Decls,
10868 Make_Object_Declaration (Loc,
10869 Defining_Identifier => Guard_Open,
10870 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10871 Expression => New_Reference_To (Standard_False, Loc)));
10872 end if;
10874 -- Delay_Count is zero, don't need M and D set (suppress warning)
10876 else
10877 M := Empty;
10878 D := Empty;
10879 end if;
10881 if Present (Terminate_Alt) then
10883 -- If the terminate alternative guard is False, use
10884 -- Simple_Mode; otherwise use Terminate_Mode.
10886 if Present (Condition (Terminate_Alt)) then
10887 Select_Mode := Make_If_Expression (Loc,
10888 New_List (Condition (Terminate_Alt),
10889 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
10890 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
10891 else
10892 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
10893 end if;
10895 elsif Else_Present or Delay_Count > 0 then
10896 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
10898 else
10899 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
10900 end if;
10902 Select_Call := Make_Select_Call (Select_Mode);
10903 Append (Select_Call, Stats);
10905 -- Now generate code to act on the result. There is an entry
10906 -- in this case for each accept statement with a non-null body,
10907 -- followed by a branch to the statements that follow the Accept.
10908 -- In the absence of delay alternatives, we generate:
10910 -- case X is
10911 -- when No_Rendezvous => -- omitted if simple mode
10912 -- goto Lab0;
10914 -- when 1 =>
10915 -- P1n;
10916 -- goto Lab1;
10918 -- when 2 =>
10919 -- P2n;
10920 -- goto Lab2;
10922 -- when others =>
10923 -- goto Exit;
10924 -- end case;
10926 -- Lab0: Else_Statements;
10927 -- goto exit;
10929 -- Lab1: Trailing_Statements1;
10930 -- goto Exit;
10932 -- Lab2: Trailing_Statements2;
10933 -- goto Exit;
10934 -- ...
10935 -- Exit:
10937 -- Generate label for common exit
10939 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
10941 -- First entry is the default case, when no rendezvous is possible
10943 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10945 if Else_Present then
10947 -- If no rendezvous is possible, the else part is executed
10949 Lab := Make_And_Declare_Label (0);
10950 Alt_Stats := New_List (
10951 Make_Goto_Statement (Loc,
10952 Name => New_Copy (Identifier (Lab))));
10954 Append (Lab, Trailing_List);
10955 Append_List (Else_Statements (N), Trailing_List);
10956 Append_To (Trailing_List,
10957 Make_Goto_Statement (Loc,
10958 Name => New_Copy (Identifier (End_Lab))));
10959 else
10960 Alt_Stats := New_List (
10961 Make_Goto_Statement (Loc,
10962 Name => New_Copy (Identifier (End_Lab))));
10963 end if;
10965 Append_To (Alt_List,
10966 Make_Case_Statement_Alternative (Loc,
10967 Discrete_Choices => Choices,
10968 Statements => Alt_Stats));
10970 -- We make use of the fact that Accept_Index is an integer type, and
10971 -- generate successive literals for entries for each accept. Only those
10972 -- for which there is a body or trailing statements get a case entry.
10974 Alt := First (Select_Alternatives (N));
10975 Proc := First (Body_List);
10976 while Present (Alt) loop
10978 if Nkind (Alt) = N_Accept_Alternative then
10979 Process_Accept_Alternative (Alt, Index, Proc);
10980 Index := Index + 1;
10982 if Present
10983 (Handled_Statement_Sequence (Accept_Statement (Alt)))
10984 then
10985 Next (Proc);
10986 end if;
10988 elsif Nkind (Alt) = N_Delay_Alternative then
10989 Process_Delay_Alternative (Alt, Delay_Num);
10990 Delay_Num := Delay_Num + 1;
10991 end if;
10993 Next (Alt);
10994 end loop;
10996 -- An others choice is always added to the main case, as well
10997 -- as the delay case (to satisfy the compiler).
10999 Append_To (Alt_List,
11000 Make_Case_Statement_Alternative (Loc,
11001 Discrete_Choices =>
11002 New_List (Make_Others_Choice (Loc)),
11003 Statements =>
11004 New_List (Make_Goto_Statement (Loc,
11005 Name => New_Copy (Identifier (End_Lab))))));
11007 Accept_Case := New_List (
11008 Make_Case_Statement (Loc,
11009 Expression => New_Reference_To (Xnam, Loc),
11010 Alternatives => Alt_List));
11012 Append_List (Trailing_List, Accept_Case);
11013 Append_List (Body_List, Decls);
11015 -- Construct case statement for trailing statements of delay
11016 -- alternatives, if there are several of them.
11018 if Delay_Count > 1 then
11019 Append_To (Delay_Alt_List,
11020 Make_Case_Statement_Alternative (Loc,
11021 Discrete_Choices =>
11022 New_List (Make_Others_Choice (Loc)),
11023 Statements =>
11024 New_List (Make_Null_Statement (Loc))));
11026 Delay_Case := New_List (
11027 Make_Case_Statement (Loc,
11028 Expression => New_Reference_To (Delay_Index, Loc),
11029 Alternatives => Delay_Alt_List));
11030 else
11031 Delay_Case := Delay_Alt_List;
11032 end if;
11034 -- If there are no delay alternatives, we append the case statement
11035 -- to the statement list.
11037 if Delay_Count = 0 then
11038 Append_List (Accept_Case, Stats);
11040 -- Delay alternatives present
11042 else
11043 -- If delay alternatives are present we generate:
11045 -- find minimum delay.
11046 -- DX := minimum delay;
11047 -- M := <delay mode>;
11048 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11049 -- DX, MX, X);
11051 -- if X = No_Rendezvous then
11052 -- case statement for delay statements.
11053 -- else
11054 -- case statement for accept alternatives.
11055 -- end if;
11057 declare
11058 Cases : Node_Id;
11059 Stmt : Node_Id;
11060 Parms : List_Id;
11061 Parm : Node_Id;
11062 Conv : Node_Id;
11064 begin
11065 -- The type of the delay expression is known to be legal
11067 if Time_Type = Standard_Duration then
11068 Conv := New_Reference_To (Delay_Min, Loc);
11070 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11071 Conv := Make_Function_Call (Loc,
11072 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
11073 New_List (New_Reference_To (Delay_Min, Loc)));
11075 else
11076 pragma Assert
11077 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11079 Conv := Make_Function_Call (Loc,
11080 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
11081 New_List (New_Reference_To (Delay_Min, Loc)));
11082 end if;
11084 Stmt := Make_Assignment_Statement (Loc,
11085 Name => New_Reference_To (D, Loc),
11086 Expression => Conv);
11088 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11090 Parms := Parameter_Associations (Select_Call);
11091 Parm := First (Parms);
11093 while Present (Parm) and then Parm /= Select_Mode loop
11094 Next (Parm);
11095 end loop;
11097 pragma Assert (Present (Parm));
11098 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
11099 Analyze (Parm);
11101 -- Prepare two new parameters of Duration and Delay_Mode type
11102 -- which represent the value and the mode of the minimum delay.
11104 Next (Parm);
11105 Insert_After (Parm, New_Reference_To (M, Loc));
11106 Insert_After (Parm, New_Reference_To (D, Loc));
11108 -- Create a call to RTS
11110 Rewrite (Select_Call,
11111 Make_Procedure_Call_Statement (Loc,
11112 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
11113 Parameter_Associations => Parms));
11115 -- This new call should follow the calculation of the minimum
11116 -- delay.
11118 Insert_List_Before (Select_Call, Delay_List);
11120 if Check_Guard then
11121 Stmt :=
11122 Make_Implicit_If_Statement (N,
11123 Condition => New_Reference_To (Guard_Open, Loc),
11124 Then_Statements => New_List (
11125 New_Copy_Tree (Stmt),
11126 New_Copy_Tree (Select_Call)),
11127 Else_Statements => Accept_Or_Raise);
11128 Rewrite (Select_Call, Stmt);
11129 else
11130 Insert_Before (Select_Call, Stmt);
11131 end if;
11133 Cases :=
11134 Make_Implicit_If_Statement (N,
11135 Condition => Make_Op_Eq (Loc,
11136 Left_Opnd => New_Reference_To (Xnam, Loc),
11137 Right_Opnd =>
11138 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
11140 Then_Statements => Delay_Case,
11141 Else_Statements => Accept_Case);
11143 Append (Cases, Stats);
11144 end;
11145 end if;
11146 Append (End_Lab, Stats);
11148 -- Replace accept statement with appropriate block
11150 Rewrite (N,
11151 Make_Block_Statement (Loc,
11152 Declarations => Decls,
11153 Handled_Statement_Sequence =>
11154 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11155 Analyze (N);
11157 -- Note: have to worry more about abort deferral in above code ???
11159 -- Final step is to unstack the Accept_Address entries for all accept
11160 -- statements appearing in accept alternatives in the select statement
11162 Alt := First (Alts);
11163 while Present (Alt) loop
11164 if Nkind (Alt) = N_Accept_Alternative then
11165 Remove_Last_Elmt (Accept_Address
11166 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11167 end if;
11169 Next (Alt);
11170 end loop;
11171 end Expand_N_Selective_Accept;
11173 --------------------------------------
11174 -- Expand_N_Single_Task_Declaration --
11175 --------------------------------------
11177 -- Single task declarations should never be present after semantic
11178 -- analysis, since we expect them to be replaced by a declaration of an
11179 -- anonymous task type, followed by a declaration of the task object. We
11180 -- include this routine to make sure that is happening!
11182 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11183 begin
11184 raise Program_Error;
11185 end Expand_N_Single_Task_Declaration;
11187 ------------------------
11188 -- Expand_N_Task_Body --
11189 ------------------------
11191 -- Given a task body
11193 -- task body tname is
11194 -- <declarations>
11195 -- begin
11196 -- <statements>
11197 -- end x;
11199 -- This expansion routine converts it into a procedure and sets the
11200 -- elaboration flag for the procedure to true, to represent the fact
11201 -- that the task body is now elaborated:
11203 -- procedure tnameB (_Task : access tnameV) is
11204 -- discriminal : dtype renames _Task.discriminant;
11206 -- procedure _clean is
11207 -- begin
11208 -- Abort_Defer.all;
11209 -- Complete_Task;
11210 -- Abort_Undefer.all;
11211 -- return;
11212 -- end _clean;
11214 -- begin
11215 -- Abort_Undefer.all;
11216 -- <declarations>
11217 -- System.Task_Stages.Complete_Activation;
11218 -- <statements>
11219 -- at end
11220 -- _clean;
11221 -- end tnameB;
11223 -- tnameE := True;
11225 -- In addition, if the task body is an activator, then a call to activate
11226 -- tasks is added at the start of the statements, before the call to
11227 -- Complete_Activation, and if in addition the task is a master then it
11228 -- must be established as a master. These calls are inserted and analyzed
11229 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11230 -- expanded.
11232 -- There is one discriminal declaration line generated for each
11233 -- discriminant that is present to provide an easy reference point for
11234 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11236 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11237 -- task body procedures have a profile (Arg : System.Address). That is
11238 -- needed because GNARLI has to use the same access-to-subprogram type
11239 -- for all task types. We depend here on knowing that in GNAT, passing
11240 -- an address argument by value is identical to passing a record value
11241 -- by access (in either case a single pointer is passed), so even though
11242 -- this procedure has the wrong profile. In fact it's all OK, since the
11243 -- callings sequence is identical.
11245 procedure Expand_N_Task_Body (N : Node_Id) is
11246 Loc : constant Source_Ptr := Sloc (N);
11247 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11248 Call : Node_Id;
11249 New_N : Node_Id;
11251 Insert_Nod : Node_Id;
11252 -- Used to determine the proper location of wrapper body insertions
11254 begin
11255 -- Add renaming declarations for discriminals and a declaration for the
11256 -- entry family index (if applicable).
11258 Install_Private_Data_Declarations
11259 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11261 -- Add a call to Abort_Undefer at the very beginning of the task
11262 -- body since this body is called with abort still deferred.
11264 if Abort_Allowed then
11265 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11266 Insert_Before
11267 (First (Statements (Handled_Statement_Sequence (N))), Call);
11268 Analyze (Call);
11269 end if;
11271 -- The statement part has already been protected with an at_end and
11272 -- cleanup actions. The call to Complete_Activation must be placed
11273 -- at the head of the sequence of statements of that block. The
11274 -- declarations have been merged in this sequence of statements but
11275 -- the first real statement is accessible from the First_Real_Statement
11276 -- field (which was set for exactly this purpose).
11278 if Restricted_Profile then
11279 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11280 else
11281 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11282 end if;
11284 Insert_Before
11285 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11286 Analyze (Call);
11288 New_N :=
11289 Make_Subprogram_Body (Loc,
11290 Specification => Build_Task_Proc_Specification (Ttyp),
11291 Declarations => Declarations (N),
11292 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11294 -- If the task contains generic instantiations, cleanup actions are
11295 -- delayed until after instantiation. Transfer the activation chain to
11296 -- the subprogram, to insure that the activation call is properly
11297 -- generated. It the task body contains inner tasks, indicate that the
11298 -- subprogram is a task master.
11300 if Delay_Cleanups (Ttyp) then
11301 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11302 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11303 end if;
11305 Rewrite (N, New_N);
11306 Analyze (N);
11308 -- Set elaboration flag immediately after task body. If the body is a
11309 -- subunit, the flag is set in the declarative part containing the stub.
11311 if Nkind (Parent (N)) /= N_Subunit then
11312 Insert_After (N,
11313 Make_Assignment_Statement (Loc,
11314 Name =>
11315 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11316 Expression => New_Reference_To (Standard_True, Loc)));
11317 end if;
11319 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11320 -- the task body. At this point all wrapper specs have been created,
11321 -- frozen and included in the dispatch table for the task type.
11323 if Ada_Version >= Ada_2005 then
11324 if Nkind (Parent (N)) = N_Subunit then
11325 Insert_Nod := Corresponding_Stub (Parent (N));
11326 else
11327 Insert_Nod := N;
11328 end if;
11330 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11331 end if;
11332 end Expand_N_Task_Body;
11334 ------------------------------------
11335 -- Expand_N_Task_Type_Declaration --
11336 ------------------------------------
11338 -- We have several things to do. First we must create a Boolean flag used
11339 -- to mark if the body is elaborated yet. This variable gets set to True
11340 -- when the body of the task is elaborated (we can't rely on the normal
11341 -- ABE mechanism for the task body, since we need to pass an access to
11342 -- this elaboration boolean to the runtime routines).
11344 -- taskE : aliased Boolean := False;
11346 -- Next a variable is declared to hold the task stack size (either the
11347 -- default : Unspecified_Size, or a value that is set by a pragma
11348 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11349 -- the variable is initialized with this value:
11351 -- taskZ : Size_Type := Unspecified_Size;
11352 -- or
11353 -- taskZ : Size_Type := Size_Type (size_expression);
11355 -- Note: No variable is needed to hold the task relative deadline since
11356 -- its value would never be static because the parameter is of a private
11357 -- type (Ada.Real_Time.Time_Span).
11359 -- Next we create a corresponding record type declaration used to represent
11360 -- values of this task. The general form of this type declaration is
11362 -- type taskV (discriminants) is record
11363 -- _Task_Id : Task_Id;
11364 -- entry_family : array (bounds) of Void;
11365 -- _Priority : Integer := priority_expression;
11366 -- _Size : Size_Type := size_expression;
11367 -- _Task_Info : Task_Info_Type := task_info_expression;
11368 -- _CPU : Integer := cpu_range_expression;
11369 -- _Relative_Deadline : Time_Span := time_span_expression;
11370 -- _Domain : Dispatching_Domain := dd_expression;
11371 -- end record;
11373 -- The discriminants are present only if the corresponding task type has
11374 -- discriminants, and they exactly mirror the task type discriminants.
11376 -- The Id field is always present. It contains the Task_Id value, as set by
11377 -- the call to Create_Task. Note that although the task is limited, the
11378 -- task value record type is not limited, so there is no problem in passing
11379 -- this field as an out parameter to Create_Task.
11381 -- One entry_family component is present for each entry family in the task
11382 -- definition. The bounds correspond to the bounds of the entry family
11383 -- (which may depend on discriminants). The element type is void, since we
11384 -- only need the bounds information for determining the entry index. Note
11385 -- that the use of an anonymous array would normally be illegal in this
11386 -- context, but this is a parser check, and the semantics is quite prepared
11387 -- to handle such a case.
11389 -- The _Size field is present only if a Storage_Size pragma appears in the
11390 -- task definition. The expression captures the argument that was present
11391 -- in the pragma, and is used to override the task stack size otherwise
11392 -- associated with the task type.
11394 -- The _Priority field is present only if the task entity has a Priority or
11395 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11396 -- definition clause). It will be filled at the freeze point, when the
11397 -- record init proc is built, to capture the expression of the rep item
11398 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11399 -- here since aspect evaluations are delayed till the freeze point.
11401 -- The _Task_Info field is present only if a Task_Info pragma appears in
11402 -- the task definition. The expression captures the argument that was
11403 -- present in the pragma, and is used to provide the Task_Image parameter
11404 -- to the call to Create_Task.
11406 -- The _CPU field is present only if the task entity has a CPU rep item
11407 -- (pragma, aspect specification or attribute definition clause). It will
11408 -- be filled at the freeze point, when the record init proc is built, to
11409 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11410 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11411 -- are delayed till the freeze point.
11413 -- The _Relative_Deadline field is present only if a Relative_Deadline
11414 -- pragma appears in the task definition. The expression captures the
11415 -- argument that was present in the pragma, and is used to provide the
11416 -- Relative_Deadline parameter to the call to Create_Task.
11418 -- The _Domain field is present only if the task entity has a
11419 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11420 -- definition clause). It will be filled at the freeze point, when the
11421 -- record init proc is built, to capture the expression of the rep item
11422 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11423 -- here since aspect evaluations are delayed till the freeze point.
11425 -- When a task is declared, an instance of the task value record is
11426 -- created. The elaboration of this declaration creates the correct bounds
11427 -- for the entry families, and also evaluates the size, priority, and
11428 -- task_Info expressions if needed. The initialization routine for the task
11429 -- type itself then calls Create_Task with appropriate parameters to
11430 -- initialize the value of the Task_Id field.
11432 -- Note: the address of this record is passed as the "Discriminants"
11433 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11434 -- body procedure, it does not matter that it does not quite match the
11435 -- GNARLI model of what is being passed (the record contains more than just
11436 -- the discriminants, but the discriminants can be found from the record
11437 -- value).
11439 -- The Entity_Id for this created record type is placed in the
11440 -- Corresponding_Record_Type field of the associated task type entity.
11442 -- Next we create a procedure specification for the task body procedure:
11444 -- procedure taskB (_Task : access taskV);
11446 -- Note that this must come after the record type declaration, since
11447 -- the spec refers to this type. It turns out that the initialization
11448 -- procedure for the value type references the task body spec, but that's
11449 -- fine, since it won't be generated till the freeze point for the type,
11450 -- which is certainly after the task body spec declaration.
11452 -- Finally, we set the task index value field of the entry attribute in
11453 -- the case of a simple entry.
11455 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11456 Loc : constant Source_Ptr := Sloc (N);
11457 TaskId : constant Entity_Id := Defining_Identifier (N);
11458 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11459 Tasknm : constant Name_Id := Chars (Tasktyp);
11460 Taskdef : constant Node_Id := Task_Definition (N);
11462 Body_Decl : Node_Id;
11463 Cdecls : List_Id;
11464 Decl_Stack : Node_Id;
11465 Elab_Decl : Node_Id;
11466 Ent_Stack : Entity_Id;
11467 Proc_Spec : Node_Id;
11468 Rec_Decl : Node_Id;
11469 Rec_Ent : Entity_Id;
11470 Size_Decl : Entity_Id;
11471 Task_Size : Node_Id;
11473 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11474 -- Searches the task definition T for the first occurrence of the pragma
11475 -- Relative Deadline. The caller has ensured that the pragma is present
11476 -- in the task definition. Note that this routine cannot be implemented
11477 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11478 -- not chained because their expansion into a procedure call statement
11479 -- would cause a break in the chain.
11481 ----------------------------------
11482 -- Get_Relative_Deadline_Pragma --
11483 ----------------------------------
11485 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11486 N : Node_Id;
11488 begin
11489 N := First (Visible_Declarations (T));
11490 while Present (N) loop
11491 if Nkind (N) = N_Pragma
11492 and then Pragma_Name (N) = Name_Relative_Deadline
11493 then
11494 return N;
11495 end if;
11497 Next (N);
11498 end loop;
11500 N := First (Private_Declarations (T));
11501 while Present (N) loop
11502 if Nkind (N) = N_Pragma
11503 and then Pragma_Name (N) = Name_Relative_Deadline
11504 then
11505 return N;
11506 end if;
11508 Next (N);
11509 end loop;
11511 raise Program_Error;
11512 end Get_Relative_Deadline_Pragma;
11514 -- Start of processing for Expand_N_Task_Type_Declaration
11516 begin
11517 -- If already expanded, nothing to do
11519 if Present (Corresponding_Record_Type (Tasktyp)) then
11520 return;
11521 end if;
11523 -- Here we will do the expansion
11525 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11527 Rec_Ent := Defining_Identifier (Rec_Decl);
11528 Cdecls := Component_Items (Component_List
11529 (Type_Definition (Rec_Decl)));
11531 Qualify_Entity_Names (N);
11533 -- First create the elaboration variable
11535 Elab_Decl :=
11536 Make_Object_Declaration (Loc,
11537 Defining_Identifier =>
11538 Make_Defining_Identifier (Sloc (Tasktyp),
11539 Chars => New_External_Name (Tasknm, 'E')),
11540 Aliased_Present => True,
11541 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
11542 Expression => New_Reference_To (Standard_False, Loc));
11544 Insert_After (N, Elab_Decl);
11546 -- Next create the declaration of the size variable (tasknmZ)
11548 Set_Storage_Size_Variable (Tasktyp,
11549 Make_Defining_Identifier (Sloc (Tasktyp),
11550 Chars => New_External_Name (Tasknm, 'Z')));
11552 if Present (Taskdef)
11553 and then Has_Storage_Size_Pragma (Taskdef)
11554 and then
11555 Is_Static_Expression
11556 (Expression
11557 (First (Pragma_Argument_Associations
11558 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11559 then
11560 Size_Decl :=
11561 Make_Object_Declaration (Loc,
11562 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11563 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
11564 Expression =>
11565 Convert_To (RTE (RE_Size_Type),
11566 Relocate_Node
11567 (Expression (First (Pragma_Argument_Associations
11568 (Get_Rep_Pragma
11569 (TaskId, Name_Storage_Size)))))));
11571 else
11572 Size_Decl :=
11573 Make_Object_Declaration (Loc,
11574 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11575 Object_Definition =>
11576 New_Reference_To (RTE (RE_Size_Type), Loc),
11577 Expression =>
11578 New_Reference_To (RTE (RE_Unspecified_Size), Loc));
11579 end if;
11581 Insert_After (Elab_Decl, Size_Decl);
11583 -- Next build the rest of the corresponding record declaration. This is
11584 -- done last, since the corresponding record initialization procedure
11585 -- will reference the previously created entities.
11587 -- Fill in the component declarations -- first the _Task_Id field
11589 Append_To (Cdecls,
11590 Make_Component_Declaration (Loc,
11591 Defining_Identifier =>
11592 Make_Defining_Identifier (Loc, Name_uTask_Id),
11593 Component_Definition =>
11594 Make_Component_Definition (Loc,
11595 Aliased_Present => False,
11596 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
11597 Loc))));
11599 -- Declare static ATCB (that is, created by the expander) if we are
11600 -- using the Restricted run time.
11602 if Restricted_Profile then
11603 Append_To (Cdecls,
11604 Make_Component_Declaration (Loc,
11605 Defining_Identifier =>
11606 Make_Defining_Identifier (Loc, Name_uATCB),
11608 Component_Definition =>
11609 Make_Component_Definition (Loc,
11610 Aliased_Present => True,
11611 Subtype_Indication => Make_Subtype_Indication (Loc,
11612 Subtype_Mark =>
11613 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11615 Constraint =>
11616 Make_Index_Or_Discriminant_Constraint (Loc,
11617 Constraints =>
11618 New_List (Make_Integer_Literal (Loc, 0)))))));
11620 end if;
11622 -- Declare static stack (that is, created by the expander) if we are
11623 -- using the Restricted run time on a bare board configuration.
11625 if Restricted_Profile
11626 and then Preallocated_Stacks_On_Target
11627 then
11628 -- First we need to extract the appropriate stack size
11630 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11632 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11633 declare
11634 Expr_N : constant Node_Id :=
11635 Expression (First (
11636 Pragma_Argument_Associations (
11637 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11638 Etyp : constant Entity_Id := Etype (Expr_N);
11639 P : constant Node_Id := Parent (Expr_N);
11641 begin
11642 -- The stack is defined inside the corresponding record.
11643 -- Therefore if the size of the stack is set by means of
11644 -- a discriminant, we must reference the discriminant of the
11645 -- corresponding record type.
11647 if Nkind (Expr_N) in N_Has_Entity
11648 and then Present (Discriminal_Link (Entity (Expr_N)))
11649 then
11650 Task_Size :=
11651 New_Reference_To
11652 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11653 Loc);
11654 Set_Parent (Task_Size, P);
11655 Set_Etype (Task_Size, Etyp);
11656 Set_Analyzed (Task_Size);
11658 else
11659 Task_Size := Relocate_Node (Expr_N);
11660 end if;
11661 end;
11663 else
11664 Task_Size :=
11665 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
11666 end if;
11668 Decl_Stack := Make_Component_Declaration (Loc,
11669 Defining_Identifier => Ent_Stack,
11671 Component_Definition =>
11672 Make_Component_Definition (Loc,
11673 Aliased_Present => True,
11674 Subtype_Indication => Make_Subtype_Indication (Loc,
11675 Subtype_Mark =>
11676 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11678 Constraint =>
11679 Make_Index_Or_Discriminant_Constraint (Loc,
11680 Constraints => New_List (Make_Range (Loc,
11681 Low_Bound => Make_Integer_Literal (Loc, 1),
11682 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11683 Task_Size)))))));
11685 Append_To (Cdecls, Decl_Stack);
11687 -- The appropriate alignment for the stack is ensured by the run-time
11688 -- code in charge of task creation.
11690 end if;
11692 -- Add components for entry families
11694 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11696 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11697 -- item is present.
11699 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11700 Append_To (Cdecls,
11701 Make_Component_Declaration (Loc,
11702 Defining_Identifier =>
11703 Make_Defining_Identifier (Loc, Name_uPriority),
11704 Component_Definition =>
11705 Make_Component_Definition (Loc,
11706 Aliased_Present => False,
11707 Subtype_Indication =>
11708 New_Reference_To (Standard_Integer, Loc))));
11709 end if;
11711 -- Add the _Size component if a Storage_Size pragma is present
11713 if Present (Taskdef)
11714 and then Has_Storage_Size_Pragma (Taskdef)
11715 then
11716 Append_To (Cdecls,
11717 Make_Component_Declaration (Loc,
11718 Defining_Identifier =>
11719 Make_Defining_Identifier (Loc, Name_uSize),
11721 Component_Definition =>
11722 Make_Component_Definition (Loc,
11723 Aliased_Present => False,
11724 Subtype_Indication =>
11725 New_Reference_To (RTE (RE_Size_Type), Loc)),
11727 Expression =>
11728 Convert_To (RTE (RE_Size_Type),
11729 Relocate_Node (
11730 Expression (First (
11731 Pragma_Argument_Associations (
11732 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11733 end if;
11735 -- Add the _Task_Info component if a Task_Info pragma is present
11737 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11738 Append_To (Cdecls,
11739 Make_Component_Declaration (Loc,
11740 Defining_Identifier =>
11741 Make_Defining_Identifier (Loc, Name_uTask_Info),
11743 Component_Definition =>
11744 Make_Component_Definition (Loc,
11745 Aliased_Present => False,
11746 Subtype_Indication =>
11747 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
11749 Expression => New_Copy (
11750 Expression (First (
11751 Pragma_Argument_Associations (
11752 Get_Rep_Pragma
11753 (TaskId, Name_Task_Info, Check_Parents => False)))))));
11754 end if;
11756 -- Add the _CPU component if a CPU rep item is present
11758 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
11759 Append_To (Cdecls,
11760 Make_Component_Declaration (Loc,
11761 Defining_Identifier =>
11762 Make_Defining_Identifier (Loc, Name_uCPU),
11764 Component_Definition =>
11765 Make_Component_Definition (Loc,
11766 Aliased_Present => False,
11767 Subtype_Indication =>
11768 New_Reference_To (RTE (RE_CPU_Range), Loc))));
11769 end if;
11771 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11772 -- present. If we are using a restricted run time this component will
11773 -- not be added (deadlines are not allowed by the Ravenscar profile).
11775 if not Restricted_Profile
11776 and then Present (Taskdef)
11777 and then Has_Relative_Deadline_Pragma (Taskdef)
11778 then
11779 Append_To (Cdecls,
11780 Make_Component_Declaration (Loc,
11781 Defining_Identifier =>
11782 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11784 Component_Definition =>
11785 Make_Component_Definition (Loc,
11786 Aliased_Present => False,
11787 Subtype_Indication =>
11788 New_Reference_To (RTE (RE_Time_Span), Loc)),
11790 Expression =>
11791 Convert_To (RTE (RE_Time_Span),
11792 Relocate_Node (
11793 Expression (First (
11794 Pragma_Argument_Associations (
11795 Get_Relative_Deadline_Pragma (Taskdef))))))));
11796 end if;
11798 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
11799 -- item is present. If we are using a restricted run time this component
11800 -- will not be added (dispatching domains are not allowed by the
11801 -- Ravenscar profile).
11803 if not Restricted_Profile
11804 and then
11805 Has_Rep_Item
11806 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
11807 then
11808 Append_To (Cdecls,
11809 Make_Component_Declaration (Loc,
11810 Defining_Identifier =>
11811 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
11813 Component_Definition =>
11814 Make_Component_Definition (Loc,
11815 Aliased_Present => False,
11816 Subtype_Indication =>
11817 New_Reference_To
11818 (RTE (RE_Dispatching_Domain_Access), Loc))));
11819 end if;
11821 Insert_After (Size_Decl, Rec_Decl);
11823 -- Analyze the record declaration immediately after construction,
11824 -- because the initialization procedure is needed for single task
11825 -- declarations before the next entity is analyzed.
11827 Analyze (Rec_Decl);
11829 -- Create the declaration of the task body procedure
11831 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
11832 Body_Decl :=
11833 Make_Subprogram_Declaration (Loc,
11834 Specification => Proc_Spec);
11836 Insert_After (Rec_Decl, Body_Decl);
11838 -- The subprogram does not comes from source, so we have to indicate the
11839 -- need for debugging information explicitly.
11841 if Comes_From_Source (Original_Node (N)) then
11842 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
11843 end if;
11845 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
11846 -- the corresponding record has been frozen.
11848 if Ada_Version >= Ada_2005 then
11849 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
11850 end if;
11852 -- Ada 2005 (AI-345): We must defer freezing to allow further
11853 -- declaration of primitive subprograms covering task interfaces
11855 if Ada_Version <= Ada_95 then
11857 -- Now we can freeze the corresponding record. This needs manually
11858 -- freezing, since it is really part of the task type, and the task
11859 -- type is frozen at this stage. We of course need the initialization
11860 -- procedure for this corresponding record type and we won't get it
11861 -- in time if we don't freeze now.
11863 declare
11864 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
11865 begin
11866 if Is_Non_Empty_List (L) then
11867 Insert_List_After (Body_Decl, L);
11868 end if;
11869 end;
11870 end if;
11872 -- Complete the expansion of access types to the current task type, if
11873 -- any were declared.
11875 Expand_Previous_Access_Type (Tasktyp);
11877 -- Create wrappers for entries that have pre/postconditions
11879 declare
11880 Ent : Entity_Id;
11882 begin
11883 Ent := First_Entity (Tasktyp);
11884 while Present (Ent) loop
11885 if Ekind_In (Ent, E_Entry, E_Entry_Family)
11886 and then Present (Pre_Post_Conditions (Contract (Ent)))
11887 then
11888 Build_PPC_Wrapper (Ent, N);
11889 end if;
11891 Next_Entity (Ent);
11892 end loop;
11893 end;
11894 end Expand_N_Task_Type_Declaration;
11896 -------------------------------
11897 -- Expand_N_Timed_Entry_Call --
11898 -------------------------------
11900 -- A timed entry call in normal case is not implemented using ATC mechanism
11901 -- anymore for efficiency reason.
11903 -- select
11904 -- T.E;
11905 -- S1;
11906 -- or
11907 -- delay D;
11908 -- S2;
11909 -- end select;
11911 -- is expanded as follows:
11913 -- 1) When T.E is a task entry_call;
11915 -- declare
11916 -- B : Boolean;
11917 -- X : Task_Entry_Index := <entry index>;
11918 -- DX : Duration := To_Duration (D);
11919 -- M : Delay_Mode := <discriminant>;
11920 -- P : parms := (parm, parm, parm);
11922 -- begin
11923 -- Timed_Protected_Entry_Call
11924 -- (<acceptor-task>, X, P'Address, DX, M, B);
11925 -- if B then
11926 -- S1;
11927 -- else
11928 -- S2;
11929 -- end if;
11930 -- end;
11932 -- 2) When T.E is a protected entry_call;
11934 -- declare
11935 -- B : Boolean;
11936 -- X : Protected_Entry_Index := <entry index>;
11937 -- DX : Duration := To_Duration (D);
11938 -- M : Delay_Mode := <discriminant>;
11939 -- P : parms := (parm, parm, parm);
11941 -- begin
11942 -- Timed_Protected_Entry_Call
11943 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
11944 -- if B then
11945 -- S1;
11946 -- else
11947 -- S2;
11948 -- end if;
11949 -- end;
11951 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
11952 -- is no delay and the triggering statements are executed. We first
11953 -- determine the kind of of the triggering call and then execute a
11954 -- synchronized operation or a direct call.
11956 -- declare
11957 -- B : Boolean := False;
11958 -- C : Ada.Tags.Prim_Op_Kind;
11959 -- DX : Duration := To_Duration (D)
11960 -- K : Ada.Tags.Tagged_Kind :=
11961 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
11962 -- M : Integer :=...;
11963 -- P : Parameters := (Param1 .. ParamN);
11964 -- S : Integer;
11966 -- begin
11967 -- if K = Ada.Tags.TK_Limited_Tagged
11968 -- or else K = Ada.Tags.TK_Tagged
11969 -- then
11970 -- <dispatching-call>;
11971 -- B := True;
11973 -- else
11974 -- S :=
11975 -- Ada.Tags.Get_Offset_Index
11976 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
11978 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
11980 -- if C = POK_Protected_Entry
11981 -- or else C = POK_Task_Entry
11982 -- then
11983 -- Param1 := P.Param1;
11984 -- ...
11985 -- ParamN := P.ParamN;
11986 -- end if;
11988 -- if B then
11989 -- if C = POK_Procedure
11990 -- or else C = POK_Protected_Procedure
11991 -- or else C = POK_Task_Procedure
11992 -- then
11993 -- <dispatching-call>;
11994 -- end if;
11995 -- end if;
11996 -- end if;
11998 -- if B then
11999 -- <triggering-statements>
12000 -- else
12001 -- <timed-statements>
12002 -- end if;
12003 -- end;
12005 -- The triggering statement and the sequence of timed statements have not
12006 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12007 -- global references if within an instantiation.
12009 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12010 Loc : constant Source_Ptr := Sloc (N);
12012 Actuals : List_Id;
12013 Blk_Typ : Entity_Id;
12014 Call : Node_Id;
12015 Call_Ent : Entity_Id;
12016 Conc_Typ_Stmts : List_Id;
12017 Concval : Node_Id;
12018 D_Alt : constant Node_Id := Delay_Alternative (N);
12019 D_Conv : Node_Id;
12020 D_Disc : Node_Id;
12021 D_Stat : Node_Id := Delay_Statement (D_Alt);
12022 D_Stats : List_Id;
12023 D_Type : Entity_Id;
12024 Decls : List_Id;
12025 Dummy : Node_Id;
12026 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12027 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12028 E_Stats : List_Id;
12029 Ename : Node_Id;
12030 Formals : List_Id;
12031 Index : Node_Id;
12032 Is_Disp_Select : Boolean;
12033 Lim_Typ_Stmts : List_Id;
12034 N_Stats : List_Id;
12035 Obj : Entity_Id;
12036 Param : Node_Id;
12037 Params : List_Id;
12038 Stmt : Node_Id;
12039 Stmts : List_Id;
12040 Unpack : List_Id;
12042 B : Entity_Id; -- Call status flag
12043 C : Entity_Id; -- Call kind
12044 D : Entity_Id; -- Delay
12045 K : Entity_Id; -- Tagged kind
12046 M : Entity_Id; -- Delay mode
12047 P : Entity_Id; -- Parameter block
12048 S : Entity_Id; -- Primitive operation slot
12050 -- Start of processing for Expand_N_Timed_Entry_Call
12052 begin
12053 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12054 -- was already reported on spec, so do not attempt to expand the call.
12056 if Restriction_Active (No_Select_Statements) then
12057 return;
12058 end if;
12060 Process_Statements_For_Controlled_Objects (E_Alt);
12061 Process_Statements_For_Controlled_Objects (D_Alt);
12063 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12065 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12066 -- may wrap them in blocks.
12068 E_Stats := Statements (E_Alt);
12069 D_Stats := Statements (D_Alt);
12071 -- The arguments in the call may require dynamic allocation, and the
12072 -- call statement may have been transformed into a block. The block
12073 -- may contain additional declarations for internal entities, and the
12074 -- original call is found by sequential search.
12076 if Nkind (E_Call) = N_Block_Statement then
12077 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12078 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12079 N_Entry_Call_Statement)
12080 loop
12081 Next (E_Call);
12082 end loop;
12083 end if;
12085 Is_Disp_Select :=
12086 Ada_Version >= Ada_2005
12087 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12089 if Is_Disp_Select then
12090 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12091 Decls := New_List;
12093 Stmts := New_List;
12095 -- Generate:
12096 -- B : Boolean := False;
12098 B := Build_B (Loc, Decls);
12100 -- Generate:
12101 -- C : Ada.Tags.Prim_Op_Kind;
12103 C := Build_C (Loc, Decls);
12105 -- Because the analysis of all statements was disabled, manually
12106 -- analyze the delay statement.
12108 Analyze (D_Stat);
12109 D_Stat := Original_Node (D_Stat);
12111 else
12112 -- Build an entry call using Simple_Entry_Call
12114 Extract_Entry (E_Call, Concval, Ename, Index);
12115 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12117 Decls := Declarations (E_Call);
12118 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12120 if No (Decls) then
12121 Decls := New_List;
12122 end if;
12124 -- Generate:
12125 -- B : Boolean;
12127 B := Make_Defining_Identifier (Loc, Name_uB);
12129 Prepend_To (Decls,
12130 Make_Object_Declaration (Loc,
12131 Defining_Identifier => B,
12132 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
12133 end if;
12135 -- Duration and mode processing
12137 D_Type := Base_Type (Etype (Expression (D_Stat)));
12139 -- Use the type of the delay expression (Calendar or Real_Time) to
12140 -- generate the appropriate conversion.
12142 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12143 D_Disc := Make_Integer_Literal (Loc, 0);
12144 D_Conv := Relocate_Node (Expression (D_Stat));
12146 elsif Is_RTE (D_Type, RO_CA_Time) then
12147 D_Disc := Make_Integer_Literal (Loc, 1);
12148 D_Conv :=
12149 Make_Function_Call (Loc,
12150 Name => New_Reference_To (RTE (RO_CA_To_Duration), Loc),
12151 Parameter_Associations =>
12152 New_List (New_Copy (Expression (D_Stat))));
12154 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12155 D_Disc := Make_Integer_Literal (Loc, 2);
12156 D_Conv :=
12157 Make_Function_Call (Loc,
12158 Name => New_Reference_To (RTE (RO_RT_To_Duration), Loc),
12159 Parameter_Associations =>
12160 New_List (New_Copy (Expression (D_Stat))));
12161 end if;
12163 D := Make_Temporary (Loc, 'D');
12165 -- Generate:
12166 -- D : Duration;
12168 Append_To (Decls,
12169 Make_Object_Declaration (Loc,
12170 Defining_Identifier => D,
12171 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
12173 M := Make_Temporary (Loc, 'M');
12175 -- Generate:
12176 -- M : Integer := (0 | 1 | 2);
12178 Append_To (Decls,
12179 Make_Object_Declaration (Loc,
12180 Defining_Identifier => M,
12181 Object_Definition => New_Reference_To (Standard_Integer, Loc),
12182 Expression => D_Disc));
12184 -- Do the assignment at this stage only because the evaluation of the
12185 -- expression must not occur before (see ACVC C97302A).
12187 Append_To (Stmts,
12188 Make_Assignment_Statement (Loc,
12189 Name => New_Reference_To (D, Loc),
12190 Expression => D_Conv));
12192 -- Parameter block processing
12194 -- Manually create the parameter block for dispatching calls. In the
12195 -- case of entries, the block has already been created during the call
12196 -- to Build_Simple_Entry_Call.
12198 if Is_Disp_Select then
12200 -- Tagged kind processing, generate:
12201 -- K : Ada.Tags.Tagged_Kind :=
12202 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12204 K := Build_K (Loc, Decls, Obj);
12206 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12207 P :=
12208 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12210 -- Dispatch table slot processing, generate:
12211 -- S : Integer;
12213 S := Build_S (Loc, Decls);
12215 -- Generate:
12216 -- S := Ada.Tags.Get_Offset_Index
12217 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12219 Conc_Typ_Stmts :=
12220 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12222 -- Generate:
12223 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12225 -- where Obj is the controlling formal parameter, S is the dispatch
12226 -- table slot number of the dispatching operation, P is the wrapped
12227 -- parameter block, D is the duration, M is the duration mode, C is
12228 -- the call kind and B is the call status.
12230 Params := New_List;
12232 Append_To (Params, New_Copy_Tree (Obj));
12233 Append_To (Params, New_Reference_To (S, Loc));
12234 Append_To (Params,
12235 Make_Attribute_Reference (Loc,
12236 Prefix => New_Reference_To (P, Loc),
12237 Attribute_Name => Name_Address));
12238 Append_To (Params, New_Reference_To (D, Loc));
12239 Append_To (Params, New_Reference_To (M, Loc));
12240 Append_To (Params, New_Reference_To (C, Loc));
12241 Append_To (Params, New_Reference_To (B, Loc));
12243 Append_To (Conc_Typ_Stmts,
12244 Make_Procedure_Call_Statement (Loc,
12245 Name =>
12246 New_Reference_To
12247 (Find_Prim_Op
12248 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12249 Parameter_Associations => Params));
12251 -- Generate:
12252 -- if C = POK_Protected_Entry
12253 -- or else C = POK_Task_Entry
12254 -- then
12255 -- Param1 := P.Param1;
12256 -- ...
12257 -- ParamN := P.ParamN;
12258 -- end if;
12260 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12262 -- Generate the if statement only when the packed parameters need
12263 -- explicit assignments to their corresponding actuals.
12265 if Present (Unpack) then
12266 Append_To (Conc_Typ_Stmts,
12267 Make_Implicit_If_Statement (N,
12269 Condition =>
12270 Make_Or_Else (Loc,
12271 Left_Opnd =>
12272 Make_Op_Eq (Loc,
12273 Left_Opnd => New_Reference_To (C, Loc),
12274 Right_Opnd =>
12275 New_Reference_To
12276 (RTE (RE_POK_Protected_Entry), Loc)),
12278 Right_Opnd =>
12279 Make_Op_Eq (Loc,
12280 Left_Opnd => New_Reference_To (C, Loc),
12281 Right_Opnd =>
12282 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
12284 Then_Statements => Unpack));
12285 end if;
12287 -- Generate:
12289 -- if B then
12290 -- if C = POK_Procedure
12291 -- or else C = POK_Protected_Procedure
12292 -- or else C = POK_Task_Procedure
12293 -- then
12294 -- <dispatching-call>
12295 -- end if;
12296 -- end if;
12298 N_Stats := New_List (
12299 Make_Implicit_If_Statement (N,
12300 Condition =>
12301 Make_Or_Else (Loc,
12302 Left_Opnd =>
12303 Make_Op_Eq (Loc,
12304 Left_Opnd => New_Reference_To (C, Loc),
12305 Right_Opnd =>
12306 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
12308 Right_Opnd =>
12309 Make_Or_Else (Loc,
12310 Left_Opnd =>
12311 Make_Op_Eq (Loc,
12312 Left_Opnd => New_Reference_To (C, Loc),
12313 Right_Opnd =>
12314 New_Reference_To (RTE (
12315 RE_POK_Protected_Procedure), Loc)),
12316 Right_Opnd =>
12317 Make_Op_Eq (Loc,
12318 Left_Opnd => New_Reference_To (C, Loc),
12319 Right_Opnd =>
12320 New_Reference_To
12321 (RTE (RE_POK_Task_Procedure), Loc)))),
12323 Then_Statements => New_List (E_Call)));
12325 Append_To (Conc_Typ_Stmts,
12326 Make_Implicit_If_Statement (N,
12327 Condition => New_Reference_To (B, Loc),
12328 Then_Statements => N_Stats));
12330 -- Generate:
12331 -- <dispatching-call>;
12332 -- B := True;
12334 Lim_Typ_Stmts :=
12335 New_List (New_Copy_Tree (E_Call),
12336 Make_Assignment_Statement (Loc,
12337 Name => New_Occurrence_Of (B, Loc),
12338 Expression => New_Occurrence_Of (Standard_True, Loc)));
12340 -- Generate:
12341 -- if K = Ada.Tags.TK_Limited_Tagged
12342 -- or else K = Ada.Tags.TK_Tagged
12343 -- then
12344 -- Lim_Typ_Stmts
12345 -- else
12346 -- Conc_Typ_Stmts
12347 -- end if;
12349 Append_To (Stmts,
12350 Make_Implicit_If_Statement (N,
12351 Condition => Build_Dispatching_Tag_Check (K, N),
12352 Then_Statements => Lim_Typ_Stmts,
12353 Else_Statements => Conc_Typ_Stmts));
12355 -- Generate:
12357 -- if B then
12358 -- <triggering-statements>
12359 -- else
12360 -- <timed-statements>
12361 -- end if;
12363 Append_To (Stmts,
12364 Make_Implicit_If_Statement (N,
12365 Condition => New_Occurrence_Of (B, Loc),
12366 Then_Statements => E_Stats,
12367 Else_Statements => D_Stats));
12369 else
12370 -- Simple case of a non-dispatching trigger. Skip assignments to
12371 -- temporaries created for in-out parameters.
12373 -- This makes unwarranted assumptions about the shape of the expanded
12374 -- tree for the call, and should be cleaned up ???
12376 Stmt := First (Stmts);
12377 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12378 Next (Stmt);
12379 end loop;
12381 -- Do the assignment at this stage only because the evaluation
12382 -- of the expression must not occur before (see ACVC C97302A).
12384 Insert_Before (Stmt,
12385 Make_Assignment_Statement (Loc,
12386 Name => New_Reference_To (D, Loc),
12387 Expression => D_Conv));
12389 Call := Stmt;
12390 Params := Parameter_Associations (Call);
12392 -- For a protected type, we build a Timed_Protected_Entry_Call
12394 if Is_Protected_Type (Etype (Concval)) then
12396 -- Create a new call statement
12398 Param := First (Params);
12399 while Present (Param)
12400 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12401 loop
12402 Next (Param);
12403 end loop;
12405 Dummy := Remove_Next (Next (Param));
12407 -- Remove garbage is following the Cancel_Param if present
12409 Dummy := Next (Param);
12411 -- Remove the mode of the Protected_Entry_Call call, then remove
12412 -- the Communication_Block of the Protected_Entry_Call call, and
12413 -- finally add Duration and a Delay_Mode parameter
12415 pragma Assert (Present (Param));
12416 Rewrite (Param, New_Reference_To (D, Loc));
12418 Rewrite (Dummy, New_Reference_To (M, Loc));
12420 -- Add a Boolean flag for successful entry call
12422 Append_To (Params, New_Reference_To (B, Loc));
12424 case Corresponding_Runtime_Package (Etype (Concval)) is
12425 when System_Tasking_Protected_Objects_Entries =>
12426 Rewrite (Call,
12427 Make_Procedure_Call_Statement (Loc,
12428 Name =>
12429 New_Reference_To
12430 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12431 Parameter_Associations => Params));
12433 when System_Tasking_Protected_Objects_Single_Entry =>
12434 Param := First (Params);
12435 while Present (Param)
12436 and then not
12437 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
12438 loop
12439 Next (Param);
12440 end loop;
12442 Remove (Param);
12444 Rewrite (Call,
12445 Make_Procedure_Call_Statement (Loc,
12446 Name =>
12447 New_Reference_To
12448 (RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
12449 Parameter_Associations => Params));
12451 when others =>
12452 raise Program_Error;
12453 end case;
12455 -- For the task case, build a Timed_Task_Entry_Call
12457 else
12458 -- Create a new call statement
12460 Append_To (Params, New_Reference_To (D, Loc));
12461 Append_To (Params, New_Reference_To (M, Loc));
12462 Append_To (Params, New_Reference_To (B, Loc));
12464 Rewrite (Call,
12465 Make_Procedure_Call_Statement (Loc,
12466 Name =>
12467 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
12468 Parameter_Associations => Params));
12469 end if;
12471 Append_To (Stmts,
12472 Make_Implicit_If_Statement (N,
12473 Condition => New_Reference_To (B, Loc),
12474 Then_Statements => E_Stats,
12475 Else_Statements => D_Stats));
12476 end if;
12478 Rewrite (N,
12479 Make_Block_Statement (Loc,
12480 Declarations => Decls,
12481 Handled_Statement_Sequence =>
12482 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12484 Analyze (N);
12485 end Expand_N_Timed_Entry_Call;
12487 ----------------------------------------
12488 -- Expand_Protected_Body_Declarations --
12489 ----------------------------------------
12491 procedure Expand_Protected_Body_Declarations
12492 (N : Node_Id;
12493 Spec_Id : Entity_Id)
12495 begin
12496 if No_Run_Time_Mode then
12497 Error_Msg_CRT ("protected body", N);
12498 return;
12500 elsif Full_Expander_Active then
12502 -- Associate discriminals with the first subprogram or entry body to
12503 -- be expanded.
12505 if Present (First_Protected_Operation (Declarations (N))) then
12506 Set_Discriminals (Parent (Spec_Id));
12507 end if;
12508 end if;
12509 end Expand_Protected_Body_Declarations;
12511 -------------------------
12512 -- External_Subprogram --
12513 -------------------------
12515 function External_Subprogram (E : Entity_Id) return Entity_Id is
12516 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12518 begin
12519 -- The internal and external subprograms follow each other on the entity
12520 -- chain. Note that previously private operations had no separate
12521 -- external subprogram. We now create one in all cases, because a
12522 -- private operation may actually appear in an external call, through
12523 -- a 'Access reference used for a callback.
12525 -- If the operation is a function that returns an anonymous access type,
12526 -- the corresponding itype appears before the operation, and must be
12527 -- skipped.
12529 -- This mechanism is fragile, there should be a real link between the
12530 -- two versions of the operation, but there is no place to put it ???
12532 if Is_Access_Type (Next_Entity (Subp)) then
12533 return Next_Entity (Next_Entity (Subp));
12534 else
12535 return Next_Entity (Subp);
12536 end if;
12537 end External_Subprogram;
12539 ------------------------------
12540 -- Extract_Dispatching_Call --
12541 ------------------------------
12543 procedure Extract_Dispatching_Call
12544 (N : Node_Id;
12545 Call_Ent : out Entity_Id;
12546 Object : out Entity_Id;
12547 Actuals : out List_Id;
12548 Formals : out List_Id)
12550 Call_Nam : Node_Id;
12552 begin
12553 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12555 if Present (Original_Node (N)) then
12556 Call_Nam := Name (Original_Node (N));
12557 else
12558 Call_Nam := Name (N);
12559 end if;
12561 -- Retrieve the name of the dispatching procedure. It contains the
12562 -- dispatch table slot number.
12564 loop
12565 case Nkind (Call_Nam) is
12566 when N_Identifier =>
12567 exit;
12569 when N_Selected_Component =>
12570 Call_Nam := Selector_Name (Call_Nam);
12572 when others =>
12573 raise Program_Error;
12575 end case;
12576 end loop;
12578 Actuals := Parameter_Associations (N);
12579 Call_Ent := Entity (Call_Nam);
12580 Formals := Parameter_Specifications (Parent (Call_Ent));
12581 Object := First (Actuals);
12583 if Present (Original_Node (Object)) then
12584 Object := Original_Node (Object);
12585 end if;
12587 -- If the type of the dispatching object is an access type then return
12588 -- an explicit dereference.
12590 if Is_Access_Type (Etype (Object)) then
12591 Object := Make_Explicit_Dereference (Sloc (N), Object);
12592 Analyze (Object);
12593 end if;
12594 end Extract_Dispatching_Call;
12596 -------------------
12597 -- Extract_Entry --
12598 -------------------
12600 procedure Extract_Entry
12601 (N : Node_Id;
12602 Concval : out Node_Id;
12603 Ename : out Node_Id;
12604 Index : out Node_Id)
12606 Nam : constant Node_Id := Name (N);
12608 begin
12609 -- For a simple entry, the name is a selected component, with the
12610 -- prefix being the task value, and the selector being the entry.
12612 if Nkind (Nam) = N_Selected_Component then
12613 Concval := Prefix (Nam);
12614 Ename := Selector_Name (Nam);
12615 Index := Empty;
12617 -- For a member of an entry family, the name is an indexed component
12618 -- where the prefix is a selected component, whose prefix in turn is
12619 -- the task value, and whose selector is the entry family. The single
12620 -- expression in the expressions list of the indexed component is the
12621 -- subscript for the family.
12623 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12624 Concval := Prefix (Prefix (Nam));
12625 Ename := Selector_Name (Prefix (Nam));
12626 Index := First (Expressions (Nam));
12627 end if;
12628 end Extract_Entry;
12630 -------------------
12631 -- Family_Offset --
12632 -------------------
12634 function Family_Offset
12635 (Loc : Source_Ptr;
12636 Hi : Node_Id;
12637 Lo : Node_Id;
12638 Ttyp : Entity_Id;
12639 Cap : Boolean) return Node_Id
12641 Ityp : Entity_Id;
12642 Real_Hi : Node_Id;
12643 Real_Lo : Node_Id;
12645 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12646 -- If one of the bounds is a reference to a discriminant, replace with
12647 -- corresponding discriminal of type. Within the body of a task retrieve
12648 -- the renamed discriminant by simple visibility, using its generated
12649 -- name. Within a protected object, find the original discriminant and
12650 -- replace it with the discriminal of the current protected operation.
12652 ------------------------------
12653 -- Convert_Discriminant_Ref --
12654 ------------------------------
12656 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12657 Loc : constant Source_Ptr := Sloc (Bound);
12658 B : Node_Id;
12659 D : Entity_Id;
12661 begin
12662 if Is_Entity_Name (Bound)
12663 and then Ekind (Entity (Bound)) = E_Discriminant
12664 then
12665 if Is_Task_Type (Ttyp)
12666 and then Has_Completion (Ttyp)
12667 then
12668 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12669 Find_Direct_Name (B);
12671 elsif Is_Protected_Type (Ttyp) then
12672 D := First_Discriminant (Ttyp);
12673 while Chars (D) /= Chars (Entity (Bound)) loop
12674 Next_Discriminant (D);
12675 end loop;
12677 B := New_Reference_To (Discriminal (D), Loc);
12679 else
12680 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
12681 end if;
12683 elsif Nkind (Bound) = N_Attribute_Reference then
12684 return Bound;
12686 else
12687 B := New_Copy_Tree (Bound);
12688 end if;
12690 return
12691 Make_Attribute_Reference (Loc,
12692 Attribute_Name => Name_Pos,
12693 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12694 Expressions => New_List (B));
12695 end Convert_Discriminant_Ref;
12697 -- Start of processing for Family_Offset
12699 begin
12700 Real_Hi := Convert_Discriminant_Ref (Hi);
12701 Real_Lo := Convert_Discriminant_Ref (Lo);
12703 if Cap then
12704 if Is_Task_Type (Ttyp) then
12705 Ityp := RTE (RE_Task_Entry_Index);
12706 else
12707 Ityp := RTE (RE_Protected_Entry_Index);
12708 end if;
12710 Real_Hi :=
12711 Make_Attribute_Reference (Loc,
12712 Prefix => New_Reference_To (Ityp, Loc),
12713 Attribute_Name => Name_Min,
12714 Expressions => New_List (
12715 Real_Hi,
12716 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12718 Real_Lo :=
12719 Make_Attribute_Reference (Loc,
12720 Prefix => New_Reference_To (Ityp, Loc),
12721 Attribute_Name => Name_Max,
12722 Expressions => New_List (
12723 Real_Lo,
12724 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12725 end if;
12727 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12728 end Family_Offset;
12730 -----------------
12731 -- Family_Size --
12732 -----------------
12734 function Family_Size
12735 (Loc : Source_Ptr;
12736 Hi : Node_Id;
12737 Lo : Node_Id;
12738 Ttyp : Entity_Id;
12739 Cap : Boolean) return Node_Id
12741 Ityp : Entity_Id;
12743 begin
12744 if Is_Task_Type (Ttyp) then
12745 Ityp := RTE (RE_Task_Entry_Index);
12746 else
12747 Ityp := RTE (RE_Protected_Entry_Index);
12748 end if;
12750 return
12751 Make_Attribute_Reference (Loc,
12752 Prefix => New_Reference_To (Ityp, Loc),
12753 Attribute_Name => Name_Max,
12754 Expressions => New_List (
12755 Make_Op_Add (Loc,
12756 Left_Opnd =>
12757 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12758 Right_Opnd =>
12759 Make_Integer_Literal (Loc, 1)),
12760 Make_Integer_Literal (Loc, 0)));
12761 end Family_Size;
12763 ----------------------------
12764 -- Find_Enclosing_Context --
12765 ----------------------------
12767 procedure Find_Enclosing_Context
12768 (N : Node_Id;
12769 Context : out Node_Id;
12770 Context_Id : out Entity_Id;
12771 Context_Decls : out List_Id)
12773 begin
12774 -- Traverse the parent chain looking for an enclosing body, block,
12775 -- package or return statement.
12777 Context := Parent (N);
12778 while not Nkind_In (Context, N_Block_Statement,
12779 N_Entry_Body,
12780 N_Extended_Return_Statement,
12781 N_Package_Body,
12782 N_Package_Declaration,
12783 N_Subprogram_Body,
12784 N_Task_Body)
12785 loop
12786 Context := Parent (Context);
12787 end loop;
12789 -- Extract the constituents of the context
12791 if Nkind (Context) = N_Extended_Return_Statement then
12792 Context_Decls := Return_Object_Declarations (Context);
12793 Context_Id := Return_Statement_Entity (Context);
12795 -- Package declarations and bodies use a common library-level activation
12796 -- chain or task master, therefore return the package declaration as the
12797 -- proper carrier for the appropriate flag.
12799 elsif Nkind (Context) = N_Package_Body then
12800 Context_Decls := Declarations (Context);
12801 Context_Id := Corresponding_Spec (Context);
12802 Context := Parent (Context_Id);
12804 if Nkind (Context) = N_Defining_Program_Unit_Name then
12805 Context := Parent (Parent (Context));
12806 else
12807 Context := Parent (Context);
12808 end if;
12810 elsif Nkind (Context) = N_Package_Declaration then
12811 Context_Decls := Visible_Declarations (Specification (Context));
12812 Context_Id := Defining_Unit_Name (Specification (Context));
12814 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12815 Context_Id := Defining_Identifier (Context_Id);
12816 end if;
12818 else
12819 Context_Decls := Declarations (Context);
12821 if Nkind (Context) = N_Block_Statement then
12822 Context_Id := Entity (Identifier (Context));
12824 elsif Nkind (Context) = N_Entry_Body then
12825 Context_Id := Defining_Identifier (Context);
12827 elsif Nkind (Context) = N_Subprogram_Body then
12828 if Present (Corresponding_Spec (Context)) then
12829 Context_Id := Corresponding_Spec (Context);
12830 else
12831 Context_Id := Defining_Unit_Name (Specification (Context));
12833 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12834 Context_Id := Defining_Identifier (Context_Id);
12835 end if;
12836 end if;
12838 elsif Nkind (Context) = N_Task_Body then
12839 Context_Id := Corresponding_Spec (Context);
12841 else
12842 raise Program_Error;
12843 end if;
12844 end if;
12846 pragma Assert (Present (Context));
12847 pragma Assert (Present (Context_Id));
12848 pragma Assert (Present (Context_Decls));
12849 end Find_Enclosing_Context;
12851 -----------------------
12852 -- Find_Master_Scope --
12853 -----------------------
12855 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
12856 S : Entity_Id;
12858 begin
12859 -- In Ada 2005, the master is the innermost enclosing scope that is not
12860 -- transient. If the enclosing block is the rewriting of a call or the
12861 -- scope is an extended return statement this is valid master. The
12862 -- master in an extended return is only used within the return, and is
12863 -- subsequently overwritten in Move_Activation_Chain, but it must exist
12864 -- now before that overwriting occurs.
12866 S := Scope (E);
12868 if Ada_Version >= Ada_2005 then
12869 while Is_Internal (S) loop
12870 if Nkind (Parent (S)) = N_Block_Statement
12871 and then
12872 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
12873 then
12874 exit;
12876 elsif Ekind (S) = E_Return_Statement then
12877 exit;
12879 else
12880 S := Scope (S);
12881 end if;
12882 end loop;
12883 end if;
12885 return S;
12886 end Find_Master_Scope;
12888 -------------------------------
12889 -- First_Protected_Operation --
12890 -------------------------------
12892 function First_Protected_Operation (D : List_Id) return Node_Id is
12893 First_Op : Node_Id;
12895 begin
12896 First_Op := First (D);
12897 while Present (First_Op)
12898 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
12899 loop
12900 Next (First_Op);
12901 end loop;
12903 return First_Op;
12904 end First_Protected_Operation;
12906 ---------------------------------------
12907 -- Install_Private_Data_Declarations --
12908 ---------------------------------------
12910 procedure Install_Private_Data_Declarations
12911 (Loc : Source_Ptr;
12912 Spec_Id : Entity_Id;
12913 Conc_Typ : Entity_Id;
12914 Body_Nod : Node_Id;
12915 Decls : List_Id;
12916 Barrier : Boolean := False;
12917 Family : Boolean := False)
12919 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
12920 Decl : Node_Id;
12921 Def : Node_Id;
12922 Insert_Node : Node_Id := Empty;
12923 Obj_Ent : Entity_Id;
12925 procedure Add (Decl : Node_Id);
12926 -- Add a single declaration after Insert_Node. If this is the first
12927 -- addition, Decl is added to the front of Decls and it becomes the
12928 -- insertion node.
12930 function Replace_Bound (Bound : Node_Id) return Node_Id;
12931 -- The bounds of an entry index may depend on discriminants, create a
12932 -- reference to the corresponding prival. Otherwise return a duplicate
12933 -- of the original bound.
12935 ---------
12936 -- Add --
12937 ---------
12939 procedure Add (Decl : Node_Id) is
12940 begin
12941 if No (Insert_Node) then
12942 Prepend_To (Decls, Decl);
12943 else
12944 Insert_After (Insert_Node, Decl);
12945 end if;
12947 Insert_Node := Decl;
12948 end Add;
12950 --------------------------
12951 -- Replace_Discriminant --
12952 --------------------------
12954 function Replace_Bound (Bound : Node_Id) return Node_Id is
12955 begin
12956 if Nkind (Bound) = N_Identifier
12957 and then Is_Discriminal (Entity (Bound))
12958 then
12959 return Make_Identifier (Loc, Chars (Entity (Bound)));
12960 else
12961 return Duplicate_Subexpr (Bound);
12962 end if;
12963 end Replace_Bound;
12965 -- Start of processing for Install_Private_Data_Declarations
12967 begin
12968 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
12969 -- formal parameter _O, _object or _task depending on the context.
12971 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
12973 -- Special processing of _O for barrier functions, protected entries
12974 -- and families.
12976 if Barrier
12977 or else
12978 (Is_Protected
12979 and then
12980 (Ekind (Spec_Id) = E_Entry
12981 or else Ekind (Spec_Id) = E_Entry_Family))
12982 then
12983 declare
12984 Conc_Rec : constant Entity_Id :=
12985 Corresponding_Record_Type (Conc_Typ);
12986 Typ_Id : constant Entity_Id :=
12987 Make_Defining_Identifier (Loc,
12988 New_External_Name (Chars (Conc_Rec), 'P'));
12989 begin
12990 -- Generate:
12991 -- type prot_typVP is access prot_typV;
12993 Decl :=
12994 Make_Full_Type_Declaration (Loc,
12995 Defining_Identifier => Typ_Id,
12996 Type_Definition =>
12997 Make_Access_To_Object_Definition (Loc,
12998 Subtype_Indication =>
12999 New_Reference_To (Conc_Rec, Loc)));
13000 Add (Decl);
13002 -- Generate:
13003 -- _object : prot_typVP := prot_typV (_O);
13005 Decl :=
13006 Make_Object_Declaration (Loc,
13007 Defining_Identifier =>
13008 Make_Defining_Identifier (Loc, Name_uObject),
13009 Object_Definition => New_Reference_To (Typ_Id, Loc),
13010 Expression =>
13011 Unchecked_Convert_To (Typ_Id,
13012 New_Reference_To (Obj_Ent, Loc)));
13013 Add (Decl);
13015 -- Set the reference to the concurrent object
13017 Obj_Ent := Defining_Identifier (Decl);
13018 end;
13019 end if;
13021 -- Step 2: Create the Protection object and build its declaration for
13022 -- any protected entry (family) of subprogram. Note for the lock-free
13023 -- implementation, the Protection object is not needed anymore.
13025 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13026 declare
13027 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13028 Prot_Typ : RE_Id;
13030 begin
13031 Set_Protection_Object (Spec_Id, Prot_Ent);
13033 -- Determine the proper protection type
13035 if Has_Attach_Handler (Conc_Typ)
13036 and then not Restricted_Profile
13037 then
13038 Prot_Typ := RE_Static_Interrupt_Protection;
13040 elsif Has_Interrupt_Handler (Conc_Typ)
13041 and then not Restriction_Active (No_Dynamic_Attachment)
13042 then
13043 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13045 else
13046 case Corresponding_Runtime_Package (Conc_Typ) is
13047 when System_Tasking_Protected_Objects_Entries =>
13048 Prot_Typ := RE_Protection_Entries;
13050 when System_Tasking_Protected_Objects_Single_Entry =>
13051 Prot_Typ := RE_Protection_Entry;
13053 when System_Tasking_Protected_Objects =>
13054 Prot_Typ := RE_Protection;
13056 when others =>
13057 raise Program_Error;
13058 end case;
13059 end if;
13061 -- Generate:
13062 -- conc_typR : protection_typ renames _object._object;
13064 Decl :=
13065 Make_Object_Renaming_Declaration (Loc,
13066 Defining_Identifier => Prot_Ent,
13067 Subtype_Mark =>
13068 New_Reference_To (RTE (Prot_Typ), Loc),
13069 Name =>
13070 Make_Selected_Component (Loc,
13071 Prefix => New_Reference_To (Obj_Ent, Loc),
13072 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13073 Add (Decl);
13074 end;
13075 end if;
13077 -- Step 3: Add discriminant renamings (if any)
13079 if Has_Discriminants (Conc_Typ) then
13080 declare
13081 D : Entity_Id;
13083 begin
13084 D := First_Discriminant (Conc_Typ);
13085 while Present (D) loop
13087 -- Adjust the source location
13089 Set_Sloc (Discriminal (D), Loc);
13091 -- Generate:
13092 -- discr_name : discr_typ renames _object.discr_name;
13093 -- or
13094 -- discr_name : discr_typ renames _task.discr_name;
13096 Decl :=
13097 Make_Object_Renaming_Declaration (Loc,
13098 Defining_Identifier => Discriminal (D),
13099 Subtype_Mark => New_Reference_To (Etype (D), Loc),
13100 Name =>
13101 Make_Selected_Component (Loc,
13102 Prefix => New_Reference_To (Obj_Ent, Loc),
13103 Selector_Name => Make_Identifier (Loc, Chars (D))));
13104 Add (Decl);
13106 Next_Discriminant (D);
13107 end loop;
13108 end;
13109 end if;
13111 -- Step 4: Add private component renamings (if any)
13113 if Is_Protected then
13114 Def := Protected_Definition (Parent (Conc_Typ));
13116 if Present (Private_Declarations (Def)) then
13117 declare
13118 Comp : Node_Id;
13119 Comp_Id : Entity_Id;
13120 Decl_Id : Entity_Id;
13122 begin
13123 Comp := First (Private_Declarations (Def));
13124 while Present (Comp) loop
13125 if Nkind (Comp) = N_Component_Declaration then
13126 Comp_Id := Defining_Identifier (Comp);
13127 Decl_Id :=
13128 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13130 -- Minimal decoration
13132 if Ekind (Spec_Id) = E_Function then
13133 Set_Ekind (Decl_Id, E_Constant);
13134 else
13135 Set_Ekind (Decl_Id, E_Variable);
13136 end if;
13138 Set_Prival (Comp_Id, Decl_Id);
13139 Set_Prival_Link (Decl_Id, Comp_Id);
13140 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13142 -- Generate:
13143 -- comp_name : comp_typ renames _object.comp_name;
13145 Decl :=
13146 Make_Object_Renaming_Declaration (Loc,
13147 Defining_Identifier => Decl_Id,
13148 Subtype_Mark =>
13149 New_Reference_To (Etype (Comp_Id), Loc),
13150 Name =>
13151 Make_Selected_Component (Loc,
13152 Prefix =>
13153 New_Reference_To (Obj_Ent, Loc),
13154 Selector_Name =>
13155 Make_Identifier (Loc, Chars (Comp_Id))));
13156 Add (Decl);
13157 end if;
13159 Next (Comp);
13160 end loop;
13161 end;
13162 end if;
13163 end if;
13165 -- Step 5: Add the declaration of the entry index and the associated
13166 -- type for barrier functions and entry families.
13168 if (Barrier and then Family)
13169 or else Ekind (Spec_Id) = E_Entry_Family
13170 then
13171 declare
13172 E : constant Entity_Id := Index_Object (Spec_Id);
13173 Index : constant Entity_Id :=
13174 Defining_Identifier (
13175 Entry_Index_Specification (
13176 Entry_Body_Formal_Part (Body_Nod)));
13177 Index_Con : constant Entity_Id :=
13178 Make_Defining_Identifier (Loc, Chars (Index));
13179 High : Node_Id;
13180 Index_Typ : Entity_Id;
13181 Low : Node_Id;
13183 begin
13184 -- Minimal decoration
13186 Set_Ekind (Index_Con, E_Constant);
13187 Set_Entry_Index_Constant (Index, Index_Con);
13188 Set_Discriminal_Link (Index_Con, Index);
13190 -- Retrieve the bounds of the entry family
13192 High := Type_High_Bound (Etype (Index));
13193 Low := Type_Low_Bound (Etype (Index));
13195 -- In the simple case the entry family is given by a subtype
13196 -- mark and the index constant has the same type.
13198 if Is_Entity_Name (Original_Node (
13199 Discrete_Subtype_Definition (Parent (Index))))
13200 then
13201 Index_Typ := Etype (Index);
13203 -- Otherwise a new subtype declaration is required
13205 else
13206 High := Replace_Bound (High);
13207 Low := Replace_Bound (Low);
13209 Index_Typ := Make_Temporary (Loc, 'J');
13211 -- Generate:
13212 -- subtype Jnn is <Etype of Index> range Low .. High;
13214 Decl :=
13215 Make_Subtype_Declaration (Loc,
13216 Defining_Identifier => Index_Typ,
13217 Subtype_Indication =>
13218 Make_Subtype_Indication (Loc,
13219 Subtype_Mark =>
13220 New_Reference_To (Base_Type (Etype (Index)), Loc),
13221 Constraint =>
13222 Make_Range_Constraint (Loc,
13223 Range_Expression =>
13224 Make_Range (Loc, Low, High))));
13225 Add (Decl);
13226 end if;
13228 Set_Etype (Index_Con, Index_Typ);
13230 -- Create the object which designates the index:
13231 -- J : constant Jnn :=
13232 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13234 -- where Jnn is the subtype created above or the original type of
13235 -- the index, _E is a formal of the protected body subprogram and
13236 -- <index expr> is the index of the first family member.
13238 Decl :=
13239 Make_Object_Declaration (Loc,
13240 Defining_Identifier => Index_Con,
13241 Constant_Present => True,
13242 Object_Definition =>
13243 New_Reference_To (Index_Typ, Loc),
13245 Expression =>
13246 Make_Attribute_Reference (Loc,
13247 Prefix =>
13248 New_Reference_To (Index_Typ, Loc),
13249 Attribute_Name => Name_Val,
13251 Expressions => New_List (
13253 Make_Op_Add (Loc,
13254 Left_Opnd =>
13255 Make_Op_Subtract (Loc,
13256 Left_Opnd =>
13257 New_Reference_To (E, Loc),
13258 Right_Opnd =>
13259 Entry_Index_Expression (Loc,
13260 Defining_Identifier (Body_Nod),
13261 Empty, Conc_Typ)),
13263 Right_Opnd =>
13264 Make_Attribute_Reference (Loc,
13265 Prefix =>
13266 New_Reference_To (Index_Typ, Loc),
13267 Attribute_Name => Name_Pos,
13268 Expressions => New_List (
13269 Make_Attribute_Reference (Loc,
13270 Prefix =>
13271 New_Reference_To (Index_Typ, Loc),
13272 Attribute_Name => Name_First)))))));
13273 Add (Decl);
13274 end;
13275 end if;
13276 end Install_Private_Data_Declarations;
13278 -----------------------
13279 -- Is_Exception_Safe --
13280 -----------------------
13282 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13284 function Has_Side_Effect (N : Node_Id) return Boolean;
13285 -- Return True whenever encountering a subprogram call or raise
13286 -- statement of any kind in the sequence of statements
13288 ---------------------
13289 -- Has_Side_Effect --
13290 ---------------------
13292 -- What is this doing buried two levels down in exp_ch9. It seems like a
13293 -- generally useful function, and indeed there may be code duplication
13294 -- going on here ???
13296 function Has_Side_Effect (N : Node_Id) return Boolean is
13297 Stmt : Node_Id;
13298 Expr : Node_Id;
13300 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13301 -- Indicate whether N is a subprogram call or a raise statement
13303 ----------------------
13304 -- Is_Call_Or_Raise --
13305 ----------------------
13307 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13308 begin
13309 return Nkind_In (N, N_Procedure_Call_Statement,
13310 N_Function_Call,
13311 N_Raise_Statement,
13312 N_Raise_Constraint_Error,
13313 N_Raise_Program_Error,
13314 N_Raise_Storage_Error);
13315 end Is_Call_Or_Raise;
13317 -- Start of processing for Has_Side_Effect
13319 begin
13320 Stmt := N;
13321 while Present (Stmt) loop
13322 if Is_Call_Or_Raise (Stmt) then
13323 return True;
13324 end if;
13326 -- An object declaration can also contain a function call or a
13327 -- raise statement.
13329 if Nkind (Stmt) = N_Object_Declaration then
13330 Expr := Expression (Stmt);
13332 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13333 return True;
13334 end if;
13335 end if;
13337 Next (Stmt);
13338 end loop;
13340 return False;
13341 end Has_Side_Effect;
13343 -- Start of processing for Is_Exception_Safe
13345 begin
13346 -- If the checks handled by the back end are not disabled, we cannot
13347 -- ensure that no exception will be raised.
13349 if not Access_Checks_Suppressed (Empty)
13350 or else not Discriminant_Checks_Suppressed (Empty)
13351 or else not Range_Checks_Suppressed (Empty)
13352 or else not Index_Checks_Suppressed (Empty)
13353 or else Opt.Stack_Checking_Enabled
13354 then
13355 return False;
13356 end if;
13358 if Has_Side_Effect (First (Declarations (Subprogram)))
13359 or else
13360 Has_Side_Effect
13361 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13362 then
13363 return False;
13364 else
13365 return True;
13366 end if;
13367 end Is_Exception_Safe;
13369 ---------------------------------
13370 -- Is_Potentially_Large_Family --
13371 ---------------------------------
13373 function Is_Potentially_Large_Family
13374 (Base_Index : Entity_Id;
13375 Conctyp : Entity_Id;
13376 Lo : Node_Id;
13377 Hi : Node_Id) return Boolean
13379 begin
13380 return Scope (Base_Index) = Standard_Standard
13381 and then Base_Index = Base_Type (Standard_Integer)
13382 and then Has_Discriminants (Conctyp)
13383 and then
13384 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13385 and then
13386 (Denotes_Discriminant (Lo, True)
13387 or else
13388 Denotes_Discriminant (Hi, True));
13389 end Is_Potentially_Large_Family;
13391 -------------------------------------
13392 -- Is_Private_Primitive_Subprogram --
13393 -------------------------------------
13395 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13396 begin
13397 return
13398 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13399 and then Is_Private_Primitive (Id);
13400 end Is_Private_Primitive_Subprogram;
13402 ------------------
13403 -- Index_Object --
13404 ------------------
13406 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13407 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13408 Formal : Entity_Id;
13410 begin
13411 Formal := First_Formal (Bod_Subp);
13412 while Present (Formal) loop
13414 -- Look for formal parameter _E
13416 if Chars (Formal) = Name_uE then
13417 return Formal;
13418 end if;
13420 Next_Formal (Formal);
13421 end loop;
13423 -- A protected body subprogram should always have the parameter in
13424 -- question.
13426 raise Program_Error;
13427 end Index_Object;
13429 --------------------------------
13430 -- Make_Initialize_Protection --
13431 --------------------------------
13433 function Make_Initialize_Protection
13434 (Protect_Rec : Entity_Id) return List_Id
13436 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13437 P_Arr : Entity_Id;
13438 Pdec : Node_Id;
13439 Ptyp : constant Node_Id :=
13440 Corresponding_Concurrent_Type (Protect_Rec);
13441 Args : List_Id;
13442 L : constant List_Id := New_List;
13443 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13444 Prio_Type : Entity_Id;
13445 Restricted : constant Boolean := Restricted_Profile;
13447 begin
13448 -- We may need two calls to properly initialize the object, one to
13449 -- Initialize_Protection, and possibly one to Install_Handlers if we
13450 -- have a pragma Attach_Handler.
13452 -- Get protected declaration. In the case of a task type declaration,
13453 -- this is simply the parent of the protected type entity. In the single
13454 -- protected object declaration, this parent will be the implicit type,
13455 -- and we can find the corresponding single protected object declaration
13456 -- by searching forward in the declaration list in the tree.
13458 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13459 -- of this type should have been removed during semantic analysis.
13461 Pdec := Parent (Ptyp);
13462 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13463 N_Single_Protected_Declaration)
13464 loop
13465 Next (Pdec);
13466 end loop;
13468 -- Build the parameter list for the call. Note that _Init is the name
13469 -- of the formal for the object to be initialized, which is the task
13470 -- value record itself.
13472 Args := New_List;
13474 -- For lock-free implementation, skip initializations of the Protection
13475 -- object.
13477 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13478 -- Object parameter. This is a pointer to the object of type
13479 -- Protection used by the GNARL to control the protected object.
13481 Append_To (Args,
13482 Make_Attribute_Reference (Loc,
13483 Prefix =>
13484 Make_Selected_Component (Loc,
13485 Prefix => Make_Identifier (Loc, Name_uInit),
13486 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13487 Attribute_Name => Name_Unchecked_Access));
13489 -- Priority parameter. Set to Unspecified_Priority unless there is a
13490 -- Priority rep item, in which case we take the value from the pragma
13491 -- or attribute definition clause, or there is an Interrupt_Priority
13492 -- rep item and no Priority rep item, and we set the ceiling to
13493 -- Interrupt_Priority'Last, an implementation-defined value, see
13494 -- (RM D.3(10)).
13496 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13497 declare
13498 Prio_Clause : constant Node_Id :=
13499 Get_Rep_Item
13500 (Ptyp, Name_Priority, Check_Parents => False);
13502 Prio : Node_Id;
13503 Temp : Entity_Id;
13505 begin
13506 -- Pragma Priority
13508 if Nkind (Prio_Clause) = N_Pragma then
13509 Prio :=
13510 Expression
13511 (First (Pragma_Argument_Associations (Prio_Clause)));
13513 -- Get_Rep_Item returns either priority pragma.
13515 if Pragma_Name (Prio_Clause) = Name_Priority then
13516 Prio_Type := RTE (RE_Any_Priority);
13517 else
13518 Prio_Type := RTE (RE_Interrupt_Priority);
13519 end if;
13521 -- Attribute definition clause Priority
13523 else
13524 if Chars (Prio_Clause) = Name_Priority then
13525 Prio_Type := RTE (RE_Any_Priority);
13526 else
13527 Prio_Type := RTE (RE_Interrupt_Priority);
13528 end if;
13530 Prio := Expression (Prio_Clause);
13531 end if;
13533 -- If priority is a static expression, then we can duplicate it
13534 -- with no problem and simply append it to the argument list.
13535 -- However, it has only be pre-analyzed, so we need to check
13536 -- now that it is in the bounds of the priority type.
13538 if Is_Static_Expression (Prio) then
13539 Set_Analyzed (Prio, False);
13540 Append_To (Args,
13541 Make_Type_Conversion (Loc,
13542 Subtype_Mark => New_Occurrence_Of (Prio_Type, Loc),
13543 Expression => Duplicate_Subexpr (Prio)));
13545 -- Otherwise, the priority may be a per-object expression, if
13546 -- it depends on a discriminant of the type. In this case,
13547 -- create local variable to capture the expression. Note that
13548 -- it is really necessary to create this variable explicitly.
13549 -- It might be thought that removing side effects would the
13550 -- appropriate approach, but that could generate declarations
13551 -- improperly placed in the enclosing scope.
13553 else
13554 Temp := Make_Temporary (Loc, 'R', Prio);
13555 Append_To (L,
13556 Make_Object_Declaration (Loc,
13557 Defining_Identifier => Temp,
13558 Object_Definition =>
13559 New_Occurrence_Of (Prio_Type, Loc),
13560 Expression => Relocate_Node (Prio)));
13562 Append_To (Args, New_Occurrence_Of (Temp, Loc));
13563 end if;
13564 end;
13566 -- When no priority is specified but an xx_Handler pragma is, we
13567 -- default to System.Interrupts.Default_Interrupt_Priority, see
13568 -- D.3(10).
13570 elsif Has_Attach_Handler (Ptyp)
13571 or else Has_Interrupt_Handler (Ptyp)
13572 then
13573 Append_To (Args,
13574 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
13576 -- Normal case, no priority or xx_Handler specified, default priority
13578 else
13579 Append_To (Args,
13580 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13581 end if;
13583 -- Test for Compiler_Info parameter. This parameter allows entry body
13584 -- procedures and barrier functions to be called from the runtime. It
13585 -- is a pointer to the record generated by the compiler to represent
13586 -- the protected object.
13588 -- A protected type without entries that covers an interface and
13589 -- overrides the abstract routines with protected procedures is
13590 -- considered equivalent to a protected type with entries in the
13591 -- context of dispatching select statements.
13593 -- Protected types with interrupt handlers (when not using a
13594 -- restricted profile) are also considered equivalent to protected
13595 -- types with entries.
13597 -- The types which are used (Static_Interrupt_Protection and
13598 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13600 declare
13601 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13603 Called_Subp : RE_Id;
13605 begin
13606 case Pkg_Id is
13607 when System_Tasking_Protected_Objects_Entries =>
13608 Called_Subp := RE_Initialize_Protection_Entries;
13610 -- Argument Compiler_Info
13612 Append_To (Args,
13613 Make_Attribute_Reference (Loc,
13614 Prefix => Make_Identifier (Loc, Name_uInit),
13615 Attribute_Name => Name_Address));
13617 when System_Tasking_Protected_Objects_Single_Entry =>
13618 Called_Subp := RE_Initialize_Protection_Entry;
13620 -- Argument Compiler_Info
13622 Append_To (Args,
13623 Make_Attribute_Reference (Loc,
13624 Prefix => Make_Identifier (Loc, Name_uInit),
13625 Attribute_Name => Name_Address));
13627 when System_Tasking_Protected_Objects =>
13628 Called_Subp := RE_Initialize_Protection;
13630 when others =>
13631 raise Program_Error;
13632 end case;
13634 -- Entry_Bodies parameter. This is a pointer to an array of
13635 -- pointers to the entry body procedures and barrier functions of
13636 -- the object. If the protected type has no entries this object
13637 -- will not exist, in this case, pass a null (it can happen when
13638 -- there are protected interrupt handlers or interfaces).
13640 if Has_Entry then
13641 P_Arr := Entry_Bodies_Array (Ptyp);
13643 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13644 -- multiple entries).
13646 Append_To (Args,
13647 Make_Attribute_Reference (Loc,
13648 Prefix => New_Reference_To (P_Arr, Loc),
13649 Attribute_Name => Name_Unrestricted_Access));
13651 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13653 -- Find index mapping function (clumsy but ok for now)
13655 while Ekind (P_Arr) /= E_Function loop
13656 Next_Entity (P_Arr);
13657 end loop;
13659 Append_To (Args,
13660 Make_Attribute_Reference (Loc,
13661 Prefix => New_Reference_To (P_Arr, Loc),
13662 Attribute_Name => Name_Unrestricted_Access));
13663 end if;
13665 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13667 -- This is the case where we have a protected object with
13668 -- interfaces and no entries, and the single entry restriction
13669 -- is in effect. We pass a null pointer for the entry
13670 -- parameter because there is no actual entry.
13672 Append_To (Args, Make_Null (Loc));
13674 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13676 -- This is the case where we have a protected object with no
13677 -- entries and:
13678 -- - either interrupt handlers with non restricted profile,
13679 -- - or interfaces
13680 -- Note that the types which are used for interrupt handlers
13681 -- (Static/Dynamic_Interrupt_Protection) are derived from
13682 -- Protection_Entries. We pass two null pointers because there
13683 -- is no actual entry, and the initialization procedure needs
13684 -- both Entry_Bodies and Find_Body_Index.
13686 Append_To (Args, Make_Null (Loc));
13687 Append_To (Args, Make_Null (Loc));
13688 end if;
13690 Append_To (L,
13691 Make_Procedure_Call_Statement (Loc,
13692 Name => New_Reference_To (RTE (Called_Subp), Loc),
13693 Parameter_Associations => Args));
13694 end;
13695 end if;
13697 if Has_Attach_Handler (Ptyp) then
13699 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13700 -- make the following call:
13702 -- Install_Handlers (_object,
13703 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13705 -- or, in the case of Ravenscar:
13707 -- Install_Restricted_Handlers
13708 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13710 declare
13711 Args : constant List_Id := New_List;
13712 Table : constant List_Id := New_List;
13713 Ritem : Node_Id := First_Rep_Item (Ptyp);
13715 begin
13716 -- Build the Attach_Handler table argument
13718 while Present (Ritem) loop
13719 if Nkind (Ritem) = N_Pragma
13720 and then Pragma_Name (Ritem) = Name_Attach_Handler
13721 then
13722 declare
13723 Handler : constant Node_Id :=
13724 First (Pragma_Argument_Associations (Ritem));
13726 Interrupt : constant Node_Id := Next (Handler);
13727 Expr : constant Node_Id := Expression (Interrupt);
13729 begin
13730 Append_To (Table,
13731 Make_Aggregate (Loc, Expressions => New_List (
13732 Unchecked_Convert_To
13733 (RTE (RE_System_Interrupt_Id), Expr),
13734 Make_Attribute_Reference (Loc,
13735 Prefix => Make_Selected_Component (Loc,
13736 Make_Identifier (Loc, Name_uInit),
13737 Duplicate_Subexpr_No_Checks
13738 (Expression (Handler))),
13739 Attribute_Name => Name_Access))));
13740 end;
13741 end if;
13743 Next_Rep_Item (Ritem);
13744 end loop;
13746 -- Append the table argument we just built
13748 Append_To (Args, Make_Aggregate (Loc, Table));
13750 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13751 -- call to the statements.
13753 if Restricted then
13754 -- Call a simplified version of Install_Handlers to be used
13755 -- when the Ravenscar restrictions are in effect
13756 -- (Install_Restricted_Handlers).
13758 Append_To (L,
13759 Make_Procedure_Call_Statement (Loc,
13760 Name =>
13761 New_Reference_To
13762 (RTE (RE_Install_Restricted_Handlers), Loc),
13763 Parameter_Associations => Args));
13765 else
13766 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13767 -- First, prepends the _object argument
13769 Prepend_To (Args,
13770 Make_Attribute_Reference (Loc,
13771 Prefix =>
13772 Make_Selected_Component (Loc,
13773 Prefix => Make_Identifier (Loc, Name_uInit),
13774 Selector_Name =>
13775 Make_Identifier (Loc, Name_uObject)),
13776 Attribute_Name => Name_Unchecked_Access));
13777 end if;
13779 -- Then, insert call to Install_Handlers
13781 Append_To (L,
13782 Make_Procedure_Call_Statement (Loc,
13783 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
13784 Parameter_Associations => Args));
13785 end if;
13786 end;
13787 end if;
13789 return L;
13790 end Make_Initialize_Protection;
13792 ---------------------------
13793 -- Make_Task_Create_Call --
13794 ---------------------------
13796 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13797 Loc : constant Source_Ptr := Sloc (Task_Rec);
13798 Args : List_Id;
13799 Ecount : Node_Id;
13800 Name : Node_Id;
13801 Tdec : Node_Id;
13802 Tdef : Node_Id;
13803 Tnam : Name_Id;
13804 Ttyp : Node_Id;
13806 begin
13807 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
13808 Tnam := Chars (Ttyp);
13810 -- Get task declaration. In the case of a task type declaration, this is
13811 -- simply the parent of the task type entity. In the single task
13812 -- declaration, this parent will be the implicit type, and we can find
13813 -- the corresponding single task declaration by searching forward in the
13814 -- declaration list in the tree.
13816 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
13817 -- this type should have been removed during semantic analysis.
13819 Tdec := Parent (Ttyp);
13820 while not Nkind_In (Tdec, N_Task_Type_Declaration,
13821 N_Single_Task_Declaration)
13822 loop
13823 Next (Tdec);
13824 end loop;
13826 -- Now we can find the task definition from this declaration
13828 Tdef := Task_Definition (Tdec);
13830 -- Build the parameter list for the call. Note that _Init is the name
13831 -- of the formal for the object to be initialized, which is the task
13832 -- value record itself.
13834 Args := New_List;
13836 -- Priority parameter. Set to Unspecified_Priority unless there is a
13837 -- Priority rep item, in which case we take the value from the rep item.
13839 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
13840 Append_To (Args,
13841 Make_Selected_Component (Loc,
13842 Prefix => Make_Identifier (Loc, Name_uInit),
13843 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
13844 else
13845 Append_To (Args,
13846 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13847 end if;
13849 -- Optional Stack parameter
13851 if Restricted_Profile then
13853 -- If the stack has been preallocated by the expander then
13854 -- pass its address. Otherwise, pass a null address.
13856 if Preallocated_Stacks_On_Target then
13857 Append_To (Args,
13858 Make_Attribute_Reference (Loc,
13859 Prefix =>
13860 Make_Selected_Component (Loc,
13861 Prefix => Make_Identifier (Loc, Name_uInit),
13862 Selector_Name => Make_Identifier (Loc, Name_uStack)),
13863 Attribute_Name => Name_Address));
13865 else
13866 Append_To (Args,
13867 New_Reference_To (RTE (RE_Null_Address), Loc));
13868 end if;
13869 end if;
13871 -- Size parameter. If no Storage_Size pragma is present, then
13872 -- the size is taken from the taskZ variable for the type, which
13873 -- is either Unspecified_Size, or has been reset by the use of
13874 -- a Storage_Size attribute definition clause. If a pragma is
13875 -- present, then the size is taken from the _Size field of the
13876 -- task value record, which was set from the pragma value.
13878 if Present (Tdef)
13879 and then Has_Storage_Size_Pragma (Tdef)
13880 then
13881 Append_To (Args,
13882 Make_Selected_Component (Loc,
13883 Prefix => Make_Identifier (Loc, Name_uInit),
13884 Selector_Name => Make_Identifier (Loc, Name_uSize)));
13886 else
13887 Append_To (Args,
13888 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
13889 end if;
13891 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
13892 -- Task_Info pragma, in which case we take the value from the pragma.
13894 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
13895 Append_To (Args,
13896 Make_Selected_Component (Loc,
13897 Prefix => Make_Identifier (Loc, Name_uInit),
13898 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
13900 else
13901 Append_To (Args,
13902 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
13903 end if;
13905 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
13906 -- in which case we take the value from the rep item. The parameter is
13907 -- passed as an Integer because in the case of unspecified CPU the
13908 -- value is not in the range of CPU_Range.
13910 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
13911 Append_To (Args,
13912 Convert_To (Standard_Integer,
13913 Make_Selected_Component (Loc,
13914 Prefix => Make_Identifier (Loc, Name_uInit),
13915 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
13916 else
13917 Append_To (Args,
13918 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
13919 end if;
13921 if not Restricted_Profile then
13923 -- Deadline parameter. If no Relative_Deadline pragma is present,
13924 -- then the deadline is Time_Span_Zero. If a pragma is present, then
13925 -- the deadline is taken from the _Relative_Deadline field of the
13926 -- task value record, which was set from the pragma value. Note that
13927 -- this parameter must not be generated for the restricted profiles
13928 -- since Ravenscar does not allow deadlines.
13930 -- Case where pragma Relative_Deadline applies: use given value
13932 if Present (Tdef)
13933 and then Has_Relative_Deadline_Pragma (Tdef)
13934 then
13935 Append_To (Args,
13936 Make_Selected_Component (Loc,
13937 Prefix =>
13938 Make_Identifier (Loc, Name_uInit),
13939 Selector_Name =>
13940 Make_Identifier (Loc, Name_uRelative_Deadline)));
13942 -- No pragma Relative_Deadline apply to the task
13944 else
13945 Append_To (Args,
13946 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
13947 end if;
13949 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
13950 -- present, then the dispatching domain is null. If a rep item is
13951 -- present, then the dispatching domain is taken from the
13952 -- _Dispatching_Domain field of the task value record, which was set
13953 -- from the rep item value. Note that this parameter must not be
13954 -- generated for the restricted profiles since Ravenscar does not
13955 -- allow dispatching domains.
13957 -- Case where Dispatching_Domain rep item applies: use given value
13959 if Has_Rep_Item
13960 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
13961 then
13962 Append_To (Args,
13963 Make_Selected_Component (Loc,
13964 Prefix =>
13965 Make_Identifier (Loc, Name_uInit),
13966 Selector_Name =>
13967 Make_Identifier (Loc, Name_uDispatching_Domain)));
13969 -- No pragma or aspect Dispatching_Domain apply to the task
13971 else
13972 Append_To (Args, Make_Null (Loc));
13973 end if;
13975 -- Number of entries. This is an expression of the form:
13977 -- n + _Init.a'Length + _Init.a'B'Length + ...
13979 -- where a,b... are the entry family names for the task definition
13981 Ecount :=
13982 Build_Entry_Count_Expression
13983 (Ttyp,
13984 Component_Items
13985 (Component_List
13986 (Type_Definition
13987 (Parent (Corresponding_Record_Type (Ttyp))))),
13988 Loc);
13989 Append_To (Args, Ecount);
13991 -- Master parameter. This is a reference to the _Master parameter of
13992 -- the initialization procedure, except in the case of the pragma
13993 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
13994 -- System.Tasking.Library_Task_Level.
13996 if Restriction_Active (No_Task_Hierarchy) = False then
13997 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
13998 else
13999 Append_To (Args,
14000 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14001 end if;
14002 end if;
14004 -- State parameter. This is a pointer to the task body procedure. The
14005 -- required value is obtained by taking 'Unrestricted_Access of the task
14006 -- body procedure and converting it (with an unchecked conversion) to
14007 -- the type required by the task kernel. For further details, see the
14008 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14009 -- than 'Address in order to avoid creating trampolines.
14011 declare
14012 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14013 Subp_Ptr_Typ : constant Node_Id :=
14014 Create_Itype (E_Access_Subprogram_Type, Tdec);
14015 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14017 begin
14018 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14019 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14021 -- Be sure to freeze a reference to the access-to-subprogram type,
14022 -- otherwise gigi will complain that it's in the wrong scope, because
14023 -- it's actually inside the init procedure for the record type that
14024 -- corresponds to the task type.
14026 -- This processing is causing a crash in the .NET/JVM back ends that
14027 -- is not yet understood, so skip it in these cases ???
14029 if VM_Target = No_VM then
14030 Set_Itype (Ref, Subp_Ptr_Typ);
14031 Append_Freeze_Action (Task_Rec, Ref);
14033 Append_To (Args,
14034 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14035 Make_Qualified_Expression (Loc,
14036 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
14037 Expression =>
14038 Make_Attribute_Reference (Loc,
14039 Prefix =>
14040 New_Occurrence_Of (Body_Proc, Loc),
14041 Attribute_Name => Name_Unrestricted_Access))));
14043 -- For the .NET/JVM cases revert to the original code below ???
14045 else
14046 Append_To (Args,
14047 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14048 Make_Attribute_Reference (Loc,
14049 Prefix =>
14050 New_Occurrence_Of (Body_Proc, Loc),
14051 Attribute_Name => Name_Address)));
14052 end if;
14053 end;
14055 -- Discriminants parameter. This is just the address of the task
14056 -- value record itself (which contains the discriminant values
14058 Append_To (Args,
14059 Make_Attribute_Reference (Loc,
14060 Prefix => Make_Identifier (Loc, Name_uInit),
14061 Attribute_Name => Name_Address));
14063 -- Elaborated parameter. This is an access to the elaboration Boolean
14065 Append_To (Args,
14066 Make_Attribute_Reference (Loc,
14067 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14068 Attribute_Name => Name_Unchecked_Access));
14070 -- Add Chain parameter (not done for sequential elaboration policy, see
14071 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14073 if Partition_Elaboration_Policy /= 'S' then
14074 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14075 end if;
14077 -- Task name parameter. Take this from the _Task_Id parameter to the
14078 -- init call unless there is a Task_Name pragma, in which case we take
14079 -- the value from the pragma.
14081 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14082 -- Copy expression in full, because it may be dynamic and have
14083 -- side effects.
14085 Append_To (Args,
14086 New_Copy_Tree
14087 (Expression
14088 (First
14089 (Pragma_Argument_Associations
14090 (Get_Rep_Pragma
14091 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14093 else
14094 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14095 end if;
14097 -- Created_Task parameter. This is the _Task_Id field of the task
14098 -- record value
14100 Append_To (Args,
14101 Make_Selected_Component (Loc,
14102 Prefix => Make_Identifier (Loc, Name_uInit),
14103 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14105 declare
14106 Create_RE : RE_Id;
14108 begin
14109 if Restricted_Profile then
14110 if Partition_Elaboration_Policy = 'S' then
14111 Create_RE := RE_Create_Restricted_Task_Sequential;
14112 else
14113 Create_RE := RE_Create_Restricted_Task;
14114 end if;
14115 else
14116 Create_RE := RE_Create_Task;
14117 end if;
14119 Name := New_Reference_To (RTE (Create_RE), Loc);
14120 end;
14122 return
14123 Make_Procedure_Call_Statement (Loc,
14124 Name => Name,
14125 Parameter_Associations => Args);
14126 end Make_Task_Create_Call;
14128 ------------------------------
14129 -- Next_Protected_Operation --
14130 ------------------------------
14132 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14133 Next_Op : Node_Id;
14135 begin
14136 Next_Op := Next (N);
14137 while Present (Next_Op)
14138 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
14139 loop
14140 Next (Next_Op);
14141 end loop;
14143 return Next_Op;
14144 end Next_Protected_Operation;
14146 ---------------------
14147 -- Null_Statements --
14148 ---------------------
14150 function Null_Statements (Stats : List_Id) return Boolean is
14151 Stmt : Node_Id;
14153 begin
14154 Stmt := First (Stats);
14155 while Nkind (Stmt) /= N_Empty
14156 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14157 or else
14158 (Nkind (Stmt) = N_Pragma
14159 and then
14160 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14161 Name_Unmodified,
14162 Name_Warnings)))
14163 loop
14164 Next (Stmt);
14165 end loop;
14167 return Nkind (Stmt) = N_Empty;
14168 end Null_Statements;
14170 --------------------------
14171 -- Parameter_Block_Pack --
14172 --------------------------
14174 function Parameter_Block_Pack
14175 (Loc : Source_Ptr;
14176 Blk_Typ : Entity_Id;
14177 Actuals : List_Id;
14178 Formals : List_Id;
14179 Decls : List_Id;
14180 Stmts : List_Id) return Node_Id
14182 Actual : Entity_Id;
14183 Expr : Node_Id := Empty;
14184 Formal : Entity_Id;
14185 Has_Param : Boolean := False;
14186 P : Entity_Id;
14187 Params : List_Id;
14188 Temp_Asn : Node_Id;
14189 Temp_Nam : Node_Id;
14191 begin
14192 Actual := First (Actuals);
14193 Formal := Defining_Identifier (First (Formals));
14194 Params := New_List;
14196 while Present (Actual) loop
14197 if Is_By_Copy_Type (Etype (Actual)) then
14198 -- Generate:
14199 -- Jnn : aliased <formal-type>
14201 Temp_Nam := Make_Temporary (Loc, 'J');
14203 Append_To (Decls,
14204 Make_Object_Declaration (Loc,
14205 Aliased_Present =>
14206 True,
14207 Defining_Identifier =>
14208 Temp_Nam,
14209 Object_Definition =>
14210 New_Reference_To (Etype (Formal), Loc)));
14212 if Ekind (Formal) /= E_Out_Parameter then
14214 -- Generate:
14215 -- Jnn := <actual>
14217 Temp_Asn :=
14218 New_Reference_To (Temp_Nam, Loc);
14220 Set_Assignment_OK (Temp_Asn);
14222 Append_To (Stmts,
14223 Make_Assignment_Statement (Loc,
14224 Name =>
14225 Temp_Asn,
14226 Expression =>
14227 New_Copy_Tree (Actual)));
14228 end if;
14230 -- Generate:
14231 -- Jnn'unchecked_access
14233 Append_To (Params,
14234 Make_Attribute_Reference (Loc,
14235 Attribute_Name =>
14236 Name_Unchecked_Access,
14237 Prefix =>
14238 New_Reference_To (Temp_Nam, Loc)));
14240 Has_Param := True;
14242 -- The controlling parameter is omitted
14244 else
14245 if not Is_Controlling_Actual (Actual) then
14246 Append_To (Params,
14247 Make_Reference (Loc, New_Copy_Tree (Actual)));
14249 Has_Param := True;
14250 end if;
14251 end if;
14253 Next_Actual (Actual);
14254 Next_Formal_With_Extras (Formal);
14255 end loop;
14257 if Has_Param then
14258 Expr := Make_Aggregate (Loc, Params);
14259 end if;
14261 -- Generate:
14262 -- P : Ann := (
14263 -- J1'unchecked_access;
14264 -- <actual2>'reference;
14265 -- ...);
14267 P := Make_Temporary (Loc, 'P');
14269 Append_To (Decls,
14270 Make_Object_Declaration (Loc,
14271 Defining_Identifier =>
14273 Object_Definition =>
14274 New_Reference_To (Blk_Typ, Loc),
14275 Expression =>
14276 Expr));
14278 return P;
14279 end Parameter_Block_Pack;
14281 ----------------------------
14282 -- Parameter_Block_Unpack --
14283 ----------------------------
14285 function Parameter_Block_Unpack
14286 (Loc : Source_Ptr;
14287 P : Entity_Id;
14288 Actuals : List_Id;
14289 Formals : List_Id) return List_Id
14291 Actual : Entity_Id;
14292 Asnmt : Node_Id;
14293 Formal : Entity_Id;
14294 Has_Asnmt : Boolean := False;
14295 Result : constant List_Id := New_List;
14297 begin
14298 Actual := First (Actuals);
14299 Formal := Defining_Identifier (First (Formals));
14300 while Present (Actual) loop
14301 if Is_By_Copy_Type (Etype (Actual))
14302 and then Ekind (Formal) /= E_In_Parameter
14303 then
14304 -- Generate:
14305 -- <actual> := P.<formal>;
14307 Asnmt :=
14308 Make_Assignment_Statement (Loc,
14309 Name =>
14310 New_Copy (Actual),
14311 Expression =>
14312 Make_Explicit_Dereference (Loc,
14313 Make_Selected_Component (Loc,
14314 Prefix =>
14315 New_Reference_To (P, Loc),
14316 Selector_Name =>
14317 Make_Identifier (Loc, Chars (Formal)))));
14319 Set_Assignment_OK (Name (Asnmt));
14320 Append_To (Result, Asnmt);
14322 Has_Asnmt := True;
14323 end if;
14325 Next_Actual (Actual);
14326 Next_Formal_With_Extras (Formal);
14327 end loop;
14329 if Has_Asnmt then
14330 return Result;
14331 else
14332 return New_List (Make_Null_Statement (Loc));
14333 end if;
14334 end Parameter_Block_Unpack;
14336 ----------------------
14337 -- Set_Discriminals --
14338 ----------------------
14340 procedure Set_Discriminals (Dec : Node_Id) is
14341 D : Entity_Id;
14342 Pdef : Entity_Id;
14343 D_Minal : Entity_Id;
14345 begin
14346 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14347 Pdef := Defining_Identifier (Dec);
14349 if Has_Discriminants (Pdef) then
14350 D := First_Discriminant (Pdef);
14351 while Present (D) loop
14352 D_Minal :=
14353 Make_Defining_Identifier (Sloc (D),
14354 Chars => New_External_Name (Chars (D), 'D'));
14356 Set_Ekind (D_Minal, E_Constant);
14357 Set_Etype (D_Minal, Etype (D));
14358 Set_Scope (D_Minal, Pdef);
14359 Set_Discriminal (D, D_Minal);
14360 Set_Discriminal_Link (D_Minal, D);
14362 Next_Discriminant (D);
14363 end loop;
14364 end if;
14365 end Set_Discriminals;
14367 -----------------------
14368 -- Trivial_Accept_OK --
14369 -----------------------
14371 function Trivial_Accept_OK return Boolean is
14372 begin
14373 case Opt.Task_Dispatching_Policy is
14375 -- If we have the default task dispatching policy in effect, we can
14376 -- definitely do the optimization (one way of looking at this is to
14377 -- think of the formal definition of the default policy being allowed
14378 -- to run any task it likes after a rendezvous, so even if notionally
14379 -- a full rescheduling occurs, we can say that our dispatching policy
14380 -- (i.e. the default dispatching policy) reorders the queue to be the
14381 -- same as just before the call.
14383 when ' ' =>
14384 return True;
14386 -- FIFO_Within_Priorities certainly does not permit this
14387 -- optimization since the Rendezvous is a scheduling action that may
14388 -- require some other task to be run.
14390 when 'F' =>
14391 return False;
14393 -- For now, disallow the optimization for all other policies. This
14394 -- may be over-conservative, but it is certainly not incorrect.
14396 when others =>
14397 return False;
14399 end case;
14400 end Trivial_Accept_OK;
14402 end Exp_Ch9;