[multiple changes]
[gcc.git] / gcc / ada / exp_ch9.adb
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-2011, 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 ------------------------------------------------------------------------------
25
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_Ch11; use Sem_Ch11;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
66
67 package body Exp_Ch9 is
68
69 -- The following constant establishes the upper bound for the index of
70 -- an entry family. It is used to limit the allocated size of protected
71 -- types with defaulted discriminant of an integer type, when the bound
72 -- of some entry family depends on a discriminant. The limitation to
73 -- entry families of 128K should be reasonable in all cases, and is a
74 -- documented implementation restriction. It will be lifted when protected
75 -- entry families are re-implemented as a single ordered queue.
76
77 Entry_Family_Bound : constant Int := 2**16;
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
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.
91
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.
100
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.
112
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.
117
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.
124
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.
130
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).
138
139 function Build_Entry_Count_Expression
140 (Concurrent_Type : Node_Id;
141 Component_List : List_Id;
142 Loc : Source_Ptr) return Node_Id;
143 -- Compute number of entries for concurrent object. This is a count of
144 -- simple entries, followed by an expression that computes the length
145 -- of the range of each entry family. A single array with that size is
146 -- allocated for each concurrent object of the type.
147
148 function Build_Parameter_Block
149 (Loc : Source_Ptr;
150 Actuals : List_Id;
151 Formals : List_Id;
152 Decls : List_Id) return Entity_Id;
153 -- Generate an access type for each actual parameter in the list Actuals.
154 -- Create an encapsulating record that contains all the actuals and return
155 -- its type. Generate:
156 -- type Ann1 is access all <actual1-type>
157 -- ...
158 -- type AnnN is access all <actualN-type>
159 -- type Pnn is record
160 -- <formal1> : Ann1;
161 -- ...
162 -- <formalN> : AnnN;
163 -- end record;
164
165 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
166 -- Build body of wrapper procedure for an entry or entry family that has
167 -- pre/postconditions. The body gathers the PPC's and expands them in the
168 -- usual way, and performs the entry call itself. This way preconditions
169 -- are evaluated before the call is queued. E is the entry in question,
170 -- and Decl is the enclosing synchronized type declaration at whose
171 -- freeze point the generated body is analyzed.
172
173 function Build_Renamed_Formal_Declaration
174 (New_F : Entity_Id;
175 Formal : Entity_Id;
176 Comp : Entity_Id;
177 Renamed_Formal : Node_Id) return Node_Id;
178 -- Create a renaming declaration for a formal, within a protected entry
179 -- body or an accept body. The renamed object is a component of the
180 -- parameter block that is a parameter in the entry call.
181
182 -- In Ada2012, If the formal is an incomplete tagged type, the renaming
183 -- does not dereference the corresponding component to prevent an illegal
184 -- use of the incomplete type (AI05-0151).
185
186 procedure Build_Wrapper_Bodies
187 (Loc : Source_Ptr;
188 Typ : Entity_Id;
189 N : Node_Id);
190 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
191 -- record of a concurrent type. N is the insertion node where all bodies
192 -- will be placed. This routine builds the bodies of the subprograms which
193 -- serve as an indirection mechanism to overriding primitives of concurrent
194 -- types, entries and protected procedures. Any new body is analyzed.
195
196 procedure Build_Wrapper_Specs
197 (Loc : Source_Ptr;
198 Typ : Entity_Id;
199 N : in out Node_Id);
200 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
201 -- record of a concurrent type. N is the insertion node where all specs
202 -- will be placed. This routine builds the specs of the subprograms which
203 -- serve as an indirection mechanism to overriding primitives of concurrent
204 -- types, entries and protected procedures. Any new spec is analyzed.
205
206 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
207 -- Build the function that translates the entry index in the call
208 -- (which depends on the size of entry families) into an index into the
209 -- Entry_Bodies_Array, to determine the body and barrier function used
210 -- in a protected entry call. A pointer to this function appears in every
211 -- protected object.
212
213 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
214 -- Build subprogram declaration for previous one
215
216 function Build_Protected_Entry
217 (N : Node_Id;
218 Ent : Entity_Id;
219 Pid : Node_Id) return Node_Id;
220 -- Build the procedure implementing the statement sequence of the specified
221 -- entry body.
222
223 function Build_Protected_Entry_Specification
224 (Loc : Source_Ptr;
225 Def_Id : Entity_Id;
226 Ent_Id : Entity_Id) return Node_Id;
227 -- Build a specification for the procedure implementing the statements of
228 -- the specified entry body. Add attributes associating it with the entry
229 -- defining identifier Ent_Id.
230
231 function Build_Protected_Spec
232 (N : Node_Id;
233 Obj_Type : Entity_Id;
234 Ident : Entity_Id;
235 Unprotected : Boolean := False) return List_Id;
236 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
237 -- Subprogram_Type. Builds signature of protected subprogram, adding the
238 -- formal that corresponds to the object itself. For an access to protected
239 -- subprogram, there is no object type to specify, so the parameter has
240 -- type Address and mode In. An indirect call through such a pointer will
241 -- convert the address to a reference to the actual object. The object is
242 -- a limited record and therefore a by_reference type.
243
244 function Build_Protected_Subprogram_Body
245 (N : Node_Id;
246 Pid : Node_Id;
247 N_Op_Spec : Node_Id) return Node_Id;
248 -- This function is used to construct the protected version of a protected
249 -- subprogram. Its statement sequence first defers abort, then locks
250 -- the associated protected object, and then enters a block that contains
251 -- a call to the unprotected version of the subprogram (for details, see
252 -- Build_Unprotected_Subprogram_Body). This block statement requires
253 -- a cleanup handler that unlocks the object in all cases.
254 -- (see Exp_Ch7.Expand_Cleanup_Actions).
255
256 function Build_Selected_Name
257 (Prefix : Entity_Id;
258 Selector : Entity_Id;
259 Append_Char : Character := ' ') return Name_Id;
260 -- Build a name in the form of Prefix__Selector, with an optional
261 -- character appended. This is used for internal subprograms generated
262 -- for operations of protected types, including barrier functions.
263 -- For the subprograms generated for entry bodies and entry barriers,
264 -- the generated name includes a sequence number that makes names
265 -- unique in the presence of entry overloading. This is necessary
266 -- because entry body procedures and barrier functions all have the
267 -- same signature.
268
269 procedure Build_Simple_Entry_Call
270 (N : Node_Id;
271 Concval : Node_Id;
272 Ename : Node_Id;
273 Index : Node_Id);
274 -- Some comments here would be useful ???
275
276 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
277 -- This routine constructs a specification for the procedure that we will
278 -- build for the task body for task type T. The spec has the form:
279 --
280 -- procedure tnameB (_Task : access tnameV);
281 --
282 -- where name is the character name taken from the task type entity that
283 -- is passed as the argument to the procedure, and tnameV is the task
284 -- value type that is associated with the task type.
285
286 function Build_Unprotected_Subprogram_Body
287 (N : Node_Id;
288 Pid : Node_Id) return Node_Id;
289 -- This routine constructs the unprotected version of a protected
290 -- subprogram body, which is contains all of the code in the
291 -- original, unexpanded body. This is the version of the protected
292 -- subprogram that is called from all protected operations on the same
293 -- object, including the protected version of the same subprogram.
294
295 procedure Collect_Entry_Families
296 (Loc : Source_Ptr;
297 Cdecls : List_Id;
298 Current_Node : in out Node_Id;
299 Conctyp : Entity_Id);
300 -- For each entry family in a concurrent type, create an anonymous array
301 -- type of the right size, and add a component to the corresponding_record.
302
303 function Concurrent_Object
304 (Spec_Id : Entity_Id;
305 Conc_Typ : Entity_Id) return Entity_Id;
306 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
307 -- the entity associated with the concurrent object in the Protected_Body_
308 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
309 -- denotes formal parameter _O, _object or _task.
310
311 function Copy_Result_Type (Res : Node_Id) return Node_Id;
312 -- Copy the result type of a function specification, when building the
313 -- internal operation corresponding to a protected function, or when
314 -- expanding an access to protected function. If the result is an anonymous
315 -- access to subprogram itself, we need to create a new signature with the
316 -- same parameter names and the same resolved types, but with new entities
317 -- for the formals.
318
319 procedure Debug_Private_Data_Declarations (Decls : List_Id);
320 -- Decls is a list which may contain the declarations created by Install_
321 -- Private_Data_Declarations. All generated entities are marked as needing
322 -- debug info and debug nodes are manually generation where necessary. This
323 -- step of the expansion must to be done after private data has been moved
324 -- to its final resting scope to ensure proper visibility of debug objects.
325
326 function Family_Offset
327 (Loc : Source_Ptr;
328 Hi : Node_Id;
329 Lo : Node_Id;
330 Ttyp : Entity_Id;
331 Cap : Boolean) return Node_Id;
332 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in
333 -- an accept statement, or the upper bound in the discrete subtype of
334 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
335 -- the concurrent type of the entry. If Cap is true, the result is
336 -- capped according to Entry_Family_Bound.
337
338 function Family_Size
339 (Loc : Source_Ptr;
340 Hi : Node_Id;
341 Lo : Node_Id;
342 Ttyp : Entity_Id;
343 Cap : Boolean) return Node_Id;
344 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
345 -- a family, and handle properly the superflat case. This is equivalent
346 -- to the use of 'Length on the index type, but must use Family_Offset
347 -- to handle properly the case of bounds that depend on discriminants.
348 -- If Cap is true, the result is capped according to Entry_Family_Bound.
349
350 procedure Extract_Dispatching_Call
351 (N : Node_Id;
352 Call_Ent : out Entity_Id;
353 Object : out Entity_Id;
354 Actuals : out List_Id;
355 Formals : out List_Id);
356 -- Given a dispatching call, extract the entity of the name of the call,
357 -- its actual dispatching object, its actual parameters and the formal
358 -- parameters of the overridden interface-level version. If the type of
359 -- the dispatching object is an access type then an explicit dereference
360 -- is returned in Object.
361
362 procedure Extract_Entry
363 (N : Node_Id;
364 Concval : out Node_Id;
365 Ename : out Node_Id;
366 Index : out Node_Id);
367 -- Given an entry call, returns the associated concurrent object,
368 -- the entry name, and the entry family index.
369
370 function Find_Task_Or_Protected_Pragma
371 (T : Node_Id;
372 P : Name_Id) return Node_Id;
373 -- Searches the task or protected definition T for the first occurrence
374 -- of the pragma whose name is given by P. The caller has ensured that
375 -- the pragma is present in the task definition. A special case is that
376 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
377 -- ??? Should be implemented with the rep item chain mechanism.
378
379 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
380 -- Given a subprogram identifier, return the entity which is associated
381 -- with the protection entry index in the Protected_Body_Subprogram or the
382 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
383 -- parameter _E.
384
385 function Is_Potentially_Large_Family
386 (Base_Index : Entity_Id;
387 Conctyp : Entity_Id;
388 Lo : Node_Id;
389 Hi : Node_Id) return Boolean;
390
391 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
392 -- Determine whether Id is a function or a procedure and is marked as a
393 -- private primitive.
394
395 function Null_Statements (Stats : List_Id) return Boolean;
396 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
397 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
398 -- well to still count as null. Returns True for a null sequence. The
399 -- argument is the list of statements from the DO-END sequence.
400
401 function Parameter_Block_Pack
402 (Loc : Source_Ptr;
403 Blk_Typ : Entity_Id;
404 Actuals : List_Id;
405 Formals : List_Id;
406 Decls : List_Id;
407 Stmts : List_Id) return Entity_Id;
408 -- Set the components of the generated parameter block with the values of
409 -- the actual parameters. Generate aliased temporaries to capture the
410 -- values for types that are passed by copy. Otherwise generate a reference
411 -- to the actual's value. Return the address of the aggregate block.
412 -- Generate:
413 -- Jnn1 : alias <formal-type1>;
414 -- Jnn1 := <actual1>;
415 -- ...
416 -- P : Blk_Typ := (
417 -- Jnn1'unchecked_access;
418 -- <actual2>'reference;
419 -- ...);
420
421 function Parameter_Block_Unpack
422 (Loc : Source_Ptr;
423 P : Entity_Id;
424 Actuals : List_Id;
425 Formals : List_Id) return List_Id;
426 -- Retrieve the values of the components from the parameter block and
427 -- assign then to the original actual parameters. Generate:
428 -- <actual1> := P.<formal1>;
429 -- ...
430 -- <actualN> := P.<formalN>;
431
432 function Trivial_Accept_OK return Boolean;
433 -- If there is no DO-END block for an accept, or if the DO-END block has
434 -- only null statements, then it is possible to do the Rendezvous with much
435 -- less overhead using the Accept_Trivial routine in the run-time library.
436 -- However, this is not always a valid optimization. Whether it is valid or
437 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
438 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
439 -- a rescheduling is required, so this optimization is not allowed. This
440 -- function returns True if the optimization is permitted.
441
442 -----------------------------
443 -- Actual_Index_Expression --
444 -----------------------------
445
446 function Actual_Index_Expression
447 (Sloc : Source_Ptr;
448 Ent : Entity_Id;
449 Index : Node_Id;
450 Tsk : Entity_Id) return Node_Id
451 is
452 Ttyp : constant Entity_Id := Etype (Tsk);
453 Expr : Node_Id;
454 Num : Node_Id;
455 Lo : Node_Id;
456 Hi : Node_Id;
457 Prev : Entity_Id;
458 S : Node_Id;
459
460 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
461 -- Compute difference between bounds of entry family
462
463 --------------------------
464 -- Actual_Family_Offset --
465 --------------------------
466
467 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
468
469 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
470 -- Replace a reference to a discriminant with a selected component
471 -- denoting the discriminant of the target task.
472
473 -----------------------------
474 -- Actual_Discriminant_Ref --
475 -----------------------------
476
477 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
478 Typ : constant Entity_Id := Etype (Bound);
479 B : Node_Id;
480
481 begin
482 if not Is_Entity_Name (Bound)
483 or else Ekind (Entity (Bound)) /= E_Discriminant
484 then
485 if Nkind (Bound) = N_Attribute_Reference then
486 return Bound;
487 else
488 B := New_Copy_Tree (Bound);
489 end if;
490
491 else
492 B :=
493 Make_Selected_Component (Sloc,
494 Prefix => New_Copy_Tree (Tsk),
495 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
496
497 Analyze_And_Resolve (B, Typ);
498 end if;
499
500 return
501 Make_Attribute_Reference (Sloc,
502 Attribute_Name => Name_Pos,
503 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
504 Expressions => New_List (B));
505 end Actual_Discriminant_Ref;
506
507 -- Start of processing for Actual_Family_Offset
508
509 begin
510 return
511 Make_Op_Subtract (Sloc,
512 Left_Opnd => Actual_Discriminant_Ref (Hi),
513 Right_Opnd => Actual_Discriminant_Ref (Lo));
514 end Actual_Family_Offset;
515
516 -- Start of processing for Actual_Index_Expression
517
518 begin
519 -- The queues of entries and entry families appear in textual order in
520 -- the associated record. The entry index is computed as the sum of the
521 -- number of queues for all entries that precede the designated one, to
522 -- which is added the index expression, if this expression denotes a
523 -- member of a family.
524
525 -- The following is a place holder for the count of simple entries
526
527 Num := Make_Integer_Literal (Sloc, 1);
528
529 -- We construct an expression which is a series of addition operations.
530 -- See comments in Entry_Index_Expression, which is identical in
531 -- structure.
532
533 if Present (Index) then
534 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
535
536 Expr :=
537 Make_Op_Add (Sloc,
538 Left_Opnd => Num,
539
540 Right_Opnd =>
541 Actual_Family_Offset (
542 Make_Attribute_Reference (Sloc,
543 Attribute_Name => Name_Pos,
544 Prefix => New_Reference_To (Base_Type (S), Sloc),
545 Expressions => New_List (Relocate_Node (Index))),
546 Type_Low_Bound (S)));
547 else
548 Expr := Num;
549 end if;
550
551 -- Now add lengths of preceding entries and entry families
552
553 Prev := First_Entity (Ttyp);
554
555 while Chars (Prev) /= Chars (Ent)
556 or else (Ekind (Prev) /= Ekind (Ent))
557 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
558 loop
559 if Ekind (Prev) = E_Entry then
560 Set_Intval (Num, Intval (Num) + 1);
561
562 elsif Ekind (Prev) = E_Entry_Family then
563 S :=
564 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
565
566 -- The need for the following full view retrieval stems from
567 -- this complex case of nested generics and tasking:
568
569 -- generic
570 -- type Formal_Index is range <>;
571 -- ...
572 -- package Outer is
573 -- type Index is private;
574 -- generic
575 -- ...
576 -- package Inner is
577 -- procedure P;
578 -- end Inner;
579 -- private
580 -- type Index is new Formal_Index range 1 .. 10;
581 -- end Outer;
582
583 -- package body Outer is
584 -- task type T is
585 -- entry Fam (Index); -- (2)
586 -- entry E;
587 -- end T;
588 -- package body Inner is -- (3)
589 -- procedure P is
590 -- begin
591 -- T.E; -- (1)
592 -- end P;
593 -- end Inner;
594 -- ...
595
596 -- We are currently building the index expression for the entry
597 -- call "T.E" (1). Part of the expansion must mention the range
598 -- of the discrete type "Index" (2) of entry family "Fam".
599 -- However only the private view of type "Index" is available to
600 -- the inner generic (3) because there was no prior mention of
601 -- the type inside "Inner". This visibility requirement is
602 -- implicit and cannot be detected during the construction of
603 -- the generic trees and needs special handling.
604
605 if In_Instance_Body
606 and then Is_Private_Type (S)
607 and then Present (Full_View (S))
608 then
609 S := Full_View (S);
610 end if;
611
612 Lo := Type_Low_Bound (S);
613 Hi := Type_High_Bound (S);
614
615 Expr :=
616 Make_Op_Add (Sloc,
617 Left_Opnd => Expr,
618 Right_Opnd =>
619 Make_Op_Add (Sloc,
620 Left_Opnd =>
621 Actual_Family_Offset (Hi, Lo),
622 Right_Opnd =>
623 Make_Integer_Literal (Sloc, 1)));
624
625 -- Other components are anonymous types to be ignored
626
627 else
628 null;
629 end if;
630
631 Next_Entity (Prev);
632 end loop;
633
634 return Expr;
635 end Actual_Index_Expression;
636
637 --------------------------
638 -- Add_Formal_Renamings --
639 --------------------------
640
641 procedure Add_Formal_Renamings
642 (Spec : Node_Id;
643 Decls : List_Id;
644 Ent : Entity_Id;
645 Loc : Source_Ptr)
646 is
647 Ptr : constant Entity_Id :=
648 Defining_Identifier
649 (Next (First (Parameter_Specifications (Spec))));
650 -- The name of the formal that holds the address of the parameter block
651 -- for the call.
652
653 Comp : Entity_Id;
654 Decl : Node_Id;
655 Formal : Entity_Id;
656 New_F : Entity_Id;
657 Renamed_Formal : Node_Id;
658
659 begin
660 Formal := First_Formal (Ent);
661 while Present (Formal) loop
662 Comp := Entry_Component (Formal);
663 New_F :=
664 Make_Defining_Identifier (Sloc (Formal),
665 Chars => Chars (Formal));
666 Set_Etype (New_F, Etype (Formal));
667 Set_Scope (New_F, Ent);
668
669 -- Now we set debug info needed on New_F even though it does not
670 -- come from source, so that the debugger will get the right
671 -- information for these generated names.
672
673 Set_Debug_Info_Needed (New_F);
674
675 if Ekind (Formal) = E_In_Parameter then
676 Set_Ekind (New_F, E_Constant);
677 else
678 Set_Ekind (New_F, E_Variable);
679 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
680 end if;
681
682 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
683
684 Renamed_Formal :=
685 Make_Selected_Component (Loc,
686 Prefix =>
687 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
688 Make_Identifier (Loc, Chars (Ptr))),
689 Selector_Name => New_Reference_To (Comp, Loc));
690
691 Decl :=
692 Build_Renamed_Formal_Declaration
693 (New_F, Formal, Comp, Renamed_Formal);
694
695 Append (Decl, Decls);
696 Set_Renamed_Object (Formal, New_F);
697 Next_Formal (Formal);
698 end loop;
699 end Add_Formal_Renamings;
700
701 ------------------------
702 -- Add_Object_Pointer --
703 ------------------------
704
705 procedure Add_Object_Pointer
706 (Loc : Source_Ptr;
707 Conc_Typ : Entity_Id;
708 Decls : List_Id)
709 is
710 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
711 Decl : Node_Id;
712 Obj_Ptr : Node_Id;
713
714 begin
715 -- Create the renaming declaration for the Protection object of a
716 -- protected type. _Object is used by Complete_Entry_Body.
717 -- ??? An attempt to make this a renaming was unsuccessful.
718
719 -- Build the entity for the access type
720
721 Obj_Ptr :=
722 Make_Defining_Identifier (Loc,
723 New_External_Name (Chars (Rec_Typ), 'P'));
724
725 -- Generate:
726 -- _object : poVP := poVP!O;
727
728 Decl :=
729 Make_Object_Declaration (Loc,
730 Defining_Identifier =>
731 Make_Defining_Identifier (Loc, Name_uObject),
732 Object_Definition =>
733 New_Reference_To (Obj_Ptr, Loc),
734 Expression =>
735 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
736 Set_Debug_Info_Needed (Defining_Identifier (Decl));
737 Prepend_To (Decls, Decl);
738
739 -- Generate:
740 -- type poVP is access poV;
741
742 Decl :=
743 Make_Full_Type_Declaration (Loc,
744 Defining_Identifier =>
745 Obj_Ptr,
746 Type_Definition =>
747 Make_Access_To_Object_Definition (Loc,
748 Subtype_Indication =>
749 New_Reference_To (Rec_Typ, Loc)));
750 Set_Debug_Info_Needed (Defining_Identifier (Decl));
751 Prepend_To (Decls, Decl);
752 end Add_Object_Pointer;
753
754 -----------------------
755 -- Build_Accept_Body --
756 -----------------------
757
758 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
759 Loc : constant Source_Ptr := Sloc (Astat);
760 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
761 New_S : Node_Id;
762 Hand : Node_Id;
763 Call : Node_Id;
764 Ohandle : Node_Id;
765
766 begin
767 -- At the end of the statement sequence, Complete_Rendezvous is called.
768 -- A label skipping the Complete_Rendezvous, and all other accept
769 -- processing, has already been added for the expansion of requeue
770 -- statements. The Sloc is copied from the last statement since it
771 -- is really part of this last statement.
772
773 Call :=
774 Build_Runtime_Call
775 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
776 Insert_Before (Last (Statements (Stats)), Call);
777 Analyze (Call);
778
779 -- If exception handlers are present, then append Complete_Rendezvous
780 -- calls to the handlers, and construct the required outer block. As
781 -- above, the Sloc is copied from the last statement in the sequence.
782
783 if Present (Exception_Handlers (Stats)) then
784 Hand := First (Exception_Handlers (Stats));
785 while Present (Hand) loop
786 Call :=
787 Build_Runtime_Call
788 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
789 Append (Call, Statements (Hand));
790 Analyze (Call);
791 Next (Hand);
792 end loop;
793
794 New_S :=
795 Make_Handled_Sequence_Of_Statements (Loc,
796 Statements => New_List (
797 Make_Block_Statement (Loc,
798 Handled_Statement_Sequence => Stats)));
799
800 else
801 New_S := Stats;
802 end if;
803
804 -- At this stage we know that the new statement sequence does not
805 -- have an exception handler part, so we supply one to call
806 -- Exceptional_Complete_Rendezvous. This handler is
807
808 -- when all others =>
809 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
810
811 -- We handle Abort_Signal to make sure that we properly catch the abort
812 -- case and wake up the caller.
813
814 Ohandle := Make_Others_Choice (Loc);
815 Set_All_Others (Ohandle);
816
817 Set_Exception_Handlers (New_S,
818 New_List (
819 Make_Implicit_Exception_Handler (Loc,
820 Exception_Choices => New_List (Ohandle),
821
822 Statements => New_List (
823 Make_Procedure_Call_Statement (Sloc (Stats),
824 Name => New_Reference_To (
825 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
826 Parameter_Associations => New_List (
827 Make_Function_Call (Sloc (Stats),
828 Name => New_Reference_To (
829 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
830
831 Set_Parent (New_S, Astat); -- temp parent for Analyze call
832 Analyze_Exception_Handlers (Exception_Handlers (New_S));
833 Expand_Exception_Handlers (New_S);
834
835 -- Exceptional_Complete_Rendezvous must be called with abort
836 -- still deferred, which is the case for a "when all others" handler.
837
838 return New_S;
839 end Build_Accept_Body;
840
841 -----------------------------------
842 -- Build_Activation_Chain_Entity --
843 -----------------------------------
844
845 procedure Build_Activation_Chain_Entity (N : Node_Id) is
846 P : Node_Id;
847 Decls : List_Id;
848 Chain : Entity_Id;
849
850 begin
851 -- Loop to find enclosing construct containing activation chain variable
852 -- The construct is a body, a block, or an extended return.
853
854 P := Parent (N);
855
856 while not Nkind_In (P, N_Subprogram_Body,
857 N_Entry_Body,
858 N_Package_Declaration,
859 N_Package_Body,
860 N_Block_Statement,
861 N_Task_Body,
862 N_Extended_Return_Statement)
863 loop
864 P := Parent (P);
865 end loop;
866
867 -- If we are in a package body, the activation chain variable is
868 -- declared in the body, but the Activation_Chain_Entity is attached
869 -- to the spec.
870
871 if Nkind (P) = N_Package_Body then
872 Decls := Declarations (P);
873 P := Unit_Declaration_Node (Corresponding_Spec (P));
874
875 elsif Nkind (P) = N_Package_Declaration then
876 Decls := Visible_Declarations (Specification (P));
877
878 elsif Nkind (P) = N_Extended_Return_Statement then
879 Decls := Return_Object_Declarations (P);
880
881 else
882 Decls := Declarations (P);
883 end if;
884
885 -- If activation chain entity not already declared, declare it
886
887 if Nkind (P) = N_Extended_Return_Statement
888 or else No (Activation_Chain_Entity (P))
889 then
890 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
891
892 -- Note: An extended return statement is not really a task activator,
893 -- but it does have an activation chain on which to store the tasks
894 -- temporarily. On successful return, the tasks on this chain are
895 -- moved to the chain passed in by the caller. We do not build an
896 -- Activation_Chain_Entity for an N_Extended_Return_Statement,
897 -- because we do not want to build a call to Activate_Tasks. Task
898 -- activation is the responsibility of the caller.
899
900 if Nkind (P) /= N_Extended_Return_Statement then
901 Set_Activation_Chain_Entity (P, Chain);
902 end if;
903
904 Prepend_To (Decls,
905 Make_Object_Declaration (Sloc (P),
906 Defining_Identifier => Chain,
907 Aliased_Present => True,
908 Object_Definition =>
909 New_Reference_To (RTE (RE_Activation_Chain), Sloc (P))));
910
911 Analyze (First (Decls));
912 end if;
913 end Build_Activation_Chain_Entity;
914
915 ----------------------------
916 -- Build_Barrier_Function --
917 ----------------------------
918
919 function Build_Barrier_Function
920 (N : Node_Id;
921 Ent : Entity_Id;
922 Pid : Node_Id) return Node_Id
923 is
924 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
925 Cond : constant Node_Id := Condition (Ent_Formals);
926 Loc : constant Source_Ptr := Sloc (Cond);
927 Func_Id : constant Entity_Id := Barrier_Function (Ent);
928 Op_Decls : constant List_Id := New_List;
929 Stmt : Node_Id;
930 Func_Body : Node_Id;
931
932 begin
933 -- Add a declaration for the Protection object, renaming declarations
934 -- for the discriminals and privals and finally a declaration for the
935 -- entry family index (if applicable).
936
937 Install_Private_Data_Declarations (Sloc (N),
938 Spec_Id => Func_Id,
939 Conc_Typ => Pid,
940 Body_Nod => N,
941 Decls => Op_Decls,
942 Barrier => True,
943 Family => Ekind (Ent) = E_Entry_Family);
944
945 -- If compiling with -fpreserve-control-flow, make sure we insert an
946 -- IF statement so that the back-end knows to generate a conditional
947 -- branch instruction, even if the condition is just the name of a
948 -- boolean object.
949
950 if Opt.Suppress_Control_Flow_Optimizations then
951 Stmt := Make_Implicit_If_Statement (Cond,
952 Condition => Cond,
953 Then_Statements => New_List (
954 Make_Simple_Return_Statement (Loc,
955 New_Occurrence_Of (Standard_True, Loc))),
956 Else_Statements => New_List (
957 Make_Simple_Return_Statement (Loc,
958 New_Occurrence_Of (Standard_False, Loc))));
959
960 else
961 Stmt := Make_Simple_Return_Statement (Loc, Cond);
962 end if;
963
964 -- Note: the condition in the barrier function needs to be properly
965 -- processed for the C/Fortran boolean possibility, but this happens
966 -- automatically since the return statement does this normalization.
967
968 Func_Body :=
969 Make_Subprogram_Body (Loc,
970 Specification =>
971 Build_Barrier_Function_Specification (Loc,
972 Make_Defining_Identifier (Loc, Chars (Func_Id))),
973 Declarations => Op_Decls,
974 Handled_Statement_Sequence =>
975 Make_Handled_Sequence_Of_Statements (Loc,
976 Statements => New_List (Stmt)));
977 Set_Is_Entry_Barrier_Function (Func_Body);
978
979 return Func_Body;
980 end Build_Barrier_Function;
981
982 ------------------------------------------
983 -- Build_Barrier_Function_Specification --
984 ------------------------------------------
985
986 function Build_Barrier_Function_Specification
987 (Loc : Source_Ptr;
988 Def_Id : Entity_Id) return Node_Id
989 is
990 begin
991 Set_Debug_Info_Needed (Def_Id);
992
993 return Make_Function_Specification (Loc,
994 Defining_Unit_Name => Def_Id,
995 Parameter_Specifications => New_List (
996 Make_Parameter_Specification (Loc,
997 Defining_Identifier =>
998 Make_Defining_Identifier (Loc, Name_uO),
999 Parameter_Type =>
1000 New_Reference_To (RTE (RE_Address), Loc)),
1001
1002 Make_Parameter_Specification (Loc,
1003 Defining_Identifier =>
1004 Make_Defining_Identifier (Loc, Name_uE),
1005 Parameter_Type =>
1006 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1007
1008 Result_Definition =>
1009 New_Reference_To (Standard_Boolean, Loc));
1010 end Build_Barrier_Function_Specification;
1011
1012 --------------------------
1013 -- Build_Call_With_Task --
1014 --------------------------
1015
1016 function Build_Call_With_Task
1017 (N : Node_Id;
1018 E : Entity_Id) return Node_Id
1019 is
1020 Loc : constant Source_Ptr := Sloc (N);
1021 begin
1022 return
1023 Make_Function_Call (Loc,
1024 Name => New_Reference_To (E, Loc),
1025 Parameter_Associations => New_List (Concurrent_Ref (N)));
1026 end Build_Call_With_Task;
1027
1028 --------------------------------
1029 -- Build_Corresponding_Record --
1030 --------------------------------
1031
1032 function Build_Corresponding_Record
1033 (N : Node_Id;
1034 Ctyp : Entity_Id;
1035 Loc : Source_Ptr) return Node_Id
1036 is
1037 Rec_Ent : constant Entity_Id :=
1038 Make_Defining_Identifier
1039 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1040 Disc : Entity_Id;
1041 Dlist : List_Id;
1042 New_Disc : Entity_Id;
1043 Cdecls : List_Id;
1044
1045 begin
1046 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1047 Set_Ekind (Rec_Ent, E_Record_Type);
1048 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1049 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1050 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1051 Set_Stored_Constraint (Rec_Ent, No_Elist);
1052 Cdecls := New_List;
1053
1054 -- Use discriminals to create list of discriminants for record, and
1055 -- create new discriminals for use in default expressions, etc. It is
1056 -- worth noting that a task discriminant gives rise to 5 entities;
1057
1058 -- a) The original discriminant.
1059 -- b) The discriminal for use in the task.
1060 -- c) The discriminant of the corresponding record.
1061 -- d) The discriminal for the init proc of the corresponding record.
1062 -- e) The local variable that renames the discriminant in the procedure
1063 -- for the task body.
1064
1065 -- In fact the discriminals b) are used in the renaming declarations
1066 -- for e). See details in einfo (Handling of Discriminants).
1067
1068 if Present (Discriminant_Specifications (N)) then
1069 Dlist := New_List;
1070 Disc := First_Discriminant (Ctyp);
1071
1072 while Present (Disc) loop
1073 New_Disc := CR_Discriminant (Disc);
1074
1075 Append_To (Dlist,
1076 Make_Discriminant_Specification (Loc,
1077 Defining_Identifier => New_Disc,
1078 Discriminant_Type =>
1079 New_Occurrence_Of (Etype (Disc), Loc),
1080 Expression =>
1081 New_Copy (Discriminant_Default_Value (Disc))));
1082
1083 Next_Discriminant (Disc);
1084 end loop;
1085
1086 else
1087 Dlist := No_List;
1088 end if;
1089
1090 -- Now we can construct the record type declaration. Note that this
1091 -- record is "limited tagged". It is "limited" to reflect the underlying
1092 -- limitedness of the task or protected object that it represents, and
1093 -- ensuring for example that it is properly passed by reference. It is
1094 -- "tagged" to give support to dispatching calls through interfaces. We
1095 -- propagate here the list of interfaces covered by the concurrent type
1096 -- (Ada 2005: AI-345).
1097
1098 return
1099 Make_Full_Type_Declaration (Loc,
1100 Defining_Identifier => Rec_Ent,
1101 Discriminant_Specifications => Dlist,
1102 Type_Definition =>
1103 Make_Record_Definition (Loc,
1104 Component_List =>
1105 Make_Component_List (Loc,
1106 Component_Items => Cdecls),
1107 Tagged_Present =>
1108 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1109 Interface_List => Interface_List (N),
1110 Limited_Present => True));
1111 end Build_Corresponding_Record;
1112
1113 ----------------------------------
1114 -- Build_Entry_Count_Expression --
1115 ----------------------------------
1116
1117 function Build_Entry_Count_Expression
1118 (Concurrent_Type : Node_Id;
1119 Component_List : List_Id;
1120 Loc : Source_Ptr) return Node_Id
1121 is
1122 Eindx : Nat;
1123 Ent : Entity_Id;
1124 Ecount : Node_Id;
1125 Comp : Node_Id;
1126 Lo : Node_Id;
1127 Hi : Node_Id;
1128 Typ : Entity_Id;
1129 Large : Boolean;
1130
1131 begin
1132 -- Count number of non-family entries
1133
1134 Eindx := 0;
1135 Ent := First_Entity (Concurrent_Type);
1136 while Present (Ent) loop
1137 if Ekind (Ent) = E_Entry then
1138 Eindx := Eindx + 1;
1139 end if;
1140
1141 Next_Entity (Ent);
1142 end loop;
1143
1144 Ecount := Make_Integer_Literal (Loc, Eindx);
1145
1146 -- Loop through entry families building the addition nodes
1147
1148 Ent := First_Entity (Concurrent_Type);
1149 Comp := First (Component_List);
1150 while Present (Ent) loop
1151 if Ekind (Ent) = E_Entry_Family then
1152 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1153 Next (Comp);
1154 end loop;
1155
1156 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1157 Hi := Type_High_Bound (Typ);
1158 Lo := Type_Low_Bound (Typ);
1159 Large := Is_Potentially_Large_Family
1160 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1161 Ecount :=
1162 Make_Op_Add (Loc,
1163 Left_Opnd => Ecount,
1164 Right_Opnd => Family_Size
1165 (Loc, Hi, Lo, Concurrent_Type, Large));
1166 end if;
1167
1168 Next_Entity (Ent);
1169 end loop;
1170
1171 return Ecount;
1172 end Build_Entry_Count_Expression;
1173
1174 -----------------------
1175 -- Build_Entry_Names --
1176 -----------------------
1177
1178 function Build_Entry_Names (Conc_Typ : Entity_Id) return Node_Id is
1179 Loc : constant Source_Ptr := Sloc (Conc_Typ);
1180 B_Decls : List_Id;
1181 B_Stmts : List_Id;
1182 Comp : Node_Id;
1183 Index : Entity_Id;
1184 Index_Typ : RE_Id;
1185 Typ : Entity_Id := Conc_Typ;
1186
1187 procedure Build_Entry_Family_Name (Id : Entity_Id);
1188 -- Generate:
1189 -- for Lnn in Family_Low .. Family_High loop
1190 -- Inn := Inn + 1;
1191 -- Set_Entry_Name
1192 -- (_init._object <or> _init._task_id,
1193 -- Inn,
1194 -- new String ("<Entry name>(" & Lnn'Img & ")"));
1195 -- end loop;
1196 -- Note that the bounds of the range may reference discriminants. The
1197 -- above construct is added directly to the statements of the block.
1198
1199 procedure Build_Entry_Name (Id : Entity_Id);
1200 -- Generate:
1201 -- Inn := Inn + 1;
1202 -- Set_Entry_Name
1203 -- (_init._object <or>_init._task_id,
1204 -- Inn,
1205 -- new String ("<Entry name>");
1206 -- The above construct is added directly to the statements of the block.
1207
1208 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id;
1209 -- Generate the call to the runtime routine Set_Entry_Name with actuals
1210 -- _init._task_id or _init._object, Inn and Arg3.
1211
1212 procedure Increment_Index (Stmts : List_Id);
1213 -- Generate the following and add it to Stmts
1214 -- Inn := Inn + 1;
1215
1216 -----------------------------
1217 -- Build_Entry_Family_Name --
1218 -----------------------------
1219
1220 procedure Build_Entry_Family_Name (Id : Entity_Id) is
1221 Def : constant Node_Id :=
1222 Discrete_Subtype_Definition (Parent (Id));
1223 L_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
1224 L_Stmts : constant List_Id := New_List;
1225 Val : Node_Id;
1226
1227 function Build_Range (Def : Node_Id) return Node_Id;
1228 -- Given a discrete subtype definition of an entry family, generate a
1229 -- range node which covers the range of Def's type.
1230
1231 -----------------
1232 -- Build_Range --
1233 -----------------
1234
1235 function Build_Range (Def : Node_Id) return Node_Id is
1236 High : Node_Id := Type_High_Bound (Etype (Def));
1237 Low : Node_Id := Type_Low_Bound (Etype (Def));
1238
1239 begin
1240 -- If a bound references a discriminant, generate an identifier
1241 -- with the same name. Resolution will map it to the formals of
1242 -- the init proc.
1243
1244 if Is_Entity_Name (Low)
1245 and then Ekind (Entity (Low)) = E_Discriminant
1246 then
1247 Low := Make_Identifier (Loc, Chars (Low));
1248 else
1249 Low := New_Copy_Tree (Low);
1250 end if;
1251
1252 if Is_Entity_Name (High)
1253 and then Ekind (Entity (High)) = E_Discriminant
1254 then
1255 High := Make_Identifier (Loc, Chars (High));
1256 else
1257 High := New_Copy_Tree (High);
1258 end if;
1259
1260 return
1261 Make_Range (Loc,
1262 Low_Bound => Low,
1263 High_Bound => High);
1264 end Build_Range;
1265
1266 -- Start of processing for Build_Entry_Family_Name
1267
1268 begin
1269 Get_Name_String (Chars (Id));
1270
1271 -- Add a leading '('
1272
1273 Add_Char_To_Name_Buffer ('(');
1274
1275 -- Generate:
1276 -- new String'("<Entry name>(" & Lnn'Img & ")");
1277
1278 -- This is an implicit heap allocation, and Comes_From_Source is
1279 -- False, which ensures that it will get flagged as a violation of
1280 -- No_Implicit_Heap_Allocations when that restriction applies.
1281
1282 Val :=
1283 Make_Allocator (Loc,
1284 Make_Qualified_Expression (Loc,
1285 Subtype_Mark =>
1286 New_Reference_To (Standard_String, Loc),
1287 Expression =>
1288 Make_Op_Concat (Loc,
1289 Left_Opnd =>
1290 Make_Op_Concat (Loc,
1291 Left_Opnd =>
1292 Make_String_Literal (Loc,
1293 Strval => String_From_Name_Buffer),
1294 Right_Opnd =>
1295 Make_Attribute_Reference (Loc,
1296 Prefix =>
1297 New_Reference_To (L_Id, Loc),
1298 Attribute_Name => Name_Img)),
1299 Right_Opnd =>
1300 Make_String_Literal (Loc,
1301 Strval => ")"))));
1302
1303 Increment_Index (L_Stmts);
1304 Append_To (L_Stmts, Build_Set_Entry_Name_Call (Val));
1305
1306 -- Generate:
1307 -- for Lnn in Family_Low .. Family_High loop
1308 -- Inn := Inn + 1;
1309 -- Set_Entry_Name
1310 -- (_init._object <or> _init._task_id, Inn, <Val>);
1311 -- end loop;
1312
1313 Append_To (B_Stmts,
1314 Make_Loop_Statement (Loc,
1315 Iteration_Scheme =>
1316 Make_Iteration_Scheme (Loc,
1317 Loop_Parameter_Specification =>
1318 Make_Loop_Parameter_Specification (Loc,
1319 Defining_Identifier => L_Id,
1320 Discrete_Subtype_Definition => Build_Range (Def))),
1321 Statements => L_Stmts,
1322 End_Label => Empty));
1323 end Build_Entry_Family_Name;
1324
1325 ----------------------
1326 -- Build_Entry_Name --
1327 ----------------------
1328
1329 procedure Build_Entry_Name (Id : Entity_Id) is
1330 Val : Node_Id;
1331
1332 begin
1333 Get_Name_String (Chars (Id));
1334
1335 -- This is an implicit heap allocation, and Comes_From_Source is
1336 -- False, which ensures that it will get flagged as a violation of
1337 -- No_Implicit_Heap_Allocations when that restriction applies.
1338
1339 Val :=
1340 Make_Allocator (Loc,
1341 Make_Qualified_Expression (Loc,
1342 Subtype_Mark =>
1343 New_Reference_To (Standard_String, Loc),
1344 Expression =>
1345 Make_String_Literal (Loc,
1346 String_From_Name_Buffer)));
1347
1348 Increment_Index (B_Stmts);
1349 Append_To (B_Stmts, Build_Set_Entry_Name_Call (Val));
1350 end Build_Entry_Name;
1351
1352 -------------------------------
1353 -- Build_Set_Entry_Name_Call --
1354 -------------------------------
1355
1356 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id is
1357 Arg1 : Name_Id;
1358 Proc : RE_Id;
1359
1360 begin
1361 -- Determine the proper name for the first argument and the RTS
1362 -- routine to call.
1363
1364 if Is_Protected_Type (Typ) then
1365 Arg1 := Name_uObject;
1366 Proc := RO_PE_Set_Entry_Name;
1367
1368 else pragma Assert (Is_Task_Type (Typ));
1369 Arg1 := Name_uTask_Id;
1370 Proc := RO_TS_Set_Entry_Name;
1371 end if;
1372
1373 -- Generate:
1374 -- Set_Entry_Name (_init.Arg1, Inn, Arg3);
1375
1376 return
1377 Make_Procedure_Call_Statement (Loc,
1378 Name =>
1379 New_Reference_To (RTE (Proc), Loc),
1380 Parameter_Associations => New_List (
1381 Make_Selected_Component (Loc, -- _init._object
1382 Prefix => -- _init._task_id
1383 Make_Identifier (Loc, Name_uInit),
1384 Selector_Name =>
1385 Make_Identifier (Loc, Arg1)),
1386 New_Reference_To (Index, Loc), -- Inn
1387 Arg3)); -- Val
1388 end Build_Set_Entry_Name_Call;
1389
1390 ---------------------
1391 -- Increment_Index --
1392 ---------------------
1393
1394 procedure Increment_Index (Stmts : List_Id) is
1395 begin
1396 -- Generate:
1397 -- Inn := Inn + 1;
1398
1399 Append_To (Stmts,
1400 Make_Assignment_Statement (Loc,
1401 Name =>
1402 New_Reference_To (Index, Loc),
1403 Expression =>
1404 Make_Op_Add (Loc,
1405 Left_Opnd =>
1406 New_Reference_To (Index, Loc),
1407 Right_Opnd =>
1408 Make_Integer_Literal (Loc, 1))));
1409 end Increment_Index;
1410
1411 -- Start of processing for Build_Entry_Names
1412
1413 begin
1414 -- Retrieve the original concurrent type
1415
1416 if Is_Concurrent_Record_Type (Typ) then
1417 Typ := Corresponding_Concurrent_Type (Typ);
1418 end if;
1419
1420 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
1421
1422 -- Nothing to do if the type has no entries
1423
1424 if not Has_Entries (Typ) then
1425 return Empty;
1426 end if;
1427
1428 -- Avoid generating entry names for a protected type with only one entry
1429
1430 if Is_Protected_Type (Typ)
1431 and then Find_Protection_Type (Typ) /= RTE (RE_Protection_Entries)
1432 then
1433 return Empty;
1434 end if;
1435
1436 Index := Make_Temporary (Loc, 'I');
1437
1438 -- Step 1: Generate the declaration of the index variable:
1439 -- Inn : Protected_Entry_Index := 0;
1440 -- or
1441 -- Inn : Task_Entry_Index := 0;
1442
1443 if Is_Protected_Type (Typ) then
1444 Index_Typ := RE_Protected_Entry_Index;
1445 else
1446 Index_Typ := RE_Task_Entry_Index;
1447 end if;
1448
1449 B_Decls := New_List;
1450 Append_To (B_Decls,
1451 Make_Object_Declaration (Loc,
1452 Defining_Identifier => Index,
1453 Object_Definition => New_Reference_To (RTE (Index_Typ), Loc),
1454 Expression => Make_Integer_Literal (Loc, 0)));
1455
1456 B_Stmts := New_List;
1457
1458 -- Step 2: Generate a call to Set_Entry_Name for each entry and entry
1459 -- family member.
1460
1461 Comp := First_Entity (Typ);
1462 while Present (Comp) loop
1463 if Ekind (Comp) = E_Entry then
1464 Build_Entry_Name (Comp);
1465
1466 elsif Ekind (Comp) = E_Entry_Family then
1467 Build_Entry_Family_Name (Comp);
1468 end if;
1469
1470 Next_Entity (Comp);
1471 end loop;
1472
1473 -- Step 3: Wrap the statements in a block
1474
1475 return
1476 Make_Block_Statement (Loc,
1477 Declarations => B_Decls,
1478 Handled_Statement_Sequence =>
1479 Make_Handled_Sequence_Of_Statements (Loc,
1480 Statements => B_Stmts));
1481 end Build_Entry_Names;
1482
1483 ---------------------------
1484 -- Build_Parameter_Block --
1485 ---------------------------
1486
1487 function Build_Parameter_Block
1488 (Loc : Source_Ptr;
1489 Actuals : List_Id;
1490 Formals : List_Id;
1491 Decls : List_Id) return Entity_Id
1492 is
1493 Actual : Entity_Id;
1494 Comp_Nam : Node_Id;
1495 Comps : List_Id;
1496 Formal : Entity_Id;
1497 Has_Comp : Boolean := False;
1498 Rec_Nam : Node_Id;
1499
1500 begin
1501 Actual := First (Actuals);
1502 Comps := New_List;
1503 Formal := Defining_Identifier (First (Formals));
1504
1505 while Present (Actual) loop
1506 if not Is_Controlling_Actual (Actual) then
1507
1508 -- Generate:
1509 -- type Ann is access all <actual-type>
1510
1511 Comp_Nam := Make_Temporary (Loc, 'A');
1512
1513 Append_To (Decls,
1514 Make_Full_Type_Declaration (Loc,
1515 Defining_Identifier => Comp_Nam,
1516 Type_Definition =>
1517 Make_Access_To_Object_Definition (Loc,
1518 All_Present => True,
1519 Constant_Present => Ekind (Formal) = E_In_Parameter,
1520 Subtype_Indication =>
1521 New_Reference_To (Etype (Actual), Loc))));
1522
1523 -- Generate:
1524 -- Param : Ann;
1525
1526 Append_To (Comps,
1527 Make_Component_Declaration (Loc,
1528 Defining_Identifier =>
1529 Make_Defining_Identifier (Loc, Chars (Formal)),
1530 Component_Definition =>
1531 Make_Component_Definition (Loc,
1532 Aliased_Present =>
1533 False,
1534 Subtype_Indication =>
1535 New_Reference_To (Comp_Nam, Loc))));
1536
1537 Has_Comp := True;
1538 end if;
1539
1540 Next_Actual (Actual);
1541 Next_Formal_With_Extras (Formal);
1542 end loop;
1543
1544 Rec_Nam := Make_Temporary (Loc, 'P');
1545
1546 if Has_Comp then
1547
1548 -- Generate:
1549 -- type Pnn is record
1550 -- Param1 : Ann1;
1551 -- ...
1552 -- ParamN : AnnN;
1553
1554 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1555 -- the original parameter names and Ann1 .. AnnN are the access to
1556 -- actual types.
1557
1558 Append_To (Decls,
1559 Make_Full_Type_Declaration (Loc,
1560 Defining_Identifier =>
1561 Rec_Nam,
1562 Type_Definition =>
1563 Make_Record_Definition (Loc,
1564 Component_List =>
1565 Make_Component_List (Loc, Comps))));
1566 else
1567 -- Generate:
1568 -- type Pnn is null record;
1569
1570 Append_To (Decls,
1571 Make_Full_Type_Declaration (Loc,
1572 Defining_Identifier =>
1573 Rec_Nam,
1574 Type_Definition =>
1575 Make_Record_Definition (Loc,
1576 Null_Present => True,
1577 Component_List => Empty)));
1578 end if;
1579
1580 return Rec_Nam;
1581 end Build_Parameter_Block;
1582
1583 --------------------------------------
1584 -- Build_Renamed_Formal_Declaration --
1585 --------------------------------------
1586
1587 function Build_Renamed_Formal_Declaration
1588 (New_F : Entity_Id;
1589 Formal : Entity_Id;
1590 Comp : Entity_Id;
1591 Renamed_Formal : Node_Id) return Node_Id
1592 is
1593 Loc : constant Source_Ptr := Sloc (New_F);
1594 Decl : Node_Id;
1595
1596 begin
1597 -- If the formal is a tagged incomplete type, it is already passed
1598 -- by reference, so it is sufficient to rename the pointer component
1599 -- that corresponds to the actual. Otherwise we need to dereference
1600 -- the pointer component to obtain the actual.
1601
1602 if Is_Incomplete_Type (Etype (Formal))
1603 and then Is_Tagged_Type (Etype (Formal))
1604 then
1605 Decl :=
1606 Make_Object_Renaming_Declaration (Loc,
1607 Defining_Identifier => New_F,
1608 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1609 Name => Renamed_Formal);
1610
1611 else
1612 Decl :=
1613 Make_Object_Renaming_Declaration (Loc,
1614 Defining_Identifier => New_F,
1615 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1616 Name =>
1617 Make_Explicit_Dereference (Loc, Renamed_Formal));
1618 end if;
1619
1620 return Decl;
1621 end Build_Renamed_Formal_Declaration;
1622
1623 -----------------------
1624 -- Build_PPC_Wrapper --
1625 -----------------------
1626
1627 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1628 Loc : constant Source_Ptr := Sloc (E);
1629 Synch_Type : constant Entity_Id := Scope (E);
1630
1631 Wrapper_Id : constant Entity_Id :=
1632 Make_Defining_Identifier (Loc,
1633 Chars => New_External_Name (Chars (E), 'E'));
1634 -- the wrapper procedure name
1635
1636 Wrapper_Body : Node_Id;
1637
1638 Synch_Id : constant Entity_Id :=
1639 Make_Defining_Identifier (Loc,
1640 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1641 -- The parameter that designates the synchronized object in the call
1642
1643 Actuals : constant List_Id := New_List;
1644 -- the actuals in the entry call.
1645
1646 Decls : constant List_Id := New_List;
1647
1648 Entry_Call : Node_Id;
1649 Entry_Name : Node_Id;
1650
1651 Specs : List_Id;
1652 -- The specification of the wrapper procedure
1653
1654 begin
1655
1656 -- Only build the wrapper if entry has pre/postconditions.
1657 -- Should this be done unconditionally instead ???
1658
1659 declare
1660 P : Node_Id;
1661
1662 begin
1663 P := Spec_PPC_List (Contract (E));
1664 if No (P) then
1665 return;
1666 end if;
1667
1668 -- Transfer ppc pragmas to the declarations of the wrapper
1669
1670 while Present (P) loop
1671 if Pragma_Name (P) = Name_Precondition
1672 or else Pragma_Name (P) = Name_Postcondition
1673 then
1674 Append (Relocate_Node (P), Decls);
1675 Set_Analyzed (Last (Decls), False);
1676 end if;
1677
1678 P := Next_Pragma (P);
1679 end loop;
1680 end;
1681
1682 -- First formal is synchronized object
1683
1684 Specs := New_List (
1685 Make_Parameter_Specification (Loc,
1686 Defining_Identifier => Synch_Id,
1687 Out_Present => True,
1688 In_Present => True,
1689 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1690
1691 Entry_Name :=
1692 Make_Selected_Component (Loc,
1693 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1694 Selector_Name => New_Occurrence_Of (E, Loc));
1695
1696 -- If entity is entry family, second formal is the corresponding index,
1697 -- and entry name is an indexed component.
1698
1699 if Ekind (E) = E_Entry_Family then
1700 declare
1701 Index : constant Entity_Id :=
1702 Make_Defining_Identifier (Loc, Name_I);
1703 begin
1704 Append_To (Specs,
1705 Make_Parameter_Specification (Loc,
1706 Defining_Identifier => Index,
1707 Parameter_Type =>
1708 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1709
1710 Entry_Name :=
1711 Make_Indexed_Component (Loc,
1712 Prefix => Entry_Name,
1713 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
1714 end;
1715 end if;
1716
1717 Entry_Call :=
1718 Make_Procedure_Call_Statement (Loc,
1719 Name => Entry_Name,
1720 Parameter_Associations => Actuals);
1721
1722 -- Now add formals that match those of the entry, and build actuals for
1723 -- the nested entry call.
1724
1725 declare
1726 Form : Entity_Id;
1727 New_Form : Entity_Id;
1728 Parm_Spec : Node_Id;
1729
1730 begin
1731 Form := First_Formal (E);
1732 while Present (Form) loop
1733 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
1734 Parm_Spec :=
1735 Make_Parameter_Specification (Loc,
1736 Defining_Identifier => New_Form,
1737 Out_Present => Out_Present (Parent (Form)),
1738 In_Present => In_Present (Parent (Form)),
1739 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
1740
1741 Append (Parm_Spec, Specs);
1742 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
1743 Next_Formal (Form);
1744 end loop;
1745 end;
1746
1747 -- Add renaming declarations for the discriminants of the enclosing
1748 -- type, which may be visible in the preconditions.
1749
1750 if Has_Discriminants (Synch_Type) then
1751 declare
1752 D : Entity_Id;
1753 Decl : Node_Id;
1754
1755 begin
1756 D := First_Discriminant (Synch_Type);
1757 while Present (D) loop
1758 Decl :=
1759 Make_Object_Renaming_Declaration (Loc,
1760 Defining_Identifier =>
1761 Make_Defining_Identifier (Loc, Chars (D)),
1762 Subtype_Mark => New_Reference_To (Etype (D), Loc),
1763 Name =>
1764 Make_Selected_Component (Loc,
1765 Prefix => New_Reference_To (Synch_Id, Loc),
1766 Selector_Name => Make_Identifier (Loc, Chars (D))));
1767 Prepend (Decl, Decls);
1768 Next_Discriminant (D);
1769 end loop;
1770 end;
1771 end if;
1772
1773 Set_PPC_Wrapper (E, Wrapper_Id);
1774 Wrapper_Body :=
1775 Make_Subprogram_Body (Loc,
1776 Specification =>
1777 Make_Procedure_Specification (Loc,
1778 Defining_Unit_Name => Wrapper_Id,
1779 Parameter_Specifications => Specs),
1780 Declarations => Decls,
1781 Handled_Statement_Sequence =>
1782 Make_Handled_Sequence_Of_Statements (Loc,
1783 Statements => New_List (Entry_Call)));
1784
1785 -- The wrapper body is analyzed when the enclosing type is frozen
1786
1787 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
1788 end Build_PPC_Wrapper;
1789
1790 --------------------------
1791 -- Build_Wrapper_Bodies --
1792 --------------------------
1793
1794 procedure Build_Wrapper_Bodies
1795 (Loc : Source_Ptr;
1796 Typ : Entity_Id;
1797 N : Node_Id)
1798 is
1799 Rec_Typ : Entity_Id;
1800
1801 function Build_Wrapper_Body
1802 (Loc : Source_Ptr;
1803 Subp_Id : Entity_Id;
1804 Obj_Typ : Entity_Id;
1805 Formals : List_Id) return Node_Id;
1806 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1807 -- associated with a protected or task type. Subp_Id is the subprogram
1808 -- name which will be wrapped. Obj_Typ is the type of the new formal
1809 -- parameter which handles dispatching and object notation. Formals are
1810 -- the original formals of Subp_Id which will be explicitly replicated.
1811
1812 ------------------------
1813 -- Build_Wrapper_Body --
1814 ------------------------
1815
1816 function Build_Wrapper_Body
1817 (Loc : Source_Ptr;
1818 Subp_Id : Entity_Id;
1819 Obj_Typ : Entity_Id;
1820 Formals : List_Id) return Node_Id
1821 is
1822 Body_Spec : Node_Id;
1823
1824 begin
1825 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1826
1827 -- The subprogram is not overriding or is not a primitive declared
1828 -- between two views.
1829
1830 if No (Body_Spec) then
1831 return Empty;
1832 end if;
1833
1834 declare
1835 Actuals : List_Id := No_List;
1836 Conv_Id : Node_Id;
1837 First_Form : Node_Id;
1838 Formal : Node_Id;
1839 Nam : Node_Id;
1840
1841 begin
1842 -- Map formals to actuals. Use the list built for the wrapper
1843 -- spec, skipping the object notation parameter.
1844
1845 First_Form := First (Parameter_Specifications (Body_Spec));
1846
1847 Formal := First_Form;
1848 Next (Formal);
1849
1850 if Present (Formal) then
1851 Actuals := New_List;
1852 while Present (Formal) loop
1853 Append_To (Actuals,
1854 Make_Identifier (Loc,
1855 Chars => Chars (Defining_Identifier (Formal))));
1856 Next (Formal);
1857 end loop;
1858 end if;
1859
1860 -- Special processing for primitives declared between a private
1861 -- type and its completion: the wrapper needs a properly typed
1862 -- parameter if the wrapped operation has a controlling first
1863 -- parameter. Note that this might not be the case for a function
1864 -- with a controlling result.
1865
1866 if Is_Private_Primitive_Subprogram (Subp_Id) then
1867 if No (Actuals) then
1868 Actuals := New_List;
1869 end if;
1870
1871 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1872 Prepend_To (Actuals,
1873 Unchecked_Convert_To
1874 (Corresponding_Concurrent_Type (Obj_Typ),
1875 Make_Identifier (Loc, Name_uO)));
1876
1877 else
1878 Prepend_To (Actuals,
1879 Make_Identifier (Loc,
1880 Chars => Chars (Defining_Identifier (First_Form))));
1881 end if;
1882
1883 Nam := New_Reference_To (Subp_Id, Loc);
1884 else
1885 -- An access-to-variable object parameter requires an explicit
1886 -- dereference in the unchecked conversion. This case occurs
1887 -- when a protected entry wrapper must override an interface
1888 -- level procedure with interface access as first parameter.
1889
1890 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1891
1892 if Nkind (Parameter_Type (First_Form)) =
1893 N_Access_Definition
1894 then
1895 Conv_Id :=
1896 Make_Explicit_Dereference (Loc,
1897 Prefix => Make_Identifier (Loc, Name_uO));
1898 else
1899 Conv_Id := Make_Identifier (Loc, Name_uO);
1900 end if;
1901
1902 Nam :=
1903 Make_Selected_Component (Loc,
1904 Prefix =>
1905 Unchecked_Convert_To
1906 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1907 Selector_Name => New_Reference_To (Subp_Id, Loc));
1908 end if;
1909
1910 -- Create the subprogram body. For a function, the call to the
1911 -- actual subprogram has to be converted to the corresponding
1912 -- record if it is a controlling result.
1913
1914 if Ekind (Subp_Id) = E_Function then
1915 declare
1916 Res : Node_Id;
1917
1918 begin
1919 Res :=
1920 Make_Function_Call (Loc,
1921 Name => Nam,
1922 Parameter_Associations => Actuals);
1923
1924 if Has_Controlling_Result (Subp_Id) then
1925 Res :=
1926 Unchecked_Convert_To
1927 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1928 end if;
1929
1930 return
1931 Make_Subprogram_Body (Loc,
1932 Specification => Body_Spec,
1933 Declarations => Empty_List,
1934 Handled_Statement_Sequence =>
1935 Make_Handled_Sequence_Of_Statements (Loc,
1936 Statements => New_List (
1937 Make_Simple_Return_Statement (Loc, Res))));
1938 end;
1939
1940 else
1941 return
1942 Make_Subprogram_Body (Loc,
1943 Specification => Body_Spec,
1944 Declarations => Empty_List,
1945 Handled_Statement_Sequence =>
1946 Make_Handled_Sequence_Of_Statements (Loc,
1947 Statements => New_List (
1948 Make_Procedure_Call_Statement (Loc,
1949 Name => Nam,
1950 Parameter_Associations => Actuals))));
1951 end if;
1952 end;
1953 end Build_Wrapper_Body;
1954
1955 -- Start of processing for Build_Wrapper_Bodies
1956
1957 begin
1958 if Is_Concurrent_Type (Typ) then
1959 Rec_Typ := Corresponding_Record_Type (Typ);
1960 else
1961 Rec_Typ := Typ;
1962 end if;
1963
1964 -- Generate wrapper bodies for a concurrent type which implements an
1965 -- interface.
1966
1967 if Present (Interfaces (Rec_Typ)) then
1968 declare
1969 Insert_Nod : Node_Id;
1970 Prim : Entity_Id;
1971 Prim_Elmt : Elmt_Id;
1972 Prim_Decl : Node_Id;
1973 Subp : Entity_Id;
1974 Wrap_Body : Node_Id;
1975 Wrap_Id : Entity_Id;
1976
1977 begin
1978 Insert_Nod := N;
1979
1980 -- Examine all primitive operations of the corresponding record
1981 -- type, looking for wrapper specs. Generate bodies in order to
1982 -- complete them.
1983
1984 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
1985 while Present (Prim_Elmt) loop
1986 Prim := Node (Prim_Elmt);
1987
1988 if (Ekind (Prim) = E_Function
1989 or else Ekind (Prim) = E_Procedure)
1990 and then Is_Primitive_Wrapper (Prim)
1991 then
1992 Subp := Wrapped_Entity (Prim);
1993 Prim_Decl := Parent (Parent (Prim));
1994
1995 Wrap_Body :=
1996 Build_Wrapper_Body (Loc,
1997 Subp_Id => Subp,
1998 Obj_Typ => Rec_Typ,
1999 Formals => Parameter_Specifications (Parent (Subp)));
2000 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2001
2002 Set_Corresponding_Spec (Wrap_Body, Prim);
2003 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2004
2005 Insert_After (Insert_Nod, Wrap_Body);
2006 Insert_Nod := Wrap_Body;
2007
2008 Analyze (Wrap_Body);
2009 end if;
2010
2011 Next_Elmt (Prim_Elmt);
2012 end loop;
2013 end;
2014 end if;
2015 end Build_Wrapper_Bodies;
2016
2017 ------------------------
2018 -- Build_Wrapper_Spec --
2019 ------------------------
2020
2021 function Build_Wrapper_Spec
2022 (Subp_Id : Entity_Id;
2023 Obj_Typ : Entity_Id;
2024 Formals : List_Id) return Node_Id
2025 is
2026 Loc : constant Source_Ptr := Sloc (Subp_Id);
2027 First_Param : Node_Id;
2028 Iface : Entity_Id;
2029 Iface_Elmt : Elmt_Id;
2030 Iface_Op : Entity_Id;
2031 Iface_Op_Elmt : Elmt_Id;
2032
2033 function Overriding_Possible
2034 (Iface_Op : Entity_Id;
2035 Wrapper : Entity_Id) return Boolean;
2036 -- Determine whether a primitive operation can be overridden by Wrapper.
2037 -- Iface_Op is the candidate primitive operation of an interface type,
2038 -- Wrapper is the generated entry wrapper.
2039
2040 function Replicate_Formals
2041 (Loc : Source_Ptr;
2042 Formals : List_Id) return List_Id;
2043 -- An explicit parameter replication is required due to the Is_Entry_
2044 -- Formal flag being set for all the formals of an entry. The explicit
2045 -- replication removes the flag that would otherwise cause a different
2046 -- path of analysis.
2047
2048 -------------------------
2049 -- Overriding_Possible --
2050 -------------------------
2051
2052 function Overriding_Possible
2053 (Iface_Op : Entity_Id;
2054 Wrapper : Entity_Id) return Boolean
2055 is
2056 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2057 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2058
2059 function Type_Conformant_Parameters
2060 (Iface_Op_Params : List_Id;
2061 Wrapper_Params : List_Id) return Boolean;
2062 -- Determine whether the parameters of the generated entry wrapper
2063 -- and those of a primitive operation are type conformant. During
2064 -- this check, the first parameter of the primitive operation is
2065 -- skipped if it is a controlling argument: protected functions
2066 -- may have a controlling result.
2067
2068 --------------------------------
2069 -- Type_Conformant_Parameters --
2070 --------------------------------
2071
2072 function Type_Conformant_Parameters
2073 (Iface_Op_Params : List_Id;
2074 Wrapper_Params : List_Id) return Boolean
2075 is
2076 Iface_Op_Param : Node_Id;
2077 Iface_Op_Typ : Entity_Id;
2078 Wrapper_Param : Node_Id;
2079 Wrapper_Typ : Entity_Id;
2080
2081 begin
2082 -- Skip the first (controlling) parameter of primitive operation
2083
2084 Iface_Op_Param := First (Iface_Op_Params);
2085
2086 if Present (First_Formal (Iface_Op))
2087 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2088 then
2089 Iface_Op_Param := Next (Iface_Op_Param);
2090 end if;
2091
2092 Wrapper_Param := First (Wrapper_Params);
2093 while Present (Iface_Op_Param)
2094 and then Present (Wrapper_Param)
2095 loop
2096 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2097 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2098
2099 -- The two parameters must be mode conformant
2100
2101 if not Conforming_Types
2102 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2103 then
2104 return False;
2105 end if;
2106
2107 Next (Iface_Op_Param);
2108 Next (Wrapper_Param);
2109 end loop;
2110
2111 -- One of the lists is longer than the other
2112
2113 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2114 return False;
2115 end if;
2116
2117 return True;
2118 end Type_Conformant_Parameters;
2119
2120 -- Start of processing for Overriding_Possible
2121
2122 begin
2123 if Chars (Iface_Op) /= Chars (Wrapper) then
2124 return False;
2125 end if;
2126
2127 -- If an inherited subprogram is implemented by a protected procedure
2128 -- or an entry, then the first parameter of the inherited subprogram
2129 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2130
2131 if Ekind (Iface_Op) = E_Procedure
2132 and then Present (Parameter_Specifications (Iface_Op_Spec))
2133 then
2134 declare
2135 Obj_Param : constant Node_Id :=
2136 First (Parameter_Specifications (Iface_Op_Spec));
2137 begin
2138 if not Out_Present (Obj_Param)
2139 and then Nkind (Parameter_Type (Obj_Param)) /=
2140 N_Access_Definition
2141 then
2142 return False;
2143 end if;
2144 end;
2145 end if;
2146
2147 return
2148 Type_Conformant_Parameters (
2149 Parameter_Specifications (Iface_Op_Spec),
2150 Parameter_Specifications (Wrapper_Spec));
2151 end Overriding_Possible;
2152
2153 -----------------------
2154 -- Replicate_Formals --
2155 -----------------------
2156
2157 function Replicate_Formals
2158 (Loc : Source_Ptr;
2159 Formals : List_Id) return List_Id
2160 is
2161 New_Formals : constant List_Id := New_List;
2162 Formal : Node_Id;
2163 Param_Type : Node_Id;
2164
2165 begin
2166 Formal := First (Formals);
2167
2168 -- Skip the object parameter when dealing with primitives declared
2169 -- between two views.
2170
2171 if Is_Private_Primitive_Subprogram (Subp_Id)
2172 and then not Has_Controlling_Result (Subp_Id)
2173 then
2174 Formal := Next (Formal);
2175 end if;
2176
2177 while Present (Formal) loop
2178
2179 -- Create an explicit copy of the entry parameter
2180
2181 -- When creating the wrapper subprogram for a primitive operation
2182 -- of a protected interface we must construct an equivalent
2183 -- signature to that of the overriding operation. For regular
2184 -- parameters we can just use the type of the formal, but for
2185 -- access to subprogram parameters we need to reanalyze the
2186 -- parameter type to create local entities for the signature of
2187 -- the subprogram type. Using the entities of the overriding
2188 -- subprogram will result in out-of-scope errors in the back-end.
2189
2190 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2191 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2192 else
2193 Param_Type :=
2194 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2195 end if;
2196
2197 Append_To (New_Formals,
2198 Make_Parameter_Specification (Loc,
2199 Defining_Identifier =>
2200 Make_Defining_Identifier (Loc,
2201 Chars => Chars (Defining_Identifier (Formal))),
2202 In_Present => In_Present (Formal),
2203 Out_Present => Out_Present (Formal),
2204 Parameter_Type => Param_Type));
2205
2206 Next (Formal);
2207 end loop;
2208
2209 return New_Formals;
2210 end Replicate_Formals;
2211
2212 -- Start of processing for Build_Wrapper_Spec
2213
2214 begin
2215 -- There is no point in building wrappers for non-tagged concurrent
2216 -- types.
2217
2218 pragma Assert (Is_Tagged_Type (Obj_Typ));
2219
2220 -- An entry or a protected procedure can override a routine where the
2221 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2222 -- type. Since the wrapper must have the exact same signature as that of
2223 -- the overridden subprogram, we try to find the overriding candidate
2224 -- and use its controlling formal.
2225
2226 First_Param := Empty;
2227
2228 -- Check every implemented interface
2229
2230 if Present (Interfaces (Obj_Typ)) then
2231 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2232 Search : while Present (Iface_Elmt) loop
2233 Iface := Node (Iface_Elmt);
2234
2235 -- Check every interface primitive
2236
2237 if Present (Primitive_Operations (Iface)) then
2238 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2239 while Present (Iface_Op_Elmt) loop
2240 Iface_Op := Node (Iface_Op_Elmt);
2241
2242 -- Ignore predefined primitives
2243
2244 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2245 Iface_Op := Ultimate_Alias (Iface_Op);
2246
2247 -- The current primitive operation can be overridden by
2248 -- the generated entry wrapper.
2249
2250 if Overriding_Possible (Iface_Op, Subp_Id) then
2251 First_Param :=
2252 First (Parameter_Specifications (Parent (Iface_Op)));
2253
2254 exit Search;
2255 end if;
2256 end if;
2257
2258 Next_Elmt (Iface_Op_Elmt);
2259 end loop;
2260 end if;
2261
2262 Next_Elmt (Iface_Elmt);
2263 end loop Search;
2264 end if;
2265
2266 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2267 -- this subprogram and this is not a primitive declared between two
2268 -- views then force the generation of a wrapper. As an optimization,
2269 -- previous versions of the frontend avoid generating the wrapper;
2270 -- however, the wrapper facilitates locating and reporting an error
2271 -- when a duplicate declaration is found later. See example in
2272 -- AI05-0090-1.
2273
2274 if No (First_Param)
2275 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2276 then
2277 if Is_Task_Type
2278 (Corresponding_Concurrent_Type (Obj_Typ))
2279 then
2280 First_Param :=
2281 Make_Parameter_Specification (Loc,
2282 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2283 In_Present => True,
2284 Out_Present => False,
2285 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2286
2287 -- For entries and procedures of protected types the mode of
2288 -- the controlling argument must be in-out.
2289
2290 else
2291 First_Param :=
2292 Make_Parameter_Specification (Loc,
2293 Defining_Identifier =>
2294 Make_Defining_Identifier (Loc,
2295 Chars => Name_uO),
2296 In_Present => True,
2297 Out_Present => (Ekind (Subp_Id) /= E_Function),
2298 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2299 end if;
2300 end if;
2301
2302 declare
2303 Wrapper_Id : constant Entity_Id :=
2304 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2305 New_Formals : List_Id;
2306 Obj_Param : Node_Id;
2307 Obj_Param_Typ : Entity_Id;
2308
2309 begin
2310 -- Minimum decoration is needed to catch the entity in
2311 -- Sem_Ch6.Override_Dispatching_Operation.
2312
2313 if Ekind (Subp_Id) = E_Function then
2314 Set_Ekind (Wrapper_Id, E_Function);
2315 else
2316 Set_Ekind (Wrapper_Id, E_Procedure);
2317 end if;
2318
2319 Set_Is_Primitive_Wrapper (Wrapper_Id);
2320 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2321 Set_Is_Private_Primitive (Wrapper_Id,
2322 Is_Private_Primitive_Subprogram (Subp_Id));
2323
2324 -- Process the formals
2325
2326 New_Formals := Replicate_Formals (Loc, Formals);
2327
2328 -- A function with a controlling result and no first controlling
2329 -- formal needs no additional parameter.
2330
2331 if Has_Controlling_Result (Subp_Id)
2332 and then
2333 (No (First_Formal (Subp_Id))
2334 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2335 then
2336 null;
2337
2338 -- Routine Subp_Id has been found to override an interface primitive.
2339 -- If the interface operation has an access parameter, create a copy
2340 -- of it, with the same null exclusion indicator if present.
2341
2342 elsif Present (First_Param) then
2343 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2344 Obj_Param_Typ :=
2345 Make_Access_Definition (Loc,
2346 Subtype_Mark =>
2347 New_Reference_To (Obj_Typ, Loc));
2348 Set_Null_Exclusion_Present (Obj_Param_Typ,
2349 Null_Exclusion_Present (Parameter_Type (First_Param)));
2350
2351 else
2352 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2353 end if;
2354
2355 Obj_Param :=
2356 Make_Parameter_Specification (Loc,
2357 Defining_Identifier =>
2358 Make_Defining_Identifier (Loc,
2359 Chars => Name_uO),
2360 In_Present => In_Present (First_Param),
2361 Out_Present => Out_Present (First_Param),
2362 Parameter_Type => Obj_Param_Typ);
2363
2364 Prepend_To (New_Formals, Obj_Param);
2365
2366 -- If we are dealing with a primitive declared between two views,
2367 -- implemented by a synchronized operation, we need to create
2368 -- a default parameter. The mode of the parameter must match that
2369 -- of the primitive operation.
2370
2371 else
2372 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2373 Obj_Param :=
2374 Make_Parameter_Specification (Loc,
2375 Defining_Identifier =>
2376 Make_Defining_Identifier (Loc, Name_uO),
2377 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2378 Out_Present => Ekind (Subp_Id) /= E_Function,
2379 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2380 Prepend_To (New_Formals, Obj_Param);
2381 end if;
2382
2383 -- Build the final spec. If it is a function with a controlling
2384 -- result, it is a primitive operation of the corresponding
2385 -- record type, so mark the spec accordingly.
2386
2387 if Ekind (Subp_Id) = E_Function then
2388 declare
2389 Res_Def : Node_Id;
2390
2391 begin
2392 if Has_Controlling_Result (Subp_Id) then
2393 Res_Def :=
2394 New_Occurrence_Of
2395 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2396 else
2397 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2398 end if;
2399
2400 return
2401 Make_Function_Specification (Loc,
2402 Defining_Unit_Name => Wrapper_Id,
2403 Parameter_Specifications => New_Formals,
2404 Result_Definition => Res_Def);
2405 end;
2406 else
2407 return
2408 Make_Procedure_Specification (Loc,
2409 Defining_Unit_Name => Wrapper_Id,
2410 Parameter_Specifications => New_Formals);
2411 end if;
2412 end;
2413 end Build_Wrapper_Spec;
2414
2415 -------------------------
2416 -- Build_Wrapper_Specs --
2417 -------------------------
2418
2419 procedure Build_Wrapper_Specs
2420 (Loc : Source_Ptr;
2421 Typ : Entity_Id;
2422 N : in out Node_Id)
2423 is
2424 Def : Node_Id;
2425 Rec_Typ : Entity_Id;
2426 procedure Scan_Declarations (L : List_Id);
2427 -- Common processing for visible and private declarations
2428 -- of a protected type.
2429
2430 procedure Scan_Declarations (L : List_Id) is
2431 Decl : Node_Id;
2432 Wrap_Decl : Node_Id;
2433 Wrap_Spec : Node_Id;
2434
2435 begin
2436 if No (L) then
2437 return;
2438 end if;
2439
2440 Decl := First (L);
2441 while Present (Decl) loop
2442 Wrap_Spec := Empty;
2443
2444 if Nkind (Decl) = N_Entry_Declaration
2445 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2446 then
2447 Wrap_Spec :=
2448 Build_Wrapper_Spec
2449 (Subp_Id => Defining_Identifier (Decl),
2450 Obj_Typ => Rec_Typ,
2451 Formals => Parameter_Specifications (Decl));
2452
2453 elsif Nkind (Decl) = N_Subprogram_Declaration then
2454 Wrap_Spec :=
2455 Build_Wrapper_Spec
2456 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2457 Obj_Typ => Rec_Typ,
2458 Formals =>
2459 Parameter_Specifications (Specification (Decl)));
2460 end if;
2461
2462 if Present (Wrap_Spec) then
2463 Wrap_Decl :=
2464 Make_Subprogram_Declaration (Loc,
2465 Specification => Wrap_Spec);
2466
2467 Insert_After (N, Wrap_Decl);
2468 N := Wrap_Decl;
2469
2470 Analyze (Wrap_Decl);
2471 end if;
2472
2473 Next (Decl);
2474 end loop;
2475 end Scan_Declarations;
2476
2477 -- start of processing for Build_Wrapper_Specs
2478
2479 begin
2480 if Is_Protected_Type (Typ) then
2481 Def := Protected_Definition (Parent (Typ));
2482 else pragma Assert (Is_Task_Type (Typ));
2483 Def := Task_Definition (Parent (Typ));
2484 end if;
2485
2486 Rec_Typ := Corresponding_Record_Type (Typ);
2487
2488 -- Generate wrapper specs for a concurrent type which implements an
2489 -- interface. Operations in both the visible and private parts may
2490 -- implement progenitor operations.
2491
2492 if Present (Interfaces (Rec_Typ))
2493 and then Present (Def)
2494 then
2495 Scan_Declarations (Visible_Declarations (Def));
2496 Scan_Declarations (Private_Declarations (Def));
2497 end if;
2498 end Build_Wrapper_Specs;
2499
2500 ---------------------------
2501 -- Build_Find_Body_Index --
2502 ---------------------------
2503
2504 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2505 Loc : constant Source_Ptr := Sloc (Typ);
2506 Ent : Entity_Id;
2507 E_Typ : Entity_Id;
2508 Has_F : Boolean := False;
2509 Index : Nat;
2510 If_St : Node_Id := Empty;
2511 Lo : Node_Id;
2512 Hi : Node_Id;
2513 Decls : List_Id := New_List;
2514 Ret : Node_Id;
2515 Spec : Node_Id;
2516 Siz : Node_Id := Empty;
2517
2518 procedure Add_If_Clause (Expr : Node_Id);
2519 -- Add test for range of current entry
2520
2521 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2522 -- If a bound of an entry is given by a discriminant, retrieve the
2523 -- actual value of the discriminant from the enclosing object.
2524
2525 -------------------
2526 -- Add_If_Clause --
2527 -------------------
2528
2529 procedure Add_If_Clause (Expr : Node_Id) is
2530 Cond : Node_Id;
2531 Stats : constant List_Id :=
2532 New_List (
2533 Make_Simple_Return_Statement (Loc,
2534 Expression => Make_Integer_Literal (Loc, Index + 1)));
2535
2536 begin
2537 -- Index for current entry body
2538
2539 Index := Index + 1;
2540
2541 -- Compute total length of entry queues so far
2542
2543 if No (Siz) then
2544 Siz := Expr;
2545 else
2546 Siz :=
2547 Make_Op_Add (Loc,
2548 Left_Opnd => Siz,
2549 Right_Opnd => Expr);
2550 end if;
2551
2552 Cond :=
2553 Make_Op_Le (Loc,
2554 Left_Opnd => Make_Identifier (Loc, Name_uE),
2555 Right_Opnd => Siz);
2556
2557 -- Map entry queue indexes in the range of the current family
2558 -- into the current index, that designates the entry body.
2559
2560 if No (If_St) then
2561 If_St :=
2562 Make_Implicit_If_Statement (Typ,
2563 Condition => Cond,
2564 Then_Statements => Stats,
2565 Elsif_Parts => New_List);
2566
2567 Ret := If_St;
2568
2569 else
2570 Append (
2571 Make_Elsif_Part (Loc,
2572 Condition => Cond,
2573 Then_Statements => Stats),
2574 Elsif_Parts (If_St));
2575 end if;
2576 end Add_If_Clause;
2577
2578 ------------------------------
2579 -- Convert_Discriminant_Ref --
2580 ------------------------------
2581
2582 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2583 B : Node_Id;
2584
2585 begin
2586 if Is_Entity_Name (Bound)
2587 and then Ekind (Entity (Bound)) = E_Discriminant
2588 then
2589 B :=
2590 Make_Selected_Component (Loc,
2591 Prefix =>
2592 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2593 Make_Explicit_Dereference (Loc,
2594 Make_Identifier (Loc, Name_uObject))),
2595 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2596 Set_Etype (B, Etype (Entity (Bound)));
2597 else
2598 B := New_Copy_Tree (Bound);
2599 end if;
2600
2601 return B;
2602 end Convert_Discriminant_Ref;
2603
2604 -- Start of processing for Build_Find_Body_Index
2605
2606 begin
2607 Spec := Build_Find_Body_Index_Spec (Typ);
2608
2609 Ent := First_Entity (Typ);
2610 while Present (Ent) loop
2611 if Ekind (Ent) = E_Entry_Family then
2612 Has_F := True;
2613 exit;
2614 end if;
2615
2616 Next_Entity (Ent);
2617 end loop;
2618
2619 if not Has_F then
2620
2621 -- If the protected type has no entry families, there is a one-one
2622 -- correspondence between entry queue and entry body.
2623
2624 Ret :=
2625 Make_Simple_Return_Statement (Loc,
2626 Expression => Make_Identifier (Loc, Name_uE));
2627
2628 else
2629 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2630 -- the following:
2631 --
2632 -- if E <= l1 then return 1;
2633 -- elsif E <= l1 + l2 then return 2;
2634 -- ...
2635
2636 Index := 0;
2637 Siz := Empty;
2638 Ent := First_Entity (Typ);
2639
2640 Add_Object_Pointer (Loc, Typ, Decls);
2641
2642 while Present (Ent) loop
2643 if Ekind (Ent) = E_Entry then
2644 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2645
2646 elsif Ekind (Ent) = E_Entry_Family then
2647 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2648 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2649 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2650 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2651 end if;
2652
2653 Next_Entity (Ent);
2654 end loop;
2655
2656 if Index = 1 then
2657 Decls := New_List;
2658 Ret :=
2659 Make_Simple_Return_Statement (Loc,
2660 Expression => Make_Integer_Literal (Loc, 1));
2661
2662 elsif Nkind (Ret) = N_If_Statement then
2663
2664 -- Ranges are in increasing order, so last one doesn't need guard
2665
2666 declare
2667 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2668 begin
2669 Remove (Nod);
2670 Set_Else_Statements (Ret, Then_Statements (Nod));
2671 end;
2672 end if;
2673 end if;
2674
2675 return
2676 Make_Subprogram_Body (Loc,
2677 Specification => Spec,
2678 Declarations => Decls,
2679 Handled_Statement_Sequence =>
2680 Make_Handled_Sequence_Of_Statements (Loc,
2681 Statements => New_List (Ret)));
2682 end Build_Find_Body_Index;
2683
2684 --------------------------------
2685 -- Build_Find_Body_Index_Spec --
2686 --------------------------------
2687
2688 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2689 Loc : constant Source_Ptr := Sloc (Typ);
2690 Id : constant Entity_Id :=
2691 Make_Defining_Identifier (Loc,
2692 Chars => New_External_Name (Chars (Typ), 'F'));
2693 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2694 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2695
2696 begin
2697 return
2698 Make_Function_Specification (Loc,
2699 Defining_Unit_Name => Id,
2700 Parameter_Specifications => New_List (
2701 Make_Parameter_Specification (Loc,
2702 Defining_Identifier => Parm1,
2703 Parameter_Type =>
2704 New_Reference_To (RTE (RE_Address), Loc)),
2705
2706 Make_Parameter_Specification (Loc,
2707 Defining_Identifier => Parm2,
2708 Parameter_Type =>
2709 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
2710 Result_Definition => New_Occurrence_Of (
2711 RTE (RE_Protected_Entry_Index), Loc));
2712 end Build_Find_Body_Index_Spec;
2713
2714 -------------------------
2715 -- Build_Master_Entity --
2716 -------------------------
2717
2718 procedure Build_Master_Entity (E : Entity_Id) is
2719 Loc : constant Source_Ptr := Sloc (E);
2720 P : Node_Id;
2721 Decl : Node_Id;
2722 S : Entity_Id;
2723
2724 begin
2725 S := Find_Master_Scope (E);
2726
2727 -- Nothing to do if we already built a master entity for this scope
2728 -- or if there is no task hierarchy.
2729
2730 if Has_Master_Entity (S)
2731 or else Restriction_Active (No_Task_Hierarchy)
2732 then
2733 return;
2734 end if;
2735
2736 -- Otherwise first build the master entity
2737 -- _Master : constant Master_Id := Current_Master.all;
2738 -- and insert it just before the current declaration
2739
2740 Decl :=
2741 Make_Object_Declaration (Loc,
2742 Defining_Identifier =>
2743 Make_Defining_Identifier (Loc, Name_uMaster),
2744 Constant_Present => True,
2745 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
2746 Expression =>
2747 Make_Explicit_Dereference (Loc,
2748 New_Reference_To (RTE (RE_Current_Master), Loc)));
2749
2750 P := Parent (E);
2751 Insert_Before (P, Decl);
2752 Analyze (Decl);
2753
2754 Set_Has_Master_Entity (S);
2755
2756 -- Now mark the containing scope as a task master
2757
2758 while Nkind (P) /= N_Compilation_Unit loop
2759 P := Parent (P);
2760
2761 -- If we fall off the top, we are at the outer level, and the
2762 -- environment task is our effective master, so nothing to mark.
2763
2764 if Nkind_In
2765 (P, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2766 then
2767 Set_Is_Task_Master (P, True);
2768 return;
2769
2770 elsif Nkind (Parent (P)) = N_Subunit then
2771 P := Corresponding_Stub (Parent (P));
2772 end if;
2773 end loop;
2774 end Build_Master_Entity;
2775
2776 -----------------------------------------
2777 -- Build_Private_Protected_Declaration --
2778 -----------------------------------------
2779
2780 function Build_Private_Protected_Declaration
2781 (N : Node_Id) return Entity_Id
2782 is
2783 Loc : constant Source_Ptr := Sloc (N);
2784 Body_Id : constant Entity_Id := Defining_Entity (N);
2785 Decl : Node_Id;
2786 Plist : List_Id;
2787 Formal : Entity_Id;
2788 New_Spec : Node_Id;
2789 Spec_Id : Entity_Id;
2790
2791 begin
2792 Formal := First_Formal (Body_Id);
2793
2794 -- The protected operation always has at least one formal, namely the
2795 -- object itself, but it is only placed in the parameter list if
2796 -- expansion is enabled.
2797
2798 if Present (Formal) or else Expander_Active then
2799 Plist := Copy_Parameter_List (Body_Id);
2800 else
2801 Plist := No_List;
2802 end if;
2803
2804 if Nkind (Specification (N)) = N_Procedure_Specification then
2805 New_Spec :=
2806 Make_Procedure_Specification (Loc,
2807 Defining_Unit_Name =>
2808 Make_Defining_Identifier (Sloc (Body_Id),
2809 Chars => Chars (Body_Id)),
2810 Parameter_Specifications =>
2811 Plist);
2812 else
2813 New_Spec :=
2814 Make_Function_Specification (Loc,
2815 Defining_Unit_Name =>
2816 Make_Defining_Identifier (Sloc (Body_Id),
2817 Chars => Chars (Body_Id)),
2818 Parameter_Specifications => Plist,
2819 Result_Definition =>
2820 New_Occurrence_Of (Etype (Body_Id), Loc));
2821 end if;
2822
2823 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
2824 Insert_Before (N, Decl);
2825 Spec_Id := Defining_Unit_Name (New_Spec);
2826
2827 -- Indicate that the entity comes from source, to ensure that cross-
2828 -- reference information is properly generated. The body itself is
2829 -- rewritten during expansion, and the body entity will not appear in
2830 -- calls to the operation.
2831
2832 Set_Comes_From_Source (Spec_Id, True);
2833 Analyze (Decl);
2834 Set_Has_Completion (Spec_Id);
2835 Set_Convention (Spec_Id, Convention_Protected);
2836 return Spec_Id;
2837 end Build_Private_Protected_Declaration;
2838
2839 ---------------------------
2840 -- Build_Protected_Entry --
2841 ---------------------------
2842
2843 function Build_Protected_Entry
2844 (N : Node_Id;
2845 Ent : Entity_Id;
2846 Pid : Node_Id) return Node_Id
2847 is
2848 Loc : constant Source_Ptr := Sloc (N);
2849
2850 Decls : constant List_Id := Declarations (N);
2851 End_Lab : constant Node_Id :=
2852 End_Label (Handled_Statement_Sequence (N));
2853 End_Loc : constant Source_Ptr :=
2854 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
2855 -- Used for the generated call to Complete_Entry_Body
2856
2857 Han_Loc : Source_Ptr;
2858 -- Used for the exception handler, inserted at end of the body
2859
2860 Op_Decls : constant List_Id := New_List;
2861 Complete : Node_Id;
2862 Edef : Entity_Id;
2863 Espec : Node_Id;
2864 Ohandle : Node_Id;
2865 Op_Stats : List_Id;
2866
2867 begin
2868 -- Set the source location on the exception handler only when debugging
2869 -- the expanded code (see Make_Implicit_Exception_Handler).
2870
2871 if Debug_Generated_Code then
2872 Han_Loc := End_Loc;
2873
2874 -- Otherwise the inserted code should not be visible to the debugger
2875
2876 else
2877 Han_Loc := No_Location;
2878 end if;
2879
2880 Edef :=
2881 Make_Defining_Identifier (Loc,
2882 Chars => Chars (Protected_Body_Subprogram (Ent)));
2883 Espec :=
2884 Build_Protected_Entry_Specification (Loc, Edef, Empty);
2885
2886 -- Add the following declarations:
2887 -- type poVP is access poV;
2888 -- _object : poVP := poVP (_O);
2889 --
2890 -- where _O is the formal parameter associated with the concurrent
2891 -- object. These declarations are needed for Complete_Entry_Body.
2892
2893 Add_Object_Pointer (Loc, Pid, Op_Decls);
2894
2895 -- Add renamings for all formals, the Protection object, discriminals,
2896 -- privals and the entry index constant for use by debugger.
2897
2898 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
2899 Debug_Private_Data_Declarations (Decls);
2900
2901 case Corresponding_Runtime_Package (Pid) is
2902 when System_Tasking_Protected_Objects_Entries =>
2903 Complete :=
2904 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
2905
2906 when System_Tasking_Protected_Objects_Single_Entry =>
2907 Complete :=
2908 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
2909
2910 when others =>
2911 raise Program_Error;
2912 end case;
2913
2914 Op_Stats := New_List (
2915 Make_Block_Statement (Loc,
2916 Declarations => Decls,
2917 Handled_Statement_Sequence =>
2918 Handled_Statement_Sequence (N)),
2919
2920 Make_Procedure_Call_Statement (End_Loc,
2921 Name => Complete,
2922 Parameter_Associations => New_List (
2923 Make_Attribute_Reference (End_Loc,
2924 Prefix =>
2925 Make_Selected_Component (End_Loc,
2926 Prefix => Make_Identifier (End_Loc, Name_uObject),
2927 Selector_Name => Make_Identifier (End_Loc, Name_uObject)),
2928 Attribute_Name => Name_Unchecked_Access))));
2929
2930 -- When exceptions can not be propagated, we never need to call
2931 -- Exception_Complete_Entry_Body
2932
2933 if No_Exception_Handlers_Set then
2934 return
2935 Make_Subprogram_Body (Loc,
2936 Specification => Espec,
2937 Declarations => Op_Decls,
2938 Handled_Statement_Sequence =>
2939 Make_Handled_Sequence_Of_Statements (Loc,
2940 Statements => Op_Stats,
2941 End_Label => End_Lab));
2942
2943 else
2944 Ohandle := Make_Others_Choice (Loc);
2945 Set_All_Others (Ohandle);
2946
2947 case Corresponding_Runtime_Package (Pid) is
2948 when System_Tasking_Protected_Objects_Entries =>
2949 Complete :=
2950 New_Reference_To
2951 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
2952
2953 when System_Tasking_Protected_Objects_Single_Entry =>
2954 Complete :=
2955 New_Reference_To
2956 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
2957
2958 when others =>
2959 raise Program_Error;
2960 end case;
2961
2962 -- Establish link between subprogram body entity and source entry.
2963
2964 Set_Corresponding_Protected_Entry (Edef, Ent);
2965
2966 -- Create body of entry procedure. The renaming declarations are
2967 -- placed ahead of the block that contains the actual entry body.
2968
2969 return
2970 Make_Subprogram_Body (Loc,
2971 Specification => Espec,
2972 Declarations => Op_Decls,
2973 Handled_Statement_Sequence =>
2974 Make_Handled_Sequence_Of_Statements (Loc,
2975 Statements => Op_Stats,
2976 End_Label => End_Lab,
2977 Exception_Handlers => New_List (
2978 Make_Implicit_Exception_Handler (Han_Loc,
2979 Exception_Choices => New_List (Ohandle),
2980
2981 Statements => New_List (
2982 Make_Procedure_Call_Statement (Han_Loc,
2983 Name => Complete,
2984 Parameter_Associations => New_List (
2985 Make_Attribute_Reference (Han_Loc,
2986 Prefix =>
2987 Make_Selected_Component (Han_Loc,
2988 Prefix =>
2989 Make_Identifier (Han_Loc, Name_uObject),
2990 Selector_Name =>
2991 Make_Identifier (Han_Loc, Name_uObject)),
2992 Attribute_Name => Name_Unchecked_Access),
2993
2994 Make_Function_Call (Han_Loc,
2995 Name => New_Reference_To (
2996 RTE (RE_Get_GNAT_Exception), Loc)))))))));
2997 end if;
2998 end Build_Protected_Entry;
2999
3000 -----------------------------------------
3001 -- Build_Protected_Entry_Specification --
3002 -----------------------------------------
3003
3004 function Build_Protected_Entry_Specification
3005 (Loc : Source_Ptr;
3006 Def_Id : Entity_Id;
3007 Ent_Id : Entity_Id) return Node_Id
3008 is
3009 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3010
3011 begin
3012 Set_Debug_Info_Needed (Def_Id);
3013
3014 if Present (Ent_Id) then
3015 Append_Elmt (P, Accept_Address (Ent_Id));
3016 end if;
3017
3018 return
3019 Make_Procedure_Specification (Loc,
3020 Defining_Unit_Name => Def_Id,
3021 Parameter_Specifications => New_List (
3022 Make_Parameter_Specification (Loc,
3023 Defining_Identifier =>
3024 Make_Defining_Identifier (Loc, Name_uO),
3025 Parameter_Type =>
3026 New_Reference_To (RTE (RE_Address), Loc)),
3027
3028 Make_Parameter_Specification (Loc,
3029 Defining_Identifier => P,
3030 Parameter_Type =>
3031 New_Reference_To (RTE (RE_Address), Loc)),
3032
3033 Make_Parameter_Specification (Loc,
3034 Defining_Identifier =>
3035 Make_Defining_Identifier (Loc, Name_uE),
3036 Parameter_Type =>
3037 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
3038 end Build_Protected_Entry_Specification;
3039
3040 --------------------------
3041 -- Build_Protected_Spec --
3042 --------------------------
3043
3044 function Build_Protected_Spec
3045 (N : Node_Id;
3046 Obj_Type : Entity_Id;
3047 Ident : Entity_Id;
3048 Unprotected : Boolean := False) return List_Id
3049 is
3050 Loc : constant Source_Ptr := Sloc (N);
3051 Decl : Node_Id;
3052 Formal : Entity_Id;
3053 New_Plist : List_Id;
3054 New_Param : Node_Id;
3055
3056 begin
3057 New_Plist := New_List;
3058
3059 Formal := First_Formal (Ident);
3060 while Present (Formal) loop
3061 New_Param :=
3062 Make_Parameter_Specification (Loc,
3063 Defining_Identifier =>
3064 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3065 In_Present => In_Present (Parent (Formal)),
3066 Out_Present => Out_Present (Parent (Formal)),
3067 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
3068
3069 if Unprotected then
3070 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
3071 end if;
3072
3073 Append (New_Param, New_Plist);
3074 Next_Formal (Formal);
3075 end loop;
3076
3077 -- If the subprogram is a procedure and the context is not an access
3078 -- to protected subprogram, the parameter is in-out. Otherwise it is
3079 -- an in parameter.
3080
3081 Decl :=
3082 Make_Parameter_Specification (Loc,
3083 Defining_Identifier =>
3084 Make_Defining_Identifier (Loc, Name_uObject),
3085 In_Present => True,
3086 Out_Present =>
3087 (Etype (Ident) = Standard_Void_Type
3088 and then not Is_RTE (Obj_Type, RE_Address)),
3089 Parameter_Type =>
3090 New_Reference_To (Obj_Type, Loc));
3091 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3092 Prepend_To (New_Plist, Decl);
3093
3094 return New_Plist;
3095 end Build_Protected_Spec;
3096
3097 ---------------------------------------
3098 -- Build_Protected_Sub_Specification --
3099 ---------------------------------------
3100
3101 function Build_Protected_Sub_Specification
3102 (N : Node_Id;
3103 Prot_Typ : Entity_Id;
3104 Mode : Subprogram_Protection_Mode) return Node_Id
3105 is
3106 Loc : constant Source_Ptr := Sloc (N);
3107 Decl : Node_Id;
3108 Def_Id : Entity_Id;
3109 New_Id : Entity_Id;
3110 New_Plist : List_Id;
3111 New_Spec : Node_Id;
3112
3113 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3114 (Dispatching_Mode => ' ',
3115 Protected_Mode => 'P',
3116 Unprotected_Mode => 'N');
3117
3118 begin
3119 if Ekind (Defining_Unit_Name (Specification (N))) =
3120 E_Subprogram_Body
3121 then
3122 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3123 else
3124 Decl := N;
3125 end if;
3126
3127 Def_Id := Defining_Unit_Name (Specification (Decl));
3128
3129 New_Plist :=
3130 Build_Protected_Spec
3131 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3132 Mode = Unprotected_Mode);
3133 New_Id :=
3134 Make_Defining_Identifier (Loc,
3135 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3136
3137 -- The unprotected operation carries the user code, and debugging
3138 -- information must be generated for it, even though this spec does
3139 -- not come from source. It is also convenient to allow gdb to step
3140 -- into the protected operation, even though it only contains lock/
3141 -- unlock calls.
3142
3143 Set_Debug_Info_Needed (New_Id);
3144
3145 -- If a pragma Eliminate applies to the source entity, the internal
3146 -- subprograms will be eliminated as well.
3147
3148 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3149
3150 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3151 New_Spec :=
3152 Make_Procedure_Specification (Loc,
3153 Defining_Unit_Name => New_Id,
3154 Parameter_Specifications => New_Plist);
3155
3156 -- Create a new specification for the anonymous subprogram type
3157
3158 else
3159 New_Spec :=
3160 Make_Function_Specification (Loc,
3161 Defining_Unit_Name => New_Id,
3162 Parameter_Specifications => New_Plist,
3163 Result_Definition =>
3164 Copy_Result_Type (Result_Definition (Specification (Decl))));
3165
3166 Set_Return_Present (Defining_Unit_Name (New_Spec));
3167 end if;
3168
3169 return New_Spec;
3170 end Build_Protected_Sub_Specification;
3171
3172 -------------------------------------
3173 -- Build_Protected_Subprogram_Body --
3174 -------------------------------------
3175
3176 function Build_Protected_Subprogram_Body
3177 (N : Node_Id;
3178 Pid : Node_Id;
3179 N_Op_Spec : Node_Id) return Node_Id
3180 is
3181 Loc : constant Source_Ptr := Sloc (N);
3182 Op_Spec : Node_Id;
3183 P_Op_Spec : Node_Id;
3184 Uactuals : List_Id;
3185 Pformal : Node_Id;
3186 Unprot_Call : Node_Id;
3187 Sub_Body : Node_Id;
3188 Lock_Name : Node_Id;
3189 Lock_Stmt : Node_Id;
3190 Service_Name : Node_Id;
3191 R : Node_Id;
3192 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3193 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3194 Stmts : List_Id;
3195 Object_Parm : Node_Id;
3196 Exc_Safe : Boolean;
3197
3198 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
3199 -- Tell whether a given subprogram cannot raise an exception
3200
3201 -----------------------
3202 -- Is_Exception_Safe --
3203 -----------------------
3204
3205 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
3206
3207 function Has_Side_Effect (N : Node_Id) return Boolean;
3208 -- Return True whenever encountering a subprogram call or raise
3209 -- statement of any kind in the sequence of statements
3210
3211 ---------------------
3212 -- Has_Side_Effect --
3213 ---------------------
3214
3215 -- What is this doing buried two levels down in exp_ch9. It seems
3216 -- like a generally useful function, and indeed there may be code
3217 -- duplication going on here ???
3218
3219 function Has_Side_Effect (N : Node_Id) return Boolean is
3220 Stmt : Node_Id;
3221 Expr : Node_Id;
3222
3223 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
3224 -- Indicate whether N is a subprogram call or a raise statement
3225
3226 ----------------------
3227 -- Is_Call_Or_Raise --
3228 ----------------------
3229
3230 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
3231 begin
3232 return Nkind_In (N, N_Procedure_Call_Statement,
3233 N_Function_Call,
3234 N_Raise_Statement,
3235 N_Raise_Constraint_Error,
3236 N_Raise_Program_Error,
3237 N_Raise_Storage_Error);
3238 end Is_Call_Or_Raise;
3239
3240 -- Start of processing for Has_Side_Effect
3241
3242 begin
3243 Stmt := N;
3244 while Present (Stmt) loop
3245 if Is_Call_Or_Raise (Stmt) then
3246 return True;
3247 end if;
3248
3249 -- An object declaration can also contain a function call
3250 -- or a raise statement
3251
3252 if Nkind (Stmt) = N_Object_Declaration then
3253 Expr := Expression (Stmt);
3254
3255 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
3256 return True;
3257 end if;
3258 end if;
3259
3260 Next (Stmt);
3261 end loop;
3262
3263 return False;
3264 end Has_Side_Effect;
3265
3266 -- Start of processing for Is_Exception_Safe
3267
3268 begin
3269 -- If the checks handled by the back end are not disabled, we cannot
3270 -- ensure that no exception will be raised.
3271
3272 if not Access_Checks_Suppressed (Empty)
3273 or else not Discriminant_Checks_Suppressed (Empty)
3274 or else not Range_Checks_Suppressed (Empty)
3275 or else not Index_Checks_Suppressed (Empty)
3276 or else Opt.Stack_Checking_Enabled
3277 then
3278 return False;
3279 end if;
3280
3281 if Has_Side_Effect (First (Declarations (Subprogram)))
3282 or else
3283 Has_Side_Effect (
3284 First (Statements (Handled_Statement_Sequence (Subprogram))))
3285 then
3286 return False;
3287 else
3288 return True;
3289 end if;
3290 end Is_Exception_Safe;
3291
3292 -- Start of processing for Build_Protected_Subprogram_Body
3293
3294 begin
3295 Op_Spec := Specification (N);
3296 Exc_Safe := Is_Exception_Safe (N);
3297
3298 P_Op_Spec :=
3299 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3300
3301 -- Build a list of the formal parameters of the protected version of
3302 -- the subprogram to use as the actual parameters of the unprotected
3303 -- version.
3304
3305 Uactuals := New_List;
3306 Pformal := First (Parameter_Specifications (P_Op_Spec));
3307 while Present (Pformal) loop
3308 Append_To (Uactuals,
3309 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3310 Next (Pformal);
3311 end loop;
3312
3313 -- Make a call to the unprotected version of the subprogram built above
3314 -- for use by the protected version built below.
3315
3316 if Nkind (Op_Spec) = N_Function_Specification then
3317 if Exc_Safe then
3318 R := Make_Temporary (Loc, 'R');
3319 Unprot_Call :=
3320 Make_Object_Declaration (Loc,
3321 Defining_Identifier => R,
3322 Constant_Present => True,
3323 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
3324 Expression =>
3325 Make_Function_Call (Loc,
3326 Name => Make_Identifier (Loc,
3327 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3328 Parameter_Associations => Uactuals));
3329
3330 Return_Stmt :=
3331 Make_Simple_Return_Statement (Loc,
3332 Expression => New_Reference_To (R, Loc));
3333
3334 else
3335 Unprot_Call := Make_Simple_Return_Statement (Loc,
3336 Expression => Make_Function_Call (Loc,
3337 Name =>
3338 Make_Identifier (Loc,
3339 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3340 Parameter_Associations => Uactuals));
3341 end if;
3342
3343 else
3344 Unprot_Call :=
3345 Make_Procedure_Call_Statement (Loc,
3346 Name =>
3347 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3348 Parameter_Associations => Uactuals);
3349 end if;
3350
3351 -- Wrap call in block that will be covered by an at_end handler
3352
3353 if not Exc_Safe then
3354 Unprot_Call := Make_Block_Statement (Loc,
3355 Handled_Statement_Sequence =>
3356 Make_Handled_Sequence_Of_Statements (Loc,
3357 Statements => New_List (Unprot_Call)));
3358 end if;
3359
3360 -- Make the protected subprogram body. This locks the protected
3361 -- object and calls the unprotected version of the subprogram.
3362
3363 case Corresponding_Runtime_Package (Pid) is
3364 when System_Tasking_Protected_Objects_Entries =>
3365 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
3366 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
3367
3368 when System_Tasking_Protected_Objects_Single_Entry =>
3369 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
3370 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
3371
3372 when System_Tasking_Protected_Objects =>
3373 Lock_Name := New_Reference_To (RTE (RE_Lock), Loc);
3374 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
3375
3376 when others =>
3377 raise Program_Error;
3378 end case;
3379
3380 Object_Parm :=
3381 Make_Attribute_Reference (Loc,
3382 Prefix =>
3383 Make_Selected_Component (Loc,
3384 Prefix => Make_Identifier (Loc, Name_uObject),
3385 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3386 Attribute_Name => Name_Unchecked_Access);
3387
3388 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
3389 Name => Lock_Name,
3390 Parameter_Associations => New_List (Object_Parm));
3391
3392 if Abort_Allowed then
3393 Stmts := New_List (
3394 Make_Procedure_Call_Statement (Loc,
3395 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
3396 Parameter_Associations => Empty_List),
3397 Lock_Stmt);
3398
3399 else
3400 Stmts := New_List (Lock_Stmt);
3401 end if;
3402
3403 if not Exc_Safe then
3404 Append (Unprot_Call, Stmts);
3405 else
3406 if Nkind (Op_Spec) = N_Function_Specification then
3407 Pre_Stmts := Stmts;
3408 Stmts := Empty_List;
3409 else
3410 Append (Unprot_Call, Stmts);
3411 end if;
3412
3413 Append (
3414 Make_Procedure_Call_Statement (Loc,
3415 Name => Service_Name,
3416 Parameter_Associations =>
3417 New_List (New_Copy_Tree (Object_Parm))),
3418 Stmts);
3419
3420 if Abort_Allowed then
3421 Append (
3422 Make_Procedure_Call_Statement (Loc,
3423 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
3424 Parameter_Associations => Empty_List),
3425 Stmts);
3426 end if;
3427
3428 if Nkind (Op_Spec) = N_Function_Specification then
3429 Append (Return_Stmt, Stmts);
3430 Append (Make_Block_Statement (Loc,
3431 Declarations => New_List (Unprot_Call),
3432 Handled_Statement_Sequence =>
3433 Make_Handled_Sequence_Of_Statements (Loc,
3434 Statements => Stmts)), Pre_Stmts);
3435 Stmts := Pre_Stmts;
3436 end if;
3437 end if;
3438
3439 Sub_Body :=
3440 Make_Subprogram_Body (Loc,
3441 Declarations => Empty_List,
3442 Specification => P_Op_Spec,
3443 Handled_Statement_Sequence =>
3444 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3445
3446 if not Exc_Safe then
3447 Set_Is_Protected_Subprogram_Body (Sub_Body);
3448 end if;
3449
3450 return Sub_Body;
3451 end Build_Protected_Subprogram_Body;
3452
3453 -------------------------------------
3454 -- Build_Protected_Subprogram_Call --
3455 -------------------------------------
3456
3457 procedure Build_Protected_Subprogram_Call
3458 (N : Node_Id;
3459 Name : Node_Id;
3460 Rec : Node_Id;
3461 External : Boolean := True)
3462 is
3463 Loc : constant Source_Ptr := Sloc (N);
3464 Sub : constant Entity_Id := Entity (Name);
3465 New_Sub : Node_Id;
3466 Params : List_Id;
3467
3468 begin
3469 if External then
3470 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
3471 else
3472 New_Sub :=
3473 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
3474 end if;
3475
3476 if Present (Parameter_Associations (N)) then
3477 Params := New_Copy_List_Tree (Parameter_Associations (N));
3478 else
3479 Params := New_List;
3480 end if;
3481
3482 -- If the type is an untagged derived type, convert to the root type,
3483 -- which is the one on which the operations are defined.
3484
3485 if Nkind (Rec) = N_Unchecked_Type_Conversion
3486 and then not Is_Tagged_Type (Etype (Rec))
3487 and then Is_Derived_Type (Etype (Rec))
3488 then
3489 Set_Etype (Rec, Root_Type (Etype (Rec)));
3490 Set_Subtype_Mark (Rec,
3491 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
3492 end if;
3493
3494 Prepend (Rec, Params);
3495
3496 if Ekind (Sub) = E_Procedure then
3497 Rewrite (N,
3498 Make_Procedure_Call_Statement (Loc,
3499 Name => New_Sub,
3500 Parameter_Associations => Params));
3501
3502 else
3503 pragma Assert (Ekind (Sub) = E_Function);
3504 Rewrite (N,
3505 Make_Function_Call (Loc,
3506 Name => New_Sub,
3507 Parameter_Associations => Params));
3508 end if;
3509
3510 if External
3511 and then Nkind (Rec) = N_Unchecked_Type_Conversion
3512 and then Is_Entity_Name (Expression (Rec))
3513 and then Is_Shared_Passive (Entity (Expression (Rec)))
3514 then
3515 Add_Shared_Var_Lock_Procs (N);
3516 end if;
3517 end Build_Protected_Subprogram_Call;
3518
3519 -------------------------
3520 -- Build_Selected_Name --
3521 -------------------------
3522
3523 function Build_Selected_Name
3524 (Prefix : Entity_Id;
3525 Selector : Entity_Id;
3526 Append_Char : Character := ' ') return Name_Id
3527 is
3528 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
3529 Select_Len : Natural;
3530
3531 begin
3532 Get_Name_String (Chars (Selector));
3533 Select_Len := Name_Len;
3534 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
3535 Get_Name_String (Chars (Prefix));
3536
3537 -- If scope is anonymous type, discard suffix to recover name of
3538 -- single protected object. Otherwise use protected type name.
3539
3540 if Name_Buffer (Name_Len) = 'T' then
3541 Name_Len := Name_Len - 1;
3542 end if;
3543
3544 Add_Str_To_Name_Buffer ("__");
3545 for J in 1 .. Select_Len loop
3546 Add_Char_To_Name_Buffer (Select_Buffer (J));
3547 end loop;
3548
3549 -- Now add the Append_Char if specified. The encoding to follow
3550 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
3551 -- then the entity is associated to a protected type subprogram.
3552 -- Otherwise, it is a protected type entry. For each case, the
3553 -- encoding to follow for the suffix is documented in exp_dbug.ads.
3554
3555 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
3556
3557 if Append_Char /= ' ' then
3558 if Append_Char = 'P' or Append_Char = 'N' then
3559 Add_Char_To_Name_Buffer (Append_Char);
3560 return Name_Find;
3561 else
3562 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
3563 return New_External_Name (Name_Find, ' ', -1);
3564 end if;
3565 else
3566 return Name_Find;
3567 end if;
3568 end Build_Selected_Name;
3569
3570 -----------------------------
3571 -- Build_Simple_Entry_Call --
3572 -----------------------------
3573
3574 -- A task entry call is converted to a call to Call_Simple
3575
3576 -- declare
3577 -- P : parms := (parm, parm, parm);
3578 -- begin
3579 -- Call_Simple (acceptor-task, entry-index, P'Address);
3580 -- parm := P.param;
3581 -- parm := P.param;
3582 -- ...
3583 -- end;
3584
3585 -- Here Pnn is an aggregate of the type constructed for the entry to hold
3586 -- the parameters, and the constructed aggregate value contains either the
3587 -- parameters or, in the case of non-elementary types, references to these
3588 -- parameters. Then the address of this aggregate is passed to the runtime
3589 -- routine, along with the task id value and the task entry index value.
3590 -- Pnn is only required if parameters are present.
3591
3592 -- The assignments after the call are present only in the case of in-out
3593 -- or out parameters for elementary types, and are used to assign back the
3594 -- resulting values of such parameters.
3595
3596 -- Note: the reason that we insert a block here is that in the context
3597 -- of selects, conditional entry calls etc. the entry call statement
3598 -- appears on its own, not as an element of a list.
3599
3600 -- A protected entry call is converted to a Protected_Entry_Call:
3601
3602 -- declare
3603 -- P : E1_Params := (param, param, param);
3604 -- Pnn : Boolean;
3605 -- Bnn : Communications_Block;
3606
3607 -- declare
3608 -- P : E1_Params := (param, param, param);
3609 -- Bnn : Communications_Block;
3610
3611 -- begin
3612 -- Protected_Entry_Call (
3613 -- Object => po._object'Access,
3614 -- E => <entry index>;
3615 -- Uninterpreted_Data => P'Address;
3616 -- Mode => Simple_Call;
3617 -- Block => Bnn);
3618 -- parm := P.param;
3619 -- parm := P.param;
3620 -- ...
3621 -- end;
3622
3623 procedure Build_Simple_Entry_Call
3624 (N : Node_Id;
3625 Concval : Node_Id;
3626 Ename : Node_Id;
3627 Index : Node_Id)
3628 is
3629 begin
3630 Expand_Call (N);
3631
3632 -- If call has been inlined, nothing left to do
3633
3634 if Nkind (N) = N_Block_Statement then
3635 return;
3636 end if;
3637
3638 -- Convert entry call to Call_Simple call
3639
3640 declare
3641 Loc : constant Source_Ptr := Sloc (N);
3642 Parms : constant List_Id := Parameter_Associations (N);
3643 Stats : constant List_Id := New_List;
3644 Actual : Node_Id;
3645 Call : Node_Id;
3646 Comm_Name : Entity_Id;
3647 Conctyp : Node_Id;
3648 Decls : List_Id;
3649 Ent : Entity_Id;
3650 Ent_Acc : Entity_Id;
3651 Formal : Node_Id;
3652 Iface_Tag : Entity_Id;
3653 Iface_Typ : Entity_Id;
3654 N_Node : Node_Id;
3655 N_Var : Node_Id;
3656 P : Entity_Id;
3657 Parm1 : Node_Id;
3658 Parm2 : Node_Id;
3659 Parm3 : Node_Id;
3660 Pdecl : Node_Id;
3661 Plist : List_Id;
3662 X : Entity_Id;
3663 Xdecl : Node_Id;
3664
3665 begin
3666 -- Simple entry and entry family cases merge here
3667
3668 Ent := Entity (Ename);
3669 Ent_Acc := Entry_Parameters_Type (Ent);
3670 Conctyp := Etype (Concval);
3671
3672 -- If prefix is an access type, dereference to obtain the task type
3673
3674 if Is_Access_Type (Conctyp) then
3675 Conctyp := Designated_Type (Conctyp);
3676 end if;
3677
3678 -- Special case for protected subprogram calls
3679
3680 if Is_Protected_Type (Conctyp)
3681 and then Is_Subprogram (Entity (Ename))
3682 then
3683 if not Is_Eliminated (Entity (Ename)) then
3684 Build_Protected_Subprogram_Call
3685 (N, Ename, Convert_Concurrent (Concval, Conctyp));
3686 Analyze (N);
3687 end if;
3688
3689 return;
3690 end if;
3691
3692 -- First parameter is the Task_Id value from the task value or the
3693 -- Object from the protected object value, obtained by selecting
3694 -- the _Task_Id or _Object from the result of doing an unchecked
3695 -- conversion to convert the value to the corresponding record type.
3696
3697 if Nkind (Concval) = N_Function_Call
3698 and then Is_Task_Type (Conctyp)
3699 and then Ada_Version >= Ada_2005
3700 then
3701 declare
3702 ExpR : constant Node_Id := Relocate_Node (Concval);
3703 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
3704 Decl : Node_Id;
3705
3706 begin
3707 Decl :=
3708 Make_Object_Declaration (Loc,
3709 Defining_Identifier => Obj,
3710 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
3711 Expression => ExpR);
3712 Set_Etype (Obj, Conctyp);
3713 Decls := New_List (Decl);
3714 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
3715 end;
3716
3717 else
3718 Decls := New_List;
3719 end if;
3720
3721 Parm1 := Concurrent_Ref (Concval);
3722
3723 -- Second parameter is the entry index, computed by the routine
3724 -- provided for this purpose. The value of this expression is
3725 -- assigned to an intermediate variable to assure that any entry
3726 -- family index expressions are evaluated before the entry
3727 -- parameters.
3728
3729 if Abort_Allowed
3730 or else Restriction_Active (No_Entry_Queue) = False
3731 or else not Is_Protected_Type (Conctyp)
3732 or else Number_Entries (Conctyp) > 1
3733 or else (Has_Attach_Handler (Conctyp)
3734 and then not Restricted_Profile)
3735 then
3736 X := Make_Defining_Identifier (Loc, Name_uX);
3737
3738 Xdecl :=
3739 Make_Object_Declaration (Loc,
3740 Defining_Identifier => X,
3741 Object_Definition =>
3742 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
3743 Expression => Actual_Index_Expression (
3744 Loc, Entity (Ename), Index, Concval));
3745
3746 Append_To (Decls, Xdecl);
3747 Parm2 := New_Reference_To (X, Loc);
3748
3749 else
3750 Xdecl := Empty;
3751 Parm2 := Empty;
3752 end if;
3753
3754 -- The third parameter is the packaged parameters. If there are
3755 -- none, then it is just the null address, since nothing is passed.
3756
3757 if No (Parms) then
3758 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
3759 P := Empty;
3760
3761 -- Case of parameters present, where third argument is the address
3762 -- of a packaged record containing the required parameter values.
3763
3764 else
3765 -- First build a list of parameter values, which are references to
3766 -- objects of the parameter types.
3767
3768 Plist := New_List;
3769
3770 Actual := First_Actual (N);
3771 Formal := First_Formal (Ent);
3772
3773 while Present (Actual) loop
3774
3775 -- If it is a by_copy_type, copy it to a new variable. The
3776 -- packaged record has a field that points to this variable.
3777
3778 if Is_By_Copy_Type (Etype (Actual)) then
3779 N_Node :=
3780 Make_Object_Declaration (Loc,
3781 Defining_Identifier => Make_Temporary (Loc, 'J'),
3782 Aliased_Present => True,
3783 Object_Definition =>
3784 New_Reference_To (Etype (Formal), Loc));
3785
3786 -- Mark the object as not needing initialization since the
3787 -- initialization is performed separately, avoiding errors
3788 -- on cases such as formals of null-excluding access types.
3789
3790 Set_No_Initialization (N_Node);
3791
3792 -- We must make an assignment statement separate for the
3793 -- case of limited type. We cannot assign it unless the
3794 -- Assignment_OK flag is set first. An out formal of an
3795 -- access type must also be initialized from the actual,
3796 -- as stated in RM 6.4.1 (13).
3797
3798 if Ekind (Formal) /= E_Out_Parameter
3799 or else Is_Access_Type (Etype (Formal))
3800 then
3801 N_Var :=
3802 New_Reference_To (Defining_Identifier (N_Node), Loc);
3803 Set_Assignment_OK (N_Var);
3804 Append_To (Stats,
3805 Make_Assignment_Statement (Loc,
3806 Name => N_Var,
3807 Expression => Relocate_Node (Actual)));
3808 end if;
3809
3810 Append (N_Node, Decls);
3811
3812 Append_To (Plist,
3813 Make_Attribute_Reference (Loc,
3814 Attribute_Name => Name_Unchecked_Access,
3815 Prefix =>
3816 New_Reference_To (Defining_Identifier (N_Node), Loc)));
3817
3818 -- If it is a VM_By_Copy_Actual, copy it to a new variable
3819
3820 elsif Is_VM_By_Copy_Actual (Actual) then
3821 N_Node :=
3822 Make_Object_Declaration (Loc,
3823 Defining_Identifier => Make_Temporary (Loc, 'J'),
3824 Aliased_Present => True,
3825 Object_Definition =>
3826 New_Reference_To (Etype (Formal), Loc),
3827 Expression => New_Copy_Tree (Actual));
3828 Set_Assignment_OK (N_Node);
3829
3830 Append (N_Node, Decls);
3831
3832 Append_To (Plist,
3833 Make_Attribute_Reference (Loc,
3834 Attribute_Name => Name_Unchecked_Access,
3835 Prefix =>
3836 New_Reference_To (Defining_Identifier (N_Node), Loc)));
3837
3838 else
3839 -- Interface class-wide formal
3840
3841 if Ada_Version >= Ada_2005
3842 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
3843 and then Is_Interface (Etype (Formal))
3844 then
3845 Iface_Typ := Etype (Etype (Formal));
3846
3847 -- Generate:
3848 -- formal_iface_type! (actual.iface_tag)'reference
3849
3850 Iface_Tag :=
3851 Find_Interface_Tag (Etype (Actual), Iface_Typ);
3852 pragma Assert (Present (Iface_Tag));
3853
3854 Append_To (Plist,
3855 Make_Reference (Loc,
3856 Unchecked_Convert_To (Iface_Typ,
3857 Make_Selected_Component (Loc,
3858 Prefix =>
3859 Relocate_Node (Actual),
3860 Selector_Name =>
3861 New_Reference_To (Iface_Tag, Loc)))));
3862 else
3863 -- Generate:
3864 -- actual'reference
3865
3866 Append_To (Plist,
3867 Make_Reference (Loc, Relocate_Node (Actual)));
3868 end if;
3869 end if;
3870
3871 Next_Actual (Actual);
3872 Next_Formal_With_Extras (Formal);
3873 end loop;
3874
3875 -- Now build the declaration of parameters initialized with the
3876 -- aggregate containing this constructed parameter list.
3877
3878 P := Make_Defining_Identifier (Loc, Name_uP);
3879
3880 Pdecl :=
3881 Make_Object_Declaration (Loc,
3882 Defining_Identifier => P,
3883 Object_Definition =>
3884 New_Reference_To (Designated_Type (Ent_Acc), Loc),
3885 Expression =>
3886 Make_Aggregate (Loc, Expressions => Plist));
3887
3888 Parm3 :=
3889 Make_Attribute_Reference (Loc,
3890 Prefix => New_Reference_To (P, Loc),
3891 Attribute_Name => Name_Address);
3892
3893 Append (Pdecl, Decls);
3894 end if;
3895
3896 -- Now we can create the call, case of protected type
3897
3898 if Is_Protected_Type (Conctyp) then
3899 case Corresponding_Runtime_Package (Conctyp) is
3900 when System_Tasking_Protected_Objects_Entries =>
3901
3902 -- Change the type of the index declaration
3903
3904 Set_Object_Definition (Xdecl,
3905 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
3906
3907 -- Some additional declarations for protected entry calls
3908
3909 if No (Decls) then
3910 Decls := New_List;
3911 end if;
3912
3913 -- Bnn : Communications_Block;
3914
3915 Comm_Name := Make_Temporary (Loc, 'B');
3916
3917 Append_To (Decls,
3918 Make_Object_Declaration (Loc,
3919 Defining_Identifier => Comm_Name,
3920 Object_Definition =>
3921 New_Reference_To (RTE (RE_Communication_Block), Loc)));
3922
3923 -- Some additional statements for protected entry calls
3924
3925 -- Protected_Entry_Call (
3926 -- Object => po._object'Access,
3927 -- E => <entry index>;
3928 -- Uninterpreted_Data => P'Address;
3929 -- Mode => Simple_Call;
3930 -- Block => Bnn);
3931
3932 Call :=
3933 Make_Procedure_Call_Statement (Loc,
3934 Name =>
3935 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
3936
3937 Parameter_Associations => New_List (
3938 Make_Attribute_Reference (Loc,
3939 Attribute_Name => Name_Unchecked_Access,
3940 Prefix => Parm1),
3941 Parm2,
3942 Parm3,
3943 New_Reference_To (RTE (RE_Simple_Call), Loc),
3944 New_Occurrence_Of (Comm_Name, Loc)));
3945
3946 when System_Tasking_Protected_Objects_Single_Entry =>
3947 -- Protected_Single_Entry_Call (
3948 -- Object => po._object'Access,
3949 -- Uninterpreted_Data => P'Address;
3950 -- Mode => Simple_Call);
3951
3952 Call :=
3953 Make_Procedure_Call_Statement (Loc,
3954 Name => New_Reference_To (
3955 RTE (RE_Protected_Single_Entry_Call), Loc),
3956
3957 Parameter_Associations => New_List (
3958 Make_Attribute_Reference (Loc,
3959 Attribute_Name => Name_Unchecked_Access,
3960 Prefix => Parm1),
3961 Parm3,
3962 New_Reference_To (RTE (RE_Simple_Call), Loc)));
3963
3964 when others =>
3965 raise Program_Error;
3966 end case;
3967
3968 -- Case of task type
3969
3970 else
3971 Call :=
3972 Make_Procedure_Call_Statement (Loc,
3973 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
3974 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
3975
3976 end if;
3977
3978 Append_To (Stats, Call);
3979
3980 -- If there are out or in/out parameters by copy add assignment
3981 -- statements for the result values.
3982
3983 if Present (Parms) then
3984 Actual := First_Actual (N);
3985 Formal := First_Formal (Ent);
3986
3987 Set_Assignment_OK (Actual);
3988 while Present (Actual) loop
3989 if (Is_By_Copy_Type (Etype (Actual))
3990 or else Is_VM_By_Copy_Actual (Actual))
3991 and then Ekind (Formal) /= E_In_Parameter
3992 then
3993 N_Node :=
3994 Make_Assignment_Statement (Loc,
3995 Name => New_Copy (Actual),
3996 Expression =>
3997 Make_Explicit_Dereference (Loc,
3998 Make_Selected_Component (Loc,
3999 Prefix => New_Reference_To (P, Loc),
4000 Selector_Name =>
4001 Make_Identifier (Loc, Chars (Formal)))));
4002
4003 -- In all cases (including limited private types) we want
4004 -- the assignment to be valid.
4005
4006 Set_Assignment_OK (Name (N_Node));
4007
4008 -- If the call is the triggering alternative in an
4009 -- asynchronous select, or the entry_call alternative of a
4010 -- conditional entry call, the assignments for in-out
4011 -- parameters are incorporated into the statement list that
4012 -- follows, so that there are executed only if the entry
4013 -- call succeeds.
4014
4015 if (Nkind (Parent (N)) = N_Triggering_Alternative
4016 and then N = Triggering_Statement (Parent (N)))
4017 or else
4018 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4019 and then N = Entry_Call_Statement (Parent (N)))
4020 then
4021 if No (Statements (Parent (N))) then
4022 Set_Statements (Parent (N), New_List);
4023 end if;
4024
4025 Prepend (N_Node, Statements (Parent (N)));
4026
4027 else
4028 Insert_After (Call, N_Node);
4029 end if;
4030 end if;
4031
4032 Next_Actual (Actual);
4033 Next_Formal_With_Extras (Formal);
4034 end loop;
4035 end if;
4036
4037 -- Finally, create block and analyze it
4038
4039 Rewrite (N,
4040 Make_Block_Statement (Loc,
4041 Declarations => Decls,
4042 Handled_Statement_Sequence =>
4043 Make_Handled_Sequence_Of_Statements (Loc,
4044 Statements => Stats)));
4045
4046 Analyze (N);
4047 end;
4048 end Build_Simple_Entry_Call;
4049
4050 --------------------------------
4051 -- Build_Task_Activation_Call --
4052 --------------------------------
4053
4054 procedure Build_Task_Activation_Call (N : Node_Id) is
4055 Loc : constant Source_Ptr := Sloc (N);
4056 Chain : Entity_Id;
4057 Call : Node_Id;
4058 Name : Node_Id;
4059 P : Node_Id;
4060
4061 begin
4062 -- Get the activation chain entity. Except in the case of a package
4063 -- body, this is in the node that was passed. For a package body, we
4064 -- have to find the corresponding package declaration node.
4065
4066 if Nkind (N) = N_Package_Body then
4067 P := Corresponding_Spec (N);
4068 loop
4069 P := Parent (P);
4070 exit when Nkind (P) = N_Package_Declaration;
4071 end loop;
4072
4073 Chain := Activation_Chain_Entity (P);
4074
4075 else
4076 Chain := Activation_Chain_Entity (N);
4077 end if;
4078
4079 if Present (Chain) then
4080 if Restricted_Profile then
4081 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
4082 else
4083 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
4084 end if;
4085
4086 Call :=
4087 Make_Procedure_Call_Statement (Loc,
4088 Name => Name,
4089 Parameter_Associations =>
4090 New_List (Make_Attribute_Reference (Loc,
4091 Prefix => New_Occurrence_Of (Chain, Loc),
4092 Attribute_Name => Name_Unchecked_Access)));
4093
4094 if Nkind (N) = N_Package_Declaration then
4095 if Present (Corresponding_Body (N)) then
4096 null;
4097
4098 elsif Present (Private_Declarations (Specification (N))) then
4099 Append (Call, Private_Declarations (Specification (N)));
4100
4101 else
4102 Append (Call, Visible_Declarations (Specification (N)));
4103 end if;
4104
4105 else
4106 if Present (Handled_Statement_Sequence (N)) then
4107
4108 -- The call goes at the start of the statement sequence
4109 -- after the start of exception range label if one is present.
4110
4111 declare
4112 Stm : Node_Id;
4113
4114 begin
4115 Stm := First (Statements (Handled_Statement_Sequence (N)));
4116
4117 -- A special case, skip exception range label if one is
4118 -- present (from front end zcx processing).
4119
4120 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4121 Next (Stm);
4122 end if;
4123
4124 -- Another special case, if the first statement is a block
4125 -- from optimization of a local raise to a goto, then the
4126 -- call goes inside this block.
4127
4128 if Nkind (Stm) = N_Block_Statement
4129 and then Exception_Junk (Stm)
4130 then
4131 Stm :=
4132 First (Statements (Handled_Statement_Sequence (Stm)));
4133 end if;
4134
4135 -- Insertion point is after any exception label pushes,
4136 -- since we want it covered by any local handlers.
4137
4138 while Nkind (Stm) in N_Push_xxx_Label loop
4139 Next (Stm);
4140 end loop;
4141
4142 -- Now we have the proper insertion point
4143
4144 Insert_Before (Stm, Call);
4145 end;
4146
4147 else
4148 Set_Handled_Statement_Sequence (N,
4149 Make_Handled_Sequence_Of_Statements (Loc,
4150 Statements => New_List (Call)));
4151 end if;
4152 end if;
4153
4154 Analyze (Call);
4155 Check_Task_Activation (N);
4156 end if;
4157 end Build_Task_Activation_Call;
4158
4159 -------------------------------
4160 -- Build_Task_Allocate_Block --
4161 -------------------------------
4162
4163 procedure Build_Task_Allocate_Block
4164 (Actions : List_Id;
4165 N : Node_Id;
4166 Args : List_Id)
4167 is
4168 T : constant Entity_Id := Entity (Expression (N));
4169 Init : constant Entity_Id := Base_Init_Proc (T);
4170 Loc : constant Source_Ptr := Sloc (N);
4171 Chain : constant Entity_Id :=
4172 Make_Defining_Identifier (Loc, Name_uChain);
4173 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4174 Block : Node_Id;
4175
4176 begin
4177 Block :=
4178 Make_Block_Statement (Loc,
4179 Identifier => New_Reference_To (Blkent, Loc),
4180 Declarations => New_List (
4181
4182 -- _Chain : Activation_Chain;
4183
4184 Make_Object_Declaration (Loc,
4185 Defining_Identifier => Chain,
4186 Aliased_Present => True,
4187 Object_Definition =>
4188 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4189
4190 Handled_Statement_Sequence =>
4191 Make_Handled_Sequence_Of_Statements (Loc,
4192
4193 Statements => New_List (
4194
4195 -- Init (Args);
4196
4197 Make_Procedure_Call_Statement (Loc,
4198 Name => New_Reference_To (Init, Loc),
4199 Parameter_Associations => Args),
4200
4201 -- Activate_Tasks (_Chain);
4202
4203 Make_Procedure_Call_Statement (Loc,
4204 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4205 Parameter_Associations => New_List (
4206 Make_Attribute_Reference (Loc,
4207 Prefix => New_Reference_To (Chain, Loc),
4208 Attribute_Name => Name_Unchecked_Access))))),
4209
4210 Has_Created_Identifier => True,
4211 Is_Task_Allocation_Block => True);
4212
4213 Append_To (Actions,
4214 Make_Implicit_Label_Declaration (Loc,
4215 Defining_Identifier => Blkent,
4216 Label_Construct => Block));
4217
4218 Append_To (Actions, Block);
4219
4220 Set_Activation_Chain_Entity (Block, Chain);
4221 end Build_Task_Allocate_Block;
4222
4223 -----------------------------------------------
4224 -- Build_Task_Allocate_Block_With_Init_Stmts --
4225 -----------------------------------------------
4226
4227 procedure Build_Task_Allocate_Block_With_Init_Stmts
4228 (Actions : List_Id;
4229 N : Node_Id;
4230 Init_Stmts : List_Id)
4231 is
4232 Loc : constant Source_Ptr := Sloc (N);
4233 Chain : constant Entity_Id :=
4234 Make_Defining_Identifier (Loc, Name_uChain);
4235 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4236 Block : Node_Id;
4237
4238 begin
4239 Append_To (Init_Stmts,
4240 Make_Procedure_Call_Statement (Loc,
4241 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4242 Parameter_Associations => New_List (
4243 Make_Attribute_Reference (Loc,
4244 Prefix => New_Reference_To (Chain, Loc),
4245 Attribute_Name => Name_Unchecked_Access))));
4246
4247 Block :=
4248 Make_Block_Statement (Loc,
4249 Identifier => New_Reference_To (Blkent, Loc),
4250 Declarations => New_List (
4251
4252 -- _Chain : Activation_Chain;
4253
4254 Make_Object_Declaration (Loc,
4255 Defining_Identifier => Chain,
4256 Aliased_Present => True,
4257 Object_Definition =>
4258 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4259
4260 Handled_Statement_Sequence =>
4261 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4262
4263 Has_Created_Identifier => True,
4264 Is_Task_Allocation_Block => True);
4265
4266 Append_To (Actions,
4267 Make_Implicit_Label_Declaration (Loc,
4268 Defining_Identifier => Blkent,
4269 Label_Construct => Block));
4270
4271 Append_To (Actions, Block);
4272
4273 Set_Activation_Chain_Entity (Block, Chain);
4274 end Build_Task_Allocate_Block_With_Init_Stmts;
4275
4276 -----------------------------------
4277 -- Build_Task_Proc_Specification --
4278 -----------------------------------
4279
4280 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4281 Loc : constant Source_Ptr := Sloc (T);
4282 Spec_Id : Entity_Id;
4283
4284 begin
4285 -- Case of explicit task type, suffix TB
4286
4287 if Comes_From_Source (T) then
4288 Spec_Id :=
4289 Make_Defining_Identifier (Loc,
4290 Chars => New_External_Name (Chars (T), "TB"));
4291
4292 -- Case of anonymous task type, suffix B
4293
4294 else
4295 Spec_Id :=
4296 Make_Defining_Identifier (Loc,
4297 Chars => New_External_Name (Chars (T), 'B'));
4298 end if;
4299
4300 Set_Is_Internal (Spec_Id);
4301
4302 -- Associate the procedure with the task, if this is the declaration
4303 -- (and not the body) of the procedure.
4304
4305 if No (Task_Body_Procedure (T)) then
4306 Set_Task_Body_Procedure (T, Spec_Id);
4307 end if;
4308
4309 return
4310 Make_Procedure_Specification (Loc,
4311 Defining_Unit_Name => Spec_Id,
4312 Parameter_Specifications => New_List (
4313 Make_Parameter_Specification (Loc,
4314 Defining_Identifier =>
4315 Make_Defining_Identifier (Loc, Name_uTask),
4316 Parameter_Type =>
4317 Make_Access_Definition (Loc,
4318 Subtype_Mark =>
4319 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
4320 end Build_Task_Proc_Specification;
4321
4322 ---------------------------------------
4323 -- Build_Unprotected_Subprogram_Body --
4324 ---------------------------------------
4325
4326 function Build_Unprotected_Subprogram_Body
4327 (N : Node_Id;
4328 Pid : Node_Id) return Node_Id
4329 is
4330 Decls : constant List_Id := Declarations (N);
4331
4332 begin
4333 -- Add renamings for the Protection object, discriminals, privals and
4334 -- the entry index constant for use by debugger.
4335
4336 Debug_Private_Data_Declarations (Decls);
4337
4338 -- Make an unprotected version of the subprogram for use within the same
4339 -- object, with a new name and an additional parameter representing the
4340 -- object.
4341
4342 return
4343 Make_Subprogram_Body (Sloc (N),
4344 Specification =>
4345 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
4346 Declarations => Decls,
4347 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
4348 end Build_Unprotected_Subprogram_Body;
4349
4350 ----------------------------
4351 -- Collect_Entry_Families --
4352 ----------------------------
4353
4354 procedure Collect_Entry_Families
4355 (Loc : Source_Ptr;
4356 Cdecls : List_Id;
4357 Current_Node : in out Node_Id;
4358 Conctyp : Entity_Id)
4359 is
4360 Efam : Entity_Id;
4361 Efam_Decl : Node_Id;
4362 Efam_Type : Entity_Id;
4363
4364 begin
4365 Efam := First_Entity (Conctyp);
4366 while Present (Efam) loop
4367 if Ekind (Efam) = E_Entry_Family then
4368 Efam_Type := Make_Temporary (Loc, 'F');
4369
4370 declare
4371 Bas : Entity_Id :=
4372 Base_Type
4373 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
4374
4375 Bas_Decl : Node_Id := Empty;
4376 Lo, Hi : Node_Id;
4377
4378 begin
4379 Get_Index_Bounds
4380 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
4381
4382 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
4383 Bas := Make_Temporary (Loc, 'B');
4384
4385 Bas_Decl :=
4386 Make_Subtype_Declaration (Loc,
4387 Defining_Identifier => Bas,
4388 Subtype_Indication =>
4389 Make_Subtype_Indication (Loc,
4390 Subtype_Mark =>
4391 New_Occurrence_Of (Standard_Integer, Loc),
4392 Constraint =>
4393 Make_Range_Constraint (Loc,
4394 Range_Expression => Make_Range (Loc,
4395 Make_Integer_Literal
4396 (Loc, -Entry_Family_Bound),
4397 Make_Integer_Literal
4398 (Loc, Entry_Family_Bound - 1)))));
4399
4400 Insert_After (Current_Node, Bas_Decl);
4401 Current_Node := Bas_Decl;
4402 Analyze (Bas_Decl);
4403 end if;
4404
4405 Efam_Decl :=
4406 Make_Full_Type_Declaration (Loc,
4407 Defining_Identifier => Efam_Type,
4408 Type_Definition =>
4409 Make_Unconstrained_Array_Definition (Loc,
4410 Subtype_Marks =>
4411 (New_List (New_Occurrence_Of (Bas, Loc))),
4412
4413 Component_Definition =>
4414 Make_Component_Definition (Loc,
4415 Aliased_Present => False,
4416 Subtype_Indication =>
4417 New_Reference_To (Standard_Character, Loc))));
4418 end;
4419
4420 Insert_After (Current_Node, Efam_Decl);
4421 Current_Node := Efam_Decl;
4422 Analyze (Efam_Decl);
4423
4424 Append_To (Cdecls,
4425 Make_Component_Declaration (Loc,
4426 Defining_Identifier =>
4427 Make_Defining_Identifier (Loc, Chars (Efam)),
4428
4429 Component_Definition =>
4430 Make_Component_Definition (Loc,
4431 Aliased_Present => False,
4432 Subtype_Indication =>
4433 Make_Subtype_Indication (Loc,
4434 Subtype_Mark =>
4435 New_Occurrence_Of (Efam_Type, Loc),
4436
4437 Constraint =>
4438 Make_Index_Or_Discriminant_Constraint (Loc,
4439 Constraints => New_List (
4440 New_Occurrence_Of
4441 (Etype (Discrete_Subtype_Definition
4442 (Parent (Efam))), Loc)))))));
4443
4444 end if;
4445
4446 Next_Entity (Efam);
4447 end loop;
4448 end Collect_Entry_Families;
4449
4450 -----------------------
4451 -- Concurrent_Object --
4452 -----------------------
4453
4454 function Concurrent_Object
4455 (Spec_Id : Entity_Id;
4456 Conc_Typ : Entity_Id) return Entity_Id
4457 is
4458 begin
4459 -- Parameter _O or _object
4460
4461 if Is_Protected_Type (Conc_Typ) then
4462 return First_Formal (Protected_Body_Subprogram (Spec_Id));
4463
4464 -- Parameter _task
4465
4466 else
4467 pragma Assert (Is_Task_Type (Conc_Typ));
4468 return First_Formal (Task_Body_Procedure (Conc_Typ));
4469 end if;
4470 end Concurrent_Object;
4471
4472 ----------------------
4473 -- Copy_Result_Type --
4474 ----------------------
4475
4476 function Copy_Result_Type (Res : Node_Id) return Node_Id is
4477 New_Res : constant Node_Id := New_Copy_Tree (Res);
4478 Par_Spec : Node_Id;
4479 Formal : Entity_Id;
4480
4481 begin
4482 -- If the result type is an access_to_subprogram, we must create
4483 -- new entities for its spec.
4484
4485 if Nkind (New_Res) = N_Access_Definition
4486 and then Present (Access_To_Subprogram_Definition (New_Res))
4487 then
4488 -- Provide new entities for the formals
4489
4490 Par_Spec := First (Parameter_Specifications
4491 (Access_To_Subprogram_Definition (New_Res)));
4492 while Present (Par_Spec) loop
4493 Formal := Defining_Identifier (Par_Spec);
4494 Set_Defining_Identifier (Par_Spec,
4495 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
4496 Next (Par_Spec);
4497 end loop;
4498 end if;
4499
4500 return New_Res;
4501 end Copy_Result_Type;
4502
4503 --------------------
4504 -- Concurrent_Ref --
4505 --------------------
4506
4507 -- The expression returned for a reference to a concurrent object has the
4508 -- form:
4509
4510 -- taskV!(name)._Task_Id
4511
4512 -- for a task, and
4513
4514 -- objectV!(name)._Object
4515
4516 -- for a protected object. For the case of an access to a concurrent
4517 -- object, there is an extra explicit dereference:
4518
4519 -- taskV!(name.all)._Task_Id
4520 -- objectV!(name.all)._Object
4521
4522 -- here taskV and objectV are the types for the associated records, which
4523 -- contain the required _Task_Id and _Object fields for tasks and protected
4524 -- objects, respectively.
4525
4526 -- For the case of a task type name, the expression is
4527
4528 -- Self;
4529
4530 -- i.e. a call to the Self function which returns precisely this Task_Id
4531
4532 -- For the case of a protected type name, the expression is
4533
4534 -- objectR
4535
4536 -- which is a renaming of the _object field of the current object
4537 -- record, passed into protected operations as a parameter.
4538
4539 function Concurrent_Ref (N : Node_Id) return Node_Id is
4540 Loc : constant Source_Ptr := Sloc (N);
4541 Ntyp : constant Entity_Id := Etype (N);
4542 Dtyp : Entity_Id;
4543 Sel : Name_Id;
4544
4545 function Is_Current_Task (T : Entity_Id) return Boolean;
4546 -- Check whether the reference is to the immediately enclosing task
4547 -- type, or to an outer one (rare but legal).
4548
4549 ---------------------
4550 -- Is_Current_Task --
4551 ---------------------
4552
4553 function Is_Current_Task (T : Entity_Id) return Boolean is
4554 Scop : Entity_Id;
4555
4556 begin
4557 Scop := Current_Scope;
4558 while Present (Scop)
4559 and then Scop /= Standard_Standard
4560 loop
4561
4562 if Scop = T then
4563 return True;
4564
4565 elsif Is_Task_Type (Scop) then
4566 return False;
4567
4568 -- If this is a procedure nested within the task type, we must
4569 -- assume that it can be called from an inner task, and therefore
4570 -- cannot treat it as a local reference.
4571
4572 elsif Is_Overloadable (Scop)
4573 and then In_Open_Scopes (T)
4574 then
4575 return False;
4576
4577 else
4578 Scop := Scope (Scop);
4579 end if;
4580 end loop;
4581
4582 -- We know that we are within the task body, so should have found it
4583 -- in scope.
4584
4585 raise Program_Error;
4586 end Is_Current_Task;
4587
4588 -- Start of processing for Concurrent_Ref
4589
4590 begin
4591 if Is_Access_Type (Ntyp) then
4592 Dtyp := Designated_Type (Ntyp);
4593
4594 if Is_Protected_Type (Dtyp) then
4595 Sel := Name_uObject;
4596 else
4597 Sel := Name_uTask_Id;
4598 end if;
4599
4600 return
4601 Make_Selected_Component (Loc,
4602 Prefix =>
4603 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
4604 Make_Explicit_Dereference (Loc, N)),
4605 Selector_Name => Make_Identifier (Loc, Sel));
4606
4607 elsif Is_Entity_Name (N)
4608 and then Is_Concurrent_Type (Entity (N))
4609 then
4610 if Is_Task_Type (Entity (N)) then
4611
4612 if Is_Current_Task (Entity (N)) then
4613 return
4614 Make_Function_Call (Loc,
4615 Name => New_Reference_To (RTE (RE_Self), Loc));
4616
4617 else
4618 declare
4619 Decl : Node_Id;
4620 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
4621 T_Body : constant Node_Id :=
4622 Parent (Corresponding_Body (Parent (Entity (N))));
4623
4624 begin
4625 Decl :=
4626 Make_Object_Declaration (Loc,
4627 Defining_Identifier => T_Self,
4628 Object_Definition =>
4629 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
4630 Expression =>
4631 Make_Function_Call (Loc,
4632 Name => New_Reference_To (RTE (RE_Self), Loc)));
4633 Prepend (Decl, Declarations (T_Body));
4634 Analyze (Decl);
4635 Set_Scope (T_Self, Entity (N));
4636 return New_Occurrence_Of (T_Self, Loc);
4637 end;
4638 end if;
4639
4640 else
4641 pragma Assert (Is_Protected_Type (Entity (N)));
4642
4643 return
4644 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
4645 end if;
4646
4647 else
4648 if Is_Protected_Type (Ntyp) then
4649 Sel := Name_uObject;
4650
4651 elsif Is_Task_Type (Ntyp) then
4652 Sel := Name_uTask_Id;
4653
4654 else
4655 raise Program_Error;
4656 end if;
4657
4658 return
4659 Make_Selected_Component (Loc,
4660 Prefix =>
4661 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
4662 New_Copy_Tree (N)),
4663 Selector_Name => Make_Identifier (Loc, Sel));
4664 end if;
4665 end Concurrent_Ref;
4666
4667 ------------------------
4668 -- Convert_Concurrent --
4669 ------------------------
4670
4671 function Convert_Concurrent
4672 (N : Node_Id;
4673 Typ : Entity_Id) return Node_Id
4674 is
4675 begin
4676 if not Is_Concurrent_Type (Typ) then
4677 return N;
4678 else
4679 return
4680 Unchecked_Convert_To
4681 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
4682 end if;
4683 end Convert_Concurrent;
4684
4685 -------------------------------------
4686 -- Debug_Private_Data_Declarations --
4687 -------------------------------------
4688
4689 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
4690 Debug_Nod : Node_Id;
4691 Decl : Node_Id;
4692
4693 begin
4694 Decl := First (Decls);
4695 while Present (Decl)
4696 and then not Comes_From_Source (Decl)
4697 loop
4698 -- Declaration for concurrent entity _object and its access type,
4699 -- along with the entry index subtype:
4700 -- type prot_typVP is access prot_typV;
4701 -- _object : prot_typVP := prot_typV (_O);
4702 -- subtype Jnn is <Type of Index> range Low .. High;
4703
4704 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
4705 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4706
4707 -- Declaration for the Protection object, discriminals, privals and
4708 -- entry index constant:
4709 -- conc_typR : protection_typ renames _object._object;
4710 -- discr_nameD : discr_typ renames _object.discr_name;
4711 -- discr_nameD : discr_typ renames _task.discr_name;
4712 -- prival_name : comp_typ renames _object.comp_name;
4713 -- J : constant Jnn :=
4714 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
4715
4716 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
4717 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4718 Debug_Nod := Debug_Renaming_Declaration (Decl);
4719
4720 if Present (Debug_Nod) then
4721 Insert_After (Decl, Debug_Nod);
4722 end if;
4723 end if;
4724
4725 Next (Decl);
4726 end loop;
4727 end Debug_Private_Data_Declarations;
4728
4729 ----------------------------
4730 -- Entry_Index_Expression --
4731 ----------------------------
4732
4733 function Entry_Index_Expression
4734 (Sloc : Source_Ptr;
4735 Ent : Entity_Id;
4736 Index : Node_Id;
4737 Ttyp : Entity_Id) return Node_Id
4738 is
4739 Expr : Node_Id;
4740 Num : Node_Id;
4741 Lo : Node_Id;
4742 Hi : Node_Id;
4743 Prev : Entity_Id;
4744 S : Node_Id;
4745
4746 begin
4747 -- The queues of entries and entry families appear in textual order in
4748 -- the associated record. The entry index is computed as the sum of the
4749 -- number of queues for all entries that precede the designated one, to
4750 -- which is added the index expression, if this expression denotes a
4751 -- member of a family.
4752
4753 -- The following is a place holder for the count of simple entries
4754
4755 Num := Make_Integer_Literal (Sloc, 1);
4756
4757 -- We construct an expression which is a series of addition operations.
4758 -- The first operand is the number of single entries that precede this
4759 -- one, the second operand is the index value relative to the start of
4760 -- the referenced family, and the remaining operands are the lengths of
4761 -- the entry families that precede this entry, i.e. the constructed
4762 -- expression is:
4763
4764 -- number_simple_entries +
4765 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
4766 -- family'length + ...
4767
4768 -- where index-value is the given index value, and s is the index
4769 -- subtype (we have to use pos because the subtype might be an
4770 -- enumeration type preventing direct subtraction). Note that the task
4771 -- entry array is one-indexed.
4772
4773 -- The upper bound of the entry family may be a discriminant, so we
4774 -- retrieve the lower bound explicitly to compute offset, rather than
4775 -- using the index subtype which may mention a discriminant.
4776
4777 if Present (Index) then
4778 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
4779
4780 Expr :=
4781 Make_Op_Add (Sloc,
4782 Left_Opnd => Num,
4783
4784 Right_Opnd =>
4785 Family_Offset (
4786 Sloc,
4787 Make_Attribute_Reference (Sloc,
4788 Attribute_Name => Name_Pos,
4789 Prefix => New_Reference_To (Base_Type (S), Sloc),
4790 Expressions => New_List (Relocate_Node (Index))),
4791 Type_Low_Bound (S),
4792 Ttyp,
4793 False));
4794 else
4795 Expr := Num;
4796 end if;
4797
4798 -- Now add lengths of preceding entries and entry families
4799
4800 Prev := First_Entity (Ttyp);
4801
4802 while Chars (Prev) /= Chars (Ent)
4803 or else (Ekind (Prev) /= Ekind (Ent))
4804 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
4805 loop
4806 if Ekind (Prev) = E_Entry then
4807 Set_Intval (Num, Intval (Num) + 1);
4808
4809 elsif Ekind (Prev) = E_Entry_Family then
4810 S :=
4811 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
4812 Lo := Type_Low_Bound (S);
4813 Hi := Type_High_Bound (S);
4814
4815 Expr :=
4816 Make_Op_Add (Sloc,
4817 Left_Opnd => Expr,
4818 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
4819
4820 -- Other components are anonymous types to be ignored
4821
4822 else
4823 null;
4824 end if;
4825
4826 Next_Entity (Prev);
4827 end loop;
4828
4829 return Expr;
4830 end Entry_Index_Expression;
4831
4832 ---------------------------
4833 -- Establish_Task_Master --
4834 ---------------------------
4835
4836 procedure Establish_Task_Master (N : Node_Id) is
4837 Call : Node_Id;
4838 begin
4839 if Restriction_Active (No_Task_Hierarchy) = False then
4840 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
4841 Prepend_To (Declarations (N), Call);
4842 Analyze (Call);
4843 end if;
4844 end Establish_Task_Master;
4845
4846 --------------------------------
4847 -- Expand_Accept_Declarations --
4848 --------------------------------
4849
4850 -- Part of the expansion of an accept statement involves the creation of
4851 -- a declaration that can be referenced from the statement sequence of
4852 -- the accept:
4853
4854 -- Ann : Address;
4855
4856 -- This declaration is inserted immediately before the accept statement
4857 -- and it is important that it be inserted before the statements of the
4858 -- statement sequence are analyzed. Thus it would be too late to create
4859 -- this declaration in the Expand_N_Accept_Statement routine, which is
4860 -- why there is a separate procedure to be called directly from Sem_Ch9.
4861
4862 -- Ann is used to hold the address of the record containing the parameters
4863 -- (see Expand_N_Entry_Call for more details on how this record is built).
4864 -- References to the parameters do an unchecked conversion of this address
4865 -- to a pointer to the required record type, and then access the field that
4866 -- holds the value of the required parameter. The entity for the address
4867 -- variable is held as the top stack element (i.e. the last element) of the
4868 -- Accept_Address stack in the corresponding entry entity, and this element
4869 -- must be set in place before the statements are processed.
4870
4871 -- The above description applies to the case of a stand alone accept
4872 -- statement, i.e. one not appearing as part of a select alternative.
4873
4874 -- For the case of an accept that appears as part of a select alternative
4875 -- of a selective accept, we must still create the declaration right away,
4876 -- since Ann is needed immediately, but there is an important difference:
4877
4878 -- The declaration is inserted before the selective accept, not before
4879 -- the accept statement (which is not part of a list anyway, and so would
4880 -- not accommodate inserted declarations)
4881
4882 -- We only need one address variable for the entire selective accept. So
4883 -- the Ann declaration is created only for the first accept alternative,
4884 -- and subsequent accept alternatives reference the same Ann variable.
4885
4886 -- We can distinguish the two cases by seeing whether the accept statement
4887 -- is part of a list. If not, then it must be in an accept alternative.
4888
4889 -- To expand the requeue statement, a label is provided at the end of the
4890 -- accept statement or alternative of which it is a part, so that the
4891 -- statement can be skipped after the requeue is complete. This label is
4892 -- created here rather than during the expansion of the accept statement,
4893 -- because it will be needed by any requeue statements within the accept,
4894 -- which are expanded before the accept.
4895
4896 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
4897 Loc : constant Source_Ptr := Sloc (N);
4898 Stats : constant Node_Id := Handled_Statement_Sequence (N);
4899 Ann : Entity_Id := Empty;
4900 Adecl : Node_Id;
4901 Lab_Id : Node_Id;
4902 Lab : Node_Id;
4903 Ldecl : Node_Id;
4904 Ldecl2 : Node_Id;
4905
4906 begin
4907 if Full_Expander_Active then
4908 -- If we have no handled statement sequence, we may need to build
4909 -- a dummy sequence consisting of a null statement. This can be
4910 -- skipped if the trivial accept optimization is permitted.
4911
4912 if not Trivial_Accept_OK
4913 and then
4914 (No (Stats) or else Null_Statements (Statements (Stats)))
4915 then
4916 Set_Handled_Statement_Sequence (N,
4917 Make_Handled_Sequence_Of_Statements (Loc,
4918 Statements => New_List (Make_Null_Statement (Loc))));
4919 end if;
4920
4921 -- Create and declare two labels to be placed at the end of the
4922 -- accept statement. The first label is used to allow requeues to
4923 -- skip the remainder of entry processing. The second label is used
4924 -- to skip the remainder of entry processing if the rendezvous
4925 -- completes in the middle of the accept body.
4926
4927 if Present (Handled_Statement_Sequence (N)) then
4928 declare
4929 Ent : Entity_Id;
4930
4931 begin
4932 Ent := Make_Temporary (Loc, 'L');
4933 Lab_Id := New_Reference_To (Ent, Loc);
4934 Lab := Make_Label (Loc, Lab_Id);
4935 Ldecl :=
4936 Make_Implicit_Label_Declaration (Loc,
4937 Defining_Identifier => Ent,
4938 Label_Construct => Lab);
4939 Append (Lab, Statements (Handled_Statement_Sequence (N)));
4940
4941 Ent := Make_Temporary (Loc, 'L');
4942 Lab_Id := New_Reference_To (Ent, Loc);
4943 Lab := Make_Label (Loc, Lab_Id);
4944 Ldecl2 :=
4945 Make_Implicit_Label_Declaration (Loc,
4946 Defining_Identifier => Ent,
4947 Label_Construct => Lab);
4948 Append (Lab, Statements (Handled_Statement_Sequence (N)));
4949 end;
4950
4951 else
4952 Ldecl := Empty;
4953 Ldecl2 := Empty;
4954 end if;
4955
4956 -- Case of stand alone accept statement
4957
4958 if Is_List_Member (N) then
4959
4960 if Present (Handled_Statement_Sequence (N)) then
4961 Ann := Make_Temporary (Loc, 'A');
4962
4963 Adecl :=
4964 Make_Object_Declaration (Loc,
4965 Defining_Identifier => Ann,
4966 Object_Definition =>
4967 New_Reference_To (RTE (RE_Address), Loc));
4968
4969 Insert_Before (N, Adecl);
4970 Analyze (Adecl);
4971
4972 Insert_Before (N, Ldecl);
4973 Analyze (Ldecl);
4974
4975 Insert_Before (N, Ldecl2);
4976 Analyze (Ldecl2);
4977 end if;
4978
4979 -- Case of accept statement which is in an accept alternative
4980
4981 else
4982 declare
4983 Acc_Alt : constant Node_Id := Parent (N);
4984 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
4985 Alt : Node_Id;
4986
4987 begin
4988 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
4989 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
4990
4991 -- ??? Consider a single label for select statements
4992
4993 if Present (Handled_Statement_Sequence (N)) then
4994 Prepend (Ldecl2,
4995 Statements (Handled_Statement_Sequence (N)));
4996 Analyze (Ldecl2);
4997
4998 Prepend (Ldecl,
4999 Statements (Handled_Statement_Sequence (N)));
5000 Analyze (Ldecl);
5001 end if;
5002
5003 -- Find first accept alternative of the selective accept. A
5004 -- valid selective accept must have at least one accept in it.
5005
5006 Alt := First (Select_Alternatives (Sel_Acc));
5007
5008 while Nkind (Alt) /= N_Accept_Alternative loop
5009 Next (Alt);
5010 end loop;
5011
5012 -- If we are the first accept statement, then we have to create
5013 -- the Ann variable, as for the stand alone case, except that
5014 -- it is inserted before the selective accept. Similarly, a
5015 -- label for requeue expansion must be declared.
5016
5017 if N = Accept_Statement (Alt) then
5018 Ann := Make_Temporary (Loc, 'A');
5019 Adecl :=
5020 Make_Object_Declaration (Loc,
5021 Defining_Identifier => Ann,
5022 Object_Definition =>
5023 New_Reference_To (RTE (RE_Address), Loc));
5024
5025 Insert_Before (Sel_Acc, Adecl);
5026 Analyze (Adecl);
5027
5028 -- If we are not the first accept statement, then find the Ann
5029 -- variable allocated by the first accept and use it.
5030
5031 else
5032 Ann :=
5033 Node (Last_Elmt (Accept_Address
5034 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5035 end if;
5036 end;
5037 end if;
5038
5039 -- Merge here with Ann either created or referenced, and Adecl
5040 -- pointing to the corresponding declaration. Remaining processing
5041 -- is the same for the two cases.
5042
5043 if Present (Ann) then
5044 Append_Elmt (Ann, Accept_Address (Ent));
5045 Set_Debug_Info_Needed (Ann);
5046 end if;
5047
5048 -- Create renaming declarations for the entry formals. Each reference
5049 -- to a formal becomes a dereference of a component of the parameter
5050 -- block, whose address is held in Ann. These declarations are
5051 -- eventually inserted into the accept block, and analyzed there so
5052 -- that they have the proper scope for gdb and do not conflict with
5053 -- other declarations.
5054
5055 if Present (Parameter_Specifications (N))
5056 and then Present (Handled_Statement_Sequence (N))
5057 then
5058 declare
5059 Comp : Entity_Id;
5060 Decl : Node_Id;
5061 Formal : Entity_Id;
5062 New_F : Entity_Id;
5063 Renamed_Formal : Node_Id;
5064
5065 begin
5066 Push_Scope (Ent);
5067 Formal := First_Formal (Ent);
5068
5069 while Present (Formal) loop
5070 Comp := Entry_Component (Formal);
5071 New_F :=
5072 Make_Defining_Identifier (Loc, Chars (Formal));
5073
5074 Set_Etype (New_F, Etype (Formal));
5075 Set_Scope (New_F, Ent);
5076
5077 -- Now we set debug info needed on New_F even though it does
5078 -- not come from source, so that the debugger will get the
5079 -- right information for these generated names.
5080
5081 Set_Debug_Info_Needed (New_F);
5082
5083 if Ekind (Formal) = E_In_Parameter then
5084 Set_Ekind (New_F, E_Constant);
5085 else
5086 Set_Ekind (New_F, E_Variable);
5087 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5088 end if;
5089
5090 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5091
5092 Renamed_Formal :=
5093 Make_Selected_Component (Loc,
5094 Prefix =>
5095 Unchecked_Convert_To (
5096 Entry_Parameters_Type (Ent),
5097 New_Reference_To (Ann, Loc)),
5098 Selector_Name =>
5099 New_Reference_To (Comp, Loc));
5100
5101 Decl :=
5102 Build_Renamed_Formal_Declaration
5103 (New_F, Formal, Comp, Renamed_Formal);
5104
5105 if No (Declarations (N)) then
5106 Set_Declarations (N, New_List);
5107 end if;
5108
5109 Append (Decl, Declarations (N));
5110 Set_Renamed_Object (Formal, New_F);
5111 Next_Formal (Formal);
5112 end loop;
5113
5114 End_Scope;
5115 end;
5116 end if;
5117 end if;
5118 end Expand_Accept_Declarations;
5119
5120 ---------------------------------------------
5121 -- Expand_Access_Protected_Subprogram_Type --
5122 ---------------------------------------------
5123
5124 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5125 Loc : constant Source_Ptr := Sloc (N);
5126 Comps : List_Id;
5127 T : constant Entity_Id := Defining_Identifier (N);
5128 D_T : constant Entity_Id := Designated_Type (T);
5129 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5130 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5131 P_List : constant List_Id := Build_Protected_Spec
5132 (N, RTE (RE_Address), D_T, False);
5133 Decl1 : Node_Id;
5134 Decl2 : Node_Id;
5135 Def1 : Node_Id;
5136
5137 begin
5138 -- Create access to subprogram with full signature
5139
5140 if Etype (D_T) /= Standard_Void_Type then
5141 Def1 :=
5142 Make_Access_Function_Definition (Loc,
5143 Parameter_Specifications => P_List,
5144 Result_Definition =>
5145 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5146
5147 else
5148 Def1 :=
5149 Make_Access_Procedure_Definition (Loc,
5150 Parameter_Specifications => P_List);
5151 end if;
5152
5153 Decl1 :=
5154 Make_Full_Type_Declaration (Loc,
5155 Defining_Identifier => D_T2,
5156 Type_Definition => Def1);
5157
5158 Insert_After (N, Decl1);
5159 Analyze (Decl1);
5160
5161 -- Associate the access to subprogram with its original access to
5162 -- protected subprogram type. Needed by the backend to know that this
5163 -- type corresponds with an access to protected subprogram type.
5164
5165 Set_Original_Access_Type (D_T2, T);
5166
5167 -- Create Equivalent_Type, a record with two components for an access to
5168 -- object and an access to subprogram.
5169
5170 Comps := New_List (
5171 Make_Component_Declaration (Loc,
5172 Defining_Identifier => Make_Temporary (Loc, 'P'),
5173 Component_Definition =>
5174 Make_Component_Definition (Loc,
5175 Aliased_Present => False,
5176 Subtype_Indication =>
5177 New_Occurrence_Of (RTE (RE_Address), Loc))),
5178
5179 Make_Component_Declaration (Loc,
5180 Defining_Identifier => Make_Temporary (Loc, 'S'),
5181 Component_Definition =>
5182 Make_Component_Definition (Loc,
5183 Aliased_Present => False,
5184 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5185
5186 Decl2 :=
5187 Make_Full_Type_Declaration (Loc,
5188 Defining_Identifier => E_T,
5189 Type_Definition =>
5190 Make_Record_Definition (Loc,
5191 Component_List =>
5192 Make_Component_List (Loc,
5193 Component_Items => Comps)));
5194
5195 Insert_After (Decl1, Decl2);
5196 Analyze (Decl2);
5197 Set_Equivalent_Type (T, E_T);
5198 end Expand_Access_Protected_Subprogram_Type;
5199
5200 --------------------------
5201 -- Expand_Entry_Barrier --
5202 --------------------------
5203
5204 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5205 Cond : constant Node_Id :=
5206 Condition (Entry_Body_Formal_Part (N));
5207 Prot : constant Entity_Id := Scope (Ent);
5208 Spec_Decl : constant Node_Id := Parent (Prot);
5209 Func : Node_Id;
5210 B_F : Node_Id;
5211 Body_Decl : Node_Id;
5212
5213 begin
5214 if No_Run_Time_Mode then
5215 Error_Msg_CRT ("entry barrier", N);
5216 return;
5217 end if;
5218
5219 -- The body of the entry barrier must be analyzed in the context of the
5220 -- protected object, but its scope is external to it, just as any other
5221 -- unprotected version of a protected operation. The specification has
5222 -- been produced when the protected type declaration was elaborated. We
5223 -- build the body, insert it in the enclosing scope, but analyze it in
5224 -- the current context. A more uniform approach would be to treat the
5225 -- barrier just as a protected function, and discard the protected
5226 -- version of it because it is never called.
5227
5228 if Full_Expander_Active then
5229 B_F := Build_Barrier_Function (N, Ent, Prot);
5230 Func := Barrier_Function (Ent);
5231 Set_Corresponding_Spec (B_F, Func);
5232
5233 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
5234
5235 if Nkind (Parent (Body_Decl)) = N_Subunit then
5236 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
5237 end if;
5238
5239 Insert_Before_And_Analyze (Body_Decl, B_F);
5240
5241 Set_Discriminals (Spec_Decl);
5242 Set_Scope (Func, Scope (Prot));
5243
5244 else
5245 Analyze_And_Resolve (Cond, Any_Boolean);
5246 end if;
5247
5248 -- The Ravenscar profile restricts barriers to simple variables declared
5249 -- within the protected object. We also allow Boolean constants, since
5250 -- these appear in several published examples and are also allowed by
5251 -- the Aonix compiler.
5252
5253 -- Note that after analysis variables in this context will be replaced
5254 -- by the corresponding prival, that is to say a renaming of a selected
5255 -- component of the form _Object.Var. If expansion is disabled, as
5256 -- within a generic, we check that the entity appears in the current
5257 -- scope.
5258
5259 if Is_Entity_Name (Cond) then
5260
5261 -- A small optimization of useless renamings. If the scope of the
5262 -- entity of the condition is not the barrier function, then the
5263 -- condition does not reference any of the generated renamings
5264 -- within the function.
5265
5266 if Full_Expander_Active
5267 and then Scope (Entity (Cond)) /= Func
5268 then
5269 Set_Declarations (B_F, Empty_List);
5270 end if;
5271
5272 if Entity (Cond) = Standard_False
5273 or else
5274 Entity (Cond) = Standard_True
5275 then
5276 return;
5277
5278 elsif not Expander_Active
5279 and then Scope (Entity (Cond)) = Current_Scope
5280 then
5281 return;
5282
5283 -- Check for case of _object.all.field (note that the explicit
5284 -- dereference gets inserted by analyze/expand of _object.field)
5285
5286 elsif Present (Renamed_Object (Entity (Cond)))
5287 and then
5288 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
5289 and then
5290 Chars
5291 (Prefix
5292 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
5293 then
5294 return;
5295 end if;
5296 end if;
5297
5298 -- It is not a boolean variable or literal, so check the restriction
5299
5300 Check_Restriction (Simple_Barriers, Cond);
5301 end Expand_Entry_Barrier;
5302
5303 ------------------------------
5304 -- Expand_N_Abort_Statement --
5305 ------------------------------
5306
5307 -- Expand abort T1, T2, .. Tn; into:
5308 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
5309
5310 procedure Expand_N_Abort_Statement (N : Node_Id) is
5311 Loc : constant Source_Ptr := Sloc (N);
5312 Tlist : constant List_Id := Names (N);
5313 Count : Nat;
5314 Aggr : Node_Id;
5315 Tasknm : Node_Id;
5316
5317 begin
5318 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
5319 Count := 0;
5320
5321 Tasknm := First (Tlist);
5322
5323 while Present (Tasknm) loop
5324 Count := Count + 1;
5325
5326 -- A task interface class-wide type object is being aborted.
5327 -- Retrieve its _task_id by calling a dispatching routine.
5328
5329 if Ada_Version >= Ada_2005
5330 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
5331 and then Is_Interface (Etype (Tasknm))
5332 and then Is_Task_Interface (Etype (Tasknm))
5333 then
5334 Append_To (Component_Associations (Aggr),
5335 Make_Component_Association (Loc,
5336 Choices => New_List (
5337 Make_Integer_Literal (Loc, Count)),
5338 Expression =>
5339
5340 -- Task_Id (Tasknm._disp_get_task_id)
5341
5342 Make_Unchecked_Type_Conversion (Loc,
5343 Subtype_Mark =>
5344 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
5345 Expression =>
5346 Make_Selected_Component (Loc,
5347 Prefix => New_Copy_Tree (Tasknm),
5348 Selector_Name =>
5349 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
5350
5351 else
5352 Append_To (Component_Associations (Aggr),
5353 Make_Component_Association (Loc,
5354 Choices => New_List (
5355 Make_Integer_Literal (Loc, Count)),
5356 Expression => Concurrent_Ref (Tasknm)));
5357 end if;
5358
5359 Next (Tasknm);
5360 end loop;
5361
5362 Rewrite (N,
5363 Make_Procedure_Call_Statement (Loc,
5364 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
5365 Parameter_Associations => New_List (
5366 Make_Qualified_Expression (Loc,
5367 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
5368 Expression => Aggr))));
5369
5370 Analyze (N);
5371 end Expand_N_Abort_Statement;
5372
5373 -------------------------------
5374 -- Expand_N_Accept_Statement --
5375 -------------------------------
5376
5377 -- This procedure handles expansion of accept statements that stand
5378 -- alone, i.e. they are not part of an accept alternative. The expansion
5379 -- of accept statement in accept alternatives is handled by the routines
5380 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
5381 -- following description applies only to stand alone accept statements.
5382
5383 -- If there is no handled statement sequence, or only null statements,
5384 -- then this is called a trivial accept, and the expansion is:
5385
5386 -- Accept_Trivial (entry-index)
5387
5388 -- If there is a handled statement sequence, then the expansion is:
5389
5390 -- Ann : Address;
5391 -- {Lnn : Label}
5392
5393 -- begin
5394 -- begin
5395 -- Accept_Call (entry-index, Ann);
5396 -- Renaming_Declarations for formals
5397 -- <statement sequence from N_Accept_Statement node>
5398 -- Complete_Rendezvous;
5399 -- <<Lnn>>
5400 --
5401 -- exception
5402 -- when ... =>
5403 -- <exception handler from N_Accept_Statement node>
5404 -- Complete_Rendezvous;
5405 -- when ... =>
5406 -- <exception handler from N_Accept_Statement node>
5407 -- Complete_Rendezvous;
5408 -- ...
5409 -- end;
5410
5411 -- exception
5412 -- when all others =>
5413 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5414 -- end;
5415
5416 -- The first three declarations were already inserted ahead of the accept
5417 -- statement by the Expand_Accept_Declarations procedure, which was called
5418 -- directly from the semantics during analysis of the accept statement,
5419 -- before analyzing its contained statements.
5420
5421 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
5422 -- from possible expansion activity (the original source of course does
5423 -- not have any declarations associated with the accept statement, since
5424 -- an accept statement has no declarative part). In particular, if the
5425 -- expander is active, the first such declaration is the declaration of
5426 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
5427 --
5428 -- The two blocks are merged into a single block if the inner block has
5429 -- no exception handlers, but otherwise two blocks are required, since
5430 -- exceptions might be raised in the exception handlers of the inner
5431 -- block, and Exceptional_Complete_Rendezvous must be called.
5432
5433 procedure Expand_N_Accept_Statement (N : Node_Id) is
5434 Loc : constant Source_Ptr := Sloc (N);
5435 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5436 Ename : constant Node_Id := Entry_Direct_Name (N);
5437 Eindx : constant Node_Id := Entry_Index (N);
5438 Eent : constant Entity_Id := Entity (Ename);
5439 Acstack : constant Elist_Id := Accept_Address (Eent);
5440 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
5441 Ttyp : constant Entity_Id := Etype (Scope (Eent));
5442 Blkent : Entity_Id;
5443 Call : Node_Id;
5444 Block : Node_Id;
5445
5446 -- Start of processing for Expand_N_Accept_Statement
5447
5448 begin
5449 -- If accept statement is not part of a list, then its parent must be
5450 -- an accept alternative, and, as described above, we do not do any
5451 -- expansion for such accept statements at this level.
5452
5453 if not Is_List_Member (N) then
5454 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
5455 return;
5456
5457 -- Trivial accept case (no statement sequence, or null statements).
5458 -- If the accept statement has declarations, then just insert them
5459 -- before the procedure call.
5460
5461 elsif Trivial_Accept_OK
5462 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5463 then
5464 -- Remove declarations for renamings, because the parameter block
5465 -- will not be assigned.
5466
5467 declare
5468 D : Node_Id;
5469 Next_D : Node_Id;
5470
5471 begin
5472 D := First (Declarations (N));
5473
5474 while Present (D) loop
5475 Next_D := Next (D);
5476 if Nkind (D) = N_Object_Renaming_Declaration then
5477 Remove (D);
5478 end if;
5479
5480 D := Next_D;
5481 end loop;
5482 end;
5483
5484 if Present (Declarations (N)) then
5485 Insert_Actions (N, Declarations (N));
5486 end if;
5487
5488 Rewrite (N,
5489 Make_Procedure_Call_Statement (Loc,
5490 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
5491 Parameter_Associations => New_List (
5492 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
5493
5494 Analyze (N);
5495
5496 -- Discard Entry_Address that was created for it, so it will not be
5497 -- emitted if this accept statement is in the statement part of a
5498 -- delay alternative.
5499
5500 if Present (Stats) then
5501 Remove_Last_Elmt (Acstack);
5502 end if;
5503
5504 -- Case of statement sequence present
5505
5506 else
5507 -- Construct the block, using the declarations from the accept
5508 -- statement if any to initialize the declarations of the block.
5509
5510 Blkent := Make_Temporary (Loc, 'A');
5511 Set_Ekind (Blkent, E_Block);
5512 Set_Etype (Blkent, Standard_Void_Type);
5513 Set_Scope (Blkent, Current_Scope);
5514
5515 Block :=
5516 Make_Block_Statement (Loc,
5517 Identifier => New_Reference_To (Blkent, Loc),
5518 Declarations => Declarations (N),
5519 Handled_Statement_Sequence => Build_Accept_Body (N));
5520
5521 -- For the analysis of the generated declarations, the parent node
5522 -- must be properly set.
5523
5524 Set_Parent (Block, Parent (N));
5525
5526 -- Prepend call to Accept_Call to main statement sequence If the
5527 -- accept has exception handlers, the statement sequence is wrapped
5528 -- in a block. Insert call and renaming declarations in the
5529 -- declarations of the block, so they are elaborated before the
5530 -- handlers.
5531
5532 Call :=
5533 Make_Procedure_Call_Statement (Loc,
5534 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
5535 Parameter_Associations => New_List (
5536 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
5537 New_Reference_To (Ann, Loc)));
5538
5539 if Parent (Stats) = N then
5540 Prepend (Call, Statements (Stats));
5541 else
5542 Set_Declarations
5543 (Parent (Stats),
5544 New_List (Call));
5545 end if;
5546
5547 Analyze (Call);
5548
5549 Push_Scope (Blkent);
5550
5551 declare
5552 D : Node_Id;
5553 Next_D : Node_Id;
5554 Typ : Entity_Id;
5555
5556 begin
5557 D := First (Declarations (N));
5558 while Present (D) loop
5559 Next_D := Next (D);
5560
5561 if Nkind (D) = N_Object_Renaming_Declaration then
5562
5563 -- The renaming declarations for the formals were created
5564 -- during analysis of the accept statement, and attached to
5565 -- the list of declarations. Place them now in the context
5566 -- of the accept block or subprogram.
5567
5568 Remove (D);
5569 Typ := Entity (Subtype_Mark (D));
5570 Insert_After (Call, D);
5571 Analyze (D);
5572
5573 -- If the formal is class_wide, it does not have an actual
5574 -- subtype. The analysis of the renaming declaration creates
5575 -- one, but we need to retain the class-wide nature of the
5576 -- entity.
5577
5578 if Is_Class_Wide_Type (Typ) then
5579 Set_Etype (Defining_Identifier (D), Typ);
5580 end if;
5581
5582 end if;
5583
5584 D := Next_D;
5585 end loop;
5586 end;
5587
5588 End_Scope;
5589
5590 -- Replace the accept statement by the new block
5591
5592 Rewrite (N, Block);
5593 Analyze (N);
5594
5595 -- Last step is to unstack the Accept_Address value
5596
5597 Remove_Last_Elmt (Acstack);
5598 end if;
5599 end Expand_N_Accept_Statement;
5600
5601 ----------------------------------
5602 -- Expand_N_Asynchronous_Select --
5603 ----------------------------------
5604
5605 -- This procedure assumes that the trigger statement is an entry call or
5606 -- a dispatching procedure call. A delay alternative should already have
5607 -- been expanded into an entry call to the appropriate delay object Wait
5608 -- entry.
5609
5610 -- If the trigger is a task entry call, the select is implemented with
5611 -- a Task_Entry_Call:
5612
5613 -- declare
5614 -- B : Boolean;
5615 -- C : Boolean;
5616 -- P : parms := (parm, parm, parm);
5617
5618 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5619
5620 -- procedure _clean is
5621 -- begin
5622 -- ...
5623 -- Cancel_Task_Entry_Call (C);
5624 -- ...
5625 -- end _clean;
5626
5627 -- begin
5628 -- Abort_Defer;
5629 -- Task_Entry_Call
5630 -- (<acceptor-task>, -- Acceptor
5631 -- <entry-index>, -- E
5632 -- P'Address, -- Uninterpreted_Data
5633 -- Asynchronous_Call, -- Mode
5634 -- B); -- Rendezvous_Successful
5635
5636 -- begin
5637 -- begin
5638 -- Abort_Undefer;
5639 -- <abortable-part>
5640 -- at end
5641 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5642 -- end;
5643 -- exception
5644 -- when Abort_Signal => Abort_Undefer;
5645 -- end;
5646
5647 -- parm := P.param;
5648 -- parm := P.param;
5649 -- ...
5650 -- if not C then
5651 -- <triggered-statements>
5652 -- end if;
5653 -- end;
5654
5655 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
5656 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
5657 -- as follows:
5658
5659 -- declare
5660 -- P : parms := (parm, parm, parm);
5661 -- begin
5662 -- Call_Simple (acceptor-task, entry-index, P'Address);
5663 -- parm := P.param;
5664 -- parm := P.param;
5665 -- ...
5666 -- end;
5667
5668 -- so the task at hand is to convert the latter expansion into the former
5669
5670 -- If the trigger is a protected entry call, the select is implemented
5671 -- with Protected_Entry_Call:
5672
5673 -- declare
5674 -- P : E1_Params := (param, param, param);
5675 -- Bnn : Communications_Block;
5676
5677 -- begin
5678 -- declare
5679
5680 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5681
5682 -- procedure _clean is
5683 -- begin
5684 -- ...
5685 -- if Enqueued (Bnn) then
5686 -- Cancel_Protected_Entry_Call (Bnn);
5687 -- end if;
5688 -- ...
5689 -- end _clean;
5690
5691 -- begin
5692 -- begin
5693 -- Protected_Entry_Call
5694 -- (po._object'Access, -- Object
5695 -- <entry index>, -- E
5696 -- P'Address, -- Uninterpreted_Data
5697 -- Asynchronous_Call, -- Mode
5698 -- Bnn); -- Block
5699
5700 -- if Enqueued (Bnn) then
5701 -- <abortable-part>
5702 -- end if;
5703 -- at end
5704 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5705 -- end;
5706 -- exception
5707 -- when Abort_Signal => Abort_Undefer;
5708 -- end;
5709
5710 -- if not Cancelled (Bnn) then
5711 -- <triggered-statements>
5712 -- end if;
5713 -- end;
5714
5715 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
5716 -- entry call:
5717
5718 -- declare
5719 -- P : E1_Params := (param, param, param);
5720 -- Bnn : Communications_Block;
5721
5722 -- begin
5723 -- Protected_Entry_Call
5724 -- (po._object'Access, -- Object
5725 -- <entry index>, -- E
5726 -- P'Address, -- Uninterpreted_Data
5727 -- Simple_Call, -- Mode
5728 -- Bnn); -- Block
5729 -- parm := P.param;
5730 -- parm := P.param;
5731 -- ...
5732 -- end;
5733
5734 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
5735 -- expanded into:
5736
5737 -- declare
5738 -- B : Boolean := False;
5739 -- Bnn : Communication_Block;
5740 -- C : Ada.Tags.Prim_Op_Kind;
5741 -- D : System.Storage_Elements.Dummy_Communication_Block;
5742 -- K : Ada.Tags.Tagged_Kind :=
5743 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
5744 -- P : Parameters := (Param1 .. ParamN);
5745 -- S : Integer;
5746 -- U : Boolean;
5747
5748 -- begin
5749 -- if K = Ada.Tags.TK_Limited_Tagged then
5750 -- <dispatching-call>;
5751 -- <triggering-statements>;
5752
5753 -- else
5754 -- S :=
5755 -- Ada.Tags.Get_Offset_Index
5756 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
5757
5758 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
5759
5760 -- if C = POK_Protected_Entry then
5761 -- declare
5762 -- procedure _clean is
5763 -- begin
5764 -- if Enqueued (Bnn) then
5765 -- Cancel_Protected_Entry_Call (Bnn);
5766 -- end if;
5767 -- end _clean;
5768
5769 -- begin
5770 -- begin
5771 -- _Disp_Asynchronous_Select
5772 -- (<object>, S, P'Address, D, B);
5773 -- Bnn := Communication_Block (D);
5774
5775 -- Param1 := P.Param1;
5776 -- ...
5777 -- ParamN := P.ParamN;
5778
5779 -- if Enqueued (Bnn) then
5780 -- <abortable-statements>
5781 -- end if;
5782 -- at end
5783 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5784 -- end;
5785 -- exception
5786 -- when Abort_Signal => Abort_Undefer;
5787 -- end;
5788
5789 -- if not Cancelled (Bnn) then
5790 -- <triggering-statements>
5791 -- end if;
5792
5793 -- elsif C = POK_Task_Entry then
5794 -- declare
5795 -- procedure _clean is
5796 -- begin
5797 -- Cancel_Task_Entry_Call (U);
5798 -- end _clean;
5799
5800 -- begin
5801 -- Abort_Defer;
5802
5803 -- _Disp_Asynchronous_Select
5804 -- (<object>, S, P'Address, D, B);
5805 -- Bnn := Communication_Bloc (D);
5806
5807 -- Param1 := P.Param1;
5808 -- ...
5809 -- ParamN := P.ParamN;
5810
5811 -- begin
5812 -- begin
5813 -- Abort_Undefer;
5814 -- <abortable-statements>
5815 -- at end
5816 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5817 -- end;
5818 -- exception
5819 -- when Abort_Signal => Abort_Undefer;
5820 -- end;
5821
5822 -- if not U then
5823 -- <triggering-statements>
5824 -- end if;
5825 -- end;
5826
5827 -- else
5828 -- <dispatching-call>;
5829 -- <triggering-statements>
5830 -- end if;
5831 -- end if;
5832 -- end;
5833
5834 -- The job is to convert this to the asynchronous form
5835
5836 -- If the trigger is a delay statement, it will have been expanded into a
5837 -- call to one of the GNARL delay procedures. This routine will convert
5838 -- this into a protected entry call on a delay object and then continue
5839 -- processing as for a protected entry call trigger. This requires
5840 -- declaring a Delay_Block object and adding a pointer to this object to
5841 -- the parameter list of the delay procedure to form the parameter list of
5842 -- the entry call. This object is used by the runtime to queue the delay
5843 -- request.
5844
5845 -- For a description of the use of P and the assignments after the call,
5846 -- see Expand_N_Entry_Call_Statement.
5847
5848 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
5849 Loc : constant Source_Ptr := Sloc (N);
5850 Abrt : constant Node_Id := Abortable_Part (N);
5851 Astats : constant List_Id := Statements (Abrt);
5852 Trig : constant Node_Id := Triggering_Alternative (N);
5853 Tstats : constant List_Id := Statements (Trig);
5854
5855 Abort_Block_Ent : Entity_Id;
5856 Abortable_Block : Node_Id;
5857 Actuals : List_Id;
5858 Blk_Ent : Entity_Id;
5859 Blk_Typ : Entity_Id;
5860 Call : Node_Id;
5861 Call_Ent : Entity_Id;
5862 Cancel_Param : Entity_Id;
5863 Cleanup_Block : Node_Id;
5864 Cleanup_Block_Ent : Entity_Id;
5865 Cleanup_Stmts : List_Id;
5866 Conc_Typ_Stmts : List_Id;
5867 Concval : Node_Id;
5868 Dblock_Ent : Entity_Id;
5869 Decl : Node_Id;
5870 Decls : List_Id;
5871 Ecall : Node_Id;
5872 Ename : Node_Id;
5873 Enqueue_Call : Node_Id;
5874 Formals : List_Id;
5875 Hdle : List_Id;
5876 Handler_Stmt : Node_Id;
5877 Index : Node_Id;
5878 Lim_Typ_Stmts : List_Id;
5879 N_Orig : Node_Id;
5880 Obj : Entity_Id;
5881 Param : Node_Id;
5882 Params : List_Id;
5883 Pdef : Entity_Id;
5884 ProtE_Stmts : List_Id;
5885 ProtP_Stmts : List_Id;
5886 Stmt : Node_Id;
5887 Stmts : List_Id;
5888 TaskE_Stmts : List_Id;
5889
5890 B : Entity_Id; -- Call status flag
5891 Bnn : Entity_Id; -- Communication block
5892 C : Entity_Id; -- Call kind
5893 K : Entity_Id; -- Tagged kind
5894 P : Entity_Id; -- Parameter block
5895 S : Entity_Id; -- Primitive operation slot
5896 T : Entity_Id; -- Additional status flag
5897
5898 begin
5899 Process_Statements_For_Controlled_Objects (Trig);
5900 Process_Statements_For_Controlled_Objects (Abrt);
5901
5902 Blk_Ent := Make_Temporary (Loc, 'A');
5903 Ecall := Triggering_Statement (Trig);
5904
5905 -- The arguments in the call may require dynamic allocation, and the
5906 -- call statement may have been transformed into a block. The block
5907 -- may contain additional declarations for internal entities, and the
5908 -- original call is found by sequential search.
5909
5910 if Nkind (Ecall) = N_Block_Statement then
5911 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
5912 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
5913 N_Entry_Call_Statement)
5914 loop
5915 Next (Ecall);
5916 end loop;
5917 end if;
5918
5919 -- This is either a dispatching call or a delay statement used as a
5920 -- trigger which was expanded into a procedure call.
5921
5922 if Nkind (Ecall) = N_Procedure_Call_Statement then
5923 if Ada_Version >= Ada_2005
5924 and then
5925 (No (Original_Node (Ecall))
5926 or else not Nkind_In (Original_Node (Ecall),
5927 N_Delay_Relative_Statement,
5928 N_Delay_Until_Statement))
5929 then
5930 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
5931
5932 Decls := New_List;
5933 Stmts := New_List;
5934
5935 -- Call status flag processing, generate:
5936 -- B : Boolean := False;
5937
5938 B := Build_B (Loc, Decls);
5939
5940 -- Communication block processing, generate:
5941 -- Bnn : Communication_Block;
5942
5943 Bnn := Make_Temporary (Loc, 'B');
5944 Append_To (Decls,
5945 Make_Object_Declaration (Loc,
5946 Defining_Identifier => Bnn,
5947 Object_Definition =>
5948 New_Reference_To (RTE (RE_Communication_Block), Loc)));
5949
5950 -- Call kind processing, generate:
5951 -- C : Ada.Tags.Prim_Op_Kind;
5952
5953 C := Build_C (Loc, Decls);
5954
5955 -- Tagged kind processing, generate:
5956 -- K : Ada.Tags.Tagged_Kind :=
5957 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
5958
5959 -- Dummy communication block, generate:
5960 -- D : Dummy_Communication_Block;
5961
5962 Append_To (Decls,
5963 Make_Object_Declaration (Loc,
5964 Defining_Identifier =>
5965 Make_Defining_Identifier (Loc, Name_uD),
5966 Object_Definition =>
5967 New_Reference_To (
5968 RTE (RE_Dummy_Communication_Block), Loc)));
5969
5970 K := Build_K (Loc, Decls, Obj);
5971
5972 -- Parameter block processing
5973
5974 Blk_Typ := Build_Parameter_Block
5975 (Loc, Actuals, Formals, Decls);
5976 P := Parameter_Block_Pack
5977 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
5978
5979 -- Dispatch table slot processing, generate:
5980 -- S : Integer;
5981
5982 S := Build_S (Loc, Decls);
5983
5984 -- Additional status flag processing, generate:
5985 -- Tnn : Boolean;
5986
5987 T := Make_Temporary (Loc, 'T');
5988 Append_To (Decls,
5989 Make_Object_Declaration (Loc,
5990 Defining_Identifier => T,
5991 Object_Definition =>
5992 New_Reference_To (Standard_Boolean, Loc)));
5993
5994 ------------------------------
5995 -- Protected entry handling --
5996 ------------------------------
5997
5998 -- Generate:
5999 -- Param1 := P.Param1;
6000 -- ...
6001 -- ParamN := P.ParamN;
6002
6003 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6004
6005 -- Generate:
6006 -- Bnn := Communication_Block (D);
6007
6008 Prepend_To (Cleanup_Stmts,
6009 Make_Assignment_Statement (Loc,
6010 Name =>
6011 New_Reference_To (Bnn, Loc),
6012 Expression =>
6013 Make_Unchecked_Type_Conversion (Loc,
6014 Subtype_Mark =>
6015 New_Reference_To (RTE (RE_Communication_Block), Loc),
6016 Expression => Make_Identifier (Loc, Name_uD))));
6017
6018 -- Generate:
6019 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6020
6021 Prepend_To (Cleanup_Stmts,
6022 Make_Procedure_Call_Statement (Loc,
6023 Name =>
6024 New_Reference_To (
6025 Find_Prim_Op (Etype (Etype (Obj)),
6026 Name_uDisp_Asynchronous_Select),
6027 Loc),
6028 Parameter_Associations =>
6029 New_List (
6030 New_Copy_Tree (Obj), -- <object>
6031 New_Reference_To (S, Loc), -- S
6032 Make_Attribute_Reference (Loc, -- P'Address
6033 Prefix =>
6034 New_Reference_To (P, Loc),
6035 Attribute_Name =>
6036 Name_Address),
6037 Make_Identifier (Loc, Name_uD), -- D
6038 New_Reference_To (B, Loc)))); -- B
6039
6040 -- Generate:
6041 -- if Enqueued (Bnn) then
6042 -- <abortable-statements>
6043 -- end if;
6044
6045 Append_To (Cleanup_Stmts,
6046 Make_If_Statement (Loc,
6047 Condition =>
6048 Make_Function_Call (Loc,
6049 Name =>
6050 New_Reference_To (RTE (RE_Enqueued), Loc),
6051 Parameter_Associations =>
6052 New_List (
6053 New_Reference_To (Bnn, Loc))),
6054
6055 Then_Statements =>
6056 New_Copy_List_Tree (Astats)));
6057
6058 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6059 -- will then generate a _clean for the communication block Bnn.
6060
6061 -- Generate:
6062 -- declare
6063 -- procedure _clean is
6064 -- begin
6065 -- if Enqueued (Bnn) then
6066 -- Cancel_Protected_Entry_Call (Bnn);
6067 -- end if;
6068 -- end _clean;
6069 -- begin
6070 -- Cleanup_Stmts
6071 -- at end
6072 -- _clean;
6073 -- end;
6074
6075 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6076 Cleanup_Block :=
6077 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
6078
6079 -- Wrap the cleanup block in an exception handling block
6080
6081 -- Generate:
6082 -- begin
6083 -- Cleanup_Block
6084 -- exception
6085 -- when Abort_Signal => Abort_Undefer;
6086 -- end;
6087
6088 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6089 ProtE_Stmts :=
6090 New_List (
6091 Make_Implicit_Label_Declaration (Loc,
6092 Defining_Identifier =>
6093 Abort_Block_Ent),
6094
6095 Build_Abort_Block
6096 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6097
6098 -- Generate:
6099 -- if not Cancelled (Bnn) then
6100 -- <triggering-statements>
6101 -- end if;
6102
6103 Append_To (ProtE_Stmts,
6104 Make_If_Statement (Loc,
6105 Condition =>
6106 Make_Op_Not (Loc,
6107 Right_Opnd =>
6108 Make_Function_Call (Loc,
6109 Name =>
6110 New_Reference_To (RTE (RE_Cancelled), Loc),
6111 Parameter_Associations =>
6112 New_List (
6113 New_Reference_To (Bnn, Loc)))),
6114
6115 Then_Statements =>
6116 New_Copy_List_Tree (Tstats)));
6117
6118 -------------------------
6119 -- Task entry handling --
6120 -------------------------
6121
6122 -- Generate:
6123 -- Param1 := P.Param1;
6124 -- ...
6125 -- ParamN := P.ParamN;
6126
6127 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6128
6129 -- Generate:
6130 -- Bnn := Communication_Block (D);
6131
6132 Append_To (TaskE_Stmts,
6133 Make_Assignment_Statement (Loc,
6134 Name =>
6135 New_Reference_To (Bnn, Loc),
6136 Expression =>
6137 Make_Unchecked_Type_Conversion (Loc,
6138 Subtype_Mark =>
6139 New_Reference_To (RTE (RE_Communication_Block), Loc),
6140 Expression => Make_Identifier (Loc, Name_uD))));
6141
6142 -- Generate:
6143 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6144
6145 Prepend_To (TaskE_Stmts,
6146 Make_Procedure_Call_Statement (Loc,
6147 Name =>
6148 New_Reference_To (
6149 Find_Prim_Op (Etype (Etype (Obj)),
6150 Name_uDisp_Asynchronous_Select),
6151 Loc),
6152 Parameter_Associations =>
6153 New_List (
6154 New_Copy_Tree (Obj), -- <object>
6155 New_Reference_To (S, Loc), -- S
6156 Make_Attribute_Reference (Loc, -- P'Address
6157 Prefix =>
6158 New_Reference_To (P, Loc),
6159 Attribute_Name =>
6160 Name_Address),
6161 Make_Identifier (Loc, Name_uD), -- D
6162 New_Reference_To (B, Loc)))); -- B
6163
6164 -- Generate:
6165 -- Abort_Defer;
6166
6167 Prepend_To (TaskE_Stmts,
6168 Make_Procedure_Call_Statement (Loc,
6169 Name =>
6170 New_Reference_To (RTE (RE_Abort_Defer), Loc),
6171 Parameter_Associations =>
6172 No_List));
6173
6174 -- Generate:
6175 -- Abort_Undefer;
6176 -- <abortable-statements>
6177
6178 Cleanup_Stmts := New_Copy_List_Tree (Astats);
6179
6180 Prepend_To (Cleanup_Stmts,
6181 Make_Procedure_Call_Statement (Loc,
6182 Name =>
6183 New_Reference_To (RTE (RE_Abort_Undefer), Loc),
6184 Parameter_Associations =>
6185 No_List));
6186
6187 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6188 -- will generate a _clean for the additional status flag.
6189
6190 -- Generate:
6191 -- declare
6192 -- procedure _clean is
6193 -- begin
6194 -- Cancel_Task_Entry_Call (U);
6195 -- end _clean;
6196 -- begin
6197 -- Cleanup_Stmts
6198 -- at end
6199 -- _clean;
6200 -- end;
6201
6202 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6203 Cleanup_Block :=
6204 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
6205
6206 -- Wrap the cleanup block in an exception handling block
6207
6208 -- Generate:
6209 -- begin
6210 -- Cleanup_Block
6211 -- exception
6212 -- when Abort_Signal => Abort_Undefer;
6213 -- end;
6214
6215 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6216
6217 Append_To (TaskE_Stmts,
6218 Make_Implicit_Label_Declaration (Loc,
6219 Defining_Identifier => Abort_Block_Ent));
6220
6221 Append_To (TaskE_Stmts,
6222 Build_Abort_Block
6223 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6224
6225 -- Generate:
6226 -- if not T then
6227 -- <triggering-statements>
6228 -- end if;
6229
6230 Append_To (TaskE_Stmts,
6231 Make_If_Statement (Loc,
6232 Condition =>
6233 Make_Op_Not (Loc,
6234 Right_Opnd =>
6235 New_Reference_To (T, Loc)),
6236
6237 Then_Statements =>
6238 New_Copy_List_Tree (Tstats)));
6239
6240 ----------------------------------
6241 -- Protected procedure handling --
6242 ----------------------------------
6243
6244 -- Generate:
6245 -- <dispatching-call>;
6246 -- <triggering-statements>
6247
6248 ProtP_Stmts := New_Copy_List_Tree (Tstats);
6249 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
6250
6251 -- Generate:
6252 -- S := Ada.Tags.Get_Offset_Index
6253 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
6254
6255 Conc_Typ_Stmts :=
6256 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
6257
6258 -- Generate:
6259 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6260
6261 Append_To (Conc_Typ_Stmts,
6262 Make_Procedure_Call_Statement (Loc,
6263 Name =>
6264 New_Reference_To (
6265 Find_Prim_Op (Etype (Etype (Obj)),
6266 Name_uDisp_Get_Prim_Op_Kind),
6267 Loc),
6268 Parameter_Associations =>
6269 New_List (
6270 New_Copy_Tree (Obj),
6271 New_Reference_To (S, Loc),
6272 New_Reference_To (C, Loc))));
6273
6274 -- Generate:
6275 -- if C = POK_Procedure_Entry then
6276 -- ProtE_Stmts
6277 -- elsif C = POK_Task_Entry then
6278 -- TaskE_Stmts
6279 -- else
6280 -- ProtP_Stmts
6281 -- end if;
6282
6283 Append_To (Conc_Typ_Stmts,
6284 Make_If_Statement (Loc,
6285 Condition =>
6286 Make_Op_Eq (Loc,
6287 Left_Opnd =>
6288 New_Reference_To (C, Loc),
6289 Right_Opnd =>
6290 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
6291
6292 Then_Statements =>
6293 ProtE_Stmts,
6294
6295 Elsif_Parts =>
6296 New_List (
6297 Make_Elsif_Part (Loc,
6298 Condition =>
6299 Make_Op_Eq (Loc,
6300 Left_Opnd =>
6301 New_Reference_To (C, Loc),
6302 Right_Opnd =>
6303 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
6304
6305 Then_Statements =>
6306 TaskE_Stmts)),
6307
6308 Else_Statements =>
6309 ProtP_Stmts));
6310
6311 -- Generate:
6312 -- <dispatching-call>;
6313 -- <triggering-statements>
6314
6315 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
6316 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
6317
6318 -- Generate:
6319 -- if K = Ada.Tags.TK_Limited_Tagged then
6320 -- Lim_Typ_Stmts
6321 -- else
6322 -- Conc_Typ_Stmts
6323 -- end if;
6324
6325 Append_To (Stmts,
6326 Make_If_Statement (Loc,
6327 Condition =>
6328 Make_Op_Eq (Loc,
6329 Left_Opnd =>
6330 New_Reference_To (K, Loc),
6331 Right_Opnd =>
6332 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
6333
6334 Then_Statements =>
6335 Lim_Typ_Stmts,
6336
6337 Else_Statements =>
6338 Conc_Typ_Stmts));
6339
6340 Rewrite (N,
6341 Make_Block_Statement (Loc,
6342 Declarations =>
6343 Decls,
6344 Handled_Statement_Sequence =>
6345 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6346
6347 Analyze (N);
6348 return;
6349
6350 -- Delay triggering statement processing
6351
6352 else
6353 -- Add a Delay_Block object to the parameter list of the delay
6354 -- procedure to form the parameter list of the Wait entry call.
6355
6356 Dblock_Ent := Make_Temporary (Loc, 'D');
6357
6358 Pdef := Entity (Name (Ecall));
6359
6360 if Is_RTE (Pdef, RO_CA_Delay_For) then
6361 Enqueue_Call :=
6362 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
6363
6364 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
6365 Enqueue_Call :=
6366 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
6367
6368 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
6369 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
6370 end if;
6371
6372 Append_To (Parameter_Associations (Ecall),
6373 Make_Attribute_Reference (Loc,
6374 Prefix => New_Reference_To (Dblock_Ent, Loc),
6375 Attribute_Name => Name_Unchecked_Access));
6376
6377 -- Create the inner block to protect the abortable part
6378
6379 Hdle := New_List (Build_Abort_Block_Handler (Loc));
6380
6381 Prepend_To (Astats,
6382 Make_Procedure_Call_Statement (Loc,
6383 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
6384
6385 Abortable_Block :=
6386 Make_Block_Statement (Loc,
6387 Identifier => New_Reference_To (Blk_Ent, Loc),
6388 Handled_Statement_Sequence =>
6389 Make_Handled_Sequence_Of_Statements (Loc,
6390 Statements => Astats),
6391 Has_Created_Identifier => True,
6392 Is_Asynchronous_Call_Block => True);
6393
6394 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
6395
6396 Rewrite (Ecall,
6397 Make_Implicit_If_Statement (N,
6398 Condition => Make_Function_Call (Loc,
6399 Name => Enqueue_Call,
6400 Parameter_Associations => Parameter_Associations (Ecall)),
6401 Then_Statements =>
6402 New_List (Make_Block_Statement (Loc,
6403 Handled_Statement_Sequence =>
6404 Make_Handled_Sequence_Of_Statements (Loc,
6405 Statements => New_List (
6406 Make_Implicit_Label_Declaration (Loc,
6407 Defining_Identifier => Blk_Ent,
6408 Label_Construct => Abortable_Block),
6409 Abortable_Block),
6410 Exception_Handlers => Hdle)))));
6411
6412 Stmts := New_List (Ecall);
6413
6414 -- Construct statement sequence for new block
6415
6416 Append_To (Stmts,
6417 Make_Implicit_If_Statement (N,
6418 Condition => Make_Function_Call (Loc,
6419 Name => New_Reference_To (
6420 RTE (RE_Timed_Out), Loc),
6421 Parameter_Associations => New_List (
6422 Make_Attribute_Reference (Loc,
6423 Prefix => New_Reference_To (Dblock_Ent, Loc),
6424 Attribute_Name => Name_Unchecked_Access))),
6425 Then_Statements => Tstats));
6426
6427 -- The result is the new block
6428
6429 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
6430
6431 Rewrite (N,
6432 Make_Block_Statement (Loc,
6433 Declarations => New_List (
6434 Make_Object_Declaration (Loc,
6435 Defining_Identifier => Dblock_Ent,
6436 Aliased_Present => True,
6437 Object_Definition => New_Reference_To (
6438 RTE (RE_Delay_Block), Loc))),
6439
6440 Handled_Statement_Sequence =>
6441 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6442
6443 Analyze (N);
6444 return;
6445 end if;
6446
6447 else
6448 N_Orig := N;
6449 end if;
6450
6451 Extract_Entry (Ecall, Concval, Ename, Index);
6452 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
6453
6454 Stmts := Statements (Handled_Statement_Sequence (Ecall));
6455 Decls := Declarations (Ecall);
6456
6457 if Is_Protected_Type (Etype (Concval)) then
6458
6459 -- Get the declarations of the block expanded from the entry call
6460
6461 Decl := First (Decls);
6462 while Present (Decl)
6463 and then
6464 (Nkind (Decl) /= N_Object_Declaration
6465 or else not Is_RTE (Etype (Object_Definition (Decl)),
6466 RE_Communication_Block))
6467 loop
6468 Next (Decl);
6469 end loop;
6470
6471 pragma Assert (Present (Decl));
6472 Cancel_Param := Defining_Identifier (Decl);
6473
6474 -- Change the mode of the Protected_Entry_Call call
6475
6476 -- Protected_Entry_Call (
6477 -- Object => po._object'Access,
6478 -- E => <entry index>;
6479 -- Uninterpreted_Data => P'Address;
6480 -- Mode => Asynchronous_Call;
6481 -- Block => Bnn);
6482
6483 Stmt := First (Stmts);
6484
6485 -- Skip assignments to temporaries created for in-out parameters
6486
6487 -- This makes unwarranted assumptions about the shape of the expanded
6488 -- tree for the call, and should be cleaned up ???
6489
6490 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6491 Next (Stmt);
6492 end loop;
6493
6494 Call := Stmt;
6495
6496 Param := First (Parameter_Associations (Call));
6497 while Present (Param)
6498 and then not Is_RTE (Etype (Param), RE_Call_Modes)
6499 loop
6500 Next (Param);
6501 end loop;
6502
6503 pragma Assert (Present (Param));
6504 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
6505 Analyze (Param);
6506
6507 -- Append an if statement to execute the abortable part
6508
6509 -- Generate:
6510 -- if Enqueued (Bnn) then
6511
6512 Append_To (Stmts,
6513 Make_Implicit_If_Statement (N,
6514 Condition => Make_Function_Call (Loc,
6515 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
6516 Parameter_Associations => New_List (
6517 New_Reference_To (Cancel_Param, Loc))),
6518 Then_Statements => Astats));
6519
6520 Abortable_Block :=
6521 Make_Block_Statement (Loc,
6522 Identifier => New_Reference_To (Blk_Ent, Loc),
6523 Handled_Statement_Sequence =>
6524 Make_Handled_Sequence_Of_Statements (Loc,
6525 Statements => Stmts),
6526 Has_Created_Identifier => True,
6527 Is_Asynchronous_Call_Block => True);
6528
6529 -- For the VM call Update_Exception instead of Abort_Undefer.
6530 -- See 4jexcept.ads for an explanation.
6531
6532 if VM_Target = No_VM then
6533 if Exception_Mechanism = Back_End_Exceptions then
6534
6535 -- Aborts are not deferred at beginning of exception handlers
6536 -- in ZCX.
6537
6538 Handler_Stmt := Make_Null_Statement (Loc);
6539
6540 else
6541 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
6542 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
6543 Parameter_Associations => No_List);
6544 end if;
6545 else
6546 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
6547 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
6548 Parameter_Associations => New_List (
6549 Make_Function_Call (Loc,
6550 Name => New_Occurrence_Of
6551 (RTE (RE_Current_Target_Exception), Loc))));
6552 end if;
6553
6554 Stmts := New_List (
6555 Make_Block_Statement (Loc,
6556 Handled_Statement_Sequence =>
6557 Make_Handled_Sequence_Of_Statements (Loc,
6558 Statements => New_List (
6559 Make_Implicit_Label_Declaration (Loc,
6560 Defining_Identifier => Blk_Ent,
6561 Label_Construct => Abortable_Block),
6562 Abortable_Block),
6563
6564 -- exception
6565
6566 Exception_Handlers => New_List (
6567 Make_Implicit_Exception_Handler (Loc,
6568
6569 -- when Abort_Signal =>
6570 -- Abort_Undefer.all;
6571
6572 Exception_Choices =>
6573 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
6574 Statements => New_List (Handler_Stmt))))),
6575
6576 -- if not Cancelled (Bnn) then
6577 -- triggered statements
6578 -- end if;
6579
6580 Make_Implicit_If_Statement (N,
6581 Condition => Make_Op_Not (Loc,
6582 Right_Opnd =>
6583 Make_Function_Call (Loc,
6584 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
6585 Parameter_Associations => New_List (
6586 New_Occurrence_Of (Cancel_Param, Loc)))),
6587 Then_Statements => Tstats));
6588
6589 -- Asynchronous task entry call
6590
6591 else
6592 if No (Decls) then
6593 Decls := New_List;
6594 end if;
6595
6596 B := Make_Defining_Identifier (Loc, Name_uB);
6597
6598 -- Insert declaration of B in declarations of existing block
6599
6600 Prepend_To (Decls,
6601 Make_Object_Declaration (Loc,
6602 Defining_Identifier => B,
6603 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6604
6605 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
6606
6607 -- Insert declaration of C in declarations of existing block
6608
6609 Prepend_To (Decls,
6610 Make_Object_Declaration (Loc,
6611 Defining_Identifier => Cancel_Param,
6612 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6613
6614 -- Remove and save the call to Call_Simple
6615
6616 Stmt := First (Stmts);
6617
6618 -- Skip assignments to temporaries created for in-out parameters.
6619 -- This makes unwarranted assumptions about the shape of the expanded
6620 -- tree for the call, and should be cleaned up ???
6621
6622 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6623 Next (Stmt);
6624 end loop;
6625
6626 Call := Stmt;
6627
6628 -- Create the inner block to protect the abortable part
6629
6630 Hdle := New_List (Build_Abort_Block_Handler (Loc));
6631
6632 Prepend_To (Astats,
6633 Make_Procedure_Call_Statement (Loc,
6634 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
6635
6636 Abortable_Block :=
6637 Make_Block_Statement (Loc,
6638 Identifier => New_Reference_To (Blk_Ent, Loc),
6639 Handled_Statement_Sequence =>
6640 Make_Handled_Sequence_Of_Statements (Loc,
6641 Statements => Astats),
6642 Has_Created_Identifier => True,
6643 Is_Asynchronous_Call_Block => True);
6644
6645 Insert_After (Call,
6646 Make_Block_Statement (Loc,
6647 Handled_Statement_Sequence =>
6648 Make_Handled_Sequence_Of_Statements (Loc,
6649 Statements => New_List (
6650 Make_Implicit_Label_Declaration (Loc,
6651 Defining_Identifier =>
6652 Blk_Ent,
6653 Label_Construct =>
6654 Abortable_Block),
6655 Abortable_Block),
6656 Exception_Handlers => Hdle)));
6657
6658 -- Create new call statement
6659
6660 Params := Parameter_Associations (Call);
6661
6662 Append_To (Params,
6663 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
6664 Append_To (Params,
6665 New_Reference_To (B, Loc));
6666
6667 Rewrite (Call,
6668 Make_Procedure_Call_Statement (Loc,
6669 Name =>
6670 New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
6671 Parameter_Associations => Params));
6672
6673 -- Construct statement sequence for new block
6674
6675 Append_To (Stmts,
6676 Make_Implicit_If_Statement (N,
6677 Condition =>
6678 Make_Op_Not (Loc,
6679 New_Reference_To (Cancel_Param, Loc)),
6680 Then_Statements => Tstats));
6681
6682 -- Protected the call against abort
6683
6684 Prepend_To (Stmts,
6685 Make_Procedure_Call_Statement (Loc,
6686 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
6687 Parameter_Associations => Empty_List));
6688 end if;
6689
6690 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
6691
6692 -- The result is the new block
6693
6694 Rewrite (N_Orig,
6695 Make_Block_Statement (Loc,
6696 Declarations => Decls,
6697 Handled_Statement_Sequence =>
6698 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6699
6700 Analyze (N_Orig);
6701 end Expand_N_Asynchronous_Select;
6702
6703 -------------------------------------
6704 -- Expand_N_Conditional_Entry_Call --
6705 -------------------------------------
6706
6707 -- The conditional task entry call is converted to a call to
6708 -- Task_Entry_Call:
6709
6710 -- declare
6711 -- B : Boolean;
6712 -- P : parms := (parm, parm, parm);
6713
6714 -- begin
6715 -- Task_Entry_Call
6716 -- (<acceptor-task>, -- Acceptor
6717 -- <entry-index>, -- E
6718 -- P'Address, -- Uninterpreted_Data
6719 -- Conditional_Call, -- Mode
6720 -- B); -- Rendezvous_Successful
6721 -- parm := P.param;
6722 -- parm := P.param;
6723 -- ...
6724 -- if B then
6725 -- normal-statements
6726 -- else
6727 -- else-statements
6728 -- end if;
6729 -- end;
6730
6731 -- For a description of the use of P and the assignments after the call,
6732 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
6733 -- conditional entry call has already been expanded (by the Expand_N_Entry
6734 -- _Call_Statement procedure) as follows:
6735
6736 -- declare
6737 -- P : parms := (parm, parm, parm);
6738 -- begin
6739 -- ... info for in-out parameters
6740 -- Call_Simple (acceptor-task, entry-index, P'Address);
6741 -- parm := P.param;
6742 -- parm := P.param;
6743 -- ...
6744 -- end;
6745
6746 -- so the task at hand is to convert the latter expansion into the former
6747
6748 -- The conditional protected entry call is converted to a call to
6749 -- Protected_Entry_Call:
6750
6751 -- declare
6752 -- P : parms := (parm, parm, parm);
6753 -- Bnn : Communications_Block;
6754
6755 -- begin
6756 -- Protected_Entry_Call
6757 -- (po._object'Access, -- Object
6758 -- <entry index>, -- E
6759 -- P'Address, -- Uninterpreted_Data
6760 -- Conditional_Call, -- Mode
6761 -- Bnn); -- Block
6762 -- parm := P.param;
6763 -- parm := P.param;
6764 -- ...
6765 -- if Cancelled (Bnn) then
6766 -- else-statements
6767 -- else
6768 -- normal-statements
6769 -- end if;
6770 -- end;
6771
6772 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
6773 -- into:
6774
6775 -- declare
6776 -- B : Boolean := False;
6777 -- C : Ada.Tags.Prim_Op_Kind;
6778 -- K : Ada.Tags.Tagged_Kind :=
6779 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6780 -- P : Parameters := (Param1 .. ParamN);
6781 -- S : Integer;
6782
6783 -- begin
6784 -- if K = Ada.Tags.TK_Limited_Tagged then
6785 -- <dispatching-call>;
6786 -- <triggering-statements>
6787
6788 -- else
6789 -- S :=
6790 -- Ada.Tags.Get_Offset_Index
6791 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6792
6793 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
6794
6795 -- if C = POK_Protected_Entry
6796 -- or else C = POK_Task_Entry
6797 -- then
6798 -- Param1 := P.Param1;
6799 -- ...
6800 -- ParamN := P.ParamN;
6801 -- end if;
6802
6803 -- if B then
6804 -- if C = POK_Procedure
6805 -- or else C = POK_Protected_Procedure
6806 -- or else C = POK_Task_Procedure
6807 -- then
6808 -- <dispatching-call>;
6809 -- end if;
6810
6811 -- <triggering-statements>
6812 -- else
6813 -- <else-statements>
6814 -- end if;
6815 -- end if;
6816 -- end;
6817
6818 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
6819 Loc : constant Source_Ptr := Sloc (N);
6820 Alt : constant Node_Id := Entry_Call_Alternative (N);
6821 Blk : Node_Id := Entry_Call_Statement (Alt);
6822
6823 Actuals : List_Id;
6824 Blk_Typ : Entity_Id;
6825 Call : Node_Id;
6826 Call_Ent : Entity_Id;
6827 Conc_Typ_Stmts : List_Id;
6828 Decl : Node_Id;
6829 Decls : List_Id;
6830 Formals : List_Id;
6831 Lim_Typ_Stmts : List_Id;
6832 N_Stats : List_Id;
6833 Obj : Entity_Id;
6834 Param : Node_Id;
6835 Params : List_Id;
6836 Stmt : Node_Id;
6837 Stmts : List_Id;
6838 Transient_Blk : Node_Id;
6839 Unpack : List_Id;
6840
6841 B : Entity_Id; -- Call status flag
6842 C : Entity_Id; -- Call kind
6843 K : Entity_Id; -- Tagged kind
6844 P : Entity_Id; -- Parameter block
6845 S : Entity_Id; -- Primitive operation slot
6846
6847 begin
6848 Process_Statements_For_Controlled_Objects (N);
6849
6850 if Ada_Version >= Ada_2005
6851 and then Nkind (Blk) = N_Procedure_Call_Statement
6852 then
6853 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
6854
6855 Decls := New_List;
6856 Stmts := New_List;
6857
6858 -- Call status flag processing, generate:
6859 -- B : Boolean := False;
6860
6861 B := Build_B (Loc, Decls);
6862
6863 -- Call kind processing, generate:
6864 -- C : Ada.Tags.Prim_Op_Kind;
6865
6866 C := Build_C (Loc, Decls);
6867
6868 -- Tagged kind processing, generate:
6869 -- K : Ada.Tags.Tagged_Kind :=
6870 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6871
6872 K := Build_K (Loc, Decls, Obj);
6873
6874 -- Parameter block processing
6875
6876 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
6877 P := Parameter_Block_Pack
6878 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6879
6880 -- Dispatch table slot processing, generate:
6881 -- S : Integer;
6882
6883 S := Build_S (Loc, Decls);
6884
6885 -- Generate:
6886 -- S := Ada.Tags.Get_Offset_Index
6887 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
6888
6889 Conc_Typ_Stmts :=
6890 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
6891
6892 -- Generate:
6893 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
6894
6895 Append_To (Conc_Typ_Stmts,
6896 Make_Procedure_Call_Statement (Loc,
6897 Name =>
6898 New_Reference_To (
6899 Find_Prim_Op (Etype (Etype (Obj)),
6900 Name_uDisp_Conditional_Select),
6901 Loc),
6902 Parameter_Associations =>
6903 New_List (
6904 New_Copy_Tree (Obj), -- <object>
6905 New_Reference_To (S, Loc), -- S
6906 Make_Attribute_Reference (Loc, -- P'Address
6907 Prefix =>
6908 New_Reference_To (P, Loc),
6909 Attribute_Name =>
6910 Name_Address),
6911 New_Reference_To (C, Loc), -- C
6912 New_Reference_To (B, Loc)))); -- B
6913
6914 -- Generate:
6915 -- if C = POK_Protected_Entry
6916 -- or else C = POK_Task_Entry
6917 -- then
6918 -- Param1 := P.Param1;
6919 -- ...
6920 -- ParamN := P.ParamN;
6921 -- end if;
6922
6923 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6924
6925 -- Generate the if statement only when the packed parameters need
6926 -- explicit assignments to their corresponding actuals.
6927
6928 if Present (Unpack) then
6929 Append_To (Conc_Typ_Stmts,
6930 Make_If_Statement (Loc,
6931
6932 Condition =>
6933 Make_Or_Else (Loc,
6934 Left_Opnd =>
6935 Make_Op_Eq (Loc,
6936 Left_Opnd =>
6937 New_Reference_To (C, Loc),
6938 Right_Opnd =>
6939 New_Reference_To (RTE (
6940 RE_POK_Protected_Entry), Loc)),
6941 Right_Opnd =>
6942 Make_Op_Eq (Loc,
6943 Left_Opnd =>
6944 New_Reference_To (C, Loc),
6945 Right_Opnd =>
6946 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
6947
6948 Then_Statements =>
6949 Unpack));
6950 end if;
6951
6952 -- Generate:
6953 -- if B then
6954 -- if C = POK_Procedure
6955 -- or else C = POK_Protected_Procedure
6956 -- or else C = POK_Task_Procedure
6957 -- then
6958 -- <dispatching-call>
6959 -- end if;
6960 -- <normal-statements>
6961 -- else
6962 -- <else-statements>
6963 -- end if;
6964
6965 N_Stats := New_Copy_List_Tree (Statements (Alt));
6966
6967 Prepend_To (N_Stats,
6968 Make_If_Statement (Loc,
6969 Condition =>
6970 Make_Or_Else (Loc,
6971 Left_Opnd =>
6972 Make_Op_Eq (Loc,
6973 Left_Opnd =>
6974 New_Reference_To (C, Loc),
6975 Right_Opnd =>
6976 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
6977
6978 Right_Opnd =>
6979 Make_Or_Else (Loc,
6980 Left_Opnd =>
6981 Make_Op_Eq (Loc,
6982 Left_Opnd =>
6983 New_Reference_To (C, Loc),
6984 Right_Opnd =>
6985 New_Reference_To (RTE (
6986 RE_POK_Protected_Procedure), Loc)),
6987
6988 Right_Opnd =>
6989 Make_Op_Eq (Loc,
6990 Left_Opnd =>
6991 New_Reference_To (C, Loc),
6992 Right_Opnd =>
6993 New_Reference_To (RTE (
6994 RE_POK_Task_Procedure), Loc)))),
6995
6996 Then_Statements =>
6997 New_List (Blk)));
6998
6999 Append_To (Conc_Typ_Stmts,
7000 Make_If_Statement (Loc,
7001 Condition => New_Reference_To (B, Loc),
7002 Then_Statements => N_Stats,
7003 Else_Statements => Else_Statements (N)));
7004
7005 -- Generate:
7006 -- <dispatching-call>;
7007 -- <triggering-statements>
7008
7009 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7010 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7011
7012 -- Generate:
7013 -- if K = Ada.Tags.TK_Limited_Tagged then
7014 -- Lim_Typ_Stmts
7015 -- else
7016 -- Conc_Typ_Stmts
7017 -- end if;
7018
7019 Append_To (Stmts,
7020 Make_If_Statement (Loc,
7021 Condition =>
7022 Make_Op_Eq (Loc,
7023 Left_Opnd =>
7024 New_Reference_To (K, Loc),
7025 Right_Opnd =>
7026 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7027
7028 Then_Statements =>
7029 Lim_Typ_Stmts,
7030
7031 Else_Statements =>
7032 Conc_Typ_Stmts));
7033
7034 Rewrite (N,
7035 Make_Block_Statement (Loc,
7036 Declarations =>
7037 Decls,
7038 Handled_Statement_Sequence =>
7039 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7040
7041 -- As described above, The entry alternative is transformed into a
7042 -- block that contains the gnulli call, and possibly assignment
7043 -- statements for in-out parameters. The gnulli call may itself be
7044 -- rewritten into a transient block if some unconstrained parameters
7045 -- require it. We need to retrieve the call to complete its parameter
7046 -- list.
7047
7048 else
7049 Transient_Blk :=
7050 First_Real_Statement (Handled_Statement_Sequence (Blk));
7051
7052 if Present (Transient_Blk)
7053 and then Nkind (Transient_Blk) = N_Block_Statement
7054 then
7055 Blk := Transient_Blk;
7056 end if;
7057
7058 Stmts := Statements (Handled_Statement_Sequence (Blk));
7059 Stmt := First (Stmts);
7060 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7061 Next (Stmt);
7062 end loop;
7063
7064 Call := Stmt;
7065 Params := Parameter_Associations (Call);
7066
7067 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7068
7069 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7070
7071 Param := First (Params);
7072 while Present (Param)
7073 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7074 loop
7075 Next (Param);
7076 end loop;
7077
7078 pragma Assert (Present (Param));
7079 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
7080
7081 Analyze (Param);
7082
7083 -- Find the Communication_Block parameter for the call to the
7084 -- Cancelled function.
7085
7086 Decl := First (Declarations (Blk));
7087 while Present (Decl)
7088 and then not Is_RTE (Etype (Object_Definition (Decl)),
7089 RE_Communication_Block)
7090 loop
7091 Next (Decl);
7092 end loop;
7093
7094 -- Add an if statement to execute the else part if the call
7095 -- does not succeed (as indicated by the Cancelled predicate).
7096
7097 Append_To (Stmts,
7098 Make_Implicit_If_Statement (N,
7099 Condition => Make_Function_Call (Loc,
7100 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
7101 Parameter_Associations => New_List (
7102 New_Reference_To (Defining_Identifier (Decl), Loc))),
7103 Then_Statements => Else_Statements (N),
7104 Else_Statements => Statements (Alt)));
7105
7106 else
7107 B := Make_Defining_Identifier (Loc, Name_uB);
7108
7109 -- Insert declaration of B in declarations of existing block
7110
7111 if No (Declarations (Blk)) then
7112 Set_Declarations (Blk, New_List);
7113 end if;
7114
7115 Prepend_To (Declarations (Blk),
7116 Make_Object_Declaration (Loc,
7117 Defining_Identifier => B,
7118 Object_Definition =>
7119 New_Reference_To (Standard_Boolean, Loc)));
7120
7121 -- Create new call statement
7122
7123 Append_To (Params,
7124 New_Reference_To (RTE (RE_Conditional_Call), Loc));
7125 Append_To (Params, New_Reference_To (B, Loc));
7126
7127 Rewrite (Call,
7128 Make_Procedure_Call_Statement (Loc,
7129 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7130 Parameter_Associations => Params));
7131
7132 -- Construct statement sequence for new block
7133
7134 Append_To (Stmts,
7135 Make_Implicit_If_Statement (N,
7136 Condition => New_Reference_To (B, Loc),
7137 Then_Statements => Statements (Alt),
7138 Else_Statements => Else_Statements (N)));
7139 end if;
7140
7141 -- The result is the new block
7142
7143 Rewrite (N,
7144 Make_Block_Statement (Loc,
7145 Declarations => Declarations (Blk),
7146 Handled_Statement_Sequence =>
7147 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7148 end if;
7149
7150 Analyze (N);
7151 end Expand_N_Conditional_Entry_Call;
7152
7153 ---------------------------------------
7154 -- Expand_N_Delay_Relative_Statement --
7155 ---------------------------------------
7156
7157 -- Delay statement is implemented as a procedure call to Delay_For
7158 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
7159 -- simple delays imposed by the use of Protected Objects.
7160
7161 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
7162 Loc : constant Source_Ptr := Sloc (N);
7163 begin
7164 Rewrite (N,
7165 Make_Procedure_Call_Statement (Loc,
7166 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
7167 Parameter_Associations => New_List (Expression (N))));
7168 Analyze (N);
7169 end Expand_N_Delay_Relative_Statement;
7170
7171 ------------------------------------
7172 -- Expand_N_Delay_Until_Statement --
7173 ------------------------------------
7174
7175 -- Delay Until statement is implemented as a procedure call to
7176 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
7177
7178 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
7179 Loc : constant Source_Ptr := Sloc (N);
7180 Typ : Entity_Id;
7181
7182 begin
7183 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
7184 Typ := RTE (RO_CA_Delay_Until);
7185 else
7186 Typ := RTE (RO_RT_Delay_Until);
7187 end if;
7188
7189 Rewrite (N,
7190 Make_Procedure_Call_Statement (Loc,
7191 Name => New_Reference_To (Typ, Loc),
7192 Parameter_Associations => New_List (Expression (N))));
7193
7194 Analyze (N);
7195 end Expand_N_Delay_Until_Statement;
7196
7197 -------------------------
7198 -- Expand_N_Entry_Body --
7199 -------------------------
7200
7201 procedure Expand_N_Entry_Body (N : Node_Id) is
7202 begin
7203 -- Associate discriminals with the next protected operation body to be
7204 -- expanded.
7205
7206 if Present (Next_Protected_Operation (N)) then
7207 Set_Discriminals (Parent (Current_Scope));
7208 end if;
7209 end Expand_N_Entry_Body;
7210
7211 -----------------------------------
7212 -- Expand_N_Entry_Call_Statement --
7213 -----------------------------------
7214
7215 -- An entry call is expanded into GNARLI calls to implement a simple entry
7216 -- call (see Build_Simple_Entry_Call).
7217
7218 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
7219 Concval : Node_Id;
7220 Ename : Node_Id;
7221 Index : Node_Id;
7222
7223 begin
7224 if No_Run_Time_Mode then
7225 Error_Msg_CRT ("entry call", N);
7226 return;
7227 end if;
7228
7229 -- If this entry call is part of an asynchronous select, don't expand it
7230 -- here; it will be expanded with the select statement. Don't expand
7231 -- timed entry calls either, as they are translated into asynchronous
7232 -- entry calls.
7233
7234 -- ??? This whole approach is questionable; it may be better to go back
7235 -- to allowing the expansion to take place and then attempting to fix it
7236 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
7237 -- whether the expanded call is on a task or protected entry.
7238
7239 if (Nkind (Parent (N)) /= N_Triggering_Alternative
7240 or else N /= Triggering_Statement (Parent (N)))
7241 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
7242 or else N /= Entry_Call_Statement (Parent (N))
7243 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
7244 then
7245 Extract_Entry (N, Concval, Ename, Index);
7246 Build_Simple_Entry_Call (N, Concval, Ename, Index);
7247 end if;
7248 end Expand_N_Entry_Call_Statement;
7249
7250 --------------------------------
7251 -- Expand_N_Entry_Declaration --
7252 --------------------------------
7253
7254 -- If there are parameters, then first, each of the formals is marked by
7255 -- setting Is_Entry_Formal. Next a record type is built which is used to
7256 -- hold the parameter values. The name of this record type is entryP where
7257 -- entry is the name of the entry, with an additional corresponding access
7258 -- type called entryPA. The record type has matching components for each
7259 -- formal (the component names are the same as the formal names). For
7260 -- elementary types, the component type matches the formal type. For
7261 -- composite types, an access type is declared (with the name formalA)
7262 -- which designates the formal type, and the type of the component is this
7263 -- access type. Finally the Entry_Component of each formal is set to
7264 -- reference the corresponding record component.
7265
7266 procedure Expand_N_Entry_Declaration (N : Node_Id) is
7267 Loc : constant Source_Ptr := Sloc (N);
7268 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
7269 Components : List_Id;
7270 Formal : Node_Id;
7271 Ftype : Entity_Id;
7272 Last_Decl : Node_Id;
7273 Component : Entity_Id;
7274 Ctype : Entity_Id;
7275 Decl : Node_Id;
7276 Rec_Ent : Entity_Id;
7277 Acc_Ent : Entity_Id;
7278
7279 begin
7280 Formal := First_Formal (Entry_Ent);
7281 Last_Decl := N;
7282
7283 -- Most processing is done only if parameters are present
7284
7285 if Present (Formal) then
7286 Components := New_List;
7287
7288 -- Loop through formals
7289
7290 while Present (Formal) loop
7291 Set_Is_Entry_Formal (Formal);
7292 Component :=
7293 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
7294 Set_Entry_Component (Formal, Component);
7295 Set_Entry_Formal (Component, Formal);
7296 Ftype := Etype (Formal);
7297
7298 -- Declare new access type and then append
7299
7300 Ctype := Make_Temporary (Loc, 'A');
7301
7302 Decl :=
7303 Make_Full_Type_Declaration (Loc,
7304 Defining_Identifier => Ctype,
7305 Type_Definition =>
7306 Make_Access_To_Object_Definition (Loc,
7307 All_Present => True,
7308 Constant_Present => Ekind (Formal) = E_In_Parameter,
7309 Subtype_Indication => New_Reference_To (Ftype, Loc)));
7310
7311 Insert_After (Last_Decl, Decl);
7312 Last_Decl := Decl;
7313
7314 Append_To (Components,
7315 Make_Component_Declaration (Loc,
7316 Defining_Identifier => Component,
7317 Component_Definition =>
7318 Make_Component_Definition (Loc,
7319 Aliased_Present => False,
7320 Subtype_Indication => New_Reference_To (Ctype, Loc))));
7321
7322 Next_Formal_With_Extras (Formal);
7323 end loop;
7324
7325 -- Create the Entry_Parameter_Record declaration
7326
7327 Rec_Ent := Make_Temporary (Loc, 'P');
7328
7329 Decl :=
7330 Make_Full_Type_Declaration (Loc,
7331 Defining_Identifier => Rec_Ent,
7332 Type_Definition =>
7333 Make_Record_Definition (Loc,
7334 Component_List =>
7335 Make_Component_List (Loc,
7336 Component_Items => Components)));
7337
7338 Insert_After (Last_Decl, Decl);
7339 Last_Decl := Decl;
7340
7341 -- Construct and link in the corresponding access type
7342
7343 Acc_Ent := Make_Temporary (Loc, 'A');
7344
7345 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
7346
7347 Decl :=
7348 Make_Full_Type_Declaration (Loc,
7349 Defining_Identifier => Acc_Ent,
7350 Type_Definition =>
7351 Make_Access_To_Object_Definition (Loc,
7352 All_Present => True,
7353 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
7354
7355 Insert_After (Last_Decl, Decl);
7356 end if;
7357 end Expand_N_Entry_Declaration;
7358
7359 -----------------------------
7360 -- Expand_N_Protected_Body --
7361 -----------------------------
7362
7363 -- Protected bodies are expanded to the completion of the subprograms
7364 -- created for the corresponding protected type. These are a protected and
7365 -- unprotected version of each protected subprogram in the object, a
7366 -- function to calculate each entry barrier, and a procedure to execute the
7367 -- sequence of statements of each protected entry body. For example, for
7368 -- protected type ptype:
7369
7370 -- function entB
7371 -- (O : System.Address;
7372 -- E : Protected_Entry_Index)
7373 -- return Boolean
7374 -- is
7375 -- <discriminant renamings>
7376 -- <private object renamings>
7377 -- begin
7378 -- return <barrier expression>;
7379 -- end entB;
7380
7381 -- procedure pprocN (_object : in out poV;...) is
7382 -- <discriminant renamings>
7383 -- <private object renamings>
7384 -- begin
7385 -- <sequence of statements>
7386 -- end pprocN;
7387
7388 -- procedure pprocP (_object : in out poV;...) is
7389 -- procedure _clean is
7390 -- Pn : Boolean;
7391 -- begin
7392 -- ptypeS (_object, Pn);
7393 -- Unlock (_object._object'Access);
7394 -- Abort_Undefer.all;
7395 -- end _clean;
7396
7397 -- begin
7398 -- Abort_Defer.all;
7399 -- Lock (_object._object'Access);
7400 -- pprocN (_object;...);
7401 -- at end
7402 -- _clean;
7403 -- end pproc;
7404
7405 -- function pfuncN (_object : poV;...) return Return_Type is
7406 -- <discriminant renamings>
7407 -- <private object renamings>
7408 -- begin
7409 -- <sequence of statements>
7410 -- end pfuncN;
7411
7412 -- function pfuncP (_object : poV) return Return_Type is
7413 -- procedure _clean is
7414 -- begin
7415 -- Unlock (_object._object'Access);
7416 -- Abort_Undefer.all;
7417 -- end _clean;
7418
7419 -- begin
7420 -- Abort_Defer.all;
7421 -- Lock (_object._object'Access);
7422 -- return pfuncN (_object);
7423
7424 -- at end
7425 -- _clean;
7426 -- end pfunc;
7427
7428 -- procedure entE
7429 -- (O : System.Address;
7430 -- P : System.Address;
7431 -- E : Protected_Entry_Index)
7432 -- is
7433 -- <discriminant renamings>
7434 -- <private object renamings>
7435 -- type poVP is access poV;
7436 -- _Object : ptVP := ptVP!(O);
7437
7438 -- begin
7439 -- begin
7440 -- <statement sequence>
7441 -- Complete_Entry_Body (_Object._Object);
7442 -- exception
7443 -- when all others =>
7444 -- Exceptional_Complete_Entry_Body (
7445 -- _Object._Object, Get_GNAT_Exception);
7446 -- end;
7447 -- end entE;
7448
7449 -- The type poV is the record created for the protected type to hold
7450 -- the state of the protected object.
7451
7452 procedure Expand_N_Protected_Body (N : Node_Id) is
7453 Loc : constant Source_Ptr := Sloc (N);
7454 Pid : constant Entity_Id := Corresponding_Spec (N);
7455
7456 Current_Node : Node_Id;
7457 Disp_Op_Body : Node_Id;
7458 New_Op_Body : Node_Id;
7459 Num_Entries : Natural := 0;
7460 Op_Body : Node_Id;
7461 Op_Id : Entity_Id;
7462
7463 function Build_Dispatching_Subprogram_Body
7464 (N : Node_Id;
7465 Pid : Node_Id;
7466 Prot_Bod : Node_Id) return Node_Id;
7467 -- Build a dispatching version of the protected subprogram body. The
7468 -- newly generated subprogram contains a call to the original protected
7469 -- body. The following code is generated:
7470 --
7471 -- function <protected-function-name> (Param1 .. ParamN) return
7472 -- <return-type> is
7473 -- begin
7474 -- return <protected-function-name>P (Param1 .. ParamN);
7475 -- end <protected-function-name>;
7476 --
7477 -- or
7478 --
7479 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
7480 -- begin
7481 -- <protected-procedure-name>P (Param1 .. ParamN);
7482 -- end <protected-procedure-name>
7483
7484 ---------------------------------------
7485 -- Build_Dispatching_Subprogram_Body --
7486 ---------------------------------------
7487
7488 function Build_Dispatching_Subprogram_Body
7489 (N : Node_Id;
7490 Pid : Node_Id;
7491 Prot_Bod : Node_Id) return Node_Id
7492 is
7493 Loc : constant Source_Ptr := Sloc (N);
7494 Actuals : List_Id;
7495 Formal : Node_Id;
7496 Spec : Node_Id;
7497 Stmts : List_Id;
7498
7499 begin
7500 -- Generate a specification without a letter suffix in order to
7501 -- override an interface function or procedure.
7502
7503 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
7504
7505 -- The formal parameters become the actuals of the protected function
7506 -- or procedure call.
7507
7508 Actuals := New_List;
7509 Formal := First (Parameter_Specifications (Spec));
7510 while Present (Formal) loop
7511 Append_To (Actuals,
7512 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
7513
7514 Next (Formal);
7515 end loop;
7516
7517 if Nkind (Spec) = N_Procedure_Specification then
7518 Stmts :=
7519 New_List (
7520 Make_Procedure_Call_Statement (Loc,
7521 Name =>
7522 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
7523 Parameter_Associations => Actuals));
7524 else
7525 pragma Assert (Nkind (Spec) = N_Function_Specification);
7526
7527 Stmts :=
7528 New_List (
7529 Make_Simple_Return_Statement (Loc,
7530 Expression =>
7531 Make_Function_Call (Loc,
7532 Name =>
7533 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
7534 Parameter_Associations => Actuals)));
7535 end if;
7536
7537 return
7538 Make_Subprogram_Body (Loc,
7539 Declarations => Empty_List,
7540 Specification => Spec,
7541 Handled_Statement_Sequence =>
7542 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7543 end Build_Dispatching_Subprogram_Body;
7544
7545 -- Start of processing for Expand_N_Protected_Body
7546
7547 begin
7548 if No_Run_Time_Mode then
7549 Error_Msg_CRT ("protected body", N);
7550 return;
7551 end if;
7552
7553 -- This is the proper body corresponding to a stub. The declarations
7554 -- must be inserted at the point of the stub, which in turn is in the
7555 -- declarative part of the parent unit.
7556
7557 if Nkind (Parent (N)) = N_Subunit then
7558 Current_Node := Corresponding_Stub (Parent (N));
7559 else
7560 Current_Node := N;
7561 end if;
7562
7563 Op_Body := First (Declarations (N));
7564
7565 -- The protected body is replaced with the bodies of its
7566 -- protected operations, and the declarations for internal objects
7567 -- that may have been created for entry family bounds.
7568
7569 Rewrite (N, Make_Null_Statement (Sloc (N)));
7570 Analyze (N);
7571
7572 while Present (Op_Body) loop
7573 case Nkind (Op_Body) is
7574 when N_Subprogram_Declaration =>
7575 null;
7576
7577 when N_Subprogram_Body =>
7578
7579 -- Do not create bodies for eliminated operations
7580
7581 if not Is_Eliminated (Defining_Entity (Op_Body))
7582 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
7583 then
7584 New_Op_Body :=
7585 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
7586
7587 Insert_After (Current_Node, New_Op_Body);
7588 Current_Node := New_Op_Body;
7589 Analyze (New_Op_Body);
7590
7591 -- Build the corresponding protected operation. It may
7592 -- appear that this is needed only if this is a visible
7593 -- operation of the type, or if it is an interrupt handler,
7594 -- and this was the strategy used previously in GNAT.
7595 -- However, the operation may be exported through a 'Access
7596 -- to an external caller. This is the common idiom in code
7597 -- that uses the Ada 2005 Timing_Events package. As a result
7598 -- we need to produce the protected body for both visible
7599 -- and private operations, as well as operations that only
7600 -- have a body in the source, and for which we create a
7601 -- declaration in the protected body itself.
7602
7603 if Present (Corresponding_Spec (Op_Body)) then
7604 New_Op_Body :=
7605 Build_Protected_Subprogram_Body (
7606 Op_Body, Pid, Specification (New_Op_Body));
7607
7608 Insert_After (Current_Node, New_Op_Body);
7609 Analyze (New_Op_Body);
7610
7611 Current_Node := New_Op_Body;
7612
7613 -- Generate an overriding primitive operation body for
7614 -- this subprogram if the protected type implements an
7615 -- interface.
7616
7617 if Ada_Version >= Ada_2005
7618 and then
7619 Present (Interfaces (Corresponding_Record_Type (Pid)))
7620 then
7621 Disp_Op_Body :=
7622 Build_Dispatching_Subprogram_Body
7623 (Op_Body, Pid, New_Op_Body);
7624
7625 Insert_After (Current_Node, Disp_Op_Body);
7626 Analyze (Disp_Op_Body);
7627
7628 Current_Node := Disp_Op_Body;
7629 end if;
7630 end if;
7631 end if;
7632
7633 when N_Entry_Body =>
7634 Op_Id := Defining_Identifier (Op_Body);
7635 Num_Entries := Num_Entries + 1;
7636
7637 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
7638
7639 Insert_After (Current_Node, New_Op_Body);
7640 Current_Node := New_Op_Body;
7641 Analyze (New_Op_Body);
7642
7643 when N_Implicit_Label_Declaration =>
7644 null;
7645
7646 when N_Itype_Reference =>
7647 Insert_After (Current_Node, New_Copy (Op_Body));
7648
7649 when N_Freeze_Entity =>
7650 New_Op_Body := New_Copy (Op_Body);
7651
7652 if Present (Entity (Op_Body))
7653 and then Freeze_Node (Entity (Op_Body)) = Op_Body
7654 then
7655 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
7656 end if;
7657
7658 Insert_After (Current_Node, New_Op_Body);
7659 Current_Node := New_Op_Body;
7660 Analyze (New_Op_Body);
7661
7662 when N_Pragma =>
7663 New_Op_Body := New_Copy (Op_Body);
7664 Insert_After (Current_Node, New_Op_Body);
7665 Current_Node := New_Op_Body;
7666 Analyze (New_Op_Body);
7667
7668 when N_Object_Declaration =>
7669 pragma Assert (not Comes_From_Source (Op_Body));
7670 New_Op_Body := New_Copy (Op_Body);
7671 Insert_After (Current_Node, New_Op_Body);
7672 Current_Node := New_Op_Body;
7673 Analyze (New_Op_Body);
7674
7675 when others =>
7676 raise Program_Error;
7677
7678 end case;
7679
7680 Next (Op_Body);
7681 end loop;
7682
7683 -- Finally, create the body of the function that maps an entry index
7684 -- into the corresponding body index, except when there is no entry, or
7685 -- in a Ravenscar-like profile.
7686
7687 if Corresponding_Runtime_Package (Pid) =
7688 System_Tasking_Protected_Objects_Entries
7689 then
7690 New_Op_Body := Build_Find_Body_Index (Pid);
7691 Insert_After (Current_Node, New_Op_Body);
7692 Current_Node := New_Op_Body;
7693 Analyze (New_Op_Body);
7694 end if;
7695
7696 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
7697 -- protected body. At this point all wrapper specs have been created,
7698 -- frozen and included in the dispatch table for the protected type.
7699
7700 if Ada_Version >= Ada_2005 then
7701 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
7702 end if;
7703 end Expand_N_Protected_Body;
7704
7705 -----------------------------------------
7706 -- Expand_N_Protected_Type_Declaration --
7707 -----------------------------------------
7708
7709 -- First we create a corresponding record type declaration used to
7710 -- represent values of this protected type.
7711 -- The general form of this type declaration is
7712
7713 -- type poV (discriminants) is record
7714 -- _Object : aliased <kind>Protection
7715 -- [(<entry count> [, <handler count>])];
7716 -- [entry_family : array (bounds) of Void;]
7717 -- <private data fields>
7718 -- end record;
7719
7720 -- The discriminants are present only if the corresponding protected type
7721 -- has discriminants, and they exactly mirror the protected type
7722 -- discriminants. The private data fields similarly mirror the private
7723 -- declarations of the protected type.
7724
7725 -- The Object field is always present. It contains RTS specific data used
7726 -- to control the protected object. It is declared as Aliased so that it
7727 -- can be passed as a pointer to the RTS. This allows the protected record
7728 -- to be referenced within RTS data structures. An appropriate Protection
7729 -- type and discriminant are generated.
7730
7731 -- The Service field is present for protected objects with entries. It
7732 -- contains sufficient information to allow the entry service procedure for
7733 -- this object to be called when the object is not known till runtime.
7734
7735 -- One entry_family component is present for each entry family in the
7736 -- task definition (see Expand_N_Task_Type_Declaration).
7737
7738 -- When a protected object is declared, an instance of the protected type
7739 -- value record is created. The elaboration of this declaration creates the
7740 -- correct bounds for the entry families, and also evaluates the priority
7741 -- expression if needed. The initialization routine for the protected type
7742 -- itself then calls Initialize_Protection with appropriate parameters to
7743 -- initialize the value of the Task_Id field. Install_Handlers may be also
7744 -- called if a pragma Attach_Handler applies.
7745
7746 -- Note: this record is passed to the subprograms created by the expansion
7747 -- of protected subprograms and entries. It is an in parameter to protected
7748 -- functions and an in out parameter to procedures and entry bodies. The
7749 -- Entity_Id for this created record type is placed in the
7750 -- Corresponding_Record_Type field of the associated protected type entity.
7751
7752 -- Next we create a procedure specifications for protected subprograms and
7753 -- entry bodies. For each protected subprograms two subprograms are
7754 -- created, an unprotected and a protected version. The unprotected version
7755 -- is called from within other operations of the same protected object.
7756
7757 -- We also build the call to register the procedure if a pragma
7758 -- Interrupt_Handler applies.
7759
7760 -- A single subprogram is created to service all entry bodies; it has an
7761 -- additional boolean out parameter indicating that the previous entry call
7762 -- made by the current task was serviced immediately, i.e. not by proxy.
7763 -- The O parameter contains a pointer to a record object of the type
7764 -- described above. An untyped interface is used here to allow this
7765 -- procedure to be called in places where the type of the object to be
7766 -- serviced is not known. This must be done, for example, when a call that
7767 -- may have been requeued is cancelled; the corresponding object must be
7768 -- serviced, but which object that is not known till runtime.
7769
7770 -- procedure ptypeS
7771 -- (O : System.Address; P : out Boolean);
7772 -- procedure pprocN (_object : in out poV);
7773 -- procedure pproc (_object : in out poV);
7774 -- function pfuncN (_object : poV);
7775 -- function pfunc (_object : poV);
7776 -- ...
7777
7778 -- Note that this must come after the record type declaration, since
7779 -- the specs refer to this type.
7780
7781 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
7782 Loc : constant Source_Ptr := Sloc (N);
7783 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
7784
7785 Pdef : constant Node_Id := Protected_Definition (N);
7786 -- This contains two lists; one for visible and one for private decls
7787
7788 Rec_Decl : Node_Id;
7789 Cdecls : List_Id;
7790 Discr_Map : constant Elist_Id := New_Elmt_List;
7791 Priv : Node_Id;
7792 New_Priv : Node_Id;
7793 Comp : Node_Id;
7794 Comp_Id : Entity_Id;
7795 Sub : Node_Id;
7796 Current_Node : Node_Id := N;
7797 Bdef : Entity_Id := Empty; -- avoid uninit warning
7798 Edef : Entity_Id := Empty; -- avoid uninit warning
7799 Entries_Aggr : Node_Id;
7800 Body_Id : Entity_Id;
7801 Body_Arr : Node_Id;
7802 E_Count : Int;
7803 Object_Comp : Node_Id;
7804
7805 procedure Check_Inlining (Subp : Entity_Id);
7806 -- If the original operation has a pragma Inline, propagate the flag
7807 -- to the internal body, for possible inlining later on. The source
7808 -- operation is invisible to the back-end and is never actually called.
7809
7810 function Static_Component_Size (Comp : Entity_Id) return Boolean;
7811 -- When compiling under the Ravenscar profile, private components must
7812 -- have a static size, or else a protected object will require heap
7813 -- allocation, violating the corresponding restriction. It is preferable
7814 -- to make this check here, because it provides a better error message
7815 -- than the back-end, which refers to the object as a whole.
7816
7817 procedure Register_Handler;
7818 -- For a protected operation that is an interrupt handler, add the
7819 -- freeze action that will register it as such.
7820
7821 --------------------
7822 -- Check_Inlining --
7823 --------------------
7824
7825 procedure Check_Inlining (Subp : Entity_Id) is
7826 begin
7827 if Is_Inlined (Subp) then
7828 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
7829 Set_Is_Inlined (Subp, False);
7830 end if;
7831 end Check_Inlining;
7832
7833 ---------------------------------
7834 -- Check_Static_Component_Size --
7835 ---------------------------------
7836
7837 function Static_Component_Size (Comp : Entity_Id) return Boolean is
7838 Typ : constant Entity_Id := Etype (Comp);
7839 C : Entity_Id;
7840
7841 begin
7842 if Is_Scalar_Type (Typ) then
7843 return True;
7844
7845 elsif Is_Array_Type (Typ) then
7846 return Compile_Time_Known_Bounds (Typ);
7847
7848 elsif Is_Record_Type (Typ) then
7849 C := First_Component (Typ);
7850 while Present (C) loop
7851 if not Static_Component_Size (C) then
7852 return False;
7853 end if;
7854
7855 Next_Component (C);
7856 end loop;
7857
7858 return True;
7859
7860 -- Any other types will be checked by the back-end
7861
7862 else
7863 return True;
7864 end if;
7865 end Static_Component_Size;
7866
7867 ----------------------
7868 -- Register_Handler --
7869 ----------------------
7870
7871 procedure Register_Handler is
7872
7873 -- All semantic checks already done in Sem_Prag
7874
7875 Prot_Proc : constant Entity_Id :=
7876 Defining_Unit_Name
7877 (Specification (Current_Node));
7878
7879 Proc_Address : constant Node_Id :=
7880 Make_Attribute_Reference (Loc,
7881 Prefix => New_Reference_To (Prot_Proc, Loc),
7882 Attribute_Name => Name_Address);
7883
7884 RTS_Call : constant Entity_Id :=
7885 Make_Procedure_Call_Statement (Loc,
7886 Name =>
7887 New_Reference_To (
7888 RTE (RE_Register_Interrupt_Handler), Loc),
7889 Parameter_Associations =>
7890 New_List (Proc_Address));
7891 begin
7892 Append_Freeze_Action (Prot_Proc, RTS_Call);
7893 end Register_Handler;
7894
7895 -- Start of processing for Expand_N_Protected_Type_Declaration
7896
7897 begin
7898 if Present (Corresponding_Record_Type (Prot_Typ)) then
7899 return;
7900 else
7901 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
7902 end if;
7903
7904 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
7905
7906 Qualify_Entity_Names (N);
7907
7908 -- If the type has discriminants, their occurrences in the declaration
7909 -- have been replaced by the corresponding discriminals. For components
7910 -- that are constrained by discriminants, their homologues in the
7911 -- corresponding record type must refer to the discriminants of that
7912 -- record, so we must apply a new renaming to subtypes_indications:
7913
7914 -- protected discriminant => discriminal => record discriminant
7915
7916 -- This replacement is not applied to default expressions, for which
7917 -- the discriminal is correct.
7918
7919 if Has_Discriminants (Prot_Typ) then
7920 declare
7921 Disc : Entity_Id;
7922 Decl : Node_Id;
7923
7924 begin
7925 Disc := First_Discriminant (Prot_Typ);
7926 Decl := First (Discriminant_Specifications (Rec_Decl));
7927 while Present (Disc) loop
7928 Append_Elmt (Discriminal (Disc), Discr_Map);
7929 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
7930 Next_Discriminant (Disc);
7931 Next (Decl);
7932 end loop;
7933 end;
7934 end if;
7935
7936 -- Fill in the component declarations
7937
7938 -- Add components for entry families. For each entry family, create an
7939 -- anonymous type declaration with the same size, and analyze the type.
7940
7941 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
7942
7943 -- Prepend the _Object field with the right type to the component list.
7944 -- We need to compute the number of entries, and in some cases the
7945 -- number of Attach_Handler pragmas.
7946
7947 declare
7948 Ritem : Node_Id;
7949 Num_Attach_Handler : Int := 0;
7950 Protection_Subtype : Node_Id;
7951 Entry_Count_Expr : constant Node_Id :=
7952 Build_Entry_Count_Expression
7953 (Prot_Typ, Cdecls, Loc);
7954
7955 begin
7956 -- Could this be simplified using Corresponding_Runtime_Package???
7957
7958 if Has_Attach_Handler (Prot_Typ) then
7959 Ritem := First_Rep_Item (Prot_Typ);
7960 while Present (Ritem) loop
7961 if Nkind (Ritem) = N_Pragma
7962 and then Pragma_Name (Ritem) = Name_Attach_Handler
7963 then
7964 Num_Attach_Handler := Num_Attach_Handler + 1;
7965 end if;
7966
7967 Next_Rep_Item (Ritem);
7968 end loop;
7969
7970 if Restricted_Profile then
7971 if Has_Entries (Prot_Typ) then
7972 Protection_Subtype :=
7973 New_Reference_To (RTE (RE_Protection_Entry), Loc);
7974 else
7975 Protection_Subtype :=
7976 New_Reference_To (RTE (RE_Protection), Loc);
7977 end if;
7978 else
7979 Protection_Subtype :=
7980 Make_Subtype_Indication
7981 (Sloc => Loc,
7982 Subtype_Mark =>
7983 New_Reference_To
7984 (RTE (RE_Static_Interrupt_Protection), Loc),
7985 Constraint =>
7986 Make_Index_Or_Discriminant_Constraint (
7987 Sloc => Loc,
7988 Constraints => New_List (
7989 Entry_Count_Expr,
7990 Make_Integer_Literal (Loc, Num_Attach_Handler))));
7991 end if;
7992
7993 elsif Has_Interrupt_Handler (Prot_Typ) then
7994 Protection_Subtype :=
7995 Make_Subtype_Indication (
7996 Sloc => Loc,
7997 Subtype_Mark => New_Reference_To
7998 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
7999 Constraint =>
8000 Make_Index_Or_Discriminant_Constraint (
8001 Sloc => Loc,
8002 Constraints => New_List (Entry_Count_Expr)));
8003
8004 -- Type has explicit entries or generated primitive entry wrappers
8005
8006 elsif Has_Entries (Prot_Typ)
8007 or else (Ada_Version >= Ada_2005
8008 and then Present (Interface_List (N)))
8009 then
8010 case Corresponding_Runtime_Package (Prot_Typ) is
8011 when System_Tasking_Protected_Objects_Entries =>
8012 Protection_Subtype :=
8013 Make_Subtype_Indication (Loc,
8014 Subtype_Mark =>
8015 New_Reference_To (RTE (RE_Protection_Entries), Loc),
8016 Constraint =>
8017 Make_Index_Or_Discriminant_Constraint (
8018 Sloc => Loc,
8019 Constraints => New_List (Entry_Count_Expr)));
8020
8021 when System_Tasking_Protected_Objects_Single_Entry =>
8022 Protection_Subtype :=
8023 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8024
8025 when others =>
8026 raise Program_Error;
8027 end case;
8028
8029 else
8030 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
8031 end if;
8032
8033 Object_Comp :=
8034 Make_Component_Declaration (Loc,
8035 Defining_Identifier =>
8036 Make_Defining_Identifier (Loc, Name_uObject),
8037 Component_Definition =>
8038 Make_Component_Definition (Loc,
8039 Aliased_Present => True,
8040 Subtype_Indication => Protection_Subtype));
8041 end;
8042
8043 pragma Assert (Present (Pdef));
8044
8045 -- Add private field components
8046
8047 if Present (Private_Declarations (Pdef)) then
8048 Priv := First (Private_Declarations (Pdef));
8049
8050 while Present (Priv) loop
8051
8052 if Nkind (Priv) = N_Component_Declaration then
8053 if not Static_Component_Size (Defining_Identifier (Priv)) then
8054
8055 -- When compiling for a restricted profile, the private
8056 -- components must have a static size. If not, this is an
8057 -- error for a single protected declaration, and rates a
8058 -- warning on a protected type declaration.
8059
8060 if not Comes_From_Source (Prot_Typ) then
8061 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8062
8063 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8064 Error_Msg_N ("component has non-static size?", Priv);
8065 Error_Msg_NE
8066 ("\creation of protected object of type& will violate"
8067 & " restriction No_Implicit_Heap_Allocations?",
8068 Priv, Prot_Typ);
8069 end if;
8070 end if;
8071
8072 -- The component definition consists of a subtype indication,
8073 -- or (in Ada 2005) an access definition. Make a copy of the
8074 -- proper definition.
8075
8076 declare
8077 Old_Comp : constant Node_Id := Component_Definition (Priv);
8078 Oent : constant Entity_Id := Defining_Identifier (Priv);
8079 New_Comp : Node_Id;
8080 Nent : constant Entity_Id :=
8081 Make_Defining_Identifier (Sloc (Oent),
8082 Chars => Chars (Oent));
8083
8084 begin
8085 if Present (Subtype_Indication (Old_Comp)) then
8086 New_Comp :=
8087 Make_Component_Definition (Sloc (Oent),
8088 Aliased_Present => False,
8089 Subtype_Indication =>
8090 New_Copy_Tree (Subtype_Indication (Old_Comp),
8091 Discr_Map));
8092 else
8093 New_Comp :=
8094 Make_Component_Definition (Sloc (Oent),
8095 Aliased_Present => False,
8096 Access_Definition =>
8097 New_Copy_Tree (Access_Definition (Old_Comp),
8098 Discr_Map));
8099 end if;
8100
8101 New_Priv :=
8102 Make_Component_Declaration (Loc,
8103 Defining_Identifier => Nent,
8104 Component_Definition => New_Comp,
8105 Expression => Expression (Priv));
8106
8107 Set_Has_Per_Object_Constraint (Nent,
8108 Has_Per_Object_Constraint (Oent));
8109
8110 Append_To (Cdecls, New_Priv);
8111 end;
8112
8113 elsif Nkind (Priv) = N_Subprogram_Declaration then
8114
8115 -- Make the unprotected version of the subprogram available
8116 -- for expansion of intra object calls. There is need for
8117 -- a protected version only if the subprogram is an interrupt
8118 -- handler, otherwise this operation can only be called from
8119 -- within the body.
8120
8121 Sub :=
8122 Make_Subprogram_Declaration (Loc,
8123 Specification =>
8124 Build_Protected_Sub_Specification
8125 (Priv, Prot_Typ, Unprotected_Mode));
8126
8127 Insert_After (Current_Node, Sub);
8128 Analyze (Sub);
8129
8130 Set_Protected_Body_Subprogram
8131 (Defining_Unit_Name (Specification (Priv)),
8132 Defining_Unit_Name (Specification (Sub)));
8133 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
8134 Current_Node := Sub;
8135
8136 Sub :=
8137 Make_Subprogram_Declaration (Loc,
8138 Specification =>
8139 Build_Protected_Sub_Specification
8140 (Priv, Prot_Typ, Protected_Mode));
8141
8142 Insert_After (Current_Node, Sub);
8143 Analyze (Sub);
8144 Current_Node := Sub;
8145
8146 if Is_Interrupt_Handler
8147 (Defining_Unit_Name (Specification (Priv)))
8148 then
8149 if not Restricted_Profile then
8150 Register_Handler;
8151 end if;
8152 end if;
8153 end if;
8154
8155 Next (Priv);
8156 end loop;
8157 end if;
8158
8159 -- Put the _Object component after the private component so that it
8160 -- be finalized early as required by 9.4 (20)
8161
8162 Append_To (Cdecls, Object_Comp);
8163
8164 Insert_After (Current_Node, Rec_Decl);
8165 Current_Node := Rec_Decl;
8166
8167 -- Analyze the record declaration immediately after construction,
8168 -- because the initialization procedure is needed for single object
8169 -- declarations before the next entity is analyzed (the freeze call
8170 -- that generates this initialization procedure is found below).
8171
8172 Analyze (Rec_Decl, Suppress => All_Checks);
8173
8174 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
8175 -- the corresponding record is frozen. If any wrappers are generated,
8176 -- Current_Node is updated accordingly.
8177
8178 if Ada_Version >= Ada_2005 then
8179 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
8180 end if;
8181
8182 -- Collect pointers to entry bodies and their barriers, to be placed
8183 -- in the Entry_Bodies_Array for the type. For each entry/family we
8184 -- add an expression to the aggregate which is the initial value of
8185 -- this array. The array is declared after all protected subprograms.
8186
8187 if Has_Entries (Prot_Typ) then
8188 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
8189 else
8190 Entries_Aggr := Empty;
8191 end if;
8192
8193 -- Build two new procedure specifications for each protected subprogram;
8194 -- one to call from outside the object and one to call from inside.
8195 -- Build a barrier function and an entry body action procedure
8196 -- specification for each protected entry. Initialize the entry body
8197 -- array. If subprogram is flagged as eliminated, do not generate any
8198 -- internal operations.
8199
8200 E_Count := 0;
8201
8202 Comp := First (Visible_Declarations (Pdef));
8203
8204 while Present (Comp) loop
8205 if Nkind (Comp) = N_Subprogram_Declaration then
8206 Sub :=
8207 Make_Subprogram_Declaration (Loc,
8208 Specification =>
8209 Build_Protected_Sub_Specification
8210 (Comp, Prot_Typ, Unprotected_Mode));
8211
8212 Insert_After (Current_Node, Sub);
8213 Analyze (Sub);
8214
8215 Set_Protected_Body_Subprogram
8216 (Defining_Unit_Name (Specification (Comp)),
8217 Defining_Unit_Name (Specification (Sub)));
8218 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
8219
8220 -- Make the protected version of the subprogram available for
8221 -- expansion of external calls.
8222
8223 Current_Node := Sub;
8224
8225 Sub :=
8226 Make_Subprogram_Declaration (Loc,
8227 Specification =>
8228 Build_Protected_Sub_Specification
8229 (Comp, Prot_Typ, Protected_Mode));
8230
8231 Insert_After (Current_Node, Sub);
8232 Analyze (Sub);
8233
8234 Current_Node := Sub;
8235
8236 -- Generate an overriding primitive operation specification for
8237 -- this subprogram if the protected type implements an interface.
8238
8239 if Ada_Version >= Ada_2005
8240 and then
8241 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
8242 then
8243 Sub :=
8244 Make_Subprogram_Declaration (Loc,
8245 Specification =>
8246 Build_Protected_Sub_Specification
8247 (Comp, Prot_Typ, Dispatching_Mode));
8248
8249 Insert_After (Current_Node, Sub);
8250 Analyze (Sub);
8251
8252 Current_Node := Sub;
8253 end if;
8254
8255 -- If a pragma Interrupt_Handler applies, build and add a call to
8256 -- Register_Interrupt_Handler to the freezing actions of the
8257 -- protected version (Current_Node) of the subprogram:
8258
8259 -- system.interrupts.register_interrupt_handler
8260 -- (prot_procP'address);
8261
8262 if not Restricted_Profile
8263 and then Is_Interrupt_Handler
8264 (Defining_Unit_Name (Specification (Comp)))
8265 then
8266 Register_Handler;
8267 end if;
8268
8269 elsif Nkind (Comp) = N_Entry_Declaration then
8270 E_Count := E_Count + 1;
8271 Comp_Id := Defining_Identifier (Comp);
8272
8273 Edef :=
8274 Make_Defining_Identifier (Loc,
8275 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8276 Sub :=
8277 Make_Subprogram_Declaration (Loc,
8278 Specification =>
8279 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
8280
8281 Insert_After (Current_Node, Sub);
8282 Analyze (Sub);
8283
8284 -- build wrapper procedure for pre/postconditions.
8285
8286 Build_PPC_Wrapper (Comp_Id, N);
8287
8288 Set_Protected_Body_Subprogram
8289 (Defining_Identifier (Comp),
8290 Defining_Unit_Name (Specification (Sub)));
8291
8292 Current_Node := Sub;
8293
8294 Bdef :=
8295 Make_Defining_Identifier (Loc,
8296 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
8297 Sub :=
8298 Make_Subprogram_Declaration (Loc,
8299 Specification =>
8300 Build_Barrier_Function_Specification (Loc, Bdef));
8301
8302 Insert_After (Current_Node, Sub);
8303 Analyze (Sub);
8304 Set_Protected_Body_Subprogram (Bdef, Bdef);
8305 Set_Barrier_Function (Comp_Id, Bdef);
8306 Set_Scope (Bdef, Scope (Comp_Id));
8307 Current_Node := Sub;
8308
8309 -- Collect pointers to the protected subprogram and the barrier
8310 -- of the current entry, for insertion into Entry_Bodies_Array.
8311
8312 Append (
8313 Make_Aggregate (Loc,
8314 Expressions => New_List (
8315 Make_Attribute_Reference (Loc,
8316 Prefix => New_Reference_To (Bdef, Loc),
8317 Attribute_Name => Name_Unrestricted_Access),
8318 Make_Attribute_Reference (Loc,
8319 Prefix => New_Reference_To (Edef, Loc),
8320 Attribute_Name => Name_Unrestricted_Access))),
8321 Expressions (Entries_Aggr));
8322
8323 end if;
8324
8325 Next (Comp);
8326 end loop;
8327
8328 -- If there are some private entry declarations, expand it as if they
8329 -- were visible entries.
8330
8331 if Present (Private_Declarations (Pdef)) then
8332 Comp := First (Private_Declarations (Pdef));
8333 while Present (Comp) loop
8334 if Nkind (Comp) = N_Entry_Declaration then
8335 E_Count := E_Count + 1;
8336 Comp_Id := Defining_Identifier (Comp);
8337
8338 Edef :=
8339 Make_Defining_Identifier (Loc,
8340 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8341 Sub :=
8342 Make_Subprogram_Declaration (Loc,
8343 Specification =>
8344 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
8345
8346 Insert_After (Current_Node, Sub);
8347 Analyze (Sub);
8348
8349 Set_Protected_Body_Subprogram
8350 (Defining_Identifier (Comp),
8351 Defining_Unit_Name (Specification (Sub)));
8352
8353 Current_Node := Sub;
8354
8355 Bdef :=
8356 Make_Defining_Identifier (Loc,
8357 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8358
8359 Sub :=
8360 Make_Subprogram_Declaration (Loc,
8361 Specification =>
8362 Build_Barrier_Function_Specification (Loc, Bdef));
8363
8364 Insert_After (Current_Node, Sub);
8365 Analyze (Sub);
8366 Set_Protected_Body_Subprogram (Bdef, Bdef);
8367 Set_Barrier_Function (Comp_Id, Bdef);
8368 Set_Scope (Bdef, Scope (Comp_Id));
8369 Current_Node := Sub;
8370
8371 -- Collect pointers to the protected subprogram and the barrier
8372 -- of the current entry, for insertion into Entry_Bodies_Array.
8373
8374 Append_To (Expressions (Entries_Aggr),
8375 Make_Aggregate (Loc,
8376 Expressions => New_List (
8377 Make_Attribute_Reference (Loc,
8378 Prefix => New_Reference_To (Bdef, Loc),
8379 Attribute_Name => Name_Unrestricted_Access),
8380 Make_Attribute_Reference (Loc,
8381 Prefix => New_Reference_To (Edef, Loc),
8382 Attribute_Name => Name_Unrestricted_Access))));
8383 end if;
8384
8385 Next (Comp);
8386 end loop;
8387 end if;
8388
8389 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
8390 -- all protected subprograms have been collected.
8391
8392 if Has_Entries (Prot_Typ) then
8393 Body_Id :=
8394 Make_Defining_Identifier (Sloc (Prot_Typ),
8395 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
8396
8397 case Corresponding_Runtime_Package (Prot_Typ) is
8398 when System_Tasking_Protected_Objects_Entries =>
8399 Body_Arr := Make_Object_Declaration (Loc,
8400 Defining_Identifier => Body_Id,
8401 Aliased_Present => True,
8402 Object_Definition =>
8403 Make_Subtype_Indication (Loc,
8404 Subtype_Mark => New_Reference_To (
8405 RTE (RE_Protected_Entry_Body_Array), Loc),
8406 Constraint =>
8407 Make_Index_Or_Discriminant_Constraint (Loc,
8408 Constraints => New_List (
8409 Make_Range (Loc,
8410 Make_Integer_Literal (Loc, 1),
8411 Make_Integer_Literal (Loc, E_Count))))),
8412 Expression => Entries_Aggr);
8413
8414 when System_Tasking_Protected_Objects_Single_Entry =>
8415 Body_Arr := Make_Object_Declaration (Loc,
8416 Defining_Identifier => Body_Id,
8417 Aliased_Present => True,
8418 Object_Definition => New_Reference_To
8419 (RTE (RE_Entry_Body), Loc),
8420 Expression =>
8421 Make_Aggregate (Loc,
8422 Expressions => New_List (
8423 Make_Attribute_Reference (Loc,
8424 Prefix => New_Reference_To (Bdef, Loc),
8425 Attribute_Name => Name_Unrestricted_Access),
8426 Make_Attribute_Reference (Loc,
8427 Prefix => New_Reference_To (Edef, Loc),
8428 Attribute_Name => Name_Unrestricted_Access))));
8429
8430 when others =>
8431 raise Program_Error;
8432 end case;
8433
8434 -- A pointer to this array will be placed in the corresponding record
8435 -- by its initialization procedure so this needs to be analyzed here.
8436
8437 Insert_After (Current_Node, Body_Arr);
8438 Current_Node := Body_Arr;
8439 Analyze (Body_Arr);
8440
8441 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
8442
8443 -- Finally, build the function that maps an entry index into the
8444 -- corresponding body. A pointer to this function is placed in each
8445 -- object of the type. Except for a ravenscar-like profile (no abort,
8446 -- no entry queue, 1 entry)
8447
8448 if Corresponding_Runtime_Package (Prot_Typ) =
8449 System_Tasking_Protected_Objects_Entries
8450 then
8451 Sub :=
8452 Make_Subprogram_Declaration (Loc,
8453 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
8454 Insert_After (Current_Node, Sub);
8455 Analyze (Sub);
8456 end if;
8457 end if;
8458 end Expand_N_Protected_Type_Declaration;
8459
8460 --------------------------------
8461 -- Expand_N_Requeue_Statement --
8462 --------------------------------
8463
8464 -- A non-dispatching requeue statement is expanded into one of four GNARLI
8465 -- operations, depending on the source and destination (task or protected
8466 -- object). A dispatching requeue statement is expanded into a call to the
8467 -- predefined primitive _Disp_Requeue. In addition, code is generated to
8468 -- jump around the remainder of processing for the original entry and, if
8469 -- the destination is (different) protected object, to attempt to service
8470 -- it. The following illustrates the various cases:
8471
8472 -- procedure entE
8473 -- (O : System.Address;
8474 -- P : System.Address;
8475 -- E : Protected_Entry_Index)
8476 -- is
8477 -- <discriminant renamings>
8478 -- <private object renamings>
8479 -- type poVP is access poV;
8480 -- _object : ptVP := ptVP!(O);
8481
8482 -- begin
8483 -- begin
8484 -- <start of statement sequence for entry>
8485
8486 -- -- Requeue from one protected entry body to another protected
8487 -- -- entry.
8488
8489 -- Requeue_Protected_Entry (
8490 -- _object._object'Access,
8491 -- new._object'Access,
8492 -- E,
8493 -- Abort_Present);
8494 -- return;
8495
8496 -- <some more of the statement sequence for entry>
8497
8498 -- -- Requeue from an entry body to a task entry
8499
8500 -- Requeue_Protected_To_Task_Entry (
8501 -- New._task_id,
8502 -- E,
8503 -- Abort_Present);
8504 -- return;
8505
8506 -- <rest of statement sequence for entry>
8507 -- Complete_Entry_Body (_object._object);
8508
8509 -- exception
8510 -- when all others =>
8511 -- Exceptional_Complete_Entry_Body (
8512 -- _object._object, Get_GNAT_Exception);
8513 -- end;
8514 -- end entE;
8515
8516 -- Requeue of a task entry call to a task entry
8517
8518 -- Accept_Call (E, Ann);
8519 -- <start of statement sequence for accept statement>
8520 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
8521 -- goto Lnn;
8522 -- <rest of statement sequence for accept statement>
8523 -- <<Lnn>>
8524 -- Complete_Rendezvous;
8525
8526 -- exception
8527 -- when all others =>
8528 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8529
8530 -- Requeue of a task entry call to a protected entry
8531
8532 -- Accept_Call (E, Ann);
8533 -- <start of statement sequence for accept statement>
8534 -- Requeue_Task_To_Protected_Entry (
8535 -- new._object'Access,
8536 -- E,
8537 -- Abort_Present);
8538 -- newS (new, Pnn);
8539 -- goto Lnn;
8540 -- <rest of statement sequence for accept statement>
8541 -- <<Lnn>>
8542 -- Complete_Rendezvous;
8543
8544 -- exception
8545 -- when all others =>
8546 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8547
8548 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8549 -- marked by pragma Implemented (XXX, By_Entry).
8550
8551 -- The requeue is inside a protected entry:
8552
8553 -- procedure entE
8554 -- (O : System.Address;
8555 -- P : System.Address;
8556 -- E : Protected_Entry_Index)
8557 -- is
8558 -- <discriminant renamings>
8559 -- <private object renamings>
8560 -- type poVP is access poV;
8561 -- _object : ptVP := ptVP!(O);
8562
8563 -- begin
8564 -- begin
8565 -- <start of statement sequence for entry>
8566
8567 -- _Disp_Requeue
8568 -- (<interface class-wide object>,
8569 -- True,
8570 -- _object'Address,
8571 -- Ada.Tags.Get_Offset_Index
8572 -- (Tag (_object),
8573 -- <interface dispatch table index of target entry>),
8574 -- Abort_Present);
8575 -- return;
8576
8577 -- <rest of statement sequence for entry>
8578 -- Complete_Entry_Body (_object._object);
8579
8580 -- exception
8581 -- when all others =>
8582 -- Exceptional_Complete_Entry_Body (
8583 -- _object._object, Get_GNAT_Exception);
8584 -- end;
8585 -- end entE;
8586
8587 -- The requeue is inside a task entry:
8588
8589 -- Accept_Call (E, Ann);
8590 -- <start of statement sequence for accept statement>
8591 -- _Disp_Requeue
8592 -- (<interface class-wide object>,
8593 -- False,
8594 -- null,
8595 -- Ada.Tags.Get_Offset_Index
8596 -- (Tag (_object),
8597 -- <interface dispatch table index of target entrt>),
8598 -- Abort_Present);
8599 -- newS (new, Pnn);
8600 -- goto Lnn;
8601 -- <rest of statement sequence for accept statement>
8602 -- <<Lnn>>
8603 -- Complete_Rendezvous;
8604
8605 -- exception
8606 -- when all others =>
8607 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8608
8609 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8610 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
8611 -- statement is replaced by a dispatching call with actual parameters taken
8612 -- from the inner-most accept statement or entry body.
8613
8614 -- Target.Primitive (Param1, ..., ParamN);
8615
8616 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8617 -- marked by pragma Implemented (XXX, By_Any) or not marked at all.
8618
8619 -- declare
8620 -- S : constant Offset_Index :=
8621 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
8622 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
8623
8624 -- begin
8625 -- if C = POK_Protected_Entry
8626 -- or else C = POK_Task_Entry
8627 -- then
8628 -- <statements for dispatching requeue>
8629
8630 -- elsif C = POK_Protected_Procedure then
8631 -- <dispatching call equivalent>
8632
8633 -- else
8634 -- raise Program_Error;
8635 -- end if;
8636 -- end;
8637
8638 procedure Expand_N_Requeue_Statement (N : Node_Id) is
8639 Loc : constant Source_Ptr := Sloc (N);
8640 Conc_Typ : Entity_Id;
8641 Concval : Node_Id;
8642 Ename : Node_Id;
8643 Index : Node_Id;
8644 Old_Typ : Entity_Id;
8645
8646 function Build_Dispatching_Call_Equivalent return Node_Id;
8647 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8648 -- the form Concval.Ename. It is statically known that Ename is allowed
8649 -- to be implemented by a protected procedure. Create a dispatching call
8650 -- equivalent of Concval.Ename taking the actual parameters from the
8651 -- inner-most accept statement or entry body.
8652
8653 function Build_Dispatching_Requeue return Node_Id;
8654 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8655 -- the form Concval.Ename. It is statically known that Ename is allowed
8656 -- to be implemented by a protected or a task entry. Create a call to
8657 -- primitive _Disp_Requeue which handles the low-level actions.
8658
8659 function Build_Dispatching_Requeue_To_Any return Node_Id;
8660 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8661 -- the form Concval.Ename. Ename is either marked by pragma Implemented
8662 -- (XXX, By_Any) or not marked at all. Create a block which determines
8663 -- at runtime whether Ename denotes an entry or a procedure and perform
8664 -- the appropriate kind of dispatching select.
8665
8666 function Build_Normal_Requeue return Node_Id;
8667 -- N denotes a non-dispatching requeue statement to either a task or a
8668 -- protected entry. Build the appropriate runtime call to perform the
8669 -- action.
8670
8671 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
8672 -- For a protected entry, create a return statement to skip the rest of
8673 -- the entry body. Otherwise, create a goto statement to skip the rest
8674 -- of a task accept statement. The lookup for the enclosing entry body
8675 -- or accept statement starts from Search.
8676
8677 ---------------------------------------
8678 -- Build_Dispatching_Call_Equivalent --
8679 ---------------------------------------
8680
8681 function Build_Dispatching_Call_Equivalent return Node_Id is
8682 Call_Ent : constant Entity_Id := Entity (Ename);
8683 Obj : constant Node_Id := Original_Node (Concval);
8684 Acc_Ent : Node_Id;
8685 Actuals : List_Id;
8686 Formal : Node_Id;
8687 Formals : List_Id;
8688
8689 begin
8690 -- Climb the parent chain looking for the inner-most entry body or
8691 -- accept statement.
8692
8693 Acc_Ent := N;
8694 while Present (Acc_Ent)
8695 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
8696 N_Entry_Body)
8697 loop
8698 Acc_Ent := Parent (Acc_Ent);
8699 end loop;
8700
8701 -- A requeue statement should be housed inside an entry body or an
8702 -- accept statement at some level. If this is not the case, then the
8703 -- tree is malformed.
8704
8705 pragma Assert (Present (Acc_Ent));
8706
8707 -- Recover the list of formal parameters
8708
8709 if Nkind (Acc_Ent) = N_Entry_Body then
8710 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
8711 end if;
8712
8713 Formals := Parameter_Specifications (Acc_Ent);
8714
8715 -- Create the actual parameters for the dispatching call. These are
8716 -- simply copies of the entry body or accept statement formals in the
8717 -- same order as they appear.
8718
8719 Actuals := No_List;
8720
8721 if Present (Formals) then
8722 Actuals := New_List;
8723 Formal := First (Formals);
8724 while Present (Formal) loop
8725 Append_To (Actuals,
8726 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8727 Next (Formal);
8728 end loop;
8729 end if;
8730
8731 -- Generate:
8732 -- Obj.Call_Ent (Actuals);
8733
8734 return
8735 Make_Procedure_Call_Statement (Loc,
8736 Name =>
8737 Make_Selected_Component (Loc,
8738 Prefix => Make_Identifier (Loc, Chars (Obj)),
8739 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
8740
8741 Parameter_Associations => Actuals);
8742 end Build_Dispatching_Call_Equivalent;
8743
8744 -------------------------------
8745 -- Build_Dispatching_Requeue --
8746 -------------------------------
8747
8748 function Build_Dispatching_Requeue return Node_Id is
8749 Params : constant List_Id := New_List;
8750
8751 begin
8752 -- Process the "with abort" parameter
8753
8754 Prepend_To (Params,
8755 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
8756
8757 -- Process the entry wrapper's position in the primary dispatch
8758 -- table parameter. Generate:
8759
8760 -- Ada.Tags.Get_Offset_Index
8761 -- (Ada.Tags.Tag (Concval),
8762 -- <interface dispatch table position of Ename>)
8763
8764 if Tagged_Type_Expansion then
8765 Prepend_To (Params,
8766 Make_Function_Call (Loc,
8767 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
8768 Parameter_Associations => New_List (
8769 Unchecked_Convert_To (RTE (RE_Tag), Concval),
8770 Make_Integer_Literal (Loc, DT_Position (Entity (Ename))))));
8771
8772 -- VM targets
8773
8774 else
8775 Prepend_To (Params,
8776 Make_Function_Call (Loc,
8777 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
8778
8779 Parameter_Associations => New_List (
8780
8781 -- Obj_Typ
8782
8783 Make_Attribute_Reference (Loc,
8784 Prefix => Concval,
8785 Attribute_Name => Name_Tag),
8786
8787 -- Tag_Typ
8788
8789 Make_Attribute_Reference (Loc,
8790 Prefix => New_Reference_To (Etype (Concval), Loc),
8791 Attribute_Name => Name_Tag),
8792
8793 -- Position
8794
8795 Make_Integer_Literal (Loc, DT_Position (Entity (Ename))))));
8796 end if;
8797
8798 -- Specific actuals for protected to XXX requeue
8799
8800 if Is_Protected_Type (Old_Typ) then
8801 Prepend_To (Params,
8802 Make_Attribute_Reference (Loc, -- _object'Address
8803 Prefix =>
8804 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
8805 Attribute_Name => Name_Address));
8806
8807 Prepend_To (Params, -- True
8808 New_Reference_To (Standard_True, Loc));
8809
8810 -- Specific actuals for task to XXX requeue
8811
8812 else
8813 pragma Assert (Is_Task_Type (Old_Typ));
8814
8815 Prepend_To (Params, -- null
8816 New_Reference_To (RTE (RE_Null_Address), Loc));
8817
8818 Prepend_To (Params, -- False
8819 New_Reference_To (Standard_False, Loc));
8820 end if;
8821
8822 -- Add the object parameter
8823
8824 Prepend_To (Params, New_Copy_Tree (Concval));
8825
8826 -- Generate:
8827 -- _Disp_Requeue (<Params>);
8828
8829 return
8830 Make_Procedure_Call_Statement (Loc,
8831 Name => Make_Identifier (Loc, Name_uDisp_Requeue),
8832 Parameter_Associations => Params);
8833 end Build_Dispatching_Requeue;
8834
8835 --------------------------------------
8836 -- Build_Dispatching_Requeue_To_Any --
8837 --------------------------------------
8838
8839 function Build_Dispatching_Requeue_To_Any return Node_Id is
8840 Call_Ent : constant Entity_Id := Entity (Ename);
8841 Obj : constant Node_Id := Original_Node (Concval);
8842 Skip : constant Node_Id := Build_Skip_Statement (N);
8843 C : Entity_Id;
8844 Decls : List_Id;
8845 S : Entity_Id;
8846 Stmts : List_Id;
8847
8848 begin
8849 Decls := New_List;
8850 Stmts := New_List;
8851
8852 -- Dispatch table slot processing, generate:
8853 -- S : Integer;
8854
8855 S := Build_S (Loc, Decls);
8856
8857 -- Call kind processing, generate:
8858 -- C : Ada.Tags.Prim_Op_Kind;
8859
8860 C := Build_C (Loc, Decls);
8861
8862 -- Generate:
8863 -- S := Ada.Tags.Get_Offset_Index
8864 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
8865
8866 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
8867
8868 -- Generate:
8869 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
8870
8871 Append_To (Stmts,
8872 Make_Procedure_Call_Statement (Loc,
8873 Name =>
8874 New_Reference_To (
8875 Find_Prim_Op (Etype (Etype (Obj)),
8876 Name_uDisp_Get_Prim_Op_Kind),
8877 Loc),
8878 Parameter_Associations => New_List (
8879 New_Copy_Tree (Obj),
8880 New_Reference_To (S, Loc),
8881 New_Reference_To (C, Loc))));
8882
8883 Append_To (Stmts,
8884
8885 -- if C = POK_Protected_Entry
8886 -- or else C = POK_Task_Entry
8887 -- then
8888
8889 Make_If_Statement (Loc,
8890 Condition =>
8891 Make_Op_Or (Loc,
8892 Left_Opnd =>
8893 Make_Op_Eq (Loc,
8894 Left_Opnd =>
8895 New_Reference_To (C, Loc),
8896 Right_Opnd =>
8897 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
8898
8899 Right_Opnd =>
8900 Make_Op_Eq (Loc,
8901 Left_Opnd =>
8902 New_Reference_To (C, Loc),
8903 Right_Opnd =>
8904 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
8905
8906 -- Dispatching requeue equivalent
8907
8908 Then_Statements => New_List (
8909 Build_Dispatching_Requeue,
8910 Skip),
8911
8912 -- elsif C = POK_Protected_Procedure then
8913
8914 Elsif_Parts => New_List (
8915 Make_Elsif_Part (Loc,
8916 Condition =>
8917 Make_Op_Eq (Loc,
8918 Left_Opnd =>
8919 New_Reference_To (C, Loc),
8920 Right_Opnd =>
8921 New_Reference_To (
8922 RTE (RE_POK_Protected_Procedure), Loc)),
8923
8924 -- Dispatching call equivalent
8925
8926 Then_Statements => New_List (
8927 Build_Dispatching_Call_Equivalent))),
8928
8929 -- else
8930 -- raise Program_Error;
8931 -- end if;
8932
8933 Else_Statements => New_List (
8934 Make_Raise_Program_Error (Loc,
8935 Reason => PE_Explicit_Raise))));
8936
8937 -- Wrap everything into a block
8938
8939 return
8940 Make_Block_Statement (Loc,
8941 Declarations => Decls,
8942 Handled_Statement_Sequence =>
8943 Make_Handled_Sequence_Of_Statements (Loc,
8944 Statements => Stmts));
8945 end Build_Dispatching_Requeue_To_Any;
8946
8947 --------------------------
8948 -- Build_Normal_Requeue --
8949 --------------------------
8950
8951 function Build_Normal_Requeue return Node_Id is
8952 Params : constant List_Id := New_List;
8953 Param : Node_Id;
8954 RT_Call : Node_Id;
8955
8956 begin
8957 -- Process the "with abort" parameter
8958
8959 Prepend_To (Params,
8960 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
8961
8962 -- Add the index expression to the parameters. It is common among all
8963 -- four cases.
8964
8965 Prepend_To (Params,
8966 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
8967
8968 if Is_Protected_Type (Old_Typ) then
8969 declare
8970 Self_Param : Node_Id;
8971
8972 begin
8973 Self_Param :=
8974 Make_Attribute_Reference (Loc,
8975 Prefix =>
8976 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
8977 Attribute_Name =>
8978 Name_Unchecked_Access);
8979
8980 -- Protected to protected requeue
8981
8982 if Is_Protected_Type (Conc_Typ) then
8983 RT_Call :=
8984 New_Reference_To (
8985 RTE (RE_Requeue_Protected_Entry), Loc);
8986
8987 Param :=
8988 Make_Attribute_Reference (Loc,
8989 Prefix =>
8990 Concurrent_Ref (Concval),
8991 Attribute_Name =>
8992 Name_Unchecked_Access);
8993
8994 -- Protected to task requeue
8995
8996 else pragma Assert (Is_Task_Type (Conc_Typ));
8997 RT_Call :=
8998 New_Reference_To (
8999 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9000
9001 Param := Concurrent_Ref (Concval);
9002 end if;
9003
9004 Prepend_To (Params, Param);
9005 Prepend_To (Params, Self_Param);
9006 end;
9007
9008 else pragma Assert (Is_Task_Type (Old_Typ));
9009
9010 -- Task to protected requeue
9011
9012 if Is_Protected_Type (Conc_Typ) then
9013 RT_Call :=
9014 New_Reference_To (
9015 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
9016
9017 Param :=
9018 Make_Attribute_Reference (Loc,
9019 Prefix =>
9020 Concurrent_Ref (Concval),
9021 Attribute_Name =>
9022 Name_Unchecked_Access);
9023
9024 -- Task to task requeue
9025
9026 else pragma Assert (Is_Task_Type (Conc_Typ));
9027 RT_Call :=
9028 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
9029
9030 Param := Concurrent_Ref (Concval);
9031 end if;
9032
9033 Prepend_To (Params, Param);
9034 end if;
9035
9036 return
9037 Make_Procedure_Call_Statement (Loc,
9038 Name => RT_Call,
9039 Parameter_Associations => Params);
9040 end Build_Normal_Requeue;
9041
9042 --------------------------
9043 -- Build_Skip_Statement --
9044 --------------------------
9045
9046 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
9047 Skip_Stmt : Node_Id;
9048
9049 begin
9050 -- Build a return statement to skip the rest of the entire body
9051
9052 if Is_Protected_Type (Old_Typ) then
9053 Skip_Stmt := Make_Simple_Return_Statement (Loc);
9054
9055 -- If the requeue is within a task, find the end label of the
9056 -- enclosing accept statement and create a goto statement to it.
9057
9058 else
9059 declare
9060 Acc : Node_Id;
9061 Label : Node_Id;
9062
9063 begin
9064 -- Climb the parent chain looking for the enclosing accept
9065 -- statement.
9066
9067 Acc := Parent (Search);
9068 while Present (Acc)
9069 and then Nkind (Acc) /= N_Accept_Statement
9070 loop
9071 Acc := Parent (Acc);
9072 end loop;
9073
9074 -- The last statement is the second label used for completing
9075 -- the rendezvous the usual way. The label we are looking for
9076 -- is right before it.
9077
9078 Label :=
9079 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
9080
9081 pragma Assert (Nkind (Label) = N_Label);
9082
9083 -- Generate a goto statement to skip the rest of the accept
9084
9085 Skip_Stmt :=
9086 Make_Goto_Statement (Loc,
9087 Name =>
9088 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
9089 end;
9090 end if;
9091
9092 Set_Analyzed (Skip_Stmt);
9093
9094 return Skip_Stmt;
9095 end Build_Skip_Statement;
9096
9097 -- Start of processing for Expand_N_Requeue_Statement
9098
9099 begin
9100 -- Extract the components of the entry call
9101
9102 Extract_Entry (N, Concval, Ename, Index);
9103 Conc_Typ := Etype (Concval);
9104
9105 -- Examine the scope stack in order to find nearest enclosing protected
9106 -- or task type. This will constitute our invocation source.
9107
9108 Old_Typ := Current_Scope;
9109 while Present (Old_Typ)
9110 and then not Is_Protected_Type (Old_Typ)
9111 and then not Is_Task_Type (Old_Typ)
9112 loop
9113 Old_Typ := Scope (Old_Typ);
9114 end loop;
9115
9116 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
9117 -- Concval.Ename where the type of Concval is class-wide concurrent
9118 -- interface.
9119
9120 if Ada_Version >= Ada_2012
9121 and then Present (Concval)
9122 and then Is_Class_Wide_Type (Conc_Typ)
9123 and then Is_Concurrent_Interface (Conc_Typ)
9124 then
9125 declare
9126 Has_Impl : Boolean := False;
9127 Impl_Kind : Name_Id := No_Name;
9128
9129 begin
9130 -- Check whether the Ename is flagged by pragma Implemented
9131
9132 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
9133 Has_Impl := True;
9134 Impl_Kind := Implementation_Kind (Entity (Ename));
9135 end if;
9136
9137 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9138 -- an entry. Create a call to predefined primitive _Disp_Requeue.
9139
9140 if Has_Impl
9141 and then Impl_Kind = Name_By_Entry
9142 then
9143 Rewrite (N, Build_Dispatching_Requeue);
9144 Analyze (N);
9145 Insert_After (N, Build_Skip_Statement (N));
9146
9147 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9148 -- a protected procedure. In this case the requeue is transformed
9149 -- into a dispatching call.
9150
9151 elsif Has_Impl
9152 and then Impl_Kind = Name_By_Protected_Procedure
9153 then
9154 Rewrite (N, Build_Dispatching_Call_Equivalent);
9155 Analyze (N);
9156
9157 -- The procedure_or_entry_NAME's implementation kind is either
9158 -- By_Any or pragma Implemented was not applied at all. In this
9159 -- case a runtime test determines whether Ename denotes an entry
9160 -- or a protected procedure and performs the appropriate call.
9161
9162 else
9163 Rewrite (N, Build_Dispatching_Requeue_To_Any);
9164 Analyze (N);
9165 end if;
9166 end;
9167
9168 -- Processing for regular (non-dispatching) requeues
9169
9170 else
9171 Rewrite (N, Build_Normal_Requeue);
9172 Analyze (N);
9173 Insert_After (N, Build_Skip_Statement (N));
9174 end if;
9175 end Expand_N_Requeue_Statement;
9176
9177 -------------------------------
9178 -- Expand_N_Selective_Accept --
9179 -------------------------------
9180
9181 procedure Expand_N_Selective_Accept (N : Node_Id) is
9182 Loc : constant Source_Ptr := Sloc (N);
9183 Alts : constant List_Id := Select_Alternatives (N);
9184
9185 -- Note: in the below declarations a lot of new lists are allocated
9186 -- unconditionally which may well not end up being used. That's
9187 -- not a good idea since it wastes space gratuitously ???
9188
9189 Accept_Case : List_Id;
9190 Accept_List : constant List_Id := New_List;
9191
9192 Alt : Node_Id;
9193 Alt_List : constant List_Id := New_List;
9194 Alt_Stats : List_Id;
9195 Ann : Entity_Id := Empty;
9196
9197 Block : Node_Id;
9198 Check_Guard : Boolean := True;
9199
9200 Decls : constant List_Id := New_List;
9201 Stats : constant List_Id := New_List;
9202 Body_List : constant List_Id := New_List;
9203 Trailing_List : constant List_Id := New_List;
9204
9205 Choices : List_Id;
9206 Else_Present : Boolean := False;
9207 Terminate_Alt : Node_Id := Empty;
9208 Select_Mode : Node_Id;
9209
9210 Delay_Case : List_Id;
9211 Delay_Count : Integer := 0;
9212 Delay_Val : Entity_Id;
9213 Delay_Index : Entity_Id;
9214 Delay_Min : Entity_Id;
9215 Delay_Num : Int := 1;
9216 Delay_Alt_List : List_Id := New_List;
9217 Delay_List : constant List_Id := New_List;
9218 D : Entity_Id;
9219 M : Entity_Id;
9220
9221 First_Delay : Boolean := True;
9222 Guard_Open : Entity_Id;
9223
9224 End_Lab : Node_Id;
9225 Index : Int := 1;
9226 Lab : Node_Id;
9227 Num_Alts : Int;
9228 Num_Accept : Nat := 0;
9229 Proc : Node_Id;
9230 Q : Node_Id;
9231 Time_Type : Entity_Id;
9232 X : Node_Id;
9233 Select_Call : Node_Id;
9234
9235 Qnam : constant Entity_Id :=
9236 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
9237
9238 Xnam : constant Entity_Id :=
9239 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
9240
9241 -----------------------
9242 -- Local subprograms --
9243 -----------------------
9244
9245 function Accept_Or_Raise return List_Id;
9246 -- For the rare case where delay alternatives all have guards, and
9247 -- all of them are closed, it is still possible that there were open
9248 -- accept alternatives with no callers. We must reexamine the
9249 -- Accept_List, and execute a selective wait with no else if some
9250 -- accept is open. If none, we raise program_error.
9251
9252 procedure Add_Accept (Alt : Node_Id);
9253 -- Process a single accept statement in a select alternative. Build
9254 -- procedure for body of accept, and add entry to dispatch table with
9255 -- expression for guard, in preparation for call to run time select.
9256
9257 function Make_And_Declare_Label (Num : Int) return Node_Id;
9258 -- Manufacture a label using Num as a serial number and declare it.
9259 -- The declaration is appended to Decls. The label marks the trailing
9260 -- statements of an accept or delay alternative.
9261
9262 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
9263 -- Build call to Selective_Wait runtime routine
9264
9265 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
9266 -- Add code to compare value of delay with previous values, and
9267 -- generate case entry for trailing statements.
9268
9269 procedure Process_Accept_Alternative
9270 (Alt : Node_Id;
9271 Index : Int;
9272 Proc : Node_Id);
9273 -- Add code to call corresponding procedure, and branch to
9274 -- trailing statements, if any.
9275
9276 ---------------------
9277 -- Accept_Or_Raise --
9278 ---------------------
9279
9280 function Accept_Or_Raise return List_Id is
9281 Cond : Node_Id;
9282 Stats : List_Id;
9283 J : constant Entity_Id := Make_Temporary (Loc, 'J');
9284
9285 begin
9286 -- We generate the following:
9287
9288 -- for J in q'range loop
9289 -- if q(J).S /=null_task_entry then
9290 -- selective_wait (simple_mode,...);
9291 -- done := True;
9292 -- exit;
9293 -- end if;
9294 -- end loop;
9295 --
9296 -- if no rendez_vous then
9297 -- raise program_error;
9298 -- end if;
9299
9300 -- Note that the code needs to know that the selector name
9301 -- in an Accept_Alternative is named S.
9302
9303 Cond := Make_Op_Ne (Loc,
9304 Left_Opnd =>
9305 Make_Selected_Component (Loc,
9306 Prefix =>
9307 Make_Indexed_Component (Loc,
9308 Prefix => New_Reference_To (Qnam, Loc),
9309 Expressions => New_List (New_Reference_To (J, Loc))),
9310 Selector_Name => Make_Identifier (Loc, Name_S)),
9311 Right_Opnd =>
9312 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
9313
9314 Stats := New_List (
9315 Make_Implicit_Loop_Statement (N,
9316 Identifier => Empty,
9317 Iteration_Scheme =>
9318 Make_Iteration_Scheme (Loc,
9319 Loop_Parameter_Specification =>
9320 Make_Loop_Parameter_Specification (Loc,
9321 Defining_Identifier => J,
9322 Discrete_Subtype_Definition =>
9323 Make_Attribute_Reference (Loc,
9324 Prefix => New_Reference_To (Qnam, Loc),
9325 Attribute_Name => Name_Range,
9326 Expressions => New_List (
9327 Make_Integer_Literal (Loc, 1))))),
9328
9329 Statements => New_List (
9330 Make_Implicit_If_Statement (N,
9331 Condition => Cond,
9332 Then_Statements => New_List (
9333 Make_Select_Call (
9334 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
9335 Make_Exit_Statement (Loc))))));
9336
9337 Append_To (Stats,
9338 Make_Raise_Program_Error (Loc,
9339 Condition => Make_Op_Eq (Loc,
9340 Left_Opnd => New_Reference_To (Xnam, Loc),
9341 Right_Opnd =>
9342 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
9343 Reason => PE_All_Guards_Closed));
9344
9345 return Stats;
9346 end Accept_Or_Raise;
9347
9348 ----------------
9349 -- Add_Accept --
9350 ----------------
9351
9352 procedure Add_Accept (Alt : Node_Id) is
9353 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
9354 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
9355 Eloc : constant Source_Ptr := Sloc (Ename);
9356 Eent : constant Entity_Id := Entity (Ename);
9357 Index : constant Node_Id := Entry_Index (Acc_Stm);
9358 Null_Body : Node_Id;
9359 Proc_Body : Node_Id;
9360 PB_Ent : Entity_Id;
9361 Expr : Node_Id;
9362 Call : Node_Id;
9363
9364 begin
9365 if No (Ann) then
9366 Ann := Node (Last_Elmt (Accept_Address (Eent)));
9367 end if;
9368
9369 if Present (Condition (Alt)) then
9370 Expr :=
9371 Make_Conditional_Expression (Eloc, New_List (
9372 Condition (Alt),
9373 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
9374 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
9375 else
9376 Expr :=
9377 Entry_Index_Expression
9378 (Eloc, Eent, Index, Scope (Eent));
9379 end if;
9380
9381 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
9382 Null_Body := New_Reference_To (Standard_False, Eloc);
9383
9384 if Abort_Allowed then
9385 Call := Make_Procedure_Call_Statement (Eloc,
9386 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
9387 Insert_Before (First (Statements (Handled_Statement_Sequence (
9388 Accept_Statement (Alt)))), Call);
9389 Analyze (Call);
9390 end if;
9391
9392 PB_Ent :=
9393 Make_Defining_Identifier (Eloc,
9394 New_External_Name (Chars (Ename), 'A', Num_Accept));
9395
9396 if Comes_From_Source (Alt) then
9397 Set_Debug_Info_Needed (PB_Ent);
9398 end if;
9399
9400 Proc_Body :=
9401 Make_Subprogram_Body (Eloc,
9402 Specification =>
9403 Make_Procedure_Specification (Eloc,
9404 Defining_Unit_Name => PB_Ent),
9405 Declarations => Declarations (Acc_Stm),
9406 Handled_Statement_Sequence =>
9407 Build_Accept_Body (Accept_Statement (Alt)));
9408
9409 -- During the analysis of the body of the accept statement, any
9410 -- zero cost exception handler records were collected in the
9411 -- Accept_Handler_Records field of the N_Accept_Alternative node.
9412 -- This is where we move them to where they belong, namely the
9413 -- newly created procedure.
9414
9415 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
9416 Append (Proc_Body, Body_List);
9417
9418 else
9419 Null_Body := New_Reference_To (Standard_True, Eloc);
9420
9421 -- if accept statement has declarations, insert above, given that
9422 -- we are not creating a body for the accept.
9423
9424 if Present (Declarations (Acc_Stm)) then
9425 Insert_Actions (N, Declarations (Acc_Stm));
9426 end if;
9427 end if;
9428
9429 Append_To (Accept_List,
9430 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
9431
9432 Num_Accept := Num_Accept + 1;
9433 end Add_Accept;
9434
9435 ----------------------------
9436 -- Make_And_Declare_Label --
9437 ----------------------------
9438
9439 function Make_And_Declare_Label (Num : Int) return Node_Id is
9440 Lab_Id : Node_Id;
9441
9442 begin
9443 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
9444 Lab :=
9445 Make_Label (Loc, Lab_Id);
9446
9447 Append_To (Decls,
9448 Make_Implicit_Label_Declaration (Loc,
9449 Defining_Identifier =>
9450 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
9451 Label_Construct => Lab));
9452
9453 return Lab;
9454 end Make_And_Declare_Label;
9455
9456 ----------------------
9457 -- Make_Select_Call --
9458 ----------------------
9459
9460 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
9461 Params : constant List_Id := New_List;
9462
9463 begin
9464 Append (
9465 Make_Attribute_Reference (Loc,
9466 Prefix => New_Reference_To (Qnam, Loc),
9467 Attribute_Name => Name_Unchecked_Access),
9468 Params);
9469 Append (Select_Mode, Params);
9470 Append (New_Reference_To (Ann, Loc), Params);
9471 Append (New_Reference_To (Xnam, Loc), Params);
9472
9473 return
9474 Make_Procedure_Call_Statement (Loc,
9475 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
9476 Parameter_Associations => Params);
9477 end Make_Select_Call;
9478
9479 --------------------------------
9480 -- Process_Accept_Alternative --
9481 --------------------------------
9482
9483 procedure Process_Accept_Alternative
9484 (Alt : Node_Id;
9485 Index : Int;
9486 Proc : Node_Id)
9487 is
9488 Choices : List_Id := No_List;
9489 Alt_Stats : List_Id;
9490
9491 begin
9492 Adjust_Condition (Condition (Alt));
9493 Alt_Stats := No_List;
9494
9495 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
9496 Choices := New_List (
9497 Make_Integer_Literal (Loc, Index));
9498
9499 Alt_Stats := New_List (
9500 Make_Procedure_Call_Statement (Sloc (Proc),
9501 Name => New_Reference_To (
9502 Defining_Unit_Name (Specification (Proc)), Sloc (Proc))));
9503 end if;
9504
9505 if Statements (Alt) /= Empty_List then
9506
9507 if No (Alt_Stats) then
9508
9509 -- Accept with no body, followed by trailing statements
9510
9511 Choices := New_List (
9512 Make_Integer_Literal (Loc, Index));
9513
9514 Alt_Stats := New_List;
9515 end if;
9516
9517 -- After the call, if any, branch to trailing statements. We
9518 -- create a label for each, as well as the corresponding label
9519 -- declaration.
9520
9521 Lab := Make_And_Declare_Label (Index);
9522 Append_To (Alt_Stats,
9523 Make_Goto_Statement (Loc,
9524 Name => New_Copy (Identifier (Lab))));
9525
9526 Append (Lab, Trailing_List);
9527 Append_List (Statements (Alt), Trailing_List);
9528 Append_To (Trailing_List,
9529 Make_Goto_Statement (Loc,
9530 Name => New_Copy (Identifier (End_Lab))));
9531 end if;
9532
9533 if Present (Alt_Stats) then
9534
9535 -- Procedure call. and/or trailing statements
9536
9537 Append_To (Alt_List,
9538 Make_Case_Statement_Alternative (Loc,
9539 Discrete_Choices => Choices,
9540 Statements => Alt_Stats));
9541 end if;
9542 end Process_Accept_Alternative;
9543
9544 -------------------------------
9545 -- Process_Delay_Alternative --
9546 -------------------------------
9547
9548 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
9549 Choices : List_Id;
9550 Cond : Node_Id;
9551 Delay_Alt : List_Id;
9552
9553 begin
9554 -- Deal with C/Fortran boolean as delay condition
9555
9556 Adjust_Condition (Condition (Alt));
9557
9558 -- Determine the smallest specified delay
9559
9560 -- for each delay alternative generate:
9561
9562 -- if guard-expression then
9563 -- Delay_Val := delay-expression;
9564 -- Guard_Open := True;
9565 -- if Delay_Val < Delay_Min then
9566 -- Delay_Min := Delay_Val;
9567 -- Delay_Index := Index;
9568 -- end if;
9569 -- end if;
9570
9571 -- The enclosing if-statement is omitted if there is no guard
9572
9573 if Delay_Count = 1
9574 or else First_Delay
9575 then
9576 First_Delay := False;
9577
9578 Delay_Alt := New_List (
9579 Make_Assignment_Statement (Loc,
9580 Name => New_Reference_To (Delay_Min, Loc),
9581 Expression => Expression (Delay_Statement (Alt))));
9582
9583 if Delay_Count > 1 then
9584 Append_To (Delay_Alt,
9585 Make_Assignment_Statement (Loc,
9586 Name => New_Reference_To (Delay_Index, Loc),
9587 Expression => Make_Integer_Literal (Loc, Index)));
9588 end if;
9589
9590 else
9591 Delay_Alt := New_List (
9592 Make_Assignment_Statement (Loc,
9593 Name => New_Reference_To (Delay_Val, Loc),
9594 Expression => Expression (Delay_Statement (Alt))));
9595
9596 if Time_Type = Standard_Duration then
9597 Cond :=
9598 Make_Op_Lt (Loc,
9599 Left_Opnd => New_Reference_To (Delay_Val, Loc),
9600 Right_Opnd => New_Reference_To (Delay_Min, Loc));
9601
9602 else
9603 -- The scope of the time type must define a comparison
9604 -- operator. The scope itself may not be visible, so we
9605 -- construct a node with entity information to insure that
9606 -- semantic analysis can find the proper operator.
9607
9608 Cond :=
9609 Make_Function_Call (Loc,
9610 Name => Make_Selected_Component (Loc,
9611 Prefix => New_Reference_To (Scope (Time_Type), Loc),
9612 Selector_Name =>
9613 Make_Operator_Symbol (Loc,
9614 Chars => Name_Op_Lt,
9615 Strval => No_String)),
9616 Parameter_Associations =>
9617 New_List (
9618 New_Reference_To (Delay_Val, Loc),
9619 New_Reference_To (Delay_Min, Loc)));
9620
9621 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
9622 end if;
9623
9624 Append_To (Delay_Alt,
9625 Make_Implicit_If_Statement (N,
9626 Condition => Cond,
9627 Then_Statements => New_List (
9628 Make_Assignment_Statement (Loc,
9629 Name => New_Reference_To (Delay_Min, Loc),
9630 Expression => New_Reference_To (Delay_Val, Loc)),
9631
9632 Make_Assignment_Statement (Loc,
9633 Name => New_Reference_To (Delay_Index, Loc),
9634 Expression => Make_Integer_Literal (Loc, Index)))));
9635 end if;
9636
9637 if Check_Guard then
9638 Append_To (Delay_Alt,
9639 Make_Assignment_Statement (Loc,
9640 Name => New_Reference_To (Guard_Open, Loc),
9641 Expression => New_Reference_To (Standard_True, Loc)));
9642 end if;
9643
9644 if Present (Condition (Alt)) then
9645 Delay_Alt := New_List (
9646 Make_Implicit_If_Statement (N,
9647 Condition => Condition (Alt),
9648 Then_Statements => Delay_Alt));
9649 end if;
9650
9651 Append_List (Delay_Alt, Delay_List);
9652
9653 -- If the delay alternative has a statement part, add choice to the
9654 -- case statements for delays.
9655
9656 if Present (Statements (Alt)) then
9657
9658 if Delay_Count = 1 then
9659 Append_List (Statements (Alt), Delay_Alt_List);
9660
9661 else
9662 Choices := New_List (
9663 Make_Integer_Literal (Loc, Index));
9664
9665 Append_To (Delay_Alt_List,
9666 Make_Case_Statement_Alternative (Loc,
9667 Discrete_Choices => Choices,
9668 Statements => Statements (Alt)));
9669 end if;
9670
9671 elsif Delay_Count = 1 then
9672
9673 -- If the single delay has no trailing statements, add a branch
9674 -- to the exit label to the selective wait.
9675
9676 Delay_Alt_List := New_List (
9677 Make_Goto_Statement (Loc,
9678 Name => New_Copy (Identifier (End_Lab))));
9679
9680 end if;
9681 end Process_Delay_Alternative;
9682
9683 -- Start of processing for Expand_N_Selective_Accept
9684
9685 begin
9686 Process_Statements_For_Controlled_Objects (N);
9687
9688 -- First insert some declarations before the select. The first is:
9689
9690 -- Ann : Address
9691
9692 -- This variable holds the parameters passed to the accept body. This
9693 -- declaration has already been inserted by the time we get here by
9694 -- a call to Expand_Accept_Declarations made from the semantics when
9695 -- processing the first accept statement contained in the select. We
9696 -- can find this entity as Accept_Address (E), where E is any of the
9697 -- entries references by contained accept statements.
9698
9699 -- The first step is to scan the list of Selective_Accept_Statements
9700 -- to find this entity, and also count the number of accepts, and
9701 -- determine if terminated, delay or else is present:
9702
9703 Num_Alts := 0;
9704
9705 Alt := First (Alts);
9706 while Present (Alt) loop
9707 Process_Statements_For_Controlled_Objects (Alt);
9708
9709 if Nkind (Alt) = N_Accept_Alternative then
9710 Add_Accept (Alt);
9711
9712 elsif Nkind (Alt) = N_Delay_Alternative then
9713 Delay_Count := Delay_Count + 1;
9714
9715 -- If the delays are relative delays, the delay expressions have
9716 -- type Standard_Duration. Otherwise they must have some time type
9717 -- recognized by GNAT.
9718
9719 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
9720 Time_Type := Standard_Duration;
9721 else
9722 Time_Type := Etype (Expression (Delay_Statement (Alt)));
9723
9724 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
9725 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
9726 then
9727 null;
9728 else
9729 Error_Msg_NE (
9730 "& is not a time type (RM 9.6(6))",
9731 Expression (Delay_Statement (Alt)), Time_Type);
9732 Time_Type := Standard_Duration;
9733 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
9734 end if;
9735 end if;
9736
9737 if No (Condition (Alt)) then
9738
9739 -- This guard will always be open
9740
9741 Check_Guard := False;
9742 end if;
9743
9744 elsif Nkind (Alt) = N_Terminate_Alternative then
9745 Adjust_Condition (Condition (Alt));
9746 Terminate_Alt := Alt;
9747 end if;
9748
9749 Num_Alts := Num_Alts + 1;
9750 Next (Alt);
9751 end loop;
9752
9753 Else_Present := Present (Else_Statements (N));
9754
9755 -- At the same time (see procedure Add_Accept) we build the accept list:
9756
9757 -- Qnn : Accept_List (1 .. num-select) := (
9758 -- (null-body, entry-index),
9759 -- (null-body, entry-index),
9760 -- ..
9761 -- (null_body, entry-index));
9762
9763 -- In the above declaration, null-body is True if the corresponding
9764 -- accept has no body, and false otherwise. The entry is either the
9765 -- entry index expression if there is no guard, or if a guard is
9766 -- present, then a conditional expression of the form:
9767
9768 -- (if guard then entry-index else Null_Task_Entry)
9769
9770 -- If a guard is statically known to be false, the entry can simply
9771 -- be omitted from the accept list.
9772
9773 Q :=
9774 Make_Object_Declaration (Loc,
9775 Defining_Identifier => Qnam,
9776 Object_Definition =>
9777 New_Reference_To (RTE (RE_Accept_List), Loc),
9778 Aliased_Present => True,
9779
9780 Expression =>
9781 Make_Qualified_Expression (Loc,
9782 Subtype_Mark =>
9783 New_Reference_To (RTE (RE_Accept_List), Loc),
9784 Expression =>
9785 Make_Aggregate (Loc, Expressions => Accept_List)));
9786
9787 Append (Q, Decls);
9788
9789 -- Then we declare the variable that holds the index for the accept
9790 -- that will be selected for service:
9791
9792 -- Xnn : Select_Index;
9793
9794 X :=
9795 Make_Object_Declaration (Loc,
9796 Defining_Identifier => Xnam,
9797 Object_Definition =>
9798 New_Reference_To (RTE (RE_Select_Index), Loc),
9799 Expression =>
9800 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
9801
9802 Append (X, Decls);
9803
9804 -- After this follow procedure declarations for each accept body
9805
9806 -- procedure Pnn is
9807 -- begin
9808 -- ...
9809 -- end;
9810
9811 -- where the ... are statements from the corresponding procedure body.
9812 -- No parameters are involved, since the parameters are passed via Ann
9813 -- and the parameter references have already been expanded to be direct
9814 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
9815 -- any embedded tasking statements (which would normally be illegal in
9816 -- procedures), have been converted to calls to the tasking runtime so
9817 -- there is no problem in putting them into procedures.
9818
9819 -- The original accept statement has been expanded into a block in
9820 -- the same fashion as for simple accepts (see Build_Accept_Body).
9821
9822 -- Note: we don't really need to build these procedures for the case
9823 -- where no delay statement is present, but it is just as easy to
9824 -- build them unconditionally, and not significantly inefficient,
9825 -- since if they are short they will be inlined anyway.
9826
9827 -- The procedure declarations have been assembled in Body_List
9828
9829 -- If delays are present, we must compute the required delay.
9830 -- We first generate the declarations:
9831
9832 -- Delay_Index : Boolean := 0;
9833 -- Delay_Min : Some_Time_Type.Time;
9834 -- Delay_Val : Some_Time_Type.Time;
9835
9836 -- Delay_Index will be set to the index of the minimum delay, i.e. the
9837 -- active delay that is actually chosen as the basis for the possible
9838 -- delay if an immediate rendez-vous is not possible.
9839
9840 -- In the most common case there is a single delay statement, and this
9841 -- is handled specially.
9842
9843 if Delay_Count > 0 then
9844
9845 -- Generate the required declarations
9846
9847 Delay_Val :=
9848 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
9849 Delay_Index :=
9850 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
9851 Delay_Min :=
9852 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
9853
9854 Append_To (Decls,
9855 Make_Object_Declaration (Loc,
9856 Defining_Identifier => Delay_Val,
9857 Object_Definition => New_Reference_To (Time_Type, Loc)));
9858
9859 Append_To (Decls,
9860 Make_Object_Declaration (Loc,
9861 Defining_Identifier => Delay_Index,
9862 Object_Definition => New_Reference_To (Standard_Integer, Loc),
9863 Expression => Make_Integer_Literal (Loc, 0)));
9864
9865 Append_To (Decls,
9866 Make_Object_Declaration (Loc,
9867 Defining_Identifier => Delay_Min,
9868 Object_Definition => New_Reference_To (Time_Type, Loc),
9869 Expression =>
9870 Unchecked_Convert_To (Time_Type,
9871 Make_Attribute_Reference (Loc,
9872 Prefix =>
9873 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
9874 Attribute_Name => Name_Last))));
9875
9876 -- Create Duration and Delay_Mode objects used for passing a delay
9877 -- value to RTS
9878
9879 D := Make_Temporary (Loc, 'D');
9880 M := Make_Temporary (Loc, 'M');
9881
9882 declare
9883 Discr : Entity_Id;
9884
9885 begin
9886 -- Note that these values are defined in s-osprim.ads and must
9887 -- be kept in sync:
9888 --
9889 -- Relative : constant := 0;
9890 -- Absolute_Calendar : constant := 1;
9891 -- Absolute_RT : constant := 2;
9892
9893 if Time_Type = Standard_Duration then
9894 Discr := Make_Integer_Literal (Loc, 0);
9895
9896 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
9897 Discr := Make_Integer_Literal (Loc, 1);
9898
9899 else
9900 pragma Assert
9901 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
9902 Discr := Make_Integer_Literal (Loc, 2);
9903 end if;
9904
9905 Append_To (Decls,
9906 Make_Object_Declaration (Loc,
9907 Defining_Identifier => D,
9908 Object_Definition =>
9909 New_Reference_To (Standard_Duration, Loc)));
9910
9911 Append_To (Decls,
9912 Make_Object_Declaration (Loc,
9913 Defining_Identifier => M,
9914 Object_Definition =>
9915 New_Reference_To (Standard_Integer, Loc),
9916 Expression => Discr));
9917 end;
9918
9919 if Check_Guard then
9920 Guard_Open :=
9921 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
9922
9923 Append_To (Decls,
9924 Make_Object_Declaration (Loc,
9925 Defining_Identifier => Guard_Open,
9926 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
9927 Expression => New_Reference_To (Standard_False, Loc)));
9928 end if;
9929
9930 -- Delay_Count is zero, don't need M and D set (suppress warning)
9931
9932 else
9933 M := Empty;
9934 D := Empty;
9935 end if;
9936
9937 if Present (Terminate_Alt) then
9938
9939 -- If the terminate alternative guard is False, use
9940 -- Simple_Mode; otherwise use Terminate_Mode.
9941
9942 if Present (Condition (Terminate_Alt)) then
9943 Select_Mode := Make_Conditional_Expression (Loc,
9944 New_List (Condition (Terminate_Alt),
9945 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
9946 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
9947 else
9948 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
9949 end if;
9950
9951 elsif Else_Present or Delay_Count > 0 then
9952 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
9953
9954 else
9955 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
9956 end if;
9957
9958 Select_Call := Make_Select_Call (Select_Mode);
9959 Append (Select_Call, Stats);
9960
9961 -- Now generate code to act on the result. There is an entry
9962 -- in this case for each accept statement with a non-null body,
9963 -- followed by a branch to the statements that follow the Accept.
9964 -- In the absence of delay alternatives, we generate:
9965
9966 -- case X is
9967 -- when No_Rendezvous => -- omitted if simple mode
9968 -- goto Lab0;
9969
9970 -- when 1 =>
9971 -- P1n;
9972 -- goto Lab1;
9973
9974 -- when 2 =>
9975 -- P2n;
9976 -- goto Lab2;
9977
9978 -- when others =>
9979 -- goto Exit;
9980 -- end case;
9981 --
9982 -- Lab0: Else_Statements;
9983 -- goto exit;
9984
9985 -- Lab1: Trailing_Statements1;
9986 -- goto Exit;
9987 --
9988 -- Lab2: Trailing_Statements2;
9989 -- goto Exit;
9990 -- ...
9991 -- Exit:
9992
9993 -- Generate label for common exit
9994
9995 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
9996
9997 -- First entry is the default case, when no rendezvous is possible
9998
9999 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10000
10001 if Else_Present then
10002
10003 -- If no rendezvous is possible, the else part is executed
10004
10005 Lab := Make_And_Declare_Label (0);
10006 Alt_Stats := New_List (
10007 Make_Goto_Statement (Loc,
10008 Name => New_Copy (Identifier (Lab))));
10009
10010 Append (Lab, Trailing_List);
10011 Append_List (Else_Statements (N), Trailing_List);
10012 Append_To (Trailing_List,
10013 Make_Goto_Statement (Loc,
10014 Name => New_Copy (Identifier (End_Lab))));
10015 else
10016 Alt_Stats := New_List (
10017 Make_Goto_Statement (Loc,
10018 Name => New_Copy (Identifier (End_Lab))));
10019 end if;
10020
10021 Append_To (Alt_List,
10022 Make_Case_Statement_Alternative (Loc,
10023 Discrete_Choices => Choices,
10024 Statements => Alt_Stats));
10025
10026 -- We make use of the fact that Accept_Index is an integer type, and
10027 -- generate successive literals for entries for each accept. Only those
10028 -- for which there is a body or trailing statements get a case entry.
10029
10030 Alt := First (Select_Alternatives (N));
10031 Proc := First (Body_List);
10032 while Present (Alt) loop
10033
10034 if Nkind (Alt) = N_Accept_Alternative then
10035 Process_Accept_Alternative (Alt, Index, Proc);
10036 Index := Index + 1;
10037
10038 if Present
10039 (Handled_Statement_Sequence (Accept_Statement (Alt)))
10040 then
10041 Next (Proc);
10042 end if;
10043
10044 elsif Nkind (Alt) = N_Delay_Alternative then
10045 Process_Delay_Alternative (Alt, Delay_Num);
10046 Delay_Num := Delay_Num + 1;
10047 end if;
10048
10049 Next (Alt);
10050 end loop;
10051
10052 -- An others choice is always added to the main case, as well
10053 -- as the delay case (to satisfy the compiler).
10054
10055 Append_To (Alt_List,
10056 Make_Case_Statement_Alternative (Loc,
10057 Discrete_Choices =>
10058 New_List (Make_Others_Choice (Loc)),
10059 Statements =>
10060 New_List (Make_Goto_Statement (Loc,
10061 Name => New_Copy (Identifier (End_Lab))))));
10062
10063 Accept_Case := New_List (
10064 Make_Case_Statement (Loc,
10065 Expression => New_Reference_To (Xnam, Loc),
10066 Alternatives => Alt_List));
10067
10068 Append_List (Trailing_List, Accept_Case);
10069 Append (End_Lab, Accept_Case);
10070 Append_List (Body_List, Decls);
10071
10072 -- Construct case statement for trailing statements of delay
10073 -- alternatives, if there are several of them.
10074
10075 if Delay_Count > 1 then
10076 Append_To (Delay_Alt_List,
10077 Make_Case_Statement_Alternative (Loc,
10078 Discrete_Choices =>
10079 New_List (Make_Others_Choice (Loc)),
10080 Statements =>
10081 New_List (Make_Null_Statement (Loc))));
10082
10083 Delay_Case := New_List (
10084 Make_Case_Statement (Loc,
10085 Expression => New_Reference_To (Delay_Index, Loc),
10086 Alternatives => Delay_Alt_List));
10087 else
10088 Delay_Case := Delay_Alt_List;
10089 end if;
10090
10091 -- If there are no delay alternatives, we append the case statement
10092 -- to the statement list.
10093
10094 if Delay_Count = 0 then
10095 Append_List (Accept_Case, Stats);
10096
10097 -- Delay alternatives present
10098
10099 else
10100 -- If delay alternatives are present we generate:
10101
10102 -- find minimum delay.
10103 -- DX := minimum delay;
10104 -- M := <delay mode>;
10105 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
10106 -- DX, MX, X);
10107 --
10108 -- if X = No_Rendezvous then
10109 -- case statement for delay statements.
10110 -- else
10111 -- case statement for accept alternatives.
10112 -- end if;
10113
10114 declare
10115 Cases : Node_Id;
10116 Stmt : Node_Id;
10117 Parms : List_Id;
10118 Parm : Node_Id;
10119 Conv : Node_Id;
10120
10121 begin
10122 -- The type of the delay expression is known to be legal
10123
10124 if Time_Type = Standard_Duration then
10125 Conv := New_Reference_To (Delay_Min, Loc);
10126
10127 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10128 Conv := Make_Function_Call (Loc,
10129 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
10130 New_List (New_Reference_To (Delay_Min, Loc)));
10131
10132 else
10133 pragma Assert
10134 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10135
10136 Conv := Make_Function_Call (Loc,
10137 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
10138 New_List (New_Reference_To (Delay_Min, Loc)));
10139 end if;
10140
10141 Stmt := Make_Assignment_Statement (Loc,
10142 Name => New_Reference_To (D, Loc),
10143 Expression => Conv);
10144
10145 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
10146
10147 Parms := Parameter_Associations (Select_Call);
10148 Parm := First (Parms);
10149
10150 while Present (Parm)
10151 and then Parm /= Select_Mode
10152 loop
10153 Next (Parm);
10154 end loop;
10155
10156 pragma Assert (Present (Parm));
10157 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
10158 Analyze (Parm);
10159
10160 -- Prepare two new parameters of Duration and Delay_Mode type
10161 -- which represent the value and the mode of the minimum delay.
10162
10163 Next (Parm);
10164 Insert_After (Parm, New_Reference_To (M, Loc));
10165 Insert_After (Parm, New_Reference_To (D, Loc));
10166
10167 -- Create a call to RTS
10168
10169 Rewrite (Select_Call,
10170 Make_Procedure_Call_Statement (Loc,
10171 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
10172 Parameter_Associations => Parms));
10173
10174 -- This new call should follow the calculation of the minimum
10175 -- delay.
10176
10177 Insert_List_Before (Select_Call, Delay_List);
10178
10179 if Check_Guard then
10180 Stmt :=
10181 Make_Implicit_If_Statement (N,
10182 Condition => New_Reference_To (Guard_Open, Loc),
10183 Then_Statements =>
10184 New_List (New_Copy_Tree (Stmt),
10185 New_Copy_Tree (Select_Call)),
10186 Else_Statements => Accept_Or_Raise);
10187 Rewrite (Select_Call, Stmt);
10188 else
10189 Insert_Before (Select_Call, Stmt);
10190 end if;
10191
10192 Cases :=
10193 Make_Implicit_If_Statement (N,
10194 Condition => Make_Op_Eq (Loc,
10195 Left_Opnd => New_Reference_To (Xnam, Loc),
10196 Right_Opnd =>
10197 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10198
10199 Then_Statements => Delay_Case,
10200 Else_Statements => Accept_Case);
10201
10202 Append (Cases, Stats);
10203 end;
10204 end if;
10205
10206 -- Replace accept statement with appropriate block
10207
10208 Block :=
10209 Make_Block_Statement (Loc,
10210 Declarations => Decls,
10211 Handled_Statement_Sequence =>
10212 Make_Handled_Sequence_Of_Statements (Loc,
10213 Statements => Stats));
10214
10215 Rewrite (N, Block);
10216 Analyze (N);
10217
10218 -- Note: have to worry more about abort deferral in above code ???
10219
10220 -- Final step is to unstack the Accept_Address entries for all accept
10221 -- statements appearing in accept alternatives in the select statement
10222
10223 Alt := First (Alts);
10224 while Present (Alt) loop
10225 if Nkind (Alt) = N_Accept_Alternative then
10226 Remove_Last_Elmt (Accept_Address
10227 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
10228 end if;
10229
10230 Next (Alt);
10231 end loop;
10232 end Expand_N_Selective_Accept;
10233
10234 --------------------------------------
10235 -- Expand_N_Single_Task_Declaration --
10236 --------------------------------------
10237
10238 -- Single task declarations should never be present after semantic
10239 -- analysis, since we expect them to be replaced by a declaration of an
10240 -- anonymous task type, followed by a declaration of the task object. We
10241 -- include this routine to make sure that is happening!
10242
10243 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
10244 begin
10245 raise Program_Error;
10246 end Expand_N_Single_Task_Declaration;
10247
10248 ------------------------
10249 -- Expand_N_Task_Body --
10250 ------------------------
10251
10252 -- Given a task body
10253
10254 -- task body tname is
10255 -- <declarations>
10256 -- begin
10257 -- <statements>
10258 -- end x;
10259
10260 -- This expansion routine converts it into a procedure and sets the
10261 -- elaboration flag for the procedure to true, to represent the fact
10262 -- that the task body is now elaborated:
10263
10264 -- procedure tnameB (_Task : access tnameV) is
10265 -- discriminal : dtype renames _Task.discriminant;
10266
10267 -- procedure _clean is
10268 -- begin
10269 -- Abort_Defer.all;
10270 -- Complete_Task;
10271 -- Abort_Undefer.all;
10272 -- return;
10273 -- end _clean;
10274
10275 -- begin
10276 -- Abort_Undefer.all;
10277 -- <declarations>
10278 -- System.Task_Stages.Complete_Activation;
10279 -- <statements>
10280 -- at end
10281 -- _clean;
10282 -- end tnameB;
10283
10284 -- tnameE := True;
10285
10286 -- In addition, if the task body is an activator, then a call to activate
10287 -- tasks is added at the start of the statements, before the call to
10288 -- Complete_Activation, and if in addition the task is a master then it
10289 -- must be established as a master. These calls are inserted and analyzed
10290 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
10291 -- expanded.
10292
10293 -- There is one discriminal declaration line generated for each
10294 -- discriminant that is present to provide an easy reference point for
10295 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
10296
10297 -- Note on relationship to GNARLI definition. In the GNARLI definition,
10298 -- task body procedures have a profile (Arg : System.Address). That is
10299 -- needed because GNARLI has to use the same access-to-subprogram type
10300 -- for all task types. We depend here on knowing that in GNAT, passing
10301 -- an address argument by value is identical to passing a record value
10302 -- by access (in either case a single pointer is passed), so even though
10303 -- this procedure has the wrong profile. In fact it's all OK, since the
10304 -- callings sequence is identical.
10305
10306 procedure Expand_N_Task_Body (N : Node_Id) is
10307 Loc : constant Source_Ptr := Sloc (N);
10308 Ttyp : constant Entity_Id := Corresponding_Spec (N);
10309 Call : Node_Id;
10310 New_N : Node_Id;
10311
10312 Insert_Nod : Node_Id;
10313 -- Used to determine the proper location of wrapper body insertions
10314
10315 begin
10316 -- Add renaming declarations for discriminals and a declaration for the
10317 -- entry family index (if applicable).
10318
10319 Install_Private_Data_Declarations
10320 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
10321
10322 -- Add a call to Abort_Undefer at the very beginning of the task
10323 -- body since this body is called with abort still deferred.
10324
10325 if Abort_Allowed then
10326 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10327 Insert_Before
10328 (First (Statements (Handled_Statement_Sequence (N))), Call);
10329 Analyze (Call);
10330 end if;
10331
10332 -- The statement part has already been protected with an at_end and
10333 -- cleanup actions. The call to Complete_Activation must be placed
10334 -- at the head of the sequence of statements of that block. The
10335 -- declarations have been merged in this sequence of statements but
10336 -- the first real statement is accessible from the First_Real_Statement
10337 -- field (which was set for exactly this purpose).
10338
10339 if Restricted_Profile then
10340 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
10341 else
10342 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
10343 end if;
10344
10345 Insert_Before
10346 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
10347 Analyze (Call);
10348
10349 New_N :=
10350 Make_Subprogram_Body (Loc,
10351 Specification => Build_Task_Proc_Specification (Ttyp),
10352 Declarations => Declarations (N),
10353 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
10354
10355 -- If the task contains generic instantiations, cleanup actions are
10356 -- delayed until after instantiation. Transfer the activation chain to
10357 -- the subprogram, to insure that the activation call is properly
10358 -- generated. It the task body contains inner tasks, indicate that the
10359 -- subprogram is a task master.
10360
10361 if Delay_Cleanups (Ttyp) then
10362 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
10363 Set_Is_Task_Master (New_N, Is_Task_Master (N));
10364 end if;
10365
10366 Rewrite (N, New_N);
10367 Analyze (N);
10368
10369 -- Set elaboration flag immediately after task body. If the body is a
10370 -- subunit, the flag is set in the declarative part containing the stub.
10371
10372 if Nkind (Parent (N)) /= N_Subunit then
10373 Insert_After (N,
10374 Make_Assignment_Statement (Loc,
10375 Name =>
10376 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
10377 Expression => New_Reference_To (Standard_True, Loc)));
10378 end if;
10379
10380 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
10381 -- the task body. At this point all wrapper specs have been created,
10382 -- frozen and included in the dispatch table for the task type.
10383
10384 if Ada_Version >= Ada_2005 then
10385 if Nkind (Parent (N)) = N_Subunit then
10386 Insert_Nod := Corresponding_Stub (Parent (N));
10387 else
10388 Insert_Nod := N;
10389 end if;
10390
10391 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
10392 end if;
10393 end Expand_N_Task_Body;
10394
10395 ------------------------------------
10396 -- Expand_N_Task_Type_Declaration --
10397 ------------------------------------
10398
10399 -- We have several things to do. First we must create a Boolean flag used
10400 -- to mark if the body is elaborated yet. This variable gets set to True
10401 -- when the body of the task is elaborated (we can't rely on the normal
10402 -- ABE mechanism for the task body, since we need to pass an access to
10403 -- this elaboration boolean to the runtime routines).
10404
10405 -- taskE : aliased Boolean := False;
10406
10407 -- Next a variable is declared to hold the task stack size (either the
10408 -- default : Unspecified_Size, or a value that is set by a pragma
10409 -- Storage_Size). If the value of the pragma Storage_Size is static, then
10410 -- the variable is initialized with this value:
10411
10412 -- taskZ : Size_Type := Unspecified_Size;
10413 -- or
10414 -- taskZ : Size_Type := Size_Type (size_expression);
10415
10416 -- Note: No variable is needed to hold the task relative deadline since
10417 -- its value would never be static because the parameter is of a private
10418 -- type (Ada.Real_Time.Time_Span).
10419
10420 -- Next we create a corresponding record type declaration used to represent
10421 -- values of this task. The general form of this type declaration is
10422
10423 -- type taskV (discriminants) is record
10424 -- _Task_Id : Task_Id;
10425 -- entry_family : array (bounds) of Void;
10426 -- _Priority : Integer := priority_expression;
10427 -- _Size : Size_Type := Size_Type (size_expression);
10428 -- _Task_Info : Task_Info_Type := task_info_expression;
10429 -- _CPU : Integer := cpu_range_expression;
10430 -- end record;
10431
10432 -- The discriminants are present only if the corresponding task type has
10433 -- discriminants, and they exactly mirror the task type discriminants.
10434
10435 -- The Id field is always present. It contains the Task_Id value, as set by
10436 -- the call to Create_Task. Note that although the task is limited, the
10437 -- task value record type is not limited, so there is no problem in passing
10438 -- this field as an out parameter to Create_Task.
10439
10440 -- One entry_family component is present for each entry family in the task
10441 -- definition. The bounds correspond to the bounds of the entry family
10442 -- (which may depend on discriminants). The element type is void, since we
10443 -- only need the bounds information for determining the entry index. Note
10444 -- that the use of an anonymous array would normally be illegal in this
10445 -- context, but this is a parser check, and the semantics is quite prepared
10446 -- to handle such a case.
10447
10448 -- The _Size field is present only if a Storage_Size pragma appears in the
10449 -- task definition. The expression captures the argument that was present
10450 -- in the pragma, and is used to override the task stack size otherwise
10451 -- associated with the task type.
10452
10453 -- The _Priority field is present only if a Priority or Interrupt_Priority
10454 -- pragma appears in the task definition. The expression captures the
10455 -- argument that was present in the pragma, and is used to provide the Size
10456 -- parameter to the call to Create_Task.
10457
10458 -- The _Task_Info field is present only if a Task_Info pragma appears in
10459 -- the task definition. The expression captures the argument that was
10460 -- present in the pragma, and is used to provide the Task_Image parameter
10461 -- to the call to Create_Task.
10462
10463 -- The _CPU field is present only if a CPU pragma appears in the task
10464 -- definition. The expression captures the argument that was present in
10465 -- the pragma, and is used to provide the CPU parameter to the call to
10466 -- Create_Task.
10467
10468 -- The _Relative_Deadline field is present only if a Relative_Deadline
10469 -- pragma appears in the task definition. The expression captures the
10470 -- argument that was present in the pragma, and is used to provide the
10471 -- Relative_Deadline parameter to the call to Create_Task.
10472
10473 -- When a task is declared, an instance of the task value record is
10474 -- created. The elaboration of this declaration creates the correct bounds
10475 -- for the entry families, and also evaluates the size, priority, and
10476 -- task_Info expressions if needed. The initialization routine for the task
10477 -- type itself then calls Create_Task with appropriate parameters to
10478 -- initialize the value of the Task_Id field.
10479
10480 -- Note: the address of this record is passed as the "Discriminants"
10481 -- parameter for Create_Task. Since Create_Task merely passes this onto the
10482 -- body procedure, it does not matter that it does not quite match the
10483 -- GNARLI model of what is being passed (the record contains more than just
10484 -- the discriminants, but the discriminants can be found from the record
10485 -- value).
10486
10487 -- The Entity_Id for this created record type is placed in the
10488 -- Corresponding_Record_Type field of the associated task type entity.
10489
10490 -- Next we create a procedure specification for the task body procedure:
10491
10492 -- procedure taskB (_Task : access taskV);
10493
10494 -- Note that this must come after the record type declaration, since
10495 -- the spec refers to this type. It turns out that the initialization
10496 -- procedure for the value type references the task body spec, but that's
10497 -- fine, since it won't be generated till the freeze point for the type,
10498 -- which is certainly after the task body spec declaration.
10499
10500 -- Finally, we set the task index value field of the entry attribute in
10501 -- the case of a simple entry.
10502
10503 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
10504 Loc : constant Source_Ptr := Sloc (N);
10505 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
10506 Tasknm : constant Name_Id := Chars (Tasktyp);
10507 Taskdef : constant Node_Id := Task_Definition (N);
10508
10509 Proc_Spec : Node_Id;
10510 Rec_Decl : Node_Id;
10511 Rec_Ent : Entity_Id;
10512 Cdecls : List_Id;
10513 Elab_Decl : Node_Id;
10514 Size_Decl : Node_Id;
10515 Body_Decl : Node_Id;
10516 Task_Size : Node_Id;
10517 Ent_Stack : Entity_Id;
10518 Decl_Stack : Node_Id;
10519
10520 begin
10521 -- If already expanded, nothing to do
10522
10523 if Present (Corresponding_Record_Type (Tasktyp)) then
10524 return;
10525 end if;
10526
10527 -- Here we will do the expansion
10528
10529 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
10530
10531 Rec_Ent := Defining_Identifier (Rec_Decl);
10532 Cdecls := Component_Items (Component_List
10533 (Type_Definition (Rec_Decl)));
10534
10535 Qualify_Entity_Names (N);
10536
10537 -- First create the elaboration variable
10538
10539 Elab_Decl :=
10540 Make_Object_Declaration (Loc,
10541 Defining_Identifier =>
10542 Make_Defining_Identifier (Sloc (Tasktyp),
10543 Chars => New_External_Name (Tasknm, 'E')),
10544 Aliased_Present => True,
10545 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10546 Expression => New_Reference_To (Standard_False, Loc));
10547 Insert_After (N, Elab_Decl);
10548
10549 -- Next create the declaration of the size variable (tasknmZ)
10550
10551 Set_Storage_Size_Variable (Tasktyp,
10552 Make_Defining_Identifier (Sloc (Tasktyp),
10553 Chars => New_External_Name (Tasknm, 'Z')));
10554
10555 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) and then
10556 Is_Static_Expression (Expression (First (
10557 Pragma_Argument_Associations (Find_Task_Or_Protected_Pragma (
10558 Taskdef, Name_Storage_Size)))))
10559 then
10560 Size_Decl :=
10561 Make_Object_Declaration (Loc,
10562 Defining_Identifier => Storage_Size_Variable (Tasktyp),
10563 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
10564 Expression =>
10565 Convert_To (RTE (RE_Size_Type),
10566 Relocate_Node (
10567 Expression (First (
10568 Pragma_Argument_Associations (
10569 Find_Task_Or_Protected_Pragma
10570 (Taskdef, Name_Storage_Size)))))));
10571
10572 else
10573 Size_Decl :=
10574 Make_Object_Declaration (Loc,
10575 Defining_Identifier => Storage_Size_Variable (Tasktyp),
10576 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
10577 Expression => New_Reference_To (RTE (RE_Unspecified_Size), Loc));
10578 end if;
10579
10580 Insert_After (Elab_Decl, Size_Decl);
10581
10582 -- Next build the rest of the corresponding record declaration. This is
10583 -- done last, since the corresponding record initialization procedure
10584 -- will reference the previously created entities.
10585
10586 -- Fill in the component declarations -- first the _Task_Id field
10587
10588 Append_To (Cdecls,
10589 Make_Component_Declaration (Loc,
10590 Defining_Identifier =>
10591 Make_Defining_Identifier (Loc, Name_uTask_Id),
10592 Component_Definition =>
10593 Make_Component_Definition (Loc,
10594 Aliased_Present => False,
10595 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
10596 Loc))));
10597
10598 -- Declare static ATCB (that is, created by the expander) if we are
10599 -- using the Restricted run time.
10600
10601 if Restricted_Profile then
10602 Append_To (Cdecls,
10603 Make_Component_Declaration (Loc,
10604 Defining_Identifier =>
10605 Make_Defining_Identifier (Loc, Name_uATCB),
10606
10607 Component_Definition =>
10608 Make_Component_Definition (Loc,
10609 Aliased_Present => True,
10610 Subtype_Indication => Make_Subtype_Indication (Loc,
10611 Subtype_Mark => New_Occurrence_Of
10612 (RTE (RE_Ada_Task_Control_Block), Loc),
10613
10614 Constraint =>
10615 Make_Index_Or_Discriminant_Constraint (Loc,
10616 Constraints =>
10617 New_List (Make_Integer_Literal (Loc, 0)))))));
10618
10619 end if;
10620
10621 -- Declare static stack (that is, created by the expander) if we are
10622 -- using the Restricted run time on a bare board configuration.
10623
10624 if Restricted_Profile
10625 and then Preallocated_Stacks_On_Target
10626 then
10627 -- First we need to extract the appropriate stack size
10628
10629 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
10630
10631 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
10632 declare
10633 Expr_N : constant Node_Id :=
10634 Expression (First (
10635 Pragma_Argument_Associations (
10636 Find_Task_Or_Protected_Pragma
10637 (Taskdef, Name_Storage_Size))));
10638 Etyp : constant Entity_Id := Etype (Expr_N);
10639 P : constant Node_Id := Parent (Expr_N);
10640
10641 begin
10642 -- The stack is defined inside the corresponding record.
10643 -- Therefore if the size of the stack is set by means of
10644 -- a discriminant, we must reference the discriminant of the
10645 -- corresponding record type.
10646
10647 if Nkind (Expr_N) in N_Has_Entity
10648 and then Present (Discriminal_Link (Entity (Expr_N)))
10649 then
10650 Task_Size :=
10651 New_Reference_To
10652 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
10653 Loc);
10654 Set_Parent (Task_Size, P);
10655 Set_Etype (Task_Size, Etyp);
10656 Set_Analyzed (Task_Size);
10657
10658 else
10659 Task_Size := Relocate_Node (Expr_N);
10660 end if;
10661 end;
10662
10663 else
10664 Task_Size :=
10665 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
10666 end if;
10667
10668 Decl_Stack := Make_Component_Declaration (Loc,
10669 Defining_Identifier => Ent_Stack,
10670
10671 Component_Definition =>
10672 Make_Component_Definition (Loc,
10673 Aliased_Present => True,
10674 Subtype_Indication => Make_Subtype_Indication (Loc,
10675 Subtype_Mark =>
10676 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
10677
10678 Constraint =>
10679 Make_Index_Or_Discriminant_Constraint (Loc,
10680 Constraints => New_List (Make_Range (Loc,
10681 Low_Bound => Make_Integer_Literal (Loc, 1),
10682 High_Bound => Convert_To (RTE (RE_Storage_Offset),
10683 Task_Size)))))));
10684
10685 Append_To (Cdecls, Decl_Stack);
10686
10687 -- The appropriate alignment for the stack is ensured by the run-time
10688 -- code in charge of task creation.
10689
10690 end if;
10691
10692 -- Add components for entry families
10693
10694 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
10695
10696 -- Add the _Priority component if a Priority pragma is present
10697
10698 if Present (Taskdef) and then Has_Pragma_Priority (Taskdef) then
10699 declare
10700 Prag : constant Node_Id :=
10701 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
10702 Expr : Node_Id;
10703
10704 begin
10705 Expr := First (Pragma_Argument_Associations (Prag));
10706
10707 if Nkind (Expr) = N_Pragma_Argument_Association then
10708 Expr := Expression (Expr);
10709 end if;
10710
10711 Expr := New_Copy_Tree (Expr);
10712
10713 -- Add conversion to proper type to do range check if required
10714 -- Note that for runtime units, we allow out of range interrupt
10715 -- priority values to be used in a priority pragma. This is for
10716 -- the benefit of some versions of System.Interrupts which use
10717 -- a special server task with maximum interrupt priority.
10718
10719 if Pragma_Name (Prag) = Name_Priority
10720 and then not GNAT_Mode
10721 then
10722 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
10723 else
10724 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
10725 end if;
10726
10727 Append_To (Cdecls,
10728 Make_Component_Declaration (Loc,
10729 Defining_Identifier =>
10730 Make_Defining_Identifier (Loc, Name_uPriority),
10731 Component_Definition =>
10732 Make_Component_Definition (Loc,
10733 Aliased_Present => False,
10734 Subtype_Indication => New_Reference_To (Standard_Integer,
10735 Loc)),
10736 Expression => Expr));
10737 end;
10738 end if;
10739
10740 -- Add the _Task_Size component if a Storage_Size pragma is present
10741
10742 if Present (Taskdef)
10743 and then Has_Storage_Size_Pragma (Taskdef)
10744 then
10745 Append_To (Cdecls,
10746 Make_Component_Declaration (Loc,
10747 Defining_Identifier =>
10748 Make_Defining_Identifier (Loc, Name_uSize),
10749
10750 Component_Definition =>
10751 Make_Component_Definition (Loc,
10752 Aliased_Present => False,
10753 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
10754 Loc)),
10755
10756 Expression =>
10757 Convert_To (RTE (RE_Size_Type),
10758 Relocate_Node (
10759 Expression (First (
10760 Pragma_Argument_Associations (
10761 Find_Task_Or_Protected_Pragma
10762 (Taskdef, Name_Storage_Size))))))));
10763 end if;
10764
10765 -- Add the _Task_Info component if a Task_Info pragma is present
10766
10767 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
10768 Append_To (Cdecls,
10769 Make_Component_Declaration (Loc,
10770 Defining_Identifier =>
10771 Make_Defining_Identifier (Loc, Name_uTask_Info),
10772
10773 Component_Definition =>
10774 Make_Component_Definition (Loc,
10775 Aliased_Present => False,
10776 Subtype_Indication =>
10777 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
10778
10779 Expression => New_Copy (
10780 Expression (First (
10781 Pragma_Argument_Associations (
10782 Find_Task_Or_Protected_Pragma
10783 (Taskdef, Name_Task_Info)))))));
10784 end if;
10785
10786 -- Add the _CPU component if a CPU pragma is present
10787
10788 if Present (Taskdef) and then Has_Pragma_CPU (Taskdef) then
10789 Append_To (Cdecls,
10790 Make_Component_Declaration (Loc,
10791 Defining_Identifier =>
10792 Make_Defining_Identifier (Loc, Name_uCPU),
10793
10794 Component_Definition =>
10795 Make_Component_Definition (Loc,
10796 Aliased_Present => False,
10797 Subtype_Indication =>
10798 New_Reference_To (RTE (RE_CPU_Range), Loc)),
10799
10800 Expression => New_Copy (
10801 Expression (First (
10802 Pragma_Argument_Associations (
10803 Find_Task_Or_Protected_Pragma
10804 (Taskdef, Name_CPU)))))));
10805 end if;
10806
10807 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
10808 -- present. If we are using a restricted run time this component will
10809 -- not be added (deadlines are not allowed by the Ravenscar profile).
10810
10811 if not Restricted_Profile
10812 and then Present (Taskdef)
10813 and then Has_Relative_Deadline_Pragma (Taskdef)
10814 then
10815 Append_To (Cdecls,
10816 Make_Component_Declaration (Loc,
10817 Defining_Identifier =>
10818 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
10819
10820 Component_Definition =>
10821 Make_Component_Definition (Loc,
10822 Aliased_Present => False,
10823 Subtype_Indication =>
10824 New_Reference_To (RTE (RE_Time_Span), Loc)),
10825
10826 Expression =>
10827 Convert_To (RTE (RE_Time_Span),
10828 Relocate_Node (
10829 Expression (First (
10830 Pragma_Argument_Associations (
10831 Find_Task_Or_Protected_Pragma
10832 (Taskdef, Name_Relative_Deadline))))))));
10833 end if;
10834
10835 Insert_After (Size_Decl, Rec_Decl);
10836
10837 -- Analyze the record declaration immediately after construction,
10838 -- because the initialization procedure is needed for single task
10839 -- declarations before the next entity is analyzed.
10840
10841 Analyze (Rec_Decl);
10842
10843 -- Create the declaration of the task body procedure
10844
10845 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
10846 Body_Decl :=
10847 Make_Subprogram_Declaration (Loc,
10848 Specification => Proc_Spec);
10849
10850 Insert_After (Rec_Decl, Body_Decl);
10851
10852 -- The subprogram does not comes from source, so we have to indicate the
10853 -- need for debugging information explicitly.
10854
10855 if Comes_From_Source (Original_Node (N)) then
10856 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
10857 end if;
10858
10859 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
10860 -- the corresponding record has been frozen.
10861
10862 if Ada_Version >= Ada_2005 then
10863 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
10864 end if;
10865
10866 -- Ada 2005 (AI-345): We must defer freezing to allow further
10867 -- declaration of primitive subprograms covering task interfaces
10868
10869 if Ada_Version <= Ada_95 then
10870
10871 -- Now we can freeze the corresponding record. This needs manually
10872 -- freezing, since it is really part of the task type, and the task
10873 -- type is frozen at this stage. We of course need the initialization
10874 -- procedure for this corresponding record type and we won't get it
10875 -- in time if we don't freeze now.
10876
10877 declare
10878 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
10879 begin
10880 if Is_Non_Empty_List (L) then
10881 Insert_List_After (Body_Decl, L);
10882 end if;
10883 end;
10884 end if;
10885
10886 -- Complete the expansion of access types to the current task type, if
10887 -- any were declared.
10888
10889 Expand_Previous_Access_Type (Tasktyp);
10890
10891 -- Create wrappers for entries that have pre/postconditions
10892
10893 declare
10894 Ent : Entity_Id;
10895
10896 begin
10897 Ent := First_Entity (Tasktyp);
10898 while Present (Ent) loop
10899 if Ekind_In (Ent, E_Entry, E_Entry_Family)
10900 and then Present (Spec_PPC_List (Contract (Ent)))
10901 then
10902 Build_PPC_Wrapper (Ent, N);
10903 end if;
10904
10905 Next_Entity (Ent);
10906 end loop;
10907 end;
10908 end Expand_N_Task_Type_Declaration;
10909
10910 -------------------------------
10911 -- Expand_N_Timed_Entry_Call --
10912 -------------------------------
10913
10914 -- A timed entry call in normal case is not implemented using ATC mechanism
10915 -- anymore for efficiency reason.
10916
10917 -- select
10918 -- T.E;
10919 -- S1;
10920 -- or
10921 -- Delay D;
10922 -- S2;
10923 -- end select;
10924
10925 -- is expanded as follow:
10926
10927 -- 1) When T.E is a task entry_call;
10928
10929 -- declare
10930 -- B : Boolean;
10931 -- X : Task_Entry_Index := <entry index>;
10932 -- DX : Duration := To_Duration (D);
10933 -- M : Delay_Mode := <discriminant>;
10934 -- P : parms := (parm, parm, parm);
10935
10936 -- begin
10937 -- Timed_Protected_Entry_Call
10938 -- (<acceptor-task>, X, P'Address, DX, M, B);
10939 -- if B then
10940 -- S1;
10941 -- else
10942 -- S2;
10943 -- end if;
10944 -- end;
10945
10946 -- 2) When T.E is a protected entry_call;
10947
10948 -- declare
10949 -- B : Boolean;
10950 -- X : Protected_Entry_Index := <entry index>;
10951 -- DX : Duration := To_Duration (D);
10952 -- M : Delay_Mode := <discriminant>;
10953 -- P : parms := (parm, parm, parm);
10954
10955 -- begin
10956 -- Timed_Protected_Entry_Call
10957 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
10958 -- if B then
10959 -- S1;
10960 -- else
10961 -- S2;
10962 -- end if;
10963 -- end;
10964
10965 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call;
10966
10967 -- declare
10968 -- B : Boolean := False;
10969 -- C : Ada.Tags.Prim_Op_Kind;
10970 -- DX : Duration := To_Duration (D)
10971 -- K : Ada.Tags.Tagged_Kind :=
10972 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
10973 -- M : Integer :=...;
10974 -- P : Parameters := (Param1 .. ParamN);
10975 -- S : Integer;
10976
10977 -- begin
10978 -- if K = Ada.Tags.TK_Limited_Tagged then
10979 -- <dispatching-call>;
10980 -- <triggering-statements>
10981
10982 -- else
10983 -- S :=
10984 -- Ada.Tags.Get_Offset_Index
10985 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
10986
10987 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
10988
10989 -- if C = POK_Protected_Entry
10990 -- or else C = POK_Task_Entry
10991 -- then
10992 -- Param1 := P.Param1;
10993 -- ...
10994 -- ParamN := P.ParamN;
10995 -- end if;
10996
10997 -- if B then
10998 -- if C = POK_Procedure
10999 -- or else C = POK_Protected_Procedure
11000 -- or else C = POK_Task_Procedure
11001 -- then
11002 -- <dispatching-call>;
11003 -- end if;
11004
11005 -- <triggering-statements>
11006 -- else
11007 -- <timed-statements>
11008 -- end if;
11009 -- end if;
11010 -- end;
11011
11012 -- The triggering statement and the sequence of timed statements have not
11013 -- been analyzed yet (see Analyzed_Timed_Entry_Call). They may contain
11014 -- local declarations, and therefore the copies that are made during
11015 -- expansion must be disjoint, as for any other inlining.
11016
11017 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
11018 Loc : constant Source_Ptr := Sloc (N);
11019
11020 E_Call : Node_Id :=
11021 Entry_Call_Statement (Entry_Call_Alternative (N));
11022 E_Stats : constant List_Id :=
11023 Statements (Entry_Call_Alternative (N));
11024 D_Stat : Node_Id :=
11025 Delay_Statement (Delay_Alternative (N));
11026 D_Stats : constant List_Id :=
11027 Statements (Delay_Alternative (N));
11028
11029 Actuals : List_Id;
11030 Blk_Typ : Entity_Id;
11031 Call : Node_Id;
11032 Call_Ent : Entity_Id;
11033 Conc_Typ_Stmts : List_Id;
11034 Concval : Node_Id;
11035 D_Conv : Node_Id;
11036 D_Disc : Node_Id;
11037 D_Type : Entity_Id;
11038 Decls : List_Id;
11039 Dummy : Node_Id;
11040 Ename : Node_Id;
11041 Formals : List_Id;
11042 Index : Node_Id;
11043 Is_Disp_Select : Boolean;
11044 Lim_Typ_Stmts : List_Id;
11045 N_Stats : List_Id;
11046 Obj : Entity_Id;
11047 Param : Node_Id;
11048 Params : List_Id;
11049 Stmt : Node_Id;
11050 Stmts : List_Id;
11051 Unpack : List_Id;
11052
11053 B : Entity_Id; -- Call status flag
11054 C : Entity_Id; -- Call kind
11055 D : Entity_Id; -- Delay
11056 K : Entity_Id; -- Tagged kind
11057 M : Entity_Id; -- Delay mode
11058 P : Entity_Id; -- Parameter block
11059 S : Entity_Id; -- Primitive operation slot
11060
11061 begin
11062 -- Under the Ravenscar profile, timed entry calls are excluded. An error
11063 -- was already reported on spec, so do not attempt to expand the call.
11064
11065 if Restriction_Active (No_Select_Statements) then
11066 return;
11067 end if;
11068
11069 Process_Statements_For_Controlled_Objects (Entry_Call_Alternative (N));
11070 Process_Statements_For_Controlled_Objects (Delay_Alternative (N));
11071
11072 -- The arguments in the call may require dynamic allocation, and the
11073 -- call statement may have been transformed into a block. The block
11074 -- may contain additional declarations for internal entities, and the
11075 -- original call is found by sequential search.
11076
11077 if Nkind (E_Call) = N_Block_Statement then
11078 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
11079 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
11080 N_Entry_Call_Statement)
11081 loop
11082 Next (E_Call);
11083 end loop;
11084 end if;
11085
11086 Is_Disp_Select :=
11087 Ada_Version >= Ada_2005
11088 and then Nkind (E_Call) = N_Procedure_Call_Statement;
11089
11090 if Is_Disp_Select then
11091 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
11092
11093 Decls := New_List;
11094 Stmts := New_List;
11095
11096 -- Generate:
11097 -- B : Boolean := False;
11098
11099 B := Build_B (Loc, Decls);
11100
11101 -- Generate:
11102 -- C : Ada.Tags.Prim_Op_Kind;
11103
11104 C := Build_C (Loc, Decls);
11105
11106 -- Because the analysis of all statements was disabled, manually
11107 -- analyze the delay statement.
11108
11109 Analyze (D_Stat);
11110 D_Stat := Original_Node (D_Stat);
11111
11112 else
11113 -- Build an entry call using Simple_Entry_Call
11114
11115 Extract_Entry (E_Call, Concval, Ename, Index);
11116 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
11117
11118 Decls := Declarations (E_Call);
11119 Stmts := Statements (Handled_Statement_Sequence (E_Call));
11120
11121 if No (Decls) then
11122 Decls := New_List;
11123 end if;
11124
11125 -- Generate:
11126 -- B : Boolean;
11127
11128 B := Make_Defining_Identifier (Loc, Name_uB);
11129
11130 Prepend_To (Decls,
11131 Make_Object_Declaration (Loc,
11132 Defining_Identifier =>
11133 B,
11134 Object_Definition =>
11135 New_Reference_To (Standard_Boolean, Loc)));
11136 end if;
11137
11138 -- Duration and mode processing
11139
11140 D_Type := Base_Type (Etype (Expression (D_Stat)));
11141
11142 -- Use the type of the delay expression (Calendar or Real_Time) to
11143 -- generate the appropriate conversion.
11144
11145 if Nkind (D_Stat) = N_Delay_Relative_Statement then
11146 D_Disc := Make_Integer_Literal (Loc, 0);
11147 D_Conv := Relocate_Node (Expression (D_Stat));
11148
11149 elsif Is_RTE (D_Type, RO_CA_Time) then
11150 D_Disc := Make_Integer_Literal (Loc, 1);
11151 D_Conv := Make_Function_Call (Loc,
11152 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
11153 New_List (New_Copy (Expression (D_Stat))));
11154
11155 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
11156 D_Disc := Make_Integer_Literal (Loc, 2);
11157 D_Conv := Make_Function_Call (Loc,
11158 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
11159 New_List (New_Copy (Expression (D_Stat))));
11160 end if;
11161
11162 D := Make_Temporary (Loc, 'D');
11163
11164 -- Generate:
11165 -- D : Duration;
11166
11167 Append_To (Decls,
11168 Make_Object_Declaration (Loc,
11169 Defining_Identifier =>
11170 D,
11171 Object_Definition =>
11172 New_Reference_To (Standard_Duration, Loc)));
11173
11174 M := Make_Temporary (Loc, 'M');
11175
11176 -- Generate:
11177 -- M : Integer := (0 | 1 | 2);
11178
11179 Append_To (Decls,
11180 Make_Object_Declaration (Loc,
11181 Defining_Identifier =>
11182 M,
11183 Object_Definition =>
11184 New_Reference_To (Standard_Integer, Loc),
11185 Expression =>
11186 D_Disc));
11187
11188 -- Do the assignment at this stage only because the evaluation of the
11189 -- expression must not occur before (see ACVC C97302A).
11190
11191 Append_To (Stmts,
11192 Make_Assignment_Statement (Loc,
11193 Name =>
11194 New_Reference_To (D, Loc),
11195 Expression =>
11196 D_Conv));
11197
11198 -- Parameter block processing
11199
11200 -- Manually create the parameter block for dispatching calls. In the
11201 -- case of entries, the block has already been created during the call
11202 -- to Build_Simple_Entry_Call.
11203
11204 if Is_Disp_Select then
11205
11206 -- Tagged kind processing, generate:
11207 -- K : Ada.Tags.Tagged_Kind :=
11208 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
11209
11210 K := Build_K (Loc, Decls, Obj);
11211
11212 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
11213 P := Parameter_Block_Pack
11214 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
11215
11216 -- Dispatch table slot processing, generate:
11217 -- S : Integer;
11218
11219 S := Build_S (Loc, Decls);
11220
11221 -- Generate:
11222 -- S := Ada.Tags.Get_Offset_Index
11223 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
11224
11225 Conc_Typ_Stmts :=
11226 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
11227
11228 -- Generate:
11229 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
11230
11231 -- where Obj is the controlling formal parameter, S is the dispatch
11232 -- table slot number of the dispatching operation, P is the wrapped
11233 -- parameter block, D is the duration, M is the duration mode, C is
11234 -- the call kind and B is the call status.
11235
11236 Params := New_List;
11237
11238 Append_To (Params, New_Copy_Tree (Obj));
11239 Append_To (Params, New_Reference_To (S, Loc));
11240 Append_To (Params, Make_Attribute_Reference (Loc,
11241 Prefix => New_Reference_To (P, Loc),
11242 Attribute_Name => Name_Address));
11243 Append_To (Params, New_Reference_To (D, Loc));
11244 Append_To (Params, New_Reference_To (M, Loc));
11245 Append_To (Params, New_Reference_To (C, Loc));
11246 Append_To (Params, New_Reference_To (B, Loc));
11247
11248 Append_To (Conc_Typ_Stmts,
11249 Make_Procedure_Call_Statement (Loc,
11250 Name =>
11251 New_Reference_To (
11252 Find_Prim_Op (Etype (Etype (Obj)),
11253 Name_uDisp_Timed_Select),
11254 Loc),
11255 Parameter_Associations =>
11256 Params));
11257
11258 -- Generate:
11259 -- if C = POK_Protected_Entry
11260 -- or else C = POK_Task_Entry
11261 -- then
11262 -- Param1 := P.Param1;
11263 -- ...
11264 -- ParamN := P.ParamN;
11265 -- end if;
11266
11267 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
11268
11269 -- Generate the if statement only when the packed parameters need
11270 -- explicit assignments to their corresponding actuals.
11271
11272 if Present (Unpack) then
11273 Append_To (Conc_Typ_Stmts,
11274 Make_If_Statement (Loc,
11275
11276 Condition =>
11277 Make_Or_Else (Loc,
11278 Left_Opnd =>
11279 Make_Op_Eq (Loc,
11280 Left_Opnd =>
11281 New_Reference_To (C, Loc),
11282 Right_Opnd =>
11283 New_Reference_To (RTE (
11284 RE_POK_Protected_Entry), Loc)),
11285 Right_Opnd =>
11286 Make_Op_Eq (Loc,
11287 Left_Opnd =>
11288 New_Reference_To (C, Loc),
11289 Right_Opnd =>
11290 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
11291
11292 Then_Statements =>
11293 Unpack));
11294 end if;
11295
11296 -- Generate:
11297
11298 -- if B then
11299 -- if C = POK_Procedure
11300 -- or else C = POK_Protected_Procedure
11301 -- or else C = POK_Task_Procedure
11302 -- then
11303 -- <dispatching-call>
11304 -- end if;
11305 -- <triggering-statements>
11306 -- else
11307 -- <timed-statements>
11308 -- end if;
11309
11310 N_Stats := Copy_Separate_List (E_Stats);
11311
11312 Prepend_To (N_Stats,
11313 Make_If_Statement (Loc,
11314
11315 Condition =>
11316 Make_Or_Else (Loc,
11317 Left_Opnd =>
11318 Make_Op_Eq (Loc,
11319 Left_Opnd =>
11320 New_Reference_To (C, Loc),
11321 Right_Opnd =>
11322 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
11323 Right_Opnd =>
11324 Make_Or_Else (Loc,
11325 Left_Opnd =>
11326 Make_Op_Eq (Loc,
11327 Left_Opnd =>
11328 New_Reference_To (C, Loc),
11329 Right_Opnd =>
11330 New_Reference_To (RTE (
11331 RE_POK_Protected_Procedure), Loc)),
11332 Right_Opnd =>
11333 Make_Op_Eq (Loc,
11334 Left_Opnd =>
11335 New_Reference_To (C, Loc),
11336 Right_Opnd =>
11337 New_Reference_To (RTE (
11338 RE_POK_Task_Procedure), Loc)))),
11339
11340 Then_Statements =>
11341 New_List (E_Call)));
11342
11343 Append_To (Conc_Typ_Stmts,
11344 Make_If_Statement (Loc,
11345 Condition => New_Reference_To (B, Loc),
11346 Then_Statements => N_Stats,
11347 Else_Statements => D_Stats));
11348
11349 -- Generate:
11350 -- <dispatching-call>;
11351 -- <triggering-statements>
11352
11353 Lim_Typ_Stmts := Copy_Separate_List (E_Stats);
11354 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (E_Call));
11355
11356 -- Generate:
11357 -- if K = Ada.Tags.TK_Limited_Tagged then
11358 -- Lim_Typ_Stmts
11359 -- else
11360 -- Conc_Typ_Stmts
11361 -- end if;
11362
11363 Append_To (Stmts,
11364 Make_If_Statement (Loc,
11365 Condition =>
11366 Make_Op_Eq (Loc,
11367 Left_Opnd =>
11368 New_Reference_To (K, Loc),
11369 Right_Opnd =>
11370 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
11371
11372 Then_Statements =>
11373 Lim_Typ_Stmts,
11374
11375 Else_Statements =>
11376 Conc_Typ_Stmts));
11377
11378 else
11379 -- Skip assignments to temporaries created for in-out parameters.
11380 -- This makes unwarranted assumptions about the shape of the expanded
11381 -- tree for the call, and should be cleaned up ???
11382
11383 Stmt := First (Stmts);
11384 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
11385 Next (Stmt);
11386 end loop;
11387
11388 -- Do the assignment at this stage only because the evaluation
11389 -- of the expression must not occur before (see ACVC C97302A).
11390
11391 Insert_Before (Stmt,
11392 Make_Assignment_Statement (Loc,
11393 Name => New_Reference_To (D, Loc),
11394 Expression => D_Conv));
11395
11396 Call := Stmt;
11397 Params := Parameter_Associations (Call);
11398
11399 -- For a protected type, we build a Timed_Protected_Entry_Call
11400
11401 if Is_Protected_Type (Etype (Concval)) then
11402
11403 -- Create a new call statement
11404
11405 Param := First (Params);
11406 while Present (Param)
11407 and then not Is_RTE (Etype (Param), RE_Call_Modes)
11408 loop
11409 Next (Param);
11410 end loop;
11411
11412 Dummy := Remove_Next (Next (Param));
11413
11414 -- Remove garbage is following the Cancel_Param if present
11415
11416 Dummy := Next (Param);
11417
11418 -- Remove the mode of the Protected_Entry_Call call, then remove
11419 -- the Communication_Block of the Protected_Entry_Call call, and
11420 -- finally add Duration and a Delay_Mode parameter
11421
11422 pragma Assert (Present (Param));
11423 Rewrite (Param, New_Reference_To (D, Loc));
11424
11425 Rewrite (Dummy, New_Reference_To (M, Loc));
11426
11427 -- Add a Boolean flag for successful entry call
11428
11429 Append_To (Params, New_Reference_To (B, Loc));
11430
11431 case Corresponding_Runtime_Package (Etype (Concval)) is
11432 when System_Tasking_Protected_Objects_Entries =>
11433 Rewrite (Call,
11434 Make_Procedure_Call_Statement (Loc,
11435 Name =>
11436 New_Reference_To
11437 (RTE (RE_Timed_Protected_Entry_Call), Loc),
11438 Parameter_Associations => Params));
11439
11440 when System_Tasking_Protected_Objects_Single_Entry =>
11441 Param := First (Params);
11442 while Present (Param)
11443 and then not
11444 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
11445 loop
11446 Next (Param);
11447 end loop;
11448
11449 Remove (Param);
11450
11451 Rewrite (Call,
11452 Make_Procedure_Call_Statement (Loc,
11453 Name => New_Reference_To (
11454 RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
11455 Parameter_Associations => Params));
11456
11457 when others =>
11458 raise Program_Error;
11459 end case;
11460
11461 -- For the task case, build a Timed_Task_Entry_Call
11462
11463 else
11464 -- Create a new call statement
11465
11466 Append_To (Params, New_Reference_To (D, Loc));
11467 Append_To (Params, New_Reference_To (M, Loc));
11468 Append_To (Params, New_Reference_To (B, Loc));
11469
11470 Rewrite (Call,
11471 Make_Procedure_Call_Statement (Loc,
11472 Name =>
11473 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
11474 Parameter_Associations => Params));
11475 end if;
11476
11477 Append_To (Stmts,
11478 Make_Implicit_If_Statement (N,
11479 Condition => New_Reference_To (B, Loc),
11480 Then_Statements => E_Stats,
11481 Else_Statements => D_Stats));
11482 end if;
11483
11484 Rewrite (N,
11485 Make_Block_Statement (Loc,
11486 Declarations => Decls,
11487 Handled_Statement_Sequence =>
11488 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
11489
11490 Analyze (N);
11491 end Expand_N_Timed_Entry_Call;
11492
11493 ----------------------------------------
11494 -- Expand_Protected_Body_Declarations --
11495 ----------------------------------------
11496
11497 procedure Expand_Protected_Body_Declarations
11498 (N : Node_Id;
11499 Spec_Id : Entity_Id)
11500 is
11501 begin
11502 if No_Run_Time_Mode then
11503 Error_Msg_CRT ("protected body", N);
11504 return;
11505
11506 elsif Full_Expander_Active then
11507 -- Associate discriminals with the first subprogram or entry body to
11508 -- be expanded.
11509
11510 if Present (First_Protected_Operation (Declarations (N))) then
11511 Set_Discriminals (Parent (Spec_Id));
11512 end if;
11513 end if;
11514 end Expand_Protected_Body_Declarations;
11515
11516 -------------------------
11517 -- External_Subprogram --
11518 -------------------------
11519
11520 function External_Subprogram (E : Entity_Id) return Entity_Id is
11521 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
11522
11523 begin
11524 -- The internal and external subprograms follow each other on the entity
11525 -- chain. Note that previously private operations had no separate
11526 -- external subprogram. We now create one in all cases, because a
11527 -- private operation may actually appear in an external call, through
11528 -- a 'Access reference used for a callback.
11529
11530 -- If the operation is a function that returns an anonymous access type,
11531 -- the corresponding itype appears before the operation, and must be
11532 -- skipped.
11533
11534 -- This mechanism is fragile, there should be a real link between the
11535 -- two versions of the operation, but there is no place to put it ???
11536
11537 if Is_Access_Type (Next_Entity (Subp)) then
11538 return Next_Entity (Next_Entity (Subp));
11539 else
11540 return Next_Entity (Subp);
11541 end if;
11542 end External_Subprogram;
11543
11544 ------------------------------
11545 -- Extract_Dispatching_Call --
11546 ------------------------------
11547
11548 procedure Extract_Dispatching_Call
11549 (N : Node_Id;
11550 Call_Ent : out Entity_Id;
11551 Object : out Entity_Id;
11552 Actuals : out List_Id;
11553 Formals : out List_Id)
11554 is
11555 Call_Nam : Node_Id;
11556
11557 begin
11558 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
11559
11560 if Present (Original_Node (N)) then
11561 Call_Nam := Name (Original_Node (N));
11562 else
11563 Call_Nam := Name (N);
11564 end if;
11565
11566 -- Retrieve the name of the dispatching procedure. It contains the
11567 -- dispatch table slot number.
11568
11569 loop
11570 case Nkind (Call_Nam) is
11571 when N_Identifier =>
11572 exit;
11573
11574 when N_Selected_Component =>
11575 Call_Nam := Selector_Name (Call_Nam);
11576
11577 when others =>
11578 raise Program_Error;
11579
11580 end case;
11581 end loop;
11582
11583 Actuals := Parameter_Associations (N);
11584 Call_Ent := Entity (Call_Nam);
11585 Formals := Parameter_Specifications (Parent (Call_Ent));
11586 Object := First (Actuals);
11587
11588 if Present (Original_Node (Object)) then
11589 Object := Original_Node (Object);
11590 end if;
11591
11592 -- If the type of the dispatching object is an access type then return
11593 -- an explicit dereference.
11594
11595 if Is_Access_Type (Etype (Object)) then
11596 Object := Make_Explicit_Dereference (Sloc (N), Object);
11597 Analyze (Object);
11598 end if;
11599 end Extract_Dispatching_Call;
11600
11601 -------------------
11602 -- Extract_Entry --
11603 -------------------
11604
11605 procedure Extract_Entry
11606 (N : Node_Id;
11607 Concval : out Node_Id;
11608 Ename : out Node_Id;
11609 Index : out Node_Id)
11610 is
11611 Nam : constant Node_Id := Name (N);
11612
11613 begin
11614 -- For a simple entry, the name is a selected component, with the
11615 -- prefix being the task value, and the selector being the entry.
11616
11617 if Nkind (Nam) = N_Selected_Component then
11618 Concval := Prefix (Nam);
11619 Ename := Selector_Name (Nam);
11620 Index := Empty;
11621
11622 -- For a member of an entry family, the name is an indexed component
11623 -- where the prefix is a selected component, whose prefix in turn is
11624 -- the task value, and whose selector is the entry family. The single
11625 -- expression in the expressions list of the indexed component is the
11626 -- subscript for the family.
11627
11628 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
11629 Concval := Prefix (Prefix (Nam));
11630 Ename := Selector_Name (Prefix (Nam));
11631 Index := First (Expressions (Nam));
11632 end if;
11633 end Extract_Entry;
11634
11635 -------------------
11636 -- Family_Offset --
11637 -------------------
11638
11639 function Family_Offset
11640 (Loc : Source_Ptr;
11641 Hi : Node_Id;
11642 Lo : Node_Id;
11643 Ttyp : Entity_Id;
11644 Cap : Boolean) return Node_Id
11645 is
11646 Ityp : Entity_Id;
11647 Real_Hi : Node_Id;
11648 Real_Lo : Node_Id;
11649
11650 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
11651 -- If one of the bounds is a reference to a discriminant, replace with
11652 -- corresponding discriminal of type. Within the body of a task retrieve
11653 -- the renamed discriminant by simple visibility, using its generated
11654 -- name. Within a protected object, find the original discriminant and
11655 -- replace it with the discriminal of the current protected operation.
11656
11657 ------------------------------
11658 -- Convert_Discriminant_Ref --
11659 ------------------------------
11660
11661 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
11662 Loc : constant Source_Ptr := Sloc (Bound);
11663 B : Node_Id;
11664 D : Entity_Id;
11665
11666 begin
11667 if Is_Entity_Name (Bound)
11668 and then Ekind (Entity (Bound)) = E_Discriminant
11669 then
11670 if Is_Task_Type (Ttyp)
11671 and then Has_Completion (Ttyp)
11672 then
11673 B := Make_Identifier (Loc, Chars (Entity (Bound)));
11674 Find_Direct_Name (B);
11675
11676 elsif Is_Protected_Type (Ttyp) then
11677 D := First_Discriminant (Ttyp);
11678 while Chars (D) /= Chars (Entity (Bound)) loop
11679 Next_Discriminant (D);
11680 end loop;
11681
11682 B := New_Reference_To (Discriminal (D), Loc);
11683
11684 else
11685 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
11686 end if;
11687
11688 elsif Nkind (Bound) = N_Attribute_Reference then
11689 return Bound;
11690
11691 else
11692 B := New_Copy_Tree (Bound);
11693 end if;
11694
11695 return
11696 Make_Attribute_Reference (Loc,
11697 Attribute_Name => Name_Pos,
11698 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
11699 Expressions => New_List (B));
11700 end Convert_Discriminant_Ref;
11701
11702 -- Start of processing for Family_Offset
11703
11704 begin
11705 Real_Hi := Convert_Discriminant_Ref (Hi);
11706 Real_Lo := Convert_Discriminant_Ref (Lo);
11707
11708 if Cap then
11709 if Is_Task_Type (Ttyp) then
11710 Ityp := RTE (RE_Task_Entry_Index);
11711 else
11712 Ityp := RTE (RE_Protected_Entry_Index);
11713 end if;
11714
11715 Real_Hi :=
11716 Make_Attribute_Reference (Loc,
11717 Prefix => New_Reference_To (Ityp, Loc),
11718 Attribute_Name => Name_Min,
11719 Expressions => New_List (
11720 Real_Hi,
11721 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
11722
11723 Real_Lo :=
11724 Make_Attribute_Reference (Loc,
11725 Prefix => New_Reference_To (Ityp, Loc),
11726 Attribute_Name => Name_Max,
11727 Expressions => New_List (
11728 Real_Lo,
11729 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
11730 end if;
11731
11732 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
11733 end Family_Offset;
11734
11735 -----------------
11736 -- Family_Size --
11737 -----------------
11738
11739 function Family_Size
11740 (Loc : Source_Ptr;
11741 Hi : Node_Id;
11742 Lo : Node_Id;
11743 Ttyp : Entity_Id;
11744 Cap : Boolean) return Node_Id
11745 is
11746 Ityp : Entity_Id;
11747
11748 begin
11749 if Is_Task_Type (Ttyp) then
11750 Ityp := RTE (RE_Task_Entry_Index);
11751 else
11752 Ityp := RTE (RE_Protected_Entry_Index);
11753 end if;
11754
11755 return
11756 Make_Attribute_Reference (Loc,
11757 Prefix => New_Reference_To (Ityp, Loc),
11758 Attribute_Name => Name_Max,
11759 Expressions => New_List (
11760 Make_Op_Add (Loc,
11761 Left_Opnd =>
11762 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
11763 Right_Opnd =>
11764 Make_Integer_Literal (Loc, 1)),
11765 Make_Integer_Literal (Loc, 0)));
11766 end Family_Size;
11767
11768 -----------------------
11769 -- Find_Master_Scope --
11770 -----------------------
11771
11772 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
11773 S : Entity_Id;
11774
11775 begin
11776 -- In Ada2005, the master is the innermost enclosing scope that is not
11777 -- transient. If the enclosing block is the rewriting of a call or the
11778 -- scope is an extended return statement this is valid master. The
11779 -- master in an extended return is only used within the return, and is
11780 -- subsequently overwritten in Move_Activation_Chain, but it must exist
11781 -- now before that overwriting occurs.
11782
11783 S := Scope (E);
11784
11785 if Ada_Version >= Ada_2005 then
11786 while Is_Internal (S) loop
11787 if Nkind (Parent (S)) = N_Block_Statement
11788 and then
11789 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
11790 then
11791 exit;
11792
11793 elsif Ekind (S) = E_Return_Statement then
11794 exit;
11795
11796 else
11797 S := Scope (S);
11798 end if;
11799 end loop;
11800 end if;
11801
11802 return S;
11803 end Find_Master_Scope;
11804
11805 -----------------------------------
11806 -- Find_Task_Or_Protected_Pragma --
11807 -----------------------------------
11808
11809 function Find_Task_Or_Protected_Pragma
11810 (T : Node_Id;
11811 P : Name_Id) return Node_Id
11812 is
11813 N : Node_Id;
11814
11815 begin
11816 N := First (Visible_Declarations (T));
11817 while Present (N) loop
11818 if Nkind (N) = N_Pragma then
11819 if Pragma_Name (N) = P then
11820 return N;
11821
11822 elsif P = Name_Priority
11823 and then Pragma_Name (N) = Name_Interrupt_Priority
11824 then
11825 return N;
11826
11827 else
11828 Next (N);
11829 end if;
11830
11831 else
11832 Next (N);
11833 end if;
11834 end loop;
11835
11836 N := First (Private_Declarations (T));
11837 while Present (N) loop
11838 if Nkind (N) = N_Pragma then
11839 if Pragma_Name (N) = P then
11840 return N;
11841
11842 elsif P = Name_Priority
11843 and then Pragma_Name (N) = Name_Interrupt_Priority
11844 then
11845 return N;
11846
11847 else
11848 Next (N);
11849 end if;
11850
11851 else
11852 Next (N);
11853 end if;
11854 end loop;
11855
11856 raise Program_Error;
11857 end Find_Task_Or_Protected_Pragma;
11858
11859 -------------------------------
11860 -- First_Protected_Operation --
11861 -------------------------------
11862
11863 function First_Protected_Operation (D : List_Id) return Node_Id is
11864 First_Op : Node_Id;
11865
11866 begin
11867 First_Op := First (D);
11868 while Present (First_Op)
11869 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
11870 loop
11871 Next (First_Op);
11872 end loop;
11873
11874 return First_Op;
11875 end First_Protected_Operation;
11876
11877 ---------------------------------------
11878 -- Install_Private_Data_Declarations --
11879 ---------------------------------------
11880
11881 procedure Install_Private_Data_Declarations
11882 (Loc : Source_Ptr;
11883 Spec_Id : Entity_Id;
11884 Conc_Typ : Entity_Id;
11885 Body_Nod : Node_Id;
11886 Decls : List_Id;
11887 Barrier : Boolean := False;
11888 Family : Boolean := False)
11889 is
11890 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
11891 Decl : Node_Id;
11892 Def : Node_Id;
11893 Insert_Node : Node_Id := Empty;
11894 Obj_Ent : Entity_Id;
11895
11896 procedure Add (Decl : Node_Id);
11897 -- Add a single declaration after Insert_Node. If this is the first
11898 -- addition, Decl is added to the front of Decls and it becomes the
11899 -- insertion node.
11900
11901 function Replace_Bound (Bound : Node_Id) return Node_Id;
11902 -- The bounds of an entry index may depend on discriminants, create a
11903 -- reference to the corresponding prival. Otherwise return a duplicate
11904 -- of the original bound.
11905
11906 ---------
11907 -- Add --
11908 ---------
11909
11910 procedure Add (Decl : Node_Id) is
11911 begin
11912 if No (Insert_Node) then
11913 Prepend_To (Decls, Decl);
11914 else
11915 Insert_After (Insert_Node, Decl);
11916 end if;
11917
11918 Insert_Node := Decl;
11919 end Add;
11920
11921 --------------------------
11922 -- Replace_Discriminant --
11923 --------------------------
11924
11925 function Replace_Bound (Bound : Node_Id) return Node_Id is
11926 begin
11927 if Nkind (Bound) = N_Identifier
11928 and then Is_Discriminal (Entity (Bound))
11929 then
11930 return Make_Identifier (Loc, Chars (Entity (Bound)));
11931 else
11932 return Duplicate_Subexpr (Bound);
11933 end if;
11934 end Replace_Bound;
11935
11936 -- Start of processing for Install_Private_Data_Declarations
11937
11938 begin
11939 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
11940 -- formal parameter _O, _object or _task depending on the context.
11941
11942 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
11943
11944 -- Special processing of _O for barrier functions, protected entries
11945 -- and families.
11946
11947 if Barrier
11948 or else
11949 (Is_Protected
11950 and then
11951 (Ekind (Spec_Id) = E_Entry
11952 or else Ekind (Spec_Id) = E_Entry_Family))
11953 then
11954 declare
11955 Conc_Rec : constant Entity_Id :=
11956 Corresponding_Record_Type (Conc_Typ);
11957 Typ_Id : constant Entity_Id :=
11958 Make_Defining_Identifier (Loc,
11959 New_External_Name (Chars (Conc_Rec), 'P'));
11960 begin
11961 -- Generate:
11962 -- type prot_typVP is access prot_typV;
11963
11964 Decl :=
11965 Make_Full_Type_Declaration (Loc,
11966 Defining_Identifier => Typ_Id,
11967 Type_Definition =>
11968 Make_Access_To_Object_Definition (Loc,
11969 Subtype_Indication =>
11970 New_Reference_To (Conc_Rec, Loc)));
11971 Add (Decl);
11972
11973 -- Generate:
11974 -- _object : prot_typVP := prot_typV (_O);
11975
11976 Decl :=
11977 Make_Object_Declaration (Loc,
11978 Defining_Identifier =>
11979 Make_Defining_Identifier (Loc, Name_uObject),
11980 Object_Definition => New_Reference_To (Typ_Id, Loc),
11981 Expression =>
11982 Unchecked_Convert_To (Typ_Id,
11983 New_Reference_To (Obj_Ent, Loc)));
11984 Add (Decl);
11985
11986 -- Set the reference to the concurrent object
11987
11988 Obj_Ent := Defining_Identifier (Decl);
11989 end;
11990 end if;
11991
11992 -- Step 2: Create the Protection object and build its declaration for
11993 -- any protected entry (family) of subprogram.
11994
11995 if Is_Protected then
11996 declare
11997 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
11998 Prot_Typ : RE_Id;
11999
12000 begin
12001 Set_Protection_Object (Spec_Id, Prot_Ent);
12002
12003 -- Determine the proper protection type
12004
12005 if Has_Attach_Handler (Conc_Typ)
12006 and then not Restricted_Profile
12007 then
12008 Prot_Typ := RE_Static_Interrupt_Protection;
12009
12010 elsif Has_Interrupt_Handler (Conc_Typ) then
12011 Prot_Typ := RE_Dynamic_Interrupt_Protection;
12012
12013 -- The type has explicit entries or generated primitive entry
12014 -- wrappers.
12015
12016 elsif Has_Entries (Conc_Typ)
12017 or else
12018 (Ada_Version >= Ada_2005
12019 and then Present (Interface_List (Parent (Conc_Typ))))
12020 then
12021 case Corresponding_Runtime_Package (Conc_Typ) is
12022 when System_Tasking_Protected_Objects_Entries =>
12023 Prot_Typ := RE_Protection_Entries;
12024
12025 when System_Tasking_Protected_Objects_Single_Entry =>
12026 Prot_Typ := RE_Protection_Entry;
12027
12028 when others =>
12029 raise Program_Error;
12030 end case;
12031
12032 else
12033 Prot_Typ := RE_Protection;
12034 end if;
12035
12036 -- Generate:
12037 -- conc_typR : protection_typ renames _object._object;
12038
12039 Decl :=
12040 Make_Object_Renaming_Declaration (Loc,
12041 Defining_Identifier => Prot_Ent,
12042 Subtype_Mark =>
12043 New_Reference_To (RTE (Prot_Typ), Loc),
12044 Name =>
12045 Make_Selected_Component (Loc,
12046 Prefix => New_Reference_To (Obj_Ent, Loc),
12047 Selector_Name => Make_Identifier (Loc, Name_uObject)));
12048 Add (Decl);
12049 end;
12050 end if;
12051
12052 -- Step 3: Add discriminant renamings (if any)
12053
12054 if Has_Discriminants (Conc_Typ) then
12055 declare
12056 D : Entity_Id;
12057
12058 begin
12059 D := First_Discriminant (Conc_Typ);
12060 while Present (D) loop
12061
12062 -- Adjust the source location
12063
12064 Set_Sloc (Discriminal (D), Loc);
12065
12066 -- Generate:
12067 -- discr_name : discr_typ renames _object.discr_name;
12068 -- or
12069 -- discr_name : discr_typ renames _task.discr_name;
12070
12071 Decl :=
12072 Make_Object_Renaming_Declaration (Loc,
12073 Defining_Identifier => Discriminal (D),
12074 Subtype_Mark => New_Reference_To (Etype (D), Loc),
12075 Name =>
12076 Make_Selected_Component (Loc,
12077 Prefix => New_Reference_To (Obj_Ent, Loc),
12078 Selector_Name => Make_Identifier (Loc, Chars (D))));
12079 Add (Decl);
12080
12081 Next_Discriminant (D);
12082 end loop;
12083 end;
12084 end if;
12085
12086 -- Step 4: Add private component renamings (if any)
12087
12088 if Is_Protected then
12089 Def := Protected_Definition (Parent (Conc_Typ));
12090
12091 if Present (Private_Declarations (Def)) then
12092 declare
12093 Comp : Node_Id;
12094 Comp_Id : Entity_Id;
12095 Decl_Id : Entity_Id;
12096
12097 begin
12098 Comp := First (Private_Declarations (Def));
12099 while Present (Comp) loop
12100 if Nkind (Comp) = N_Component_Declaration then
12101 Comp_Id := Defining_Identifier (Comp);
12102 Decl_Id :=
12103 Make_Defining_Identifier (Loc, Chars (Comp_Id));
12104
12105 -- Minimal decoration
12106
12107 if Ekind (Spec_Id) = E_Function then
12108 Set_Ekind (Decl_Id, E_Constant);
12109 else
12110 Set_Ekind (Decl_Id, E_Variable);
12111 end if;
12112
12113 Set_Prival (Comp_Id, Decl_Id);
12114 Set_Prival_Link (Decl_Id, Comp_Id);
12115 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
12116
12117 -- Generate:
12118 -- comp_name : comp_typ renames _object.comp_name;
12119
12120 Decl :=
12121 Make_Object_Renaming_Declaration (Loc,
12122 Defining_Identifier => Decl_Id,
12123 Subtype_Mark =>
12124 New_Reference_To (Etype (Comp_Id), Loc),
12125 Name =>
12126 Make_Selected_Component (Loc,
12127 Prefix =>
12128 New_Reference_To (Obj_Ent, Loc),
12129 Selector_Name =>
12130 Make_Identifier (Loc, Chars (Comp_Id))));
12131 Add (Decl);
12132 end if;
12133
12134 Next (Comp);
12135 end loop;
12136 end;
12137 end if;
12138 end if;
12139
12140 -- Step 5: Add the declaration of the entry index and the associated
12141 -- type for barrier functions and entry families.
12142
12143 if (Barrier and then Family)
12144 or else Ekind (Spec_Id) = E_Entry_Family
12145 then
12146 declare
12147 E : constant Entity_Id := Index_Object (Spec_Id);
12148 Index : constant Entity_Id :=
12149 Defining_Identifier (
12150 Entry_Index_Specification (
12151 Entry_Body_Formal_Part (Body_Nod)));
12152 Index_Con : constant Entity_Id :=
12153 Make_Defining_Identifier (Loc, Chars (Index));
12154 High : Node_Id;
12155 Index_Typ : Entity_Id;
12156 Low : Node_Id;
12157
12158 begin
12159 -- Minimal decoration
12160
12161 Set_Ekind (Index_Con, E_Constant);
12162 Set_Entry_Index_Constant (Index, Index_Con);
12163 Set_Discriminal_Link (Index_Con, Index);
12164
12165 -- Retrieve the bounds of the entry family
12166
12167 High := Type_High_Bound (Etype (Index));
12168 Low := Type_Low_Bound (Etype (Index));
12169
12170 -- In the simple case the entry family is given by a subtype
12171 -- mark and the index constant has the same type.
12172
12173 if Is_Entity_Name (Original_Node (
12174 Discrete_Subtype_Definition (Parent (Index))))
12175 then
12176 Index_Typ := Etype (Index);
12177
12178 -- Otherwise a new subtype declaration is required
12179
12180 else
12181 High := Replace_Bound (High);
12182 Low := Replace_Bound (Low);
12183
12184 Index_Typ := Make_Temporary (Loc, 'J');
12185
12186 -- Generate:
12187 -- subtype Jnn is <Etype of Index> range Low .. High;
12188
12189 Decl :=
12190 Make_Subtype_Declaration (Loc,
12191 Defining_Identifier => Index_Typ,
12192 Subtype_Indication =>
12193 Make_Subtype_Indication (Loc,
12194 Subtype_Mark =>
12195 New_Reference_To (Base_Type (Etype (Index)), Loc),
12196 Constraint =>
12197 Make_Range_Constraint (Loc,
12198 Range_Expression =>
12199 Make_Range (Loc, Low, High))));
12200 Add (Decl);
12201 end if;
12202
12203 Set_Etype (Index_Con, Index_Typ);
12204
12205 -- Create the object which designates the index:
12206 -- J : constant Jnn :=
12207 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
12208 --
12209 -- where Jnn is the subtype created above or the original type of
12210 -- the index, _E is a formal of the protected body subprogram and
12211 -- <index expr> is the index of the first family member.
12212
12213 Decl :=
12214 Make_Object_Declaration (Loc,
12215 Defining_Identifier => Index_Con,
12216 Constant_Present => True,
12217 Object_Definition =>
12218 New_Reference_To (Index_Typ, Loc),
12219
12220 Expression =>
12221 Make_Attribute_Reference (Loc,
12222 Prefix =>
12223 New_Reference_To (Index_Typ, Loc),
12224 Attribute_Name => Name_Val,
12225
12226 Expressions => New_List (
12227
12228 Make_Op_Add (Loc,
12229 Left_Opnd =>
12230 Make_Op_Subtract (Loc,
12231 Left_Opnd =>
12232 New_Reference_To (E, Loc),
12233 Right_Opnd =>
12234 Entry_Index_Expression (Loc,
12235 Defining_Identifier (Body_Nod),
12236 Empty, Conc_Typ)),
12237
12238 Right_Opnd =>
12239 Make_Attribute_Reference (Loc,
12240 Prefix =>
12241 New_Reference_To (Index_Typ, Loc),
12242 Attribute_Name => Name_Pos,
12243 Expressions => New_List (
12244 Make_Attribute_Reference (Loc,
12245 Prefix =>
12246 New_Reference_To (Index_Typ, Loc),
12247 Attribute_Name => Name_First)))))));
12248 Add (Decl);
12249 end;
12250 end if;
12251 end Install_Private_Data_Declarations;
12252
12253 ---------------------------------
12254 -- Is_Potentially_Large_Family --
12255 ---------------------------------
12256
12257 function Is_Potentially_Large_Family
12258 (Base_Index : Entity_Id;
12259 Conctyp : Entity_Id;
12260 Lo : Node_Id;
12261 Hi : Node_Id) return Boolean
12262 is
12263 begin
12264 return Scope (Base_Index) = Standard_Standard
12265 and then Base_Index = Base_Type (Standard_Integer)
12266 and then Has_Discriminants (Conctyp)
12267 and then Present
12268 (Discriminant_Default_Value (First_Discriminant (Conctyp)))
12269 and then
12270 (Denotes_Discriminant (Lo, True)
12271 or else Denotes_Discriminant (Hi, True));
12272 end Is_Potentially_Large_Family;
12273
12274 -------------------------------------
12275 -- Is_Private_Primitive_Subprogram --
12276 -------------------------------------
12277
12278 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
12279 begin
12280 return
12281 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
12282 and then Is_Private_Primitive (Id);
12283 end Is_Private_Primitive_Subprogram;
12284
12285 ------------------
12286 -- Index_Object --
12287 ------------------
12288
12289 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
12290 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
12291 Formal : Entity_Id;
12292
12293 begin
12294 Formal := First_Formal (Bod_Subp);
12295 while Present (Formal) loop
12296
12297 -- Look for formal parameter _E
12298
12299 if Chars (Formal) = Name_uE then
12300 return Formal;
12301 end if;
12302
12303 Next_Formal (Formal);
12304 end loop;
12305
12306 -- A protected body subprogram should always have the parameter in
12307 -- question.
12308
12309 raise Program_Error;
12310 end Index_Object;
12311
12312 --------------------------------
12313 -- Make_Initialize_Protection --
12314 --------------------------------
12315
12316 function Make_Initialize_Protection
12317 (Protect_Rec : Entity_Id) return List_Id
12318 is
12319 Loc : constant Source_Ptr := Sloc (Protect_Rec);
12320 P_Arr : Entity_Id;
12321 Pdef : Node_Id;
12322 Pdec : Node_Id;
12323 Ptyp : constant Node_Id :=
12324 Corresponding_Concurrent_Type (Protect_Rec);
12325 Args : List_Id;
12326 L : constant List_Id := New_List;
12327 Has_Entry : constant Boolean := Has_Entries (Ptyp);
12328 Restricted : constant Boolean := Restricted_Profile;
12329
12330 begin
12331 -- We may need two calls to properly initialize the object, one to
12332 -- Initialize_Protection, and possibly one to Install_Handlers if we
12333 -- have a pragma Attach_Handler.
12334
12335 -- Get protected declaration. In the case of a task type declaration,
12336 -- this is simply the parent of the protected type entity. In the single
12337 -- protected object declaration, this parent will be the implicit type,
12338 -- and we can find the corresponding single protected object declaration
12339 -- by searching forward in the declaration list in the tree.
12340
12341 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
12342 -- of this type should have been removed during semantic analysis.
12343
12344 Pdec := Parent (Ptyp);
12345 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
12346 N_Single_Protected_Declaration)
12347 loop
12348 Next (Pdec);
12349 end loop;
12350
12351 -- Now we can find the object definition from this declaration
12352
12353 Pdef := Protected_Definition (Pdec);
12354
12355 -- Build the parameter list for the call. Note that _Init is the name
12356 -- of the formal for the object to be initialized, which is the task
12357 -- value record itself.
12358
12359 Args := New_List;
12360
12361 -- Object parameter. This is a pointer to the object of type
12362 -- Protection used by the GNARL to control the protected object.
12363
12364 Append_To (Args,
12365 Make_Attribute_Reference (Loc,
12366 Prefix =>
12367 Make_Selected_Component (Loc,
12368 Prefix => Make_Identifier (Loc, Name_uInit),
12369 Selector_Name => Make_Identifier (Loc, Name_uObject)),
12370 Attribute_Name => Name_Unchecked_Access));
12371
12372 -- Priority parameter. Set to Unspecified_Priority unless there is a
12373 -- priority pragma, in which case we take the value from the pragma,
12374 -- or there is an interrupt pragma and no priority pragma, and we
12375 -- set the ceiling to Interrupt_Priority'Last, an implementation-
12376 -- defined value, see D.3(10).
12377
12378 if Present (Pdef)
12379 and then Has_Pragma_Priority (Pdef)
12380 then
12381 declare
12382 Prio : constant Node_Id :=
12383 Expression
12384 (First
12385 (Pragma_Argument_Associations
12386 (Find_Task_Or_Protected_Pragma
12387 (Pdef, Name_Priority))));
12388 Temp : Entity_Id;
12389
12390 begin
12391 -- If priority is a static expression, then we can duplicate it
12392 -- with no problem and simply append it to the argument list.
12393
12394 if Is_Static_Expression (Prio) then
12395 Append_To (Args,
12396 Duplicate_Subexpr_No_Checks (Prio));
12397
12398 -- Otherwise, the priority may be a per-object expression, if it
12399 -- depends on a discriminant of the type. In this case, create
12400 -- local variable to capture the expression. Note that it is
12401 -- really necessary to create this variable explicitly. It might
12402 -- be thought that removing side effects would the appropriate
12403 -- approach, but that could generate declarations improperly
12404 -- placed in the enclosing scope.
12405
12406 -- Note: Use System.Any_Priority as the expected type for the
12407 -- non-static priority expression, in case the expression has not
12408 -- been analyzed yet (as occurs for example with pragma
12409 -- Interrupt_Priority).
12410
12411 else
12412 Temp := Make_Temporary (Loc, 'R', Prio);
12413 Append_To (L,
12414 Make_Object_Declaration (Loc,
12415 Defining_Identifier => Temp,
12416 Object_Definition =>
12417 New_Occurrence_Of (RTE (RE_Any_Priority), Loc),
12418 Expression => Relocate_Node (Prio)));
12419
12420 Append_To (Args, New_Occurrence_Of (Temp, Loc));
12421 end if;
12422 end;
12423
12424 -- When no priority is specified but an xx_Handler pragma is, we default
12425 -- to System.Interrupts.Default_Interrupt_Priority, see D.3(10).
12426
12427 elsif Has_Interrupt_Handler (Ptyp)
12428 or else Has_Attach_Handler (Ptyp)
12429 then
12430 Append_To (Args,
12431 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
12432
12433 -- Normal case, no priority or xx_Handler specified, default priority
12434
12435 else
12436 Append_To (Args,
12437 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
12438 end if;
12439
12440 -- Test for Compiler_Info parameter. This parameter allows entry body
12441 -- procedures and barrier functions to be called from the runtime. It
12442 -- is a pointer to the record generated by the compiler to represent
12443 -- the protected object.
12444
12445 -- A protected type without entries that covers an interface and
12446 -- overrides the abstract routines with protected procedures is
12447 -- considered equivalent to a protected type with entries in the
12448 -- context of dispatching select statements.
12449
12450 if Has_Entry
12451 or else Has_Interrupt_Handler (Ptyp)
12452 or else Has_Attach_Handler (Ptyp)
12453 or else Has_Interfaces (Protect_Rec)
12454 then
12455 declare
12456 Pkg_Id : constant RTU_Id :=
12457 Corresponding_Runtime_Package (Ptyp);
12458 Called_Subp : RE_Id;
12459
12460 begin
12461 case Pkg_Id is
12462 when System_Tasking_Protected_Objects_Entries =>
12463 Called_Subp := RE_Initialize_Protection_Entries;
12464
12465 when System_Tasking_Protected_Objects =>
12466 Called_Subp := RE_Initialize_Protection;
12467
12468 when System_Tasking_Protected_Objects_Single_Entry =>
12469 Called_Subp := RE_Initialize_Protection_Entry;
12470
12471 when others =>
12472 raise Program_Error;
12473 end case;
12474
12475 if Has_Entry
12476 or else not Restricted
12477 or else Has_Interfaces (Protect_Rec)
12478 then
12479 Append_To (Args,
12480 Make_Attribute_Reference (Loc,
12481 Prefix => Make_Identifier (Loc, Name_uInit),
12482 Attribute_Name => Name_Address));
12483 end if;
12484
12485 -- Entry_Bodies parameter. This is a pointer to an array of
12486 -- pointers to the entry body procedures and barrier functions of
12487 -- the object. If the protected type has no entries this object
12488 -- will not exist, in this case, pass a null.
12489
12490 if Has_Entry then
12491 P_Arr := Entry_Bodies_Array (Ptyp);
12492
12493 Append_To (Args,
12494 Make_Attribute_Reference (Loc,
12495 Prefix => New_Reference_To (P_Arr, Loc),
12496 Attribute_Name => Name_Unrestricted_Access));
12497
12498 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
12499
12500 -- Find index mapping function (clumsy but ok for now)
12501
12502 while Ekind (P_Arr) /= E_Function loop
12503 Next_Entity (P_Arr);
12504 end loop;
12505
12506 Append_To (Args,
12507 Make_Attribute_Reference (Loc,
12508 Prefix =>
12509 New_Reference_To (P_Arr, Loc),
12510 Attribute_Name => Name_Unrestricted_Access));
12511
12512 -- Build_Entry_Names generation flag. When set to true, the
12513 -- runtime will allocate an array to hold the string names
12514 -- of protected entries.
12515
12516 if not Restricted_Profile then
12517 if Entry_Names_OK then
12518 Append_To (Args,
12519 New_Reference_To (Standard_True, Loc));
12520 else
12521 Append_To (Args,
12522 New_Reference_To (Standard_False, Loc));
12523 end if;
12524 end if;
12525 end if;
12526
12527 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
12528 Append_To (Args, Make_Null (Loc));
12529
12530 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
12531 Append_To (Args, Make_Null (Loc));
12532 Append_To (Args, Make_Null (Loc));
12533 Append_To (Args, New_Reference_To (Standard_False, Loc));
12534 end if;
12535
12536 Append_To (L,
12537 Make_Procedure_Call_Statement (Loc,
12538 Name => New_Reference_To (RTE (Called_Subp), Loc),
12539 Parameter_Associations => Args));
12540 end;
12541 else
12542 Append_To (L,
12543 Make_Procedure_Call_Statement (Loc,
12544 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
12545 Parameter_Associations => Args));
12546 end if;
12547
12548 if Has_Attach_Handler (Ptyp) then
12549
12550 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
12551 -- make the following call:
12552
12553 -- Install_Handlers (_object,
12554 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
12555
12556 -- or, in the case of Ravenscar:
12557
12558 -- Install_Restricted_Handlers
12559 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
12560
12561 declare
12562 Args : constant List_Id := New_List;
12563 Table : constant List_Id := New_List;
12564 Ritem : Node_Id := First_Rep_Item (Ptyp);
12565
12566 begin
12567 -- Build the Attach_Handler table argument
12568
12569 while Present (Ritem) loop
12570 if Nkind (Ritem) = N_Pragma
12571 and then Pragma_Name (Ritem) = Name_Attach_Handler
12572 then
12573 declare
12574 Handler : constant Node_Id :=
12575 First (Pragma_Argument_Associations (Ritem));
12576
12577 Interrupt : constant Node_Id := Next (Handler);
12578 Expr : constant Node_Id := Expression (Interrupt);
12579
12580 begin
12581 Append_To (Table,
12582 Make_Aggregate (Loc, Expressions => New_List (
12583 Unchecked_Convert_To
12584 (RTE (RE_System_Interrupt_Id), Expr),
12585 Make_Attribute_Reference (Loc,
12586 Prefix => Make_Selected_Component (Loc,
12587 Make_Identifier (Loc, Name_uInit),
12588 Duplicate_Subexpr_No_Checks
12589 (Expression (Handler))),
12590 Attribute_Name => Name_Access))));
12591 end;
12592 end if;
12593
12594 Next_Rep_Item (Ritem);
12595 end loop;
12596
12597 -- Append the table argument we just built
12598
12599 Append_To (Args, Make_Aggregate (Loc, Table));
12600
12601 -- Append the Install_Handlers (or Install_Restricted_Handlers)
12602 -- call to the statements.
12603
12604 if Restricted then
12605 -- Call a simplified version of Install_Handlers to be used
12606 -- when the Ravenscar restrictions are in effect
12607 -- (Install_Restricted_Handlers).
12608
12609 Append_To (L,
12610 Make_Procedure_Call_Statement (Loc,
12611 Name =>
12612 New_Reference_To
12613 (RTE (RE_Install_Restricted_Handlers), Loc),
12614 Parameter_Associations => Args));
12615
12616 else
12617 -- First, prepends the _object argument
12618
12619 Prepend_To (Args,
12620 Make_Attribute_Reference (Loc,
12621 Prefix =>
12622 Make_Selected_Component (Loc,
12623 Prefix => Make_Identifier (Loc, Name_uInit),
12624 Selector_Name => Make_Identifier (Loc, Name_uObject)),
12625 Attribute_Name => Name_Unchecked_Access));
12626
12627 -- Then, insert call to Install_Handlers
12628
12629 Append_To (L,
12630 Make_Procedure_Call_Statement (Loc,
12631 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
12632 Parameter_Associations => Args));
12633 end if;
12634 end;
12635 end if;
12636
12637 return L;
12638 end Make_Initialize_Protection;
12639
12640 ---------------------------
12641 -- Make_Task_Create_Call --
12642 ---------------------------
12643
12644 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
12645 Loc : constant Source_Ptr := Sloc (Task_Rec);
12646 Args : List_Id;
12647 Ecount : Node_Id;
12648 Name : Node_Id;
12649 Tdec : Node_Id;
12650 Tdef : Node_Id;
12651 Tnam : Name_Id;
12652 Ttyp : Node_Id;
12653
12654 begin
12655 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
12656 Tnam := Chars (Ttyp);
12657
12658 -- Get task declaration. In the case of a task type declaration, this is
12659 -- simply the parent of the task type entity. In the single task
12660 -- declaration, this parent will be the implicit type, and we can find
12661 -- the corresponding single task declaration by searching forward in the
12662 -- declaration list in the tree.
12663
12664 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
12665 -- this type should have been removed during semantic analysis.
12666
12667 Tdec := Parent (Ttyp);
12668 while not Nkind_In (Tdec, N_Task_Type_Declaration,
12669 N_Single_Task_Declaration)
12670 loop
12671 Next (Tdec);
12672 end loop;
12673
12674 -- Now we can find the task definition from this declaration
12675
12676 Tdef := Task_Definition (Tdec);
12677
12678 -- Build the parameter list for the call. Note that _Init is the name
12679 -- of the formal for the object to be initialized, which is the task
12680 -- value record itself.
12681
12682 Args := New_List;
12683
12684 -- Priority parameter. Set to Unspecified_Priority unless there is a
12685 -- priority pragma, in which case we take the value from the pragma.
12686
12687 if Present (Tdef) and then Has_Pragma_Priority (Tdef) then
12688 Append_To (Args,
12689 Make_Selected_Component (Loc,
12690 Prefix => Make_Identifier (Loc, Name_uInit),
12691 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
12692 else
12693 Append_To (Args,
12694 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
12695 end if;
12696
12697 -- Optional Stack parameter
12698
12699 if Restricted_Profile then
12700
12701 -- If the stack has been preallocated by the expander then
12702 -- pass its address. Otherwise, pass a null address.
12703
12704 if Preallocated_Stacks_On_Target then
12705 Append_To (Args,
12706 Make_Attribute_Reference (Loc,
12707 Prefix =>
12708 Make_Selected_Component (Loc,
12709 Prefix => Make_Identifier (Loc, Name_uInit),
12710 Selector_Name => Make_Identifier (Loc, Name_uStack)),
12711 Attribute_Name => Name_Address));
12712
12713 else
12714 Append_To (Args,
12715 New_Reference_To (RTE (RE_Null_Address), Loc));
12716 end if;
12717 end if;
12718
12719 -- Size parameter. If no Storage_Size pragma is present, then
12720 -- the size is taken from the taskZ variable for the type, which
12721 -- is either Unspecified_Size, or has been reset by the use of
12722 -- a Storage_Size attribute definition clause. If a pragma is
12723 -- present, then the size is taken from the _Size field of the
12724 -- task value record, which was set from the pragma value.
12725
12726 if Present (Tdef)
12727 and then Has_Storage_Size_Pragma (Tdef)
12728 then
12729 Append_To (Args,
12730 Make_Selected_Component (Loc,
12731 Prefix => Make_Identifier (Loc, Name_uInit),
12732 Selector_Name => Make_Identifier (Loc, Name_uSize)));
12733
12734 else
12735 Append_To (Args,
12736 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
12737 end if;
12738
12739 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
12740 -- Task_Info pragma, in which case we take the value from the pragma.
12741
12742 if Present (Tdef)
12743 and then Has_Task_Info_Pragma (Tdef)
12744 then
12745 Append_To (Args,
12746 Make_Selected_Component (Loc,
12747 Prefix => Make_Identifier (Loc, Name_uInit),
12748 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
12749
12750 else
12751 Append_To (Args,
12752 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
12753 end if;
12754
12755 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU pragma,
12756 -- in which case we take the value from the pragma. The parameter is
12757 -- passed as an Integer because in the case of unspecified CPU the
12758 -- value is not in the range of CPU_Range.
12759
12760 if Present (Tdef) and then Has_Pragma_CPU (Tdef) then
12761 Append_To (Args,
12762 Convert_To (Standard_Integer,
12763 Make_Selected_Component (Loc,
12764 Prefix => Make_Identifier (Loc, Name_uInit),
12765 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
12766
12767 else
12768 Append_To (Args,
12769 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
12770 end if;
12771
12772 if not Restricted_Profile then
12773
12774 -- Deadline parameter. If no Relative_Deadline pragma is present,
12775 -- then the deadline is Time_Span_Zero. If a pragma is present, then
12776 -- the deadline is taken from the _Relative_Deadline field of the
12777 -- task value record, which was set from the pragma value. Note that
12778 -- this parameter must not be generated for the restricted profiles
12779 -- since Ravenscar does not allow deadlines.
12780
12781 -- Case where pragma Relative_Deadline applies: use given value
12782
12783 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
12784 Append_To (Args,
12785 Make_Selected_Component (Loc,
12786 Prefix =>
12787 Make_Identifier (Loc, Name_uInit),
12788 Selector_Name =>
12789 Make_Identifier (Loc, Name_uRelative_Deadline)));
12790
12791 -- No pragma Relative_Deadline apply to the task
12792
12793 else
12794 Append_To (Args,
12795 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
12796 end if;
12797
12798 -- Number of entries. This is an expression of the form:
12799
12800 -- n + _Init.a'Length + _Init.a'B'Length + ...
12801
12802 -- where a,b... are the entry family names for the task definition
12803
12804 Ecount :=
12805 Build_Entry_Count_Expression
12806 (Ttyp,
12807 Component_Items
12808 (Component_List
12809 (Type_Definition
12810 (Parent (Corresponding_Record_Type (Ttyp))))),
12811 Loc);
12812 Append_To (Args, Ecount);
12813
12814 -- Master parameter. This is a reference to the _Master parameter of
12815 -- the initialization procedure, except in the case of the pragma
12816 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
12817 -- System.Tasking.Library_Task_Level.
12818
12819 if Restriction_Active (No_Task_Hierarchy) = False then
12820 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
12821 else
12822 Append_To (Args,
12823 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
12824 end if;
12825 end if;
12826
12827 -- State parameter. This is a pointer to the task body procedure. The
12828 -- required value is obtained by taking 'Unrestricted_Access of the task
12829 -- body procedure and converting it (with an unchecked conversion) to
12830 -- the type required by the task kernel. For further details, see the
12831 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
12832 -- than 'Address in order to avoid creating trampolines.
12833
12834 declare
12835 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
12836 Subp_Ptr_Typ : constant Node_Id :=
12837 Create_Itype (E_Access_Subprogram_Type, Tdec);
12838 Ref : constant Node_Id := Make_Itype_Reference (Loc);
12839
12840 begin
12841 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
12842 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
12843
12844 -- Be sure to freeze a reference to the access-to-subprogram type,
12845 -- otherwise gigi will complain that it's in the wrong scope, because
12846 -- it's actually inside the init procedure for the record type that
12847 -- corresponds to the task type.
12848
12849 -- This processing is causing a crash in the .NET/JVM back ends that
12850 -- is not yet understood, so skip it in these cases ???
12851
12852 if VM_Target = No_VM then
12853 Set_Itype (Ref, Subp_Ptr_Typ);
12854 Append_Freeze_Action (Task_Rec, Ref);
12855
12856 Append_To (Args,
12857 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
12858 Make_Qualified_Expression (Loc,
12859 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
12860 Expression =>
12861 Make_Attribute_Reference (Loc,
12862 Prefix =>
12863 New_Occurrence_Of (Body_Proc, Loc),
12864 Attribute_Name => Name_Unrestricted_Access))));
12865
12866 -- For the .NET/JVM cases revert to the original code below ???
12867
12868 else
12869 Append_To (Args,
12870 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
12871 Make_Attribute_Reference (Loc,
12872 Prefix =>
12873 New_Occurrence_Of (Body_Proc, Loc),
12874 Attribute_Name => Name_Address)));
12875 end if;
12876 end;
12877
12878 -- Discriminants parameter. This is just the address of the task
12879 -- value record itself (which contains the discriminant values
12880
12881 Append_To (Args,
12882 Make_Attribute_Reference (Loc,
12883 Prefix => Make_Identifier (Loc, Name_uInit),
12884 Attribute_Name => Name_Address));
12885
12886 -- Elaborated parameter. This is an access to the elaboration Boolean
12887
12888 Append_To (Args,
12889 Make_Attribute_Reference (Loc,
12890 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
12891 Attribute_Name => Name_Unchecked_Access));
12892
12893 -- Chain parameter. This is a reference to the _Chain parameter of
12894 -- the initialization procedure.
12895
12896 Append_To (Args, Make_Identifier (Loc, Name_uChain));
12897
12898 -- Task name parameter. Take this from the _Task_Id parameter to the
12899 -- init call unless there is a Task_Name pragma, in which case we take
12900 -- the value from the pragma.
12901
12902 if Present (Tdef)
12903 and then Has_Task_Name_Pragma (Tdef)
12904 then
12905 -- Copy expression in full, because it may be dynamic and have
12906 -- side effects.
12907
12908 Append_To (Args,
12909 New_Copy_Tree
12910 (Expression (First
12911 (Pragma_Argument_Associations
12912 (Find_Task_Or_Protected_Pragma
12913 (Tdef, Name_Task_Name))))));
12914
12915 else
12916 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
12917 end if;
12918
12919 -- Created_Task parameter. This is the _Task_Id field of the task
12920 -- record value
12921
12922 Append_To (Args,
12923 Make_Selected_Component (Loc,
12924 Prefix => Make_Identifier (Loc, Name_uInit),
12925 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
12926
12927 -- Build_Entry_Names generation flag. When set to true, the runtime
12928 -- will allocate an array to hold the string names of task entries.
12929
12930 if not Restricted_Profile then
12931 if Has_Entries (Ttyp)
12932 and then Entry_Names_OK
12933 then
12934 Append_To (Args, New_Reference_To (Standard_True, Loc));
12935 else
12936 Append_To (Args, New_Reference_To (Standard_False, Loc));
12937 end if;
12938 end if;
12939
12940 if Restricted_Profile then
12941 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
12942 else
12943 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
12944 end if;
12945
12946 return
12947 Make_Procedure_Call_Statement (Loc,
12948 Name => Name,
12949 Parameter_Associations => Args);
12950 end Make_Task_Create_Call;
12951
12952 ------------------------------
12953 -- Next_Protected_Operation --
12954 ------------------------------
12955
12956 function Next_Protected_Operation (N : Node_Id) return Node_Id is
12957 Next_Op : Node_Id;
12958
12959 begin
12960 Next_Op := Next (N);
12961 while Present (Next_Op)
12962 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
12963 loop
12964 Next (Next_Op);
12965 end loop;
12966
12967 return Next_Op;
12968 end Next_Protected_Operation;
12969
12970 ---------------------
12971 -- Null_Statements --
12972 ---------------------
12973
12974 function Null_Statements (Stats : List_Id) return Boolean is
12975 Stmt : Node_Id;
12976
12977 begin
12978 Stmt := First (Stats);
12979 while Nkind (Stmt) /= N_Empty
12980 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
12981 or else
12982 (Nkind (Stmt) = N_Pragma
12983 and then (Pragma_Name (Stmt) = Name_Unreferenced
12984 or else
12985 Pragma_Name (Stmt) = Name_Unmodified
12986 or else
12987 Pragma_Name (Stmt) = Name_Warnings)))
12988 loop
12989 Next (Stmt);
12990 end loop;
12991
12992 return Nkind (Stmt) = N_Empty;
12993 end Null_Statements;
12994
12995 --------------------------
12996 -- Parameter_Block_Pack --
12997 --------------------------
12998
12999 function Parameter_Block_Pack
13000 (Loc : Source_Ptr;
13001 Blk_Typ : Entity_Id;
13002 Actuals : List_Id;
13003 Formals : List_Id;
13004 Decls : List_Id;
13005 Stmts : List_Id) return Node_Id
13006 is
13007 Actual : Entity_Id;
13008 Expr : Node_Id := Empty;
13009 Formal : Entity_Id;
13010 Has_Param : Boolean := False;
13011 P : Entity_Id;
13012 Params : List_Id;
13013 Temp_Asn : Node_Id;
13014 Temp_Nam : Node_Id;
13015
13016 begin
13017 Actual := First (Actuals);
13018 Formal := Defining_Identifier (First (Formals));
13019 Params := New_List;
13020
13021 while Present (Actual) loop
13022 if Is_By_Copy_Type (Etype (Actual)) then
13023 -- Generate:
13024 -- Jnn : aliased <formal-type>
13025
13026 Temp_Nam := Make_Temporary (Loc, 'J');
13027
13028 Append_To (Decls,
13029 Make_Object_Declaration (Loc,
13030 Aliased_Present =>
13031 True,
13032 Defining_Identifier =>
13033 Temp_Nam,
13034 Object_Definition =>
13035 New_Reference_To (Etype (Formal), Loc)));
13036
13037 if Ekind (Formal) /= E_Out_Parameter then
13038
13039 -- Generate:
13040 -- Jnn := <actual>
13041
13042 Temp_Asn :=
13043 New_Reference_To (Temp_Nam, Loc);
13044
13045 Set_Assignment_OK (Temp_Asn);
13046
13047 Append_To (Stmts,
13048 Make_Assignment_Statement (Loc,
13049 Name =>
13050 Temp_Asn,
13051 Expression =>
13052 New_Copy_Tree (Actual)));
13053 end if;
13054
13055 -- Generate:
13056 -- Jnn'unchecked_access
13057
13058 Append_To (Params,
13059 Make_Attribute_Reference (Loc,
13060 Attribute_Name =>
13061 Name_Unchecked_Access,
13062 Prefix =>
13063 New_Reference_To (Temp_Nam, Loc)));
13064
13065 Has_Param := True;
13066
13067 -- The controlling parameter is omitted
13068
13069 else
13070 if not Is_Controlling_Actual (Actual) then
13071 Append_To (Params,
13072 Make_Reference (Loc, New_Copy_Tree (Actual)));
13073
13074 Has_Param := True;
13075 end if;
13076 end if;
13077
13078 Next_Actual (Actual);
13079 Next_Formal_With_Extras (Formal);
13080 end loop;
13081
13082 if Has_Param then
13083 Expr := Make_Aggregate (Loc, Params);
13084 end if;
13085
13086 -- Generate:
13087 -- P : Ann := (
13088 -- J1'unchecked_access;
13089 -- <actual2>'reference;
13090 -- ...);
13091
13092 P := Make_Temporary (Loc, 'P');
13093
13094 Append_To (Decls,
13095 Make_Object_Declaration (Loc,
13096 Defining_Identifier =>
13097 P,
13098 Object_Definition =>
13099 New_Reference_To (Blk_Typ, Loc),
13100 Expression =>
13101 Expr));
13102
13103 return P;
13104 end Parameter_Block_Pack;
13105
13106 ----------------------------
13107 -- Parameter_Block_Unpack --
13108 ----------------------------
13109
13110 function Parameter_Block_Unpack
13111 (Loc : Source_Ptr;
13112 P : Entity_Id;
13113 Actuals : List_Id;
13114 Formals : List_Id) return List_Id
13115 is
13116 Actual : Entity_Id;
13117 Asnmt : Node_Id;
13118 Formal : Entity_Id;
13119 Has_Asnmt : Boolean := False;
13120 Result : constant List_Id := New_List;
13121
13122 begin
13123 Actual := First (Actuals);
13124 Formal := Defining_Identifier (First (Formals));
13125 while Present (Actual) loop
13126 if Is_By_Copy_Type (Etype (Actual))
13127 and then Ekind (Formal) /= E_In_Parameter
13128 then
13129 -- Generate:
13130 -- <actual> := P.<formal>;
13131
13132 Asnmt :=
13133 Make_Assignment_Statement (Loc,
13134 Name =>
13135 New_Copy (Actual),
13136 Expression =>
13137 Make_Explicit_Dereference (Loc,
13138 Make_Selected_Component (Loc,
13139 Prefix =>
13140 New_Reference_To (P, Loc),
13141 Selector_Name =>
13142 Make_Identifier (Loc, Chars (Formal)))));
13143
13144 Set_Assignment_OK (Name (Asnmt));
13145 Append_To (Result, Asnmt);
13146
13147 Has_Asnmt := True;
13148 end if;
13149
13150 Next_Actual (Actual);
13151 Next_Formal_With_Extras (Formal);
13152 end loop;
13153
13154 if Has_Asnmt then
13155 return Result;
13156 else
13157 return New_List (Make_Null_Statement (Loc));
13158 end if;
13159 end Parameter_Block_Unpack;
13160
13161 ----------------------
13162 -- Set_Discriminals --
13163 ----------------------
13164
13165 procedure Set_Discriminals (Dec : Node_Id) is
13166 D : Entity_Id;
13167 Pdef : Entity_Id;
13168 D_Minal : Entity_Id;
13169
13170 begin
13171 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
13172 Pdef := Defining_Identifier (Dec);
13173
13174 if Has_Discriminants (Pdef) then
13175 D := First_Discriminant (Pdef);
13176 while Present (D) loop
13177 D_Minal :=
13178 Make_Defining_Identifier (Sloc (D),
13179 Chars => New_External_Name (Chars (D), 'D'));
13180
13181 Set_Ekind (D_Minal, E_Constant);
13182 Set_Etype (D_Minal, Etype (D));
13183 Set_Scope (D_Minal, Pdef);
13184 Set_Discriminal (D, D_Minal);
13185 Set_Discriminal_Link (D_Minal, D);
13186
13187 Next_Discriminant (D);
13188 end loop;
13189 end if;
13190 end Set_Discriminals;
13191
13192 -----------------------
13193 -- Trivial_Accept_OK --
13194 -----------------------
13195
13196 function Trivial_Accept_OK return Boolean is
13197 begin
13198 case Opt.Task_Dispatching_Policy is
13199
13200 -- If we have the default task dispatching policy in effect, we can
13201 -- definitely do the optimization (one way of looking at this is to
13202 -- think of the formal definition of the default policy being allowed
13203 -- to run any task it likes after a rendezvous, so even if notionally
13204 -- a full rescheduling occurs, we can say that our dispatching policy
13205 -- (i.e. the default dispatching policy) reorders the queue to be the
13206 -- same as just before the call.
13207
13208 when ' ' =>
13209 return True;
13210
13211 -- FIFO_Within_Priorities certainly does not permit this
13212 -- optimization since the Rendezvous is a scheduling action that may
13213 -- require some other task to be run.
13214
13215 when 'F' =>
13216 return False;
13217
13218 -- For now, disallow the optimization for all other policies. This
13219 -- may be over-conservative, but it is certainly not incorrect.
13220
13221 when others =>
13222 return False;
13223
13224 end case;
13225 end Trivial_Accept_OK;
13226
13227 end Exp_Ch9;