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