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