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