[multiple changes]
[gcc.git] / gcc / ada / exp_ch9.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, 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 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id);
132 -- Build the body of a wrapper procedure for an entry or entry family that
133 -- has contract cases, preconditions, or postconditions. The body gathers
134 -- the executable contract items and expands them in the usual way, and
135 -- performs the entry call itself. This way preconditions are evaluated
136 -- before the call is queued. E is the entry in question, and Decl is the
137 -- enclosing synchronized type declaration at whose freeze point the
138 -- generated body is analyzed.
139
140 function Build_Corresponding_Record
141 (N : Node_Id;
142 Ctyp : Node_Id;
143 Loc : Source_Ptr) return Node_Id;
144 -- Common to tasks and protected types. Copy discriminant specifications,
145 -- build record declaration. N is the type declaration, Ctyp is the
146 -- concurrent entity (task type or protected type).
147
148 function Build_Dispatching_Tag_Check
149 (K : Entity_Id;
150 N : Node_Id) return Node_Id;
151 -- Utility to create the tree to check whether the dispatching call in
152 -- a timed entry call, a conditional entry call, or an asynchronous
153 -- transfer of control is a call to a primitive of a non-synchronized type.
154 -- K is the temporary that holds the tagged kind of the target object, and
155 -- N is the enclosing construct.
156
157 function Build_Entry_Count_Expression
158 (Concurrent_Type : Node_Id;
159 Component_List : List_Id;
160 Loc : Source_Ptr) return Node_Id;
161 -- Compute number of entries for concurrent object. This is a count of
162 -- simple entries, followed by an expression that computes the length
163 -- of the range of each entry family. A single array with that size is
164 -- allocated for each concurrent object of the type.
165
166 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
167 -- Build the function that translates the entry index in the call
168 -- (which depends on the size of entry families) into an index into the
169 -- Entry_Bodies_Array, to determine the body and barrier function used
170 -- in a protected entry call. A pointer to this function appears in every
171 -- protected object.
172
173 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
174 -- Build subprogram declaration for previous one
175
176 function Build_Lock_Free_Protected_Subprogram_Body
177 (N : Node_Id;
178 Prot_Typ : Node_Id;
179 Unprot_Spec : Node_Id) return Node_Id;
180 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
181 -- the subprogram specification of the unprotected version of N. Transform
182 -- N such that it invokes the unprotected version of the body.
183
184 function Build_Lock_Free_Unprotected_Subprogram_Body
185 (N : Node_Id;
186 Prot_Typ : Node_Id) return Node_Id;
187 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
188 -- of N where the original statements of N are synchronized through atomic
189 -- actions such as compare and exchange. Prior to invoking this routine, it
190 -- has been established that N can be implemented in a lock-free fashion.
191
192 function Build_Parameter_Block
193 (Loc : Source_Ptr;
194 Actuals : List_Id;
195 Formals : List_Id;
196 Decls : List_Id) return Entity_Id;
197 -- Generate an access type for each actual parameter in the list Actuals.
198 -- Create an encapsulating record that contains all the actuals and return
199 -- its type. Generate:
200 -- type Ann1 is access all <actual1-type>
201 -- ...
202 -- type AnnN is access all <actualN-type>
203 -- type Pnn is record
204 -- <formal1> : Ann1;
205 -- ...
206 -- <formalN> : AnnN;
207 -- end record;
208
209 function Build_Protected_Entry
210 (N : Node_Id;
211 Ent : Entity_Id;
212 Pid : Node_Id) return Node_Id;
213 -- Build the procedure implementing the statement sequence of the specified
214 -- entry body.
215
216 function Build_Protected_Entry_Specification
217 (Loc : Source_Ptr;
218 Def_Id : Entity_Id;
219 Ent_Id : Entity_Id) return Node_Id;
220 -- Build a specification for the procedure implementing the statements of
221 -- the specified entry body. Add attributes associating it with the entry
222 -- defining identifier Ent_Id.
223
224 function Build_Protected_Spec
225 (N : Node_Id;
226 Obj_Type : Entity_Id;
227 Ident : Entity_Id;
228 Unprotected : Boolean := False) return List_Id;
229 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
230 -- Subprogram_Type. Builds signature of protected subprogram, adding the
231 -- formal that corresponds to the object itself. For an access to protected
232 -- subprogram, there is no object type to specify, so the parameter has
233 -- type Address and mode In. An indirect call through such a pointer will
234 -- convert the address to a reference to the actual object. The object is
235 -- a limited record and therefore a by_reference type.
236
237 function Build_Protected_Subprogram_Body
238 (N : Node_Id;
239 Pid : Node_Id;
240 N_Op_Spec : Node_Id) return Node_Id;
241 -- This function is used to construct the protected version of a protected
242 -- subprogram. Its statement sequence first defers abort, then locks the
243 -- associated protected object, and then enters a block that contains a
244 -- call to the unprotected version of the subprogram (for details, see
245 -- Build_Unprotected_Subprogram_Body). This block statement requires a
246 -- cleanup handler that unlocks the object in all cases. For details,
247 -- see Exp_Ch7.Expand_Cleanup_Actions.
248
249 function Build_Renamed_Formal_Declaration
250 (New_F : Entity_Id;
251 Formal : Entity_Id;
252 Comp : Entity_Id;
253 Renamed_Formal : Node_Id) return Node_Id;
254 -- Create a renaming declaration for a formal, within a protected entry
255 -- body or an accept body. The renamed object is a component of the
256 -- parameter block that is a parameter in the entry call.
257 --
258 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
259 -- does not dereference the corresponding component to prevent an illegal
260 -- use of the incomplete type (AI05-0151).
261
262 function Build_Selected_Name
263 (Prefix : Entity_Id;
264 Selector : Entity_Id;
265 Append_Char : Character := ' ') return Name_Id;
266 -- Build a name in the form of Prefix__Selector, with an optional character
267 -- appended. This is used for internal subprograms generated for operations
268 -- of protected types, including barrier functions. For the subprograms
269 -- generated for entry bodies and entry barriers, the generated name
270 -- includes a sequence number that makes names unique in the presence of
271 -- entry overloading. This is necessary because entry body procedures and
272 -- barrier functions all have the same signature.
273
274 procedure Build_Simple_Entry_Call
275 (N : Node_Id;
276 Concval : Node_Id;
277 Ename : Node_Id;
278 Index : Node_Id);
279 -- Some comments here would be useful ???
280
281 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
282 -- This routine constructs a specification for the procedure that we will
283 -- build for the task body for task type T. The spec has the form:
284 --
285 -- procedure tnameB (_Task : access tnameV);
286 --
287 -- where name is the character name taken from the task type entity that
288 -- is passed as the argument to the procedure, and tnameV is the task
289 -- value type that is associated with the task type.
290
291 function Build_Unprotected_Subprogram_Body
292 (N : Node_Id;
293 Pid : Node_Id) return Node_Id;
294 -- This routine constructs the unprotected version of a protected
295 -- subprogram body, which is contains all of the code in the original,
296 -- unexpanded body. This is the version of the protected subprogram that is
297 -- called from all protected operations on the same object, including the
298 -- protected version of the same subprogram.
299
300 procedure Build_Wrapper_Bodies
301 (Loc : Source_Ptr;
302 Typ : Entity_Id;
303 N : Node_Id);
304 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
305 -- record of a concurrent type. N is the insertion node where all bodies
306 -- will be placed. This routine builds the bodies of the subprograms which
307 -- serve as an indirection mechanism to overriding primitives of concurrent
308 -- types, entries and protected procedures. Any new body is analyzed.
309
310 procedure Build_Wrapper_Specs
311 (Loc : Source_Ptr;
312 Typ : Entity_Id;
313 N : in out Node_Id);
314 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
315 -- record of a concurrent type. N is the insertion node where all specs
316 -- will be placed. This routine builds the specs of the subprograms which
317 -- serve as an indirection mechanism to overriding primitives of concurrent
318 -- types, entries and protected procedures. Any new spec is analyzed.
319
320 procedure Collect_Entry_Families
321 (Loc : Source_Ptr;
322 Cdecls : List_Id;
323 Current_Node : in out Node_Id;
324 Conctyp : Entity_Id);
325 -- For each entry family in a concurrent type, create an anonymous array
326 -- type of the right size, and add a component to the corresponding_record.
327
328 function Concurrent_Object
329 (Spec_Id : Entity_Id;
330 Conc_Typ : Entity_Id) return Entity_Id;
331 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
332 -- the entity associated with the concurrent object in the Protected_Body_
333 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
334 -- denotes formal parameter _O, _object or _task.
335
336 function Copy_Result_Type (Res : Node_Id) return Node_Id;
337 -- Copy the result type of a function specification, when building the
338 -- internal operation corresponding to a protected function, or when
339 -- expanding an access to protected function. If the result is an anonymous
340 -- access to subprogram itself, we need to create a new signature with the
341 -- same parameter names and the same resolved types, but with new entities
342 -- for the formals.
343
344 procedure Debug_Private_Data_Declarations (Decls : List_Id);
345 -- Decls is a list which may contain the declarations created by Install_
346 -- Private_Data_Declarations. All generated entities are marked as needing
347 -- debug info and debug nodes are manually generation where necessary. This
348 -- step of the expansion must to be done after private data has been moved
349 -- to its final resting scope to ensure proper visibility of debug objects.
350
351 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
352 -- If control flow optimizations are suppressed, and Alt is an accept,
353 -- delay, or entry call alternative with no trailing statements, insert
354 -- a null trailing statement with the given Loc (which is the sloc of
355 -- the accept, delay, or entry call statement). There might not be any
356 -- generated code for the accept, delay, or entry call itself (the effect
357 -- of these statements is part of the general processsing done for the
358 -- enclosing selective accept, timed entry call, or asynchronous select),
359 -- and the null statement is there to carry the sloc of that statement to
360 -- the back-end for trace-based coverage analysis purposes.
361
362 procedure Extract_Dispatching_Call
363 (N : Node_Id;
364 Call_Ent : out Entity_Id;
365 Object : out Entity_Id;
366 Actuals : out List_Id;
367 Formals : out List_Id);
368 -- Given a dispatching call, extract the entity of the name of the call,
369 -- its actual dispatching object, its actual parameters and the formal
370 -- parameters of the overridden interface-level version. If the type of
371 -- the dispatching object is an access type then an explicit dereference
372 -- is returned in Object.
373
374 procedure Extract_Entry
375 (N : Node_Id;
376 Concval : out Node_Id;
377 Ename : out Node_Id;
378 Index : out Node_Id);
379 -- Given an entry call, returns the associated concurrent object, the entry
380 -- name, and the entry family index.
381
382 function Family_Offset
383 (Loc : Source_Ptr;
384 Hi : Node_Id;
385 Lo : Node_Id;
386 Ttyp : Entity_Id;
387 Cap : Boolean) return Node_Id;
388 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
389 -- accept statement, or the upper bound in the discrete subtype of an entry
390 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
391 -- type of the entry. If Cap is true, the result is capped according to
392 -- Entry_Family_Bound.
393
394 function Family_Size
395 (Loc : Source_Ptr;
396 Hi : Node_Id;
397 Lo : Node_Id;
398 Ttyp : Entity_Id;
399 Cap : Boolean) return Node_Id;
400 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
401 -- family, and handle properly the superflat case. This is equivalent to
402 -- the use of 'Length on the index type, but must use Family_Offset to
403 -- handle properly the case of bounds that depend on discriminants. If
404 -- Cap is true, the result is capped according to Entry_Family_Bound.
405
406 procedure Find_Enclosing_Context
407 (N : Node_Id;
408 Context : out Node_Id;
409 Context_Id : out Entity_Id;
410 Context_Decls : out List_Id);
411 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
412 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
413 -- nearest enclosing body, block, package, or return statement and return
414 -- its constituents. Context is the enclosing construct, Context_Id is
415 -- the scope of Context_Id and Context_Decls is the declarative list of
416 -- Context.
417
418 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
419 -- Given a subprogram identifier, return the entity which is associated
420 -- with the protection entry index in the Protected_Body_Subprogram or
421 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
422 -- parameter _E.
423
424 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
425 -- Tell whether a given subprogram cannot raise an exception
426
427 function Is_Potentially_Large_Family
428 (Base_Index : Entity_Id;
429 Conctyp : Entity_Id;
430 Lo : Node_Id;
431 Hi : Node_Id) return Boolean;
432
433 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
434 -- Determine whether Id is a function or a procedure and is marked as a
435 -- private primitive.
436
437 function Null_Statements (Stats : List_Id) return Boolean;
438 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
439 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
440 -- to still count as null. Returns True for a null sequence. The argument
441 -- is the list of statements from the DO-END sequence.
442
443 function Parameter_Block_Pack
444 (Loc : Source_Ptr;
445 Blk_Typ : Entity_Id;
446 Actuals : List_Id;
447 Formals : List_Id;
448 Decls : List_Id;
449 Stmts : List_Id) return Entity_Id;
450 -- Set the components of the generated parameter block with the values
451 -- of the actual parameters. Generate aliased temporaries to capture the
452 -- values for types that are passed by copy. Otherwise generate a reference
453 -- to the actual's value. Return the address of the aggregate block.
454 -- Generate:
455 -- Jnn1 : alias <formal-type1>;
456 -- Jnn1 := <actual1>;
457 -- ...
458 -- P : Blk_Typ := (
459 -- Jnn1'unchecked_access;
460 -- <actual2>'reference;
461 -- ...);
462
463 function Parameter_Block_Unpack
464 (Loc : Source_Ptr;
465 P : Entity_Id;
466 Actuals : List_Id;
467 Formals : List_Id) return List_Id;
468 -- Retrieve the values of the components from the parameter block and
469 -- assign then to the original actual parameters. Generate:
470 -- <actual1> := P.<formal1>;
471 -- ...
472 -- <actualN> := P.<formalN>;
473
474 function Trivial_Accept_OK return Boolean;
475 -- If there is no DO-END block for an accept, or if the DO-END block has
476 -- only null statements, then it is possible to do the Rendezvous with much
477 -- less overhead using the Accept_Trivial routine in the run-time library.
478 -- However, this is not always a valid optimization. Whether it is valid or
479 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
480 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
481 -- a rescheduling is required, so this optimization is not allowed. This
482 -- function returns True if the optimization is permitted.
483
484 -----------------------------
485 -- Actual_Index_Expression --
486 -----------------------------
487
488 function Actual_Index_Expression
489 (Sloc : Source_Ptr;
490 Ent : Entity_Id;
491 Index : Node_Id;
492 Tsk : Entity_Id) return Node_Id
493 is
494 Ttyp : constant Entity_Id := Etype (Tsk);
495 Expr : Node_Id;
496 Num : Node_Id;
497 Lo : Node_Id;
498 Hi : Node_Id;
499 Prev : Entity_Id;
500 S : Node_Id;
501
502 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
503 -- Compute difference between bounds of entry family
504
505 --------------------------
506 -- Actual_Family_Offset --
507 --------------------------
508
509 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
510
511 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
512 -- Replace a reference to a discriminant with a selected component
513 -- denoting the discriminant of the target task.
514
515 -----------------------------
516 -- Actual_Discriminant_Ref --
517 -----------------------------
518
519 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
520 Typ : constant Entity_Id := Etype (Bound);
521 B : Node_Id;
522
523 begin
524 if not Is_Entity_Name (Bound)
525 or else Ekind (Entity (Bound)) /= E_Discriminant
526 then
527 if Nkind (Bound) = N_Attribute_Reference then
528 return Bound;
529 else
530 B := New_Copy_Tree (Bound);
531 end if;
532
533 else
534 B :=
535 Make_Selected_Component (Sloc,
536 Prefix => New_Copy_Tree (Tsk),
537 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
538
539 Analyze_And_Resolve (B, Typ);
540 end if;
541
542 return
543 Make_Attribute_Reference (Sloc,
544 Attribute_Name => Name_Pos,
545 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
546 Expressions => New_List (B));
547 end Actual_Discriminant_Ref;
548
549 -- Start of processing for Actual_Family_Offset
550
551 begin
552 return
553 Make_Op_Subtract (Sloc,
554 Left_Opnd => Actual_Discriminant_Ref (Hi),
555 Right_Opnd => Actual_Discriminant_Ref (Lo));
556 end Actual_Family_Offset;
557
558 -- Start of processing for Actual_Index_Expression
559
560 begin
561 -- The queues of entries and entry families appear in textual order in
562 -- the associated record. The entry index is computed as the sum of the
563 -- number of queues for all entries that precede the designated one, to
564 -- which is added the index expression, if this expression denotes a
565 -- member of a family.
566
567 -- The following is a place holder for the count of simple entries
568
569 Num := Make_Integer_Literal (Sloc, 1);
570
571 -- We construct an expression which is a series of addition operations.
572 -- See comments in Entry_Index_Expression, which is identical in
573 -- structure.
574
575 if Present (Index) then
576 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
577
578 Expr :=
579 Make_Op_Add (Sloc,
580 Left_Opnd => Num,
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 => Actual_Family_Offset (Hi, Lo),
662 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
663
664 -- Other components are anonymous types to be ignored
665
666 else
667 null;
668 end if;
669
670 Next_Entity (Prev);
671 end loop;
672
673 return Expr;
674 end Actual_Index_Expression;
675
676 --------------------------
677 -- Add_Formal_Renamings --
678 --------------------------
679
680 procedure Add_Formal_Renamings
681 (Spec : Node_Id;
682 Decls : List_Id;
683 Ent : Entity_Id;
684 Loc : Source_Ptr)
685 is
686 Ptr : constant Entity_Id :=
687 Defining_Identifier
688 (Next (First (Parameter_Specifications (Spec))));
689 -- The name of the formal that holds the address of the parameter block
690 -- for the call.
691
692 Comp : Entity_Id;
693 Decl : Node_Id;
694 Formal : Entity_Id;
695 New_F : Entity_Id;
696 Renamed_Formal : Node_Id;
697
698 begin
699 Formal := First_Formal (Ent);
700 while Present (Formal) loop
701 Comp := Entry_Component (Formal);
702 New_F :=
703 Make_Defining_Identifier (Sloc (Formal),
704 Chars => Chars (Formal));
705 Set_Etype (New_F, Etype (Formal));
706 Set_Scope (New_F, Ent);
707
708 -- Now we set debug info needed on New_F even though it does not come
709 -- from source, so that the debugger will get the right information
710 -- for these generated names.
711
712 Set_Debug_Info_Needed (New_F);
713
714 if Ekind (Formal) = E_In_Parameter then
715 Set_Ekind (New_F, E_Constant);
716 else
717 Set_Ekind (New_F, E_Variable);
718 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
719 end if;
720
721 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
722
723 Renamed_Formal :=
724 Make_Selected_Component (Loc,
725 Prefix =>
726 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
727 Make_Identifier (Loc, Chars (Ptr))),
728 Selector_Name => New_Occurrence_Of (Comp, Loc));
729
730 Decl :=
731 Build_Renamed_Formal_Declaration
732 (New_F, Formal, Comp, Renamed_Formal);
733
734 Append (Decl, Decls);
735 Set_Renamed_Object (Formal, New_F);
736 Next_Formal (Formal);
737 end loop;
738 end Add_Formal_Renamings;
739
740 ------------------------
741 -- Add_Object_Pointer --
742 ------------------------
743
744 procedure Add_Object_Pointer
745 (Loc : Source_Ptr;
746 Conc_Typ : Entity_Id;
747 Decls : List_Id)
748 is
749 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
750 Decl : Node_Id;
751 Obj_Ptr : Node_Id;
752
753 begin
754 -- Create the renaming declaration for the Protection object of a
755 -- protected type. _Object is used by Complete_Entry_Body.
756 -- ??? An attempt to make this a renaming was unsuccessful.
757
758 -- Build the entity for the access type
759
760 Obj_Ptr :=
761 Make_Defining_Identifier (Loc,
762 New_External_Name (Chars (Rec_Typ), 'P'));
763
764 -- Generate:
765 -- _object : poVP := poVP!O;
766
767 Decl :=
768 Make_Object_Declaration (Loc,
769 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
770 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
771 Expression =>
772 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
773 Set_Debug_Info_Needed (Defining_Identifier (Decl));
774 Prepend_To (Decls, Decl);
775
776 -- Generate:
777 -- type poVP is access poV;
778
779 Decl :=
780 Make_Full_Type_Declaration (Loc,
781 Defining_Identifier =>
782 Obj_Ptr,
783 Type_Definition =>
784 Make_Access_To_Object_Definition (Loc,
785 Subtype_Indication =>
786 New_Occurrence_Of (Rec_Typ, Loc)));
787 Set_Debug_Info_Needed (Defining_Identifier (Decl));
788 Prepend_To (Decls, Decl);
789 end Add_Object_Pointer;
790
791 -----------------------
792 -- Build_Accept_Body --
793 -----------------------
794
795 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
796 Loc : constant Source_Ptr := Sloc (Astat);
797 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
798 New_S : Node_Id;
799 Hand : Node_Id;
800 Call : Node_Id;
801 Ohandle : Node_Id;
802
803 begin
804 -- At the end of the statement sequence, Complete_Rendezvous is called.
805 -- A label skipping the Complete_Rendezvous, and all other accept
806 -- processing, has already been added for the expansion of requeue
807 -- statements. The Sloc is copied from the last statement since it
808 -- is really part of this last statement.
809
810 Call :=
811 Build_Runtime_Call
812 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
813 Insert_Before (Last (Statements (Stats)), Call);
814 Analyze (Call);
815
816 -- If exception handlers are present, then append Complete_Rendezvous
817 -- calls to the handlers, and construct the required outer block. As
818 -- above, the Sloc is copied from the last statement in the sequence.
819
820 if Present (Exception_Handlers (Stats)) then
821 Hand := First (Exception_Handlers (Stats));
822 while Present (Hand) loop
823 Call :=
824 Build_Runtime_Call
825 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
826 Append (Call, Statements (Hand));
827 Analyze (Call);
828 Next (Hand);
829 end loop;
830
831 New_S :=
832 Make_Handled_Sequence_Of_Statements (Loc,
833 Statements => New_List (
834 Make_Block_Statement (Loc,
835 Handled_Statement_Sequence => Stats)));
836
837 else
838 New_S := Stats;
839 end if;
840
841 -- At this stage we know that the new statement sequence does
842 -- not have an exception handler part, so we supply one to call
843 -- Exceptional_Complete_Rendezvous. This handler is
844
845 -- when all others =>
846 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
847
848 -- We handle Abort_Signal to make sure that we properly catch the abort
849 -- case and wake up the caller.
850
851 Ohandle := Make_Others_Choice (Loc);
852 Set_All_Others (Ohandle);
853
854 Set_Exception_Handlers (New_S,
855 New_List (
856 Make_Implicit_Exception_Handler (Loc,
857 Exception_Choices => New_List (Ohandle),
858
859 Statements => New_List (
860 Make_Procedure_Call_Statement (Sloc (Stats),
861 Name => New_Occurrence_Of (
862 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
863 Parameter_Associations => New_List (
864 Make_Function_Call (Sloc (Stats),
865 Name =>
866 New_Occurrence_Of
867 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
868
869 Set_Parent (New_S, Astat); -- temp parent for Analyze call
870 Analyze_Exception_Handlers (Exception_Handlers (New_S));
871 Expand_Exception_Handlers (New_S);
872
873 -- Exceptional_Complete_Rendezvous must be called with abort still
874 -- deferred, which is the case for a "when all others" handler.
875
876 return New_S;
877 end Build_Accept_Body;
878
879 -----------------------------------
880 -- Build_Activation_Chain_Entity --
881 -----------------------------------
882
883 procedure Build_Activation_Chain_Entity (N : Node_Id) is
884 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
885 -- Determine whether an extended return statement has activation chain
886
887 --------------------------
888 -- Has_Activation_Chain --
889 --------------------------
890
891 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
892 Decl : Node_Id;
893
894 begin
895 Decl := First (Return_Object_Declarations (Stmt));
896 while Present (Decl) loop
897 if Nkind (Decl) = N_Object_Declaration
898 and then Chars (Defining_Identifier (Decl)) = Name_uChain
899 then
900 return True;
901 end if;
902
903 Next (Decl);
904 end loop;
905
906 return False;
907 end Has_Activation_Chain;
908
909 -- Local variables
910
911 Context : Node_Id;
912 Context_Id : Entity_Id;
913 Decls : List_Id;
914
915 -- Start of processing for Build_Activation_Chain_Entity
916
917 begin
918 -- Activation chain is never used for sequential elaboration policy, see
919 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
920
921 if Partition_Elaboration_Policy = 'S' then
922 return;
923 end if;
924
925 Find_Enclosing_Context (N, Context, Context_Id, Decls);
926
927 -- If activation chain entity has not been declared already, create one
928
929 if Nkind (Context) = N_Extended_Return_Statement
930 or else No (Activation_Chain_Entity (Context))
931 then
932 -- Since extended return statements do not store the entity of the
933 -- chain, examine the return object declarations to avoid creating
934 -- a duplicate.
935
936 if Nkind (Context) = N_Extended_Return_Statement
937 and then Has_Activation_Chain (Context)
938 then
939 return;
940 end if;
941
942 declare
943 Loc : constant Source_Ptr := Sloc (Context);
944 Chain : Entity_Id;
945 Decl : Node_Id;
946
947 begin
948 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
949
950 -- Note: An extended return statement is not really a task
951 -- activator, but it does have an activation chain on which to
952 -- store the tasks temporarily. On successful return, the tasks
953 -- on this chain are moved to the chain passed in by the caller.
954 -- We do not build an Activation_Chain_Entity for an extended
955 -- return statement, because we do not want to build a call to
956 -- Activate_Tasks. Task activation is the responsibility of the
957 -- caller.
958
959 if Nkind (Context) /= N_Extended_Return_Statement then
960 Set_Activation_Chain_Entity (Context, Chain);
961 end if;
962
963 Decl :=
964 Make_Object_Declaration (Loc,
965 Defining_Identifier => Chain,
966 Aliased_Present => True,
967 Object_Definition =>
968 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
969
970 Prepend_To (Decls, Decl);
971
972 -- Ensure that _chain appears in the proper scope of the context
973
974 if Context_Id /= Current_Scope then
975 Push_Scope (Context_Id);
976 Analyze (Decl);
977 Pop_Scope;
978 else
979 Analyze (Decl);
980 end if;
981 end;
982 end if;
983 end Build_Activation_Chain_Entity;
984
985 ----------------------------
986 -- Build_Barrier_Function --
987 ----------------------------
988
989 function Build_Barrier_Function
990 (N : Node_Id;
991 Ent : Entity_Id;
992 Pid : Node_Id) return Node_Id
993 is
994 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
995 Cond : constant Node_Id := Condition (Ent_Formals);
996 Loc : constant Source_Ptr := Sloc (Cond);
997 Func_Id : constant Entity_Id := Barrier_Function (Ent);
998 Op_Decls : constant List_Id := New_List;
999 Stmt : Node_Id;
1000 Func_Body : Node_Id;
1001
1002 begin
1003 -- Add a declaration for the Protection object, renaming declarations
1004 -- for the discriminals and privals and finally a declaration for the
1005 -- entry family index (if applicable).
1006
1007 Install_Private_Data_Declarations (Sloc (N),
1008 Spec_Id => Func_Id,
1009 Conc_Typ => Pid,
1010 Body_Nod => N,
1011 Decls => Op_Decls,
1012 Barrier => True,
1013 Family => Ekind (Ent) = E_Entry_Family);
1014
1015 -- If compiling with -fpreserve-control-flow, make sure we insert an
1016 -- IF statement so that the back-end knows to generate a conditional
1017 -- branch instruction, even if the condition is just the name of a
1018 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1019 -- such redundant IF statements under -fpreserve-control-flow
1020 -- (whether coming from this routine, or directly from source).
1021
1022 if Opt.Suppress_Control_Flow_Optimizations then
1023 Stmt :=
1024 Make_Implicit_If_Statement (Cond,
1025 Condition => Cond,
1026 Then_Statements => New_List (
1027 Make_Simple_Return_Statement (Loc,
1028 New_Occurrence_Of (Standard_True, Loc))),
1029
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
1068 Make_Function_Specification (Loc,
1069 Defining_Unit_Name => Def_Id,
1070 Parameter_Specifications => New_List (
1071 Make_Parameter_Specification (Loc,
1072 Defining_Identifier =>
1073 Make_Defining_Identifier (Loc, Name_uO),
1074 Parameter_Type =>
1075 New_Occurrence_Of (RTE (RE_Address), Loc)),
1076
1077 Make_Parameter_Specification (Loc,
1078 Defining_Identifier =>
1079 Make_Defining_Identifier (Loc, Name_uE),
1080 Parameter_Type =>
1081 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1082
1083 Result_Definition =>
1084 New_Occurrence_Of (Standard_Boolean, Loc));
1085 end Build_Barrier_Function_Specification;
1086
1087 --------------------------
1088 -- Build_Call_With_Task --
1089 --------------------------
1090
1091 function Build_Call_With_Task
1092 (N : Node_Id;
1093 E : Entity_Id) return Node_Id
1094 is
1095 Loc : constant Source_Ptr := Sloc (N);
1096 begin
1097 return
1098 Make_Function_Call (Loc,
1099 Name => New_Occurrence_Of (E, Loc),
1100 Parameter_Associations => New_List (Concurrent_Ref (N)));
1101 end Build_Call_With_Task;
1102
1103 -----------------------------
1104 -- Build_Class_Wide_Master --
1105 -----------------------------
1106
1107 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1108 Loc : constant Source_Ptr := Sloc (Typ);
1109 Master_Id : Entity_Id;
1110 Master_Scope : Entity_Id;
1111 Name_Id : Node_Id;
1112 Related_Node : Node_Id;
1113 Ren_Decl : Node_Id;
1114
1115 begin
1116 -- Nothing to do if there is no task hierarchy
1117
1118 if Restriction_Active (No_Task_Hierarchy) then
1119 return;
1120 end if;
1121
1122 -- Find the declaration that created the access type, which is either a
1123 -- type declaration, or an object declaration with an access definition,
1124 -- in which case the type is anonymous.
1125
1126 if Is_Itype (Typ) then
1127 Related_Node := Associated_Node_For_Itype (Typ);
1128 else
1129 Related_Node := Parent (Typ);
1130 end if;
1131
1132 Master_Scope := Find_Master_Scope (Typ);
1133
1134 -- Nothing to do if the master scope already contains a _master entity.
1135 -- The only exception to this is the following scenario:
1136
1137 -- Source_Scope
1138 -- Transient_Scope_1
1139 -- _master
1140
1141 -- Transient_Scope_2
1142 -- use of master
1143
1144 -- In this case the source scope is marked as having the master entity
1145 -- even though the actual declaration appears inside an inner scope. If
1146 -- the second transient scope requires a _master, it cannot use the one
1147 -- already declared because the entity is not visible.
1148
1149 Name_Id := Make_Identifier (Loc, Name_uMaster);
1150
1151 if not Has_Master_Entity (Master_Scope)
1152 or else No (Current_Entity_In_Scope (Name_Id))
1153 then
1154 declare
1155 Master_Decl : Node_Id;
1156 begin
1157 Set_Has_Master_Entity (Master_Scope);
1158
1159 -- Generate:
1160 -- _master : constant Integer := Current_Master.all;
1161
1162 Master_Decl :=
1163 Make_Object_Declaration (Loc,
1164 Defining_Identifier =>
1165 Make_Defining_Identifier (Loc, Name_uMaster),
1166 Constant_Present => True,
1167 Object_Definition =>
1168 New_Occurrence_Of (Standard_Integer, Loc),
1169 Expression =>
1170 Make_Explicit_Dereference (Loc,
1171 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1172
1173 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1174 Analyze (Master_Decl);
1175
1176 -- Mark the containing scope as a task master. Masters associated
1177 -- with return statements are already marked at this stage (see
1178 -- Analyze_Subprogram_Body).
1179
1180 if Ekind (Current_Scope) /= E_Return_Statement then
1181 declare
1182 Par : Node_Id := Related_Node;
1183
1184 begin
1185 while Nkind (Par) /= N_Compilation_Unit loop
1186 Par := Parent (Par);
1187
1188 -- If we fall off the top, we are at the outer level,
1189 -- and the environment task is our effective master,
1190 -- so nothing to mark.
1191
1192 if Nkind_In (Par, N_Block_Statement,
1193 N_Subprogram_Body,
1194 N_Task_Body)
1195 then
1196 Set_Is_Task_Master (Par);
1197 exit;
1198 end if;
1199 end loop;
1200 end;
1201 end if;
1202 end;
1203 end if;
1204
1205 Master_Id :=
1206 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1207
1208 -- Generate:
1209 -- typeMnn renames _master;
1210
1211 Ren_Decl :=
1212 Make_Object_Renaming_Declaration (Loc,
1213 Defining_Identifier => Master_Id,
1214 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1215 Name => Name_Id);
1216
1217 Insert_Action (Related_Node, Ren_Decl);
1218
1219 Set_Master_Id (Typ, Master_Id);
1220 end Build_Class_Wide_Master;
1221
1222 ----------------------------
1223 -- Build_Contract_Wrapper --
1224 ----------------------------
1225
1226 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1227 Conc_Typ : constant Entity_Id := Scope (E);
1228 Loc : constant Source_Ptr := Sloc (E);
1229
1230 procedure Add_Discriminant_Renamings
1231 (Obj_Id : Entity_Id;
1232 Decls : List_Id);
1233 -- Add renaming declarations for all discriminants of concurrent type
1234 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1235 -- represents the concurrent object.
1236
1237 procedure Add_Matching_Formals
1238 (Formals : List_Id;
1239 Actuals : in out List_Id);
1240 -- Add formal parameters that match those of entry E to list Formals.
1241 -- The routine also adds matching actuals for the new formals to list
1242 -- Actuals.
1243
1244 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1245 -- Relocate pragma Prag to list To. The routine creates a new list if
1246 -- To does not exist.
1247
1248 --------------------------------
1249 -- Add_Discriminant_Renamings --
1250 --------------------------------
1251
1252 procedure Add_Discriminant_Renamings
1253 (Obj_Id : Entity_Id;
1254 Decls : List_Id)
1255 is
1256 Discr : Entity_Id;
1257
1258 begin
1259 -- Inspect the discriminants of the concurrent type and generate a
1260 -- renaming for each one.
1261
1262 if Has_Discriminants (Conc_Typ) then
1263 Discr := First_Discriminant (Conc_Typ);
1264 while Present (Discr) loop
1265 Prepend_To (Decls,
1266 Make_Object_Renaming_Declaration (Loc,
1267 Defining_Identifier =>
1268 Make_Defining_Identifier (Loc, Chars (Discr)),
1269 Subtype_Mark =>
1270 New_Occurrence_Of (Etype (Discr), Loc),
1271 Name =>
1272 Make_Selected_Component (Loc,
1273 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1274 Selector_Name =>
1275 Make_Identifier (Loc, Chars (Discr)))));
1276
1277 Next_Discriminant (Discr);
1278 end loop;
1279 end if;
1280 end Add_Discriminant_Renamings;
1281
1282 --------------------------
1283 -- Add_Matching_Formals --
1284 --------------------------
1285
1286 procedure Add_Matching_Formals
1287 (Formals : List_Id;
1288 Actuals : in out List_Id)
1289 is
1290 Formal : Entity_Id;
1291 New_Formal : Entity_Id;
1292
1293 begin
1294 -- Inspect the formal parameters of the entry and generate a new
1295 -- matching formal with the same name for the wrapper. A reference
1296 -- to the new formal becomes an actual in the entry call.
1297
1298 Formal := First_Formal (E);
1299 while Present (Formal) loop
1300 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1301 Append_To (Formals,
1302 Make_Parameter_Specification (Loc,
1303 Defining_Identifier => New_Formal,
1304 In_Present => In_Present (Parent (Formal)),
1305 Out_Present => Out_Present (Parent (Formal)),
1306 Parameter_Type =>
1307 New_Occurrence_Of (Etype (Formal), Loc)));
1308
1309 if No (Actuals) then
1310 Actuals := New_List;
1311 end if;
1312
1313 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1314 Next_Formal (Formal);
1315 end loop;
1316 end Add_Matching_Formals;
1317
1318 ---------------------
1319 -- Transfer_Pragma --
1320 ---------------------
1321
1322 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1323 New_Prag : Node_Id;
1324
1325 begin
1326 if No (To) then
1327 To := New_List;
1328 end if;
1329
1330 New_Prag := Relocate_Node (Prag);
1331
1332 Set_Analyzed (New_Prag, False);
1333 Append (New_Prag, To);
1334 end Transfer_Pragma;
1335
1336 -- Local variables
1337
1338 Items : constant Node_Id := Contract (E);
1339 Actuals : List_Id := No_List;
1340 Call : Node_Id;
1341 Call_Nam : Node_Id;
1342 Decls : List_Id := No_List;
1343 Formals : List_Id;
1344 Has_Pragma : Boolean := False;
1345 Index_Id : Entity_Id;
1346 Obj_Id : Entity_Id;
1347 Prag : Node_Id;
1348 Wrapper_Id : Entity_Id;
1349
1350 -- Start of processing for Build_Contract_Wrapper
1351
1352 begin
1353 -- This routine generates a specialized wrapper for a protected or task
1354 -- entry [family] which implements precondition/postcondition semantics.
1355 -- Preconditions and case guards of contract cases are checked before
1356 -- the protected action or rendezvous takes place. Postconditions and
1357 -- consequences of contract cases are checked after the protected action
1358 -- or rendezvous takes place. The structure of the generated wrapper is
1359 -- as follows:
1360
1361 -- procedure Wrapper
1362 -- (Obj_Id : Conc_Typ; -- concurrent object
1363 -- [Index : Index_Typ;] -- index of entry family
1364 -- [Formal_1 : ...; -- parameters of original entry
1365 -- Formal_N : ...])
1366 -- is
1367 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1368 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1369
1370 -- <precondition checks>
1371 -- <case guard checks>
1372
1373 -- procedure _Postconditions is
1374 -- begin
1375 -- <postcondition checks>
1376 -- <consequence checks>
1377 -- end _Postconditions;
1378
1379 -- begin
1380 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1381 -- _Postconditions;
1382 -- end Wrapper;
1383
1384 -- Create the wrapper only when the entry has at least one executable
1385 -- contract item such as contract cases, precondition or postcondition.
1386
1387 if Present (Items) then
1388
1389 -- Inspect the list of pre/postconditions and transfer all available
1390 -- pragmas to the declarative list of the wrapper.
1391
1392 Prag := Pre_Post_Conditions (Items);
1393 while Present (Prag) loop
1394 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
1395 Name_Precondition)
1396 and then Is_Checked (Prag)
1397 then
1398 Has_Pragma := True;
1399 Transfer_Pragma (Prag, To => Decls);
1400 end if;
1401
1402 Prag := Next_Pragma (Prag);
1403 end loop;
1404
1405 -- Inspect the list of test/contract cases and transfer only contract
1406 -- cases pragmas to the declarative part of the wrapper.
1407
1408 Prag := Contract_Test_Cases (Items);
1409 while Present (Prag) loop
1410 if Pragma_Name (Prag) = Name_Contract_Cases
1411 and then Is_Checked (Prag)
1412 then
1413 Has_Pragma := True;
1414 Transfer_Pragma (Prag, To => Decls);
1415 end if;
1416
1417 Prag := Next_Pragma (Prag);
1418 end loop;
1419 end if;
1420
1421 -- The entry lacks executable contract items and a wrapper is not needed
1422
1423 if not Has_Pragma then
1424 return;
1425 end if;
1426
1427 -- Create the profile of the wrapper. The first formal parameter is the
1428 -- concurrent object.
1429
1430 Obj_Id :=
1431 Make_Defining_Identifier (Loc,
1432 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1433
1434 Formals := New_List (
1435 Make_Parameter_Specification (Loc,
1436 Defining_Identifier => Obj_Id,
1437 Out_Present => True,
1438 In_Present => True,
1439 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1440
1441 -- Construct the call to the original entry. The call will be gradually
1442 -- augmented with an optional entry index and extra parameters.
1443
1444 Call_Nam :=
1445 Make_Selected_Component (Loc,
1446 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1447 Selector_Name => New_Occurrence_Of (E, Loc));
1448
1449 -- When creating a wrapper for an entry family, the second formal is the
1450 -- entry index.
1451
1452 if Ekind (E) = E_Entry_Family then
1453 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1454
1455 Append_To (Formals,
1456 Make_Parameter_Specification (Loc,
1457 Defining_Identifier => Index_Id,
1458 Parameter_Type =>
1459 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1460
1461 -- The call to the original entry becomes an indexed component to
1462 -- accommodate the entry index.
1463
1464 Call_Nam :=
1465 Make_Indexed_Component (Loc,
1466 Prefix => Call_Nam,
1467 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1468 end if;
1469
1470 -- Add formal parameters to match those of the entry and build actuals
1471 -- for the entry call.
1472
1473 Add_Matching_Formals (Formals, Actuals);
1474
1475 Call :=
1476 Make_Procedure_Call_Statement (Loc,
1477 Name => Call_Nam,
1478 Parameter_Associations => Actuals);
1479
1480 -- Add renaming declarations for the discriminants of the enclosing type
1481 -- as the various contract items may reference them.
1482
1483 Add_Discriminant_Renamings (Obj_Id, Decls);
1484
1485 Wrapper_Id :=
1486 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1487 Set_Contract_Wrapper (E, Wrapper_Id);
1488
1489 -- The wrapper body is analyzed when the enclosing type is frozen
1490
1491 Append_Freeze_Action (Defining_Entity (Decl),
1492 Make_Subprogram_Body (Loc,
1493 Specification =>
1494 Make_Procedure_Specification (Loc,
1495 Defining_Unit_Name => Wrapper_Id,
1496 Parameter_Specifications => Formals),
1497 Declarations => Decls,
1498 Handled_Statement_Sequence =>
1499 Make_Handled_Sequence_Of_Statements (Loc,
1500 Statements => New_List (Call))));
1501 end Build_Contract_Wrapper;
1502
1503 --------------------------------
1504 -- Build_Corresponding_Record --
1505 --------------------------------
1506
1507 function Build_Corresponding_Record
1508 (N : Node_Id;
1509 Ctyp : Entity_Id;
1510 Loc : Source_Ptr) return Node_Id
1511 is
1512 Rec_Ent : constant Entity_Id :=
1513 Make_Defining_Identifier
1514 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1515 Disc : Entity_Id;
1516 Dlist : List_Id;
1517 New_Disc : Entity_Id;
1518 Cdecls : List_Id;
1519
1520 begin
1521 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1522 Set_Ekind (Rec_Ent, E_Record_Type);
1523 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1524 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1525 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1526 Set_Stored_Constraint (Rec_Ent, No_Elist);
1527 Cdecls := New_List;
1528
1529 -- Propagate type invariants to the corresponding record type
1530
1531 Set_Has_Invariants (Rec_Ent, Has_Invariants (Ctyp));
1532 Set_Has_Inheritable_Invariants (Rec_Ent,
1533 Has_Inheritable_Invariants (Ctyp));
1534
1535 -- Use discriminals to create list of discriminants for record, and
1536 -- create new discriminals for use in default expressions, etc. It is
1537 -- worth noting that a task discriminant gives rise to 5 entities;
1538
1539 -- a) The original discriminant.
1540 -- b) The discriminal for use in the task.
1541 -- c) The discriminant of the corresponding record.
1542 -- d) The discriminal for the init proc of the corresponding record.
1543 -- e) The local variable that renames the discriminant in the procedure
1544 -- for the task body.
1545
1546 -- In fact the discriminals b) are used in the renaming declarations
1547 -- for e). See details in einfo (Handling of Discriminants).
1548
1549 if Present (Discriminant_Specifications (N)) then
1550 Dlist := New_List;
1551 Disc := First_Discriminant (Ctyp);
1552
1553 while Present (Disc) loop
1554 New_Disc := CR_Discriminant (Disc);
1555
1556 Append_To (Dlist,
1557 Make_Discriminant_Specification (Loc,
1558 Defining_Identifier => New_Disc,
1559 Discriminant_Type =>
1560 New_Occurrence_Of (Etype (Disc), Loc),
1561 Expression =>
1562 New_Copy (Discriminant_Default_Value (Disc))));
1563
1564 Next_Discriminant (Disc);
1565 end loop;
1566
1567 else
1568 Dlist := No_List;
1569 end if;
1570
1571 -- Now we can construct the record type declaration. Note that this
1572 -- record is "limited tagged". It is "limited" to reflect the underlying
1573 -- limitedness of the task or protected object that it represents, and
1574 -- ensuring for example that it is properly passed by reference. It is
1575 -- "tagged" to give support to dispatching calls through interfaces. We
1576 -- propagate here the list of interfaces covered by the concurrent type
1577 -- (Ada 2005: AI-345).
1578
1579 return
1580 Make_Full_Type_Declaration (Loc,
1581 Defining_Identifier => Rec_Ent,
1582 Discriminant_Specifications => Dlist,
1583 Type_Definition =>
1584 Make_Record_Definition (Loc,
1585 Component_List =>
1586 Make_Component_List (Loc, Component_Items => Cdecls),
1587 Tagged_Present =>
1588 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1589 Interface_List => Interface_List (N),
1590 Limited_Present => True));
1591 end Build_Corresponding_Record;
1592
1593 ---------------------------------
1594 -- Build_Dispatching_Tag_Check --
1595 ---------------------------------
1596
1597 function Build_Dispatching_Tag_Check
1598 (K : Entity_Id;
1599 N : Node_Id) return Node_Id
1600 is
1601 Loc : constant Source_Ptr := Sloc (N);
1602
1603 begin
1604 return
1605 Make_Op_Or (Loc,
1606 Make_Op_Eq (Loc,
1607 Left_Opnd =>
1608 New_Occurrence_Of (K, Loc),
1609 Right_Opnd =>
1610 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1611
1612 Make_Op_Eq (Loc,
1613 Left_Opnd =>
1614 New_Occurrence_Of (K, Loc),
1615 Right_Opnd =>
1616 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1617 end Build_Dispatching_Tag_Check;
1618
1619 ----------------------------------
1620 -- Build_Entry_Count_Expression --
1621 ----------------------------------
1622
1623 function Build_Entry_Count_Expression
1624 (Concurrent_Type : Node_Id;
1625 Component_List : List_Id;
1626 Loc : Source_Ptr) return Node_Id
1627 is
1628 Eindx : Nat;
1629 Ent : Entity_Id;
1630 Ecount : Node_Id;
1631 Comp : Node_Id;
1632 Lo : Node_Id;
1633 Hi : Node_Id;
1634 Typ : Entity_Id;
1635 Large : Boolean;
1636
1637 begin
1638 -- Count number of non-family entries
1639
1640 Eindx := 0;
1641 Ent := First_Entity (Concurrent_Type);
1642 while Present (Ent) loop
1643 if Ekind (Ent) = E_Entry then
1644 Eindx := Eindx + 1;
1645 end if;
1646
1647 Next_Entity (Ent);
1648 end loop;
1649
1650 Ecount := Make_Integer_Literal (Loc, Eindx);
1651
1652 -- Loop through entry families building the addition nodes
1653
1654 Ent := First_Entity (Concurrent_Type);
1655 Comp := First (Component_List);
1656 while Present (Ent) loop
1657 if Ekind (Ent) = E_Entry_Family then
1658 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1659 Next (Comp);
1660 end loop;
1661
1662 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1663 Hi := Type_High_Bound (Typ);
1664 Lo := Type_Low_Bound (Typ);
1665 Large := Is_Potentially_Large_Family
1666 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1667 Ecount :=
1668 Make_Op_Add (Loc,
1669 Left_Opnd => Ecount,
1670 Right_Opnd =>
1671 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1672 end if;
1673
1674 Next_Entity (Ent);
1675 end loop;
1676
1677 return Ecount;
1678 end Build_Entry_Count_Expression;
1679
1680 -----------------------
1681 -- Build_Entry_Names --
1682 -----------------------
1683
1684 procedure Build_Entry_Names
1685 (Obj_Ref : Node_Id;
1686 Obj_Typ : Entity_Id;
1687 Stmts : List_Id)
1688 is
1689 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1690 Data : Entity_Id := Empty;
1691 Index : Entity_Id := Empty;
1692 Typ : Entity_Id := Obj_Typ;
1693
1694 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1695 -- Given an entry [family], create a static string which denotes the
1696 -- name of Comp_Id and assign it to the underlying data structure which
1697 -- contains the entry names of a concurrent object.
1698
1699 function Object_Reference return Node_Id;
1700 -- Return a reference to field _object or _task_id depending on the
1701 -- concurrent object being processed.
1702
1703 ----------------------
1704 -- Build_Entry_Name --
1705 ----------------------
1706
1707 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1708 function Build_Range (Def : Node_Id) return Node_Id;
1709 -- Given a discrete subtype definition of an entry family, generate a
1710 -- range node which covers the range of Def's type.
1711
1712 procedure Create_Index_And_Data;
1713 -- Generate the declarations of variables Index and Data. Subsequent
1714 -- calls do nothing.
1715
1716 function Increment_Index return Node_Id;
1717 -- Increment the index used in the assignment of string names to the
1718 -- Data array.
1719
1720 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1721 -- Given the name of a temporary variable, create the following
1722 -- declaration for it:
1723 --
1724 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1725
1726 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1727 -- Given the name of a temporary variable, place it in the array of
1728 -- string names. Generate:
1729 --
1730 -- Data (Index) := Def_Id'Unchecked_Access;
1731
1732 -----------------
1733 -- Build_Range --
1734 -----------------
1735
1736 function Build_Range (Def : Node_Id) return Node_Id is
1737 High : Node_Id := Type_High_Bound (Etype (Def));
1738 Low : Node_Id := Type_Low_Bound (Etype (Def));
1739
1740 begin
1741 -- If a bound references a discriminant, generate an identifier
1742 -- with the same name. Resolution will map it to the formals of
1743 -- the init proc.
1744
1745 if Is_Entity_Name (Low)
1746 and then Ekind (Entity (Low)) = E_Discriminant
1747 then
1748 Low :=
1749 Make_Selected_Component (Loc,
1750 Prefix => New_Copy_Tree (Obj_Ref),
1751 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1752 else
1753 Low := New_Copy_Tree (Low);
1754 end if;
1755
1756 if Is_Entity_Name (High)
1757 and then Ekind (Entity (High)) = E_Discriminant
1758 then
1759 High :=
1760 Make_Selected_Component (Loc,
1761 Prefix => New_Copy_Tree (Obj_Ref),
1762 Selector_Name => Make_Identifier (Loc, Chars (High)));
1763 else
1764 High := New_Copy_Tree (High);
1765 end if;
1766
1767 return
1768 Make_Range (Loc,
1769 Low_Bound => Low,
1770 High_Bound => High);
1771 end Build_Range;
1772
1773 ---------------------------
1774 -- Create_Index_And_Data --
1775 ---------------------------
1776
1777 procedure Create_Index_And_Data is
1778 begin
1779 if No (Index) and then No (Data) then
1780 declare
1781 Count : RE_Id;
1782 Data_Typ : RE_Id;
1783 Size : Entity_Id;
1784
1785 begin
1786 if Is_Protected_Type (Typ) then
1787 Count := RO_PE_Number_Of_Entries;
1788 Data_Typ := RE_Protected_Entry_Names_Array;
1789 else
1790 Count := RO_ST_Number_Of_Entries;
1791 Data_Typ := RE_Task_Entry_Names_Array;
1792 end if;
1793
1794 -- Step 1: Generate the declaration of the index variable:
1795
1796 -- Index : Entry_Index := 1;
1797
1798 Index := Make_Temporary (Loc, 'I');
1799
1800 Append_To (Stmts,
1801 Make_Object_Declaration (Loc,
1802 Defining_Identifier => Index,
1803 Object_Definition =>
1804 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1805 Expression => Make_Integer_Literal (Loc, 1)));
1806
1807 -- Step 2: Generate the declaration of an array to house all
1808 -- names:
1809
1810 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1811 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1812
1813 Size := Make_Temporary (Loc, 'S');
1814
1815 Append_To (Stmts,
1816 Make_Object_Declaration (Loc,
1817 Defining_Identifier => Size,
1818 Constant_Present => True,
1819 Object_Definition =>
1820 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1821 Expression =>
1822 Make_Function_Call (Loc,
1823 Name =>
1824 New_Occurrence_Of (RTE (Count), Loc),
1825 Parameter_Associations =>
1826 New_List (Object_Reference))));
1827
1828 Data := Make_Temporary (Loc, 'A');
1829
1830 Append_To (Stmts,
1831 Make_Object_Declaration (Loc,
1832 Defining_Identifier => Data,
1833 Aliased_Present => True,
1834 Object_Definition =>
1835 New_Occurrence_Of (RTE (Data_Typ), Loc),
1836 Expression =>
1837 Make_Aggregate (Loc,
1838 Component_Associations => New_List (
1839 Make_Component_Association (Loc,
1840 Choices => New_List (
1841 Make_Range (Loc,
1842 Low_Bound =>
1843 Make_Integer_Literal (Loc, 1),
1844 High_Bound =>
1845 New_Occurrence_Of (Size, Loc))),
1846 Expression => Make_Null (Loc))))));
1847 end;
1848 end if;
1849 end Create_Index_And_Data;
1850
1851 ---------------------
1852 -- Increment_Index --
1853 ---------------------
1854
1855 function Increment_Index return Node_Id is
1856 begin
1857 return
1858 Make_Assignment_Statement (Loc,
1859 Name => New_Occurrence_Of (Index, Loc),
1860 Expression =>
1861 Make_Op_Add (Loc,
1862 Left_Opnd => New_Occurrence_Of (Index, Loc),
1863 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1864 end Increment_Index;
1865
1866 ----------------------
1867 -- Name_Declaration --
1868 ----------------------
1869
1870 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1871 begin
1872 return
1873 Make_Object_Declaration (Loc,
1874 Defining_Identifier => Def_Id,
1875 Aliased_Present => True,
1876 Constant_Present => True,
1877 Object_Definition =>
1878 New_Occurrence_Of (Standard_String, Loc),
1879 Expression =>
1880 Make_String_Literal (Loc, String_From_Name_Buffer));
1881 end Name_Declaration;
1882
1883 --------------------
1884 -- Set_Entry_Name --
1885 --------------------
1886
1887 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1888 begin
1889 return
1890 Make_Assignment_Statement (Loc,
1891 Name =>
1892 Make_Indexed_Component (Loc,
1893 Prefix => New_Occurrence_Of (Data, Loc),
1894 Expressions => New_List (New_Occurrence_Of (Index, Loc))),
1895
1896 Expression =>
1897 Make_Attribute_Reference (Loc,
1898 Prefix => New_Occurrence_Of (Def_Id, Loc),
1899 Attribute_Name => Name_Unchecked_Access));
1900 end Set_Entry_Name;
1901
1902 -- Local variables
1903
1904 Temp_Id : Entity_Id;
1905 Subt_Def : Node_Id;
1906
1907 -- Start of processing for Build_Entry_Name
1908
1909 begin
1910 if Ekind (Comp_Id) = E_Entry_Family then
1911 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1912
1913 Create_Index_And_Data;
1914
1915 -- Step 1: Create the string name of the entry family.
1916 -- Generate:
1917 -- Temp : aliased constant String := "name ()";
1918
1919 Temp_Id := Make_Temporary (Loc, 'S');
1920 Get_Name_String (Chars (Comp_Id));
1921 Add_Char_To_Name_Buffer (' ');
1922 Add_Char_To_Name_Buffer ('(');
1923 Add_Char_To_Name_Buffer (')');
1924
1925 Append_To (Stmts, Name_Declaration (Temp_Id));
1926
1927 -- Generate:
1928 -- for Member in Family_Low .. Family_High loop
1929 -- Set_Entry_Name (...);
1930 -- Index := Index + 1;
1931 -- end loop;
1932
1933 Append_To (Stmts,
1934 Make_Loop_Statement (Loc,
1935 Iteration_Scheme =>
1936 Make_Iteration_Scheme (Loc,
1937 Loop_Parameter_Specification =>
1938 Make_Loop_Parameter_Specification (Loc,
1939 Defining_Identifier =>
1940 Make_Temporary (Loc, 'L'),
1941 Discrete_Subtype_Definition =>
1942 Build_Range (Subt_Def))),
1943
1944 Statements => New_List (
1945 Set_Entry_Name (Temp_Id),
1946 Increment_Index),
1947 End_Label => Empty));
1948
1949 -- Entry
1950
1951 else
1952 Create_Index_And_Data;
1953
1954 -- Step 1: Create the string name of the entry. Generate:
1955 -- Temp : aliased constant String := "name";
1956
1957 Temp_Id := Make_Temporary (Loc, 'S');
1958 Get_Name_String (Chars (Comp_Id));
1959
1960 Append_To (Stmts, Name_Declaration (Temp_Id));
1961
1962 -- Step 2: Associate the string name with the underlying data
1963 -- structure.
1964
1965 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1966 Append_To (Stmts, Increment_Index);
1967 end if;
1968 end Build_Entry_Name;
1969
1970 ----------------------
1971 -- Object_Reference --
1972 ----------------------
1973
1974 function Object_Reference return Node_Id is
1975 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1976 Field : Name_Id;
1977 Ref : Node_Id;
1978
1979 begin
1980 if Is_Protected_Type (Typ) then
1981 Field := Name_uObject;
1982 else
1983 Field := Name_uTask_Id;
1984 end if;
1985
1986 Ref :=
1987 Make_Selected_Component (Loc,
1988 Prefix =>
1989 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1990 Selector_Name => Make_Identifier (Loc, Field));
1991
1992 if Is_Protected_Type (Typ) then
1993 Ref :=
1994 Make_Attribute_Reference (Loc,
1995 Prefix => Ref,
1996 Attribute_Name => Name_Unchecked_Access);
1997 end if;
1998
1999 return Ref;
2000 end Object_Reference;
2001
2002 -- Local variables
2003
2004 Comp : Node_Id;
2005 Proc : RE_Id;
2006
2007 -- Start of processing for Build_Entry_Names
2008
2009 begin
2010 -- Retrieve the original concurrent type
2011
2012 if Is_Concurrent_Record_Type (Typ) then
2013 Typ := Corresponding_Concurrent_Type (Typ);
2014 end if;
2015
2016 pragma Assert (Is_Concurrent_Type (Typ));
2017
2018 -- Nothing to do if the type has no entries
2019
2020 if not Has_Entries (Typ) then
2021 return;
2022 end if;
2023
2024 -- Avoid generating entry names for a protected type with only one entry
2025
2026 if Is_Protected_Type (Typ)
2027 and then Find_Protection_Type (Base_Type (Typ)) /=
2028 RTE (RE_Protection_Entries)
2029 then
2030 return;
2031 end if;
2032
2033 -- Step 1: Populate the array with statically generated strings denoting
2034 -- entries and entry family names.
2035
2036 Comp := First_Entity (Typ);
2037 while Present (Comp) loop
2038 if Comes_From_Source (Comp)
2039 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
2040 then
2041 Build_Entry_Name (Comp);
2042 end if;
2043
2044 Next_Entity (Comp);
2045 end loop;
2046
2047 -- Step 2: Associate the array with the related concurrent object:
2048
2049 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
2050
2051 if Present (Data) then
2052 if Is_Protected_Type (Typ) then
2053 Proc := RO_PE_Set_Entry_Names;
2054 else
2055 Proc := RO_ST_Set_Entry_Names;
2056 end if;
2057
2058 Append_To (Stmts,
2059 Make_Procedure_Call_Statement (Loc,
2060 Name => New_Occurrence_Of (RTE (Proc), Loc),
2061 Parameter_Associations => New_List (
2062 Object_Reference,
2063 Make_Attribute_Reference (Loc,
2064 Prefix => New_Occurrence_Of (Data, Loc),
2065 Attribute_Name => Name_Unchecked_Access))));
2066 end if;
2067 end Build_Entry_Names;
2068
2069 ---------------------------
2070 -- Build_Parameter_Block --
2071 ---------------------------
2072
2073 function Build_Parameter_Block
2074 (Loc : Source_Ptr;
2075 Actuals : List_Id;
2076 Formals : List_Id;
2077 Decls : List_Id) return Entity_Id
2078 is
2079 Actual : Entity_Id;
2080 Comp_Nam : Node_Id;
2081 Comps : List_Id;
2082 Formal : Entity_Id;
2083 Has_Comp : Boolean := False;
2084 Rec_Nam : Node_Id;
2085
2086 begin
2087 Actual := First (Actuals);
2088 Comps := New_List;
2089 Formal := Defining_Identifier (First (Formals));
2090
2091 while Present (Actual) loop
2092 if not Is_Controlling_Actual (Actual) then
2093
2094 -- Generate:
2095 -- type Ann is access all <actual-type>
2096
2097 Comp_Nam := Make_Temporary (Loc, 'A');
2098 Set_Is_Param_Block_Component_Type (Comp_Nam);
2099
2100 Append_To (Decls,
2101 Make_Full_Type_Declaration (Loc,
2102 Defining_Identifier => Comp_Nam,
2103 Type_Definition =>
2104 Make_Access_To_Object_Definition (Loc,
2105 All_Present => True,
2106 Constant_Present => Ekind (Formal) = E_In_Parameter,
2107 Subtype_Indication =>
2108 New_Occurrence_Of (Etype (Actual), Loc))));
2109
2110 -- Generate:
2111 -- Param : Ann;
2112
2113 Append_To (Comps,
2114 Make_Component_Declaration (Loc,
2115 Defining_Identifier =>
2116 Make_Defining_Identifier (Loc, Chars (Formal)),
2117 Component_Definition =>
2118 Make_Component_Definition (Loc,
2119 Aliased_Present =>
2120 False,
2121 Subtype_Indication =>
2122 New_Occurrence_Of (Comp_Nam, Loc))));
2123
2124 Has_Comp := True;
2125 end if;
2126
2127 Next_Actual (Actual);
2128 Next_Formal_With_Extras (Formal);
2129 end loop;
2130
2131 Rec_Nam := Make_Temporary (Loc, 'P');
2132
2133 if Has_Comp then
2134
2135 -- Generate:
2136 -- type Pnn is record
2137 -- Param1 : Ann1;
2138 -- ...
2139 -- ParamN : AnnN;
2140
2141 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
2142 -- the original parameter names and Ann1 .. AnnN are the access to
2143 -- actual types.
2144
2145 Append_To (Decls,
2146 Make_Full_Type_Declaration (Loc,
2147 Defining_Identifier =>
2148 Rec_Nam,
2149 Type_Definition =>
2150 Make_Record_Definition (Loc,
2151 Component_List =>
2152 Make_Component_List (Loc, Comps))));
2153 else
2154 -- Generate:
2155 -- type Pnn is null record;
2156
2157 Append_To (Decls,
2158 Make_Full_Type_Declaration (Loc,
2159 Defining_Identifier =>
2160 Rec_Nam,
2161 Type_Definition =>
2162 Make_Record_Definition (Loc,
2163 Null_Present => True,
2164 Component_List => Empty)));
2165 end if;
2166
2167 return Rec_Nam;
2168 end Build_Parameter_Block;
2169
2170 --------------------------------------
2171 -- Build_Renamed_Formal_Declaration --
2172 --------------------------------------
2173
2174 function Build_Renamed_Formal_Declaration
2175 (New_F : Entity_Id;
2176 Formal : Entity_Id;
2177 Comp : Entity_Id;
2178 Renamed_Formal : Node_Id) return Node_Id
2179 is
2180 Loc : constant Source_Ptr := Sloc (New_F);
2181 Decl : Node_Id;
2182
2183 begin
2184 -- If the formal is a tagged incomplete type, it is already passed
2185 -- by reference, so it is sufficient to rename the pointer component
2186 -- that corresponds to the actual. Otherwise we need to dereference
2187 -- the pointer component to obtain the actual.
2188
2189 if Is_Incomplete_Type (Etype (Formal))
2190 and then Is_Tagged_Type (Etype (Formal))
2191 then
2192 Decl :=
2193 Make_Object_Renaming_Declaration (Loc,
2194 Defining_Identifier => New_F,
2195 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
2196 Name => Renamed_Formal);
2197
2198 else
2199 Decl :=
2200 Make_Object_Renaming_Declaration (Loc,
2201 Defining_Identifier => New_F,
2202 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
2203 Name =>
2204 Make_Explicit_Dereference (Loc, Renamed_Formal));
2205 end if;
2206
2207 return Decl;
2208 end Build_Renamed_Formal_Declaration;
2209
2210 --------------------------
2211 -- Build_Wrapper_Bodies --
2212 --------------------------
2213
2214 procedure Build_Wrapper_Bodies
2215 (Loc : Source_Ptr;
2216 Typ : Entity_Id;
2217 N : Node_Id)
2218 is
2219 Rec_Typ : Entity_Id;
2220
2221 function Build_Wrapper_Body
2222 (Loc : Source_Ptr;
2223 Subp_Id : Entity_Id;
2224 Obj_Typ : Entity_Id;
2225 Formals : List_Id) return Node_Id;
2226 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2227 -- associated with a protected or task type. Subp_Id is the subprogram
2228 -- name which will be wrapped. Obj_Typ is the type of the new formal
2229 -- parameter which handles dispatching and object notation. Formals are
2230 -- the original formals of Subp_Id which will be explicitly replicated.
2231
2232 ------------------------
2233 -- Build_Wrapper_Body --
2234 ------------------------
2235
2236 function Build_Wrapper_Body
2237 (Loc : Source_Ptr;
2238 Subp_Id : Entity_Id;
2239 Obj_Typ : Entity_Id;
2240 Formals : List_Id) return Node_Id
2241 is
2242 Body_Spec : Node_Id;
2243
2244 begin
2245 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2246
2247 -- The subprogram is not overriding or is not a primitive declared
2248 -- between two views.
2249
2250 if No (Body_Spec) then
2251 return Empty;
2252 end if;
2253
2254 declare
2255 Actuals : List_Id := No_List;
2256 Conv_Id : Node_Id;
2257 First_Form : Node_Id;
2258 Formal : Node_Id;
2259 Nam : Node_Id;
2260
2261 begin
2262 -- Map formals to actuals. Use the list built for the wrapper
2263 -- spec, skipping the object notation parameter.
2264
2265 First_Form := First (Parameter_Specifications (Body_Spec));
2266
2267 Formal := First_Form;
2268 Next (Formal);
2269
2270 if Present (Formal) then
2271 Actuals := New_List;
2272 while Present (Formal) loop
2273 Append_To (Actuals,
2274 Make_Identifier (Loc,
2275 Chars => Chars (Defining_Identifier (Formal))));
2276 Next (Formal);
2277 end loop;
2278 end if;
2279
2280 -- Special processing for primitives declared between a private
2281 -- type and its completion: the wrapper needs a properly typed
2282 -- parameter if the wrapped operation has a controlling first
2283 -- parameter. Note that this might not be the case for a function
2284 -- with a controlling result.
2285
2286 if Is_Private_Primitive_Subprogram (Subp_Id) then
2287 if No (Actuals) then
2288 Actuals := New_List;
2289 end if;
2290
2291 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2292 Prepend_To (Actuals,
2293 Unchecked_Convert_To
2294 (Corresponding_Concurrent_Type (Obj_Typ),
2295 Make_Identifier (Loc, Name_uO)));
2296
2297 else
2298 Prepend_To (Actuals,
2299 Make_Identifier (Loc,
2300 Chars => Chars (Defining_Identifier (First_Form))));
2301 end if;
2302
2303 Nam := New_Occurrence_Of (Subp_Id, Loc);
2304 else
2305 -- An access-to-variable object parameter requires an explicit
2306 -- dereference in the unchecked conversion. This case occurs
2307 -- when a protected entry wrapper must override an interface
2308 -- level procedure with interface access as first parameter.
2309
2310 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2311
2312 if Nkind (Parameter_Type (First_Form)) =
2313 N_Access_Definition
2314 then
2315 Conv_Id :=
2316 Make_Explicit_Dereference (Loc,
2317 Prefix => Make_Identifier (Loc, Name_uO));
2318 else
2319 Conv_Id := Make_Identifier (Loc, Name_uO);
2320 end if;
2321
2322 Nam :=
2323 Make_Selected_Component (Loc,
2324 Prefix =>
2325 Unchecked_Convert_To
2326 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2327 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
2328 end if;
2329
2330 -- Create the subprogram body. For a function, the call to the
2331 -- actual subprogram has to be converted to the corresponding
2332 -- record if it is a controlling result.
2333
2334 if Ekind (Subp_Id) = E_Function then
2335 declare
2336 Res : Node_Id;
2337
2338 begin
2339 Res :=
2340 Make_Function_Call (Loc,
2341 Name => Nam,
2342 Parameter_Associations => Actuals);
2343
2344 if Has_Controlling_Result (Subp_Id) then
2345 Res :=
2346 Unchecked_Convert_To
2347 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2348 end if;
2349
2350 return
2351 Make_Subprogram_Body (Loc,
2352 Specification => Body_Spec,
2353 Declarations => Empty_List,
2354 Handled_Statement_Sequence =>
2355 Make_Handled_Sequence_Of_Statements (Loc,
2356 Statements => New_List (
2357 Make_Simple_Return_Statement (Loc, Res))));
2358 end;
2359
2360 else
2361 return
2362 Make_Subprogram_Body (Loc,
2363 Specification => Body_Spec,
2364 Declarations => Empty_List,
2365 Handled_Statement_Sequence =>
2366 Make_Handled_Sequence_Of_Statements (Loc,
2367 Statements => New_List (
2368 Make_Procedure_Call_Statement (Loc,
2369 Name => Nam,
2370 Parameter_Associations => Actuals))));
2371 end if;
2372 end;
2373 end Build_Wrapper_Body;
2374
2375 -- Start of processing for Build_Wrapper_Bodies
2376
2377 begin
2378 if Is_Concurrent_Type (Typ) then
2379 Rec_Typ := Corresponding_Record_Type (Typ);
2380 else
2381 Rec_Typ := Typ;
2382 end if;
2383
2384 -- Generate wrapper bodies for a concurrent type which implements an
2385 -- interface.
2386
2387 if Present (Interfaces (Rec_Typ)) then
2388 declare
2389 Insert_Nod : Node_Id;
2390 Prim : Entity_Id;
2391 Prim_Elmt : Elmt_Id;
2392 Prim_Decl : Node_Id;
2393 Subp : Entity_Id;
2394 Wrap_Body : Node_Id;
2395 Wrap_Id : Entity_Id;
2396
2397 begin
2398 Insert_Nod := N;
2399
2400 -- Examine all primitive operations of the corresponding record
2401 -- type, looking for wrapper specs. Generate bodies in order to
2402 -- complete them.
2403
2404 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2405 while Present (Prim_Elmt) loop
2406 Prim := Node (Prim_Elmt);
2407
2408 if (Ekind (Prim) = E_Function
2409 or else Ekind (Prim) = E_Procedure)
2410 and then Is_Primitive_Wrapper (Prim)
2411 then
2412 Subp := Wrapped_Entity (Prim);
2413 Prim_Decl := Parent (Parent (Prim));
2414
2415 Wrap_Body :=
2416 Build_Wrapper_Body (Loc,
2417 Subp_Id => Subp,
2418 Obj_Typ => Rec_Typ,
2419 Formals => Parameter_Specifications (Parent (Subp)));
2420 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2421
2422 Set_Corresponding_Spec (Wrap_Body, Prim);
2423 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2424
2425 Insert_After (Insert_Nod, Wrap_Body);
2426 Insert_Nod := Wrap_Body;
2427
2428 Analyze (Wrap_Body);
2429 end if;
2430
2431 Next_Elmt (Prim_Elmt);
2432 end loop;
2433 end;
2434 end if;
2435 end Build_Wrapper_Bodies;
2436
2437 ------------------------
2438 -- Build_Wrapper_Spec --
2439 ------------------------
2440
2441 function Build_Wrapper_Spec
2442 (Subp_Id : Entity_Id;
2443 Obj_Typ : Entity_Id;
2444 Formals : List_Id) return Node_Id
2445 is
2446 Loc : constant Source_Ptr := Sloc (Subp_Id);
2447 First_Param : Node_Id;
2448 Iface : Entity_Id;
2449 Iface_Elmt : Elmt_Id;
2450 Iface_Op : Entity_Id;
2451 Iface_Op_Elmt : Elmt_Id;
2452
2453 function Overriding_Possible
2454 (Iface_Op : Entity_Id;
2455 Wrapper : Entity_Id) return Boolean;
2456 -- Determine whether a primitive operation can be overridden by Wrapper.
2457 -- Iface_Op is the candidate primitive operation of an interface type,
2458 -- Wrapper is the generated entry wrapper.
2459
2460 function Replicate_Formals
2461 (Loc : Source_Ptr;
2462 Formals : List_Id) return List_Id;
2463 -- An explicit parameter replication is required due to the Is_Entry_
2464 -- Formal flag being set for all the formals of an entry. The explicit
2465 -- replication removes the flag that would otherwise cause a different
2466 -- path of analysis.
2467
2468 -------------------------
2469 -- Overriding_Possible --
2470 -------------------------
2471
2472 function Overriding_Possible
2473 (Iface_Op : Entity_Id;
2474 Wrapper : Entity_Id) return Boolean
2475 is
2476 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2477 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2478
2479 function Type_Conformant_Parameters
2480 (Iface_Op_Params : List_Id;
2481 Wrapper_Params : List_Id) return Boolean;
2482 -- Determine whether the parameters of the generated entry wrapper
2483 -- and those of a primitive operation are type conformant. During
2484 -- this check, the first parameter of the primitive operation is
2485 -- skipped if it is a controlling argument: protected functions
2486 -- may have a controlling result.
2487
2488 --------------------------------
2489 -- Type_Conformant_Parameters --
2490 --------------------------------
2491
2492 function Type_Conformant_Parameters
2493 (Iface_Op_Params : List_Id;
2494 Wrapper_Params : List_Id) return Boolean
2495 is
2496 Iface_Op_Param : Node_Id;
2497 Iface_Op_Typ : Entity_Id;
2498 Wrapper_Param : Node_Id;
2499 Wrapper_Typ : Entity_Id;
2500
2501 begin
2502 -- Skip the first (controlling) parameter of primitive operation
2503
2504 Iface_Op_Param := First (Iface_Op_Params);
2505
2506 if Present (First_Formal (Iface_Op))
2507 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2508 then
2509 Iface_Op_Param := Next (Iface_Op_Param);
2510 end if;
2511
2512 Wrapper_Param := First (Wrapper_Params);
2513 while Present (Iface_Op_Param)
2514 and then Present (Wrapper_Param)
2515 loop
2516 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2517 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2518
2519 -- The two parameters must be mode conformant
2520
2521 if not Conforming_Types
2522 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2523 then
2524 return False;
2525 end if;
2526
2527 Next (Iface_Op_Param);
2528 Next (Wrapper_Param);
2529 end loop;
2530
2531 -- One of the lists is longer than the other
2532
2533 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2534 return False;
2535 end if;
2536
2537 return True;
2538 end Type_Conformant_Parameters;
2539
2540 -- Start of processing for Overriding_Possible
2541
2542 begin
2543 if Chars (Iface_Op) /= Chars (Wrapper) then
2544 return False;
2545 end if;
2546
2547 -- If an inherited subprogram is implemented by a protected procedure
2548 -- or an entry, then the first parameter of the inherited subprogram
2549 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2550
2551 if Ekind (Iface_Op) = E_Procedure
2552 and then Present (Parameter_Specifications (Iface_Op_Spec))
2553 then
2554 declare
2555 Obj_Param : constant Node_Id :=
2556 First (Parameter_Specifications (Iface_Op_Spec));
2557 begin
2558 if not Out_Present (Obj_Param)
2559 and then Nkind (Parameter_Type (Obj_Param)) /=
2560 N_Access_Definition
2561 then
2562 return False;
2563 end if;
2564 end;
2565 end if;
2566
2567 return
2568 Type_Conformant_Parameters (
2569 Parameter_Specifications (Iface_Op_Spec),
2570 Parameter_Specifications (Wrapper_Spec));
2571 end Overriding_Possible;
2572
2573 -----------------------
2574 -- Replicate_Formals --
2575 -----------------------
2576
2577 function Replicate_Formals
2578 (Loc : Source_Ptr;
2579 Formals : List_Id) return List_Id
2580 is
2581 New_Formals : constant List_Id := New_List;
2582 Formal : Node_Id;
2583 Param_Type : Node_Id;
2584
2585 begin
2586 Formal := First (Formals);
2587
2588 -- Skip the object parameter when dealing with primitives declared
2589 -- between two views.
2590
2591 if Is_Private_Primitive_Subprogram (Subp_Id)
2592 and then not Has_Controlling_Result (Subp_Id)
2593 then
2594 Formal := Next (Formal);
2595 end if;
2596
2597 while Present (Formal) loop
2598
2599 -- Create an explicit copy of the entry parameter
2600
2601 -- When creating the wrapper subprogram for a primitive operation
2602 -- of a protected interface we must construct an equivalent
2603 -- signature to that of the overriding operation. For regular
2604 -- parameters we can just use the type of the formal, but for
2605 -- access to subprogram parameters we need to reanalyze the
2606 -- parameter type to create local entities for the signature of
2607 -- the subprogram type. Using the entities of the overriding
2608 -- subprogram will result in out-of-scope errors in the back-end.
2609
2610 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2611 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2612 else
2613 Param_Type :=
2614 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2615 end if;
2616
2617 Append_To (New_Formals,
2618 Make_Parameter_Specification (Loc,
2619 Defining_Identifier =>
2620 Make_Defining_Identifier (Loc,
2621 Chars => Chars
2622 (Defining_Identifier (Formal))),
2623 In_Present => In_Present (Formal),
2624 Out_Present => Out_Present (Formal),
2625 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2626 Parameter_Type => Param_Type));
2627
2628 Next (Formal);
2629 end loop;
2630
2631 return New_Formals;
2632 end Replicate_Formals;
2633
2634 -- Start of processing for Build_Wrapper_Spec
2635
2636 begin
2637 -- No point in building wrappers for untagged concurrent types
2638
2639 pragma Assert (Is_Tagged_Type (Obj_Typ));
2640
2641 -- An entry or a protected procedure can override a routine where the
2642 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2643 -- type. Since the wrapper must have the exact same signature as that of
2644 -- the overridden subprogram, we try to find the overriding candidate
2645 -- and use its controlling formal.
2646
2647 First_Param := Empty;
2648
2649 -- Check every implemented interface
2650
2651 if Present (Interfaces (Obj_Typ)) then
2652 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2653 Search : while Present (Iface_Elmt) loop
2654 Iface := Node (Iface_Elmt);
2655
2656 -- Check every interface primitive
2657
2658 if Present (Primitive_Operations (Iface)) then
2659 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2660 while Present (Iface_Op_Elmt) loop
2661 Iface_Op := Node (Iface_Op_Elmt);
2662
2663 -- Ignore predefined primitives
2664
2665 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2666 Iface_Op := Ultimate_Alias (Iface_Op);
2667
2668 -- The current primitive operation can be overridden by
2669 -- the generated entry wrapper.
2670
2671 if Overriding_Possible (Iface_Op, Subp_Id) then
2672 First_Param :=
2673 First (Parameter_Specifications (Parent (Iface_Op)));
2674
2675 exit Search;
2676 end if;
2677 end if;
2678
2679 Next_Elmt (Iface_Op_Elmt);
2680 end loop;
2681 end if;
2682
2683 Next_Elmt (Iface_Elmt);
2684 end loop Search;
2685 end if;
2686
2687 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2688 -- this subprogram and this is not a primitive declared between two
2689 -- views then force the generation of a wrapper. As an optimization,
2690 -- previous versions of the frontend avoid generating the wrapper;
2691 -- however, the wrapper facilitates locating and reporting an error
2692 -- when a duplicate declaration is found later. See example in
2693 -- AI05-0090-1.
2694
2695 if No (First_Param)
2696 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2697 then
2698 if Is_Task_Type
2699 (Corresponding_Concurrent_Type (Obj_Typ))
2700 then
2701 First_Param :=
2702 Make_Parameter_Specification (Loc,
2703 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2704 In_Present => True,
2705 Out_Present => False,
2706 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2707
2708 -- For entries and procedures of protected types the mode of
2709 -- the controlling argument must be in-out.
2710
2711 else
2712 First_Param :=
2713 Make_Parameter_Specification (Loc,
2714 Defining_Identifier =>
2715 Make_Defining_Identifier (Loc,
2716 Chars => Name_uO),
2717 In_Present => True,
2718 Out_Present => (Ekind (Subp_Id) /= E_Function),
2719 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2720 end if;
2721 end if;
2722
2723 declare
2724 Wrapper_Id : constant Entity_Id :=
2725 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2726 New_Formals : List_Id;
2727 Obj_Param : Node_Id;
2728 Obj_Param_Typ : Entity_Id;
2729
2730 begin
2731 -- Minimum decoration is needed to catch the entity in
2732 -- Sem_Ch6.Override_Dispatching_Operation.
2733
2734 if Ekind (Subp_Id) = E_Function then
2735 Set_Ekind (Wrapper_Id, E_Function);
2736 else
2737 Set_Ekind (Wrapper_Id, E_Procedure);
2738 end if;
2739
2740 Set_Is_Primitive_Wrapper (Wrapper_Id);
2741 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2742 Set_Is_Private_Primitive (Wrapper_Id,
2743 Is_Private_Primitive_Subprogram (Subp_Id));
2744
2745 -- Process the formals
2746
2747 New_Formals := Replicate_Formals (Loc, Formals);
2748
2749 -- A function with a controlling result and no first controlling
2750 -- formal needs no additional parameter.
2751
2752 if Has_Controlling_Result (Subp_Id)
2753 and then
2754 (No (First_Formal (Subp_Id))
2755 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2756 then
2757 null;
2758
2759 -- Routine Subp_Id has been found to override an interface primitive.
2760 -- If the interface operation has an access parameter, create a copy
2761 -- of it, with the same null exclusion indicator if present.
2762
2763 elsif Present (First_Param) then
2764 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2765 Obj_Param_Typ :=
2766 Make_Access_Definition (Loc,
2767 Subtype_Mark =>
2768 New_Occurrence_Of (Obj_Typ, Loc),
2769 Null_Exclusion_Present =>
2770 Null_Exclusion_Present (Parameter_Type (First_Param)),
2771 Constant_Present =>
2772 Constant_Present (Parameter_Type (First_Param)));
2773 else
2774 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2775 end if;
2776
2777 Obj_Param :=
2778 Make_Parameter_Specification (Loc,
2779 Defining_Identifier =>
2780 Make_Defining_Identifier (Loc,
2781 Chars => Name_uO),
2782 In_Present => In_Present (First_Param),
2783 Out_Present => Out_Present (First_Param),
2784 Parameter_Type => Obj_Param_Typ);
2785
2786 Prepend_To (New_Formals, Obj_Param);
2787
2788 -- If we are dealing with a primitive declared between two views,
2789 -- implemented by a synchronized operation, we need to create
2790 -- a default parameter. The mode of the parameter must match that
2791 -- of the primitive operation.
2792
2793 else
2794 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2795 Obj_Param :=
2796 Make_Parameter_Specification (Loc,
2797 Defining_Identifier =>
2798 Make_Defining_Identifier (Loc, Name_uO),
2799 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2800 Out_Present => Ekind (Subp_Id) /= E_Function,
2801 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2802 Prepend_To (New_Formals, Obj_Param);
2803 end if;
2804
2805 -- Build the final spec. If it is a function with a controlling
2806 -- result, it is a primitive operation of the corresponding
2807 -- record type, so mark the spec accordingly.
2808
2809 if Ekind (Subp_Id) = E_Function then
2810 declare
2811 Res_Def : Node_Id;
2812
2813 begin
2814 if Has_Controlling_Result (Subp_Id) then
2815 Res_Def :=
2816 New_Occurrence_Of
2817 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2818 else
2819 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2820 end if;
2821
2822 return
2823 Make_Function_Specification (Loc,
2824 Defining_Unit_Name => Wrapper_Id,
2825 Parameter_Specifications => New_Formals,
2826 Result_Definition => Res_Def);
2827 end;
2828 else
2829 return
2830 Make_Procedure_Specification (Loc,
2831 Defining_Unit_Name => Wrapper_Id,
2832 Parameter_Specifications => New_Formals);
2833 end if;
2834 end;
2835 end Build_Wrapper_Spec;
2836
2837 -------------------------
2838 -- Build_Wrapper_Specs --
2839 -------------------------
2840
2841 procedure Build_Wrapper_Specs
2842 (Loc : Source_Ptr;
2843 Typ : Entity_Id;
2844 N : in out Node_Id)
2845 is
2846 Def : Node_Id;
2847 Rec_Typ : Entity_Id;
2848 procedure Scan_Declarations (L : List_Id);
2849 -- Common processing for visible and private declarations
2850 -- of a protected type.
2851
2852 procedure Scan_Declarations (L : List_Id) is
2853 Decl : Node_Id;
2854 Wrap_Decl : Node_Id;
2855 Wrap_Spec : Node_Id;
2856
2857 begin
2858 if No (L) then
2859 return;
2860 end if;
2861
2862 Decl := First (L);
2863 while Present (Decl) loop
2864 Wrap_Spec := Empty;
2865
2866 if Nkind (Decl) = N_Entry_Declaration
2867 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2868 then
2869 Wrap_Spec :=
2870 Build_Wrapper_Spec
2871 (Subp_Id => Defining_Identifier (Decl),
2872 Obj_Typ => Rec_Typ,
2873 Formals => Parameter_Specifications (Decl));
2874
2875 elsif Nkind (Decl) = N_Subprogram_Declaration then
2876 Wrap_Spec :=
2877 Build_Wrapper_Spec
2878 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2879 Obj_Typ => Rec_Typ,
2880 Formals =>
2881 Parameter_Specifications (Specification (Decl)));
2882 end if;
2883
2884 if Present (Wrap_Spec) then
2885 Wrap_Decl :=
2886 Make_Subprogram_Declaration (Loc,
2887 Specification => Wrap_Spec);
2888
2889 Insert_After (N, Wrap_Decl);
2890 N := Wrap_Decl;
2891
2892 Analyze (Wrap_Decl);
2893 end if;
2894
2895 Next (Decl);
2896 end loop;
2897 end Scan_Declarations;
2898
2899 -- start of processing for Build_Wrapper_Specs
2900
2901 begin
2902 if Is_Protected_Type (Typ) then
2903 Def := Protected_Definition (Parent (Typ));
2904 else pragma Assert (Is_Task_Type (Typ));
2905 Def := Task_Definition (Parent (Typ));
2906 end if;
2907
2908 Rec_Typ := Corresponding_Record_Type (Typ);
2909
2910 -- Generate wrapper specs for a concurrent type which implements an
2911 -- interface. Operations in both the visible and private parts may
2912 -- implement progenitor operations.
2913
2914 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2915 Scan_Declarations (Visible_Declarations (Def));
2916 Scan_Declarations (Private_Declarations (Def));
2917 end if;
2918 end Build_Wrapper_Specs;
2919
2920 ---------------------------
2921 -- Build_Find_Body_Index --
2922 ---------------------------
2923
2924 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2925 Loc : constant Source_Ptr := Sloc (Typ);
2926 Ent : Entity_Id;
2927 E_Typ : Entity_Id;
2928 Has_F : Boolean := False;
2929 Index : Nat;
2930 If_St : Node_Id := Empty;
2931 Lo : Node_Id;
2932 Hi : Node_Id;
2933 Decls : List_Id := New_List;
2934 Ret : Node_Id;
2935 Spec : Node_Id;
2936 Siz : Node_Id := Empty;
2937
2938 procedure Add_If_Clause (Expr : Node_Id);
2939 -- Add test for range of current entry
2940
2941 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2942 -- If a bound of an entry is given by a discriminant, retrieve the
2943 -- actual value of the discriminant from the enclosing object.
2944
2945 -------------------
2946 -- Add_If_Clause --
2947 -------------------
2948
2949 procedure Add_If_Clause (Expr : Node_Id) is
2950 Cond : Node_Id;
2951 Stats : constant List_Id :=
2952 New_List (
2953 Make_Simple_Return_Statement (Loc,
2954 Expression => Make_Integer_Literal (Loc, Index + 1)));
2955
2956 begin
2957 -- Index for current entry body
2958
2959 Index := Index + 1;
2960
2961 -- Compute total length of entry queues so far
2962
2963 if No (Siz) then
2964 Siz := Expr;
2965 else
2966 Siz :=
2967 Make_Op_Add (Loc,
2968 Left_Opnd => Siz,
2969 Right_Opnd => Expr);
2970 end if;
2971
2972 Cond :=
2973 Make_Op_Le (Loc,
2974 Left_Opnd => Make_Identifier (Loc, Name_uE),
2975 Right_Opnd => Siz);
2976
2977 -- Map entry queue indexes in the range of the current family
2978 -- into the current index, that designates the entry body.
2979
2980 if No (If_St) then
2981 If_St :=
2982 Make_Implicit_If_Statement (Typ,
2983 Condition => Cond,
2984 Then_Statements => Stats,
2985 Elsif_Parts => New_List);
2986 Ret := If_St;
2987
2988 else
2989 Append_To (Elsif_Parts (If_St),
2990 Make_Elsif_Part (Loc,
2991 Condition => Cond,
2992 Then_Statements => Stats));
2993 end if;
2994 end Add_If_Clause;
2995
2996 ------------------------------
2997 -- Convert_Discriminant_Ref --
2998 ------------------------------
2999
3000 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
3001 B : Node_Id;
3002
3003 begin
3004 if Is_Entity_Name (Bound)
3005 and then Ekind (Entity (Bound)) = E_Discriminant
3006 then
3007 B :=
3008 Make_Selected_Component (Loc,
3009 Prefix =>
3010 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
3011 Make_Explicit_Dereference (Loc,
3012 Make_Identifier (Loc, Name_uObject))),
3013 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
3014 Set_Etype (B, Etype (Entity (Bound)));
3015 else
3016 B := New_Copy_Tree (Bound);
3017 end if;
3018
3019 return B;
3020 end Convert_Discriminant_Ref;
3021
3022 -- Start of processing for Build_Find_Body_Index
3023
3024 begin
3025 Spec := Build_Find_Body_Index_Spec (Typ);
3026
3027 Ent := First_Entity (Typ);
3028 while Present (Ent) loop
3029 if Ekind (Ent) = E_Entry_Family then
3030 Has_F := True;
3031 exit;
3032 end if;
3033
3034 Next_Entity (Ent);
3035 end loop;
3036
3037 if not Has_F then
3038
3039 -- If the protected type has no entry families, there is a one-one
3040 -- correspondence between entry queue and entry body.
3041
3042 Ret :=
3043 Make_Simple_Return_Statement (Loc,
3044 Expression => Make_Identifier (Loc, Name_uE));
3045
3046 else
3047 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
3048 -- the following:
3049
3050 -- if E <= l1 then return 1;
3051 -- elsif E <= l1 + l2 then return 2;
3052 -- ...
3053
3054 Index := 0;
3055 Siz := Empty;
3056 Ent := First_Entity (Typ);
3057
3058 Add_Object_Pointer (Loc, Typ, Decls);
3059
3060 while Present (Ent) loop
3061 if Ekind (Ent) = E_Entry then
3062 Add_If_Clause (Make_Integer_Literal (Loc, 1));
3063
3064 elsif Ekind (Ent) = E_Entry_Family then
3065 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
3066 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
3067 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
3068 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
3069 end if;
3070
3071 Next_Entity (Ent);
3072 end loop;
3073
3074 if Index = 1 then
3075 Decls := New_List;
3076 Ret :=
3077 Make_Simple_Return_Statement (Loc,
3078 Expression => Make_Integer_Literal (Loc, 1));
3079
3080 elsif Nkind (Ret) = N_If_Statement then
3081
3082 -- Ranges are in increasing order, so last one doesn't need guard
3083
3084 declare
3085 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
3086 begin
3087 Remove (Nod);
3088 Set_Else_Statements (Ret, Then_Statements (Nod));
3089 end;
3090 end if;
3091 end if;
3092
3093 return
3094 Make_Subprogram_Body (Loc,
3095 Specification => Spec,
3096 Declarations => Decls,
3097 Handled_Statement_Sequence =>
3098 Make_Handled_Sequence_Of_Statements (Loc,
3099 Statements => New_List (Ret)));
3100 end Build_Find_Body_Index;
3101
3102 --------------------------------
3103 -- Build_Find_Body_Index_Spec --
3104 --------------------------------
3105
3106 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
3107 Loc : constant Source_Ptr := Sloc (Typ);
3108 Id : constant Entity_Id :=
3109 Make_Defining_Identifier (Loc,
3110 Chars => New_External_Name (Chars (Typ), 'F'));
3111 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
3112 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
3113
3114 begin
3115 return
3116 Make_Function_Specification (Loc,
3117 Defining_Unit_Name => Id,
3118 Parameter_Specifications => New_List (
3119 Make_Parameter_Specification (Loc,
3120 Defining_Identifier => Parm1,
3121 Parameter_Type =>
3122 New_Occurrence_Of (RTE (RE_Address), Loc)),
3123
3124 Make_Parameter_Specification (Loc,
3125 Defining_Identifier => Parm2,
3126 Parameter_Type =>
3127 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
3128
3129 Result_Definition => New_Occurrence_Of (
3130 RTE (RE_Protected_Entry_Index), Loc));
3131 end Build_Find_Body_Index_Spec;
3132
3133 -----------------------------------------------
3134 -- Build_Lock_Free_Protected_Subprogram_Body --
3135 -----------------------------------------------
3136
3137 function Build_Lock_Free_Protected_Subprogram_Body
3138 (N : Node_Id;
3139 Prot_Typ : Node_Id;
3140 Unprot_Spec : Node_Id) return Node_Id
3141 is
3142 Actuals : constant List_Id := New_List;
3143 Loc : constant Source_Ptr := Sloc (N);
3144 Spec : constant Node_Id := Specification (N);
3145 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3146 Formal : Node_Id;
3147 Prot_Spec : Node_Id;
3148 Stmt : Node_Id;
3149
3150 begin
3151 -- Create the protected version of the body
3152
3153 Prot_Spec :=
3154 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3155
3156 -- Build the actual parameters which appear in the call to the
3157 -- unprotected version of the body.
3158
3159 Formal := First (Parameter_Specifications (Prot_Spec));
3160 while Present (Formal) loop
3161 Append_To (Actuals,
3162 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3163
3164 Next (Formal);
3165 end loop;
3166
3167 -- Function case, generate:
3168 -- return <Unprot_Func_Call>;
3169
3170 if Nkind (Spec) = N_Function_Specification then
3171 Stmt :=
3172 Make_Simple_Return_Statement (Loc,
3173 Expression =>
3174 Make_Function_Call (Loc,
3175 Name =>
3176 Make_Identifier (Loc, Chars (Unprot_Id)),
3177 Parameter_Associations => Actuals));
3178
3179 -- Procedure case, call the unprotected version
3180
3181 else
3182 Stmt :=
3183 Make_Procedure_Call_Statement (Loc,
3184 Name =>
3185 Make_Identifier (Loc, Chars (Unprot_Id)),
3186 Parameter_Associations => Actuals);
3187 end if;
3188
3189 return
3190 Make_Subprogram_Body (Loc,
3191 Declarations => Empty_List,
3192 Specification => Prot_Spec,
3193 Handled_Statement_Sequence =>
3194 Make_Handled_Sequence_Of_Statements (Loc,
3195 Statements => New_List (Stmt)));
3196 end Build_Lock_Free_Protected_Subprogram_Body;
3197
3198 -------------------------------------------------
3199 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3200 -------------------------------------------------
3201
3202 -- Procedures which meet the lock-free implementation requirements and
3203 -- reference a unique scalar component Comp are expanded in the following
3204 -- manner:
3205
3206 -- procedure P (...) is
3207 -- Expected_Comp : constant Comp_Type :=
3208 -- Comp_Type
3209 -- (System.Atomic_Primitives.Lock_Free_Read_N
3210 -- (_Object.Comp'Address));
3211 -- begin
3212 -- loop
3213 -- declare
3214 -- <original declarations before the object renaming declaration
3215 -- of Comp>
3216 --
3217 -- Desired_Comp : Comp_Type := Expected_Comp;
3218 -- Comp : Comp_Type renames Desired_Comp;
3219 --
3220 -- <original delarations after the object renaming declaration
3221 -- of Comp>
3222 --
3223 -- begin
3224 -- <original statements>
3225 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3226 -- (_Object.Comp'Address,
3227 -- Interfaces.Unsigned_N (Expected_Comp),
3228 -- Interfaces.Unsigned_N (Desired_Comp));
3229 -- end;
3230 -- end loop;
3231 -- end P;
3232
3233 -- Each return and raise statement of P is transformed into an atomic
3234 -- status check:
3235
3236 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3237 -- (_Object.Comp'Address,
3238 -- Interfaces.Unsigned_N (Expected_Comp),
3239 -- Interfaces.Unsigned_N (Desired_Comp));
3240 -- then
3241 -- <original statement>
3242 -- else
3243 -- goto L0;
3244 -- end if;
3245
3246 -- Functions which meet the lock-free implementation requirements and
3247 -- reference a unique scalar component Comp are expanded in the following
3248 -- manner:
3249
3250 -- function F (...) return ... is
3251 -- <original declarations before the object renaming declaration
3252 -- of Comp>
3253 --
3254 -- Expected_Comp : constant Comp_Type :=
3255 -- Comp_Type
3256 -- (System.Atomic_Primitives.Lock_Free_Read_N
3257 -- (_Object.Comp'Address));
3258 -- Comp : Comp_Type renames Expected_Comp;
3259 --
3260 -- <original delarations after the object renaming declaration of
3261 -- Comp>
3262 --
3263 -- begin
3264 -- <original statements>
3265 -- end F;
3266
3267 function Build_Lock_Free_Unprotected_Subprogram_Body
3268 (N : Node_Id;
3269 Prot_Typ : Node_Id) return Node_Id
3270 is
3271 function Referenced_Component (N : Node_Id) return Entity_Id;
3272 -- Subprograms which meet the lock-free implementation criteria are
3273 -- allowed to reference only one unique component. Return the prival
3274 -- of the said component.
3275
3276 --------------------------
3277 -- Referenced_Component --
3278 --------------------------
3279
3280 function Referenced_Component (N : Node_Id) return Entity_Id is
3281 Comp : Entity_Id;
3282 Decl : Node_Id;
3283 Source_Comp : Entity_Id := Empty;
3284
3285 begin
3286 -- Find the unique source component which N references in its
3287 -- statements.
3288
3289 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3290 declare
3291 Element : Lock_Free_Subprogram renames
3292 Lock_Free_Subprogram_Table.Table (Index);
3293 begin
3294 if Element.Sub_Body = N then
3295 Source_Comp := Element.Comp_Id;
3296 exit;
3297 end if;
3298 end;
3299 end loop;
3300
3301 if No (Source_Comp) then
3302 return Empty;
3303 end if;
3304
3305 -- Find the prival which corresponds to the source component within
3306 -- the declarations of N.
3307
3308 Decl := First (Declarations (N));
3309 while Present (Decl) loop
3310
3311 -- Privals appear as object renamings
3312
3313 if Nkind (Decl) = N_Object_Renaming_Declaration then
3314 Comp := Defining_Identifier (Decl);
3315
3316 if Present (Prival_Link (Comp))
3317 and then Prival_Link (Comp) = Source_Comp
3318 then
3319 return Comp;
3320 end if;
3321 end if;
3322
3323 Next (Decl);
3324 end loop;
3325
3326 return Empty;
3327 end Referenced_Component;
3328
3329 -- Local variables
3330
3331 Comp : constant Entity_Id := Referenced_Component (N);
3332 Loc : constant Source_Ptr := Sloc (N);
3333 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3334 Decls : List_Id := Declarations (N);
3335
3336 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3337
3338 begin
3339 -- Add renamings for the protection object, discriminals, privals, and
3340 -- the entry index constant for use by debugger.
3341
3342 Debug_Private_Data_Declarations (Decls);
3343
3344 -- Perform the lock-free expansion when the subprogram references a
3345 -- protected component.
3346
3347 if Present (Comp) then
3348 Protected_Component_Ref : declare
3349 Comp_Decl : constant Node_Id := Parent (Comp);
3350 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3351 Comp_Type : constant Entity_Id := Etype (Comp);
3352
3353 Is_Procedure : constant Boolean :=
3354 Ekind (Corresponding_Spec (N)) = E_Procedure;
3355 -- Indicates if N is a protected procedure body
3356
3357 Block_Decls : List_Id;
3358 Try_Write : Entity_Id;
3359 Desired_Comp : Entity_Id;
3360 Decl : Node_Id;
3361 Label : Node_Id;
3362 Label_Id : Entity_Id := Empty;
3363 Read : Entity_Id;
3364 Expected_Comp : Entity_Id;
3365 Stmt : Node_Id;
3366 Stmts : List_Id :=
3367 New_Copy_List (Statements (Hand_Stmt_Seq));
3368 Typ_Size : Int;
3369 Unsigned : Entity_Id;
3370
3371 function Process_Node (N : Node_Id) return Traverse_Result;
3372 -- Transform a single node if it is a return statement, a raise
3373 -- statement or a reference to Comp.
3374
3375 procedure Process_Stmts (Stmts : List_Id);
3376 -- Given a statement sequence Stmts, wrap any return or raise
3377 -- statements in the following manner:
3378 --
3379 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3380 -- (_Object.Comp'Address,
3381 -- Interfaces.Unsigned_N (Expected_Comp),
3382 -- Interfaces.Unsigned_N (Desired_Comp))
3383 -- then
3384 -- <Stmt>;
3385 -- else
3386 -- goto L0;
3387 -- end if;
3388
3389 ------------------
3390 -- Process_Node --
3391 ------------------
3392
3393 function Process_Node (N : Node_Id) return Traverse_Result is
3394
3395 procedure Wrap_Statement (Stmt : Node_Id);
3396 -- Wrap an arbitrary statement inside an if statement where the
3397 -- condition does an atomic check on the state of the object.
3398
3399 --------------------
3400 -- Wrap_Statement --
3401 --------------------
3402
3403 procedure Wrap_Statement (Stmt : Node_Id) is
3404 begin
3405 -- The first time through, create the declaration of a label
3406 -- which is used to skip the remainder of source statements
3407 -- if the state of the object has changed.
3408
3409 if No (Label_Id) then
3410 Label_Id :=
3411 Make_Identifier (Loc, New_External_Name ('L', 0));
3412 Set_Entity (Label_Id,
3413 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3414 end if;
3415
3416 -- Generate:
3417 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3418 -- (_Object.Comp'Address,
3419 -- Interfaces.Unsigned_N (Expected_Comp),
3420 -- Interfaces.Unsigned_N (Desired_Comp))
3421 -- then
3422 -- <Stmt>;
3423 -- else
3424 -- goto L0;
3425 -- end if;
3426
3427 Rewrite (Stmt,
3428 Make_Implicit_If_Statement (N,
3429 Condition =>
3430 Make_Function_Call (Loc,
3431 Name =>
3432 New_Occurrence_Of (Try_Write, Loc),
3433 Parameter_Associations => New_List (
3434 Make_Attribute_Reference (Loc,
3435 Prefix => Relocate_Node (Comp_Sel_Nam),
3436 Attribute_Name => Name_Address),
3437
3438 Unchecked_Convert_To (Unsigned,
3439 New_Occurrence_Of (Expected_Comp, Loc)),
3440
3441 Unchecked_Convert_To (Unsigned,
3442 New_Occurrence_Of (Desired_Comp, Loc)))),
3443
3444 Then_Statements => New_List (Relocate_Node (Stmt)),
3445
3446 Else_Statements => New_List (
3447 Make_Goto_Statement (Loc,
3448 Name =>
3449 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3450 end Wrap_Statement;
3451
3452 -- Start of processing for Process_Node
3453
3454 begin
3455 -- Wrap each return and raise statement that appear inside a
3456 -- procedure. Skip the last return statement which is added by
3457 -- default since it is transformed into an exit statement.
3458
3459 if Is_Procedure
3460 and then ((Nkind (N) = N_Simple_Return_Statement
3461 and then N /= Last (Stmts))
3462 or else Nkind (N) = N_Extended_Return_Statement
3463 or else (Nkind_In (N, N_Raise_Constraint_Error,
3464 N_Raise_Program_Error,
3465 N_Raise_Statement,
3466 N_Raise_Storage_Error)
3467 and then Comes_From_Source (N)))
3468 then
3469 Wrap_Statement (N);
3470 return Skip;
3471 end if;
3472
3473 -- Force reanalysis
3474
3475 Set_Analyzed (N, False);
3476
3477 return OK;
3478 end Process_Node;
3479
3480 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3481
3482 -------------------
3483 -- Process_Stmts --
3484 -------------------
3485
3486 procedure Process_Stmts (Stmts : List_Id) is
3487 Stmt : Node_Id;
3488 begin
3489 Stmt := First (Stmts);
3490 while Present (Stmt) loop
3491 Process_Nodes (Stmt);
3492 Next (Stmt);
3493 end loop;
3494 end Process_Stmts;
3495
3496 -- Start of processing for Protected_Component_Ref
3497
3498 begin
3499 -- Get the type size
3500
3501 if Known_Static_Esize (Comp_Type) then
3502 Typ_Size := UI_To_Int (Esize (Comp_Type));
3503
3504 -- If the Esize (Object_Size) is unknown at compile time, look at
3505 -- the RM_Size (Value_Size) since it may have been set by an
3506 -- explicit representation clause.
3507
3508 elsif Known_Static_RM_Size (Comp_Type) then
3509 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3510
3511 -- Should not happen since this has already been checked in
3512 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3513
3514 else
3515 raise Program_Error;
3516 end if;
3517
3518 -- Retrieve all relevant atomic routines and types
3519
3520 case Typ_Size is
3521 when 8 =>
3522 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3523 Read := RTE (RE_Lock_Free_Read_8);
3524 Unsigned := RTE (RE_Uint8);
3525
3526 when 16 =>
3527 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3528 Read := RTE (RE_Lock_Free_Read_16);
3529 Unsigned := RTE (RE_Uint16);
3530
3531 when 32 =>
3532 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3533 Read := RTE (RE_Lock_Free_Read_32);
3534 Unsigned := RTE (RE_Uint32);
3535
3536 when 64 =>
3537 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3538 Read := RTE (RE_Lock_Free_Read_64);
3539 Unsigned := RTE (RE_Uint64);
3540
3541 when others =>
3542 raise Program_Error;
3543 end case;
3544
3545 -- Generate:
3546 -- Expected_Comp : constant Comp_Type :=
3547 -- Comp_Type
3548 -- (System.Atomic_Primitives.Lock_Free_Read_N
3549 -- (_Object.Comp'Address));
3550
3551 Expected_Comp :=
3552 Make_Defining_Identifier (Loc,
3553 New_External_Name (Chars (Comp), Suffix => "_saved"));
3554
3555 Decl :=
3556 Make_Object_Declaration (Loc,
3557 Defining_Identifier => Expected_Comp,
3558 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3559 Constant_Present => True,
3560 Expression =>
3561 Unchecked_Convert_To (Comp_Type,
3562 Make_Function_Call (Loc,
3563 Name => New_Occurrence_Of (Read, Loc),
3564 Parameter_Associations => New_List (
3565 Make_Attribute_Reference (Loc,
3566 Prefix => Relocate_Node (Comp_Sel_Nam),
3567 Attribute_Name => Name_Address)))));
3568
3569 -- Protected procedures
3570
3571 if Is_Procedure then
3572 -- Move the original declarations inside the generated block
3573
3574 Block_Decls := Decls;
3575
3576 -- Reset the declarations list of the protected procedure to
3577 -- contain only Decl.
3578
3579 Decls := New_List (Decl);
3580
3581 -- Generate:
3582 -- Desired_Comp : Comp_Type := Expected_Comp;
3583
3584 Desired_Comp :=
3585 Make_Defining_Identifier (Loc,
3586 New_External_Name (Chars (Comp), Suffix => "_current"));
3587
3588 -- Insert the declarations of Expected_Comp and Desired_Comp in
3589 -- the block declarations right before the renaming of the
3590 -- protected component.
3591
3592 Insert_Before (Comp_Decl,
3593 Make_Object_Declaration (Loc,
3594 Defining_Identifier => Desired_Comp,
3595 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3596 Expression =>
3597 New_Occurrence_Of (Expected_Comp, Loc)));
3598
3599 -- Protected function
3600
3601 else
3602 Desired_Comp := Expected_Comp;
3603
3604 -- Insert the declaration of Expected_Comp in the function
3605 -- declarations right before the renaming of the protected
3606 -- component.
3607
3608 Insert_Before (Comp_Decl, Decl);
3609 end if;
3610
3611 -- Rewrite the protected component renaming declaration to be a
3612 -- renaming of Desired_Comp.
3613
3614 -- Generate:
3615 -- Comp : Comp_Type renames Desired_Comp;
3616
3617 Rewrite (Comp_Decl,
3618 Make_Object_Renaming_Declaration (Loc,
3619 Defining_Identifier =>
3620 Defining_Identifier (Comp_Decl),
3621 Subtype_Mark =>
3622 New_Occurrence_Of (Comp_Type, Loc),
3623 Name =>
3624 New_Occurrence_Of (Desired_Comp, Loc)));
3625
3626 -- Wrap any return or raise statements in Stmts in same the manner
3627 -- described in Process_Stmts.
3628
3629 Process_Stmts (Stmts);
3630
3631 -- Generate:
3632 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3633 -- (_Object.Comp'Address,
3634 -- Interfaces.Unsigned_N (Expected_Comp),
3635 -- Interfaces.Unsigned_N (Desired_Comp))
3636
3637 if Is_Procedure then
3638 Stmt :=
3639 Make_Exit_Statement (Loc,
3640 Condition =>
3641 Make_Function_Call (Loc,
3642 Name =>
3643 New_Occurrence_Of (Try_Write, Loc),
3644 Parameter_Associations => New_List (
3645 Make_Attribute_Reference (Loc,
3646 Prefix => Relocate_Node (Comp_Sel_Nam),
3647 Attribute_Name => Name_Address),
3648
3649 Unchecked_Convert_To (Unsigned,
3650 New_Occurrence_Of (Expected_Comp, Loc)),
3651
3652 Unchecked_Convert_To (Unsigned,
3653 New_Occurrence_Of (Desired_Comp, Loc)))));
3654
3655 -- Small optimization: transform the default return statement
3656 -- of a procedure into the atomic exit statement.
3657
3658 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3659 Rewrite (Last (Stmts), Stmt);
3660 else
3661 Append_To (Stmts, Stmt);
3662 end if;
3663 end if;
3664
3665 -- Create the declaration of the label used to skip the rest of
3666 -- the source statements when the object state changes.
3667
3668 if Present (Label_Id) then
3669 Label := Make_Label (Loc, Label_Id);
3670 Append_To (Decls,
3671 Make_Implicit_Label_Declaration (Loc,
3672 Defining_Identifier => Entity (Label_Id),
3673 Label_Construct => Label));
3674 Append_To (Stmts, Label);
3675 end if;
3676
3677 -- Generate:
3678 -- loop
3679 -- declare
3680 -- <Decls>
3681 -- begin
3682 -- <Stmts>
3683 -- end;
3684 -- end loop;
3685
3686 if Is_Procedure then
3687 Stmts :=
3688 New_List (
3689 Make_Loop_Statement (Loc,
3690 Statements => New_List (
3691 Make_Block_Statement (Loc,
3692 Declarations => Block_Decls,
3693 Handled_Statement_Sequence =>
3694 Make_Handled_Sequence_Of_Statements (Loc,
3695 Statements => Stmts))),
3696 End_Label => Empty));
3697 end if;
3698
3699 Hand_Stmt_Seq :=
3700 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3701 end Protected_Component_Ref;
3702 end if;
3703
3704 -- Make an unprotected version of the subprogram for use within the same
3705 -- object, with new name and extra parameter representing the object.
3706
3707 return
3708 Make_Subprogram_Body (Loc,
3709 Specification =>
3710 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3711 Declarations => Decls,
3712 Handled_Statement_Sequence => Hand_Stmt_Seq);
3713 end Build_Lock_Free_Unprotected_Subprogram_Body;
3714
3715 -------------------------
3716 -- Build_Master_Entity --
3717 -------------------------
3718
3719 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3720 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3721 Context : Node_Id;
3722 Context_Id : Entity_Id;
3723 Decl : Node_Id;
3724 Decls : List_Id;
3725 Par : Node_Id;
3726
3727 begin
3728 if Is_Itype (Obj_Or_Typ) then
3729 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3730 else
3731 Par := Parent (Obj_Or_Typ);
3732 end if;
3733
3734 -- When creating a master for a record component which is either a task
3735 -- or access-to-task, the enclosing record is the master scope and the
3736 -- proper insertion point is the component list.
3737
3738 if Is_Record_Type (Current_Scope) then
3739 Context := Par;
3740 Context_Id := Current_Scope;
3741 Decls := List_Containing (Context);
3742
3743 -- Default case for object declarations and access types. Note that the
3744 -- context is updated to the nearest enclosing body, block, package, or
3745 -- return statement.
3746
3747 else
3748 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3749 end if;
3750
3751 -- Do not create a master if one already exists or there is no task
3752 -- hierarchy.
3753
3754 if Has_Master_Entity (Context_Id)
3755 or else Restriction_Active (No_Task_Hierarchy)
3756 then
3757 return;
3758 end if;
3759
3760 -- Create a master, generate:
3761 -- _Master : constant Master_Id := Current_Master.all;
3762
3763 Decl :=
3764 Make_Object_Declaration (Loc,
3765 Defining_Identifier =>
3766 Make_Defining_Identifier (Loc, Name_uMaster),
3767 Constant_Present => True,
3768 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3769 Expression =>
3770 Make_Explicit_Dereference (Loc,
3771 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3772
3773 -- The master is inserted at the start of the declarative list of the
3774 -- context.
3775
3776 Prepend_To (Decls, Decl);
3777
3778 -- In certain cases where transient scopes are involved, the immediate
3779 -- scope is not always the proper master scope. Ensure that the master
3780 -- declaration and entity appear in the same context.
3781
3782 if Context_Id /= Current_Scope then
3783 Push_Scope (Context_Id);
3784 Analyze (Decl);
3785 Pop_Scope;
3786 else
3787 Analyze (Decl);
3788 end if;
3789
3790 -- Mark the enclosing scope and its associated construct as being task
3791 -- masters.
3792
3793 Set_Has_Master_Entity (Context_Id);
3794
3795 while Present (Context)
3796 and then Nkind (Context) /= N_Compilation_Unit
3797 loop
3798 if Nkind_In (Context, N_Block_Statement,
3799 N_Subprogram_Body,
3800 N_Task_Body)
3801 then
3802 Set_Is_Task_Master (Context);
3803 exit;
3804
3805 elsif Nkind (Parent (Context)) = N_Subunit then
3806 Context := Corresponding_Stub (Parent (Context));
3807 end if;
3808
3809 Context := Parent (Context);
3810 end loop;
3811 end Build_Master_Entity;
3812
3813 ---------------------------
3814 -- Build_Master_Renaming --
3815 ---------------------------
3816
3817 procedure Build_Master_Renaming
3818 (Ptr_Typ : Entity_Id;
3819 Ins_Nod : Node_Id := Empty)
3820 is
3821 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3822 Context : Node_Id;
3823 Master_Decl : Node_Id;
3824 Master_Id : Entity_Id;
3825
3826 begin
3827 -- Nothing to do if there is no task hierarchy
3828
3829 if Restriction_Active (No_Task_Hierarchy) then
3830 return;
3831 end if;
3832
3833 -- Determine the proper context to insert the master renaming
3834
3835 if Present (Ins_Nod) then
3836 Context := Ins_Nod;
3837 elsif Is_Itype (Ptr_Typ) then
3838 Context := Associated_Node_For_Itype (Ptr_Typ);
3839 else
3840 Context := Parent (Ptr_Typ);
3841 end if;
3842
3843 -- Generate:
3844 -- <Ptr_Typ>M : Master_Id renames _Master;
3845
3846 Master_Id :=
3847 Make_Defining_Identifier (Loc,
3848 New_External_Name (Chars (Ptr_Typ), 'M'));
3849
3850 Master_Decl :=
3851 Make_Object_Renaming_Declaration (Loc,
3852 Defining_Identifier => Master_Id,
3853 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3854 Name => Make_Identifier (Loc, Name_uMaster));
3855
3856 Insert_Action (Context, Master_Decl);
3857
3858 -- The renamed master now services the access type
3859
3860 Set_Master_Id (Ptr_Typ, Master_Id);
3861 end Build_Master_Renaming;
3862
3863 -----------------------------------------
3864 -- Build_Private_Protected_Declaration --
3865 -----------------------------------------
3866
3867 function Build_Private_Protected_Declaration
3868 (N : Node_Id) return Entity_Id
3869 is
3870 Loc : constant Source_Ptr := Sloc (N);
3871 Body_Id : constant Entity_Id := Defining_Entity (N);
3872 Decl : Node_Id;
3873 Plist : List_Id;
3874 Formal : Entity_Id;
3875 New_Spec : Node_Id;
3876 Spec_Id : Entity_Id;
3877
3878 begin
3879 Formal := First_Formal (Body_Id);
3880
3881 -- The protected operation always has at least one formal, namely the
3882 -- object itself, but it is only placed in the parameter list if
3883 -- expansion is enabled.
3884
3885 if Present (Formal) or else Expander_Active then
3886 Plist := Copy_Parameter_List (Body_Id);
3887 else
3888 Plist := No_List;
3889 end if;
3890
3891 if Nkind (Specification (N)) = N_Procedure_Specification then
3892 New_Spec :=
3893 Make_Procedure_Specification (Loc,
3894 Defining_Unit_Name =>
3895 Make_Defining_Identifier (Sloc (Body_Id),
3896 Chars => Chars (Body_Id)),
3897 Parameter_Specifications =>
3898 Plist);
3899 else
3900 New_Spec :=
3901 Make_Function_Specification (Loc,
3902 Defining_Unit_Name =>
3903 Make_Defining_Identifier (Sloc (Body_Id),
3904 Chars => Chars (Body_Id)),
3905 Parameter_Specifications => Plist,
3906 Result_Definition =>
3907 New_Occurrence_Of (Etype (Body_Id), Loc));
3908 end if;
3909
3910 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3911 Insert_Before (N, Decl);
3912 Spec_Id := Defining_Unit_Name (New_Spec);
3913
3914 -- Indicate that the entity comes from source, to ensure that cross-
3915 -- reference information is properly generated. The body itself is
3916 -- rewritten during expansion, and the body entity will not appear in
3917 -- calls to the operation.
3918
3919 Set_Comes_From_Source (Spec_Id, True);
3920 Analyze (Decl);
3921 Set_Has_Completion (Spec_Id);
3922 Set_Convention (Spec_Id, Convention_Protected);
3923 return Spec_Id;
3924 end Build_Private_Protected_Declaration;
3925
3926 ---------------------------
3927 -- Build_Protected_Entry --
3928 ---------------------------
3929
3930 function Build_Protected_Entry
3931 (N : Node_Id;
3932 Ent : Entity_Id;
3933 Pid : Node_Id) return Node_Id
3934 is
3935 Bod_Decls : constant List_Id := New_List;
3936 Decls : constant List_Id := Declarations (N);
3937 End_Lab : constant Node_Id :=
3938 End_Label (Handled_Statement_Sequence (N));
3939 End_Loc : constant Source_Ptr :=
3940 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3941 -- Used for the generated call to Complete_Entry_Body
3942
3943 Loc : constant Source_Ptr := Sloc (N);
3944
3945 Bod_Id : Entity_Id;
3946 Bod_Spec : Node_Id;
3947 Bod_Stmts : List_Id;
3948 Complete : Node_Id;
3949 Ohandle : Node_Id;
3950
3951 EH_Loc : Source_Ptr;
3952 -- Used for the exception handler, inserted at end of the body
3953
3954 begin
3955 -- Set the source location on the exception handler only when debugging
3956 -- the expanded code (see Make_Implicit_Exception_Handler).
3957
3958 if Debug_Generated_Code then
3959 EH_Loc := End_Loc;
3960
3961 -- Otherwise the inserted code should not be visible to the debugger
3962
3963 else
3964 EH_Loc := No_Location;
3965 end if;
3966
3967 Bod_Id :=
3968 Make_Defining_Identifier (Loc,
3969 Chars => Chars (Protected_Body_Subprogram (Ent)));
3970 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3971
3972 -- Add the following declarations:
3973
3974 -- type poVP is access poV;
3975 -- _object : poVP := poVP (_O);
3976
3977 -- where _O is the formal parameter associated with the concurrent
3978 -- object. These declarations are needed for Complete_Entry_Body.
3979
3980 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3981
3982 -- Add renamings for all formals, the Protection object, discriminals,
3983 -- privals and the entry index constant for use by debugger.
3984
3985 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3986 Debug_Private_Data_Declarations (Decls);
3987
3988 -- Put the declarations and the statements from the entry
3989
3990 Bod_Stmts :=
3991 New_List (
3992 Make_Block_Statement (Loc,
3993 Declarations => Decls,
3994 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3995
3996 case Corresponding_Runtime_Package (Pid) is
3997 when System_Tasking_Protected_Objects_Entries =>
3998 Append_To (Bod_Stmts,
3999 Make_Procedure_Call_Statement (End_Loc,
4000 Name =>
4001 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
4002 Parameter_Associations => New_List (
4003 Make_Attribute_Reference (End_Loc,
4004 Prefix =>
4005 Make_Selected_Component (End_Loc,
4006 Prefix =>
4007 Make_Identifier (End_Loc, Name_uObject),
4008 Selector_Name =>
4009 Make_Identifier (End_Loc, Name_uObject)),
4010 Attribute_Name => Name_Unchecked_Access))));
4011
4012 when System_Tasking_Protected_Objects_Single_Entry =>
4013
4014 -- Historically, a call to Complete_Single_Entry_Body was
4015 -- inserted, but it was a null procedure.
4016
4017 null;
4018
4019 when others =>
4020 raise Program_Error;
4021 end case;
4022
4023 -- When exceptions can not be propagated, we never need to call
4024 -- Exception_Complete_Entry_Body.
4025
4026 if No_Exception_Handlers_Set then
4027 return
4028 Make_Subprogram_Body (Loc,
4029 Specification => Bod_Spec,
4030 Declarations => Bod_Decls,
4031 Handled_Statement_Sequence =>
4032 Make_Handled_Sequence_Of_Statements (Loc,
4033 Statements => Bod_Stmts,
4034 End_Label => End_Lab));
4035
4036 else
4037 Ohandle := Make_Others_Choice (Loc);
4038 Set_All_Others (Ohandle);
4039
4040 case Corresponding_Runtime_Package (Pid) is
4041 when System_Tasking_Protected_Objects_Entries =>
4042 Complete :=
4043 New_Occurrence_Of
4044 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
4045
4046 when System_Tasking_Protected_Objects_Single_Entry =>
4047 Complete :=
4048 New_Occurrence_Of
4049 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
4050
4051 when others =>
4052 raise Program_Error;
4053 end case;
4054
4055 -- Establish link between subprogram body entity and source entry
4056
4057 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
4058
4059 -- Create body of entry procedure. The renaming declarations are
4060 -- placed ahead of the block that contains the actual entry body.
4061
4062 return
4063 Make_Subprogram_Body (Loc,
4064 Specification => Bod_Spec,
4065 Declarations => Bod_Decls,
4066 Handled_Statement_Sequence =>
4067 Make_Handled_Sequence_Of_Statements (Loc,
4068 Statements => Bod_Stmts,
4069 End_Label => End_Lab,
4070 Exception_Handlers => New_List (
4071 Make_Implicit_Exception_Handler (EH_Loc,
4072 Exception_Choices => New_List (Ohandle),
4073
4074 Statements => New_List (
4075 Make_Procedure_Call_Statement (EH_Loc,
4076 Name => Complete,
4077 Parameter_Associations => New_List (
4078 Make_Attribute_Reference (EH_Loc,
4079 Prefix =>
4080 Make_Selected_Component (EH_Loc,
4081 Prefix =>
4082 Make_Identifier (EH_Loc, Name_uObject),
4083 Selector_Name =>
4084 Make_Identifier (EH_Loc, Name_uObject)),
4085 Attribute_Name => Name_Unchecked_Access),
4086
4087 Make_Function_Call (EH_Loc,
4088 Name =>
4089 New_Occurrence_Of
4090 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
4091 end if;
4092 end Build_Protected_Entry;
4093
4094 -----------------------------------------
4095 -- Build_Protected_Entry_Specification --
4096 -----------------------------------------
4097
4098 function Build_Protected_Entry_Specification
4099 (Loc : Source_Ptr;
4100 Def_Id : Entity_Id;
4101 Ent_Id : Entity_Id) return Node_Id
4102 is
4103 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
4104
4105 begin
4106 Set_Debug_Info_Needed (Def_Id);
4107
4108 if Present (Ent_Id) then
4109 Append_Elmt (P, Accept_Address (Ent_Id));
4110 end if;
4111
4112 return
4113 Make_Procedure_Specification (Loc,
4114 Defining_Unit_Name => Def_Id,
4115 Parameter_Specifications => New_List (
4116 Make_Parameter_Specification (Loc,
4117 Defining_Identifier =>
4118 Make_Defining_Identifier (Loc, Name_uO),
4119 Parameter_Type =>
4120 New_Occurrence_Of (RTE (RE_Address), Loc)),
4121
4122 Make_Parameter_Specification (Loc,
4123 Defining_Identifier => P,
4124 Parameter_Type =>
4125 New_Occurrence_Of (RTE (RE_Address), Loc)),
4126
4127 Make_Parameter_Specification (Loc,
4128 Defining_Identifier =>
4129 Make_Defining_Identifier (Loc, Name_uE),
4130 Parameter_Type =>
4131 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
4132 end Build_Protected_Entry_Specification;
4133
4134 --------------------------
4135 -- Build_Protected_Spec --
4136 --------------------------
4137
4138 function Build_Protected_Spec
4139 (N : Node_Id;
4140 Obj_Type : Entity_Id;
4141 Ident : Entity_Id;
4142 Unprotected : Boolean := False) return List_Id
4143 is
4144 Loc : constant Source_Ptr := Sloc (N);
4145 Decl : Node_Id;
4146 Formal : Entity_Id;
4147 New_Plist : List_Id;
4148 New_Param : Node_Id;
4149
4150 begin
4151 New_Plist := New_List;
4152
4153 Formal := First_Formal (Ident);
4154 while Present (Formal) loop
4155 New_Param :=
4156 Make_Parameter_Specification (Loc,
4157 Defining_Identifier =>
4158 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4159 Aliased_Present => Aliased_Present (Parent (Formal)),
4160 In_Present => In_Present (Parent (Formal)),
4161 Out_Present => Out_Present (Parent (Formal)),
4162 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
4163
4164 if Unprotected then
4165 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4166 end if;
4167
4168 Append (New_Param, New_Plist);
4169 Next_Formal (Formal);
4170 end loop;
4171
4172 -- If the subprogram is a procedure and the context is not an access
4173 -- to protected subprogram, the parameter is in-out. Otherwise it is
4174 -- an in parameter.
4175
4176 Decl :=
4177 Make_Parameter_Specification (Loc,
4178 Defining_Identifier =>
4179 Make_Defining_Identifier (Loc, Name_uObject),
4180 In_Present => True,
4181 Out_Present =>
4182 (Etype (Ident) = Standard_Void_Type
4183 and then not Is_RTE (Obj_Type, RE_Address)),
4184 Parameter_Type =>
4185 New_Occurrence_Of (Obj_Type, Loc));
4186 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4187 Prepend_To (New_Plist, Decl);
4188
4189 return New_Plist;
4190 end Build_Protected_Spec;
4191
4192 ---------------------------------------
4193 -- Build_Protected_Sub_Specification --
4194 ---------------------------------------
4195
4196 function Build_Protected_Sub_Specification
4197 (N : Node_Id;
4198 Prot_Typ : Entity_Id;
4199 Mode : Subprogram_Protection_Mode) return Node_Id
4200 is
4201 Loc : constant Source_Ptr := Sloc (N);
4202 Decl : Node_Id;
4203 Def_Id : Entity_Id;
4204 New_Id : Entity_Id;
4205 New_Plist : List_Id;
4206 New_Spec : Node_Id;
4207
4208 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4209 (Dispatching_Mode => ' ',
4210 Protected_Mode => 'P',
4211 Unprotected_Mode => 'N');
4212
4213 begin
4214 if Ekind (Defining_Unit_Name (Specification (N))) =
4215 E_Subprogram_Body
4216 then
4217 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4218 else
4219 Decl := N;
4220 end if;
4221
4222 Def_Id := Defining_Unit_Name (Specification (Decl));
4223
4224 New_Plist :=
4225 Build_Protected_Spec
4226 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4227 Mode = Unprotected_Mode);
4228 New_Id :=
4229 Make_Defining_Identifier (Loc,
4230 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4231
4232 -- The unprotected operation carries the user code, and debugging
4233 -- information must be generated for it, even though this spec does
4234 -- not come from source. It is also convenient to allow gdb to step
4235 -- into the protected operation, even though it only contains lock/
4236 -- unlock calls.
4237
4238 Set_Debug_Info_Needed (New_Id);
4239
4240 -- If a pragma Eliminate applies to the source entity, the internal
4241 -- subprograms will be eliminated as well.
4242
4243 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4244
4245 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4246 New_Spec :=
4247 Make_Procedure_Specification (Loc,
4248 Defining_Unit_Name => New_Id,
4249 Parameter_Specifications => New_Plist);
4250
4251 -- Create a new specification for the anonymous subprogram type
4252
4253 else
4254 New_Spec :=
4255 Make_Function_Specification (Loc,
4256 Defining_Unit_Name => New_Id,
4257 Parameter_Specifications => New_Plist,
4258 Result_Definition =>
4259 Copy_Result_Type (Result_Definition (Specification (Decl))));
4260
4261 Set_Return_Present (Defining_Unit_Name (New_Spec));
4262 end if;
4263
4264 return New_Spec;
4265 end Build_Protected_Sub_Specification;
4266
4267 -------------------------------------
4268 -- Build_Protected_Subprogram_Body --
4269 -------------------------------------
4270
4271 function Build_Protected_Subprogram_Body
4272 (N : Node_Id;
4273 Pid : Node_Id;
4274 N_Op_Spec : Node_Id) return Node_Id
4275 is
4276 Loc : constant Source_Ptr := Sloc (N);
4277 Op_Spec : Node_Id;
4278 P_Op_Spec : Node_Id;
4279 Uactuals : List_Id;
4280 Pformal : Node_Id;
4281 Unprot_Call : Node_Id;
4282 Sub_Body : Node_Id;
4283 Lock_Name : Node_Id;
4284 Lock_Stmt : Node_Id;
4285 R : Node_Id;
4286 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4287 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4288 Stmts : List_Id;
4289 Object_Parm : Node_Id;
4290 Exc_Safe : Boolean;
4291 Lock_Kind : RE_Id;
4292
4293 begin
4294 Op_Spec := Specification (N);
4295 Exc_Safe := Is_Exception_Safe (N);
4296
4297 P_Op_Spec :=
4298 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4299
4300 -- Build a list of the formal parameters of the protected version of
4301 -- the subprogram to use as the actual parameters of the unprotected
4302 -- version.
4303
4304 Uactuals := New_List;
4305 Pformal := First (Parameter_Specifications (P_Op_Spec));
4306 while Present (Pformal) loop
4307 Append_To (Uactuals,
4308 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4309 Next (Pformal);
4310 end loop;
4311
4312 -- Make a call to the unprotected version of the subprogram built above
4313 -- for use by the protected version built below.
4314
4315 if Nkind (Op_Spec) = N_Function_Specification then
4316 if Exc_Safe then
4317 R := Make_Temporary (Loc, 'R');
4318 Unprot_Call :=
4319 Make_Object_Declaration (Loc,
4320 Defining_Identifier => R,
4321 Constant_Present => True,
4322 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4323 Expression =>
4324 Make_Function_Call (Loc,
4325 Name => Make_Identifier (Loc,
4326 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4327 Parameter_Associations => Uactuals));
4328
4329 Return_Stmt :=
4330 Make_Simple_Return_Statement (Loc,
4331 Expression => New_Occurrence_Of (R, Loc));
4332
4333 else
4334 Unprot_Call := Make_Simple_Return_Statement (Loc,
4335 Expression => Make_Function_Call (Loc,
4336 Name =>
4337 Make_Identifier (Loc,
4338 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4339 Parameter_Associations => Uactuals));
4340 end if;
4341
4342 Lock_Kind := RE_Lock_Read_Only;
4343
4344 else
4345 Unprot_Call :=
4346 Make_Procedure_Call_Statement (Loc,
4347 Name =>
4348 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4349 Parameter_Associations => Uactuals);
4350
4351 Lock_Kind := RE_Lock;
4352 end if;
4353
4354 -- Wrap call in block that will be covered by an at_end handler
4355
4356 if not Exc_Safe then
4357 Unprot_Call := Make_Block_Statement (Loc,
4358 Handled_Statement_Sequence =>
4359 Make_Handled_Sequence_Of_Statements (Loc,
4360 Statements => New_List (Unprot_Call)));
4361 end if;
4362
4363 -- Make the protected subprogram body. This locks the protected
4364 -- object and calls the unprotected version of the subprogram.
4365
4366 case Corresponding_Runtime_Package (Pid) is
4367 when System_Tasking_Protected_Objects_Entries =>
4368 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
4369
4370 when System_Tasking_Protected_Objects_Single_Entry =>
4371 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4372
4373 when System_Tasking_Protected_Objects =>
4374 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4375
4376 when others =>
4377 raise Program_Error;
4378 end case;
4379
4380 Object_Parm :=
4381 Make_Attribute_Reference (Loc,
4382 Prefix =>
4383 Make_Selected_Component (Loc,
4384 Prefix => Make_Identifier (Loc, Name_uObject),
4385 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4386 Attribute_Name => Name_Unchecked_Access);
4387
4388 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4389 Name => Lock_Name,
4390 Parameter_Associations => New_List (Object_Parm));
4391
4392 if Abort_Allowed then
4393 Stmts := New_List (
4394 Make_Procedure_Call_Statement (Loc,
4395 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
4396 Parameter_Associations => Empty_List),
4397 Lock_Stmt);
4398
4399 else
4400 Stmts := New_List (Lock_Stmt);
4401 end if;
4402
4403 if not Exc_Safe then
4404 Append (Unprot_Call, Stmts);
4405 else
4406 if Nkind (Op_Spec) = N_Function_Specification then
4407 Pre_Stmts := Stmts;
4408 Stmts := Empty_List;
4409 else
4410 Append (Unprot_Call, Stmts);
4411 end if;
4412
4413 -- Historical note: Previously, call to the cleanup was inserted
4414 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4415 -- which is also shared by the 'not Exc_Safe' path.
4416
4417 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4418
4419 if Nkind (Op_Spec) = N_Function_Specification then
4420 Append (Return_Stmt, Stmts);
4421 Append (Make_Block_Statement (Loc,
4422 Declarations => New_List (Unprot_Call),
4423 Handled_Statement_Sequence =>
4424 Make_Handled_Sequence_Of_Statements (Loc,
4425 Statements => Stmts)), Pre_Stmts);
4426 Stmts := Pre_Stmts;
4427 end if;
4428 end if;
4429
4430 Sub_Body :=
4431 Make_Subprogram_Body (Loc,
4432 Declarations => Empty_List,
4433 Specification => P_Op_Spec,
4434 Handled_Statement_Sequence =>
4435 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4436
4437 -- Mark this subprogram as a protected subprogram body so that the
4438 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4439 -- path as otherwise the cleanup has already been inserted.
4440
4441 if not Exc_Safe then
4442 Set_Is_Protected_Subprogram_Body (Sub_Body);
4443 end if;
4444
4445 return Sub_Body;
4446 end Build_Protected_Subprogram_Body;
4447
4448 -------------------------------------
4449 -- Build_Protected_Subprogram_Call --
4450 -------------------------------------
4451
4452 procedure Build_Protected_Subprogram_Call
4453 (N : Node_Id;
4454 Name : Node_Id;
4455 Rec : Node_Id;
4456 External : Boolean := True)
4457 is
4458 Loc : constant Source_Ptr := Sloc (N);
4459 Sub : constant Entity_Id := Entity (Name);
4460 New_Sub : Node_Id;
4461 Params : List_Id;
4462
4463 begin
4464 if External then
4465 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4466 else
4467 New_Sub :=
4468 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4469 end if;
4470
4471 if Present (Parameter_Associations (N)) then
4472 Params := New_Copy_List_Tree (Parameter_Associations (N));
4473 else
4474 Params := New_List;
4475 end if;
4476
4477 -- If the type is an untagged derived type, convert to the root type,
4478 -- which is the one on which the operations are defined.
4479
4480 if Nkind (Rec) = N_Unchecked_Type_Conversion
4481 and then not Is_Tagged_Type (Etype (Rec))
4482 and then Is_Derived_Type (Etype (Rec))
4483 then
4484 Set_Etype (Rec, Root_Type (Etype (Rec)));
4485 Set_Subtype_Mark (Rec,
4486 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4487 end if;
4488
4489 Prepend (Rec, Params);
4490
4491 if Ekind (Sub) = E_Procedure then
4492 Rewrite (N,
4493 Make_Procedure_Call_Statement (Loc,
4494 Name => New_Sub,
4495 Parameter_Associations => Params));
4496
4497 else
4498 pragma Assert (Ekind (Sub) = E_Function);
4499 Rewrite (N,
4500 Make_Function_Call (Loc,
4501 Name => New_Sub,
4502 Parameter_Associations => Params));
4503
4504 -- Preserve type of call for subsequent processing (required for
4505 -- call to Wrap_Transient_Expression in the case of a shared passive
4506 -- protected).
4507
4508 Set_Etype (N, Etype (New_Sub));
4509 end if;
4510
4511 if External
4512 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4513 and then Is_Entity_Name (Expression (Rec))
4514 and then Is_Shared_Passive (Entity (Expression (Rec)))
4515 then
4516 Add_Shared_Var_Lock_Procs (N);
4517 end if;
4518 end Build_Protected_Subprogram_Call;
4519
4520 ---------------------------------------------
4521 -- Build_Protected_Subprogram_Call_Cleanup --
4522 ---------------------------------------------
4523
4524 procedure Build_Protected_Subprogram_Call_Cleanup
4525 (Op_Spec : Node_Id;
4526 Conc_Typ : Node_Id;
4527 Loc : Source_Ptr;
4528 Stmts : List_Id)
4529 is
4530 Nam : Node_Id;
4531
4532 begin
4533 -- If the associated protected object has entries, a protected
4534 -- procedure has to service entry queues. In this case generate:
4535
4536 -- Service_Entries (_object._object'Access);
4537
4538 if Nkind (Op_Spec) = N_Procedure_Specification
4539 and then Has_Entries (Conc_Typ)
4540 then
4541 case Corresponding_Runtime_Package (Conc_Typ) is
4542 when System_Tasking_Protected_Objects_Entries =>
4543 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4544
4545 when System_Tasking_Protected_Objects_Single_Entry =>
4546 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4547
4548 when others =>
4549 raise Program_Error;
4550 end case;
4551
4552 Append_To (Stmts,
4553 Make_Procedure_Call_Statement (Loc,
4554 Name => Nam,
4555 Parameter_Associations => New_List (
4556 Make_Attribute_Reference (Loc,
4557 Prefix =>
4558 Make_Selected_Component (Loc,
4559 Prefix => Make_Identifier (Loc, Name_uObject),
4560 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4561 Attribute_Name => Name_Unchecked_Access))));
4562
4563 else
4564 -- Generate:
4565 -- Unlock (_object._object'Access);
4566
4567 case Corresponding_Runtime_Package (Conc_Typ) is
4568 when System_Tasking_Protected_Objects_Entries =>
4569 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4570
4571 when System_Tasking_Protected_Objects_Single_Entry =>
4572 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4573
4574 when System_Tasking_Protected_Objects =>
4575 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4576
4577 when others =>
4578 raise Program_Error;
4579 end case;
4580
4581 Append_To (Stmts,
4582 Make_Procedure_Call_Statement (Loc,
4583 Name => Nam,
4584 Parameter_Associations => New_List (
4585 Make_Attribute_Reference (Loc,
4586 Prefix =>
4587 Make_Selected_Component (Loc,
4588 Prefix => Make_Identifier (Loc, Name_uObject),
4589 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4590 Attribute_Name => Name_Unchecked_Access))));
4591 end if;
4592
4593 -- Generate:
4594 -- Abort_Undefer;
4595
4596 if Abort_Allowed then
4597 Append_To (Stmts,
4598 Make_Procedure_Call_Statement (Loc,
4599 Name =>
4600 New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
4601 Parameter_Associations => Empty_List));
4602 end if;
4603 end Build_Protected_Subprogram_Call_Cleanup;
4604
4605 -------------------------
4606 -- Build_Selected_Name --
4607 -------------------------
4608
4609 function Build_Selected_Name
4610 (Prefix : Entity_Id;
4611 Selector : Entity_Id;
4612 Append_Char : Character := ' ') return Name_Id
4613 is
4614 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4615 Select_Len : Natural;
4616
4617 begin
4618 Get_Name_String (Chars (Selector));
4619 Select_Len := Name_Len;
4620 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4621 Get_Name_String (Chars (Prefix));
4622
4623 -- If scope is anonymous type, discard suffix to recover name of
4624 -- single protected object. Otherwise use protected type name.
4625
4626 if Name_Buffer (Name_Len) = 'T' then
4627 Name_Len := Name_Len - 1;
4628 end if;
4629
4630 Add_Str_To_Name_Buffer ("__");
4631 for J in 1 .. Select_Len loop
4632 Add_Char_To_Name_Buffer (Select_Buffer (J));
4633 end loop;
4634
4635 -- Now add the Append_Char if specified. The encoding to follow
4636 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4637 -- then the entity is associated to a protected type subprogram.
4638 -- Otherwise, it is a protected type entry. For each case, the
4639 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4640
4641 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4642
4643 if Append_Char /= ' ' then
4644 if Append_Char = 'P' or Append_Char = 'N' then
4645 Add_Char_To_Name_Buffer (Append_Char);
4646 return Name_Find;
4647 else
4648 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4649 return New_External_Name (Name_Find, ' ', -1);
4650 end if;
4651 else
4652 return Name_Find;
4653 end if;
4654 end Build_Selected_Name;
4655
4656 -----------------------------
4657 -- Build_Simple_Entry_Call --
4658 -----------------------------
4659
4660 -- A task entry call is converted to a call to Call_Simple
4661
4662 -- declare
4663 -- P : parms := (parm, parm, parm);
4664 -- begin
4665 -- Call_Simple (acceptor-task, entry-index, P'Address);
4666 -- parm := P.param;
4667 -- parm := P.param;
4668 -- ...
4669 -- end;
4670
4671 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4672 -- the parameters, and the constructed aggregate value contains either the
4673 -- parameters or, in the case of non-elementary types, references to these
4674 -- parameters. Then the address of this aggregate is passed to the runtime
4675 -- routine, along with the task id value and the task entry index value.
4676 -- Pnn is only required if parameters are present.
4677
4678 -- The assignments after the call are present only in the case of in-out
4679 -- or out parameters for elementary types, and are used to assign back the
4680 -- resulting values of such parameters.
4681
4682 -- Note: the reason that we insert a block here is that in the context
4683 -- of selects, conditional entry calls etc. the entry call statement
4684 -- appears on its own, not as an element of a list.
4685
4686 -- A protected entry call is converted to a Protected_Entry_Call:
4687
4688 -- declare
4689 -- P : E1_Params := (param, param, param);
4690 -- Pnn : Boolean;
4691 -- Bnn : Communications_Block;
4692
4693 -- declare
4694 -- P : E1_Params := (param, param, param);
4695 -- Bnn : Communications_Block;
4696
4697 -- begin
4698 -- Protected_Entry_Call (
4699 -- Object => po._object'Access,
4700 -- E => <entry index>;
4701 -- Uninterpreted_Data => P'Address;
4702 -- Mode => Simple_Call;
4703 -- Block => Bnn);
4704 -- parm := P.param;
4705 -- parm := P.param;
4706 -- ...
4707 -- end;
4708
4709 procedure Build_Simple_Entry_Call
4710 (N : Node_Id;
4711 Concval : Node_Id;
4712 Ename : Node_Id;
4713 Index : Node_Id)
4714 is
4715 begin
4716 Expand_Call (N);
4717
4718 -- If call has been inlined, nothing left to do
4719
4720 if Nkind (N) = N_Block_Statement then
4721 return;
4722 end if;
4723
4724 -- Convert entry call to Call_Simple call
4725
4726 declare
4727 Loc : constant Source_Ptr := Sloc (N);
4728 Parms : constant List_Id := Parameter_Associations (N);
4729 Stats : constant List_Id := New_List;
4730 Actual : Node_Id;
4731 Call : Node_Id;
4732 Comm_Name : Entity_Id;
4733 Conctyp : Node_Id;
4734 Decls : List_Id;
4735 Ent : Entity_Id;
4736 Ent_Acc : Entity_Id;
4737 Formal : Node_Id;
4738 Iface_Tag : Entity_Id;
4739 Iface_Typ : Entity_Id;
4740 N_Node : Node_Id;
4741 N_Var : Node_Id;
4742 P : Entity_Id;
4743 Parm1 : Node_Id;
4744 Parm2 : Node_Id;
4745 Parm3 : Node_Id;
4746 Pdecl : Node_Id;
4747 Plist : List_Id;
4748 X : Entity_Id;
4749 Xdecl : Node_Id;
4750
4751 begin
4752 -- Simple entry and entry family cases merge here
4753
4754 Ent := Entity (Ename);
4755 Ent_Acc := Entry_Parameters_Type (Ent);
4756 Conctyp := Etype (Concval);
4757
4758 -- If prefix is an access type, dereference to obtain the task type
4759
4760 if Is_Access_Type (Conctyp) then
4761 Conctyp := Designated_Type (Conctyp);
4762 end if;
4763
4764 -- Special case for protected subprogram calls
4765
4766 if Is_Protected_Type (Conctyp)
4767 and then Is_Subprogram (Entity (Ename))
4768 then
4769 if not Is_Eliminated (Entity (Ename)) then
4770 Build_Protected_Subprogram_Call
4771 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4772 Analyze (N);
4773 end if;
4774
4775 return;
4776 end if;
4777
4778 -- First parameter is the Task_Id value from the task value or the
4779 -- Object from the protected object value, obtained by selecting
4780 -- the _Task_Id or _Object from the result of doing an unchecked
4781 -- conversion to convert the value to the corresponding record type.
4782
4783 if Nkind (Concval) = N_Function_Call
4784 and then Is_Task_Type (Conctyp)
4785 and then Ada_Version >= Ada_2005
4786 then
4787 declare
4788 ExpR : constant Node_Id := Relocate_Node (Concval);
4789 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4790 Decl : Node_Id;
4791
4792 begin
4793 Decl :=
4794 Make_Object_Declaration (Loc,
4795 Defining_Identifier => Obj,
4796 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4797 Expression => ExpR);
4798 Set_Etype (Obj, Conctyp);
4799 Decls := New_List (Decl);
4800 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4801 end;
4802
4803 else
4804 Decls := New_List;
4805 end if;
4806
4807 Parm1 := Concurrent_Ref (Concval);
4808
4809 -- Second parameter is the entry index, computed by the routine
4810 -- provided for this purpose. The value of this expression is
4811 -- assigned to an intermediate variable to assure that any entry
4812 -- family index expressions are evaluated before the entry
4813 -- parameters.
4814
4815 if not Is_Protected_Type (Conctyp)
4816 or else
4817 Corresponding_Runtime_Package (Conctyp) =
4818 System_Tasking_Protected_Objects_Entries
4819 then
4820 X := Make_Defining_Identifier (Loc, Name_uX);
4821
4822 Xdecl :=
4823 Make_Object_Declaration (Loc,
4824 Defining_Identifier => X,
4825 Object_Definition =>
4826 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4827 Expression => Actual_Index_Expression (
4828 Loc, Entity (Ename), Index, Concval));
4829
4830 Append_To (Decls, Xdecl);
4831 Parm2 := New_Occurrence_Of (X, Loc);
4832
4833 else
4834 Xdecl := Empty;
4835 Parm2 := Empty;
4836 end if;
4837
4838 -- The third parameter is the packaged parameters. If there are
4839 -- none, then it is just the null address, since nothing is passed.
4840
4841 if No (Parms) then
4842 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4843 P := Empty;
4844
4845 -- Case of parameters present, where third argument is the address
4846 -- of a packaged record containing the required parameter values.
4847
4848 else
4849 -- First build a list of parameter values, which are references to
4850 -- objects of the parameter types.
4851
4852 Plist := New_List;
4853
4854 Actual := First_Actual (N);
4855 Formal := First_Formal (Ent);
4856 while Present (Actual) loop
4857
4858 -- If it is a by-copy type, copy it to a new variable. The
4859 -- packaged record has a field that points to this variable.
4860
4861 if Is_By_Copy_Type (Etype (Actual)) then
4862 N_Node :=
4863 Make_Object_Declaration (Loc,
4864 Defining_Identifier => Make_Temporary (Loc, 'J'),
4865 Aliased_Present => True,
4866 Object_Definition =>
4867 New_Occurrence_Of (Etype (Formal), Loc));
4868
4869 -- Mark the object as not needing initialization since the
4870 -- initialization is performed separately, avoiding errors
4871 -- on cases such as formals of null-excluding access types.
4872
4873 Set_No_Initialization (N_Node);
4874
4875 -- We must make a separate assignment statement for the
4876 -- case of limited types. We cannot assign it unless the
4877 -- Assignment_OK flag is set first. An out formal of an
4878 -- access type or whose type has a Default_Value must also
4879 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4880 -- but no constraint, predicate, or null-exclusion check is
4881 -- applied before the call.
4882
4883 if Ekind (Formal) /= E_Out_Parameter
4884 or else Is_Access_Type (Etype (Formal))
4885 or else
4886 (Is_Scalar_Type (Etype (Formal))
4887 and then
4888 Present (Default_Aspect_Value (Etype (Formal))))
4889 then
4890 N_Var :=
4891 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4892 Set_Assignment_OK (N_Var);
4893 Append_To (Stats,
4894 Make_Assignment_Statement (Loc,
4895 Name => N_Var,
4896 Expression => Relocate_Node (Actual)));
4897
4898 -- Mark the object as internal, so we don't later reset
4899 -- No_Initialization flag in Default_Initialize_Object,
4900 -- which would lead to needless default initialization.
4901 -- We don't set this outside the if statement, because
4902 -- out scalar parameters without Default_Value do require
4903 -- default initialization if Initialize_Scalars applies.
4904
4905 Set_Is_Internal (Defining_Identifier (N_Node));
4906
4907 -- If actual is an out parameter of a null-excluding
4908 -- access type, there is access check on entry, so set
4909 -- Suppress_Assignment_Checks on the generated statement
4910 -- that assigns the actual to the parameter block
4911
4912 Set_Suppress_Assignment_Checks (Last (Stats));
4913 end if;
4914
4915 Append (N_Node, Decls);
4916
4917 Append_To (Plist,
4918 Make_Attribute_Reference (Loc,
4919 Attribute_Name => Name_Unchecked_Access,
4920 Prefix =>
4921 New_Occurrence_Of
4922 (Defining_Identifier (N_Node), Loc)));
4923
4924 else
4925 -- Interface class-wide formal
4926
4927 if Ada_Version >= Ada_2005
4928 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4929 and then Is_Interface (Etype (Formal))
4930 then
4931 Iface_Typ := Etype (Etype (Formal));
4932
4933 -- Generate:
4934 -- formal_iface_type! (actual.iface_tag)'reference
4935
4936 Iface_Tag :=
4937 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4938 pragma Assert (Present (Iface_Tag));
4939
4940 Append_To (Plist,
4941 Make_Reference (Loc,
4942 Unchecked_Convert_To (Iface_Typ,
4943 Make_Selected_Component (Loc,
4944 Prefix =>
4945 Relocate_Node (Actual),
4946 Selector_Name =>
4947 New_Occurrence_Of (Iface_Tag, Loc)))));
4948 else
4949 -- Generate:
4950 -- actual'reference
4951
4952 Append_To (Plist,
4953 Make_Reference (Loc, Relocate_Node (Actual)));
4954 end if;
4955 end if;
4956
4957 Next_Actual (Actual);
4958 Next_Formal_With_Extras (Formal);
4959 end loop;
4960
4961 -- Now build the declaration of parameters initialized with the
4962 -- aggregate containing this constructed parameter list.
4963
4964 P := Make_Defining_Identifier (Loc, Name_uP);
4965
4966 Pdecl :=
4967 Make_Object_Declaration (Loc,
4968 Defining_Identifier => P,
4969 Object_Definition =>
4970 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4971 Expression =>
4972 Make_Aggregate (Loc, Expressions => Plist));
4973
4974 Parm3 :=
4975 Make_Attribute_Reference (Loc,
4976 Prefix => New_Occurrence_Of (P, Loc),
4977 Attribute_Name => Name_Address);
4978
4979 Append (Pdecl, Decls);
4980 end if;
4981
4982 -- Now we can create the call, case of protected type
4983
4984 if Is_Protected_Type (Conctyp) then
4985 case Corresponding_Runtime_Package (Conctyp) is
4986 when System_Tasking_Protected_Objects_Entries =>
4987
4988 -- Change the type of the index declaration
4989
4990 Set_Object_Definition (Xdecl,
4991 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4992
4993 -- Some additional declarations for protected entry calls
4994
4995 if No (Decls) then
4996 Decls := New_List;
4997 end if;
4998
4999 -- Bnn : Communications_Block;
5000
5001 Comm_Name := Make_Temporary (Loc, 'B');
5002
5003 Append_To (Decls,
5004 Make_Object_Declaration (Loc,
5005 Defining_Identifier => Comm_Name,
5006 Object_Definition =>
5007 New_Occurrence_Of
5008 (RTE (RE_Communication_Block), Loc)));
5009
5010 -- Some additional statements for protected entry calls
5011
5012 -- Protected_Entry_Call (
5013 -- Object => po._object'Access,
5014 -- E => <entry index>;
5015 -- Uninterpreted_Data => P'Address;
5016 -- Mode => Simple_Call;
5017 -- Block => Bnn);
5018
5019 Call :=
5020 Make_Procedure_Call_Statement (Loc,
5021 Name =>
5022 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
5023
5024 Parameter_Associations => New_List (
5025 Make_Attribute_Reference (Loc,
5026 Attribute_Name => Name_Unchecked_Access,
5027 Prefix => Parm1),
5028 Parm2,
5029 Parm3,
5030 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
5031 New_Occurrence_Of (Comm_Name, Loc)));
5032
5033 when System_Tasking_Protected_Objects_Single_Entry =>
5034 -- Protected_Single_Entry_Call (
5035 -- Object => po._object'Access,
5036 -- Uninterpreted_Data => P'Address);
5037
5038 Call :=
5039 Make_Procedure_Call_Statement (Loc,
5040 Name =>
5041 New_Occurrence_Of
5042 (RTE (RE_Protected_Single_Entry_Call), Loc),
5043
5044 Parameter_Associations => New_List (
5045 Make_Attribute_Reference (Loc,
5046 Attribute_Name => Name_Unchecked_Access,
5047 Prefix => Parm1),
5048 Parm3));
5049
5050 when others =>
5051 raise Program_Error;
5052 end case;
5053
5054 -- Case of task type
5055
5056 else
5057 Call :=
5058 Make_Procedure_Call_Statement (Loc,
5059 Name =>
5060 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
5061 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
5062
5063 end if;
5064
5065 Append_To (Stats, Call);
5066
5067 -- If there are out or in/out parameters by copy add assignment
5068 -- statements for the result values.
5069
5070 if Present (Parms) then
5071 Actual := First_Actual (N);
5072 Formal := First_Formal (Ent);
5073
5074 Set_Assignment_OK (Actual);
5075 while Present (Actual) loop
5076 if Is_By_Copy_Type (Etype (Actual))
5077 and then Ekind (Formal) /= E_In_Parameter
5078 then
5079 N_Node :=
5080 Make_Assignment_Statement (Loc,
5081 Name => New_Copy (Actual),
5082 Expression =>
5083 Make_Explicit_Dereference (Loc,
5084 Make_Selected_Component (Loc,
5085 Prefix => New_Occurrence_Of (P, Loc),
5086 Selector_Name =>
5087 Make_Identifier (Loc, Chars (Formal)))));
5088
5089 -- In all cases (including limited private types) we want
5090 -- the assignment to be valid.
5091
5092 Set_Assignment_OK (Name (N_Node));
5093
5094 -- If the call is the triggering alternative in an
5095 -- asynchronous select, or the entry_call alternative of a
5096 -- conditional entry call, the assignments for in-out
5097 -- parameters are incorporated into the statement list that
5098 -- follows, so that there are executed only if the entry
5099 -- call succeeds.
5100
5101 if (Nkind (Parent (N)) = N_Triggering_Alternative
5102 and then N = Triggering_Statement (Parent (N)))
5103 or else
5104 (Nkind (Parent (N)) = N_Entry_Call_Alternative
5105 and then N = Entry_Call_Statement (Parent (N)))
5106 then
5107 if No (Statements (Parent (N))) then
5108 Set_Statements (Parent (N), New_List);
5109 end if;
5110
5111 Prepend (N_Node, Statements (Parent (N)));
5112
5113 else
5114 Insert_After (Call, N_Node);
5115 end if;
5116 end if;
5117
5118 Next_Actual (Actual);
5119 Next_Formal_With_Extras (Formal);
5120 end loop;
5121 end if;
5122
5123 -- Finally, create block and analyze it
5124
5125 Rewrite (N,
5126 Make_Block_Statement (Loc,
5127 Declarations => Decls,
5128 Handled_Statement_Sequence =>
5129 Make_Handled_Sequence_Of_Statements (Loc,
5130 Statements => Stats)));
5131
5132 Analyze (N);
5133 end;
5134 end Build_Simple_Entry_Call;
5135
5136 --------------------------------
5137 -- Build_Task_Activation_Call --
5138 --------------------------------
5139
5140 procedure Build_Task_Activation_Call (N : Node_Id) is
5141 Loc : constant Source_Ptr := Sloc (N);
5142 Chain : Entity_Id;
5143 Call : Node_Id;
5144 Name : Node_Id;
5145 P : Node_Id;
5146
5147 begin
5148 -- For sequential elaboration policy, all the tasks will be activated at
5149 -- the end of the elaboration.
5150
5151 if Partition_Elaboration_Policy = 'S' then
5152 return;
5153 end if;
5154
5155 -- Get the activation chain entity. Except in the case of a package
5156 -- body, this is in the node that was passed. For a package body, we
5157 -- have to find the corresponding package declaration node.
5158
5159 if Nkind (N) = N_Package_Body then
5160 P := Corresponding_Spec (N);
5161 loop
5162 P := Parent (P);
5163 exit when Nkind (P) = N_Package_Declaration;
5164 end loop;
5165
5166 Chain := Activation_Chain_Entity (P);
5167
5168 else
5169 Chain := Activation_Chain_Entity (N);
5170 end if;
5171
5172 if Present (Chain) then
5173 if Restricted_Profile then
5174 Name := New_Occurrence_Of
5175 (RTE (RE_Activate_Restricted_Tasks), Loc);
5176 else
5177 Name := New_Occurrence_Of
5178 (RTE (RE_Activate_Tasks), Loc);
5179 end if;
5180
5181 Call :=
5182 Make_Procedure_Call_Statement (Loc,
5183 Name => Name,
5184 Parameter_Associations =>
5185 New_List (Make_Attribute_Reference (Loc,
5186 Prefix => New_Occurrence_Of (Chain, Loc),
5187 Attribute_Name => Name_Unchecked_Access)));
5188
5189 if Nkind (N) = N_Package_Declaration then
5190 if Present (Corresponding_Body (N)) then
5191 null;
5192
5193 elsif Present (Private_Declarations (Specification (N))) then
5194 Append (Call, Private_Declarations (Specification (N)));
5195
5196 else
5197 Append (Call, Visible_Declarations (Specification (N)));
5198 end if;
5199
5200 else
5201 if Present (Handled_Statement_Sequence (N)) then
5202
5203 -- The call goes at the start of the statement sequence after
5204 -- the start of exception range label if one is present.
5205
5206 declare
5207 Stm : Node_Id;
5208
5209 begin
5210 Stm := First (Statements (Handled_Statement_Sequence (N)));
5211
5212 -- A special case, skip exception range label if one is
5213 -- present (from front end zcx processing).
5214
5215 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
5216 Next (Stm);
5217 end if;
5218
5219 -- Another special case, if the first statement is a block
5220 -- from optimization of a local raise to a goto, then the
5221 -- call goes inside this block.
5222
5223 if Nkind (Stm) = N_Block_Statement
5224 and then Exception_Junk (Stm)
5225 then
5226 Stm :=
5227 First (Statements (Handled_Statement_Sequence (Stm)));
5228 end if;
5229
5230 -- Insertion point is after any exception label pushes,
5231 -- since we want it covered by any local handlers.
5232
5233 while Nkind (Stm) in N_Push_xxx_Label loop
5234 Next (Stm);
5235 end loop;
5236
5237 -- Now we have the proper insertion point
5238
5239 Insert_Before (Stm, Call);
5240 end;
5241
5242 else
5243 Set_Handled_Statement_Sequence (N,
5244 Make_Handled_Sequence_Of_Statements (Loc,
5245 Statements => New_List (Call)));
5246 end if;
5247 end if;
5248
5249 Analyze (Call);
5250 Check_Task_Activation (N);
5251 end if;
5252 end Build_Task_Activation_Call;
5253
5254 -------------------------------
5255 -- Build_Task_Allocate_Block --
5256 -------------------------------
5257
5258 procedure Build_Task_Allocate_Block
5259 (Actions : List_Id;
5260 N : Node_Id;
5261 Args : List_Id)
5262 is
5263 T : constant Entity_Id := Entity (Expression (N));
5264 Init : constant Entity_Id := Base_Init_Proc (T);
5265 Loc : constant Source_Ptr := Sloc (N);
5266 Chain : constant Entity_Id :=
5267 Make_Defining_Identifier (Loc, Name_uChain);
5268 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5269 Block : Node_Id;
5270
5271 begin
5272 Block :=
5273 Make_Block_Statement (Loc,
5274 Identifier => New_Occurrence_Of (Blkent, Loc),
5275 Declarations => New_List (
5276
5277 -- _Chain : Activation_Chain;
5278
5279 Make_Object_Declaration (Loc,
5280 Defining_Identifier => Chain,
5281 Aliased_Present => True,
5282 Object_Definition =>
5283 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5284
5285 Handled_Statement_Sequence =>
5286 Make_Handled_Sequence_Of_Statements (Loc,
5287
5288 Statements => New_List (
5289
5290 -- Init (Args);
5291
5292 Make_Procedure_Call_Statement (Loc,
5293 Name => New_Occurrence_Of (Init, Loc),
5294 Parameter_Associations => Args),
5295
5296 -- Activate_Tasks (_Chain);
5297
5298 Make_Procedure_Call_Statement (Loc,
5299 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5300 Parameter_Associations => New_List (
5301 Make_Attribute_Reference (Loc,
5302 Prefix => New_Occurrence_Of (Chain, Loc),
5303 Attribute_Name => Name_Unchecked_Access))))),
5304
5305 Has_Created_Identifier => True,
5306 Is_Task_Allocation_Block => True);
5307
5308 Append_To (Actions,
5309 Make_Implicit_Label_Declaration (Loc,
5310 Defining_Identifier => Blkent,
5311 Label_Construct => Block));
5312
5313 Append_To (Actions, Block);
5314
5315 Set_Activation_Chain_Entity (Block, Chain);
5316 end Build_Task_Allocate_Block;
5317
5318 -----------------------------------------------
5319 -- Build_Task_Allocate_Block_With_Init_Stmts --
5320 -----------------------------------------------
5321
5322 procedure Build_Task_Allocate_Block_With_Init_Stmts
5323 (Actions : List_Id;
5324 N : Node_Id;
5325 Init_Stmts : List_Id)
5326 is
5327 Loc : constant Source_Ptr := Sloc (N);
5328 Chain : constant Entity_Id :=
5329 Make_Defining_Identifier (Loc, Name_uChain);
5330 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5331 Block : Node_Id;
5332
5333 begin
5334 Append_To (Init_Stmts,
5335 Make_Procedure_Call_Statement (Loc,
5336 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5337 Parameter_Associations => New_List (
5338 Make_Attribute_Reference (Loc,
5339 Prefix => New_Occurrence_Of (Chain, Loc),
5340 Attribute_Name => Name_Unchecked_Access))));
5341
5342 Block :=
5343 Make_Block_Statement (Loc,
5344 Identifier => New_Occurrence_Of (Blkent, Loc),
5345 Declarations => New_List (
5346
5347 -- _Chain : Activation_Chain;
5348
5349 Make_Object_Declaration (Loc,
5350 Defining_Identifier => Chain,
5351 Aliased_Present => True,
5352 Object_Definition =>
5353 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5354
5355 Handled_Statement_Sequence =>
5356 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5357
5358 Has_Created_Identifier => True,
5359 Is_Task_Allocation_Block => True);
5360
5361 Append_To (Actions,
5362 Make_Implicit_Label_Declaration (Loc,
5363 Defining_Identifier => Blkent,
5364 Label_Construct => Block));
5365
5366 Append_To (Actions, Block);
5367
5368 Set_Activation_Chain_Entity (Block, Chain);
5369 end Build_Task_Allocate_Block_With_Init_Stmts;
5370
5371 -----------------------------------
5372 -- Build_Task_Proc_Specification --
5373 -----------------------------------
5374
5375 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5376 Loc : constant Source_Ptr := Sloc (T);
5377 Spec_Id : Entity_Id;
5378
5379 begin
5380 -- Case of explicit task type, suffix TB
5381
5382 if Comes_From_Source (T) then
5383 Spec_Id :=
5384 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
5385
5386 -- Case of anonymous task type, suffix B
5387
5388 else
5389 Spec_Id :=
5390 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5391 end if;
5392
5393 Set_Is_Internal (Spec_Id);
5394
5395 -- Associate the procedure with the task, if this is the declaration
5396 -- (and not the body) of the procedure.
5397
5398 if No (Task_Body_Procedure (T)) then
5399 Set_Task_Body_Procedure (T, Spec_Id);
5400 end if;
5401
5402 return
5403 Make_Procedure_Specification (Loc,
5404 Defining_Unit_Name => Spec_Id,
5405 Parameter_Specifications => New_List (
5406 Make_Parameter_Specification (Loc,
5407 Defining_Identifier =>
5408 Make_Defining_Identifier (Loc, Name_uTask),
5409 Parameter_Type =>
5410 Make_Access_Definition (Loc,
5411 Subtype_Mark =>
5412 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5413 end Build_Task_Proc_Specification;
5414
5415 ---------------------------------------
5416 -- Build_Unprotected_Subprogram_Body --
5417 ---------------------------------------
5418
5419 function Build_Unprotected_Subprogram_Body
5420 (N : Node_Id;
5421 Pid : Node_Id) return Node_Id
5422 is
5423 Decls : constant List_Id := Declarations (N);
5424
5425 begin
5426 -- Add renamings for the Protection object, discriminals, privals, and
5427 -- the entry index constant for use by debugger.
5428
5429 Debug_Private_Data_Declarations (Decls);
5430
5431 -- Make an unprotected version of the subprogram for use within the same
5432 -- object, with a new name and an additional parameter representing the
5433 -- object.
5434
5435 return
5436 Make_Subprogram_Body (Sloc (N),
5437 Specification =>
5438 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5439 Declarations => Decls,
5440 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5441 end Build_Unprotected_Subprogram_Body;
5442
5443 ----------------------------
5444 -- Collect_Entry_Families --
5445 ----------------------------
5446
5447 procedure Collect_Entry_Families
5448 (Loc : Source_Ptr;
5449 Cdecls : List_Id;
5450 Current_Node : in out Node_Id;
5451 Conctyp : Entity_Id)
5452 is
5453 Efam : Entity_Id;
5454 Efam_Decl : Node_Id;
5455 Efam_Type : Entity_Id;
5456
5457 begin
5458 Efam := First_Entity (Conctyp);
5459 while Present (Efam) loop
5460 if Ekind (Efam) = E_Entry_Family then
5461 Efam_Type := Make_Temporary (Loc, 'F');
5462
5463 declare
5464 Bas : Entity_Id :=
5465 Base_Type
5466 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5467
5468 Bas_Decl : Node_Id := Empty;
5469 Lo, Hi : Node_Id;
5470
5471 begin
5472 Get_Index_Bounds
5473 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5474
5475 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5476 Bas := Make_Temporary (Loc, 'B');
5477
5478 Bas_Decl :=
5479 Make_Subtype_Declaration (Loc,
5480 Defining_Identifier => Bas,
5481 Subtype_Indication =>
5482 Make_Subtype_Indication (Loc,
5483 Subtype_Mark =>
5484 New_Occurrence_Of (Standard_Integer, Loc),
5485 Constraint =>
5486 Make_Range_Constraint (Loc,
5487 Range_Expression => Make_Range (Loc,
5488 Make_Integer_Literal
5489 (Loc, -Entry_Family_Bound),
5490 Make_Integer_Literal
5491 (Loc, Entry_Family_Bound - 1)))));
5492
5493 Insert_After (Current_Node, Bas_Decl);
5494 Current_Node := Bas_Decl;
5495 Analyze (Bas_Decl);
5496 end if;
5497
5498 Efam_Decl :=
5499 Make_Full_Type_Declaration (Loc,
5500 Defining_Identifier => Efam_Type,
5501 Type_Definition =>
5502 Make_Unconstrained_Array_Definition (Loc,
5503 Subtype_Marks =>
5504 (New_List (New_Occurrence_Of (Bas, Loc))),
5505
5506 Component_Definition =>
5507 Make_Component_Definition (Loc,
5508 Aliased_Present => False,
5509 Subtype_Indication =>
5510 New_Occurrence_Of (Standard_Character, Loc))));
5511 end;
5512
5513 Insert_After (Current_Node, Efam_Decl);
5514 Current_Node := Efam_Decl;
5515 Analyze (Efam_Decl);
5516
5517 Append_To (Cdecls,
5518 Make_Component_Declaration (Loc,
5519 Defining_Identifier =>
5520 Make_Defining_Identifier (Loc, Chars (Efam)),
5521
5522 Component_Definition =>
5523 Make_Component_Definition (Loc,
5524 Aliased_Present => False,
5525 Subtype_Indication =>
5526 Make_Subtype_Indication (Loc,
5527 Subtype_Mark =>
5528 New_Occurrence_Of (Efam_Type, Loc),
5529
5530 Constraint =>
5531 Make_Index_Or_Discriminant_Constraint (Loc,
5532 Constraints => New_List (
5533 New_Occurrence_Of
5534 (Etype (Discrete_Subtype_Definition
5535 (Parent (Efam))), Loc)))))));
5536
5537 end if;
5538
5539 Next_Entity (Efam);
5540 end loop;
5541 end Collect_Entry_Families;
5542
5543 -----------------------
5544 -- Concurrent_Object --
5545 -----------------------
5546
5547 function Concurrent_Object
5548 (Spec_Id : Entity_Id;
5549 Conc_Typ : Entity_Id) return Entity_Id
5550 is
5551 begin
5552 -- Parameter _O or _object
5553
5554 if Is_Protected_Type (Conc_Typ) then
5555 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5556
5557 -- Parameter _task
5558
5559 else
5560 pragma Assert (Is_Task_Type (Conc_Typ));
5561 return First_Formal (Task_Body_Procedure (Conc_Typ));
5562 end if;
5563 end Concurrent_Object;
5564
5565 ----------------------
5566 -- Copy_Result_Type --
5567 ----------------------
5568
5569 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5570 New_Res : constant Node_Id := New_Copy_Tree (Res);
5571 Par_Spec : Node_Id;
5572 Formal : Entity_Id;
5573
5574 begin
5575 -- If the result type is an access_to_subprogram, we must create new
5576 -- entities for its spec.
5577
5578 if Nkind (New_Res) = N_Access_Definition
5579 and then Present (Access_To_Subprogram_Definition (New_Res))
5580 then
5581 -- Provide new entities for the formals
5582
5583 Par_Spec := First (Parameter_Specifications
5584 (Access_To_Subprogram_Definition (New_Res)));
5585 while Present (Par_Spec) loop
5586 Formal := Defining_Identifier (Par_Spec);
5587 Set_Defining_Identifier (Par_Spec,
5588 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5589 Next (Par_Spec);
5590 end loop;
5591 end if;
5592
5593 return New_Res;
5594 end Copy_Result_Type;
5595
5596 --------------------
5597 -- Concurrent_Ref --
5598 --------------------
5599
5600 -- The expression returned for a reference to a concurrent object has the
5601 -- form:
5602
5603 -- taskV!(name)._Task_Id
5604
5605 -- for a task, and
5606
5607 -- objectV!(name)._Object
5608
5609 -- for a protected object. For the case of an access to a concurrent
5610 -- object, there is an extra explicit dereference:
5611
5612 -- taskV!(name.all)._Task_Id
5613 -- objectV!(name.all)._Object
5614
5615 -- here taskV and objectV are the types for the associated records, which
5616 -- contain the required _Task_Id and _Object fields for tasks and protected
5617 -- objects, respectively.
5618
5619 -- For the case of a task type name, the expression is
5620
5621 -- Self;
5622
5623 -- i.e. a call to the Self function which returns precisely this Task_Id
5624
5625 -- For the case of a protected type name, the expression is
5626
5627 -- objectR
5628
5629 -- which is a renaming of the _object field of the current object
5630 -- record, passed into protected operations as a parameter.
5631
5632 function Concurrent_Ref (N : Node_Id) return Node_Id is
5633 Loc : constant Source_Ptr := Sloc (N);
5634 Ntyp : constant Entity_Id := Etype (N);
5635 Dtyp : Entity_Id;
5636 Sel : Name_Id;
5637
5638 function Is_Current_Task (T : Entity_Id) return Boolean;
5639 -- Check whether the reference is to the immediately enclosing task
5640 -- type, or to an outer one (rare but legal).
5641
5642 ---------------------
5643 -- Is_Current_Task --
5644 ---------------------
5645
5646 function Is_Current_Task (T : Entity_Id) return Boolean is
5647 Scop : Entity_Id;
5648
5649 begin
5650 Scop := Current_Scope;
5651 while Present (Scop) and then Scop /= Standard_Standard loop
5652 if Scop = T then
5653 return True;
5654
5655 elsif Is_Task_Type (Scop) then
5656 return False;
5657
5658 -- If this is a procedure nested within the task type, we must
5659 -- assume that it can be called from an inner task, and therefore
5660 -- cannot treat it as a local reference.
5661
5662 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5663 return False;
5664
5665 else
5666 Scop := Scope (Scop);
5667 end if;
5668 end loop;
5669
5670 -- We know that we are within the task body, so should have found it
5671 -- in scope.
5672
5673 raise Program_Error;
5674 end Is_Current_Task;
5675
5676 -- Start of processing for Concurrent_Ref
5677
5678 begin
5679 if Is_Access_Type (Ntyp) then
5680 Dtyp := Designated_Type (Ntyp);
5681
5682 if Is_Protected_Type (Dtyp) then
5683 Sel := Name_uObject;
5684 else
5685 Sel := Name_uTask_Id;
5686 end if;
5687
5688 return
5689 Make_Selected_Component (Loc,
5690 Prefix =>
5691 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5692 Make_Explicit_Dereference (Loc, N)),
5693 Selector_Name => Make_Identifier (Loc, Sel));
5694
5695 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5696 if Is_Task_Type (Entity (N)) then
5697
5698 if Is_Current_Task (Entity (N)) then
5699 return
5700 Make_Function_Call (Loc,
5701 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5702
5703 else
5704 declare
5705 Decl : Node_Id;
5706 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5707 T_Body : constant Node_Id :=
5708 Parent (Corresponding_Body (Parent (Entity (N))));
5709
5710 begin
5711 Decl :=
5712 Make_Object_Declaration (Loc,
5713 Defining_Identifier => T_Self,
5714 Object_Definition =>
5715 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5716 Expression =>
5717 Make_Function_Call (Loc,
5718 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5719 Prepend (Decl, Declarations (T_Body));
5720 Analyze (Decl);
5721 Set_Scope (T_Self, Entity (N));
5722 return New_Occurrence_Of (T_Self, Loc);
5723 end;
5724 end if;
5725
5726 else
5727 pragma Assert (Is_Protected_Type (Entity (N)));
5728
5729 return
5730 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5731 end if;
5732
5733 else
5734 if Is_Protected_Type (Ntyp) then
5735 Sel := Name_uObject;
5736 elsif Is_Task_Type (Ntyp) then
5737 Sel := Name_uTask_Id;
5738 else
5739 raise Program_Error;
5740 end if;
5741
5742 return
5743 Make_Selected_Component (Loc,
5744 Prefix =>
5745 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5746 New_Copy_Tree (N)),
5747 Selector_Name => Make_Identifier (Loc, Sel));
5748 end if;
5749 end Concurrent_Ref;
5750
5751 ------------------------
5752 -- Convert_Concurrent --
5753 ------------------------
5754
5755 function Convert_Concurrent
5756 (N : Node_Id;
5757 Typ : Entity_Id) return Node_Id
5758 is
5759 begin
5760 if not Is_Concurrent_Type (Typ) then
5761 return N;
5762 else
5763 return
5764 Unchecked_Convert_To
5765 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5766 end if;
5767 end Convert_Concurrent;
5768
5769 -------------------------------------
5770 -- Debug_Private_Data_Declarations --
5771 -------------------------------------
5772
5773 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5774 Debug_Nod : Node_Id;
5775 Decl : Node_Id;
5776
5777 begin
5778 Decl := First (Decls);
5779 while Present (Decl) and then not Comes_From_Source (Decl) loop
5780
5781 -- Declaration for concurrent entity _object and its access type,
5782 -- along with the entry index subtype:
5783 -- type prot_typVP is access prot_typV;
5784 -- _object : prot_typVP := prot_typV (_O);
5785 -- subtype Jnn is <Type of Index> range Low .. High;
5786
5787 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5788 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5789
5790 -- Declaration for the Protection object, discriminals, privals, and
5791 -- entry index constant:
5792 -- conc_typR : protection_typ renames _object._object;
5793 -- discr_nameD : discr_typ renames _object.discr_name;
5794 -- discr_nameD : discr_typ renames _task.discr_name;
5795 -- prival_name : comp_typ renames _object.comp_name;
5796 -- J : constant Jnn :=
5797 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5798
5799 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5800 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5801 Debug_Nod := Debug_Renaming_Declaration (Decl);
5802
5803 if Present (Debug_Nod) then
5804 Insert_After (Decl, Debug_Nod);
5805 end if;
5806 end if;
5807
5808 Next (Decl);
5809 end loop;
5810 end Debug_Private_Data_Declarations;
5811
5812 ------------------------------
5813 -- Ensure_Statement_Present --
5814 ------------------------------
5815
5816 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5817 Stmt : Node_Id;
5818
5819 begin
5820 if Opt.Suppress_Control_Flow_Optimizations
5821 and then Is_Empty_List (Statements (Alt))
5822 then
5823 Stmt := Make_Null_Statement (Loc);
5824
5825 -- Mark NULL statement as coming from source so that it is not
5826 -- eliminated by GIGI.
5827
5828 -- Another covert channel. If this is a requirement, it must be
5829 -- documented in sinfo/einfo ???
5830
5831 Set_Comes_From_Source (Stmt, True);
5832
5833 Set_Statements (Alt, New_List (Stmt));
5834 end if;
5835 end Ensure_Statement_Present;
5836
5837 ----------------------------
5838 -- Entry_Index_Expression --
5839 ----------------------------
5840
5841 function Entry_Index_Expression
5842 (Sloc : Source_Ptr;
5843 Ent : Entity_Id;
5844 Index : Node_Id;
5845 Ttyp : Entity_Id) return Node_Id
5846 is
5847 Expr : Node_Id;
5848 Num : Node_Id;
5849 Lo : Node_Id;
5850 Hi : Node_Id;
5851 Prev : Entity_Id;
5852 S : Node_Id;
5853
5854 begin
5855 -- The queues of entries and entry families appear in textual order in
5856 -- the associated record. The entry index is computed as the sum of the
5857 -- number of queues for all entries that precede the designated one, to
5858 -- which is added the index expression, if this expression denotes a
5859 -- member of a family.
5860
5861 -- The following is a place holder for the count of simple entries
5862
5863 Num := Make_Integer_Literal (Sloc, 1);
5864
5865 -- We construct an expression which is a series of addition operations.
5866 -- The first operand is the number of single entries that precede this
5867 -- one, the second operand is the index value relative to the start of
5868 -- the referenced family, and the remaining operands are the lengths of
5869 -- the entry families that precede this entry, i.e. the constructed
5870 -- expression is:
5871
5872 -- number_simple_entries +
5873 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5874 -- family'length + ...
5875
5876 -- where index-value is the given index value, and s is the index
5877 -- subtype (we have to use pos because the subtype might be an
5878 -- enumeration type preventing direct subtraction). Note that the task
5879 -- entry array is one-indexed.
5880
5881 -- The upper bound of the entry family may be a discriminant, so we
5882 -- retrieve the lower bound explicitly to compute offset, rather than
5883 -- using the index subtype which may mention a discriminant.
5884
5885 if Present (Index) then
5886 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5887
5888 Expr :=
5889 Make_Op_Add (Sloc,
5890 Left_Opnd => Num,
5891 Right_Opnd =>
5892 Family_Offset
5893 (Sloc,
5894 Make_Attribute_Reference (Sloc,
5895 Attribute_Name => Name_Pos,
5896 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5897 Expressions => New_List (Relocate_Node (Index))),
5898 Type_Low_Bound (S),
5899 Ttyp,
5900 False));
5901 else
5902 Expr := Num;
5903 end if;
5904
5905 -- Now add lengths of preceding entries and entry families
5906
5907 Prev := First_Entity (Ttyp);
5908 while Chars (Prev) /= Chars (Ent)
5909 or else (Ekind (Prev) /= Ekind (Ent))
5910 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5911 loop
5912 if Ekind (Prev) = E_Entry then
5913 Set_Intval (Num, Intval (Num) + 1);
5914
5915 elsif Ekind (Prev) = E_Entry_Family then
5916 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5917 Lo := Type_Low_Bound (S);
5918 Hi := Type_High_Bound (S);
5919
5920 Expr :=
5921 Make_Op_Add (Sloc,
5922 Left_Opnd => Expr,
5923 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5924
5925 -- Other components are anonymous types to be ignored
5926
5927 else
5928 null;
5929 end if;
5930
5931 Next_Entity (Prev);
5932 end loop;
5933
5934 return Expr;
5935 end Entry_Index_Expression;
5936
5937 ---------------------------
5938 -- Establish_Task_Master --
5939 ---------------------------
5940
5941 procedure Establish_Task_Master (N : Node_Id) is
5942 Call : Node_Id;
5943
5944 begin
5945 if Restriction_Active (No_Task_Hierarchy) = False then
5946 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5947
5948 -- The block may have no declarations (and nevertheless be a task
5949 -- master) if it contains a call that may return an object that
5950 -- contains tasks.
5951
5952 if No (Declarations (N)) then
5953 Set_Declarations (N, New_List (Call));
5954 else
5955 Prepend_To (Declarations (N), Call);
5956 end if;
5957
5958 Analyze (Call);
5959 end if;
5960 end Establish_Task_Master;
5961
5962 --------------------------------
5963 -- Expand_Accept_Declarations --
5964 --------------------------------
5965
5966 -- Part of the expansion of an accept statement involves the creation of
5967 -- a declaration that can be referenced from the statement sequence of
5968 -- the accept:
5969
5970 -- Ann : Address;
5971
5972 -- This declaration is inserted immediately before the accept statement
5973 -- and it is important that it be inserted before the statements of the
5974 -- statement sequence are analyzed. Thus it would be too late to create
5975 -- this declaration in the Expand_N_Accept_Statement routine, which is
5976 -- why there is a separate procedure to be called directly from Sem_Ch9.
5977
5978 -- Ann is used to hold the address of the record containing the parameters
5979 -- (see Expand_N_Entry_Call for more details on how this record is built).
5980 -- References to the parameters do an unchecked conversion of this address
5981 -- to a pointer to the required record type, and then access the field that
5982 -- holds the value of the required parameter. The entity for the address
5983 -- variable is held as the top stack element (i.e. the last element) of the
5984 -- Accept_Address stack in the corresponding entry entity, and this element
5985 -- must be set in place before the statements are processed.
5986
5987 -- The above description applies to the case of a stand alone accept
5988 -- statement, i.e. one not appearing as part of a select alternative.
5989
5990 -- For the case of an accept that appears as part of a select alternative
5991 -- of a selective accept, we must still create the declaration right away,
5992 -- since Ann is needed immediately, but there is an important difference:
5993
5994 -- The declaration is inserted before the selective accept, not before
5995 -- the accept statement (which is not part of a list anyway, and so would
5996 -- not accommodate inserted declarations)
5997
5998 -- We only need one address variable for the entire selective accept. So
5999 -- the Ann declaration is created only for the first accept alternative,
6000 -- and subsequent accept alternatives reference the same Ann variable.
6001
6002 -- We can distinguish the two cases by seeing whether the accept statement
6003 -- is part of a list. If not, then it must be in an accept alternative.
6004
6005 -- To expand the requeue statement, a label is provided at the end of the
6006 -- accept statement or alternative of which it is a part, so that the
6007 -- statement can be skipped after the requeue is complete. This label is
6008 -- created here rather than during the expansion of the accept statement,
6009 -- because it will be needed by any requeue statements within the accept,
6010 -- which are expanded before the accept.
6011
6012 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
6013 Loc : constant Source_Ptr := Sloc (N);
6014 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6015 Ann : Entity_Id := Empty;
6016 Adecl : Node_Id;
6017 Lab : Node_Id;
6018 Ldecl : Node_Id;
6019 Ldecl2 : Node_Id;
6020
6021 begin
6022 if Expander_Active then
6023
6024 -- If we have no handled statement sequence, we may need to build
6025 -- a dummy sequence consisting of a null statement. This can be
6026 -- skipped if the trivial accept optimization is permitted.
6027
6028 if not Trivial_Accept_OK
6029 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6030 then
6031 Set_Handled_Statement_Sequence (N,
6032 Make_Handled_Sequence_Of_Statements (Loc,
6033 Statements => New_List (Make_Null_Statement (Loc))));
6034 end if;
6035
6036 -- Create and declare two labels to be placed at the end of the
6037 -- accept statement. The first label is used to allow requeues to
6038 -- skip the remainder of entry processing. The second label is used
6039 -- to skip the remainder of entry processing if the rendezvous
6040 -- completes in the middle of the accept body.
6041
6042 if Present (Handled_Statement_Sequence (N)) then
6043 declare
6044 Ent : Entity_Id;
6045
6046 begin
6047 Ent := Make_Temporary (Loc, 'L');
6048 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6049 Ldecl :=
6050 Make_Implicit_Label_Declaration (Loc,
6051 Defining_Identifier => Ent,
6052 Label_Construct => Lab);
6053 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6054
6055 Ent := Make_Temporary (Loc, 'L');
6056 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6057 Ldecl2 :=
6058 Make_Implicit_Label_Declaration (Loc,
6059 Defining_Identifier => Ent,
6060 Label_Construct => Lab);
6061 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6062 end;
6063
6064 else
6065 Ldecl := Empty;
6066 Ldecl2 := Empty;
6067 end if;
6068
6069 -- Case of stand alone accept statement
6070
6071 if Is_List_Member (N) then
6072
6073 if Present (Handled_Statement_Sequence (N)) then
6074 Ann := Make_Temporary (Loc, 'A');
6075
6076 Adecl :=
6077 Make_Object_Declaration (Loc,
6078 Defining_Identifier => Ann,
6079 Object_Definition =>
6080 New_Occurrence_Of (RTE (RE_Address), Loc));
6081
6082 Insert_Before_And_Analyze (N, Adecl);
6083 Insert_Before_And_Analyze (N, Ldecl);
6084 Insert_Before_And_Analyze (N, Ldecl2);
6085 end if;
6086
6087 -- Case of accept statement which is in an accept alternative
6088
6089 else
6090 declare
6091 Acc_Alt : constant Node_Id := Parent (N);
6092 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
6093 Alt : Node_Id;
6094
6095 begin
6096 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
6097 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
6098
6099 -- ??? Consider a single label for select statements
6100
6101 if Present (Handled_Statement_Sequence (N)) then
6102 Prepend (Ldecl2,
6103 Statements (Handled_Statement_Sequence (N)));
6104 Analyze (Ldecl2);
6105
6106 Prepend (Ldecl,
6107 Statements (Handled_Statement_Sequence (N)));
6108 Analyze (Ldecl);
6109 end if;
6110
6111 -- Find first accept alternative of the selective accept. A
6112 -- valid selective accept must have at least one accept in it.
6113
6114 Alt := First (Select_Alternatives (Sel_Acc));
6115
6116 while Nkind (Alt) /= N_Accept_Alternative loop
6117 Next (Alt);
6118 end loop;
6119
6120 -- If this is the first accept statement, then we have to
6121 -- create the Ann variable, as for the stand alone case, except
6122 -- that it is inserted before the selective accept. Similarly,
6123 -- a label for requeue expansion must be declared.
6124
6125 if N = Accept_Statement (Alt) then
6126 Ann := Make_Temporary (Loc, 'A');
6127 Adecl :=
6128 Make_Object_Declaration (Loc,
6129 Defining_Identifier => Ann,
6130 Object_Definition =>
6131 New_Occurrence_Of (RTE (RE_Address), Loc));
6132
6133 Insert_Before_And_Analyze (Sel_Acc, Adecl);
6134
6135 -- If this is not the first accept statement, then find the Ann
6136 -- variable allocated by the first accept and use it.
6137
6138 else
6139 Ann :=
6140 Node (Last_Elmt (Accept_Address
6141 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
6142 end if;
6143 end;
6144 end if;
6145
6146 -- Merge here with Ann either created or referenced, and Adecl
6147 -- pointing to the corresponding declaration. Remaining processing
6148 -- is the same for the two cases.
6149
6150 if Present (Ann) then
6151 Append_Elmt (Ann, Accept_Address (Ent));
6152 Set_Debug_Info_Needed (Ann);
6153 end if;
6154
6155 -- Create renaming declarations for the entry formals. Each reference
6156 -- to a formal becomes a dereference of a component of the parameter
6157 -- block, whose address is held in Ann. These declarations are
6158 -- eventually inserted into the accept block, and analyzed there so
6159 -- that they have the proper scope for gdb and do not conflict with
6160 -- other declarations.
6161
6162 if Present (Parameter_Specifications (N))
6163 and then Present (Handled_Statement_Sequence (N))
6164 then
6165 declare
6166 Comp : Entity_Id;
6167 Decl : Node_Id;
6168 Formal : Entity_Id;
6169 New_F : Entity_Id;
6170 Renamed_Formal : Node_Id;
6171
6172 begin
6173 Push_Scope (Ent);
6174 Formal := First_Formal (Ent);
6175
6176 while Present (Formal) loop
6177 Comp := Entry_Component (Formal);
6178 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
6179
6180 Set_Etype (New_F, Etype (Formal));
6181 Set_Scope (New_F, Ent);
6182
6183 -- Now we set debug info needed on New_F even though it does
6184 -- not come from source, so that the debugger will get the
6185 -- right information for these generated names.
6186
6187 Set_Debug_Info_Needed (New_F);
6188
6189 if Ekind (Formal) = E_In_Parameter then
6190 Set_Ekind (New_F, E_Constant);
6191 else
6192 Set_Ekind (New_F, E_Variable);
6193 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
6194 end if;
6195
6196 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
6197
6198 Renamed_Formal :=
6199 Make_Selected_Component (Loc,
6200 Prefix =>
6201 Unchecked_Convert_To (
6202 Entry_Parameters_Type (Ent),
6203 New_Occurrence_Of (Ann, Loc)),
6204 Selector_Name =>
6205 New_Occurrence_Of (Comp, Loc));
6206
6207 Decl :=
6208 Build_Renamed_Formal_Declaration
6209 (New_F, Formal, Comp, Renamed_Formal);
6210
6211 if No (Declarations (N)) then
6212 Set_Declarations (N, New_List);
6213 end if;
6214
6215 Append (Decl, Declarations (N));
6216 Set_Renamed_Object (Formal, New_F);
6217 Next_Formal (Formal);
6218 end loop;
6219
6220 End_Scope;
6221 end;
6222 end if;
6223 end if;
6224 end Expand_Accept_Declarations;
6225
6226 ---------------------------------------------
6227 -- Expand_Access_Protected_Subprogram_Type --
6228 ---------------------------------------------
6229
6230 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6231 Loc : constant Source_Ptr := Sloc (N);
6232 Comps : List_Id;
6233 T : constant Entity_Id := Defining_Identifier (N);
6234 D_T : constant Entity_Id := Designated_Type (T);
6235 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6236 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6237 P_List : constant List_Id := Build_Protected_Spec
6238 (N, RTE (RE_Address), D_T, False);
6239 Decl1 : Node_Id;
6240 Decl2 : Node_Id;
6241 Def1 : Node_Id;
6242
6243 begin
6244 -- Create access to subprogram with full signature
6245
6246 if Etype (D_T) /= Standard_Void_Type then
6247 Def1 :=
6248 Make_Access_Function_Definition (Loc,
6249 Parameter_Specifications => P_List,
6250 Result_Definition =>
6251 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6252
6253 else
6254 Def1 :=
6255 Make_Access_Procedure_Definition (Loc,
6256 Parameter_Specifications => P_List);
6257 end if;
6258
6259 Decl1 :=
6260 Make_Full_Type_Declaration (Loc,
6261 Defining_Identifier => D_T2,
6262 Type_Definition => Def1);
6263
6264 Insert_After_And_Analyze (N, Decl1);
6265
6266 -- Associate the access to subprogram with its original access to
6267 -- protected subprogram type. Needed by the backend to know that this
6268 -- type corresponds with an access to protected subprogram type.
6269
6270 Set_Original_Access_Type (D_T2, T);
6271
6272 -- Create Equivalent_Type, a record with two components for an access to
6273 -- object and an access to subprogram.
6274
6275 Comps := New_List (
6276 Make_Component_Declaration (Loc,
6277 Defining_Identifier => Make_Temporary (Loc, 'P'),
6278 Component_Definition =>
6279 Make_Component_Definition (Loc,
6280 Aliased_Present => False,
6281 Subtype_Indication =>
6282 New_Occurrence_Of (RTE (RE_Address), Loc))),
6283
6284 Make_Component_Declaration (Loc,
6285 Defining_Identifier => Make_Temporary (Loc, 'S'),
6286 Component_Definition =>
6287 Make_Component_Definition (Loc,
6288 Aliased_Present => False,
6289 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6290
6291 Decl2 :=
6292 Make_Full_Type_Declaration (Loc,
6293 Defining_Identifier => E_T,
6294 Type_Definition =>
6295 Make_Record_Definition (Loc,
6296 Component_List =>
6297 Make_Component_List (Loc, Component_Items => Comps)));
6298
6299 Insert_After_And_Analyze (Decl1, Decl2);
6300 Set_Equivalent_Type (T, E_T);
6301 end Expand_Access_Protected_Subprogram_Type;
6302
6303 --------------------------
6304 -- Expand_Entry_Barrier --
6305 --------------------------
6306
6307 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6308 Cond : constant Node_Id :=
6309 Condition (Entry_Body_Formal_Part (N));
6310 Prot : constant Entity_Id := Scope (Ent);
6311 Spec_Decl : constant Node_Id := Parent (Prot);
6312 Func : Entity_Id := Empty;
6313 B_F : Node_Id;
6314 Body_Decl : Node_Id;
6315
6316 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
6317 -- Check whether entity in Barrier is external to protected type.
6318 -- If so, barrier may not be properly synchronized.
6319
6320 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
6321 -- Check whether N follows the Pure_Barriers restriction. Return OK if
6322 -- so.
6323
6324 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
6325 -- Check whether entity name N denotes a component of the protected
6326 -- object. This is used to check the Simple_Barrier restriction.
6327
6328 ----------------------
6329 -- Is_Global_Entity --
6330 ----------------------
6331
6332 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6333 E : Entity_Id;
6334 S : Entity_Id;
6335
6336 begin
6337 if Is_Entity_Name (N) and then Present (Entity (N)) then
6338 E := Entity (N);
6339 S := Scope (E);
6340
6341 if Ekind (E) = E_Variable then
6342
6343 -- If the variable is local to the barrier function generated
6344 -- during expansion, it is ok. If expansion is not performed,
6345 -- then Func is Empty so this test cannot succeed.
6346
6347 if Scope (E) = Func then
6348 null;
6349
6350 -- A protected call from a barrier to another object is ok
6351
6352 elsif Ekind (Etype (E)) = E_Protected_Type then
6353 null;
6354
6355 -- If the variable is within the package body we consider
6356 -- this safe. This is a common (if dubious) idiom.
6357
6358 elsif S = Scope (Prot)
6359 and then Ekind_In (S, E_Package, E_Generic_Package)
6360 and then Nkind (Parent (E)) = N_Object_Declaration
6361 and then Nkind (Parent (Parent (E))) = N_Package_Body
6362 then
6363 null;
6364
6365 else
6366 Error_Msg_N ("potentially unsynchronized barrier??", N);
6367 Error_Msg_N ("\& should be private component of type??", N);
6368 end if;
6369 end if;
6370 end if;
6371
6372 return OK;
6373 end Is_Global_Entity;
6374
6375 procedure Check_Unprotected_Barrier is
6376 new Traverse_Proc (Is_Global_Entity);
6377
6378 ----------------------------
6379 -- Is_Simple_Barrier_Name --
6380 ----------------------------
6381
6382 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
6383 Renamed : Node_Id;
6384
6385 begin
6386 -- Check for case of _object.all.field (note that the explicit
6387 -- dereference gets inserted by analyze/expand of _object.field).
6388
6389 if Expander_Active then
6390 Renamed := Renamed_Object (Entity (N));
6391
6392 return
6393 Present (Renamed)
6394 and then Nkind (Renamed) = N_Selected_Component
6395 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6396 else
6397 return Scope (Entity (N)) = Current_Scope;
6398 end if;
6399 end Is_Simple_Barrier_Name;
6400
6401 ---------------------
6402 -- Is_Pure_Barrier --
6403 ---------------------
6404
6405 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6406 begin
6407 case Nkind (N) is
6408 when N_Expanded_Name |
6409 N_Identifier =>
6410 if No (Entity (N)) then
6411 return Abandon;
6412 end if;
6413
6414 case Ekind (Entity (N)) is
6415 when E_Constant |
6416 E_Discriminant |
6417 E_Named_Integer |
6418 E_Named_Real |
6419 E_Enumeration_Literal =>
6420 return OK;
6421
6422 when E_Component |
6423 E_Variable =>
6424
6425 -- A variable in the protected type is expanded as a
6426 -- component.
6427
6428 if Is_Simple_Barrier_Name (N) then
6429 return OK;
6430 end if;
6431
6432 when others =>
6433 null;
6434 end case;
6435
6436 when N_Integer_Literal |
6437 N_Real_Literal |
6438 N_Character_Literal =>
6439 return OK;
6440
6441 when N_Op_Boolean |
6442 N_Op_Not =>
6443 if Ekind (Entity (N)) = E_Operator then
6444 return OK;
6445 end if;
6446
6447 when N_Short_Circuit =>
6448 return OK;
6449
6450 when others =>
6451 null;
6452 end case;
6453
6454 return Abandon;
6455 end Is_Pure_Barrier;
6456
6457 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6458
6459 -- Start of processing for Expand_Entry_Barrier
6460
6461 begin
6462 if No_Run_Time_Mode then
6463 Error_Msg_CRT ("entry barrier", N);
6464 return;
6465 end if;
6466
6467 -- The body of the entry barrier must be analyzed in the context of the
6468 -- protected object, but its scope is external to it, just as any other
6469 -- unprotected version of a protected operation. The specification has
6470 -- been produced when the protected type declaration was elaborated. We
6471 -- build the body, insert it in the enclosing scope, but analyze it in
6472 -- the current context. A more uniform approach would be to treat the
6473 -- barrier just as a protected function, and discard the protected
6474 -- version of it because it is never called.
6475
6476 if Expander_Active then
6477 B_F := Build_Barrier_Function (N, Ent, Prot);
6478 Func := Barrier_Function (Ent);
6479 Set_Corresponding_Spec (B_F, Func);
6480
6481 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6482
6483 if Nkind (Parent (Body_Decl)) = N_Subunit then
6484 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6485 end if;
6486
6487 Insert_Before_And_Analyze (Body_Decl, B_F);
6488
6489 Set_Discriminals (Spec_Decl);
6490 Set_Scope (Func, Scope (Prot));
6491
6492 else
6493 Analyze_And_Resolve (Cond, Any_Boolean);
6494 end if;
6495
6496 -- Check Pure_Barriers restriction
6497
6498 if Check_Pure_Barriers (Cond) = Abandon then
6499 Check_Restriction (Pure_Barriers, Cond);
6500 end if;
6501
6502 -- The Ravenscar profile restricts barriers to simple variables declared
6503 -- within the protected object. We also allow Boolean constants, since
6504 -- these appear in several published examples and are also allowed by
6505 -- other compilers.
6506
6507 -- Note that after analysis variables in this context will be replaced
6508 -- by the corresponding prival, that is to say a renaming of a selected
6509 -- component of the form _Object.Var. If expansion is disabled, as
6510 -- within a generic, we check that the entity appears in the current
6511 -- scope.
6512
6513 if Is_Entity_Name (Cond) then
6514
6515 -- A small optimization of useless renamings. If the scope of the
6516 -- entity of the condition is not the barrier function, then the
6517 -- condition does not reference any of the generated renamings
6518 -- within the function.
6519
6520 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6521 Set_Declarations (B_F, Empty_List);
6522 end if;
6523
6524 if Entity (Cond) = Standard_False
6525 or else
6526 Entity (Cond) = Standard_True
6527 then
6528 return;
6529
6530 elsif Is_Simple_Barrier_Name (Cond) then
6531 return;
6532 end if;
6533 end if;
6534
6535 -- It is not a boolean variable or literal, so check the restriction.
6536 -- Note that it is safe to be calling Check_Restriction from here, even
6537 -- though this is part of the expander, since Expand_Entry_Barrier is
6538 -- called from Sem_Ch9 even in -gnatc mode.
6539
6540 Check_Restriction (Simple_Barriers, Cond);
6541
6542 -- Emit warning if barrier contains global entities and is thus
6543 -- potentially unsynchronized.
6544
6545 Check_Unprotected_Barrier (Cond);
6546 end Expand_Entry_Barrier;
6547
6548 ------------------------------
6549 -- Expand_N_Abort_Statement --
6550 ------------------------------
6551
6552 -- Expand abort T1, T2, .. Tn; into:
6553 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6554
6555 procedure Expand_N_Abort_Statement (N : Node_Id) is
6556 Loc : constant Source_Ptr := Sloc (N);
6557 Tlist : constant List_Id := Names (N);
6558 Count : Nat;
6559 Aggr : Node_Id;
6560 Tasknm : Node_Id;
6561
6562 begin
6563 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6564 Count := 0;
6565
6566 Tasknm := First (Tlist);
6567
6568 while Present (Tasknm) loop
6569 Count := Count + 1;
6570
6571 -- A task interface class-wide type object is being aborted. Retrieve
6572 -- its _task_id by calling a dispatching routine.
6573
6574 if Ada_Version >= Ada_2005
6575 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6576 and then Is_Interface (Etype (Tasknm))
6577 and then Is_Task_Interface (Etype (Tasknm))
6578 then
6579 Append_To (Component_Associations (Aggr),
6580 Make_Component_Association (Loc,
6581 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6582 Expression =>
6583
6584 -- Task_Id (Tasknm._disp_get_task_id)
6585
6586 Make_Unchecked_Type_Conversion (Loc,
6587 Subtype_Mark =>
6588 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6589 Expression =>
6590 Make_Selected_Component (Loc,
6591 Prefix => New_Copy_Tree (Tasknm),
6592 Selector_Name =>
6593 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6594
6595 else
6596 Append_To (Component_Associations (Aggr),
6597 Make_Component_Association (Loc,
6598 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6599 Expression => Concurrent_Ref (Tasknm)));
6600 end if;
6601
6602 Next (Tasknm);
6603 end loop;
6604
6605 Rewrite (N,
6606 Make_Procedure_Call_Statement (Loc,
6607 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6608 Parameter_Associations => New_List (
6609 Make_Qualified_Expression (Loc,
6610 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6611 Expression => Aggr))));
6612
6613 Analyze (N);
6614 end Expand_N_Abort_Statement;
6615
6616 -------------------------------
6617 -- Expand_N_Accept_Statement --
6618 -------------------------------
6619
6620 -- This procedure handles expansion of accept statements that stand alone,
6621 -- i.e. they are not part of an accept alternative. The expansion of
6622 -- accept statement in accept alternatives is handled by the routines
6623 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6624 -- following description applies only to stand alone accept statements.
6625
6626 -- If there is no handled statement sequence, or only null statements, then
6627 -- this is called a trivial accept, and the expansion is:
6628
6629 -- Accept_Trivial (entry-index)
6630
6631 -- If there is a handled statement sequence, then the expansion is:
6632
6633 -- Ann : Address;
6634 -- {Lnn : Label}
6635
6636 -- begin
6637 -- begin
6638 -- Accept_Call (entry-index, Ann);
6639 -- Renaming_Declarations for formals
6640 -- <statement sequence from N_Accept_Statement node>
6641 -- Complete_Rendezvous;
6642 -- <<Lnn>>
6643 --
6644 -- exception
6645 -- when ... =>
6646 -- <exception handler from N_Accept_Statement node>
6647 -- Complete_Rendezvous;
6648 -- when ... =>
6649 -- <exception handler from N_Accept_Statement node>
6650 -- Complete_Rendezvous;
6651 -- ...
6652 -- end;
6653
6654 -- exception
6655 -- when all others =>
6656 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6657 -- end;
6658
6659 -- The first three declarations were already inserted ahead of the accept
6660 -- statement by the Expand_Accept_Declarations procedure, which was called
6661 -- directly from the semantics during analysis of the accept statement,
6662 -- before analyzing its contained statements.
6663
6664 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6665 -- from possible expansion activity (the original source of course does
6666 -- not have any declarations associated with the accept statement, since
6667 -- an accept statement has no declarative part). In particular, if the
6668 -- expander is active, the first such declaration is the declaration of
6669 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6670
6671 -- The two blocks are merged into a single block if the inner block has
6672 -- no exception handlers, but otherwise two blocks are required, since
6673 -- exceptions might be raised in the exception handlers of the inner
6674 -- block, and Exceptional_Complete_Rendezvous must be called.
6675
6676 procedure Expand_N_Accept_Statement (N : Node_Id) is
6677 Loc : constant Source_Ptr := Sloc (N);
6678 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6679 Ename : constant Node_Id := Entry_Direct_Name (N);
6680 Eindx : constant Node_Id := Entry_Index (N);
6681 Eent : constant Entity_Id := Entity (Ename);
6682 Acstack : constant Elist_Id := Accept_Address (Eent);
6683 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6684 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6685 Blkent : Entity_Id;
6686 Call : Node_Id;
6687 Block : Node_Id;
6688
6689 begin
6690 -- If the accept statement is not part of a list, then its parent must
6691 -- be an accept alternative, and, as described above, we do not do any
6692 -- expansion for such accept statements at this level.
6693
6694 if not Is_List_Member (N) then
6695 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6696 return;
6697
6698 -- Trivial accept case (no statement sequence, or null statements).
6699 -- If the accept statement has declarations, then just insert them
6700 -- before the procedure call.
6701
6702 elsif Trivial_Accept_OK
6703 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6704 then
6705 -- Remove declarations for renamings, because the parameter block
6706 -- will not be assigned.
6707
6708 declare
6709 D : Node_Id;
6710 Next_D : Node_Id;
6711
6712 begin
6713 D := First (Declarations (N));
6714 while Present (D) loop
6715 Next_D := Next (D);
6716 if Nkind (D) = N_Object_Renaming_Declaration then
6717 Remove (D);
6718 end if;
6719
6720 D := Next_D;
6721 end loop;
6722 end;
6723
6724 if Present (Declarations (N)) then
6725 Insert_Actions (N, Declarations (N));
6726 end if;
6727
6728 Rewrite (N,
6729 Make_Procedure_Call_Statement (Loc,
6730 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6731 Parameter_Associations => New_List (
6732 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6733
6734 Analyze (N);
6735
6736 -- Discard Entry_Address that was created for it, so it will not be
6737 -- emitted if this accept statement is in the statement part of a
6738 -- delay alternative.
6739
6740 if Present (Stats) then
6741 Remove_Last_Elmt (Acstack);
6742 end if;
6743
6744 -- Case of statement sequence present
6745
6746 else
6747 -- Construct the block, using the declarations from the accept
6748 -- statement if any to initialize the declarations of the block.
6749
6750 Blkent := Make_Temporary (Loc, 'A');
6751 Set_Ekind (Blkent, E_Block);
6752 Set_Etype (Blkent, Standard_Void_Type);
6753 Set_Scope (Blkent, Current_Scope);
6754
6755 Block :=
6756 Make_Block_Statement (Loc,
6757 Identifier => New_Occurrence_Of (Blkent, Loc),
6758 Declarations => Declarations (N),
6759 Handled_Statement_Sequence => Build_Accept_Body (N));
6760
6761 -- For the analysis of the generated declarations, the parent node
6762 -- must be properly set.
6763
6764 Set_Parent (Block, Parent (N));
6765
6766 -- Prepend call to Accept_Call to main statement sequence If the
6767 -- accept has exception handlers, the statement sequence is wrapped
6768 -- in a block. Insert call and renaming declarations in the
6769 -- declarations of the block, so they are elaborated before the
6770 -- handlers.
6771
6772 Call :=
6773 Make_Procedure_Call_Statement (Loc,
6774 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6775 Parameter_Associations => New_List (
6776 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6777 New_Occurrence_Of (Ann, Loc)));
6778
6779 if Parent (Stats) = N then
6780 Prepend (Call, Statements (Stats));
6781 else
6782 Set_Declarations (Parent (Stats), New_List (Call));
6783 end if;
6784
6785 Analyze (Call);
6786
6787 Push_Scope (Blkent);
6788
6789 declare
6790 D : Node_Id;
6791 Next_D : Node_Id;
6792 Typ : Entity_Id;
6793
6794 begin
6795 D := First (Declarations (N));
6796 while Present (D) loop
6797 Next_D := Next (D);
6798
6799 if Nkind (D) = N_Object_Renaming_Declaration then
6800
6801 -- The renaming declarations for the formals were created
6802 -- during analysis of the accept statement, and attached to
6803 -- the list of declarations. Place them now in the context
6804 -- of the accept block or subprogram.
6805
6806 Remove (D);
6807 Typ := Entity (Subtype_Mark (D));
6808 Insert_After (Call, D);
6809 Analyze (D);
6810
6811 -- If the formal is class_wide, it does not have an actual
6812 -- subtype. The analysis of the renaming declaration creates
6813 -- one, but we need to retain the class-wide nature of the
6814 -- entity.
6815
6816 if Is_Class_Wide_Type (Typ) then
6817 Set_Etype (Defining_Identifier (D), Typ);
6818 end if;
6819
6820 end if;
6821
6822 D := Next_D;
6823 end loop;
6824 end;
6825
6826 End_Scope;
6827
6828 -- Replace the accept statement by the new block
6829
6830 Rewrite (N, Block);
6831 Analyze (N);
6832
6833 -- Last step is to unstack the Accept_Address value
6834
6835 Remove_Last_Elmt (Acstack);
6836 end if;
6837 end Expand_N_Accept_Statement;
6838
6839 ----------------------------------
6840 -- Expand_N_Asynchronous_Select --
6841 ----------------------------------
6842
6843 -- This procedure assumes that the trigger statement is an entry call or
6844 -- a dispatching procedure call. A delay alternative should already have
6845 -- been expanded into an entry call to the appropriate delay object Wait
6846 -- entry.
6847
6848 -- If the trigger is a task entry call, the select is implemented with
6849 -- a Task_Entry_Call:
6850
6851 -- declare
6852 -- B : Boolean;
6853 -- C : Boolean;
6854 -- P : parms := (parm, parm, parm);
6855
6856 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6857
6858 -- procedure _clean is
6859 -- begin
6860 -- ...
6861 -- Cancel_Task_Entry_Call (C);
6862 -- ...
6863 -- end _clean;
6864
6865 -- begin
6866 -- Abort_Defer;
6867 -- Task_Entry_Call
6868 -- (<acceptor-task>, -- Acceptor
6869 -- <entry-index>, -- E
6870 -- P'Address, -- Uninterpreted_Data
6871 -- Asynchronous_Call, -- Mode
6872 -- B); -- Rendezvous_Successful
6873
6874 -- begin
6875 -- begin
6876 -- Abort_Undefer;
6877 -- <abortable-part>
6878 -- at end
6879 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6880 -- end;
6881 -- exception
6882 -- when Abort_Signal => Abort_Undefer;
6883 -- end;
6884
6885 -- parm := P.param;
6886 -- parm := P.param;
6887 -- ...
6888 -- if not C then
6889 -- <triggered-statements>
6890 -- end if;
6891 -- end;
6892
6893 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6894 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6895 -- as follows:
6896
6897 -- declare
6898 -- P : parms := (parm, parm, parm);
6899 -- begin
6900 -- Call_Simple (acceptor-task, entry-index, P'Address);
6901 -- parm := P.param;
6902 -- parm := P.param;
6903 -- ...
6904 -- end;
6905
6906 -- so the task at hand is to convert the latter expansion into the former
6907
6908 -- If the trigger is a protected entry call, the select is implemented
6909 -- with Protected_Entry_Call:
6910
6911 -- declare
6912 -- P : E1_Params := (param, param, param);
6913 -- Bnn : Communications_Block;
6914
6915 -- begin
6916 -- declare
6917
6918 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6919
6920 -- procedure _clean is
6921 -- begin
6922 -- ...
6923 -- if Enqueued (Bnn) then
6924 -- Cancel_Protected_Entry_Call (Bnn);
6925 -- end if;
6926 -- ...
6927 -- end _clean;
6928
6929 -- begin
6930 -- begin
6931 -- Protected_Entry_Call
6932 -- (po._object'Access, -- Object
6933 -- <entry index>, -- E
6934 -- P'Address, -- Uninterpreted_Data
6935 -- Asynchronous_Call, -- Mode
6936 -- Bnn); -- Block
6937
6938 -- if Enqueued (Bnn) then
6939 -- <abortable-part>
6940 -- end if;
6941 -- at end
6942 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6943 -- end;
6944 -- exception
6945 -- when Abort_Signal => Abort_Undefer;
6946 -- end;
6947
6948 -- if not Cancelled (Bnn) then
6949 -- <triggered-statements>
6950 -- end if;
6951 -- end;
6952
6953 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6954 -- entry call:
6955
6956 -- declare
6957 -- P : E1_Params := (param, param, param);
6958 -- Bnn : Communications_Block;
6959
6960 -- begin
6961 -- Protected_Entry_Call
6962 -- (po._object'Access, -- Object
6963 -- <entry index>, -- E
6964 -- P'Address, -- Uninterpreted_Data
6965 -- Simple_Call, -- Mode
6966 -- Bnn); -- Block
6967 -- parm := P.param;
6968 -- parm := P.param;
6969 -- ...
6970 -- end;
6971
6972 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6973 -- expanded into:
6974
6975 -- declare
6976 -- B : Boolean := False;
6977 -- Bnn : Communication_Block;
6978 -- C : Ada.Tags.Prim_Op_Kind;
6979 -- D : System.Storage_Elements.Dummy_Communication_Block;
6980 -- K : Ada.Tags.Tagged_Kind :=
6981 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6982 -- P : Parameters := (Param1 .. ParamN);
6983 -- S : Integer;
6984 -- U : Boolean;
6985
6986 -- begin
6987 -- if K = Ada.Tags.TK_Limited_Tagged
6988 -- or else K = Ada.Tags.TK_Tagged
6989 -- then
6990 -- <dispatching-call>;
6991 -- <triggering-statements>;
6992
6993 -- else
6994 -- S :=
6995 -- Ada.Tags.Get_Offset_Index
6996 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6997
6998 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6999
7000 -- if C = POK_Protected_Entry then
7001 -- declare
7002 -- procedure _clean is
7003 -- begin
7004 -- if Enqueued (Bnn) then
7005 -- Cancel_Protected_Entry_Call (Bnn);
7006 -- end if;
7007 -- end _clean;
7008
7009 -- begin
7010 -- begin
7011 -- _Disp_Asynchronous_Select
7012 -- (<object>, S, P'Address, D, B);
7013 -- Bnn := Communication_Block (D);
7014
7015 -- Param1 := P.Param1;
7016 -- ...
7017 -- ParamN := P.ParamN;
7018
7019 -- if Enqueued (Bnn) then
7020 -- <abortable-statements>
7021 -- end if;
7022 -- at end
7023 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7024 -- end;
7025 -- exception
7026 -- when Abort_Signal => Abort_Undefer;
7027 -- end;
7028
7029 -- if not Cancelled (Bnn) then
7030 -- <triggering-statements>
7031 -- end if;
7032
7033 -- elsif C = POK_Task_Entry then
7034 -- declare
7035 -- procedure _clean is
7036 -- begin
7037 -- Cancel_Task_Entry_Call (U);
7038 -- end _clean;
7039
7040 -- begin
7041 -- Abort_Defer;
7042
7043 -- _Disp_Asynchronous_Select
7044 -- (<object>, S, P'Address, D, B);
7045 -- Bnn := Communication_Bloc (D);
7046
7047 -- Param1 := P.Param1;
7048 -- ...
7049 -- ParamN := P.ParamN;
7050
7051 -- begin
7052 -- begin
7053 -- Abort_Undefer;
7054 -- <abortable-statements>
7055 -- at end
7056 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7057 -- end;
7058 -- exception
7059 -- when Abort_Signal => Abort_Undefer;
7060 -- end;
7061
7062 -- if not U then
7063 -- <triggering-statements>
7064 -- end if;
7065 -- end;
7066
7067 -- else
7068 -- <dispatching-call>;
7069 -- <triggering-statements>
7070 -- end if;
7071 -- end if;
7072 -- end;
7073
7074 -- The job is to convert this to the asynchronous form
7075
7076 -- If the trigger is a delay statement, it will have been expanded into
7077 -- a call to one of the GNARL delay procedures. This routine will convert
7078 -- this into a protected entry call on a delay object and then continue
7079 -- processing as for a protected entry call trigger. This requires
7080 -- declaring a Delay_Block object and adding a pointer to this object to
7081 -- the parameter list of the delay procedure to form the parameter list of
7082 -- the entry call. This object is used by the runtime to queue the delay
7083 -- request.
7084
7085 -- For a description of the use of P and the assignments after the call,
7086 -- see Expand_N_Entry_Call_Statement.
7087
7088 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
7089 Loc : constant Source_Ptr := Sloc (N);
7090 Abrt : constant Node_Id := Abortable_Part (N);
7091 Trig : constant Node_Id := Triggering_Alternative (N);
7092
7093 Abort_Block_Ent : Entity_Id;
7094 Abortable_Block : Node_Id;
7095 Actuals : List_Id;
7096 Astats : List_Id;
7097 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
7098 Blk_Typ : Entity_Id;
7099 Call : Node_Id;
7100 Call_Ent : Entity_Id;
7101 Cancel_Param : Entity_Id;
7102 Cleanup_Block : Node_Id;
7103 Cleanup_Block_Ent : Entity_Id;
7104 Cleanup_Stmts : List_Id;
7105 Conc_Typ_Stmts : List_Id;
7106 Concval : Node_Id;
7107 Dblock_Ent : Entity_Id;
7108 Decl : Node_Id;
7109 Decls : List_Id;
7110 Ecall : Node_Id;
7111 Ename : Node_Id;
7112 Enqueue_Call : Node_Id;
7113 Formals : List_Id;
7114 Hdle : List_Id;
7115 Handler_Stmt : Node_Id;
7116 Index : Node_Id;
7117 Lim_Typ_Stmts : List_Id;
7118 N_Orig : Node_Id;
7119 Obj : Entity_Id;
7120 Param : Node_Id;
7121 Params : List_Id;
7122 Pdef : Entity_Id;
7123 ProtE_Stmts : List_Id;
7124 ProtP_Stmts : List_Id;
7125 Stmt : Node_Id;
7126 Stmts : List_Id;
7127 TaskE_Stmts : List_Id;
7128 Tstats : List_Id;
7129
7130 B : Entity_Id; -- Call status flag
7131 Bnn : Entity_Id; -- Communication block
7132 C : Entity_Id; -- Call kind
7133 K : Entity_Id; -- Tagged kind
7134 P : Entity_Id; -- Parameter block
7135 S : Entity_Id; -- Primitive operation slot
7136 T : Entity_Id; -- Additional status flag
7137
7138 procedure Rewrite_Abortable_Part;
7139 -- If the trigger is a dispatching call, the expansion inserts multiple
7140 -- copies of the abortable part. This is both inefficient, and may lead
7141 -- to duplicate definitions that the back-end will reject, when the
7142 -- abortable part includes loops. This procedure rewrites the abortable
7143 -- part into a call to a generated procedure.
7144
7145 ----------------------------
7146 -- Rewrite_Abortable_Part --
7147 ----------------------------
7148
7149 procedure Rewrite_Abortable_Part is
7150 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7151 Decl : Node_Id;
7152
7153 begin
7154 Decl :=
7155 Make_Subprogram_Body (Loc,
7156 Specification =>
7157 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
7158 Declarations => New_List,
7159 Handled_Statement_Sequence =>
7160 Make_Handled_Sequence_Of_Statements (Loc, Astats));
7161 Insert_Before (N, Decl);
7162 Analyze (Decl);
7163
7164 -- Rewrite abortable part into a call to this procedure.
7165
7166 Astats :=
7167 New_List (
7168 Make_Procedure_Call_Statement (Loc,
7169 Name => New_Occurrence_Of (Proc, Loc)));
7170 end Rewrite_Abortable_Part;
7171
7172 begin
7173 Process_Statements_For_Controlled_Objects (Trig);
7174 Process_Statements_For_Controlled_Objects (Abrt);
7175
7176 Ecall := Triggering_Statement (Trig);
7177
7178 Ensure_Statement_Present (Sloc (Ecall), Trig);
7179
7180 -- Retrieve Astats and Tstats now because the finalization machinery may
7181 -- wrap them in blocks.
7182
7183 Astats := Statements (Abrt);
7184 Tstats := Statements (Trig);
7185
7186 -- The arguments in the call may require dynamic allocation, and the
7187 -- call statement may have been transformed into a block. The block
7188 -- may contain additional declarations for internal entities, and the
7189 -- original call is found by sequential search.
7190
7191 if Nkind (Ecall) = N_Block_Statement then
7192 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
7193 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
7194 N_Entry_Call_Statement)
7195 loop
7196 Next (Ecall);
7197 end loop;
7198 end if;
7199
7200 -- This is either a dispatching call or a delay statement used as a
7201 -- trigger which was expanded into a procedure call.
7202
7203 if Nkind (Ecall) = N_Procedure_Call_Statement then
7204 if Ada_Version >= Ada_2005
7205 and then
7206 (No (Original_Node (Ecall))
7207 or else not Nkind_In (Original_Node (Ecall),
7208 N_Delay_Relative_Statement,
7209 N_Delay_Until_Statement))
7210 then
7211 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
7212
7213 Rewrite_Abortable_Part;
7214 Decls := New_List;
7215 Stmts := New_List;
7216
7217 -- Call status flag processing, generate:
7218 -- B : Boolean := False;
7219
7220 B := Build_B (Loc, Decls);
7221
7222 -- Communication block processing, generate:
7223 -- Bnn : Communication_Block;
7224
7225 Bnn := Make_Temporary (Loc, 'B');
7226 Append_To (Decls,
7227 Make_Object_Declaration (Loc,
7228 Defining_Identifier => Bnn,
7229 Object_Definition =>
7230 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
7231
7232 -- Call kind processing, generate:
7233 -- C : Ada.Tags.Prim_Op_Kind;
7234
7235 C := Build_C (Loc, Decls);
7236
7237 -- Tagged kind processing, generate:
7238 -- K : Ada.Tags.Tagged_Kind :=
7239 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7240
7241 -- Dummy communication block, generate:
7242 -- D : Dummy_Communication_Block;
7243
7244 Append_To (Decls,
7245 Make_Object_Declaration (Loc,
7246 Defining_Identifier =>
7247 Make_Defining_Identifier (Loc, Name_uD),
7248 Object_Definition =>
7249 New_Occurrence_Of
7250 (RTE (RE_Dummy_Communication_Block), Loc)));
7251
7252 K := Build_K (Loc, Decls, Obj);
7253
7254 -- Parameter block processing
7255
7256 Blk_Typ := Build_Parameter_Block
7257 (Loc, Actuals, Formals, Decls);
7258 P := Parameter_Block_Pack
7259 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7260
7261 -- Dispatch table slot processing, generate:
7262 -- S : Integer;
7263
7264 S := Build_S (Loc, Decls);
7265
7266 -- Additional status flag processing, generate:
7267 -- Tnn : Boolean;
7268
7269 T := Make_Temporary (Loc, 'T');
7270 Append_To (Decls,
7271 Make_Object_Declaration (Loc,
7272 Defining_Identifier => T,
7273 Object_Definition =>
7274 New_Occurrence_Of (Standard_Boolean, Loc)));
7275
7276 ------------------------------
7277 -- Protected entry handling --
7278 ------------------------------
7279
7280 -- Generate:
7281 -- Param1 := P.Param1;
7282 -- ...
7283 -- ParamN := P.ParamN;
7284
7285 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7286
7287 -- Generate:
7288 -- Bnn := Communication_Block (D);
7289
7290 Prepend_To (Cleanup_Stmts,
7291 Make_Assignment_Statement (Loc,
7292 Name => New_Occurrence_Of (Bnn, Loc),
7293 Expression =>
7294 Make_Unchecked_Type_Conversion (Loc,
7295 Subtype_Mark =>
7296 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7297 Expression => Make_Identifier (Loc, Name_uD))));
7298
7299 -- Generate:
7300 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7301
7302 Prepend_To (Cleanup_Stmts,
7303 Make_Procedure_Call_Statement (Loc,
7304 Name =>
7305 New_Occurrence_Of
7306 (Find_Prim_Op
7307 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7308 Loc),
7309 Parameter_Associations =>
7310 New_List (
7311 New_Copy_Tree (Obj), -- <object>
7312 New_Occurrence_Of (S, Loc), -- S
7313 Make_Attribute_Reference (Loc, -- P'Address
7314 Prefix => New_Occurrence_Of (P, Loc),
7315 Attribute_Name => Name_Address),
7316 Make_Identifier (Loc, Name_uD), -- D
7317 New_Occurrence_Of (B, Loc)))); -- B
7318
7319 -- Generate:
7320 -- if Enqueued (Bnn) then
7321 -- <abortable-statements>
7322 -- end if;
7323
7324 Append_To (Cleanup_Stmts,
7325 Make_Implicit_If_Statement (N,
7326 Condition =>
7327 Make_Function_Call (Loc,
7328 Name =>
7329 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7330 Parameter_Associations =>
7331 New_List (New_Occurrence_Of (Bnn, Loc))),
7332
7333 Then_Statements =>
7334 New_Copy_List_Tree (Astats)));
7335
7336 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7337 -- will then generate a _clean for the communication block Bnn.
7338
7339 -- Generate:
7340 -- declare
7341 -- procedure _clean is
7342 -- begin
7343 -- if Enqueued (Bnn) then
7344 -- Cancel_Protected_Entry_Call (Bnn);
7345 -- end if;
7346 -- end _clean;
7347 -- begin
7348 -- Cleanup_Stmts
7349 -- at end
7350 -- _clean;
7351 -- end;
7352
7353 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7354 Cleanup_Block :=
7355 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7356
7357 -- Wrap the cleanup block in an exception handling block
7358
7359 -- Generate:
7360 -- begin
7361 -- Cleanup_Block
7362 -- exception
7363 -- when Abort_Signal => Abort_Undefer;
7364 -- end;
7365
7366 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7367 ProtE_Stmts :=
7368 New_List (
7369 Make_Implicit_Label_Declaration (Loc,
7370 Defining_Identifier => Abort_Block_Ent),
7371
7372 Build_Abort_Block
7373 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7374
7375 -- Generate:
7376 -- if not Cancelled (Bnn) then
7377 -- <triggering-statements>
7378 -- end if;
7379
7380 Append_To (ProtE_Stmts,
7381 Make_Implicit_If_Statement (N,
7382 Condition =>
7383 Make_Op_Not (Loc,
7384 Right_Opnd =>
7385 Make_Function_Call (Loc,
7386 Name =>
7387 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7388 Parameter_Associations =>
7389 New_List (New_Occurrence_Of (Bnn, Loc)))),
7390
7391 Then_Statements =>
7392 New_Copy_List_Tree (Tstats)));
7393
7394 -------------------------
7395 -- Task entry handling --
7396 -------------------------
7397
7398 -- Generate:
7399 -- Param1 := P.Param1;
7400 -- ...
7401 -- ParamN := P.ParamN;
7402
7403 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7404
7405 -- Generate:
7406 -- Bnn := Communication_Block (D);
7407
7408 Append_To (TaskE_Stmts,
7409 Make_Assignment_Statement (Loc,
7410 Name =>
7411 New_Occurrence_Of (Bnn, Loc),
7412 Expression =>
7413 Make_Unchecked_Type_Conversion (Loc,
7414 Subtype_Mark =>
7415 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7416 Expression => Make_Identifier (Loc, Name_uD))));
7417
7418 -- Generate:
7419 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7420
7421 Prepend_To (TaskE_Stmts,
7422 Make_Procedure_Call_Statement (Loc,
7423 Name =>
7424 New_Occurrence_Of (
7425 Find_Prim_Op (Etype (Etype (Obj)),
7426 Name_uDisp_Asynchronous_Select),
7427 Loc),
7428
7429 Parameter_Associations =>
7430 New_List (
7431 New_Copy_Tree (Obj), -- <object>
7432 New_Occurrence_Of (S, Loc), -- S
7433 Make_Attribute_Reference (Loc, -- P'Address
7434 Prefix => New_Occurrence_Of (P, Loc),
7435 Attribute_Name => Name_Address),
7436 Make_Identifier (Loc, Name_uD), -- D
7437 New_Occurrence_Of (B, Loc)))); -- B
7438
7439 -- Generate:
7440 -- Abort_Defer;
7441
7442 Prepend_To (TaskE_Stmts,
7443 Make_Procedure_Call_Statement (Loc,
7444 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
7445 Parameter_Associations => No_List));
7446
7447 -- Generate:
7448 -- Abort_Undefer;
7449 -- <abortable-statements>
7450
7451 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7452
7453 Prepend_To (Cleanup_Stmts,
7454 Make_Procedure_Call_Statement (Loc,
7455 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
7456 Parameter_Associations => No_List));
7457
7458 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7459 -- will generate a _clean for the additional status flag.
7460
7461 -- Generate:
7462 -- declare
7463 -- procedure _clean is
7464 -- begin
7465 -- Cancel_Task_Entry_Call (U);
7466 -- end _clean;
7467 -- begin
7468 -- Cleanup_Stmts
7469 -- at end
7470 -- _clean;
7471 -- end;
7472
7473 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7474 Cleanup_Block :=
7475 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7476
7477 -- Wrap the cleanup block in an exception handling block
7478
7479 -- Generate:
7480 -- begin
7481 -- Cleanup_Block
7482 -- exception
7483 -- when Abort_Signal => Abort_Undefer;
7484 -- end;
7485
7486 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7487
7488 Append_To (TaskE_Stmts,
7489 Make_Implicit_Label_Declaration (Loc,
7490 Defining_Identifier => Abort_Block_Ent));
7491
7492 Append_To (TaskE_Stmts,
7493 Build_Abort_Block
7494 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7495
7496 -- Generate:
7497 -- if not T then
7498 -- <triggering-statements>
7499 -- end if;
7500
7501 Append_To (TaskE_Stmts,
7502 Make_Implicit_If_Statement (N,
7503 Condition =>
7504 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7505
7506 Then_Statements =>
7507 New_Copy_List_Tree (Tstats)));
7508
7509 ----------------------------------
7510 -- Protected procedure handling --
7511 ----------------------------------
7512
7513 -- Generate:
7514 -- <dispatching-call>;
7515 -- <triggering-statements>
7516
7517 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7518 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7519
7520 -- Generate:
7521 -- S := Ada.Tags.Get_Offset_Index
7522 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7523
7524 Conc_Typ_Stmts :=
7525 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7526
7527 -- Generate:
7528 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7529
7530 Append_To (Conc_Typ_Stmts,
7531 Make_Procedure_Call_Statement (Loc,
7532 Name =>
7533 New_Occurrence_Of
7534 (Find_Prim_Op (Etype (Etype (Obj)),
7535 Name_uDisp_Get_Prim_Op_Kind),
7536 Loc),
7537 Parameter_Associations =>
7538 New_List (
7539 New_Copy_Tree (Obj),
7540 New_Occurrence_Of (S, Loc),
7541 New_Occurrence_Of (C, Loc))));
7542
7543 -- Generate:
7544 -- if C = POK_Procedure_Entry then
7545 -- ProtE_Stmts
7546 -- elsif C = POK_Task_Entry then
7547 -- TaskE_Stmts
7548 -- else
7549 -- ProtP_Stmts
7550 -- end if;
7551
7552 Append_To (Conc_Typ_Stmts,
7553 Make_Implicit_If_Statement (N,
7554 Condition =>
7555 Make_Op_Eq (Loc,
7556 Left_Opnd =>
7557 New_Occurrence_Of (C, Loc),
7558 Right_Opnd =>
7559 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7560
7561 Then_Statements =>
7562 ProtE_Stmts,
7563
7564 Elsif_Parts =>
7565 New_List (
7566 Make_Elsif_Part (Loc,
7567 Condition =>
7568 Make_Op_Eq (Loc,
7569 Left_Opnd =>
7570 New_Occurrence_Of (C, Loc),
7571 Right_Opnd =>
7572 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7573
7574 Then_Statements =>
7575 TaskE_Stmts)),
7576
7577 Else_Statements =>
7578 ProtP_Stmts));
7579
7580 -- Generate:
7581 -- <dispatching-call>;
7582 -- <triggering-statements>
7583
7584 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7585 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7586
7587 -- Generate:
7588 -- if K = Ada.Tags.TK_Limited_Tagged
7589 -- or else K = Ada.Tags.TK_Tagged
7590 -- then
7591 -- Lim_Typ_Stmts
7592 -- else
7593 -- Conc_Typ_Stmts
7594 -- end if;
7595
7596 Append_To (Stmts,
7597 Make_Implicit_If_Statement (N,
7598 Condition => Build_Dispatching_Tag_Check (K, N),
7599 Then_Statements => Lim_Typ_Stmts,
7600 Else_Statements => Conc_Typ_Stmts));
7601
7602 Rewrite (N,
7603 Make_Block_Statement (Loc,
7604 Declarations =>
7605 Decls,
7606 Handled_Statement_Sequence =>
7607 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7608
7609 Analyze (N);
7610 return;
7611
7612 -- Delay triggering statement processing
7613
7614 else
7615 -- Add a Delay_Block object to the parameter list of the delay
7616 -- procedure to form the parameter list of the Wait entry call.
7617
7618 Dblock_Ent := Make_Temporary (Loc, 'D');
7619
7620 Pdef := Entity (Name (Ecall));
7621
7622 if Is_RTE (Pdef, RO_CA_Delay_For) then
7623 Enqueue_Call :=
7624 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7625
7626 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7627 Enqueue_Call :=
7628 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7629
7630 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7631 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7632 end if;
7633
7634 Append_To (Parameter_Associations (Ecall),
7635 Make_Attribute_Reference (Loc,
7636 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7637 Attribute_Name => Name_Unchecked_Access));
7638
7639 -- Create the inner block to protect the abortable part
7640
7641 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7642
7643 Prepend_To (Astats,
7644 Make_Procedure_Call_Statement (Loc,
7645 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc)));
7646
7647 Abortable_Block :=
7648 Make_Block_Statement (Loc,
7649 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7650 Handled_Statement_Sequence =>
7651 Make_Handled_Sequence_Of_Statements (Loc,
7652 Statements => Astats),
7653 Has_Created_Identifier => True,
7654 Is_Asynchronous_Call_Block => True);
7655
7656 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7657
7658 Rewrite (Ecall,
7659 Make_Implicit_If_Statement (N,
7660 Condition =>
7661 Make_Function_Call (Loc,
7662 Name => Enqueue_Call,
7663 Parameter_Associations => Parameter_Associations (Ecall)),
7664 Then_Statements =>
7665 New_List (Make_Block_Statement (Loc,
7666 Handled_Statement_Sequence =>
7667 Make_Handled_Sequence_Of_Statements (Loc,
7668 Statements => New_List (
7669 Make_Implicit_Label_Declaration (Loc,
7670 Defining_Identifier => Blk_Ent,
7671 Label_Construct => Abortable_Block),
7672 Abortable_Block),
7673 Exception_Handlers => Hdle)))));
7674
7675 Stmts := New_List (Ecall);
7676
7677 -- Construct statement sequence for new block
7678
7679 Append_To (Stmts,
7680 Make_Implicit_If_Statement (N,
7681 Condition =>
7682 Make_Function_Call (Loc,
7683 Name => New_Occurrence_Of (
7684 RTE (RE_Timed_Out), Loc),
7685 Parameter_Associations => New_List (
7686 Make_Attribute_Reference (Loc,
7687 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7688 Attribute_Name => Name_Unchecked_Access))),
7689 Then_Statements => Tstats));
7690
7691 -- The result is the new block
7692
7693 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7694
7695 Rewrite (N,
7696 Make_Block_Statement (Loc,
7697 Declarations => New_List (
7698 Make_Object_Declaration (Loc,
7699 Defining_Identifier => Dblock_Ent,
7700 Aliased_Present => True,
7701 Object_Definition =>
7702 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7703
7704 Handled_Statement_Sequence =>
7705 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7706
7707 Analyze (N);
7708 return;
7709 end if;
7710
7711 else
7712 N_Orig := N;
7713 end if;
7714
7715 Extract_Entry (Ecall, Concval, Ename, Index);
7716 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7717
7718 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7719 Decls := Declarations (Ecall);
7720
7721 if Is_Protected_Type (Etype (Concval)) then
7722
7723 -- Get the declarations of the block expanded from the entry call
7724
7725 Decl := First (Decls);
7726 while Present (Decl)
7727 and then (Nkind (Decl) /= N_Object_Declaration
7728 or else not Is_RTE (Etype (Object_Definition (Decl)),
7729 RE_Communication_Block))
7730 loop
7731 Next (Decl);
7732 end loop;
7733
7734 pragma Assert (Present (Decl));
7735 Cancel_Param := Defining_Identifier (Decl);
7736
7737 -- Change the mode of the Protected_Entry_Call call
7738
7739 -- Protected_Entry_Call (
7740 -- Object => po._object'Access,
7741 -- E => <entry index>;
7742 -- Uninterpreted_Data => P'Address;
7743 -- Mode => Asynchronous_Call;
7744 -- Block => Bnn);
7745
7746 -- Skip assignments to temporaries created for in-out parameters
7747
7748 -- This makes unwarranted assumptions about the shape of the expanded
7749 -- tree for the call, and should be cleaned up ???
7750
7751 Stmt := First (Stmts);
7752 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7753 Next (Stmt);
7754 end loop;
7755
7756 Call := Stmt;
7757
7758 Param := First (Parameter_Associations (Call));
7759 while Present (Param)
7760 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7761 loop
7762 Next (Param);
7763 end loop;
7764
7765 pragma Assert (Present (Param));
7766 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7767 Analyze (Param);
7768
7769 -- Append an if statement to execute the abortable part
7770
7771 -- Generate:
7772 -- if Enqueued (Bnn) then
7773
7774 Append_To (Stmts,
7775 Make_Implicit_If_Statement (N,
7776 Condition =>
7777 Make_Function_Call (Loc,
7778 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7779 Parameter_Associations => New_List (
7780 New_Occurrence_Of (Cancel_Param, Loc))),
7781 Then_Statements => Astats));
7782
7783 Abortable_Block :=
7784 Make_Block_Statement (Loc,
7785 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7786 Handled_Statement_Sequence =>
7787 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7788 Has_Created_Identifier => True,
7789 Is_Asynchronous_Call_Block => True);
7790
7791 if Exception_Mechanism = Back_End_Exceptions then
7792
7793 -- Aborts are not deferred at beginning of exception handlers
7794 -- in ZCX.
7795
7796 Handler_Stmt := Make_Null_Statement (Loc);
7797
7798 else
7799 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7800 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
7801 Parameter_Associations => No_List);
7802 end if;
7803
7804 Stmts := New_List (
7805 Make_Block_Statement (Loc,
7806 Handled_Statement_Sequence =>
7807 Make_Handled_Sequence_Of_Statements (Loc,
7808 Statements => New_List (
7809 Make_Implicit_Label_Declaration (Loc,
7810 Defining_Identifier => Blk_Ent,
7811 Label_Construct => Abortable_Block),
7812 Abortable_Block),
7813
7814 -- exception
7815
7816 Exception_Handlers => New_List (
7817 Make_Implicit_Exception_Handler (Loc,
7818
7819 -- when Abort_Signal =>
7820 -- Abort_Undefer.all;
7821
7822 Exception_Choices =>
7823 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7824 Statements => New_List (Handler_Stmt))))),
7825
7826 -- if not Cancelled (Bnn) then
7827 -- triggered statements
7828 -- end if;
7829
7830 Make_Implicit_If_Statement (N,
7831 Condition => Make_Op_Not (Loc,
7832 Right_Opnd =>
7833 Make_Function_Call (Loc,
7834 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7835 Parameter_Associations => New_List (
7836 New_Occurrence_Of (Cancel_Param, Loc)))),
7837 Then_Statements => Tstats));
7838
7839 -- Asynchronous task entry call
7840
7841 else
7842 if No (Decls) then
7843 Decls := New_List;
7844 end if;
7845
7846 B := Make_Defining_Identifier (Loc, Name_uB);
7847
7848 -- Insert declaration of B in declarations of existing block
7849
7850 Prepend_To (Decls,
7851 Make_Object_Declaration (Loc,
7852 Defining_Identifier => B,
7853 Object_Definition =>
7854 New_Occurrence_Of (Standard_Boolean, Loc)));
7855
7856 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7857
7858 -- Insert declaration of C in declarations of existing block
7859
7860 Prepend_To (Decls,
7861 Make_Object_Declaration (Loc,
7862 Defining_Identifier => Cancel_Param,
7863 Object_Definition =>
7864 New_Occurrence_Of (Standard_Boolean, Loc)));
7865
7866 -- Remove and save the call to Call_Simple
7867
7868 Stmt := First (Stmts);
7869
7870 -- Skip assignments to temporaries created for in-out parameters.
7871 -- This makes unwarranted assumptions about the shape of the expanded
7872 -- tree for the call, and should be cleaned up ???
7873
7874 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7875 Next (Stmt);
7876 end loop;
7877
7878 Call := Stmt;
7879
7880 -- Create the inner block to protect the abortable part
7881
7882 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7883
7884 Prepend_To (Astats,
7885 Make_Procedure_Call_Statement (Loc,
7886 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc)));
7887
7888 Abortable_Block :=
7889 Make_Block_Statement (Loc,
7890 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7891 Handled_Statement_Sequence =>
7892 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7893 Has_Created_Identifier => True,
7894 Is_Asynchronous_Call_Block => True);
7895
7896 Insert_After (Call,
7897 Make_Block_Statement (Loc,
7898 Handled_Statement_Sequence =>
7899 Make_Handled_Sequence_Of_Statements (Loc,
7900 Statements => New_List (
7901 Make_Implicit_Label_Declaration (Loc,
7902 Defining_Identifier => Blk_Ent,
7903 Label_Construct => Abortable_Block),
7904 Abortable_Block),
7905 Exception_Handlers => Hdle)));
7906
7907 -- Create new call statement
7908
7909 Params := Parameter_Associations (Call);
7910
7911 Append_To (Params,
7912 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7913 Append_To (Params, New_Occurrence_Of (B, Loc));
7914
7915 Rewrite (Call,
7916 Make_Procedure_Call_Statement (Loc,
7917 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7918 Parameter_Associations => Params));
7919
7920 -- Construct statement sequence for new block
7921
7922 Append_To (Stmts,
7923 Make_Implicit_If_Statement (N,
7924 Condition =>
7925 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7926 Then_Statements => Tstats));
7927
7928 -- Protected the call against abort
7929
7930 Prepend_To (Stmts,
7931 Make_Procedure_Call_Statement (Loc,
7932 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
7933 Parameter_Associations => Empty_List));
7934 end if;
7935
7936 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7937
7938 -- The result is the new block
7939
7940 Rewrite (N_Orig,
7941 Make_Block_Statement (Loc,
7942 Declarations => Decls,
7943 Handled_Statement_Sequence =>
7944 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7945
7946 Analyze (N_Orig);
7947 end Expand_N_Asynchronous_Select;
7948
7949 -------------------------------------
7950 -- Expand_N_Conditional_Entry_Call --
7951 -------------------------------------
7952
7953 -- The conditional task entry call is converted to a call to
7954 -- Task_Entry_Call:
7955
7956 -- declare
7957 -- B : Boolean;
7958 -- P : parms := (parm, parm, parm);
7959
7960 -- begin
7961 -- Task_Entry_Call
7962 -- (<acceptor-task>, -- Acceptor
7963 -- <entry-index>, -- E
7964 -- P'Address, -- Uninterpreted_Data
7965 -- Conditional_Call, -- Mode
7966 -- B); -- Rendezvous_Successful
7967 -- parm := P.param;
7968 -- parm := P.param;
7969 -- ...
7970 -- if B then
7971 -- normal-statements
7972 -- else
7973 -- else-statements
7974 -- end if;
7975 -- end;
7976
7977 -- For a description of the use of P and the assignments after the call,
7978 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7979 -- conditional entry call has already been expanded (by the Expand_N_Entry
7980 -- _Call_Statement procedure) as follows:
7981
7982 -- declare
7983 -- P : parms := (parm, parm, parm);
7984 -- begin
7985 -- ... info for in-out parameters
7986 -- Call_Simple (acceptor-task, entry-index, P'Address);
7987 -- parm := P.param;
7988 -- parm := P.param;
7989 -- ...
7990 -- end;
7991
7992 -- so the task at hand is to convert the latter expansion into the former
7993
7994 -- The conditional protected entry call is converted to a call to
7995 -- Protected_Entry_Call:
7996
7997 -- declare
7998 -- P : parms := (parm, parm, parm);
7999 -- Bnn : Communications_Block;
8000
8001 -- begin
8002 -- Protected_Entry_Call
8003 -- (po._object'Access, -- Object
8004 -- <entry index>, -- E
8005 -- P'Address, -- Uninterpreted_Data
8006 -- Conditional_Call, -- Mode
8007 -- Bnn); -- Block
8008 -- parm := P.param;
8009 -- parm := P.param;
8010 -- ...
8011 -- if Cancelled (Bnn) then
8012 -- else-statements
8013 -- else
8014 -- normal-statements
8015 -- end if;
8016 -- end;
8017
8018 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
8019 -- into:
8020
8021 -- declare
8022 -- B : Boolean := False;
8023 -- C : Ada.Tags.Prim_Op_Kind;
8024 -- K : Ada.Tags.Tagged_Kind :=
8025 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8026 -- P : Parameters := (Param1 .. ParamN);
8027 -- S : Integer;
8028
8029 -- begin
8030 -- if K = Ada.Tags.TK_Limited_Tagged
8031 -- or else K = Ada.Tags.TK_Tagged
8032 -- then
8033 -- <dispatching-call>;
8034 -- <triggering-statements>
8035
8036 -- else
8037 -- S :=
8038 -- Ada.Tags.Get_Offset_Index
8039 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
8040
8041 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8042
8043 -- if C = POK_Protected_Entry
8044 -- or else C = POK_Task_Entry
8045 -- then
8046 -- Param1 := P.Param1;
8047 -- ...
8048 -- ParamN := P.ParamN;
8049 -- end if;
8050
8051 -- if B then
8052 -- if C = POK_Procedure
8053 -- or else C = POK_Protected_Procedure
8054 -- or else C = POK_Task_Procedure
8055 -- then
8056 -- <dispatching-call>;
8057 -- end if;
8058
8059 -- <triggering-statements>
8060 -- else
8061 -- <else-statements>
8062 -- end if;
8063 -- end if;
8064 -- end;
8065
8066 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
8067 Loc : constant Source_Ptr := Sloc (N);
8068 Alt : constant Node_Id := Entry_Call_Alternative (N);
8069 Blk : Node_Id := Entry_Call_Statement (Alt);
8070
8071 Actuals : List_Id;
8072 Blk_Typ : Entity_Id;
8073 Call : Node_Id;
8074 Call_Ent : Entity_Id;
8075 Conc_Typ_Stmts : List_Id;
8076 Decl : Node_Id;
8077 Decls : List_Id;
8078 Formals : List_Id;
8079 Lim_Typ_Stmts : List_Id;
8080 N_Stats : List_Id;
8081 Obj : Entity_Id;
8082 Param : Node_Id;
8083 Params : List_Id;
8084 Stmt : Node_Id;
8085 Stmts : List_Id;
8086 Transient_Blk : Node_Id;
8087 Unpack : List_Id;
8088
8089 B : Entity_Id; -- Call status flag
8090 C : Entity_Id; -- Call kind
8091 K : Entity_Id; -- Tagged kind
8092 P : Entity_Id; -- Parameter block
8093 S : Entity_Id; -- Primitive operation slot
8094
8095 begin
8096 Process_Statements_For_Controlled_Objects (N);
8097
8098 if Ada_Version >= Ada_2005
8099 and then Nkind (Blk) = N_Procedure_Call_Statement
8100 then
8101 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
8102
8103 Decls := New_List;
8104 Stmts := New_List;
8105
8106 -- Call status flag processing, generate:
8107 -- B : Boolean := False;
8108
8109 B := Build_B (Loc, Decls);
8110
8111 -- Call kind processing, generate:
8112 -- C : Ada.Tags.Prim_Op_Kind;
8113
8114 C := Build_C (Loc, Decls);
8115
8116 -- Tagged kind processing, generate:
8117 -- K : Ada.Tags.Tagged_Kind :=
8118 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8119
8120 K := Build_K (Loc, Decls, Obj);
8121
8122 -- Parameter block processing
8123
8124 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
8125 P := Parameter_Block_Pack
8126 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
8127
8128 -- Dispatch table slot processing, generate:
8129 -- S : Integer;
8130
8131 S := Build_S (Loc, Decls);
8132
8133 -- Generate:
8134 -- S := Ada.Tags.Get_Offset_Index
8135 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
8136
8137 Conc_Typ_Stmts :=
8138 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
8139
8140 -- Generate:
8141 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8142
8143 Append_To (Conc_Typ_Stmts,
8144 Make_Procedure_Call_Statement (Loc,
8145 Name =>
8146 New_Occurrence_Of (
8147 Find_Prim_Op (Etype (Etype (Obj)),
8148 Name_uDisp_Conditional_Select),
8149 Loc),
8150 Parameter_Associations =>
8151 New_List (
8152 New_Copy_Tree (Obj), -- <object>
8153 New_Occurrence_Of (S, Loc), -- S
8154 Make_Attribute_Reference (Loc, -- P'Address
8155 Prefix => New_Occurrence_Of (P, Loc),
8156 Attribute_Name => Name_Address),
8157 New_Occurrence_Of (C, Loc), -- C
8158 New_Occurrence_Of (B, Loc)))); -- B
8159
8160 -- Generate:
8161 -- if C = POK_Protected_Entry
8162 -- or else C = POK_Task_Entry
8163 -- then
8164 -- Param1 := P.Param1;
8165 -- ...
8166 -- ParamN := P.ParamN;
8167 -- end if;
8168
8169 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
8170
8171 -- Generate the if statement only when the packed parameters need
8172 -- explicit assignments to their corresponding actuals.
8173
8174 if Present (Unpack) then
8175 Append_To (Conc_Typ_Stmts,
8176 Make_Implicit_If_Statement (N,
8177 Condition =>
8178 Make_Or_Else (Loc,
8179 Left_Opnd =>
8180 Make_Op_Eq (Loc,
8181 Left_Opnd =>
8182 New_Occurrence_Of (C, Loc),
8183 Right_Opnd =>
8184 New_Occurrence_Of (RTE (
8185 RE_POK_Protected_Entry), Loc)),
8186
8187 Right_Opnd =>
8188 Make_Op_Eq (Loc,
8189 Left_Opnd =>
8190 New_Occurrence_Of (C, Loc),
8191 Right_Opnd =>
8192 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
8193
8194 Then_Statements => Unpack));
8195 end if;
8196
8197 -- Generate:
8198 -- if B then
8199 -- if C = POK_Procedure
8200 -- or else C = POK_Protected_Procedure
8201 -- or else C = POK_Task_Procedure
8202 -- then
8203 -- <dispatching-call>
8204 -- end if;
8205 -- <normal-statements>
8206 -- else
8207 -- <else-statements>
8208 -- end if;
8209
8210 N_Stats := New_Copy_List_Tree (Statements (Alt));
8211
8212 Prepend_To (N_Stats,
8213 Make_Implicit_If_Statement (N,
8214 Condition =>
8215 Make_Or_Else (Loc,
8216 Left_Opnd =>
8217 Make_Op_Eq (Loc,
8218 Left_Opnd =>
8219 New_Occurrence_Of (C, Loc),
8220 Right_Opnd =>
8221 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
8222
8223 Right_Opnd =>
8224 Make_Or_Else (Loc,
8225 Left_Opnd =>
8226 Make_Op_Eq (Loc,
8227 Left_Opnd =>
8228 New_Occurrence_Of (C, Loc),
8229 Right_Opnd =>
8230 New_Occurrence_Of (RTE (
8231 RE_POK_Protected_Procedure), Loc)),
8232
8233 Right_Opnd =>
8234 Make_Op_Eq (Loc,
8235 Left_Opnd =>
8236 New_Occurrence_Of (C, Loc),
8237 Right_Opnd =>
8238 New_Occurrence_Of (RTE (
8239 RE_POK_Task_Procedure), Loc)))),
8240
8241 Then_Statements =>
8242 New_List (Blk)));
8243
8244 Append_To (Conc_Typ_Stmts,
8245 Make_Implicit_If_Statement (N,
8246 Condition => New_Occurrence_Of (B, Loc),
8247 Then_Statements => N_Stats,
8248 Else_Statements => Else_Statements (N)));
8249
8250 -- Generate:
8251 -- <dispatching-call>;
8252 -- <triggering-statements>
8253
8254 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8255 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8256
8257 -- Generate:
8258 -- if K = Ada.Tags.TK_Limited_Tagged
8259 -- or else K = Ada.Tags.TK_Tagged
8260 -- then
8261 -- Lim_Typ_Stmts
8262 -- else
8263 -- Conc_Typ_Stmts
8264 -- end if;
8265
8266 Append_To (Stmts,
8267 Make_Implicit_If_Statement (N,
8268 Condition => Build_Dispatching_Tag_Check (K, N),
8269 Then_Statements => Lim_Typ_Stmts,
8270 Else_Statements => Conc_Typ_Stmts));
8271
8272 Rewrite (N,
8273 Make_Block_Statement (Loc,
8274 Declarations =>
8275 Decls,
8276 Handled_Statement_Sequence =>
8277 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8278
8279 -- As described above, the entry alternative is transformed into a
8280 -- block that contains the gnulli call, and possibly assignment
8281 -- statements for in-out parameters. The gnulli call may itself be
8282 -- rewritten into a transient block if some unconstrained parameters
8283 -- require it. We need to retrieve the call to complete its parameter
8284 -- list.
8285
8286 else
8287 Transient_Blk :=
8288 First_Real_Statement (Handled_Statement_Sequence (Blk));
8289
8290 if Present (Transient_Blk)
8291 and then Nkind (Transient_Blk) = N_Block_Statement
8292 then
8293 Blk := Transient_Blk;
8294 end if;
8295
8296 Stmts := Statements (Handled_Statement_Sequence (Blk));
8297 Stmt := First (Stmts);
8298 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8299 Next (Stmt);
8300 end loop;
8301
8302 Call := Stmt;
8303 Params := Parameter_Associations (Call);
8304
8305 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8306
8307 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8308
8309 Param := First (Params);
8310 while Present (Param)
8311 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8312 loop
8313 Next (Param);
8314 end loop;
8315
8316 pragma Assert (Present (Param));
8317 Rewrite (Param,
8318 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8319
8320 Analyze (Param);
8321
8322 -- Find the Communication_Block parameter for the call to the
8323 -- Cancelled function.
8324
8325 Decl := First (Declarations (Blk));
8326 while Present (Decl)
8327 and then not Is_RTE (Etype (Object_Definition (Decl)),
8328 RE_Communication_Block)
8329 loop
8330 Next (Decl);
8331 end loop;
8332
8333 -- Add an if statement to execute the else part if the call
8334 -- does not succeed (as indicated by the Cancelled predicate).
8335
8336 Append_To (Stmts,
8337 Make_Implicit_If_Statement (N,
8338 Condition => Make_Function_Call (Loc,
8339 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8340 Parameter_Associations => New_List (
8341 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8342 Then_Statements => Else_Statements (N),
8343 Else_Statements => Statements (Alt)));
8344
8345 else
8346 B := Make_Defining_Identifier (Loc, Name_uB);
8347
8348 -- Insert declaration of B in declarations of existing block
8349
8350 if No (Declarations (Blk)) then
8351 Set_Declarations (Blk, New_List);
8352 end if;
8353
8354 Prepend_To (Declarations (Blk),
8355 Make_Object_Declaration (Loc,
8356 Defining_Identifier => B,
8357 Object_Definition =>
8358 New_Occurrence_Of (Standard_Boolean, Loc)));
8359
8360 -- Create new call statement
8361
8362 Append_To (Params,
8363 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8364 Append_To (Params, New_Occurrence_Of (B, Loc));
8365
8366 Rewrite (Call,
8367 Make_Procedure_Call_Statement (Loc,
8368 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8369 Parameter_Associations => Params));
8370
8371 -- Construct statement sequence for new block
8372
8373 Append_To (Stmts,
8374 Make_Implicit_If_Statement (N,
8375 Condition => New_Occurrence_Of (B, Loc),
8376 Then_Statements => Statements (Alt),
8377 Else_Statements => Else_Statements (N)));
8378 end if;
8379
8380 -- The result is the new block
8381
8382 Rewrite (N,
8383 Make_Block_Statement (Loc,
8384 Declarations => Declarations (Blk),
8385 Handled_Statement_Sequence =>
8386 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8387 end if;
8388
8389 Analyze (N);
8390 end Expand_N_Conditional_Entry_Call;
8391
8392 ---------------------------------------
8393 -- Expand_N_Delay_Relative_Statement --
8394 ---------------------------------------
8395
8396 -- Delay statement is implemented as a procedure call to Delay_For
8397 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8398 -- simple delays imposed by the use of Protected Objects.
8399
8400 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8401 Loc : constant Source_Ptr := Sloc (N);
8402 begin
8403 Rewrite (N,
8404 Make_Procedure_Call_Statement (Loc,
8405 Name => New_Occurrence_Of (RTE (RO_CA_Delay_For), Loc),
8406 Parameter_Associations => New_List (Expression (N))));
8407 Analyze (N);
8408 end Expand_N_Delay_Relative_Statement;
8409
8410 ------------------------------------
8411 -- Expand_N_Delay_Until_Statement --
8412 ------------------------------------
8413
8414 -- Delay Until statement is implemented as a procedure call to
8415 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8416
8417 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8418 Loc : constant Source_Ptr := Sloc (N);
8419 Typ : Entity_Id;
8420
8421 begin
8422 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8423 Typ := RTE (RO_CA_Delay_Until);
8424 else
8425 Typ := RTE (RO_RT_Delay_Until);
8426 end if;
8427
8428 Rewrite (N,
8429 Make_Procedure_Call_Statement (Loc,
8430 Name => New_Occurrence_Of (Typ, Loc),
8431 Parameter_Associations => New_List (Expression (N))));
8432
8433 Analyze (N);
8434 end Expand_N_Delay_Until_Statement;
8435
8436 -------------------------
8437 -- Expand_N_Entry_Body --
8438 -------------------------
8439
8440 procedure Expand_N_Entry_Body (N : Node_Id) is
8441 begin
8442 -- Associate discriminals with the next protected operation body to be
8443 -- expanded.
8444
8445 if Present (Next_Protected_Operation (N)) then
8446 Set_Discriminals (Parent (Current_Scope));
8447 end if;
8448 end Expand_N_Entry_Body;
8449
8450 -----------------------------------
8451 -- Expand_N_Entry_Call_Statement --
8452 -----------------------------------
8453
8454 -- An entry call is expanded into GNARLI calls to implement a simple entry
8455 -- call (see Build_Simple_Entry_Call).
8456
8457 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8458 Concval : Node_Id;
8459 Ename : Node_Id;
8460 Index : Node_Id;
8461
8462 begin
8463 if No_Run_Time_Mode then
8464 Error_Msg_CRT ("entry call", N);
8465 return;
8466 end if;
8467
8468 -- If this entry call is part of an asynchronous select, don't expand it
8469 -- here; it will be expanded with the select statement. Don't expand
8470 -- timed entry calls either, as they are translated into asynchronous
8471 -- entry calls.
8472
8473 -- ??? This whole approach is questionable; it may be better to go back
8474 -- to allowing the expansion to take place and then attempting to fix it
8475 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8476 -- whether the expanded call is on a task or protected entry.
8477
8478 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8479 or else N /= Triggering_Statement (Parent (N)))
8480 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8481 or else N /= Entry_Call_Statement (Parent (N))
8482 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8483 then
8484 Extract_Entry (N, Concval, Ename, Index);
8485 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8486 end if;
8487 end Expand_N_Entry_Call_Statement;
8488
8489 --------------------------------
8490 -- Expand_N_Entry_Declaration --
8491 --------------------------------
8492
8493 -- If there are parameters, then first, each of the formals is marked by
8494 -- setting Is_Entry_Formal. Next a record type is built which is used to
8495 -- hold the parameter values. The name of this record type is entryP where
8496 -- entry is the name of the entry, with an additional corresponding access
8497 -- type called entryPA. The record type has matching components for each
8498 -- formal (the component names are the same as the formal names). For
8499 -- elementary types, the component type matches the formal type. For
8500 -- composite types, an access type is declared (with the name formalA)
8501 -- which designates the formal type, and the type of the component is this
8502 -- access type. Finally the Entry_Component of each formal is set to
8503 -- reference the corresponding record component.
8504
8505 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8506 Loc : constant Source_Ptr := Sloc (N);
8507 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8508 Components : List_Id;
8509 Formal : Node_Id;
8510 Ftype : Entity_Id;
8511 Last_Decl : Node_Id;
8512 Component : Entity_Id;
8513 Ctype : Entity_Id;
8514 Decl : Node_Id;
8515 Rec_Ent : Entity_Id;
8516 Acc_Ent : Entity_Id;
8517
8518 begin
8519 Formal := First_Formal (Entry_Ent);
8520 Last_Decl := N;
8521
8522 -- Most processing is done only if parameters are present
8523
8524 if Present (Formal) then
8525 Components := New_List;
8526
8527 -- Loop through formals
8528
8529 while Present (Formal) loop
8530 Set_Is_Entry_Formal (Formal);
8531 Component :=
8532 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8533 Set_Entry_Component (Formal, Component);
8534 Set_Entry_Formal (Component, Formal);
8535 Ftype := Etype (Formal);
8536
8537 -- Declare new access type and then append
8538
8539 Ctype := Make_Temporary (Loc, 'A');
8540 Set_Is_Param_Block_Component_Type (Ctype);
8541
8542 Decl :=
8543 Make_Full_Type_Declaration (Loc,
8544 Defining_Identifier => Ctype,
8545 Type_Definition =>
8546 Make_Access_To_Object_Definition (Loc,
8547 All_Present => True,
8548 Constant_Present => Ekind (Formal) = E_In_Parameter,
8549 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8550
8551 Insert_After (Last_Decl, Decl);
8552 Last_Decl := Decl;
8553
8554 Append_To (Components,
8555 Make_Component_Declaration (Loc,
8556 Defining_Identifier => Component,
8557 Component_Definition =>
8558 Make_Component_Definition (Loc,
8559 Aliased_Present => False,
8560 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8561
8562 Next_Formal_With_Extras (Formal);
8563 end loop;
8564
8565 -- Create the Entry_Parameter_Record declaration
8566
8567 Rec_Ent := Make_Temporary (Loc, 'P');
8568
8569 Decl :=
8570 Make_Full_Type_Declaration (Loc,
8571 Defining_Identifier => Rec_Ent,
8572 Type_Definition =>
8573 Make_Record_Definition (Loc,
8574 Component_List =>
8575 Make_Component_List (Loc,
8576 Component_Items => Components)));
8577
8578 Insert_After (Last_Decl, Decl);
8579 Last_Decl := Decl;
8580
8581 -- Construct and link in the corresponding access type
8582
8583 Acc_Ent := Make_Temporary (Loc, 'A');
8584
8585 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8586
8587 Decl :=
8588 Make_Full_Type_Declaration (Loc,
8589 Defining_Identifier => Acc_Ent,
8590 Type_Definition =>
8591 Make_Access_To_Object_Definition (Loc,
8592 All_Present => True,
8593 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8594
8595 Insert_After (Last_Decl, Decl);
8596 end if;
8597 end Expand_N_Entry_Declaration;
8598
8599 -----------------------------
8600 -- Expand_N_Protected_Body --
8601 -----------------------------
8602
8603 -- Protected bodies are expanded to the completion of the subprograms
8604 -- created for the corresponding protected type. These are a protected and
8605 -- unprotected version of each protected subprogram in the object, a
8606 -- function to calculate each entry barrier, and a procedure to execute the
8607 -- sequence of statements of each protected entry body. For example, for
8608 -- protected type ptype:
8609
8610 -- function entB
8611 -- (O : System.Address;
8612 -- E : Protected_Entry_Index)
8613 -- return Boolean
8614 -- is
8615 -- <discriminant renamings>
8616 -- <private object renamings>
8617 -- begin
8618 -- return <barrier expression>;
8619 -- end entB;
8620
8621 -- procedure pprocN (_object : in out poV;...) is
8622 -- <discriminant renamings>
8623 -- <private object renamings>
8624 -- begin
8625 -- <sequence of statements>
8626 -- end pprocN;
8627
8628 -- procedure pprocP (_object : in out poV;...) is
8629 -- procedure _clean is
8630 -- Pn : Boolean;
8631 -- begin
8632 -- ptypeS (_object, Pn);
8633 -- Unlock (_object._object'Access);
8634 -- Abort_Undefer.all;
8635 -- end _clean;
8636
8637 -- begin
8638 -- Abort_Defer.all;
8639 -- Lock (_object._object'Access);
8640 -- pprocN (_object;...);
8641 -- at end
8642 -- _clean;
8643 -- end pproc;
8644
8645 -- function pfuncN (_object : poV;...) return Return_Type is
8646 -- <discriminant renamings>
8647 -- <private object renamings>
8648 -- begin
8649 -- <sequence of statements>
8650 -- end pfuncN;
8651
8652 -- function pfuncP (_object : poV) return Return_Type is
8653 -- procedure _clean is
8654 -- begin
8655 -- Unlock (_object._object'Access);
8656 -- Abort_Undefer.all;
8657 -- end _clean;
8658
8659 -- begin
8660 -- Abort_Defer.all;
8661 -- Lock (_object._object'Access);
8662 -- return pfuncN (_object);
8663
8664 -- at end
8665 -- _clean;
8666 -- end pfunc;
8667
8668 -- procedure entE
8669 -- (O : System.Address;
8670 -- P : System.Address;
8671 -- E : Protected_Entry_Index)
8672 -- is
8673 -- <discriminant renamings>
8674 -- <private object renamings>
8675 -- type poVP is access poV;
8676 -- _Object : ptVP := ptVP!(O);
8677
8678 -- begin
8679 -- begin
8680 -- <statement sequence>
8681 -- Complete_Entry_Body (_Object._Object);
8682 -- exception
8683 -- when all others =>
8684 -- Exceptional_Complete_Entry_Body (
8685 -- _Object._Object, Get_GNAT_Exception);
8686 -- end;
8687 -- end entE;
8688
8689 -- The type poV is the record created for the protected type to hold
8690 -- the state of the protected object.
8691
8692 procedure Expand_N_Protected_Body (N : Node_Id) is
8693 Loc : constant Source_Ptr := Sloc (N);
8694 Pid : constant Entity_Id := Corresponding_Spec (N);
8695
8696 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8697 -- This flag indicates whether the lock free implementation is active
8698
8699 Current_Node : Node_Id;
8700 Disp_Op_Body : Node_Id;
8701 New_Op_Body : Node_Id;
8702 Op_Body : Node_Id;
8703 Op_Id : Entity_Id;
8704
8705 function Build_Dispatching_Subprogram_Body
8706 (N : Node_Id;
8707 Pid : Node_Id;
8708 Prot_Bod : Node_Id) return Node_Id;
8709 -- Build a dispatching version of the protected subprogram body. The
8710 -- newly generated subprogram contains a call to the original protected
8711 -- body. The following code is generated:
8712 --
8713 -- function <protected-function-name> (Param1 .. ParamN) return
8714 -- <return-type> is
8715 -- begin
8716 -- return <protected-function-name>P (Param1 .. ParamN);
8717 -- end <protected-function-name>;
8718 --
8719 -- or
8720 --
8721 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8722 -- begin
8723 -- <protected-procedure-name>P (Param1 .. ParamN);
8724 -- end <protected-procedure-name>
8725
8726 ---------------------------------------
8727 -- Build_Dispatching_Subprogram_Body --
8728 ---------------------------------------
8729
8730 function Build_Dispatching_Subprogram_Body
8731 (N : Node_Id;
8732 Pid : Node_Id;
8733 Prot_Bod : Node_Id) return Node_Id
8734 is
8735 Loc : constant Source_Ptr := Sloc (N);
8736 Actuals : List_Id;
8737 Formal : Node_Id;
8738 Spec : Node_Id;
8739 Stmts : List_Id;
8740
8741 begin
8742 -- Generate a specification without a letter suffix in order to
8743 -- override an interface function or procedure.
8744
8745 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8746
8747 -- The formal parameters become the actuals of the protected function
8748 -- or procedure call.
8749
8750 Actuals := New_List;
8751 Formal := First (Parameter_Specifications (Spec));
8752 while Present (Formal) loop
8753 Append_To (Actuals,
8754 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8755 Next (Formal);
8756 end loop;
8757
8758 if Nkind (Spec) = N_Procedure_Specification then
8759 Stmts :=
8760 New_List (
8761 Make_Procedure_Call_Statement (Loc,
8762 Name =>
8763 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8764 Parameter_Associations => Actuals));
8765
8766 else
8767 pragma Assert (Nkind (Spec) = N_Function_Specification);
8768
8769 Stmts :=
8770 New_List (
8771 Make_Simple_Return_Statement (Loc,
8772 Expression =>
8773 Make_Function_Call (Loc,
8774 Name =>
8775 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8776 Parameter_Associations => Actuals)));
8777 end if;
8778
8779 return
8780 Make_Subprogram_Body (Loc,
8781 Declarations => Empty_List,
8782 Specification => Spec,
8783 Handled_Statement_Sequence =>
8784 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8785 end Build_Dispatching_Subprogram_Body;
8786
8787 -- Start of processing for Expand_N_Protected_Body
8788
8789 begin
8790 if No_Run_Time_Mode then
8791 Error_Msg_CRT ("protected body", N);
8792 return;
8793 end if;
8794
8795 -- This is the proper body corresponding to a stub. The declarations
8796 -- must be inserted at the point of the stub, which in turn is in the
8797 -- declarative part of the parent unit.
8798
8799 if Nkind (Parent (N)) = N_Subunit then
8800 Current_Node := Corresponding_Stub (Parent (N));
8801 else
8802 Current_Node := N;
8803 end if;
8804
8805 Op_Body := First (Declarations (N));
8806
8807 -- The protected body is replaced with the bodies of its
8808 -- protected operations, and the declarations for internal objects
8809 -- that may have been created for entry family bounds.
8810
8811 Rewrite (N, Make_Null_Statement (Sloc (N)));
8812 Analyze (N);
8813
8814 while Present (Op_Body) loop
8815 case Nkind (Op_Body) is
8816 when N_Subprogram_Declaration =>
8817 null;
8818
8819 when N_Subprogram_Body =>
8820
8821 -- Do not create bodies for eliminated operations
8822
8823 if not Is_Eliminated (Defining_Entity (Op_Body))
8824 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8825 then
8826 if Lock_Free_Active then
8827 New_Op_Body :=
8828 Build_Lock_Free_Unprotected_Subprogram_Body
8829 (Op_Body, Pid);
8830 else
8831 New_Op_Body :=
8832 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8833 end if;
8834
8835 Insert_After (Current_Node, New_Op_Body);
8836 Current_Node := New_Op_Body;
8837 Analyze (New_Op_Body);
8838
8839 -- Build the corresponding protected operation. It may
8840 -- appear that this is needed only if this is a visible
8841 -- operation of the type, or if it is an interrupt handler,
8842 -- and this was the strategy used previously in GNAT.
8843
8844 -- However, the operation may be exported through a 'Access
8845 -- to an external caller. This is the common idiom in code
8846 -- that uses the Ada 2005 Timing_Events package. As a result
8847 -- we need to produce the protected body for both visible
8848 -- and private operations, as well as operations that only
8849 -- have a body in the source, and for which we create a
8850 -- declaration in the protected body itself.
8851
8852 if Present (Corresponding_Spec (Op_Body)) then
8853 if Lock_Free_Active then
8854 New_Op_Body :=
8855 Build_Lock_Free_Protected_Subprogram_Body
8856 (Op_Body, Pid, Specification (New_Op_Body));
8857 else
8858 New_Op_Body :=
8859 Build_Protected_Subprogram_Body
8860 (Op_Body, Pid, Specification (New_Op_Body));
8861 end if;
8862
8863 Insert_After (Current_Node, New_Op_Body);
8864 Analyze (New_Op_Body);
8865
8866 Current_Node := New_Op_Body;
8867
8868 -- Generate an overriding primitive operation body for
8869 -- this subprogram if the protected type implements an
8870 -- interface.
8871
8872 if Ada_Version >= Ada_2005
8873 and then
8874 Present (Interfaces (Corresponding_Record_Type (Pid)))
8875 then
8876 Disp_Op_Body :=
8877 Build_Dispatching_Subprogram_Body
8878 (Op_Body, Pid, New_Op_Body);
8879
8880 Insert_After (Current_Node, Disp_Op_Body);
8881 Analyze (Disp_Op_Body);
8882
8883 Current_Node := Disp_Op_Body;
8884 end if;
8885 end if;
8886 end if;
8887
8888 when N_Entry_Body =>
8889 Op_Id := Defining_Identifier (Op_Body);
8890 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8891
8892 Insert_After (Current_Node, New_Op_Body);
8893 Current_Node := New_Op_Body;
8894 Analyze (New_Op_Body);
8895
8896 when N_Implicit_Label_Declaration =>
8897 null;
8898
8899 when N_Itype_Reference =>
8900 Insert_After (Current_Node, New_Copy (Op_Body));
8901
8902 when N_Freeze_Entity =>
8903 New_Op_Body := New_Copy (Op_Body);
8904
8905 if Present (Entity (Op_Body))
8906 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8907 then
8908 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8909 end if;
8910
8911 Insert_After (Current_Node, New_Op_Body);
8912 Current_Node := New_Op_Body;
8913 Analyze (New_Op_Body);
8914
8915 when N_Pragma =>
8916 New_Op_Body := New_Copy (Op_Body);
8917 Insert_After (Current_Node, New_Op_Body);
8918 Current_Node := New_Op_Body;
8919 Analyze (New_Op_Body);
8920
8921 when N_Object_Declaration =>
8922 pragma Assert (not Comes_From_Source (Op_Body));
8923 New_Op_Body := New_Copy (Op_Body);
8924 Insert_After (Current_Node, New_Op_Body);
8925 Current_Node := New_Op_Body;
8926 Analyze (New_Op_Body);
8927
8928 when others =>
8929 raise Program_Error;
8930
8931 end case;
8932
8933 Next (Op_Body);
8934 end loop;
8935
8936 -- Finally, create the body of the function that maps an entry index
8937 -- into the corresponding body index, except when there is no entry, or
8938 -- in a Ravenscar-like profile.
8939
8940 if Corresponding_Runtime_Package (Pid) =
8941 System_Tasking_Protected_Objects_Entries
8942 then
8943 New_Op_Body := Build_Find_Body_Index (Pid);
8944 Insert_After (Current_Node, New_Op_Body);
8945 Current_Node := New_Op_Body;
8946 Analyze (New_Op_Body);
8947 end if;
8948
8949 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8950 -- protected body. At this point all wrapper specs have been created,
8951 -- frozen and included in the dispatch table for the protected type.
8952
8953 if Ada_Version >= Ada_2005 then
8954 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8955 end if;
8956 end Expand_N_Protected_Body;
8957
8958 -----------------------------------------
8959 -- Expand_N_Protected_Type_Declaration --
8960 -----------------------------------------
8961
8962 -- First we create a corresponding record type declaration used to
8963 -- represent values of this protected type.
8964 -- The general form of this type declaration is
8965
8966 -- type poV (discriminants) is record
8967 -- _Object : aliased <kind>Protection
8968 -- [(<entry count> [, <handler count>])];
8969 -- [entry_family : array (bounds) of Void;]
8970 -- <private data fields>
8971 -- end record;
8972
8973 -- The discriminants are present only if the corresponding protected type
8974 -- has discriminants, and they exactly mirror the protected type
8975 -- discriminants. The private data fields similarly mirror the private
8976 -- declarations of the protected type.
8977
8978 -- The Object field is always present. It contains RTS specific data used
8979 -- to control the protected object. It is declared as Aliased so that it
8980 -- can be passed as a pointer to the RTS. This allows the protected record
8981 -- to be referenced within RTS data structures. An appropriate Protection
8982 -- type and discriminant are generated.
8983
8984 -- The Service field is present for protected objects with entries. It
8985 -- contains sufficient information to allow the entry service procedure for
8986 -- this object to be called when the object is not known till runtime.
8987
8988 -- One entry_family component is present for each entry family in the
8989 -- task definition (see Expand_N_Task_Type_Declaration).
8990
8991 -- When a protected object is declared, an instance of the protected type
8992 -- value record is created. The elaboration of this declaration creates the
8993 -- correct bounds for the entry families, and also evaluates the priority
8994 -- expression if needed. The initialization routine for the protected type
8995 -- itself then calls Initialize_Protection with appropriate parameters to
8996 -- initialize the value of the Task_Id field. Install_Handlers may be also
8997 -- called if a pragma Attach_Handler applies.
8998
8999 -- Note: this record is passed to the subprograms created by the expansion
9000 -- of protected subprograms and entries. It is an in parameter to protected
9001 -- functions and an in out parameter to procedures and entry bodies. The
9002 -- Entity_Id for this created record type is placed in the
9003 -- Corresponding_Record_Type field of the associated protected type entity.
9004
9005 -- Next we create a procedure specifications for protected subprograms and
9006 -- entry bodies. For each protected subprograms two subprograms are
9007 -- created, an unprotected and a protected version. The unprotected version
9008 -- is called from within other operations of the same protected object.
9009
9010 -- We also build the call to register the procedure if a pragma
9011 -- Interrupt_Handler applies.
9012
9013 -- A single subprogram is created to service all entry bodies; it has an
9014 -- additional boolean out parameter indicating that the previous entry call
9015 -- made by the current task was serviced immediately, i.e. not by proxy.
9016 -- The O parameter contains a pointer to a record object of the type
9017 -- described above. An untyped interface is used here to allow this
9018 -- procedure to be called in places where the type of the object to be
9019 -- serviced is not known. This must be done, for example, when a call that
9020 -- may have been requeued is cancelled; the corresponding object must be
9021 -- serviced, but which object that is not known till runtime.
9022
9023 -- procedure ptypeS
9024 -- (O : System.Address; P : out Boolean);
9025 -- procedure pprocN (_object : in out poV);
9026 -- procedure pproc (_object : in out poV);
9027 -- function pfuncN (_object : poV);
9028 -- function pfunc (_object : poV);
9029 -- ...
9030
9031 -- Note that this must come after the record type declaration, since
9032 -- the specs refer to this type.
9033
9034 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
9035 Discr_Map : constant Elist_Id := New_Elmt_List;
9036 Loc : constant Source_Ptr := Sloc (N);
9037 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
9038
9039 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
9040 -- This flag indicates whether the lock free implementation is active
9041
9042 Pdef : constant Node_Id := Protected_Definition (N);
9043 -- This contains two lists; one for visible and one for private decls
9044
9045 Body_Arr : Node_Id;
9046 Body_Id : Entity_Id;
9047 Cdecls : List_Id;
9048 Comp : Node_Id;
9049 Current_Node : Node_Id := N;
9050 E_Count : Int;
9051 Entries_Aggr : Node_Id;
9052 New_Priv : Node_Id;
9053 Object_Comp : Node_Id;
9054 Priv : Node_Id;
9055 Rec_Decl : Node_Id;
9056
9057 procedure Check_Inlining (Subp : Entity_Id);
9058 -- If the original operation has a pragma Inline, propagate the flag
9059 -- to the internal body, for possible inlining later on. The source
9060 -- operation is invisible to the back-end and is never actually called.
9061
9062 function Discriminated_Size (Comp : Entity_Id) return Boolean;
9063 -- If a component size is not static then a warning will be emitted
9064 -- in Ravenscar or other restricted contexts. When a component is non-
9065 -- static because of a discriminant constraint we can specialize the
9066 -- warning by mentioning discriminants explicitly.
9067
9068 procedure Expand_Entry_Declaration (Decl : Node_Id);
9069 -- Create the entry barrier and the procedure body for entry declaration
9070 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
9071
9072 function Static_Component_Size (Comp : Entity_Id) return Boolean;
9073 -- When compiling under the Ravenscar profile, private components must
9074 -- have a static size, or else a protected object will require heap
9075 -- allocation, violating the corresponding restriction. It is preferable
9076 -- to make this check here, because it provides a better error message
9077 -- than the back-end, which refers to the object as a whole.
9078
9079 procedure Register_Handler;
9080 -- For a protected operation that is an interrupt handler, add the
9081 -- freeze action that will register it as such.
9082
9083 --------------------
9084 -- Check_Inlining --
9085 --------------------
9086
9087 procedure Check_Inlining (Subp : Entity_Id) is
9088 begin
9089 if Is_Inlined (Subp) then
9090 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
9091 Set_Is_Inlined (Subp, False);
9092 end if;
9093 end Check_Inlining;
9094
9095 ------------------------
9096 -- Discriminated_Size --
9097 ------------------------
9098
9099 function Discriminated_Size (Comp : Entity_Id) return Boolean is
9100 Typ : constant Entity_Id := Etype (Comp);
9101 Index : Node_Id;
9102
9103 function Non_Static_Bound (Bound : Node_Id) return Boolean;
9104 -- Check whether the bound of an index is non-static and does denote
9105 -- a discriminant, in which case any protected object of the type
9106 -- will have a non-static size.
9107
9108 ----------------------
9109 -- Non_Static_Bound --
9110 ----------------------
9111
9112 function Non_Static_Bound (Bound : Node_Id) return Boolean is
9113 begin
9114 if Is_OK_Static_Expression (Bound) then
9115 return False;
9116
9117 elsif Is_Entity_Name (Bound)
9118 and then Present (Discriminal_Link (Entity (Bound)))
9119 then
9120 return False;
9121
9122 else
9123 return True;
9124 end if;
9125 end Non_Static_Bound;
9126
9127 -- Start of processing for Discriminated_Size
9128
9129 begin
9130 if not Is_Array_Type (Typ) then
9131 return False;
9132 end if;
9133
9134 if Ekind (Typ) = E_Array_Subtype then
9135 Index := First_Index (Typ);
9136 while Present (Index) loop
9137 if Non_Static_Bound (Low_Bound (Index))
9138 or else Non_Static_Bound (High_Bound (Index))
9139 then
9140 return False;
9141 end if;
9142
9143 Next_Index (Index);
9144 end loop;
9145
9146 return True;
9147 end if;
9148
9149 return False;
9150 end Discriminated_Size;
9151
9152 ---------------------------
9153 -- Static_Component_Size --
9154 ---------------------------
9155
9156 function Static_Component_Size (Comp : Entity_Id) return Boolean is
9157 Typ : constant Entity_Id := Etype (Comp);
9158 C : Entity_Id;
9159
9160 begin
9161 if Is_Scalar_Type (Typ) then
9162 return True;
9163
9164 elsif Is_Array_Type (Typ) then
9165 return Compile_Time_Known_Bounds (Typ);
9166
9167 elsif Is_Record_Type (Typ) then
9168 C := First_Component (Typ);
9169 while Present (C) loop
9170 if not Static_Component_Size (C) then
9171 return False;
9172 end if;
9173
9174 Next_Component (C);
9175 end loop;
9176
9177 return True;
9178
9179 -- Any other type will be checked by the back-end
9180
9181 else
9182 return True;
9183 end if;
9184 end Static_Component_Size;
9185
9186 ------------------------------
9187 -- Expand_Entry_Declaration --
9188 ------------------------------
9189
9190 procedure Expand_Entry_Declaration (Decl : Node_Id) is
9191 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
9192 Bar_Id : Entity_Id;
9193 Bod_Id : Entity_Id;
9194 Subp : Node_Id;
9195
9196 begin
9197 E_Count := E_Count + 1;
9198
9199 -- Create the protected body subprogram
9200
9201 Bod_Id :=
9202 Make_Defining_Identifier (Loc,
9203 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
9204 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
9205
9206 Subp :=
9207 Make_Subprogram_Declaration (Loc,
9208 Specification =>
9209 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
9210
9211 Insert_After (Current_Node, Subp);
9212 Current_Node := Subp;
9213
9214 Analyze (Subp);
9215
9216 -- Build a wrapper procedure to handle contract cases, preconditions,
9217 -- and postconditions.
9218
9219 Build_Contract_Wrapper (Ent_Id, N);
9220
9221 -- Create the barrier function
9222
9223 Bar_Id :=
9224 Make_Defining_Identifier (Loc,
9225 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
9226 Set_Barrier_Function (Ent_Id, Bar_Id);
9227
9228 Subp :=
9229 Make_Subprogram_Declaration (Loc,
9230 Specification =>
9231 Build_Barrier_Function_Specification (Loc, Bar_Id));
9232 Set_Is_Entry_Barrier_Function (Subp);
9233
9234 Insert_After (Current_Node, Subp);
9235 Current_Node := Subp;
9236
9237 Analyze (Subp);
9238
9239 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
9240 Set_Scope (Bar_Id, Scope (Ent_Id));
9241
9242 -- Collect pointers to the protected subprogram and the barrier
9243 -- of the current entry, for insertion into Entry_Bodies_Array.
9244
9245 Append_To (Expressions (Entries_Aggr),
9246 Make_Aggregate (Loc,
9247 Expressions => New_List (
9248 Make_Attribute_Reference (Loc,
9249 Prefix => New_Occurrence_Of (Bar_Id, Loc),
9250 Attribute_Name => Name_Unrestricted_Access),
9251 Make_Attribute_Reference (Loc,
9252 Prefix => New_Occurrence_Of (Bod_Id, Loc),
9253 Attribute_Name => Name_Unrestricted_Access))));
9254 end Expand_Entry_Declaration;
9255
9256 ----------------------
9257 -- Register_Handler --
9258 ----------------------
9259
9260 procedure Register_Handler is
9261
9262 -- All semantic checks already done in Sem_Prag
9263
9264 Prot_Proc : constant Entity_Id :=
9265 Defining_Unit_Name (Specification (Current_Node));
9266
9267 Proc_Address : constant Node_Id :=
9268 Make_Attribute_Reference (Loc,
9269 Prefix =>
9270 New_Occurrence_Of (Prot_Proc, Loc),
9271 Attribute_Name => Name_Address);
9272
9273 RTS_Call : constant Entity_Id :=
9274 Make_Procedure_Call_Statement (Loc,
9275 Name =>
9276 New_Occurrence_Of
9277 (RTE (RE_Register_Interrupt_Handler), Loc),
9278 Parameter_Associations => New_List (Proc_Address));
9279 begin
9280 Append_Freeze_Action (Prot_Proc, RTS_Call);
9281 end Register_Handler;
9282
9283 -- Local variables
9284
9285 Sub : Node_Id;
9286
9287 -- Start of processing for Expand_N_Protected_Type_Declaration
9288
9289 begin
9290 if Present (Corresponding_Record_Type (Prot_Typ)) then
9291 return;
9292 else
9293 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9294 end if;
9295
9296 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9297
9298 Qualify_Entity_Names (N);
9299
9300 -- If the type has discriminants, their occurrences in the declaration
9301 -- have been replaced by the corresponding discriminals. For components
9302 -- that are constrained by discriminants, their homologues in the
9303 -- corresponding record type must refer to the discriminants of that
9304 -- record, so we must apply a new renaming to subtypes_indications:
9305
9306 -- protected discriminant => discriminal => record discriminant
9307
9308 -- This replacement is not applied to default expressions, for which
9309 -- the discriminal is correct.
9310
9311 if Has_Discriminants (Prot_Typ) then
9312 declare
9313 Disc : Entity_Id;
9314 Decl : Node_Id;
9315
9316 begin
9317 Disc := First_Discriminant (Prot_Typ);
9318 Decl := First (Discriminant_Specifications (Rec_Decl));
9319 while Present (Disc) loop
9320 Append_Elmt (Discriminal (Disc), Discr_Map);
9321 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9322 Next_Discriminant (Disc);
9323 Next (Decl);
9324 end loop;
9325 end;
9326 end if;
9327
9328 -- Fill in the component declarations
9329
9330 -- Add components for entry families. For each entry family, create an
9331 -- anonymous type declaration with the same size, and analyze the type.
9332
9333 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9334
9335 pragma Assert (Present (Pdef));
9336
9337 -- Add private field components
9338
9339 if Present (Private_Declarations (Pdef)) then
9340 Priv := First (Private_Declarations (Pdef));
9341 while Present (Priv) loop
9342 if Nkind (Priv) = N_Component_Declaration then
9343 if not Static_Component_Size (Defining_Identifier (Priv)) then
9344
9345 -- When compiling for a restricted profile, the private
9346 -- components must have a static size. If not, this is an
9347 -- error for a single protected declaration, and rates a
9348 -- warning on a protected type declaration.
9349
9350 if not Comes_From_Source (Prot_Typ) then
9351
9352 -- It's ok to be checking this restriction at expansion
9353 -- time, because this is only for the restricted profile,
9354 -- which is not subject to strict RM conformance, so it
9355 -- is OK to miss this check in -gnatc mode.
9356
9357 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9358 Check_Restriction
9359 (No_Implicit_Protected_Object_Allocations, Priv);
9360
9361 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9362 if not Discriminated_Size (Defining_Identifier (Priv))
9363 then
9364 -- Any object of the type will be non-static.
9365
9366 Error_Msg_N ("component has non-static size??", Priv);
9367 Error_Msg_NE
9368 ("\creation of protected object of type& will "
9369 & "violate restriction "
9370 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9371 else
9372
9373 -- Object will be non-static if discriminants are.
9374
9375 Error_Msg_NE
9376 ("creation of protected object of type& with "
9377 & "non-static discriminants will violate"
9378 & " restriction No_Implicit_Heap_Allocations??",
9379 Priv, Prot_Typ);
9380 end if;
9381
9382 -- Likewise for No_Implicit_Protected_Object_Allocations
9383
9384 elsif Restriction_Active
9385 (No_Implicit_Protected_Object_Allocations)
9386 then
9387 if not Discriminated_Size (Defining_Identifier (Priv))
9388 then
9389 -- Any object of the type will be non-static.
9390
9391 Error_Msg_N ("component has non-static size??", Priv);
9392 Error_Msg_NE
9393 ("\creation of protected object of type& will "
9394 & "violate restriction "
9395 & "No_Implicit_Protected_Object_Allocations??",
9396 Priv, Prot_Typ);
9397 else
9398 -- Object will be non-static if discriminants are.
9399
9400 Error_Msg_NE
9401 ("creation of protected object of type& with "
9402 & "non-static discriminants will violate "
9403 & "restriction "
9404 & "No_Implicit_Protected_Object_Allocations??",
9405 Priv, Prot_Typ);
9406 end if;
9407 end if;
9408 end if;
9409
9410 -- The component definition consists of a subtype indication,
9411 -- or (in Ada 2005) an access definition. Make a copy of the
9412 -- proper definition.
9413
9414 declare
9415 Old_Comp : constant Node_Id := Component_Definition (Priv);
9416 Oent : constant Entity_Id := Defining_Identifier (Priv);
9417 Nent : constant Entity_Id :=
9418 Make_Defining_Identifier (Sloc (Oent),
9419 Chars => Chars (Oent));
9420 New_Comp : Node_Id;
9421
9422 begin
9423 if Present (Subtype_Indication (Old_Comp)) then
9424 New_Comp :=
9425 Make_Component_Definition (Sloc (Oent),
9426 Aliased_Present => False,
9427 Subtype_Indication =>
9428 New_Copy_Tree
9429 (Subtype_Indication (Old_Comp), Discr_Map));
9430 else
9431 New_Comp :=
9432 Make_Component_Definition (Sloc (Oent),
9433 Aliased_Present => False,
9434 Access_Definition =>
9435 New_Copy_Tree
9436 (Access_Definition (Old_Comp), Discr_Map));
9437 end if;
9438
9439 New_Priv :=
9440 Make_Component_Declaration (Loc,
9441 Defining_Identifier => Nent,
9442 Component_Definition => New_Comp,
9443 Expression => Expression (Priv));
9444
9445 Set_Has_Per_Object_Constraint (Nent,
9446 Has_Per_Object_Constraint (Oent));
9447
9448 Append_To (Cdecls, New_Priv);
9449 end;
9450
9451 elsif Nkind (Priv) = N_Subprogram_Declaration then
9452
9453 -- Make the unprotected version of the subprogram available
9454 -- for expansion of intra object calls. There is need for
9455 -- a protected version only if the subprogram is an interrupt
9456 -- handler, otherwise this operation can only be called from
9457 -- within the body.
9458
9459 Sub :=
9460 Make_Subprogram_Declaration (Loc,
9461 Specification =>
9462 Build_Protected_Sub_Specification
9463 (Priv, Prot_Typ, Unprotected_Mode));
9464
9465 Insert_After (Current_Node, Sub);
9466 Analyze (Sub);
9467
9468 Set_Protected_Body_Subprogram
9469 (Defining_Unit_Name (Specification (Priv)),
9470 Defining_Unit_Name (Specification (Sub)));
9471 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9472 Current_Node := Sub;
9473
9474 Sub :=
9475 Make_Subprogram_Declaration (Loc,
9476 Specification =>
9477 Build_Protected_Sub_Specification
9478 (Priv, Prot_Typ, Protected_Mode));
9479
9480 Insert_After (Current_Node, Sub);
9481 Analyze (Sub);
9482 Current_Node := Sub;
9483
9484 if Is_Interrupt_Handler
9485 (Defining_Unit_Name (Specification (Priv)))
9486 then
9487 if not Restricted_Profile then
9488 Register_Handler;
9489 end if;
9490 end if;
9491 end if;
9492
9493 Next (Priv);
9494 end loop;
9495 end if;
9496
9497 -- Except for the lock-free implementation, append the _Object field
9498 -- with the right type to the component list. We need to compute the
9499 -- number of entries, and in some cases the number of Attach_Handler
9500 -- pragmas.
9501
9502 if not Lock_Free_Active then
9503 declare
9504 Entry_Count_Expr : constant Node_Id :=
9505 Build_Entry_Count_Expression
9506 (Prot_Typ, Cdecls, Loc);
9507 Num_Attach_Handler : Int := 0;
9508 Protection_Subtype : Node_Id;
9509 Ritem : Node_Id;
9510
9511 begin
9512 if Has_Attach_Handler (Prot_Typ) then
9513 Ritem := First_Rep_Item (Prot_Typ);
9514 while Present (Ritem) loop
9515 if Nkind (Ritem) = N_Pragma
9516 and then Pragma_Name (Ritem) = Name_Attach_Handler
9517 then
9518 Num_Attach_Handler := Num_Attach_Handler + 1;
9519 end if;
9520
9521 Next_Rep_Item (Ritem);
9522 end loop;
9523 end if;
9524
9525 -- Determine the proper protection type. There are two special
9526 -- cases: 1) when the protected type has dynamic interrupt
9527 -- handlers, and 2) when it has static handlers and we use a
9528 -- restricted profile.
9529
9530 if Has_Attach_Handler (Prot_Typ)
9531 and then not Restricted_Profile
9532 then
9533 Protection_Subtype :=
9534 Make_Subtype_Indication (Loc,
9535 Subtype_Mark =>
9536 New_Occurrence_Of
9537 (RTE (RE_Static_Interrupt_Protection), Loc),
9538 Constraint =>
9539 Make_Index_Or_Discriminant_Constraint (Loc,
9540 Constraints => New_List (
9541 Entry_Count_Expr,
9542 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9543
9544 elsif Has_Interrupt_Handler (Prot_Typ)
9545 and then not Restriction_Active (No_Dynamic_Attachment)
9546 then
9547 Protection_Subtype :=
9548 Make_Subtype_Indication (Loc,
9549 Subtype_Mark =>
9550 New_Occurrence_Of
9551 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9552 Constraint =>
9553 Make_Index_Or_Discriminant_Constraint (Loc,
9554 Constraints => New_List (Entry_Count_Expr)));
9555
9556 else
9557 case Corresponding_Runtime_Package (Prot_Typ) is
9558 when System_Tasking_Protected_Objects_Entries =>
9559 Protection_Subtype :=
9560 Make_Subtype_Indication (Loc,
9561 Subtype_Mark =>
9562 New_Occurrence_Of
9563 (RTE (RE_Protection_Entries), Loc),
9564 Constraint =>
9565 Make_Index_Or_Discriminant_Constraint (Loc,
9566 Constraints => New_List (Entry_Count_Expr)));
9567
9568 when System_Tasking_Protected_Objects_Single_Entry =>
9569 Protection_Subtype :=
9570 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9571
9572 when System_Tasking_Protected_Objects =>
9573 Protection_Subtype :=
9574 New_Occurrence_Of (RTE (RE_Protection), Loc);
9575
9576 when others =>
9577 raise Program_Error;
9578 end case;
9579 end if;
9580
9581 Object_Comp :=
9582 Make_Component_Declaration (Loc,
9583 Defining_Identifier =>
9584 Make_Defining_Identifier (Loc, Name_uObject),
9585 Component_Definition =>
9586 Make_Component_Definition (Loc,
9587 Aliased_Present => True,
9588 Subtype_Indication => Protection_Subtype));
9589 end;
9590
9591 -- Put the _Object component after the private component so that it
9592 -- be finalized early as required by 9.4 (20)
9593
9594 Append_To (Cdecls, Object_Comp);
9595 end if;
9596
9597 Insert_After (Current_Node, Rec_Decl);
9598 Current_Node := Rec_Decl;
9599
9600 -- Analyze the record declaration immediately after construction,
9601 -- because the initialization procedure is needed for single object
9602 -- declarations before the next entity is analyzed (the freeze call
9603 -- that generates this initialization procedure is found below).
9604
9605 Analyze (Rec_Decl, Suppress => All_Checks);
9606
9607 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9608 -- the corresponding record is frozen. If any wrappers are generated,
9609 -- Current_Node is updated accordingly.
9610
9611 if Ada_Version >= Ada_2005 then
9612 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9613 end if;
9614
9615 -- Collect pointers to entry bodies and their barriers, to be placed
9616 -- in the Entry_Bodies_Array for the type. For each entry/family we
9617 -- add an expression to the aggregate which is the initial value of
9618 -- this array. The array is declared after all protected subprograms.
9619
9620 if Has_Entries (Prot_Typ) then
9621 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9622 else
9623 Entries_Aggr := Empty;
9624 end if;
9625
9626 -- Build two new procedure specifications for each protected subprogram;
9627 -- one to call from outside the object and one to call from inside.
9628 -- Build a barrier function and an entry body action procedure
9629 -- specification for each protected entry. Initialize the entry body
9630 -- array. If subprogram is flagged as eliminated, do not generate any
9631 -- internal operations.
9632
9633 E_Count := 0;
9634 Comp := First (Visible_Declarations (Pdef));
9635 while Present (Comp) loop
9636 if Nkind (Comp) = N_Subprogram_Declaration then
9637 Sub :=
9638 Make_Subprogram_Declaration (Loc,
9639 Specification =>
9640 Build_Protected_Sub_Specification
9641 (Comp, Prot_Typ, Unprotected_Mode));
9642
9643 Insert_After (Current_Node, Sub);
9644 Analyze (Sub);
9645
9646 Set_Protected_Body_Subprogram
9647 (Defining_Unit_Name (Specification (Comp)),
9648 Defining_Unit_Name (Specification (Sub)));
9649 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9650
9651 -- Make the protected version of the subprogram available for
9652 -- expansion of external calls.
9653
9654 Current_Node := Sub;
9655
9656 Sub :=
9657 Make_Subprogram_Declaration (Loc,
9658 Specification =>
9659 Build_Protected_Sub_Specification
9660 (Comp, Prot_Typ, Protected_Mode));
9661
9662 Insert_After (Current_Node, Sub);
9663 Analyze (Sub);
9664
9665 Current_Node := Sub;
9666
9667 -- Generate an overriding primitive operation specification for
9668 -- this subprogram if the protected type implements an interface.
9669
9670 if Ada_Version >= Ada_2005
9671 and then
9672 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9673 then
9674 Sub :=
9675 Make_Subprogram_Declaration (Loc,
9676 Specification =>
9677 Build_Protected_Sub_Specification
9678 (Comp, Prot_Typ, Dispatching_Mode));
9679
9680 Insert_After (Current_Node, Sub);
9681 Analyze (Sub);
9682
9683 Current_Node := Sub;
9684 end if;
9685
9686 -- If a pragma Interrupt_Handler applies, build and add a call to
9687 -- Register_Interrupt_Handler to the freezing actions of the
9688 -- protected version (Current_Node) of the subprogram:
9689
9690 -- system.interrupts.register_interrupt_handler
9691 -- (prot_procP'address);
9692
9693 if not Restricted_Profile
9694 and then Is_Interrupt_Handler
9695 (Defining_Unit_Name (Specification (Comp)))
9696 then
9697 Register_Handler;
9698 end if;
9699
9700 elsif Nkind (Comp) = N_Entry_Declaration then
9701 Expand_Entry_Declaration (Comp);
9702 end if;
9703
9704 Next (Comp);
9705 end loop;
9706
9707 -- If there are some private entry declarations, expand it as if they
9708 -- were visible entries.
9709
9710 if Present (Private_Declarations (Pdef)) then
9711 Comp := First (Private_Declarations (Pdef));
9712 while Present (Comp) loop
9713 if Nkind (Comp) = N_Entry_Declaration then
9714 Expand_Entry_Declaration (Comp);
9715 end if;
9716
9717 Next (Comp);
9718 end loop;
9719 end if;
9720
9721 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9722 -- all protected subprograms have been collected.
9723
9724 if Has_Entries (Prot_Typ) then
9725 Body_Id :=
9726 Make_Defining_Identifier (Sloc (Prot_Typ),
9727 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9728
9729 case Corresponding_Runtime_Package (Prot_Typ) is
9730 when System_Tasking_Protected_Objects_Entries =>
9731 Body_Arr :=
9732 Make_Object_Declaration (Loc,
9733 Defining_Identifier => Body_Id,
9734 Aliased_Present => True,
9735 Object_Definition =>
9736 Make_Subtype_Indication (Loc,
9737 Subtype_Mark =>
9738 New_Occurrence_Of
9739 (RTE (RE_Protected_Entry_Body_Array), Loc),
9740 Constraint =>
9741 Make_Index_Or_Discriminant_Constraint (Loc,
9742 Constraints => New_List (
9743 Make_Range (Loc,
9744 Make_Integer_Literal (Loc, 1),
9745 Make_Integer_Literal (Loc, E_Count))))),
9746 Expression => Entries_Aggr);
9747
9748 when System_Tasking_Protected_Objects_Single_Entry =>
9749 Body_Arr :=
9750 Make_Object_Declaration (Loc,
9751 Defining_Identifier => Body_Id,
9752 Aliased_Present => True,
9753 Object_Definition =>
9754 New_Occurrence_Of (RTE (RE_Entry_Body), Loc),
9755 Expression => Remove_Head (Expressions (Entries_Aggr)));
9756
9757 when others =>
9758 raise Program_Error;
9759 end case;
9760
9761 -- A pointer to this array will be placed in the corresponding record
9762 -- by its initialization procedure so this needs to be analyzed here.
9763
9764 Insert_After (Current_Node, Body_Arr);
9765 Current_Node := Body_Arr;
9766 Analyze (Body_Arr);
9767
9768 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9769
9770 -- Finally, build the function that maps an entry index into the
9771 -- corresponding body. A pointer to this function is placed in each
9772 -- object of the type. Except for a ravenscar-like profile (no abort,
9773 -- no entry queue, 1 entry)
9774
9775 if Corresponding_Runtime_Package (Prot_Typ) =
9776 System_Tasking_Protected_Objects_Entries
9777 then
9778 Sub :=
9779 Make_Subprogram_Declaration (Loc,
9780 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9781 Insert_After (Current_Node, Sub);
9782 Analyze (Sub);
9783 end if;
9784 end if;
9785 end Expand_N_Protected_Type_Declaration;
9786
9787 --------------------------------
9788 -- Expand_N_Requeue_Statement --
9789 --------------------------------
9790
9791 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9792 -- operations, depending on the source and destination (task or protected
9793 -- object). A dispatching requeue statement is expanded into a call to the
9794 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9795 -- jump around the remainder of processing for the original entry and, if
9796 -- the destination is (different) protected object, to attempt to service
9797 -- it. The following illustrates the various cases:
9798
9799 -- procedure entE
9800 -- (O : System.Address;
9801 -- P : System.Address;
9802 -- E : Protected_Entry_Index)
9803 -- is
9804 -- <discriminant renamings>
9805 -- <private object renamings>
9806 -- type poVP is access poV;
9807 -- _object : ptVP := ptVP!(O);
9808
9809 -- begin
9810 -- begin
9811 -- <start of statement sequence for entry>
9812
9813 -- -- Requeue from one protected entry body to another protected
9814 -- -- entry.
9815
9816 -- Requeue_Protected_Entry (
9817 -- _object._object'Access,
9818 -- new._object'Access,
9819 -- E,
9820 -- Abort_Present);
9821 -- return;
9822
9823 -- <some more of the statement sequence for entry>
9824
9825 -- -- Requeue from an entry body to a task entry
9826
9827 -- Requeue_Protected_To_Task_Entry (
9828 -- New._task_id,
9829 -- E,
9830 -- Abort_Present);
9831 -- return;
9832
9833 -- <rest of statement sequence for entry>
9834 -- Complete_Entry_Body (_object._object);
9835
9836 -- exception
9837 -- when all others =>
9838 -- Exceptional_Complete_Entry_Body (
9839 -- _object._object, Get_GNAT_Exception);
9840 -- end;
9841 -- end entE;
9842
9843 -- Requeue of a task entry call to a task entry
9844
9845 -- Accept_Call (E, Ann);
9846 -- <start of statement sequence for accept statement>
9847 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9848 -- goto Lnn;
9849 -- <rest of statement sequence for accept statement>
9850 -- <<Lnn>>
9851 -- Complete_Rendezvous;
9852
9853 -- exception
9854 -- when all others =>
9855 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9856
9857 -- Requeue of a task entry call to a protected entry
9858
9859 -- Accept_Call (E, Ann);
9860 -- <start of statement sequence for accept statement>
9861 -- Requeue_Task_To_Protected_Entry (
9862 -- new._object'Access,
9863 -- E,
9864 -- Abort_Present);
9865 -- newS (new, Pnn);
9866 -- goto Lnn;
9867 -- <rest of statement sequence for accept statement>
9868 -- <<Lnn>>
9869 -- Complete_Rendezvous;
9870
9871 -- exception
9872 -- when all others =>
9873 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9874
9875 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9876 -- marked by pragma Implemented (XXX, By_Entry).
9877
9878 -- The requeue is inside a protected entry:
9879
9880 -- procedure entE
9881 -- (O : System.Address;
9882 -- P : System.Address;
9883 -- E : Protected_Entry_Index)
9884 -- is
9885 -- <discriminant renamings>
9886 -- <private object renamings>
9887 -- type poVP is access poV;
9888 -- _object : ptVP := ptVP!(O);
9889
9890 -- begin
9891 -- begin
9892 -- <start of statement sequence for entry>
9893
9894 -- _Disp_Requeue
9895 -- (<interface class-wide object>,
9896 -- True,
9897 -- _object'Address,
9898 -- Ada.Tags.Get_Offset_Index
9899 -- (Tag (_object),
9900 -- <interface dispatch table index of target entry>),
9901 -- Abort_Present);
9902 -- return;
9903
9904 -- <rest of statement sequence for entry>
9905 -- Complete_Entry_Body (_object._object);
9906
9907 -- exception
9908 -- when all others =>
9909 -- Exceptional_Complete_Entry_Body (
9910 -- _object._object, Get_GNAT_Exception);
9911 -- end;
9912 -- end entE;
9913
9914 -- The requeue is inside a task entry:
9915
9916 -- Accept_Call (E, Ann);
9917 -- <start of statement sequence for accept statement>
9918 -- _Disp_Requeue
9919 -- (<interface class-wide object>,
9920 -- False,
9921 -- null,
9922 -- Ada.Tags.Get_Offset_Index
9923 -- (Tag (_object),
9924 -- <interface dispatch table index of target entrt>),
9925 -- Abort_Present);
9926 -- newS (new, Pnn);
9927 -- goto Lnn;
9928 -- <rest of statement sequence for accept statement>
9929 -- <<Lnn>>
9930 -- Complete_Rendezvous;
9931
9932 -- exception
9933 -- when all others =>
9934 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9935
9936 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9937 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9938 -- statement is replaced by a dispatching call with actual parameters taken
9939 -- from the inner-most accept statement or entry body.
9940
9941 -- Target.Primitive (Param1, ..., ParamN);
9942
9943 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9944 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9945 -- at all.
9946
9947 -- declare
9948 -- S : constant Offset_Index :=
9949 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9950 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9951
9952 -- begin
9953 -- if C = POK_Protected_Entry
9954 -- or else C = POK_Task_Entry
9955 -- then
9956 -- <statements for dispatching requeue>
9957
9958 -- elsif C = POK_Protected_Procedure then
9959 -- <dispatching call equivalent>
9960
9961 -- else
9962 -- raise Program_Error;
9963 -- end if;
9964 -- end;
9965
9966 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9967 Loc : constant Source_Ptr := Sloc (N);
9968 Conc_Typ : Entity_Id;
9969 Concval : Node_Id;
9970 Ename : Node_Id;
9971 Index : Node_Id;
9972 Old_Typ : Entity_Id;
9973
9974 function Build_Dispatching_Call_Equivalent return Node_Id;
9975 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9976 -- the form Concval.Ename. It is statically known that Ename is allowed
9977 -- to be implemented by a protected procedure. Create a dispatching call
9978 -- equivalent of Concval.Ename taking the actual parameters from the
9979 -- inner-most accept statement or entry body.
9980
9981 function Build_Dispatching_Requeue return Node_Id;
9982 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9983 -- the form Concval.Ename. It is statically known that Ename is allowed
9984 -- to be implemented by a protected or a task entry. Create a call to
9985 -- primitive _Disp_Requeue which handles the low-level actions.
9986
9987 function Build_Dispatching_Requeue_To_Any return Node_Id;
9988 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9989 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9990 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9991 -- determines at runtime whether Ename denotes an entry or a procedure
9992 -- and perform the appropriate kind of dispatching select.
9993
9994 function Build_Normal_Requeue return Node_Id;
9995 -- N denotes a non-dispatching requeue statement to either a task or a
9996 -- protected entry. Build the appropriate runtime call to perform the
9997 -- action.
9998
9999 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
10000 -- For a protected entry, create a return statement to skip the rest of
10001 -- the entry body. Otherwise, create a goto statement to skip the rest
10002 -- of a task accept statement. The lookup for the enclosing entry body
10003 -- or accept statement starts from Search.
10004
10005 ---------------------------------------
10006 -- Build_Dispatching_Call_Equivalent --
10007 ---------------------------------------
10008
10009 function Build_Dispatching_Call_Equivalent return Node_Id is
10010 Call_Ent : constant Entity_Id := Entity (Ename);
10011 Obj : constant Node_Id := Original_Node (Concval);
10012 Acc_Ent : Node_Id;
10013 Actuals : List_Id;
10014 Formal : Node_Id;
10015 Formals : List_Id;
10016
10017 begin
10018 -- Climb the parent chain looking for the inner-most entry body or
10019 -- accept statement.
10020
10021 Acc_Ent := N;
10022 while Present (Acc_Ent)
10023 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
10024 N_Entry_Body)
10025 loop
10026 Acc_Ent := Parent (Acc_Ent);
10027 end loop;
10028
10029 -- A requeue statement should be housed inside an entry body or an
10030 -- accept statement at some level. If this is not the case, then the
10031 -- tree is malformed.
10032
10033 pragma Assert (Present (Acc_Ent));
10034
10035 -- Recover the list of formal parameters
10036
10037 if Nkind (Acc_Ent) = N_Entry_Body then
10038 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
10039 end if;
10040
10041 Formals := Parameter_Specifications (Acc_Ent);
10042
10043 -- Create the actual parameters for the dispatching call. These are
10044 -- simply copies of the entry body or accept statement formals in the
10045 -- same order as they appear.
10046
10047 Actuals := No_List;
10048
10049 if Present (Formals) then
10050 Actuals := New_List;
10051 Formal := First (Formals);
10052 while Present (Formal) loop
10053 Append_To (Actuals,
10054 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
10055 Next (Formal);
10056 end loop;
10057 end if;
10058
10059 -- Generate:
10060 -- Obj.Call_Ent (Actuals);
10061
10062 return
10063 Make_Procedure_Call_Statement (Loc,
10064 Name =>
10065 Make_Selected_Component (Loc,
10066 Prefix => Make_Identifier (Loc, Chars (Obj)),
10067 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
10068
10069 Parameter_Associations => Actuals);
10070 end Build_Dispatching_Call_Equivalent;
10071
10072 -------------------------------
10073 -- Build_Dispatching_Requeue --
10074 -------------------------------
10075
10076 function Build_Dispatching_Requeue return Node_Id is
10077 Params : constant List_Id := New_List;
10078
10079 begin
10080 -- Process the "with abort" parameter
10081
10082 Prepend_To (Params,
10083 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10084
10085 -- Process the entry wrapper's position in the primary dispatch
10086 -- table parameter. Generate:
10087
10088 -- Ada.Tags.Get_Entry_Index
10089 -- (T => To_Tag_Ptr (Obj'Address).all,
10090 -- Position =>
10091 -- Ada.Tags.Get_Offset_Index
10092 -- (Ada.Tags.Tag (Concval),
10093 -- <interface dispatch table position of Ename>));
10094
10095 -- Note that Obj'Address is recursively expanded into a call to
10096 -- Base_Address (Obj).
10097
10098 if Tagged_Type_Expansion then
10099 Prepend_To (Params,
10100 Make_Function_Call (Loc,
10101 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10102 Parameter_Associations => New_List (
10103
10104 Make_Explicit_Dereference (Loc,
10105 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
10106 Make_Attribute_Reference (Loc,
10107 Prefix => New_Copy_Tree (Concval),
10108 Attribute_Name => Name_Address))),
10109
10110 Make_Function_Call (Loc,
10111 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10112 Parameter_Associations => New_List (
10113 Unchecked_Convert_To (RTE (RE_Tag), Concval),
10114 Make_Integer_Literal (Loc,
10115 DT_Position (Entity (Ename))))))));
10116
10117 -- VM targets
10118
10119 else
10120 Prepend_To (Params,
10121 Make_Function_Call (Loc,
10122 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10123 Parameter_Associations => New_List (
10124
10125 Make_Attribute_Reference (Loc,
10126 Prefix => Concval,
10127 Attribute_Name => Name_Tag),
10128
10129 Make_Function_Call (Loc,
10130 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10131
10132 Parameter_Associations => New_List (
10133
10134 -- Obj_Tag
10135
10136 Make_Attribute_Reference (Loc,
10137 Prefix => Concval,
10138 Attribute_Name => Name_Tag),
10139
10140 -- Tag_Typ
10141
10142 Make_Attribute_Reference (Loc,
10143 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
10144 Attribute_Name => Name_Tag),
10145
10146 -- Position
10147
10148 Make_Integer_Literal (Loc,
10149 DT_Position (Entity (Ename))))))));
10150 end if;
10151
10152 -- Specific actuals for protected to XXX requeue
10153
10154 if Is_Protected_Type (Old_Typ) then
10155 Prepend_To (Params,
10156 Make_Attribute_Reference (Loc, -- _object'Address
10157 Prefix =>
10158 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10159 Attribute_Name => Name_Address));
10160
10161 Prepend_To (Params, -- True
10162 New_Occurrence_Of (Standard_True, Loc));
10163
10164 -- Specific actuals for task to XXX requeue
10165
10166 else
10167 pragma Assert (Is_Task_Type (Old_Typ));
10168
10169 Prepend_To (Params, -- null
10170 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10171
10172 Prepend_To (Params, -- False
10173 New_Occurrence_Of (Standard_False, Loc));
10174 end if;
10175
10176 -- Add the object parameter
10177
10178 Prepend_To (Params, New_Copy_Tree (Concval));
10179
10180 -- Generate:
10181 -- _Disp_Requeue (<Params>);
10182
10183 -- Find entity for Disp_Requeue operation, which belongs to
10184 -- the type and may not be directly visible.
10185
10186 declare
10187 Elmt : Elmt_Id;
10188 Op : Entity_Id;
10189
10190 begin
10191 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10192 while Present (Elmt) loop
10193 Op := Node (Elmt);
10194 exit when Chars (Op) = Name_uDisp_Requeue;
10195 Next_Elmt (Elmt);
10196 end loop;
10197
10198 return
10199 Make_Procedure_Call_Statement (Loc,
10200 Name => New_Occurrence_Of (Op, Loc),
10201 Parameter_Associations => Params);
10202 end;
10203 end Build_Dispatching_Requeue;
10204
10205 --------------------------------------
10206 -- Build_Dispatching_Requeue_To_Any --
10207 --------------------------------------
10208
10209 function Build_Dispatching_Requeue_To_Any return Node_Id is
10210 Call_Ent : constant Entity_Id := Entity (Ename);
10211 Obj : constant Node_Id := Original_Node (Concval);
10212 Skip : constant Node_Id := Build_Skip_Statement (N);
10213 C : Entity_Id;
10214 Decls : List_Id;
10215 S : Entity_Id;
10216 Stmts : List_Id;
10217
10218 begin
10219 Decls := New_List;
10220 Stmts := New_List;
10221
10222 -- Dispatch table slot processing, generate:
10223 -- S : Integer;
10224
10225 S := Build_S (Loc, Decls);
10226
10227 -- Call kind processing, generate:
10228 -- C : Ada.Tags.Prim_Op_Kind;
10229
10230 C := Build_C (Loc, Decls);
10231
10232 -- Generate:
10233 -- S := Ada.Tags.Get_Offset_Index
10234 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10235
10236 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10237
10238 -- Generate:
10239 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10240
10241 Append_To (Stmts,
10242 Make_Procedure_Call_Statement (Loc,
10243 Name =>
10244 New_Occurrence_Of (
10245 Find_Prim_Op (Etype (Etype (Obj)),
10246 Name_uDisp_Get_Prim_Op_Kind),
10247 Loc),
10248 Parameter_Associations => New_List (
10249 New_Copy_Tree (Obj),
10250 New_Occurrence_Of (S, Loc),
10251 New_Occurrence_Of (C, Loc))));
10252
10253 Append_To (Stmts,
10254
10255 -- if C = POK_Protected_Entry
10256 -- or else C = POK_Task_Entry
10257 -- then
10258
10259 Make_Implicit_If_Statement (N,
10260 Condition =>
10261 Make_Op_Or (Loc,
10262 Left_Opnd =>
10263 Make_Op_Eq (Loc,
10264 Left_Opnd =>
10265 New_Occurrence_Of (C, Loc),
10266 Right_Opnd =>
10267 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10268
10269 Right_Opnd =>
10270 Make_Op_Eq (Loc,
10271 Left_Opnd =>
10272 New_Occurrence_Of (C, Loc),
10273 Right_Opnd =>
10274 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10275
10276 -- Dispatching requeue equivalent
10277
10278 Then_Statements => New_List (
10279 Build_Dispatching_Requeue,
10280 Skip),
10281
10282 -- elsif C = POK_Protected_Procedure then
10283
10284 Elsif_Parts => New_List (
10285 Make_Elsif_Part (Loc,
10286 Condition =>
10287 Make_Op_Eq (Loc,
10288 Left_Opnd =>
10289 New_Occurrence_Of (C, Loc),
10290 Right_Opnd =>
10291 New_Occurrence_Of (
10292 RTE (RE_POK_Protected_Procedure), Loc)),
10293
10294 -- Dispatching call equivalent
10295
10296 Then_Statements => New_List (
10297 Build_Dispatching_Call_Equivalent))),
10298
10299 -- else
10300 -- raise Program_Error;
10301 -- end if;
10302
10303 Else_Statements => New_List (
10304 Make_Raise_Program_Error (Loc,
10305 Reason => PE_Explicit_Raise))));
10306
10307 -- Wrap everything into a block
10308
10309 return
10310 Make_Block_Statement (Loc,
10311 Declarations => Decls,
10312 Handled_Statement_Sequence =>
10313 Make_Handled_Sequence_Of_Statements (Loc,
10314 Statements => Stmts));
10315 end Build_Dispatching_Requeue_To_Any;
10316
10317 --------------------------
10318 -- Build_Normal_Requeue --
10319 --------------------------
10320
10321 function Build_Normal_Requeue return Node_Id is
10322 Params : constant List_Id := New_List;
10323 Param : Node_Id;
10324 RT_Call : Node_Id;
10325
10326 begin
10327 -- Process the "with abort" parameter
10328
10329 Prepend_To (Params,
10330 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10331
10332 -- Add the index expression to the parameters. It is common among all
10333 -- four cases.
10334
10335 Prepend_To (Params,
10336 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10337
10338 if Is_Protected_Type (Old_Typ) then
10339 declare
10340 Self_Param : Node_Id;
10341
10342 begin
10343 Self_Param :=
10344 Make_Attribute_Reference (Loc,
10345 Prefix =>
10346 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10347 Attribute_Name =>
10348 Name_Unchecked_Access);
10349
10350 -- Protected to protected requeue
10351
10352 if Is_Protected_Type (Conc_Typ) then
10353 RT_Call :=
10354 New_Occurrence_Of (
10355 RTE (RE_Requeue_Protected_Entry), Loc);
10356
10357 Param :=
10358 Make_Attribute_Reference (Loc,
10359 Prefix =>
10360 Concurrent_Ref (Concval),
10361 Attribute_Name =>
10362 Name_Unchecked_Access);
10363
10364 -- Protected to task requeue
10365
10366 else pragma Assert (Is_Task_Type (Conc_Typ));
10367 RT_Call :=
10368 New_Occurrence_Of (
10369 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10370
10371 Param := Concurrent_Ref (Concval);
10372 end if;
10373
10374 Prepend_To (Params, Param);
10375 Prepend_To (Params, Self_Param);
10376 end;
10377
10378 else pragma Assert (Is_Task_Type (Old_Typ));
10379
10380 -- Task to protected requeue
10381
10382 if Is_Protected_Type (Conc_Typ) then
10383 RT_Call :=
10384 New_Occurrence_Of (
10385 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10386
10387 Param :=
10388 Make_Attribute_Reference (Loc,
10389 Prefix =>
10390 Concurrent_Ref (Concval),
10391 Attribute_Name =>
10392 Name_Unchecked_Access);
10393
10394 -- Task to task requeue
10395
10396 else pragma Assert (Is_Task_Type (Conc_Typ));
10397 RT_Call :=
10398 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10399
10400 Param := Concurrent_Ref (Concval);
10401 end if;
10402
10403 Prepend_To (Params, Param);
10404 end if;
10405
10406 return
10407 Make_Procedure_Call_Statement (Loc,
10408 Name => RT_Call,
10409 Parameter_Associations => Params);
10410 end Build_Normal_Requeue;
10411
10412 --------------------------
10413 -- Build_Skip_Statement --
10414 --------------------------
10415
10416 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10417 Skip_Stmt : Node_Id;
10418
10419 begin
10420 -- Build a return statement to skip the rest of the entire body
10421
10422 if Is_Protected_Type (Old_Typ) then
10423 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10424
10425 -- If the requeue is within a task, find the end label of the
10426 -- enclosing accept statement and create a goto statement to it.
10427
10428 else
10429 declare
10430 Acc : Node_Id;
10431 Label : Node_Id;
10432
10433 begin
10434 -- Climb the parent chain looking for the enclosing accept
10435 -- statement.
10436
10437 Acc := Parent (Search);
10438 while Present (Acc)
10439 and then Nkind (Acc) /= N_Accept_Statement
10440 loop
10441 Acc := Parent (Acc);
10442 end loop;
10443
10444 -- The last statement is the second label used for completing
10445 -- the rendezvous the usual way. The label we are looking for
10446 -- is right before it.
10447
10448 Label :=
10449 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10450
10451 pragma Assert (Nkind (Label) = N_Label);
10452
10453 -- Generate a goto statement to skip the rest of the accept
10454
10455 Skip_Stmt :=
10456 Make_Goto_Statement (Loc,
10457 Name =>
10458 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10459 end;
10460 end if;
10461
10462 Set_Analyzed (Skip_Stmt);
10463
10464 return Skip_Stmt;
10465 end Build_Skip_Statement;
10466
10467 -- Start of processing for Expand_N_Requeue_Statement
10468
10469 begin
10470 -- Extract the components of the entry call
10471
10472 Extract_Entry (N, Concval, Ename, Index);
10473 Conc_Typ := Etype (Concval);
10474
10475 -- If the prefix is an access to class-wide type, dereference to get
10476 -- object and entry type.
10477
10478 if Is_Access_Type (Conc_Typ) then
10479 Conc_Typ := Designated_Type (Conc_Typ);
10480 Rewrite (Concval,
10481 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10482 Analyze_And_Resolve (Concval, Conc_Typ);
10483 end if;
10484
10485 -- Examine the scope stack in order to find nearest enclosing protected
10486 -- or task type. This will constitute our invocation source.
10487
10488 Old_Typ := Current_Scope;
10489 while Present (Old_Typ)
10490 and then not Is_Protected_Type (Old_Typ)
10491 and then not Is_Task_Type (Old_Typ)
10492 loop
10493 Old_Typ := Scope (Old_Typ);
10494 end loop;
10495
10496 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10497 -- Concval.Ename where the type of Concval is class-wide concurrent
10498 -- interface.
10499
10500 if Ada_Version >= Ada_2012
10501 and then Present (Concval)
10502 and then Is_Class_Wide_Type (Conc_Typ)
10503 and then Is_Concurrent_Interface (Conc_Typ)
10504 then
10505 declare
10506 Has_Impl : Boolean := False;
10507 Impl_Kind : Name_Id := No_Name;
10508
10509 begin
10510 -- Check whether the Ename is flagged by pragma Implemented
10511
10512 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10513 Has_Impl := True;
10514 Impl_Kind := Implementation_Kind (Entity (Ename));
10515 end if;
10516
10517 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10518 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10519
10520 if Has_Impl and then Impl_Kind = Name_By_Entry then
10521 Rewrite (N, Build_Dispatching_Requeue);
10522 Analyze (N);
10523 Insert_After (N, Build_Skip_Statement (N));
10524
10525 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10526 -- a protected procedure. In this case the requeue is transformed
10527 -- into a dispatching call.
10528
10529 elsif Has_Impl
10530 and then Impl_Kind = Name_By_Protected_Procedure
10531 then
10532 Rewrite (N, Build_Dispatching_Call_Equivalent);
10533 Analyze (N);
10534
10535 -- The procedure_or_entry_NAME's implementation kind is either
10536 -- By_Any, Optional, or pragma Implemented was not applied at all.
10537 -- In this case a runtime test determines whether Ename denotes an
10538 -- entry or a protected procedure and performs the appropriate
10539 -- call.
10540
10541 else
10542 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10543 Analyze (N);
10544 end if;
10545 end;
10546
10547 -- Processing for regular (non-dispatching) requeues
10548
10549 else
10550 Rewrite (N, Build_Normal_Requeue);
10551 Analyze (N);
10552 Insert_After (N, Build_Skip_Statement (N));
10553 end if;
10554 end Expand_N_Requeue_Statement;
10555
10556 -------------------------------
10557 -- Expand_N_Selective_Accept --
10558 -------------------------------
10559
10560 procedure Expand_N_Selective_Accept (N : Node_Id) is
10561 Loc : constant Source_Ptr := Sloc (N);
10562 Alts : constant List_Id := Select_Alternatives (N);
10563
10564 -- Note: in the below declarations a lot of new lists are allocated
10565 -- unconditionally which may well not end up being used. That's not
10566 -- a good idea since it wastes space gratuitously ???
10567
10568 Accept_Case : List_Id;
10569 Accept_List : constant List_Id := New_List;
10570
10571 Alt : Node_Id;
10572 Alt_List : constant List_Id := New_List;
10573 Alt_Stats : List_Id;
10574 Ann : Entity_Id := Empty;
10575
10576 Check_Guard : Boolean := True;
10577
10578 Decls : constant List_Id := New_List;
10579 Stats : constant List_Id := New_List;
10580 Body_List : constant List_Id := New_List;
10581 Trailing_List : constant List_Id := New_List;
10582
10583 Choices : List_Id;
10584 Else_Present : Boolean := False;
10585 Terminate_Alt : Node_Id := Empty;
10586 Select_Mode : Node_Id;
10587
10588 Delay_Case : List_Id;
10589 Delay_Count : Integer := 0;
10590 Delay_Val : Entity_Id;
10591 Delay_Index : Entity_Id;
10592 Delay_Min : Entity_Id;
10593 Delay_Num : Int := 1;
10594 Delay_Alt_List : List_Id := New_List;
10595 Delay_List : constant List_Id := New_List;
10596 D : Entity_Id;
10597 M : Entity_Id;
10598
10599 First_Delay : Boolean := True;
10600 Guard_Open : Entity_Id;
10601
10602 End_Lab : Node_Id;
10603 Index : Int := 1;
10604 Lab : Node_Id;
10605 Num_Alts : Int;
10606 Num_Accept : Nat := 0;
10607 Proc : Node_Id;
10608 Time_Type : Entity_Id;
10609 Select_Call : Node_Id;
10610
10611 Qnam : constant Entity_Id :=
10612 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10613
10614 Xnam : constant Entity_Id :=
10615 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10616
10617 -----------------------
10618 -- Local subprograms --
10619 -----------------------
10620
10621 function Accept_Or_Raise return List_Id;
10622 -- For the rare case where delay alternatives all have guards, and
10623 -- all of them are closed, it is still possible that there were open
10624 -- accept alternatives with no callers. We must reexamine the
10625 -- Accept_List, and execute a selective wait with no else if some
10626 -- accept is open. If none, we raise program_error.
10627
10628 procedure Add_Accept (Alt : Node_Id);
10629 -- Process a single accept statement in a select alternative. Build
10630 -- procedure for body of accept, and add entry to dispatch table with
10631 -- expression for guard, in preparation for call to run time select.
10632
10633 function Make_And_Declare_Label (Num : Int) return Node_Id;
10634 -- Manufacture a label using Num as a serial number and declare it.
10635 -- The declaration is appended to Decls. The label marks the trailing
10636 -- statements of an accept or delay alternative.
10637
10638 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10639 -- Build call to Selective_Wait runtime routine
10640
10641 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10642 -- Add code to compare value of delay with previous values, and
10643 -- generate case entry for trailing statements.
10644
10645 procedure Process_Accept_Alternative
10646 (Alt : Node_Id;
10647 Index : Int;
10648 Proc : Node_Id);
10649 -- Add code to call corresponding procedure, and branch to
10650 -- trailing statements, if any.
10651
10652 ---------------------
10653 -- Accept_Or_Raise --
10654 ---------------------
10655
10656 function Accept_Or_Raise return List_Id is
10657 Cond : Node_Id;
10658 Stats : List_Id;
10659 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10660
10661 begin
10662 -- We generate the following:
10663
10664 -- for J in q'range loop
10665 -- if q(J).S /=null_task_entry then
10666 -- selective_wait (simple_mode,...);
10667 -- done := True;
10668 -- exit;
10669 -- end if;
10670 -- end loop;
10671 --
10672 -- if no rendez_vous then
10673 -- raise program_error;
10674 -- end if;
10675
10676 -- Note that the code needs to know that the selector name
10677 -- in an Accept_Alternative is named S.
10678
10679 Cond := Make_Op_Ne (Loc,
10680 Left_Opnd =>
10681 Make_Selected_Component (Loc,
10682 Prefix =>
10683 Make_Indexed_Component (Loc,
10684 Prefix => New_Occurrence_Of (Qnam, Loc),
10685 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10686 Selector_Name => Make_Identifier (Loc, Name_S)),
10687 Right_Opnd =>
10688 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10689
10690 Stats := New_List (
10691 Make_Implicit_Loop_Statement (N,
10692 Iteration_Scheme =>
10693 Make_Iteration_Scheme (Loc,
10694 Loop_Parameter_Specification =>
10695 Make_Loop_Parameter_Specification (Loc,
10696 Defining_Identifier => J,
10697 Discrete_Subtype_Definition =>
10698 Make_Attribute_Reference (Loc,
10699 Prefix => New_Occurrence_Of (Qnam, Loc),
10700 Attribute_Name => Name_Range,
10701 Expressions => New_List (
10702 Make_Integer_Literal (Loc, 1))))),
10703
10704 Statements => New_List (
10705 Make_Implicit_If_Statement (N,
10706 Condition => Cond,
10707 Then_Statements => New_List (
10708 Make_Select_Call (
10709 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10710 Make_Exit_Statement (Loc))))));
10711
10712 Append_To (Stats,
10713 Make_Raise_Program_Error (Loc,
10714 Condition => Make_Op_Eq (Loc,
10715 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10716 Right_Opnd =>
10717 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10718 Reason => PE_All_Guards_Closed));
10719
10720 return Stats;
10721 end Accept_Or_Raise;
10722
10723 ----------------
10724 -- Add_Accept --
10725 ----------------
10726
10727 procedure Add_Accept (Alt : Node_Id) is
10728 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10729 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10730 Eloc : constant Source_Ptr := Sloc (Ename);
10731 Eent : constant Entity_Id := Entity (Ename);
10732 Index : constant Node_Id := Entry_Index (Acc_Stm);
10733 Null_Body : Node_Id;
10734 Proc_Body : Node_Id;
10735 PB_Ent : Entity_Id;
10736 Expr : Node_Id;
10737 Call : Node_Id;
10738
10739 begin
10740 if No (Ann) then
10741 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10742 end if;
10743
10744 if Present (Condition (Alt)) then
10745 Expr :=
10746 Make_If_Expression (Eloc, New_List (
10747 Condition (Alt),
10748 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10749 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10750 else
10751 Expr :=
10752 Entry_Index_Expression
10753 (Eloc, Eent, Index, Scope (Eent));
10754 end if;
10755
10756 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10757 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10758
10759 -- Always add call to Abort_Undefer when generating code, since
10760 -- this is what the runtime expects (abort deferred in
10761 -- Selective_Wait). In CodePeer mode this only confuses the
10762 -- analysis with unknown calls, so don't do it.
10763
10764 if not CodePeer_Mode then
10765 Call :=
10766 Make_Procedure_Call_Statement (Eloc,
10767 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Eloc));
10768 Insert_Before
10769 (First (Statements (Handled_Statement_Sequence
10770 (Accept_Statement (Alt)))),
10771 Call);
10772 Analyze (Call);
10773 end if;
10774
10775 PB_Ent :=
10776 Make_Defining_Identifier (Eloc,
10777 New_External_Name (Chars (Ename), 'A', Num_Accept));
10778
10779 if Comes_From_Source (Alt) then
10780 Set_Debug_Info_Needed (PB_Ent);
10781 end if;
10782
10783 Proc_Body :=
10784 Make_Subprogram_Body (Eloc,
10785 Specification =>
10786 Make_Procedure_Specification (Eloc,
10787 Defining_Unit_Name => PB_Ent),
10788 Declarations => Declarations (Acc_Stm),
10789 Handled_Statement_Sequence =>
10790 Build_Accept_Body (Accept_Statement (Alt)));
10791
10792 -- During the analysis of the body of the accept statement, any
10793 -- zero cost exception handler records were collected in the
10794 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10795 -- This is where we move them to where they belong, namely the
10796 -- newly created procedure.
10797
10798 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10799 Append (Proc_Body, Body_List);
10800
10801 else
10802 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10803
10804 -- if accept statement has declarations, insert above, given that
10805 -- we are not creating a body for the accept.
10806
10807 if Present (Declarations (Acc_Stm)) then
10808 Insert_Actions (N, Declarations (Acc_Stm));
10809 end if;
10810 end if;
10811
10812 Append_To (Accept_List,
10813 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10814
10815 Num_Accept := Num_Accept + 1;
10816 end Add_Accept;
10817
10818 ----------------------------
10819 -- Make_And_Declare_Label --
10820 ----------------------------
10821
10822 function Make_And_Declare_Label (Num : Int) return Node_Id is
10823 Lab_Id : Node_Id;
10824
10825 begin
10826 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10827 Lab :=
10828 Make_Label (Loc, Lab_Id);
10829
10830 Append_To (Decls,
10831 Make_Implicit_Label_Declaration (Loc,
10832 Defining_Identifier =>
10833 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10834 Label_Construct => Lab));
10835
10836 return Lab;
10837 end Make_And_Declare_Label;
10838
10839 ----------------------
10840 -- Make_Select_Call --
10841 ----------------------
10842
10843 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10844 Params : constant List_Id := New_List;
10845
10846 begin
10847 Append_To (Params,
10848 Make_Attribute_Reference (Loc,
10849 Prefix => New_Occurrence_Of (Qnam, Loc),
10850 Attribute_Name => Name_Unchecked_Access));
10851 Append_To (Params, Select_Mode);
10852 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10853 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10854
10855 return
10856 Make_Procedure_Call_Statement (Loc,
10857 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10858 Parameter_Associations => Params);
10859 end Make_Select_Call;
10860
10861 --------------------------------
10862 -- Process_Accept_Alternative --
10863 --------------------------------
10864
10865 procedure Process_Accept_Alternative
10866 (Alt : Node_Id;
10867 Index : Int;
10868 Proc : Node_Id)
10869 is
10870 Astmt : constant Node_Id := Accept_Statement (Alt);
10871 Alt_Stats : List_Id;
10872
10873 begin
10874 Adjust_Condition (Condition (Alt));
10875
10876 -- Accept with body
10877
10878 if Present (Handled_Statement_Sequence (Astmt)) then
10879 Alt_Stats :=
10880 New_List (
10881 Make_Procedure_Call_Statement (Sloc (Proc),
10882 Name =>
10883 New_Occurrence_Of
10884 (Defining_Unit_Name (Specification (Proc)),
10885 Sloc (Proc))));
10886
10887 -- Accept with no body (followed by trailing statements)
10888
10889 else
10890 Alt_Stats := Empty_List;
10891 end if;
10892
10893 Ensure_Statement_Present (Sloc (Astmt), Alt);
10894
10895 -- After the call, if any, branch to trailing statements, if any.
10896 -- We create a label for each, as well as the corresponding label
10897 -- declaration.
10898
10899 if not Is_Empty_List (Statements (Alt)) then
10900 Lab := Make_And_Declare_Label (Index);
10901 Append (Lab, Trailing_List);
10902 Append_List (Statements (Alt), Trailing_List);
10903 Append_To (Trailing_List,
10904 Make_Goto_Statement (Loc,
10905 Name => New_Copy (Identifier (End_Lab))));
10906
10907 else
10908 Lab := End_Lab;
10909 end if;
10910
10911 Append_To (Alt_Stats,
10912 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10913
10914 Append_To (Alt_List,
10915 Make_Case_Statement_Alternative (Loc,
10916 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10917 Statements => Alt_Stats));
10918 end Process_Accept_Alternative;
10919
10920 -------------------------------
10921 -- Process_Delay_Alternative --
10922 -------------------------------
10923
10924 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10925 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10926 Cond : Node_Id;
10927 Delay_Alt : List_Id;
10928
10929 begin
10930 -- Deal with C/Fortran boolean as delay condition
10931
10932 Adjust_Condition (Condition (Alt));
10933
10934 -- Determine the smallest specified delay
10935
10936 -- for each delay alternative generate:
10937
10938 -- if guard-expression then
10939 -- Delay_Val := delay-expression;
10940 -- Guard_Open := True;
10941 -- if Delay_Val < Delay_Min then
10942 -- Delay_Min := Delay_Val;
10943 -- Delay_Index := Index;
10944 -- end if;
10945 -- end if;
10946
10947 -- The enclosing if-statement is omitted if there is no guard
10948
10949 if Delay_Count = 1 or else First_Delay then
10950 First_Delay := False;
10951
10952 Delay_Alt := New_List (
10953 Make_Assignment_Statement (Loc,
10954 Name => New_Occurrence_Of (Delay_Min, Loc),
10955 Expression => Expression (Delay_Statement (Alt))));
10956
10957 if Delay_Count > 1 then
10958 Append_To (Delay_Alt,
10959 Make_Assignment_Statement (Loc,
10960 Name => New_Occurrence_Of (Delay_Index, Loc),
10961 Expression => Make_Integer_Literal (Loc, Index)));
10962 end if;
10963
10964 else
10965 Delay_Alt := New_List (
10966 Make_Assignment_Statement (Loc,
10967 Name => New_Occurrence_Of (Delay_Val, Loc),
10968 Expression => Expression (Delay_Statement (Alt))));
10969
10970 if Time_Type = Standard_Duration then
10971 Cond :=
10972 Make_Op_Lt (Loc,
10973 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10974 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10975
10976 else
10977 -- The scope of the time type must define a comparison
10978 -- operator. The scope itself may not be visible, so we
10979 -- construct a node with entity information to insure that
10980 -- semantic analysis can find the proper operator.
10981
10982 Cond :=
10983 Make_Function_Call (Loc,
10984 Name => Make_Selected_Component (Loc,
10985 Prefix =>
10986 New_Occurrence_Of (Scope (Time_Type), Loc),
10987 Selector_Name =>
10988 Make_Operator_Symbol (Loc,
10989 Chars => Name_Op_Lt,
10990 Strval => No_String)),
10991 Parameter_Associations =>
10992 New_List (
10993 New_Occurrence_Of (Delay_Val, Loc),
10994 New_Occurrence_Of (Delay_Min, Loc)));
10995
10996 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10997 end if;
10998
10999 Append_To (Delay_Alt,
11000 Make_Implicit_If_Statement (N,
11001 Condition => Cond,
11002 Then_Statements => New_List (
11003 Make_Assignment_Statement (Loc,
11004 Name => New_Occurrence_Of (Delay_Min, Loc),
11005 Expression => New_Occurrence_Of (Delay_Val, Loc)),
11006
11007 Make_Assignment_Statement (Loc,
11008 Name => New_Occurrence_Of (Delay_Index, Loc),
11009 Expression => Make_Integer_Literal (Loc, Index)))));
11010 end if;
11011
11012 if Check_Guard then
11013 Append_To (Delay_Alt,
11014 Make_Assignment_Statement (Loc,
11015 Name => New_Occurrence_Of (Guard_Open, Loc),
11016 Expression => New_Occurrence_Of (Standard_True, Loc)));
11017 end if;
11018
11019 if Present (Condition (Alt)) then
11020 Delay_Alt := New_List (
11021 Make_Implicit_If_Statement (N,
11022 Condition => Condition (Alt),
11023 Then_Statements => Delay_Alt));
11024 end if;
11025
11026 Append_List (Delay_Alt, Delay_List);
11027
11028 Ensure_Statement_Present (Dloc, Alt);
11029
11030 -- If the delay alternative has a statement part, add choice to the
11031 -- case statements for delays.
11032
11033 if not Is_Empty_List (Statements (Alt)) then
11034
11035 if Delay_Count = 1 then
11036 Append_List (Statements (Alt), Delay_Alt_List);
11037
11038 else
11039 Append_To (Delay_Alt_List,
11040 Make_Case_Statement_Alternative (Loc,
11041 Discrete_Choices => New_List (
11042 Make_Integer_Literal (Loc, Index)),
11043 Statements => Statements (Alt)));
11044 end if;
11045
11046 elsif Delay_Count = 1 then
11047
11048 -- If the single delay has no trailing statements, add a branch
11049 -- to the exit label to the selective wait.
11050
11051 Delay_Alt_List := New_List (
11052 Make_Goto_Statement (Loc,
11053 Name => New_Copy (Identifier (End_Lab))));
11054
11055 end if;
11056 end Process_Delay_Alternative;
11057
11058 -- Start of processing for Expand_N_Selective_Accept
11059
11060 begin
11061 Process_Statements_For_Controlled_Objects (N);
11062
11063 -- First insert some declarations before the select. The first is:
11064
11065 -- Ann : Address
11066
11067 -- This variable holds the parameters passed to the accept body. This
11068 -- declaration has already been inserted by the time we get here by
11069 -- a call to Expand_Accept_Declarations made from the semantics when
11070 -- processing the first accept statement contained in the select. We
11071 -- can find this entity as Accept_Address (E), where E is any of the
11072 -- entries references by contained accept statements.
11073
11074 -- The first step is to scan the list of Selective_Accept_Statements
11075 -- to find this entity, and also count the number of accepts, and
11076 -- determine if terminated, delay or else is present:
11077
11078 Num_Alts := 0;
11079
11080 Alt := First (Alts);
11081 while Present (Alt) loop
11082 Process_Statements_For_Controlled_Objects (Alt);
11083
11084 if Nkind (Alt) = N_Accept_Alternative then
11085 Add_Accept (Alt);
11086
11087 elsif Nkind (Alt) = N_Delay_Alternative then
11088 Delay_Count := Delay_Count + 1;
11089
11090 -- If the delays are relative delays, the delay expressions have
11091 -- type Standard_Duration. Otherwise they must have some time type
11092 -- recognized by GNAT.
11093
11094 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
11095 Time_Type := Standard_Duration;
11096 else
11097 Time_Type := Etype (Expression (Delay_Statement (Alt)));
11098
11099 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
11100 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
11101 then
11102 null;
11103 else
11104 Error_Msg_NE (
11105 "& is not a time type (RM 9.6(6))",
11106 Expression (Delay_Statement (Alt)), Time_Type);
11107 Time_Type := Standard_Duration;
11108 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
11109 end if;
11110 end if;
11111
11112 if No (Condition (Alt)) then
11113
11114 -- This guard will always be open
11115
11116 Check_Guard := False;
11117 end if;
11118
11119 elsif Nkind (Alt) = N_Terminate_Alternative then
11120 Adjust_Condition (Condition (Alt));
11121 Terminate_Alt := Alt;
11122 end if;
11123
11124 Num_Alts := Num_Alts + 1;
11125 Next (Alt);
11126 end loop;
11127
11128 Else_Present := Present (Else_Statements (N));
11129
11130 -- At the same time (see procedure Add_Accept) we build the accept list:
11131
11132 -- Qnn : Accept_List (1 .. num-select) := (
11133 -- (null-body, entry-index),
11134 -- (null-body, entry-index),
11135 -- ..
11136 -- (null_body, entry-index));
11137
11138 -- In the above declaration, null-body is True if the corresponding
11139 -- accept has no body, and false otherwise. The entry is either the
11140 -- entry index expression if there is no guard, or if a guard is
11141 -- present, then an if expression of the form:
11142
11143 -- (if guard then entry-index else Null_Task_Entry)
11144
11145 -- If a guard is statically known to be false, the entry can simply
11146 -- be omitted from the accept list.
11147
11148 Append_To (Decls,
11149 Make_Object_Declaration (Loc,
11150 Defining_Identifier => Qnam,
11151 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11152 Aliased_Present => True,
11153 Expression =>
11154 Make_Qualified_Expression (Loc,
11155 Subtype_Mark =>
11156 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11157 Expression =>
11158 Make_Aggregate (Loc, Expressions => Accept_List))));
11159
11160 -- Then we declare the variable that holds the index for the accept
11161 -- that will be selected for service:
11162
11163 -- Xnn : Select_Index;
11164
11165 Append_To (Decls,
11166 Make_Object_Declaration (Loc,
11167 Defining_Identifier => Xnam,
11168 Object_Definition =>
11169 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11170 Expression =>
11171 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11172
11173 -- After this follow procedure declarations for each accept body
11174
11175 -- procedure Pnn is
11176 -- begin
11177 -- ...
11178 -- end;
11179
11180 -- where the ... are statements from the corresponding procedure body.
11181 -- No parameters are involved, since the parameters are passed via Ann
11182 -- and the parameter references have already been expanded to be direct
11183 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11184 -- any embedded tasking statements (which would normally be illegal in
11185 -- procedures), have been converted to calls to the tasking runtime so
11186 -- there is no problem in putting them into procedures.
11187
11188 -- The original accept statement has been expanded into a block in
11189 -- the same fashion as for simple accepts (see Build_Accept_Body).
11190
11191 -- Note: we don't really need to build these procedures for the case
11192 -- where no delay statement is present, but it is just as easy to
11193 -- build them unconditionally, and not significantly inefficient,
11194 -- since if they are short they will be inlined anyway.
11195
11196 -- The procedure declarations have been assembled in Body_List
11197
11198 -- If delays are present, we must compute the required delay.
11199 -- We first generate the declarations:
11200
11201 -- Delay_Index : Boolean := 0;
11202 -- Delay_Min : Some_Time_Type.Time;
11203 -- Delay_Val : Some_Time_Type.Time;
11204
11205 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11206 -- active delay that is actually chosen as the basis for the possible
11207 -- delay if an immediate rendez-vous is not possible.
11208
11209 -- In the most common case there is a single delay statement, and this
11210 -- is handled specially.
11211
11212 if Delay_Count > 0 then
11213
11214 -- Generate the required declarations
11215
11216 Delay_Val :=
11217 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11218 Delay_Index :=
11219 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11220 Delay_Min :=
11221 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11222
11223 Append_To (Decls,
11224 Make_Object_Declaration (Loc,
11225 Defining_Identifier => Delay_Val,
11226 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11227
11228 Append_To (Decls,
11229 Make_Object_Declaration (Loc,
11230 Defining_Identifier => Delay_Index,
11231 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11232 Expression => Make_Integer_Literal (Loc, 0)));
11233
11234 Append_To (Decls,
11235 Make_Object_Declaration (Loc,
11236 Defining_Identifier => Delay_Min,
11237 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11238 Expression =>
11239 Unchecked_Convert_To (Time_Type,
11240 Make_Attribute_Reference (Loc,
11241 Prefix =>
11242 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11243 Attribute_Name => Name_Last))));
11244
11245 -- Create Duration and Delay_Mode objects used for passing a delay
11246 -- value to RTS
11247
11248 D := Make_Temporary (Loc, 'D');
11249 M := Make_Temporary (Loc, 'M');
11250
11251 declare
11252 Discr : Entity_Id;
11253
11254 begin
11255 -- Note that these values are defined in s-osprim.ads and must
11256 -- be kept in sync:
11257 --
11258 -- Relative : constant := 0;
11259 -- Absolute_Calendar : constant := 1;
11260 -- Absolute_RT : constant := 2;
11261
11262 if Time_Type = Standard_Duration then
11263 Discr := Make_Integer_Literal (Loc, 0);
11264
11265 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11266 Discr := Make_Integer_Literal (Loc, 1);
11267
11268 else
11269 pragma Assert
11270 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11271 Discr := Make_Integer_Literal (Loc, 2);
11272 end if;
11273
11274 Append_To (Decls,
11275 Make_Object_Declaration (Loc,
11276 Defining_Identifier => D,
11277 Object_Definition =>
11278 New_Occurrence_Of (Standard_Duration, Loc)));
11279
11280 Append_To (Decls,
11281 Make_Object_Declaration (Loc,
11282 Defining_Identifier => M,
11283 Object_Definition =>
11284 New_Occurrence_Of (Standard_Integer, Loc),
11285 Expression => Discr));
11286 end;
11287
11288 if Check_Guard then
11289 Guard_Open :=
11290 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11291
11292 Append_To (Decls,
11293 Make_Object_Declaration (Loc,
11294 Defining_Identifier => Guard_Open,
11295 Object_Definition =>
11296 New_Occurrence_Of (Standard_Boolean, Loc),
11297 Expression =>
11298 New_Occurrence_Of (Standard_False, Loc)));
11299 end if;
11300
11301 -- Delay_Count is zero, don't need M and D set (suppress warning)
11302
11303 else
11304 M := Empty;
11305 D := Empty;
11306 end if;
11307
11308 if Present (Terminate_Alt) then
11309
11310 -- If the terminate alternative guard is False, use
11311 -- Simple_Mode; otherwise use Terminate_Mode.
11312
11313 if Present (Condition (Terminate_Alt)) then
11314 Select_Mode := Make_If_Expression (Loc,
11315 New_List (Condition (Terminate_Alt),
11316 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11317 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11318 else
11319 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11320 end if;
11321
11322 elsif Else_Present or Delay_Count > 0 then
11323 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11324
11325 else
11326 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11327 end if;
11328
11329 Select_Call := Make_Select_Call (Select_Mode);
11330 Append (Select_Call, Stats);
11331
11332 -- Now generate code to act on the result. There is an entry
11333 -- in this case for each accept statement with a non-null body,
11334 -- followed by a branch to the statements that follow the Accept.
11335 -- In the absence of delay alternatives, we generate:
11336
11337 -- case X is
11338 -- when No_Rendezvous => -- omitted if simple mode
11339 -- goto Lab0;
11340
11341 -- when 1 =>
11342 -- P1n;
11343 -- goto Lab1;
11344
11345 -- when 2 =>
11346 -- P2n;
11347 -- goto Lab2;
11348
11349 -- when others =>
11350 -- goto Exit;
11351 -- end case;
11352 --
11353 -- Lab0: Else_Statements;
11354 -- goto exit;
11355
11356 -- Lab1: Trailing_Statements1;
11357 -- goto Exit;
11358 --
11359 -- Lab2: Trailing_Statements2;
11360 -- goto Exit;
11361 -- ...
11362 -- Exit:
11363
11364 -- Generate label for common exit
11365
11366 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11367
11368 -- First entry is the default case, when no rendezvous is possible
11369
11370 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11371
11372 if Else_Present then
11373
11374 -- If no rendezvous is possible, the else part is executed
11375
11376 Lab := Make_And_Declare_Label (0);
11377 Alt_Stats := New_List (
11378 Make_Goto_Statement (Loc,
11379 Name => New_Copy (Identifier (Lab))));
11380
11381 Append (Lab, Trailing_List);
11382 Append_List (Else_Statements (N), Trailing_List);
11383 Append_To (Trailing_List,
11384 Make_Goto_Statement (Loc,
11385 Name => New_Copy (Identifier (End_Lab))));
11386 else
11387 Alt_Stats := New_List (
11388 Make_Goto_Statement (Loc,
11389 Name => New_Copy (Identifier (End_Lab))));
11390 end if;
11391
11392 Append_To (Alt_List,
11393 Make_Case_Statement_Alternative (Loc,
11394 Discrete_Choices => Choices,
11395 Statements => Alt_Stats));
11396
11397 -- We make use of the fact that Accept_Index is an integer type, and
11398 -- generate successive literals for entries for each accept. Only those
11399 -- for which there is a body or trailing statements get a case entry.
11400
11401 Alt := First (Select_Alternatives (N));
11402 Proc := First (Body_List);
11403 while Present (Alt) loop
11404
11405 if Nkind (Alt) = N_Accept_Alternative then
11406 Process_Accept_Alternative (Alt, Index, Proc);
11407 Index := Index + 1;
11408
11409 if Present
11410 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11411 then
11412 Next (Proc);
11413 end if;
11414
11415 elsif Nkind (Alt) = N_Delay_Alternative then
11416 Process_Delay_Alternative (Alt, Delay_Num);
11417 Delay_Num := Delay_Num + 1;
11418 end if;
11419
11420 Next (Alt);
11421 end loop;
11422
11423 -- An others choice is always added to the main case, as well
11424 -- as the delay case (to satisfy the compiler).
11425
11426 Append_To (Alt_List,
11427 Make_Case_Statement_Alternative (Loc,
11428 Discrete_Choices =>
11429 New_List (Make_Others_Choice (Loc)),
11430 Statements =>
11431 New_List (Make_Goto_Statement (Loc,
11432 Name => New_Copy (Identifier (End_Lab))))));
11433
11434 Accept_Case := New_List (
11435 Make_Case_Statement (Loc,
11436 Expression => New_Occurrence_Of (Xnam, Loc),
11437 Alternatives => Alt_List));
11438
11439 Append_List (Trailing_List, Accept_Case);
11440 Append_List (Body_List, Decls);
11441
11442 -- Construct case statement for trailing statements of delay
11443 -- alternatives, if there are several of them.
11444
11445 if Delay_Count > 1 then
11446 Append_To (Delay_Alt_List,
11447 Make_Case_Statement_Alternative (Loc,
11448 Discrete_Choices =>
11449 New_List (Make_Others_Choice (Loc)),
11450 Statements =>
11451 New_List (Make_Null_Statement (Loc))));
11452
11453 Delay_Case := New_List (
11454 Make_Case_Statement (Loc,
11455 Expression => New_Occurrence_Of (Delay_Index, Loc),
11456 Alternatives => Delay_Alt_List));
11457 else
11458 Delay_Case := Delay_Alt_List;
11459 end if;
11460
11461 -- If there are no delay alternatives, we append the case statement
11462 -- to the statement list.
11463
11464 if Delay_Count = 0 then
11465 Append_List (Accept_Case, Stats);
11466
11467 -- Delay alternatives present
11468
11469 else
11470 -- If delay alternatives are present we generate:
11471
11472 -- find minimum delay.
11473 -- DX := minimum delay;
11474 -- M := <delay mode>;
11475 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11476 -- DX, MX, X);
11477 --
11478 -- if X = No_Rendezvous then
11479 -- case statement for delay statements.
11480 -- else
11481 -- case statement for accept alternatives.
11482 -- end if;
11483
11484 declare
11485 Cases : Node_Id;
11486 Stmt : Node_Id;
11487 Parms : List_Id;
11488 Parm : Node_Id;
11489 Conv : Node_Id;
11490
11491 begin
11492 -- The type of the delay expression is known to be legal
11493
11494 if Time_Type = Standard_Duration then
11495 Conv := New_Occurrence_Of (Delay_Min, Loc);
11496
11497 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11498 Conv := Make_Function_Call (Loc,
11499 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11500 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11501
11502 else
11503 pragma Assert
11504 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11505
11506 Conv := Make_Function_Call (Loc,
11507 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11508 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11509 end if;
11510
11511 Stmt := Make_Assignment_Statement (Loc,
11512 Name => New_Occurrence_Of (D, Loc),
11513 Expression => Conv);
11514
11515 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11516
11517 Parms := Parameter_Associations (Select_Call);
11518
11519 Parm := First (Parms);
11520 while Present (Parm) and then Parm /= Select_Mode loop
11521 Next (Parm);
11522 end loop;
11523
11524 pragma Assert (Present (Parm));
11525 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11526 Analyze (Parm);
11527
11528 -- Prepare two new parameters of Duration and Delay_Mode type
11529 -- which represent the value and the mode of the minimum delay.
11530
11531 Next (Parm);
11532 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11533 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11534
11535 -- Create a call to RTS
11536
11537 Rewrite (Select_Call,
11538 Make_Procedure_Call_Statement (Loc,
11539 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11540 Parameter_Associations => Parms));
11541
11542 -- This new call should follow the calculation of the minimum
11543 -- delay.
11544
11545 Insert_List_Before (Select_Call, Delay_List);
11546
11547 if Check_Guard then
11548 Stmt :=
11549 Make_Implicit_If_Statement (N,
11550 Condition => New_Occurrence_Of (Guard_Open, Loc),
11551 Then_Statements => New_List (
11552 New_Copy_Tree (Stmt),
11553 New_Copy_Tree (Select_Call)),
11554 Else_Statements => Accept_Or_Raise);
11555 Rewrite (Select_Call, Stmt);
11556 else
11557 Insert_Before (Select_Call, Stmt);
11558 end if;
11559
11560 Cases :=
11561 Make_Implicit_If_Statement (N,
11562 Condition => Make_Op_Eq (Loc,
11563 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11564 Right_Opnd =>
11565 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11566
11567 Then_Statements => Delay_Case,
11568 Else_Statements => Accept_Case);
11569
11570 Append (Cases, Stats);
11571 end;
11572 end if;
11573
11574 Append (End_Lab, Stats);
11575
11576 -- Replace accept statement with appropriate block
11577
11578 Rewrite (N,
11579 Make_Block_Statement (Loc,
11580 Declarations => Decls,
11581 Handled_Statement_Sequence =>
11582 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11583 Analyze (N);
11584
11585 -- Note: have to worry more about abort deferral in above code ???
11586
11587 -- Final step is to unstack the Accept_Address entries for all accept
11588 -- statements appearing in accept alternatives in the select statement
11589
11590 Alt := First (Alts);
11591 while Present (Alt) loop
11592 if Nkind (Alt) = N_Accept_Alternative then
11593 Remove_Last_Elmt (Accept_Address
11594 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11595 end if;
11596
11597 Next (Alt);
11598 end loop;
11599 end Expand_N_Selective_Accept;
11600
11601 -------------------------------------------
11602 -- Expand_N_Single_Protected_Declaration --
11603 -------------------------------------------
11604
11605 -- A single protected declaration should never be present after semantic
11606 -- analysis because it is transformed into a protected type declaration
11607 -- and an accompanying anonymous object. This routine ensures that the
11608 -- transformation takes place.
11609
11610 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11611 begin
11612 raise Program_Error;
11613 end Expand_N_Single_Protected_Declaration;
11614
11615 --------------------------------------
11616 -- Expand_N_Single_Task_Declaration --
11617 --------------------------------------
11618
11619 -- A single task declaration should never be present after semantic
11620 -- analysis because it is transformed into a task type declaration and
11621 -- an accompanying anonymous object. This routine ensures that the
11622 -- transformation takes place.
11623
11624 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11625 begin
11626 raise Program_Error;
11627 end Expand_N_Single_Task_Declaration;
11628
11629 ------------------------
11630 -- Expand_N_Task_Body --
11631 ------------------------
11632
11633 -- Given a task body
11634
11635 -- task body tname is
11636 -- <declarations>
11637 -- begin
11638 -- <statements>
11639 -- end x;
11640
11641 -- This expansion routine converts it into a procedure and sets the
11642 -- elaboration flag for the procedure to true, to represent the fact
11643 -- that the task body is now elaborated:
11644
11645 -- procedure tnameB (_Task : access tnameV) is
11646 -- discriminal : dtype renames _Task.discriminant;
11647
11648 -- procedure _clean is
11649 -- begin
11650 -- Abort_Defer.all;
11651 -- Complete_Task;
11652 -- Abort_Undefer.all;
11653 -- return;
11654 -- end _clean;
11655
11656 -- begin
11657 -- Abort_Undefer.all;
11658 -- <declarations>
11659 -- System.Task_Stages.Complete_Activation;
11660 -- <statements>
11661 -- at end
11662 -- _clean;
11663 -- end tnameB;
11664
11665 -- tnameE := True;
11666
11667 -- In addition, if the task body is an activator, then a call to activate
11668 -- tasks is added at the start of the statements, before the call to
11669 -- Complete_Activation, and if in addition the task is a master then it
11670 -- must be established as a master. These calls are inserted and analyzed
11671 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11672 -- expanded.
11673
11674 -- There is one discriminal declaration line generated for each
11675 -- discriminant that is present to provide an easy reference point for
11676 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11677
11678 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11679 -- task body procedures have a profile (Arg : System.Address). That is
11680 -- needed because GNARLI has to use the same access-to-subprogram type
11681 -- for all task types. We depend here on knowing that in GNAT, passing
11682 -- an address argument by value is identical to passing a record value
11683 -- by access (in either case a single pointer is passed), so even though
11684 -- this procedure has the wrong profile. In fact it's all OK, since the
11685 -- callings sequence is identical.
11686
11687 procedure Expand_N_Task_Body (N : Node_Id) is
11688 Loc : constant Source_Ptr := Sloc (N);
11689 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11690 Call : Node_Id;
11691 New_N : Node_Id;
11692
11693 Insert_Nod : Node_Id;
11694 -- Used to determine the proper location of wrapper body insertions
11695
11696 begin
11697 -- if no task body procedure, means we had an error in configurable
11698 -- run-time mode, and there is no point in proceeding further.
11699
11700 if No (Task_Body_Procedure (Ttyp)) then
11701 return;
11702 end if;
11703
11704 -- Add renaming declarations for discriminals and a declaration for the
11705 -- entry family index (if applicable).
11706
11707 Install_Private_Data_Declarations
11708 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11709
11710 -- Add a call to Abort_Undefer at the very beginning of the task
11711 -- body since this body is called with abort still deferred.
11712
11713 if Abort_Allowed then
11714 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11715 Insert_Before
11716 (First (Statements (Handled_Statement_Sequence (N))), Call);
11717 Analyze (Call);
11718 end if;
11719
11720 -- The statement part has already been protected with an at_end and
11721 -- cleanup actions. The call to Complete_Activation must be placed
11722 -- at the head of the sequence of statements of that block. The
11723 -- declarations have been merged in this sequence of statements but
11724 -- the first real statement is accessible from the First_Real_Statement
11725 -- field (which was set for exactly this purpose).
11726
11727 if Restricted_Profile then
11728 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11729 else
11730 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11731 end if;
11732
11733 Insert_Before
11734 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11735 Analyze (Call);
11736
11737 New_N :=
11738 Make_Subprogram_Body (Loc,
11739 Specification => Build_Task_Proc_Specification (Ttyp),
11740 Declarations => Declarations (N),
11741 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11742 Set_Is_Task_Body_Procedure (New_N);
11743
11744 -- If the task contains generic instantiations, cleanup actions are
11745 -- delayed until after instantiation. Transfer the activation chain to
11746 -- the subprogram, to insure that the activation call is properly
11747 -- generated. It the task body contains inner tasks, indicate that the
11748 -- subprogram is a task master.
11749
11750 if Delay_Cleanups (Ttyp) then
11751 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11752 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11753 end if;
11754
11755 Rewrite (N, New_N);
11756 Analyze (N);
11757
11758 -- Set elaboration flag immediately after task body. If the body is a
11759 -- subunit, the flag is set in the declarative part containing the stub.
11760
11761 if Nkind (Parent (N)) /= N_Subunit then
11762 Insert_After (N,
11763 Make_Assignment_Statement (Loc,
11764 Name =>
11765 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11766 Expression => New_Occurrence_Of (Standard_True, Loc)));
11767 end if;
11768
11769 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11770 -- the task body. At this point all wrapper specs have been created,
11771 -- frozen and included in the dispatch table for the task type.
11772
11773 if Ada_Version >= Ada_2005 then
11774 if Nkind (Parent (N)) = N_Subunit then
11775 Insert_Nod := Corresponding_Stub (Parent (N));
11776 else
11777 Insert_Nod := N;
11778 end if;
11779
11780 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11781 end if;
11782 end Expand_N_Task_Body;
11783
11784 ------------------------------------
11785 -- Expand_N_Task_Type_Declaration --
11786 ------------------------------------
11787
11788 -- We have several things to do. First we must create a Boolean flag used
11789 -- to mark if the body is elaborated yet. This variable gets set to True
11790 -- when the body of the task is elaborated (we can't rely on the normal
11791 -- ABE mechanism for the task body, since we need to pass an access to
11792 -- this elaboration boolean to the runtime routines).
11793
11794 -- taskE : aliased Boolean := False;
11795
11796 -- Next a variable is declared to hold the task stack size (either the
11797 -- default : Unspecified_Size, or a value that is set by a pragma
11798 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11799 -- the variable is initialized with this value:
11800
11801 -- taskZ : Size_Type := Unspecified_Size;
11802 -- or
11803 -- taskZ : Size_Type := Size_Type (size_expression);
11804
11805 -- Note: No variable is needed to hold the task relative deadline since
11806 -- its value would never be static because the parameter is of a private
11807 -- type (Ada.Real_Time.Time_Span).
11808
11809 -- Next we create a corresponding record type declaration used to represent
11810 -- values of this task. The general form of this type declaration is
11811
11812 -- type taskV (discriminants) is record
11813 -- _Task_Id : Task_Id;
11814 -- entry_family : array (bounds) of Void;
11815 -- _Priority : Integer := priority_expression;
11816 -- _Size : Size_Type := size_expression;
11817 -- _Task_Info : Task_Info_Type := task_info_expression;
11818 -- _CPU : Integer := cpu_range_expression;
11819 -- _Relative_Deadline : Time_Span := time_span_expression;
11820 -- _Domain : Dispatching_Domain := dd_expression;
11821 -- end record;
11822
11823 -- The discriminants are present only if the corresponding task type has
11824 -- discriminants, and they exactly mirror the task type discriminants.
11825
11826 -- The Id field is always present. It contains the Task_Id value, as set by
11827 -- the call to Create_Task. Note that although the task is limited, the
11828 -- task value record type is not limited, so there is no problem in passing
11829 -- this field as an out parameter to Create_Task.
11830
11831 -- One entry_family component is present for each entry family in the task
11832 -- definition. The bounds correspond to the bounds of the entry family
11833 -- (which may depend on discriminants). The element type is void, since we
11834 -- only need the bounds information for determining the entry index. Note
11835 -- that the use of an anonymous array would normally be illegal in this
11836 -- context, but this is a parser check, and the semantics is quite prepared
11837 -- to handle such a case.
11838
11839 -- The _Size field is present only if a Storage_Size pragma appears in the
11840 -- task definition. The expression captures the argument that was present
11841 -- in the pragma, and is used to override the task stack size otherwise
11842 -- associated with the task type.
11843
11844 -- The _Priority field is present only if the task entity has a Priority or
11845 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11846 -- definition clause). It will be filled at the freeze point, when the
11847 -- record init proc is built, to capture the expression of the rep item
11848 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11849 -- here since aspect evaluations are delayed till the freeze point.
11850
11851 -- The _Task_Info field is present only if a Task_Info pragma appears in
11852 -- the task definition. The expression captures the argument that was
11853 -- present in the pragma, and is used to provide the Task_Image parameter
11854 -- to the call to Create_Task.
11855
11856 -- The _CPU field is present only if the task entity has a CPU rep item
11857 -- (pragma, aspect specification or attribute definition clause). It will
11858 -- be filled at the freeze point, when the record init proc is built, to
11859 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11860 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11861 -- are delayed till the freeze point.
11862
11863 -- The _Relative_Deadline field is present only if a Relative_Deadline
11864 -- pragma appears in the task definition. The expression captures the
11865 -- argument that was present in the pragma, and is used to provide the
11866 -- Relative_Deadline parameter to the call to Create_Task.
11867
11868 -- The _Domain field is present only if the task entity has a
11869 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11870 -- definition clause). It will be filled at the freeze point, when the
11871 -- record init proc is built, to capture the expression of the rep item
11872 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11873 -- here since aspect evaluations are delayed till the freeze point.
11874
11875 -- When a task is declared, an instance of the task value record is
11876 -- created. The elaboration of this declaration creates the correct bounds
11877 -- for the entry families, and also evaluates the size, priority, and
11878 -- task_Info expressions if needed. The initialization routine for the task
11879 -- type itself then calls Create_Task with appropriate parameters to
11880 -- initialize the value of the Task_Id field.
11881
11882 -- Note: the address of this record is passed as the "Discriminants"
11883 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11884 -- body procedure, it does not matter that it does not quite match the
11885 -- GNARLI model of what is being passed (the record contains more than just
11886 -- the discriminants, but the discriminants can be found from the record
11887 -- value).
11888
11889 -- The Entity_Id for this created record type is placed in the
11890 -- Corresponding_Record_Type field of the associated task type entity.
11891
11892 -- Next we create a procedure specification for the task body procedure:
11893
11894 -- procedure taskB (_Task : access taskV);
11895
11896 -- Note that this must come after the record type declaration, since
11897 -- the spec refers to this type. It turns out that the initialization
11898 -- procedure for the value type references the task body spec, but that's
11899 -- fine, since it won't be generated till the freeze point for the type,
11900 -- which is certainly after the task body spec declaration.
11901
11902 -- Finally, we set the task index value field of the entry attribute in
11903 -- the case of a simple entry.
11904
11905 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11906 Loc : constant Source_Ptr := Sloc (N);
11907 TaskId : constant Entity_Id := Defining_Identifier (N);
11908 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11909 Tasknm : constant Name_Id := Chars (Tasktyp);
11910 Taskdef : constant Node_Id := Task_Definition (N);
11911
11912 Body_Decl : Node_Id;
11913 Cdecls : List_Id;
11914 Decl_Stack : Node_Id;
11915 Elab_Decl : Node_Id;
11916 Ent_Stack : Entity_Id;
11917 Proc_Spec : Node_Id;
11918 Rec_Decl : Node_Id;
11919 Rec_Ent : Entity_Id;
11920 Size_Decl : Entity_Id;
11921 Task_Size : Node_Id;
11922
11923 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11924 -- Searches the task definition T for the first occurrence of the pragma
11925 -- Relative Deadline. The caller has ensured that the pragma is present
11926 -- in the task definition. Note that this routine cannot be implemented
11927 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11928 -- not chained because their expansion into a procedure call statement
11929 -- would cause a break in the chain.
11930
11931 ----------------------------------
11932 -- Get_Relative_Deadline_Pragma --
11933 ----------------------------------
11934
11935 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11936 N : Node_Id;
11937
11938 begin
11939 N := First (Visible_Declarations (T));
11940 while Present (N) loop
11941 if Nkind (N) = N_Pragma
11942 and then Pragma_Name (N) = Name_Relative_Deadline
11943 then
11944 return N;
11945 end if;
11946
11947 Next (N);
11948 end loop;
11949
11950 N := First (Private_Declarations (T));
11951 while Present (N) loop
11952 if Nkind (N) = N_Pragma
11953 and then Pragma_Name (N) = Name_Relative_Deadline
11954 then
11955 return N;
11956 end if;
11957
11958 Next (N);
11959 end loop;
11960
11961 raise Program_Error;
11962 end Get_Relative_Deadline_Pragma;
11963
11964 -- Start of processing for Expand_N_Task_Type_Declaration
11965
11966 begin
11967 -- If already expanded, nothing to do
11968
11969 if Present (Corresponding_Record_Type (Tasktyp)) then
11970 return;
11971 end if;
11972
11973 -- Here we will do the expansion
11974
11975 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11976
11977 Rec_Ent := Defining_Identifier (Rec_Decl);
11978 Cdecls := Component_Items (Component_List
11979 (Type_Definition (Rec_Decl)));
11980
11981 Qualify_Entity_Names (N);
11982
11983 -- First create the elaboration variable
11984
11985 Elab_Decl :=
11986 Make_Object_Declaration (Loc,
11987 Defining_Identifier =>
11988 Make_Defining_Identifier (Sloc (Tasktyp),
11989 Chars => New_External_Name (Tasknm, 'E')),
11990 Aliased_Present => True,
11991 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11992 Expression => New_Occurrence_Of (Standard_False, Loc));
11993
11994 Insert_After (N, Elab_Decl);
11995
11996 -- Next create the declaration of the size variable (tasknmZ)
11997
11998 Set_Storage_Size_Variable (Tasktyp,
11999 Make_Defining_Identifier (Sloc (Tasktyp),
12000 Chars => New_External_Name (Tasknm, 'Z')));
12001
12002 if Present (Taskdef)
12003 and then Has_Storage_Size_Pragma (Taskdef)
12004 and then
12005 Is_OK_Static_Expression
12006 (Expression
12007 (First (Pragma_Argument_Associations
12008 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
12009 then
12010 Size_Decl :=
12011 Make_Object_Declaration (Loc,
12012 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12013 Object_Definition =>
12014 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12015 Expression =>
12016 Convert_To (RTE (RE_Size_Type),
12017 Relocate_Node
12018 (Expression (First (Pragma_Argument_Associations
12019 (Get_Rep_Pragma
12020 (TaskId, Name_Storage_Size)))))));
12021
12022 else
12023 Size_Decl :=
12024 Make_Object_Declaration (Loc,
12025 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12026 Object_Definition =>
12027 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12028 Expression =>
12029 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
12030 end if;
12031
12032 Insert_After (Elab_Decl, Size_Decl);
12033
12034 -- Next build the rest of the corresponding record declaration. This is
12035 -- done last, since the corresponding record initialization procedure
12036 -- will reference the previously created entities.
12037
12038 -- Fill in the component declarations -- first the _Task_Id field
12039
12040 Append_To (Cdecls,
12041 Make_Component_Declaration (Loc,
12042 Defining_Identifier =>
12043 Make_Defining_Identifier (Loc, Name_uTask_Id),
12044 Component_Definition =>
12045 Make_Component_Definition (Loc,
12046 Aliased_Present => False,
12047 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
12048 Loc))));
12049
12050 -- Declare static ATCB (that is, created by the expander) if we are
12051 -- using the Restricted run time.
12052
12053 if Restricted_Profile then
12054 Append_To (Cdecls,
12055 Make_Component_Declaration (Loc,
12056 Defining_Identifier =>
12057 Make_Defining_Identifier (Loc, Name_uATCB),
12058
12059 Component_Definition =>
12060 Make_Component_Definition (Loc,
12061 Aliased_Present => True,
12062 Subtype_Indication => Make_Subtype_Indication (Loc,
12063 Subtype_Mark =>
12064 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
12065
12066 Constraint =>
12067 Make_Index_Or_Discriminant_Constraint (Loc,
12068 Constraints =>
12069 New_List (Make_Integer_Literal (Loc, 0)))))));
12070
12071 end if;
12072
12073 -- Declare static stack (that is, created by the expander) if we are
12074 -- using the Restricted run time on a bare board configuration.
12075
12076 if Restricted_Profile and then Preallocated_Stacks_On_Target then
12077
12078 -- First we need to extract the appropriate stack size
12079
12080 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
12081
12082 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12083 declare
12084 Expr_N : constant Node_Id :=
12085 Expression (First (
12086 Pragma_Argument_Associations (
12087 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
12088 Etyp : constant Entity_Id := Etype (Expr_N);
12089 P : constant Node_Id := Parent (Expr_N);
12090
12091 begin
12092 -- The stack is defined inside the corresponding record.
12093 -- Therefore if the size of the stack is set by means of
12094 -- a discriminant, we must reference the discriminant of the
12095 -- corresponding record type.
12096
12097 if Nkind (Expr_N) in N_Has_Entity
12098 and then Present (Discriminal_Link (Entity (Expr_N)))
12099 then
12100 Task_Size :=
12101 New_Occurrence_Of
12102 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
12103 Loc);
12104 Set_Parent (Task_Size, P);
12105 Set_Etype (Task_Size, Etyp);
12106 Set_Analyzed (Task_Size);
12107
12108 else
12109 Task_Size := Relocate_Node (Expr_N);
12110 end if;
12111 end;
12112
12113 else
12114 Task_Size :=
12115 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
12116 end if;
12117
12118 Decl_Stack := Make_Component_Declaration (Loc,
12119 Defining_Identifier => Ent_Stack,
12120
12121 Component_Definition =>
12122 Make_Component_Definition (Loc,
12123 Aliased_Present => True,
12124 Subtype_Indication => Make_Subtype_Indication (Loc,
12125 Subtype_Mark =>
12126 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
12127
12128 Constraint =>
12129 Make_Index_Or_Discriminant_Constraint (Loc,
12130 Constraints => New_List (Make_Range (Loc,
12131 Low_Bound => Make_Integer_Literal (Loc, 1),
12132 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12133 Task_Size)))))));
12134
12135 Append_To (Cdecls, Decl_Stack);
12136
12137 -- The appropriate alignment for the stack is ensured by the run-time
12138 -- code in charge of task creation.
12139
12140 end if;
12141
12142 -- Add components for entry families
12143
12144 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12145
12146 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12147 -- item is present.
12148
12149 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12150 Append_To (Cdecls,
12151 Make_Component_Declaration (Loc,
12152 Defining_Identifier =>
12153 Make_Defining_Identifier (Loc, Name_uPriority),
12154 Component_Definition =>
12155 Make_Component_Definition (Loc,
12156 Aliased_Present => False,
12157 Subtype_Indication =>
12158 New_Occurrence_Of (Standard_Integer, Loc))));
12159 end if;
12160
12161 -- Add the _Size component if a Storage_Size pragma is present
12162
12163 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12164 Append_To (Cdecls,
12165 Make_Component_Declaration (Loc,
12166 Defining_Identifier =>
12167 Make_Defining_Identifier (Loc, Name_uSize),
12168
12169 Component_Definition =>
12170 Make_Component_Definition (Loc,
12171 Aliased_Present => False,
12172 Subtype_Indication =>
12173 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12174
12175 Expression =>
12176 Convert_To (RTE (RE_Size_Type),
12177 Relocate_Node (
12178 Expression (First (
12179 Pragma_Argument_Associations (
12180 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12181 end if;
12182
12183 -- Add the _Task_Info component if a Task_Info pragma is present
12184
12185 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12186 Append_To (Cdecls,
12187 Make_Component_Declaration (Loc,
12188 Defining_Identifier =>
12189 Make_Defining_Identifier (Loc, Name_uTask_Info),
12190
12191 Component_Definition =>
12192 Make_Component_Definition (Loc,
12193 Aliased_Present => False,
12194 Subtype_Indication =>
12195 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12196
12197 Expression => New_Copy (
12198 Expression (First (
12199 Pragma_Argument_Associations (
12200 Get_Rep_Pragma
12201 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12202 end if;
12203
12204 -- Add the _CPU component if a CPU rep item is present
12205
12206 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12207 Append_To (Cdecls,
12208 Make_Component_Declaration (Loc,
12209 Defining_Identifier =>
12210 Make_Defining_Identifier (Loc, Name_uCPU),
12211
12212 Component_Definition =>
12213 Make_Component_Definition (Loc,
12214 Aliased_Present => False,
12215 Subtype_Indication =>
12216 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12217 end if;
12218
12219 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12220 -- present. If we are using a restricted run time this component will
12221 -- not be added (deadlines are not allowed by the Ravenscar profile).
12222
12223 if not Restricted_Profile
12224 and then Present (Taskdef)
12225 and then Has_Relative_Deadline_Pragma (Taskdef)
12226 then
12227 Append_To (Cdecls,
12228 Make_Component_Declaration (Loc,
12229 Defining_Identifier =>
12230 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12231
12232 Component_Definition =>
12233 Make_Component_Definition (Loc,
12234 Aliased_Present => False,
12235 Subtype_Indication =>
12236 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12237
12238 Expression =>
12239 Convert_To (RTE (RE_Time_Span),
12240 Relocate_Node (
12241 Expression (First (
12242 Pragma_Argument_Associations (
12243 Get_Relative_Deadline_Pragma (Taskdef))))))));
12244 end if;
12245
12246 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12247 -- item is present. If we are using a restricted run time this component
12248 -- will not be added (dispatching domains are not allowed by the
12249 -- Ravenscar profile).
12250
12251 if not Restricted_Profile
12252 and then
12253 Has_Rep_Item
12254 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12255 then
12256 Append_To (Cdecls,
12257 Make_Component_Declaration (Loc,
12258 Defining_Identifier =>
12259 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12260
12261 Component_Definition =>
12262 Make_Component_Definition (Loc,
12263 Aliased_Present => False,
12264 Subtype_Indication =>
12265 New_Occurrence_Of
12266 (RTE (RE_Dispatching_Domain_Access), Loc))));
12267 end if;
12268
12269 Insert_After (Size_Decl, Rec_Decl);
12270
12271 -- Analyze the record declaration immediately after construction,
12272 -- because the initialization procedure is needed for single task
12273 -- declarations before the next entity is analyzed.
12274
12275 Analyze (Rec_Decl);
12276
12277 -- Create the declaration of the task body procedure
12278
12279 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12280 Body_Decl :=
12281 Make_Subprogram_Declaration (Loc,
12282 Specification => Proc_Spec);
12283 Set_Is_Task_Body_Procedure (Body_Decl);
12284
12285 Insert_After (Rec_Decl, Body_Decl);
12286
12287 -- The subprogram does not comes from source, so we have to indicate the
12288 -- need for debugging information explicitly.
12289
12290 if Comes_From_Source (Original_Node (N)) then
12291 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12292 end if;
12293
12294 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12295 -- the corresponding record has been frozen.
12296
12297 if Ada_Version >= Ada_2005 then
12298 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12299 end if;
12300
12301 -- Ada 2005 (AI-345): We must defer freezing to allow further
12302 -- declaration of primitive subprograms covering task interfaces
12303
12304 if Ada_Version <= Ada_95 then
12305
12306 -- Now we can freeze the corresponding record. This needs manually
12307 -- freezing, since it is really part of the task type, and the task
12308 -- type is frozen at this stage. We of course need the initialization
12309 -- procedure for this corresponding record type and we won't get it
12310 -- in time if we don't freeze now.
12311
12312 declare
12313 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12314 begin
12315 if Is_Non_Empty_List (L) then
12316 Insert_List_After (Body_Decl, L);
12317 end if;
12318 end;
12319 end if;
12320
12321 -- Complete the expansion of access types to the current task type, if
12322 -- any were declared.
12323
12324 Expand_Previous_Access_Type (Tasktyp);
12325
12326 -- Create wrappers for entries that have contract cases, preconditions
12327 -- and postconditions.
12328
12329 declare
12330 Ent : Entity_Id;
12331
12332 begin
12333 Ent := First_Entity (Tasktyp);
12334 while Present (Ent) loop
12335 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12336 Build_Contract_Wrapper (Ent, N);
12337 end if;
12338
12339 Next_Entity (Ent);
12340 end loop;
12341 end;
12342 end Expand_N_Task_Type_Declaration;
12343
12344 -------------------------------
12345 -- Expand_N_Timed_Entry_Call --
12346 -------------------------------
12347
12348 -- A timed entry call in normal case is not implemented using ATC mechanism
12349 -- anymore for efficiency reason.
12350
12351 -- select
12352 -- T.E;
12353 -- S1;
12354 -- or
12355 -- delay D;
12356 -- S2;
12357 -- end select;
12358
12359 -- is expanded as follows:
12360
12361 -- 1) When T.E is a task entry_call;
12362
12363 -- declare
12364 -- B : Boolean;
12365 -- X : Task_Entry_Index := <entry index>;
12366 -- DX : Duration := To_Duration (D);
12367 -- M : Delay_Mode := <discriminant>;
12368 -- P : parms := (parm, parm, parm);
12369
12370 -- begin
12371 -- Timed_Protected_Entry_Call
12372 -- (<acceptor-task>, X, P'Address, DX, M, B);
12373 -- if B then
12374 -- S1;
12375 -- else
12376 -- S2;
12377 -- end if;
12378 -- end;
12379
12380 -- 2) When T.E is a protected entry_call;
12381
12382 -- declare
12383 -- B : Boolean;
12384 -- X : Protected_Entry_Index := <entry index>;
12385 -- DX : Duration := To_Duration (D);
12386 -- M : Delay_Mode := <discriminant>;
12387 -- P : parms := (parm, parm, parm);
12388
12389 -- begin
12390 -- Timed_Protected_Entry_Call
12391 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12392 -- if B then
12393 -- S1;
12394 -- else
12395 -- S2;
12396 -- end if;
12397 -- end;
12398
12399 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12400 -- is no delay and the triggering statements are executed. We first
12401 -- determine the kind of the triggering call and then execute a
12402 -- synchronized operation or a direct call.
12403
12404 -- declare
12405 -- B : Boolean := False;
12406 -- C : Ada.Tags.Prim_Op_Kind;
12407 -- DX : Duration := To_Duration (D)
12408 -- K : Ada.Tags.Tagged_Kind :=
12409 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12410 -- M : Integer :=...;
12411 -- P : Parameters := (Param1 .. ParamN);
12412 -- S : Integer;
12413
12414 -- begin
12415 -- if K = Ada.Tags.TK_Limited_Tagged
12416 -- or else K = Ada.Tags.TK_Tagged
12417 -- then
12418 -- <dispatching-call>;
12419 -- B := True;
12420
12421 -- else
12422 -- S :=
12423 -- Ada.Tags.Get_Offset_Index
12424 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12425
12426 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12427
12428 -- if C = POK_Protected_Entry
12429 -- or else C = POK_Task_Entry
12430 -- then
12431 -- Param1 := P.Param1;
12432 -- ...
12433 -- ParamN := P.ParamN;
12434 -- end if;
12435
12436 -- if B then
12437 -- if C = POK_Procedure
12438 -- or else C = POK_Protected_Procedure
12439 -- or else C = POK_Task_Procedure
12440 -- then
12441 -- <dispatching-call>;
12442 -- end if;
12443 -- end if;
12444 -- end if;
12445
12446 -- if B then
12447 -- <triggering-statements>
12448 -- else
12449 -- <timed-statements>
12450 -- end if;
12451 -- end;
12452
12453 -- The triggering statement and the sequence of timed statements have not
12454 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12455 -- global references if within an instantiation.
12456
12457 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12458 Loc : constant Source_Ptr := Sloc (N);
12459
12460 Actuals : List_Id;
12461 Blk_Typ : Entity_Id;
12462 Call : Node_Id;
12463 Call_Ent : Entity_Id;
12464 Conc_Typ_Stmts : List_Id;
12465 Concval : Node_Id;
12466 D_Alt : constant Node_Id := Delay_Alternative (N);
12467 D_Conv : Node_Id;
12468 D_Disc : Node_Id;
12469 D_Stat : Node_Id := Delay_Statement (D_Alt);
12470 D_Stats : List_Id;
12471 D_Type : Entity_Id;
12472 Decls : List_Id;
12473 Dummy : Node_Id;
12474 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12475 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12476 E_Stats : List_Id;
12477 Ename : Node_Id;
12478 Formals : List_Id;
12479 Index : Node_Id;
12480 Is_Disp_Select : Boolean;
12481 Lim_Typ_Stmts : List_Id;
12482 N_Stats : List_Id;
12483 Obj : Entity_Id;
12484 Param : Node_Id;
12485 Params : List_Id;
12486 Stmt : Node_Id;
12487 Stmts : List_Id;
12488 Unpack : List_Id;
12489
12490 B : Entity_Id; -- Call status flag
12491 C : Entity_Id; -- Call kind
12492 D : Entity_Id; -- Delay
12493 K : Entity_Id; -- Tagged kind
12494 M : Entity_Id; -- Delay mode
12495 P : Entity_Id; -- Parameter block
12496 S : Entity_Id; -- Primitive operation slot
12497
12498 -- Start of processing for Expand_N_Timed_Entry_Call
12499
12500 begin
12501 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12502 -- was already reported on spec, so do not attempt to expand the call.
12503
12504 if Restriction_Active (No_Select_Statements) then
12505 return;
12506 end if;
12507
12508 Process_Statements_For_Controlled_Objects (E_Alt);
12509 Process_Statements_For_Controlled_Objects (D_Alt);
12510
12511 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12512
12513 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12514 -- may wrap them in blocks.
12515
12516 E_Stats := Statements (E_Alt);
12517 D_Stats := Statements (D_Alt);
12518
12519 -- The arguments in the call may require dynamic allocation, and the
12520 -- call statement may have been transformed into a block. The block
12521 -- may contain additional declarations for internal entities, and the
12522 -- original call is found by sequential search.
12523
12524 if Nkind (E_Call) = N_Block_Statement then
12525 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12526 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12527 N_Entry_Call_Statement)
12528 loop
12529 Next (E_Call);
12530 end loop;
12531 end if;
12532
12533 Is_Disp_Select :=
12534 Ada_Version >= Ada_2005
12535 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12536
12537 if Is_Disp_Select then
12538 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12539 Decls := New_List;
12540
12541 Stmts := New_List;
12542
12543 -- Generate:
12544 -- B : Boolean := False;
12545
12546 B := Build_B (Loc, Decls);
12547
12548 -- Generate:
12549 -- C : Ada.Tags.Prim_Op_Kind;
12550
12551 C := Build_C (Loc, Decls);
12552
12553 -- Because the analysis of all statements was disabled, manually
12554 -- analyze the delay statement.
12555
12556 Analyze (D_Stat);
12557 D_Stat := Original_Node (D_Stat);
12558
12559 else
12560 -- Build an entry call using Simple_Entry_Call
12561
12562 Extract_Entry (E_Call, Concval, Ename, Index);
12563 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12564
12565 Decls := Declarations (E_Call);
12566 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12567
12568 if No (Decls) then
12569 Decls := New_List;
12570 end if;
12571
12572 -- Generate:
12573 -- B : Boolean;
12574
12575 B := Make_Defining_Identifier (Loc, Name_uB);
12576
12577 Prepend_To (Decls,
12578 Make_Object_Declaration (Loc,
12579 Defining_Identifier => B,
12580 Object_Definition =>
12581 New_Occurrence_Of (Standard_Boolean, Loc)));
12582 end if;
12583
12584 -- Duration and mode processing
12585
12586 D_Type := Base_Type (Etype (Expression (D_Stat)));
12587
12588 -- Use the type of the delay expression (Calendar or Real_Time) to
12589 -- generate the appropriate conversion.
12590
12591 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12592 D_Disc := Make_Integer_Literal (Loc, 0);
12593 D_Conv := Relocate_Node (Expression (D_Stat));
12594
12595 elsif Is_RTE (D_Type, RO_CA_Time) then
12596 D_Disc := Make_Integer_Literal (Loc, 1);
12597 D_Conv :=
12598 Make_Function_Call (Loc,
12599 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12600 Parameter_Associations =>
12601 New_List (New_Copy (Expression (D_Stat))));
12602
12603 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12604 D_Disc := Make_Integer_Literal (Loc, 2);
12605 D_Conv :=
12606 Make_Function_Call (Loc,
12607 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12608 Parameter_Associations =>
12609 New_List (New_Copy (Expression (D_Stat))));
12610 end if;
12611
12612 D := Make_Temporary (Loc, 'D');
12613
12614 -- Generate:
12615 -- D : Duration;
12616
12617 Append_To (Decls,
12618 Make_Object_Declaration (Loc,
12619 Defining_Identifier => D,
12620 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12621
12622 M := Make_Temporary (Loc, 'M');
12623
12624 -- Generate:
12625 -- M : Integer := (0 | 1 | 2);
12626
12627 Append_To (Decls,
12628 Make_Object_Declaration (Loc,
12629 Defining_Identifier => M,
12630 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12631 Expression => D_Disc));
12632
12633 -- Do the assignment at this stage only because the evaluation of the
12634 -- expression must not occur before (see ACVC C97302A).
12635
12636 Append_To (Stmts,
12637 Make_Assignment_Statement (Loc,
12638 Name => New_Occurrence_Of (D, Loc),
12639 Expression => D_Conv));
12640
12641 -- Parameter block processing
12642
12643 -- Manually create the parameter block for dispatching calls. In the
12644 -- case of entries, the block has already been created during the call
12645 -- to Build_Simple_Entry_Call.
12646
12647 if Is_Disp_Select then
12648
12649 -- Tagged kind processing, generate:
12650 -- K : Ada.Tags.Tagged_Kind :=
12651 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12652
12653 K := Build_K (Loc, Decls, Obj);
12654
12655 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12656 P :=
12657 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12658
12659 -- Dispatch table slot processing, generate:
12660 -- S : Integer;
12661
12662 S := Build_S (Loc, Decls);
12663
12664 -- Generate:
12665 -- S := Ada.Tags.Get_Offset_Index
12666 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12667
12668 Conc_Typ_Stmts :=
12669 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12670
12671 -- Generate:
12672 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12673
12674 -- where Obj is the controlling formal parameter, S is the dispatch
12675 -- table slot number of the dispatching operation, P is the wrapped
12676 -- parameter block, D is the duration, M is the duration mode, C is
12677 -- the call kind and B is the call status.
12678
12679 Params := New_List;
12680
12681 Append_To (Params, New_Copy_Tree (Obj));
12682 Append_To (Params, New_Occurrence_Of (S, Loc));
12683 Append_To (Params,
12684 Make_Attribute_Reference (Loc,
12685 Prefix => New_Occurrence_Of (P, Loc),
12686 Attribute_Name => Name_Address));
12687 Append_To (Params, New_Occurrence_Of (D, Loc));
12688 Append_To (Params, New_Occurrence_Of (M, Loc));
12689 Append_To (Params, New_Occurrence_Of (C, Loc));
12690 Append_To (Params, New_Occurrence_Of (B, Loc));
12691
12692 Append_To (Conc_Typ_Stmts,
12693 Make_Procedure_Call_Statement (Loc,
12694 Name =>
12695 New_Occurrence_Of
12696 (Find_Prim_Op
12697 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12698 Parameter_Associations => Params));
12699
12700 -- Generate:
12701 -- if C = POK_Protected_Entry
12702 -- or else C = POK_Task_Entry
12703 -- then
12704 -- Param1 := P.Param1;
12705 -- ...
12706 -- ParamN := P.ParamN;
12707 -- end if;
12708
12709 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12710
12711 -- Generate the if statement only when the packed parameters need
12712 -- explicit assignments to their corresponding actuals.
12713
12714 if Present (Unpack) then
12715 Append_To (Conc_Typ_Stmts,
12716 Make_Implicit_If_Statement (N,
12717
12718 Condition =>
12719 Make_Or_Else (Loc,
12720 Left_Opnd =>
12721 Make_Op_Eq (Loc,
12722 Left_Opnd => New_Occurrence_Of (C, Loc),
12723 Right_Opnd =>
12724 New_Occurrence_Of
12725 (RTE (RE_POK_Protected_Entry), Loc)),
12726
12727 Right_Opnd =>
12728 Make_Op_Eq (Loc,
12729 Left_Opnd => New_Occurrence_Of (C, Loc),
12730 Right_Opnd =>
12731 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12732
12733 Then_Statements => Unpack));
12734 end if;
12735
12736 -- Generate:
12737
12738 -- if B then
12739 -- if C = POK_Procedure
12740 -- or else C = POK_Protected_Procedure
12741 -- or else C = POK_Task_Procedure
12742 -- then
12743 -- <dispatching-call>
12744 -- end if;
12745 -- end if;
12746
12747 N_Stats := New_List (
12748 Make_Implicit_If_Statement (N,
12749 Condition =>
12750 Make_Or_Else (Loc,
12751 Left_Opnd =>
12752 Make_Op_Eq (Loc,
12753 Left_Opnd => New_Occurrence_Of (C, Loc),
12754 Right_Opnd =>
12755 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12756
12757 Right_Opnd =>
12758 Make_Or_Else (Loc,
12759 Left_Opnd =>
12760 Make_Op_Eq (Loc,
12761 Left_Opnd => New_Occurrence_Of (C, Loc),
12762 Right_Opnd =>
12763 New_Occurrence_Of (RTE (
12764 RE_POK_Protected_Procedure), Loc)),
12765 Right_Opnd =>
12766 Make_Op_Eq (Loc,
12767 Left_Opnd => New_Occurrence_Of (C, Loc),
12768 Right_Opnd =>
12769 New_Occurrence_Of
12770 (RTE (RE_POK_Task_Procedure), Loc)))),
12771
12772 Then_Statements => New_List (E_Call)));
12773
12774 Append_To (Conc_Typ_Stmts,
12775 Make_Implicit_If_Statement (N,
12776 Condition => New_Occurrence_Of (B, Loc),
12777 Then_Statements => N_Stats));
12778
12779 -- Generate:
12780 -- <dispatching-call>;
12781 -- B := True;
12782
12783 Lim_Typ_Stmts :=
12784 New_List (New_Copy_Tree (E_Call),
12785 Make_Assignment_Statement (Loc,
12786 Name => New_Occurrence_Of (B, Loc),
12787 Expression => New_Occurrence_Of (Standard_True, Loc)));
12788
12789 -- Generate:
12790 -- if K = Ada.Tags.TK_Limited_Tagged
12791 -- or else K = Ada.Tags.TK_Tagged
12792 -- then
12793 -- Lim_Typ_Stmts
12794 -- else
12795 -- Conc_Typ_Stmts
12796 -- end if;
12797
12798 Append_To (Stmts,
12799 Make_Implicit_If_Statement (N,
12800 Condition => Build_Dispatching_Tag_Check (K, N),
12801 Then_Statements => Lim_Typ_Stmts,
12802 Else_Statements => Conc_Typ_Stmts));
12803
12804 -- Generate:
12805
12806 -- if B then
12807 -- <triggering-statements>
12808 -- else
12809 -- <timed-statements>
12810 -- end if;
12811
12812 Append_To (Stmts,
12813 Make_Implicit_If_Statement (N,
12814 Condition => New_Occurrence_Of (B, Loc),
12815 Then_Statements => E_Stats,
12816 Else_Statements => D_Stats));
12817
12818 else
12819 -- Simple case of a non-dispatching trigger. Skip assignments to
12820 -- temporaries created for in-out parameters.
12821
12822 -- This makes unwarranted assumptions about the shape of the expanded
12823 -- tree for the call, and should be cleaned up ???
12824
12825 Stmt := First (Stmts);
12826 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12827 Next (Stmt);
12828 end loop;
12829
12830 -- Do the assignment at this stage only because the evaluation
12831 -- of the expression must not occur before (see ACVC C97302A).
12832
12833 Insert_Before (Stmt,
12834 Make_Assignment_Statement (Loc,
12835 Name => New_Occurrence_Of (D, Loc),
12836 Expression => D_Conv));
12837
12838 Call := Stmt;
12839 Params := Parameter_Associations (Call);
12840
12841 -- For a protected type, we build a Timed_Protected_Entry_Call
12842
12843 if Is_Protected_Type (Etype (Concval)) then
12844
12845 -- Create a new call statement
12846
12847 Param := First (Params);
12848 while Present (Param)
12849 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12850 loop
12851 Next (Param);
12852 end loop;
12853
12854 Dummy := Remove_Next (Next (Param));
12855
12856 -- Remove garbage is following the Cancel_Param if present
12857
12858 Dummy := Next (Param);
12859
12860 -- Remove the mode of the Protected_Entry_Call call, then remove
12861 -- the Communication_Block of the Protected_Entry_Call call, and
12862 -- finally add Duration and a Delay_Mode parameter
12863
12864 pragma Assert (Present (Param));
12865 Rewrite (Param, New_Occurrence_Of (D, Loc));
12866
12867 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12868
12869 -- Add a Boolean flag for successful entry call
12870
12871 Append_To (Params, New_Occurrence_Of (B, Loc));
12872
12873 case Corresponding_Runtime_Package (Etype (Concval)) is
12874 when System_Tasking_Protected_Objects_Entries =>
12875 Rewrite (Call,
12876 Make_Procedure_Call_Statement (Loc,
12877 Name =>
12878 New_Occurrence_Of
12879 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12880 Parameter_Associations => Params));
12881
12882 when others =>
12883 raise Program_Error;
12884 end case;
12885
12886 -- For the task case, build a Timed_Task_Entry_Call
12887
12888 else
12889 -- Create a new call statement
12890
12891 Append_To (Params, New_Occurrence_Of (D, Loc));
12892 Append_To (Params, New_Occurrence_Of (M, Loc));
12893 Append_To (Params, New_Occurrence_Of (B, Loc));
12894
12895 Rewrite (Call,
12896 Make_Procedure_Call_Statement (Loc,
12897 Name =>
12898 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12899 Parameter_Associations => Params));
12900 end if;
12901
12902 Append_To (Stmts,
12903 Make_Implicit_If_Statement (N,
12904 Condition => New_Occurrence_Of (B, Loc),
12905 Then_Statements => E_Stats,
12906 Else_Statements => D_Stats));
12907 end if;
12908
12909 Rewrite (N,
12910 Make_Block_Statement (Loc,
12911 Declarations => Decls,
12912 Handled_Statement_Sequence =>
12913 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12914
12915 Analyze (N);
12916 end Expand_N_Timed_Entry_Call;
12917
12918 ----------------------------------------
12919 -- Expand_Protected_Body_Declarations --
12920 ----------------------------------------
12921
12922 procedure Expand_Protected_Body_Declarations
12923 (N : Node_Id;
12924 Spec_Id : Entity_Id)
12925 is
12926 begin
12927 if No_Run_Time_Mode then
12928 Error_Msg_CRT ("protected body", N);
12929 return;
12930
12931 elsif Expander_Active then
12932
12933 -- Associate discriminals with the first subprogram or entry body to
12934 -- be expanded.
12935
12936 if Present (First_Protected_Operation (Declarations (N))) then
12937 Set_Discriminals (Parent (Spec_Id));
12938 end if;
12939 end if;
12940 end Expand_Protected_Body_Declarations;
12941
12942 -------------------------
12943 -- External_Subprogram --
12944 -------------------------
12945
12946 function External_Subprogram (E : Entity_Id) return Entity_Id is
12947 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12948
12949 begin
12950 -- The internal and external subprograms follow each other on the entity
12951 -- chain. Note that previously private operations had no separate
12952 -- external subprogram. We now create one in all cases, because a
12953 -- private operation may actually appear in an external call, through
12954 -- a 'Access reference used for a callback.
12955
12956 -- If the operation is a function that returns an anonymous access type,
12957 -- the corresponding itype appears before the operation, and must be
12958 -- skipped.
12959
12960 -- This mechanism is fragile, there should be a real link between the
12961 -- two versions of the operation, but there is no place to put it ???
12962
12963 if Is_Access_Type (Next_Entity (Subp)) then
12964 return Next_Entity (Next_Entity (Subp));
12965 else
12966 return Next_Entity (Subp);
12967 end if;
12968 end External_Subprogram;
12969
12970 ------------------------------
12971 -- Extract_Dispatching_Call --
12972 ------------------------------
12973
12974 procedure Extract_Dispatching_Call
12975 (N : Node_Id;
12976 Call_Ent : out Entity_Id;
12977 Object : out Entity_Id;
12978 Actuals : out List_Id;
12979 Formals : out List_Id)
12980 is
12981 Call_Nam : Node_Id;
12982
12983 begin
12984 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12985
12986 if Present (Original_Node (N)) then
12987 Call_Nam := Name (Original_Node (N));
12988 else
12989 Call_Nam := Name (N);
12990 end if;
12991
12992 -- Retrieve the name of the dispatching procedure. It contains the
12993 -- dispatch table slot number.
12994
12995 loop
12996 case Nkind (Call_Nam) is
12997 when N_Identifier =>
12998 exit;
12999
13000 when N_Selected_Component =>
13001 Call_Nam := Selector_Name (Call_Nam);
13002
13003 when others =>
13004 raise Program_Error;
13005
13006 end case;
13007 end loop;
13008
13009 Actuals := Parameter_Associations (N);
13010 Call_Ent := Entity (Call_Nam);
13011 Formals := Parameter_Specifications (Parent (Call_Ent));
13012 Object := First (Actuals);
13013
13014 if Present (Original_Node (Object)) then
13015 Object := Original_Node (Object);
13016 end if;
13017
13018 -- If the type of the dispatching object is an access type then return
13019 -- an explicit dereference.
13020
13021 if Is_Access_Type (Etype (Object)) then
13022 Object := Make_Explicit_Dereference (Sloc (N), Object);
13023 Analyze (Object);
13024 end if;
13025 end Extract_Dispatching_Call;
13026
13027 -------------------
13028 -- Extract_Entry --
13029 -------------------
13030
13031 procedure Extract_Entry
13032 (N : Node_Id;
13033 Concval : out Node_Id;
13034 Ename : out Node_Id;
13035 Index : out Node_Id)
13036 is
13037 Nam : constant Node_Id := Name (N);
13038
13039 begin
13040 -- For a simple entry, the name is a selected component, with the
13041 -- prefix being the task value, and the selector being the entry.
13042
13043 if Nkind (Nam) = N_Selected_Component then
13044 Concval := Prefix (Nam);
13045 Ename := Selector_Name (Nam);
13046 Index := Empty;
13047
13048 -- For a member of an entry family, the name is an indexed component
13049 -- where the prefix is a selected component, whose prefix in turn is
13050 -- the task value, and whose selector is the entry family. The single
13051 -- expression in the expressions list of the indexed component is the
13052 -- subscript for the family.
13053
13054 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13055 Concval := Prefix (Prefix (Nam));
13056 Ename := Selector_Name (Prefix (Nam));
13057 Index := First (Expressions (Nam));
13058 end if;
13059
13060 -- Through indirection, the type may actually be a limited view of a
13061 -- concurrent type. When compiling a call, the non-limited view of the
13062 -- type is visible.
13063
13064 if From_Limited_With (Etype (Concval)) then
13065 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13066 end if;
13067 end Extract_Entry;
13068
13069 -------------------
13070 -- Family_Offset --
13071 -------------------
13072
13073 function Family_Offset
13074 (Loc : Source_Ptr;
13075 Hi : Node_Id;
13076 Lo : Node_Id;
13077 Ttyp : Entity_Id;
13078 Cap : Boolean) return Node_Id
13079 is
13080 Ityp : Entity_Id;
13081 Real_Hi : Node_Id;
13082 Real_Lo : Node_Id;
13083
13084 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13085 -- If one of the bounds is a reference to a discriminant, replace with
13086 -- corresponding discriminal of type. Within the body of a task retrieve
13087 -- the renamed discriminant by simple visibility, using its generated
13088 -- name. Within a protected object, find the original discriminant and
13089 -- replace it with the discriminal of the current protected operation.
13090
13091 ------------------------------
13092 -- Convert_Discriminant_Ref --
13093 ------------------------------
13094
13095 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13096 Loc : constant Source_Ptr := Sloc (Bound);
13097 B : Node_Id;
13098 D : Entity_Id;
13099
13100 begin
13101 if Is_Entity_Name (Bound)
13102 and then Ekind (Entity (Bound)) = E_Discriminant
13103 then
13104 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13105 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13106 Find_Direct_Name (B);
13107
13108 elsif Is_Protected_Type (Ttyp) then
13109 D := First_Discriminant (Ttyp);
13110 while Chars (D) /= Chars (Entity (Bound)) loop
13111 Next_Discriminant (D);
13112 end loop;
13113
13114 B := New_Occurrence_Of (Discriminal (D), Loc);
13115
13116 else
13117 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13118 end if;
13119
13120 elsif Nkind (Bound) = N_Attribute_Reference then
13121 return Bound;
13122
13123 else
13124 B := New_Copy_Tree (Bound);
13125 end if;
13126
13127 return
13128 Make_Attribute_Reference (Loc,
13129 Attribute_Name => Name_Pos,
13130 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13131 Expressions => New_List (B));
13132 end Convert_Discriminant_Ref;
13133
13134 -- Start of processing for Family_Offset
13135
13136 begin
13137 Real_Hi := Convert_Discriminant_Ref (Hi);
13138 Real_Lo := Convert_Discriminant_Ref (Lo);
13139
13140 if Cap then
13141 if Is_Task_Type (Ttyp) then
13142 Ityp := RTE (RE_Task_Entry_Index);
13143 else
13144 Ityp := RTE (RE_Protected_Entry_Index);
13145 end if;
13146
13147 Real_Hi :=
13148 Make_Attribute_Reference (Loc,
13149 Prefix => New_Occurrence_Of (Ityp, Loc),
13150 Attribute_Name => Name_Min,
13151 Expressions => New_List (
13152 Real_Hi,
13153 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13154
13155 Real_Lo :=
13156 Make_Attribute_Reference (Loc,
13157 Prefix => New_Occurrence_Of (Ityp, Loc),
13158 Attribute_Name => Name_Max,
13159 Expressions => New_List (
13160 Real_Lo,
13161 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13162 end if;
13163
13164 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13165 end Family_Offset;
13166
13167 -----------------
13168 -- Family_Size --
13169 -----------------
13170
13171 function Family_Size
13172 (Loc : Source_Ptr;
13173 Hi : Node_Id;
13174 Lo : Node_Id;
13175 Ttyp : Entity_Id;
13176 Cap : Boolean) return Node_Id
13177 is
13178 Ityp : Entity_Id;
13179
13180 begin
13181 if Is_Task_Type (Ttyp) then
13182 Ityp := RTE (RE_Task_Entry_Index);
13183 else
13184 Ityp := RTE (RE_Protected_Entry_Index);
13185 end if;
13186
13187 return
13188 Make_Attribute_Reference (Loc,
13189 Prefix => New_Occurrence_Of (Ityp, Loc),
13190 Attribute_Name => Name_Max,
13191 Expressions => New_List (
13192 Make_Op_Add (Loc,
13193 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13194 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13195 Make_Integer_Literal (Loc, 0)));
13196 end Family_Size;
13197
13198 ----------------------------
13199 -- Find_Enclosing_Context --
13200 ----------------------------
13201
13202 procedure Find_Enclosing_Context
13203 (N : Node_Id;
13204 Context : out Node_Id;
13205 Context_Id : out Entity_Id;
13206 Context_Decls : out List_Id)
13207 is
13208 begin
13209 -- Traverse the parent chain looking for an enclosing body, block,
13210 -- package or return statement.
13211
13212 Context := Parent (N);
13213 while not Nkind_In (Context, N_Block_Statement,
13214 N_Entry_Body,
13215 N_Extended_Return_Statement,
13216 N_Package_Body,
13217 N_Package_Declaration,
13218 N_Subprogram_Body,
13219 N_Task_Body)
13220 loop
13221 Context := Parent (Context);
13222 end loop;
13223
13224 -- Extract the constituents of the context
13225
13226 if Nkind (Context) = N_Extended_Return_Statement then
13227 Context_Decls := Return_Object_Declarations (Context);
13228 Context_Id := Return_Statement_Entity (Context);
13229
13230 -- Package declarations and bodies use a common library-level activation
13231 -- chain or task master, therefore return the package declaration as the
13232 -- proper carrier for the appropriate flag.
13233
13234 elsif Nkind (Context) = N_Package_Body then
13235 Context_Decls := Declarations (Context);
13236 Context_Id := Corresponding_Spec (Context);
13237 Context := Parent (Context_Id);
13238
13239 if Nkind (Context) = N_Defining_Program_Unit_Name then
13240 Context := Parent (Parent (Context));
13241 else
13242 Context := Parent (Context);
13243 end if;
13244
13245 elsif Nkind (Context) = N_Package_Declaration then
13246 Context_Decls := Visible_Declarations (Specification (Context));
13247 Context_Id := Defining_Unit_Name (Specification (Context));
13248
13249 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13250 Context_Id := Defining_Identifier (Context_Id);
13251 end if;
13252
13253 else
13254 Context_Decls := Declarations (Context);
13255
13256 if Nkind (Context) = N_Block_Statement then
13257 Context_Id := Entity (Identifier (Context));
13258
13259 elsif Nkind (Context) = N_Entry_Body then
13260 Context_Id := Defining_Identifier (Context);
13261
13262 elsif Nkind (Context) = N_Subprogram_Body then
13263 if Present (Corresponding_Spec (Context)) then
13264 Context_Id := Corresponding_Spec (Context);
13265 else
13266 Context_Id := Defining_Unit_Name (Specification (Context));
13267
13268 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13269 Context_Id := Defining_Identifier (Context_Id);
13270 end if;
13271 end if;
13272
13273 elsif Nkind (Context) = N_Task_Body then
13274 Context_Id := Corresponding_Spec (Context);
13275
13276 else
13277 raise Program_Error;
13278 end if;
13279 end if;
13280
13281 pragma Assert (Present (Context));
13282 pragma Assert (Present (Context_Id));
13283 pragma Assert (Present (Context_Decls));
13284 end Find_Enclosing_Context;
13285
13286 -----------------------
13287 -- Find_Master_Scope --
13288 -----------------------
13289
13290 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13291 S : Entity_Id;
13292
13293 begin
13294 -- In Ada 2005, the master is the innermost enclosing scope that is not
13295 -- transient. If the enclosing block is the rewriting of a call or the
13296 -- scope is an extended return statement this is valid master. The
13297 -- master in an extended return is only used within the return, and is
13298 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13299 -- now before that overwriting occurs.
13300
13301 S := Scope (E);
13302
13303 if Ada_Version >= Ada_2005 then
13304 while Is_Internal (S) loop
13305 if Nkind (Parent (S)) = N_Block_Statement
13306 and then
13307 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13308 then
13309 exit;
13310
13311 elsif Ekind (S) = E_Return_Statement then
13312 exit;
13313
13314 else
13315 S := Scope (S);
13316 end if;
13317 end loop;
13318 end if;
13319
13320 return S;
13321 end Find_Master_Scope;
13322
13323 -------------------------------
13324 -- First_Protected_Operation --
13325 -------------------------------
13326
13327 function First_Protected_Operation (D : List_Id) return Node_Id is
13328 First_Op : Node_Id;
13329
13330 begin
13331 First_Op := First (D);
13332 while Present (First_Op)
13333 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13334 loop
13335 Next (First_Op);
13336 end loop;
13337
13338 return First_Op;
13339 end First_Protected_Operation;
13340
13341 ---------------------------------------
13342 -- Install_Private_Data_Declarations --
13343 ---------------------------------------
13344
13345 procedure Install_Private_Data_Declarations
13346 (Loc : Source_Ptr;
13347 Spec_Id : Entity_Id;
13348 Conc_Typ : Entity_Id;
13349 Body_Nod : Node_Id;
13350 Decls : List_Id;
13351 Barrier : Boolean := False;
13352 Family : Boolean := False)
13353 is
13354 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13355 Decl : Node_Id;
13356 Def : Node_Id;
13357 Insert_Node : Node_Id := Empty;
13358 Obj_Ent : Entity_Id;
13359
13360 procedure Add (Decl : Node_Id);
13361 -- Add a single declaration after Insert_Node. If this is the first
13362 -- addition, Decl is added to the front of Decls and it becomes the
13363 -- insertion node.
13364
13365 function Replace_Bound (Bound : Node_Id) return Node_Id;
13366 -- The bounds of an entry index may depend on discriminants, create a
13367 -- reference to the corresponding prival. Otherwise return a duplicate
13368 -- of the original bound.
13369
13370 ---------
13371 -- Add --
13372 ---------
13373
13374 procedure Add (Decl : Node_Id) is
13375 begin
13376 if No (Insert_Node) then
13377 Prepend_To (Decls, Decl);
13378 else
13379 Insert_After (Insert_Node, Decl);
13380 end if;
13381
13382 Insert_Node := Decl;
13383 end Add;
13384
13385 --------------------------
13386 -- Replace_Discriminant --
13387 --------------------------
13388
13389 function Replace_Bound (Bound : Node_Id) return Node_Id is
13390 begin
13391 if Nkind (Bound) = N_Identifier
13392 and then Is_Discriminal (Entity (Bound))
13393 then
13394 return Make_Identifier (Loc, Chars (Entity (Bound)));
13395 else
13396 return Duplicate_Subexpr (Bound);
13397 end if;
13398 end Replace_Bound;
13399
13400 -- Start of processing for Install_Private_Data_Declarations
13401
13402 begin
13403 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13404 -- formal parameter _O, _object or _task depending on the context.
13405
13406 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13407
13408 -- Special processing of _O for barrier functions, protected entries
13409 -- and families.
13410
13411 if Barrier
13412 or else
13413 (Is_Protected
13414 and then
13415 (Ekind (Spec_Id) = E_Entry
13416 or else Ekind (Spec_Id) = E_Entry_Family))
13417 then
13418 declare
13419 Conc_Rec : constant Entity_Id :=
13420 Corresponding_Record_Type (Conc_Typ);
13421 Typ_Id : constant Entity_Id :=
13422 Make_Defining_Identifier (Loc,
13423 New_External_Name (Chars (Conc_Rec), 'P'));
13424 begin
13425 -- Generate:
13426 -- type prot_typVP is access prot_typV;
13427
13428 Decl :=
13429 Make_Full_Type_Declaration (Loc,
13430 Defining_Identifier => Typ_Id,
13431 Type_Definition =>
13432 Make_Access_To_Object_Definition (Loc,
13433 Subtype_Indication =>
13434 New_Occurrence_Of (Conc_Rec, Loc)));
13435 Add (Decl);
13436
13437 -- Generate:
13438 -- _object : prot_typVP := prot_typV (_O);
13439
13440 Decl :=
13441 Make_Object_Declaration (Loc,
13442 Defining_Identifier =>
13443 Make_Defining_Identifier (Loc, Name_uObject),
13444 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13445 Expression =>
13446 Unchecked_Convert_To (Typ_Id,
13447 New_Occurrence_Of (Obj_Ent, Loc)));
13448 Add (Decl);
13449
13450 -- Set the reference to the concurrent object
13451
13452 Obj_Ent := Defining_Identifier (Decl);
13453 end;
13454 end if;
13455
13456 -- Step 2: Create the Protection object and build its declaration for
13457 -- any protected entry (family) of subprogram. Note for the lock-free
13458 -- implementation, the Protection object is not needed anymore.
13459
13460 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13461 declare
13462 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13463 Prot_Typ : RE_Id;
13464
13465 begin
13466 Set_Protection_Object (Spec_Id, Prot_Ent);
13467
13468 -- Determine the proper protection type
13469
13470 if Has_Attach_Handler (Conc_Typ)
13471 and then not Restricted_Profile
13472 then
13473 Prot_Typ := RE_Static_Interrupt_Protection;
13474
13475 elsif Has_Interrupt_Handler (Conc_Typ)
13476 and then not Restriction_Active (No_Dynamic_Attachment)
13477 then
13478 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13479
13480 else
13481 case Corresponding_Runtime_Package (Conc_Typ) is
13482 when System_Tasking_Protected_Objects_Entries =>
13483 Prot_Typ := RE_Protection_Entries;
13484
13485 when System_Tasking_Protected_Objects_Single_Entry =>
13486 Prot_Typ := RE_Protection_Entry;
13487
13488 when System_Tasking_Protected_Objects =>
13489 Prot_Typ := RE_Protection;
13490
13491 when others =>
13492 raise Program_Error;
13493 end case;
13494 end if;
13495
13496 -- Generate:
13497 -- conc_typR : protection_typ renames _object._object;
13498
13499 Decl :=
13500 Make_Object_Renaming_Declaration (Loc,
13501 Defining_Identifier => Prot_Ent,
13502 Subtype_Mark =>
13503 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13504 Name =>
13505 Make_Selected_Component (Loc,
13506 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13507 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13508 Add (Decl);
13509 end;
13510 end if;
13511
13512 -- Step 3: Add discriminant renamings (if any)
13513
13514 if Has_Discriminants (Conc_Typ) then
13515 declare
13516 D : Entity_Id;
13517
13518 begin
13519 D := First_Discriminant (Conc_Typ);
13520 while Present (D) loop
13521
13522 -- Adjust the source location
13523
13524 Set_Sloc (Discriminal (D), Loc);
13525
13526 -- Generate:
13527 -- discr_name : discr_typ renames _object.discr_name;
13528 -- or
13529 -- discr_name : discr_typ renames _task.discr_name;
13530
13531 Decl :=
13532 Make_Object_Renaming_Declaration (Loc,
13533 Defining_Identifier => Discriminal (D),
13534 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13535 Name =>
13536 Make_Selected_Component (Loc,
13537 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13538 Selector_Name => Make_Identifier (Loc, Chars (D))));
13539 Add (Decl);
13540
13541 Next_Discriminant (D);
13542 end loop;
13543 end;
13544 end if;
13545
13546 -- Step 4: Add private component renamings (if any)
13547
13548 if Is_Protected then
13549 Def := Protected_Definition (Parent (Conc_Typ));
13550
13551 if Present (Private_Declarations (Def)) then
13552 declare
13553 Comp : Node_Id;
13554 Comp_Id : Entity_Id;
13555 Decl_Id : Entity_Id;
13556
13557 begin
13558 Comp := First (Private_Declarations (Def));
13559 while Present (Comp) loop
13560 if Nkind (Comp) = N_Component_Declaration then
13561 Comp_Id := Defining_Identifier (Comp);
13562 Decl_Id :=
13563 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13564
13565 -- Minimal decoration
13566
13567 if Ekind (Spec_Id) = E_Function then
13568 Set_Ekind (Decl_Id, E_Constant);
13569 else
13570 Set_Ekind (Decl_Id, E_Variable);
13571 end if;
13572
13573 Set_Prival (Comp_Id, Decl_Id);
13574 Set_Prival_Link (Decl_Id, Comp_Id);
13575 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13576
13577 -- Generate:
13578 -- comp_name : comp_typ renames _object.comp_name;
13579
13580 Decl :=
13581 Make_Object_Renaming_Declaration (Loc,
13582 Defining_Identifier => Decl_Id,
13583 Subtype_Mark =>
13584 New_Occurrence_Of (Etype (Comp_Id), Loc),
13585 Name =>
13586 Make_Selected_Component (Loc,
13587 Prefix =>
13588 New_Occurrence_Of (Obj_Ent, Loc),
13589 Selector_Name =>
13590 Make_Identifier (Loc, Chars (Comp_Id))));
13591 Add (Decl);
13592 end if;
13593
13594 Next (Comp);
13595 end loop;
13596 end;
13597 end if;
13598 end if;
13599
13600 -- Step 5: Add the declaration of the entry index and the associated
13601 -- type for barrier functions and entry families.
13602
13603 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13604 declare
13605 E : constant Entity_Id := Index_Object (Spec_Id);
13606 Index : constant Entity_Id :=
13607 Defining_Identifier
13608 (Entry_Index_Specification
13609 (Entry_Body_Formal_Part (Body_Nod)));
13610 Index_Con : constant Entity_Id :=
13611 Make_Defining_Identifier (Loc, Chars (Index));
13612 High : Node_Id;
13613 Index_Typ : Entity_Id;
13614 Low : Node_Id;
13615
13616 begin
13617 -- Minimal decoration
13618
13619 Set_Ekind (Index_Con, E_Constant);
13620 Set_Entry_Index_Constant (Index, Index_Con);
13621 Set_Discriminal_Link (Index_Con, Index);
13622
13623 -- Retrieve the bounds of the entry family
13624
13625 High := Type_High_Bound (Etype (Index));
13626 Low := Type_Low_Bound (Etype (Index));
13627
13628 -- In the simple case the entry family is given by a subtype
13629 -- mark and the index constant has the same type.
13630
13631 if Is_Entity_Name (Original_Node (
13632 Discrete_Subtype_Definition (Parent (Index))))
13633 then
13634 Index_Typ := Etype (Index);
13635
13636 -- Otherwise a new subtype declaration is required
13637
13638 else
13639 High := Replace_Bound (High);
13640 Low := Replace_Bound (Low);
13641
13642 Index_Typ := Make_Temporary (Loc, 'J');
13643
13644 -- Generate:
13645 -- subtype Jnn is <Etype of Index> range Low .. High;
13646
13647 Decl :=
13648 Make_Subtype_Declaration (Loc,
13649 Defining_Identifier => Index_Typ,
13650 Subtype_Indication =>
13651 Make_Subtype_Indication (Loc,
13652 Subtype_Mark =>
13653 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13654 Constraint =>
13655 Make_Range_Constraint (Loc,
13656 Range_Expression =>
13657 Make_Range (Loc, Low, High))));
13658 Add (Decl);
13659 end if;
13660
13661 Set_Etype (Index_Con, Index_Typ);
13662
13663 -- Create the object which designates the index:
13664 -- J : constant Jnn :=
13665 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13666 --
13667 -- where Jnn is the subtype created above or the original type of
13668 -- the index, _E is a formal of the protected body subprogram and
13669 -- <index expr> is the index of the first family member.
13670
13671 Decl :=
13672 Make_Object_Declaration (Loc,
13673 Defining_Identifier => Index_Con,
13674 Constant_Present => True,
13675 Object_Definition =>
13676 New_Occurrence_Of (Index_Typ, Loc),
13677
13678 Expression =>
13679 Make_Attribute_Reference (Loc,
13680 Prefix =>
13681 New_Occurrence_Of (Index_Typ, Loc),
13682 Attribute_Name => Name_Val,
13683
13684 Expressions => New_List (
13685
13686 Make_Op_Add (Loc,
13687 Left_Opnd =>
13688 Make_Op_Subtract (Loc,
13689 Left_Opnd => New_Occurrence_Of (E, Loc),
13690 Right_Opnd =>
13691 Entry_Index_Expression (Loc,
13692 Defining_Identifier (Body_Nod),
13693 Empty, Conc_Typ)),
13694
13695 Right_Opnd =>
13696 Make_Attribute_Reference (Loc,
13697 Prefix =>
13698 New_Occurrence_Of (Index_Typ, Loc),
13699 Attribute_Name => Name_Pos,
13700 Expressions => New_List (
13701 Make_Attribute_Reference (Loc,
13702 Prefix =>
13703 New_Occurrence_Of (Index_Typ, Loc),
13704 Attribute_Name => Name_First)))))));
13705 Add (Decl);
13706 end;
13707 end if;
13708 end Install_Private_Data_Declarations;
13709
13710 -----------------------
13711 -- Is_Exception_Safe --
13712 -----------------------
13713
13714 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13715
13716 function Has_Side_Effect (N : Node_Id) return Boolean;
13717 -- Return True whenever encountering a subprogram call or raise
13718 -- statement of any kind in the sequence of statements
13719
13720 ---------------------
13721 -- Has_Side_Effect --
13722 ---------------------
13723
13724 -- What is this doing buried two levels down in exp_ch9. It seems like a
13725 -- generally useful function, and indeed there may be code duplication
13726 -- going on here ???
13727
13728 function Has_Side_Effect (N : Node_Id) return Boolean is
13729 Stmt : Node_Id;
13730 Expr : Node_Id;
13731
13732 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13733 -- Indicate whether N is a subprogram call or a raise statement
13734
13735 ----------------------
13736 -- Is_Call_Or_Raise --
13737 ----------------------
13738
13739 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13740 begin
13741 return Nkind_In (N, N_Procedure_Call_Statement,
13742 N_Function_Call,
13743 N_Raise_Statement,
13744 N_Raise_Constraint_Error,
13745 N_Raise_Program_Error,
13746 N_Raise_Storage_Error);
13747 end Is_Call_Or_Raise;
13748
13749 -- Start of processing for Has_Side_Effect
13750
13751 begin
13752 Stmt := N;
13753 while Present (Stmt) loop
13754 if Is_Call_Or_Raise (Stmt) then
13755 return True;
13756 end if;
13757
13758 -- An object declaration can also contain a function call or a
13759 -- raise statement.
13760
13761 if Nkind (Stmt) = N_Object_Declaration then
13762 Expr := Expression (Stmt);
13763
13764 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13765 return True;
13766 end if;
13767 end if;
13768
13769 Next (Stmt);
13770 end loop;
13771
13772 return False;
13773 end Has_Side_Effect;
13774
13775 -- Start of processing for Is_Exception_Safe
13776
13777 begin
13778 -- When exceptions can't be propagated, the subprogram returns normally
13779
13780 if No_Exception_Handlers_Set then
13781 return True;
13782 end if;
13783
13784 -- If the checks handled by the back end are not disabled, we cannot
13785 -- ensure that no exception will be raised.
13786
13787 if not Access_Checks_Suppressed (Empty)
13788 or else not Discriminant_Checks_Suppressed (Empty)
13789 or else not Range_Checks_Suppressed (Empty)
13790 or else not Index_Checks_Suppressed (Empty)
13791 or else Opt.Stack_Checking_Enabled
13792 then
13793 return False;
13794 end if;
13795
13796 if Has_Side_Effect (First (Declarations (Subprogram)))
13797 or else
13798 Has_Side_Effect
13799 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13800 then
13801 return False;
13802 else
13803 return True;
13804 end if;
13805 end Is_Exception_Safe;
13806
13807 ---------------------------------
13808 -- Is_Potentially_Large_Family --
13809 ---------------------------------
13810
13811 function Is_Potentially_Large_Family
13812 (Base_Index : Entity_Id;
13813 Conctyp : Entity_Id;
13814 Lo : Node_Id;
13815 Hi : Node_Id) return Boolean
13816 is
13817 begin
13818 return Scope (Base_Index) = Standard_Standard
13819 and then Base_Index = Base_Type (Standard_Integer)
13820 and then Has_Discriminants (Conctyp)
13821 and then
13822 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13823 and then
13824 (Denotes_Discriminant (Lo, True)
13825 or else
13826 Denotes_Discriminant (Hi, True));
13827 end Is_Potentially_Large_Family;
13828
13829 -------------------------------------
13830 -- Is_Private_Primitive_Subprogram --
13831 -------------------------------------
13832
13833 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13834 begin
13835 return
13836 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13837 and then Is_Private_Primitive (Id);
13838 end Is_Private_Primitive_Subprogram;
13839
13840 ------------------
13841 -- Index_Object --
13842 ------------------
13843
13844 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13845 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13846 Formal : Entity_Id;
13847
13848 begin
13849 Formal := First_Formal (Bod_Subp);
13850 while Present (Formal) loop
13851
13852 -- Look for formal parameter _E
13853
13854 if Chars (Formal) = Name_uE then
13855 return Formal;
13856 end if;
13857
13858 Next_Formal (Formal);
13859 end loop;
13860
13861 -- A protected body subprogram should always have the parameter in
13862 -- question.
13863
13864 raise Program_Error;
13865 end Index_Object;
13866
13867 --------------------------------
13868 -- Make_Initialize_Protection --
13869 --------------------------------
13870
13871 function Make_Initialize_Protection
13872 (Protect_Rec : Entity_Id) return List_Id
13873 is
13874 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13875 P_Arr : Entity_Id;
13876 Pdec : Node_Id;
13877 Ptyp : constant Node_Id :=
13878 Corresponding_Concurrent_Type (Protect_Rec);
13879 Args : List_Id;
13880 L : constant List_Id := New_List;
13881 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13882 Prio_Type : Entity_Id;
13883 Prio_Var : Entity_Id := Empty;
13884 Restricted : constant Boolean := Restricted_Profile;
13885
13886 begin
13887 -- We may need two calls to properly initialize the object, one to
13888 -- Initialize_Protection, and possibly one to Install_Handlers if we
13889 -- have a pragma Attach_Handler.
13890
13891 -- Get protected declaration. In the case of a task type declaration,
13892 -- this is simply the parent of the protected type entity. In the single
13893 -- protected object declaration, this parent will be the implicit type,
13894 -- and we can find the corresponding single protected object declaration
13895 -- by searching forward in the declaration list in the tree.
13896
13897 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13898 -- of this type should have been removed during semantic analysis.
13899
13900 Pdec := Parent (Ptyp);
13901 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13902 N_Single_Protected_Declaration)
13903 loop
13904 Next (Pdec);
13905 end loop;
13906
13907 -- Build the parameter list for the call. Note that _Init is the name
13908 -- of the formal for the object to be initialized, which is the task
13909 -- value record itself.
13910
13911 Args := New_List;
13912
13913 -- For lock-free implementation, skip initializations of the Protection
13914 -- object.
13915
13916 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13917
13918 -- Object parameter. This is a pointer to the object of type
13919 -- Protection used by the GNARL to control the protected object.
13920
13921 Append_To (Args,
13922 Make_Attribute_Reference (Loc,
13923 Prefix =>
13924 Make_Selected_Component (Loc,
13925 Prefix => Make_Identifier (Loc, Name_uInit),
13926 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13927 Attribute_Name => Name_Unchecked_Access));
13928
13929 -- Priority parameter. Set to Unspecified_Priority unless there is a
13930 -- Priority rep item, in which case we take the value from the pragma
13931 -- or attribute definition clause, or there is an Interrupt_Priority
13932 -- rep item and no Priority rep item, and we set the ceiling to
13933 -- Interrupt_Priority'Last, an implementation-defined value, see
13934 -- (RM D.3(10)).
13935
13936 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13937 declare
13938 Prio_Clause : constant Node_Id :=
13939 Get_Rep_Item
13940 (Ptyp, Name_Priority, Check_Parents => False);
13941
13942 Prio : Node_Id;
13943
13944 begin
13945 -- Pragma Priority
13946
13947 if Nkind (Prio_Clause) = N_Pragma then
13948 Prio :=
13949 Expression
13950 (First (Pragma_Argument_Associations (Prio_Clause)));
13951
13952 -- Get_Rep_Item returns either priority pragma.
13953
13954 if Pragma_Name (Prio_Clause) = Name_Priority then
13955 Prio_Type := RTE (RE_Any_Priority);
13956 else
13957 Prio_Type := RTE (RE_Interrupt_Priority);
13958 end if;
13959
13960 -- Attribute definition clause Priority
13961
13962 else
13963 if Chars (Prio_Clause) = Name_Priority then
13964 Prio_Type := RTE (RE_Any_Priority);
13965 else
13966 Prio_Type := RTE (RE_Interrupt_Priority);
13967 end if;
13968
13969 Prio := Expression (Prio_Clause);
13970 end if;
13971
13972 -- Always create a locale variable to capture the priority.
13973 -- The priority is also passed to Install_Restriced_Handlers.
13974 -- Note that it is really necessary to create this variable
13975 -- explicitly. It might be thought that removing side effects
13976 -- would the appropriate approach, but that could generate
13977 -- declarations improperly placed in the enclosing scope.
13978
13979 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13980 Append_To (L,
13981 Make_Object_Declaration (Loc,
13982 Defining_Identifier => Prio_Var,
13983 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13984 Expression => Relocate_Node (Prio)));
13985
13986 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13987 end;
13988
13989 -- When no priority is specified but an xx_Handler pragma is, we
13990 -- default to System.Interrupts.Default_Interrupt_Priority, see
13991 -- D.3(10).
13992
13993 elsif Has_Attach_Handler (Ptyp)
13994 or else Has_Interrupt_Handler (Ptyp)
13995 then
13996 Append_To (Args,
13997 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13998
13999 -- Normal case, no priority or xx_Handler specified, default priority
14000
14001 else
14002 Append_To (Args,
14003 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14004 end if;
14005
14006 -- Test for Compiler_Info parameter. This parameter allows entry body
14007 -- procedures and barrier functions to be called from the runtime. It
14008 -- is a pointer to the record generated by the compiler to represent
14009 -- the protected object.
14010
14011 -- A protected type without entries that covers an interface and
14012 -- overrides the abstract routines with protected procedures is
14013 -- considered equivalent to a protected type with entries in the
14014 -- context of dispatching select statements.
14015
14016 -- Protected types with interrupt handlers (when not using a
14017 -- restricted profile) are also considered equivalent to protected
14018 -- types with entries.
14019
14020 -- The types which are used (Static_Interrupt_Protection and
14021 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
14022
14023 declare
14024 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
14025
14026 Called_Subp : RE_Id;
14027
14028 begin
14029 case Pkg_Id is
14030 when System_Tasking_Protected_Objects_Entries =>
14031 Called_Subp := RE_Initialize_Protection_Entries;
14032
14033 -- Argument Compiler_Info
14034
14035 Append_To (Args,
14036 Make_Attribute_Reference (Loc,
14037 Prefix => Make_Identifier (Loc, Name_uInit),
14038 Attribute_Name => Name_Address));
14039
14040 when System_Tasking_Protected_Objects_Single_Entry =>
14041 Called_Subp := RE_Initialize_Protection_Entry;
14042
14043 -- Argument Compiler_Info
14044
14045 Append_To (Args,
14046 Make_Attribute_Reference (Loc,
14047 Prefix => Make_Identifier (Loc, Name_uInit),
14048 Attribute_Name => Name_Address));
14049
14050 when System_Tasking_Protected_Objects =>
14051 Called_Subp := RE_Initialize_Protection;
14052
14053 when others =>
14054 raise Program_Error;
14055 end case;
14056
14057 -- Entry_Bodies parameter. This is a pointer to an array of
14058 -- pointers to the entry body procedures and barrier functions of
14059 -- the object. If the protected type has no entries this object
14060 -- will not exist, in this case, pass a null (it can happen when
14061 -- there are protected interrupt handlers or interfaces).
14062
14063 if Has_Entry then
14064 P_Arr := Entry_Bodies_Array (Ptyp);
14065
14066 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14067 -- multiple entries).
14068
14069 Append_To (Args,
14070 Make_Attribute_Reference (Loc,
14071 Prefix => New_Occurrence_Of (P_Arr, Loc),
14072 Attribute_Name => Name_Unrestricted_Access));
14073
14074 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14075
14076 -- Find index mapping function (clumsy but ok for now)
14077
14078 while Ekind (P_Arr) /= E_Function loop
14079 Next_Entity (P_Arr);
14080 end loop;
14081
14082 Append_To (Args,
14083 Make_Attribute_Reference (Loc,
14084 Prefix => New_Occurrence_Of (P_Arr, Loc),
14085 Attribute_Name => Name_Unrestricted_Access));
14086 end if;
14087
14088 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14089
14090 -- This is the case where we have a protected object with
14091 -- interfaces and no entries, and the single entry restriction
14092 -- is in effect. We pass a null pointer for the entry
14093 -- parameter because there is no actual entry.
14094
14095 Append_To (Args, Make_Null (Loc));
14096
14097 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14098
14099 -- This is the case where we have a protected object with no
14100 -- entries and:
14101 -- - either interrupt handlers with non restricted profile,
14102 -- - or interfaces
14103 -- Note that the types which are used for interrupt handlers
14104 -- (Static/Dynamic_Interrupt_Protection) are derived from
14105 -- Protection_Entries. We pass two null pointers because there
14106 -- is no actual entry, and the initialization procedure needs
14107 -- both Entry_Bodies and Find_Body_Index.
14108
14109 Append_To (Args, Make_Null (Loc));
14110 Append_To (Args, Make_Null (Loc));
14111 end if;
14112
14113 Append_To (L,
14114 Make_Procedure_Call_Statement (Loc,
14115 Name =>
14116 New_Occurrence_Of (RTE (Called_Subp), Loc),
14117 Parameter_Associations => Args));
14118 end;
14119 end if;
14120
14121 if Has_Attach_Handler (Ptyp) then
14122
14123 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14124 -- make the following call:
14125
14126 -- Install_Handlers (_object,
14127 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14128
14129 -- or, in the case of Ravenscar:
14130
14131 -- Install_Restricted_Handlers
14132 -- (Prio, (Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14133
14134 declare
14135 Args : constant List_Id := New_List;
14136 Table : constant List_Id := New_List;
14137 Ritem : Node_Id := First_Rep_Item (Ptyp);
14138
14139 begin
14140 -- Build the Priority parameter (only for ravenscar)
14141
14142 if Restricted then
14143
14144 -- Priority comes from a pragma
14145
14146 if Present (Prio_Var) then
14147 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14148
14149 -- Priority is the default one
14150
14151 else
14152 Append_To (Args,
14153 New_Occurrence_Of
14154 (RTE (RE_Default_Interrupt_Priority), Loc));
14155 end if;
14156 end if;
14157
14158 -- Build the Attach_Handler table argument
14159
14160 while Present (Ritem) loop
14161 if Nkind (Ritem) = N_Pragma
14162 and then Pragma_Name (Ritem) = Name_Attach_Handler
14163 then
14164 declare
14165 Handler : constant Node_Id :=
14166 First (Pragma_Argument_Associations (Ritem));
14167
14168 Interrupt : constant Node_Id := Next (Handler);
14169 Expr : constant Node_Id := Expression (Interrupt);
14170
14171 begin
14172 Append_To (Table,
14173 Make_Aggregate (Loc, Expressions => New_List (
14174 Unchecked_Convert_To
14175 (RTE (RE_System_Interrupt_Id), Expr),
14176 Make_Attribute_Reference (Loc,
14177 Prefix =>
14178 Make_Selected_Component (Loc,
14179 Prefix =>
14180 Make_Identifier (Loc, Name_uInit),
14181 Selector_Name =>
14182 Duplicate_Subexpr_No_Checks
14183 (Expression (Handler))),
14184 Attribute_Name => Name_Access))));
14185 end;
14186 end if;
14187
14188 Next_Rep_Item (Ritem);
14189 end loop;
14190
14191 -- Append the table argument we just built
14192
14193 Append_To (Args, Make_Aggregate (Loc, Table));
14194
14195 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14196 -- call to the statements.
14197
14198 if Restricted then
14199 -- Call a simplified version of Install_Handlers to be used
14200 -- when the Ravenscar restrictions are in effect
14201 -- (Install_Restricted_Handlers).
14202
14203 Append_To (L,
14204 Make_Procedure_Call_Statement (Loc,
14205 Name =>
14206 New_Occurrence_Of
14207 (RTE (RE_Install_Restricted_Handlers), Loc),
14208 Parameter_Associations => Args));
14209
14210 else
14211 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14212
14213 -- First, prepends the _object argument
14214
14215 Prepend_To (Args,
14216 Make_Attribute_Reference (Loc,
14217 Prefix =>
14218 Make_Selected_Component (Loc,
14219 Prefix => Make_Identifier (Loc, Name_uInit),
14220 Selector_Name =>
14221 Make_Identifier (Loc, Name_uObject)),
14222 Attribute_Name => Name_Unchecked_Access));
14223 end if;
14224
14225 -- Then, insert call to Install_Handlers
14226
14227 Append_To (L,
14228 Make_Procedure_Call_Statement (Loc,
14229 Name =>
14230 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14231 Parameter_Associations => Args));
14232 end if;
14233 end;
14234 end if;
14235
14236 return L;
14237 end Make_Initialize_Protection;
14238
14239 ---------------------------
14240 -- Make_Task_Create_Call --
14241 ---------------------------
14242
14243 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14244 Loc : constant Source_Ptr := Sloc (Task_Rec);
14245 Args : List_Id;
14246 Ecount : Node_Id;
14247 Name : Node_Id;
14248 Tdec : Node_Id;
14249 Tdef : Node_Id;
14250 Tnam : Name_Id;
14251 Ttyp : Node_Id;
14252
14253 begin
14254 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14255 Tnam := Chars (Ttyp);
14256
14257 -- Get task declaration. In the case of a task type declaration, this is
14258 -- simply the parent of the task type entity. In the single task
14259 -- declaration, this parent will be the implicit type, and we can find
14260 -- the corresponding single task declaration by searching forward in the
14261 -- declaration list in the tree.
14262
14263 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14264 -- this type should have been removed during semantic analysis.
14265
14266 Tdec := Parent (Ttyp);
14267 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14268 N_Single_Task_Declaration)
14269 loop
14270 Next (Tdec);
14271 end loop;
14272
14273 -- Now we can find the task definition from this declaration
14274
14275 Tdef := Task_Definition (Tdec);
14276
14277 -- Build the parameter list for the call. Note that _Init is the name
14278 -- of the formal for the object to be initialized, which is the task
14279 -- value record itself.
14280
14281 Args := New_List;
14282
14283 -- Priority parameter. Set to Unspecified_Priority unless there is a
14284 -- Priority rep item, in which case we take the value from the rep item.
14285
14286 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14287 Append_To (Args,
14288 Make_Selected_Component (Loc,
14289 Prefix => Make_Identifier (Loc, Name_uInit),
14290 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14291 else
14292 Append_To (Args,
14293 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14294 end if;
14295
14296 -- Optional Stack parameter
14297
14298 if Restricted_Profile then
14299
14300 -- If the stack has been preallocated by the expander then
14301 -- pass its address. Otherwise, pass a null address.
14302
14303 if Preallocated_Stacks_On_Target then
14304 Append_To (Args,
14305 Make_Attribute_Reference (Loc,
14306 Prefix =>
14307 Make_Selected_Component (Loc,
14308 Prefix => Make_Identifier (Loc, Name_uInit),
14309 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14310 Attribute_Name => Name_Address));
14311
14312 else
14313 Append_To (Args,
14314 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14315 end if;
14316 end if;
14317
14318 -- Size parameter. If no Storage_Size pragma is present, then
14319 -- the size is taken from the taskZ variable for the type, which
14320 -- is either Unspecified_Size, or has been reset by the use of
14321 -- a Storage_Size attribute definition clause. If a pragma is
14322 -- present, then the size is taken from the _Size field of the
14323 -- task value record, which was set from the pragma value.
14324
14325 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14326 Append_To (Args,
14327 Make_Selected_Component (Loc,
14328 Prefix => Make_Identifier (Loc, Name_uInit),
14329 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14330
14331 else
14332 Append_To (Args,
14333 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14334 end if;
14335
14336 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14337 -- Task_Info pragma, in which case we take the value from the pragma.
14338
14339 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14340 Append_To (Args,
14341 Make_Selected_Component (Loc,
14342 Prefix => Make_Identifier (Loc, Name_uInit),
14343 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14344
14345 else
14346 Append_To (Args,
14347 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14348 end if;
14349
14350 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14351 -- in which case we take the value from the rep item. The parameter is
14352 -- passed as an Integer because in the case of unspecified CPU the
14353 -- value is not in the range of CPU_Range.
14354
14355 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14356 Append_To (Args,
14357 Convert_To (Standard_Integer,
14358 Make_Selected_Component (Loc,
14359 Prefix => Make_Identifier (Loc, Name_uInit),
14360 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14361 else
14362 Append_To (Args,
14363 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14364 end if;
14365
14366 if not Restricted_Profile then
14367
14368 -- Deadline parameter. If no Relative_Deadline pragma is present,
14369 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14370 -- the deadline is taken from the _Relative_Deadline field of the
14371 -- task value record, which was set from the pragma value. Note that
14372 -- this parameter must not be generated for the restricted profiles
14373 -- since Ravenscar does not allow deadlines.
14374
14375 -- Case where pragma Relative_Deadline applies: use given value
14376
14377 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14378 Append_To (Args,
14379 Make_Selected_Component (Loc,
14380 Prefix => Make_Identifier (Loc, Name_uInit),
14381 Selector_Name =>
14382 Make_Identifier (Loc, Name_uRelative_Deadline)));
14383
14384 -- No pragma Relative_Deadline apply to the task
14385
14386 else
14387 Append_To (Args,
14388 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14389 end if;
14390
14391 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14392 -- present, then the dispatching domain is null. If a rep item is
14393 -- present, then the dispatching domain is taken from the
14394 -- _Dispatching_Domain field of the task value record, which was set
14395 -- from the rep item value.
14396
14397 -- Case where Dispatching_Domain rep item applies: use given value
14398
14399 if Has_Rep_Item
14400 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14401 then
14402 Append_To (Args,
14403 Make_Selected_Component (Loc,
14404 Prefix =>
14405 Make_Identifier (Loc, Name_uInit),
14406 Selector_Name =>
14407 Make_Identifier (Loc, Name_uDispatching_Domain)));
14408
14409 -- No pragma or aspect Dispatching_Domain applies to the task
14410
14411 else
14412 Append_To (Args, Make_Null (Loc));
14413 end if;
14414
14415 -- Number of entries. This is an expression of the form:
14416
14417 -- n + _Init.a'Length + _Init.a'B'Length + ...
14418
14419 -- where a,b... are the entry family names for the task definition
14420
14421 Ecount :=
14422 Build_Entry_Count_Expression
14423 (Ttyp,
14424 Component_Items
14425 (Component_List
14426 (Type_Definition
14427 (Parent (Corresponding_Record_Type (Ttyp))))),
14428 Loc);
14429 Append_To (Args, Ecount);
14430
14431 -- Master parameter. This is a reference to the _Master parameter of
14432 -- the initialization procedure, except in the case of the pragma
14433 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14434 -- System.Tasking.Library_Task_Level.
14435
14436 if Restriction_Active (No_Task_Hierarchy) = False then
14437 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14438 else
14439 Append_To (Args,
14440 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14441 end if;
14442 end if;
14443
14444 -- State parameter. This is a pointer to the task body procedure. The
14445 -- required value is obtained by taking 'Unrestricted_Access of the task
14446 -- body procedure and converting it (with an unchecked conversion) to
14447 -- the type required by the task kernel. For further details, see the
14448 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14449 -- than 'Address in order to avoid creating trampolines.
14450
14451 declare
14452 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14453 Subp_Ptr_Typ : constant Node_Id :=
14454 Create_Itype (E_Access_Subprogram_Type, Tdec);
14455 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14456
14457 begin
14458 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14459 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14460
14461 -- Be sure to freeze a reference to the access-to-subprogram type,
14462 -- otherwise gigi will complain that it's in the wrong scope, because
14463 -- it's actually inside the init procedure for the record type that
14464 -- corresponds to the task type.
14465
14466 Set_Itype (Ref, Subp_Ptr_Typ);
14467 Append_Freeze_Action (Task_Rec, Ref);
14468
14469 Append_To (Args,
14470 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14471 Make_Qualified_Expression (Loc,
14472 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14473 Expression =>
14474 Make_Attribute_Reference (Loc,
14475 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14476 Attribute_Name => Name_Unrestricted_Access))));
14477 end;
14478
14479 -- Discriminants parameter. This is just the address of the task
14480 -- value record itself (which contains the discriminant values
14481
14482 Append_To (Args,
14483 Make_Attribute_Reference (Loc,
14484 Prefix => Make_Identifier (Loc, Name_uInit),
14485 Attribute_Name => Name_Address));
14486
14487 -- Elaborated parameter. This is an access to the elaboration Boolean
14488
14489 Append_To (Args,
14490 Make_Attribute_Reference (Loc,
14491 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14492 Attribute_Name => Name_Unchecked_Access));
14493
14494 -- Add Chain parameter (not done for sequential elaboration policy, see
14495 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14496
14497 if Partition_Elaboration_Policy /= 'S' then
14498 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14499 end if;
14500
14501 -- Task name parameter. Take this from the _Task_Id parameter to the
14502 -- init call unless there is a Task_Name pragma, in which case we take
14503 -- the value from the pragma.
14504
14505 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14506 -- Copy expression in full, because it may be dynamic and have
14507 -- side effects.
14508
14509 Append_To (Args,
14510 New_Copy_Tree
14511 (Expression
14512 (First
14513 (Pragma_Argument_Associations
14514 (Get_Rep_Pragma
14515 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14516
14517 else
14518 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14519 end if;
14520
14521 -- Created_Task parameter. This is the _Task_Id field of the task
14522 -- record value
14523
14524 Append_To (Args,
14525 Make_Selected_Component (Loc,
14526 Prefix => Make_Identifier (Loc, Name_uInit),
14527 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14528
14529 declare
14530 Create_RE : RE_Id;
14531
14532 begin
14533 if Restricted_Profile then
14534 if Partition_Elaboration_Policy = 'S' then
14535 Create_RE := RE_Create_Restricted_Task_Sequential;
14536 else
14537 Create_RE := RE_Create_Restricted_Task;
14538 end if;
14539 else
14540 Create_RE := RE_Create_Task;
14541 end if;
14542
14543 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14544 end;
14545
14546 return
14547 Make_Procedure_Call_Statement (Loc,
14548 Name => Name,
14549 Parameter_Associations => Args);
14550 end Make_Task_Create_Call;
14551
14552 ------------------------------
14553 -- Next_Protected_Operation --
14554 ------------------------------
14555
14556 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14557 Next_Op : Node_Id;
14558
14559 begin
14560 -- Check whether there is a subsequent body for a protected operation
14561 -- in the current protected body. In Ada2012 that includes expression
14562 -- functions that are completions.
14563
14564 Next_Op := Next (N);
14565 while Present (Next_Op)
14566 and then not Nkind_In (Next_Op,
14567 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14568 loop
14569 Next (Next_Op);
14570 end loop;
14571
14572 return Next_Op;
14573 end Next_Protected_Operation;
14574
14575 ---------------------
14576 -- Null_Statements --
14577 ---------------------
14578
14579 function Null_Statements (Stats : List_Id) return Boolean is
14580 Stmt : Node_Id;
14581
14582 begin
14583 Stmt := First (Stats);
14584 while Nkind (Stmt) /= N_Empty
14585 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14586 or else
14587 (Nkind (Stmt) = N_Pragma
14588 and then
14589 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14590 Name_Unmodified,
14591 Name_Warnings)))
14592 loop
14593 Next (Stmt);
14594 end loop;
14595
14596 return Nkind (Stmt) = N_Empty;
14597 end Null_Statements;
14598
14599 --------------------------
14600 -- Parameter_Block_Pack --
14601 --------------------------
14602
14603 function Parameter_Block_Pack
14604 (Loc : Source_Ptr;
14605 Blk_Typ : Entity_Id;
14606 Actuals : List_Id;
14607 Formals : List_Id;
14608 Decls : List_Id;
14609 Stmts : List_Id) return Node_Id
14610 is
14611 Actual : Entity_Id;
14612 Expr : Node_Id := Empty;
14613 Formal : Entity_Id;
14614 Has_Param : Boolean := False;
14615 P : Entity_Id;
14616 Params : List_Id;
14617 Temp_Asn : Node_Id;
14618 Temp_Nam : Node_Id;
14619
14620 begin
14621 Actual := First (Actuals);
14622 Formal := Defining_Identifier (First (Formals));
14623 Params := New_List;
14624 while Present (Actual) loop
14625 if Is_By_Copy_Type (Etype (Actual)) then
14626 -- Generate:
14627 -- Jnn : aliased <formal-type>
14628
14629 Temp_Nam := Make_Temporary (Loc, 'J');
14630
14631 Append_To (Decls,
14632 Make_Object_Declaration (Loc,
14633 Aliased_Present => True,
14634 Defining_Identifier => Temp_Nam,
14635 Object_Definition =>
14636 New_Occurrence_Of (Etype (Formal), Loc)));
14637
14638 if Ekind (Formal) /= E_Out_Parameter then
14639
14640 -- Generate:
14641 -- Jnn := <actual>
14642
14643 Temp_Asn :=
14644 New_Occurrence_Of (Temp_Nam, Loc);
14645
14646 Set_Assignment_OK (Temp_Asn);
14647
14648 Append_To (Stmts,
14649 Make_Assignment_Statement (Loc,
14650 Name => Temp_Asn,
14651 Expression => New_Copy_Tree (Actual)));
14652 end if;
14653
14654 -- Generate:
14655 -- Jnn'unchecked_access
14656
14657 Append_To (Params,
14658 Make_Attribute_Reference (Loc,
14659 Attribute_Name => Name_Unchecked_Access,
14660 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14661
14662 Has_Param := True;
14663
14664 -- The controlling parameter is omitted
14665
14666 else
14667 if not Is_Controlling_Actual (Actual) then
14668 Append_To (Params,
14669 Make_Reference (Loc, New_Copy_Tree (Actual)));
14670
14671 Has_Param := True;
14672 end if;
14673 end if;
14674
14675 Next_Actual (Actual);
14676 Next_Formal_With_Extras (Formal);
14677 end loop;
14678
14679 if Has_Param then
14680 Expr := Make_Aggregate (Loc, Params);
14681 end if;
14682
14683 -- Generate:
14684 -- P : Ann := (
14685 -- J1'unchecked_access;
14686 -- <actual2>'reference;
14687 -- ...);
14688
14689 P := Make_Temporary (Loc, 'P');
14690
14691 Append_To (Decls,
14692 Make_Object_Declaration (Loc,
14693 Defining_Identifier => P,
14694 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14695 Expression => Expr));
14696
14697 return P;
14698 end Parameter_Block_Pack;
14699
14700 ----------------------------
14701 -- Parameter_Block_Unpack --
14702 ----------------------------
14703
14704 function Parameter_Block_Unpack
14705 (Loc : Source_Ptr;
14706 P : Entity_Id;
14707 Actuals : List_Id;
14708 Formals : List_Id) return List_Id
14709 is
14710 Actual : Entity_Id;
14711 Asnmt : Node_Id;
14712 Formal : Entity_Id;
14713 Has_Asnmt : Boolean := False;
14714 Result : constant List_Id := New_List;
14715
14716 begin
14717 Actual := First (Actuals);
14718 Formal := Defining_Identifier (First (Formals));
14719 while Present (Actual) loop
14720 if Is_By_Copy_Type (Etype (Actual))
14721 and then Ekind (Formal) /= E_In_Parameter
14722 then
14723 -- Generate:
14724 -- <actual> := P.<formal>;
14725
14726 Asnmt :=
14727 Make_Assignment_Statement (Loc,
14728 Name =>
14729 New_Copy (Actual),
14730 Expression =>
14731 Make_Explicit_Dereference (Loc,
14732 Make_Selected_Component (Loc,
14733 Prefix =>
14734 New_Occurrence_Of (P, Loc),
14735 Selector_Name =>
14736 Make_Identifier (Loc, Chars (Formal)))));
14737
14738 Set_Assignment_OK (Name (Asnmt));
14739 Append_To (Result, Asnmt);
14740
14741 Has_Asnmt := True;
14742 end if;
14743
14744 Next_Actual (Actual);
14745 Next_Formal_With_Extras (Formal);
14746 end loop;
14747
14748 if Has_Asnmt then
14749 return Result;
14750 else
14751 return New_List (Make_Null_Statement (Loc));
14752 end if;
14753 end Parameter_Block_Unpack;
14754
14755 ----------------------
14756 -- Set_Discriminals --
14757 ----------------------
14758
14759 procedure Set_Discriminals (Dec : Node_Id) is
14760 D : Entity_Id;
14761 Pdef : Entity_Id;
14762 D_Minal : Entity_Id;
14763
14764 begin
14765 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14766 Pdef := Defining_Identifier (Dec);
14767
14768 if Has_Discriminants (Pdef) then
14769 D := First_Discriminant (Pdef);
14770 while Present (D) loop
14771 D_Minal :=
14772 Make_Defining_Identifier (Sloc (D),
14773 Chars => New_External_Name (Chars (D), 'D'));
14774
14775 Set_Ekind (D_Minal, E_Constant);
14776 Set_Etype (D_Minal, Etype (D));
14777 Set_Scope (D_Minal, Pdef);
14778 Set_Discriminal (D, D_Minal);
14779 Set_Discriminal_Link (D_Minal, D);
14780
14781 Next_Discriminant (D);
14782 end loop;
14783 end if;
14784 end Set_Discriminals;
14785
14786 -----------------------
14787 -- Trivial_Accept_OK --
14788 -----------------------
14789
14790 function Trivial_Accept_OK return Boolean is
14791 begin
14792 case Opt.Task_Dispatching_Policy is
14793
14794 -- If we have the default task dispatching policy in effect, we can
14795 -- definitely do the optimization (one way of looking at this is to
14796 -- think of the formal definition of the default policy being allowed
14797 -- to run any task it likes after a rendezvous, so even if notionally
14798 -- a full rescheduling occurs, we can say that our dispatching policy
14799 -- (i.e. the default dispatching policy) reorders the queue to be the
14800 -- same as just before the call.
14801
14802 when ' ' =>
14803 return True;
14804
14805 -- FIFO_Within_Priorities certainly does not permit this
14806 -- optimization since the Rendezvous is a scheduling action that may
14807 -- require some other task to be run.
14808
14809 when 'F' =>
14810 return False;
14811
14812 -- For now, disallow the optimization for all other policies. This
14813 -- may be over-conservative, but it is certainly not incorrect.
14814
14815 when others =>
14816 return False;
14817
14818 end case;
14819 end Trivial_Accept_OK;
14820
14821 end Exp_Ch9;