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