]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch9.adb
[multiple changes]
[thirdparty/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-2016, 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 Pos := 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_Decl : Node_Id;
1110 Master_Id : Entity_Id;
1111 Master_Scope : Entity_Id;
1112 Name_Id : Node_Id;
1113 Related_Node : Node_Id;
1114 Ren_Decl : Node_Id;
1115
1116 begin
1117 -- Nothing to do if there is no task hierarchy
1118
1119 if Restriction_Active (No_Task_Hierarchy) then
1120 return;
1121 end if;
1122
1123 -- Find the declaration that created the access type, which is either a
1124 -- type declaration, or an object declaration with an access definition,
1125 -- in which case the type is anonymous.
1126
1127 if Is_Itype (Typ) then
1128 Related_Node := Associated_Node_For_Itype (Typ);
1129 else
1130 Related_Node := Parent (Typ);
1131 end if;
1132
1133 Master_Scope := Find_Master_Scope (Typ);
1134
1135 -- Nothing to do if the master scope already contains a _master entity.
1136 -- The only exception to this is the following scenario:
1137
1138 -- Source_Scope
1139 -- Transient_Scope_1
1140 -- _master
1141
1142 -- Transient_Scope_2
1143 -- use of master
1144
1145 -- In this case the source scope is marked as having the master entity
1146 -- even though the actual declaration appears inside an inner scope. If
1147 -- the second transient scope requires a _master, it cannot use the one
1148 -- already declared because the entity is not visible.
1149
1150 Name_Id := Make_Identifier (Loc, Name_uMaster);
1151 Master_Decl := Empty;
1152
1153 if not Has_Master_Entity (Master_Scope)
1154 or else No (Current_Entity_In_Scope (Name_Id))
1155 then
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 -- If the master is declared locally, add the renaming declaration
1218 -- immediately after it, to prevent access-before-elaboration in the
1219 -- back-end.
1220
1221 if Present (Master_Decl) then
1222 Insert_After (Master_Decl, Ren_Decl);
1223 Analyze (Ren_Decl);
1224
1225 else
1226 Insert_Action (Related_Node, Ren_Decl);
1227 end if;
1228
1229 Set_Master_Id (Typ, Master_Id);
1230 end Build_Class_Wide_Master;
1231
1232 ----------------------------
1233 -- Build_Contract_Wrapper --
1234 ----------------------------
1235
1236 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1237 Conc_Typ : constant Entity_Id := Scope (E);
1238 Loc : constant Source_Ptr := Sloc (E);
1239
1240 procedure Add_Discriminant_Renamings
1241 (Obj_Id : Entity_Id;
1242 Decls : List_Id);
1243 -- Add renaming declarations for all discriminants of concurrent type
1244 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1245 -- represents the concurrent object.
1246
1247 procedure Add_Matching_Formals
1248 (Formals : List_Id;
1249 Actuals : in out List_Id);
1250 -- Add formal parameters that match those of entry E to list Formals.
1251 -- The routine also adds matching actuals for the new formals to list
1252 -- Actuals.
1253
1254 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1255 -- Relocate pragma Prag to list To. The routine creates a new list if
1256 -- To does not exist.
1257
1258 --------------------------------
1259 -- Add_Discriminant_Renamings --
1260 --------------------------------
1261
1262 procedure Add_Discriminant_Renamings
1263 (Obj_Id : Entity_Id;
1264 Decls : List_Id)
1265 is
1266 Discr : Entity_Id;
1267
1268 begin
1269 -- Inspect the discriminants of the concurrent type and generate a
1270 -- renaming for each one.
1271
1272 if Has_Discriminants (Conc_Typ) then
1273 Discr := First_Discriminant (Conc_Typ);
1274 while Present (Discr) loop
1275 Prepend_To (Decls,
1276 Make_Object_Renaming_Declaration (Loc,
1277 Defining_Identifier =>
1278 Make_Defining_Identifier (Loc, Chars (Discr)),
1279 Subtype_Mark =>
1280 New_Occurrence_Of (Etype (Discr), Loc),
1281 Name =>
1282 Make_Selected_Component (Loc,
1283 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1284 Selector_Name =>
1285 Make_Identifier (Loc, Chars (Discr)))));
1286
1287 Next_Discriminant (Discr);
1288 end loop;
1289 end if;
1290 end Add_Discriminant_Renamings;
1291
1292 --------------------------
1293 -- Add_Matching_Formals --
1294 --------------------------
1295
1296 procedure Add_Matching_Formals
1297 (Formals : List_Id;
1298 Actuals : in out List_Id)
1299 is
1300 Formal : Entity_Id;
1301 New_Formal : Entity_Id;
1302
1303 begin
1304 -- Inspect the formal parameters of the entry and generate a new
1305 -- matching formal with the same name for the wrapper. A reference
1306 -- to the new formal becomes an actual in the entry call.
1307
1308 Formal := First_Formal (E);
1309 while Present (Formal) loop
1310 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1311 Append_To (Formals,
1312 Make_Parameter_Specification (Loc,
1313 Defining_Identifier => New_Formal,
1314 In_Present => In_Present (Parent (Formal)),
1315 Out_Present => Out_Present (Parent (Formal)),
1316 Parameter_Type =>
1317 New_Occurrence_Of (Etype (Formal), Loc)));
1318
1319 if No (Actuals) then
1320 Actuals := New_List;
1321 end if;
1322
1323 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1324 Next_Formal (Formal);
1325 end loop;
1326 end Add_Matching_Formals;
1327
1328 ---------------------
1329 -- Transfer_Pragma --
1330 ---------------------
1331
1332 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1333 New_Prag : Node_Id;
1334
1335 begin
1336 if No (To) then
1337 To := New_List;
1338 end if;
1339
1340 New_Prag := Relocate_Node (Prag);
1341
1342 Set_Analyzed (New_Prag, False);
1343 Append (New_Prag, To);
1344 end Transfer_Pragma;
1345
1346 -- Local variables
1347
1348 Items : constant Node_Id := Contract (E);
1349 Actuals : List_Id := No_List;
1350 Call : Node_Id;
1351 Call_Nam : Node_Id;
1352 Decls : List_Id := No_List;
1353 Formals : List_Id;
1354 Has_Pragma : Boolean := False;
1355 Index_Id : Entity_Id;
1356 Obj_Id : Entity_Id;
1357 Prag : Node_Id;
1358 Wrapper_Id : Entity_Id;
1359
1360 -- Start of processing for Build_Contract_Wrapper
1361
1362 begin
1363 -- This routine generates a specialized wrapper for a protected or task
1364 -- entry [family] which implements precondition/postcondition semantics.
1365 -- Preconditions and case guards of contract cases are checked before
1366 -- the protected action or rendezvous takes place. Postconditions and
1367 -- consequences of contract cases are checked after the protected action
1368 -- or rendezvous takes place. The structure of the generated wrapper is
1369 -- as follows:
1370
1371 -- procedure Wrapper
1372 -- (Obj_Id : Conc_Typ; -- concurrent object
1373 -- [Index : Index_Typ;] -- index of entry family
1374 -- [Formal_1 : ...; -- parameters of original entry
1375 -- Formal_N : ...])
1376 -- is
1377 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1378 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1379
1380 -- <precondition checks>
1381 -- <case guard checks>
1382
1383 -- procedure _Postconditions is
1384 -- begin
1385 -- <postcondition checks>
1386 -- <consequence checks>
1387 -- end _Postconditions;
1388
1389 -- begin
1390 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1391 -- _Postconditions;
1392 -- end Wrapper;
1393
1394 -- Create the wrapper only when the entry has at least one executable
1395 -- contract item such as contract cases, precondition or postcondition.
1396
1397 if Present (Items) then
1398
1399 -- Inspect the list of pre/postconditions and transfer all available
1400 -- pragmas to the declarative list of the wrapper.
1401
1402 Prag := Pre_Post_Conditions (Items);
1403 while Present (Prag) loop
1404 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
1405 Name_Precondition)
1406 and then Is_Checked (Prag)
1407 then
1408 Has_Pragma := True;
1409 Transfer_Pragma (Prag, To => Decls);
1410 end if;
1411
1412 Prag := Next_Pragma (Prag);
1413 end loop;
1414
1415 -- Inspect the list of test/contract cases and transfer only contract
1416 -- cases pragmas to the declarative part of the wrapper.
1417
1418 Prag := Contract_Test_Cases (Items);
1419 while Present (Prag) loop
1420 if Pragma_Name (Prag) = Name_Contract_Cases
1421 and then Is_Checked (Prag)
1422 then
1423 Has_Pragma := True;
1424 Transfer_Pragma (Prag, To => Decls);
1425 end if;
1426
1427 Prag := Next_Pragma (Prag);
1428 end loop;
1429 end if;
1430
1431 -- The entry lacks executable contract items and a wrapper is not needed
1432
1433 if not Has_Pragma then
1434 return;
1435 end if;
1436
1437 -- Create the profile of the wrapper. The first formal parameter is the
1438 -- concurrent object.
1439
1440 Obj_Id :=
1441 Make_Defining_Identifier (Loc,
1442 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1443
1444 Formals := New_List (
1445 Make_Parameter_Specification (Loc,
1446 Defining_Identifier => Obj_Id,
1447 Out_Present => True,
1448 In_Present => True,
1449 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1450
1451 -- Construct the call to the original entry. The call will be gradually
1452 -- augmented with an optional entry index and extra parameters.
1453
1454 Call_Nam :=
1455 Make_Selected_Component (Loc,
1456 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1457 Selector_Name => New_Occurrence_Of (E, Loc));
1458
1459 -- When creating a wrapper for an entry family, the second formal is the
1460 -- entry index.
1461
1462 if Ekind (E) = E_Entry_Family then
1463 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1464
1465 Append_To (Formals,
1466 Make_Parameter_Specification (Loc,
1467 Defining_Identifier => Index_Id,
1468 Parameter_Type =>
1469 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1470
1471 -- The call to the original entry becomes an indexed component to
1472 -- accommodate the entry index.
1473
1474 Call_Nam :=
1475 Make_Indexed_Component (Loc,
1476 Prefix => Call_Nam,
1477 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1478 end if;
1479
1480 -- Add formal parameters to match those of the entry and build actuals
1481 -- for the entry call.
1482
1483 Add_Matching_Formals (Formals, Actuals);
1484
1485 Call :=
1486 Make_Procedure_Call_Statement (Loc,
1487 Name => Call_Nam,
1488 Parameter_Associations => Actuals);
1489
1490 -- Add renaming declarations for the discriminants of the enclosing type
1491 -- as the various contract items may reference them.
1492
1493 Add_Discriminant_Renamings (Obj_Id, Decls);
1494
1495 Wrapper_Id :=
1496 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1497 Set_Contract_Wrapper (E, Wrapper_Id);
1498
1499 -- The wrapper body is analyzed when the enclosing type is frozen
1500
1501 Append_Freeze_Action (Defining_Entity (Decl),
1502 Make_Subprogram_Body (Loc,
1503 Specification =>
1504 Make_Procedure_Specification (Loc,
1505 Defining_Unit_Name => Wrapper_Id,
1506 Parameter_Specifications => Formals),
1507 Declarations => Decls,
1508 Handled_Statement_Sequence =>
1509 Make_Handled_Sequence_Of_Statements (Loc,
1510 Statements => New_List (Call))));
1511 end Build_Contract_Wrapper;
1512
1513 --------------------------------
1514 -- Build_Corresponding_Record --
1515 --------------------------------
1516
1517 function Build_Corresponding_Record
1518 (N : Node_Id;
1519 Ctyp : Entity_Id;
1520 Loc : Source_Ptr) return Node_Id
1521 is
1522 Rec_Ent : constant Entity_Id :=
1523 Make_Defining_Identifier
1524 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1525 Disc : Entity_Id;
1526 Dlist : List_Id;
1527 New_Disc : Entity_Id;
1528 Cdecls : List_Id;
1529
1530 begin
1531 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1532 Set_Ekind (Rec_Ent, E_Record_Type);
1533 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1534 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1535 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1536 Set_Stored_Constraint (Rec_Ent, No_Elist);
1537 Cdecls := New_List;
1538
1539 -- Use discriminals to create list of discriminants for record, and
1540 -- create new discriminals for use in default expressions, etc. It is
1541 -- worth noting that a task discriminant gives rise to 5 entities;
1542
1543 -- a) The original discriminant.
1544 -- b) The discriminal for use in the task.
1545 -- c) The discriminant of the corresponding record.
1546 -- d) The discriminal for the init proc of the corresponding record.
1547 -- e) The local variable that renames the discriminant in the procedure
1548 -- for the task body.
1549
1550 -- In fact the discriminals b) are used in the renaming declarations
1551 -- for e). See details in einfo (Handling of Discriminants).
1552
1553 if Present (Discriminant_Specifications (N)) then
1554 Dlist := New_List;
1555 Disc := First_Discriminant (Ctyp);
1556
1557 while Present (Disc) loop
1558 New_Disc := CR_Discriminant (Disc);
1559
1560 Append_To (Dlist,
1561 Make_Discriminant_Specification (Loc,
1562 Defining_Identifier => New_Disc,
1563 Discriminant_Type =>
1564 New_Occurrence_Of (Etype (Disc), Loc),
1565 Expression =>
1566 New_Copy (Discriminant_Default_Value (Disc))));
1567
1568 Next_Discriminant (Disc);
1569 end loop;
1570
1571 else
1572 Dlist := No_List;
1573 end if;
1574
1575 -- Now we can construct the record type declaration. Note that this
1576 -- record is "limited tagged". It is "limited" to reflect the underlying
1577 -- limitedness of the task or protected object that it represents, and
1578 -- ensuring for example that it is properly passed by reference. It is
1579 -- "tagged" to give support to dispatching calls through interfaces. We
1580 -- propagate here the list of interfaces covered by the concurrent type
1581 -- (Ada 2005: AI-345).
1582
1583 return
1584 Make_Full_Type_Declaration (Loc,
1585 Defining_Identifier => Rec_Ent,
1586 Discriminant_Specifications => Dlist,
1587 Type_Definition =>
1588 Make_Record_Definition (Loc,
1589 Component_List =>
1590 Make_Component_List (Loc, Component_Items => Cdecls),
1591 Tagged_Present =>
1592 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1593 Interface_List => Interface_List (N),
1594 Limited_Present => True));
1595 end Build_Corresponding_Record;
1596
1597 ---------------------------------
1598 -- Build_Dispatching_Tag_Check --
1599 ---------------------------------
1600
1601 function Build_Dispatching_Tag_Check
1602 (K : Entity_Id;
1603 N : Node_Id) return Node_Id
1604 is
1605 Loc : constant Source_Ptr := Sloc (N);
1606
1607 begin
1608 return
1609 Make_Op_Or (Loc,
1610 Make_Op_Eq (Loc,
1611 Left_Opnd =>
1612 New_Occurrence_Of (K, Loc),
1613 Right_Opnd =>
1614 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1615
1616 Make_Op_Eq (Loc,
1617 Left_Opnd =>
1618 New_Occurrence_Of (K, Loc),
1619 Right_Opnd =>
1620 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1621 end Build_Dispatching_Tag_Check;
1622
1623 ----------------------------------
1624 -- Build_Entry_Count_Expression --
1625 ----------------------------------
1626
1627 function Build_Entry_Count_Expression
1628 (Concurrent_Type : Node_Id;
1629 Component_List : List_Id;
1630 Loc : Source_Ptr) return Node_Id
1631 is
1632 Eindx : Nat;
1633 Ent : Entity_Id;
1634 Ecount : Node_Id;
1635 Comp : Node_Id;
1636 Lo : Node_Id;
1637 Hi : Node_Id;
1638 Typ : Entity_Id;
1639 Large : Boolean;
1640
1641 begin
1642 -- Count number of non-family entries
1643
1644 Eindx := 0;
1645 Ent := First_Entity (Concurrent_Type);
1646 while Present (Ent) loop
1647 if Ekind (Ent) = E_Entry then
1648 Eindx := Eindx + 1;
1649 end if;
1650
1651 Next_Entity (Ent);
1652 end loop;
1653
1654 Ecount := Make_Integer_Literal (Loc, Eindx);
1655
1656 -- Loop through entry families building the addition nodes
1657
1658 Ent := First_Entity (Concurrent_Type);
1659 Comp := First (Component_List);
1660 while Present (Ent) loop
1661 if Ekind (Ent) = E_Entry_Family then
1662 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1663 Next (Comp);
1664 end loop;
1665
1666 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1667 Hi := Type_High_Bound (Typ);
1668 Lo := Type_Low_Bound (Typ);
1669 Large := Is_Potentially_Large_Family
1670 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1671 Ecount :=
1672 Make_Op_Add (Loc,
1673 Left_Opnd => Ecount,
1674 Right_Opnd =>
1675 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1676 end if;
1677
1678 Next_Entity (Ent);
1679 end loop;
1680
1681 return Ecount;
1682 end Build_Entry_Count_Expression;
1683
1684 -----------------------
1685 -- Build_Entry_Names --
1686 -----------------------
1687
1688 procedure Build_Entry_Names
1689 (Obj_Ref : Node_Id;
1690 Obj_Typ : Entity_Id;
1691 Stmts : List_Id)
1692 is
1693 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1694 Data : Entity_Id := Empty;
1695 Index : Entity_Id := Empty;
1696 Typ : Entity_Id := Obj_Typ;
1697
1698 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1699 -- Given an entry [family], create a static string which denotes the
1700 -- name of Comp_Id and assign it to the underlying data structure which
1701 -- contains the entry names of a concurrent object.
1702
1703 function Object_Reference return Node_Id;
1704 -- Return a reference to field _object or _task_id depending on the
1705 -- concurrent object being processed.
1706
1707 ----------------------
1708 -- Build_Entry_Name --
1709 ----------------------
1710
1711 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1712 function Build_Range (Def : Node_Id) return Node_Id;
1713 -- Given a discrete subtype definition of an entry family, generate a
1714 -- range node which covers the range of Def's type.
1715
1716 procedure Create_Index_And_Data;
1717 -- Generate the declarations of variables Index and Data. Subsequent
1718 -- calls do nothing.
1719
1720 function Increment_Index return Node_Id;
1721 -- Increment the index used in the assignment of string names to the
1722 -- Data array.
1723
1724 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1725 -- Given the name of a temporary variable, create the following
1726 -- declaration for it:
1727 --
1728 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1729
1730 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1731 -- Given the name of a temporary variable, place it in the array of
1732 -- string names. Generate:
1733 --
1734 -- Data (Index) := Def_Id'Unchecked_Access;
1735
1736 -----------------
1737 -- Build_Range --
1738 -----------------
1739
1740 function Build_Range (Def : Node_Id) return Node_Id is
1741 High : Node_Id := Type_High_Bound (Etype (Def));
1742 Low : Node_Id := Type_Low_Bound (Etype (Def));
1743
1744 begin
1745 -- If a bound references a discriminant, generate an identifier
1746 -- with the same name. Resolution will map it to the formals of
1747 -- the init proc.
1748
1749 if Is_Entity_Name (Low)
1750 and then Ekind (Entity (Low)) = E_Discriminant
1751 then
1752 Low :=
1753 Make_Selected_Component (Loc,
1754 Prefix => New_Copy_Tree (Obj_Ref),
1755 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1756 else
1757 Low := New_Copy_Tree (Low);
1758 end if;
1759
1760 if Is_Entity_Name (High)
1761 and then Ekind (Entity (High)) = E_Discriminant
1762 then
1763 High :=
1764 Make_Selected_Component (Loc,
1765 Prefix => New_Copy_Tree (Obj_Ref),
1766 Selector_Name => Make_Identifier (Loc, Chars (High)));
1767 else
1768 High := New_Copy_Tree (High);
1769 end if;
1770
1771 return
1772 Make_Range (Loc,
1773 Low_Bound => Low,
1774 High_Bound => High);
1775 end Build_Range;
1776
1777 ---------------------------
1778 -- Create_Index_And_Data --
1779 ---------------------------
1780
1781 procedure Create_Index_And_Data is
1782 begin
1783 if No (Index) and then No (Data) then
1784 declare
1785 Count : RE_Id;
1786 Data_Typ : RE_Id;
1787 Size : Entity_Id;
1788
1789 begin
1790 if Is_Protected_Type (Typ) then
1791 Count := RO_PE_Number_Of_Entries;
1792 Data_Typ := RE_Protected_Entry_Names_Array;
1793 else
1794 Count := RO_ST_Number_Of_Entries;
1795 Data_Typ := RE_Task_Entry_Names_Array;
1796 end if;
1797
1798 -- Step 1: Generate the declaration of the index variable:
1799
1800 -- Index : Entry_Index := 1;
1801
1802 Index := Make_Temporary (Loc, 'I');
1803
1804 Append_To (Stmts,
1805 Make_Object_Declaration (Loc,
1806 Defining_Identifier => Index,
1807 Object_Definition =>
1808 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1809 Expression => Make_Integer_Literal (Loc, 1)));
1810
1811 -- Step 2: Generate the declaration of an array to house all
1812 -- names:
1813
1814 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1815 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1816
1817 Size := Make_Temporary (Loc, 'S');
1818
1819 Append_To (Stmts,
1820 Make_Object_Declaration (Loc,
1821 Defining_Identifier => Size,
1822 Constant_Present => True,
1823 Object_Definition =>
1824 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1825 Expression =>
1826 Make_Function_Call (Loc,
1827 Name =>
1828 New_Occurrence_Of (RTE (Count), Loc),
1829 Parameter_Associations =>
1830 New_List (Object_Reference))));
1831
1832 Data := Make_Temporary (Loc, 'A');
1833
1834 Append_To (Stmts,
1835 Make_Object_Declaration (Loc,
1836 Defining_Identifier => Data,
1837 Aliased_Present => True,
1838 Object_Definition =>
1839 New_Occurrence_Of (RTE (Data_Typ), Loc),
1840 Expression =>
1841 Make_Aggregate (Loc,
1842 Component_Associations => New_List (
1843 Make_Component_Association (Loc,
1844 Choices => New_List (
1845 Make_Range (Loc,
1846 Low_Bound =>
1847 Make_Integer_Literal (Loc, 1),
1848 High_Bound =>
1849 New_Occurrence_Of (Size, Loc))),
1850 Expression => Make_Null (Loc))))));
1851 end;
1852 end if;
1853 end Create_Index_And_Data;
1854
1855 ---------------------
1856 -- Increment_Index --
1857 ---------------------
1858
1859 function Increment_Index return Node_Id is
1860 begin
1861 return
1862 Make_Assignment_Statement (Loc,
1863 Name => New_Occurrence_Of (Index, Loc),
1864 Expression =>
1865 Make_Op_Add (Loc,
1866 Left_Opnd => New_Occurrence_Of (Index, Loc),
1867 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1868 end Increment_Index;
1869
1870 ----------------------
1871 -- Name_Declaration --
1872 ----------------------
1873
1874 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1875 begin
1876 return
1877 Make_Object_Declaration (Loc,
1878 Defining_Identifier => Def_Id,
1879 Aliased_Present => True,
1880 Constant_Present => True,
1881 Object_Definition =>
1882 New_Occurrence_Of (Standard_String, Loc),
1883 Expression =>
1884 Make_String_Literal (Loc, String_From_Name_Buffer));
1885 end Name_Declaration;
1886
1887 --------------------
1888 -- Set_Entry_Name --
1889 --------------------
1890
1891 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1892 begin
1893 return
1894 Make_Assignment_Statement (Loc,
1895 Name =>
1896 Make_Indexed_Component (Loc,
1897 Prefix => New_Occurrence_Of (Data, Loc),
1898 Expressions => New_List (New_Occurrence_Of (Index, Loc))),
1899
1900 Expression =>
1901 Make_Attribute_Reference (Loc,
1902 Prefix => New_Occurrence_Of (Def_Id, Loc),
1903 Attribute_Name => Name_Unchecked_Access));
1904 end Set_Entry_Name;
1905
1906 -- Local variables
1907
1908 Temp_Id : Entity_Id;
1909 Subt_Def : Node_Id;
1910
1911 -- Start of processing for Build_Entry_Name
1912
1913 begin
1914 if Ekind (Comp_Id) = E_Entry_Family then
1915 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1916
1917 Create_Index_And_Data;
1918
1919 -- Step 1: Create the string name of the entry family.
1920 -- Generate:
1921 -- Temp : aliased constant String := "name ()";
1922
1923 Temp_Id := Make_Temporary (Loc, 'S');
1924 Get_Name_String (Chars (Comp_Id));
1925 Add_Char_To_Name_Buffer (' ');
1926 Add_Char_To_Name_Buffer ('(');
1927 Add_Char_To_Name_Buffer (')');
1928
1929 Append_To (Stmts, Name_Declaration (Temp_Id));
1930
1931 -- Generate:
1932 -- for Member in Family_Low .. Family_High loop
1933 -- Set_Entry_Name (...);
1934 -- Index := Index + 1;
1935 -- end loop;
1936
1937 Append_To (Stmts,
1938 Make_Loop_Statement (Loc,
1939 Iteration_Scheme =>
1940 Make_Iteration_Scheme (Loc,
1941 Loop_Parameter_Specification =>
1942 Make_Loop_Parameter_Specification (Loc,
1943 Defining_Identifier =>
1944 Make_Temporary (Loc, 'L'),
1945 Discrete_Subtype_Definition =>
1946 Build_Range (Subt_Def))),
1947
1948 Statements => New_List (
1949 Set_Entry_Name (Temp_Id),
1950 Increment_Index),
1951 End_Label => Empty));
1952
1953 -- Entry
1954
1955 else
1956 Create_Index_And_Data;
1957
1958 -- Step 1: Create the string name of the entry. Generate:
1959 -- Temp : aliased constant String := "name";
1960
1961 Temp_Id := Make_Temporary (Loc, 'S');
1962 Get_Name_String (Chars (Comp_Id));
1963
1964 Append_To (Stmts, Name_Declaration (Temp_Id));
1965
1966 -- Step 2: Associate the string name with the underlying data
1967 -- structure.
1968
1969 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1970 Append_To (Stmts, Increment_Index);
1971 end if;
1972 end Build_Entry_Name;
1973
1974 ----------------------
1975 -- Object_Reference --
1976 ----------------------
1977
1978 function Object_Reference return Node_Id is
1979 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1980 Field : Name_Id;
1981 Ref : Node_Id;
1982
1983 begin
1984 if Is_Protected_Type (Typ) then
1985 Field := Name_uObject;
1986 else
1987 Field := Name_uTask_Id;
1988 end if;
1989
1990 Ref :=
1991 Make_Selected_Component (Loc,
1992 Prefix =>
1993 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1994 Selector_Name => Make_Identifier (Loc, Field));
1995
1996 if Is_Protected_Type (Typ) then
1997 Ref :=
1998 Make_Attribute_Reference (Loc,
1999 Prefix => Ref,
2000 Attribute_Name => Name_Unchecked_Access);
2001 end if;
2002
2003 return Ref;
2004 end Object_Reference;
2005
2006 -- Local variables
2007
2008 Comp : Node_Id;
2009 Proc : RE_Id;
2010
2011 -- Start of processing for Build_Entry_Names
2012
2013 begin
2014 -- Retrieve the original concurrent type
2015
2016 if Is_Concurrent_Record_Type (Typ) then
2017 Typ := Corresponding_Concurrent_Type (Typ);
2018 end if;
2019
2020 pragma Assert (Is_Concurrent_Type (Typ));
2021
2022 -- Nothing to do if the type has no entries
2023
2024 if not Has_Entries (Typ) then
2025 return;
2026 end if;
2027
2028 -- Avoid generating entry names for a protected type with only one entry
2029
2030 if Is_Protected_Type (Typ)
2031 and then Find_Protection_Type (Base_Type (Typ)) /=
2032 RTE (RE_Protection_Entries)
2033 then
2034 return;
2035 end if;
2036
2037 -- Step 1: Populate the array with statically generated strings denoting
2038 -- entries and entry family names.
2039
2040 Comp := First_Entity (Typ);
2041 while Present (Comp) loop
2042 if Comes_From_Source (Comp)
2043 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
2044 then
2045 Build_Entry_Name (Comp);
2046 end if;
2047
2048 Next_Entity (Comp);
2049 end loop;
2050
2051 -- Step 2: Associate the array with the related concurrent object:
2052
2053 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
2054
2055 if Present (Data) then
2056 if Is_Protected_Type (Typ) then
2057 Proc := RO_PE_Set_Entry_Names;
2058 else
2059 Proc := RO_ST_Set_Entry_Names;
2060 end if;
2061
2062 Append_To (Stmts,
2063 Make_Procedure_Call_Statement (Loc,
2064 Name => New_Occurrence_Of (RTE (Proc), Loc),
2065 Parameter_Associations => New_List (
2066 Object_Reference,
2067 Make_Attribute_Reference (Loc,
2068 Prefix => New_Occurrence_Of (Data, Loc),
2069 Attribute_Name => Name_Unchecked_Access))));
2070 end if;
2071 end Build_Entry_Names;
2072
2073 ---------------------------
2074 -- Build_Parameter_Block --
2075 ---------------------------
2076
2077 function Build_Parameter_Block
2078 (Loc : Source_Ptr;
2079 Actuals : List_Id;
2080 Formals : List_Id;
2081 Decls : List_Id) return Entity_Id
2082 is
2083 Actual : Entity_Id;
2084 Comp_Nam : Node_Id;
2085 Comps : List_Id;
2086 Formal : Entity_Id;
2087 Has_Comp : Boolean := False;
2088 Rec_Nam : Node_Id;
2089
2090 begin
2091 Actual := First (Actuals);
2092 Comps := New_List;
2093 Formal := Defining_Identifier (First (Formals));
2094
2095 while Present (Actual) loop
2096 if not Is_Controlling_Actual (Actual) then
2097
2098 -- Generate:
2099 -- type Ann is access all <actual-type>
2100
2101 Comp_Nam := Make_Temporary (Loc, 'A');
2102 Set_Is_Param_Block_Component_Type (Comp_Nam);
2103
2104 Append_To (Decls,
2105 Make_Full_Type_Declaration (Loc,
2106 Defining_Identifier => Comp_Nam,
2107 Type_Definition =>
2108 Make_Access_To_Object_Definition (Loc,
2109 All_Present => True,
2110 Constant_Present => Ekind (Formal) = E_In_Parameter,
2111 Subtype_Indication =>
2112 New_Occurrence_Of (Etype (Actual), Loc))));
2113
2114 -- Generate:
2115 -- Param : Ann;
2116
2117 Append_To (Comps,
2118 Make_Component_Declaration (Loc,
2119 Defining_Identifier =>
2120 Make_Defining_Identifier (Loc, Chars (Formal)),
2121 Component_Definition =>
2122 Make_Component_Definition (Loc,
2123 Aliased_Present =>
2124 False,
2125 Subtype_Indication =>
2126 New_Occurrence_Of (Comp_Nam, Loc))));
2127
2128 Has_Comp := True;
2129 end if;
2130
2131 Next_Actual (Actual);
2132 Next_Formal_With_Extras (Formal);
2133 end loop;
2134
2135 Rec_Nam := Make_Temporary (Loc, 'P');
2136
2137 if Has_Comp then
2138
2139 -- Generate:
2140 -- type Pnn is record
2141 -- Param1 : Ann1;
2142 -- ...
2143 -- ParamN : AnnN;
2144
2145 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
2146 -- the original parameter names and Ann1 .. AnnN are the access to
2147 -- actual types.
2148
2149 Append_To (Decls,
2150 Make_Full_Type_Declaration (Loc,
2151 Defining_Identifier =>
2152 Rec_Nam,
2153 Type_Definition =>
2154 Make_Record_Definition (Loc,
2155 Component_List =>
2156 Make_Component_List (Loc, Comps))));
2157 else
2158 -- Generate:
2159 -- type Pnn is null record;
2160
2161 Append_To (Decls,
2162 Make_Full_Type_Declaration (Loc,
2163 Defining_Identifier =>
2164 Rec_Nam,
2165 Type_Definition =>
2166 Make_Record_Definition (Loc,
2167 Null_Present => True,
2168 Component_List => Empty)));
2169 end if;
2170
2171 return Rec_Nam;
2172 end Build_Parameter_Block;
2173
2174 --------------------------------------
2175 -- Build_Renamed_Formal_Declaration --
2176 --------------------------------------
2177
2178 function Build_Renamed_Formal_Declaration
2179 (New_F : Entity_Id;
2180 Formal : Entity_Id;
2181 Comp : Entity_Id;
2182 Renamed_Formal : Node_Id) return Node_Id
2183 is
2184 Loc : constant Source_Ptr := Sloc (New_F);
2185 Decl : Node_Id;
2186
2187 begin
2188 -- If the formal is a tagged incomplete type, it is already passed
2189 -- by reference, so it is sufficient to rename the pointer component
2190 -- that corresponds to the actual. Otherwise we need to dereference
2191 -- the pointer component to obtain the actual.
2192
2193 if Is_Incomplete_Type (Etype (Formal))
2194 and then Is_Tagged_Type (Etype (Formal))
2195 then
2196 Decl :=
2197 Make_Object_Renaming_Declaration (Loc,
2198 Defining_Identifier => New_F,
2199 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
2200 Name => Renamed_Formal);
2201
2202 else
2203 Decl :=
2204 Make_Object_Renaming_Declaration (Loc,
2205 Defining_Identifier => New_F,
2206 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
2207 Name =>
2208 Make_Explicit_Dereference (Loc, Renamed_Formal));
2209 end if;
2210
2211 return Decl;
2212 end Build_Renamed_Formal_Declaration;
2213
2214 --------------------------
2215 -- Build_Wrapper_Bodies --
2216 --------------------------
2217
2218 procedure Build_Wrapper_Bodies
2219 (Loc : Source_Ptr;
2220 Typ : Entity_Id;
2221 N : Node_Id)
2222 is
2223 Rec_Typ : Entity_Id;
2224
2225 function Build_Wrapper_Body
2226 (Loc : Source_Ptr;
2227 Subp_Id : Entity_Id;
2228 Obj_Typ : Entity_Id;
2229 Formals : List_Id) return Node_Id;
2230 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2231 -- associated with a protected or task type. Subp_Id is the subprogram
2232 -- name which will be wrapped. Obj_Typ is the type of the new formal
2233 -- parameter which handles dispatching and object notation. Formals are
2234 -- the original formals of Subp_Id which will be explicitly replicated.
2235
2236 ------------------------
2237 -- Build_Wrapper_Body --
2238 ------------------------
2239
2240 function Build_Wrapper_Body
2241 (Loc : Source_Ptr;
2242 Subp_Id : Entity_Id;
2243 Obj_Typ : Entity_Id;
2244 Formals : List_Id) return Node_Id
2245 is
2246 Body_Spec : Node_Id;
2247
2248 begin
2249 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2250
2251 -- The subprogram is not overriding or is not a primitive declared
2252 -- between two views.
2253
2254 if No (Body_Spec) then
2255 return Empty;
2256 end if;
2257
2258 declare
2259 Actuals : List_Id := No_List;
2260 Conv_Id : Node_Id;
2261 First_Form : Node_Id;
2262 Formal : Node_Id;
2263 Nam : Node_Id;
2264
2265 begin
2266 -- Map formals to actuals. Use the list built for the wrapper
2267 -- spec, skipping the object notation parameter.
2268
2269 First_Form := First (Parameter_Specifications (Body_Spec));
2270
2271 Formal := First_Form;
2272 Next (Formal);
2273
2274 if Present (Formal) then
2275 Actuals := New_List;
2276 while Present (Formal) loop
2277 Append_To (Actuals,
2278 Make_Identifier (Loc,
2279 Chars => Chars (Defining_Identifier (Formal))));
2280 Next (Formal);
2281 end loop;
2282 end if;
2283
2284 -- Special processing for primitives declared between a private
2285 -- type and its completion: the wrapper needs a properly typed
2286 -- parameter if the wrapped operation has a controlling first
2287 -- parameter. Note that this might not be the case for a function
2288 -- with a controlling result.
2289
2290 if Is_Private_Primitive_Subprogram (Subp_Id) then
2291 if No (Actuals) then
2292 Actuals := New_List;
2293 end if;
2294
2295 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2296 Prepend_To (Actuals,
2297 Unchecked_Convert_To
2298 (Corresponding_Concurrent_Type (Obj_Typ),
2299 Make_Identifier (Loc, Name_uO)));
2300
2301 else
2302 Prepend_To (Actuals,
2303 Make_Identifier (Loc,
2304 Chars => Chars (Defining_Identifier (First_Form))));
2305 end if;
2306
2307 Nam := New_Occurrence_Of (Subp_Id, Loc);
2308 else
2309 -- An access-to-variable object parameter requires an explicit
2310 -- dereference in the unchecked conversion. This case occurs
2311 -- when a protected entry wrapper must override an interface
2312 -- level procedure with interface access as first parameter.
2313
2314 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2315
2316 if Nkind (Parameter_Type (First_Form)) =
2317 N_Access_Definition
2318 then
2319 Conv_Id :=
2320 Make_Explicit_Dereference (Loc,
2321 Prefix => Make_Identifier (Loc, Name_uO));
2322 else
2323 Conv_Id := Make_Identifier (Loc, Name_uO);
2324 end if;
2325
2326 Nam :=
2327 Make_Selected_Component (Loc,
2328 Prefix =>
2329 Unchecked_Convert_To
2330 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2331 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
2332 end if;
2333
2334 -- Create the subprogram body. For a function, the call to the
2335 -- actual subprogram has to be converted to the corresponding
2336 -- record if it is a controlling result.
2337
2338 if Ekind (Subp_Id) = E_Function then
2339 declare
2340 Res : Node_Id;
2341
2342 begin
2343 Res :=
2344 Make_Function_Call (Loc,
2345 Name => Nam,
2346 Parameter_Associations => Actuals);
2347
2348 if Has_Controlling_Result (Subp_Id) then
2349 Res :=
2350 Unchecked_Convert_To
2351 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2352 end if;
2353
2354 return
2355 Make_Subprogram_Body (Loc,
2356 Specification => Body_Spec,
2357 Declarations => Empty_List,
2358 Handled_Statement_Sequence =>
2359 Make_Handled_Sequence_Of_Statements (Loc,
2360 Statements => New_List (
2361 Make_Simple_Return_Statement (Loc, Res))));
2362 end;
2363
2364 else
2365 return
2366 Make_Subprogram_Body (Loc,
2367 Specification => Body_Spec,
2368 Declarations => Empty_List,
2369 Handled_Statement_Sequence =>
2370 Make_Handled_Sequence_Of_Statements (Loc,
2371 Statements => New_List (
2372 Make_Procedure_Call_Statement (Loc,
2373 Name => Nam,
2374 Parameter_Associations => Actuals))));
2375 end if;
2376 end;
2377 end Build_Wrapper_Body;
2378
2379 -- Start of processing for Build_Wrapper_Bodies
2380
2381 begin
2382 if Is_Concurrent_Type (Typ) then
2383 Rec_Typ := Corresponding_Record_Type (Typ);
2384 else
2385 Rec_Typ := Typ;
2386 end if;
2387
2388 -- Generate wrapper bodies for a concurrent type which implements an
2389 -- interface.
2390
2391 if Present (Interfaces (Rec_Typ)) then
2392 declare
2393 Insert_Nod : Node_Id;
2394 Prim : Entity_Id;
2395 Prim_Elmt : Elmt_Id;
2396 Prim_Decl : Node_Id;
2397 Subp : Entity_Id;
2398 Wrap_Body : Node_Id;
2399 Wrap_Id : Entity_Id;
2400
2401 begin
2402 Insert_Nod := N;
2403
2404 -- Examine all primitive operations of the corresponding record
2405 -- type, looking for wrapper specs. Generate bodies in order to
2406 -- complete them.
2407
2408 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2409 while Present (Prim_Elmt) loop
2410 Prim := Node (Prim_Elmt);
2411
2412 if (Ekind (Prim) = E_Function
2413 or else Ekind (Prim) = E_Procedure)
2414 and then Is_Primitive_Wrapper (Prim)
2415 then
2416 Subp := Wrapped_Entity (Prim);
2417 Prim_Decl := Parent (Parent (Prim));
2418
2419 Wrap_Body :=
2420 Build_Wrapper_Body (Loc,
2421 Subp_Id => Subp,
2422 Obj_Typ => Rec_Typ,
2423 Formals => Parameter_Specifications (Parent (Subp)));
2424 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2425
2426 Set_Corresponding_Spec (Wrap_Body, Prim);
2427 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2428
2429 Insert_After (Insert_Nod, Wrap_Body);
2430 Insert_Nod := Wrap_Body;
2431
2432 Analyze (Wrap_Body);
2433 end if;
2434
2435 Next_Elmt (Prim_Elmt);
2436 end loop;
2437 end;
2438 end if;
2439 end Build_Wrapper_Bodies;
2440
2441 ------------------------
2442 -- Build_Wrapper_Spec --
2443 ------------------------
2444
2445 function Build_Wrapper_Spec
2446 (Subp_Id : Entity_Id;
2447 Obj_Typ : Entity_Id;
2448 Formals : List_Id) return Node_Id
2449 is
2450 function Overriding_Possible
2451 (Iface_Op : Entity_Id;
2452 Wrapper : Entity_Id) return Boolean;
2453 -- Determine whether a primitive operation can be overridden by Wrapper.
2454 -- Iface_Op is the candidate primitive operation of an interface type,
2455 -- Wrapper is the generated entry wrapper.
2456
2457 function Replicate_Formals
2458 (Loc : Source_Ptr;
2459 Formals : List_Id) return List_Id;
2460 -- An explicit parameter replication is required due to the Is_Entry_
2461 -- Formal flag being set for all the formals of an entry. The explicit
2462 -- replication removes the flag that would otherwise cause a different
2463 -- path of analysis.
2464
2465 -------------------------
2466 -- Overriding_Possible --
2467 -------------------------
2468
2469 function Overriding_Possible
2470 (Iface_Op : Entity_Id;
2471 Wrapper : Entity_Id) return Boolean
2472 is
2473 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2474 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2475
2476 function Type_Conformant_Parameters
2477 (Iface_Op_Params : List_Id;
2478 Wrapper_Params : List_Id) return Boolean;
2479 -- Determine whether the parameters of the generated entry wrapper
2480 -- and those of a primitive operation are type conformant. During
2481 -- this check, the first parameter of the primitive operation is
2482 -- skipped if it is a controlling argument: protected functions
2483 -- may have a controlling result.
2484
2485 --------------------------------
2486 -- Type_Conformant_Parameters --
2487 --------------------------------
2488
2489 function Type_Conformant_Parameters
2490 (Iface_Op_Params : List_Id;
2491 Wrapper_Params : List_Id) return Boolean
2492 is
2493 Iface_Op_Param : Node_Id;
2494 Iface_Op_Typ : Entity_Id;
2495 Wrapper_Param : Node_Id;
2496 Wrapper_Typ : Entity_Id;
2497
2498 begin
2499 -- Skip the first (controlling) parameter of primitive operation
2500
2501 Iface_Op_Param := First (Iface_Op_Params);
2502
2503 if Present (First_Formal (Iface_Op))
2504 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2505 then
2506 Iface_Op_Param := Next (Iface_Op_Param);
2507 end if;
2508
2509 Wrapper_Param := First (Wrapper_Params);
2510 while Present (Iface_Op_Param)
2511 and then Present (Wrapper_Param)
2512 loop
2513 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2514 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2515
2516 -- The two parameters must be mode conformant
2517
2518 if not Conforming_Types
2519 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2520 then
2521 return False;
2522 end if;
2523
2524 Next (Iface_Op_Param);
2525 Next (Wrapper_Param);
2526 end loop;
2527
2528 -- One of the lists is longer than the other
2529
2530 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2531 return False;
2532 end if;
2533
2534 return True;
2535 end Type_Conformant_Parameters;
2536
2537 -- Start of processing for Overriding_Possible
2538
2539 begin
2540 if Chars (Iface_Op) /= Chars (Wrapper) then
2541 return False;
2542 end if;
2543
2544 -- If an inherited subprogram is implemented by a protected procedure
2545 -- or an entry, then the first parameter of the inherited subprogram
2546 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2547
2548 if Ekind (Iface_Op) = E_Procedure
2549 and then Present (Parameter_Specifications (Iface_Op_Spec))
2550 then
2551 declare
2552 Obj_Param : constant Node_Id :=
2553 First (Parameter_Specifications (Iface_Op_Spec));
2554 begin
2555 if not Out_Present (Obj_Param)
2556 and then Nkind (Parameter_Type (Obj_Param)) /=
2557 N_Access_Definition
2558 then
2559 return False;
2560 end if;
2561 end;
2562 end if;
2563
2564 return
2565 Type_Conformant_Parameters
2566 (Parameter_Specifications (Iface_Op_Spec),
2567 Parameter_Specifications (Wrapper_Spec));
2568 end Overriding_Possible;
2569
2570 -----------------------
2571 -- Replicate_Formals --
2572 -----------------------
2573
2574 function Replicate_Formals
2575 (Loc : Source_Ptr;
2576 Formals : List_Id) return List_Id
2577 is
2578 New_Formals : constant List_Id := New_List;
2579 Formal : Node_Id;
2580 Param_Type : Node_Id;
2581
2582 begin
2583 Formal := First (Formals);
2584
2585 -- Skip the object parameter when dealing with primitives declared
2586 -- between two views.
2587
2588 if Is_Private_Primitive_Subprogram (Subp_Id)
2589 and then not Has_Controlling_Result (Subp_Id)
2590 then
2591 Formal := Next (Formal);
2592 end if;
2593
2594 while Present (Formal) loop
2595
2596 -- Create an explicit copy of the entry parameter
2597
2598 -- When creating the wrapper subprogram for a primitive operation
2599 -- of a protected interface we must construct an equivalent
2600 -- signature to that of the overriding operation. For regular
2601 -- parameters we can just use the type of the formal, but for
2602 -- access to subprogram parameters we need to reanalyze the
2603 -- parameter type to create local entities for the signature of
2604 -- the subprogram type. Using the entities of the overriding
2605 -- subprogram will result in out-of-scope errors in the back-end.
2606
2607 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2608 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2609 else
2610 Param_Type :=
2611 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2612 end if;
2613
2614 Append_To (New_Formals,
2615 Make_Parameter_Specification (Loc,
2616 Defining_Identifier =>
2617 Make_Defining_Identifier (Loc,
2618 Chars => Chars (Defining_Identifier (Formal))),
2619 In_Present => In_Present (Formal),
2620 Out_Present => Out_Present (Formal),
2621 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2622 Parameter_Type => Param_Type));
2623
2624 Next (Formal);
2625 end loop;
2626
2627 return New_Formals;
2628 end Replicate_Formals;
2629
2630 -- Local variables
2631
2632 Loc : constant Source_Ptr := Sloc (Subp_Id);
2633 First_Param : Node_Id := Empty;
2634 Iface : Entity_Id;
2635 Iface_Elmt : Elmt_Id;
2636 Iface_Op : Entity_Id;
2637 Iface_Op_Elmt : Elmt_Id;
2638 Overridden_Subp : Entity_Id;
2639
2640 -- Start of processing for Build_Wrapper_Spec
2641
2642 begin
2643 -- No point in building wrappers for untagged concurrent types
2644
2645 pragma Assert (Is_Tagged_Type (Obj_Typ));
2646
2647 -- Check if this subprogram has a profile that matches some interface
2648 -- primitive.
2649
2650 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2651
2652 if Present (Overridden_Subp) then
2653 First_Param :=
2654 First (Parameter_Specifications (Parent (Overridden_Subp)));
2655
2656 -- An entry or a protected procedure can override a routine where the
2657 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2658 -- type. Since the wrapper must have the exact same signature as that of
2659 -- the overridden subprogram, we try to find the overriding candidate
2660 -- and use its controlling formal.
2661
2662 -- Check every implemented interface
2663
2664 elsif Present (Interfaces (Obj_Typ)) then
2665 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2666 Search : while Present (Iface_Elmt) loop
2667 Iface := Node (Iface_Elmt);
2668
2669 -- Check every interface primitive
2670
2671 if Present (Primitive_Operations (Iface)) then
2672 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2673 while Present (Iface_Op_Elmt) loop
2674 Iface_Op := Node (Iface_Op_Elmt);
2675
2676 -- Ignore predefined primitives
2677
2678 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2679 Iface_Op := Ultimate_Alias (Iface_Op);
2680
2681 -- The current primitive operation can be overridden by
2682 -- the generated entry wrapper.
2683
2684 if Overriding_Possible (Iface_Op, Subp_Id) then
2685 First_Param :=
2686 First (Parameter_Specifications (Parent (Iface_Op)));
2687
2688 exit Search;
2689 end if;
2690 end if;
2691
2692 Next_Elmt (Iface_Op_Elmt);
2693 end loop;
2694 end if;
2695
2696 Next_Elmt (Iface_Elmt);
2697 end loop Search;
2698 end if;
2699
2700 -- Do not generate the wrapper if no interface primitive is covered by
2701 -- the subprogram and it is not a primitive declared between two views
2702 -- (see Process_Full_View).
2703
2704 if No (First_Param)
2705 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2706 then
2707 return Empty;
2708 end if;
2709
2710 declare
2711 Wrapper_Id : constant Entity_Id :=
2712 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2713 New_Formals : List_Id;
2714 Obj_Param : Node_Id;
2715 Obj_Param_Typ : Entity_Id;
2716
2717 begin
2718 -- Minimum decoration is needed to catch the entity in
2719 -- Sem_Ch6.Override_Dispatching_Operation.
2720
2721 if Ekind (Subp_Id) = E_Function then
2722 Set_Ekind (Wrapper_Id, E_Function);
2723 else
2724 Set_Ekind (Wrapper_Id, E_Procedure);
2725 end if;
2726
2727 Set_Is_Primitive_Wrapper (Wrapper_Id);
2728 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2729 Set_Is_Private_Primitive (Wrapper_Id,
2730 Is_Private_Primitive_Subprogram (Subp_Id));
2731
2732 -- Process the formals
2733
2734 New_Formals := Replicate_Formals (Loc, Formals);
2735
2736 -- A function with a controlling result and no first controlling
2737 -- formal needs no additional parameter.
2738
2739 if Has_Controlling_Result (Subp_Id)
2740 and then
2741 (No (First_Formal (Subp_Id))
2742 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2743 then
2744 null;
2745
2746 -- Routine Subp_Id has been found to override an interface primitive.
2747 -- If the interface operation has an access parameter, create a copy
2748 -- of it, with the same null exclusion indicator if present.
2749
2750 elsif Present (First_Param) then
2751 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2752 Obj_Param_Typ :=
2753 Make_Access_Definition (Loc,
2754 Subtype_Mark =>
2755 New_Occurrence_Of (Obj_Typ, Loc),
2756 Null_Exclusion_Present =>
2757 Null_Exclusion_Present (Parameter_Type (First_Param)),
2758 Constant_Present =>
2759 Constant_Present (Parameter_Type (First_Param)));
2760 else
2761 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2762 end if;
2763
2764 Obj_Param :=
2765 Make_Parameter_Specification (Loc,
2766 Defining_Identifier =>
2767 Make_Defining_Identifier (Loc,
2768 Chars => Name_uO),
2769 In_Present => In_Present (First_Param),
2770 Out_Present => Out_Present (First_Param),
2771 Parameter_Type => Obj_Param_Typ);
2772
2773 Prepend_To (New_Formals, Obj_Param);
2774
2775 -- If we are dealing with a primitive declared between two views,
2776 -- implemented by a synchronized operation, we need to create
2777 -- a default parameter. The mode of the parameter must match that
2778 -- of the primitive operation.
2779
2780 else
2781 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2782
2783 Obj_Param :=
2784 Make_Parameter_Specification (Loc,
2785 Defining_Identifier =>
2786 Make_Defining_Identifier (Loc, Name_uO),
2787 In_Present =>
2788 In_Present (Parent (First_Entity (Subp_Id))),
2789 Out_Present => Ekind (Subp_Id) /= E_Function,
2790 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2791
2792 Prepend_To (New_Formals, Obj_Param);
2793 end if;
2794
2795 -- Build the final spec. If it is a function with a controlling
2796 -- result, it is a primitive operation of the corresponding
2797 -- record type, so mark the spec accordingly.
2798
2799 if Ekind (Subp_Id) = E_Function then
2800 declare
2801 Res_Def : Node_Id;
2802
2803 begin
2804 if Has_Controlling_Result (Subp_Id) then
2805 Res_Def :=
2806 New_Occurrence_Of
2807 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2808 else
2809 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2810 end if;
2811
2812 return
2813 Make_Function_Specification (Loc,
2814 Defining_Unit_Name => Wrapper_Id,
2815 Parameter_Specifications => New_Formals,
2816 Result_Definition => Res_Def);
2817 end;
2818 else
2819 return
2820 Make_Procedure_Specification (Loc,
2821 Defining_Unit_Name => Wrapper_Id,
2822 Parameter_Specifications => New_Formals);
2823 end if;
2824 end;
2825 end Build_Wrapper_Spec;
2826
2827 -------------------------
2828 -- Build_Wrapper_Specs --
2829 -------------------------
2830
2831 procedure Build_Wrapper_Specs
2832 (Loc : Source_Ptr;
2833 Typ : Entity_Id;
2834 N : in out Node_Id)
2835 is
2836 Def : Node_Id;
2837 Rec_Typ : Entity_Id;
2838 procedure Scan_Declarations (L : List_Id);
2839 -- Common processing for visible and private declarations
2840 -- of a protected type.
2841
2842 procedure Scan_Declarations (L : List_Id) is
2843 Decl : Node_Id;
2844 Wrap_Decl : Node_Id;
2845 Wrap_Spec : Node_Id;
2846
2847 begin
2848 if No (L) then
2849 return;
2850 end if;
2851
2852 Decl := First (L);
2853 while Present (Decl) loop
2854 Wrap_Spec := Empty;
2855
2856 if Nkind (Decl) = N_Entry_Declaration
2857 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2858 then
2859 Wrap_Spec :=
2860 Build_Wrapper_Spec
2861 (Subp_Id => Defining_Identifier (Decl),
2862 Obj_Typ => Rec_Typ,
2863 Formals => Parameter_Specifications (Decl));
2864
2865 elsif Nkind (Decl) = N_Subprogram_Declaration then
2866 Wrap_Spec :=
2867 Build_Wrapper_Spec
2868 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2869 Obj_Typ => Rec_Typ,
2870 Formals =>
2871 Parameter_Specifications (Specification (Decl)));
2872 end if;
2873
2874 if Present (Wrap_Spec) then
2875 Wrap_Decl :=
2876 Make_Subprogram_Declaration (Loc,
2877 Specification => Wrap_Spec);
2878
2879 Insert_After (N, Wrap_Decl);
2880 N := Wrap_Decl;
2881
2882 Analyze (Wrap_Decl);
2883 end if;
2884
2885 Next (Decl);
2886 end loop;
2887 end Scan_Declarations;
2888
2889 -- start of processing for Build_Wrapper_Specs
2890
2891 begin
2892 if Is_Protected_Type (Typ) then
2893 Def := Protected_Definition (Parent (Typ));
2894 else pragma Assert (Is_Task_Type (Typ));
2895 Def := Task_Definition (Parent (Typ));
2896 end if;
2897
2898 Rec_Typ := Corresponding_Record_Type (Typ);
2899
2900 -- Generate wrapper specs for a concurrent type which implements an
2901 -- interface. Operations in both the visible and private parts may
2902 -- implement progenitor operations.
2903
2904 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2905 Scan_Declarations (Visible_Declarations (Def));
2906 Scan_Declarations (Private_Declarations (Def));
2907 end if;
2908 end Build_Wrapper_Specs;
2909
2910 ---------------------------
2911 -- Build_Find_Body_Index --
2912 ---------------------------
2913
2914 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2915 Loc : constant Source_Ptr := Sloc (Typ);
2916 Ent : Entity_Id;
2917 E_Typ : Entity_Id;
2918 Has_F : Boolean := False;
2919 Index : Nat;
2920 If_St : Node_Id := Empty;
2921 Lo : Node_Id;
2922 Hi : Node_Id;
2923 Decls : List_Id := New_List;
2924 Ret : Node_Id;
2925 Spec : Node_Id;
2926 Siz : Node_Id := Empty;
2927
2928 procedure Add_If_Clause (Expr : Node_Id);
2929 -- Add test for range of current entry
2930
2931 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2932 -- If a bound of an entry is given by a discriminant, retrieve the
2933 -- actual value of the discriminant from the enclosing object.
2934
2935 -------------------
2936 -- Add_If_Clause --
2937 -------------------
2938
2939 procedure Add_If_Clause (Expr : Node_Id) is
2940 Cond : Node_Id;
2941 Stats : constant List_Id :=
2942 New_List (
2943 Make_Simple_Return_Statement (Loc,
2944 Expression => Make_Integer_Literal (Loc, Index + 1)));
2945
2946 begin
2947 -- Index for current entry body
2948
2949 Index := Index + 1;
2950
2951 -- Compute total length of entry queues so far
2952
2953 if No (Siz) then
2954 Siz := Expr;
2955 else
2956 Siz :=
2957 Make_Op_Add (Loc,
2958 Left_Opnd => Siz,
2959 Right_Opnd => Expr);
2960 end if;
2961
2962 Cond :=
2963 Make_Op_Le (Loc,
2964 Left_Opnd => Make_Identifier (Loc, Name_uE),
2965 Right_Opnd => Siz);
2966
2967 -- Map entry queue indexes in the range of the current family
2968 -- into the current index, that designates the entry body.
2969
2970 if No (If_St) then
2971 If_St :=
2972 Make_Implicit_If_Statement (Typ,
2973 Condition => Cond,
2974 Then_Statements => Stats,
2975 Elsif_Parts => New_List);
2976 Ret := If_St;
2977
2978 else
2979 Append_To (Elsif_Parts (If_St),
2980 Make_Elsif_Part (Loc,
2981 Condition => Cond,
2982 Then_Statements => Stats));
2983 end if;
2984 end Add_If_Clause;
2985
2986 ------------------------------
2987 -- Convert_Discriminant_Ref --
2988 ------------------------------
2989
2990 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2991 B : Node_Id;
2992
2993 begin
2994 if Is_Entity_Name (Bound)
2995 and then Ekind (Entity (Bound)) = E_Discriminant
2996 then
2997 B :=
2998 Make_Selected_Component (Loc,
2999 Prefix =>
3000 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
3001 Make_Explicit_Dereference (Loc,
3002 Make_Identifier (Loc, Name_uObject))),
3003 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
3004 Set_Etype (B, Etype (Entity (Bound)));
3005 else
3006 B := New_Copy_Tree (Bound);
3007 end if;
3008
3009 return B;
3010 end Convert_Discriminant_Ref;
3011
3012 -- Start of processing for Build_Find_Body_Index
3013
3014 begin
3015 Spec := Build_Find_Body_Index_Spec (Typ);
3016
3017 Ent := First_Entity (Typ);
3018 while Present (Ent) loop
3019 if Ekind (Ent) = E_Entry_Family then
3020 Has_F := True;
3021 exit;
3022 end if;
3023
3024 Next_Entity (Ent);
3025 end loop;
3026
3027 if not Has_F then
3028
3029 -- If the protected type has no entry families, there is a one-one
3030 -- correspondence between entry queue and entry body.
3031
3032 Ret :=
3033 Make_Simple_Return_Statement (Loc,
3034 Expression => Make_Identifier (Loc, Name_uE));
3035
3036 else
3037 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
3038 -- the following:
3039
3040 -- if E <= l1 then return 1;
3041 -- elsif E <= l1 + l2 then return 2;
3042 -- ...
3043
3044 Index := 0;
3045 Siz := Empty;
3046 Ent := First_Entity (Typ);
3047
3048 Add_Object_Pointer (Loc, Typ, Decls);
3049
3050 while Present (Ent) loop
3051 if Ekind (Ent) = E_Entry then
3052 Add_If_Clause (Make_Integer_Literal (Loc, 1));
3053
3054 elsif Ekind (Ent) = E_Entry_Family then
3055 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
3056 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
3057 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
3058 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
3059 end if;
3060
3061 Next_Entity (Ent);
3062 end loop;
3063
3064 if Index = 1 then
3065 Decls := New_List;
3066 Ret :=
3067 Make_Simple_Return_Statement (Loc,
3068 Expression => Make_Integer_Literal (Loc, 1));
3069
3070 elsif Nkind (Ret) = N_If_Statement then
3071
3072 -- Ranges are in increasing order, so last one doesn't need guard
3073
3074 declare
3075 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
3076 begin
3077 Remove (Nod);
3078 Set_Else_Statements (Ret, Then_Statements (Nod));
3079 end;
3080 end if;
3081 end if;
3082
3083 return
3084 Make_Subprogram_Body (Loc,
3085 Specification => Spec,
3086 Declarations => Decls,
3087 Handled_Statement_Sequence =>
3088 Make_Handled_Sequence_Of_Statements (Loc,
3089 Statements => New_List (Ret)));
3090 end Build_Find_Body_Index;
3091
3092 --------------------------------
3093 -- Build_Find_Body_Index_Spec --
3094 --------------------------------
3095
3096 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
3097 Loc : constant Source_Ptr := Sloc (Typ);
3098 Id : constant Entity_Id :=
3099 Make_Defining_Identifier (Loc,
3100 Chars => New_External_Name (Chars (Typ), 'F'));
3101 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
3102 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
3103
3104 begin
3105 return
3106 Make_Function_Specification (Loc,
3107 Defining_Unit_Name => Id,
3108 Parameter_Specifications => New_List (
3109 Make_Parameter_Specification (Loc,
3110 Defining_Identifier => Parm1,
3111 Parameter_Type =>
3112 New_Occurrence_Of (RTE (RE_Address), Loc)),
3113
3114 Make_Parameter_Specification (Loc,
3115 Defining_Identifier => Parm2,
3116 Parameter_Type =>
3117 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
3118
3119 Result_Definition => New_Occurrence_Of (
3120 RTE (RE_Protected_Entry_Index), Loc));
3121 end Build_Find_Body_Index_Spec;
3122
3123 -----------------------------------------------
3124 -- Build_Lock_Free_Protected_Subprogram_Body --
3125 -----------------------------------------------
3126
3127 function Build_Lock_Free_Protected_Subprogram_Body
3128 (N : Node_Id;
3129 Prot_Typ : Node_Id;
3130 Unprot_Spec : Node_Id) return Node_Id
3131 is
3132 Actuals : constant List_Id := New_List;
3133 Loc : constant Source_Ptr := Sloc (N);
3134 Spec : constant Node_Id := Specification (N);
3135 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3136 Formal : Node_Id;
3137 Prot_Spec : Node_Id;
3138 Stmt : Node_Id;
3139
3140 begin
3141 -- Create the protected version of the body
3142
3143 Prot_Spec :=
3144 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3145
3146 -- Build the actual parameters which appear in the call to the
3147 -- unprotected version of the body.
3148
3149 Formal := First (Parameter_Specifications (Prot_Spec));
3150 while Present (Formal) loop
3151 Append_To (Actuals,
3152 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3153
3154 Next (Formal);
3155 end loop;
3156
3157 -- Function case, generate:
3158 -- return <Unprot_Func_Call>;
3159
3160 if Nkind (Spec) = N_Function_Specification then
3161 Stmt :=
3162 Make_Simple_Return_Statement (Loc,
3163 Expression =>
3164 Make_Function_Call (Loc,
3165 Name =>
3166 Make_Identifier (Loc, Chars (Unprot_Id)),
3167 Parameter_Associations => Actuals));
3168
3169 -- Procedure case, call the unprotected version
3170
3171 else
3172 Stmt :=
3173 Make_Procedure_Call_Statement (Loc,
3174 Name =>
3175 Make_Identifier (Loc, Chars (Unprot_Id)),
3176 Parameter_Associations => Actuals);
3177 end if;
3178
3179 return
3180 Make_Subprogram_Body (Loc,
3181 Declarations => Empty_List,
3182 Specification => Prot_Spec,
3183 Handled_Statement_Sequence =>
3184 Make_Handled_Sequence_Of_Statements (Loc,
3185 Statements => New_List (Stmt)));
3186 end Build_Lock_Free_Protected_Subprogram_Body;
3187
3188 -------------------------------------------------
3189 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3190 -------------------------------------------------
3191
3192 -- Procedures which meet the lock-free implementation requirements and
3193 -- reference a unique scalar component Comp are expanded in the following
3194 -- manner:
3195
3196 -- procedure P (...) is
3197 -- Expected_Comp : constant Comp_Type :=
3198 -- Comp_Type
3199 -- (System.Atomic_Primitives.Lock_Free_Read_N
3200 -- (_Object.Comp'Address));
3201 -- begin
3202 -- loop
3203 -- declare
3204 -- <original declarations before the object renaming declaration
3205 -- of Comp>
3206 --
3207 -- Desired_Comp : Comp_Type := Expected_Comp;
3208 -- Comp : Comp_Type renames Desired_Comp;
3209 --
3210 -- <original delarations after the object renaming declaration
3211 -- of Comp>
3212 --
3213 -- begin
3214 -- <original statements>
3215 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3216 -- (_Object.Comp'Address,
3217 -- Interfaces.Unsigned_N (Expected_Comp),
3218 -- Interfaces.Unsigned_N (Desired_Comp));
3219 -- end;
3220 -- end loop;
3221 -- end P;
3222
3223 -- Each return and raise statement of P is transformed into an atomic
3224 -- status check:
3225
3226 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3227 -- (_Object.Comp'Address,
3228 -- Interfaces.Unsigned_N (Expected_Comp),
3229 -- Interfaces.Unsigned_N (Desired_Comp));
3230 -- then
3231 -- <original statement>
3232 -- else
3233 -- goto L0;
3234 -- end if;
3235
3236 -- Functions which meet the lock-free implementation requirements and
3237 -- reference a unique scalar component Comp are expanded in the following
3238 -- manner:
3239
3240 -- function F (...) return ... is
3241 -- <original declarations before the object renaming declaration
3242 -- of Comp>
3243 --
3244 -- Expected_Comp : constant Comp_Type :=
3245 -- Comp_Type
3246 -- (System.Atomic_Primitives.Lock_Free_Read_N
3247 -- (_Object.Comp'Address));
3248 -- Comp : Comp_Type renames Expected_Comp;
3249 --
3250 -- <original delarations after the object renaming declaration of
3251 -- Comp>
3252 --
3253 -- begin
3254 -- <original statements>
3255 -- end F;
3256
3257 function Build_Lock_Free_Unprotected_Subprogram_Body
3258 (N : Node_Id;
3259 Prot_Typ : Node_Id) return Node_Id
3260 is
3261 function Referenced_Component (N : Node_Id) return Entity_Id;
3262 -- Subprograms which meet the lock-free implementation criteria are
3263 -- allowed to reference only one unique component. Return the prival
3264 -- of the said component.
3265
3266 --------------------------
3267 -- Referenced_Component --
3268 --------------------------
3269
3270 function Referenced_Component (N : Node_Id) return Entity_Id is
3271 Comp : Entity_Id;
3272 Decl : Node_Id;
3273 Source_Comp : Entity_Id := Empty;
3274
3275 begin
3276 -- Find the unique source component which N references in its
3277 -- statements.
3278
3279 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3280 declare
3281 Element : Lock_Free_Subprogram renames
3282 Lock_Free_Subprogram_Table.Table (Index);
3283 begin
3284 if Element.Sub_Body = N then
3285 Source_Comp := Element.Comp_Id;
3286 exit;
3287 end if;
3288 end;
3289 end loop;
3290
3291 if No (Source_Comp) then
3292 return Empty;
3293 end if;
3294
3295 -- Find the prival which corresponds to the source component within
3296 -- the declarations of N.
3297
3298 Decl := First (Declarations (N));
3299 while Present (Decl) loop
3300
3301 -- Privals appear as object renamings
3302
3303 if Nkind (Decl) = N_Object_Renaming_Declaration then
3304 Comp := Defining_Identifier (Decl);
3305
3306 if Present (Prival_Link (Comp))
3307 and then Prival_Link (Comp) = Source_Comp
3308 then
3309 return Comp;
3310 end if;
3311 end if;
3312
3313 Next (Decl);
3314 end loop;
3315
3316 return Empty;
3317 end Referenced_Component;
3318
3319 -- Local variables
3320
3321 Comp : constant Entity_Id := Referenced_Component (N);
3322 Loc : constant Source_Ptr := Sloc (N);
3323 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3324 Decls : List_Id := Declarations (N);
3325
3326 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3327
3328 begin
3329 -- Add renamings for the protection object, discriminals, privals, and
3330 -- the entry index constant for use by debugger.
3331
3332 Debug_Private_Data_Declarations (Decls);
3333
3334 -- Perform the lock-free expansion when the subprogram references a
3335 -- protected component.
3336
3337 if Present (Comp) then
3338 Protected_Component_Ref : declare
3339 Comp_Decl : constant Node_Id := Parent (Comp);
3340 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3341 Comp_Type : constant Entity_Id := Etype (Comp);
3342
3343 Is_Procedure : constant Boolean :=
3344 Ekind (Corresponding_Spec (N)) = E_Procedure;
3345 -- Indicates if N is a protected procedure body
3346
3347 Block_Decls : List_Id;
3348 Try_Write : Entity_Id;
3349 Desired_Comp : Entity_Id;
3350 Decl : Node_Id;
3351 Label : Node_Id;
3352 Label_Id : Entity_Id := Empty;
3353 Read : Entity_Id;
3354 Expected_Comp : Entity_Id;
3355 Stmt : Node_Id;
3356 Stmts : List_Id :=
3357 New_Copy_List (Statements (Hand_Stmt_Seq));
3358 Typ_Size : Int;
3359 Unsigned : Entity_Id;
3360
3361 function Process_Node (N : Node_Id) return Traverse_Result;
3362 -- Transform a single node if it is a return statement, a raise
3363 -- statement or a reference to Comp.
3364
3365 procedure Process_Stmts (Stmts : List_Id);
3366 -- Given a statement sequence Stmts, wrap any return or raise
3367 -- statements in the following manner:
3368 --
3369 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3370 -- (_Object.Comp'Address,
3371 -- Interfaces.Unsigned_N (Expected_Comp),
3372 -- Interfaces.Unsigned_N (Desired_Comp))
3373 -- then
3374 -- <Stmt>;
3375 -- else
3376 -- goto L0;
3377 -- end if;
3378
3379 ------------------
3380 -- Process_Node --
3381 ------------------
3382
3383 function Process_Node (N : Node_Id) return Traverse_Result is
3384
3385 procedure Wrap_Statement (Stmt : Node_Id);
3386 -- Wrap an arbitrary statement inside an if statement where the
3387 -- condition does an atomic check on the state of the object.
3388
3389 --------------------
3390 -- Wrap_Statement --
3391 --------------------
3392
3393 procedure Wrap_Statement (Stmt : Node_Id) is
3394 begin
3395 -- The first time through, create the declaration of a label
3396 -- which is used to skip the remainder of source statements
3397 -- if the state of the object has changed.
3398
3399 if No (Label_Id) then
3400 Label_Id :=
3401 Make_Identifier (Loc, New_External_Name ('L', 0));
3402 Set_Entity (Label_Id,
3403 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3404 end if;
3405
3406 -- Generate:
3407 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3408 -- (_Object.Comp'Address,
3409 -- Interfaces.Unsigned_N (Expected_Comp),
3410 -- Interfaces.Unsigned_N (Desired_Comp))
3411 -- then
3412 -- <Stmt>;
3413 -- else
3414 -- goto L0;
3415 -- end if;
3416
3417 Rewrite (Stmt,
3418 Make_Implicit_If_Statement (N,
3419 Condition =>
3420 Make_Function_Call (Loc,
3421 Name =>
3422 New_Occurrence_Of (Try_Write, Loc),
3423 Parameter_Associations => New_List (
3424 Make_Attribute_Reference (Loc,
3425 Prefix => Relocate_Node (Comp_Sel_Nam),
3426 Attribute_Name => Name_Address),
3427
3428 Unchecked_Convert_To (Unsigned,
3429 New_Occurrence_Of (Expected_Comp, Loc)),
3430
3431 Unchecked_Convert_To (Unsigned,
3432 New_Occurrence_Of (Desired_Comp, Loc)))),
3433
3434 Then_Statements => New_List (Relocate_Node (Stmt)),
3435
3436 Else_Statements => New_List (
3437 Make_Goto_Statement (Loc,
3438 Name =>
3439 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3440 end Wrap_Statement;
3441
3442 -- Start of processing for Process_Node
3443
3444 begin
3445 -- Wrap each return and raise statement that appear inside a
3446 -- procedure. Skip the last return statement which is added by
3447 -- default since it is transformed into an exit statement.
3448
3449 if Is_Procedure
3450 and then ((Nkind (N) = N_Simple_Return_Statement
3451 and then N /= Last (Stmts))
3452 or else Nkind (N) = N_Extended_Return_Statement
3453 or else (Nkind_In (N, N_Raise_Constraint_Error,
3454 N_Raise_Program_Error,
3455 N_Raise_Statement,
3456 N_Raise_Storage_Error)
3457 and then Comes_From_Source (N)))
3458 then
3459 Wrap_Statement (N);
3460 return Skip;
3461 end if;
3462
3463 -- Force reanalysis
3464
3465 Set_Analyzed (N, False);
3466
3467 return OK;
3468 end Process_Node;
3469
3470 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3471
3472 -------------------
3473 -- Process_Stmts --
3474 -------------------
3475
3476 procedure Process_Stmts (Stmts : List_Id) is
3477 Stmt : Node_Id;
3478 begin
3479 Stmt := First (Stmts);
3480 while Present (Stmt) loop
3481 Process_Nodes (Stmt);
3482 Next (Stmt);
3483 end loop;
3484 end Process_Stmts;
3485
3486 -- Start of processing for Protected_Component_Ref
3487
3488 begin
3489 -- Get the type size
3490
3491 if Known_Static_Esize (Comp_Type) then
3492 Typ_Size := UI_To_Int (Esize (Comp_Type));
3493
3494 -- If the Esize (Object_Size) is unknown at compile time, look at
3495 -- the RM_Size (Value_Size) since it may have been set by an
3496 -- explicit representation clause.
3497
3498 elsif Known_Static_RM_Size (Comp_Type) then
3499 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3500
3501 -- Should not happen since this has already been checked in
3502 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3503
3504 else
3505 raise Program_Error;
3506 end if;
3507
3508 -- Retrieve all relevant atomic routines and types
3509
3510 case Typ_Size is
3511 when 8 =>
3512 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3513 Read := RTE (RE_Lock_Free_Read_8);
3514 Unsigned := RTE (RE_Uint8);
3515
3516 when 16 =>
3517 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3518 Read := RTE (RE_Lock_Free_Read_16);
3519 Unsigned := RTE (RE_Uint16);
3520
3521 when 32 =>
3522 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3523 Read := RTE (RE_Lock_Free_Read_32);
3524 Unsigned := RTE (RE_Uint32);
3525
3526 when 64 =>
3527 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3528 Read := RTE (RE_Lock_Free_Read_64);
3529 Unsigned := RTE (RE_Uint64);
3530
3531 when others =>
3532 raise Program_Error;
3533 end case;
3534
3535 -- Generate:
3536 -- Expected_Comp : constant Comp_Type :=
3537 -- Comp_Type
3538 -- (System.Atomic_Primitives.Lock_Free_Read_N
3539 -- (_Object.Comp'Address));
3540
3541 Expected_Comp :=
3542 Make_Defining_Identifier (Loc,
3543 New_External_Name (Chars (Comp), Suffix => "_saved"));
3544
3545 Decl :=
3546 Make_Object_Declaration (Loc,
3547 Defining_Identifier => Expected_Comp,
3548 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3549 Constant_Present => True,
3550 Expression =>
3551 Unchecked_Convert_To (Comp_Type,
3552 Make_Function_Call (Loc,
3553 Name => New_Occurrence_Of (Read, Loc),
3554 Parameter_Associations => New_List (
3555 Make_Attribute_Reference (Loc,
3556 Prefix => Relocate_Node (Comp_Sel_Nam),
3557 Attribute_Name => Name_Address)))));
3558
3559 -- Protected procedures
3560
3561 if Is_Procedure then
3562 -- Move the original declarations inside the generated block
3563
3564 Block_Decls := Decls;
3565
3566 -- Reset the declarations list of the protected procedure to
3567 -- contain only Decl.
3568
3569 Decls := New_List (Decl);
3570
3571 -- Generate:
3572 -- Desired_Comp : Comp_Type := Expected_Comp;
3573
3574 Desired_Comp :=
3575 Make_Defining_Identifier (Loc,
3576 New_External_Name (Chars (Comp), Suffix => "_current"));
3577
3578 -- Insert the declarations of Expected_Comp and Desired_Comp in
3579 -- the block declarations right before the renaming of the
3580 -- protected component.
3581
3582 Insert_Before (Comp_Decl,
3583 Make_Object_Declaration (Loc,
3584 Defining_Identifier => Desired_Comp,
3585 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3586 Expression =>
3587 New_Occurrence_Of (Expected_Comp, Loc)));
3588
3589 -- Protected function
3590
3591 else
3592 Desired_Comp := Expected_Comp;
3593
3594 -- Insert the declaration of Expected_Comp in the function
3595 -- declarations right before the renaming of the protected
3596 -- component.
3597
3598 Insert_Before (Comp_Decl, Decl);
3599 end if;
3600
3601 -- Rewrite the protected component renaming declaration to be a
3602 -- renaming of Desired_Comp.
3603
3604 -- Generate:
3605 -- Comp : Comp_Type renames Desired_Comp;
3606
3607 Rewrite (Comp_Decl,
3608 Make_Object_Renaming_Declaration (Loc,
3609 Defining_Identifier =>
3610 Defining_Identifier (Comp_Decl),
3611 Subtype_Mark =>
3612 New_Occurrence_Of (Comp_Type, Loc),
3613 Name =>
3614 New_Occurrence_Of (Desired_Comp, Loc)));
3615
3616 -- Wrap any return or raise statements in Stmts in same the manner
3617 -- described in Process_Stmts.
3618
3619 Process_Stmts (Stmts);
3620
3621 -- Generate:
3622 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3623 -- (_Object.Comp'Address,
3624 -- Interfaces.Unsigned_N (Expected_Comp),
3625 -- Interfaces.Unsigned_N (Desired_Comp))
3626
3627 if Is_Procedure then
3628 Stmt :=
3629 Make_Exit_Statement (Loc,
3630 Condition =>
3631 Make_Function_Call (Loc,
3632 Name =>
3633 New_Occurrence_Of (Try_Write, Loc),
3634 Parameter_Associations => New_List (
3635 Make_Attribute_Reference (Loc,
3636 Prefix => Relocate_Node (Comp_Sel_Nam),
3637 Attribute_Name => Name_Address),
3638
3639 Unchecked_Convert_To (Unsigned,
3640 New_Occurrence_Of (Expected_Comp, Loc)),
3641
3642 Unchecked_Convert_To (Unsigned,
3643 New_Occurrence_Of (Desired_Comp, Loc)))));
3644
3645 -- Small optimization: transform the default return statement
3646 -- of a procedure into the atomic exit statement.
3647
3648 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3649 Rewrite (Last (Stmts), Stmt);
3650 else
3651 Append_To (Stmts, Stmt);
3652 end if;
3653 end if;
3654
3655 -- Create the declaration of the label used to skip the rest of
3656 -- the source statements when the object state changes.
3657
3658 if Present (Label_Id) then
3659 Label := Make_Label (Loc, Label_Id);
3660 Append_To (Decls,
3661 Make_Implicit_Label_Declaration (Loc,
3662 Defining_Identifier => Entity (Label_Id),
3663 Label_Construct => Label));
3664 Append_To (Stmts, Label);
3665 end if;
3666
3667 -- Generate:
3668 -- loop
3669 -- declare
3670 -- <Decls>
3671 -- begin
3672 -- <Stmts>
3673 -- end;
3674 -- end loop;
3675
3676 if Is_Procedure then
3677 Stmts :=
3678 New_List (
3679 Make_Loop_Statement (Loc,
3680 Statements => New_List (
3681 Make_Block_Statement (Loc,
3682 Declarations => Block_Decls,
3683 Handled_Statement_Sequence =>
3684 Make_Handled_Sequence_Of_Statements (Loc,
3685 Statements => Stmts))),
3686 End_Label => Empty));
3687 end if;
3688
3689 Hand_Stmt_Seq :=
3690 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3691 end Protected_Component_Ref;
3692 end if;
3693
3694 -- Make an unprotected version of the subprogram for use within the same
3695 -- object, with new name and extra parameter representing the object.
3696
3697 return
3698 Make_Subprogram_Body (Loc,
3699 Specification =>
3700 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3701 Declarations => Decls,
3702 Handled_Statement_Sequence => Hand_Stmt_Seq);
3703 end Build_Lock_Free_Unprotected_Subprogram_Body;
3704
3705 -------------------------
3706 -- Build_Master_Entity --
3707 -------------------------
3708
3709 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3710 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3711 Context : Node_Id;
3712 Context_Id : Entity_Id;
3713 Decl : Node_Id;
3714 Decls : List_Id;
3715 Par : Node_Id;
3716
3717 begin
3718 if Is_Itype (Obj_Or_Typ) then
3719 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3720 else
3721 Par := Parent (Obj_Or_Typ);
3722 end if;
3723
3724 -- When creating a master for a record component which is either a task
3725 -- or access-to-task, the enclosing record is the master scope and the
3726 -- proper insertion point is the component list.
3727
3728 if Is_Record_Type (Current_Scope) then
3729 Context := Par;
3730 Context_Id := Current_Scope;
3731 Decls := List_Containing (Context);
3732
3733 -- Default case for object declarations and access types. Note that the
3734 -- context is updated to the nearest enclosing body, block, package, or
3735 -- return statement.
3736
3737 else
3738 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3739 end if;
3740
3741 -- Do not create a master if one already exists or there is no task
3742 -- hierarchy.
3743
3744 if Has_Master_Entity (Context_Id)
3745 or else Restriction_Active (No_Task_Hierarchy)
3746 then
3747 return;
3748 end if;
3749
3750 -- Create a master, generate:
3751 -- _Master : constant Master_Id := Current_Master.all;
3752
3753 Decl :=
3754 Make_Object_Declaration (Loc,
3755 Defining_Identifier =>
3756 Make_Defining_Identifier (Loc, Name_uMaster),
3757 Constant_Present => True,
3758 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3759 Expression =>
3760 Make_Explicit_Dereference (Loc,
3761 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3762
3763 -- The master is inserted at the start of the declarative list of the
3764 -- context.
3765
3766 Prepend_To (Decls, Decl);
3767
3768 -- In certain cases where transient scopes are involved, the immediate
3769 -- scope is not always the proper master scope. Ensure that the master
3770 -- declaration and entity appear in the same context.
3771
3772 if Context_Id /= Current_Scope then
3773 Push_Scope (Context_Id);
3774 Analyze (Decl);
3775 Pop_Scope;
3776 else
3777 Analyze (Decl);
3778 end if;
3779
3780 -- Mark the enclosing scope and its associated construct as being task
3781 -- masters.
3782
3783 Set_Has_Master_Entity (Context_Id);
3784
3785 while Present (Context)
3786 and then Nkind (Context) /= N_Compilation_Unit
3787 loop
3788 if Nkind_In (Context, N_Block_Statement,
3789 N_Subprogram_Body,
3790 N_Task_Body)
3791 then
3792 Set_Is_Task_Master (Context);
3793 exit;
3794
3795 elsif Nkind (Parent (Context)) = N_Subunit then
3796 Context := Corresponding_Stub (Parent (Context));
3797 end if;
3798
3799 Context := Parent (Context);
3800 end loop;
3801 end Build_Master_Entity;
3802
3803 ---------------------------
3804 -- Build_Master_Renaming --
3805 ---------------------------
3806
3807 procedure Build_Master_Renaming
3808 (Ptr_Typ : Entity_Id;
3809 Ins_Nod : Node_Id := Empty)
3810 is
3811 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3812 Context : Node_Id;
3813 Master_Decl : Node_Id;
3814 Master_Id : Entity_Id;
3815
3816 begin
3817 -- Nothing to do if there is no task hierarchy
3818
3819 if Restriction_Active (No_Task_Hierarchy) then
3820 return;
3821 end if;
3822
3823 -- Determine the proper context to insert the master renaming
3824
3825 if Present (Ins_Nod) then
3826 Context := Ins_Nod;
3827 elsif Is_Itype (Ptr_Typ) then
3828 Context := Associated_Node_For_Itype (Ptr_Typ);
3829 else
3830 Context := Parent (Ptr_Typ);
3831 end if;
3832
3833 -- Generate:
3834 -- <Ptr_Typ>M : Master_Id renames _Master;
3835
3836 Master_Id :=
3837 Make_Defining_Identifier (Loc,
3838 New_External_Name (Chars (Ptr_Typ), 'M'));
3839
3840 Master_Decl :=
3841 Make_Object_Renaming_Declaration (Loc,
3842 Defining_Identifier => Master_Id,
3843 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3844 Name => Make_Identifier (Loc, Name_uMaster));
3845
3846 Insert_Action (Context, Master_Decl);
3847
3848 -- The renamed master now services the access type
3849
3850 Set_Master_Id (Ptr_Typ, Master_Id);
3851 end Build_Master_Renaming;
3852
3853 -----------------------------------------
3854 -- Build_Private_Protected_Declaration --
3855 -----------------------------------------
3856
3857 function Build_Private_Protected_Declaration
3858 (N : Node_Id) return Entity_Id
3859 is
3860 Loc : constant Source_Ptr := Sloc (N);
3861 Body_Id : constant Entity_Id := Defining_Entity (N);
3862 Decl : Node_Id;
3863 Plist : List_Id;
3864 Formal : Entity_Id;
3865 New_Spec : Node_Id;
3866 Spec_Id : Entity_Id;
3867
3868 begin
3869 Formal := First_Formal (Body_Id);
3870
3871 -- The protected operation always has at least one formal, namely the
3872 -- object itself, but it is only placed in the parameter list if
3873 -- expansion is enabled.
3874
3875 if Present (Formal) or else Expander_Active then
3876 Plist := Copy_Parameter_List (Body_Id);
3877 else
3878 Plist := No_List;
3879 end if;
3880
3881 if Nkind (Specification (N)) = N_Procedure_Specification then
3882 New_Spec :=
3883 Make_Procedure_Specification (Loc,
3884 Defining_Unit_Name =>
3885 Make_Defining_Identifier (Sloc (Body_Id),
3886 Chars => Chars (Body_Id)),
3887 Parameter_Specifications =>
3888 Plist);
3889 else
3890 New_Spec :=
3891 Make_Function_Specification (Loc,
3892 Defining_Unit_Name =>
3893 Make_Defining_Identifier (Sloc (Body_Id),
3894 Chars => Chars (Body_Id)),
3895 Parameter_Specifications => Plist,
3896 Result_Definition =>
3897 New_Occurrence_Of (Etype (Body_Id), Loc));
3898 end if;
3899
3900 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3901 Insert_Before (N, Decl);
3902 Spec_Id := Defining_Unit_Name (New_Spec);
3903
3904 -- Indicate that the entity comes from source, to ensure that cross-
3905 -- reference information is properly generated. The body itself is
3906 -- rewritten during expansion, and the body entity will not appear in
3907 -- calls to the operation.
3908
3909 Set_Comes_From_Source (Spec_Id, True);
3910 Analyze (Decl);
3911 Set_Has_Completion (Spec_Id);
3912 Set_Convention (Spec_Id, Convention_Protected);
3913 return Spec_Id;
3914 end Build_Private_Protected_Declaration;
3915
3916 ---------------------------
3917 -- Build_Protected_Entry --
3918 ---------------------------
3919
3920 function Build_Protected_Entry
3921 (N : Node_Id;
3922 Ent : Entity_Id;
3923 Pid : Node_Id) return Node_Id
3924 is
3925 Bod_Decls : constant List_Id := New_List;
3926 Decls : constant List_Id := Declarations (N);
3927 End_Lab : constant Node_Id :=
3928 End_Label (Handled_Statement_Sequence (N));
3929 End_Loc : constant Source_Ptr :=
3930 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3931 -- Used for the generated call to Complete_Entry_Body
3932
3933 Loc : constant Source_Ptr := Sloc (N);
3934
3935 Bod_Id : Entity_Id;
3936 Bod_Spec : Node_Id;
3937 Bod_Stmts : List_Id;
3938 Complete : Node_Id;
3939 Ohandle : Node_Id;
3940
3941 EH_Loc : Source_Ptr;
3942 -- Used for the exception handler, inserted at end of the body
3943
3944 begin
3945 -- Set the source location on the exception handler only when debugging
3946 -- the expanded code (see Make_Implicit_Exception_Handler).
3947
3948 if Debug_Generated_Code then
3949 EH_Loc := End_Loc;
3950
3951 -- Otherwise the inserted code should not be visible to the debugger
3952
3953 else
3954 EH_Loc := No_Location;
3955 end if;
3956
3957 Bod_Id :=
3958 Make_Defining_Identifier (Loc,
3959 Chars => Chars (Protected_Body_Subprogram (Ent)));
3960 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3961
3962 -- Add the following declarations:
3963
3964 -- type poVP is access poV;
3965 -- _object : poVP := poVP (_O);
3966
3967 -- where _O is the formal parameter associated with the concurrent
3968 -- object. These declarations are needed for Complete_Entry_Body.
3969
3970 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3971
3972 -- Add renamings for all formals, the Protection object, discriminals,
3973 -- privals and the entry index constant for use by debugger.
3974
3975 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3976 Debug_Private_Data_Declarations (Decls);
3977
3978 -- Put the declarations and the statements from the entry
3979
3980 Bod_Stmts :=
3981 New_List (
3982 Make_Block_Statement (Loc,
3983 Declarations => Decls,
3984 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3985
3986 case Corresponding_Runtime_Package (Pid) is
3987 when System_Tasking_Protected_Objects_Entries =>
3988 Append_To (Bod_Stmts,
3989 Make_Procedure_Call_Statement (End_Loc,
3990 Name =>
3991 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3992 Parameter_Associations => New_List (
3993 Make_Attribute_Reference (End_Loc,
3994 Prefix =>
3995 Make_Selected_Component (End_Loc,
3996 Prefix =>
3997 Make_Identifier (End_Loc, Name_uObject),
3998 Selector_Name =>
3999 Make_Identifier (End_Loc, Name_uObject)),
4000 Attribute_Name => Name_Unchecked_Access))));
4001
4002 when System_Tasking_Protected_Objects_Single_Entry =>
4003
4004 -- Historically, a call to Complete_Single_Entry_Body was
4005 -- inserted, but it was a null procedure.
4006
4007 null;
4008
4009 when others =>
4010 raise Program_Error;
4011 end case;
4012
4013 -- When exceptions can not be propagated, we never need to call
4014 -- Exception_Complete_Entry_Body.
4015
4016 if No_Exception_Handlers_Set then
4017 return
4018 Make_Subprogram_Body (Loc,
4019 Specification => Bod_Spec,
4020 Declarations => Bod_Decls,
4021 Handled_Statement_Sequence =>
4022 Make_Handled_Sequence_Of_Statements (Loc,
4023 Statements => Bod_Stmts,
4024 End_Label => End_Lab));
4025
4026 else
4027 Ohandle := Make_Others_Choice (Loc);
4028 Set_All_Others (Ohandle);
4029
4030 case Corresponding_Runtime_Package (Pid) is
4031 when System_Tasking_Protected_Objects_Entries =>
4032 Complete :=
4033 New_Occurrence_Of
4034 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
4035
4036 when System_Tasking_Protected_Objects_Single_Entry =>
4037 Complete :=
4038 New_Occurrence_Of
4039 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
4040
4041 when others =>
4042 raise Program_Error;
4043 end case;
4044
4045 -- Establish link between subprogram body entity and source entry
4046
4047 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
4048
4049 -- Create body of entry procedure. The renaming declarations are
4050 -- placed ahead of the block that contains the actual entry body.
4051
4052 return
4053 Make_Subprogram_Body (Loc,
4054 Specification => Bod_Spec,
4055 Declarations => Bod_Decls,
4056 Handled_Statement_Sequence =>
4057 Make_Handled_Sequence_Of_Statements (Loc,
4058 Statements => Bod_Stmts,
4059 End_Label => End_Lab,
4060 Exception_Handlers => New_List (
4061 Make_Implicit_Exception_Handler (EH_Loc,
4062 Exception_Choices => New_List (Ohandle),
4063
4064 Statements => New_List (
4065 Make_Procedure_Call_Statement (EH_Loc,
4066 Name => Complete,
4067 Parameter_Associations => New_List (
4068 Make_Attribute_Reference (EH_Loc,
4069 Prefix =>
4070 Make_Selected_Component (EH_Loc,
4071 Prefix =>
4072 Make_Identifier (EH_Loc, Name_uObject),
4073 Selector_Name =>
4074 Make_Identifier (EH_Loc, Name_uObject)),
4075 Attribute_Name => Name_Unchecked_Access),
4076
4077 Make_Function_Call (EH_Loc,
4078 Name =>
4079 New_Occurrence_Of
4080 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
4081 end if;
4082 end Build_Protected_Entry;
4083
4084 -----------------------------------------
4085 -- Build_Protected_Entry_Specification --
4086 -----------------------------------------
4087
4088 function Build_Protected_Entry_Specification
4089 (Loc : Source_Ptr;
4090 Def_Id : Entity_Id;
4091 Ent_Id : Entity_Id) return Node_Id
4092 is
4093 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
4094
4095 begin
4096 Set_Debug_Info_Needed (Def_Id);
4097
4098 if Present (Ent_Id) then
4099 Append_Elmt (P, Accept_Address (Ent_Id));
4100 end if;
4101
4102 return
4103 Make_Procedure_Specification (Loc,
4104 Defining_Unit_Name => Def_Id,
4105 Parameter_Specifications => New_List (
4106 Make_Parameter_Specification (Loc,
4107 Defining_Identifier =>
4108 Make_Defining_Identifier (Loc, Name_uO),
4109 Parameter_Type =>
4110 New_Occurrence_Of (RTE (RE_Address), Loc)),
4111
4112 Make_Parameter_Specification (Loc,
4113 Defining_Identifier => P,
4114 Parameter_Type =>
4115 New_Occurrence_Of (RTE (RE_Address), Loc)),
4116
4117 Make_Parameter_Specification (Loc,
4118 Defining_Identifier =>
4119 Make_Defining_Identifier (Loc, Name_uE),
4120 Parameter_Type =>
4121 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
4122 end Build_Protected_Entry_Specification;
4123
4124 --------------------------
4125 -- Build_Protected_Spec --
4126 --------------------------
4127
4128 function Build_Protected_Spec
4129 (N : Node_Id;
4130 Obj_Type : Entity_Id;
4131 Ident : Entity_Id;
4132 Unprotected : Boolean := False) return List_Id
4133 is
4134 Loc : constant Source_Ptr := Sloc (N);
4135 Decl : Node_Id;
4136 Formal : Entity_Id;
4137 New_Plist : List_Id;
4138 New_Param : Node_Id;
4139
4140 begin
4141 New_Plist := New_List;
4142
4143 Formal := First_Formal (Ident);
4144 while Present (Formal) loop
4145 New_Param :=
4146 Make_Parameter_Specification (Loc,
4147 Defining_Identifier =>
4148 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4149 Aliased_Present => Aliased_Present (Parent (Formal)),
4150 In_Present => In_Present (Parent (Formal)),
4151 Out_Present => Out_Present (Parent (Formal)),
4152 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
4153
4154 if Unprotected then
4155 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4156 end if;
4157
4158 Append (New_Param, New_Plist);
4159 Next_Formal (Formal);
4160 end loop;
4161
4162 -- If the subprogram is a procedure and the context is not an access
4163 -- to protected subprogram, the parameter is in-out. Otherwise it is
4164 -- an in parameter.
4165
4166 Decl :=
4167 Make_Parameter_Specification (Loc,
4168 Defining_Identifier =>
4169 Make_Defining_Identifier (Loc, Name_uObject),
4170 In_Present => True,
4171 Out_Present =>
4172 (Etype (Ident) = Standard_Void_Type
4173 and then not Is_RTE (Obj_Type, RE_Address)),
4174 Parameter_Type =>
4175 New_Occurrence_Of (Obj_Type, Loc));
4176 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4177 Prepend_To (New_Plist, Decl);
4178
4179 return New_Plist;
4180 end Build_Protected_Spec;
4181
4182 ---------------------------------------
4183 -- Build_Protected_Sub_Specification --
4184 ---------------------------------------
4185
4186 function Build_Protected_Sub_Specification
4187 (N : Node_Id;
4188 Prot_Typ : Entity_Id;
4189 Mode : Subprogram_Protection_Mode) return Node_Id
4190 is
4191 Loc : constant Source_Ptr := Sloc (N);
4192 Decl : Node_Id;
4193 Def_Id : Entity_Id;
4194 New_Id : Entity_Id;
4195 New_Plist : List_Id;
4196 New_Spec : Node_Id;
4197
4198 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4199 (Dispatching_Mode => ' ',
4200 Protected_Mode => 'P',
4201 Unprotected_Mode => 'N');
4202
4203 begin
4204 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
4205 then
4206 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4207 else
4208 Decl := N;
4209 end if;
4210
4211 Def_Id := Defining_Unit_Name (Specification (Decl));
4212
4213 New_Plist :=
4214 Build_Protected_Spec
4215 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4216 Mode = Unprotected_Mode);
4217 New_Id :=
4218 Make_Defining_Identifier (Loc,
4219 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4220
4221 -- Reference the original nondispatching subprogram since the analysis
4222 -- of the object.operation notation may need its original name (see
4223 -- Sem_Ch4.Names_Match).
4224
4225 if Mode = Dispatching_Mode then
4226 Set_Ekind (New_Id, Ekind (Def_Id));
4227 Set_Original_Protected_Subprogram (New_Id, Def_Id);
4228 end if;
4229
4230 -- The unprotected operation carries the user code, and debugging
4231 -- information must be generated for it, even though this spec does
4232 -- not come from source. It is also convenient to allow gdb to step
4233 -- into the protected operation, even though it only contains lock/
4234 -- unlock calls.
4235
4236 Set_Debug_Info_Needed (New_Id);
4237
4238 -- If a pragma Eliminate applies to the source entity, the internal
4239 -- subprograms will be eliminated as well.
4240
4241 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4242
4243 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4244 New_Spec :=
4245 Make_Procedure_Specification (Loc,
4246 Defining_Unit_Name => New_Id,
4247 Parameter_Specifications => New_Plist);
4248
4249 -- Create a new specification for the anonymous subprogram type
4250
4251 else
4252 New_Spec :=
4253 Make_Function_Specification (Loc,
4254 Defining_Unit_Name => New_Id,
4255 Parameter_Specifications => New_Plist,
4256 Result_Definition =>
4257 Copy_Result_Type (Result_Definition (Specification (Decl))));
4258
4259 Set_Return_Present (Defining_Unit_Name (New_Spec));
4260 end if;
4261
4262 return New_Spec;
4263 end Build_Protected_Sub_Specification;
4264
4265 -------------------------------------
4266 -- Build_Protected_Subprogram_Body --
4267 -------------------------------------
4268
4269 function Build_Protected_Subprogram_Body
4270 (N : Node_Id;
4271 Pid : Node_Id;
4272 N_Op_Spec : Node_Id) return Node_Id
4273 is
4274 Loc : constant Source_Ptr := Sloc (N);
4275 Op_Spec : Node_Id;
4276 P_Op_Spec : Node_Id;
4277 Uactuals : List_Id;
4278 Pformal : Node_Id;
4279 Unprot_Call : Node_Id;
4280 Sub_Body : Node_Id;
4281 Lock_Name : Node_Id;
4282 Lock_Stmt : Node_Id;
4283 R : Node_Id;
4284 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4285 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4286 Stmts : List_Id;
4287 Object_Parm : Node_Id;
4288 Exc_Safe : Boolean;
4289 Lock_Kind : RE_Id;
4290
4291 begin
4292 Op_Spec := Specification (N);
4293 Exc_Safe := Is_Exception_Safe (N);
4294
4295 P_Op_Spec :=
4296 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4297
4298 -- Build a list of the formal parameters of the protected version of
4299 -- the subprogram to use as the actual parameters of the unprotected
4300 -- version.
4301
4302 Uactuals := New_List;
4303 Pformal := First (Parameter_Specifications (P_Op_Spec));
4304 while Present (Pformal) loop
4305 Append_To (Uactuals,
4306 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4307 Next (Pformal);
4308 end loop;
4309
4310 -- Make a call to the unprotected version of the subprogram built above
4311 -- for use by the protected version built below.
4312
4313 if Nkind (Op_Spec) = N_Function_Specification then
4314 if Exc_Safe then
4315 R := Make_Temporary (Loc, 'R');
4316
4317 Unprot_Call :=
4318 Make_Object_Declaration (Loc,
4319 Defining_Identifier => R,
4320 Constant_Present => True,
4321 Object_Definition =>
4322 New_Copy (Result_Definition (N_Op_Spec)),
4323 Expression =>
4324 Make_Function_Call (Loc,
4325 Name =>
4326 Make_Identifier (Loc,
4327 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4328 Parameter_Associations => Uactuals));
4329
4330 Return_Stmt :=
4331 Make_Simple_Return_Statement (Loc,
4332 Expression => New_Occurrence_Of (R, Loc));
4333
4334 else
4335 Unprot_Call :=
4336 Make_Simple_Return_Statement (Loc,
4337 Expression =>
4338 Make_Function_Call (Loc,
4339 Name =>
4340 Make_Identifier (Loc,
4341 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4342 Parameter_Associations => Uactuals));
4343 end if;
4344
4345 Lock_Kind := RE_Lock_Read_Only;
4346
4347 else
4348 Unprot_Call :=
4349 Make_Procedure_Call_Statement (Loc,
4350 Name =>
4351 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4352 Parameter_Associations => Uactuals);
4353
4354 Lock_Kind := RE_Lock;
4355 end if;
4356
4357 -- Wrap call in block that will be covered by an at_end handler
4358
4359 if not Exc_Safe then
4360 Unprot_Call :=
4361 Make_Block_Statement (Loc,
4362 Handled_Statement_Sequence =>
4363 Make_Handled_Sequence_Of_Statements (Loc,
4364 Statements => New_List (Unprot_Call)));
4365 end if;
4366
4367 -- Make the protected subprogram body. This locks the protected
4368 -- object and calls the unprotected version of the subprogram.
4369
4370 case Corresponding_Runtime_Package (Pid) is
4371 when System_Tasking_Protected_Objects_Entries =>
4372 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
4373
4374 when System_Tasking_Protected_Objects_Single_Entry =>
4375 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4376
4377 when System_Tasking_Protected_Objects =>
4378 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4379
4380 when others =>
4381 raise Program_Error;
4382 end case;
4383
4384 Object_Parm :=
4385 Make_Attribute_Reference (Loc,
4386 Prefix =>
4387 Make_Selected_Component (Loc,
4388 Prefix => Make_Identifier (Loc, Name_uObject),
4389 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4390 Attribute_Name => Name_Unchecked_Access);
4391
4392 Lock_Stmt :=
4393 Make_Procedure_Call_Statement (Loc,
4394 Name => Lock_Name,
4395 Parameter_Associations => New_List (Object_Parm));
4396
4397 if Abort_Allowed then
4398 Stmts := New_List (
4399 Build_Runtime_Call (Loc, RE_Abort_Defer),
4400 Lock_Stmt);
4401
4402 else
4403 Stmts := New_List (Lock_Stmt);
4404 end if;
4405
4406 if not Exc_Safe then
4407 Append (Unprot_Call, Stmts);
4408 else
4409 if Nkind (Op_Spec) = N_Function_Specification then
4410 Pre_Stmts := Stmts;
4411 Stmts := Empty_List;
4412 else
4413 Append (Unprot_Call, Stmts);
4414 end if;
4415
4416 -- Historical note: Previously, call to the cleanup was inserted
4417 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4418 -- which is also shared by the 'not Exc_Safe' path.
4419
4420 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4421
4422 if Nkind (Op_Spec) = N_Function_Specification then
4423 Append_To (Stmts, Return_Stmt);
4424 Append_To (Pre_Stmts,
4425 Make_Block_Statement (Loc,
4426 Declarations => New_List (Unprot_Call),
4427 Handled_Statement_Sequence =>
4428 Make_Handled_Sequence_Of_Statements (Loc,
4429 Statements => Stmts)));
4430 Stmts := Pre_Stmts;
4431 end if;
4432 end if;
4433
4434 Sub_Body :=
4435 Make_Subprogram_Body (Loc,
4436 Declarations => Empty_List,
4437 Specification => P_Op_Spec,
4438 Handled_Statement_Sequence =>
4439 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4440
4441 -- Mark this subprogram as a protected subprogram body so that the
4442 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4443 -- path as otherwise the cleanup has already been inserted.
4444
4445 if not Exc_Safe then
4446 Set_Is_Protected_Subprogram_Body (Sub_Body);
4447 end if;
4448
4449 return Sub_Body;
4450 end Build_Protected_Subprogram_Body;
4451
4452 -------------------------------------
4453 -- Build_Protected_Subprogram_Call --
4454 -------------------------------------
4455
4456 procedure Build_Protected_Subprogram_Call
4457 (N : Node_Id;
4458 Name : Node_Id;
4459 Rec : Node_Id;
4460 External : Boolean := True)
4461 is
4462 Loc : constant Source_Ptr := Sloc (N);
4463 Sub : constant Entity_Id := Entity (Name);
4464 New_Sub : Node_Id;
4465 Params : List_Id;
4466
4467 begin
4468 if External then
4469 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4470 else
4471 New_Sub :=
4472 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4473 end if;
4474
4475 if Present (Parameter_Associations (N)) then
4476 Params := New_Copy_List_Tree (Parameter_Associations (N));
4477 else
4478 Params := New_List;
4479 end if;
4480
4481 -- If the type is an untagged derived type, convert to the root type,
4482 -- which is the one on which the operations are defined.
4483
4484 if Nkind (Rec) = N_Unchecked_Type_Conversion
4485 and then not Is_Tagged_Type (Etype (Rec))
4486 and then Is_Derived_Type (Etype (Rec))
4487 then
4488 Set_Etype (Rec, Root_Type (Etype (Rec)));
4489 Set_Subtype_Mark (Rec,
4490 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4491 end if;
4492
4493 Prepend (Rec, Params);
4494
4495 if Ekind (Sub) = E_Procedure then
4496 Rewrite (N,
4497 Make_Procedure_Call_Statement (Loc,
4498 Name => New_Sub,
4499 Parameter_Associations => Params));
4500
4501 else
4502 pragma Assert (Ekind (Sub) = E_Function);
4503 Rewrite (N,
4504 Make_Function_Call (Loc,
4505 Name => New_Sub,
4506 Parameter_Associations => Params));
4507
4508 -- Preserve type of call for subsequent processing (required for
4509 -- call to Wrap_Transient_Expression in the case of a shared passive
4510 -- protected).
4511
4512 Set_Etype (N, Etype (New_Sub));
4513 end if;
4514
4515 if External
4516 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4517 and then Is_Entity_Name (Expression (Rec))
4518 and then Is_Shared_Passive (Entity (Expression (Rec)))
4519 then
4520 Add_Shared_Var_Lock_Procs (N);
4521 end if;
4522 end Build_Protected_Subprogram_Call;
4523
4524 ---------------------------------------------
4525 -- Build_Protected_Subprogram_Call_Cleanup --
4526 ---------------------------------------------
4527
4528 procedure Build_Protected_Subprogram_Call_Cleanup
4529 (Op_Spec : Node_Id;
4530 Conc_Typ : Node_Id;
4531 Loc : Source_Ptr;
4532 Stmts : List_Id)
4533 is
4534 Nam : Node_Id;
4535
4536 begin
4537 -- If the associated protected object has entries, a protected
4538 -- procedure has to service entry queues. In this case generate:
4539
4540 -- Service_Entries (_object._object'Access);
4541
4542 if Nkind (Op_Spec) = N_Procedure_Specification
4543 and then Has_Entries (Conc_Typ)
4544 then
4545 case Corresponding_Runtime_Package (Conc_Typ) is
4546 when System_Tasking_Protected_Objects_Entries =>
4547 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4548
4549 when System_Tasking_Protected_Objects_Single_Entry =>
4550 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4551
4552 when others =>
4553 raise Program_Error;
4554 end case;
4555
4556 Append_To (Stmts,
4557 Make_Procedure_Call_Statement (Loc,
4558 Name => Nam,
4559 Parameter_Associations => New_List (
4560 Make_Attribute_Reference (Loc,
4561 Prefix =>
4562 Make_Selected_Component (Loc,
4563 Prefix => Make_Identifier (Loc, Name_uObject),
4564 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4565 Attribute_Name => Name_Unchecked_Access))));
4566
4567 else
4568 -- Generate:
4569 -- Unlock (_object._object'Access);
4570
4571 case Corresponding_Runtime_Package (Conc_Typ) is
4572 when System_Tasking_Protected_Objects_Entries =>
4573 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4574
4575 when System_Tasking_Protected_Objects_Single_Entry =>
4576 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4577
4578 when System_Tasking_Protected_Objects =>
4579 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4580
4581 when others =>
4582 raise Program_Error;
4583 end case;
4584
4585 Append_To (Stmts,
4586 Make_Procedure_Call_Statement (Loc,
4587 Name => Nam,
4588 Parameter_Associations => New_List (
4589 Make_Attribute_Reference (Loc,
4590 Prefix =>
4591 Make_Selected_Component (Loc,
4592 Prefix => Make_Identifier (Loc, Name_uObject),
4593 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4594 Attribute_Name => Name_Unchecked_Access))));
4595 end if;
4596
4597 -- Generate:
4598 -- Abort_Undefer;
4599
4600 if Abort_Allowed then
4601 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
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 T : constant Entity_Id := Defining_Identifier (N);
6233 D_T : constant Entity_Id := Designated_Type (T);
6234 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6235 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6236 P_List : constant List_Id :=
6237 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
6238
6239 Comps : List_Id;
6240 Decl1 : Node_Id;
6241 Decl2 : Node_Id;
6242 Def1 : Node_Id;
6243
6244 begin
6245 -- Create access to subprogram with full signature
6246
6247 if Etype (D_T) /= Standard_Void_Type then
6248 Def1 :=
6249 Make_Access_Function_Definition (Loc,
6250 Parameter_Specifications => P_List,
6251 Result_Definition =>
6252 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6253
6254 else
6255 Def1 :=
6256 Make_Access_Procedure_Definition (Loc,
6257 Parameter_Specifications => P_List);
6258 end if;
6259
6260 Decl1 :=
6261 Make_Full_Type_Declaration (Loc,
6262 Defining_Identifier => D_T2,
6263 Type_Definition => Def1);
6264
6265 -- Declare the new types before the original one since the latter will
6266 -- refer to them through the Equivalent_Type slot.
6267
6268 Insert_Before_And_Analyze (N, Decl1);
6269
6270 -- Associate the access to subprogram with its original access to
6271 -- protected subprogram type. Needed by the backend to know that this
6272 -- type corresponds with an access to protected subprogram type.
6273
6274 Set_Original_Access_Type (D_T2, T);
6275
6276 -- Create Equivalent_Type, a record with two components for an access to
6277 -- object and an access to subprogram.
6278
6279 Comps := New_List (
6280 Make_Component_Declaration (Loc,
6281 Defining_Identifier => Make_Temporary (Loc, 'P'),
6282 Component_Definition =>
6283 Make_Component_Definition (Loc,
6284 Aliased_Present => False,
6285 Subtype_Indication =>
6286 New_Occurrence_Of (RTE (RE_Address), Loc))),
6287
6288 Make_Component_Declaration (Loc,
6289 Defining_Identifier => Make_Temporary (Loc, 'S'),
6290 Component_Definition =>
6291 Make_Component_Definition (Loc,
6292 Aliased_Present => False,
6293 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6294
6295 Decl2 :=
6296 Make_Full_Type_Declaration (Loc,
6297 Defining_Identifier => E_T,
6298 Type_Definition =>
6299 Make_Record_Definition (Loc,
6300 Component_List =>
6301 Make_Component_List (Loc, Component_Items => Comps)));
6302
6303 Insert_Before_And_Analyze (N, Decl2);
6304 Set_Equivalent_Type (T, E_T);
6305 end Expand_Access_Protected_Subprogram_Type;
6306
6307 --------------------------
6308 -- Expand_Entry_Barrier --
6309 --------------------------
6310
6311 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6312 Cond : constant Node_Id :=
6313 Condition (Entry_Body_Formal_Part (N));
6314 Prot : constant Entity_Id := Scope (Ent);
6315 Spec_Decl : constant Node_Id := Parent (Prot);
6316 Func : Entity_Id := Empty;
6317 B_F : Node_Id;
6318 Body_Decl : Node_Id;
6319
6320 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
6321 -- Check whether entity in Barrier is external to protected type.
6322 -- If so, barrier may not be properly synchronized.
6323
6324 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
6325 -- Check whether N follows the Pure_Barriers restriction. Return OK if
6326 -- so.
6327
6328 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
6329 -- Check whether entity name N denotes a component of the protected
6330 -- object. This is used to check the Simple_Barrier restriction.
6331
6332 ----------------------
6333 -- Is_Global_Entity --
6334 ----------------------
6335
6336 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6337 E : Entity_Id;
6338 S : Entity_Id;
6339
6340 begin
6341 if Is_Entity_Name (N) and then Present (Entity (N)) then
6342 E := Entity (N);
6343 S := Scope (E);
6344
6345 if Ekind (E) = E_Variable then
6346
6347 -- If the variable is local to the barrier function generated
6348 -- during expansion, it is ok. If expansion is not performed,
6349 -- then Func is Empty so this test cannot succeed.
6350
6351 if Scope (E) = Func then
6352 null;
6353
6354 -- A protected call from a barrier to another object is ok
6355
6356 elsif Ekind (Etype (E)) = E_Protected_Type then
6357 null;
6358
6359 -- If the variable is within the package body we consider
6360 -- this safe. This is a common (if dubious) idiom.
6361
6362 elsif S = Scope (Prot)
6363 and then Ekind_In (S, E_Package, E_Generic_Package)
6364 and then Nkind (Parent (E)) = N_Object_Declaration
6365 and then Nkind (Parent (Parent (E))) = N_Package_Body
6366 then
6367 null;
6368
6369 else
6370 Error_Msg_N ("potentially unsynchronized barrier??", N);
6371 Error_Msg_N ("\& should be private component of type??", N);
6372 end if;
6373 end if;
6374 end if;
6375
6376 return OK;
6377 end Is_Global_Entity;
6378
6379 procedure Check_Unprotected_Barrier is
6380 new Traverse_Proc (Is_Global_Entity);
6381
6382 ----------------------------
6383 -- Is_Simple_Barrier_Name --
6384 ----------------------------
6385
6386 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
6387 Renamed : Node_Id;
6388
6389 begin
6390 -- Check for case of _object.all.field (note that the explicit
6391 -- dereference gets inserted by analyze/expand of _object.field).
6392
6393 if Expander_Active then
6394 Renamed := Renamed_Object (Entity (N));
6395
6396 return
6397 Present (Renamed)
6398 and then Nkind (Renamed) = N_Selected_Component
6399 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6400 else
6401 return Scope (Entity (N)) = Current_Scope;
6402 end if;
6403 end Is_Simple_Barrier_Name;
6404
6405 ---------------------
6406 -- Is_Pure_Barrier --
6407 ---------------------
6408
6409 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6410 begin
6411 case Nkind (N) is
6412 when N_Expanded_Name |
6413 N_Identifier =>
6414 if No (Entity (N)) then
6415 return Abandon;
6416 end if;
6417
6418 case Ekind (Entity (N)) is
6419 when E_Constant |
6420 E_Discriminant |
6421 E_Named_Integer |
6422 E_Named_Real |
6423 E_Enumeration_Literal =>
6424 return OK;
6425
6426 when E_Component |
6427 E_Variable =>
6428
6429 -- A variable in the protected type is expanded as a
6430 -- component.
6431
6432 if Is_Simple_Barrier_Name (N) then
6433 return OK;
6434 end if;
6435
6436 when others =>
6437 null;
6438 end case;
6439
6440 when N_Integer_Literal |
6441 N_Real_Literal |
6442 N_Character_Literal =>
6443 return OK;
6444
6445 when N_Op_Boolean |
6446 N_Op_Not =>
6447 if Ekind (Entity (N)) = E_Operator then
6448 return OK;
6449 end if;
6450
6451 when N_Short_Circuit =>
6452 return OK;
6453
6454 when others =>
6455 null;
6456 end case;
6457
6458 return Abandon;
6459 end Is_Pure_Barrier;
6460
6461 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6462
6463 -- Start of processing for Expand_Entry_Barrier
6464
6465 begin
6466 if No_Run_Time_Mode then
6467 Error_Msg_CRT ("entry barrier", N);
6468 return;
6469 end if;
6470
6471 -- The body of the entry barrier must be analyzed in the context of the
6472 -- protected object, but its scope is external to it, just as any other
6473 -- unprotected version of a protected operation. The specification has
6474 -- been produced when the protected type declaration was elaborated. We
6475 -- build the body, insert it in the enclosing scope, but analyze it in
6476 -- the current context. A more uniform approach would be to treat the
6477 -- barrier just as a protected function, and discard the protected
6478 -- version of it because it is never called.
6479
6480 if Expander_Active then
6481 B_F := Build_Barrier_Function (N, Ent, Prot);
6482 Func := Barrier_Function (Ent);
6483 Set_Corresponding_Spec (B_F, Func);
6484
6485 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6486
6487 if Nkind (Parent (Body_Decl)) = N_Subunit then
6488 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6489 end if;
6490
6491 Insert_Before_And_Analyze (Body_Decl, B_F);
6492
6493 Set_Discriminals (Spec_Decl);
6494 Set_Scope (Func, Scope (Prot));
6495
6496 else
6497 Analyze_And_Resolve (Cond, Any_Boolean);
6498 end if;
6499
6500 -- Check Pure_Barriers restriction
6501
6502 if Check_Pure_Barriers (Cond) = Abandon then
6503 Check_Restriction (Pure_Barriers, Cond);
6504 end if;
6505
6506 -- The Ravenscar profile restricts barriers to simple variables declared
6507 -- within the protected object. We also allow Boolean constants, since
6508 -- these appear in several published examples and are also allowed by
6509 -- other compilers.
6510
6511 -- Note that after analysis variables in this context will be replaced
6512 -- by the corresponding prival, that is to say a renaming of a selected
6513 -- component of the form _Object.Var. If expansion is disabled, as
6514 -- within a generic, we check that the entity appears in the current
6515 -- scope.
6516
6517 if Is_Entity_Name (Cond) then
6518
6519 -- A small optimization of useless renamings. If the scope of the
6520 -- entity of the condition is not the barrier function, then the
6521 -- condition does not reference any of the generated renamings
6522 -- within the function.
6523
6524 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6525 Set_Declarations (B_F, Empty_List);
6526 end if;
6527
6528 if Entity (Cond) = Standard_False
6529 or else
6530 Entity (Cond) = Standard_True
6531 then
6532 return;
6533
6534 elsif Is_Simple_Barrier_Name (Cond) then
6535 return;
6536 end if;
6537 end if;
6538
6539 -- It is not a boolean variable or literal, so check the restriction.
6540 -- Note that it is safe to be calling Check_Restriction from here, even
6541 -- though this is part of the expander, since Expand_Entry_Barrier is
6542 -- called from Sem_Ch9 even in -gnatc mode.
6543
6544 Check_Restriction (Simple_Barriers, Cond);
6545
6546 -- Emit warning if barrier contains global entities and is thus
6547 -- potentially unsynchronized.
6548
6549 Check_Unprotected_Barrier (Cond);
6550 end Expand_Entry_Barrier;
6551
6552 ------------------------------
6553 -- Expand_N_Abort_Statement --
6554 ------------------------------
6555
6556 -- Expand abort T1, T2, .. Tn; into:
6557 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6558
6559 procedure Expand_N_Abort_Statement (N : Node_Id) is
6560 Loc : constant Source_Ptr := Sloc (N);
6561 Tlist : constant List_Id := Names (N);
6562 Count : Nat;
6563 Aggr : Node_Id;
6564 Tasknm : Node_Id;
6565
6566 begin
6567 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6568 Count := 0;
6569
6570 Tasknm := First (Tlist);
6571
6572 while Present (Tasknm) loop
6573 Count := Count + 1;
6574
6575 -- A task interface class-wide type object is being aborted. Retrieve
6576 -- its _task_id by calling a dispatching routine.
6577
6578 if Ada_Version >= Ada_2005
6579 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6580 and then Is_Interface (Etype (Tasknm))
6581 and then Is_Task_Interface (Etype (Tasknm))
6582 then
6583 Append_To (Component_Associations (Aggr),
6584 Make_Component_Association (Loc,
6585 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6586 Expression =>
6587
6588 -- Task_Id (Tasknm._disp_get_task_id)
6589
6590 Make_Unchecked_Type_Conversion (Loc,
6591 Subtype_Mark =>
6592 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6593 Expression =>
6594 Make_Selected_Component (Loc,
6595 Prefix => New_Copy_Tree (Tasknm),
6596 Selector_Name =>
6597 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6598
6599 else
6600 Append_To (Component_Associations (Aggr),
6601 Make_Component_Association (Loc,
6602 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6603 Expression => Concurrent_Ref (Tasknm)));
6604 end if;
6605
6606 Next (Tasknm);
6607 end loop;
6608
6609 Rewrite (N,
6610 Make_Procedure_Call_Statement (Loc,
6611 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6612 Parameter_Associations => New_List (
6613 Make_Qualified_Expression (Loc,
6614 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6615 Expression => Aggr))));
6616
6617 Analyze (N);
6618 end Expand_N_Abort_Statement;
6619
6620 -------------------------------
6621 -- Expand_N_Accept_Statement --
6622 -------------------------------
6623
6624 -- This procedure handles expansion of accept statements that stand alone,
6625 -- i.e. they are not part of an accept alternative. The expansion of
6626 -- accept statement in accept alternatives is handled by the routines
6627 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6628 -- following description applies only to stand alone accept statements.
6629
6630 -- If there is no handled statement sequence, or only null statements, then
6631 -- this is called a trivial accept, and the expansion is:
6632
6633 -- Accept_Trivial (entry-index)
6634
6635 -- If there is a handled statement sequence, then the expansion is:
6636
6637 -- Ann : Address;
6638 -- {Lnn : Label}
6639
6640 -- begin
6641 -- begin
6642 -- Accept_Call (entry-index, Ann);
6643 -- Renaming_Declarations for formals
6644 -- <statement sequence from N_Accept_Statement node>
6645 -- Complete_Rendezvous;
6646 -- <<Lnn>>
6647 --
6648 -- exception
6649 -- when ... =>
6650 -- <exception handler from N_Accept_Statement node>
6651 -- Complete_Rendezvous;
6652 -- when ... =>
6653 -- <exception handler from N_Accept_Statement node>
6654 -- Complete_Rendezvous;
6655 -- ...
6656 -- end;
6657
6658 -- exception
6659 -- when all others =>
6660 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6661 -- end;
6662
6663 -- The first three declarations were already inserted ahead of the accept
6664 -- statement by the Expand_Accept_Declarations procedure, which was called
6665 -- directly from the semantics during analysis of the accept statement,
6666 -- before analyzing its contained statements.
6667
6668 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6669 -- from possible expansion activity (the original source of course does
6670 -- not have any declarations associated with the accept statement, since
6671 -- an accept statement has no declarative part). In particular, if the
6672 -- expander is active, the first such declaration is the declaration of
6673 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6674
6675 -- The two blocks are merged into a single block if the inner block has
6676 -- no exception handlers, but otherwise two blocks are required, since
6677 -- exceptions might be raised in the exception handlers of the inner
6678 -- block, and Exceptional_Complete_Rendezvous must be called.
6679
6680 procedure Expand_N_Accept_Statement (N : Node_Id) is
6681 Loc : constant Source_Ptr := Sloc (N);
6682 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6683 Ename : constant Node_Id := Entry_Direct_Name (N);
6684 Eindx : constant Node_Id := Entry_Index (N);
6685 Eent : constant Entity_Id := Entity (Ename);
6686 Acstack : constant Elist_Id := Accept_Address (Eent);
6687 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6688 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6689 Blkent : Entity_Id;
6690 Call : Node_Id;
6691 Block : Node_Id;
6692
6693 begin
6694 -- If the accept statement is not part of a list, then its parent must
6695 -- be an accept alternative, and, as described above, we do not do any
6696 -- expansion for such accept statements at this level.
6697
6698 if not Is_List_Member (N) then
6699 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6700 return;
6701
6702 -- Trivial accept case (no statement sequence, or null statements).
6703 -- If the accept statement has declarations, then just insert them
6704 -- before the procedure call.
6705
6706 elsif Trivial_Accept_OK
6707 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6708 then
6709 -- Remove declarations for renamings, because the parameter block
6710 -- will not be assigned.
6711
6712 declare
6713 D : Node_Id;
6714 Next_D : Node_Id;
6715
6716 begin
6717 D := First (Declarations (N));
6718 while Present (D) loop
6719 Next_D := Next (D);
6720 if Nkind (D) = N_Object_Renaming_Declaration then
6721 Remove (D);
6722 end if;
6723
6724 D := Next_D;
6725 end loop;
6726 end;
6727
6728 if Present (Declarations (N)) then
6729 Insert_Actions (N, Declarations (N));
6730 end if;
6731
6732 Rewrite (N,
6733 Make_Procedure_Call_Statement (Loc,
6734 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6735 Parameter_Associations => New_List (
6736 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6737
6738 Analyze (N);
6739
6740 -- Discard Entry_Address that was created for it, so it will not be
6741 -- emitted if this accept statement is in the statement part of a
6742 -- delay alternative.
6743
6744 if Present (Stats) then
6745 Remove_Last_Elmt (Acstack);
6746 end if;
6747
6748 -- Case of statement sequence present
6749
6750 else
6751 -- Construct the block, using the declarations from the accept
6752 -- statement if any to initialize the declarations of the block.
6753
6754 Blkent := Make_Temporary (Loc, 'A');
6755 Set_Ekind (Blkent, E_Block);
6756 Set_Etype (Blkent, Standard_Void_Type);
6757 Set_Scope (Blkent, Current_Scope);
6758
6759 Block :=
6760 Make_Block_Statement (Loc,
6761 Identifier => New_Occurrence_Of (Blkent, Loc),
6762 Declarations => Declarations (N),
6763 Handled_Statement_Sequence => Build_Accept_Body (N));
6764
6765 -- For the analysis of the generated declarations, the parent node
6766 -- must be properly set.
6767
6768 Set_Parent (Block, Parent (N));
6769
6770 -- Prepend call to Accept_Call to main statement sequence If the
6771 -- accept has exception handlers, the statement sequence is wrapped
6772 -- in a block. Insert call and renaming declarations in the
6773 -- declarations of the block, so they are elaborated before the
6774 -- handlers.
6775
6776 Call :=
6777 Make_Procedure_Call_Statement (Loc,
6778 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6779 Parameter_Associations => New_List (
6780 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6781 New_Occurrence_Of (Ann, Loc)));
6782
6783 if Parent (Stats) = N then
6784 Prepend (Call, Statements (Stats));
6785 else
6786 Set_Declarations (Parent (Stats), New_List (Call));
6787 end if;
6788
6789 Analyze (Call);
6790
6791 Push_Scope (Blkent);
6792
6793 declare
6794 D : Node_Id;
6795 Next_D : Node_Id;
6796 Typ : Entity_Id;
6797
6798 begin
6799 D := First (Declarations (N));
6800 while Present (D) loop
6801 Next_D := Next (D);
6802
6803 if Nkind (D) = N_Object_Renaming_Declaration then
6804
6805 -- The renaming declarations for the formals were created
6806 -- during analysis of the accept statement, and attached to
6807 -- the list of declarations. Place them now in the context
6808 -- of the accept block or subprogram.
6809
6810 Remove (D);
6811 Typ := Entity (Subtype_Mark (D));
6812 Insert_After (Call, D);
6813 Analyze (D);
6814
6815 -- If the formal is class_wide, it does not have an actual
6816 -- subtype. The analysis of the renaming declaration creates
6817 -- one, but we need to retain the class-wide nature of the
6818 -- entity.
6819
6820 if Is_Class_Wide_Type (Typ) then
6821 Set_Etype (Defining_Identifier (D), Typ);
6822 end if;
6823
6824 end if;
6825
6826 D := Next_D;
6827 end loop;
6828 end;
6829
6830 End_Scope;
6831
6832 -- Replace the accept statement by the new block
6833
6834 Rewrite (N, Block);
6835 Analyze (N);
6836
6837 -- Last step is to unstack the Accept_Address value
6838
6839 Remove_Last_Elmt (Acstack);
6840 end if;
6841 end Expand_N_Accept_Statement;
6842
6843 ----------------------------------
6844 -- Expand_N_Asynchronous_Select --
6845 ----------------------------------
6846
6847 -- This procedure assumes that the trigger statement is an entry call or
6848 -- a dispatching procedure call. A delay alternative should already have
6849 -- been expanded into an entry call to the appropriate delay object Wait
6850 -- entry.
6851
6852 -- If the trigger is a task entry call, the select is implemented with
6853 -- a Task_Entry_Call:
6854
6855 -- declare
6856 -- B : Boolean;
6857 -- C : Boolean;
6858 -- P : parms := (parm, parm, parm);
6859
6860 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6861
6862 -- procedure _clean is
6863 -- begin
6864 -- ...
6865 -- Cancel_Task_Entry_Call (C);
6866 -- ...
6867 -- end _clean;
6868
6869 -- begin
6870 -- Abort_Defer;
6871 -- Task_Entry_Call
6872 -- (<acceptor-task>, -- Acceptor
6873 -- <entry-index>, -- E
6874 -- P'Address, -- Uninterpreted_Data
6875 -- Asynchronous_Call, -- Mode
6876 -- B); -- Rendezvous_Successful
6877
6878 -- begin
6879 -- begin
6880 -- Abort_Undefer;
6881 -- <abortable-part>
6882 -- at end
6883 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6884 -- end;
6885 -- exception
6886 -- when Abort_Signal => Abort_Undefer;
6887 -- end;
6888
6889 -- parm := P.param;
6890 -- parm := P.param;
6891 -- ...
6892 -- if not C then
6893 -- <triggered-statements>
6894 -- end if;
6895 -- end;
6896
6897 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6898 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6899 -- as follows:
6900
6901 -- declare
6902 -- P : parms := (parm, parm, parm);
6903 -- begin
6904 -- Call_Simple (acceptor-task, entry-index, P'Address);
6905 -- parm := P.param;
6906 -- parm := P.param;
6907 -- ...
6908 -- end;
6909
6910 -- so the task at hand is to convert the latter expansion into the former
6911
6912 -- If the trigger is a protected entry call, the select is implemented
6913 -- with Protected_Entry_Call:
6914
6915 -- declare
6916 -- P : E1_Params := (param, param, param);
6917 -- Bnn : Communications_Block;
6918
6919 -- begin
6920 -- declare
6921
6922 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6923
6924 -- procedure _clean is
6925 -- begin
6926 -- ...
6927 -- if Enqueued (Bnn) then
6928 -- Cancel_Protected_Entry_Call (Bnn);
6929 -- end if;
6930 -- ...
6931 -- end _clean;
6932
6933 -- begin
6934 -- begin
6935 -- Protected_Entry_Call
6936 -- (po._object'Access, -- Object
6937 -- <entry index>, -- E
6938 -- P'Address, -- Uninterpreted_Data
6939 -- Asynchronous_Call, -- Mode
6940 -- Bnn); -- Block
6941
6942 -- if Enqueued (Bnn) then
6943 -- <abortable-part>
6944 -- end if;
6945 -- at end
6946 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6947 -- end;
6948 -- exception
6949 -- when Abort_Signal => Abort_Undefer;
6950 -- end;
6951
6952 -- if not Cancelled (Bnn) then
6953 -- <triggered-statements>
6954 -- end if;
6955 -- end;
6956
6957 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6958 -- entry call:
6959
6960 -- declare
6961 -- P : E1_Params := (param, param, param);
6962 -- Bnn : Communications_Block;
6963
6964 -- begin
6965 -- Protected_Entry_Call
6966 -- (po._object'Access, -- Object
6967 -- <entry index>, -- E
6968 -- P'Address, -- Uninterpreted_Data
6969 -- Simple_Call, -- Mode
6970 -- Bnn); -- Block
6971 -- parm := P.param;
6972 -- parm := P.param;
6973 -- ...
6974 -- end;
6975
6976 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6977 -- expanded into:
6978
6979 -- declare
6980 -- B : Boolean := False;
6981 -- Bnn : Communication_Block;
6982 -- C : Ada.Tags.Prim_Op_Kind;
6983 -- D : System.Storage_Elements.Dummy_Communication_Block;
6984 -- K : Ada.Tags.Tagged_Kind :=
6985 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6986 -- P : Parameters := (Param1 .. ParamN);
6987 -- S : Integer;
6988 -- U : Boolean;
6989
6990 -- begin
6991 -- if K = Ada.Tags.TK_Limited_Tagged
6992 -- or else K = Ada.Tags.TK_Tagged
6993 -- then
6994 -- <dispatching-call>;
6995 -- <triggering-statements>;
6996
6997 -- else
6998 -- S :=
6999 -- Ada.Tags.Get_Offset_Index
7000 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7001
7002 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7003
7004 -- if C = POK_Protected_Entry then
7005 -- declare
7006 -- procedure _clean is
7007 -- begin
7008 -- if Enqueued (Bnn) then
7009 -- Cancel_Protected_Entry_Call (Bnn);
7010 -- end if;
7011 -- end _clean;
7012
7013 -- begin
7014 -- begin
7015 -- _Disp_Asynchronous_Select
7016 -- (<object>, S, P'Address, D, B);
7017 -- Bnn := Communication_Block (D);
7018
7019 -- Param1 := P.Param1;
7020 -- ...
7021 -- ParamN := P.ParamN;
7022
7023 -- if Enqueued (Bnn) then
7024 -- <abortable-statements>
7025 -- end if;
7026 -- at end
7027 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7028 -- end;
7029 -- exception
7030 -- when Abort_Signal => Abort_Undefer;
7031 -- end;
7032
7033 -- if not Cancelled (Bnn) then
7034 -- <triggering-statements>
7035 -- end if;
7036
7037 -- elsif C = POK_Task_Entry then
7038 -- declare
7039 -- procedure _clean is
7040 -- begin
7041 -- Cancel_Task_Entry_Call (U);
7042 -- end _clean;
7043
7044 -- begin
7045 -- Abort_Defer;
7046
7047 -- _Disp_Asynchronous_Select
7048 -- (<object>, S, P'Address, D, B);
7049 -- Bnn := Communication_Bloc (D);
7050
7051 -- Param1 := P.Param1;
7052 -- ...
7053 -- ParamN := P.ParamN;
7054
7055 -- begin
7056 -- begin
7057 -- Abort_Undefer;
7058 -- <abortable-statements>
7059 -- at end
7060 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7061 -- end;
7062 -- exception
7063 -- when Abort_Signal => Abort_Undefer;
7064 -- end;
7065
7066 -- if not U then
7067 -- <triggering-statements>
7068 -- end if;
7069 -- end;
7070
7071 -- else
7072 -- <dispatching-call>;
7073 -- <triggering-statements>
7074 -- end if;
7075 -- end if;
7076 -- end;
7077
7078 -- The job is to convert this to the asynchronous form
7079
7080 -- If the trigger is a delay statement, it will have been expanded into
7081 -- a call to one of the GNARL delay procedures. This routine will convert
7082 -- this into a protected entry call on a delay object and then continue
7083 -- processing as for a protected entry call trigger. This requires
7084 -- declaring a Delay_Block object and adding a pointer to this object to
7085 -- the parameter list of the delay procedure to form the parameter list of
7086 -- the entry call. This object is used by the runtime to queue the delay
7087 -- request.
7088
7089 -- For a description of the use of P and the assignments after the call,
7090 -- see Expand_N_Entry_Call_Statement.
7091
7092 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
7093 Loc : constant Source_Ptr := Sloc (N);
7094 Abrt : constant Node_Id := Abortable_Part (N);
7095 Trig : constant Node_Id := Triggering_Alternative (N);
7096
7097 Abort_Block_Ent : Entity_Id;
7098 Abortable_Block : Node_Id;
7099 Actuals : List_Id;
7100 Astats : List_Id;
7101 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
7102 Blk_Typ : Entity_Id;
7103 Call : Node_Id;
7104 Call_Ent : Entity_Id;
7105 Cancel_Param : Entity_Id;
7106 Cleanup_Block : Node_Id;
7107 Cleanup_Block_Ent : Entity_Id;
7108 Cleanup_Stmts : List_Id;
7109 Conc_Typ_Stmts : List_Id;
7110 Concval : Node_Id;
7111 Dblock_Ent : Entity_Id;
7112 Decl : Node_Id;
7113 Decls : List_Id;
7114 Ecall : Node_Id;
7115 Ename : Node_Id;
7116 Enqueue_Call : Node_Id;
7117 Formals : List_Id;
7118 Hdle : List_Id;
7119 Handler_Stmt : Node_Id;
7120 Index : Node_Id;
7121 Lim_Typ_Stmts : List_Id;
7122 N_Orig : Node_Id;
7123 Obj : Entity_Id;
7124 Param : Node_Id;
7125 Params : List_Id;
7126 Pdef : Entity_Id;
7127 ProtE_Stmts : List_Id;
7128 ProtP_Stmts : List_Id;
7129 Stmt : Node_Id;
7130 Stmts : List_Id;
7131 TaskE_Stmts : List_Id;
7132 Tstats : List_Id;
7133
7134 B : Entity_Id; -- Call status flag
7135 Bnn : Entity_Id; -- Communication block
7136 C : Entity_Id; -- Call kind
7137 K : Entity_Id; -- Tagged kind
7138 P : Entity_Id; -- Parameter block
7139 S : Entity_Id; -- Primitive operation slot
7140 T : Entity_Id; -- Additional status flag
7141
7142 procedure Rewrite_Abortable_Part;
7143 -- If the trigger is a dispatching call, the expansion inserts multiple
7144 -- copies of the abortable part. This is both inefficient, and may lead
7145 -- to duplicate definitions that the back-end will reject, when the
7146 -- abortable part includes loops. This procedure rewrites the abortable
7147 -- part into a call to a generated procedure.
7148
7149 ----------------------------
7150 -- Rewrite_Abortable_Part --
7151 ----------------------------
7152
7153 procedure Rewrite_Abortable_Part is
7154 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7155 Decl : Node_Id;
7156
7157 begin
7158 Decl :=
7159 Make_Subprogram_Body (Loc,
7160 Specification =>
7161 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
7162 Declarations => New_List,
7163 Handled_Statement_Sequence =>
7164 Make_Handled_Sequence_Of_Statements (Loc, Astats));
7165 Insert_Before (N, Decl);
7166 Analyze (Decl);
7167
7168 -- Rewrite abortable part into a call to this procedure.
7169
7170 Astats :=
7171 New_List (
7172 Make_Procedure_Call_Statement (Loc,
7173 Name => New_Occurrence_Of (Proc, Loc)));
7174 end Rewrite_Abortable_Part;
7175
7176 -- Start of processing for Expand_N_Asynchronous_Select
7177
7178 begin
7179 -- Asynchronous select is not supported on restricted runtimes. Don't
7180 -- try to expand.
7181
7182 if Restricted_Profile then
7183 return;
7184 end if;
7185
7186 Process_Statements_For_Controlled_Objects (Trig);
7187 Process_Statements_For_Controlled_Objects (Abrt);
7188
7189 Ecall := Triggering_Statement (Trig);
7190
7191 Ensure_Statement_Present (Sloc (Ecall), Trig);
7192
7193 -- Retrieve Astats and Tstats now because the finalization machinery may
7194 -- wrap them in blocks.
7195
7196 Astats := Statements (Abrt);
7197 Tstats := Statements (Trig);
7198
7199 -- The arguments in the call may require dynamic allocation, and the
7200 -- call statement may have been transformed into a block. The block
7201 -- may contain additional declarations for internal entities, and the
7202 -- original call is found by sequential search.
7203
7204 if Nkind (Ecall) = N_Block_Statement then
7205 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
7206 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
7207 N_Entry_Call_Statement)
7208 loop
7209 Next (Ecall);
7210 end loop;
7211 end if;
7212
7213 -- This is either a dispatching call or a delay statement used as a
7214 -- trigger which was expanded into a procedure call.
7215
7216 if Nkind (Ecall) = N_Procedure_Call_Statement then
7217 if Ada_Version >= Ada_2005
7218 and then
7219 (No (Original_Node (Ecall))
7220 or else not Nkind_In (Original_Node (Ecall),
7221 N_Delay_Relative_Statement,
7222 N_Delay_Until_Statement))
7223 then
7224 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
7225
7226 Rewrite_Abortable_Part;
7227 Decls := New_List;
7228 Stmts := New_List;
7229
7230 -- Call status flag processing, generate:
7231 -- B : Boolean := False;
7232
7233 B := Build_B (Loc, Decls);
7234
7235 -- Communication block processing, generate:
7236 -- Bnn : Communication_Block;
7237
7238 Bnn := Make_Temporary (Loc, 'B');
7239 Append_To (Decls,
7240 Make_Object_Declaration (Loc,
7241 Defining_Identifier => Bnn,
7242 Object_Definition =>
7243 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
7244
7245 -- Call kind processing, generate:
7246 -- C : Ada.Tags.Prim_Op_Kind;
7247
7248 C := Build_C (Loc, Decls);
7249
7250 -- Tagged kind processing, generate:
7251 -- K : Ada.Tags.Tagged_Kind :=
7252 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7253
7254 -- Dummy communication block, generate:
7255 -- D : Dummy_Communication_Block;
7256
7257 Append_To (Decls,
7258 Make_Object_Declaration (Loc,
7259 Defining_Identifier =>
7260 Make_Defining_Identifier (Loc, Name_uD),
7261 Object_Definition =>
7262 New_Occurrence_Of
7263 (RTE (RE_Dummy_Communication_Block), Loc)));
7264
7265 K := Build_K (Loc, Decls, Obj);
7266
7267 -- Parameter block processing
7268
7269 Blk_Typ := Build_Parameter_Block
7270 (Loc, Actuals, Formals, Decls);
7271 P := Parameter_Block_Pack
7272 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7273
7274 -- Dispatch table slot processing, generate:
7275 -- S : Integer;
7276
7277 S := Build_S (Loc, Decls);
7278
7279 -- Additional status flag processing, generate:
7280 -- Tnn : Boolean;
7281
7282 T := Make_Temporary (Loc, 'T');
7283 Append_To (Decls,
7284 Make_Object_Declaration (Loc,
7285 Defining_Identifier => T,
7286 Object_Definition =>
7287 New_Occurrence_Of (Standard_Boolean, Loc)));
7288
7289 ------------------------------
7290 -- Protected entry handling --
7291 ------------------------------
7292
7293 -- Generate:
7294 -- Param1 := P.Param1;
7295 -- ...
7296 -- ParamN := P.ParamN;
7297
7298 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7299
7300 -- Generate:
7301 -- Bnn := Communication_Block (D);
7302
7303 Prepend_To (Cleanup_Stmts,
7304 Make_Assignment_Statement (Loc,
7305 Name => New_Occurrence_Of (Bnn, Loc),
7306 Expression =>
7307 Make_Unchecked_Type_Conversion (Loc,
7308 Subtype_Mark =>
7309 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7310 Expression => Make_Identifier (Loc, Name_uD))));
7311
7312 -- Generate:
7313 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7314
7315 Prepend_To (Cleanup_Stmts,
7316 Make_Procedure_Call_Statement (Loc,
7317 Name =>
7318 New_Occurrence_Of
7319 (Find_Prim_Op
7320 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7321 Loc),
7322 Parameter_Associations =>
7323 New_List (
7324 New_Copy_Tree (Obj), -- <object>
7325 New_Occurrence_Of (S, Loc), -- S
7326 Make_Attribute_Reference (Loc, -- P'Address
7327 Prefix => New_Occurrence_Of (P, Loc),
7328 Attribute_Name => Name_Address),
7329 Make_Identifier (Loc, Name_uD), -- D
7330 New_Occurrence_Of (B, Loc)))); -- B
7331
7332 -- Generate:
7333 -- if Enqueued (Bnn) then
7334 -- <abortable-statements>
7335 -- end if;
7336
7337 Append_To (Cleanup_Stmts,
7338 Make_Implicit_If_Statement (N,
7339 Condition =>
7340 Make_Function_Call (Loc,
7341 Name =>
7342 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7343 Parameter_Associations =>
7344 New_List (New_Occurrence_Of (Bnn, Loc))),
7345
7346 Then_Statements =>
7347 New_Copy_List_Tree (Astats)));
7348
7349 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7350 -- will then generate a _clean for the communication block Bnn.
7351
7352 -- Generate:
7353 -- declare
7354 -- procedure _clean is
7355 -- begin
7356 -- if Enqueued (Bnn) then
7357 -- Cancel_Protected_Entry_Call (Bnn);
7358 -- end if;
7359 -- end _clean;
7360 -- begin
7361 -- Cleanup_Stmts
7362 -- at end
7363 -- _clean;
7364 -- end;
7365
7366 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7367 Cleanup_Block :=
7368 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7369
7370 -- Wrap the cleanup block in an exception handling block
7371
7372 -- Generate:
7373 -- begin
7374 -- Cleanup_Block
7375 -- exception
7376 -- when Abort_Signal => Abort_Undefer;
7377 -- end;
7378
7379 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7380 ProtE_Stmts :=
7381 New_List (
7382 Make_Implicit_Label_Declaration (Loc,
7383 Defining_Identifier => Abort_Block_Ent),
7384
7385 Build_Abort_Block
7386 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7387
7388 -- Generate:
7389 -- if not Cancelled (Bnn) then
7390 -- <triggering-statements>
7391 -- end if;
7392
7393 Append_To (ProtE_Stmts,
7394 Make_Implicit_If_Statement (N,
7395 Condition =>
7396 Make_Op_Not (Loc,
7397 Right_Opnd =>
7398 Make_Function_Call (Loc,
7399 Name =>
7400 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7401 Parameter_Associations =>
7402 New_List (New_Occurrence_Of (Bnn, Loc)))),
7403
7404 Then_Statements =>
7405 New_Copy_List_Tree (Tstats)));
7406
7407 -------------------------
7408 -- Task entry handling --
7409 -------------------------
7410
7411 -- Generate:
7412 -- Param1 := P.Param1;
7413 -- ...
7414 -- ParamN := P.ParamN;
7415
7416 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7417
7418 -- Generate:
7419 -- Bnn := Communication_Block (D);
7420
7421 Append_To (TaskE_Stmts,
7422 Make_Assignment_Statement (Loc,
7423 Name =>
7424 New_Occurrence_Of (Bnn, Loc),
7425 Expression =>
7426 Make_Unchecked_Type_Conversion (Loc,
7427 Subtype_Mark =>
7428 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7429 Expression => Make_Identifier (Loc, Name_uD))));
7430
7431 -- Generate:
7432 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7433
7434 Prepend_To (TaskE_Stmts,
7435 Make_Procedure_Call_Statement (Loc,
7436 Name =>
7437 New_Occurrence_Of (
7438 Find_Prim_Op (Etype (Etype (Obj)),
7439 Name_uDisp_Asynchronous_Select),
7440 Loc),
7441
7442 Parameter_Associations => New_List (
7443 New_Copy_Tree (Obj), -- <object>
7444 New_Occurrence_Of (S, Loc), -- S
7445 Make_Attribute_Reference (Loc, -- P'Address
7446 Prefix => New_Occurrence_Of (P, Loc),
7447 Attribute_Name => Name_Address),
7448 Make_Identifier (Loc, Name_uD), -- D
7449 New_Occurrence_Of (B, Loc)))); -- B
7450
7451 -- Generate:
7452 -- Abort_Defer;
7453
7454 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7455
7456 -- Generate:
7457 -- Abort_Undefer;
7458 -- <abortable-statements>
7459
7460 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7461
7462 Prepend_To
7463 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7464
7465 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7466 -- will generate a _clean for the additional status flag.
7467
7468 -- Generate:
7469 -- declare
7470 -- procedure _clean is
7471 -- begin
7472 -- Cancel_Task_Entry_Call (U);
7473 -- end _clean;
7474 -- begin
7475 -- Cleanup_Stmts
7476 -- at end
7477 -- _clean;
7478 -- end;
7479
7480 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7481 Cleanup_Block :=
7482 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7483
7484 -- Wrap the cleanup block in an exception handling block
7485
7486 -- Generate:
7487 -- begin
7488 -- Cleanup_Block
7489 -- exception
7490 -- when Abort_Signal => Abort_Undefer;
7491 -- end;
7492
7493 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7494
7495 Append_To (TaskE_Stmts,
7496 Make_Implicit_Label_Declaration (Loc,
7497 Defining_Identifier => Abort_Block_Ent));
7498
7499 Append_To (TaskE_Stmts,
7500 Build_Abort_Block
7501 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7502
7503 -- Generate:
7504 -- if not T then
7505 -- <triggering-statements>
7506 -- end if;
7507
7508 Append_To (TaskE_Stmts,
7509 Make_Implicit_If_Statement (N,
7510 Condition =>
7511 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7512
7513 Then_Statements =>
7514 New_Copy_List_Tree (Tstats)));
7515
7516 ----------------------------------
7517 -- Protected procedure handling --
7518 ----------------------------------
7519
7520 -- Generate:
7521 -- <dispatching-call>;
7522 -- <triggering-statements>
7523
7524 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7525 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7526
7527 -- Generate:
7528 -- S := Ada.Tags.Get_Offset_Index
7529 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7530
7531 Conc_Typ_Stmts :=
7532 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7533
7534 -- Generate:
7535 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7536
7537 Append_To (Conc_Typ_Stmts,
7538 Make_Procedure_Call_Statement (Loc,
7539 Name =>
7540 New_Occurrence_Of
7541 (Find_Prim_Op (Etype (Etype (Obj)),
7542 Name_uDisp_Get_Prim_Op_Kind),
7543 Loc),
7544 Parameter_Associations =>
7545 New_List (
7546 New_Copy_Tree (Obj),
7547 New_Occurrence_Of (S, Loc),
7548 New_Occurrence_Of (C, Loc))));
7549
7550 -- Generate:
7551 -- if C = POK_Procedure_Entry then
7552 -- ProtE_Stmts
7553 -- elsif C = POK_Task_Entry then
7554 -- TaskE_Stmts
7555 -- else
7556 -- ProtP_Stmts
7557 -- end if;
7558
7559 Append_To (Conc_Typ_Stmts,
7560 Make_Implicit_If_Statement (N,
7561 Condition =>
7562 Make_Op_Eq (Loc,
7563 Left_Opnd =>
7564 New_Occurrence_Of (C, Loc),
7565 Right_Opnd =>
7566 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7567
7568 Then_Statements =>
7569 ProtE_Stmts,
7570
7571 Elsif_Parts =>
7572 New_List (
7573 Make_Elsif_Part (Loc,
7574 Condition =>
7575 Make_Op_Eq (Loc,
7576 Left_Opnd =>
7577 New_Occurrence_Of (C, Loc),
7578 Right_Opnd =>
7579 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7580
7581 Then_Statements =>
7582 TaskE_Stmts)),
7583
7584 Else_Statements =>
7585 ProtP_Stmts));
7586
7587 -- Generate:
7588 -- <dispatching-call>;
7589 -- <triggering-statements>
7590
7591 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7592 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7593
7594 -- Generate:
7595 -- if K = Ada.Tags.TK_Limited_Tagged
7596 -- or else K = Ada.Tags.TK_Tagged
7597 -- then
7598 -- Lim_Typ_Stmts
7599 -- else
7600 -- Conc_Typ_Stmts
7601 -- end if;
7602
7603 Append_To (Stmts,
7604 Make_Implicit_If_Statement (N,
7605 Condition => Build_Dispatching_Tag_Check (K, N),
7606 Then_Statements => Lim_Typ_Stmts,
7607 Else_Statements => Conc_Typ_Stmts));
7608
7609 Rewrite (N,
7610 Make_Block_Statement (Loc,
7611 Declarations =>
7612 Decls,
7613 Handled_Statement_Sequence =>
7614 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7615
7616 Analyze (N);
7617 return;
7618
7619 -- Delay triggering statement processing
7620
7621 else
7622 -- Add a Delay_Block object to the parameter list of the delay
7623 -- procedure to form the parameter list of the Wait entry call.
7624
7625 Dblock_Ent := Make_Temporary (Loc, 'D');
7626
7627 Pdef := Entity (Name (Ecall));
7628
7629 if Is_RTE (Pdef, RO_CA_Delay_For) then
7630 Enqueue_Call :=
7631 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7632
7633 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7634 Enqueue_Call :=
7635 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7636
7637 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7638 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7639 end if;
7640
7641 Append_To (Parameter_Associations (Ecall),
7642 Make_Attribute_Reference (Loc,
7643 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7644 Attribute_Name => Name_Unchecked_Access));
7645
7646 -- Create the inner block to protect the abortable part
7647
7648 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7649
7650 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7651
7652 Abortable_Block :=
7653 Make_Block_Statement (Loc,
7654 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7655 Handled_Statement_Sequence =>
7656 Make_Handled_Sequence_Of_Statements (Loc,
7657 Statements => Astats),
7658 Has_Created_Identifier => True,
7659 Is_Asynchronous_Call_Block => True);
7660
7661 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7662
7663 Rewrite (Ecall,
7664 Make_Implicit_If_Statement (N,
7665 Condition =>
7666 Make_Function_Call (Loc,
7667 Name => Enqueue_Call,
7668 Parameter_Associations => Parameter_Associations (Ecall)),
7669 Then_Statements =>
7670 New_List (Make_Block_Statement (Loc,
7671 Handled_Statement_Sequence =>
7672 Make_Handled_Sequence_Of_Statements (Loc,
7673 Statements => New_List (
7674 Make_Implicit_Label_Declaration (Loc,
7675 Defining_Identifier => Blk_Ent,
7676 Label_Construct => Abortable_Block),
7677 Abortable_Block),
7678 Exception_Handlers => Hdle)))));
7679
7680 Stmts := New_List (Ecall);
7681
7682 -- Construct statement sequence for new block
7683
7684 Append_To (Stmts,
7685 Make_Implicit_If_Statement (N,
7686 Condition =>
7687 Make_Function_Call (Loc,
7688 Name => New_Occurrence_Of (
7689 RTE (RE_Timed_Out), Loc),
7690 Parameter_Associations => New_List (
7691 Make_Attribute_Reference (Loc,
7692 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7693 Attribute_Name => Name_Unchecked_Access))),
7694 Then_Statements => Tstats));
7695
7696 -- The result is the new block
7697
7698 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7699
7700 Rewrite (N,
7701 Make_Block_Statement (Loc,
7702 Declarations => New_List (
7703 Make_Object_Declaration (Loc,
7704 Defining_Identifier => Dblock_Ent,
7705 Aliased_Present => True,
7706 Object_Definition =>
7707 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7708
7709 Handled_Statement_Sequence =>
7710 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7711
7712 Analyze (N);
7713 return;
7714 end if;
7715
7716 else
7717 N_Orig := N;
7718 end if;
7719
7720 Extract_Entry (Ecall, Concval, Ename, Index);
7721 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7722
7723 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7724 Decls := Declarations (Ecall);
7725
7726 if Is_Protected_Type (Etype (Concval)) then
7727
7728 -- Get the declarations of the block expanded from the entry call
7729
7730 Decl := First (Decls);
7731 while Present (Decl)
7732 and then (Nkind (Decl) /= N_Object_Declaration
7733 or else not Is_RTE (Etype (Object_Definition (Decl)),
7734 RE_Communication_Block))
7735 loop
7736 Next (Decl);
7737 end loop;
7738
7739 pragma Assert (Present (Decl));
7740 Cancel_Param := Defining_Identifier (Decl);
7741
7742 -- Change the mode of the Protected_Entry_Call call
7743
7744 -- Protected_Entry_Call (
7745 -- Object => po._object'Access,
7746 -- E => <entry index>;
7747 -- Uninterpreted_Data => P'Address;
7748 -- Mode => Asynchronous_Call;
7749 -- Block => Bnn);
7750
7751 -- Skip assignments to temporaries created for in-out parameters
7752
7753 -- This makes unwarranted assumptions about the shape of the expanded
7754 -- tree for the call, and should be cleaned up ???
7755
7756 Stmt := First (Stmts);
7757 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7758 Next (Stmt);
7759 end loop;
7760
7761 Call := Stmt;
7762
7763 Param := First (Parameter_Associations (Call));
7764 while Present (Param)
7765 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7766 loop
7767 Next (Param);
7768 end loop;
7769
7770 pragma Assert (Present (Param));
7771 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7772 Analyze (Param);
7773
7774 -- Append an if statement to execute the abortable part
7775
7776 -- Generate:
7777 -- if Enqueued (Bnn) then
7778
7779 Append_To (Stmts,
7780 Make_Implicit_If_Statement (N,
7781 Condition =>
7782 Make_Function_Call (Loc,
7783 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7784 Parameter_Associations => New_List (
7785 New_Occurrence_Of (Cancel_Param, Loc))),
7786 Then_Statements => Astats));
7787
7788 Abortable_Block :=
7789 Make_Block_Statement (Loc,
7790 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7791 Handled_Statement_Sequence =>
7792 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7793 Has_Created_Identifier => True,
7794 Is_Asynchronous_Call_Block => True);
7795
7796 -- Aborts are not deferred at beginning of exception handlers in
7797 -- ZCX mode.
7798
7799 if ZCX_Exceptions then
7800 Handler_Stmt := Make_Null_Statement (Loc);
7801
7802 else
7803 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7804 end if;
7805
7806 Stmts := New_List (
7807 Make_Block_Statement (Loc,
7808 Handled_Statement_Sequence =>
7809 Make_Handled_Sequence_Of_Statements (Loc,
7810 Statements => New_List (
7811 Make_Implicit_Label_Declaration (Loc,
7812 Defining_Identifier => Blk_Ent,
7813 Label_Construct => Abortable_Block),
7814 Abortable_Block),
7815
7816 -- exception
7817
7818 Exception_Handlers => New_List (
7819 Make_Implicit_Exception_Handler (Loc,
7820
7821 -- when Abort_Signal =>
7822 -- Abort_Undefer.all;
7823
7824 Exception_Choices =>
7825 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7826 Statements => New_List (Handler_Stmt))))),
7827
7828 -- if not Cancelled (Bnn) then
7829 -- triggered statements
7830 -- end if;
7831
7832 Make_Implicit_If_Statement (N,
7833 Condition => Make_Op_Not (Loc,
7834 Right_Opnd =>
7835 Make_Function_Call (Loc,
7836 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7837 Parameter_Associations => New_List (
7838 New_Occurrence_Of (Cancel_Param, Loc)))),
7839 Then_Statements => Tstats));
7840
7841 -- Asynchronous task entry call
7842
7843 else
7844 if No (Decls) then
7845 Decls := New_List;
7846 end if;
7847
7848 B := Make_Defining_Identifier (Loc, Name_uB);
7849
7850 -- Insert declaration of B in declarations of existing block
7851
7852 Prepend_To (Decls,
7853 Make_Object_Declaration (Loc,
7854 Defining_Identifier => B,
7855 Object_Definition =>
7856 New_Occurrence_Of (Standard_Boolean, Loc)));
7857
7858 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7859
7860 -- Insert declaration of C in declarations of existing block
7861
7862 Prepend_To (Decls,
7863 Make_Object_Declaration (Loc,
7864 Defining_Identifier => Cancel_Param,
7865 Object_Definition =>
7866 New_Occurrence_Of (Standard_Boolean, Loc)));
7867
7868 -- Remove and save the call to Call_Simple
7869
7870 Stmt := First (Stmts);
7871
7872 -- Skip assignments to temporaries created for in-out parameters.
7873 -- This makes unwarranted assumptions about the shape of the expanded
7874 -- tree for the call, and should be cleaned up ???
7875
7876 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7877 Next (Stmt);
7878 end loop;
7879
7880 Call := Stmt;
7881
7882 -- Create the inner block to protect the abortable part
7883
7884 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7885
7886 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
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, Build_Runtime_Call (Loc, RE_Abort_Defer));
7931 end if;
7932
7933 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7934
7935 -- The result is the new block
7936
7937 Rewrite (N_Orig,
7938 Make_Block_Statement (Loc,
7939 Declarations => Decls,
7940 Handled_Statement_Sequence =>
7941 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7942
7943 Analyze (N_Orig);
7944 end Expand_N_Asynchronous_Select;
7945
7946 -------------------------------------
7947 -- Expand_N_Conditional_Entry_Call --
7948 -------------------------------------
7949
7950 -- The conditional task entry call is converted to a call to
7951 -- Task_Entry_Call:
7952
7953 -- declare
7954 -- B : Boolean;
7955 -- P : parms := (parm, parm, parm);
7956
7957 -- begin
7958 -- Task_Entry_Call
7959 -- (<acceptor-task>, -- Acceptor
7960 -- <entry-index>, -- E
7961 -- P'Address, -- Uninterpreted_Data
7962 -- Conditional_Call, -- Mode
7963 -- B); -- Rendezvous_Successful
7964 -- parm := P.param;
7965 -- parm := P.param;
7966 -- ...
7967 -- if B then
7968 -- normal-statements
7969 -- else
7970 -- else-statements
7971 -- end if;
7972 -- end;
7973
7974 -- For a description of the use of P and the assignments after the call,
7975 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7976 -- conditional entry call has already been expanded (by the Expand_N_Entry
7977 -- _Call_Statement procedure) as follows:
7978
7979 -- declare
7980 -- P : parms := (parm, parm, parm);
7981 -- begin
7982 -- ... info for in-out parameters
7983 -- Call_Simple (acceptor-task, entry-index, P'Address);
7984 -- parm := P.param;
7985 -- parm := P.param;
7986 -- ...
7987 -- end;
7988
7989 -- so the task at hand is to convert the latter expansion into the former
7990
7991 -- The conditional protected entry call is converted to a call to
7992 -- Protected_Entry_Call:
7993
7994 -- declare
7995 -- P : parms := (parm, parm, parm);
7996 -- Bnn : Communications_Block;
7997
7998 -- begin
7999 -- Protected_Entry_Call
8000 -- (po._object'Access, -- Object
8001 -- <entry index>, -- E
8002 -- P'Address, -- Uninterpreted_Data
8003 -- Conditional_Call, -- Mode
8004 -- Bnn); -- Block
8005 -- parm := P.param;
8006 -- parm := P.param;
8007 -- ...
8008 -- if Cancelled (Bnn) then
8009 -- else-statements
8010 -- else
8011 -- normal-statements
8012 -- end if;
8013 -- end;
8014
8015 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
8016 -- into:
8017
8018 -- declare
8019 -- B : Boolean := False;
8020 -- C : Ada.Tags.Prim_Op_Kind;
8021 -- K : Ada.Tags.Tagged_Kind :=
8022 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8023 -- P : Parameters := (Param1 .. ParamN);
8024 -- S : Integer;
8025
8026 -- begin
8027 -- if K = Ada.Tags.TK_Limited_Tagged
8028 -- or else K = Ada.Tags.TK_Tagged
8029 -- then
8030 -- <dispatching-call>;
8031 -- <triggering-statements>
8032
8033 -- else
8034 -- S :=
8035 -- Ada.Tags.Get_Offset_Index
8036 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
8037
8038 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8039
8040 -- if C = POK_Protected_Entry
8041 -- or else C = POK_Task_Entry
8042 -- then
8043 -- Param1 := P.Param1;
8044 -- ...
8045 -- ParamN := P.ParamN;
8046 -- end if;
8047
8048 -- if B then
8049 -- if C = POK_Procedure
8050 -- or else C = POK_Protected_Procedure
8051 -- or else C = POK_Task_Procedure
8052 -- then
8053 -- <dispatching-call>;
8054 -- end if;
8055
8056 -- <triggering-statements>
8057 -- else
8058 -- <else-statements>
8059 -- end if;
8060 -- end if;
8061 -- end;
8062
8063 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
8064 Loc : constant Source_Ptr := Sloc (N);
8065 Alt : constant Node_Id := Entry_Call_Alternative (N);
8066 Blk : Node_Id := Entry_Call_Statement (Alt);
8067
8068 Actuals : List_Id;
8069 Blk_Typ : Entity_Id;
8070 Call : Node_Id;
8071 Call_Ent : Entity_Id;
8072 Conc_Typ_Stmts : List_Id;
8073 Decl : Node_Id;
8074 Decls : List_Id;
8075 Formals : List_Id;
8076 Lim_Typ_Stmts : List_Id;
8077 N_Stats : List_Id;
8078 Obj : Entity_Id;
8079 Param : Node_Id;
8080 Params : List_Id;
8081 Stmt : Node_Id;
8082 Stmts : List_Id;
8083 Transient_Blk : Node_Id;
8084 Unpack : List_Id;
8085
8086 B : Entity_Id; -- Call status flag
8087 C : Entity_Id; -- Call kind
8088 K : Entity_Id; -- Tagged kind
8089 P : Entity_Id; -- Parameter block
8090 S : Entity_Id; -- Primitive operation slot
8091
8092 begin
8093 Process_Statements_For_Controlled_Objects (N);
8094
8095 if Ada_Version >= Ada_2005
8096 and then Nkind (Blk) = N_Procedure_Call_Statement
8097 then
8098 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
8099
8100 Decls := New_List;
8101 Stmts := New_List;
8102
8103 -- Call status flag processing, generate:
8104 -- B : Boolean := False;
8105
8106 B := Build_B (Loc, Decls);
8107
8108 -- Call kind processing, generate:
8109 -- C : Ada.Tags.Prim_Op_Kind;
8110
8111 C := Build_C (Loc, Decls);
8112
8113 -- Tagged kind processing, generate:
8114 -- K : Ada.Tags.Tagged_Kind :=
8115 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8116
8117 K := Build_K (Loc, Decls, Obj);
8118
8119 -- Parameter block processing
8120
8121 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
8122 P := Parameter_Block_Pack
8123 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
8124
8125 -- Dispatch table slot processing, generate:
8126 -- S : Integer;
8127
8128 S := Build_S (Loc, Decls);
8129
8130 -- Generate:
8131 -- S := Ada.Tags.Get_Offset_Index
8132 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
8133
8134 Conc_Typ_Stmts :=
8135 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
8136
8137 -- Generate:
8138 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8139
8140 Append_To (Conc_Typ_Stmts,
8141 Make_Procedure_Call_Statement (Loc,
8142 Name =>
8143 New_Occurrence_Of (
8144 Find_Prim_Op (Etype (Etype (Obj)),
8145 Name_uDisp_Conditional_Select),
8146 Loc),
8147 Parameter_Associations =>
8148 New_List (
8149 New_Copy_Tree (Obj), -- <object>
8150 New_Occurrence_Of (S, Loc), -- S
8151 Make_Attribute_Reference (Loc, -- P'Address
8152 Prefix => New_Occurrence_Of (P, Loc),
8153 Attribute_Name => Name_Address),
8154 New_Occurrence_Of (C, Loc), -- C
8155 New_Occurrence_Of (B, Loc)))); -- B
8156
8157 -- Generate:
8158 -- if C = POK_Protected_Entry
8159 -- or else C = POK_Task_Entry
8160 -- then
8161 -- Param1 := P.Param1;
8162 -- ...
8163 -- ParamN := P.ParamN;
8164 -- end if;
8165
8166 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
8167
8168 -- Generate the if statement only when the packed parameters need
8169 -- explicit assignments to their corresponding actuals.
8170
8171 if Present (Unpack) then
8172 Append_To (Conc_Typ_Stmts,
8173 Make_Implicit_If_Statement (N,
8174 Condition =>
8175 Make_Or_Else (Loc,
8176 Left_Opnd =>
8177 Make_Op_Eq (Loc,
8178 Left_Opnd =>
8179 New_Occurrence_Of (C, Loc),
8180 Right_Opnd =>
8181 New_Occurrence_Of (RTE (
8182 RE_POK_Protected_Entry), Loc)),
8183
8184 Right_Opnd =>
8185 Make_Op_Eq (Loc,
8186 Left_Opnd =>
8187 New_Occurrence_Of (C, Loc),
8188 Right_Opnd =>
8189 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
8190
8191 Then_Statements => Unpack));
8192 end if;
8193
8194 -- Generate:
8195 -- if B then
8196 -- if C = POK_Procedure
8197 -- or else C = POK_Protected_Procedure
8198 -- or else C = POK_Task_Procedure
8199 -- then
8200 -- <dispatching-call>
8201 -- end if;
8202 -- <normal-statements>
8203 -- else
8204 -- <else-statements>
8205 -- end if;
8206
8207 N_Stats := New_Copy_List_Tree (Statements (Alt));
8208
8209 Prepend_To (N_Stats,
8210 Make_Implicit_If_Statement (N,
8211 Condition =>
8212 Make_Or_Else (Loc,
8213 Left_Opnd =>
8214 Make_Op_Eq (Loc,
8215 Left_Opnd =>
8216 New_Occurrence_Of (C, Loc),
8217 Right_Opnd =>
8218 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
8219
8220 Right_Opnd =>
8221 Make_Or_Else (Loc,
8222 Left_Opnd =>
8223 Make_Op_Eq (Loc,
8224 Left_Opnd =>
8225 New_Occurrence_Of (C, Loc),
8226 Right_Opnd =>
8227 New_Occurrence_Of (RTE (
8228 RE_POK_Protected_Procedure), Loc)),
8229
8230 Right_Opnd =>
8231 Make_Op_Eq (Loc,
8232 Left_Opnd =>
8233 New_Occurrence_Of (C, Loc),
8234 Right_Opnd =>
8235 New_Occurrence_Of (RTE (
8236 RE_POK_Task_Procedure), Loc)))),
8237
8238 Then_Statements =>
8239 New_List (Blk)));
8240
8241 Append_To (Conc_Typ_Stmts,
8242 Make_Implicit_If_Statement (N,
8243 Condition => New_Occurrence_Of (B, Loc),
8244 Then_Statements => N_Stats,
8245 Else_Statements => Else_Statements (N)));
8246
8247 -- Generate:
8248 -- <dispatching-call>;
8249 -- <triggering-statements>
8250
8251 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8252 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8253
8254 -- Generate:
8255 -- if K = Ada.Tags.TK_Limited_Tagged
8256 -- or else K = Ada.Tags.TK_Tagged
8257 -- then
8258 -- Lim_Typ_Stmts
8259 -- else
8260 -- Conc_Typ_Stmts
8261 -- end if;
8262
8263 Append_To (Stmts,
8264 Make_Implicit_If_Statement (N,
8265 Condition => Build_Dispatching_Tag_Check (K, N),
8266 Then_Statements => Lim_Typ_Stmts,
8267 Else_Statements => Conc_Typ_Stmts));
8268
8269 Rewrite (N,
8270 Make_Block_Statement (Loc,
8271 Declarations =>
8272 Decls,
8273 Handled_Statement_Sequence =>
8274 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8275
8276 -- As described above, the entry alternative is transformed into a
8277 -- block that contains the gnulli call, and possibly assignment
8278 -- statements for in-out parameters. The gnulli call may itself be
8279 -- rewritten into a transient block if some unconstrained parameters
8280 -- require it. We need to retrieve the call to complete its parameter
8281 -- list.
8282
8283 else
8284 Transient_Blk :=
8285 First_Real_Statement (Handled_Statement_Sequence (Blk));
8286
8287 if Present (Transient_Blk)
8288 and then Nkind (Transient_Blk) = N_Block_Statement
8289 then
8290 Blk := Transient_Blk;
8291 end if;
8292
8293 Stmts := Statements (Handled_Statement_Sequence (Blk));
8294 Stmt := First (Stmts);
8295 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8296 Next (Stmt);
8297 end loop;
8298
8299 Call := Stmt;
8300 Params := Parameter_Associations (Call);
8301
8302 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8303
8304 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8305
8306 Param := First (Params);
8307 while Present (Param)
8308 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8309 loop
8310 Next (Param);
8311 end loop;
8312
8313 pragma Assert (Present (Param));
8314 Rewrite (Param,
8315 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8316
8317 Analyze (Param);
8318
8319 -- Find the Communication_Block parameter for the call to the
8320 -- Cancelled function.
8321
8322 Decl := First (Declarations (Blk));
8323 while Present (Decl)
8324 and then not Is_RTE (Etype (Object_Definition (Decl)),
8325 RE_Communication_Block)
8326 loop
8327 Next (Decl);
8328 end loop;
8329
8330 -- Add an if statement to execute the else part if the call
8331 -- does not succeed (as indicated by the Cancelled predicate).
8332
8333 Append_To (Stmts,
8334 Make_Implicit_If_Statement (N,
8335 Condition => Make_Function_Call (Loc,
8336 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8337 Parameter_Associations => New_List (
8338 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8339 Then_Statements => Else_Statements (N),
8340 Else_Statements => Statements (Alt)));
8341
8342 else
8343 B := Make_Defining_Identifier (Loc, Name_uB);
8344
8345 -- Insert declaration of B in declarations of existing block
8346
8347 if No (Declarations (Blk)) then
8348 Set_Declarations (Blk, New_List);
8349 end if;
8350
8351 Prepend_To (Declarations (Blk),
8352 Make_Object_Declaration (Loc,
8353 Defining_Identifier => B,
8354 Object_Definition =>
8355 New_Occurrence_Of (Standard_Boolean, Loc)));
8356
8357 -- Create new call statement
8358
8359 Append_To (Params,
8360 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8361 Append_To (Params, New_Occurrence_Of (B, Loc));
8362
8363 Rewrite (Call,
8364 Make_Procedure_Call_Statement (Loc,
8365 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8366 Parameter_Associations => Params));
8367
8368 -- Construct statement sequence for new block
8369
8370 Append_To (Stmts,
8371 Make_Implicit_If_Statement (N,
8372 Condition => New_Occurrence_Of (B, Loc),
8373 Then_Statements => Statements (Alt),
8374 Else_Statements => Else_Statements (N)));
8375 end if;
8376
8377 -- The result is the new block
8378
8379 Rewrite (N,
8380 Make_Block_Statement (Loc,
8381 Declarations => Declarations (Blk),
8382 Handled_Statement_Sequence =>
8383 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8384 end if;
8385
8386 Analyze (N);
8387 end Expand_N_Conditional_Entry_Call;
8388
8389 ---------------------------------------
8390 -- Expand_N_Delay_Relative_Statement --
8391 ---------------------------------------
8392
8393 -- Delay statement is implemented as a procedure call to Delay_For
8394 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8395 -- simple delays imposed by the use of Protected Objects.
8396
8397 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8398 Loc : constant Source_Ptr := Sloc (N);
8399 Proc : Entity_Id;
8400
8401 begin
8402 -- Try to use System.Relative_Delays.Delay_For only if available. This
8403 -- is the implementation used on restricted platforms when Ada.Calendar
8404 -- is not available.
8405
8406 if RTE_Available (RO_RD_Delay_For) then
8407 Proc := RTE (RO_RD_Delay_For);
8408
8409 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8410 -- message if not available.
8411
8412 else
8413 Proc := RTE (RO_CA_Delay_For);
8414 end if;
8415
8416 Rewrite (N,
8417 Make_Procedure_Call_Statement (Loc,
8418 Name => New_Occurrence_Of (Proc, Loc),
8419 Parameter_Associations => New_List (Expression (N))));
8420 Analyze (N);
8421 end Expand_N_Delay_Relative_Statement;
8422
8423 ------------------------------------
8424 -- Expand_N_Delay_Until_Statement --
8425 ------------------------------------
8426
8427 -- Delay Until statement is implemented as a procedure call to
8428 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8429
8430 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8431 Loc : constant Source_Ptr := Sloc (N);
8432 Typ : Entity_Id;
8433
8434 begin
8435 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8436 Typ := RTE (RO_CA_Delay_Until);
8437 else
8438 Typ := RTE (RO_RT_Delay_Until);
8439 end if;
8440
8441 Rewrite (N,
8442 Make_Procedure_Call_Statement (Loc,
8443 Name => New_Occurrence_Of (Typ, Loc),
8444 Parameter_Associations => New_List (Expression (N))));
8445
8446 Analyze (N);
8447 end Expand_N_Delay_Until_Statement;
8448
8449 -------------------------
8450 -- Expand_N_Entry_Body --
8451 -------------------------
8452
8453 procedure Expand_N_Entry_Body (N : Node_Id) is
8454 begin
8455 -- Associate discriminals with the next protected operation body to be
8456 -- expanded.
8457
8458 if Present (Next_Protected_Operation (N)) then
8459 Set_Discriminals (Parent (Current_Scope));
8460 end if;
8461 end Expand_N_Entry_Body;
8462
8463 -----------------------------------
8464 -- Expand_N_Entry_Call_Statement --
8465 -----------------------------------
8466
8467 -- An entry call is expanded into GNARLI calls to implement a simple entry
8468 -- call (see Build_Simple_Entry_Call).
8469
8470 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8471 Concval : Node_Id;
8472 Ename : Node_Id;
8473 Index : Node_Id;
8474
8475 begin
8476 if No_Run_Time_Mode then
8477 Error_Msg_CRT ("entry call", N);
8478 return;
8479 end if;
8480
8481 -- If this entry call is part of an asynchronous select, don't expand it
8482 -- here; it will be expanded with the select statement. Don't expand
8483 -- timed entry calls either, as they are translated into asynchronous
8484 -- entry calls.
8485
8486 -- ??? This whole approach is questionable; it may be better to go back
8487 -- to allowing the expansion to take place and then attempting to fix it
8488 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8489 -- whether the expanded call is on a task or protected entry.
8490
8491 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8492 or else N /= Triggering_Statement (Parent (N)))
8493 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8494 or else N /= Entry_Call_Statement (Parent (N))
8495 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8496 then
8497 Extract_Entry (N, Concval, Ename, Index);
8498 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8499 end if;
8500 end Expand_N_Entry_Call_Statement;
8501
8502 --------------------------------
8503 -- Expand_N_Entry_Declaration --
8504 --------------------------------
8505
8506 -- If there are parameters, then first, each of the formals is marked by
8507 -- setting Is_Entry_Formal. Next a record type is built which is used to
8508 -- hold the parameter values. The name of this record type is entryP where
8509 -- entry is the name of the entry, with an additional corresponding access
8510 -- type called entryPA. The record type has matching components for each
8511 -- formal (the component names are the same as the formal names). For
8512 -- elementary types, the component type matches the formal type. For
8513 -- composite types, an access type is declared (with the name formalA)
8514 -- which designates the formal type, and the type of the component is this
8515 -- access type. Finally the Entry_Component of each formal is set to
8516 -- reference the corresponding record component.
8517
8518 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8519 Loc : constant Source_Ptr := Sloc (N);
8520 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8521 Components : List_Id;
8522 Formal : Node_Id;
8523 Ftype : Entity_Id;
8524 Last_Decl : Node_Id;
8525 Component : Entity_Id;
8526 Ctype : Entity_Id;
8527 Decl : Node_Id;
8528 Rec_Ent : Entity_Id;
8529 Acc_Ent : Entity_Id;
8530
8531 begin
8532 Formal := First_Formal (Entry_Ent);
8533 Last_Decl := N;
8534
8535 -- Most processing is done only if parameters are present
8536
8537 if Present (Formal) then
8538 Components := New_List;
8539
8540 -- Loop through formals
8541
8542 while Present (Formal) loop
8543 Set_Is_Entry_Formal (Formal);
8544 Component :=
8545 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8546 Set_Entry_Component (Formal, Component);
8547 Set_Entry_Formal (Component, Formal);
8548 Ftype := Etype (Formal);
8549
8550 -- Declare new access type and then append
8551
8552 Ctype := Make_Temporary (Loc, 'A');
8553 Set_Is_Param_Block_Component_Type (Ctype);
8554
8555 Decl :=
8556 Make_Full_Type_Declaration (Loc,
8557 Defining_Identifier => Ctype,
8558 Type_Definition =>
8559 Make_Access_To_Object_Definition (Loc,
8560 All_Present => True,
8561 Constant_Present => Ekind (Formal) = E_In_Parameter,
8562 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8563
8564 Insert_After (Last_Decl, Decl);
8565 Last_Decl := Decl;
8566
8567 Append_To (Components,
8568 Make_Component_Declaration (Loc,
8569 Defining_Identifier => Component,
8570 Component_Definition =>
8571 Make_Component_Definition (Loc,
8572 Aliased_Present => False,
8573 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8574
8575 Next_Formal_With_Extras (Formal);
8576 end loop;
8577
8578 -- Create the Entry_Parameter_Record declaration
8579
8580 Rec_Ent := Make_Temporary (Loc, 'P');
8581
8582 Decl :=
8583 Make_Full_Type_Declaration (Loc,
8584 Defining_Identifier => Rec_Ent,
8585 Type_Definition =>
8586 Make_Record_Definition (Loc,
8587 Component_List =>
8588 Make_Component_List (Loc,
8589 Component_Items => Components)));
8590
8591 Insert_After (Last_Decl, Decl);
8592 Last_Decl := Decl;
8593
8594 -- Construct and link in the corresponding access type
8595
8596 Acc_Ent := Make_Temporary (Loc, 'A');
8597
8598 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8599
8600 Decl :=
8601 Make_Full_Type_Declaration (Loc,
8602 Defining_Identifier => Acc_Ent,
8603 Type_Definition =>
8604 Make_Access_To_Object_Definition (Loc,
8605 All_Present => True,
8606 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8607
8608 Insert_After (Last_Decl, Decl);
8609 end if;
8610 end Expand_N_Entry_Declaration;
8611
8612 -----------------------------
8613 -- Expand_N_Protected_Body --
8614 -----------------------------
8615
8616 -- Protected bodies are expanded to the completion of the subprograms
8617 -- created for the corresponding protected type. These are a protected and
8618 -- unprotected version of each protected subprogram in the object, a
8619 -- function to calculate each entry barrier, and a procedure to execute the
8620 -- sequence of statements of each protected entry body. For example, for
8621 -- protected type ptype:
8622
8623 -- function entB
8624 -- (O : System.Address;
8625 -- E : Protected_Entry_Index)
8626 -- return Boolean
8627 -- is
8628 -- <discriminant renamings>
8629 -- <private object renamings>
8630 -- begin
8631 -- return <barrier expression>;
8632 -- end entB;
8633
8634 -- procedure pprocN (_object : in out poV;...) is
8635 -- <discriminant renamings>
8636 -- <private object renamings>
8637 -- begin
8638 -- <sequence of statements>
8639 -- end pprocN;
8640
8641 -- procedure pprocP (_object : in out poV;...) is
8642 -- procedure _clean is
8643 -- Pn : Boolean;
8644 -- begin
8645 -- ptypeS (_object, Pn);
8646 -- Unlock (_object._object'Access);
8647 -- Abort_Undefer.all;
8648 -- end _clean;
8649
8650 -- begin
8651 -- Abort_Defer.all;
8652 -- Lock (_object._object'Access);
8653 -- pprocN (_object;...);
8654 -- at end
8655 -- _clean;
8656 -- end pproc;
8657
8658 -- function pfuncN (_object : poV;...) return Return_Type is
8659 -- <discriminant renamings>
8660 -- <private object renamings>
8661 -- begin
8662 -- <sequence of statements>
8663 -- end pfuncN;
8664
8665 -- function pfuncP (_object : poV) return Return_Type is
8666 -- procedure _clean is
8667 -- begin
8668 -- Unlock (_object._object'Access);
8669 -- Abort_Undefer.all;
8670 -- end _clean;
8671
8672 -- begin
8673 -- Abort_Defer.all;
8674 -- Lock (_object._object'Access);
8675 -- return pfuncN (_object);
8676
8677 -- at end
8678 -- _clean;
8679 -- end pfunc;
8680
8681 -- procedure entE
8682 -- (O : System.Address;
8683 -- P : System.Address;
8684 -- E : Protected_Entry_Index)
8685 -- is
8686 -- <discriminant renamings>
8687 -- <private object renamings>
8688 -- type poVP is access poV;
8689 -- _Object : ptVP := ptVP!(O);
8690
8691 -- begin
8692 -- begin
8693 -- <statement sequence>
8694 -- Complete_Entry_Body (_Object._Object);
8695 -- exception
8696 -- when all others =>
8697 -- Exceptional_Complete_Entry_Body (
8698 -- _Object._Object, Get_GNAT_Exception);
8699 -- end;
8700 -- end entE;
8701
8702 -- The type poV is the record created for the protected type to hold
8703 -- the state of the protected object.
8704
8705 procedure Expand_N_Protected_Body (N : Node_Id) is
8706 Loc : constant Source_Ptr := Sloc (N);
8707 Pid : constant Entity_Id := Corresponding_Spec (N);
8708
8709 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8710 -- This flag indicates whether the lock free implementation is active
8711
8712 Current_Node : Node_Id;
8713 Disp_Op_Body : Node_Id;
8714 New_Op_Body : Node_Id;
8715 Op_Body : Node_Id;
8716 Op_Id : Entity_Id;
8717
8718 function Build_Dispatching_Subprogram_Body
8719 (N : Node_Id;
8720 Pid : Node_Id;
8721 Prot_Bod : Node_Id) return Node_Id;
8722 -- Build a dispatching version of the protected subprogram body. The
8723 -- newly generated subprogram contains a call to the original protected
8724 -- body. The following code is generated:
8725 --
8726 -- function <protected-function-name> (Param1 .. ParamN) return
8727 -- <return-type> is
8728 -- begin
8729 -- return <protected-function-name>P (Param1 .. ParamN);
8730 -- end <protected-function-name>;
8731 --
8732 -- or
8733 --
8734 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8735 -- begin
8736 -- <protected-procedure-name>P (Param1 .. ParamN);
8737 -- end <protected-procedure-name>
8738
8739 ---------------------------------------
8740 -- Build_Dispatching_Subprogram_Body --
8741 ---------------------------------------
8742
8743 function Build_Dispatching_Subprogram_Body
8744 (N : Node_Id;
8745 Pid : Node_Id;
8746 Prot_Bod : Node_Id) return Node_Id
8747 is
8748 Loc : constant Source_Ptr := Sloc (N);
8749 Actuals : List_Id;
8750 Formal : Node_Id;
8751 Spec : Node_Id;
8752 Stmts : List_Id;
8753
8754 begin
8755 -- Generate a specification without a letter suffix in order to
8756 -- override an interface function or procedure.
8757
8758 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8759
8760 -- The formal parameters become the actuals of the protected function
8761 -- or procedure call.
8762
8763 Actuals := New_List;
8764 Formal := First (Parameter_Specifications (Spec));
8765 while Present (Formal) loop
8766 Append_To (Actuals,
8767 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8768 Next (Formal);
8769 end loop;
8770
8771 if Nkind (Spec) = N_Procedure_Specification then
8772 Stmts :=
8773 New_List (
8774 Make_Procedure_Call_Statement (Loc,
8775 Name =>
8776 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8777 Parameter_Associations => Actuals));
8778
8779 else
8780 pragma Assert (Nkind (Spec) = N_Function_Specification);
8781
8782 Stmts :=
8783 New_List (
8784 Make_Simple_Return_Statement (Loc,
8785 Expression =>
8786 Make_Function_Call (Loc,
8787 Name =>
8788 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8789 Parameter_Associations => Actuals)));
8790 end if;
8791
8792 return
8793 Make_Subprogram_Body (Loc,
8794 Declarations => Empty_List,
8795 Specification => Spec,
8796 Handled_Statement_Sequence =>
8797 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8798 end Build_Dispatching_Subprogram_Body;
8799
8800 -- Start of processing for Expand_N_Protected_Body
8801
8802 begin
8803 if No_Run_Time_Mode then
8804 Error_Msg_CRT ("protected body", N);
8805 return;
8806 end if;
8807
8808 -- This is the proper body corresponding to a stub. The declarations
8809 -- must be inserted at the point of the stub, which in turn is in the
8810 -- declarative part of the parent unit.
8811
8812 if Nkind (Parent (N)) = N_Subunit then
8813 Current_Node := Corresponding_Stub (Parent (N));
8814 else
8815 Current_Node := N;
8816 end if;
8817
8818 Op_Body := First (Declarations (N));
8819
8820 -- The protected body is replaced with the bodies of its
8821 -- protected operations, and the declarations for internal objects
8822 -- that may have been created for entry family bounds.
8823
8824 Rewrite (N, Make_Null_Statement (Sloc (N)));
8825 Analyze (N);
8826
8827 while Present (Op_Body) loop
8828 case Nkind (Op_Body) is
8829 when N_Subprogram_Declaration =>
8830 null;
8831
8832 when N_Subprogram_Body =>
8833
8834 -- Do not create bodies for eliminated operations
8835
8836 if not Is_Eliminated (Defining_Entity (Op_Body))
8837 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8838 then
8839 if Lock_Free_Active then
8840 New_Op_Body :=
8841 Build_Lock_Free_Unprotected_Subprogram_Body
8842 (Op_Body, Pid);
8843 else
8844 New_Op_Body :=
8845 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8846 end if;
8847
8848 Insert_After (Current_Node, New_Op_Body);
8849 Current_Node := New_Op_Body;
8850 Analyze (New_Op_Body);
8851
8852 -- Build the corresponding protected operation. It may
8853 -- appear that this is needed only if this is a visible
8854 -- operation of the type, or if it is an interrupt handler,
8855 -- and this was the strategy used previously in GNAT.
8856
8857 -- However, the operation may be exported through a 'Access
8858 -- to an external caller. This is the common idiom in code
8859 -- that uses the Ada 2005 Timing_Events package. As a result
8860 -- we need to produce the protected body for both visible
8861 -- and private operations, as well as operations that only
8862 -- have a body in the source, and for which we create a
8863 -- declaration in the protected body itself.
8864
8865 if Present (Corresponding_Spec (Op_Body)) then
8866 if Lock_Free_Active then
8867 New_Op_Body :=
8868 Build_Lock_Free_Protected_Subprogram_Body
8869 (Op_Body, Pid, Specification (New_Op_Body));
8870 else
8871 New_Op_Body :=
8872 Build_Protected_Subprogram_Body
8873 (Op_Body, Pid, Specification (New_Op_Body));
8874 end if;
8875
8876 Insert_After (Current_Node, New_Op_Body);
8877 Analyze (New_Op_Body);
8878
8879 Current_Node := New_Op_Body;
8880
8881 -- Generate an overriding primitive operation body for
8882 -- this subprogram if the protected type implements an
8883 -- interface.
8884
8885 if Ada_Version >= Ada_2005
8886 and then
8887 Present (Interfaces (Corresponding_Record_Type (Pid)))
8888 then
8889 Disp_Op_Body :=
8890 Build_Dispatching_Subprogram_Body
8891 (Op_Body, Pid, New_Op_Body);
8892
8893 Insert_After (Current_Node, Disp_Op_Body);
8894 Analyze (Disp_Op_Body);
8895
8896 Current_Node := Disp_Op_Body;
8897 end if;
8898 end if;
8899 end if;
8900
8901 when N_Entry_Body =>
8902 Op_Id := Defining_Identifier (Op_Body);
8903 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8904
8905 Insert_After (Current_Node, New_Op_Body);
8906 Current_Node := New_Op_Body;
8907 Analyze (New_Op_Body);
8908
8909 when N_Implicit_Label_Declaration =>
8910 null;
8911
8912 when N_Itype_Reference =>
8913 Insert_After (Current_Node, New_Copy (Op_Body));
8914
8915 when N_Freeze_Entity =>
8916 New_Op_Body := New_Copy (Op_Body);
8917
8918 if Present (Entity (Op_Body))
8919 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8920 then
8921 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8922 end if;
8923
8924 Insert_After (Current_Node, New_Op_Body);
8925 Current_Node := New_Op_Body;
8926 Analyze (New_Op_Body);
8927
8928 when N_Pragma =>
8929 New_Op_Body := New_Copy (Op_Body);
8930 Insert_After (Current_Node, New_Op_Body);
8931 Current_Node := New_Op_Body;
8932 Analyze (New_Op_Body);
8933
8934 when N_Object_Declaration =>
8935 pragma Assert (not Comes_From_Source (Op_Body));
8936 New_Op_Body := New_Copy (Op_Body);
8937 Insert_After (Current_Node, New_Op_Body);
8938 Current_Node := New_Op_Body;
8939 Analyze (New_Op_Body);
8940
8941 when others =>
8942 raise Program_Error;
8943
8944 end case;
8945
8946 Next (Op_Body);
8947 end loop;
8948
8949 -- Finally, create the body of the function that maps an entry index
8950 -- into the corresponding body index, except when there is no entry, or
8951 -- in a Ravenscar-like profile.
8952
8953 if Corresponding_Runtime_Package (Pid) =
8954 System_Tasking_Protected_Objects_Entries
8955 then
8956 New_Op_Body := Build_Find_Body_Index (Pid);
8957 Insert_After (Current_Node, New_Op_Body);
8958 Current_Node := New_Op_Body;
8959 Analyze (New_Op_Body);
8960 end if;
8961
8962 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8963 -- protected body. At this point all wrapper specs have been created,
8964 -- frozen and included in the dispatch table for the protected type.
8965
8966 if Ada_Version >= Ada_2005 then
8967 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8968 end if;
8969 end Expand_N_Protected_Body;
8970
8971 -----------------------------------------
8972 -- Expand_N_Protected_Type_Declaration --
8973 -----------------------------------------
8974
8975 -- First we create a corresponding record type declaration used to
8976 -- represent values of this protected type.
8977 -- The general form of this type declaration is
8978
8979 -- type poV (discriminants) is record
8980 -- _Object : aliased <kind>Protection
8981 -- [(<entry count> [, <handler count>])];
8982 -- [entry_family : array (bounds) of Void;]
8983 -- <private data fields>
8984 -- end record;
8985
8986 -- The discriminants are present only if the corresponding protected type
8987 -- has discriminants, and they exactly mirror the protected type
8988 -- discriminants. The private data fields similarly mirror the private
8989 -- declarations of the protected type.
8990
8991 -- The Object field is always present. It contains RTS specific data used
8992 -- to control the protected object. It is declared as Aliased so that it
8993 -- can be passed as a pointer to the RTS. This allows the protected record
8994 -- to be referenced within RTS data structures. An appropriate Protection
8995 -- type and discriminant are generated.
8996
8997 -- The Service field is present for protected objects with entries. It
8998 -- contains sufficient information to allow the entry service procedure for
8999 -- this object to be called when the object is not known till runtime.
9000
9001 -- One entry_family component is present for each entry family in the
9002 -- task definition (see Expand_N_Task_Type_Declaration).
9003
9004 -- When a protected object is declared, an instance of the protected type
9005 -- value record is created. The elaboration of this declaration creates the
9006 -- correct bounds for the entry families, and also evaluates the priority
9007 -- expression if needed. The initialization routine for the protected type
9008 -- itself then calls Initialize_Protection with appropriate parameters to
9009 -- initialize the value of the Task_Id field. Install_Handlers may be also
9010 -- called if a pragma Attach_Handler applies.
9011
9012 -- Note: this record is passed to the subprograms created by the expansion
9013 -- of protected subprograms and entries. It is an in parameter to protected
9014 -- functions and an in out parameter to procedures and entry bodies. The
9015 -- Entity_Id for this created record type is placed in the
9016 -- Corresponding_Record_Type field of the associated protected type entity.
9017
9018 -- Next we create a procedure specifications for protected subprograms and
9019 -- entry bodies. For each protected subprograms two subprograms are
9020 -- created, an unprotected and a protected version. The unprotected version
9021 -- is called from within other operations of the same protected object.
9022
9023 -- We also build the call to register the procedure if a pragma
9024 -- Interrupt_Handler applies.
9025
9026 -- A single subprogram is created to service all entry bodies; it has an
9027 -- additional boolean out parameter indicating that the previous entry call
9028 -- made by the current task was serviced immediately, i.e. not by proxy.
9029 -- The O parameter contains a pointer to a record object of the type
9030 -- described above. An untyped interface is used here to allow this
9031 -- procedure to be called in places where the type of the object to be
9032 -- serviced is not known. This must be done, for example, when a call that
9033 -- may have been requeued is cancelled; the corresponding object must be
9034 -- serviced, but which object that is not known till runtime.
9035
9036 -- procedure ptypeS
9037 -- (O : System.Address; P : out Boolean);
9038 -- procedure pprocN (_object : in out poV);
9039 -- procedure pproc (_object : in out poV);
9040 -- function pfuncN (_object : poV);
9041 -- function pfunc (_object : poV);
9042 -- ...
9043
9044 -- Note that this must come after the record type declaration, since
9045 -- the specs refer to this type.
9046
9047 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
9048 Discr_Map : constant Elist_Id := New_Elmt_List;
9049 Loc : constant Source_Ptr := Sloc (N);
9050 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
9051
9052 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
9053 -- This flag indicates whether the lock free implementation is active
9054
9055 Pdef : constant Node_Id := Protected_Definition (N);
9056 -- This contains two lists; one for visible and one for private decls
9057
9058 Current_Node : Node_Id := N;
9059 E_Count : Int;
9060 Entries_Aggr : Node_Id;
9061
9062 procedure Check_Inlining (Subp : Entity_Id);
9063 -- If the original operation has a pragma Inline, propagate the flag
9064 -- to the internal body, for possible inlining later on. The source
9065 -- operation is invisible to the back-end and is never actually called.
9066
9067 function Discriminated_Size (Comp : Entity_Id) return Boolean;
9068 -- If a component size is not static then a warning will be emitted
9069 -- in Ravenscar or other restricted contexts. When a component is non-
9070 -- static because of a discriminant constraint we can specialize the
9071 -- warning by mentioning discriminants explicitly.
9072
9073 procedure Expand_Entry_Declaration (Decl : Node_Id);
9074 -- Create the entry barrier and the procedure body for entry declaration
9075 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
9076
9077 function Static_Component_Size (Comp : Entity_Id) return Boolean;
9078 -- When compiling under the Ravenscar profile, private components must
9079 -- have a static size, or else a protected object will require heap
9080 -- allocation, violating the corresponding restriction. It is preferable
9081 -- to make this check here, because it provides a better error message
9082 -- than the back-end, which refers to the object as a whole.
9083
9084 procedure Register_Handler;
9085 -- For a protected operation that is an interrupt handler, add the
9086 -- freeze action that will register it as such.
9087
9088 --------------------
9089 -- Check_Inlining --
9090 --------------------
9091
9092 procedure Check_Inlining (Subp : Entity_Id) is
9093 begin
9094 if Is_Inlined (Subp) then
9095 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
9096 Set_Is_Inlined (Subp, False);
9097 end if;
9098 end Check_Inlining;
9099
9100 ------------------------
9101 -- Discriminated_Size --
9102 ------------------------
9103
9104 function Discriminated_Size (Comp : Entity_Id) return Boolean is
9105 Typ : constant Entity_Id := Etype (Comp);
9106 Index : Node_Id;
9107
9108 function Non_Static_Bound (Bound : Node_Id) return Boolean;
9109 -- Check whether the bound of an index is non-static and does denote
9110 -- a discriminant, in which case any protected object of the type
9111 -- will have a non-static size.
9112
9113 ----------------------
9114 -- Non_Static_Bound --
9115 ----------------------
9116
9117 function Non_Static_Bound (Bound : Node_Id) return Boolean is
9118 begin
9119 if Is_OK_Static_Expression (Bound) then
9120 return False;
9121
9122 elsif Is_Entity_Name (Bound)
9123 and then Present (Discriminal_Link (Entity (Bound)))
9124 then
9125 return False;
9126
9127 else
9128 return True;
9129 end if;
9130 end Non_Static_Bound;
9131
9132 -- Start of processing for Discriminated_Size
9133
9134 begin
9135 if not Is_Array_Type (Typ) then
9136 return False;
9137 end if;
9138
9139 if Ekind (Typ) = E_Array_Subtype then
9140 Index := First_Index (Typ);
9141 while Present (Index) loop
9142 if Non_Static_Bound (Low_Bound (Index))
9143 or else Non_Static_Bound (High_Bound (Index))
9144 then
9145 return False;
9146 end if;
9147
9148 Next_Index (Index);
9149 end loop;
9150
9151 return True;
9152 end if;
9153
9154 return False;
9155 end Discriminated_Size;
9156
9157 ---------------------------
9158 -- Static_Component_Size --
9159 ---------------------------
9160
9161 function Static_Component_Size (Comp : Entity_Id) return Boolean is
9162 Typ : constant Entity_Id := Etype (Comp);
9163 C : Entity_Id;
9164
9165 begin
9166 if Is_Scalar_Type (Typ) then
9167 return True;
9168
9169 elsif Is_Array_Type (Typ) then
9170 return Compile_Time_Known_Bounds (Typ);
9171
9172 elsif Is_Record_Type (Typ) then
9173 C := First_Component (Typ);
9174 while Present (C) loop
9175 if not Static_Component_Size (C) then
9176 return False;
9177 end if;
9178
9179 Next_Component (C);
9180 end loop;
9181
9182 return True;
9183
9184 -- Any other type will be checked by the back-end
9185
9186 else
9187 return True;
9188 end if;
9189 end Static_Component_Size;
9190
9191 ------------------------------
9192 -- Expand_Entry_Declaration --
9193 ------------------------------
9194
9195 procedure Expand_Entry_Declaration (Decl : Node_Id) is
9196 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
9197 Bar_Id : Entity_Id;
9198 Bod_Id : Entity_Id;
9199 Subp : Node_Id;
9200
9201 begin
9202 E_Count := E_Count + 1;
9203
9204 -- Create the protected body subprogram
9205
9206 Bod_Id :=
9207 Make_Defining_Identifier (Loc,
9208 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
9209 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
9210
9211 Subp :=
9212 Make_Subprogram_Declaration (Loc,
9213 Specification =>
9214 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
9215
9216 Insert_After (Current_Node, Subp);
9217 Current_Node := Subp;
9218
9219 Analyze (Subp);
9220
9221 -- Build a wrapper procedure to handle contract cases, preconditions,
9222 -- and postconditions.
9223
9224 Build_Contract_Wrapper (Ent_Id, N);
9225
9226 -- Create the barrier function
9227
9228 Bar_Id :=
9229 Make_Defining_Identifier (Loc,
9230 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
9231 Set_Barrier_Function (Ent_Id, Bar_Id);
9232
9233 Subp :=
9234 Make_Subprogram_Declaration (Loc,
9235 Specification =>
9236 Build_Barrier_Function_Specification (Loc, Bar_Id));
9237 Set_Is_Entry_Barrier_Function (Subp);
9238
9239 Insert_After (Current_Node, Subp);
9240 Current_Node := Subp;
9241
9242 Analyze (Subp);
9243
9244 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
9245 Set_Scope (Bar_Id, Scope (Ent_Id));
9246
9247 -- Collect pointers to the protected subprogram and the barrier
9248 -- of the current entry, for insertion into Entry_Bodies_Array.
9249
9250 Append_To (Expressions (Entries_Aggr),
9251 Make_Aggregate (Loc,
9252 Expressions => New_List (
9253 Make_Attribute_Reference (Loc,
9254 Prefix => New_Occurrence_Of (Bar_Id, Loc),
9255 Attribute_Name => Name_Unrestricted_Access),
9256 Make_Attribute_Reference (Loc,
9257 Prefix => New_Occurrence_Of (Bod_Id, Loc),
9258 Attribute_Name => Name_Unrestricted_Access))));
9259 end Expand_Entry_Declaration;
9260
9261 ----------------------
9262 -- Register_Handler --
9263 ----------------------
9264
9265 procedure Register_Handler is
9266
9267 -- All semantic checks already done in Sem_Prag
9268
9269 Prot_Proc : constant Entity_Id :=
9270 Defining_Unit_Name (Specification (Current_Node));
9271
9272 Proc_Address : constant Node_Id :=
9273 Make_Attribute_Reference (Loc,
9274 Prefix =>
9275 New_Occurrence_Of (Prot_Proc, Loc),
9276 Attribute_Name => Name_Address);
9277
9278 RTS_Call : constant Entity_Id :=
9279 Make_Procedure_Call_Statement (Loc,
9280 Name =>
9281 New_Occurrence_Of
9282 (RTE (RE_Register_Interrupt_Handler), Loc),
9283 Parameter_Associations => New_List (Proc_Address));
9284 begin
9285 Append_Freeze_Action (Prot_Proc, RTS_Call);
9286 end Register_Handler;
9287
9288 -- Local variables
9289
9290 Body_Arr : Node_Id;
9291 Body_Id : Entity_Id;
9292 Cdecls : List_Id;
9293 Comp : Node_Id;
9294 Expr : Node_Id;
9295 New_Priv : Node_Id;
9296 Obj_Def : Node_Id;
9297 Object_Comp : Node_Id;
9298 Priv : Node_Id;
9299 Rec_Decl : Node_Id;
9300 Sub : Node_Id;
9301
9302 -- Start of processing for Expand_N_Protected_Type_Declaration
9303
9304 begin
9305 if Present (Corresponding_Record_Type (Prot_Typ)) then
9306 return;
9307 else
9308 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9309 end if;
9310
9311 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9312
9313 Qualify_Entity_Names (N);
9314
9315 -- If the type has discriminants, their occurrences in the declaration
9316 -- have been replaced by the corresponding discriminals. For components
9317 -- that are constrained by discriminants, their homologues in the
9318 -- corresponding record type must refer to the discriminants of that
9319 -- record, so we must apply a new renaming to subtypes_indications:
9320
9321 -- protected discriminant => discriminal => record discriminant
9322
9323 -- This replacement is not applied to default expressions, for which
9324 -- the discriminal is correct.
9325
9326 if Has_Discriminants (Prot_Typ) then
9327 declare
9328 Disc : Entity_Id;
9329 Decl : Node_Id;
9330
9331 begin
9332 Disc := First_Discriminant (Prot_Typ);
9333 Decl := First (Discriminant_Specifications (Rec_Decl));
9334 while Present (Disc) loop
9335 Append_Elmt (Discriminal (Disc), Discr_Map);
9336 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9337 Next_Discriminant (Disc);
9338 Next (Decl);
9339 end loop;
9340 end;
9341 end if;
9342
9343 -- Fill in the component declarations
9344
9345 -- Add components for entry families. For each entry family, create an
9346 -- anonymous type declaration with the same size, and analyze the type.
9347
9348 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9349
9350 pragma Assert (Present (Pdef));
9351
9352 Insert_After (Current_Node, Rec_Decl);
9353 Current_Node := Rec_Decl;
9354
9355 -- Add private field components
9356
9357 if Present (Private_Declarations (Pdef)) then
9358 Priv := First (Private_Declarations (Pdef));
9359 while Present (Priv) loop
9360 if Nkind (Priv) = N_Component_Declaration then
9361 if not Static_Component_Size (Defining_Identifier (Priv)) then
9362
9363 -- When compiling for a restricted profile, the private
9364 -- components must have a static size. If not, this is an
9365 -- error for a single protected declaration, and rates a
9366 -- warning on a protected type declaration.
9367
9368 if not Comes_From_Source (Prot_Typ) then
9369
9370 -- It's ok to be checking this restriction at expansion
9371 -- time, because this is only for the restricted profile,
9372 -- which is not subject to strict RM conformance, so it
9373 -- is OK to miss this check in -gnatc mode.
9374
9375 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9376 Check_Restriction
9377 (No_Implicit_Protected_Object_Allocations, Priv);
9378
9379 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9380 if not Discriminated_Size (Defining_Identifier (Priv))
9381 then
9382 -- Any object of the type will be non-static.
9383
9384 Error_Msg_N ("component has non-static size??", Priv);
9385 Error_Msg_NE
9386 ("\creation of protected object of type& will "
9387 & "violate restriction "
9388 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9389 else
9390
9391 -- Object will be non-static if discriminants are.
9392
9393 Error_Msg_NE
9394 ("creation of protected object of type& with "
9395 & "non-static discriminants will violate"
9396 & " restriction No_Implicit_Heap_Allocations??",
9397 Priv, Prot_Typ);
9398 end if;
9399
9400 -- Likewise for No_Implicit_Protected_Object_Allocations
9401
9402 elsif Restriction_Active
9403 (No_Implicit_Protected_Object_Allocations)
9404 then
9405 if not Discriminated_Size (Defining_Identifier (Priv))
9406 then
9407 -- Any object of the type will be non-static.
9408
9409 Error_Msg_N ("component has non-static size??", Priv);
9410 Error_Msg_NE
9411 ("\creation of protected object of type& will "
9412 & "violate restriction "
9413 & "No_Implicit_Protected_Object_Allocations??",
9414 Priv, Prot_Typ);
9415 else
9416 -- Object will be non-static if discriminants are.
9417
9418 Error_Msg_NE
9419 ("creation of protected object of type& with "
9420 & "non-static discriminants will violate "
9421 & "restriction "
9422 & "No_Implicit_Protected_Object_Allocations??",
9423 Priv, Prot_Typ);
9424 end if;
9425 end if;
9426 end if;
9427
9428 -- The component definition consists of a subtype indication,
9429 -- or (in Ada 2005) an access definition. Make a copy of the
9430 -- proper definition.
9431
9432 declare
9433 Old_Comp : constant Node_Id := Component_Definition (Priv);
9434 Oent : constant Entity_Id := Defining_Identifier (Priv);
9435 Nent : constant Entity_Id :=
9436 Make_Defining_Identifier (Sloc (Oent),
9437 Chars => Chars (Oent));
9438 New_Comp : Node_Id;
9439
9440 begin
9441 if Present (Subtype_Indication (Old_Comp)) then
9442 New_Comp :=
9443 Make_Component_Definition (Sloc (Oent),
9444 Aliased_Present => False,
9445 Subtype_Indication =>
9446 New_Copy_Tree
9447 (Subtype_Indication (Old_Comp), Discr_Map));
9448 else
9449 New_Comp :=
9450 Make_Component_Definition (Sloc (Oent),
9451 Aliased_Present => False,
9452 Access_Definition =>
9453 New_Copy_Tree
9454 (Access_Definition (Old_Comp), Discr_Map));
9455 end if;
9456
9457 New_Priv :=
9458 Make_Component_Declaration (Loc,
9459 Defining_Identifier => Nent,
9460 Component_Definition => New_Comp,
9461 Expression => Expression (Priv));
9462
9463 Set_Has_Per_Object_Constraint (Nent,
9464 Has_Per_Object_Constraint (Oent));
9465
9466 Append_To (Cdecls, New_Priv);
9467 end;
9468
9469 elsif Nkind (Priv) = N_Subprogram_Declaration then
9470
9471 -- Make the unprotected version of the subprogram available
9472 -- for expansion of intra object calls. There is need for
9473 -- a protected version only if the subprogram is an interrupt
9474 -- handler, otherwise this operation can only be called from
9475 -- within the body.
9476
9477 Sub :=
9478 Make_Subprogram_Declaration (Loc,
9479 Specification =>
9480 Build_Protected_Sub_Specification
9481 (Priv, Prot_Typ, Unprotected_Mode));
9482
9483 Insert_After (Current_Node, Sub);
9484 Analyze (Sub);
9485
9486 Set_Protected_Body_Subprogram
9487 (Defining_Unit_Name (Specification (Priv)),
9488 Defining_Unit_Name (Specification (Sub)));
9489 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9490 Current_Node := Sub;
9491
9492 Sub :=
9493 Make_Subprogram_Declaration (Loc,
9494 Specification =>
9495 Build_Protected_Sub_Specification
9496 (Priv, Prot_Typ, Protected_Mode));
9497
9498 Insert_After (Current_Node, Sub);
9499 Analyze (Sub);
9500 Current_Node := Sub;
9501
9502 if Is_Interrupt_Handler
9503 (Defining_Unit_Name (Specification (Priv)))
9504 then
9505 if not Restricted_Profile then
9506 Register_Handler;
9507 end if;
9508 end if;
9509 end if;
9510
9511 Next (Priv);
9512 end loop;
9513 end if;
9514
9515 -- Except for the lock-free implementation, append the _Object field
9516 -- with the right type to the component list. We need to compute the
9517 -- number of entries, and in some cases the number of Attach_Handler
9518 -- pragmas.
9519
9520 if not Lock_Free_Active then
9521 declare
9522 Entry_Count_Expr : constant Node_Id :=
9523 Build_Entry_Count_Expression
9524 (Prot_Typ, Cdecls, Loc);
9525 Num_Attach_Handler : Nat := 0;
9526 Protection_Subtype : Node_Id;
9527 Ritem : Node_Id;
9528
9529 begin
9530 if Has_Attach_Handler (Prot_Typ) then
9531 Ritem := First_Rep_Item (Prot_Typ);
9532 while Present (Ritem) loop
9533 if Nkind (Ritem) = N_Pragma
9534 and then Pragma_Name (Ritem) = Name_Attach_Handler
9535 then
9536 Num_Attach_Handler := Num_Attach_Handler + 1;
9537 end if;
9538
9539 Next_Rep_Item (Ritem);
9540 end loop;
9541 end if;
9542
9543 -- Determine the proper protection type. There are two special
9544 -- cases: 1) when the protected type has dynamic interrupt
9545 -- handlers, and 2) when it has static handlers and we use a
9546 -- restricted profile.
9547
9548 if Has_Attach_Handler (Prot_Typ)
9549 and then not Restricted_Profile
9550 then
9551 Protection_Subtype :=
9552 Make_Subtype_Indication (Loc,
9553 Subtype_Mark =>
9554 New_Occurrence_Of
9555 (RTE (RE_Static_Interrupt_Protection), Loc),
9556 Constraint =>
9557 Make_Index_Or_Discriminant_Constraint (Loc,
9558 Constraints => New_List (
9559 Entry_Count_Expr,
9560 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9561
9562 elsif Has_Interrupt_Handler (Prot_Typ)
9563 and then not Restriction_Active (No_Dynamic_Attachment)
9564 then
9565 Protection_Subtype :=
9566 Make_Subtype_Indication (Loc,
9567 Subtype_Mark =>
9568 New_Occurrence_Of
9569 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9570 Constraint =>
9571 Make_Index_Or_Discriminant_Constraint (Loc,
9572 Constraints => New_List (Entry_Count_Expr)));
9573
9574 else
9575 case Corresponding_Runtime_Package (Prot_Typ) is
9576 when System_Tasking_Protected_Objects_Entries =>
9577 Protection_Subtype :=
9578 Make_Subtype_Indication (Loc,
9579 Subtype_Mark =>
9580 New_Occurrence_Of
9581 (RTE (RE_Protection_Entries), Loc),
9582 Constraint =>
9583 Make_Index_Or_Discriminant_Constraint (Loc,
9584 Constraints => New_List (Entry_Count_Expr)));
9585
9586 when System_Tasking_Protected_Objects_Single_Entry =>
9587 Protection_Subtype :=
9588 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9589
9590 when System_Tasking_Protected_Objects =>
9591 Protection_Subtype :=
9592 New_Occurrence_Of (RTE (RE_Protection), Loc);
9593
9594 when others =>
9595 raise Program_Error;
9596 end case;
9597 end if;
9598
9599 Object_Comp :=
9600 Make_Component_Declaration (Loc,
9601 Defining_Identifier =>
9602 Make_Defining_Identifier (Loc, Name_uObject),
9603 Component_Definition =>
9604 Make_Component_Definition (Loc,
9605 Aliased_Present => True,
9606 Subtype_Indication => Protection_Subtype));
9607 end;
9608
9609 -- Put the _Object component after the private component so that it
9610 -- be finalized early as required by 9.4 (20)
9611
9612 Append_To (Cdecls, Object_Comp);
9613 end if;
9614
9615 -- Analyze the record declaration immediately after construction,
9616 -- because the initialization procedure is needed for single object
9617 -- declarations before the next entity is analyzed (the freeze call
9618 -- that generates this initialization procedure is found below).
9619
9620 Analyze (Rec_Decl, Suppress => All_Checks);
9621
9622 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9623 -- the corresponding record is frozen. If any wrappers are generated,
9624 -- Current_Node is updated accordingly.
9625
9626 if Ada_Version >= Ada_2005 then
9627 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9628 end if;
9629
9630 -- Collect pointers to entry bodies and their barriers, to be placed
9631 -- in the Entry_Bodies_Array for the type. For each entry/family we
9632 -- add an expression to the aggregate which is the initial value of
9633 -- this array. The array is declared after all protected subprograms.
9634
9635 if Has_Entries (Prot_Typ) then
9636 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9637 else
9638 Entries_Aggr := Empty;
9639 end if;
9640
9641 -- Build two new procedure specifications for each protected subprogram;
9642 -- one to call from outside the object and one to call from inside.
9643 -- Build a barrier function and an entry body action procedure
9644 -- specification for each protected entry. Initialize the entry body
9645 -- array. If subprogram is flagged as eliminated, do not generate any
9646 -- internal operations.
9647
9648 E_Count := 0;
9649 Comp := First (Visible_Declarations (Pdef));
9650 while Present (Comp) loop
9651 if Nkind (Comp) = N_Subprogram_Declaration then
9652 Sub :=
9653 Make_Subprogram_Declaration (Loc,
9654 Specification =>
9655 Build_Protected_Sub_Specification
9656 (Comp, Prot_Typ, Unprotected_Mode));
9657
9658 Insert_After (Current_Node, Sub);
9659 Analyze (Sub);
9660
9661 Set_Protected_Body_Subprogram
9662 (Defining_Unit_Name (Specification (Comp)),
9663 Defining_Unit_Name (Specification (Sub)));
9664 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9665
9666 -- Make the protected version of the subprogram available for
9667 -- expansion of external calls.
9668
9669 Current_Node := Sub;
9670
9671 Sub :=
9672 Make_Subprogram_Declaration (Loc,
9673 Specification =>
9674 Build_Protected_Sub_Specification
9675 (Comp, Prot_Typ, Protected_Mode));
9676
9677 Insert_After (Current_Node, Sub);
9678 Analyze (Sub);
9679
9680 Current_Node := Sub;
9681
9682 -- Generate an overriding primitive operation specification for
9683 -- this subprogram if the protected type implements an interface
9684 -- and Build_Wrapper_Spec did not generate its wrapper.
9685
9686 if Ada_Version >= Ada_2005
9687 and then
9688 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9689 then
9690 declare
9691 Found : Boolean := False;
9692 Prim_Elmt : Elmt_Id;
9693 Prim_Op : Node_Id;
9694
9695 begin
9696 Prim_Elmt :=
9697 First_Elmt
9698 (Primitive_Operations
9699 (Corresponding_Record_Type (Prot_Typ)));
9700
9701 while Present (Prim_Elmt) loop
9702 Prim_Op := Node (Prim_Elmt);
9703
9704 if Is_Primitive_Wrapper (Prim_Op)
9705 and then Wrapped_Entity (Prim_Op) =
9706 Defining_Entity (Specification (Comp))
9707 then
9708 Found := True;
9709 exit;
9710 end if;
9711
9712 Next_Elmt (Prim_Elmt);
9713 end loop;
9714
9715 if not Found then
9716 Sub :=
9717 Make_Subprogram_Declaration (Loc,
9718 Specification =>
9719 Build_Protected_Sub_Specification
9720 (Comp, Prot_Typ, Dispatching_Mode));
9721
9722 Insert_After (Current_Node, Sub);
9723 Analyze (Sub);
9724
9725 Current_Node := Sub;
9726 end if;
9727 end;
9728 end if;
9729
9730 -- If a pragma Interrupt_Handler applies, build and add a call to
9731 -- Register_Interrupt_Handler to the freezing actions of the
9732 -- protected version (Current_Node) of the subprogram:
9733
9734 -- system.interrupts.register_interrupt_handler
9735 -- (prot_procP'address);
9736
9737 if not Restricted_Profile
9738 and then Is_Interrupt_Handler
9739 (Defining_Unit_Name (Specification (Comp)))
9740 then
9741 Register_Handler;
9742 end if;
9743
9744 elsif Nkind (Comp) = N_Entry_Declaration then
9745 Expand_Entry_Declaration (Comp);
9746 end if;
9747
9748 Next (Comp);
9749 end loop;
9750
9751 -- If there are some private entry declarations, expand it as if they
9752 -- were visible entries.
9753
9754 if Present (Private_Declarations (Pdef)) then
9755 Comp := First (Private_Declarations (Pdef));
9756 while Present (Comp) loop
9757 if Nkind (Comp) = N_Entry_Declaration then
9758 Expand_Entry_Declaration (Comp);
9759 end if;
9760
9761 Next (Comp);
9762 end loop;
9763 end if;
9764
9765 -- Create the declaration of an array object which contains the values
9766 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9767 -- type. This object is later passed to the appropriate protected object
9768 -- initialization routine.
9769
9770 if Has_Entries (Prot_Typ)
9771 and then Corresponding_Runtime_Package (Prot_Typ) =
9772 System_Tasking_Protected_Objects_Entries
9773 then
9774 declare
9775 Count : Int;
9776 Item : Entity_Id;
9777 Max_Vals : Node_Id;
9778 Maxes : List_Id;
9779 Maxes_Id : Entity_Id;
9780 Need_Array : Boolean := False;
9781
9782 begin
9783 -- First check if there is any Max_Queue_Length pragma
9784
9785 Item := First_Entity (Prot_Typ);
9786 while Present (Item) loop
9787 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9788 Need_Array := True;
9789 exit;
9790 end if;
9791
9792 Next_Entity (Item);
9793 end loop;
9794
9795 -- Gather the Max_Queue_Length values of all entries in a list. A
9796 -- value of zero indicates that the entry has no limitation on its
9797 -- queue length.
9798
9799 if Need_Array then
9800 Count := 0;
9801 Item := First_Entity (Prot_Typ);
9802 Maxes := New_List;
9803 while Present (Item) loop
9804 if Is_Entry (Item) then
9805 Count := Count + 1;
9806 Append_To (Maxes,
9807 Make_Integer_Literal
9808 (Loc, Get_Max_Queue_Length (Item)));
9809 end if;
9810
9811 Next_Entity (Item);
9812 end loop;
9813
9814 -- Create the declaration of the array object. Generate:
9815
9816 -- Maxes_Id : aliased Protected_Entry_Queue_Max_Array
9817 -- (1 .. Count) := (..., ...);
9818
9819 Maxes_Id :=
9820 Make_Defining_Identifier (Loc,
9821 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9822
9823 Max_Vals :=
9824 Make_Object_Declaration (Loc,
9825 Defining_Identifier => Maxes_Id,
9826 Aliased_Present => True,
9827 Constant_Present => True,
9828 Object_Definition =>
9829 Make_Subtype_Indication (Loc,
9830 Subtype_Mark =>
9831 New_Occurrence_Of
9832 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9833 Constraint =>
9834 Make_Index_Or_Discriminant_Constraint (Loc,
9835 Constraints => New_List (
9836 Make_Range (Loc,
9837 Make_Integer_Literal (Loc, 1),
9838 Make_Integer_Literal (Loc, Count))))),
9839 Expression => Make_Aggregate (Loc, Maxes));
9840
9841 -- A pointer to this array will be placed in the corresponding
9842 -- record by its initialization procedure so this needs to be
9843 -- analyzed here.
9844
9845 Insert_After (Current_Node, Max_Vals);
9846 Current_Node := Max_Vals;
9847 Analyze (Max_Vals);
9848
9849 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9850 end if;
9851 end;
9852 end if;
9853
9854 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9855 -- all protected subprograms have been collected.
9856
9857 if Has_Entries (Prot_Typ) then
9858 Body_Id :=
9859 Make_Defining_Identifier (Sloc (Prot_Typ),
9860 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9861
9862 case Corresponding_Runtime_Package (Prot_Typ) is
9863 when System_Tasking_Protected_Objects_Entries =>
9864 Expr := Entries_Aggr;
9865 Obj_Def :=
9866 Make_Subtype_Indication (Loc,
9867 Subtype_Mark =>
9868 New_Occurrence_Of
9869 (RTE (RE_Protected_Entry_Body_Array), Loc),
9870 Constraint =>
9871 Make_Index_Or_Discriminant_Constraint (Loc,
9872 Constraints => New_List (
9873 Make_Range (Loc,
9874 Make_Integer_Literal (Loc, 1),
9875 Make_Integer_Literal (Loc, E_Count)))));
9876
9877 when System_Tasking_Protected_Objects_Single_Entry =>
9878 Expr := Remove_Head (Expressions (Entries_Aggr));
9879 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9880
9881 when others =>
9882 raise Program_Error;
9883 end case;
9884
9885 Body_Arr :=
9886 Make_Object_Declaration (Loc,
9887 Defining_Identifier => Body_Id,
9888 Aliased_Present => True,
9889 Object_Definition => Obj_Def,
9890 Expression => Expr);
9891
9892 -- A pointer to this array will be placed in the corresponding record
9893 -- by its initialization procedure so this needs to be analyzed here.
9894
9895 Insert_After (Current_Node, Body_Arr);
9896 Current_Node := Body_Arr;
9897 Analyze (Body_Arr);
9898
9899 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9900
9901 -- Finally, build the function that maps an entry index into the
9902 -- corresponding body. A pointer to this function is placed in each
9903 -- object of the type. Except for a ravenscar-like profile (no abort,
9904 -- no entry queue, 1 entry)
9905
9906 if Corresponding_Runtime_Package (Prot_Typ) =
9907 System_Tasking_Protected_Objects_Entries
9908 then
9909 Sub :=
9910 Make_Subprogram_Declaration (Loc,
9911 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9912
9913 Insert_After (Current_Node, Sub);
9914 Analyze (Sub);
9915 end if;
9916 end if;
9917 end Expand_N_Protected_Type_Declaration;
9918
9919 --------------------------------
9920 -- Expand_N_Requeue_Statement --
9921 --------------------------------
9922
9923 -- A nondispatching requeue statement is expanded into one of four GNARLI
9924 -- operations, depending on the source and destination (task or protected
9925 -- object). A dispatching requeue statement is expanded into a call to the
9926 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9927 -- jump around the remainder of processing for the original entry and, if
9928 -- the destination is (different) protected object, to attempt to service
9929 -- it. The following illustrates the various cases:
9930
9931 -- procedure entE
9932 -- (O : System.Address;
9933 -- P : System.Address;
9934 -- E : Protected_Entry_Index)
9935 -- is
9936 -- <discriminant renamings>
9937 -- <private object renamings>
9938 -- type poVP is access poV;
9939 -- _object : ptVP := ptVP!(O);
9940
9941 -- begin
9942 -- begin
9943 -- <start of statement sequence for entry>
9944
9945 -- -- Requeue from one protected entry body to another protected
9946 -- -- entry.
9947
9948 -- Requeue_Protected_Entry (
9949 -- _object._object'Access,
9950 -- new._object'Access,
9951 -- E,
9952 -- Abort_Present);
9953 -- return;
9954
9955 -- <some more of the statement sequence for entry>
9956
9957 -- -- Requeue from an entry body to a task entry
9958
9959 -- Requeue_Protected_To_Task_Entry (
9960 -- New._task_id,
9961 -- E,
9962 -- Abort_Present);
9963 -- return;
9964
9965 -- <rest of statement sequence for entry>
9966 -- Complete_Entry_Body (_object._object);
9967
9968 -- exception
9969 -- when all others =>
9970 -- Exceptional_Complete_Entry_Body (
9971 -- _object._object, Get_GNAT_Exception);
9972 -- end;
9973 -- end entE;
9974
9975 -- Requeue of a task entry call to a task entry
9976
9977 -- Accept_Call (E, Ann);
9978 -- <start of statement sequence for accept statement>
9979 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9980 -- goto Lnn;
9981 -- <rest of statement sequence for accept statement>
9982 -- <<Lnn>>
9983 -- Complete_Rendezvous;
9984
9985 -- exception
9986 -- when all others =>
9987 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9988
9989 -- Requeue of a task entry call to a protected entry
9990
9991 -- Accept_Call (E, Ann);
9992 -- <start of statement sequence for accept statement>
9993 -- Requeue_Task_To_Protected_Entry (
9994 -- new._object'Access,
9995 -- E,
9996 -- Abort_Present);
9997 -- newS (new, Pnn);
9998 -- goto Lnn;
9999 -- <rest of statement sequence for accept statement>
10000 -- <<Lnn>>
10001 -- Complete_Rendezvous;
10002
10003 -- exception
10004 -- when all others =>
10005 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10006
10007 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10008 -- marked by pragma Implemented (XXX, By_Entry).
10009
10010 -- The requeue is inside a protected entry:
10011
10012 -- procedure entE
10013 -- (O : System.Address;
10014 -- P : System.Address;
10015 -- E : Protected_Entry_Index)
10016 -- is
10017 -- <discriminant renamings>
10018 -- <private object renamings>
10019 -- type poVP is access poV;
10020 -- _object : ptVP := ptVP!(O);
10021
10022 -- begin
10023 -- begin
10024 -- <start of statement sequence for entry>
10025
10026 -- _Disp_Requeue
10027 -- (<interface class-wide object>,
10028 -- True,
10029 -- _object'Address,
10030 -- Ada.Tags.Get_Offset_Index
10031 -- (Tag (_object),
10032 -- <interface dispatch table index of target entry>),
10033 -- Abort_Present);
10034 -- return;
10035
10036 -- <rest of statement sequence for entry>
10037 -- Complete_Entry_Body (_object._object);
10038
10039 -- exception
10040 -- when all others =>
10041 -- Exceptional_Complete_Entry_Body (
10042 -- _object._object, Get_GNAT_Exception);
10043 -- end;
10044 -- end entE;
10045
10046 -- The requeue is inside a task entry:
10047
10048 -- Accept_Call (E, Ann);
10049 -- <start of statement sequence for accept statement>
10050 -- _Disp_Requeue
10051 -- (<interface class-wide object>,
10052 -- False,
10053 -- null,
10054 -- Ada.Tags.Get_Offset_Index
10055 -- (Tag (_object),
10056 -- <interface dispatch table index of target entrt>),
10057 -- Abort_Present);
10058 -- newS (new, Pnn);
10059 -- goto Lnn;
10060 -- <rest of statement sequence for accept statement>
10061 -- <<Lnn>>
10062 -- Complete_Rendezvous;
10063
10064 -- exception
10065 -- when all others =>
10066 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10067
10068 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10069 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
10070 -- statement is replaced by a dispatching call with actual parameters taken
10071 -- from the inner-most accept statement or entry body.
10072
10073 -- Target.Primitive (Param1, ..., ParamN);
10074
10075 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10076 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
10077 -- at all.
10078
10079 -- declare
10080 -- S : constant Offset_Index :=
10081 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
10082 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
10083
10084 -- begin
10085 -- if C = POK_Protected_Entry
10086 -- or else C = POK_Task_Entry
10087 -- then
10088 -- <statements for dispatching requeue>
10089
10090 -- elsif C = POK_Protected_Procedure then
10091 -- <dispatching call equivalent>
10092
10093 -- else
10094 -- raise Program_Error;
10095 -- end if;
10096 -- end;
10097
10098 procedure Expand_N_Requeue_Statement (N : Node_Id) is
10099 Loc : constant Source_Ptr := Sloc (N);
10100 Conc_Typ : Entity_Id;
10101 Concval : Node_Id;
10102 Ename : Node_Id;
10103 Index : Node_Id;
10104 Old_Typ : Entity_Id;
10105
10106 function Build_Dispatching_Call_Equivalent return Node_Id;
10107 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10108 -- the form Concval.Ename. It is statically known that Ename is allowed
10109 -- to be implemented by a protected procedure. Create a dispatching call
10110 -- equivalent of Concval.Ename taking the actual parameters from the
10111 -- inner-most accept statement or entry body.
10112
10113 function Build_Dispatching_Requeue return Node_Id;
10114 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10115 -- the form Concval.Ename. It is statically known that Ename is allowed
10116 -- to be implemented by a protected or a task entry. Create a call to
10117 -- primitive _Disp_Requeue which handles the low-level actions.
10118
10119 function Build_Dispatching_Requeue_To_Any return Node_Id;
10120 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10121 -- the form Concval.Ename. Ename is either marked by pragma Implemented
10122 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
10123 -- determines at runtime whether Ename denotes an entry or a procedure
10124 -- and perform the appropriate kind of dispatching select.
10125
10126 function Build_Normal_Requeue return Node_Id;
10127 -- N denotes a nondispatching requeue statement to either a task or a
10128 -- protected entry. Build the appropriate runtime call to perform the
10129 -- action.
10130
10131 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
10132 -- For a protected entry, create a return statement to skip the rest of
10133 -- the entry body. Otherwise, create a goto statement to skip the rest
10134 -- of a task accept statement. The lookup for the enclosing entry body
10135 -- or accept statement starts from Search.
10136
10137 ---------------------------------------
10138 -- Build_Dispatching_Call_Equivalent --
10139 ---------------------------------------
10140
10141 function Build_Dispatching_Call_Equivalent return Node_Id is
10142 Call_Ent : constant Entity_Id := Entity (Ename);
10143 Obj : constant Node_Id := Original_Node (Concval);
10144 Acc_Ent : Node_Id;
10145 Actuals : List_Id;
10146 Formal : Node_Id;
10147 Formals : List_Id;
10148
10149 begin
10150 -- Climb the parent chain looking for the inner-most entry body or
10151 -- accept statement.
10152
10153 Acc_Ent := N;
10154 while Present (Acc_Ent)
10155 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
10156 N_Entry_Body)
10157 loop
10158 Acc_Ent := Parent (Acc_Ent);
10159 end loop;
10160
10161 -- A requeue statement should be housed inside an entry body or an
10162 -- accept statement at some level. If this is not the case, then the
10163 -- tree is malformed.
10164
10165 pragma Assert (Present (Acc_Ent));
10166
10167 -- Recover the list of formal parameters
10168
10169 if Nkind (Acc_Ent) = N_Entry_Body then
10170 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
10171 end if;
10172
10173 Formals := Parameter_Specifications (Acc_Ent);
10174
10175 -- Create the actual parameters for the dispatching call. These are
10176 -- simply copies of the entry body or accept statement formals in the
10177 -- same order as they appear.
10178
10179 Actuals := No_List;
10180
10181 if Present (Formals) then
10182 Actuals := New_List;
10183 Formal := First (Formals);
10184 while Present (Formal) loop
10185 Append_To (Actuals,
10186 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
10187 Next (Formal);
10188 end loop;
10189 end if;
10190
10191 -- Generate:
10192 -- Obj.Call_Ent (Actuals);
10193
10194 return
10195 Make_Procedure_Call_Statement (Loc,
10196 Name =>
10197 Make_Selected_Component (Loc,
10198 Prefix => Make_Identifier (Loc, Chars (Obj)),
10199 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
10200
10201 Parameter_Associations => Actuals);
10202 end Build_Dispatching_Call_Equivalent;
10203
10204 -------------------------------
10205 -- Build_Dispatching_Requeue --
10206 -------------------------------
10207
10208 function Build_Dispatching_Requeue return Node_Id is
10209 Params : constant List_Id := New_List;
10210
10211 begin
10212 -- Process the "with abort" parameter
10213
10214 Prepend_To (Params,
10215 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10216
10217 -- Process the entry wrapper's position in the primary dispatch
10218 -- table parameter. Generate:
10219
10220 -- Ada.Tags.Get_Entry_Index
10221 -- (T => To_Tag_Ptr (Obj'Address).all,
10222 -- Position =>
10223 -- Ada.Tags.Get_Offset_Index
10224 -- (Ada.Tags.Tag (Concval),
10225 -- <interface dispatch table position of Ename>));
10226
10227 -- Note that Obj'Address is recursively expanded into a call to
10228 -- Base_Address (Obj).
10229
10230 if Tagged_Type_Expansion then
10231 Prepend_To (Params,
10232 Make_Function_Call (Loc,
10233 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10234 Parameter_Associations => New_List (
10235
10236 Make_Explicit_Dereference (Loc,
10237 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
10238 Make_Attribute_Reference (Loc,
10239 Prefix => New_Copy_Tree (Concval),
10240 Attribute_Name => Name_Address))),
10241
10242 Make_Function_Call (Loc,
10243 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10244 Parameter_Associations => New_List (
10245 Unchecked_Convert_To (RTE (RE_Tag), Concval),
10246 Make_Integer_Literal (Loc,
10247 DT_Position (Entity (Ename))))))));
10248
10249 -- VM targets
10250
10251 else
10252 Prepend_To (Params,
10253 Make_Function_Call (Loc,
10254 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10255 Parameter_Associations => New_List (
10256
10257 Make_Attribute_Reference (Loc,
10258 Prefix => Concval,
10259 Attribute_Name => Name_Tag),
10260
10261 Make_Function_Call (Loc,
10262 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10263
10264 Parameter_Associations => New_List (
10265
10266 -- Obj_Tag
10267
10268 Make_Attribute_Reference (Loc,
10269 Prefix => Concval,
10270 Attribute_Name => Name_Tag),
10271
10272 -- Tag_Typ
10273
10274 Make_Attribute_Reference (Loc,
10275 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
10276 Attribute_Name => Name_Tag),
10277
10278 -- Position
10279
10280 Make_Integer_Literal (Loc,
10281 DT_Position (Entity (Ename))))))));
10282 end if;
10283
10284 -- Specific actuals for protected to XXX requeue
10285
10286 if Is_Protected_Type (Old_Typ) then
10287 Prepend_To (Params,
10288 Make_Attribute_Reference (Loc, -- _object'Address
10289 Prefix =>
10290 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10291 Attribute_Name => Name_Address));
10292
10293 Prepend_To (Params, -- True
10294 New_Occurrence_Of (Standard_True, Loc));
10295
10296 -- Specific actuals for task to XXX requeue
10297
10298 else
10299 pragma Assert (Is_Task_Type (Old_Typ));
10300
10301 Prepend_To (Params, -- null
10302 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10303
10304 Prepend_To (Params, -- False
10305 New_Occurrence_Of (Standard_False, Loc));
10306 end if;
10307
10308 -- Add the object parameter
10309
10310 Prepend_To (Params, New_Copy_Tree (Concval));
10311
10312 -- Generate:
10313 -- _Disp_Requeue (<Params>);
10314
10315 -- Find entity for Disp_Requeue operation, which belongs to
10316 -- the type and may not be directly visible.
10317
10318 declare
10319 Elmt : Elmt_Id;
10320 Op : Entity_Id;
10321
10322 begin
10323 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10324 while Present (Elmt) loop
10325 Op := Node (Elmt);
10326 exit when Chars (Op) = Name_uDisp_Requeue;
10327 Next_Elmt (Elmt);
10328 end loop;
10329
10330 return
10331 Make_Procedure_Call_Statement (Loc,
10332 Name => New_Occurrence_Of (Op, Loc),
10333 Parameter_Associations => Params);
10334 end;
10335 end Build_Dispatching_Requeue;
10336
10337 --------------------------------------
10338 -- Build_Dispatching_Requeue_To_Any --
10339 --------------------------------------
10340
10341 function Build_Dispatching_Requeue_To_Any return Node_Id is
10342 Call_Ent : constant Entity_Id := Entity (Ename);
10343 Obj : constant Node_Id := Original_Node (Concval);
10344 Skip : constant Node_Id := Build_Skip_Statement (N);
10345 C : Entity_Id;
10346 Decls : List_Id;
10347 S : Entity_Id;
10348 Stmts : List_Id;
10349
10350 begin
10351 Decls := New_List;
10352 Stmts := New_List;
10353
10354 -- Dispatch table slot processing, generate:
10355 -- S : Integer;
10356
10357 S := Build_S (Loc, Decls);
10358
10359 -- Call kind processing, generate:
10360 -- C : Ada.Tags.Prim_Op_Kind;
10361
10362 C := Build_C (Loc, Decls);
10363
10364 -- Generate:
10365 -- S := Ada.Tags.Get_Offset_Index
10366 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10367
10368 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10369
10370 -- Generate:
10371 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10372
10373 Append_To (Stmts,
10374 Make_Procedure_Call_Statement (Loc,
10375 Name =>
10376 New_Occurrence_Of (
10377 Find_Prim_Op (Etype (Etype (Obj)),
10378 Name_uDisp_Get_Prim_Op_Kind),
10379 Loc),
10380 Parameter_Associations => New_List (
10381 New_Copy_Tree (Obj),
10382 New_Occurrence_Of (S, Loc),
10383 New_Occurrence_Of (C, Loc))));
10384
10385 Append_To (Stmts,
10386
10387 -- if C = POK_Protected_Entry
10388 -- or else C = POK_Task_Entry
10389 -- then
10390
10391 Make_Implicit_If_Statement (N,
10392 Condition =>
10393 Make_Op_Or (Loc,
10394 Left_Opnd =>
10395 Make_Op_Eq (Loc,
10396 Left_Opnd =>
10397 New_Occurrence_Of (C, Loc),
10398 Right_Opnd =>
10399 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10400
10401 Right_Opnd =>
10402 Make_Op_Eq (Loc,
10403 Left_Opnd =>
10404 New_Occurrence_Of (C, Loc),
10405 Right_Opnd =>
10406 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10407
10408 -- Dispatching requeue equivalent
10409
10410 Then_Statements => New_List (
10411 Build_Dispatching_Requeue,
10412 Skip),
10413
10414 -- elsif C = POK_Protected_Procedure then
10415
10416 Elsif_Parts => New_List (
10417 Make_Elsif_Part (Loc,
10418 Condition =>
10419 Make_Op_Eq (Loc,
10420 Left_Opnd =>
10421 New_Occurrence_Of (C, Loc),
10422 Right_Opnd =>
10423 New_Occurrence_Of (
10424 RTE (RE_POK_Protected_Procedure), Loc)),
10425
10426 -- Dispatching call equivalent
10427
10428 Then_Statements => New_List (
10429 Build_Dispatching_Call_Equivalent))),
10430
10431 -- else
10432 -- raise Program_Error;
10433 -- end if;
10434
10435 Else_Statements => New_List (
10436 Make_Raise_Program_Error (Loc,
10437 Reason => PE_Explicit_Raise))));
10438
10439 -- Wrap everything into a block
10440
10441 return
10442 Make_Block_Statement (Loc,
10443 Declarations => Decls,
10444 Handled_Statement_Sequence =>
10445 Make_Handled_Sequence_Of_Statements (Loc,
10446 Statements => Stmts));
10447 end Build_Dispatching_Requeue_To_Any;
10448
10449 --------------------------
10450 -- Build_Normal_Requeue --
10451 --------------------------
10452
10453 function Build_Normal_Requeue return Node_Id is
10454 Params : constant List_Id := New_List;
10455 Param : Node_Id;
10456 RT_Call : Node_Id;
10457
10458 begin
10459 -- Process the "with abort" parameter
10460
10461 Prepend_To (Params,
10462 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10463
10464 -- Add the index expression to the parameters. It is common among all
10465 -- four cases.
10466
10467 Prepend_To (Params,
10468 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10469
10470 if Is_Protected_Type (Old_Typ) then
10471 declare
10472 Self_Param : Node_Id;
10473
10474 begin
10475 Self_Param :=
10476 Make_Attribute_Reference (Loc,
10477 Prefix =>
10478 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10479 Attribute_Name =>
10480 Name_Unchecked_Access);
10481
10482 -- Protected to protected requeue
10483
10484 if Is_Protected_Type (Conc_Typ) then
10485 RT_Call :=
10486 New_Occurrence_Of (
10487 RTE (RE_Requeue_Protected_Entry), Loc);
10488
10489 Param :=
10490 Make_Attribute_Reference (Loc,
10491 Prefix =>
10492 Concurrent_Ref (Concval),
10493 Attribute_Name =>
10494 Name_Unchecked_Access);
10495
10496 -- Protected to task requeue
10497
10498 else pragma Assert (Is_Task_Type (Conc_Typ));
10499 RT_Call :=
10500 New_Occurrence_Of (
10501 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10502
10503 Param := Concurrent_Ref (Concval);
10504 end if;
10505
10506 Prepend_To (Params, Param);
10507 Prepend_To (Params, Self_Param);
10508 end;
10509
10510 else pragma Assert (Is_Task_Type (Old_Typ));
10511
10512 -- Task to protected requeue
10513
10514 if Is_Protected_Type (Conc_Typ) then
10515 RT_Call :=
10516 New_Occurrence_Of (
10517 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10518
10519 Param :=
10520 Make_Attribute_Reference (Loc,
10521 Prefix =>
10522 Concurrent_Ref (Concval),
10523 Attribute_Name =>
10524 Name_Unchecked_Access);
10525
10526 -- Task to task requeue
10527
10528 else pragma Assert (Is_Task_Type (Conc_Typ));
10529 RT_Call :=
10530 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10531
10532 Param := Concurrent_Ref (Concval);
10533 end if;
10534
10535 Prepend_To (Params, Param);
10536 end if;
10537
10538 return
10539 Make_Procedure_Call_Statement (Loc,
10540 Name => RT_Call,
10541 Parameter_Associations => Params);
10542 end Build_Normal_Requeue;
10543
10544 --------------------------
10545 -- Build_Skip_Statement --
10546 --------------------------
10547
10548 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10549 Skip_Stmt : Node_Id;
10550
10551 begin
10552 -- Build a return statement to skip the rest of the entire body
10553
10554 if Is_Protected_Type (Old_Typ) then
10555 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10556
10557 -- If the requeue is within a task, find the end label of the
10558 -- enclosing accept statement and create a goto statement to it.
10559
10560 else
10561 declare
10562 Acc : Node_Id;
10563 Label : Node_Id;
10564
10565 begin
10566 -- Climb the parent chain looking for the enclosing accept
10567 -- statement.
10568
10569 Acc := Parent (Search);
10570 while Present (Acc)
10571 and then Nkind (Acc) /= N_Accept_Statement
10572 loop
10573 Acc := Parent (Acc);
10574 end loop;
10575
10576 -- The last statement is the second label used for completing
10577 -- the rendezvous the usual way. The label we are looking for
10578 -- is right before it.
10579
10580 Label :=
10581 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10582
10583 pragma Assert (Nkind (Label) = N_Label);
10584
10585 -- Generate a goto statement to skip the rest of the accept
10586
10587 Skip_Stmt :=
10588 Make_Goto_Statement (Loc,
10589 Name =>
10590 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10591 end;
10592 end if;
10593
10594 Set_Analyzed (Skip_Stmt);
10595
10596 return Skip_Stmt;
10597 end Build_Skip_Statement;
10598
10599 -- Start of processing for Expand_N_Requeue_Statement
10600
10601 begin
10602 -- Extract the components of the entry call
10603
10604 Extract_Entry (N, Concval, Ename, Index);
10605 Conc_Typ := Etype (Concval);
10606
10607 -- If the prefix is an access to class-wide type, dereference to get
10608 -- object and entry type.
10609
10610 if Is_Access_Type (Conc_Typ) then
10611 Conc_Typ := Designated_Type (Conc_Typ);
10612 Rewrite (Concval,
10613 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10614 Analyze_And_Resolve (Concval, Conc_Typ);
10615 end if;
10616
10617 -- Examine the scope stack in order to find nearest enclosing protected
10618 -- or task type. This will constitute our invocation source.
10619
10620 Old_Typ := Current_Scope;
10621 while Present (Old_Typ)
10622 and then not Is_Protected_Type (Old_Typ)
10623 and then not Is_Task_Type (Old_Typ)
10624 loop
10625 Old_Typ := Scope (Old_Typ);
10626 end loop;
10627
10628 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10629 -- Concval.Ename where the type of Concval is class-wide concurrent
10630 -- interface.
10631
10632 if Ada_Version >= Ada_2012
10633 and then Present (Concval)
10634 and then Is_Class_Wide_Type (Conc_Typ)
10635 and then Is_Concurrent_Interface (Conc_Typ)
10636 then
10637 declare
10638 Has_Impl : Boolean := False;
10639 Impl_Kind : Name_Id := No_Name;
10640
10641 begin
10642 -- Check whether the Ename is flagged by pragma Implemented
10643
10644 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10645 Has_Impl := True;
10646 Impl_Kind := Implementation_Kind (Entity (Ename));
10647 end if;
10648
10649 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10650 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10651
10652 if Has_Impl and then Impl_Kind = Name_By_Entry then
10653 Rewrite (N, Build_Dispatching_Requeue);
10654 Analyze (N);
10655 Insert_After (N, Build_Skip_Statement (N));
10656
10657 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10658 -- a protected procedure. In this case the requeue is transformed
10659 -- into a dispatching call.
10660
10661 elsif Has_Impl
10662 and then Impl_Kind = Name_By_Protected_Procedure
10663 then
10664 Rewrite (N, Build_Dispatching_Call_Equivalent);
10665 Analyze (N);
10666
10667 -- The procedure_or_entry_NAME's implementation kind is either
10668 -- By_Any, Optional, or pragma Implemented was not applied at all.
10669 -- In this case a runtime test determines whether Ename denotes an
10670 -- entry or a protected procedure and performs the appropriate
10671 -- call.
10672
10673 else
10674 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10675 Analyze (N);
10676 end if;
10677 end;
10678
10679 -- Processing for regular (nondispatching) requeues
10680
10681 else
10682 Rewrite (N, Build_Normal_Requeue);
10683 Analyze (N);
10684 Insert_After (N, Build_Skip_Statement (N));
10685 end if;
10686 end Expand_N_Requeue_Statement;
10687
10688 -------------------------------
10689 -- Expand_N_Selective_Accept --
10690 -------------------------------
10691
10692 procedure Expand_N_Selective_Accept (N : Node_Id) is
10693 Loc : constant Source_Ptr := Sloc (N);
10694 Alts : constant List_Id := Select_Alternatives (N);
10695
10696 -- Note: in the below declarations a lot of new lists are allocated
10697 -- unconditionally which may well not end up being used. That's not
10698 -- a good idea since it wastes space gratuitously ???
10699
10700 Accept_Case : List_Id;
10701 Accept_List : constant List_Id := New_List;
10702
10703 Alt : Node_Id;
10704 Alt_List : constant List_Id := New_List;
10705 Alt_Stats : List_Id;
10706 Ann : Entity_Id := Empty;
10707
10708 Check_Guard : Boolean := True;
10709
10710 Decls : constant List_Id := New_List;
10711 Stats : constant List_Id := New_List;
10712 Body_List : constant List_Id := New_List;
10713 Trailing_List : constant List_Id := New_List;
10714
10715 Choices : List_Id;
10716 Else_Present : Boolean := False;
10717 Terminate_Alt : Node_Id := Empty;
10718 Select_Mode : Node_Id;
10719
10720 Delay_Case : List_Id;
10721 Delay_Count : Integer := 0;
10722 Delay_Val : Entity_Id;
10723 Delay_Index : Entity_Id;
10724 Delay_Min : Entity_Id;
10725 Delay_Num : Pos := 1;
10726 Delay_Alt_List : List_Id := New_List;
10727 Delay_List : constant List_Id := New_List;
10728 D : Entity_Id;
10729 M : Entity_Id;
10730
10731 First_Delay : Boolean := True;
10732 Guard_Open : Entity_Id;
10733
10734 End_Lab : Node_Id;
10735 Index : Pos := 1;
10736 Lab : Node_Id;
10737 Num_Alts : Nat;
10738 Num_Accept : Nat := 0;
10739 Proc : Node_Id;
10740 Time_Type : Entity_Id;
10741 Select_Call : Node_Id;
10742
10743 Qnam : constant Entity_Id :=
10744 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10745
10746 Xnam : constant Entity_Id :=
10747 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10748
10749 -----------------------
10750 -- Local subprograms --
10751 -----------------------
10752
10753 function Accept_Or_Raise return List_Id;
10754 -- For the rare case where delay alternatives all have guards, and
10755 -- all of them are closed, it is still possible that there were open
10756 -- accept alternatives with no callers. We must reexamine the
10757 -- Accept_List, and execute a selective wait with no else if some
10758 -- accept is open. If none, we raise program_error.
10759
10760 procedure Add_Accept (Alt : Node_Id);
10761 -- Process a single accept statement in a select alternative. Build
10762 -- procedure for body of accept, and add entry to dispatch table with
10763 -- expression for guard, in preparation for call to run time select.
10764
10765 function Make_And_Declare_Label (Num : Int) return Node_Id;
10766 -- Manufacture a label using Num as a serial number and declare it.
10767 -- The declaration is appended to Decls. The label marks the trailing
10768 -- statements of an accept or delay alternative.
10769
10770 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10771 -- Build call to Selective_Wait runtime routine
10772
10773 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10774 -- Add code to compare value of delay with previous values, and
10775 -- generate case entry for trailing statements.
10776
10777 procedure Process_Accept_Alternative
10778 (Alt : Node_Id;
10779 Index : Int;
10780 Proc : Node_Id);
10781 -- Add code to call corresponding procedure, and branch to
10782 -- trailing statements, if any.
10783
10784 ---------------------
10785 -- Accept_Or_Raise --
10786 ---------------------
10787
10788 function Accept_Or_Raise return List_Id is
10789 Cond : Node_Id;
10790 Stats : List_Id;
10791 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10792
10793 begin
10794 -- We generate the following:
10795
10796 -- for J in q'range loop
10797 -- if q(J).S /=null_task_entry then
10798 -- selective_wait (simple_mode,...);
10799 -- done := True;
10800 -- exit;
10801 -- end if;
10802 -- end loop;
10803 --
10804 -- if no rendez_vous then
10805 -- raise program_error;
10806 -- end if;
10807
10808 -- Note that the code needs to know that the selector name
10809 -- in an Accept_Alternative is named S.
10810
10811 Cond := Make_Op_Ne (Loc,
10812 Left_Opnd =>
10813 Make_Selected_Component (Loc,
10814 Prefix =>
10815 Make_Indexed_Component (Loc,
10816 Prefix => New_Occurrence_Of (Qnam, Loc),
10817 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10818 Selector_Name => Make_Identifier (Loc, Name_S)),
10819 Right_Opnd =>
10820 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10821
10822 Stats := New_List (
10823 Make_Implicit_Loop_Statement (N,
10824 Iteration_Scheme =>
10825 Make_Iteration_Scheme (Loc,
10826 Loop_Parameter_Specification =>
10827 Make_Loop_Parameter_Specification (Loc,
10828 Defining_Identifier => J,
10829 Discrete_Subtype_Definition =>
10830 Make_Attribute_Reference (Loc,
10831 Prefix => New_Occurrence_Of (Qnam, Loc),
10832 Attribute_Name => Name_Range,
10833 Expressions => New_List (
10834 Make_Integer_Literal (Loc, 1))))),
10835
10836 Statements => New_List (
10837 Make_Implicit_If_Statement (N,
10838 Condition => Cond,
10839 Then_Statements => New_List (
10840 Make_Select_Call (
10841 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10842 Make_Exit_Statement (Loc))))));
10843
10844 Append_To (Stats,
10845 Make_Raise_Program_Error (Loc,
10846 Condition => Make_Op_Eq (Loc,
10847 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10848 Right_Opnd =>
10849 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10850 Reason => PE_All_Guards_Closed));
10851
10852 return Stats;
10853 end Accept_Or_Raise;
10854
10855 ----------------
10856 -- Add_Accept --
10857 ----------------
10858
10859 procedure Add_Accept (Alt : Node_Id) is
10860 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10861 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10862 Eloc : constant Source_Ptr := Sloc (Ename);
10863 Eent : constant Entity_Id := Entity (Ename);
10864 Index : constant Node_Id := Entry_Index (Acc_Stm);
10865 Null_Body : Node_Id;
10866 Proc_Body : Node_Id;
10867 PB_Ent : Entity_Id;
10868 Expr : Node_Id;
10869 Call : Node_Id;
10870
10871 begin
10872 if No (Ann) then
10873 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10874 end if;
10875
10876 if Present (Condition (Alt)) then
10877 Expr :=
10878 Make_If_Expression (Eloc, New_List (
10879 Condition (Alt),
10880 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10881 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10882 else
10883 Expr :=
10884 Entry_Index_Expression
10885 (Eloc, Eent, Index, Scope (Eent));
10886 end if;
10887
10888 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10889 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10890
10891 -- Always add call to Abort_Undefer when generating code, since
10892 -- this is what the runtime expects (abort deferred in
10893 -- Selective_Wait). In CodePeer mode this only confuses the
10894 -- analysis with unknown calls, so don't do it.
10895
10896 if not CodePeer_Mode then
10897 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10898 Insert_Before
10899 (First (Statements (Handled_Statement_Sequence
10900 (Accept_Statement (Alt)))),
10901 Call);
10902 Analyze (Call);
10903 end if;
10904
10905 PB_Ent :=
10906 Make_Defining_Identifier (Eloc,
10907 New_External_Name (Chars (Ename), 'A', Num_Accept));
10908
10909 if Comes_From_Source (Alt) then
10910 Set_Debug_Info_Needed (PB_Ent);
10911 end if;
10912
10913 Proc_Body :=
10914 Make_Subprogram_Body (Eloc,
10915 Specification =>
10916 Make_Procedure_Specification (Eloc,
10917 Defining_Unit_Name => PB_Ent),
10918 Declarations => Declarations (Acc_Stm),
10919 Handled_Statement_Sequence =>
10920 Build_Accept_Body (Accept_Statement (Alt)));
10921
10922 -- During the analysis of the body of the accept statement, any
10923 -- zero cost exception handler records were collected in the
10924 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10925 -- This is where we move them to where they belong, namely the
10926 -- newly created procedure.
10927
10928 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10929 Append (Proc_Body, Body_List);
10930
10931 else
10932 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10933
10934 -- if accept statement has declarations, insert above, given that
10935 -- we are not creating a body for the accept.
10936
10937 if Present (Declarations (Acc_Stm)) then
10938 Insert_Actions (N, Declarations (Acc_Stm));
10939 end if;
10940 end if;
10941
10942 Append_To (Accept_List,
10943 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10944
10945 Num_Accept := Num_Accept + 1;
10946 end Add_Accept;
10947
10948 ----------------------------
10949 -- Make_And_Declare_Label --
10950 ----------------------------
10951
10952 function Make_And_Declare_Label (Num : Int) return Node_Id is
10953 Lab_Id : Node_Id;
10954
10955 begin
10956 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10957 Lab :=
10958 Make_Label (Loc, Lab_Id);
10959
10960 Append_To (Decls,
10961 Make_Implicit_Label_Declaration (Loc,
10962 Defining_Identifier =>
10963 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10964 Label_Construct => Lab));
10965
10966 return Lab;
10967 end Make_And_Declare_Label;
10968
10969 ----------------------
10970 -- Make_Select_Call --
10971 ----------------------
10972
10973 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10974 Params : constant List_Id := New_List;
10975
10976 begin
10977 Append_To (Params,
10978 Make_Attribute_Reference (Loc,
10979 Prefix => New_Occurrence_Of (Qnam, Loc),
10980 Attribute_Name => Name_Unchecked_Access));
10981 Append_To (Params, Select_Mode);
10982 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10983 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10984
10985 return
10986 Make_Procedure_Call_Statement (Loc,
10987 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10988 Parameter_Associations => Params);
10989 end Make_Select_Call;
10990
10991 --------------------------------
10992 -- Process_Accept_Alternative --
10993 --------------------------------
10994
10995 procedure Process_Accept_Alternative
10996 (Alt : Node_Id;
10997 Index : Int;
10998 Proc : Node_Id)
10999 is
11000 Astmt : constant Node_Id := Accept_Statement (Alt);
11001 Alt_Stats : List_Id;
11002
11003 begin
11004 Adjust_Condition (Condition (Alt));
11005
11006 -- Accept with body
11007
11008 if Present (Handled_Statement_Sequence (Astmt)) then
11009 Alt_Stats :=
11010 New_List (
11011 Make_Procedure_Call_Statement (Sloc (Proc),
11012 Name =>
11013 New_Occurrence_Of
11014 (Defining_Unit_Name (Specification (Proc)),
11015 Sloc (Proc))));
11016
11017 -- Accept with no body (followed by trailing statements)
11018
11019 else
11020 Alt_Stats := Empty_List;
11021 end if;
11022
11023 Ensure_Statement_Present (Sloc (Astmt), Alt);
11024
11025 -- After the call, if any, branch to trailing statements, if any.
11026 -- We create a label for each, as well as the corresponding label
11027 -- declaration.
11028
11029 if not Is_Empty_List (Statements (Alt)) then
11030 Lab := Make_And_Declare_Label (Index);
11031 Append (Lab, Trailing_List);
11032 Append_List (Statements (Alt), Trailing_List);
11033 Append_To (Trailing_List,
11034 Make_Goto_Statement (Loc,
11035 Name => New_Copy (Identifier (End_Lab))));
11036
11037 else
11038 Lab := End_Lab;
11039 end if;
11040
11041 Append_To (Alt_Stats,
11042 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
11043
11044 Append_To (Alt_List,
11045 Make_Case_Statement_Alternative (Loc,
11046 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
11047 Statements => Alt_Stats));
11048 end Process_Accept_Alternative;
11049
11050 -------------------------------
11051 -- Process_Delay_Alternative --
11052 -------------------------------
11053
11054 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
11055 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
11056 Cond : Node_Id;
11057 Delay_Alt : List_Id;
11058
11059 begin
11060 -- Deal with C/Fortran boolean as delay condition
11061
11062 Adjust_Condition (Condition (Alt));
11063
11064 -- Determine the smallest specified delay
11065
11066 -- for each delay alternative generate:
11067
11068 -- if guard-expression then
11069 -- Delay_Val := delay-expression;
11070 -- Guard_Open := True;
11071 -- if Delay_Val < Delay_Min then
11072 -- Delay_Min := Delay_Val;
11073 -- Delay_Index := Index;
11074 -- end if;
11075 -- end if;
11076
11077 -- The enclosing if-statement is omitted if there is no guard
11078
11079 if Delay_Count = 1 or else First_Delay then
11080 First_Delay := False;
11081
11082 Delay_Alt := New_List (
11083 Make_Assignment_Statement (Loc,
11084 Name => New_Occurrence_Of (Delay_Min, Loc),
11085 Expression => Expression (Delay_Statement (Alt))));
11086
11087 if Delay_Count > 1 then
11088 Append_To (Delay_Alt,
11089 Make_Assignment_Statement (Loc,
11090 Name => New_Occurrence_Of (Delay_Index, Loc),
11091 Expression => Make_Integer_Literal (Loc, Index)));
11092 end if;
11093
11094 else
11095 Delay_Alt := New_List (
11096 Make_Assignment_Statement (Loc,
11097 Name => New_Occurrence_Of (Delay_Val, Loc),
11098 Expression => Expression (Delay_Statement (Alt))));
11099
11100 if Time_Type = Standard_Duration then
11101 Cond :=
11102 Make_Op_Lt (Loc,
11103 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
11104 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
11105
11106 else
11107 -- The scope of the time type must define a comparison
11108 -- operator. The scope itself may not be visible, so we
11109 -- construct a node with entity information to insure that
11110 -- semantic analysis can find the proper operator.
11111
11112 Cond :=
11113 Make_Function_Call (Loc,
11114 Name => Make_Selected_Component (Loc,
11115 Prefix =>
11116 New_Occurrence_Of (Scope (Time_Type), Loc),
11117 Selector_Name =>
11118 Make_Operator_Symbol (Loc,
11119 Chars => Name_Op_Lt,
11120 Strval => No_String)),
11121 Parameter_Associations =>
11122 New_List (
11123 New_Occurrence_Of (Delay_Val, Loc),
11124 New_Occurrence_Of (Delay_Min, Loc)));
11125
11126 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
11127 end if;
11128
11129 Append_To (Delay_Alt,
11130 Make_Implicit_If_Statement (N,
11131 Condition => Cond,
11132 Then_Statements => New_List (
11133 Make_Assignment_Statement (Loc,
11134 Name => New_Occurrence_Of (Delay_Min, Loc),
11135 Expression => New_Occurrence_Of (Delay_Val, Loc)),
11136
11137 Make_Assignment_Statement (Loc,
11138 Name => New_Occurrence_Of (Delay_Index, Loc),
11139 Expression => Make_Integer_Literal (Loc, Index)))));
11140 end if;
11141
11142 if Check_Guard then
11143 Append_To (Delay_Alt,
11144 Make_Assignment_Statement (Loc,
11145 Name => New_Occurrence_Of (Guard_Open, Loc),
11146 Expression => New_Occurrence_Of (Standard_True, Loc)));
11147 end if;
11148
11149 if Present (Condition (Alt)) then
11150 Delay_Alt := New_List (
11151 Make_Implicit_If_Statement (N,
11152 Condition => Condition (Alt),
11153 Then_Statements => Delay_Alt));
11154 end if;
11155
11156 Append_List (Delay_Alt, Delay_List);
11157
11158 Ensure_Statement_Present (Dloc, Alt);
11159
11160 -- If the delay alternative has a statement part, add choice to the
11161 -- case statements for delays.
11162
11163 if not Is_Empty_List (Statements (Alt)) then
11164
11165 if Delay_Count = 1 then
11166 Append_List (Statements (Alt), Delay_Alt_List);
11167
11168 else
11169 Append_To (Delay_Alt_List,
11170 Make_Case_Statement_Alternative (Loc,
11171 Discrete_Choices => New_List (
11172 Make_Integer_Literal (Loc, Index)),
11173 Statements => Statements (Alt)));
11174 end if;
11175
11176 elsif Delay_Count = 1 then
11177
11178 -- If the single delay has no trailing statements, add a branch
11179 -- to the exit label to the selective wait.
11180
11181 Delay_Alt_List := New_List (
11182 Make_Goto_Statement (Loc,
11183 Name => New_Copy (Identifier (End_Lab))));
11184
11185 end if;
11186 end Process_Delay_Alternative;
11187
11188 -- Start of processing for Expand_N_Selective_Accept
11189
11190 begin
11191 Process_Statements_For_Controlled_Objects (N);
11192
11193 -- First insert some declarations before the select. The first is:
11194
11195 -- Ann : Address
11196
11197 -- This variable holds the parameters passed to the accept body. This
11198 -- declaration has already been inserted by the time we get here by
11199 -- a call to Expand_Accept_Declarations made from the semantics when
11200 -- processing the first accept statement contained in the select. We
11201 -- can find this entity as Accept_Address (E), where E is any of the
11202 -- entries references by contained accept statements.
11203
11204 -- The first step is to scan the list of Selective_Accept_Statements
11205 -- to find this entity, and also count the number of accepts, and
11206 -- determine if terminated, delay or else is present:
11207
11208 Num_Alts := 0;
11209
11210 Alt := First (Alts);
11211 while Present (Alt) loop
11212 Process_Statements_For_Controlled_Objects (Alt);
11213
11214 if Nkind (Alt) = N_Accept_Alternative then
11215 Add_Accept (Alt);
11216
11217 elsif Nkind (Alt) = N_Delay_Alternative then
11218 Delay_Count := Delay_Count + 1;
11219
11220 -- If the delays are relative delays, the delay expressions have
11221 -- type Standard_Duration. Otherwise they must have some time type
11222 -- recognized by GNAT.
11223
11224 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
11225 Time_Type := Standard_Duration;
11226 else
11227 Time_Type := Etype (Expression (Delay_Statement (Alt)));
11228
11229 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
11230 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
11231 then
11232 null;
11233 else
11234 Error_Msg_NE (
11235 "& is not a time type (RM 9.6(6))",
11236 Expression (Delay_Statement (Alt)), Time_Type);
11237 Time_Type := Standard_Duration;
11238 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
11239 end if;
11240 end if;
11241
11242 if No (Condition (Alt)) then
11243
11244 -- This guard will always be open
11245
11246 Check_Guard := False;
11247 end if;
11248
11249 elsif Nkind (Alt) = N_Terminate_Alternative then
11250 Adjust_Condition (Condition (Alt));
11251 Terminate_Alt := Alt;
11252 end if;
11253
11254 Num_Alts := Num_Alts + 1;
11255 Next (Alt);
11256 end loop;
11257
11258 Else_Present := Present (Else_Statements (N));
11259
11260 -- At the same time (see procedure Add_Accept) we build the accept list:
11261
11262 -- Qnn : Accept_List (1 .. num-select) := (
11263 -- (null-body, entry-index),
11264 -- (null-body, entry-index),
11265 -- ..
11266 -- (null_body, entry-index));
11267
11268 -- In the above declaration, null-body is True if the corresponding
11269 -- accept has no body, and false otherwise. The entry is either the
11270 -- entry index expression if there is no guard, or if a guard is
11271 -- present, then an if expression of the form:
11272
11273 -- (if guard then entry-index else Null_Task_Entry)
11274
11275 -- If a guard is statically known to be false, the entry can simply
11276 -- be omitted from the accept list.
11277
11278 Append_To (Decls,
11279 Make_Object_Declaration (Loc,
11280 Defining_Identifier => Qnam,
11281 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11282 Aliased_Present => True,
11283 Expression =>
11284 Make_Qualified_Expression (Loc,
11285 Subtype_Mark =>
11286 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11287 Expression =>
11288 Make_Aggregate (Loc, Expressions => Accept_List))));
11289
11290 -- Then we declare the variable that holds the index for the accept
11291 -- that will be selected for service:
11292
11293 -- Xnn : Select_Index;
11294
11295 Append_To (Decls,
11296 Make_Object_Declaration (Loc,
11297 Defining_Identifier => Xnam,
11298 Object_Definition =>
11299 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11300 Expression =>
11301 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11302
11303 -- After this follow procedure declarations for each accept body
11304
11305 -- procedure Pnn is
11306 -- begin
11307 -- ...
11308 -- end;
11309
11310 -- where the ... are statements from the corresponding procedure body.
11311 -- No parameters are involved, since the parameters are passed via Ann
11312 -- and the parameter references have already been expanded to be direct
11313 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11314 -- any embedded tasking statements (which would normally be illegal in
11315 -- procedures), have been converted to calls to the tasking runtime so
11316 -- there is no problem in putting them into procedures.
11317
11318 -- The original accept statement has been expanded into a block in
11319 -- the same fashion as for simple accepts (see Build_Accept_Body).
11320
11321 -- Note: we don't really need to build these procedures for the case
11322 -- where no delay statement is present, but it is just as easy to
11323 -- build them unconditionally, and not significantly inefficient,
11324 -- since if they are short they will be inlined anyway.
11325
11326 -- The procedure declarations have been assembled in Body_List
11327
11328 -- If delays are present, we must compute the required delay.
11329 -- We first generate the declarations:
11330
11331 -- Delay_Index : Boolean := 0;
11332 -- Delay_Min : Some_Time_Type.Time;
11333 -- Delay_Val : Some_Time_Type.Time;
11334
11335 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11336 -- active delay that is actually chosen as the basis for the possible
11337 -- delay if an immediate rendez-vous is not possible.
11338
11339 -- In the most common case there is a single delay statement, and this
11340 -- is handled specially.
11341
11342 if Delay_Count > 0 then
11343
11344 -- Generate the required declarations
11345
11346 Delay_Val :=
11347 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11348 Delay_Index :=
11349 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11350 Delay_Min :=
11351 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11352
11353 Append_To (Decls,
11354 Make_Object_Declaration (Loc,
11355 Defining_Identifier => Delay_Val,
11356 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11357
11358 Append_To (Decls,
11359 Make_Object_Declaration (Loc,
11360 Defining_Identifier => Delay_Index,
11361 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11362 Expression => Make_Integer_Literal (Loc, 0)));
11363
11364 Append_To (Decls,
11365 Make_Object_Declaration (Loc,
11366 Defining_Identifier => Delay_Min,
11367 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11368 Expression =>
11369 Unchecked_Convert_To (Time_Type,
11370 Make_Attribute_Reference (Loc,
11371 Prefix =>
11372 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11373 Attribute_Name => Name_Last))));
11374
11375 -- Create Duration and Delay_Mode objects used for passing a delay
11376 -- value to RTS
11377
11378 D := Make_Temporary (Loc, 'D');
11379 M := Make_Temporary (Loc, 'M');
11380
11381 declare
11382 Discr : Entity_Id;
11383
11384 begin
11385 -- Note that these values are defined in s-osprim.ads and must
11386 -- be kept in sync:
11387 --
11388 -- Relative : constant := 0;
11389 -- Absolute_Calendar : constant := 1;
11390 -- Absolute_RT : constant := 2;
11391
11392 if Time_Type = Standard_Duration then
11393 Discr := Make_Integer_Literal (Loc, 0);
11394
11395 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11396 Discr := Make_Integer_Literal (Loc, 1);
11397
11398 else
11399 pragma Assert
11400 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11401 Discr := Make_Integer_Literal (Loc, 2);
11402 end if;
11403
11404 Append_To (Decls,
11405 Make_Object_Declaration (Loc,
11406 Defining_Identifier => D,
11407 Object_Definition =>
11408 New_Occurrence_Of (Standard_Duration, Loc)));
11409
11410 Append_To (Decls,
11411 Make_Object_Declaration (Loc,
11412 Defining_Identifier => M,
11413 Object_Definition =>
11414 New_Occurrence_Of (Standard_Integer, Loc),
11415 Expression => Discr));
11416 end;
11417
11418 if Check_Guard then
11419 Guard_Open :=
11420 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11421
11422 Append_To (Decls,
11423 Make_Object_Declaration (Loc,
11424 Defining_Identifier => Guard_Open,
11425 Object_Definition =>
11426 New_Occurrence_Of (Standard_Boolean, Loc),
11427 Expression =>
11428 New_Occurrence_Of (Standard_False, Loc)));
11429 end if;
11430
11431 -- Delay_Count is zero, don't need M and D set (suppress warning)
11432
11433 else
11434 M := Empty;
11435 D := Empty;
11436 end if;
11437
11438 if Present (Terminate_Alt) then
11439
11440 -- If the terminate alternative guard is False, use
11441 -- Simple_Mode; otherwise use Terminate_Mode.
11442
11443 if Present (Condition (Terminate_Alt)) then
11444 Select_Mode := Make_If_Expression (Loc,
11445 New_List (Condition (Terminate_Alt),
11446 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11447 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11448 else
11449 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11450 end if;
11451
11452 elsif Else_Present or Delay_Count > 0 then
11453 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11454
11455 else
11456 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11457 end if;
11458
11459 Select_Call := Make_Select_Call (Select_Mode);
11460 Append (Select_Call, Stats);
11461
11462 -- Now generate code to act on the result. There is an entry
11463 -- in this case for each accept statement with a non-null body,
11464 -- followed by a branch to the statements that follow the Accept.
11465 -- In the absence of delay alternatives, we generate:
11466
11467 -- case X is
11468 -- when No_Rendezvous => -- omitted if simple mode
11469 -- goto Lab0;
11470
11471 -- when 1 =>
11472 -- P1n;
11473 -- goto Lab1;
11474
11475 -- when 2 =>
11476 -- P2n;
11477 -- goto Lab2;
11478
11479 -- when others =>
11480 -- goto Exit;
11481 -- end case;
11482 --
11483 -- Lab0: Else_Statements;
11484 -- goto exit;
11485
11486 -- Lab1: Trailing_Statements1;
11487 -- goto Exit;
11488 --
11489 -- Lab2: Trailing_Statements2;
11490 -- goto Exit;
11491 -- ...
11492 -- Exit:
11493
11494 -- Generate label for common exit
11495
11496 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11497
11498 -- First entry is the default case, when no rendezvous is possible
11499
11500 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11501
11502 if Else_Present then
11503
11504 -- If no rendezvous is possible, the else part is executed
11505
11506 Lab := Make_And_Declare_Label (0);
11507 Alt_Stats := New_List (
11508 Make_Goto_Statement (Loc,
11509 Name => New_Copy (Identifier (Lab))));
11510
11511 Append (Lab, Trailing_List);
11512 Append_List (Else_Statements (N), Trailing_List);
11513 Append_To (Trailing_List,
11514 Make_Goto_Statement (Loc,
11515 Name => New_Copy (Identifier (End_Lab))));
11516 else
11517 Alt_Stats := New_List (
11518 Make_Goto_Statement (Loc,
11519 Name => New_Copy (Identifier (End_Lab))));
11520 end if;
11521
11522 Append_To (Alt_List,
11523 Make_Case_Statement_Alternative (Loc,
11524 Discrete_Choices => Choices,
11525 Statements => Alt_Stats));
11526
11527 -- We make use of the fact that Accept_Index is an integer type, and
11528 -- generate successive literals for entries for each accept. Only those
11529 -- for which there is a body or trailing statements get a case entry.
11530
11531 Alt := First (Select_Alternatives (N));
11532 Proc := First (Body_List);
11533 while Present (Alt) loop
11534
11535 if Nkind (Alt) = N_Accept_Alternative then
11536 Process_Accept_Alternative (Alt, Index, Proc);
11537 Index := Index + 1;
11538
11539 if Present
11540 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11541 then
11542 Next (Proc);
11543 end if;
11544
11545 elsif Nkind (Alt) = N_Delay_Alternative then
11546 Process_Delay_Alternative (Alt, Delay_Num);
11547 Delay_Num := Delay_Num + 1;
11548 end if;
11549
11550 Next (Alt);
11551 end loop;
11552
11553 -- An others choice is always added to the main case, as well
11554 -- as the delay case (to satisfy the compiler).
11555
11556 Append_To (Alt_List,
11557 Make_Case_Statement_Alternative (Loc,
11558 Discrete_Choices =>
11559 New_List (Make_Others_Choice (Loc)),
11560 Statements =>
11561 New_List (Make_Goto_Statement (Loc,
11562 Name => New_Copy (Identifier (End_Lab))))));
11563
11564 Accept_Case := New_List (
11565 Make_Case_Statement (Loc,
11566 Expression => New_Occurrence_Of (Xnam, Loc),
11567 Alternatives => Alt_List));
11568
11569 Append_List (Trailing_List, Accept_Case);
11570 Append_List (Body_List, Decls);
11571
11572 -- Construct case statement for trailing statements of delay
11573 -- alternatives, if there are several of them.
11574
11575 if Delay_Count > 1 then
11576 Append_To (Delay_Alt_List,
11577 Make_Case_Statement_Alternative (Loc,
11578 Discrete_Choices =>
11579 New_List (Make_Others_Choice (Loc)),
11580 Statements =>
11581 New_List (Make_Null_Statement (Loc))));
11582
11583 Delay_Case := New_List (
11584 Make_Case_Statement (Loc,
11585 Expression => New_Occurrence_Of (Delay_Index, Loc),
11586 Alternatives => Delay_Alt_List));
11587 else
11588 Delay_Case := Delay_Alt_List;
11589 end if;
11590
11591 -- If there are no delay alternatives, we append the case statement
11592 -- to the statement list.
11593
11594 if Delay_Count = 0 then
11595 Append_List (Accept_Case, Stats);
11596
11597 -- Delay alternatives present
11598
11599 else
11600 -- If delay alternatives are present we generate:
11601
11602 -- find minimum delay.
11603 -- DX := minimum delay;
11604 -- M := <delay mode>;
11605 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11606 -- DX, MX, X);
11607 --
11608 -- if X = No_Rendezvous then
11609 -- case statement for delay statements.
11610 -- else
11611 -- case statement for accept alternatives.
11612 -- end if;
11613
11614 declare
11615 Cases : Node_Id;
11616 Stmt : Node_Id;
11617 Parms : List_Id;
11618 Parm : Node_Id;
11619 Conv : Node_Id;
11620
11621 begin
11622 -- The type of the delay expression is known to be legal
11623
11624 if Time_Type = Standard_Duration then
11625 Conv := New_Occurrence_Of (Delay_Min, Loc);
11626
11627 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11628 Conv := Make_Function_Call (Loc,
11629 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11630 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11631
11632 else
11633 pragma Assert
11634 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11635
11636 Conv := Make_Function_Call (Loc,
11637 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11638 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11639 end if;
11640
11641 Stmt := Make_Assignment_Statement (Loc,
11642 Name => New_Occurrence_Of (D, Loc),
11643 Expression => Conv);
11644
11645 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11646
11647 Parms := Parameter_Associations (Select_Call);
11648
11649 Parm := First (Parms);
11650 while Present (Parm) and then Parm /= Select_Mode loop
11651 Next (Parm);
11652 end loop;
11653
11654 pragma Assert (Present (Parm));
11655 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11656 Analyze (Parm);
11657
11658 -- Prepare two new parameters of Duration and Delay_Mode type
11659 -- which represent the value and the mode of the minimum delay.
11660
11661 Next (Parm);
11662 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11663 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11664
11665 -- Create a call to RTS
11666
11667 Rewrite (Select_Call,
11668 Make_Procedure_Call_Statement (Loc,
11669 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11670 Parameter_Associations => Parms));
11671
11672 -- This new call should follow the calculation of the minimum
11673 -- delay.
11674
11675 Insert_List_Before (Select_Call, Delay_List);
11676
11677 if Check_Guard then
11678 Stmt :=
11679 Make_Implicit_If_Statement (N,
11680 Condition => New_Occurrence_Of (Guard_Open, Loc),
11681 Then_Statements => New_List (
11682 New_Copy_Tree (Stmt),
11683 New_Copy_Tree (Select_Call)),
11684 Else_Statements => Accept_Or_Raise);
11685 Rewrite (Select_Call, Stmt);
11686 else
11687 Insert_Before (Select_Call, Stmt);
11688 end if;
11689
11690 Cases :=
11691 Make_Implicit_If_Statement (N,
11692 Condition => Make_Op_Eq (Loc,
11693 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11694 Right_Opnd =>
11695 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11696
11697 Then_Statements => Delay_Case,
11698 Else_Statements => Accept_Case);
11699
11700 Append (Cases, Stats);
11701 end;
11702 end if;
11703
11704 Append (End_Lab, Stats);
11705
11706 -- Replace accept statement with appropriate block
11707
11708 Rewrite (N,
11709 Make_Block_Statement (Loc,
11710 Declarations => Decls,
11711 Handled_Statement_Sequence =>
11712 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11713 Analyze (N);
11714
11715 -- Note: have to worry more about abort deferral in above code ???
11716
11717 -- Final step is to unstack the Accept_Address entries for all accept
11718 -- statements appearing in accept alternatives in the select statement
11719
11720 Alt := First (Alts);
11721 while Present (Alt) loop
11722 if Nkind (Alt) = N_Accept_Alternative then
11723 Remove_Last_Elmt (Accept_Address
11724 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11725 end if;
11726
11727 Next (Alt);
11728 end loop;
11729 end Expand_N_Selective_Accept;
11730
11731 -------------------------------------------
11732 -- Expand_N_Single_Protected_Declaration --
11733 -------------------------------------------
11734
11735 -- A single protected declaration should never be present after semantic
11736 -- analysis because it is transformed into a protected type declaration
11737 -- and an accompanying anonymous object. This routine ensures that the
11738 -- transformation takes place.
11739
11740 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11741 begin
11742 raise Program_Error;
11743 end Expand_N_Single_Protected_Declaration;
11744
11745 --------------------------------------
11746 -- Expand_N_Single_Task_Declaration --
11747 --------------------------------------
11748
11749 -- A single task declaration should never be present after semantic
11750 -- analysis because it is transformed into a task type declaration and
11751 -- an accompanying anonymous object. This routine ensures that the
11752 -- transformation takes place.
11753
11754 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11755 begin
11756 raise Program_Error;
11757 end Expand_N_Single_Task_Declaration;
11758
11759 ------------------------
11760 -- Expand_N_Task_Body --
11761 ------------------------
11762
11763 -- Given a task body
11764
11765 -- task body tname is
11766 -- <declarations>
11767 -- begin
11768 -- <statements>
11769 -- end x;
11770
11771 -- This expansion routine converts it into a procedure and sets the
11772 -- elaboration flag for the procedure to true, to represent the fact
11773 -- that the task body is now elaborated:
11774
11775 -- procedure tnameB (_Task : access tnameV) is
11776 -- discriminal : dtype renames _Task.discriminant;
11777
11778 -- procedure _clean is
11779 -- begin
11780 -- Abort_Defer.all;
11781 -- Complete_Task;
11782 -- Abort_Undefer.all;
11783 -- return;
11784 -- end _clean;
11785
11786 -- begin
11787 -- Abort_Undefer.all;
11788 -- <declarations>
11789 -- System.Task_Stages.Complete_Activation;
11790 -- <statements>
11791 -- at end
11792 -- _clean;
11793 -- end tnameB;
11794
11795 -- tnameE := True;
11796
11797 -- In addition, if the task body is an activator, then a call to activate
11798 -- tasks is added at the start of the statements, before the call to
11799 -- Complete_Activation, and if in addition the task is a master then it
11800 -- must be established as a master. These calls are inserted and analyzed
11801 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11802 -- expanded.
11803
11804 -- There is one discriminal declaration line generated for each
11805 -- discriminant that is present to provide an easy reference point for
11806 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11807
11808 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11809 -- task body procedures have a profile (Arg : System.Address). That is
11810 -- needed because GNARLI has to use the same access-to-subprogram type
11811 -- for all task types. We depend here on knowing that in GNAT, passing
11812 -- an address argument by value is identical to passing a record value
11813 -- by access (in either case a single pointer is passed), so even though
11814 -- this procedure has the wrong profile. In fact it's all OK, since the
11815 -- callings sequence is identical.
11816
11817 procedure Expand_N_Task_Body (N : Node_Id) is
11818 Loc : constant Source_Ptr := Sloc (N);
11819 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11820 Call : Node_Id;
11821 New_N : Node_Id;
11822
11823 Insert_Nod : Node_Id;
11824 -- Used to determine the proper location of wrapper body insertions
11825
11826 begin
11827 -- if no task body procedure, means we had an error in configurable
11828 -- run-time mode, and there is no point in proceeding further.
11829
11830 if No (Task_Body_Procedure (Ttyp)) then
11831 return;
11832 end if;
11833
11834 -- Add renaming declarations for discriminals and a declaration for the
11835 -- entry family index (if applicable).
11836
11837 Install_Private_Data_Declarations
11838 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11839
11840 -- Add a call to Abort_Undefer at the very beginning of the task
11841 -- body since this body is called with abort still deferred.
11842
11843 if Abort_Allowed then
11844 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11845 Insert_Before
11846 (First (Statements (Handled_Statement_Sequence (N))), Call);
11847 Analyze (Call);
11848 end if;
11849
11850 -- The statement part has already been protected with an at_end and
11851 -- cleanup actions. The call to Complete_Activation must be placed
11852 -- at the head of the sequence of statements of that block. The
11853 -- declarations have been merged in this sequence of statements but
11854 -- the first real statement is accessible from the First_Real_Statement
11855 -- field (which was set for exactly this purpose).
11856
11857 if Restricted_Profile then
11858 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11859 else
11860 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11861 end if;
11862
11863 Insert_Before
11864 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11865 Analyze (Call);
11866
11867 New_N :=
11868 Make_Subprogram_Body (Loc,
11869 Specification => Build_Task_Proc_Specification (Ttyp),
11870 Declarations => Declarations (N),
11871 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11872 Set_Is_Task_Body_Procedure (New_N);
11873
11874 -- If the task contains generic instantiations, cleanup actions are
11875 -- delayed until after instantiation. Transfer the activation chain to
11876 -- the subprogram, to insure that the activation call is properly
11877 -- generated. It the task body contains inner tasks, indicate that the
11878 -- subprogram is a task master.
11879
11880 if Delay_Cleanups (Ttyp) then
11881 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11882 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11883 end if;
11884
11885 Rewrite (N, New_N);
11886 Analyze (N);
11887
11888 -- Set elaboration flag immediately after task body. If the body is a
11889 -- subunit, the flag is set in the declarative part containing the stub.
11890
11891 if Nkind (Parent (N)) /= N_Subunit then
11892 Insert_After (N,
11893 Make_Assignment_Statement (Loc,
11894 Name =>
11895 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11896 Expression => New_Occurrence_Of (Standard_True, Loc)));
11897 end if;
11898
11899 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11900 -- the task body. At this point all wrapper specs have been created,
11901 -- frozen and included in the dispatch table for the task type.
11902
11903 if Ada_Version >= Ada_2005 then
11904 if Nkind (Parent (N)) = N_Subunit then
11905 Insert_Nod := Corresponding_Stub (Parent (N));
11906 else
11907 Insert_Nod := N;
11908 end if;
11909
11910 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11911 end if;
11912 end Expand_N_Task_Body;
11913
11914 ------------------------------------
11915 -- Expand_N_Task_Type_Declaration --
11916 ------------------------------------
11917
11918 -- We have several things to do. First we must create a Boolean flag used
11919 -- to mark if the body is elaborated yet. This variable gets set to True
11920 -- when the body of the task is elaborated (we can't rely on the normal
11921 -- ABE mechanism for the task body, since we need to pass an access to
11922 -- this elaboration boolean to the runtime routines).
11923
11924 -- taskE : aliased Boolean := False;
11925
11926 -- Next a variable is declared to hold the task stack size (either the
11927 -- default : Unspecified_Size, or a value that is set by a pragma
11928 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11929 -- the variable is initialized with this value:
11930
11931 -- taskZ : Size_Type := Unspecified_Size;
11932 -- or
11933 -- taskZ : Size_Type := Size_Type (size_expression);
11934
11935 -- Note: No variable is needed to hold the task relative deadline since
11936 -- its value would never be static because the parameter is of a private
11937 -- type (Ada.Real_Time.Time_Span).
11938
11939 -- Next we create a corresponding record type declaration used to represent
11940 -- values of this task. The general form of this type declaration is
11941
11942 -- type taskV (discriminants) is record
11943 -- _Task_Id : Task_Id;
11944 -- entry_family : array (bounds) of Void;
11945 -- _Priority : Integer := priority_expression;
11946 -- _Size : Size_Type := size_expression;
11947 -- _Task_Info : Task_Info_Type := task_info_expression;
11948 -- _CPU : Integer := cpu_range_expression;
11949 -- _Relative_Deadline : Time_Span := time_span_expression;
11950 -- _Domain : Dispatching_Domain := dd_expression;
11951 -- end record;
11952
11953 -- The discriminants are present only if the corresponding task type has
11954 -- discriminants, and they exactly mirror the task type discriminants.
11955
11956 -- The Id field is always present. It contains the Task_Id value, as set by
11957 -- the call to Create_Task. Note that although the task is limited, the
11958 -- task value record type is not limited, so there is no problem in passing
11959 -- this field as an out parameter to Create_Task.
11960
11961 -- One entry_family component is present for each entry family in the task
11962 -- definition. The bounds correspond to the bounds of the entry family
11963 -- (which may depend on discriminants). The element type is void, since we
11964 -- only need the bounds information for determining the entry index. Note
11965 -- that the use of an anonymous array would normally be illegal in this
11966 -- context, but this is a parser check, and the semantics is quite prepared
11967 -- to handle such a case.
11968
11969 -- The _Size field is present only if a Storage_Size pragma appears in the
11970 -- task definition. The expression captures the argument that was present
11971 -- in the pragma, and is used to override the task stack size otherwise
11972 -- associated with the task type.
11973
11974 -- The _Priority field is present only if the task entity has a Priority or
11975 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11976 -- definition clause). It will be filled at the freeze point, when the
11977 -- record init proc is built, to capture the expression of the rep item
11978 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11979 -- here since aspect evaluations are delayed till the freeze point.
11980
11981 -- The _Task_Info field is present only if a Task_Info pragma appears in
11982 -- the task definition. The expression captures the argument that was
11983 -- present in the pragma, and is used to provide the Task_Image parameter
11984 -- to the call to Create_Task.
11985
11986 -- The _CPU field is present only if the task entity has a CPU rep item
11987 -- (pragma, aspect specification or attribute definition clause). It will
11988 -- be filled at the freeze point, when the record init proc is built, to
11989 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11990 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11991 -- are delayed till the freeze point.
11992
11993 -- The _Relative_Deadline field is present only if a Relative_Deadline
11994 -- pragma appears in the task definition. The expression captures the
11995 -- argument that was present in the pragma, and is used to provide the
11996 -- Relative_Deadline parameter to the call to Create_Task.
11997
11998 -- The _Domain field is present only if the task entity has a
11999 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
12000 -- definition clause). It will be filled at the freeze point, when the
12001 -- record init proc is built, to capture the expression of the rep item
12002 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
12003 -- here since aspect evaluations are delayed till the freeze point.
12004
12005 -- When a task is declared, an instance of the task value record is
12006 -- created. The elaboration of this declaration creates the correct bounds
12007 -- for the entry families, and also evaluates the size, priority, and
12008 -- task_Info expressions if needed. The initialization routine for the task
12009 -- type itself then calls Create_Task with appropriate parameters to
12010 -- initialize the value of the Task_Id field.
12011
12012 -- Note: the address of this record is passed as the "Discriminants"
12013 -- parameter for Create_Task. Since Create_Task merely passes this onto the
12014 -- body procedure, it does not matter that it does not quite match the
12015 -- GNARLI model of what is being passed (the record contains more than just
12016 -- the discriminants, but the discriminants can be found from the record
12017 -- value).
12018
12019 -- The Entity_Id for this created record type is placed in the
12020 -- Corresponding_Record_Type field of the associated task type entity.
12021
12022 -- Next we create a procedure specification for the task body procedure:
12023
12024 -- procedure taskB (_Task : access taskV);
12025
12026 -- Note that this must come after the record type declaration, since
12027 -- the spec refers to this type. It turns out that the initialization
12028 -- procedure for the value type references the task body spec, but that's
12029 -- fine, since it won't be generated till the freeze point for the type,
12030 -- which is certainly after the task body spec declaration.
12031
12032 -- Finally, we set the task index value field of the entry attribute in
12033 -- the case of a simple entry.
12034
12035 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
12036 Loc : constant Source_Ptr := Sloc (N);
12037 TaskId : constant Entity_Id := Defining_Identifier (N);
12038 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
12039 Tasknm : constant Name_Id := Chars (Tasktyp);
12040 Taskdef : constant Node_Id := Task_Definition (N);
12041
12042 Body_Decl : Node_Id;
12043 Cdecls : List_Id;
12044 Decl_Stack : Node_Id;
12045 Elab_Decl : Node_Id;
12046 Ent_Stack : Entity_Id;
12047 Proc_Spec : Node_Id;
12048 Rec_Decl : Node_Id;
12049 Rec_Ent : Entity_Id;
12050 Size_Decl : Entity_Id;
12051 Task_Size : Node_Id;
12052
12053 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
12054 -- Searches the task definition T for the first occurrence of the pragma
12055 -- Relative Deadline. The caller has ensured that the pragma is present
12056 -- in the task definition. Note that this routine cannot be implemented
12057 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
12058 -- not chained because their expansion into a procedure call statement
12059 -- would cause a break in the chain.
12060
12061 ----------------------------------
12062 -- Get_Relative_Deadline_Pragma --
12063 ----------------------------------
12064
12065 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
12066 N : Node_Id;
12067
12068 begin
12069 N := First (Visible_Declarations (T));
12070 while Present (N) loop
12071 if Nkind (N) = N_Pragma
12072 and then Pragma_Name (N) = Name_Relative_Deadline
12073 then
12074 return N;
12075 end if;
12076
12077 Next (N);
12078 end loop;
12079
12080 N := First (Private_Declarations (T));
12081 while Present (N) loop
12082 if Nkind (N) = N_Pragma
12083 and then Pragma_Name (N) = Name_Relative_Deadline
12084 then
12085 return N;
12086 end if;
12087
12088 Next (N);
12089 end loop;
12090
12091 raise Program_Error;
12092 end Get_Relative_Deadline_Pragma;
12093
12094 -- Start of processing for Expand_N_Task_Type_Declaration
12095
12096 begin
12097 -- If already expanded, nothing to do
12098
12099 if Present (Corresponding_Record_Type (Tasktyp)) then
12100 return;
12101 end if;
12102
12103 -- Here we will do the expansion
12104
12105 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
12106
12107 Rec_Ent := Defining_Identifier (Rec_Decl);
12108 Cdecls := Component_Items (Component_List
12109 (Type_Definition (Rec_Decl)));
12110
12111 Qualify_Entity_Names (N);
12112
12113 -- First create the elaboration variable
12114
12115 Elab_Decl :=
12116 Make_Object_Declaration (Loc,
12117 Defining_Identifier =>
12118 Make_Defining_Identifier (Sloc (Tasktyp),
12119 Chars => New_External_Name (Tasknm, 'E')),
12120 Aliased_Present => True,
12121 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
12122 Expression => New_Occurrence_Of (Standard_False, Loc));
12123
12124 Insert_After (N, Elab_Decl);
12125
12126 -- Next create the declaration of the size variable (tasknmZ)
12127
12128 Set_Storage_Size_Variable (Tasktyp,
12129 Make_Defining_Identifier (Sloc (Tasktyp),
12130 Chars => New_External_Name (Tasknm, 'Z')));
12131
12132 if Present (Taskdef)
12133 and then Has_Storage_Size_Pragma (Taskdef)
12134 and then
12135 Is_OK_Static_Expression
12136 (Expression
12137 (First (Pragma_Argument_Associations
12138 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
12139 then
12140 Size_Decl :=
12141 Make_Object_Declaration (Loc,
12142 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12143 Object_Definition =>
12144 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12145 Expression =>
12146 Convert_To (RTE (RE_Size_Type),
12147 Relocate_Node
12148 (Expression (First (Pragma_Argument_Associations
12149 (Get_Rep_Pragma
12150 (TaskId, Name_Storage_Size)))))));
12151
12152 else
12153 Size_Decl :=
12154 Make_Object_Declaration (Loc,
12155 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12156 Object_Definition =>
12157 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12158 Expression =>
12159 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
12160 end if;
12161
12162 Insert_After (Elab_Decl, Size_Decl);
12163
12164 -- Next build the rest of the corresponding record declaration. This is
12165 -- done last, since the corresponding record initialization procedure
12166 -- will reference the previously created entities.
12167
12168 -- Fill in the component declarations -- first the _Task_Id field
12169
12170 Append_To (Cdecls,
12171 Make_Component_Declaration (Loc,
12172 Defining_Identifier =>
12173 Make_Defining_Identifier (Loc, Name_uTask_Id),
12174 Component_Definition =>
12175 Make_Component_Definition (Loc,
12176 Aliased_Present => False,
12177 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
12178 Loc))));
12179
12180 -- Declare static ATCB (that is, created by the expander) if we are
12181 -- using the Restricted run time.
12182
12183 if Restricted_Profile then
12184 Append_To (Cdecls,
12185 Make_Component_Declaration (Loc,
12186 Defining_Identifier =>
12187 Make_Defining_Identifier (Loc, Name_uATCB),
12188
12189 Component_Definition =>
12190 Make_Component_Definition (Loc,
12191 Aliased_Present => True,
12192 Subtype_Indication => Make_Subtype_Indication (Loc,
12193 Subtype_Mark =>
12194 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
12195
12196 Constraint =>
12197 Make_Index_Or_Discriminant_Constraint (Loc,
12198 Constraints =>
12199 New_List (Make_Integer_Literal (Loc, 0)))))));
12200
12201 end if;
12202
12203 -- Declare static stack (that is, created by the expander) if we are
12204 -- using the Restricted run time on a bare board configuration.
12205
12206 if Restricted_Profile and then Preallocated_Stacks_On_Target then
12207
12208 -- First we need to extract the appropriate stack size
12209
12210 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
12211
12212 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12213 declare
12214 Expr_N : constant Node_Id :=
12215 Expression (First (
12216 Pragma_Argument_Associations (
12217 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
12218 Etyp : constant Entity_Id := Etype (Expr_N);
12219 P : constant Node_Id := Parent (Expr_N);
12220
12221 begin
12222 -- The stack is defined inside the corresponding record.
12223 -- Therefore if the size of the stack is set by means of
12224 -- a discriminant, we must reference the discriminant of the
12225 -- corresponding record type.
12226
12227 if Nkind (Expr_N) in N_Has_Entity
12228 and then Present (Discriminal_Link (Entity (Expr_N)))
12229 then
12230 Task_Size :=
12231 New_Occurrence_Of
12232 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
12233 Loc);
12234 Set_Parent (Task_Size, P);
12235 Set_Etype (Task_Size, Etyp);
12236 Set_Analyzed (Task_Size);
12237
12238 else
12239 Task_Size := Relocate_Node (Expr_N);
12240 end if;
12241 end;
12242
12243 else
12244 Task_Size :=
12245 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
12246 end if;
12247
12248 Decl_Stack := Make_Component_Declaration (Loc,
12249 Defining_Identifier => Ent_Stack,
12250
12251 Component_Definition =>
12252 Make_Component_Definition (Loc,
12253 Aliased_Present => True,
12254 Subtype_Indication => Make_Subtype_Indication (Loc,
12255 Subtype_Mark =>
12256 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
12257
12258 Constraint =>
12259 Make_Index_Or_Discriminant_Constraint (Loc,
12260 Constraints => New_List (Make_Range (Loc,
12261 Low_Bound => Make_Integer_Literal (Loc, 1),
12262 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12263 Task_Size)))))));
12264
12265 Append_To (Cdecls, Decl_Stack);
12266
12267 -- The appropriate alignment for the stack is ensured by the run-time
12268 -- code in charge of task creation.
12269
12270 end if;
12271
12272 -- Add components for entry families
12273
12274 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12275
12276 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12277 -- item is present.
12278
12279 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12280 Append_To (Cdecls,
12281 Make_Component_Declaration (Loc,
12282 Defining_Identifier =>
12283 Make_Defining_Identifier (Loc, Name_uPriority),
12284 Component_Definition =>
12285 Make_Component_Definition (Loc,
12286 Aliased_Present => False,
12287 Subtype_Indication =>
12288 New_Occurrence_Of (Standard_Integer, Loc))));
12289 end if;
12290
12291 -- Add the _Size component if a Storage_Size pragma is present
12292
12293 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12294 Append_To (Cdecls,
12295 Make_Component_Declaration (Loc,
12296 Defining_Identifier =>
12297 Make_Defining_Identifier (Loc, Name_uSize),
12298
12299 Component_Definition =>
12300 Make_Component_Definition (Loc,
12301 Aliased_Present => False,
12302 Subtype_Indication =>
12303 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12304
12305 Expression =>
12306 Convert_To (RTE (RE_Size_Type),
12307 Relocate_Node (
12308 Expression (First (
12309 Pragma_Argument_Associations (
12310 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12311 end if;
12312
12313 -- Add the _Task_Info component if a Task_Info pragma is present
12314
12315 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12316 Append_To (Cdecls,
12317 Make_Component_Declaration (Loc,
12318 Defining_Identifier =>
12319 Make_Defining_Identifier (Loc, Name_uTask_Info),
12320
12321 Component_Definition =>
12322 Make_Component_Definition (Loc,
12323 Aliased_Present => False,
12324 Subtype_Indication =>
12325 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12326
12327 Expression => New_Copy (
12328 Expression (First (
12329 Pragma_Argument_Associations (
12330 Get_Rep_Pragma
12331 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12332 end if;
12333
12334 -- Add the _CPU component if a CPU rep item is present
12335
12336 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12337 Append_To (Cdecls,
12338 Make_Component_Declaration (Loc,
12339 Defining_Identifier =>
12340 Make_Defining_Identifier (Loc, Name_uCPU),
12341
12342 Component_Definition =>
12343 Make_Component_Definition (Loc,
12344 Aliased_Present => False,
12345 Subtype_Indication =>
12346 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12347 end if;
12348
12349 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12350 -- present. If we are using a restricted run time this component will
12351 -- not be added (deadlines are not allowed by the Ravenscar profile).
12352
12353 if not Restricted_Profile
12354 and then Present (Taskdef)
12355 and then Has_Relative_Deadline_Pragma (Taskdef)
12356 then
12357 Append_To (Cdecls,
12358 Make_Component_Declaration (Loc,
12359 Defining_Identifier =>
12360 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12361
12362 Component_Definition =>
12363 Make_Component_Definition (Loc,
12364 Aliased_Present => False,
12365 Subtype_Indication =>
12366 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12367
12368 Expression =>
12369 Convert_To (RTE (RE_Time_Span),
12370 Relocate_Node (
12371 Expression (First (
12372 Pragma_Argument_Associations (
12373 Get_Relative_Deadline_Pragma (Taskdef))))))));
12374 end if;
12375
12376 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12377 -- item is present. If we are using a restricted run time this component
12378 -- will not be added (dispatching domains are not allowed by the
12379 -- Ravenscar profile).
12380
12381 if not Restricted_Profile
12382 and then
12383 Has_Rep_Item
12384 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12385 then
12386 Append_To (Cdecls,
12387 Make_Component_Declaration (Loc,
12388 Defining_Identifier =>
12389 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12390
12391 Component_Definition =>
12392 Make_Component_Definition (Loc,
12393 Aliased_Present => False,
12394 Subtype_Indication =>
12395 New_Occurrence_Of
12396 (RTE (RE_Dispatching_Domain_Access), Loc))));
12397 end if;
12398
12399 Insert_After (Size_Decl, Rec_Decl);
12400
12401 -- Analyze the record declaration immediately after construction,
12402 -- because the initialization procedure is needed for single task
12403 -- declarations before the next entity is analyzed.
12404
12405 Analyze (Rec_Decl);
12406
12407 -- Create the declaration of the task body procedure
12408
12409 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12410 Body_Decl :=
12411 Make_Subprogram_Declaration (Loc,
12412 Specification => Proc_Spec);
12413 Set_Is_Task_Body_Procedure (Body_Decl);
12414
12415 Insert_After (Rec_Decl, Body_Decl);
12416
12417 -- The subprogram does not comes from source, so we have to indicate the
12418 -- need for debugging information explicitly.
12419
12420 if Comes_From_Source (Original_Node (N)) then
12421 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12422 end if;
12423
12424 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12425 -- the corresponding record has been frozen.
12426
12427 if Ada_Version >= Ada_2005 then
12428 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12429 end if;
12430
12431 -- Ada 2005 (AI-345): We must defer freezing to allow further
12432 -- declaration of primitive subprograms covering task interfaces
12433
12434 if Ada_Version <= Ada_95 then
12435
12436 -- Now we can freeze the corresponding record. This needs manually
12437 -- freezing, since it is really part of the task type, and the task
12438 -- type is frozen at this stage. We of course need the initialization
12439 -- procedure for this corresponding record type and we won't get it
12440 -- in time if we don't freeze now.
12441
12442 declare
12443 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12444 begin
12445 if Is_Non_Empty_List (L) then
12446 Insert_List_After (Body_Decl, L);
12447 end if;
12448 end;
12449 end if;
12450
12451 -- Complete the expansion of access types to the current task type, if
12452 -- any were declared.
12453
12454 Expand_Previous_Access_Type (Tasktyp);
12455
12456 -- Create wrappers for entries that have contract cases, preconditions
12457 -- and postconditions.
12458
12459 declare
12460 Ent : Entity_Id;
12461
12462 begin
12463 Ent := First_Entity (Tasktyp);
12464 while Present (Ent) loop
12465 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12466 Build_Contract_Wrapper (Ent, N);
12467 end if;
12468
12469 Next_Entity (Ent);
12470 end loop;
12471 end;
12472 end Expand_N_Task_Type_Declaration;
12473
12474 -------------------------------
12475 -- Expand_N_Timed_Entry_Call --
12476 -------------------------------
12477
12478 -- A timed entry call in normal case is not implemented using ATC mechanism
12479 -- anymore for efficiency reason.
12480
12481 -- select
12482 -- T.E;
12483 -- S1;
12484 -- or
12485 -- delay D;
12486 -- S2;
12487 -- end select;
12488
12489 -- is expanded as follows:
12490
12491 -- 1) When T.E is a task entry_call;
12492
12493 -- declare
12494 -- B : Boolean;
12495 -- X : Task_Entry_Index := <entry index>;
12496 -- DX : Duration := To_Duration (D);
12497 -- M : Delay_Mode := <discriminant>;
12498 -- P : parms := (parm, parm, parm);
12499
12500 -- begin
12501 -- Timed_Protected_Entry_Call
12502 -- (<acceptor-task>, X, P'Address, DX, M, B);
12503 -- if B then
12504 -- S1;
12505 -- else
12506 -- S2;
12507 -- end if;
12508 -- end;
12509
12510 -- 2) When T.E is a protected entry_call;
12511
12512 -- declare
12513 -- B : Boolean;
12514 -- X : Protected_Entry_Index := <entry index>;
12515 -- DX : Duration := To_Duration (D);
12516 -- M : Delay_Mode := <discriminant>;
12517 -- P : parms := (parm, parm, parm);
12518
12519 -- begin
12520 -- Timed_Protected_Entry_Call
12521 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12522 -- if B then
12523 -- S1;
12524 -- else
12525 -- S2;
12526 -- end if;
12527 -- end;
12528
12529 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12530 -- is no delay and the triggering statements are executed. We first
12531 -- determine the kind of the triggering call and then execute a
12532 -- synchronized operation or a direct call.
12533
12534 -- declare
12535 -- B : Boolean := False;
12536 -- C : Ada.Tags.Prim_Op_Kind;
12537 -- DX : Duration := To_Duration (D)
12538 -- K : Ada.Tags.Tagged_Kind :=
12539 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12540 -- M : Integer :=...;
12541 -- P : Parameters := (Param1 .. ParamN);
12542 -- S : Integer;
12543
12544 -- begin
12545 -- if K = Ada.Tags.TK_Limited_Tagged
12546 -- or else K = Ada.Tags.TK_Tagged
12547 -- then
12548 -- <dispatching-call>;
12549 -- B := True;
12550
12551 -- else
12552 -- S :=
12553 -- Ada.Tags.Get_Offset_Index
12554 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12555
12556 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12557
12558 -- if C = POK_Protected_Entry
12559 -- or else C = POK_Task_Entry
12560 -- then
12561 -- Param1 := P.Param1;
12562 -- ...
12563 -- ParamN := P.ParamN;
12564 -- end if;
12565
12566 -- if B then
12567 -- if C = POK_Procedure
12568 -- or else C = POK_Protected_Procedure
12569 -- or else C = POK_Task_Procedure
12570 -- then
12571 -- <dispatching-call>;
12572 -- end if;
12573 -- end if;
12574 -- end if;
12575
12576 -- if B then
12577 -- <triggering-statements>
12578 -- else
12579 -- <timed-statements>
12580 -- end if;
12581 -- end;
12582
12583 -- The triggering statement and the sequence of timed statements have not
12584 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12585 -- global references if within an instantiation.
12586
12587 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12588 Loc : constant Source_Ptr := Sloc (N);
12589
12590 Actuals : List_Id;
12591 Blk_Typ : Entity_Id;
12592 Call : Node_Id;
12593 Call_Ent : Entity_Id;
12594 Conc_Typ_Stmts : List_Id;
12595 Concval : Node_Id;
12596 D_Alt : constant Node_Id := Delay_Alternative (N);
12597 D_Conv : Node_Id;
12598 D_Disc : Node_Id;
12599 D_Stat : Node_Id := Delay_Statement (D_Alt);
12600 D_Stats : List_Id;
12601 D_Type : Entity_Id;
12602 Decls : List_Id;
12603 Dummy : Node_Id;
12604 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12605 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12606 E_Stats : List_Id;
12607 Ename : Node_Id;
12608 Formals : List_Id;
12609 Index : Node_Id;
12610 Is_Disp_Select : Boolean;
12611 Lim_Typ_Stmts : List_Id;
12612 N_Stats : List_Id;
12613 Obj : Entity_Id;
12614 Param : Node_Id;
12615 Params : List_Id;
12616 Stmt : Node_Id;
12617 Stmts : List_Id;
12618 Unpack : List_Id;
12619
12620 B : Entity_Id; -- Call status flag
12621 C : Entity_Id; -- Call kind
12622 D : Entity_Id; -- Delay
12623 K : Entity_Id; -- Tagged kind
12624 M : Entity_Id; -- Delay mode
12625 P : Entity_Id; -- Parameter block
12626 S : Entity_Id; -- Primitive operation slot
12627
12628 -- Start of processing for Expand_N_Timed_Entry_Call
12629
12630 begin
12631 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12632 -- was already reported on spec, so do not attempt to expand the call.
12633
12634 if Restriction_Active (No_Select_Statements) then
12635 return;
12636 end if;
12637
12638 Process_Statements_For_Controlled_Objects (E_Alt);
12639 Process_Statements_For_Controlled_Objects (D_Alt);
12640
12641 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12642
12643 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12644 -- may wrap them in blocks.
12645
12646 E_Stats := Statements (E_Alt);
12647 D_Stats := Statements (D_Alt);
12648
12649 -- The arguments in the call may require dynamic allocation, and the
12650 -- call statement may have been transformed into a block. The block
12651 -- may contain additional declarations for internal entities, and the
12652 -- original call is found by sequential search.
12653
12654 if Nkind (E_Call) = N_Block_Statement then
12655 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12656 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12657 N_Entry_Call_Statement)
12658 loop
12659 Next (E_Call);
12660 end loop;
12661 end if;
12662
12663 Is_Disp_Select :=
12664 Ada_Version >= Ada_2005
12665 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12666
12667 if Is_Disp_Select then
12668 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12669 Decls := New_List;
12670
12671 Stmts := New_List;
12672
12673 -- Generate:
12674 -- B : Boolean := False;
12675
12676 B := Build_B (Loc, Decls);
12677
12678 -- Generate:
12679 -- C : Ada.Tags.Prim_Op_Kind;
12680
12681 C := Build_C (Loc, Decls);
12682
12683 -- Because the analysis of all statements was disabled, manually
12684 -- analyze the delay statement.
12685
12686 Analyze (D_Stat);
12687 D_Stat := Original_Node (D_Stat);
12688
12689 else
12690 -- Build an entry call using Simple_Entry_Call
12691
12692 Extract_Entry (E_Call, Concval, Ename, Index);
12693 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12694
12695 Decls := Declarations (E_Call);
12696 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12697
12698 if No (Decls) then
12699 Decls := New_List;
12700 end if;
12701
12702 -- Generate:
12703 -- B : Boolean;
12704
12705 B := Make_Defining_Identifier (Loc, Name_uB);
12706
12707 Prepend_To (Decls,
12708 Make_Object_Declaration (Loc,
12709 Defining_Identifier => B,
12710 Object_Definition =>
12711 New_Occurrence_Of (Standard_Boolean, Loc)));
12712 end if;
12713
12714 -- Duration and mode processing
12715
12716 D_Type := Base_Type (Etype (Expression (D_Stat)));
12717
12718 -- Use the type of the delay expression (Calendar or Real_Time) to
12719 -- generate the appropriate conversion.
12720
12721 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12722 D_Disc := Make_Integer_Literal (Loc, 0);
12723 D_Conv := Relocate_Node (Expression (D_Stat));
12724
12725 elsif Is_RTE (D_Type, RO_CA_Time) then
12726 D_Disc := Make_Integer_Literal (Loc, 1);
12727 D_Conv :=
12728 Make_Function_Call (Loc,
12729 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12730 Parameter_Associations =>
12731 New_List (New_Copy (Expression (D_Stat))));
12732
12733 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12734 D_Disc := Make_Integer_Literal (Loc, 2);
12735 D_Conv :=
12736 Make_Function_Call (Loc,
12737 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12738 Parameter_Associations =>
12739 New_List (New_Copy (Expression (D_Stat))));
12740 end if;
12741
12742 D := Make_Temporary (Loc, 'D');
12743
12744 -- Generate:
12745 -- D : Duration;
12746
12747 Append_To (Decls,
12748 Make_Object_Declaration (Loc,
12749 Defining_Identifier => D,
12750 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12751
12752 M := Make_Temporary (Loc, 'M');
12753
12754 -- Generate:
12755 -- M : Integer := (0 | 1 | 2);
12756
12757 Append_To (Decls,
12758 Make_Object_Declaration (Loc,
12759 Defining_Identifier => M,
12760 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12761 Expression => D_Disc));
12762
12763 -- Do the assignment at this stage only because the evaluation of the
12764 -- expression must not occur before (see ACVC C97302A).
12765
12766 Append_To (Stmts,
12767 Make_Assignment_Statement (Loc,
12768 Name => New_Occurrence_Of (D, Loc),
12769 Expression => D_Conv));
12770
12771 -- Parameter block processing
12772
12773 -- Manually create the parameter block for dispatching calls. In the
12774 -- case of entries, the block has already been created during the call
12775 -- to Build_Simple_Entry_Call.
12776
12777 if Is_Disp_Select then
12778
12779 -- Tagged kind processing, generate:
12780 -- K : Ada.Tags.Tagged_Kind :=
12781 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12782
12783 K := Build_K (Loc, Decls, Obj);
12784
12785 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12786 P :=
12787 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12788
12789 -- Dispatch table slot processing, generate:
12790 -- S : Integer;
12791
12792 S := Build_S (Loc, Decls);
12793
12794 -- Generate:
12795 -- S := Ada.Tags.Get_Offset_Index
12796 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12797
12798 Conc_Typ_Stmts :=
12799 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12800
12801 -- Generate:
12802 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12803
12804 -- where Obj is the controlling formal parameter, S is the dispatch
12805 -- table slot number of the dispatching operation, P is the wrapped
12806 -- parameter block, D is the duration, M is the duration mode, C is
12807 -- the call kind and B is the call status.
12808
12809 Params := New_List;
12810
12811 Append_To (Params, New_Copy_Tree (Obj));
12812 Append_To (Params, New_Occurrence_Of (S, Loc));
12813 Append_To (Params,
12814 Make_Attribute_Reference (Loc,
12815 Prefix => New_Occurrence_Of (P, Loc),
12816 Attribute_Name => Name_Address));
12817 Append_To (Params, New_Occurrence_Of (D, Loc));
12818 Append_To (Params, New_Occurrence_Of (M, Loc));
12819 Append_To (Params, New_Occurrence_Of (C, Loc));
12820 Append_To (Params, New_Occurrence_Of (B, Loc));
12821
12822 Append_To (Conc_Typ_Stmts,
12823 Make_Procedure_Call_Statement (Loc,
12824 Name =>
12825 New_Occurrence_Of
12826 (Find_Prim_Op
12827 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12828 Parameter_Associations => Params));
12829
12830 -- Generate:
12831 -- if C = POK_Protected_Entry
12832 -- or else C = POK_Task_Entry
12833 -- then
12834 -- Param1 := P.Param1;
12835 -- ...
12836 -- ParamN := P.ParamN;
12837 -- end if;
12838
12839 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12840
12841 -- Generate the if statement only when the packed parameters need
12842 -- explicit assignments to their corresponding actuals.
12843
12844 if Present (Unpack) then
12845 Append_To (Conc_Typ_Stmts,
12846 Make_Implicit_If_Statement (N,
12847
12848 Condition =>
12849 Make_Or_Else (Loc,
12850 Left_Opnd =>
12851 Make_Op_Eq (Loc,
12852 Left_Opnd => New_Occurrence_Of (C, Loc),
12853 Right_Opnd =>
12854 New_Occurrence_Of
12855 (RTE (RE_POK_Protected_Entry), Loc)),
12856
12857 Right_Opnd =>
12858 Make_Op_Eq (Loc,
12859 Left_Opnd => New_Occurrence_Of (C, Loc),
12860 Right_Opnd =>
12861 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12862
12863 Then_Statements => Unpack));
12864 end if;
12865
12866 -- Generate:
12867
12868 -- if B then
12869 -- if C = POK_Procedure
12870 -- or else C = POK_Protected_Procedure
12871 -- or else C = POK_Task_Procedure
12872 -- then
12873 -- <dispatching-call>
12874 -- end if;
12875 -- end if;
12876
12877 N_Stats := New_List (
12878 Make_Implicit_If_Statement (N,
12879 Condition =>
12880 Make_Or_Else (Loc,
12881 Left_Opnd =>
12882 Make_Op_Eq (Loc,
12883 Left_Opnd => New_Occurrence_Of (C, Loc),
12884 Right_Opnd =>
12885 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12886
12887 Right_Opnd =>
12888 Make_Or_Else (Loc,
12889 Left_Opnd =>
12890 Make_Op_Eq (Loc,
12891 Left_Opnd => New_Occurrence_Of (C, Loc),
12892 Right_Opnd =>
12893 New_Occurrence_Of (RTE (
12894 RE_POK_Protected_Procedure), Loc)),
12895 Right_Opnd =>
12896 Make_Op_Eq (Loc,
12897 Left_Opnd => New_Occurrence_Of (C, Loc),
12898 Right_Opnd =>
12899 New_Occurrence_Of
12900 (RTE (RE_POK_Task_Procedure), Loc)))),
12901
12902 Then_Statements => New_List (E_Call)));
12903
12904 Append_To (Conc_Typ_Stmts,
12905 Make_Implicit_If_Statement (N,
12906 Condition => New_Occurrence_Of (B, Loc),
12907 Then_Statements => N_Stats));
12908
12909 -- Generate:
12910 -- <dispatching-call>;
12911 -- B := True;
12912
12913 Lim_Typ_Stmts :=
12914 New_List (New_Copy_Tree (E_Call),
12915 Make_Assignment_Statement (Loc,
12916 Name => New_Occurrence_Of (B, Loc),
12917 Expression => New_Occurrence_Of (Standard_True, Loc)));
12918
12919 -- Generate:
12920 -- if K = Ada.Tags.TK_Limited_Tagged
12921 -- or else K = Ada.Tags.TK_Tagged
12922 -- then
12923 -- Lim_Typ_Stmts
12924 -- else
12925 -- Conc_Typ_Stmts
12926 -- end if;
12927
12928 Append_To (Stmts,
12929 Make_Implicit_If_Statement (N,
12930 Condition => Build_Dispatching_Tag_Check (K, N),
12931 Then_Statements => Lim_Typ_Stmts,
12932 Else_Statements => Conc_Typ_Stmts));
12933
12934 -- Generate:
12935
12936 -- if B then
12937 -- <triggering-statements>
12938 -- else
12939 -- <timed-statements>
12940 -- end if;
12941
12942 Append_To (Stmts,
12943 Make_Implicit_If_Statement (N,
12944 Condition => New_Occurrence_Of (B, Loc),
12945 Then_Statements => E_Stats,
12946 Else_Statements => D_Stats));
12947
12948 else
12949 -- Simple case of a nondispatching trigger. Skip assignments to
12950 -- temporaries created for in-out parameters.
12951
12952 -- This makes unwarranted assumptions about the shape of the expanded
12953 -- tree for the call, and should be cleaned up ???
12954
12955 Stmt := First (Stmts);
12956 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12957 Next (Stmt);
12958 end loop;
12959
12960 -- Do the assignment at this stage only because the evaluation
12961 -- of the expression must not occur before (see ACVC C97302A).
12962
12963 Insert_Before (Stmt,
12964 Make_Assignment_Statement (Loc,
12965 Name => New_Occurrence_Of (D, Loc),
12966 Expression => D_Conv));
12967
12968 Call := Stmt;
12969 Params := Parameter_Associations (Call);
12970
12971 -- For a protected type, we build a Timed_Protected_Entry_Call
12972
12973 if Is_Protected_Type (Etype (Concval)) then
12974
12975 -- Create a new call statement
12976
12977 Param := First (Params);
12978 while Present (Param)
12979 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12980 loop
12981 Next (Param);
12982 end loop;
12983
12984 Dummy := Remove_Next (Next (Param));
12985
12986 -- Remove garbage is following the Cancel_Param if present
12987
12988 Dummy := Next (Param);
12989
12990 -- Remove the mode of the Protected_Entry_Call call, then remove
12991 -- the Communication_Block of the Protected_Entry_Call call, and
12992 -- finally add Duration and a Delay_Mode parameter
12993
12994 pragma Assert (Present (Param));
12995 Rewrite (Param, New_Occurrence_Of (D, Loc));
12996
12997 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12998
12999 -- Add a Boolean flag for successful entry call
13000
13001 Append_To (Params, New_Occurrence_Of (B, Loc));
13002
13003 case Corresponding_Runtime_Package (Etype (Concval)) is
13004 when System_Tasking_Protected_Objects_Entries =>
13005 Rewrite (Call,
13006 Make_Procedure_Call_Statement (Loc,
13007 Name =>
13008 New_Occurrence_Of
13009 (RTE (RE_Timed_Protected_Entry_Call), Loc),
13010 Parameter_Associations => Params));
13011
13012 when others =>
13013 raise Program_Error;
13014 end case;
13015
13016 -- For the task case, build a Timed_Task_Entry_Call
13017
13018 else
13019 -- Create a new call statement
13020
13021 Append_To (Params, New_Occurrence_Of (D, Loc));
13022 Append_To (Params, New_Occurrence_Of (M, Loc));
13023 Append_To (Params, New_Occurrence_Of (B, Loc));
13024
13025 Rewrite (Call,
13026 Make_Procedure_Call_Statement (Loc,
13027 Name =>
13028 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
13029 Parameter_Associations => Params));
13030 end if;
13031
13032 Append_To (Stmts,
13033 Make_Implicit_If_Statement (N,
13034 Condition => New_Occurrence_Of (B, Loc),
13035 Then_Statements => E_Stats,
13036 Else_Statements => D_Stats));
13037 end if;
13038
13039 Rewrite (N,
13040 Make_Block_Statement (Loc,
13041 Declarations => Decls,
13042 Handled_Statement_Sequence =>
13043 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
13044
13045 Analyze (N);
13046 end Expand_N_Timed_Entry_Call;
13047
13048 ----------------------------------------
13049 -- Expand_Protected_Body_Declarations --
13050 ----------------------------------------
13051
13052 procedure Expand_Protected_Body_Declarations
13053 (N : Node_Id;
13054 Spec_Id : Entity_Id)
13055 is
13056 begin
13057 if No_Run_Time_Mode then
13058 Error_Msg_CRT ("protected body", N);
13059 return;
13060
13061 elsif Expander_Active then
13062
13063 -- Associate discriminals with the first subprogram or entry body to
13064 -- be expanded.
13065
13066 if Present (First_Protected_Operation (Declarations (N))) then
13067 Set_Discriminals (Parent (Spec_Id));
13068 end if;
13069 end if;
13070 end Expand_Protected_Body_Declarations;
13071
13072 -------------------------
13073 -- External_Subprogram --
13074 -------------------------
13075
13076 function External_Subprogram (E : Entity_Id) return Entity_Id is
13077 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
13078
13079 begin
13080 -- The internal and external subprograms follow each other on the entity
13081 -- chain. Note that previously private operations had no separate
13082 -- external subprogram. We now create one in all cases, because a
13083 -- private operation may actually appear in an external call, through
13084 -- a 'Access reference used for a callback.
13085
13086 -- If the operation is a function that returns an anonymous access type,
13087 -- the corresponding itype appears before the operation, and must be
13088 -- skipped.
13089
13090 -- This mechanism is fragile, there should be a real link between the
13091 -- two versions of the operation, but there is no place to put it ???
13092
13093 if Is_Access_Type (Next_Entity (Subp)) then
13094 return Next_Entity (Next_Entity (Subp));
13095 else
13096 return Next_Entity (Subp);
13097 end if;
13098 end External_Subprogram;
13099
13100 ------------------------------
13101 -- Extract_Dispatching_Call --
13102 ------------------------------
13103
13104 procedure Extract_Dispatching_Call
13105 (N : Node_Id;
13106 Call_Ent : out Entity_Id;
13107 Object : out Entity_Id;
13108 Actuals : out List_Id;
13109 Formals : out List_Id)
13110 is
13111 Call_Nam : Node_Id;
13112
13113 begin
13114 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
13115
13116 if Present (Original_Node (N)) then
13117 Call_Nam := Name (Original_Node (N));
13118 else
13119 Call_Nam := Name (N);
13120 end if;
13121
13122 -- Retrieve the name of the dispatching procedure. It contains the
13123 -- dispatch table slot number.
13124
13125 loop
13126 case Nkind (Call_Nam) is
13127 when N_Identifier =>
13128 exit;
13129
13130 when N_Selected_Component =>
13131 Call_Nam := Selector_Name (Call_Nam);
13132
13133 when others =>
13134 raise Program_Error;
13135
13136 end case;
13137 end loop;
13138
13139 Actuals := Parameter_Associations (N);
13140 Call_Ent := Entity (Call_Nam);
13141 Formals := Parameter_Specifications (Parent (Call_Ent));
13142 Object := First (Actuals);
13143
13144 if Present (Original_Node (Object)) then
13145 Object := Original_Node (Object);
13146 end if;
13147
13148 -- If the type of the dispatching object is an access type then return
13149 -- an explicit dereference.
13150
13151 if Is_Access_Type (Etype (Object)) then
13152 Object := Make_Explicit_Dereference (Sloc (N), Object);
13153 Analyze (Object);
13154 end if;
13155 end Extract_Dispatching_Call;
13156
13157 -------------------
13158 -- Extract_Entry --
13159 -------------------
13160
13161 procedure Extract_Entry
13162 (N : Node_Id;
13163 Concval : out Node_Id;
13164 Ename : out Node_Id;
13165 Index : out Node_Id)
13166 is
13167 Nam : constant Node_Id := Name (N);
13168
13169 begin
13170 -- For a simple entry, the name is a selected component, with the
13171 -- prefix being the task value, and the selector being the entry.
13172
13173 if Nkind (Nam) = N_Selected_Component then
13174 Concval := Prefix (Nam);
13175 Ename := Selector_Name (Nam);
13176 Index := Empty;
13177
13178 -- For a member of an entry family, the name is an indexed component
13179 -- where the prefix is a selected component, whose prefix in turn is
13180 -- the task value, and whose selector is the entry family. The single
13181 -- expression in the expressions list of the indexed component is the
13182 -- subscript for the family.
13183
13184 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13185 Concval := Prefix (Prefix (Nam));
13186 Ename := Selector_Name (Prefix (Nam));
13187 Index := First (Expressions (Nam));
13188 end if;
13189
13190 -- Through indirection, the type may actually be a limited view of a
13191 -- concurrent type. When compiling a call, the non-limited view of the
13192 -- type is visible.
13193
13194 if From_Limited_With (Etype (Concval)) then
13195 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13196 end if;
13197 end Extract_Entry;
13198
13199 -------------------
13200 -- Family_Offset --
13201 -------------------
13202
13203 function Family_Offset
13204 (Loc : Source_Ptr;
13205 Hi : Node_Id;
13206 Lo : Node_Id;
13207 Ttyp : Entity_Id;
13208 Cap : Boolean) return Node_Id
13209 is
13210 Ityp : Entity_Id;
13211 Real_Hi : Node_Id;
13212 Real_Lo : Node_Id;
13213
13214 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13215 -- If one of the bounds is a reference to a discriminant, replace with
13216 -- corresponding discriminal of type. Within the body of a task retrieve
13217 -- the renamed discriminant by simple visibility, using its generated
13218 -- name. Within a protected object, find the original discriminant and
13219 -- replace it with the discriminal of the current protected operation.
13220
13221 ------------------------------
13222 -- Convert_Discriminant_Ref --
13223 ------------------------------
13224
13225 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13226 Loc : constant Source_Ptr := Sloc (Bound);
13227 B : Node_Id;
13228 D : Entity_Id;
13229
13230 begin
13231 if Is_Entity_Name (Bound)
13232 and then Ekind (Entity (Bound)) = E_Discriminant
13233 then
13234 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13235 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13236 Find_Direct_Name (B);
13237
13238 elsif Is_Protected_Type (Ttyp) then
13239 D := First_Discriminant (Ttyp);
13240 while Chars (D) /= Chars (Entity (Bound)) loop
13241 Next_Discriminant (D);
13242 end loop;
13243
13244 B := New_Occurrence_Of (Discriminal (D), Loc);
13245
13246 else
13247 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13248 end if;
13249
13250 elsif Nkind (Bound) = N_Attribute_Reference then
13251 return Bound;
13252
13253 else
13254 B := New_Copy_Tree (Bound);
13255 end if;
13256
13257 return
13258 Make_Attribute_Reference (Loc,
13259 Attribute_Name => Name_Pos,
13260 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13261 Expressions => New_List (B));
13262 end Convert_Discriminant_Ref;
13263
13264 -- Start of processing for Family_Offset
13265
13266 begin
13267 Real_Hi := Convert_Discriminant_Ref (Hi);
13268 Real_Lo := Convert_Discriminant_Ref (Lo);
13269
13270 if Cap then
13271 if Is_Task_Type (Ttyp) then
13272 Ityp := RTE (RE_Task_Entry_Index);
13273 else
13274 Ityp := RTE (RE_Protected_Entry_Index);
13275 end if;
13276
13277 Real_Hi :=
13278 Make_Attribute_Reference (Loc,
13279 Prefix => New_Occurrence_Of (Ityp, Loc),
13280 Attribute_Name => Name_Min,
13281 Expressions => New_List (
13282 Real_Hi,
13283 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13284
13285 Real_Lo :=
13286 Make_Attribute_Reference (Loc,
13287 Prefix => New_Occurrence_Of (Ityp, Loc),
13288 Attribute_Name => Name_Max,
13289 Expressions => New_List (
13290 Real_Lo,
13291 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13292 end if;
13293
13294 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13295 end Family_Offset;
13296
13297 -----------------
13298 -- Family_Size --
13299 -----------------
13300
13301 function Family_Size
13302 (Loc : Source_Ptr;
13303 Hi : Node_Id;
13304 Lo : Node_Id;
13305 Ttyp : Entity_Id;
13306 Cap : Boolean) return Node_Id
13307 is
13308 Ityp : Entity_Id;
13309
13310 begin
13311 if Is_Task_Type (Ttyp) then
13312 Ityp := RTE (RE_Task_Entry_Index);
13313 else
13314 Ityp := RTE (RE_Protected_Entry_Index);
13315 end if;
13316
13317 return
13318 Make_Attribute_Reference (Loc,
13319 Prefix => New_Occurrence_Of (Ityp, Loc),
13320 Attribute_Name => Name_Max,
13321 Expressions => New_List (
13322 Make_Op_Add (Loc,
13323 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13324 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13325 Make_Integer_Literal (Loc, 0)));
13326 end Family_Size;
13327
13328 ----------------------------
13329 -- Find_Enclosing_Context --
13330 ----------------------------
13331
13332 procedure Find_Enclosing_Context
13333 (N : Node_Id;
13334 Context : out Node_Id;
13335 Context_Id : out Entity_Id;
13336 Context_Decls : out List_Id)
13337 is
13338 begin
13339 -- Traverse the parent chain looking for an enclosing body, block,
13340 -- package or return statement.
13341
13342 Context := Parent (N);
13343 while Present (Context) loop
13344 if Nkind_In (Context, N_Entry_Body,
13345 N_Extended_Return_Statement,
13346 N_Package_Body,
13347 N_Package_Declaration,
13348 N_Subprogram_Body,
13349 N_Task_Body)
13350 then
13351 exit;
13352
13353 -- Do not consider block created to protect a list of statements with
13354 -- an Abort_Defer / Abort_Undefer_Direct pair.
13355
13356 elsif Nkind (Context) = N_Block_Statement
13357 and then not Is_Abort_Block (Context)
13358 then
13359 exit;
13360 end if;
13361
13362 Context := Parent (Context);
13363 end loop;
13364
13365 pragma Assert (Present (Context));
13366
13367 -- Extract the constituents of the context
13368
13369 if Nkind (Context) = N_Extended_Return_Statement then
13370 Context_Decls := Return_Object_Declarations (Context);
13371 Context_Id := Return_Statement_Entity (Context);
13372
13373 -- Package declarations and bodies use a common library-level activation
13374 -- chain or task master, therefore return the package declaration as the
13375 -- proper carrier for the appropriate flag.
13376
13377 elsif Nkind (Context) = N_Package_Body then
13378 Context_Decls := Declarations (Context);
13379 Context_Id := Corresponding_Spec (Context);
13380 Context := Parent (Context_Id);
13381
13382 if Nkind (Context) = N_Defining_Program_Unit_Name then
13383 Context := Parent (Parent (Context));
13384 else
13385 Context := Parent (Context);
13386 end if;
13387
13388 elsif Nkind (Context) = N_Package_Declaration then
13389 Context_Decls := Visible_Declarations (Specification (Context));
13390 Context_Id := Defining_Unit_Name (Specification (Context));
13391
13392 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13393 Context_Id := Defining_Identifier (Context_Id);
13394 end if;
13395
13396 else
13397 if Nkind (Context) = N_Block_Statement then
13398 Context_Id := Entity (Identifier (Context));
13399
13400 elsif Nkind (Context) = N_Entry_Body then
13401 Context_Id := Defining_Identifier (Context);
13402
13403 elsif Nkind (Context) = N_Subprogram_Body then
13404 if Present (Corresponding_Spec (Context)) then
13405 Context_Id := Corresponding_Spec (Context);
13406 else
13407 Context_Id := Defining_Unit_Name (Specification (Context));
13408
13409 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13410 Context_Id := Defining_Identifier (Context_Id);
13411 end if;
13412 end if;
13413
13414 elsif Nkind (Context) = N_Task_Body then
13415 Context_Id := Corresponding_Spec (Context);
13416
13417 else
13418 raise Program_Error;
13419 end if;
13420
13421 Context_Decls := Declarations (Context);
13422 end if;
13423
13424 pragma Assert (Present (Context_Id));
13425 pragma Assert (Present (Context_Decls));
13426 end Find_Enclosing_Context;
13427
13428 -----------------------
13429 -- Find_Master_Scope --
13430 -----------------------
13431
13432 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13433 S : Entity_Id;
13434
13435 begin
13436 -- In Ada 2005, the master is the innermost enclosing scope that is not
13437 -- transient. If the enclosing block is the rewriting of a call or the
13438 -- scope is an extended return statement this is valid master. The
13439 -- master in an extended return is only used within the return, and is
13440 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13441 -- now before that overwriting occurs.
13442
13443 S := Scope (E);
13444
13445 if Ada_Version >= Ada_2005 then
13446 while Is_Internal (S) loop
13447 if Nkind (Parent (S)) = N_Block_Statement
13448 and then
13449 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13450 then
13451 exit;
13452
13453 elsif Ekind (S) = E_Return_Statement then
13454 exit;
13455
13456 else
13457 S := Scope (S);
13458 end if;
13459 end loop;
13460 end if;
13461
13462 return S;
13463 end Find_Master_Scope;
13464
13465 -------------------------------
13466 -- First_Protected_Operation --
13467 -------------------------------
13468
13469 function First_Protected_Operation (D : List_Id) return Node_Id is
13470 First_Op : Node_Id;
13471
13472 begin
13473 First_Op := First (D);
13474 while Present (First_Op)
13475 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13476 loop
13477 Next (First_Op);
13478 end loop;
13479
13480 return First_Op;
13481 end First_Protected_Operation;
13482
13483 ---------------------------------------
13484 -- Install_Private_Data_Declarations --
13485 ---------------------------------------
13486
13487 procedure Install_Private_Data_Declarations
13488 (Loc : Source_Ptr;
13489 Spec_Id : Entity_Id;
13490 Conc_Typ : Entity_Id;
13491 Body_Nod : Node_Id;
13492 Decls : List_Id;
13493 Barrier : Boolean := False;
13494 Family : Boolean := False)
13495 is
13496 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13497 Decl : Node_Id;
13498 Def : Node_Id;
13499 Insert_Node : Node_Id := Empty;
13500 Obj_Ent : Entity_Id;
13501
13502 procedure Add (Decl : Node_Id);
13503 -- Add a single declaration after Insert_Node. If this is the first
13504 -- addition, Decl is added to the front of Decls and it becomes the
13505 -- insertion node.
13506
13507 function Replace_Bound (Bound : Node_Id) return Node_Id;
13508 -- The bounds of an entry index may depend on discriminants, create a
13509 -- reference to the corresponding prival. Otherwise return a duplicate
13510 -- of the original bound.
13511
13512 ---------
13513 -- Add --
13514 ---------
13515
13516 procedure Add (Decl : Node_Id) is
13517 begin
13518 if No (Insert_Node) then
13519 Prepend_To (Decls, Decl);
13520 else
13521 Insert_After (Insert_Node, Decl);
13522 end if;
13523
13524 Insert_Node := Decl;
13525 end Add;
13526
13527 --------------------------
13528 -- Replace_Discriminant --
13529 --------------------------
13530
13531 function Replace_Bound (Bound : Node_Id) return Node_Id is
13532 begin
13533 if Nkind (Bound) = N_Identifier
13534 and then Is_Discriminal (Entity (Bound))
13535 then
13536 return Make_Identifier (Loc, Chars (Entity (Bound)));
13537 else
13538 return Duplicate_Subexpr (Bound);
13539 end if;
13540 end Replace_Bound;
13541
13542 -- Start of processing for Install_Private_Data_Declarations
13543
13544 begin
13545 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13546 -- formal parameter _O, _object or _task depending on the context.
13547
13548 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13549
13550 -- Special processing of _O for barrier functions, protected entries
13551 -- and families.
13552
13553 if Barrier
13554 or else
13555 (Is_Protected
13556 and then
13557 (Ekind (Spec_Id) = E_Entry
13558 or else Ekind (Spec_Id) = E_Entry_Family))
13559 then
13560 declare
13561 Conc_Rec : constant Entity_Id :=
13562 Corresponding_Record_Type (Conc_Typ);
13563 Typ_Id : constant Entity_Id :=
13564 Make_Defining_Identifier (Loc,
13565 New_External_Name (Chars (Conc_Rec), 'P'));
13566 begin
13567 -- Generate:
13568 -- type prot_typVP is access prot_typV;
13569
13570 Decl :=
13571 Make_Full_Type_Declaration (Loc,
13572 Defining_Identifier => Typ_Id,
13573 Type_Definition =>
13574 Make_Access_To_Object_Definition (Loc,
13575 Subtype_Indication =>
13576 New_Occurrence_Of (Conc_Rec, Loc)));
13577 Add (Decl);
13578
13579 -- Generate:
13580 -- _object : prot_typVP := prot_typV (_O);
13581
13582 Decl :=
13583 Make_Object_Declaration (Loc,
13584 Defining_Identifier =>
13585 Make_Defining_Identifier (Loc, Name_uObject),
13586 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13587 Expression =>
13588 Unchecked_Convert_To (Typ_Id,
13589 New_Occurrence_Of (Obj_Ent, Loc)));
13590 Add (Decl);
13591
13592 -- Set the reference to the concurrent object
13593
13594 Obj_Ent := Defining_Identifier (Decl);
13595 end;
13596 end if;
13597
13598 -- Step 2: Create the Protection object and build its declaration for
13599 -- any protected entry (family) of subprogram. Note for the lock-free
13600 -- implementation, the Protection object is not needed anymore.
13601
13602 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13603 declare
13604 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13605 Prot_Typ : RE_Id;
13606
13607 begin
13608 Set_Protection_Object (Spec_Id, Prot_Ent);
13609
13610 -- Determine the proper protection type
13611
13612 if Has_Attach_Handler (Conc_Typ)
13613 and then not Restricted_Profile
13614 then
13615 Prot_Typ := RE_Static_Interrupt_Protection;
13616
13617 elsif Has_Interrupt_Handler (Conc_Typ)
13618 and then not Restriction_Active (No_Dynamic_Attachment)
13619 then
13620 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13621
13622 else
13623 case Corresponding_Runtime_Package (Conc_Typ) is
13624 when System_Tasking_Protected_Objects_Entries =>
13625 Prot_Typ := RE_Protection_Entries;
13626
13627 when System_Tasking_Protected_Objects_Single_Entry =>
13628 Prot_Typ := RE_Protection_Entry;
13629
13630 when System_Tasking_Protected_Objects =>
13631 Prot_Typ := RE_Protection;
13632
13633 when others =>
13634 raise Program_Error;
13635 end case;
13636 end if;
13637
13638 -- Generate:
13639 -- conc_typR : protection_typ renames _object._object;
13640
13641 Decl :=
13642 Make_Object_Renaming_Declaration (Loc,
13643 Defining_Identifier => Prot_Ent,
13644 Subtype_Mark =>
13645 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13646 Name =>
13647 Make_Selected_Component (Loc,
13648 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13649 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13650 Add (Decl);
13651 end;
13652 end if;
13653
13654 -- Step 3: Add discriminant renamings (if any)
13655
13656 if Has_Discriminants (Conc_Typ) then
13657 declare
13658 D : Entity_Id;
13659
13660 begin
13661 D := First_Discriminant (Conc_Typ);
13662 while Present (D) loop
13663
13664 -- Adjust the source location
13665
13666 Set_Sloc (Discriminal (D), Loc);
13667
13668 -- Generate:
13669 -- discr_name : discr_typ renames _object.discr_name;
13670 -- or
13671 -- discr_name : discr_typ renames _task.discr_name;
13672
13673 Decl :=
13674 Make_Object_Renaming_Declaration (Loc,
13675 Defining_Identifier => Discriminal (D),
13676 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13677 Name =>
13678 Make_Selected_Component (Loc,
13679 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13680 Selector_Name => Make_Identifier (Loc, Chars (D))));
13681 Add (Decl);
13682
13683 Next_Discriminant (D);
13684 end loop;
13685 end;
13686 end if;
13687
13688 -- Step 4: Add private component renamings (if any)
13689
13690 if Is_Protected then
13691 Def := Protected_Definition (Parent (Conc_Typ));
13692
13693 if Present (Private_Declarations (Def)) then
13694 declare
13695 Comp : Node_Id;
13696 Comp_Id : Entity_Id;
13697 Decl_Id : Entity_Id;
13698
13699 begin
13700 Comp := First (Private_Declarations (Def));
13701 while Present (Comp) loop
13702 if Nkind (Comp) = N_Component_Declaration then
13703 Comp_Id := Defining_Identifier (Comp);
13704 Decl_Id :=
13705 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13706
13707 -- Minimal decoration
13708
13709 if Ekind (Spec_Id) = E_Function then
13710 Set_Ekind (Decl_Id, E_Constant);
13711 else
13712 Set_Ekind (Decl_Id, E_Variable);
13713 end if;
13714
13715 Set_Prival (Comp_Id, Decl_Id);
13716 Set_Prival_Link (Decl_Id, Comp_Id);
13717 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13718
13719 -- Generate:
13720 -- comp_name : comp_typ renames _object.comp_name;
13721
13722 Decl :=
13723 Make_Object_Renaming_Declaration (Loc,
13724 Defining_Identifier => Decl_Id,
13725 Subtype_Mark =>
13726 New_Occurrence_Of (Etype (Comp_Id), Loc),
13727 Name =>
13728 Make_Selected_Component (Loc,
13729 Prefix =>
13730 New_Occurrence_Of (Obj_Ent, Loc),
13731 Selector_Name =>
13732 Make_Identifier (Loc, Chars (Comp_Id))));
13733 Add (Decl);
13734 end if;
13735
13736 Next (Comp);
13737 end loop;
13738 end;
13739 end if;
13740 end if;
13741
13742 -- Step 5: Add the declaration of the entry index and the associated
13743 -- type for barrier functions and entry families.
13744
13745 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13746 declare
13747 E : constant Entity_Id := Index_Object (Spec_Id);
13748 Index : constant Entity_Id :=
13749 Defining_Identifier
13750 (Entry_Index_Specification
13751 (Entry_Body_Formal_Part (Body_Nod)));
13752 Index_Con : constant Entity_Id :=
13753 Make_Defining_Identifier (Loc, Chars (Index));
13754 High : Node_Id;
13755 Index_Typ : Entity_Id;
13756 Low : Node_Id;
13757
13758 begin
13759 -- Minimal decoration
13760
13761 Set_Ekind (Index_Con, E_Constant);
13762 Set_Entry_Index_Constant (Index, Index_Con);
13763 Set_Discriminal_Link (Index_Con, Index);
13764
13765 -- Retrieve the bounds of the entry family
13766
13767 High := Type_High_Bound (Etype (Index));
13768 Low := Type_Low_Bound (Etype (Index));
13769
13770 -- In the simple case the entry family is given by a subtype
13771 -- mark and the index constant has the same type.
13772
13773 if Is_Entity_Name (Original_Node (
13774 Discrete_Subtype_Definition (Parent (Index))))
13775 then
13776 Index_Typ := Etype (Index);
13777
13778 -- Otherwise a new subtype declaration is required
13779
13780 else
13781 High := Replace_Bound (High);
13782 Low := Replace_Bound (Low);
13783
13784 Index_Typ := Make_Temporary (Loc, 'J');
13785
13786 -- Generate:
13787 -- subtype Jnn is <Etype of Index> range Low .. High;
13788
13789 Decl :=
13790 Make_Subtype_Declaration (Loc,
13791 Defining_Identifier => Index_Typ,
13792 Subtype_Indication =>
13793 Make_Subtype_Indication (Loc,
13794 Subtype_Mark =>
13795 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13796 Constraint =>
13797 Make_Range_Constraint (Loc,
13798 Range_Expression =>
13799 Make_Range (Loc, Low, High))));
13800 Add (Decl);
13801 end if;
13802
13803 Set_Etype (Index_Con, Index_Typ);
13804
13805 -- Create the object which designates the index:
13806 -- J : constant Jnn :=
13807 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13808 --
13809 -- where Jnn is the subtype created above or the original type of
13810 -- the index, _E is a formal of the protected body subprogram and
13811 -- <index expr> is the index of the first family member.
13812
13813 Decl :=
13814 Make_Object_Declaration (Loc,
13815 Defining_Identifier => Index_Con,
13816 Constant_Present => True,
13817 Object_Definition =>
13818 New_Occurrence_Of (Index_Typ, Loc),
13819
13820 Expression =>
13821 Make_Attribute_Reference (Loc,
13822 Prefix =>
13823 New_Occurrence_Of (Index_Typ, Loc),
13824 Attribute_Name => Name_Val,
13825
13826 Expressions => New_List (
13827
13828 Make_Op_Add (Loc,
13829 Left_Opnd =>
13830 Make_Op_Subtract (Loc,
13831 Left_Opnd => New_Occurrence_Of (E, Loc),
13832 Right_Opnd =>
13833 Entry_Index_Expression (Loc,
13834 Defining_Identifier (Body_Nod),
13835 Empty, Conc_Typ)),
13836
13837 Right_Opnd =>
13838 Make_Attribute_Reference (Loc,
13839 Prefix =>
13840 New_Occurrence_Of (Index_Typ, Loc),
13841 Attribute_Name => Name_Pos,
13842 Expressions => New_List (
13843 Make_Attribute_Reference (Loc,
13844 Prefix =>
13845 New_Occurrence_Of (Index_Typ, Loc),
13846 Attribute_Name => Name_First)))))));
13847 Add (Decl);
13848 end;
13849 end if;
13850 end Install_Private_Data_Declarations;
13851
13852 -----------------------
13853 -- Is_Exception_Safe --
13854 -----------------------
13855
13856 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13857
13858 function Has_Side_Effect (N : Node_Id) return Boolean;
13859 -- Return True whenever encountering a subprogram call or raise
13860 -- statement of any kind in the sequence of statements
13861
13862 ---------------------
13863 -- Has_Side_Effect --
13864 ---------------------
13865
13866 -- What is this doing buried two levels down in exp_ch9. It seems like a
13867 -- generally useful function, and indeed there may be code duplication
13868 -- going on here ???
13869
13870 function Has_Side_Effect (N : Node_Id) return Boolean is
13871 Stmt : Node_Id;
13872 Expr : Node_Id;
13873
13874 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13875 -- Indicate whether N is a subprogram call or a raise statement
13876
13877 ----------------------
13878 -- Is_Call_Or_Raise --
13879 ----------------------
13880
13881 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13882 begin
13883 return Nkind_In (N, N_Procedure_Call_Statement,
13884 N_Function_Call,
13885 N_Raise_Statement,
13886 N_Raise_Constraint_Error,
13887 N_Raise_Program_Error,
13888 N_Raise_Storage_Error);
13889 end Is_Call_Or_Raise;
13890
13891 -- Start of processing for Has_Side_Effect
13892
13893 begin
13894 Stmt := N;
13895 while Present (Stmt) loop
13896 if Is_Call_Or_Raise (Stmt) then
13897 return True;
13898 end if;
13899
13900 -- An object declaration can also contain a function call or a
13901 -- raise statement.
13902
13903 if Nkind (Stmt) = N_Object_Declaration then
13904 Expr := Expression (Stmt);
13905
13906 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13907 return True;
13908 end if;
13909 end if;
13910
13911 Next (Stmt);
13912 end loop;
13913
13914 return False;
13915 end Has_Side_Effect;
13916
13917 -- Start of processing for Is_Exception_Safe
13918
13919 begin
13920 -- When exceptions can't be propagated, the subprogram returns normally
13921
13922 if No_Exception_Handlers_Set then
13923 return True;
13924 end if;
13925
13926 -- If the checks handled by the back end are not disabled, we cannot
13927 -- ensure that no exception will be raised.
13928
13929 if not Access_Checks_Suppressed (Empty)
13930 or else not Discriminant_Checks_Suppressed (Empty)
13931 or else not Range_Checks_Suppressed (Empty)
13932 or else not Index_Checks_Suppressed (Empty)
13933 or else Opt.Stack_Checking_Enabled
13934 then
13935 return False;
13936 end if;
13937
13938 if Has_Side_Effect (First (Declarations (Subprogram)))
13939 or else
13940 Has_Side_Effect
13941 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13942 then
13943 return False;
13944 else
13945 return True;
13946 end if;
13947 end Is_Exception_Safe;
13948
13949 ---------------------------------
13950 -- Is_Potentially_Large_Family --
13951 ---------------------------------
13952
13953 function Is_Potentially_Large_Family
13954 (Base_Index : Entity_Id;
13955 Conctyp : Entity_Id;
13956 Lo : Node_Id;
13957 Hi : Node_Id) return Boolean
13958 is
13959 begin
13960 return Scope (Base_Index) = Standard_Standard
13961 and then Base_Index = Base_Type (Standard_Integer)
13962 and then Has_Discriminants (Conctyp)
13963 and then
13964 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13965 and then
13966 (Denotes_Discriminant (Lo, True)
13967 or else
13968 Denotes_Discriminant (Hi, True));
13969 end Is_Potentially_Large_Family;
13970
13971 -------------------------------------
13972 -- Is_Private_Primitive_Subprogram --
13973 -------------------------------------
13974
13975 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13976 begin
13977 return
13978 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13979 and then Is_Private_Primitive (Id);
13980 end Is_Private_Primitive_Subprogram;
13981
13982 ------------------
13983 -- Index_Object --
13984 ------------------
13985
13986 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13987 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13988 Formal : Entity_Id;
13989
13990 begin
13991 Formal := First_Formal (Bod_Subp);
13992 while Present (Formal) loop
13993
13994 -- Look for formal parameter _E
13995
13996 if Chars (Formal) = Name_uE then
13997 return Formal;
13998 end if;
13999
14000 Next_Formal (Formal);
14001 end loop;
14002
14003 -- A protected body subprogram should always have the parameter in
14004 -- question.
14005
14006 raise Program_Error;
14007 end Index_Object;
14008
14009 --------------------------------
14010 -- Make_Initialize_Protection --
14011 --------------------------------
14012
14013 function Make_Initialize_Protection
14014 (Protect_Rec : Entity_Id) return List_Id
14015 is
14016 Loc : constant Source_Ptr := Sloc (Protect_Rec);
14017 P_Arr : Entity_Id;
14018 Pdec : Node_Id;
14019 Ptyp : constant Node_Id :=
14020 Corresponding_Concurrent_Type (Protect_Rec);
14021 Args : List_Id;
14022 L : constant List_Id := New_List;
14023 Has_Entry : constant Boolean := Has_Entries (Ptyp);
14024 Prio_Type : Entity_Id;
14025 Prio_Var : Entity_Id := Empty;
14026 Restricted : constant Boolean := Restricted_Profile;
14027
14028 begin
14029 -- We may need two calls to properly initialize the object, one to
14030 -- Initialize_Protection, and possibly one to Install_Handlers if we
14031 -- have a pragma Attach_Handler.
14032
14033 -- Get protected declaration. In the case of a task type declaration,
14034 -- this is simply the parent of the protected type entity. In the single
14035 -- protected object declaration, this parent will be the implicit type,
14036 -- and we can find the corresponding single protected object declaration
14037 -- by searching forward in the declaration list in the tree.
14038
14039 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
14040 -- of this type should have been removed during semantic analysis.
14041
14042 Pdec := Parent (Ptyp);
14043 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
14044 N_Single_Protected_Declaration)
14045 loop
14046 Next (Pdec);
14047 end loop;
14048
14049 -- Build the parameter list for the call. Note that _Init is the name
14050 -- of the formal for the object to be initialized, which is the task
14051 -- value record itself.
14052
14053 Args := New_List;
14054
14055 -- For lock-free implementation, skip initializations of the Protection
14056 -- object.
14057
14058 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14059
14060 -- Object parameter. This is a pointer to the object of type
14061 -- Protection used by the GNARL to control the protected object.
14062
14063 Append_To (Args,
14064 Make_Attribute_Reference (Loc,
14065 Prefix =>
14066 Make_Selected_Component (Loc,
14067 Prefix => Make_Identifier (Loc, Name_uInit),
14068 Selector_Name => Make_Identifier (Loc, Name_uObject)),
14069 Attribute_Name => Name_Unchecked_Access));
14070
14071 -- Priority parameter. Set to Unspecified_Priority unless there is a
14072 -- Priority rep item, in which case we take the value from the pragma
14073 -- or attribute definition clause, or there is an Interrupt_Priority
14074 -- rep item and no Priority rep item, and we set the ceiling to
14075 -- Interrupt_Priority'Last, an implementation-defined value, see
14076 -- (RM D.3(10)).
14077
14078 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
14079 declare
14080 Prio_Clause : constant Node_Id :=
14081 Get_Rep_Item
14082 (Ptyp, Name_Priority, Check_Parents => False);
14083
14084 Prio : Node_Id;
14085
14086 begin
14087 -- Pragma Priority
14088
14089 if Nkind (Prio_Clause) = N_Pragma then
14090 Prio :=
14091 Expression
14092 (First (Pragma_Argument_Associations (Prio_Clause)));
14093
14094 -- Get_Rep_Item returns either priority pragma.
14095
14096 if Pragma_Name (Prio_Clause) = Name_Priority then
14097 Prio_Type := RTE (RE_Any_Priority);
14098 else
14099 Prio_Type := RTE (RE_Interrupt_Priority);
14100 end if;
14101
14102 -- Attribute definition clause Priority
14103
14104 else
14105 if Chars (Prio_Clause) = Name_Priority then
14106 Prio_Type := RTE (RE_Any_Priority);
14107 else
14108 Prio_Type := RTE (RE_Interrupt_Priority);
14109 end if;
14110
14111 Prio := Expression (Prio_Clause);
14112 end if;
14113
14114 -- Always create a locale variable to capture the priority.
14115 -- The priority is also passed to Install_Restriced_Handlers.
14116 -- Note that it is really necessary to create this variable
14117 -- explicitly. It might be thought that removing side effects
14118 -- would the appropriate approach, but that could generate
14119 -- declarations improperly placed in the enclosing scope.
14120
14121 Prio_Var := Make_Temporary (Loc, 'R', Prio);
14122 Append_To (L,
14123 Make_Object_Declaration (Loc,
14124 Defining_Identifier => Prio_Var,
14125 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
14126 Expression => Relocate_Node (Prio)));
14127
14128 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14129 end;
14130
14131 -- When no priority is specified but an xx_Handler pragma is, we
14132 -- default to System.Interrupts.Default_Interrupt_Priority, see
14133 -- D.3(10).
14134
14135 elsif Has_Attach_Handler (Ptyp)
14136 or else Has_Interrupt_Handler (Ptyp)
14137 then
14138 Append_To (Args,
14139 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
14140
14141 -- Normal case, no priority or xx_Handler specified, default priority
14142
14143 else
14144 Append_To (Args,
14145 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14146 end if;
14147
14148 -- Test for Compiler_Info parameter. This parameter allows entry body
14149 -- procedures and barrier functions to be called from the runtime. It
14150 -- is a pointer to the record generated by the compiler to represent
14151 -- the protected object.
14152
14153 -- A protected type without entries that covers an interface and
14154 -- overrides the abstract routines with protected procedures is
14155 -- considered equivalent to a protected type with entries in the
14156 -- context of dispatching select statements.
14157
14158 -- Protected types with interrupt handlers (when not using a
14159 -- restricted profile) are also considered equivalent to protected
14160 -- types with entries.
14161
14162 -- The types which are used (Static_Interrupt_Protection and
14163 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
14164
14165 declare
14166 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
14167
14168 Called_Subp : RE_Id;
14169
14170 begin
14171 case Pkg_Id is
14172 when System_Tasking_Protected_Objects_Entries =>
14173 Called_Subp := RE_Initialize_Protection_Entries;
14174
14175 -- Argument Compiler_Info
14176
14177 Append_To (Args,
14178 Make_Attribute_Reference (Loc,
14179 Prefix => Make_Identifier (Loc, Name_uInit),
14180 Attribute_Name => Name_Address));
14181
14182 when System_Tasking_Protected_Objects_Single_Entry =>
14183 Called_Subp := RE_Initialize_Protection_Entry;
14184
14185 -- Argument Compiler_Info
14186
14187 Append_To (Args,
14188 Make_Attribute_Reference (Loc,
14189 Prefix => Make_Identifier (Loc, Name_uInit),
14190 Attribute_Name => Name_Address));
14191
14192 when System_Tasking_Protected_Objects =>
14193 Called_Subp := RE_Initialize_Protection;
14194
14195 when others =>
14196 raise Program_Error;
14197 end case;
14198
14199 -- Entry_Queue_Maxes parameter. This is an access to an array of
14200 -- naturals representing the entry queue maximums for each entry
14201 -- in the protected type. Zero represents no max. The access is
14202 -- null if there is no limit for all entries (usual case).
14203
14204 if Has_Entry
14205 and then Pkg_Id /= System_Tasking_Protected_Objects_Single_Entry
14206 then
14207 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
14208 Append_To (Args,
14209 Make_Attribute_Reference (Loc,
14210 Prefix =>
14211 New_Occurrence_Of
14212 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
14213 Attribute_Name => Name_Unrestricted_Access));
14214 else
14215 Append_To (Args, Make_Null (Loc));
14216 end if;
14217
14218 -- Edge cases exist where entry initialization functions are
14219 -- called, but no entries exist, so null is appended.
14220
14221 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14222 Append_To (Args, Make_Null (Loc));
14223 end if;
14224
14225 -- Entry_Bodies parameter. This is a pointer to an array of
14226 -- pointers to the entry body procedures and barrier functions of
14227 -- the object. If the protected type has no entries this object
14228 -- will not exist, in this case, pass a null (it can happen when
14229 -- there are protected interrupt handlers or interfaces).
14230
14231 if Has_Entry then
14232 P_Arr := Entry_Bodies_Array (Ptyp);
14233
14234 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14235 -- multiple entries).
14236
14237 Append_To (Args,
14238 Make_Attribute_Reference (Loc,
14239 Prefix => New_Occurrence_Of (P_Arr, Loc),
14240 Attribute_Name => Name_Unrestricted_Access));
14241
14242 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14243
14244 -- Find index mapping function (clumsy but ok for now)
14245
14246 while Ekind (P_Arr) /= E_Function loop
14247 Next_Entity (P_Arr);
14248 end loop;
14249
14250 Append_To (Args,
14251 Make_Attribute_Reference (Loc,
14252 Prefix => New_Occurrence_Of (P_Arr, Loc),
14253 Attribute_Name => Name_Unrestricted_Access));
14254 end if;
14255
14256 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14257
14258 -- This is the case where we have a protected object with
14259 -- interfaces and no entries, and the single entry restriction
14260 -- is in effect. We pass a null pointer for the entry
14261 -- parameter because there is no actual entry.
14262
14263 Append_To (Args, Make_Null (Loc));
14264
14265 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14266
14267 -- This is the case where we have a protected object with no
14268 -- entries and:
14269 -- - either interrupt handlers with non restricted profile,
14270 -- - or interfaces
14271 -- Note that the types which are used for interrupt handlers
14272 -- (Static/Dynamic_Interrupt_Protection) are derived from
14273 -- Protection_Entries. We pass two null pointers because there
14274 -- is no actual entry, and the initialization procedure needs
14275 -- both Entry_Bodies and Find_Body_Index.
14276
14277 Append_To (Args, Make_Null (Loc));
14278 Append_To (Args, Make_Null (Loc));
14279 end if;
14280
14281 Append_To (L,
14282 Make_Procedure_Call_Statement (Loc,
14283 Name =>
14284 New_Occurrence_Of (RTE (Called_Subp), Loc),
14285 Parameter_Associations => Args));
14286 end;
14287 end if;
14288
14289 if Has_Attach_Handler (Ptyp) then
14290
14291 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14292 -- make the following call:
14293
14294 -- Install_Handlers (_object,
14295 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14296
14297 -- or, in the case of Ravenscar:
14298
14299 -- Install_Restricted_Handlers
14300 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14301
14302 declare
14303 Args : constant List_Id := New_List;
14304 Table : constant List_Id := New_List;
14305 Ritem : Node_Id := First_Rep_Item (Ptyp);
14306
14307 begin
14308 -- Build the Priority parameter (only for ravenscar)
14309
14310 if Restricted then
14311
14312 -- Priority comes from a pragma
14313
14314 if Present (Prio_Var) then
14315 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14316
14317 -- Priority is the default one
14318
14319 else
14320 Append_To (Args,
14321 New_Occurrence_Of
14322 (RTE (RE_Default_Interrupt_Priority), Loc));
14323 end if;
14324 end if;
14325
14326 -- Build the Attach_Handler table argument
14327
14328 while Present (Ritem) loop
14329 if Nkind (Ritem) = N_Pragma
14330 and then Pragma_Name (Ritem) = Name_Attach_Handler
14331 then
14332 declare
14333 Handler : constant Node_Id :=
14334 First (Pragma_Argument_Associations (Ritem));
14335
14336 Interrupt : constant Node_Id := Next (Handler);
14337 Expr : constant Node_Id := Expression (Interrupt);
14338
14339 begin
14340 Append_To (Table,
14341 Make_Aggregate (Loc, Expressions => New_List (
14342 Unchecked_Convert_To
14343 (RTE (RE_System_Interrupt_Id), Expr),
14344 Make_Attribute_Reference (Loc,
14345 Prefix =>
14346 Make_Selected_Component (Loc,
14347 Prefix =>
14348 Make_Identifier (Loc, Name_uInit),
14349 Selector_Name =>
14350 Duplicate_Subexpr_No_Checks
14351 (Expression (Handler))),
14352 Attribute_Name => Name_Access))));
14353 end;
14354 end if;
14355
14356 Next_Rep_Item (Ritem);
14357 end loop;
14358
14359 -- Append the table argument we just built
14360
14361 Append_To (Args, Make_Aggregate (Loc, Table));
14362
14363 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14364 -- call to the statements.
14365
14366 if Restricted then
14367 -- Call a simplified version of Install_Handlers to be used
14368 -- when the Ravenscar restrictions are in effect
14369 -- (Install_Restricted_Handlers).
14370
14371 Append_To (L,
14372 Make_Procedure_Call_Statement (Loc,
14373 Name =>
14374 New_Occurrence_Of
14375 (RTE (RE_Install_Restricted_Handlers), Loc),
14376 Parameter_Associations => Args));
14377
14378 else
14379 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14380
14381 -- First, prepends the _object argument
14382
14383 Prepend_To (Args,
14384 Make_Attribute_Reference (Loc,
14385 Prefix =>
14386 Make_Selected_Component (Loc,
14387 Prefix => Make_Identifier (Loc, Name_uInit),
14388 Selector_Name =>
14389 Make_Identifier (Loc, Name_uObject)),
14390 Attribute_Name => Name_Unchecked_Access));
14391 end if;
14392
14393 -- Then, insert call to Install_Handlers
14394
14395 Append_To (L,
14396 Make_Procedure_Call_Statement (Loc,
14397 Name =>
14398 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14399 Parameter_Associations => Args));
14400 end if;
14401 end;
14402 end if;
14403
14404 return L;
14405 end Make_Initialize_Protection;
14406
14407 ---------------------------
14408 -- Make_Task_Create_Call --
14409 ---------------------------
14410
14411 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14412 Loc : constant Source_Ptr := Sloc (Task_Rec);
14413 Args : List_Id;
14414 Ecount : Node_Id;
14415 Name : Node_Id;
14416 Tdec : Node_Id;
14417 Tdef : Node_Id;
14418 Tnam : Name_Id;
14419 Ttyp : Node_Id;
14420
14421 begin
14422 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14423 Tnam := Chars (Ttyp);
14424
14425 -- Get task declaration. In the case of a task type declaration, this is
14426 -- simply the parent of the task type entity. In the single task
14427 -- declaration, this parent will be the implicit type, and we can find
14428 -- the corresponding single task declaration by searching forward in the
14429 -- declaration list in the tree.
14430
14431 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14432 -- this type should have been removed during semantic analysis.
14433
14434 Tdec := Parent (Ttyp);
14435 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14436 N_Single_Task_Declaration)
14437 loop
14438 Next (Tdec);
14439 end loop;
14440
14441 -- Now we can find the task definition from this declaration
14442
14443 Tdef := Task_Definition (Tdec);
14444
14445 -- Build the parameter list for the call. Note that _Init is the name
14446 -- of the formal for the object to be initialized, which is the task
14447 -- value record itself.
14448
14449 Args := New_List;
14450
14451 -- Priority parameter. Set to Unspecified_Priority unless there is a
14452 -- Priority rep item, in which case we take the value from the rep item.
14453
14454 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14455 Append_To (Args,
14456 Make_Selected_Component (Loc,
14457 Prefix => Make_Identifier (Loc, Name_uInit),
14458 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14459 else
14460 Append_To (Args,
14461 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14462 end if;
14463
14464 -- Optional Stack parameter
14465
14466 if Restricted_Profile then
14467
14468 -- If the stack has been preallocated by the expander then
14469 -- pass its address. Otherwise, pass a null address.
14470
14471 if Preallocated_Stacks_On_Target then
14472 Append_To (Args,
14473 Make_Attribute_Reference (Loc,
14474 Prefix =>
14475 Make_Selected_Component (Loc,
14476 Prefix => Make_Identifier (Loc, Name_uInit),
14477 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14478 Attribute_Name => Name_Address));
14479
14480 else
14481 Append_To (Args,
14482 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14483 end if;
14484 end if;
14485
14486 -- Size parameter. If no Storage_Size pragma is present, then
14487 -- the size is taken from the taskZ variable for the type, which
14488 -- is either Unspecified_Size, or has been reset by the use of
14489 -- a Storage_Size attribute definition clause. If a pragma is
14490 -- present, then the size is taken from the _Size field of the
14491 -- task value record, which was set from the pragma value.
14492
14493 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14494 Append_To (Args,
14495 Make_Selected_Component (Loc,
14496 Prefix => Make_Identifier (Loc, Name_uInit),
14497 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14498
14499 else
14500 Append_To (Args,
14501 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14502 end if;
14503
14504 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14505 -- Task_Info pragma, in which case we take the value from the pragma.
14506
14507 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14508 Append_To (Args,
14509 Make_Selected_Component (Loc,
14510 Prefix => Make_Identifier (Loc, Name_uInit),
14511 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14512
14513 else
14514 Append_To (Args,
14515 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14516 end if;
14517
14518 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14519 -- in which case we take the value from the rep item. The parameter is
14520 -- passed as an Integer because in the case of unspecified CPU the
14521 -- value is not in the range of CPU_Range.
14522
14523 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14524 Append_To (Args,
14525 Convert_To (Standard_Integer,
14526 Make_Selected_Component (Loc,
14527 Prefix => Make_Identifier (Loc, Name_uInit),
14528 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14529 else
14530 Append_To (Args,
14531 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14532 end if;
14533
14534 if not Restricted_Profile then
14535
14536 -- Deadline parameter. If no Relative_Deadline pragma is present,
14537 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14538 -- the deadline is taken from the _Relative_Deadline field of the
14539 -- task value record, which was set from the pragma value. Note that
14540 -- this parameter must not be generated for the restricted profiles
14541 -- since Ravenscar does not allow deadlines.
14542
14543 -- Case where pragma Relative_Deadline applies: use given value
14544
14545 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14546 Append_To (Args,
14547 Make_Selected_Component (Loc,
14548 Prefix => Make_Identifier (Loc, Name_uInit),
14549 Selector_Name =>
14550 Make_Identifier (Loc, Name_uRelative_Deadline)));
14551
14552 -- No pragma Relative_Deadline apply to the task
14553
14554 else
14555 Append_To (Args,
14556 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14557 end if;
14558
14559 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14560 -- present, then the dispatching domain is null. If a rep item is
14561 -- present, then the dispatching domain is taken from the
14562 -- _Dispatching_Domain field of the task value record, which was set
14563 -- from the rep item value.
14564
14565 -- Case where Dispatching_Domain rep item applies: use given value
14566
14567 if Has_Rep_Item
14568 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14569 then
14570 Append_To (Args,
14571 Make_Selected_Component (Loc,
14572 Prefix =>
14573 Make_Identifier (Loc, Name_uInit),
14574 Selector_Name =>
14575 Make_Identifier (Loc, Name_uDispatching_Domain)));
14576
14577 -- No pragma or aspect Dispatching_Domain applies to the task
14578
14579 else
14580 Append_To (Args, Make_Null (Loc));
14581 end if;
14582
14583 -- Number of entries. This is an expression of the form:
14584
14585 -- n + _Init.a'Length + _Init.a'B'Length + ...
14586
14587 -- where a,b... are the entry family names for the task definition
14588
14589 Ecount :=
14590 Build_Entry_Count_Expression
14591 (Ttyp,
14592 Component_Items
14593 (Component_List
14594 (Type_Definition
14595 (Parent (Corresponding_Record_Type (Ttyp))))),
14596 Loc);
14597 Append_To (Args, Ecount);
14598
14599 -- Master parameter. This is a reference to the _Master parameter of
14600 -- the initialization procedure, except in the case of the pragma
14601 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14602 -- System.Tasking.Library_Task_Level.
14603
14604 if Restriction_Active (No_Task_Hierarchy) = False then
14605 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14606 else
14607 Append_To (Args,
14608 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14609 end if;
14610 end if;
14611
14612 -- State parameter. This is a pointer to the task body procedure. The
14613 -- required value is obtained by taking 'Unrestricted_Access of the task
14614 -- body procedure and converting it (with an unchecked conversion) to
14615 -- the type required by the task kernel. For further details, see the
14616 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14617 -- than 'Address in order to avoid creating trampolines.
14618
14619 declare
14620 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14621 Subp_Ptr_Typ : constant Node_Id :=
14622 Create_Itype (E_Access_Subprogram_Type, Tdec);
14623 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14624
14625 begin
14626 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14627 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14628
14629 -- Be sure to freeze a reference to the access-to-subprogram type,
14630 -- otherwise gigi will complain that it's in the wrong scope, because
14631 -- it's actually inside the init procedure for the record type that
14632 -- corresponds to the task type.
14633
14634 Set_Itype (Ref, Subp_Ptr_Typ);
14635 Append_Freeze_Action (Task_Rec, Ref);
14636
14637 Append_To (Args,
14638 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14639 Make_Qualified_Expression (Loc,
14640 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14641 Expression =>
14642 Make_Attribute_Reference (Loc,
14643 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14644 Attribute_Name => Name_Unrestricted_Access))));
14645 end;
14646
14647 -- Discriminants parameter. This is just the address of the task
14648 -- value record itself (which contains the discriminant values
14649
14650 Append_To (Args,
14651 Make_Attribute_Reference (Loc,
14652 Prefix => Make_Identifier (Loc, Name_uInit),
14653 Attribute_Name => Name_Address));
14654
14655 -- Elaborated parameter. This is an access to the elaboration Boolean
14656
14657 Append_To (Args,
14658 Make_Attribute_Reference (Loc,
14659 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14660 Attribute_Name => Name_Unchecked_Access));
14661
14662 -- Add Chain parameter (not done for sequential elaboration policy, see
14663 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14664
14665 if Partition_Elaboration_Policy /= 'S' then
14666 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14667 end if;
14668
14669 -- Task name parameter. Take this from the _Task_Id parameter to the
14670 -- init call unless there is a Task_Name pragma, in which case we take
14671 -- the value from the pragma.
14672
14673 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14674 -- Copy expression in full, because it may be dynamic and have
14675 -- side effects.
14676
14677 Append_To (Args,
14678 New_Copy_Tree
14679 (Expression
14680 (First
14681 (Pragma_Argument_Associations
14682 (Get_Rep_Pragma
14683 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14684
14685 else
14686 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14687 end if;
14688
14689 -- Created_Task parameter. This is the _Task_Id field of the task
14690 -- record value
14691
14692 Append_To (Args,
14693 Make_Selected_Component (Loc,
14694 Prefix => Make_Identifier (Loc, Name_uInit),
14695 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14696
14697 declare
14698 Create_RE : RE_Id;
14699
14700 begin
14701 if Restricted_Profile then
14702 if Partition_Elaboration_Policy = 'S' then
14703 Create_RE := RE_Create_Restricted_Task_Sequential;
14704 else
14705 Create_RE := RE_Create_Restricted_Task;
14706 end if;
14707 else
14708 Create_RE := RE_Create_Task;
14709 end if;
14710
14711 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14712 end;
14713
14714 return
14715 Make_Procedure_Call_Statement (Loc,
14716 Name => Name,
14717 Parameter_Associations => Args);
14718 end Make_Task_Create_Call;
14719
14720 ------------------------------
14721 -- Next_Protected_Operation --
14722 ------------------------------
14723
14724 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14725 Next_Op : Node_Id;
14726
14727 begin
14728 -- Check whether there is a subsequent body for a protected operation
14729 -- in the current protected body. In Ada2012 that includes expression
14730 -- functions that are completions.
14731
14732 Next_Op := Next (N);
14733 while Present (Next_Op)
14734 and then not Nkind_In (Next_Op,
14735 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14736 loop
14737 Next (Next_Op);
14738 end loop;
14739
14740 return Next_Op;
14741 end Next_Protected_Operation;
14742
14743 ---------------------
14744 -- Null_Statements --
14745 ---------------------
14746
14747 function Null_Statements (Stats : List_Id) return Boolean is
14748 Stmt : Node_Id;
14749
14750 begin
14751 Stmt := First (Stats);
14752 while Nkind (Stmt) /= N_Empty
14753 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14754 or else
14755 (Nkind (Stmt) = N_Pragma
14756 and then
14757 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14758 Name_Unmodified,
14759 Name_Warnings)))
14760 loop
14761 Next (Stmt);
14762 end loop;
14763
14764 return Nkind (Stmt) = N_Empty;
14765 end Null_Statements;
14766
14767 --------------------------
14768 -- Parameter_Block_Pack --
14769 --------------------------
14770
14771 function Parameter_Block_Pack
14772 (Loc : Source_Ptr;
14773 Blk_Typ : Entity_Id;
14774 Actuals : List_Id;
14775 Formals : List_Id;
14776 Decls : List_Id;
14777 Stmts : List_Id) return Node_Id
14778 is
14779 Actual : Entity_Id;
14780 Expr : Node_Id := Empty;
14781 Formal : Entity_Id;
14782 Has_Param : Boolean := False;
14783 P : Entity_Id;
14784 Params : List_Id;
14785 Temp_Asn : Node_Id;
14786 Temp_Nam : Node_Id;
14787
14788 begin
14789 Actual := First (Actuals);
14790 Formal := Defining_Identifier (First (Formals));
14791 Params := New_List;
14792 while Present (Actual) loop
14793 if Is_By_Copy_Type (Etype (Actual)) then
14794 -- Generate:
14795 -- Jnn : aliased <formal-type>
14796
14797 Temp_Nam := Make_Temporary (Loc, 'J');
14798
14799 Append_To (Decls,
14800 Make_Object_Declaration (Loc,
14801 Aliased_Present => True,
14802 Defining_Identifier => Temp_Nam,
14803 Object_Definition =>
14804 New_Occurrence_Of (Etype (Formal), Loc)));
14805
14806 if Ekind (Formal) /= E_Out_Parameter then
14807
14808 -- Generate:
14809 -- Jnn := <actual>
14810
14811 Temp_Asn :=
14812 New_Occurrence_Of (Temp_Nam, Loc);
14813
14814 Set_Assignment_OK (Temp_Asn);
14815
14816 Append_To (Stmts,
14817 Make_Assignment_Statement (Loc,
14818 Name => Temp_Asn,
14819 Expression => New_Copy_Tree (Actual)));
14820 end if;
14821
14822 -- Generate:
14823 -- Jnn'unchecked_access
14824
14825 Append_To (Params,
14826 Make_Attribute_Reference (Loc,
14827 Attribute_Name => Name_Unchecked_Access,
14828 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14829
14830 Has_Param := True;
14831
14832 -- The controlling parameter is omitted
14833
14834 else
14835 if not Is_Controlling_Actual (Actual) then
14836 Append_To (Params,
14837 Make_Reference (Loc, New_Copy_Tree (Actual)));
14838
14839 Has_Param := True;
14840 end if;
14841 end if;
14842
14843 Next_Actual (Actual);
14844 Next_Formal_With_Extras (Formal);
14845 end loop;
14846
14847 if Has_Param then
14848 Expr := Make_Aggregate (Loc, Params);
14849 end if;
14850
14851 -- Generate:
14852 -- P : Ann := (
14853 -- J1'unchecked_access;
14854 -- <actual2>'reference;
14855 -- ...);
14856
14857 P := Make_Temporary (Loc, 'P');
14858
14859 Append_To (Decls,
14860 Make_Object_Declaration (Loc,
14861 Defining_Identifier => P,
14862 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14863 Expression => Expr));
14864
14865 return P;
14866 end Parameter_Block_Pack;
14867
14868 ----------------------------
14869 -- Parameter_Block_Unpack --
14870 ----------------------------
14871
14872 function Parameter_Block_Unpack
14873 (Loc : Source_Ptr;
14874 P : Entity_Id;
14875 Actuals : List_Id;
14876 Formals : List_Id) return List_Id
14877 is
14878 Actual : Entity_Id;
14879 Asnmt : Node_Id;
14880 Formal : Entity_Id;
14881 Has_Asnmt : Boolean := False;
14882 Result : constant List_Id := New_List;
14883
14884 begin
14885 Actual := First (Actuals);
14886 Formal := Defining_Identifier (First (Formals));
14887 while Present (Actual) loop
14888 if Is_By_Copy_Type (Etype (Actual))
14889 and then Ekind (Formal) /= E_In_Parameter
14890 then
14891 -- Generate:
14892 -- <actual> := P.<formal>;
14893
14894 Asnmt :=
14895 Make_Assignment_Statement (Loc,
14896 Name =>
14897 New_Copy (Actual),
14898 Expression =>
14899 Make_Explicit_Dereference (Loc,
14900 Make_Selected_Component (Loc,
14901 Prefix =>
14902 New_Occurrence_Of (P, Loc),
14903 Selector_Name =>
14904 Make_Identifier (Loc, Chars (Formal)))));
14905
14906 Set_Assignment_OK (Name (Asnmt));
14907 Append_To (Result, Asnmt);
14908
14909 Has_Asnmt := True;
14910 end if;
14911
14912 Next_Actual (Actual);
14913 Next_Formal_With_Extras (Formal);
14914 end loop;
14915
14916 if Has_Asnmt then
14917 return Result;
14918 else
14919 return New_List (Make_Null_Statement (Loc));
14920 end if;
14921 end Parameter_Block_Unpack;
14922
14923 ----------------------
14924 -- Set_Discriminals --
14925 ----------------------
14926
14927 procedure Set_Discriminals (Dec : Node_Id) is
14928 D : Entity_Id;
14929 Pdef : Entity_Id;
14930 D_Minal : Entity_Id;
14931
14932 begin
14933 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14934 Pdef := Defining_Identifier (Dec);
14935
14936 if Has_Discriminants (Pdef) then
14937 D := First_Discriminant (Pdef);
14938 while Present (D) loop
14939 D_Minal :=
14940 Make_Defining_Identifier (Sloc (D),
14941 Chars => New_External_Name (Chars (D), 'D'));
14942
14943 Set_Ekind (D_Minal, E_Constant);
14944 Set_Etype (D_Minal, Etype (D));
14945 Set_Scope (D_Minal, Pdef);
14946 Set_Discriminal (D, D_Minal);
14947 Set_Discriminal_Link (D_Minal, D);
14948
14949 Next_Discriminant (D);
14950 end loop;
14951 end if;
14952 end Set_Discriminals;
14953
14954 -----------------------
14955 -- Trivial_Accept_OK --
14956 -----------------------
14957
14958 function Trivial_Accept_OK return Boolean is
14959 begin
14960 case Opt.Task_Dispatching_Policy is
14961
14962 -- If we have the default task dispatching policy in effect, we can
14963 -- definitely do the optimization (one way of looking at this is to
14964 -- think of the formal definition of the default policy being allowed
14965 -- to run any task it likes after a rendezvous, so even if notionally
14966 -- a full rescheduling occurs, we can say that our dispatching policy
14967 -- (i.e. the default dispatching policy) reorders the queue to be the
14968 -- same as just before the call.
14969
14970 when ' ' =>
14971 return True;
14972
14973 -- FIFO_Within_Priorities certainly does not permit this
14974 -- optimization since the Rendezvous is a scheduling action that may
14975 -- require some other task to be run.
14976
14977 when 'F' =>
14978 return False;
14979
14980 -- For now, disallow the optimization for all other policies. This
14981 -- may be over-conservative, but it is certainly not incorrect.
14982
14983 when others =>
14984 return False;
14985
14986 end case;
14987 end Trivial_Accept_OK;
14988
14989 end Exp_Ch9;