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