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