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