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