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