[multiple changes]
[gcc.git] / gcc / ada / exp_ch9.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 function Is_Count_Attribute (N : Node_Id) return Boolean;
6029 -- Check whether N is part of an expansion of the Count attribute.
6030 -- Return True if N represents the expanded function call.
6031
6032 ------------------------
6033 -- Is_Count_Attribute --
6034 ------------------------
6035
6036 function Is_Count_Attribute (N : Node_Id) return Boolean is
6037 begin
6038 return
6039 Nkind (N) = N_Function_Call
6040 and then Present (Original_Node (N))
6041 and then Nkind (Original_Node (N)) = N_Attribute_Reference
6042 and then Attribute_Name (Original_Node (N)) = Name_Count;
6043 end Is_Count_Attribute;
6044
6045 -- Start of processing for Is_Pure_Barrier
6046
6047 begin
6048 case Nkind (N) is
6049 when N_Expanded_Name
6050 | N_Identifier
6051 =>
6052 if No (Entity (N)) then
6053 return Abandon;
6054 end if;
6055
6056 if Present (Parent (N))
6057 and then Is_Count_Attribute (Parent (N))
6058 then
6059 return OK;
6060 end if;
6061
6062 case Ekind (Entity (N)) is
6063 when E_Constant
6064 | E_Discriminant
6065 | E_Enumeration_Literal
6066 | E_Named_Integer
6067 | E_Named_Real
6068 =>
6069 return OK;
6070
6071 when E_Component
6072 | E_Variable
6073 =>
6074 -- A variable in the protected type is expanded as a
6075 -- component.
6076
6077 if Is_Simple_Barrier_Name (N) then
6078 return OK;
6079 end if;
6080
6081 when others =>
6082 null;
6083 end case;
6084
6085 when N_Function_Call =>
6086 if Is_Count_Attribute (N) then
6087 return OK;
6088 end if;
6089
6090 when N_Character_Literal
6091 | N_Integer_Literal
6092 | N_Real_Literal
6093 =>
6094 return OK;
6095
6096 when N_Op_Boolean
6097 | N_Op_Not
6098 =>
6099 if Ekind (Entity (N)) = E_Operator then
6100 return OK;
6101 end if;
6102
6103 when N_Short_Circuit =>
6104 return OK;
6105
6106 when others =>
6107 null;
6108 end case;
6109
6110 return Abandon;
6111 end Is_Pure_Barrier;
6112
6113 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6114
6115 -- Start of processing for Expand_Entry_Barrier
6116
6117 begin
6118 if No_Run_Time_Mode then
6119 Error_Msg_CRT ("entry barrier", N);
6120 return;
6121 end if;
6122
6123 -- The body of the entry barrier must be analyzed in the context of the
6124 -- protected object, but its scope is external to it, just as any other
6125 -- unprotected version of a protected operation. The specification has
6126 -- been produced when the protected type declaration was elaborated. We
6127 -- build the body, insert it in the enclosing scope, but analyze it in
6128 -- the current context. A more uniform approach would be to treat the
6129 -- barrier just as a protected function, and discard the protected
6130 -- version of it because it is never called.
6131
6132 if Expander_Active then
6133 B_F := Build_Barrier_Function (N, Ent, Prot);
6134 Func := Barrier_Function (Ent);
6135 Set_Corresponding_Spec (B_F, Func);
6136
6137 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6138
6139 if Nkind (Parent (Body_Decl)) = N_Subunit then
6140 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6141 end if;
6142
6143 Insert_Before_And_Analyze (Body_Decl, B_F);
6144
6145 Set_Discriminals (Spec_Decl);
6146 Set_Scope (Func, Scope (Prot));
6147
6148 else
6149 Analyze_And_Resolve (Cond, Any_Boolean);
6150 end if;
6151
6152 -- Check Pure_Barriers restriction
6153
6154 if Check_Pure_Barriers (Cond) = Abandon then
6155 Check_Restriction (Pure_Barriers, Cond);
6156 end if;
6157
6158 -- The Ravenscar profile restricts barriers to simple variables declared
6159 -- within the protected object. We also allow Boolean constants, since
6160 -- these appear in several published examples and are also allowed by
6161 -- other compilers.
6162
6163 -- Note that after analysis variables in this context will be replaced
6164 -- by the corresponding prival, that is to say a renaming of a selected
6165 -- component of the form _Object.Var. If expansion is disabled, as
6166 -- within a generic, we check that the entity appears in the current
6167 -- scope.
6168
6169 if Is_Entity_Name (Cond) then
6170
6171 -- A small optimization of useless renamings. If the scope of the
6172 -- entity of the condition is not the barrier function, then the
6173 -- condition does not reference any of the generated renamings
6174 -- within the function.
6175
6176 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6177 Set_Declarations (B_F, Empty_List);
6178 end if;
6179
6180 if Entity (Cond) = Standard_False
6181 or else
6182 Entity (Cond) = Standard_True
6183 then
6184 return;
6185
6186 elsif Is_Simple_Barrier_Name (Cond) then
6187 return;
6188 end if;
6189 end if;
6190
6191 -- It is not a boolean variable or literal, so check the restriction.
6192 -- Note that it is safe to be calling Check_Restriction from here, even
6193 -- though this is part of the expander, since Expand_Entry_Barrier is
6194 -- called from Sem_Ch9 even in -gnatc mode.
6195
6196 Check_Restriction (Simple_Barriers, Cond);
6197
6198 -- Emit warning if barrier contains global entities and is thus
6199 -- potentially unsynchronized.
6200
6201 Check_Unprotected_Barrier (Cond);
6202 end Expand_Entry_Barrier;
6203
6204 ------------------------------
6205 -- Expand_N_Abort_Statement --
6206 ------------------------------
6207
6208 -- Expand abort T1, T2, .. Tn; into:
6209 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6210
6211 procedure Expand_N_Abort_Statement (N : Node_Id) is
6212 Loc : constant Source_Ptr := Sloc (N);
6213 Tlist : constant List_Id := Names (N);
6214 Count : Nat;
6215 Aggr : Node_Id;
6216 Tasknm : Node_Id;
6217
6218 begin
6219 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6220 Count := 0;
6221
6222 Tasknm := First (Tlist);
6223
6224 while Present (Tasknm) loop
6225 Count := Count + 1;
6226
6227 -- A task interface class-wide type object is being aborted. Retrieve
6228 -- its _task_id by calling a dispatching routine.
6229
6230 if Ada_Version >= Ada_2005
6231 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6232 and then Is_Interface (Etype (Tasknm))
6233 and then Is_Task_Interface (Etype (Tasknm))
6234 then
6235 Append_To (Component_Associations (Aggr),
6236 Make_Component_Association (Loc,
6237 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6238 Expression =>
6239
6240 -- Task_Id (Tasknm._disp_get_task_id)
6241
6242 Make_Unchecked_Type_Conversion (Loc,
6243 Subtype_Mark =>
6244 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6245 Expression =>
6246 Make_Selected_Component (Loc,
6247 Prefix => New_Copy_Tree (Tasknm),
6248 Selector_Name =>
6249 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6250
6251 else
6252 Append_To (Component_Associations (Aggr),
6253 Make_Component_Association (Loc,
6254 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6255 Expression => Concurrent_Ref (Tasknm)));
6256 end if;
6257
6258 Next (Tasknm);
6259 end loop;
6260
6261 Rewrite (N,
6262 Make_Procedure_Call_Statement (Loc,
6263 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6264 Parameter_Associations => New_List (
6265 Make_Qualified_Expression (Loc,
6266 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6267 Expression => Aggr))));
6268
6269 Analyze (N);
6270 end Expand_N_Abort_Statement;
6271
6272 -------------------------------
6273 -- Expand_N_Accept_Statement --
6274 -------------------------------
6275
6276 -- This procedure handles expansion of accept statements that stand alone,
6277 -- i.e. they are not part of an accept alternative. The expansion of
6278 -- accept statement in accept alternatives is handled by the routines
6279 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6280 -- following description applies only to stand alone accept statements.
6281
6282 -- If there is no handled statement sequence, or only null statements, then
6283 -- this is called a trivial accept, and the expansion is:
6284
6285 -- Accept_Trivial (entry-index)
6286
6287 -- If there is a handled statement sequence, then the expansion is:
6288
6289 -- Ann : Address;
6290 -- {Lnn : Label}
6291
6292 -- begin
6293 -- begin
6294 -- Accept_Call (entry-index, Ann);
6295 -- Renaming_Declarations for formals
6296 -- <statement sequence from N_Accept_Statement node>
6297 -- Complete_Rendezvous;
6298 -- <<Lnn>>
6299 --
6300 -- exception
6301 -- when ... =>
6302 -- <exception handler from N_Accept_Statement node>
6303 -- Complete_Rendezvous;
6304 -- when ... =>
6305 -- <exception handler from N_Accept_Statement node>
6306 -- Complete_Rendezvous;
6307 -- ...
6308 -- end;
6309
6310 -- exception
6311 -- when all others =>
6312 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6313 -- end;
6314
6315 -- The first three declarations were already inserted ahead of the accept
6316 -- statement by the Expand_Accept_Declarations procedure, which was called
6317 -- directly from the semantics during analysis of the accept statement,
6318 -- before analyzing its contained statements.
6319
6320 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6321 -- from possible expansion activity (the original source of course does
6322 -- not have any declarations associated with the accept statement, since
6323 -- an accept statement has no declarative part). In particular, if the
6324 -- expander is active, the first such declaration is the declaration of
6325 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6326
6327 -- The two blocks are merged into a single block if the inner block has
6328 -- no exception handlers, but otherwise two blocks are required, since
6329 -- exceptions might be raised in the exception handlers of the inner
6330 -- block, and Exceptional_Complete_Rendezvous must be called.
6331
6332 procedure Expand_N_Accept_Statement (N : Node_Id) is
6333 Loc : constant Source_Ptr := Sloc (N);
6334 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6335 Ename : constant Node_Id := Entry_Direct_Name (N);
6336 Eindx : constant Node_Id := Entry_Index (N);
6337 Eent : constant Entity_Id := Entity (Ename);
6338 Acstack : constant Elist_Id := Accept_Address (Eent);
6339 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6340 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6341 Blkent : Entity_Id;
6342 Call : Node_Id;
6343 Block : Node_Id;
6344
6345 begin
6346 -- If the accept statement is not part of a list, then its parent must
6347 -- be an accept alternative, and, as described above, we do not do any
6348 -- expansion for such accept statements at this level.
6349
6350 if not Is_List_Member (N) then
6351 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6352 return;
6353
6354 -- Trivial accept case (no statement sequence, or null statements).
6355 -- If the accept statement has declarations, then just insert them
6356 -- before the procedure call.
6357
6358 elsif Trivial_Accept_OK
6359 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6360 then
6361 -- Remove declarations for renamings, because the parameter block
6362 -- will not be assigned.
6363
6364 declare
6365 D : Node_Id;
6366 Next_D : Node_Id;
6367
6368 begin
6369 D := First (Declarations (N));
6370 while Present (D) loop
6371 Next_D := Next (D);
6372 if Nkind (D) = N_Object_Renaming_Declaration then
6373 Remove (D);
6374 end if;
6375
6376 D := Next_D;
6377 end loop;
6378 end;
6379
6380 if Present (Declarations (N)) then
6381 Insert_Actions (N, Declarations (N));
6382 end if;
6383
6384 Rewrite (N,
6385 Make_Procedure_Call_Statement (Loc,
6386 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6387 Parameter_Associations => New_List (
6388 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6389
6390 Analyze (N);
6391
6392 -- Discard Entry_Address that was created for it, so it will not be
6393 -- emitted if this accept statement is in the statement part of a
6394 -- delay alternative.
6395
6396 if Present (Stats) then
6397 Remove_Last_Elmt (Acstack);
6398 end if;
6399
6400 -- Case of statement sequence present
6401
6402 else
6403 -- Construct the block, using the declarations from the accept
6404 -- statement if any to initialize the declarations of the block.
6405
6406 Blkent := Make_Temporary (Loc, 'A');
6407 Set_Ekind (Blkent, E_Block);
6408 Set_Etype (Blkent, Standard_Void_Type);
6409 Set_Scope (Blkent, Current_Scope);
6410
6411 Block :=
6412 Make_Block_Statement (Loc,
6413 Identifier => New_Occurrence_Of (Blkent, Loc),
6414 Declarations => Declarations (N),
6415 Handled_Statement_Sequence => Build_Accept_Body (N));
6416
6417 -- For the analysis of the generated declarations, the parent node
6418 -- must be properly set.
6419
6420 Set_Parent (Block, Parent (N));
6421
6422 -- Prepend call to Accept_Call to main statement sequence If the
6423 -- accept has exception handlers, the statement sequence is wrapped
6424 -- in a block. Insert call and renaming declarations in the
6425 -- declarations of the block, so they are elaborated before the
6426 -- handlers.
6427
6428 Call :=
6429 Make_Procedure_Call_Statement (Loc,
6430 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6431 Parameter_Associations => New_List (
6432 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6433 New_Occurrence_Of (Ann, Loc)));
6434
6435 if Parent (Stats) = N then
6436 Prepend (Call, Statements (Stats));
6437 else
6438 Set_Declarations (Parent (Stats), New_List (Call));
6439 end if;
6440
6441 Analyze (Call);
6442
6443 Push_Scope (Blkent);
6444
6445 declare
6446 D : Node_Id;
6447 Next_D : Node_Id;
6448 Typ : Entity_Id;
6449
6450 begin
6451 D := First (Declarations (N));
6452 while Present (D) loop
6453 Next_D := Next (D);
6454
6455 if Nkind (D) = N_Object_Renaming_Declaration then
6456
6457 -- The renaming declarations for the formals were created
6458 -- during analysis of the accept statement, and attached to
6459 -- the list of declarations. Place them now in the context
6460 -- of the accept block or subprogram.
6461
6462 Remove (D);
6463 Typ := Entity (Subtype_Mark (D));
6464 Insert_After (Call, D);
6465 Analyze (D);
6466
6467 -- If the formal is class_wide, it does not have an actual
6468 -- subtype. The analysis of the renaming declaration creates
6469 -- one, but we need to retain the class-wide nature of the
6470 -- entity.
6471
6472 if Is_Class_Wide_Type (Typ) then
6473 Set_Etype (Defining_Identifier (D), Typ);
6474 end if;
6475
6476 end if;
6477
6478 D := Next_D;
6479 end loop;
6480 end;
6481
6482 End_Scope;
6483
6484 -- Replace the accept statement by the new block
6485
6486 Rewrite (N, Block);
6487 Analyze (N);
6488
6489 -- Last step is to unstack the Accept_Address value
6490
6491 Remove_Last_Elmt (Acstack);
6492 end if;
6493 end Expand_N_Accept_Statement;
6494
6495 ----------------------------------
6496 -- Expand_N_Asynchronous_Select --
6497 ----------------------------------
6498
6499 -- This procedure assumes that the trigger statement is an entry call or
6500 -- a dispatching procedure call. A delay alternative should already have
6501 -- been expanded into an entry call to the appropriate delay object Wait
6502 -- entry.
6503
6504 -- If the trigger is a task entry call, the select is implemented with
6505 -- a Task_Entry_Call:
6506
6507 -- declare
6508 -- B : Boolean;
6509 -- C : Boolean;
6510 -- P : parms := (parm, parm, parm);
6511
6512 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6513
6514 -- procedure _clean is
6515 -- begin
6516 -- ...
6517 -- Cancel_Task_Entry_Call (C);
6518 -- ...
6519 -- end _clean;
6520
6521 -- begin
6522 -- Abort_Defer;
6523 -- Task_Entry_Call
6524 -- (<acceptor-task>, -- Acceptor
6525 -- <entry-index>, -- E
6526 -- P'Address, -- Uninterpreted_Data
6527 -- Asynchronous_Call, -- Mode
6528 -- B); -- Rendezvous_Successful
6529
6530 -- begin
6531 -- begin
6532 -- Abort_Undefer;
6533 -- <abortable-part>
6534 -- at end
6535 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6536 -- end;
6537 -- exception
6538 -- when Abort_Signal => Abort_Undefer;
6539 -- end;
6540
6541 -- parm := P.param;
6542 -- parm := P.param;
6543 -- ...
6544 -- if not C then
6545 -- <triggered-statements>
6546 -- end if;
6547 -- end;
6548
6549 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6550 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6551 -- as follows:
6552
6553 -- declare
6554 -- P : parms := (parm, parm, parm);
6555 -- begin
6556 -- Call_Simple (acceptor-task, entry-index, P'Address);
6557 -- parm := P.param;
6558 -- parm := P.param;
6559 -- ...
6560 -- end;
6561
6562 -- so the task at hand is to convert the latter expansion into the former
6563
6564 -- If the trigger is a protected entry call, the select is implemented
6565 -- with Protected_Entry_Call:
6566
6567 -- declare
6568 -- P : E1_Params := (param, param, param);
6569 -- Bnn : Communications_Block;
6570
6571 -- begin
6572 -- declare
6573
6574 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6575
6576 -- procedure _clean is
6577 -- begin
6578 -- ...
6579 -- if Enqueued (Bnn) then
6580 -- Cancel_Protected_Entry_Call (Bnn);
6581 -- end if;
6582 -- ...
6583 -- end _clean;
6584
6585 -- begin
6586 -- begin
6587 -- Protected_Entry_Call
6588 -- (po._object'Access, -- Object
6589 -- <entry index>, -- E
6590 -- P'Address, -- Uninterpreted_Data
6591 -- Asynchronous_Call, -- Mode
6592 -- Bnn); -- Block
6593
6594 -- if Enqueued (Bnn) then
6595 -- <abortable-part>
6596 -- end if;
6597 -- at end
6598 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6599 -- end;
6600 -- exception
6601 -- when Abort_Signal => Abort_Undefer;
6602 -- end;
6603
6604 -- if not Cancelled (Bnn) then
6605 -- <triggered-statements>
6606 -- end if;
6607 -- end;
6608
6609 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6610 -- entry call:
6611
6612 -- declare
6613 -- P : E1_Params := (param, param, param);
6614 -- Bnn : Communications_Block;
6615
6616 -- begin
6617 -- Protected_Entry_Call
6618 -- (po._object'Access, -- Object
6619 -- <entry index>, -- E
6620 -- P'Address, -- Uninterpreted_Data
6621 -- Simple_Call, -- Mode
6622 -- Bnn); -- Block
6623 -- parm := P.param;
6624 -- parm := P.param;
6625 -- ...
6626 -- end;
6627
6628 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6629 -- expanded into:
6630
6631 -- declare
6632 -- B : Boolean := False;
6633 -- Bnn : Communication_Block;
6634 -- C : Ada.Tags.Prim_Op_Kind;
6635 -- D : System.Storage_Elements.Dummy_Communication_Block;
6636 -- K : Ada.Tags.Tagged_Kind :=
6637 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6638 -- P : Parameters := (Param1 .. ParamN);
6639 -- S : Integer;
6640 -- U : Boolean;
6641
6642 -- begin
6643 -- if K = Ada.Tags.TK_Limited_Tagged
6644 -- or else K = Ada.Tags.TK_Tagged
6645 -- then
6646 -- <dispatching-call>;
6647 -- <triggering-statements>;
6648
6649 -- else
6650 -- S :=
6651 -- Ada.Tags.Get_Offset_Index
6652 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6653
6654 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6655
6656 -- if C = POK_Protected_Entry then
6657 -- declare
6658 -- procedure _clean is
6659 -- begin
6660 -- if Enqueued (Bnn) then
6661 -- Cancel_Protected_Entry_Call (Bnn);
6662 -- end if;
6663 -- end _clean;
6664
6665 -- begin
6666 -- begin
6667 -- _Disp_Asynchronous_Select
6668 -- (<object>, S, P'Address, D, B);
6669 -- Bnn := Communication_Block (D);
6670
6671 -- Param1 := P.Param1;
6672 -- ...
6673 -- ParamN := P.ParamN;
6674
6675 -- if Enqueued (Bnn) then
6676 -- <abortable-statements>
6677 -- end if;
6678 -- at end
6679 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6680 -- end;
6681 -- exception
6682 -- when Abort_Signal => Abort_Undefer;
6683 -- end;
6684
6685 -- if not Cancelled (Bnn) then
6686 -- <triggering-statements>
6687 -- end if;
6688
6689 -- elsif C = POK_Task_Entry then
6690 -- declare
6691 -- procedure _clean is
6692 -- begin
6693 -- Cancel_Task_Entry_Call (U);
6694 -- end _clean;
6695
6696 -- begin
6697 -- Abort_Defer;
6698
6699 -- _Disp_Asynchronous_Select
6700 -- (<object>, S, P'Address, D, B);
6701 -- Bnn := Communication_Bloc (D);
6702
6703 -- Param1 := P.Param1;
6704 -- ...
6705 -- ParamN := P.ParamN;
6706
6707 -- begin
6708 -- begin
6709 -- Abort_Undefer;
6710 -- <abortable-statements>
6711 -- at end
6712 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6713 -- end;
6714 -- exception
6715 -- when Abort_Signal => Abort_Undefer;
6716 -- end;
6717
6718 -- if not U then
6719 -- <triggering-statements>
6720 -- end if;
6721 -- end;
6722
6723 -- else
6724 -- <dispatching-call>;
6725 -- <triggering-statements>
6726 -- end if;
6727 -- end if;
6728 -- end;
6729
6730 -- The job is to convert this to the asynchronous form
6731
6732 -- If the trigger is a delay statement, it will have been expanded into
6733 -- a call to one of the GNARL delay procedures. This routine will convert
6734 -- this into a protected entry call on a delay object and then continue
6735 -- processing as for a protected entry call trigger. This requires
6736 -- declaring a Delay_Block object and adding a pointer to this object to
6737 -- the parameter list of the delay procedure to form the parameter list of
6738 -- the entry call. This object is used by the runtime to queue the delay
6739 -- request.
6740
6741 -- For a description of the use of P and the assignments after the call,
6742 -- see Expand_N_Entry_Call_Statement.
6743
6744 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6745 Loc : constant Source_Ptr := Sloc (N);
6746 Abrt : constant Node_Id := Abortable_Part (N);
6747 Trig : constant Node_Id := Triggering_Alternative (N);
6748
6749 Abort_Block_Ent : Entity_Id;
6750 Abortable_Block : Node_Id;
6751 Actuals : List_Id;
6752 Astats : List_Id;
6753 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6754 Blk_Typ : Entity_Id;
6755 Call : Node_Id;
6756 Call_Ent : Entity_Id;
6757 Cancel_Param : Entity_Id;
6758 Cleanup_Block : Node_Id;
6759 Cleanup_Block_Ent : Entity_Id;
6760 Cleanup_Stmts : List_Id;
6761 Conc_Typ_Stmts : List_Id;
6762 Concval : Node_Id;
6763 Dblock_Ent : Entity_Id;
6764 Decl : Node_Id;
6765 Decls : List_Id;
6766 Ecall : Node_Id;
6767 Ename : Node_Id;
6768 Enqueue_Call : Node_Id;
6769 Formals : List_Id;
6770 Hdle : List_Id;
6771 Handler_Stmt : Node_Id;
6772 Index : Node_Id;
6773 Lim_Typ_Stmts : List_Id;
6774 N_Orig : Node_Id;
6775 Obj : Entity_Id;
6776 Param : Node_Id;
6777 Params : List_Id;
6778 Pdef : Entity_Id;
6779 ProtE_Stmts : List_Id;
6780 ProtP_Stmts : List_Id;
6781 Stmt : Node_Id;
6782 Stmts : List_Id;
6783 TaskE_Stmts : List_Id;
6784 Tstats : List_Id;
6785
6786 B : Entity_Id; -- Call status flag
6787 Bnn : Entity_Id; -- Communication block
6788 C : Entity_Id; -- Call kind
6789 K : Entity_Id; -- Tagged kind
6790 P : Entity_Id; -- Parameter block
6791 S : Entity_Id; -- Primitive operation slot
6792 T : Entity_Id; -- Additional status flag
6793
6794 procedure Rewrite_Abortable_Part;
6795 -- If the trigger is a dispatching call, the expansion inserts multiple
6796 -- copies of the abortable part. This is both inefficient, and may lead
6797 -- to duplicate definitions that the back-end will reject, when the
6798 -- abortable part includes loops. This procedure rewrites the abortable
6799 -- part into a call to a generated procedure.
6800
6801 ----------------------------
6802 -- Rewrite_Abortable_Part --
6803 ----------------------------
6804
6805 procedure Rewrite_Abortable_Part is
6806 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6807 Decl : Node_Id;
6808
6809 begin
6810 Decl :=
6811 Make_Subprogram_Body (Loc,
6812 Specification =>
6813 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6814 Declarations => New_List,
6815 Handled_Statement_Sequence =>
6816 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6817 Insert_Before (N, Decl);
6818 Analyze (Decl);
6819
6820 -- Rewrite abortable part into a call to this procedure
6821
6822 Astats :=
6823 New_List (
6824 Make_Procedure_Call_Statement (Loc,
6825 Name => New_Occurrence_Of (Proc, Loc)));
6826 end Rewrite_Abortable_Part;
6827
6828 -- Start of processing for Expand_N_Asynchronous_Select
6829
6830 begin
6831 -- Asynchronous select is not supported on restricted runtimes. Don't
6832 -- try to expand.
6833
6834 if Restricted_Profile then
6835 return;
6836 end if;
6837
6838 Process_Statements_For_Controlled_Objects (Trig);
6839 Process_Statements_For_Controlled_Objects (Abrt);
6840
6841 Ecall := Triggering_Statement (Trig);
6842
6843 Ensure_Statement_Present (Sloc (Ecall), Trig);
6844
6845 -- Retrieve Astats and Tstats now because the finalization machinery may
6846 -- wrap them in blocks.
6847
6848 Astats := Statements (Abrt);
6849 Tstats := Statements (Trig);
6850
6851 -- The arguments in the call may require dynamic allocation, and the
6852 -- call statement may have been transformed into a block. The block
6853 -- may contain additional declarations for internal entities, and the
6854 -- original call is found by sequential search.
6855
6856 if Nkind (Ecall) = N_Block_Statement then
6857 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6858 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6859 N_Entry_Call_Statement)
6860 loop
6861 Next (Ecall);
6862 end loop;
6863 end if;
6864
6865 -- This is either a dispatching call or a delay statement used as a
6866 -- trigger which was expanded into a procedure call.
6867
6868 if Nkind (Ecall) = N_Procedure_Call_Statement then
6869 if Ada_Version >= Ada_2005
6870 and then
6871 (No (Original_Node (Ecall))
6872 or else not Nkind_In (Original_Node (Ecall),
6873 N_Delay_Relative_Statement,
6874 N_Delay_Until_Statement))
6875 then
6876 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6877
6878 Rewrite_Abortable_Part;
6879 Decls := New_List;
6880 Stmts := New_List;
6881
6882 -- Call status flag processing, generate:
6883 -- B : Boolean := False;
6884
6885 B := Build_B (Loc, Decls);
6886
6887 -- Communication block processing, generate:
6888 -- Bnn : Communication_Block;
6889
6890 Bnn := Make_Temporary (Loc, 'B');
6891 Append_To (Decls,
6892 Make_Object_Declaration (Loc,
6893 Defining_Identifier => Bnn,
6894 Object_Definition =>
6895 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6896
6897 -- Call kind processing, generate:
6898 -- C : Ada.Tags.Prim_Op_Kind;
6899
6900 C := Build_C (Loc, Decls);
6901
6902 -- Tagged kind processing, generate:
6903 -- K : Ada.Tags.Tagged_Kind :=
6904 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6905
6906 -- Dummy communication block, generate:
6907 -- D : Dummy_Communication_Block;
6908
6909 Append_To (Decls,
6910 Make_Object_Declaration (Loc,
6911 Defining_Identifier =>
6912 Make_Defining_Identifier (Loc, Name_uD),
6913 Object_Definition =>
6914 New_Occurrence_Of
6915 (RTE (RE_Dummy_Communication_Block), Loc)));
6916
6917 K := Build_K (Loc, Decls, Obj);
6918
6919 -- Parameter block processing
6920
6921 Blk_Typ := Build_Parameter_Block
6922 (Loc, Actuals, Formals, Decls);
6923 P := Parameter_Block_Pack
6924 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6925
6926 -- Dispatch table slot processing, generate:
6927 -- S : Integer;
6928
6929 S := Build_S (Loc, Decls);
6930
6931 -- Additional status flag processing, generate:
6932 -- Tnn : Boolean;
6933
6934 T := Make_Temporary (Loc, 'T');
6935 Append_To (Decls,
6936 Make_Object_Declaration (Loc,
6937 Defining_Identifier => T,
6938 Object_Definition =>
6939 New_Occurrence_Of (Standard_Boolean, Loc)));
6940
6941 ------------------------------
6942 -- Protected entry handling --
6943 ------------------------------
6944
6945 -- Generate:
6946 -- Param1 := P.Param1;
6947 -- ...
6948 -- ParamN := P.ParamN;
6949
6950 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6951
6952 -- Generate:
6953 -- Bnn := Communication_Block (D);
6954
6955 Prepend_To (Cleanup_Stmts,
6956 Make_Assignment_Statement (Loc,
6957 Name => New_Occurrence_Of (Bnn, Loc),
6958 Expression =>
6959 Make_Unchecked_Type_Conversion (Loc,
6960 Subtype_Mark =>
6961 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
6962 Expression => Make_Identifier (Loc, Name_uD))));
6963
6964 -- Generate:
6965 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6966
6967 Prepend_To (Cleanup_Stmts,
6968 Make_Procedure_Call_Statement (Loc,
6969 Name =>
6970 New_Occurrence_Of
6971 (Find_Prim_Op
6972 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
6973 Loc),
6974 Parameter_Associations =>
6975 New_List (
6976 New_Copy_Tree (Obj), -- <object>
6977 New_Occurrence_Of (S, Loc), -- S
6978 Make_Attribute_Reference (Loc, -- P'Address
6979 Prefix => New_Occurrence_Of (P, Loc),
6980 Attribute_Name => Name_Address),
6981 Make_Identifier (Loc, Name_uD), -- D
6982 New_Occurrence_Of (B, Loc)))); -- B
6983
6984 -- Generate:
6985 -- if Enqueued (Bnn) then
6986 -- <abortable-statements>
6987 -- end if;
6988
6989 Append_To (Cleanup_Stmts,
6990 Make_Implicit_If_Statement (N,
6991 Condition =>
6992 Make_Function_Call (Loc,
6993 Name =>
6994 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
6995 Parameter_Associations =>
6996 New_List (New_Occurrence_Of (Bnn, Loc))),
6997
6998 Then_Statements =>
6999 New_Copy_List_Tree (Astats)));
7000
7001 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7002 -- will then generate a _clean for the communication block Bnn.
7003
7004 -- Generate:
7005 -- declare
7006 -- procedure _clean is
7007 -- begin
7008 -- if Enqueued (Bnn) then
7009 -- Cancel_Protected_Entry_Call (Bnn);
7010 -- end if;
7011 -- end _clean;
7012 -- begin
7013 -- Cleanup_Stmts
7014 -- at end
7015 -- _clean;
7016 -- end;
7017
7018 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7019 Cleanup_Block :=
7020 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7021
7022 -- Wrap the cleanup block in an exception handling block
7023
7024 -- Generate:
7025 -- begin
7026 -- Cleanup_Block
7027 -- exception
7028 -- when Abort_Signal => Abort_Undefer;
7029 -- end;
7030
7031 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7032 ProtE_Stmts :=
7033 New_List (
7034 Make_Implicit_Label_Declaration (Loc,
7035 Defining_Identifier => Abort_Block_Ent),
7036
7037 Build_Abort_Block
7038 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7039
7040 -- Generate:
7041 -- if not Cancelled (Bnn) then
7042 -- <triggering-statements>
7043 -- end if;
7044
7045 Append_To (ProtE_Stmts,
7046 Make_Implicit_If_Statement (N,
7047 Condition =>
7048 Make_Op_Not (Loc,
7049 Right_Opnd =>
7050 Make_Function_Call (Loc,
7051 Name =>
7052 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7053 Parameter_Associations =>
7054 New_List (New_Occurrence_Of (Bnn, Loc)))),
7055
7056 Then_Statements =>
7057 New_Copy_List_Tree (Tstats)));
7058
7059 -------------------------
7060 -- Task entry handling --
7061 -------------------------
7062
7063 -- Generate:
7064 -- Param1 := P.Param1;
7065 -- ...
7066 -- ParamN := P.ParamN;
7067
7068 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7069
7070 -- Generate:
7071 -- Bnn := Communication_Block (D);
7072
7073 Append_To (TaskE_Stmts,
7074 Make_Assignment_Statement (Loc,
7075 Name =>
7076 New_Occurrence_Of (Bnn, Loc),
7077 Expression =>
7078 Make_Unchecked_Type_Conversion (Loc,
7079 Subtype_Mark =>
7080 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7081 Expression => Make_Identifier (Loc, Name_uD))));
7082
7083 -- Generate:
7084 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7085
7086 Prepend_To (TaskE_Stmts,
7087 Make_Procedure_Call_Statement (Loc,
7088 Name =>
7089 New_Occurrence_Of (
7090 Find_Prim_Op (Etype (Etype (Obj)),
7091 Name_uDisp_Asynchronous_Select),
7092 Loc),
7093
7094 Parameter_Associations => New_List (
7095 New_Copy_Tree (Obj), -- <object>
7096 New_Occurrence_Of (S, Loc), -- S
7097 Make_Attribute_Reference (Loc, -- P'Address
7098 Prefix => New_Occurrence_Of (P, Loc),
7099 Attribute_Name => Name_Address),
7100 Make_Identifier (Loc, Name_uD), -- D
7101 New_Occurrence_Of (B, Loc)))); -- B
7102
7103 -- Generate:
7104 -- Abort_Defer;
7105
7106 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7107
7108 -- Generate:
7109 -- Abort_Undefer;
7110 -- <abortable-statements>
7111
7112 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7113
7114 Prepend_To
7115 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7116
7117 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7118 -- will generate a _clean for the additional status flag.
7119
7120 -- Generate:
7121 -- declare
7122 -- procedure _clean is
7123 -- begin
7124 -- Cancel_Task_Entry_Call (U);
7125 -- end _clean;
7126 -- begin
7127 -- Cleanup_Stmts
7128 -- at end
7129 -- _clean;
7130 -- end;
7131
7132 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7133 Cleanup_Block :=
7134 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7135
7136 -- Wrap the cleanup block in an exception handling block
7137
7138 -- Generate:
7139 -- begin
7140 -- Cleanup_Block
7141 -- exception
7142 -- when Abort_Signal => Abort_Undefer;
7143 -- end;
7144
7145 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7146
7147 Append_To (TaskE_Stmts,
7148 Make_Implicit_Label_Declaration (Loc,
7149 Defining_Identifier => Abort_Block_Ent));
7150
7151 Append_To (TaskE_Stmts,
7152 Build_Abort_Block
7153 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7154
7155 -- Generate:
7156 -- if not T then
7157 -- <triggering-statements>
7158 -- end if;
7159
7160 Append_To (TaskE_Stmts,
7161 Make_Implicit_If_Statement (N,
7162 Condition =>
7163 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7164
7165 Then_Statements =>
7166 New_Copy_List_Tree (Tstats)));
7167
7168 ----------------------------------
7169 -- Protected procedure handling --
7170 ----------------------------------
7171
7172 -- Generate:
7173 -- <dispatching-call>;
7174 -- <triggering-statements>
7175
7176 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7177 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7178
7179 -- Generate:
7180 -- S := Ada.Tags.Get_Offset_Index
7181 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7182
7183 Conc_Typ_Stmts :=
7184 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7185
7186 -- Generate:
7187 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7188
7189 Append_To (Conc_Typ_Stmts,
7190 Make_Procedure_Call_Statement (Loc,
7191 Name =>
7192 New_Occurrence_Of
7193 (Find_Prim_Op (Etype (Etype (Obj)),
7194 Name_uDisp_Get_Prim_Op_Kind),
7195 Loc),
7196 Parameter_Associations =>
7197 New_List (
7198 New_Copy_Tree (Obj),
7199 New_Occurrence_Of (S, Loc),
7200 New_Occurrence_Of (C, Loc))));
7201
7202 -- Generate:
7203 -- if C = POK_Procedure_Entry then
7204 -- ProtE_Stmts
7205 -- elsif C = POK_Task_Entry then
7206 -- TaskE_Stmts
7207 -- else
7208 -- ProtP_Stmts
7209 -- end if;
7210
7211 Append_To (Conc_Typ_Stmts,
7212 Make_Implicit_If_Statement (N,
7213 Condition =>
7214 Make_Op_Eq (Loc,
7215 Left_Opnd =>
7216 New_Occurrence_Of (C, Loc),
7217 Right_Opnd =>
7218 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7219
7220 Then_Statements =>
7221 ProtE_Stmts,
7222
7223 Elsif_Parts =>
7224 New_List (
7225 Make_Elsif_Part (Loc,
7226 Condition =>
7227 Make_Op_Eq (Loc,
7228 Left_Opnd =>
7229 New_Occurrence_Of (C, Loc),
7230 Right_Opnd =>
7231 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7232
7233 Then_Statements =>
7234 TaskE_Stmts)),
7235
7236 Else_Statements =>
7237 ProtP_Stmts));
7238
7239 -- Generate:
7240 -- <dispatching-call>;
7241 -- <triggering-statements>
7242
7243 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7244 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7245
7246 -- Generate:
7247 -- if K = Ada.Tags.TK_Limited_Tagged
7248 -- or else K = Ada.Tags.TK_Tagged
7249 -- then
7250 -- Lim_Typ_Stmts
7251 -- else
7252 -- Conc_Typ_Stmts
7253 -- end if;
7254
7255 Append_To (Stmts,
7256 Make_Implicit_If_Statement (N,
7257 Condition => Build_Dispatching_Tag_Check (K, N),
7258 Then_Statements => Lim_Typ_Stmts,
7259 Else_Statements => Conc_Typ_Stmts));
7260
7261 Rewrite (N,
7262 Make_Block_Statement (Loc,
7263 Declarations =>
7264 Decls,
7265 Handled_Statement_Sequence =>
7266 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7267
7268 Analyze (N);
7269 return;
7270
7271 -- Delay triggering statement processing
7272
7273 else
7274 -- Add a Delay_Block object to the parameter list of the delay
7275 -- procedure to form the parameter list of the Wait entry call.
7276
7277 Dblock_Ent := Make_Temporary (Loc, 'D');
7278
7279 Pdef := Entity (Name (Ecall));
7280
7281 if Is_RTE (Pdef, RO_CA_Delay_For) then
7282 Enqueue_Call :=
7283 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7284
7285 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7286 Enqueue_Call :=
7287 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7288
7289 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7290 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7291 end if;
7292
7293 Append_To (Parameter_Associations (Ecall),
7294 Make_Attribute_Reference (Loc,
7295 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7296 Attribute_Name => Name_Unchecked_Access));
7297
7298 -- Create the inner block to protect the abortable part
7299
7300 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7301
7302 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7303
7304 Abortable_Block :=
7305 Make_Block_Statement (Loc,
7306 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7307 Handled_Statement_Sequence =>
7308 Make_Handled_Sequence_Of_Statements (Loc,
7309 Statements => Astats),
7310 Has_Created_Identifier => True,
7311 Is_Asynchronous_Call_Block => True);
7312
7313 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7314
7315 Rewrite (Ecall,
7316 Make_Implicit_If_Statement (N,
7317 Condition =>
7318 Make_Function_Call (Loc,
7319 Name => Enqueue_Call,
7320 Parameter_Associations => Parameter_Associations (Ecall)),
7321 Then_Statements =>
7322 New_List (Make_Block_Statement (Loc,
7323 Handled_Statement_Sequence =>
7324 Make_Handled_Sequence_Of_Statements (Loc,
7325 Statements => New_List (
7326 Make_Implicit_Label_Declaration (Loc,
7327 Defining_Identifier => Blk_Ent,
7328 Label_Construct => Abortable_Block),
7329 Abortable_Block),
7330 Exception_Handlers => Hdle)))));
7331
7332 Stmts := New_List (Ecall);
7333
7334 -- Construct statement sequence for new block
7335
7336 Append_To (Stmts,
7337 Make_Implicit_If_Statement (N,
7338 Condition =>
7339 Make_Function_Call (Loc,
7340 Name => New_Occurrence_Of (
7341 RTE (RE_Timed_Out), Loc),
7342 Parameter_Associations => New_List (
7343 Make_Attribute_Reference (Loc,
7344 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7345 Attribute_Name => Name_Unchecked_Access))),
7346 Then_Statements => Tstats));
7347
7348 -- The result is the new block
7349
7350 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7351
7352 Rewrite (N,
7353 Make_Block_Statement (Loc,
7354 Declarations => New_List (
7355 Make_Object_Declaration (Loc,
7356 Defining_Identifier => Dblock_Ent,
7357 Aliased_Present => True,
7358 Object_Definition =>
7359 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7360
7361 Handled_Statement_Sequence =>
7362 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7363
7364 Analyze (N);
7365 return;
7366 end if;
7367
7368 else
7369 N_Orig := N;
7370 end if;
7371
7372 Extract_Entry (Ecall, Concval, Ename, Index);
7373 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7374
7375 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7376 Decls := Declarations (Ecall);
7377
7378 if Is_Protected_Type (Etype (Concval)) then
7379
7380 -- Get the declarations of the block expanded from the entry call
7381
7382 Decl := First (Decls);
7383 while Present (Decl)
7384 and then (Nkind (Decl) /= N_Object_Declaration
7385 or else not Is_RTE (Etype (Object_Definition (Decl)),
7386 RE_Communication_Block))
7387 loop
7388 Next (Decl);
7389 end loop;
7390
7391 pragma Assert (Present (Decl));
7392 Cancel_Param := Defining_Identifier (Decl);
7393
7394 -- Change the mode of the Protected_Entry_Call call
7395
7396 -- Protected_Entry_Call (
7397 -- Object => po._object'Access,
7398 -- E => <entry index>;
7399 -- Uninterpreted_Data => P'Address;
7400 -- Mode => Asynchronous_Call;
7401 -- Block => Bnn);
7402
7403 -- Skip assignments to temporaries created for in-out parameters
7404
7405 -- This makes unwarranted assumptions about the shape of the expanded
7406 -- tree for the call, and should be cleaned up ???
7407
7408 Stmt := First (Stmts);
7409 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7410 Next (Stmt);
7411 end loop;
7412
7413 Call := Stmt;
7414
7415 Param := First (Parameter_Associations (Call));
7416 while Present (Param)
7417 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7418 loop
7419 Next (Param);
7420 end loop;
7421
7422 pragma Assert (Present (Param));
7423 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7424 Analyze (Param);
7425
7426 -- Append an if statement to execute the abortable part
7427
7428 -- Generate:
7429 -- if Enqueued (Bnn) then
7430
7431 Append_To (Stmts,
7432 Make_Implicit_If_Statement (N,
7433 Condition =>
7434 Make_Function_Call (Loc,
7435 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7436 Parameter_Associations => New_List (
7437 New_Occurrence_Of (Cancel_Param, Loc))),
7438 Then_Statements => Astats));
7439
7440 Abortable_Block :=
7441 Make_Block_Statement (Loc,
7442 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7443 Handled_Statement_Sequence =>
7444 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7445 Has_Created_Identifier => True,
7446 Is_Asynchronous_Call_Block => True);
7447
7448 -- Aborts are not deferred at beginning of exception handlers in
7449 -- ZCX mode.
7450
7451 if ZCX_Exceptions then
7452 Handler_Stmt := Make_Null_Statement (Loc);
7453
7454 else
7455 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7456 end if;
7457
7458 Stmts := New_List (
7459 Make_Block_Statement (Loc,
7460 Handled_Statement_Sequence =>
7461 Make_Handled_Sequence_Of_Statements (Loc,
7462 Statements => New_List (
7463 Make_Implicit_Label_Declaration (Loc,
7464 Defining_Identifier => Blk_Ent,
7465 Label_Construct => Abortable_Block),
7466 Abortable_Block),
7467
7468 -- exception
7469
7470 Exception_Handlers => New_List (
7471 Make_Implicit_Exception_Handler (Loc,
7472
7473 -- when Abort_Signal =>
7474 -- Abort_Undefer.all;
7475
7476 Exception_Choices =>
7477 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7478 Statements => New_List (Handler_Stmt))))),
7479
7480 -- if not Cancelled (Bnn) then
7481 -- triggered statements
7482 -- end if;
7483
7484 Make_Implicit_If_Statement (N,
7485 Condition => Make_Op_Not (Loc,
7486 Right_Opnd =>
7487 Make_Function_Call (Loc,
7488 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7489 Parameter_Associations => New_List (
7490 New_Occurrence_Of (Cancel_Param, Loc)))),
7491 Then_Statements => Tstats));
7492
7493 -- Asynchronous task entry call
7494
7495 else
7496 if No (Decls) then
7497 Decls := New_List;
7498 end if;
7499
7500 B := Make_Defining_Identifier (Loc, Name_uB);
7501
7502 -- Insert declaration of B in declarations of existing block
7503
7504 Prepend_To (Decls,
7505 Make_Object_Declaration (Loc,
7506 Defining_Identifier => B,
7507 Object_Definition =>
7508 New_Occurrence_Of (Standard_Boolean, Loc)));
7509
7510 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7511
7512 -- Insert declaration of C in declarations of existing block
7513
7514 Prepend_To (Decls,
7515 Make_Object_Declaration (Loc,
7516 Defining_Identifier => Cancel_Param,
7517 Object_Definition =>
7518 New_Occurrence_Of (Standard_Boolean, Loc)));
7519
7520 -- Remove and save the call to Call_Simple
7521
7522 Stmt := First (Stmts);
7523
7524 -- Skip assignments to temporaries created for in-out parameters.
7525 -- This makes unwarranted assumptions about the shape of the expanded
7526 -- tree for the call, and should be cleaned up ???
7527
7528 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7529 Next (Stmt);
7530 end loop;
7531
7532 Call := Stmt;
7533
7534 -- Create the inner block to protect the abortable part
7535
7536 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7537
7538 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7539
7540 Abortable_Block :=
7541 Make_Block_Statement (Loc,
7542 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7543 Handled_Statement_Sequence =>
7544 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7545 Has_Created_Identifier => True,
7546 Is_Asynchronous_Call_Block => True);
7547
7548 Insert_After (Call,
7549 Make_Block_Statement (Loc,
7550 Handled_Statement_Sequence =>
7551 Make_Handled_Sequence_Of_Statements (Loc,
7552 Statements => New_List (
7553 Make_Implicit_Label_Declaration (Loc,
7554 Defining_Identifier => Blk_Ent,
7555 Label_Construct => Abortable_Block),
7556 Abortable_Block),
7557 Exception_Handlers => Hdle)));
7558
7559 -- Create new call statement
7560
7561 Params := Parameter_Associations (Call);
7562
7563 Append_To (Params,
7564 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7565 Append_To (Params, New_Occurrence_Of (B, Loc));
7566
7567 Rewrite (Call,
7568 Make_Procedure_Call_Statement (Loc,
7569 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7570 Parameter_Associations => Params));
7571
7572 -- Construct statement sequence for new block
7573
7574 Append_To (Stmts,
7575 Make_Implicit_If_Statement (N,
7576 Condition =>
7577 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7578 Then_Statements => Tstats));
7579
7580 -- Protected the call against abort
7581
7582 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7583 end if;
7584
7585 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7586
7587 -- The result is the new block
7588
7589 Rewrite (N_Orig,
7590 Make_Block_Statement (Loc,
7591 Declarations => Decls,
7592 Handled_Statement_Sequence =>
7593 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7594
7595 Analyze (N_Orig);
7596 end Expand_N_Asynchronous_Select;
7597
7598 -------------------------------------
7599 -- Expand_N_Conditional_Entry_Call --
7600 -------------------------------------
7601
7602 -- The conditional task entry call is converted to a call to
7603 -- Task_Entry_Call:
7604
7605 -- declare
7606 -- B : Boolean;
7607 -- P : parms := (parm, parm, parm);
7608
7609 -- begin
7610 -- Task_Entry_Call
7611 -- (<acceptor-task>, -- Acceptor
7612 -- <entry-index>, -- E
7613 -- P'Address, -- Uninterpreted_Data
7614 -- Conditional_Call, -- Mode
7615 -- B); -- Rendezvous_Successful
7616 -- parm := P.param;
7617 -- parm := P.param;
7618 -- ...
7619 -- if B then
7620 -- normal-statements
7621 -- else
7622 -- else-statements
7623 -- end if;
7624 -- end;
7625
7626 -- For a description of the use of P and the assignments after the call,
7627 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7628 -- conditional entry call has already been expanded (by the Expand_N_Entry
7629 -- _Call_Statement procedure) as follows:
7630
7631 -- declare
7632 -- P : parms := (parm, parm, parm);
7633 -- begin
7634 -- ... info for in-out parameters
7635 -- Call_Simple (acceptor-task, entry-index, P'Address);
7636 -- parm := P.param;
7637 -- parm := P.param;
7638 -- ...
7639 -- end;
7640
7641 -- so the task at hand is to convert the latter expansion into the former
7642
7643 -- The conditional protected entry call is converted to a call to
7644 -- Protected_Entry_Call:
7645
7646 -- declare
7647 -- P : parms := (parm, parm, parm);
7648 -- Bnn : Communications_Block;
7649
7650 -- begin
7651 -- Protected_Entry_Call
7652 -- (po._object'Access, -- Object
7653 -- <entry index>, -- E
7654 -- P'Address, -- Uninterpreted_Data
7655 -- Conditional_Call, -- Mode
7656 -- Bnn); -- Block
7657 -- parm := P.param;
7658 -- parm := P.param;
7659 -- ...
7660 -- if Cancelled (Bnn) then
7661 -- else-statements
7662 -- else
7663 -- normal-statements
7664 -- end if;
7665 -- end;
7666
7667 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7668 -- into:
7669
7670 -- declare
7671 -- B : Boolean := False;
7672 -- C : Ada.Tags.Prim_Op_Kind;
7673 -- K : Ada.Tags.Tagged_Kind :=
7674 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7675 -- P : Parameters := (Param1 .. ParamN);
7676 -- S : Integer;
7677
7678 -- begin
7679 -- if K = Ada.Tags.TK_Limited_Tagged
7680 -- or else K = Ada.Tags.TK_Tagged
7681 -- then
7682 -- <dispatching-call>;
7683 -- <triggering-statements>
7684
7685 -- else
7686 -- S :=
7687 -- Ada.Tags.Get_Offset_Index
7688 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7689
7690 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7691
7692 -- if C = POK_Protected_Entry
7693 -- or else C = POK_Task_Entry
7694 -- then
7695 -- Param1 := P.Param1;
7696 -- ...
7697 -- ParamN := P.ParamN;
7698 -- end if;
7699
7700 -- if B then
7701 -- if C = POK_Procedure
7702 -- or else C = POK_Protected_Procedure
7703 -- or else C = POK_Task_Procedure
7704 -- then
7705 -- <dispatching-call>;
7706 -- end if;
7707
7708 -- <triggering-statements>
7709 -- else
7710 -- <else-statements>
7711 -- end if;
7712 -- end if;
7713 -- end;
7714
7715 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7716 Loc : constant Source_Ptr := Sloc (N);
7717 Alt : constant Node_Id := Entry_Call_Alternative (N);
7718 Blk : Node_Id := Entry_Call_Statement (Alt);
7719
7720 Actuals : List_Id;
7721 Blk_Typ : Entity_Id;
7722 Call : Node_Id;
7723 Call_Ent : Entity_Id;
7724 Conc_Typ_Stmts : List_Id;
7725 Decl : Node_Id;
7726 Decls : List_Id;
7727 Formals : List_Id;
7728 Lim_Typ_Stmts : List_Id;
7729 N_Stats : List_Id;
7730 Obj : Entity_Id;
7731 Param : Node_Id;
7732 Params : List_Id;
7733 Stmt : Node_Id;
7734 Stmts : List_Id;
7735 Transient_Blk : Node_Id;
7736 Unpack : List_Id;
7737
7738 B : Entity_Id; -- Call status flag
7739 C : Entity_Id; -- Call kind
7740 K : Entity_Id; -- Tagged kind
7741 P : Entity_Id; -- Parameter block
7742 S : Entity_Id; -- Primitive operation slot
7743
7744 begin
7745 Process_Statements_For_Controlled_Objects (N);
7746
7747 if Ada_Version >= Ada_2005
7748 and then Nkind (Blk) = N_Procedure_Call_Statement
7749 then
7750 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7751
7752 Decls := New_List;
7753 Stmts := New_List;
7754
7755 -- Call status flag processing, generate:
7756 -- B : Boolean := False;
7757
7758 B := Build_B (Loc, Decls);
7759
7760 -- Call kind processing, generate:
7761 -- C : Ada.Tags.Prim_Op_Kind;
7762
7763 C := Build_C (Loc, Decls);
7764
7765 -- Tagged kind processing, generate:
7766 -- K : Ada.Tags.Tagged_Kind :=
7767 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7768
7769 K := Build_K (Loc, Decls, Obj);
7770
7771 -- Parameter block processing
7772
7773 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7774 P := Parameter_Block_Pack
7775 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7776
7777 -- Dispatch table slot processing, generate:
7778 -- S : Integer;
7779
7780 S := Build_S (Loc, Decls);
7781
7782 -- Generate:
7783 -- S := Ada.Tags.Get_Offset_Index
7784 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7785
7786 Conc_Typ_Stmts :=
7787 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7788
7789 -- Generate:
7790 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7791
7792 Append_To (Conc_Typ_Stmts,
7793 Make_Procedure_Call_Statement (Loc,
7794 Name =>
7795 New_Occurrence_Of (
7796 Find_Prim_Op (Etype (Etype (Obj)),
7797 Name_uDisp_Conditional_Select),
7798 Loc),
7799 Parameter_Associations =>
7800 New_List (
7801 New_Copy_Tree (Obj), -- <object>
7802 New_Occurrence_Of (S, Loc), -- S
7803 Make_Attribute_Reference (Loc, -- P'Address
7804 Prefix => New_Occurrence_Of (P, Loc),
7805 Attribute_Name => Name_Address),
7806 New_Occurrence_Of (C, Loc), -- C
7807 New_Occurrence_Of (B, Loc)))); -- B
7808
7809 -- Generate:
7810 -- if C = POK_Protected_Entry
7811 -- or else C = POK_Task_Entry
7812 -- then
7813 -- Param1 := P.Param1;
7814 -- ...
7815 -- ParamN := P.ParamN;
7816 -- end if;
7817
7818 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7819
7820 -- Generate the if statement only when the packed parameters need
7821 -- explicit assignments to their corresponding actuals.
7822
7823 if Present (Unpack) then
7824 Append_To (Conc_Typ_Stmts,
7825 Make_Implicit_If_Statement (N,
7826 Condition =>
7827 Make_Or_Else (Loc,
7828 Left_Opnd =>
7829 Make_Op_Eq (Loc,
7830 Left_Opnd =>
7831 New_Occurrence_Of (C, Loc),
7832 Right_Opnd =>
7833 New_Occurrence_Of (RTE (
7834 RE_POK_Protected_Entry), Loc)),
7835
7836 Right_Opnd =>
7837 Make_Op_Eq (Loc,
7838 Left_Opnd =>
7839 New_Occurrence_Of (C, Loc),
7840 Right_Opnd =>
7841 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7842
7843 Then_Statements => Unpack));
7844 end if;
7845
7846 -- Generate:
7847 -- if B then
7848 -- if C = POK_Procedure
7849 -- or else C = POK_Protected_Procedure
7850 -- or else C = POK_Task_Procedure
7851 -- then
7852 -- <dispatching-call>
7853 -- end if;
7854 -- <normal-statements>
7855 -- else
7856 -- <else-statements>
7857 -- end if;
7858
7859 N_Stats := New_Copy_List_Tree (Statements (Alt));
7860
7861 Prepend_To (N_Stats,
7862 Make_Implicit_If_Statement (N,
7863 Condition =>
7864 Make_Or_Else (Loc,
7865 Left_Opnd =>
7866 Make_Op_Eq (Loc,
7867 Left_Opnd =>
7868 New_Occurrence_Of (C, Loc),
7869 Right_Opnd =>
7870 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7871
7872 Right_Opnd =>
7873 Make_Or_Else (Loc,
7874 Left_Opnd =>
7875 Make_Op_Eq (Loc,
7876 Left_Opnd =>
7877 New_Occurrence_Of (C, Loc),
7878 Right_Opnd =>
7879 New_Occurrence_Of (RTE (
7880 RE_POK_Protected_Procedure), Loc)),
7881
7882 Right_Opnd =>
7883 Make_Op_Eq (Loc,
7884 Left_Opnd =>
7885 New_Occurrence_Of (C, Loc),
7886 Right_Opnd =>
7887 New_Occurrence_Of (RTE (
7888 RE_POK_Task_Procedure), Loc)))),
7889
7890 Then_Statements =>
7891 New_List (Blk)));
7892
7893 Append_To (Conc_Typ_Stmts,
7894 Make_Implicit_If_Statement (N,
7895 Condition => New_Occurrence_Of (B, Loc),
7896 Then_Statements => N_Stats,
7897 Else_Statements => Else_Statements (N)));
7898
7899 -- Generate:
7900 -- <dispatching-call>;
7901 -- <triggering-statements>
7902
7903 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7904 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7905
7906 -- Generate:
7907 -- if K = Ada.Tags.TK_Limited_Tagged
7908 -- or else K = Ada.Tags.TK_Tagged
7909 -- then
7910 -- Lim_Typ_Stmts
7911 -- else
7912 -- Conc_Typ_Stmts
7913 -- end if;
7914
7915 Append_To (Stmts,
7916 Make_Implicit_If_Statement (N,
7917 Condition => Build_Dispatching_Tag_Check (K, N),
7918 Then_Statements => Lim_Typ_Stmts,
7919 Else_Statements => Conc_Typ_Stmts));
7920
7921 Rewrite (N,
7922 Make_Block_Statement (Loc,
7923 Declarations =>
7924 Decls,
7925 Handled_Statement_Sequence =>
7926 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7927
7928 -- As described above, the entry alternative is transformed into a
7929 -- block that contains the gnulli call, and possibly assignment
7930 -- statements for in-out parameters. The gnulli call may itself be
7931 -- rewritten into a transient block if some unconstrained parameters
7932 -- require it. We need to retrieve the call to complete its parameter
7933 -- list.
7934
7935 else
7936 Transient_Blk :=
7937 First_Real_Statement (Handled_Statement_Sequence (Blk));
7938
7939 if Present (Transient_Blk)
7940 and then Nkind (Transient_Blk) = N_Block_Statement
7941 then
7942 Blk := Transient_Blk;
7943 end if;
7944
7945 Stmts := Statements (Handled_Statement_Sequence (Blk));
7946 Stmt := First (Stmts);
7947 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7948 Next (Stmt);
7949 end loop;
7950
7951 Call := Stmt;
7952 Params := Parameter_Associations (Call);
7953
7954 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7955
7956 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7957
7958 Param := First (Params);
7959 while Present (Param)
7960 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7961 loop
7962 Next (Param);
7963 end loop;
7964
7965 pragma Assert (Present (Param));
7966 Rewrite (Param,
7967 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
7968
7969 Analyze (Param);
7970
7971 -- Find the Communication_Block parameter for the call to the
7972 -- Cancelled function.
7973
7974 Decl := First (Declarations (Blk));
7975 while Present (Decl)
7976 and then not Is_RTE (Etype (Object_Definition (Decl)),
7977 RE_Communication_Block)
7978 loop
7979 Next (Decl);
7980 end loop;
7981
7982 -- Add an if statement to execute the else part if the call
7983 -- does not succeed (as indicated by the Cancelled predicate).
7984
7985 Append_To (Stmts,
7986 Make_Implicit_If_Statement (N,
7987 Condition => Make_Function_Call (Loc,
7988 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7989 Parameter_Associations => New_List (
7990 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
7991 Then_Statements => Else_Statements (N),
7992 Else_Statements => Statements (Alt)));
7993
7994 else
7995 B := Make_Defining_Identifier (Loc, Name_uB);
7996
7997 -- Insert declaration of B in declarations of existing block
7998
7999 if No (Declarations (Blk)) then
8000 Set_Declarations (Blk, New_List);
8001 end if;
8002
8003 Prepend_To (Declarations (Blk),
8004 Make_Object_Declaration (Loc,
8005 Defining_Identifier => B,
8006 Object_Definition =>
8007 New_Occurrence_Of (Standard_Boolean, Loc)));
8008
8009 -- Create new call statement
8010
8011 Append_To (Params,
8012 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8013 Append_To (Params, New_Occurrence_Of (B, Loc));
8014
8015 Rewrite (Call,
8016 Make_Procedure_Call_Statement (Loc,
8017 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8018 Parameter_Associations => Params));
8019
8020 -- Construct statement sequence for new block
8021
8022 Append_To (Stmts,
8023 Make_Implicit_If_Statement (N,
8024 Condition => New_Occurrence_Of (B, Loc),
8025 Then_Statements => Statements (Alt),
8026 Else_Statements => Else_Statements (N)));
8027 end if;
8028
8029 -- The result is the new block
8030
8031 Rewrite (N,
8032 Make_Block_Statement (Loc,
8033 Declarations => Declarations (Blk),
8034 Handled_Statement_Sequence =>
8035 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8036 end if;
8037
8038 Analyze (N);
8039 end Expand_N_Conditional_Entry_Call;
8040
8041 ---------------------------------------
8042 -- Expand_N_Delay_Relative_Statement --
8043 ---------------------------------------
8044
8045 -- Delay statement is implemented as a procedure call to Delay_For
8046 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8047 -- simple delays imposed by the use of Protected Objects.
8048
8049 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8050 Loc : constant Source_Ptr := Sloc (N);
8051 Proc : Entity_Id;
8052
8053 begin
8054 -- Try to use System.Relative_Delays.Delay_For only if available. This
8055 -- is the implementation used on restricted platforms when Ada.Calendar
8056 -- is not available.
8057
8058 if RTE_Available (RO_RD_Delay_For) then
8059 Proc := RTE (RO_RD_Delay_For);
8060
8061 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8062 -- message if not available.
8063
8064 else
8065 Proc := RTE (RO_CA_Delay_For);
8066 end if;
8067
8068 Rewrite (N,
8069 Make_Procedure_Call_Statement (Loc,
8070 Name => New_Occurrence_Of (Proc, Loc),
8071 Parameter_Associations => New_List (Expression (N))));
8072 Analyze (N);
8073 end Expand_N_Delay_Relative_Statement;
8074
8075 ------------------------------------
8076 -- Expand_N_Delay_Until_Statement --
8077 ------------------------------------
8078
8079 -- Delay Until statement is implemented as a procedure call to
8080 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8081
8082 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8083 Loc : constant Source_Ptr := Sloc (N);
8084 Typ : Entity_Id;
8085
8086 begin
8087 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8088 Typ := RTE (RO_CA_Delay_Until);
8089 else
8090 Typ := RTE (RO_RT_Delay_Until);
8091 end if;
8092
8093 Rewrite (N,
8094 Make_Procedure_Call_Statement (Loc,
8095 Name => New_Occurrence_Of (Typ, Loc),
8096 Parameter_Associations => New_List (Expression (N))));
8097
8098 Analyze (N);
8099 end Expand_N_Delay_Until_Statement;
8100
8101 -------------------------
8102 -- Expand_N_Entry_Body --
8103 -------------------------
8104
8105 procedure Expand_N_Entry_Body (N : Node_Id) is
8106 begin
8107 -- Associate discriminals with the next protected operation body to be
8108 -- expanded.
8109
8110 if Present (Next_Protected_Operation (N)) then
8111 Set_Discriminals (Parent (Current_Scope));
8112 end if;
8113 end Expand_N_Entry_Body;
8114
8115 -----------------------------------
8116 -- Expand_N_Entry_Call_Statement --
8117 -----------------------------------
8118
8119 -- An entry call is expanded into GNARLI calls to implement a simple entry
8120 -- call (see Build_Simple_Entry_Call).
8121
8122 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8123 Concval : Node_Id;
8124 Ename : Node_Id;
8125 Index : Node_Id;
8126
8127 begin
8128 if No_Run_Time_Mode then
8129 Error_Msg_CRT ("entry call", N);
8130 return;
8131 end if;
8132
8133 -- If this entry call is part of an asynchronous select, don't expand it
8134 -- here; it will be expanded with the select statement. Don't expand
8135 -- timed entry calls either, as they are translated into asynchronous
8136 -- entry calls.
8137
8138 -- ??? This whole approach is questionable; it may be better to go back
8139 -- to allowing the expansion to take place and then attempting to fix it
8140 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8141 -- whether the expanded call is on a task or protected entry.
8142
8143 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8144 or else N /= Triggering_Statement (Parent (N)))
8145 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8146 or else N /= Entry_Call_Statement (Parent (N))
8147 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8148 then
8149 Extract_Entry (N, Concval, Ename, Index);
8150 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8151 end if;
8152 end Expand_N_Entry_Call_Statement;
8153
8154 --------------------------------
8155 -- Expand_N_Entry_Declaration --
8156 --------------------------------
8157
8158 -- If there are parameters, then first, each of the formals is marked by
8159 -- setting Is_Entry_Formal. Next a record type is built which is used to
8160 -- hold the parameter values. The name of this record type is entryP where
8161 -- entry is the name of the entry, with an additional corresponding access
8162 -- type called entryPA. The record type has matching components for each
8163 -- formal (the component names are the same as the formal names). For
8164 -- elementary types, the component type matches the formal type. For
8165 -- composite types, an access type is declared (with the name formalA)
8166 -- which designates the formal type, and the type of the component is this
8167 -- access type. Finally the Entry_Component of each formal is set to
8168 -- reference the corresponding record component.
8169
8170 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8171 Loc : constant Source_Ptr := Sloc (N);
8172 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8173 Components : List_Id;
8174 Formal : Node_Id;
8175 Ftype : Entity_Id;
8176 Last_Decl : Node_Id;
8177 Component : Entity_Id;
8178 Ctype : Entity_Id;
8179 Decl : Node_Id;
8180 Rec_Ent : Entity_Id;
8181 Acc_Ent : Entity_Id;
8182
8183 begin
8184 Formal := First_Formal (Entry_Ent);
8185 Last_Decl := N;
8186
8187 -- Most processing is done only if parameters are present
8188
8189 if Present (Formal) then
8190 Components := New_List;
8191
8192 -- Loop through formals
8193
8194 while Present (Formal) loop
8195 Set_Is_Entry_Formal (Formal);
8196 Component :=
8197 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8198 Set_Entry_Component (Formal, Component);
8199 Set_Entry_Formal (Component, Formal);
8200 Ftype := Etype (Formal);
8201
8202 -- Declare new access type and then append
8203
8204 Ctype := Make_Temporary (Loc, 'A');
8205 Set_Is_Param_Block_Component_Type (Ctype);
8206
8207 Decl :=
8208 Make_Full_Type_Declaration (Loc,
8209 Defining_Identifier => Ctype,
8210 Type_Definition =>
8211 Make_Access_To_Object_Definition (Loc,
8212 All_Present => True,
8213 Constant_Present => Ekind (Formal) = E_In_Parameter,
8214 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8215
8216 Insert_After (Last_Decl, Decl);
8217 Last_Decl := Decl;
8218
8219 Append_To (Components,
8220 Make_Component_Declaration (Loc,
8221 Defining_Identifier => Component,
8222 Component_Definition =>
8223 Make_Component_Definition (Loc,
8224 Aliased_Present => False,
8225 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8226
8227 Next_Formal_With_Extras (Formal);
8228 end loop;
8229
8230 -- Create the Entry_Parameter_Record declaration
8231
8232 Rec_Ent := Make_Temporary (Loc, 'P');
8233
8234 Decl :=
8235 Make_Full_Type_Declaration (Loc,
8236 Defining_Identifier => Rec_Ent,
8237 Type_Definition =>
8238 Make_Record_Definition (Loc,
8239 Component_List =>
8240 Make_Component_List (Loc,
8241 Component_Items => Components)));
8242
8243 Insert_After (Last_Decl, Decl);
8244 Last_Decl := Decl;
8245
8246 -- Construct and link in the corresponding access type
8247
8248 Acc_Ent := Make_Temporary (Loc, 'A');
8249
8250 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8251
8252 Decl :=
8253 Make_Full_Type_Declaration (Loc,
8254 Defining_Identifier => Acc_Ent,
8255 Type_Definition =>
8256 Make_Access_To_Object_Definition (Loc,
8257 All_Present => True,
8258 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8259
8260 Insert_After (Last_Decl, Decl);
8261 end if;
8262 end Expand_N_Entry_Declaration;
8263
8264 -----------------------------
8265 -- Expand_N_Protected_Body --
8266 -----------------------------
8267
8268 -- Protected bodies are expanded to the completion of the subprograms
8269 -- created for the corresponding protected type. These are a protected and
8270 -- unprotected version of each protected subprogram in the object, a
8271 -- function to calculate each entry barrier, and a procedure to execute the
8272 -- sequence of statements of each protected entry body. For example, for
8273 -- protected type ptype:
8274
8275 -- function entB
8276 -- (O : System.Address;
8277 -- E : Protected_Entry_Index)
8278 -- return Boolean
8279 -- is
8280 -- <discriminant renamings>
8281 -- <private object renamings>
8282 -- begin
8283 -- return <barrier expression>;
8284 -- end entB;
8285
8286 -- procedure pprocN (_object : in out poV;...) is
8287 -- <discriminant renamings>
8288 -- <private object renamings>
8289 -- begin
8290 -- <sequence of statements>
8291 -- end pprocN;
8292
8293 -- procedure pprocP (_object : in out poV;...) is
8294 -- procedure _clean is
8295 -- Pn : Boolean;
8296 -- begin
8297 -- ptypeS (_object, Pn);
8298 -- Unlock (_object._object'Access);
8299 -- Abort_Undefer.all;
8300 -- end _clean;
8301
8302 -- begin
8303 -- Abort_Defer.all;
8304 -- Lock (_object._object'Access);
8305 -- pprocN (_object;...);
8306 -- at end
8307 -- _clean;
8308 -- end pproc;
8309
8310 -- function pfuncN (_object : poV;...) return Return_Type is
8311 -- <discriminant renamings>
8312 -- <private object renamings>
8313 -- begin
8314 -- <sequence of statements>
8315 -- end pfuncN;
8316
8317 -- function pfuncP (_object : poV) return Return_Type is
8318 -- procedure _clean is
8319 -- begin
8320 -- Unlock (_object._object'Access);
8321 -- Abort_Undefer.all;
8322 -- end _clean;
8323
8324 -- begin
8325 -- Abort_Defer.all;
8326 -- Lock (_object._object'Access);
8327 -- return pfuncN (_object);
8328
8329 -- at end
8330 -- _clean;
8331 -- end pfunc;
8332
8333 -- procedure entE
8334 -- (O : System.Address;
8335 -- P : System.Address;
8336 -- E : Protected_Entry_Index)
8337 -- is
8338 -- <discriminant renamings>
8339 -- <private object renamings>
8340 -- type poVP is access poV;
8341 -- _Object : ptVP := ptVP!(O);
8342
8343 -- begin
8344 -- begin
8345 -- <statement sequence>
8346 -- Complete_Entry_Body (_Object._Object);
8347 -- exception
8348 -- when all others =>
8349 -- Exceptional_Complete_Entry_Body (
8350 -- _Object._Object, Get_GNAT_Exception);
8351 -- end;
8352 -- end entE;
8353
8354 -- The type poV is the record created for the protected type to hold
8355 -- the state of the protected object.
8356
8357 procedure Expand_N_Protected_Body (N : Node_Id) is
8358 Loc : constant Source_Ptr := Sloc (N);
8359 Pid : constant Entity_Id := Corresponding_Spec (N);
8360
8361 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8362 -- This flag indicates whether the lock free implementation is active
8363
8364 Current_Node : Node_Id;
8365 Disp_Op_Body : Node_Id;
8366 New_Op_Body : Node_Id;
8367 Op_Body : Node_Id;
8368 Op_Id : Entity_Id;
8369
8370 function Build_Dispatching_Subprogram_Body
8371 (N : Node_Id;
8372 Pid : Node_Id;
8373 Prot_Bod : Node_Id) return Node_Id;
8374 -- Build a dispatching version of the protected subprogram body. The
8375 -- newly generated subprogram contains a call to the original protected
8376 -- body. The following code is generated:
8377 --
8378 -- function <protected-function-name> (Param1 .. ParamN) return
8379 -- <return-type> is
8380 -- begin
8381 -- return <protected-function-name>P (Param1 .. ParamN);
8382 -- end <protected-function-name>;
8383 --
8384 -- or
8385 --
8386 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8387 -- begin
8388 -- <protected-procedure-name>P (Param1 .. ParamN);
8389 -- end <protected-procedure-name>
8390
8391 ---------------------------------------
8392 -- Build_Dispatching_Subprogram_Body --
8393 ---------------------------------------
8394
8395 function Build_Dispatching_Subprogram_Body
8396 (N : Node_Id;
8397 Pid : Node_Id;
8398 Prot_Bod : Node_Id) return Node_Id
8399 is
8400 Loc : constant Source_Ptr := Sloc (N);
8401 Actuals : List_Id;
8402 Formal : Node_Id;
8403 Spec : Node_Id;
8404 Stmts : List_Id;
8405
8406 begin
8407 -- Generate a specification without a letter suffix in order to
8408 -- override an interface function or procedure.
8409
8410 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8411
8412 -- The formal parameters become the actuals of the protected function
8413 -- or procedure call.
8414
8415 Actuals := New_List;
8416 Formal := First (Parameter_Specifications (Spec));
8417 while Present (Formal) loop
8418 Append_To (Actuals,
8419 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8420 Next (Formal);
8421 end loop;
8422
8423 if Nkind (Spec) = N_Procedure_Specification then
8424 Stmts :=
8425 New_List (
8426 Make_Procedure_Call_Statement (Loc,
8427 Name =>
8428 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8429 Parameter_Associations => Actuals));
8430
8431 else
8432 pragma Assert (Nkind (Spec) = N_Function_Specification);
8433
8434 Stmts :=
8435 New_List (
8436 Make_Simple_Return_Statement (Loc,
8437 Expression =>
8438 Make_Function_Call (Loc,
8439 Name =>
8440 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8441 Parameter_Associations => Actuals)));
8442 end if;
8443
8444 return
8445 Make_Subprogram_Body (Loc,
8446 Declarations => Empty_List,
8447 Specification => Spec,
8448 Handled_Statement_Sequence =>
8449 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8450 end Build_Dispatching_Subprogram_Body;
8451
8452 -- Start of processing for Expand_N_Protected_Body
8453
8454 begin
8455 if No_Run_Time_Mode then
8456 Error_Msg_CRT ("protected body", N);
8457 return;
8458 end if;
8459
8460 -- This is the proper body corresponding to a stub. The declarations
8461 -- must be inserted at the point of the stub, which in turn is in the
8462 -- declarative part of the parent unit.
8463
8464 if Nkind (Parent (N)) = N_Subunit then
8465 Current_Node := Corresponding_Stub (Parent (N));
8466 else
8467 Current_Node := N;
8468 end if;
8469
8470 Op_Body := First (Declarations (N));
8471
8472 -- The protected body is replaced with the bodies of its
8473 -- protected operations, and the declarations for internal objects
8474 -- that may have been created for entry family bounds.
8475
8476 Rewrite (N, Make_Null_Statement (Sloc (N)));
8477 Analyze (N);
8478
8479 while Present (Op_Body) loop
8480 case Nkind (Op_Body) is
8481 when N_Subprogram_Declaration =>
8482 null;
8483
8484 when N_Subprogram_Body =>
8485
8486 -- Do not create bodies for eliminated operations
8487
8488 if not Is_Eliminated (Defining_Entity (Op_Body))
8489 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8490 then
8491 if Lock_Free_Active then
8492 New_Op_Body :=
8493 Build_Lock_Free_Unprotected_Subprogram_Body
8494 (Op_Body, Pid);
8495 else
8496 New_Op_Body :=
8497 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8498 end if;
8499
8500 Insert_After (Current_Node, New_Op_Body);
8501 Current_Node := New_Op_Body;
8502 Analyze (New_Op_Body);
8503
8504 -- Build the corresponding protected operation. It may
8505 -- appear that this is needed only if this is a visible
8506 -- operation of the type, or if it is an interrupt handler,
8507 -- and this was the strategy used previously in GNAT.
8508
8509 -- However, the operation may be exported through a 'Access
8510 -- to an external caller. This is the common idiom in code
8511 -- that uses the Ada 2005 Timing_Events package. As a result
8512 -- we need to produce the protected body for both visible
8513 -- and private operations, as well as operations that only
8514 -- have a body in the source, and for which we create a
8515 -- declaration in the protected body itself.
8516
8517 if Present (Corresponding_Spec (Op_Body)) then
8518 if Lock_Free_Active then
8519 New_Op_Body :=
8520 Build_Lock_Free_Protected_Subprogram_Body
8521 (Op_Body, Pid, Specification (New_Op_Body));
8522 else
8523 New_Op_Body :=
8524 Build_Protected_Subprogram_Body
8525 (Op_Body, Pid, Specification (New_Op_Body));
8526 end if;
8527
8528 Insert_After (Current_Node, New_Op_Body);
8529 Analyze (New_Op_Body);
8530
8531 Current_Node := New_Op_Body;
8532
8533 -- Generate an overriding primitive operation body for
8534 -- this subprogram if the protected type implements an
8535 -- interface.
8536
8537 if Ada_Version >= Ada_2005
8538 and then
8539 Present (Interfaces (Corresponding_Record_Type (Pid)))
8540 then
8541 Disp_Op_Body :=
8542 Build_Dispatching_Subprogram_Body
8543 (Op_Body, Pid, New_Op_Body);
8544
8545 Insert_After (Current_Node, Disp_Op_Body);
8546 Analyze (Disp_Op_Body);
8547
8548 Current_Node := Disp_Op_Body;
8549 end if;
8550 end if;
8551 end if;
8552
8553 when N_Entry_Body =>
8554 Op_Id := Defining_Identifier (Op_Body);
8555 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8556
8557 Insert_After (Current_Node, New_Op_Body);
8558 Current_Node := New_Op_Body;
8559 Analyze (New_Op_Body);
8560
8561 when N_Implicit_Label_Declaration =>
8562 null;
8563
8564 when N_Itype_Reference =>
8565 Insert_After (Current_Node, New_Copy (Op_Body));
8566
8567 when N_Freeze_Entity =>
8568 New_Op_Body := New_Copy (Op_Body);
8569
8570 if Present (Entity (Op_Body))
8571 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8572 then
8573 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8574 end if;
8575
8576 Insert_After (Current_Node, New_Op_Body);
8577 Current_Node := New_Op_Body;
8578 Analyze (New_Op_Body);
8579
8580 when N_Pragma =>
8581 New_Op_Body := New_Copy (Op_Body);
8582 Insert_After (Current_Node, New_Op_Body);
8583 Current_Node := New_Op_Body;
8584 Analyze (New_Op_Body);
8585
8586 when N_Object_Declaration =>
8587 pragma Assert (not Comes_From_Source (Op_Body));
8588 New_Op_Body := New_Copy (Op_Body);
8589 Insert_After (Current_Node, New_Op_Body);
8590 Current_Node := New_Op_Body;
8591 Analyze (New_Op_Body);
8592
8593 when others =>
8594 raise Program_Error;
8595 end case;
8596
8597 Next (Op_Body);
8598 end loop;
8599
8600 -- Finally, create the body of the function that maps an entry index
8601 -- into the corresponding body index, except when there is no entry, or
8602 -- in a Ravenscar-like profile.
8603
8604 if Corresponding_Runtime_Package (Pid) =
8605 System_Tasking_Protected_Objects_Entries
8606 then
8607 New_Op_Body := Build_Find_Body_Index (Pid);
8608 Insert_After (Current_Node, New_Op_Body);
8609 Current_Node := New_Op_Body;
8610 Analyze (New_Op_Body);
8611 end if;
8612
8613 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8614 -- protected body. At this point all wrapper specs have been created,
8615 -- frozen and included in the dispatch table for the protected type.
8616
8617 if Ada_Version >= Ada_2005 then
8618 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8619 end if;
8620 end Expand_N_Protected_Body;
8621
8622 -----------------------------------------
8623 -- Expand_N_Protected_Type_Declaration --
8624 -----------------------------------------
8625
8626 -- First we create a corresponding record type declaration used to
8627 -- represent values of this protected type.
8628 -- The general form of this type declaration is
8629
8630 -- type poV (discriminants) is record
8631 -- _Object : aliased <kind>Protection
8632 -- [(<entry count> [, <handler count>])];
8633 -- [entry_family : array (bounds) of Void;]
8634 -- <private data fields>
8635 -- end record;
8636
8637 -- The discriminants are present only if the corresponding protected type
8638 -- has discriminants, and they exactly mirror the protected type
8639 -- discriminants. The private data fields similarly mirror the private
8640 -- declarations of the protected type.
8641
8642 -- The Object field is always present. It contains RTS specific data used
8643 -- to control the protected object. It is declared as Aliased so that it
8644 -- can be passed as a pointer to the RTS. This allows the protected record
8645 -- to be referenced within RTS data structures. An appropriate Protection
8646 -- type and discriminant are generated.
8647
8648 -- The Service field is present for protected objects with entries. It
8649 -- contains sufficient information to allow the entry service procedure for
8650 -- this object to be called when the object is not known till runtime.
8651
8652 -- One entry_family component is present for each entry family in the
8653 -- task definition (see Expand_N_Task_Type_Declaration).
8654
8655 -- When a protected object is declared, an instance of the protected type
8656 -- value record is created. The elaboration of this declaration creates the
8657 -- correct bounds for the entry families, and also evaluates the priority
8658 -- expression if needed. The initialization routine for the protected type
8659 -- itself then calls Initialize_Protection with appropriate parameters to
8660 -- initialize the value of the Task_Id field. Install_Handlers may be also
8661 -- called if a pragma Attach_Handler applies.
8662
8663 -- Note: this record is passed to the subprograms created by the expansion
8664 -- of protected subprograms and entries. It is an in parameter to protected
8665 -- functions and an in out parameter to procedures and entry bodies. The
8666 -- Entity_Id for this created record type is placed in the
8667 -- Corresponding_Record_Type field of the associated protected type entity.
8668
8669 -- Next we create a procedure specifications for protected subprograms and
8670 -- entry bodies. For each protected subprograms two subprograms are
8671 -- created, an unprotected and a protected version. The unprotected version
8672 -- is called from within other operations of the same protected object.
8673
8674 -- We also build the call to register the procedure if a pragma
8675 -- Interrupt_Handler applies.
8676
8677 -- A single subprogram is created to service all entry bodies; it has an
8678 -- additional boolean out parameter indicating that the previous entry call
8679 -- made by the current task was serviced immediately, i.e. not by proxy.
8680 -- The O parameter contains a pointer to a record object of the type
8681 -- described above. An untyped interface is used here to allow this
8682 -- procedure to be called in places where the type of the object to be
8683 -- serviced is not known. This must be done, for example, when a call that
8684 -- may have been requeued is cancelled; the corresponding object must be
8685 -- serviced, but which object that is not known till runtime.
8686
8687 -- procedure ptypeS
8688 -- (O : System.Address; P : out Boolean);
8689 -- procedure pprocN (_object : in out poV);
8690 -- procedure pproc (_object : in out poV);
8691 -- function pfuncN (_object : poV);
8692 -- function pfunc (_object : poV);
8693 -- ...
8694
8695 -- Note that this must come after the record type declaration, since
8696 -- the specs refer to this type.
8697
8698 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8699 Discr_Map : constant Elist_Id := New_Elmt_List;
8700 Loc : constant Source_Ptr := Sloc (N);
8701 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8702
8703 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8704 -- This flag indicates whether the lock free implementation is active
8705
8706 Pdef : constant Node_Id := Protected_Definition (N);
8707 -- This contains two lists; one for visible and one for private decls
8708
8709 Current_Node : Node_Id := N;
8710 E_Count : Int;
8711 Entries_Aggr : Node_Id;
8712
8713 procedure Check_Inlining (Subp : Entity_Id);
8714 -- If the original operation has a pragma Inline, propagate the flag
8715 -- to the internal body, for possible inlining later on. The source
8716 -- operation is invisible to the back-end and is never actually called.
8717
8718 function Discriminated_Size (Comp : Entity_Id) return Boolean;
8719 -- If a component size is not static then a warning will be emitted
8720 -- in Ravenscar or other restricted contexts. When a component is non-
8721 -- static because of a discriminant constraint we can specialize the
8722 -- warning by mentioning discriminants explicitly.
8723
8724 procedure Expand_Entry_Declaration (Decl : Node_Id);
8725 -- Create the entry barrier and the procedure body for entry declaration
8726 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8727
8728 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8729 -- When compiling under the Ravenscar profile, private components must
8730 -- have a static size, or else a protected object will require heap
8731 -- allocation, violating the corresponding restriction. It is preferable
8732 -- to make this check here, because it provides a better error message
8733 -- than the back-end, which refers to the object as a whole.
8734
8735 procedure Register_Handler;
8736 -- For a protected operation that is an interrupt handler, add the
8737 -- freeze action that will register it as such.
8738
8739 --------------------
8740 -- Check_Inlining --
8741 --------------------
8742
8743 procedure Check_Inlining (Subp : Entity_Id) is
8744 begin
8745 if Is_Inlined (Subp) then
8746 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8747 Set_Is_Inlined (Subp, False);
8748 end if;
8749 end Check_Inlining;
8750
8751 ------------------------
8752 -- Discriminated_Size --
8753 ------------------------
8754
8755 function Discriminated_Size (Comp : Entity_Id) return Boolean is
8756 Typ : constant Entity_Id := Etype (Comp);
8757 Index : Node_Id;
8758
8759 function Non_Static_Bound (Bound : Node_Id) return Boolean;
8760 -- Check whether the bound of an index is non-static and does denote
8761 -- a discriminant, in which case any protected object of the type
8762 -- will have a non-static size.
8763
8764 ----------------------
8765 -- Non_Static_Bound --
8766 ----------------------
8767
8768 function Non_Static_Bound (Bound : Node_Id) return Boolean is
8769 begin
8770 if Is_OK_Static_Expression (Bound) then
8771 return False;
8772
8773 elsif Is_Entity_Name (Bound)
8774 and then Present (Discriminal_Link (Entity (Bound)))
8775 then
8776 return False;
8777
8778 else
8779 return True;
8780 end if;
8781 end Non_Static_Bound;
8782
8783 -- Start of processing for Discriminated_Size
8784
8785 begin
8786 if not Is_Array_Type (Typ) then
8787 return False;
8788 end if;
8789
8790 if Ekind (Typ) = E_Array_Subtype then
8791 Index := First_Index (Typ);
8792 while Present (Index) loop
8793 if Non_Static_Bound (Low_Bound (Index))
8794 or else Non_Static_Bound (High_Bound (Index))
8795 then
8796 return False;
8797 end if;
8798
8799 Next_Index (Index);
8800 end loop;
8801
8802 return True;
8803 end if;
8804
8805 return False;
8806 end Discriminated_Size;
8807
8808 ---------------------------
8809 -- Static_Component_Size --
8810 ---------------------------
8811
8812 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8813 Typ : constant Entity_Id := Etype (Comp);
8814 C : Entity_Id;
8815
8816 begin
8817 if Is_Scalar_Type (Typ) then
8818 return True;
8819
8820 elsif Is_Array_Type (Typ) then
8821 return Compile_Time_Known_Bounds (Typ);
8822
8823 elsif Is_Record_Type (Typ) then
8824 C := First_Component (Typ);
8825 while Present (C) loop
8826 if not Static_Component_Size (C) then
8827 return False;
8828 end if;
8829
8830 Next_Component (C);
8831 end loop;
8832
8833 return True;
8834
8835 -- Any other type will be checked by the back-end
8836
8837 else
8838 return True;
8839 end if;
8840 end Static_Component_Size;
8841
8842 ------------------------------
8843 -- Expand_Entry_Declaration --
8844 ------------------------------
8845
8846 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8847 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8848 Bar_Id : Entity_Id;
8849 Bod_Id : Entity_Id;
8850 Subp : Node_Id;
8851
8852 begin
8853 E_Count := E_Count + 1;
8854
8855 -- Create the protected body subprogram
8856
8857 Bod_Id :=
8858 Make_Defining_Identifier (Loc,
8859 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8860 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8861
8862 Subp :=
8863 Make_Subprogram_Declaration (Loc,
8864 Specification =>
8865 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8866
8867 Insert_After (Current_Node, Subp);
8868 Current_Node := Subp;
8869
8870 Analyze (Subp);
8871
8872 -- Build a wrapper procedure to handle contract cases, preconditions,
8873 -- and postconditions.
8874
8875 Build_Contract_Wrapper (Ent_Id, N);
8876
8877 -- Create the barrier function
8878
8879 Bar_Id :=
8880 Make_Defining_Identifier (Loc,
8881 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8882 Set_Barrier_Function (Ent_Id, Bar_Id);
8883
8884 Subp :=
8885 Make_Subprogram_Declaration (Loc,
8886 Specification =>
8887 Build_Barrier_Function_Specification (Loc, Bar_Id));
8888 Set_Is_Entry_Barrier_Function (Subp);
8889
8890 Insert_After (Current_Node, Subp);
8891 Current_Node := Subp;
8892
8893 Analyze (Subp);
8894
8895 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8896 Set_Scope (Bar_Id, Scope (Ent_Id));
8897
8898 -- Collect pointers to the protected subprogram and the barrier
8899 -- of the current entry, for insertion into Entry_Bodies_Array.
8900
8901 Append_To (Expressions (Entries_Aggr),
8902 Make_Aggregate (Loc,
8903 Expressions => New_List (
8904 Make_Attribute_Reference (Loc,
8905 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8906 Attribute_Name => Name_Unrestricted_Access),
8907 Make_Attribute_Reference (Loc,
8908 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8909 Attribute_Name => Name_Unrestricted_Access))));
8910 end Expand_Entry_Declaration;
8911
8912 ----------------------
8913 -- Register_Handler --
8914 ----------------------
8915
8916 procedure Register_Handler is
8917
8918 -- All semantic checks already done in Sem_Prag
8919
8920 Prot_Proc : constant Entity_Id :=
8921 Defining_Unit_Name (Specification (Current_Node));
8922
8923 Proc_Address : constant Node_Id :=
8924 Make_Attribute_Reference (Loc,
8925 Prefix =>
8926 New_Occurrence_Of (Prot_Proc, Loc),
8927 Attribute_Name => Name_Address);
8928
8929 RTS_Call : constant Entity_Id :=
8930 Make_Procedure_Call_Statement (Loc,
8931 Name =>
8932 New_Occurrence_Of
8933 (RTE (RE_Register_Interrupt_Handler), Loc),
8934 Parameter_Associations => New_List (Proc_Address));
8935 begin
8936 Append_Freeze_Action (Prot_Proc, RTS_Call);
8937 end Register_Handler;
8938
8939 -- Local variables
8940
8941 Body_Arr : Node_Id;
8942 Body_Id : Entity_Id;
8943 Cdecls : List_Id;
8944 Comp : Node_Id;
8945 Expr : Node_Id;
8946 New_Priv : Node_Id;
8947 Obj_Def : Node_Id;
8948 Object_Comp : Node_Id;
8949 Priv : Node_Id;
8950 Rec_Decl : Node_Id;
8951 Sub : Node_Id;
8952
8953 -- Start of processing for Expand_N_Protected_Type_Declaration
8954
8955 begin
8956 if Present (Corresponding_Record_Type (Prot_Typ)) then
8957 return;
8958 else
8959 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8960 end if;
8961
8962 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8963
8964 Qualify_Entity_Names (N);
8965
8966 -- If the type has discriminants, their occurrences in the declaration
8967 -- have been replaced by the corresponding discriminals. For components
8968 -- that are constrained by discriminants, their homologues in the
8969 -- corresponding record type must refer to the discriminants of that
8970 -- record, so we must apply a new renaming to subtypes_indications:
8971
8972 -- protected discriminant => discriminal => record discriminant
8973
8974 -- This replacement is not applied to default expressions, for which
8975 -- the discriminal is correct.
8976
8977 if Has_Discriminants (Prot_Typ) then
8978 declare
8979 Disc : Entity_Id;
8980 Decl : Node_Id;
8981
8982 begin
8983 Disc := First_Discriminant (Prot_Typ);
8984 Decl := First (Discriminant_Specifications (Rec_Decl));
8985 while Present (Disc) loop
8986 Append_Elmt (Discriminal (Disc), Discr_Map);
8987 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8988 Next_Discriminant (Disc);
8989 Next (Decl);
8990 end loop;
8991 end;
8992 end if;
8993
8994 -- Fill in the component declarations
8995
8996 -- Add components for entry families. For each entry family, create an
8997 -- anonymous type declaration with the same size, and analyze the type.
8998
8999 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9000
9001 pragma Assert (Present (Pdef));
9002
9003 Insert_After (Current_Node, Rec_Decl);
9004 Current_Node := Rec_Decl;
9005
9006 -- Add private field components
9007
9008 if Present (Private_Declarations (Pdef)) then
9009 Priv := First (Private_Declarations (Pdef));
9010 while Present (Priv) loop
9011 if Nkind (Priv) = N_Component_Declaration then
9012 if not Static_Component_Size (Defining_Identifier (Priv)) then
9013
9014 -- When compiling for a restricted profile, the private
9015 -- components must have a static size. If not, this is an
9016 -- error for a single protected declaration, and rates a
9017 -- warning on a protected type declaration.
9018
9019 if not Comes_From_Source (Prot_Typ) then
9020
9021 -- It's ok to be checking this restriction at expansion
9022 -- time, because this is only for the restricted profile,
9023 -- which is not subject to strict RM conformance, so it
9024 -- is OK to miss this check in -gnatc mode.
9025
9026 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9027 Check_Restriction
9028 (No_Implicit_Protected_Object_Allocations, Priv);
9029
9030 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9031 if not Discriminated_Size (Defining_Identifier (Priv))
9032 then
9033 -- Any object of the type will be non-static
9034
9035 Error_Msg_N ("component has non-static size??", Priv);
9036 Error_Msg_NE
9037 ("\creation of protected object of type& will "
9038 & "violate restriction "
9039 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9040 else
9041 -- Object will be non-static if discriminants are
9042
9043 Error_Msg_NE
9044 ("creation of protected object of type& with "
9045 & "non-static discriminants will violate "
9046 & "restriction No_Implicit_Heap_Allocations??",
9047 Priv, Prot_Typ);
9048 end if;
9049
9050 -- Likewise for No_Implicit_Protected_Object_Allocations
9051
9052 elsif Restriction_Active
9053 (No_Implicit_Protected_Object_Allocations)
9054 then
9055 if not Discriminated_Size (Defining_Identifier (Priv))
9056 then
9057 -- Any object of the type will be non-static
9058
9059 Error_Msg_N ("component has non-static size??", Priv);
9060 Error_Msg_NE
9061 ("\creation of protected object of type& will "
9062 & "violate restriction "
9063 & "No_Implicit_Protected_Object_Allocations??",
9064 Priv, Prot_Typ);
9065 else
9066 -- Object will be non-static if discriminants are
9067
9068 Error_Msg_NE
9069 ("creation of protected object of type& with "
9070 & "non-static discriminants will violate "
9071 & "restriction "
9072 & "No_Implicit_Protected_Object_Allocations??",
9073 Priv, Prot_Typ);
9074 end if;
9075 end if;
9076 end if;
9077
9078 -- The component definition consists of a subtype indication,
9079 -- or (in Ada 2005) an access definition. Make a copy of the
9080 -- proper definition.
9081
9082 declare
9083 Old_Comp : constant Node_Id := Component_Definition (Priv);
9084 Oent : constant Entity_Id := Defining_Identifier (Priv);
9085 Nent : constant Entity_Id :=
9086 Make_Defining_Identifier (Sloc (Oent),
9087 Chars => Chars (Oent));
9088 New_Comp : Node_Id;
9089
9090 begin
9091 if Present (Subtype_Indication (Old_Comp)) then
9092 New_Comp :=
9093 Make_Component_Definition (Sloc (Oent),
9094 Aliased_Present => False,
9095 Subtype_Indication =>
9096 New_Copy_Tree
9097 (Subtype_Indication (Old_Comp), Discr_Map));
9098 else
9099 New_Comp :=
9100 Make_Component_Definition (Sloc (Oent),
9101 Aliased_Present => False,
9102 Access_Definition =>
9103 New_Copy_Tree
9104 (Access_Definition (Old_Comp), Discr_Map));
9105 end if;
9106
9107 New_Priv :=
9108 Make_Component_Declaration (Loc,
9109 Defining_Identifier => Nent,
9110 Component_Definition => New_Comp,
9111 Expression => Expression (Priv));
9112
9113 Set_Has_Per_Object_Constraint (Nent,
9114 Has_Per_Object_Constraint (Oent));
9115
9116 Append_To (Cdecls, New_Priv);
9117 end;
9118
9119 elsif Nkind (Priv) = N_Subprogram_Declaration then
9120
9121 -- Make the unprotected version of the subprogram available
9122 -- for expansion of intra object calls. There is need for
9123 -- a protected version only if the subprogram is an interrupt
9124 -- handler, otherwise this operation can only be called from
9125 -- within the body.
9126
9127 Sub :=
9128 Make_Subprogram_Declaration (Loc,
9129 Specification =>
9130 Build_Protected_Sub_Specification
9131 (Priv, Prot_Typ, Unprotected_Mode));
9132
9133 Insert_After (Current_Node, Sub);
9134 Analyze (Sub);
9135
9136 Set_Protected_Body_Subprogram
9137 (Defining_Unit_Name (Specification (Priv)),
9138 Defining_Unit_Name (Specification (Sub)));
9139 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9140 Current_Node := Sub;
9141
9142 Sub :=
9143 Make_Subprogram_Declaration (Loc,
9144 Specification =>
9145 Build_Protected_Sub_Specification
9146 (Priv, Prot_Typ, Protected_Mode));
9147
9148 Insert_After (Current_Node, Sub);
9149 Analyze (Sub);
9150 Current_Node := Sub;
9151
9152 if Is_Interrupt_Handler
9153 (Defining_Unit_Name (Specification (Priv)))
9154 then
9155 if not Restricted_Profile then
9156 Register_Handler;
9157 end if;
9158 end if;
9159 end if;
9160
9161 Next (Priv);
9162 end loop;
9163 end if;
9164
9165 -- Except for the lock-free implementation, append the _Object field
9166 -- with the right type to the component list. We need to compute the
9167 -- number of entries, and in some cases the number of Attach_Handler
9168 -- pragmas.
9169
9170 if not Lock_Free_Active then
9171 declare
9172 Entry_Count_Expr : constant Node_Id :=
9173 Build_Entry_Count_Expression
9174 (Prot_Typ, Cdecls, Loc);
9175 Num_Attach_Handler : Nat := 0;
9176 Protection_Subtype : Node_Id;
9177 Ritem : Node_Id;
9178
9179 begin
9180 if Has_Attach_Handler (Prot_Typ) then
9181 Ritem := First_Rep_Item (Prot_Typ);
9182 while Present (Ritem) loop
9183 if Nkind (Ritem) = N_Pragma
9184 and then Pragma_Name (Ritem) = Name_Attach_Handler
9185 then
9186 Num_Attach_Handler := Num_Attach_Handler + 1;
9187 end if;
9188
9189 Next_Rep_Item (Ritem);
9190 end loop;
9191 end if;
9192
9193 -- Determine the proper protection type. There are two special
9194 -- cases: 1) when the protected type has dynamic interrupt
9195 -- handlers, and 2) when it has static handlers and we use a
9196 -- restricted profile.
9197
9198 if Has_Attach_Handler (Prot_Typ)
9199 and then not Restricted_Profile
9200 then
9201 Protection_Subtype :=
9202 Make_Subtype_Indication (Loc,
9203 Subtype_Mark =>
9204 New_Occurrence_Of
9205 (RTE (RE_Static_Interrupt_Protection), Loc),
9206 Constraint =>
9207 Make_Index_Or_Discriminant_Constraint (Loc,
9208 Constraints => New_List (
9209 Entry_Count_Expr,
9210 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9211
9212 elsif Has_Interrupt_Handler (Prot_Typ)
9213 and then not Restriction_Active (No_Dynamic_Attachment)
9214 then
9215 Protection_Subtype :=
9216 Make_Subtype_Indication (Loc,
9217 Subtype_Mark =>
9218 New_Occurrence_Of
9219 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9220 Constraint =>
9221 Make_Index_Or_Discriminant_Constraint (Loc,
9222 Constraints => New_List (Entry_Count_Expr)));
9223
9224 else
9225 case Corresponding_Runtime_Package (Prot_Typ) is
9226 when System_Tasking_Protected_Objects_Entries =>
9227 Protection_Subtype :=
9228 Make_Subtype_Indication (Loc,
9229 Subtype_Mark =>
9230 New_Occurrence_Of
9231 (RTE (RE_Protection_Entries), Loc),
9232 Constraint =>
9233 Make_Index_Or_Discriminant_Constraint (Loc,
9234 Constraints => New_List (Entry_Count_Expr)));
9235
9236 when System_Tasking_Protected_Objects_Single_Entry =>
9237 Protection_Subtype :=
9238 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9239
9240 when System_Tasking_Protected_Objects =>
9241 Protection_Subtype :=
9242 New_Occurrence_Of (RTE (RE_Protection), Loc);
9243
9244 when others =>
9245 raise Program_Error;
9246 end case;
9247 end if;
9248
9249 Object_Comp :=
9250 Make_Component_Declaration (Loc,
9251 Defining_Identifier =>
9252 Make_Defining_Identifier (Loc, Name_uObject),
9253 Component_Definition =>
9254 Make_Component_Definition (Loc,
9255 Aliased_Present => True,
9256 Subtype_Indication => Protection_Subtype));
9257 end;
9258
9259 -- Put the _Object component after the private component so that it
9260 -- be finalized early as required by 9.4 (20)
9261
9262 Append_To (Cdecls, Object_Comp);
9263 end if;
9264
9265 -- Analyze the record declaration immediately after construction,
9266 -- because the initialization procedure is needed for single object
9267 -- declarations before the next entity is analyzed (the freeze call
9268 -- that generates this initialization procedure is found below).
9269
9270 Analyze (Rec_Decl, Suppress => All_Checks);
9271
9272 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9273 -- the corresponding record is frozen. If any wrappers are generated,
9274 -- Current_Node is updated accordingly.
9275
9276 if Ada_Version >= Ada_2005 then
9277 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9278 end if;
9279
9280 -- Collect pointers to entry bodies and their barriers, to be placed
9281 -- in the Entry_Bodies_Array for the type. For each entry/family we
9282 -- add an expression to the aggregate which is the initial value of
9283 -- this array. The array is declared after all protected subprograms.
9284
9285 if Has_Entries (Prot_Typ) then
9286 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9287 else
9288 Entries_Aggr := Empty;
9289 end if;
9290
9291 -- Build two new procedure specifications for each protected subprogram;
9292 -- one to call from outside the object and one to call from inside.
9293 -- Build a barrier function and an entry body action procedure
9294 -- specification for each protected entry. Initialize the entry body
9295 -- array. If subprogram is flagged as eliminated, do not generate any
9296 -- internal operations.
9297
9298 E_Count := 0;
9299 Comp := First (Visible_Declarations (Pdef));
9300 while Present (Comp) loop
9301 if Nkind (Comp) = N_Subprogram_Declaration then
9302 Sub :=
9303 Make_Subprogram_Declaration (Loc,
9304 Specification =>
9305 Build_Protected_Sub_Specification
9306 (Comp, Prot_Typ, Unprotected_Mode));
9307
9308 Insert_After (Current_Node, Sub);
9309 Analyze (Sub);
9310
9311 Set_Protected_Body_Subprogram
9312 (Defining_Unit_Name (Specification (Comp)),
9313 Defining_Unit_Name (Specification (Sub)));
9314 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9315
9316 -- Make the protected version of the subprogram available for
9317 -- expansion of external calls.
9318
9319 Current_Node := Sub;
9320
9321 Sub :=
9322 Make_Subprogram_Declaration (Loc,
9323 Specification =>
9324 Build_Protected_Sub_Specification
9325 (Comp, Prot_Typ, Protected_Mode));
9326
9327 Insert_After (Current_Node, Sub);
9328 Analyze (Sub);
9329
9330 Current_Node := Sub;
9331
9332 -- Generate an overriding primitive operation specification for
9333 -- this subprogram if the protected type implements an interface
9334 -- and Build_Wrapper_Spec did not generate its wrapper.
9335
9336 if Ada_Version >= Ada_2005
9337 and then
9338 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9339 then
9340 declare
9341 Found : Boolean := False;
9342 Prim_Elmt : Elmt_Id;
9343 Prim_Op : Node_Id;
9344
9345 begin
9346 Prim_Elmt :=
9347 First_Elmt
9348 (Primitive_Operations
9349 (Corresponding_Record_Type (Prot_Typ)));
9350
9351 while Present (Prim_Elmt) loop
9352 Prim_Op := Node (Prim_Elmt);
9353
9354 if Is_Primitive_Wrapper (Prim_Op)
9355 and then Wrapped_Entity (Prim_Op) =
9356 Defining_Entity (Specification (Comp))
9357 then
9358 Found := True;
9359 exit;
9360 end if;
9361
9362 Next_Elmt (Prim_Elmt);
9363 end loop;
9364
9365 if not Found then
9366 Sub :=
9367 Make_Subprogram_Declaration (Loc,
9368 Specification =>
9369 Build_Protected_Sub_Specification
9370 (Comp, Prot_Typ, Dispatching_Mode));
9371
9372 Insert_After (Current_Node, Sub);
9373 Analyze (Sub);
9374
9375 Current_Node := Sub;
9376 end if;
9377 end;
9378 end if;
9379
9380 -- If a pragma Interrupt_Handler applies, build and add a call to
9381 -- Register_Interrupt_Handler to the freezing actions of the
9382 -- protected version (Current_Node) of the subprogram:
9383
9384 -- system.interrupts.register_interrupt_handler
9385 -- (prot_procP'address);
9386
9387 if not Restricted_Profile
9388 and then Is_Interrupt_Handler
9389 (Defining_Unit_Name (Specification (Comp)))
9390 then
9391 Register_Handler;
9392 end if;
9393
9394 elsif Nkind (Comp) = N_Entry_Declaration then
9395 Expand_Entry_Declaration (Comp);
9396 end if;
9397
9398 Next (Comp);
9399 end loop;
9400
9401 -- If there are some private entry declarations, expand it as if they
9402 -- were visible entries.
9403
9404 if Present (Private_Declarations (Pdef)) then
9405 Comp := First (Private_Declarations (Pdef));
9406 while Present (Comp) loop
9407 if Nkind (Comp) = N_Entry_Declaration then
9408 Expand_Entry_Declaration (Comp);
9409 end if;
9410
9411 Next (Comp);
9412 end loop;
9413 end if;
9414
9415 -- Create the declaration of an array object which contains the values
9416 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9417 -- type. This object is later passed to the appropriate protected object
9418 -- initialization routine.
9419
9420 if Has_Entries (Prot_Typ)
9421 and then Corresponding_Runtime_Package (Prot_Typ) =
9422 System_Tasking_Protected_Objects_Entries
9423 then
9424 declare
9425 Count : Int;
9426 Item : Entity_Id;
9427 Max_Vals : Node_Id;
9428 Maxes : List_Id;
9429 Maxes_Id : Entity_Id;
9430 Need_Array : Boolean := False;
9431
9432 begin
9433 -- First check if there is any Max_Queue_Length pragma
9434
9435 Item := First_Entity (Prot_Typ);
9436 while Present (Item) loop
9437 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9438 Need_Array := True;
9439 exit;
9440 end if;
9441
9442 Next_Entity (Item);
9443 end loop;
9444
9445 -- Gather the Max_Queue_Length values of all entries in a list. A
9446 -- value of zero indicates that the entry has no limitation on its
9447 -- queue length.
9448
9449 if Need_Array then
9450 Count := 0;
9451 Item := First_Entity (Prot_Typ);
9452 Maxes := New_List;
9453 while Present (Item) loop
9454 if Is_Entry (Item) then
9455 Count := Count + 1;
9456 Append_To (Maxes,
9457 Make_Integer_Literal
9458 (Loc, Get_Max_Queue_Length (Item)));
9459 end if;
9460
9461 Next_Entity (Item);
9462 end loop;
9463
9464 -- Create the declaration of the array object. Generate:
9465
9466 -- Maxes_Id : aliased constant
9467 -- Protected_Entry_Queue_Max_Array
9468 -- (1 .. Count) := (..., ...);
9469
9470 Maxes_Id :=
9471 Make_Defining_Identifier (Loc,
9472 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9473
9474 Max_Vals :=
9475 Make_Object_Declaration (Loc,
9476 Defining_Identifier => Maxes_Id,
9477 Aliased_Present => True,
9478 Constant_Present => True,
9479 Object_Definition =>
9480 Make_Subtype_Indication (Loc,
9481 Subtype_Mark =>
9482 New_Occurrence_Of
9483 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9484 Constraint =>
9485 Make_Index_Or_Discriminant_Constraint (Loc,
9486 Constraints => New_List (
9487 Make_Range (Loc,
9488 Make_Integer_Literal (Loc, 1),
9489 Make_Integer_Literal (Loc, Count))))),
9490 Expression => Make_Aggregate (Loc, Maxes));
9491
9492 -- A pointer to this array will be placed in the corresponding
9493 -- record by its initialization procedure so this needs to be
9494 -- analyzed here.
9495
9496 Insert_After (Current_Node, Max_Vals);
9497 Current_Node := Max_Vals;
9498 Analyze (Max_Vals);
9499
9500 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9501 end if;
9502 end;
9503 end if;
9504
9505 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9506 -- all protected subprograms have been collected.
9507
9508 if Has_Entries (Prot_Typ) then
9509 Body_Id :=
9510 Make_Defining_Identifier (Sloc (Prot_Typ),
9511 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9512
9513 case Corresponding_Runtime_Package (Prot_Typ) is
9514 when System_Tasking_Protected_Objects_Entries =>
9515 Expr := Entries_Aggr;
9516 Obj_Def :=
9517 Make_Subtype_Indication (Loc,
9518 Subtype_Mark =>
9519 New_Occurrence_Of
9520 (RTE (RE_Protected_Entry_Body_Array), Loc),
9521 Constraint =>
9522 Make_Index_Or_Discriminant_Constraint (Loc,
9523 Constraints => New_List (
9524 Make_Range (Loc,
9525 Make_Integer_Literal (Loc, 1),
9526 Make_Integer_Literal (Loc, E_Count)))));
9527
9528 when System_Tasking_Protected_Objects_Single_Entry =>
9529 Expr := Remove_Head (Expressions (Entries_Aggr));
9530 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9531
9532 when others =>
9533 raise Program_Error;
9534 end case;
9535
9536 Body_Arr :=
9537 Make_Object_Declaration (Loc,
9538 Defining_Identifier => Body_Id,
9539 Aliased_Present => True,
9540 Constant_Present => True,
9541 Object_Definition => Obj_Def,
9542 Expression => Expr);
9543
9544 -- A pointer to this array will be placed in the corresponding record
9545 -- by its initialization procedure so this needs to be analyzed here.
9546
9547 Insert_After (Current_Node, Body_Arr);
9548 Current_Node := Body_Arr;
9549 Analyze (Body_Arr);
9550
9551 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9552
9553 -- Finally, build the function that maps an entry index into the
9554 -- corresponding body. A pointer to this function is placed in each
9555 -- object of the type. Except for a ravenscar-like profile (no abort,
9556 -- no entry queue, 1 entry)
9557
9558 if Corresponding_Runtime_Package (Prot_Typ) =
9559 System_Tasking_Protected_Objects_Entries
9560 then
9561 Sub :=
9562 Make_Subprogram_Declaration (Loc,
9563 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9564
9565 Insert_After (Current_Node, Sub);
9566 Analyze (Sub);
9567 end if;
9568 end if;
9569 end Expand_N_Protected_Type_Declaration;
9570
9571 --------------------------------
9572 -- Expand_N_Requeue_Statement --
9573 --------------------------------
9574
9575 -- A nondispatching requeue statement is expanded into one of four GNARLI
9576 -- operations, depending on the source and destination (task or protected
9577 -- object). A dispatching requeue statement is expanded into a call to the
9578 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9579 -- jump around the remainder of processing for the original entry and, if
9580 -- the destination is (different) protected object, to attempt to service
9581 -- it. The following illustrates the various cases:
9582
9583 -- procedure entE
9584 -- (O : System.Address;
9585 -- P : System.Address;
9586 -- E : Protected_Entry_Index)
9587 -- is
9588 -- <discriminant renamings>
9589 -- <private object renamings>
9590 -- type poVP is access poV;
9591 -- _object : ptVP := ptVP!(O);
9592
9593 -- begin
9594 -- begin
9595 -- <start of statement sequence for entry>
9596
9597 -- -- Requeue from one protected entry body to another protected
9598 -- -- entry.
9599
9600 -- Requeue_Protected_Entry (
9601 -- _object._object'Access,
9602 -- new._object'Access,
9603 -- E,
9604 -- Abort_Present);
9605 -- return;
9606
9607 -- <some more of the statement sequence for entry>
9608
9609 -- -- Requeue from an entry body to a task entry
9610
9611 -- Requeue_Protected_To_Task_Entry (
9612 -- New._task_id,
9613 -- E,
9614 -- Abort_Present);
9615 -- return;
9616
9617 -- <rest of statement sequence for entry>
9618 -- Complete_Entry_Body (_object._object);
9619
9620 -- exception
9621 -- when all others =>
9622 -- Exceptional_Complete_Entry_Body (
9623 -- _object._object, Get_GNAT_Exception);
9624 -- end;
9625 -- end entE;
9626
9627 -- Requeue of a task entry call to a task entry
9628
9629 -- Accept_Call (E, Ann);
9630 -- <start of statement sequence for accept statement>
9631 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9632 -- goto Lnn;
9633 -- <rest of statement sequence for accept statement>
9634 -- <<Lnn>>
9635 -- Complete_Rendezvous;
9636
9637 -- exception
9638 -- when all others =>
9639 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9640
9641 -- Requeue of a task entry call to a protected entry
9642
9643 -- Accept_Call (E, Ann);
9644 -- <start of statement sequence for accept statement>
9645 -- Requeue_Task_To_Protected_Entry (
9646 -- new._object'Access,
9647 -- E,
9648 -- Abort_Present);
9649 -- newS (new, Pnn);
9650 -- goto Lnn;
9651 -- <rest of statement sequence for accept statement>
9652 -- <<Lnn>>
9653 -- Complete_Rendezvous;
9654
9655 -- exception
9656 -- when all others =>
9657 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9658
9659 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9660 -- marked by pragma Implemented (XXX, By_Entry).
9661
9662 -- The requeue is inside a protected entry:
9663
9664 -- procedure entE
9665 -- (O : System.Address;
9666 -- P : System.Address;
9667 -- E : Protected_Entry_Index)
9668 -- is
9669 -- <discriminant renamings>
9670 -- <private object renamings>
9671 -- type poVP is access poV;
9672 -- _object : ptVP := ptVP!(O);
9673
9674 -- begin
9675 -- begin
9676 -- <start of statement sequence for entry>
9677
9678 -- _Disp_Requeue
9679 -- (<interface class-wide object>,
9680 -- True,
9681 -- _object'Address,
9682 -- Ada.Tags.Get_Offset_Index
9683 -- (Tag (_object),
9684 -- <interface dispatch table index of target entry>),
9685 -- Abort_Present);
9686 -- return;
9687
9688 -- <rest of statement sequence for entry>
9689 -- Complete_Entry_Body (_object._object);
9690
9691 -- exception
9692 -- when all others =>
9693 -- Exceptional_Complete_Entry_Body (
9694 -- _object._object, Get_GNAT_Exception);
9695 -- end;
9696 -- end entE;
9697
9698 -- The requeue is inside a task entry:
9699
9700 -- Accept_Call (E, Ann);
9701 -- <start of statement sequence for accept statement>
9702 -- _Disp_Requeue
9703 -- (<interface class-wide object>,
9704 -- False,
9705 -- null,
9706 -- Ada.Tags.Get_Offset_Index
9707 -- (Tag (_object),
9708 -- <interface dispatch table index of target entrt>),
9709 -- Abort_Present);
9710 -- newS (new, Pnn);
9711 -- goto Lnn;
9712 -- <rest of statement sequence for accept statement>
9713 -- <<Lnn>>
9714 -- Complete_Rendezvous;
9715
9716 -- exception
9717 -- when all others =>
9718 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9719
9720 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9721 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9722 -- statement is replaced by a dispatching call with actual parameters taken
9723 -- from the inner-most accept statement or entry body.
9724
9725 -- Target.Primitive (Param1, ..., ParamN);
9726
9727 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9728 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9729 -- at all.
9730
9731 -- declare
9732 -- S : constant Offset_Index :=
9733 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9734 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9735
9736 -- begin
9737 -- if C = POK_Protected_Entry
9738 -- or else C = POK_Task_Entry
9739 -- then
9740 -- <statements for dispatching requeue>
9741
9742 -- elsif C = POK_Protected_Procedure then
9743 -- <dispatching call equivalent>
9744
9745 -- else
9746 -- raise Program_Error;
9747 -- end if;
9748 -- end;
9749
9750 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9751 Loc : constant Source_Ptr := Sloc (N);
9752 Conc_Typ : Entity_Id;
9753 Concval : Node_Id;
9754 Ename : Node_Id;
9755 Index : Node_Id;
9756 Old_Typ : Entity_Id;
9757
9758 function Build_Dispatching_Call_Equivalent return Node_Id;
9759 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9760 -- the form Concval.Ename. It is statically known that Ename is allowed
9761 -- to be implemented by a protected procedure. Create a dispatching call
9762 -- equivalent of Concval.Ename taking the actual parameters from the
9763 -- inner-most accept statement or entry body.
9764
9765 function Build_Dispatching_Requeue return Node_Id;
9766 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9767 -- the form Concval.Ename. It is statically known that Ename is allowed
9768 -- to be implemented by a protected or a task entry. Create a call to
9769 -- primitive _Disp_Requeue which handles the low-level actions.
9770
9771 function Build_Dispatching_Requeue_To_Any return Node_Id;
9772 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9773 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9774 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9775 -- determines at runtime whether Ename denotes an entry or a procedure
9776 -- and perform the appropriate kind of dispatching select.
9777
9778 function Build_Normal_Requeue return Node_Id;
9779 -- N denotes a nondispatching requeue statement to either a task or a
9780 -- protected entry. Build the appropriate runtime call to perform the
9781 -- action.
9782
9783 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9784 -- For a protected entry, create a return statement to skip the rest of
9785 -- the entry body. Otherwise, create a goto statement to skip the rest
9786 -- of a task accept statement. The lookup for the enclosing entry body
9787 -- or accept statement starts from Search.
9788
9789 ---------------------------------------
9790 -- Build_Dispatching_Call_Equivalent --
9791 ---------------------------------------
9792
9793 function Build_Dispatching_Call_Equivalent return Node_Id is
9794 Call_Ent : constant Entity_Id := Entity (Ename);
9795 Obj : constant Node_Id := Original_Node (Concval);
9796 Acc_Ent : Node_Id;
9797 Actuals : List_Id;
9798 Formal : Node_Id;
9799 Formals : List_Id;
9800
9801 begin
9802 -- Climb the parent chain looking for the inner-most entry body or
9803 -- accept statement.
9804
9805 Acc_Ent := N;
9806 while Present (Acc_Ent)
9807 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9808 N_Entry_Body)
9809 loop
9810 Acc_Ent := Parent (Acc_Ent);
9811 end loop;
9812
9813 -- A requeue statement should be housed inside an entry body or an
9814 -- accept statement at some level. If this is not the case, then the
9815 -- tree is malformed.
9816
9817 pragma Assert (Present (Acc_Ent));
9818
9819 -- Recover the list of formal parameters
9820
9821 if Nkind (Acc_Ent) = N_Entry_Body then
9822 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9823 end if;
9824
9825 Formals := Parameter_Specifications (Acc_Ent);
9826
9827 -- Create the actual parameters for the dispatching call. These are
9828 -- simply copies of the entry body or accept statement formals in the
9829 -- same order as they appear.
9830
9831 Actuals := No_List;
9832
9833 if Present (Formals) then
9834 Actuals := New_List;
9835 Formal := First (Formals);
9836 while Present (Formal) loop
9837 Append_To (Actuals,
9838 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9839 Next (Formal);
9840 end loop;
9841 end if;
9842
9843 -- Generate:
9844 -- Obj.Call_Ent (Actuals);
9845
9846 return
9847 Make_Procedure_Call_Statement (Loc,
9848 Name =>
9849 Make_Selected_Component (Loc,
9850 Prefix => Make_Identifier (Loc, Chars (Obj)),
9851 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9852
9853 Parameter_Associations => Actuals);
9854 end Build_Dispatching_Call_Equivalent;
9855
9856 -------------------------------
9857 -- Build_Dispatching_Requeue --
9858 -------------------------------
9859
9860 function Build_Dispatching_Requeue return Node_Id is
9861 Params : constant List_Id := New_List;
9862
9863 begin
9864 -- Process the "with abort" parameter
9865
9866 Prepend_To (Params,
9867 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9868
9869 -- Process the entry wrapper's position in the primary dispatch
9870 -- table parameter. Generate:
9871
9872 -- Ada.Tags.Get_Entry_Index
9873 -- (T => To_Tag_Ptr (Obj'Address).all,
9874 -- Position =>
9875 -- Ada.Tags.Get_Offset_Index
9876 -- (Ada.Tags.Tag (Concval),
9877 -- <interface dispatch table position of Ename>));
9878
9879 -- Note that Obj'Address is recursively expanded into a call to
9880 -- Base_Address (Obj).
9881
9882 if Tagged_Type_Expansion then
9883 Prepend_To (Params,
9884 Make_Function_Call (Loc,
9885 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9886 Parameter_Associations => New_List (
9887
9888 Make_Explicit_Dereference (Loc,
9889 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9890 Make_Attribute_Reference (Loc,
9891 Prefix => New_Copy_Tree (Concval),
9892 Attribute_Name => Name_Address))),
9893
9894 Make_Function_Call (Loc,
9895 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9896 Parameter_Associations => New_List (
9897 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9898 Make_Integer_Literal (Loc,
9899 DT_Position (Entity (Ename))))))));
9900
9901 -- VM targets
9902
9903 else
9904 Prepend_To (Params,
9905 Make_Function_Call (Loc,
9906 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9907 Parameter_Associations => New_List (
9908
9909 Make_Attribute_Reference (Loc,
9910 Prefix => Concval,
9911 Attribute_Name => Name_Tag),
9912
9913 Make_Function_Call (Loc,
9914 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9915
9916 Parameter_Associations => New_List (
9917
9918 -- Obj_Tag
9919
9920 Make_Attribute_Reference (Loc,
9921 Prefix => Concval,
9922 Attribute_Name => Name_Tag),
9923
9924 -- Tag_Typ
9925
9926 Make_Attribute_Reference (Loc,
9927 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9928 Attribute_Name => Name_Tag),
9929
9930 -- Position
9931
9932 Make_Integer_Literal (Loc,
9933 DT_Position (Entity (Ename))))))));
9934 end if;
9935
9936 -- Specific actuals for protected to XXX requeue
9937
9938 if Is_Protected_Type (Old_Typ) then
9939 Prepend_To (Params,
9940 Make_Attribute_Reference (Loc, -- _object'Address
9941 Prefix =>
9942 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9943 Attribute_Name => Name_Address));
9944
9945 Prepend_To (Params, -- True
9946 New_Occurrence_Of (Standard_True, Loc));
9947
9948 -- Specific actuals for task to XXX requeue
9949
9950 else
9951 pragma Assert (Is_Task_Type (Old_Typ));
9952
9953 Prepend_To (Params, -- null
9954 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
9955
9956 Prepend_To (Params, -- False
9957 New_Occurrence_Of (Standard_False, Loc));
9958 end if;
9959
9960 -- Add the object parameter
9961
9962 Prepend_To (Params, New_Copy_Tree (Concval));
9963
9964 -- Generate:
9965 -- _Disp_Requeue (<Params>);
9966
9967 -- Find entity for Disp_Requeue operation, which belongs to
9968 -- the type and may not be directly visible.
9969
9970 declare
9971 Elmt : Elmt_Id;
9972 Op : Entity_Id;
9973
9974 begin
9975 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9976 while Present (Elmt) loop
9977 Op := Node (Elmt);
9978 exit when Chars (Op) = Name_uDisp_Requeue;
9979 Next_Elmt (Elmt);
9980 end loop;
9981
9982 return
9983 Make_Procedure_Call_Statement (Loc,
9984 Name => New_Occurrence_Of (Op, Loc),
9985 Parameter_Associations => Params);
9986 end;
9987 end Build_Dispatching_Requeue;
9988
9989 --------------------------------------
9990 -- Build_Dispatching_Requeue_To_Any --
9991 --------------------------------------
9992
9993 function Build_Dispatching_Requeue_To_Any return Node_Id is
9994 Call_Ent : constant Entity_Id := Entity (Ename);
9995 Obj : constant Node_Id := Original_Node (Concval);
9996 Skip : constant Node_Id := Build_Skip_Statement (N);
9997 C : Entity_Id;
9998 Decls : List_Id;
9999 S : Entity_Id;
10000 Stmts : List_Id;
10001
10002 begin
10003 Decls := New_List;
10004 Stmts := New_List;
10005
10006 -- Dispatch table slot processing, generate:
10007 -- S : Integer;
10008
10009 S := Build_S (Loc, Decls);
10010
10011 -- Call kind processing, generate:
10012 -- C : Ada.Tags.Prim_Op_Kind;
10013
10014 C := Build_C (Loc, Decls);
10015
10016 -- Generate:
10017 -- S := Ada.Tags.Get_Offset_Index
10018 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10019
10020 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10021
10022 -- Generate:
10023 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10024
10025 Append_To (Stmts,
10026 Make_Procedure_Call_Statement (Loc,
10027 Name =>
10028 New_Occurrence_Of (
10029 Find_Prim_Op (Etype (Etype (Obj)),
10030 Name_uDisp_Get_Prim_Op_Kind),
10031 Loc),
10032 Parameter_Associations => New_List (
10033 New_Copy_Tree (Obj),
10034 New_Occurrence_Of (S, Loc),
10035 New_Occurrence_Of (C, Loc))));
10036
10037 Append_To (Stmts,
10038
10039 -- if C = POK_Protected_Entry
10040 -- or else C = POK_Task_Entry
10041 -- then
10042
10043 Make_Implicit_If_Statement (N,
10044 Condition =>
10045 Make_Op_Or (Loc,
10046 Left_Opnd =>
10047 Make_Op_Eq (Loc,
10048 Left_Opnd =>
10049 New_Occurrence_Of (C, Loc),
10050 Right_Opnd =>
10051 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10052
10053 Right_Opnd =>
10054 Make_Op_Eq (Loc,
10055 Left_Opnd =>
10056 New_Occurrence_Of (C, Loc),
10057 Right_Opnd =>
10058 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10059
10060 -- Dispatching requeue equivalent
10061
10062 Then_Statements => New_List (
10063 Build_Dispatching_Requeue,
10064 Skip),
10065
10066 -- elsif C = POK_Protected_Procedure then
10067
10068 Elsif_Parts => New_List (
10069 Make_Elsif_Part (Loc,
10070 Condition =>
10071 Make_Op_Eq (Loc,
10072 Left_Opnd =>
10073 New_Occurrence_Of (C, Loc),
10074 Right_Opnd =>
10075 New_Occurrence_Of (
10076 RTE (RE_POK_Protected_Procedure), Loc)),
10077
10078 -- Dispatching call equivalent
10079
10080 Then_Statements => New_List (
10081 Build_Dispatching_Call_Equivalent))),
10082
10083 -- else
10084 -- raise Program_Error;
10085 -- end if;
10086
10087 Else_Statements => New_List (
10088 Make_Raise_Program_Error (Loc,
10089 Reason => PE_Explicit_Raise))));
10090
10091 -- Wrap everything into a block
10092
10093 return
10094 Make_Block_Statement (Loc,
10095 Declarations => Decls,
10096 Handled_Statement_Sequence =>
10097 Make_Handled_Sequence_Of_Statements (Loc,
10098 Statements => Stmts));
10099 end Build_Dispatching_Requeue_To_Any;
10100
10101 --------------------------
10102 -- Build_Normal_Requeue --
10103 --------------------------
10104
10105 function Build_Normal_Requeue return Node_Id is
10106 Params : constant List_Id := New_List;
10107 Param : Node_Id;
10108 RT_Call : Node_Id;
10109
10110 begin
10111 -- Process the "with abort" parameter
10112
10113 Prepend_To (Params,
10114 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10115
10116 -- Add the index expression to the parameters. It is common among all
10117 -- four cases.
10118
10119 Prepend_To (Params,
10120 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10121
10122 if Is_Protected_Type (Old_Typ) then
10123 declare
10124 Self_Param : Node_Id;
10125
10126 begin
10127 Self_Param :=
10128 Make_Attribute_Reference (Loc,
10129 Prefix =>
10130 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10131 Attribute_Name =>
10132 Name_Unchecked_Access);
10133
10134 -- Protected to protected requeue
10135
10136 if Is_Protected_Type (Conc_Typ) then
10137 RT_Call :=
10138 New_Occurrence_Of (
10139 RTE (RE_Requeue_Protected_Entry), Loc);
10140
10141 Param :=
10142 Make_Attribute_Reference (Loc,
10143 Prefix =>
10144 Concurrent_Ref (Concval),
10145 Attribute_Name =>
10146 Name_Unchecked_Access);
10147
10148 -- Protected to task requeue
10149
10150 else pragma Assert (Is_Task_Type (Conc_Typ));
10151 RT_Call :=
10152 New_Occurrence_Of (
10153 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10154
10155 Param := Concurrent_Ref (Concval);
10156 end if;
10157
10158 Prepend_To (Params, Param);
10159 Prepend_To (Params, Self_Param);
10160 end;
10161
10162 else pragma Assert (Is_Task_Type (Old_Typ));
10163
10164 -- Task to protected requeue
10165
10166 if Is_Protected_Type (Conc_Typ) then
10167 RT_Call :=
10168 New_Occurrence_Of (
10169 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10170
10171 Param :=
10172 Make_Attribute_Reference (Loc,
10173 Prefix =>
10174 Concurrent_Ref (Concval),
10175 Attribute_Name =>
10176 Name_Unchecked_Access);
10177
10178 -- Task to task requeue
10179
10180 else pragma Assert (Is_Task_Type (Conc_Typ));
10181 RT_Call :=
10182 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10183
10184 Param := Concurrent_Ref (Concval);
10185 end if;
10186
10187 Prepend_To (Params, Param);
10188 end if;
10189
10190 return
10191 Make_Procedure_Call_Statement (Loc,
10192 Name => RT_Call,
10193 Parameter_Associations => Params);
10194 end Build_Normal_Requeue;
10195
10196 --------------------------
10197 -- Build_Skip_Statement --
10198 --------------------------
10199
10200 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10201 Skip_Stmt : Node_Id;
10202
10203 begin
10204 -- Build a return statement to skip the rest of the entire body
10205
10206 if Is_Protected_Type (Old_Typ) then
10207 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10208
10209 -- If the requeue is within a task, find the end label of the
10210 -- enclosing accept statement and create a goto statement to it.
10211
10212 else
10213 declare
10214 Acc : Node_Id;
10215 Label : Node_Id;
10216
10217 begin
10218 -- Climb the parent chain looking for the enclosing accept
10219 -- statement.
10220
10221 Acc := Parent (Search);
10222 while Present (Acc)
10223 and then Nkind (Acc) /= N_Accept_Statement
10224 loop
10225 Acc := Parent (Acc);
10226 end loop;
10227
10228 -- The last statement is the second label used for completing
10229 -- the rendezvous the usual way. The label we are looking for
10230 -- is right before it.
10231
10232 Label :=
10233 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10234
10235 pragma Assert (Nkind (Label) = N_Label);
10236
10237 -- Generate a goto statement to skip the rest of the accept
10238
10239 Skip_Stmt :=
10240 Make_Goto_Statement (Loc,
10241 Name =>
10242 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10243 end;
10244 end if;
10245
10246 Set_Analyzed (Skip_Stmt);
10247
10248 return Skip_Stmt;
10249 end Build_Skip_Statement;
10250
10251 -- Start of processing for Expand_N_Requeue_Statement
10252
10253 begin
10254 -- Extract the components of the entry call
10255
10256 Extract_Entry (N, Concval, Ename, Index);
10257 Conc_Typ := Etype (Concval);
10258
10259 -- If the prefix is an access to class-wide type, dereference to get
10260 -- object and entry type.
10261
10262 if Is_Access_Type (Conc_Typ) then
10263 Conc_Typ := Designated_Type (Conc_Typ);
10264 Rewrite (Concval,
10265 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10266 Analyze_And_Resolve (Concval, Conc_Typ);
10267 end if;
10268
10269 -- Examine the scope stack in order to find nearest enclosing protected
10270 -- or task type. This will constitute our invocation source.
10271
10272 Old_Typ := Current_Scope;
10273 while Present (Old_Typ)
10274 and then not Is_Protected_Type (Old_Typ)
10275 and then not Is_Task_Type (Old_Typ)
10276 loop
10277 Old_Typ := Scope (Old_Typ);
10278 end loop;
10279
10280 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10281 -- Concval.Ename where the type of Concval is class-wide concurrent
10282 -- interface.
10283
10284 if Ada_Version >= Ada_2012
10285 and then Present (Concval)
10286 and then Is_Class_Wide_Type (Conc_Typ)
10287 and then Is_Concurrent_Interface (Conc_Typ)
10288 then
10289 declare
10290 Has_Impl : Boolean := False;
10291 Impl_Kind : Name_Id := No_Name;
10292
10293 begin
10294 -- Check whether the Ename is flagged by pragma Implemented
10295
10296 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10297 Has_Impl := True;
10298 Impl_Kind := Implementation_Kind (Entity (Ename));
10299 end if;
10300
10301 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10302 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10303
10304 if Has_Impl and then Impl_Kind = Name_By_Entry then
10305 Rewrite (N, Build_Dispatching_Requeue);
10306 Analyze (N);
10307 Insert_After (N, Build_Skip_Statement (N));
10308
10309 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10310 -- a protected procedure. In this case the requeue is transformed
10311 -- into a dispatching call.
10312
10313 elsif Has_Impl
10314 and then Impl_Kind = Name_By_Protected_Procedure
10315 then
10316 Rewrite (N, Build_Dispatching_Call_Equivalent);
10317 Analyze (N);
10318
10319 -- The procedure_or_entry_NAME's implementation kind is either
10320 -- By_Any, Optional, or pragma Implemented was not applied at all.
10321 -- In this case a runtime test determines whether Ename denotes an
10322 -- entry or a protected procedure and performs the appropriate
10323 -- call.
10324
10325 else
10326 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10327 Analyze (N);
10328 end if;
10329 end;
10330
10331 -- Processing for regular (nondispatching) requeues
10332
10333 else
10334 Rewrite (N, Build_Normal_Requeue);
10335 Analyze (N);
10336 Insert_After (N, Build_Skip_Statement (N));
10337 end if;
10338 end Expand_N_Requeue_Statement;
10339
10340 -------------------------------
10341 -- Expand_N_Selective_Accept --
10342 -------------------------------
10343
10344 procedure Expand_N_Selective_Accept (N : Node_Id) is
10345 Loc : constant Source_Ptr := Sloc (N);
10346 Alts : constant List_Id := Select_Alternatives (N);
10347
10348 -- Note: in the below declarations a lot of new lists are allocated
10349 -- unconditionally which may well not end up being used. That's not
10350 -- a good idea since it wastes space gratuitously ???
10351
10352 Accept_Case : List_Id;
10353 Accept_List : constant List_Id := New_List;
10354
10355 Alt : Node_Id;
10356 Alt_List : constant List_Id := New_List;
10357 Alt_Stats : List_Id;
10358 Ann : Entity_Id := Empty;
10359
10360 Check_Guard : Boolean := True;
10361
10362 Decls : constant List_Id := New_List;
10363 Stats : constant List_Id := New_List;
10364 Body_List : constant List_Id := New_List;
10365 Trailing_List : constant List_Id := New_List;
10366
10367 Choices : List_Id;
10368 Else_Present : Boolean := False;
10369 Terminate_Alt : Node_Id := Empty;
10370 Select_Mode : Node_Id;
10371
10372 Delay_Case : List_Id;
10373 Delay_Count : Integer := 0;
10374 Delay_Val : Entity_Id;
10375 Delay_Index : Entity_Id;
10376 Delay_Min : Entity_Id;
10377 Delay_Num : Pos := 1;
10378 Delay_Alt_List : List_Id := New_List;
10379 Delay_List : constant List_Id := New_List;
10380 D : Entity_Id;
10381 M : Entity_Id;
10382
10383 First_Delay : Boolean := True;
10384 Guard_Open : Entity_Id;
10385
10386 End_Lab : Node_Id;
10387 Index : Pos := 1;
10388 Lab : Node_Id;
10389 Num_Alts : Nat;
10390 Num_Accept : Nat := 0;
10391 Proc : Node_Id;
10392 Time_Type : Entity_Id;
10393 Select_Call : Node_Id;
10394
10395 Qnam : constant Entity_Id :=
10396 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10397
10398 Xnam : constant Entity_Id :=
10399 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10400
10401 -----------------------
10402 -- Local subprograms --
10403 -----------------------
10404
10405 function Accept_Or_Raise return List_Id;
10406 -- For the rare case where delay alternatives all have guards, and
10407 -- all of them are closed, it is still possible that there were open
10408 -- accept alternatives with no callers. We must reexamine the
10409 -- Accept_List, and execute a selective wait with no else if some
10410 -- accept is open. If none, we raise program_error.
10411
10412 procedure Add_Accept (Alt : Node_Id);
10413 -- Process a single accept statement in a select alternative. Build
10414 -- procedure for body of accept, and add entry to dispatch table with
10415 -- expression for guard, in preparation for call to run time select.
10416
10417 function Make_And_Declare_Label (Num : Int) return Node_Id;
10418 -- Manufacture a label using Num as a serial number and declare it.
10419 -- The declaration is appended to Decls. The label marks the trailing
10420 -- statements of an accept or delay alternative.
10421
10422 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10423 -- Build call to Selective_Wait runtime routine
10424
10425 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10426 -- Add code to compare value of delay with previous values, and
10427 -- generate case entry for trailing statements.
10428
10429 procedure Process_Accept_Alternative
10430 (Alt : Node_Id;
10431 Index : Int;
10432 Proc : Node_Id);
10433 -- Add code to call corresponding procedure, and branch to
10434 -- trailing statements, if any.
10435
10436 ---------------------
10437 -- Accept_Or_Raise --
10438 ---------------------
10439
10440 function Accept_Or_Raise return List_Id is
10441 Cond : Node_Id;
10442 Stats : List_Id;
10443 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10444
10445 begin
10446 -- We generate the following:
10447
10448 -- for J in q'range loop
10449 -- if q(J).S /=null_task_entry then
10450 -- selective_wait (simple_mode,...);
10451 -- done := True;
10452 -- exit;
10453 -- end if;
10454 -- end loop;
10455 --
10456 -- if no rendez_vous then
10457 -- raise program_error;
10458 -- end if;
10459
10460 -- Note that the code needs to know that the selector name
10461 -- in an Accept_Alternative is named S.
10462
10463 Cond := Make_Op_Ne (Loc,
10464 Left_Opnd =>
10465 Make_Selected_Component (Loc,
10466 Prefix =>
10467 Make_Indexed_Component (Loc,
10468 Prefix => New_Occurrence_Of (Qnam, Loc),
10469 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10470 Selector_Name => Make_Identifier (Loc, Name_S)),
10471 Right_Opnd =>
10472 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10473
10474 Stats := New_List (
10475 Make_Implicit_Loop_Statement (N,
10476 Iteration_Scheme =>
10477 Make_Iteration_Scheme (Loc,
10478 Loop_Parameter_Specification =>
10479 Make_Loop_Parameter_Specification (Loc,
10480 Defining_Identifier => J,
10481 Discrete_Subtype_Definition =>
10482 Make_Attribute_Reference (Loc,
10483 Prefix => New_Occurrence_Of (Qnam, Loc),
10484 Attribute_Name => Name_Range,
10485 Expressions => New_List (
10486 Make_Integer_Literal (Loc, 1))))),
10487
10488 Statements => New_List (
10489 Make_Implicit_If_Statement (N,
10490 Condition => Cond,
10491 Then_Statements => New_List (
10492 Make_Select_Call (
10493 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10494 Make_Exit_Statement (Loc))))));
10495
10496 Append_To (Stats,
10497 Make_Raise_Program_Error (Loc,
10498 Condition => Make_Op_Eq (Loc,
10499 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10500 Right_Opnd =>
10501 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10502 Reason => PE_All_Guards_Closed));
10503
10504 return Stats;
10505 end Accept_Or_Raise;
10506
10507 ----------------
10508 -- Add_Accept --
10509 ----------------
10510
10511 procedure Add_Accept (Alt : Node_Id) is
10512 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10513 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10514 Eloc : constant Source_Ptr := Sloc (Ename);
10515 Eent : constant Entity_Id := Entity (Ename);
10516 Index : constant Node_Id := Entry_Index (Acc_Stm);
10517 Null_Body : Node_Id;
10518 Proc_Body : Node_Id;
10519 PB_Ent : Entity_Id;
10520 Expr : Node_Id;
10521 Call : Node_Id;
10522
10523 begin
10524 if No (Ann) then
10525 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10526 end if;
10527
10528 if Present (Condition (Alt)) then
10529 Expr :=
10530 Make_If_Expression (Eloc, New_List (
10531 Condition (Alt),
10532 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10533 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10534 else
10535 Expr :=
10536 Entry_Index_Expression
10537 (Eloc, Eent, Index, Scope (Eent));
10538 end if;
10539
10540 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10541 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10542
10543 -- Always add call to Abort_Undefer when generating code, since
10544 -- this is what the runtime expects (abort deferred in
10545 -- Selective_Wait). In CodePeer mode this only confuses the
10546 -- analysis with unknown calls, so don't do it.
10547
10548 if not CodePeer_Mode then
10549 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10550 Insert_Before
10551 (First (Statements (Handled_Statement_Sequence
10552 (Accept_Statement (Alt)))),
10553 Call);
10554 Analyze (Call);
10555 end if;
10556
10557 PB_Ent :=
10558 Make_Defining_Identifier (Eloc,
10559 New_External_Name (Chars (Ename), 'A', Num_Accept));
10560
10561 if Comes_From_Source (Alt) then
10562 Set_Debug_Info_Needed (PB_Ent);
10563 end if;
10564
10565 Proc_Body :=
10566 Make_Subprogram_Body (Eloc,
10567 Specification =>
10568 Make_Procedure_Specification (Eloc,
10569 Defining_Unit_Name => PB_Ent),
10570 Declarations => Declarations (Acc_Stm),
10571 Handled_Statement_Sequence =>
10572 Build_Accept_Body (Accept_Statement (Alt)));
10573
10574 -- During the analysis of the body of the accept statement, any
10575 -- zero cost exception handler records were collected in the
10576 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10577 -- This is where we move them to where they belong, namely the
10578 -- newly created procedure.
10579
10580 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10581 Append (Proc_Body, Body_List);
10582
10583 else
10584 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10585
10586 -- if accept statement has declarations, insert above, given that
10587 -- we are not creating a body for the accept.
10588
10589 if Present (Declarations (Acc_Stm)) then
10590 Insert_Actions (N, Declarations (Acc_Stm));
10591 end if;
10592 end if;
10593
10594 Append_To (Accept_List,
10595 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10596
10597 Num_Accept := Num_Accept + 1;
10598 end Add_Accept;
10599
10600 ----------------------------
10601 -- Make_And_Declare_Label --
10602 ----------------------------
10603
10604 function Make_And_Declare_Label (Num : Int) return Node_Id is
10605 Lab_Id : Node_Id;
10606
10607 begin
10608 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10609 Lab :=
10610 Make_Label (Loc, Lab_Id);
10611
10612 Append_To (Decls,
10613 Make_Implicit_Label_Declaration (Loc,
10614 Defining_Identifier =>
10615 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10616 Label_Construct => Lab));
10617
10618 return Lab;
10619 end Make_And_Declare_Label;
10620
10621 ----------------------
10622 -- Make_Select_Call --
10623 ----------------------
10624
10625 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10626 Params : constant List_Id := New_List;
10627
10628 begin
10629 Append_To (Params,
10630 Make_Attribute_Reference (Loc,
10631 Prefix => New_Occurrence_Of (Qnam, Loc),
10632 Attribute_Name => Name_Unchecked_Access));
10633 Append_To (Params, Select_Mode);
10634 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10635 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10636
10637 return
10638 Make_Procedure_Call_Statement (Loc,
10639 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10640 Parameter_Associations => Params);
10641 end Make_Select_Call;
10642
10643 --------------------------------
10644 -- Process_Accept_Alternative --
10645 --------------------------------
10646
10647 procedure Process_Accept_Alternative
10648 (Alt : Node_Id;
10649 Index : Int;
10650 Proc : Node_Id)
10651 is
10652 Astmt : constant Node_Id := Accept_Statement (Alt);
10653 Alt_Stats : List_Id;
10654
10655 begin
10656 Adjust_Condition (Condition (Alt));
10657
10658 -- Accept with body
10659
10660 if Present (Handled_Statement_Sequence (Astmt)) then
10661 Alt_Stats :=
10662 New_List (
10663 Make_Procedure_Call_Statement (Sloc (Proc),
10664 Name =>
10665 New_Occurrence_Of
10666 (Defining_Unit_Name (Specification (Proc)),
10667 Sloc (Proc))));
10668
10669 -- Accept with no body (followed by trailing statements)
10670
10671 else
10672 Alt_Stats := Empty_List;
10673 end if;
10674
10675 Ensure_Statement_Present (Sloc (Astmt), Alt);
10676
10677 -- After the call, if any, branch to trailing statements, if any.
10678 -- We create a label for each, as well as the corresponding label
10679 -- declaration.
10680
10681 if not Is_Empty_List (Statements (Alt)) then
10682 Lab := Make_And_Declare_Label (Index);
10683 Append (Lab, Trailing_List);
10684 Append_List (Statements (Alt), Trailing_List);
10685 Append_To (Trailing_List,
10686 Make_Goto_Statement (Loc,
10687 Name => New_Copy (Identifier (End_Lab))));
10688
10689 else
10690 Lab := End_Lab;
10691 end if;
10692
10693 Append_To (Alt_Stats,
10694 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10695
10696 Append_To (Alt_List,
10697 Make_Case_Statement_Alternative (Loc,
10698 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10699 Statements => Alt_Stats));
10700 end Process_Accept_Alternative;
10701
10702 -------------------------------
10703 -- Process_Delay_Alternative --
10704 -------------------------------
10705
10706 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10707 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10708 Cond : Node_Id;
10709 Delay_Alt : List_Id;
10710
10711 begin
10712 -- Deal with C/Fortran boolean as delay condition
10713
10714 Adjust_Condition (Condition (Alt));
10715
10716 -- Determine the smallest specified delay
10717
10718 -- for each delay alternative generate:
10719
10720 -- if guard-expression then
10721 -- Delay_Val := delay-expression;
10722 -- Guard_Open := True;
10723 -- if Delay_Val < Delay_Min then
10724 -- Delay_Min := Delay_Val;
10725 -- Delay_Index := Index;
10726 -- end if;
10727 -- end if;
10728
10729 -- The enclosing if-statement is omitted if there is no guard
10730
10731 if Delay_Count = 1 or else First_Delay then
10732 First_Delay := False;
10733
10734 Delay_Alt := New_List (
10735 Make_Assignment_Statement (Loc,
10736 Name => New_Occurrence_Of (Delay_Min, Loc),
10737 Expression => Expression (Delay_Statement (Alt))));
10738
10739 if Delay_Count > 1 then
10740 Append_To (Delay_Alt,
10741 Make_Assignment_Statement (Loc,
10742 Name => New_Occurrence_Of (Delay_Index, Loc),
10743 Expression => Make_Integer_Literal (Loc, Index)));
10744 end if;
10745
10746 else
10747 Delay_Alt := New_List (
10748 Make_Assignment_Statement (Loc,
10749 Name => New_Occurrence_Of (Delay_Val, Loc),
10750 Expression => Expression (Delay_Statement (Alt))));
10751
10752 if Time_Type = Standard_Duration then
10753 Cond :=
10754 Make_Op_Lt (Loc,
10755 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10756 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10757
10758 else
10759 -- The scope of the time type must define a comparison
10760 -- operator. The scope itself may not be visible, so we
10761 -- construct a node with entity information to insure that
10762 -- semantic analysis can find the proper operator.
10763
10764 Cond :=
10765 Make_Function_Call (Loc,
10766 Name => Make_Selected_Component (Loc,
10767 Prefix =>
10768 New_Occurrence_Of (Scope (Time_Type), Loc),
10769 Selector_Name =>
10770 Make_Operator_Symbol (Loc,
10771 Chars => Name_Op_Lt,
10772 Strval => No_String)),
10773 Parameter_Associations =>
10774 New_List (
10775 New_Occurrence_Of (Delay_Val, Loc),
10776 New_Occurrence_Of (Delay_Min, Loc)));
10777
10778 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10779 end if;
10780
10781 Append_To (Delay_Alt,
10782 Make_Implicit_If_Statement (N,
10783 Condition => Cond,
10784 Then_Statements => New_List (
10785 Make_Assignment_Statement (Loc,
10786 Name => New_Occurrence_Of (Delay_Min, Loc),
10787 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10788
10789 Make_Assignment_Statement (Loc,
10790 Name => New_Occurrence_Of (Delay_Index, Loc),
10791 Expression => Make_Integer_Literal (Loc, Index)))));
10792 end if;
10793
10794 if Check_Guard then
10795 Append_To (Delay_Alt,
10796 Make_Assignment_Statement (Loc,
10797 Name => New_Occurrence_Of (Guard_Open, Loc),
10798 Expression => New_Occurrence_Of (Standard_True, Loc)));
10799 end if;
10800
10801 if Present (Condition (Alt)) then
10802 Delay_Alt := New_List (
10803 Make_Implicit_If_Statement (N,
10804 Condition => Condition (Alt),
10805 Then_Statements => Delay_Alt));
10806 end if;
10807
10808 Append_List (Delay_Alt, Delay_List);
10809
10810 Ensure_Statement_Present (Dloc, Alt);
10811
10812 -- If the delay alternative has a statement part, add choice to the
10813 -- case statements for delays.
10814
10815 if not Is_Empty_List (Statements (Alt)) then
10816
10817 if Delay_Count = 1 then
10818 Append_List (Statements (Alt), Delay_Alt_List);
10819
10820 else
10821 Append_To (Delay_Alt_List,
10822 Make_Case_Statement_Alternative (Loc,
10823 Discrete_Choices => New_List (
10824 Make_Integer_Literal (Loc, Index)),
10825 Statements => Statements (Alt)));
10826 end if;
10827
10828 elsif Delay_Count = 1 then
10829
10830 -- If the single delay has no trailing statements, add a branch
10831 -- to the exit label to the selective wait.
10832
10833 Delay_Alt_List := New_List (
10834 Make_Goto_Statement (Loc,
10835 Name => New_Copy (Identifier (End_Lab))));
10836
10837 end if;
10838 end Process_Delay_Alternative;
10839
10840 -- Start of processing for Expand_N_Selective_Accept
10841
10842 begin
10843 Process_Statements_For_Controlled_Objects (N);
10844
10845 -- First insert some declarations before the select. The first is:
10846
10847 -- Ann : Address
10848
10849 -- This variable holds the parameters passed to the accept body. This
10850 -- declaration has already been inserted by the time we get here by
10851 -- a call to Expand_Accept_Declarations made from the semantics when
10852 -- processing the first accept statement contained in the select. We
10853 -- can find this entity as Accept_Address (E), where E is any of the
10854 -- entries references by contained accept statements.
10855
10856 -- The first step is to scan the list of Selective_Accept_Statements
10857 -- to find this entity, and also count the number of accepts, and
10858 -- determine if terminated, delay or else is present:
10859
10860 Num_Alts := 0;
10861
10862 Alt := First (Alts);
10863 while Present (Alt) loop
10864 Process_Statements_For_Controlled_Objects (Alt);
10865
10866 if Nkind (Alt) = N_Accept_Alternative then
10867 Add_Accept (Alt);
10868
10869 elsif Nkind (Alt) = N_Delay_Alternative then
10870 Delay_Count := Delay_Count + 1;
10871
10872 -- If the delays are relative delays, the delay expressions have
10873 -- type Standard_Duration. Otherwise they must have some time type
10874 -- recognized by GNAT.
10875
10876 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10877 Time_Type := Standard_Duration;
10878 else
10879 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10880
10881 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10882 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10883 then
10884 null;
10885 else
10886 Error_Msg_NE (
10887 "& is not a time type (RM 9.6(6))",
10888 Expression (Delay_Statement (Alt)), Time_Type);
10889 Time_Type := Standard_Duration;
10890 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10891 end if;
10892 end if;
10893
10894 if No (Condition (Alt)) then
10895
10896 -- This guard will always be open
10897
10898 Check_Guard := False;
10899 end if;
10900
10901 elsif Nkind (Alt) = N_Terminate_Alternative then
10902 Adjust_Condition (Condition (Alt));
10903 Terminate_Alt := Alt;
10904 end if;
10905
10906 Num_Alts := Num_Alts + 1;
10907 Next (Alt);
10908 end loop;
10909
10910 Else_Present := Present (Else_Statements (N));
10911
10912 -- At the same time (see procedure Add_Accept) we build the accept list:
10913
10914 -- Qnn : Accept_List (1 .. num-select) := (
10915 -- (null-body, entry-index),
10916 -- (null-body, entry-index),
10917 -- ..
10918 -- (null_body, entry-index));
10919
10920 -- In the above declaration, null-body is True if the corresponding
10921 -- accept has no body, and false otherwise. The entry is either the
10922 -- entry index expression if there is no guard, or if a guard is
10923 -- present, then an if expression of the form:
10924
10925 -- (if guard then entry-index else Null_Task_Entry)
10926
10927 -- If a guard is statically known to be false, the entry can simply
10928 -- be omitted from the accept list.
10929
10930 Append_To (Decls,
10931 Make_Object_Declaration (Loc,
10932 Defining_Identifier => Qnam,
10933 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10934 Aliased_Present => True,
10935 Expression =>
10936 Make_Qualified_Expression (Loc,
10937 Subtype_Mark =>
10938 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10939 Expression =>
10940 Make_Aggregate (Loc, Expressions => Accept_List))));
10941
10942 -- Then we declare the variable that holds the index for the accept
10943 -- that will be selected for service:
10944
10945 -- Xnn : Select_Index;
10946
10947 Append_To (Decls,
10948 Make_Object_Declaration (Loc,
10949 Defining_Identifier => Xnam,
10950 Object_Definition =>
10951 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
10952 Expression =>
10953 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
10954
10955 -- After this follow procedure declarations for each accept body
10956
10957 -- procedure Pnn is
10958 -- begin
10959 -- ...
10960 -- end;
10961
10962 -- where the ... are statements from the corresponding procedure body.
10963 -- No parameters are involved, since the parameters are passed via Ann
10964 -- and the parameter references have already been expanded to be direct
10965 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10966 -- any embedded tasking statements (which would normally be illegal in
10967 -- procedures), have been converted to calls to the tasking runtime so
10968 -- there is no problem in putting them into procedures.
10969
10970 -- The original accept statement has been expanded into a block in
10971 -- the same fashion as for simple accepts (see Build_Accept_Body).
10972
10973 -- Note: we don't really need to build these procedures for the case
10974 -- where no delay statement is present, but it is just as easy to
10975 -- build them unconditionally, and not significantly inefficient,
10976 -- since if they are short they will be inlined anyway.
10977
10978 -- The procedure declarations have been assembled in Body_List
10979
10980 -- If delays are present, we must compute the required delay.
10981 -- We first generate the declarations:
10982
10983 -- Delay_Index : Boolean := 0;
10984 -- Delay_Min : Some_Time_Type.Time;
10985 -- Delay_Val : Some_Time_Type.Time;
10986
10987 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10988 -- active delay that is actually chosen as the basis for the possible
10989 -- delay if an immediate rendez-vous is not possible.
10990
10991 -- In the most common case there is a single delay statement, and this
10992 -- is handled specially.
10993
10994 if Delay_Count > 0 then
10995
10996 -- Generate the required declarations
10997
10998 Delay_Val :=
10999 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11000 Delay_Index :=
11001 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11002 Delay_Min :=
11003 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11004
11005 Append_To (Decls,
11006 Make_Object_Declaration (Loc,
11007 Defining_Identifier => Delay_Val,
11008 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11009
11010 Append_To (Decls,
11011 Make_Object_Declaration (Loc,
11012 Defining_Identifier => Delay_Index,
11013 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11014 Expression => Make_Integer_Literal (Loc, 0)));
11015
11016 Append_To (Decls,
11017 Make_Object_Declaration (Loc,
11018 Defining_Identifier => Delay_Min,
11019 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11020 Expression =>
11021 Unchecked_Convert_To (Time_Type,
11022 Make_Attribute_Reference (Loc,
11023 Prefix =>
11024 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11025 Attribute_Name => Name_Last))));
11026
11027 -- Create Duration and Delay_Mode objects used for passing a delay
11028 -- value to RTS
11029
11030 D := Make_Temporary (Loc, 'D');
11031 M := Make_Temporary (Loc, 'M');
11032
11033 declare
11034 Discr : Entity_Id;
11035
11036 begin
11037 -- Note that these values are defined in s-osprim.ads and must
11038 -- be kept in sync:
11039 --
11040 -- Relative : constant := 0;
11041 -- Absolute_Calendar : constant := 1;
11042 -- Absolute_RT : constant := 2;
11043
11044 if Time_Type = Standard_Duration then
11045 Discr := Make_Integer_Literal (Loc, 0);
11046
11047 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11048 Discr := Make_Integer_Literal (Loc, 1);
11049
11050 else
11051 pragma Assert
11052 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11053 Discr := Make_Integer_Literal (Loc, 2);
11054 end if;
11055
11056 Append_To (Decls,
11057 Make_Object_Declaration (Loc,
11058 Defining_Identifier => D,
11059 Object_Definition =>
11060 New_Occurrence_Of (Standard_Duration, Loc)));
11061
11062 Append_To (Decls,
11063 Make_Object_Declaration (Loc,
11064 Defining_Identifier => M,
11065 Object_Definition =>
11066 New_Occurrence_Of (Standard_Integer, Loc),
11067 Expression => Discr));
11068 end;
11069
11070 if Check_Guard then
11071 Guard_Open :=
11072 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11073
11074 Append_To (Decls,
11075 Make_Object_Declaration (Loc,
11076 Defining_Identifier => Guard_Open,
11077 Object_Definition =>
11078 New_Occurrence_Of (Standard_Boolean, Loc),
11079 Expression =>
11080 New_Occurrence_Of (Standard_False, Loc)));
11081 end if;
11082
11083 -- Delay_Count is zero, don't need M and D set (suppress warning)
11084
11085 else
11086 M := Empty;
11087 D := Empty;
11088 end if;
11089
11090 if Present (Terminate_Alt) then
11091
11092 -- If the terminate alternative guard is False, use
11093 -- Simple_Mode; otherwise use Terminate_Mode.
11094
11095 if Present (Condition (Terminate_Alt)) then
11096 Select_Mode := Make_If_Expression (Loc,
11097 New_List (Condition (Terminate_Alt),
11098 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11099 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11100 else
11101 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11102 end if;
11103
11104 elsif Else_Present or Delay_Count > 0 then
11105 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11106
11107 else
11108 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11109 end if;
11110
11111 Select_Call := Make_Select_Call (Select_Mode);
11112 Append (Select_Call, Stats);
11113
11114 -- Now generate code to act on the result. There is an entry
11115 -- in this case for each accept statement with a non-null body,
11116 -- followed by a branch to the statements that follow the Accept.
11117 -- In the absence of delay alternatives, we generate:
11118
11119 -- case X is
11120 -- when No_Rendezvous => -- omitted if simple mode
11121 -- goto Lab0;
11122
11123 -- when 1 =>
11124 -- P1n;
11125 -- goto Lab1;
11126
11127 -- when 2 =>
11128 -- P2n;
11129 -- goto Lab2;
11130
11131 -- when others =>
11132 -- goto Exit;
11133 -- end case;
11134 --
11135 -- Lab0: Else_Statements;
11136 -- goto exit;
11137
11138 -- Lab1: Trailing_Statements1;
11139 -- goto Exit;
11140 --
11141 -- Lab2: Trailing_Statements2;
11142 -- goto Exit;
11143 -- ...
11144 -- Exit:
11145
11146 -- Generate label for common exit
11147
11148 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11149
11150 -- First entry is the default case, when no rendezvous is possible
11151
11152 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11153
11154 if Else_Present then
11155
11156 -- If no rendezvous is possible, the else part is executed
11157
11158 Lab := Make_And_Declare_Label (0);
11159 Alt_Stats := New_List (
11160 Make_Goto_Statement (Loc,
11161 Name => New_Copy (Identifier (Lab))));
11162
11163 Append (Lab, Trailing_List);
11164 Append_List (Else_Statements (N), Trailing_List);
11165 Append_To (Trailing_List,
11166 Make_Goto_Statement (Loc,
11167 Name => New_Copy (Identifier (End_Lab))));
11168 else
11169 Alt_Stats := New_List (
11170 Make_Goto_Statement (Loc,
11171 Name => New_Copy (Identifier (End_Lab))));
11172 end if;
11173
11174 Append_To (Alt_List,
11175 Make_Case_Statement_Alternative (Loc,
11176 Discrete_Choices => Choices,
11177 Statements => Alt_Stats));
11178
11179 -- We make use of the fact that Accept_Index is an integer type, and
11180 -- generate successive literals for entries for each accept. Only those
11181 -- for which there is a body or trailing statements get a case entry.
11182
11183 Alt := First (Select_Alternatives (N));
11184 Proc := First (Body_List);
11185 while Present (Alt) loop
11186
11187 if Nkind (Alt) = N_Accept_Alternative then
11188 Process_Accept_Alternative (Alt, Index, Proc);
11189 Index := Index + 1;
11190
11191 if Present
11192 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11193 then
11194 Next (Proc);
11195 end if;
11196
11197 elsif Nkind (Alt) = N_Delay_Alternative then
11198 Process_Delay_Alternative (Alt, Delay_Num);
11199 Delay_Num := Delay_Num + 1;
11200 end if;
11201
11202 Next (Alt);
11203 end loop;
11204
11205 -- An others choice is always added to the main case, as well
11206 -- as the delay case (to satisfy the compiler).
11207
11208 Append_To (Alt_List,
11209 Make_Case_Statement_Alternative (Loc,
11210 Discrete_Choices =>
11211 New_List (Make_Others_Choice (Loc)),
11212 Statements =>
11213 New_List (Make_Goto_Statement (Loc,
11214 Name => New_Copy (Identifier (End_Lab))))));
11215
11216 Accept_Case := New_List (
11217 Make_Case_Statement (Loc,
11218 Expression => New_Occurrence_Of (Xnam, Loc),
11219 Alternatives => Alt_List));
11220
11221 Append_List (Trailing_List, Accept_Case);
11222 Append_List (Body_List, Decls);
11223
11224 -- Construct case statement for trailing statements of delay
11225 -- alternatives, if there are several of them.
11226
11227 if Delay_Count > 1 then
11228 Append_To (Delay_Alt_List,
11229 Make_Case_Statement_Alternative (Loc,
11230 Discrete_Choices =>
11231 New_List (Make_Others_Choice (Loc)),
11232 Statements =>
11233 New_List (Make_Null_Statement (Loc))));
11234
11235 Delay_Case := New_List (
11236 Make_Case_Statement (Loc,
11237 Expression => New_Occurrence_Of (Delay_Index, Loc),
11238 Alternatives => Delay_Alt_List));
11239 else
11240 Delay_Case := Delay_Alt_List;
11241 end if;
11242
11243 -- If there are no delay alternatives, we append the case statement
11244 -- to the statement list.
11245
11246 if Delay_Count = 0 then
11247 Append_List (Accept_Case, Stats);
11248
11249 -- Delay alternatives present
11250
11251 else
11252 -- If delay alternatives are present we generate:
11253
11254 -- find minimum delay.
11255 -- DX := minimum delay;
11256 -- M := <delay mode>;
11257 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11258 -- DX, MX, X);
11259 --
11260 -- if X = No_Rendezvous then
11261 -- case statement for delay statements.
11262 -- else
11263 -- case statement for accept alternatives.
11264 -- end if;
11265
11266 declare
11267 Cases : Node_Id;
11268 Stmt : Node_Id;
11269 Parms : List_Id;
11270 Parm : Node_Id;
11271 Conv : Node_Id;
11272
11273 begin
11274 -- The type of the delay expression is known to be legal
11275
11276 if Time_Type = Standard_Duration then
11277 Conv := New_Occurrence_Of (Delay_Min, Loc);
11278
11279 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11280 Conv := Make_Function_Call (Loc,
11281 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11282 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11283
11284 else
11285 pragma Assert
11286 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11287
11288 Conv := Make_Function_Call (Loc,
11289 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11290 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11291 end if;
11292
11293 Stmt := Make_Assignment_Statement (Loc,
11294 Name => New_Occurrence_Of (D, Loc),
11295 Expression => Conv);
11296
11297 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11298
11299 Parms := Parameter_Associations (Select_Call);
11300
11301 Parm := First (Parms);
11302 while Present (Parm) and then Parm /= Select_Mode loop
11303 Next (Parm);
11304 end loop;
11305
11306 pragma Assert (Present (Parm));
11307 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11308 Analyze (Parm);
11309
11310 -- Prepare two new parameters of Duration and Delay_Mode type
11311 -- which represent the value and the mode of the minimum delay.
11312
11313 Next (Parm);
11314 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11315 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11316
11317 -- Create a call to RTS
11318
11319 Rewrite (Select_Call,
11320 Make_Procedure_Call_Statement (Loc,
11321 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11322 Parameter_Associations => Parms));
11323
11324 -- This new call should follow the calculation of the minimum
11325 -- delay.
11326
11327 Insert_List_Before (Select_Call, Delay_List);
11328
11329 if Check_Guard then
11330 Stmt :=
11331 Make_Implicit_If_Statement (N,
11332 Condition => New_Occurrence_Of (Guard_Open, Loc),
11333 Then_Statements => New_List (
11334 New_Copy_Tree (Stmt),
11335 New_Copy_Tree (Select_Call)),
11336 Else_Statements => Accept_Or_Raise);
11337 Rewrite (Select_Call, Stmt);
11338 else
11339 Insert_Before (Select_Call, Stmt);
11340 end if;
11341
11342 Cases :=
11343 Make_Implicit_If_Statement (N,
11344 Condition => Make_Op_Eq (Loc,
11345 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11346 Right_Opnd =>
11347 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11348
11349 Then_Statements => Delay_Case,
11350 Else_Statements => Accept_Case);
11351
11352 Append (Cases, Stats);
11353 end;
11354 end if;
11355
11356 Append (End_Lab, Stats);
11357
11358 -- Replace accept statement with appropriate block
11359
11360 Rewrite (N,
11361 Make_Block_Statement (Loc,
11362 Declarations => Decls,
11363 Handled_Statement_Sequence =>
11364 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11365 Analyze (N);
11366
11367 -- Note: have to worry more about abort deferral in above code ???
11368
11369 -- Final step is to unstack the Accept_Address entries for all accept
11370 -- statements appearing in accept alternatives in the select statement
11371
11372 Alt := First (Alts);
11373 while Present (Alt) loop
11374 if Nkind (Alt) = N_Accept_Alternative then
11375 Remove_Last_Elmt (Accept_Address
11376 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11377 end if;
11378
11379 Next (Alt);
11380 end loop;
11381 end Expand_N_Selective_Accept;
11382
11383 -------------------------------------------
11384 -- Expand_N_Single_Protected_Declaration --
11385 -------------------------------------------
11386
11387 -- A single protected declaration should never be present after semantic
11388 -- analysis because it is transformed into a protected type declaration
11389 -- and an accompanying anonymous object. This routine ensures that the
11390 -- transformation takes place.
11391
11392 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11393 begin
11394 raise Program_Error;
11395 end Expand_N_Single_Protected_Declaration;
11396
11397 --------------------------------------
11398 -- Expand_N_Single_Task_Declaration --
11399 --------------------------------------
11400
11401 -- A single task declaration should never be present after semantic
11402 -- analysis because it is transformed into a task type declaration and
11403 -- an accompanying anonymous object. This routine ensures that the
11404 -- transformation takes place.
11405
11406 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11407 begin
11408 raise Program_Error;
11409 end Expand_N_Single_Task_Declaration;
11410
11411 ------------------------
11412 -- Expand_N_Task_Body --
11413 ------------------------
11414
11415 -- Given a task body
11416
11417 -- task body tname is
11418 -- <declarations>
11419 -- begin
11420 -- <statements>
11421 -- end x;
11422
11423 -- This expansion routine converts it into a procedure and sets the
11424 -- elaboration flag for the procedure to true, to represent the fact
11425 -- that the task body is now elaborated:
11426
11427 -- procedure tnameB (_Task : access tnameV) is
11428 -- discriminal : dtype renames _Task.discriminant;
11429
11430 -- procedure _clean is
11431 -- begin
11432 -- Abort_Defer.all;
11433 -- Complete_Task;
11434 -- Abort_Undefer.all;
11435 -- return;
11436 -- end _clean;
11437
11438 -- begin
11439 -- Abort_Undefer.all;
11440 -- <declarations>
11441 -- System.Task_Stages.Complete_Activation;
11442 -- <statements>
11443 -- at end
11444 -- _clean;
11445 -- end tnameB;
11446
11447 -- tnameE := True;
11448
11449 -- In addition, if the task body is an activator, then a call to activate
11450 -- tasks is added at the start of the statements, before the call to
11451 -- Complete_Activation, and if in addition the task is a master then it
11452 -- must be established as a master. These calls are inserted and analyzed
11453 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11454 -- expanded.
11455
11456 -- There is one discriminal declaration line generated for each
11457 -- discriminant that is present to provide an easy reference point for
11458 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11459
11460 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11461 -- task body procedures have a profile (Arg : System.Address). That is
11462 -- needed because GNARLI has to use the same access-to-subprogram type
11463 -- for all task types. We depend here on knowing that in GNAT, passing
11464 -- an address argument by value is identical to passing a record value
11465 -- by access (in either case a single pointer is passed), so even though
11466 -- this procedure has the wrong profile. In fact it's all OK, since the
11467 -- callings sequence is identical.
11468
11469 procedure Expand_N_Task_Body (N : Node_Id) is
11470 Loc : constant Source_Ptr := Sloc (N);
11471 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11472 Call : Node_Id;
11473 New_N : Node_Id;
11474
11475 Insert_Nod : Node_Id;
11476 -- Used to determine the proper location of wrapper body insertions
11477
11478 begin
11479 -- if no task body procedure, means we had an error in configurable
11480 -- run-time mode, and there is no point in proceeding further.
11481
11482 if No (Task_Body_Procedure (Ttyp)) then
11483 return;
11484 end if;
11485
11486 -- Add renaming declarations for discriminals and a declaration for the
11487 -- entry family index (if applicable).
11488
11489 Install_Private_Data_Declarations
11490 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11491
11492 -- Add a call to Abort_Undefer at the very beginning of the task
11493 -- body since this body is called with abort still deferred.
11494
11495 if Abort_Allowed then
11496 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11497 Insert_Before
11498 (First (Statements (Handled_Statement_Sequence (N))), Call);
11499 Analyze (Call);
11500 end if;
11501
11502 -- The statement part has already been protected with an at_end and
11503 -- cleanup actions. The call to Complete_Activation must be placed
11504 -- at the head of the sequence of statements of that block. The
11505 -- declarations have been merged in this sequence of statements but
11506 -- the first real statement is accessible from the First_Real_Statement
11507 -- field (which was set for exactly this purpose).
11508
11509 if Restricted_Profile then
11510 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11511 else
11512 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11513 end if;
11514
11515 Insert_Before
11516 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11517 Analyze (Call);
11518
11519 New_N :=
11520 Make_Subprogram_Body (Loc,
11521 Specification => Build_Task_Proc_Specification (Ttyp),
11522 Declarations => Declarations (N),
11523 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11524 Set_Is_Task_Body_Procedure (New_N);
11525
11526 -- If the task contains generic instantiations, cleanup actions are
11527 -- delayed until after instantiation. Transfer the activation chain to
11528 -- the subprogram, to insure that the activation call is properly
11529 -- generated. It the task body contains inner tasks, indicate that the
11530 -- subprogram is a task master.
11531
11532 if Delay_Cleanups (Ttyp) then
11533 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11534 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11535 end if;
11536
11537 Rewrite (N, New_N);
11538 Analyze (N);
11539
11540 -- Set elaboration flag immediately after task body. If the body is a
11541 -- subunit, the flag is set in the declarative part containing the stub.
11542
11543 if Nkind (Parent (N)) /= N_Subunit then
11544 Insert_After (N,
11545 Make_Assignment_Statement (Loc,
11546 Name =>
11547 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11548 Expression => New_Occurrence_Of (Standard_True, Loc)));
11549 end if;
11550
11551 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11552 -- the task body. At this point all wrapper specs have been created,
11553 -- frozen and included in the dispatch table for the task type.
11554
11555 if Ada_Version >= Ada_2005 then
11556 if Nkind (Parent (N)) = N_Subunit then
11557 Insert_Nod := Corresponding_Stub (Parent (N));
11558 else
11559 Insert_Nod := N;
11560 end if;
11561
11562 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11563 end if;
11564 end Expand_N_Task_Body;
11565
11566 ------------------------------------
11567 -- Expand_N_Task_Type_Declaration --
11568 ------------------------------------
11569
11570 -- We have several things to do. First we must create a Boolean flag used
11571 -- to mark if the body is elaborated yet. This variable gets set to True
11572 -- when the body of the task is elaborated (we can't rely on the normal
11573 -- ABE mechanism for the task body, since we need to pass an access to
11574 -- this elaboration boolean to the runtime routines).
11575
11576 -- taskE : aliased Boolean := False;
11577
11578 -- Next a variable is declared to hold the task stack size (either the
11579 -- default : Unspecified_Size, or a value that is set by a pragma
11580 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11581 -- the variable is initialized with this value:
11582
11583 -- taskZ : Size_Type := Unspecified_Size;
11584 -- or
11585 -- taskZ : Size_Type := Size_Type (size_expression);
11586
11587 -- Note: No variable is needed to hold the task relative deadline since
11588 -- its value would never be static because the parameter is of a private
11589 -- type (Ada.Real_Time.Time_Span).
11590
11591 -- Next we create a corresponding record type declaration used to represent
11592 -- values of this task. The general form of this type declaration is
11593
11594 -- type taskV (discriminants) is record
11595 -- _Task_Id : Task_Id;
11596 -- entry_family : array (bounds) of Void;
11597 -- _Priority : Integer := priority_expression;
11598 -- _Size : Size_Type := size_expression;
11599 -- _Secondary_Stack_Size : Size_Type := size_expression;
11600 -- _Task_Info : Task_Info_Type := task_info_expression;
11601 -- _CPU : Integer := cpu_range_expression;
11602 -- _Relative_Deadline : Time_Span := time_span_expression;
11603 -- _Domain : Dispatching_Domain := dd_expression;
11604 -- end record;
11605
11606 -- The discriminants are present only if the corresponding task type has
11607 -- discriminants, and they exactly mirror the task type discriminants.
11608
11609 -- The Id field is always present. It contains the Task_Id value, as set by
11610 -- the call to Create_Task. Note that although the task is limited, the
11611 -- task value record type is not limited, so there is no problem in passing
11612 -- this field as an out parameter to Create_Task.
11613
11614 -- One entry_family component is present for each entry family in the task
11615 -- definition. The bounds correspond to the bounds of the entry family
11616 -- (which may depend on discriminants). The element type is void, since we
11617 -- only need the bounds information for determining the entry index. Note
11618 -- that the use of an anonymous array would normally be illegal in this
11619 -- context, but this is a parser check, and the semantics is quite prepared
11620 -- to handle such a case.
11621
11622 -- The _Size field is present only if a Storage_Size pragma appears in the
11623 -- task definition. The expression captures the argument that was present
11624 -- in the pragma, and is used to override the task stack size otherwise
11625 -- associated with the task type.
11626
11627 -- The _Secondary_Stack_Size field is present only the task entity has a
11628 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11629 -- when the record init proc is built, to capture the expression of the
11630 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11631 -- be filled here since aspect evaluations are delayed till the freeze
11632 -- point.
11633
11634 -- The _Priority field is present only if the task entity has a Priority or
11635 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11636 -- definition clause). It will be filled at the freeze point, when the
11637 -- record init proc is built, to capture the expression of the rep item
11638 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11639 -- here since aspect evaluations are delayed till the freeze point.
11640
11641 -- The _Task_Info field is present only if a Task_Info pragma appears in
11642 -- the task definition. The expression captures the argument that was
11643 -- present in the pragma, and is used to provide the Task_Image parameter
11644 -- to the call to Create_Task.
11645
11646 -- The _CPU field is present only if the task entity has a CPU rep item
11647 -- (pragma, aspect specification or attribute definition clause). It will
11648 -- be filled at the freeze point, when the record init proc is built, to
11649 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11650 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11651 -- are delayed till the freeze point.
11652
11653 -- The _Relative_Deadline field is present only if a Relative_Deadline
11654 -- pragma appears in the task definition. The expression captures the
11655 -- argument that was present in the pragma, and is used to provide the
11656 -- Relative_Deadline parameter to the call to Create_Task.
11657
11658 -- The _Domain field is present only if the task entity has a
11659 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11660 -- definition clause). It will be filled at the freeze point, when the
11661 -- record init proc is built, to capture the expression of the rep item
11662 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11663 -- here since aspect evaluations are delayed till the freeze point.
11664
11665 -- When a task is declared, an instance of the task value record is
11666 -- created. The elaboration of this declaration creates the correct bounds
11667 -- for the entry families, and also evaluates the size, priority, and
11668 -- task_Info expressions if needed. The initialization routine for the task
11669 -- type itself then calls Create_Task with appropriate parameters to
11670 -- initialize the value of the Task_Id field.
11671
11672 -- Note: the address of this record is passed as the "Discriminants"
11673 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11674 -- body procedure, it does not matter that it does not quite match the
11675 -- GNARLI model of what is being passed (the record contains more than just
11676 -- the discriminants, but the discriminants can be found from the record
11677 -- value).
11678
11679 -- The Entity_Id for this created record type is placed in the
11680 -- Corresponding_Record_Type field of the associated task type entity.
11681
11682 -- Next we create a procedure specification for the task body procedure:
11683
11684 -- procedure taskB (_Task : access taskV);
11685
11686 -- Note that this must come after the record type declaration, since
11687 -- the spec refers to this type. It turns out that the initialization
11688 -- procedure for the value type references the task body spec, but that's
11689 -- fine, since it won't be generated till the freeze point for the type,
11690 -- which is certainly after the task body spec declaration.
11691
11692 -- Finally, we set the task index value field of the entry attribute in
11693 -- the case of a simple entry.
11694
11695 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11696 Loc : constant Source_Ptr := Sloc (N);
11697 TaskId : constant Entity_Id := Defining_Identifier (N);
11698 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11699 Tasknm : constant Name_Id := Chars (Tasktyp);
11700 Taskdef : constant Node_Id := Task_Definition (N);
11701
11702 Body_Decl : Node_Id;
11703 Cdecls : List_Id;
11704 Decl_Stack : Node_Id;
11705 Elab_Decl : Node_Id;
11706 Ent_Stack : Entity_Id;
11707 Proc_Spec : Node_Id;
11708 Rec_Decl : Node_Id;
11709 Rec_Ent : Entity_Id;
11710 Size_Decl : Entity_Id;
11711 Task_Size : Node_Id;
11712
11713 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11714 -- Searches the task definition T for the first occurrence of the pragma
11715 -- Relative Deadline. The caller has ensured that the pragma is present
11716 -- in the task definition. Note that this routine cannot be implemented
11717 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11718 -- not chained because their expansion into a procedure call statement
11719 -- would cause a break in the chain.
11720
11721 ----------------------------------
11722 -- Get_Relative_Deadline_Pragma --
11723 ----------------------------------
11724
11725 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11726 N : Node_Id;
11727
11728 begin
11729 N := First (Visible_Declarations (T));
11730 while Present (N) loop
11731 if Nkind (N) = N_Pragma
11732 and then Pragma_Name (N) = Name_Relative_Deadline
11733 then
11734 return N;
11735 end if;
11736
11737 Next (N);
11738 end loop;
11739
11740 N := First (Private_Declarations (T));
11741 while Present (N) loop
11742 if Nkind (N) = N_Pragma
11743 and then Pragma_Name (N) = Name_Relative_Deadline
11744 then
11745 return N;
11746 end if;
11747
11748 Next (N);
11749 end loop;
11750
11751 raise Program_Error;
11752 end Get_Relative_Deadline_Pragma;
11753
11754 -- Start of processing for Expand_N_Task_Type_Declaration
11755
11756 begin
11757 -- If already expanded, nothing to do
11758
11759 if Present (Corresponding_Record_Type (Tasktyp)) then
11760 return;
11761 end if;
11762
11763 -- Here we will do the expansion
11764
11765 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11766
11767 Rec_Ent := Defining_Identifier (Rec_Decl);
11768 Cdecls := Component_Items (Component_List
11769 (Type_Definition (Rec_Decl)));
11770
11771 Qualify_Entity_Names (N);
11772
11773 -- First create the elaboration variable
11774
11775 Elab_Decl :=
11776 Make_Object_Declaration (Loc,
11777 Defining_Identifier =>
11778 Make_Defining_Identifier (Sloc (Tasktyp),
11779 Chars => New_External_Name (Tasknm, 'E')),
11780 Aliased_Present => True,
11781 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11782 Expression => New_Occurrence_Of (Standard_False, Loc));
11783
11784 Insert_After (N, Elab_Decl);
11785
11786 -- Next create the declaration of the size variable (tasknmZ)
11787
11788 Set_Storage_Size_Variable (Tasktyp,
11789 Make_Defining_Identifier (Sloc (Tasktyp),
11790 Chars => New_External_Name (Tasknm, 'Z')));
11791
11792 if Present (Taskdef)
11793 and then Has_Storage_Size_Pragma (Taskdef)
11794 and then
11795 Is_OK_Static_Expression
11796 (Expression
11797 (First (Pragma_Argument_Associations
11798 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11799 then
11800 Size_Decl :=
11801 Make_Object_Declaration (Loc,
11802 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11803 Object_Definition =>
11804 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11805 Expression =>
11806 Convert_To (RTE (RE_Size_Type),
11807 Relocate_Node
11808 (Expression (First (Pragma_Argument_Associations
11809 (Get_Rep_Pragma
11810 (TaskId, Name_Storage_Size)))))));
11811
11812 else
11813 Size_Decl :=
11814 Make_Object_Declaration (Loc,
11815 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11816 Object_Definition =>
11817 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11818 Expression =>
11819 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11820 end if;
11821
11822 Insert_After (Elab_Decl, Size_Decl);
11823
11824 -- Next build the rest of the corresponding record declaration. This is
11825 -- done last, since the corresponding record initialization procedure
11826 -- will reference the previously created entities.
11827
11828 -- Fill in the component declarations -- first the _Task_Id field
11829
11830 Append_To (Cdecls,
11831 Make_Component_Declaration (Loc,
11832 Defining_Identifier =>
11833 Make_Defining_Identifier (Loc, Name_uTask_Id),
11834 Component_Definition =>
11835 Make_Component_Definition (Loc,
11836 Aliased_Present => False,
11837 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11838 Loc))));
11839
11840 -- Declare static ATCB (that is, created by the expander) if we are
11841 -- using the Restricted run time.
11842
11843 if Restricted_Profile then
11844 Append_To (Cdecls,
11845 Make_Component_Declaration (Loc,
11846 Defining_Identifier =>
11847 Make_Defining_Identifier (Loc, Name_uATCB),
11848
11849 Component_Definition =>
11850 Make_Component_Definition (Loc,
11851 Aliased_Present => True,
11852 Subtype_Indication => Make_Subtype_Indication (Loc,
11853 Subtype_Mark =>
11854 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11855
11856 Constraint =>
11857 Make_Index_Or_Discriminant_Constraint (Loc,
11858 Constraints =>
11859 New_List (Make_Integer_Literal (Loc, 0)))))));
11860
11861 end if;
11862
11863 -- Declare static stack (that is, created by the expander) if we are
11864 -- using the Restricted run time on a bare board configuration.
11865
11866 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11867
11868 -- First we need to extract the appropriate stack size
11869
11870 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11871
11872 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11873 declare
11874 Expr_N : constant Node_Id :=
11875 Expression (First (
11876 Pragma_Argument_Associations (
11877 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11878 Etyp : constant Entity_Id := Etype (Expr_N);
11879 P : constant Node_Id := Parent (Expr_N);
11880
11881 begin
11882 -- The stack is defined inside the corresponding record.
11883 -- Therefore if the size of the stack is set by means of
11884 -- a discriminant, we must reference the discriminant of the
11885 -- corresponding record type.
11886
11887 if Nkind (Expr_N) in N_Has_Entity
11888 and then Present (Discriminal_Link (Entity (Expr_N)))
11889 then
11890 Task_Size :=
11891 New_Occurrence_Of
11892 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11893 Loc);
11894 Set_Parent (Task_Size, P);
11895 Set_Etype (Task_Size, Etyp);
11896 Set_Analyzed (Task_Size);
11897
11898 else
11899 Task_Size := Relocate_Node (Expr_N);
11900 end if;
11901 end;
11902
11903 else
11904 Task_Size :=
11905 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11906 end if;
11907
11908 Decl_Stack := Make_Component_Declaration (Loc,
11909 Defining_Identifier => Ent_Stack,
11910
11911 Component_Definition =>
11912 Make_Component_Definition (Loc,
11913 Aliased_Present => True,
11914 Subtype_Indication => Make_Subtype_Indication (Loc,
11915 Subtype_Mark =>
11916 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11917
11918 Constraint =>
11919 Make_Index_Or_Discriminant_Constraint (Loc,
11920 Constraints => New_List (Make_Range (Loc,
11921 Low_Bound => Make_Integer_Literal (Loc, 1),
11922 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11923 Task_Size)))))));
11924
11925 Append_To (Cdecls, Decl_Stack);
11926
11927 -- The appropriate alignment for the stack is ensured by the run-time
11928 -- code in charge of task creation.
11929
11930 end if;
11931
11932 -- Add components for entry families
11933
11934 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11935
11936 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11937 -- item is present.
11938
11939 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11940 Append_To (Cdecls,
11941 Make_Component_Declaration (Loc,
11942 Defining_Identifier =>
11943 Make_Defining_Identifier (Loc, Name_uPriority),
11944 Component_Definition =>
11945 Make_Component_Definition (Loc,
11946 Aliased_Present => False,
11947 Subtype_Indication =>
11948 New_Occurrence_Of (Standard_Integer, Loc))));
11949 end if;
11950
11951 -- Add the _Size component if a Storage_Size pragma is present
11952
11953 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11954 Append_To (Cdecls,
11955 Make_Component_Declaration (Loc,
11956 Defining_Identifier =>
11957 Make_Defining_Identifier (Loc, Name_uSize),
11958
11959 Component_Definition =>
11960 Make_Component_Definition (Loc,
11961 Aliased_Present => False,
11962 Subtype_Indication =>
11963 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
11964
11965 Expression =>
11966 Convert_To (RTE (RE_Size_Type),
11967 Relocate_Node (
11968 Expression (First (
11969 Pragma_Argument_Associations (
11970 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11971 end if;
11972
11973 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
11974 -- rep item is present.
11975
11976 if Has_Rep_Item
11977 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
11978 then
11979 Append_To (Cdecls,
11980 Make_Component_Declaration (Loc,
11981 Defining_Identifier =>
11982 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
11983
11984 Component_Definition =>
11985 Make_Component_Definition (Loc,
11986 Aliased_Present => False,
11987 Subtype_Indication =>
11988 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
11989 end if;
11990
11991 -- Add the _Task_Info component if a Task_Info pragma is present
11992
11993 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11994 Append_To (Cdecls,
11995 Make_Component_Declaration (Loc,
11996 Defining_Identifier =>
11997 Make_Defining_Identifier (Loc, Name_uTask_Info),
11998
11999 Component_Definition =>
12000 Make_Component_Definition (Loc,
12001 Aliased_Present => False,
12002 Subtype_Indication =>
12003 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12004
12005 Expression => New_Copy (
12006 Expression (First (
12007 Pragma_Argument_Associations (
12008 Get_Rep_Pragma
12009 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12010 end if;
12011
12012 -- Add the _CPU component if a CPU rep item is present
12013
12014 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12015 Append_To (Cdecls,
12016 Make_Component_Declaration (Loc,
12017 Defining_Identifier =>
12018 Make_Defining_Identifier (Loc, Name_uCPU),
12019
12020 Component_Definition =>
12021 Make_Component_Definition (Loc,
12022 Aliased_Present => False,
12023 Subtype_Indication =>
12024 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12025 end if;
12026
12027 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12028 -- present. If we are using a restricted run time this component will
12029 -- not be added (deadlines are not allowed by the Ravenscar profile).
12030
12031 if not Restricted_Profile
12032 and then Present (Taskdef)
12033 and then Has_Relative_Deadline_Pragma (Taskdef)
12034 then
12035 Append_To (Cdecls,
12036 Make_Component_Declaration (Loc,
12037 Defining_Identifier =>
12038 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12039
12040 Component_Definition =>
12041 Make_Component_Definition (Loc,
12042 Aliased_Present => False,
12043 Subtype_Indication =>
12044 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12045
12046 Expression =>
12047 Convert_To (RTE (RE_Time_Span),
12048 Relocate_Node (
12049 Expression (First (
12050 Pragma_Argument_Associations (
12051 Get_Relative_Deadline_Pragma (Taskdef))))))));
12052 end if;
12053
12054 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12055 -- item is present. If we are using a restricted run time this component
12056 -- will not be added (dispatching domains are not allowed by the
12057 -- Ravenscar profile).
12058
12059 if not Restricted_Profile
12060 and then
12061 Has_Rep_Item
12062 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12063 then
12064 Append_To (Cdecls,
12065 Make_Component_Declaration (Loc,
12066 Defining_Identifier =>
12067 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12068
12069 Component_Definition =>
12070 Make_Component_Definition (Loc,
12071 Aliased_Present => False,
12072 Subtype_Indication =>
12073 New_Occurrence_Of
12074 (RTE (RE_Dispatching_Domain_Access), Loc))));
12075 end if;
12076
12077 Insert_After (Size_Decl, Rec_Decl);
12078
12079 -- Analyze the record declaration immediately after construction,
12080 -- because the initialization procedure is needed for single task
12081 -- declarations before the next entity is analyzed.
12082
12083 Analyze (Rec_Decl);
12084
12085 -- Create the declaration of the task body procedure
12086
12087 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12088 Body_Decl :=
12089 Make_Subprogram_Declaration (Loc,
12090 Specification => Proc_Spec);
12091 Set_Is_Task_Body_Procedure (Body_Decl);
12092
12093 Insert_After (Rec_Decl, Body_Decl);
12094
12095 -- The subprogram does not comes from source, so we have to indicate the
12096 -- need for debugging information explicitly.
12097
12098 if Comes_From_Source (Original_Node (N)) then
12099 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12100 end if;
12101
12102 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12103 -- the corresponding record has been frozen.
12104
12105 if Ada_Version >= Ada_2005 then
12106 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12107 end if;
12108
12109 -- Ada 2005 (AI-345): We must defer freezing to allow further
12110 -- declaration of primitive subprograms covering task interfaces
12111
12112 if Ada_Version <= Ada_95 then
12113
12114 -- Now we can freeze the corresponding record. This needs manually
12115 -- freezing, since it is really part of the task type, and the task
12116 -- type is frozen at this stage. We of course need the initialization
12117 -- procedure for this corresponding record type and we won't get it
12118 -- in time if we don't freeze now.
12119
12120 declare
12121 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12122 begin
12123 if Is_Non_Empty_List (L) then
12124 Insert_List_After (Body_Decl, L);
12125 end if;
12126 end;
12127 end if;
12128
12129 -- Complete the expansion of access types to the current task type, if
12130 -- any were declared.
12131
12132 Expand_Previous_Access_Type (Tasktyp);
12133
12134 -- Create wrappers for entries that have contract cases, preconditions
12135 -- and postconditions.
12136
12137 declare
12138 Ent : Entity_Id;
12139
12140 begin
12141 Ent := First_Entity (Tasktyp);
12142 while Present (Ent) loop
12143 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12144 Build_Contract_Wrapper (Ent, N);
12145 end if;
12146
12147 Next_Entity (Ent);
12148 end loop;
12149 end;
12150 end Expand_N_Task_Type_Declaration;
12151
12152 -------------------------------
12153 -- Expand_N_Timed_Entry_Call --
12154 -------------------------------
12155
12156 -- A timed entry call in normal case is not implemented using ATC mechanism
12157 -- anymore for efficiency reason.
12158
12159 -- select
12160 -- T.E;
12161 -- S1;
12162 -- or
12163 -- delay D;
12164 -- S2;
12165 -- end select;
12166
12167 -- is expanded as follows:
12168
12169 -- 1) When T.E is a task entry_call;
12170
12171 -- declare
12172 -- B : Boolean;
12173 -- X : Task_Entry_Index := <entry index>;
12174 -- DX : Duration := To_Duration (D);
12175 -- M : Delay_Mode := <discriminant>;
12176 -- P : parms := (parm, parm, parm);
12177
12178 -- begin
12179 -- Timed_Protected_Entry_Call
12180 -- (<acceptor-task>, X, P'Address, DX, M, B);
12181 -- if B then
12182 -- S1;
12183 -- else
12184 -- S2;
12185 -- end if;
12186 -- end;
12187
12188 -- 2) When T.E is a protected entry_call;
12189
12190 -- declare
12191 -- B : Boolean;
12192 -- X : Protected_Entry_Index := <entry index>;
12193 -- DX : Duration := To_Duration (D);
12194 -- M : Delay_Mode := <discriminant>;
12195 -- P : parms := (parm, parm, parm);
12196
12197 -- begin
12198 -- Timed_Protected_Entry_Call
12199 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12200 -- if B then
12201 -- S1;
12202 -- else
12203 -- S2;
12204 -- end if;
12205 -- end;
12206
12207 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12208 -- is no delay and the triggering statements are executed. We first
12209 -- determine the kind of the triggering call and then execute a
12210 -- synchronized operation or a direct call.
12211
12212 -- declare
12213 -- B : Boolean := False;
12214 -- C : Ada.Tags.Prim_Op_Kind;
12215 -- DX : Duration := To_Duration (D)
12216 -- K : Ada.Tags.Tagged_Kind :=
12217 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12218 -- M : Integer :=...;
12219 -- P : Parameters := (Param1 .. ParamN);
12220 -- S : Integer;
12221
12222 -- begin
12223 -- if K = Ada.Tags.TK_Limited_Tagged
12224 -- or else K = Ada.Tags.TK_Tagged
12225 -- then
12226 -- <dispatching-call>;
12227 -- B := True;
12228
12229 -- else
12230 -- S :=
12231 -- Ada.Tags.Get_Offset_Index
12232 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12233
12234 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12235
12236 -- if C = POK_Protected_Entry
12237 -- or else C = POK_Task_Entry
12238 -- then
12239 -- Param1 := P.Param1;
12240 -- ...
12241 -- ParamN := P.ParamN;
12242 -- end if;
12243
12244 -- if B then
12245 -- if C = POK_Procedure
12246 -- or else C = POK_Protected_Procedure
12247 -- or else C = POK_Task_Procedure
12248 -- then
12249 -- <dispatching-call>;
12250 -- end if;
12251 -- end if;
12252 -- end if;
12253
12254 -- if B then
12255 -- <triggering-statements>
12256 -- else
12257 -- <timed-statements>
12258 -- end if;
12259 -- end;
12260
12261 -- The triggering statement and the sequence of timed statements have not
12262 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12263 -- global references if within an instantiation.
12264
12265 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12266 Loc : constant Source_Ptr := Sloc (N);
12267
12268 Actuals : List_Id;
12269 Blk_Typ : Entity_Id;
12270 Call : Node_Id;
12271 Call_Ent : Entity_Id;
12272 Conc_Typ_Stmts : List_Id;
12273 Concval : Node_Id;
12274 D_Alt : constant Node_Id := Delay_Alternative (N);
12275 D_Conv : Node_Id;
12276 D_Disc : Node_Id;
12277 D_Stat : Node_Id := Delay_Statement (D_Alt);
12278 D_Stats : List_Id;
12279 D_Type : Entity_Id;
12280 Decls : List_Id;
12281 Dummy : Node_Id;
12282 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12283 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12284 E_Stats : List_Id;
12285 Ename : Node_Id;
12286 Formals : List_Id;
12287 Index : Node_Id;
12288 Is_Disp_Select : Boolean;
12289 Lim_Typ_Stmts : List_Id;
12290 N_Stats : List_Id;
12291 Obj : Entity_Id;
12292 Param : Node_Id;
12293 Params : List_Id;
12294 Stmt : Node_Id;
12295 Stmts : List_Id;
12296 Unpack : List_Id;
12297
12298 B : Entity_Id; -- Call status flag
12299 C : Entity_Id; -- Call kind
12300 D : Entity_Id; -- Delay
12301 K : Entity_Id; -- Tagged kind
12302 M : Entity_Id; -- Delay mode
12303 P : Entity_Id; -- Parameter block
12304 S : Entity_Id; -- Primitive operation slot
12305
12306 -- Start of processing for Expand_N_Timed_Entry_Call
12307
12308 begin
12309 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12310 -- was already reported on spec, so do not attempt to expand the call.
12311
12312 if Restriction_Active (No_Select_Statements) then
12313 return;
12314 end if;
12315
12316 Process_Statements_For_Controlled_Objects (E_Alt);
12317 Process_Statements_For_Controlled_Objects (D_Alt);
12318
12319 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12320
12321 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12322 -- may wrap them in blocks.
12323
12324 E_Stats := Statements (E_Alt);
12325 D_Stats := Statements (D_Alt);
12326
12327 -- The arguments in the call may require dynamic allocation, and the
12328 -- call statement may have been transformed into a block. The block
12329 -- may contain additional declarations for internal entities, and the
12330 -- original call is found by sequential search.
12331
12332 if Nkind (E_Call) = N_Block_Statement then
12333 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12334 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12335 N_Entry_Call_Statement)
12336 loop
12337 Next (E_Call);
12338 end loop;
12339 end if;
12340
12341 Is_Disp_Select :=
12342 Ada_Version >= Ada_2005
12343 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12344
12345 if Is_Disp_Select then
12346 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12347 Decls := New_List;
12348
12349 Stmts := New_List;
12350
12351 -- Generate:
12352 -- B : Boolean := False;
12353
12354 B := Build_B (Loc, Decls);
12355
12356 -- Generate:
12357 -- C : Ada.Tags.Prim_Op_Kind;
12358
12359 C := Build_C (Loc, Decls);
12360
12361 -- Because the analysis of all statements was disabled, manually
12362 -- analyze the delay statement.
12363
12364 Analyze (D_Stat);
12365 D_Stat := Original_Node (D_Stat);
12366
12367 else
12368 -- Build an entry call using Simple_Entry_Call
12369
12370 Extract_Entry (E_Call, Concval, Ename, Index);
12371 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12372
12373 Decls := Declarations (E_Call);
12374 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12375
12376 if No (Decls) then
12377 Decls := New_List;
12378 end if;
12379
12380 -- Generate:
12381 -- B : Boolean;
12382
12383 B := Make_Defining_Identifier (Loc, Name_uB);
12384
12385 Prepend_To (Decls,
12386 Make_Object_Declaration (Loc,
12387 Defining_Identifier => B,
12388 Object_Definition =>
12389 New_Occurrence_Of (Standard_Boolean, Loc)));
12390 end if;
12391
12392 -- Duration and mode processing
12393
12394 D_Type := Base_Type (Etype (Expression (D_Stat)));
12395
12396 -- Use the type of the delay expression (Calendar or Real_Time) to
12397 -- generate the appropriate conversion.
12398
12399 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12400 D_Disc := Make_Integer_Literal (Loc, 0);
12401 D_Conv := Relocate_Node (Expression (D_Stat));
12402
12403 elsif Is_RTE (D_Type, RO_CA_Time) then
12404 D_Disc := Make_Integer_Literal (Loc, 1);
12405 D_Conv :=
12406 Make_Function_Call (Loc,
12407 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12408 Parameter_Associations =>
12409 New_List (New_Copy (Expression (D_Stat))));
12410
12411 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12412 D_Disc := Make_Integer_Literal (Loc, 2);
12413 D_Conv :=
12414 Make_Function_Call (Loc,
12415 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12416 Parameter_Associations =>
12417 New_List (New_Copy (Expression (D_Stat))));
12418 end if;
12419
12420 D := Make_Temporary (Loc, 'D');
12421
12422 -- Generate:
12423 -- D : Duration;
12424
12425 Append_To (Decls,
12426 Make_Object_Declaration (Loc,
12427 Defining_Identifier => D,
12428 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12429
12430 M := Make_Temporary (Loc, 'M');
12431
12432 -- Generate:
12433 -- M : Integer := (0 | 1 | 2);
12434
12435 Append_To (Decls,
12436 Make_Object_Declaration (Loc,
12437 Defining_Identifier => M,
12438 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12439 Expression => D_Disc));
12440
12441 -- Do the assignment at this stage only because the evaluation of the
12442 -- expression must not occur before (see ACVC C97302A).
12443
12444 Append_To (Stmts,
12445 Make_Assignment_Statement (Loc,
12446 Name => New_Occurrence_Of (D, Loc),
12447 Expression => D_Conv));
12448
12449 -- Parameter block processing
12450
12451 -- Manually create the parameter block for dispatching calls. In the
12452 -- case of entries, the block has already been created during the call
12453 -- to Build_Simple_Entry_Call.
12454
12455 if Is_Disp_Select then
12456
12457 -- Tagged kind processing, generate:
12458 -- K : Ada.Tags.Tagged_Kind :=
12459 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12460
12461 K := Build_K (Loc, Decls, Obj);
12462
12463 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12464 P :=
12465 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12466
12467 -- Dispatch table slot processing, generate:
12468 -- S : Integer;
12469
12470 S := Build_S (Loc, Decls);
12471
12472 -- Generate:
12473 -- S := Ada.Tags.Get_Offset_Index
12474 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12475
12476 Conc_Typ_Stmts :=
12477 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12478
12479 -- Generate:
12480 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12481
12482 -- where Obj is the controlling formal parameter, S is the dispatch
12483 -- table slot number of the dispatching operation, P is the wrapped
12484 -- parameter block, D is the duration, M is the duration mode, C is
12485 -- the call kind and B is the call status.
12486
12487 Params := New_List;
12488
12489 Append_To (Params, New_Copy_Tree (Obj));
12490 Append_To (Params, New_Occurrence_Of (S, Loc));
12491 Append_To (Params,
12492 Make_Attribute_Reference (Loc,
12493 Prefix => New_Occurrence_Of (P, Loc),
12494 Attribute_Name => Name_Address));
12495 Append_To (Params, New_Occurrence_Of (D, Loc));
12496 Append_To (Params, New_Occurrence_Of (M, Loc));
12497 Append_To (Params, New_Occurrence_Of (C, Loc));
12498 Append_To (Params, New_Occurrence_Of (B, Loc));
12499
12500 Append_To (Conc_Typ_Stmts,
12501 Make_Procedure_Call_Statement (Loc,
12502 Name =>
12503 New_Occurrence_Of
12504 (Find_Prim_Op
12505 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12506 Parameter_Associations => Params));
12507
12508 -- Generate:
12509 -- if C = POK_Protected_Entry
12510 -- or else C = POK_Task_Entry
12511 -- then
12512 -- Param1 := P.Param1;
12513 -- ...
12514 -- ParamN := P.ParamN;
12515 -- end if;
12516
12517 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12518
12519 -- Generate the if statement only when the packed parameters need
12520 -- explicit assignments to their corresponding actuals.
12521
12522 if Present (Unpack) then
12523 Append_To (Conc_Typ_Stmts,
12524 Make_Implicit_If_Statement (N,
12525
12526 Condition =>
12527 Make_Or_Else (Loc,
12528 Left_Opnd =>
12529 Make_Op_Eq (Loc,
12530 Left_Opnd => New_Occurrence_Of (C, Loc),
12531 Right_Opnd =>
12532 New_Occurrence_Of
12533 (RTE (RE_POK_Protected_Entry), Loc)),
12534
12535 Right_Opnd =>
12536 Make_Op_Eq (Loc,
12537 Left_Opnd => New_Occurrence_Of (C, Loc),
12538 Right_Opnd =>
12539 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12540
12541 Then_Statements => Unpack));
12542 end if;
12543
12544 -- Generate:
12545
12546 -- if B then
12547 -- if C = POK_Procedure
12548 -- or else C = POK_Protected_Procedure
12549 -- or else C = POK_Task_Procedure
12550 -- then
12551 -- <dispatching-call>
12552 -- end if;
12553 -- end if;
12554
12555 N_Stats := New_List (
12556 Make_Implicit_If_Statement (N,
12557 Condition =>
12558 Make_Or_Else (Loc,
12559 Left_Opnd =>
12560 Make_Op_Eq (Loc,
12561 Left_Opnd => New_Occurrence_Of (C, Loc),
12562 Right_Opnd =>
12563 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12564
12565 Right_Opnd =>
12566 Make_Or_Else (Loc,
12567 Left_Opnd =>
12568 Make_Op_Eq (Loc,
12569 Left_Opnd => New_Occurrence_Of (C, Loc),
12570 Right_Opnd =>
12571 New_Occurrence_Of (RTE (
12572 RE_POK_Protected_Procedure), Loc)),
12573 Right_Opnd =>
12574 Make_Op_Eq (Loc,
12575 Left_Opnd => New_Occurrence_Of (C, Loc),
12576 Right_Opnd =>
12577 New_Occurrence_Of
12578 (RTE (RE_POK_Task_Procedure), Loc)))),
12579
12580 Then_Statements => New_List (E_Call)));
12581
12582 Append_To (Conc_Typ_Stmts,
12583 Make_Implicit_If_Statement (N,
12584 Condition => New_Occurrence_Of (B, Loc),
12585 Then_Statements => N_Stats));
12586
12587 -- Generate:
12588 -- <dispatching-call>;
12589 -- B := True;
12590
12591 Lim_Typ_Stmts :=
12592 New_List (New_Copy_Tree (E_Call),
12593 Make_Assignment_Statement (Loc,
12594 Name => New_Occurrence_Of (B, Loc),
12595 Expression => New_Occurrence_Of (Standard_True, Loc)));
12596
12597 -- Generate:
12598 -- if K = Ada.Tags.TK_Limited_Tagged
12599 -- or else K = Ada.Tags.TK_Tagged
12600 -- then
12601 -- Lim_Typ_Stmts
12602 -- else
12603 -- Conc_Typ_Stmts
12604 -- end if;
12605
12606 Append_To (Stmts,
12607 Make_Implicit_If_Statement (N,
12608 Condition => Build_Dispatching_Tag_Check (K, N),
12609 Then_Statements => Lim_Typ_Stmts,
12610 Else_Statements => Conc_Typ_Stmts));
12611
12612 -- Generate:
12613
12614 -- if B then
12615 -- <triggering-statements>
12616 -- else
12617 -- <timed-statements>
12618 -- end if;
12619
12620 Append_To (Stmts,
12621 Make_Implicit_If_Statement (N,
12622 Condition => New_Occurrence_Of (B, Loc),
12623 Then_Statements => E_Stats,
12624 Else_Statements => D_Stats));
12625
12626 else
12627 -- Simple case of a nondispatching trigger. Skip assignments to
12628 -- temporaries created for in-out parameters.
12629
12630 -- This makes unwarranted assumptions about the shape of the expanded
12631 -- tree for the call, and should be cleaned up ???
12632
12633 Stmt := First (Stmts);
12634 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12635 Next (Stmt);
12636 end loop;
12637
12638 -- Do the assignment at this stage only because the evaluation
12639 -- of the expression must not occur before (see ACVC C97302A).
12640
12641 Insert_Before (Stmt,
12642 Make_Assignment_Statement (Loc,
12643 Name => New_Occurrence_Of (D, Loc),
12644 Expression => D_Conv));
12645
12646 Call := Stmt;
12647 Params := Parameter_Associations (Call);
12648
12649 -- For a protected type, we build a Timed_Protected_Entry_Call
12650
12651 if Is_Protected_Type (Etype (Concval)) then
12652
12653 -- Create a new call statement
12654
12655 Param := First (Params);
12656 while Present (Param)
12657 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12658 loop
12659 Next (Param);
12660 end loop;
12661
12662 Dummy := Remove_Next (Next (Param));
12663
12664 -- Remove garbage is following the Cancel_Param if present
12665
12666 Dummy := Next (Param);
12667
12668 -- Remove the mode of the Protected_Entry_Call call, then remove
12669 -- the Communication_Block of the Protected_Entry_Call call, and
12670 -- finally add Duration and a Delay_Mode parameter
12671
12672 pragma Assert (Present (Param));
12673 Rewrite (Param, New_Occurrence_Of (D, Loc));
12674
12675 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12676
12677 -- Add a Boolean flag for successful entry call
12678
12679 Append_To (Params, New_Occurrence_Of (B, Loc));
12680
12681 case Corresponding_Runtime_Package (Etype (Concval)) is
12682 when System_Tasking_Protected_Objects_Entries =>
12683 Rewrite (Call,
12684 Make_Procedure_Call_Statement (Loc,
12685 Name =>
12686 New_Occurrence_Of
12687 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12688 Parameter_Associations => Params));
12689
12690 when others =>
12691 raise Program_Error;
12692 end case;
12693
12694 -- For the task case, build a Timed_Task_Entry_Call
12695
12696 else
12697 -- Create a new call statement
12698
12699 Append_To (Params, New_Occurrence_Of (D, Loc));
12700 Append_To (Params, New_Occurrence_Of (M, Loc));
12701 Append_To (Params, New_Occurrence_Of (B, Loc));
12702
12703 Rewrite (Call,
12704 Make_Procedure_Call_Statement (Loc,
12705 Name =>
12706 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12707 Parameter_Associations => Params));
12708 end if;
12709
12710 Append_To (Stmts,
12711 Make_Implicit_If_Statement (N,
12712 Condition => New_Occurrence_Of (B, Loc),
12713 Then_Statements => E_Stats,
12714 Else_Statements => D_Stats));
12715 end if;
12716
12717 Rewrite (N,
12718 Make_Block_Statement (Loc,
12719 Declarations => Decls,
12720 Handled_Statement_Sequence =>
12721 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12722
12723 Analyze (N);
12724 end Expand_N_Timed_Entry_Call;
12725
12726 ----------------------------------------
12727 -- Expand_Protected_Body_Declarations --
12728 ----------------------------------------
12729
12730 procedure Expand_Protected_Body_Declarations
12731 (N : Node_Id;
12732 Spec_Id : Entity_Id)
12733 is
12734 begin
12735 if No_Run_Time_Mode then
12736 Error_Msg_CRT ("protected body", N);
12737 return;
12738
12739 elsif Expander_Active then
12740
12741 -- Associate discriminals with the first subprogram or entry body to
12742 -- be expanded.
12743
12744 if Present (First_Protected_Operation (Declarations (N))) then
12745 Set_Discriminals (Parent (Spec_Id));
12746 end if;
12747 end if;
12748 end Expand_Protected_Body_Declarations;
12749
12750 -------------------------
12751 -- External_Subprogram --
12752 -------------------------
12753
12754 function External_Subprogram (E : Entity_Id) return Entity_Id is
12755 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12756
12757 begin
12758 -- The internal and external subprograms follow each other on the entity
12759 -- chain. Note that previously private operations had no separate
12760 -- external subprogram. We now create one in all cases, because a
12761 -- private operation may actually appear in an external call, through
12762 -- a 'Access reference used for a callback.
12763
12764 -- If the operation is a function that returns an anonymous access type,
12765 -- the corresponding itype appears before the operation, and must be
12766 -- skipped.
12767
12768 -- This mechanism is fragile, there should be a real link between the
12769 -- two versions of the operation, but there is no place to put it ???
12770
12771 if Is_Access_Type (Next_Entity (Subp)) then
12772 return Next_Entity (Next_Entity (Subp));
12773 else
12774 return Next_Entity (Subp);
12775 end if;
12776 end External_Subprogram;
12777
12778 ------------------------------
12779 -- Extract_Dispatching_Call --
12780 ------------------------------
12781
12782 procedure Extract_Dispatching_Call
12783 (N : Node_Id;
12784 Call_Ent : out Entity_Id;
12785 Object : out Entity_Id;
12786 Actuals : out List_Id;
12787 Formals : out List_Id)
12788 is
12789 Call_Nam : Node_Id;
12790
12791 begin
12792 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12793
12794 if Present (Original_Node (N)) then
12795 Call_Nam := Name (Original_Node (N));
12796 else
12797 Call_Nam := Name (N);
12798 end if;
12799
12800 -- Retrieve the name of the dispatching procedure. It contains the
12801 -- dispatch table slot number.
12802
12803 loop
12804 case Nkind (Call_Nam) is
12805 when N_Identifier =>
12806 exit;
12807
12808 when N_Selected_Component =>
12809 Call_Nam := Selector_Name (Call_Nam);
12810
12811 when others =>
12812 raise Program_Error;
12813 end case;
12814 end loop;
12815
12816 Actuals := Parameter_Associations (N);
12817 Call_Ent := Entity (Call_Nam);
12818 Formals := Parameter_Specifications (Parent (Call_Ent));
12819 Object := First (Actuals);
12820
12821 if Present (Original_Node (Object)) then
12822 Object := Original_Node (Object);
12823 end if;
12824
12825 -- If the type of the dispatching object is an access type then return
12826 -- an explicit dereference.
12827
12828 if Is_Access_Type (Etype (Object)) then
12829 Object := Make_Explicit_Dereference (Sloc (N), Object);
12830 Analyze (Object);
12831 end if;
12832 end Extract_Dispatching_Call;
12833
12834 -------------------
12835 -- Extract_Entry --
12836 -------------------
12837
12838 procedure Extract_Entry
12839 (N : Node_Id;
12840 Concval : out Node_Id;
12841 Ename : out Node_Id;
12842 Index : out Node_Id)
12843 is
12844 Nam : constant Node_Id := Name (N);
12845
12846 begin
12847 -- For a simple entry, the name is a selected component, with the
12848 -- prefix being the task value, and the selector being the entry.
12849
12850 if Nkind (Nam) = N_Selected_Component then
12851 Concval := Prefix (Nam);
12852 Ename := Selector_Name (Nam);
12853 Index := Empty;
12854
12855 -- For a member of an entry family, the name is an indexed component
12856 -- where the prefix is a selected component, whose prefix in turn is
12857 -- the task value, and whose selector is the entry family. The single
12858 -- expression in the expressions list of the indexed component is the
12859 -- subscript for the family.
12860
12861 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12862 Concval := Prefix (Prefix (Nam));
12863 Ename := Selector_Name (Prefix (Nam));
12864 Index := First (Expressions (Nam));
12865 end if;
12866
12867 -- Through indirection, the type may actually be a limited view of a
12868 -- concurrent type. When compiling a call, the non-limited view of the
12869 -- type is visible.
12870
12871 if From_Limited_With (Etype (Concval)) then
12872 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
12873 end if;
12874 end Extract_Entry;
12875
12876 -------------------
12877 -- Family_Offset --
12878 -------------------
12879
12880 function Family_Offset
12881 (Loc : Source_Ptr;
12882 Hi : Node_Id;
12883 Lo : Node_Id;
12884 Ttyp : Entity_Id;
12885 Cap : Boolean) return Node_Id
12886 is
12887 Ityp : Entity_Id;
12888 Real_Hi : Node_Id;
12889 Real_Lo : Node_Id;
12890
12891 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12892 -- If one of the bounds is a reference to a discriminant, replace with
12893 -- corresponding discriminal of type. Within the body of a task retrieve
12894 -- the renamed discriminant by simple visibility, using its generated
12895 -- name. Within a protected object, find the original discriminant and
12896 -- replace it with the discriminal of the current protected operation.
12897
12898 ------------------------------
12899 -- Convert_Discriminant_Ref --
12900 ------------------------------
12901
12902 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12903 Loc : constant Source_Ptr := Sloc (Bound);
12904 B : Node_Id;
12905 D : Entity_Id;
12906
12907 begin
12908 if Is_Entity_Name (Bound)
12909 and then Ekind (Entity (Bound)) = E_Discriminant
12910 then
12911 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
12912 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12913 Find_Direct_Name (B);
12914
12915 elsif Is_Protected_Type (Ttyp) then
12916 D := First_Discriminant (Ttyp);
12917 while Chars (D) /= Chars (Entity (Bound)) loop
12918 Next_Discriminant (D);
12919 end loop;
12920
12921 B := New_Occurrence_Of (Discriminal (D), Loc);
12922
12923 else
12924 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
12925 end if;
12926
12927 elsif Nkind (Bound) = N_Attribute_Reference then
12928 return Bound;
12929
12930 else
12931 B := New_Copy_Tree (Bound);
12932 end if;
12933
12934 return
12935 Make_Attribute_Reference (Loc,
12936 Attribute_Name => Name_Pos,
12937 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12938 Expressions => New_List (B));
12939 end Convert_Discriminant_Ref;
12940
12941 -- Start of processing for Family_Offset
12942
12943 begin
12944 Real_Hi := Convert_Discriminant_Ref (Hi);
12945 Real_Lo := Convert_Discriminant_Ref (Lo);
12946
12947 if Cap then
12948 if Is_Task_Type (Ttyp) then
12949 Ityp := RTE (RE_Task_Entry_Index);
12950 else
12951 Ityp := RTE (RE_Protected_Entry_Index);
12952 end if;
12953
12954 Real_Hi :=
12955 Make_Attribute_Reference (Loc,
12956 Prefix => New_Occurrence_Of (Ityp, Loc),
12957 Attribute_Name => Name_Min,
12958 Expressions => New_List (
12959 Real_Hi,
12960 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12961
12962 Real_Lo :=
12963 Make_Attribute_Reference (Loc,
12964 Prefix => New_Occurrence_Of (Ityp, Loc),
12965 Attribute_Name => Name_Max,
12966 Expressions => New_List (
12967 Real_Lo,
12968 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12969 end if;
12970
12971 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12972 end Family_Offset;
12973
12974 -----------------
12975 -- Family_Size --
12976 -----------------
12977
12978 function Family_Size
12979 (Loc : Source_Ptr;
12980 Hi : Node_Id;
12981 Lo : Node_Id;
12982 Ttyp : Entity_Id;
12983 Cap : Boolean) return Node_Id
12984 is
12985 Ityp : Entity_Id;
12986
12987 begin
12988 if Is_Task_Type (Ttyp) then
12989 Ityp := RTE (RE_Task_Entry_Index);
12990 else
12991 Ityp := RTE (RE_Protected_Entry_Index);
12992 end if;
12993
12994 return
12995 Make_Attribute_Reference (Loc,
12996 Prefix => New_Occurrence_Of (Ityp, Loc),
12997 Attribute_Name => Name_Max,
12998 Expressions => New_List (
12999 Make_Op_Add (Loc,
13000 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13001 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13002 Make_Integer_Literal (Loc, 0)));
13003 end Family_Size;
13004
13005 ----------------------------
13006 -- Find_Enclosing_Context --
13007 ----------------------------
13008
13009 procedure Find_Enclosing_Context
13010 (N : Node_Id;
13011 Context : out Node_Id;
13012 Context_Id : out Entity_Id;
13013 Context_Decls : out List_Id)
13014 is
13015 begin
13016 -- Traverse the parent chain looking for an enclosing body, block,
13017 -- package or return statement.
13018
13019 Context := Parent (N);
13020 while Present (Context) loop
13021 if Nkind_In (Context, N_Entry_Body,
13022 N_Extended_Return_Statement,
13023 N_Package_Body,
13024 N_Package_Declaration,
13025 N_Subprogram_Body,
13026 N_Task_Body)
13027 then
13028 exit;
13029
13030 -- Do not consider block created to protect a list of statements with
13031 -- an Abort_Defer / Abort_Undefer_Direct pair.
13032
13033 elsif Nkind (Context) = N_Block_Statement
13034 and then not Is_Abort_Block (Context)
13035 then
13036 exit;
13037 end if;
13038
13039 Context := Parent (Context);
13040 end loop;
13041
13042 pragma Assert (Present (Context));
13043
13044 -- Extract the constituents of the context
13045
13046 if Nkind (Context) = N_Extended_Return_Statement then
13047 Context_Decls := Return_Object_Declarations (Context);
13048 Context_Id := Return_Statement_Entity (Context);
13049
13050 -- Package declarations and bodies use a common library-level activation
13051 -- chain or task master, therefore return the package declaration as the
13052 -- proper carrier for the appropriate flag.
13053
13054 elsif Nkind (Context) = N_Package_Body then
13055 Context_Decls := Declarations (Context);
13056 Context_Id := Corresponding_Spec (Context);
13057 Context := Parent (Context_Id);
13058
13059 if Nkind (Context) = N_Defining_Program_Unit_Name then
13060 Context := Parent (Parent (Context));
13061 else
13062 Context := Parent (Context);
13063 end if;
13064
13065 elsif Nkind (Context) = N_Package_Declaration then
13066 Context_Decls := Visible_Declarations (Specification (Context));
13067 Context_Id := Defining_Unit_Name (Specification (Context));
13068
13069 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13070 Context_Id := Defining_Identifier (Context_Id);
13071 end if;
13072
13073 else
13074 if Nkind (Context) = N_Block_Statement then
13075 Context_Id := Entity (Identifier (Context));
13076
13077 elsif Nkind (Context) = N_Entry_Body then
13078 Context_Id := Defining_Identifier (Context);
13079
13080 elsif Nkind (Context) = N_Subprogram_Body then
13081 if Present (Corresponding_Spec (Context)) then
13082 Context_Id := Corresponding_Spec (Context);
13083 else
13084 Context_Id := Defining_Unit_Name (Specification (Context));
13085
13086 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13087 Context_Id := Defining_Identifier (Context_Id);
13088 end if;
13089 end if;
13090
13091 elsif Nkind (Context) = N_Task_Body then
13092 Context_Id := Corresponding_Spec (Context);
13093
13094 else
13095 raise Program_Error;
13096 end if;
13097
13098 Context_Decls := Declarations (Context);
13099 end if;
13100
13101 pragma Assert (Present (Context_Id));
13102 pragma Assert (Present (Context_Decls));
13103 end Find_Enclosing_Context;
13104
13105 -----------------------
13106 -- Find_Master_Scope --
13107 -----------------------
13108
13109 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13110 S : Entity_Id;
13111
13112 begin
13113 -- In Ada 2005, the master is the innermost enclosing scope that is not
13114 -- transient. If the enclosing block is the rewriting of a call or the
13115 -- scope is an extended return statement this is valid master. The
13116 -- master in an extended return is only used within the return, and is
13117 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13118 -- now before that overwriting occurs.
13119
13120 S := Scope (E);
13121
13122 if Ada_Version >= Ada_2005 then
13123 while Is_Internal (S) loop
13124 if Nkind (Parent (S)) = N_Block_Statement
13125 and then
13126 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13127 then
13128 exit;
13129
13130 elsif Ekind (S) = E_Return_Statement then
13131 exit;
13132
13133 else
13134 S := Scope (S);
13135 end if;
13136 end loop;
13137 end if;
13138
13139 return S;
13140 end Find_Master_Scope;
13141
13142 -------------------------------
13143 -- First_Protected_Operation --
13144 -------------------------------
13145
13146 function First_Protected_Operation (D : List_Id) return Node_Id is
13147 First_Op : Node_Id;
13148
13149 begin
13150 First_Op := First (D);
13151 while Present (First_Op)
13152 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13153 loop
13154 Next (First_Op);
13155 end loop;
13156
13157 return First_Op;
13158 end First_Protected_Operation;
13159
13160 ---------------------------------------
13161 -- Install_Private_Data_Declarations --
13162 ---------------------------------------
13163
13164 procedure Install_Private_Data_Declarations
13165 (Loc : Source_Ptr;
13166 Spec_Id : Entity_Id;
13167 Conc_Typ : Entity_Id;
13168 Body_Nod : Node_Id;
13169 Decls : List_Id;
13170 Barrier : Boolean := False;
13171 Family : Boolean := False)
13172 is
13173 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13174 Decl : Node_Id;
13175 Def : Node_Id;
13176 Insert_Node : Node_Id := Empty;
13177 Obj_Ent : Entity_Id;
13178
13179 procedure Add (Decl : Node_Id);
13180 -- Add a single declaration after Insert_Node. If this is the first
13181 -- addition, Decl is added to the front of Decls and it becomes the
13182 -- insertion node.
13183
13184 function Replace_Bound (Bound : Node_Id) return Node_Id;
13185 -- The bounds of an entry index may depend on discriminants, create a
13186 -- reference to the corresponding prival. Otherwise return a duplicate
13187 -- of the original bound.
13188
13189 ---------
13190 -- Add --
13191 ---------
13192
13193 procedure Add (Decl : Node_Id) is
13194 begin
13195 if No (Insert_Node) then
13196 Prepend_To (Decls, Decl);
13197 else
13198 Insert_After (Insert_Node, Decl);
13199 end if;
13200
13201 Insert_Node := Decl;
13202 end Add;
13203
13204 --------------------------
13205 -- Replace_Discriminant --
13206 --------------------------
13207
13208 function Replace_Bound (Bound : Node_Id) return Node_Id is
13209 begin
13210 if Nkind (Bound) = N_Identifier
13211 and then Is_Discriminal (Entity (Bound))
13212 then
13213 return Make_Identifier (Loc, Chars (Entity (Bound)));
13214 else
13215 return Duplicate_Subexpr (Bound);
13216 end if;
13217 end Replace_Bound;
13218
13219 -- Start of processing for Install_Private_Data_Declarations
13220
13221 begin
13222 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13223 -- formal parameter _O, _object or _task depending on the context.
13224
13225 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13226
13227 -- Special processing of _O for barrier functions, protected entries
13228 -- and families.
13229
13230 if Barrier
13231 or else
13232 (Is_Protected
13233 and then
13234 (Ekind (Spec_Id) = E_Entry
13235 or else Ekind (Spec_Id) = E_Entry_Family))
13236 then
13237 declare
13238 Conc_Rec : constant Entity_Id :=
13239 Corresponding_Record_Type (Conc_Typ);
13240 Typ_Id : constant Entity_Id :=
13241 Make_Defining_Identifier (Loc,
13242 New_External_Name (Chars (Conc_Rec), 'P'));
13243 begin
13244 -- Generate:
13245 -- type prot_typVP is access prot_typV;
13246
13247 Decl :=
13248 Make_Full_Type_Declaration (Loc,
13249 Defining_Identifier => Typ_Id,
13250 Type_Definition =>
13251 Make_Access_To_Object_Definition (Loc,
13252 Subtype_Indication =>
13253 New_Occurrence_Of (Conc_Rec, Loc)));
13254 Add (Decl);
13255
13256 -- Generate:
13257 -- _object : prot_typVP := prot_typV (_O);
13258
13259 Decl :=
13260 Make_Object_Declaration (Loc,
13261 Defining_Identifier =>
13262 Make_Defining_Identifier (Loc, Name_uObject),
13263 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13264 Expression =>
13265 Unchecked_Convert_To (Typ_Id,
13266 New_Occurrence_Of (Obj_Ent, Loc)));
13267 Add (Decl);
13268
13269 -- Set the reference to the concurrent object
13270
13271 Obj_Ent := Defining_Identifier (Decl);
13272 end;
13273 end if;
13274
13275 -- Step 2: Create the Protection object and build its declaration for
13276 -- any protected entry (family) of subprogram. Note for the lock-free
13277 -- implementation, the Protection object is not needed anymore.
13278
13279 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13280 declare
13281 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13282 Prot_Typ : RE_Id;
13283
13284 begin
13285 Set_Protection_Object (Spec_Id, Prot_Ent);
13286
13287 -- Determine the proper protection type
13288
13289 if Has_Attach_Handler (Conc_Typ)
13290 and then not Restricted_Profile
13291 then
13292 Prot_Typ := RE_Static_Interrupt_Protection;
13293
13294 elsif Has_Interrupt_Handler (Conc_Typ)
13295 and then not Restriction_Active (No_Dynamic_Attachment)
13296 then
13297 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13298
13299 else
13300 case Corresponding_Runtime_Package (Conc_Typ) is
13301 when System_Tasking_Protected_Objects_Entries =>
13302 Prot_Typ := RE_Protection_Entries;
13303
13304 when System_Tasking_Protected_Objects_Single_Entry =>
13305 Prot_Typ := RE_Protection_Entry;
13306
13307 when System_Tasking_Protected_Objects =>
13308 Prot_Typ := RE_Protection;
13309
13310 when others =>
13311 raise Program_Error;
13312 end case;
13313 end if;
13314
13315 -- Generate:
13316 -- conc_typR : protection_typ renames _object._object;
13317
13318 Decl :=
13319 Make_Object_Renaming_Declaration (Loc,
13320 Defining_Identifier => Prot_Ent,
13321 Subtype_Mark =>
13322 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13323 Name =>
13324 Make_Selected_Component (Loc,
13325 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13326 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13327 Add (Decl);
13328 end;
13329 end if;
13330
13331 -- Step 3: Add discriminant renamings (if any)
13332
13333 if Has_Discriminants (Conc_Typ) then
13334 declare
13335 D : Entity_Id;
13336
13337 begin
13338 D := First_Discriminant (Conc_Typ);
13339 while Present (D) loop
13340
13341 -- Adjust the source location
13342
13343 Set_Sloc (Discriminal (D), Loc);
13344
13345 -- Generate:
13346 -- discr_name : discr_typ renames _object.discr_name;
13347 -- or
13348 -- discr_name : discr_typ renames _task.discr_name;
13349
13350 Decl :=
13351 Make_Object_Renaming_Declaration (Loc,
13352 Defining_Identifier => Discriminal (D),
13353 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13354 Name =>
13355 Make_Selected_Component (Loc,
13356 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13357 Selector_Name => Make_Identifier (Loc, Chars (D))));
13358 Add (Decl);
13359
13360 Next_Discriminant (D);
13361 end loop;
13362 end;
13363 end if;
13364
13365 -- Step 4: Add private component renamings (if any)
13366
13367 if Is_Protected then
13368 Def := Protected_Definition (Parent (Conc_Typ));
13369
13370 if Present (Private_Declarations (Def)) then
13371 declare
13372 Comp : Node_Id;
13373 Comp_Id : Entity_Id;
13374 Decl_Id : Entity_Id;
13375
13376 begin
13377 Comp := First (Private_Declarations (Def));
13378 while Present (Comp) loop
13379 if Nkind (Comp) = N_Component_Declaration then
13380 Comp_Id := Defining_Identifier (Comp);
13381 Decl_Id :=
13382 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13383
13384 -- Minimal decoration
13385
13386 if Ekind (Spec_Id) = E_Function then
13387 Set_Ekind (Decl_Id, E_Constant);
13388 else
13389 Set_Ekind (Decl_Id, E_Variable);
13390 end if;
13391
13392 Set_Prival (Comp_Id, Decl_Id);
13393 Set_Prival_Link (Decl_Id, Comp_Id);
13394 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13395
13396 -- Generate:
13397 -- comp_name : comp_typ renames _object.comp_name;
13398
13399 Decl :=
13400 Make_Object_Renaming_Declaration (Loc,
13401 Defining_Identifier => Decl_Id,
13402 Subtype_Mark =>
13403 New_Occurrence_Of (Etype (Comp_Id), Loc),
13404 Name =>
13405 Make_Selected_Component (Loc,
13406 Prefix =>
13407 New_Occurrence_Of (Obj_Ent, Loc),
13408 Selector_Name =>
13409 Make_Identifier (Loc, Chars (Comp_Id))));
13410 Add (Decl);
13411 end if;
13412
13413 Next (Comp);
13414 end loop;
13415 end;
13416 end if;
13417 end if;
13418
13419 -- Step 5: Add the declaration of the entry index and the associated
13420 -- type for barrier functions and entry families.
13421
13422 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13423 declare
13424 E : constant Entity_Id := Index_Object (Spec_Id);
13425 Index : constant Entity_Id :=
13426 Defining_Identifier
13427 (Entry_Index_Specification
13428 (Entry_Body_Formal_Part (Body_Nod)));
13429 Index_Con : constant Entity_Id :=
13430 Make_Defining_Identifier (Loc, Chars (Index));
13431 High : Node_Id;
13432 Index_Typ : Entity_Id;
13433 Low : Node_Id;
13434
13435 begin
13436 -- Minimal decoration
13437
13438 Set_Ekind (Index_Con, E_Constant);
13439 Set_Entry_Index_Constant (Index, Index_Con);
13440 Set_Discriminal_Link (Index_Con, Index);
13441
13442 -- Retrieve the bounds of the entry family
13443
13444 High := Type_High_Bound (Etype (Index));
13445 Low := Type_Low_Bound (Etype (Index));
13446
13447 -- In the simple case the entry family is given by a subtype mark
13448 -- and the index constant has the same type.
13449
13450 if Is_Entity_Name (Original_Node (
13451 Discrete_Subtype_Definition (Parent (Index))))
13452 then
13453 Index_Typ := Etype (Index);
13454
13455 -- Otherwise a new subtype declaration is required
13456
13457 else
13458 High := Replace_Bound (High);
13459 Low := Replace_Bound (Low);
13460
13461 Index_Typ := Make_Temporary (Loc, 'J');
13462
13463 -- Generate:
13464 -- subtype Jnn is <Etype of Index> range Low .. High;
13465
13466 Decl :=
13467 Make_Subtype_Declaration (Loc,
13468 Defining_Identifier => Index_Typ,
13469 Subtype_Indication =>
13470 Make_Subtype_Indication (Loc,
13471 Subtype_Mark =>
13472 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13473 Constraint =>
13474 Make_Range_Constraint (Loc,
13475 Range_Expression =>
13476 Make_Range (Loc, Low, High))));
13477 Add (Decl);
13478 end if;
13479
13480 Set_Etype (Index_Con, Index_Typ);
13481
13482 -- Create the object which designates the index:
13483 -- J : constant Jnn :=
13484 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13485 --
13486 -- where Jnn is the subtype created above or the original type of
13487 -- the index, _E is a formal of the protected body subprogram and
13488 -- <index expr> is the index of the first family member.
13489
13490 Decl :=
13491 Make_Object_Declaration (Loc,
13492 Defining_Identifier => Index_Con,
13493 Constant_Present => True,
13494 Object_Definition =>
13495 New_Occurrence_Of (Index_Typ, Loc),
13496
13497 Expression =>
13498 Make_Attribute_Reference (Loc,
13499 Prefix =>
13500 New_Occurrence_Of (Index_Typ, Loc),
13501 Attribute_Name => Name_Val,
13502
13503 Expressions => New_List (
13504
13505 Make_Op_Add (Loc,
13506 Left_Opnd =>
13507 Make_Op_Subtract (Loc,
13508 Left_Opnd => New_Occurrence_Of (E, Loc),
13509 Right_Opnd =>
13510 Entry_Index_Expression (Loc,
13511 Defining_Identifier (Body_Nod),
13512 Empty, Conc_Typ)),
13513
13514 Right_Opnd =>
13515 Make_Attribute_Reference (Loc,
13516 Prefix =>
13517 New_Occurrence_Of (Index_Typ, Loc),
13518 Attribute_Name => Name_Pos,
13519 Expressions => New_List (
13520 Make_Attribute_Reference (Loc,
13521 Prefix =>
13522 New_Occurrence_Of (Index_Typ, Loc),
13523 Attribute_Name => Name_First)))))));
13524 Add (Decl);
13525 end;
13526 end if;
13527 end Install_Private_Data_Declarations;
13528
13529 -----------------------
13530 -- Is_Exception_Safe --
13531 -----------------------
13532
13533 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13534
13535 function Has_Side_Effect (N : Node_Id) return Boolean;
13536 -- Return True whenever encountering a subprogram call or raise
13537 -- statement of any kind in the sequence of statements
13538
13539 ---------------------
13540 -- Has_Side_Effect --
13541 ---------------------
13542
13543 -- What is this doing buried two levels down in exp_ch9. It seems like a
13544 -- generally useful function, and indeed there may be code duplication
13545 -- going on here ???
13546
13547 function Has_Side_Effect (N : Node_Id) return Boolean is
13548 Stmt : Node_Id;
13549 Expr : Node_Id;
13550
13551 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13552 -- Indicate whether N is a subprogram call or a raise statement
13553
13554 ----------------------
13555 -- Is_Call_Or_Raise --
13556 ----------------------
13557
13558 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13559 begin
13560 return Nkind_In (N, N_Procedure_Call_Statement,
13561 N_Function_Call,
13562 N_Raise_Statement,
13563 N_Raise_Constraint_Error,
13564 N_Raise_Program_Error,
13565 N_Raise_Storage_Error);
13566 end Is_Call_Or_Raise;
13567
13568 -- Start of processing for Has_Side_Effect
13569
13570 begin
13571 Stmt := N;
13572 while Present (Stmt) loop
13573 if Is_Call_Or_Raise (Stmt) then
13574 return True;
13575 end if;
13576
13577 -- An object declaration can also contain a function call or a
13578 -- raise statement.
13579
13580 if Nkind (Stmt) = N_Object_Declaration then
13581 Expr := Expression (Stmt);
13582
13583 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13584 return True;
13585 end if;
13586 end if;
13587
13588 Next (Stmt);
13589 end loop;
13590
13591 return False;
13592 end Has_Side_Effect;
13593
13594 -- Start of processing for Is_Exception_Safe
13595
13596 begin
13597 -- When exceptions can't be propagated, the subprogram returns normally
13598
13599 if No_Exception_Handlers_Set then
13600 return True;
13601 end if;
13602
13603 -- If the checks handled by the back end are not disabled, we cannot
13604 -- ensure that no exception will be raised.
13605
13606 if not Access_Checks_Suppressed (Empty)
13607 or else not Discriminant_Checks_Suppressed (Empty)
13608 or else not Range_Checks_Suppressed (Empty)
13609 or else not Index_Checks_Suppressed (Empty)
13610 or else Opt.Stack_Checking_Enabled
13611 then
13612 return False;
13613 end if;
13614
13615 if Has_Side_Effect (First (Declarations (Subprogram)))
13616 or else
13617 Has_Side_Effect
13618 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13619 then
13620 return False;
13621 else
13622 return True;
13623 end if;
13624 end Is_Exception_Safe;
13625
13626 ---------------------------------
13627 -- Is_Potentially_Large_Family --
13628 ---------------------------------
13629
13630 function Is_Potentially_Large_Family
13631 (Base_Index : Entity_Id;
13632 Conctyp : Entity_Id;
13633 Lo : Node_Id;
13634 Hi : Node_Id) return Boolean
13635 is
13636 begin
13637 return Scope (Base_Index) = Standard_Standard
13638 and then Base_Index = Base_Type (Standard_Integer)
13639 and then Has_Discriminants (Conctyp)
13640 and then
13641 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13642 and then
13643 (Denotes_Discriminant (Lo, True)
13644 or else
13645 Denotes_Discriminant (Hi, True));
13646 end Is_Potentially_Large_Family;
13647
13648 -------------------------------------
13649 -- Is_Private_Primitive_Subprogram --
13650 -------------------------------------
13651
13652 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13653 begin
13654 return
13655 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13656 and then Is_Private_Primitive (Id);
13657 end Is_Private_Primitive_Subprogram;
13658
13659 ------------------
13660 -- Index_Object --
13661 ------------------
13662
13663 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13664 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13665 Formal : Entity_Id;
13666
13667 begin
13668 Formal := First_Formal (Bod_Subp);
13669 while Present (Formal) loop
13670
13671 -- Look for formal parameter _E
13672
13673 if Chars (Formal) = Name_uE then
13674 return Formal;
13675 end if;
13676
13677 Next_Formal (Formal);
13678 end loop;
13679
13680 -- A protected body subprogram should always have the parameter in
13681 -- question.
13682
13683 raise Program_Error;
13684 end Index_Object;
13685
13686 --------------------------------
13687 -- Make_Initialize_Protection --
13688 --------------------------------
13689
13690 function Make_Initialize_Protection
13691 (Protect_Rec : Entity_Id) return List_Id
13692 is
13693 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13694 P_Arr : Entity_Id;
13695 Pdec : Node_Id;
13696 Ptyp : constant Node_Id :=
13697 Corresponding_Concurrent_Type (Protect_Rec);
13698 Args : List_Id;
13699 L : constant List_Id := New_List;
13700 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13701 Prio_Type : Entity_Id;
13702 Prio_Var : Entity_Id := Empty;
13703 Restricted : constant Boolean := Restricted_Profile;
13704
13705 begin
13706 -- We may need two calls to properly initialize the object, one to
13707 -- Initialize_Protection, and possibly one to Install_Handlers if we
13708 -- have a pragma Attach_Handler.
13709
13710 -- Get protected declaration. In the case of a task type declaration,
13711 -- this is simply the parent of the protected type entity. In the single
13712 -- protected object declaration, this parent will be the implicit type,
13713 -- and we can find the corresponding single protected object declaration
13714 -- by searching forward in the declaration list in the tree.
13715
13716 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13717 -- of this type should have been removed during semantic analysis.
13718
13719 Pdec := Parent (Ptyp);
13720 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13721 N_Single_Protected_Declaration)
13722 loop
13723 Next (Pdec);
13724 end loop;
13725
13726 -- Build the parameter list for the call. Note that _Init is the name
13727 -- of the formal for the object to be initialized, which is the task
13728 -- value record itself.
13729
13730 Args := New_List;
13731
13732 -- For lock-free implementation, skip initializations of the Protection
13733 -- object.
13734
13735 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13736
13737 -- Object parameter. This is a pointer to the object of type
13738 -- Protection used by the GNARL to control the protected object.
13739
13740 Append_To (Args,
13741 Make_Attribute_Reference (Loc,
13742 Prefix =>
13743 Make_Selected_Component (Loc,
13744 Prefix => Make_Identifier (Loc, Name_uInit),
13745 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13746 Attribute_Name => Name_Unchecked_Access));
13747
13748 -- Priority parameter. Set to Unspecified_Priority unless there is a
13749 -- Priority rep item, in which case we take the value from the pragma
13750 -- or attribute definition clause, or there is an Interrupt_Priority
13751 -- rep item and no Priority rep item, and we set the ceiling to
13752 -- Interrupt_Priority'Last, an implementation-defined value, see
13753 -- (RM D.3(10)).
13754
13755 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13756 declare
13757 Prio_Clause : constant Node_Id :=
13758 Get_Rep_Item
13759 (Ptyp, Name_Priority, Check_Parents => False);
13760
13761 Prio : Node_Id;
13762
13763 begin
13764 -- Pragma Priority
13765
13766 if Nkind (Prio_Clause) = N_Pragma then
13767 Prio :=
13768 Expression
13769 (First (Pragma_Argument_Associations (Prio_Clause)));
13770
13771 -- Get_Rep_Item returns either priority pragma
13772
13773 if Pragma_Name (Prio_Clause) = Name_Priority then
13774 Prio_Type := RTE (RE_Any_Priority);
13775 else
13776 Prio_Type := RTE (RE_Interrupt_Priority);
13777 end if;
13778
13779 -- Attribute definition clause Priority
13780
13781 else
13782 if Chars (Prio_Clause) = Name_Priority then
13783 Prio_Type := RTE (RE_Any_Priority);
13784 else
13785 Prio_Type := RTE (RE_Interrupt_Priority);
13786 end if;
13787
13788 Prio := Expression (Prio_Clause);
13789 end if;
13790
13791 -- Always create a locale variable to capture the priority.
13792 -- The priority is also passed to Install_Restriced_Handlers.
13793 -- Note that it is really necessary to create this variable
13794 -- explicitly. It might be thought that removing side effects
13795 -- would the appropriate approach, but that could generate
13796 -- declarations improperly placed in the enclosing scope.
13797
13798 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13799 Append_To (L,
13800 Make_Object_Declaration (Loc,
13801 Defining_Identifier => Prio_Var,
13802 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13803 Expression => Relocate_Node (Prio)));
13804
13805 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13806 end;
13807
13808 -- When no priority is specified but an xx_Handler pragma is, we
13809 -- default to System.Interrupts.Default_Interrupt_Priority, see
13810 -- D.3(10).
13811
13812 elsif Has_Attach_Handler (Ptyp)
13813 or else Has_Interrupt_Handler (Ptyp)
13814 then
13815 Append_To (Args,
13816 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13817
13818 -- Normal case, no priority or xx_Handler specified, default priority
13819
13820 else
13821 Append_To (Args,
13822 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13823 end if;
13824
13825 -- Test for Compiler_Info parameter. This parameter allows entry body
13826 -- procedures and barrier functions to be called from the runtime. It
13827 -- is a pointer to the record generated by the compiler to represent
13828 -- the protected object.
13829
13830 -- A protected type without entries that covers an interface and
13831 -- overrides the abstract routines with protected procedures is
13832 -- considered equivalent to a protected type with entries in the
13833 -- context of dispatching select statements.
13834
13835 -- Protected types with interrupt handlers (when not using a
13836 -- restricted profile) are also considered equivalent to protected
13837 -- types with entries.
13838
13839 -- The types which are used (Static_Interrupt_Protection and
13840 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13841
13842 declare
13843 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13844
13845 Called_Subp : RE_Id;
13846
13847 begin
13848 case Pkg_Id is
13849 when System_Tasking_Protected_Objects_Entries =>
13850 Called_Subp := RE_Initialize_Protection_Entries;
13851
13852 -- Argument Compiler_Info
13853
13854 Append_To (Args,
13855 Make_Attribute_Reference (Loc,
13856 Prefix => Make_Identifier (Loc, Name_uInit),
13857 Attribute_Name => Name_Address));
13858
13859 when System_Tasking_Protected_Objects_Single_Entry =>
13860 Called_Subp := RE_Initialize_Protection_Entry;
13861
13862 -- Argument Compiler_Info
13863
13864 Append_To (Args,
13865 Make_Attribute_Reference (Loc,
13866 Prefix => Make_Identifier (Loc, Name_uInit),
13867 Attribute_Name => Name_Address));
13868
13869 when System_Tasking_Protected_Objects =>
13870 Called_Subp := RE_Initialize_Protection;
13871
13872 when others =>
13873 raise Program_Error;
13874 end case;
13875
13876 -- Entry_Queue_Maxes parameter. This is an access to an array of
13877 -- naturals representing the entry queue maximums for each entry
13878 -- in the protected type. Zero represents no max. The access is
13879 -- null if there is no limit for all entries (usual case).
13880
13881 if Has_Entry
13882 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
13883 then
13884 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
13885 Append_To (Args,
13886 Make_Attribute_Reference (Loc,
13887 Prefix =>
13888 New_Occurrence_Of
13889 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
13890 Attribute_Name => Name_Unrestricted_Access));
13891 else
13892 Append_To (Args, Make_Null (Loc));
13893 end if;
13894
13895 -- Edge cases exist where entry initialization functions are
13896 -- called, but no entries exist, so null is appended.
13897
13898 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13899 Append_To (Args, Make_Null (Loc));
13900 end if;
13901
13902 -- Entry_Bodies parameter. This is a pointer to an array of
13903 -- pointers to the entry body procedures and barrier functions of
13904 -- the object. If the protected type has no entries this object
13905 -- will not exist, in this case, pass a null (it can happen when
13906 -- there are protected interrupt handlers or interfaces).
13907
13908 if Has_Entry then
13909 P_Arr := Entry_Bodies_Array (Ptyp);
13910
13911 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13912 -- multiple entries).
13913
13914 Append_To (Args,
13915 Make_Attribute_Reference (Loc,
13916 Prefix => New_Occurrence_Of (P_Arr, Loc),
13917 Attribute_Name => Name_Unrestricted_Access));
13918
13919 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13920
13921 -- Find index mapping function (clumsy but ok for now)
13922
13923 while Ekind (P_Arr) /= E_Function loop
13924 Next_Entity (P_Arr);
13925 end loop;
13926
13927 Append_To (Args,
13928 Make_Attribute_Reference (Loc,
13929 Prefix => New_Occurrence_Of (P_Arr, Loc),
13930 Attribute_Name => Name_Unrestricted_Access));
13931 end if;
13932
13933 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13934
13935 -- This is the case where we have a protected object with
13936 -- interfaces and no entries, and the single entry restriction
13937 -- is in effect. We pass a null pointer for the entry
13938 -- parameter because there is no actual entry.
13939
13940 Append_To (Args, Make_Null (Loc));
13941
13942 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13943
13944 -- This is the case where we have a protected object with no
13945 -- entries and:
13946 -- - either interrupt handlers with non restricted profile,
13947 -- - or interfaces
13948 -- Note that the types which are used for interrupt handlers
13949 -- (Static/Dynamic_Interrupt_Protection) are derived from
13950 -- Protection_Entries. We pass two null pointers because there
13951 -- is no actual entry, and the initialization procedure needs
13952 -- both Entry_Bodies and Find_Body_Index.
13953
13954 Append_To (Args, Make_Null (Loc));
13955 Append_To (Args, Make_Null (Loc));
13956 end if;
13957
13958 Append_To (L,
13959 Make_Procedure_Call_Statement (Loc,
13960 Name =>
13961 New_Occurrence_Of (RTE (Called_Subp), Loc),
13962 Parameter_Associations => Args));
13963 end;
13964 end if;
13965
13966 if Has_Attach_Handler (Ptyp) then
13967
13968 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13969 -- make the following call:
13970
13971 -- Install_Handlers (_object,
13972 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13973
13974 -- or, in the case of Ravenscar:
13975
13976 -- Install_Restricted_Handlers
13977 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
13978
13979 declare
13980 Args : constant List_Id := New_List;
13981 Table : constant List_Id := New_List;
13982 Ritem : Node_Id := First_Rep_Item (Ptyp);
13983
13984 begin
13985 -- Build the Priority parameter (only for ravenscar)
13986
13987 if Restricted then
13988
13989 -- Priority comes from a pragma
13990
13991 if Present (Prio_Var) then
13992 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13993
13994 -- Priority is the default one
13995
13996 else
13997 Append_To (Args,
13998 New_Occurrence_Of
13999 (RTE (RE_Default_Interrupt_Priority), Loc));
14000 end if;
14001 end if;
14002
14003 -- Build the Attach_Handler table argument
14004
14005 while Present (Ritem) loop
14006 if Nkind (Ritem) = N_Pragma
14007 and then Pragma_Name (Ritem) = Name_Attach_Handler
14008 then
14009 declare
14010 Handler : constant Node_Id :=
14011 First (Pragma_Argument_Associations (Ritem));
14012
14013 Interrupt : constant Node_Id := Next (Handler);
14014 Expr : constant Node_Id := Expression (Interrupt);
14015
14016 begin
14017 Append_To (Table,
14018 Make_Aggregate (Loc, Expressions => New_List (
14019 Unchecked_Convert_To
14020 (RTE (RE_System_Interrupt_Id), Expr),
14021 Make_Attribute_Reference (Loc,
14022 Prefix =>
14023 Make_Selected_Component (Loc,
14024 Prefix =>
14025 Make_Identifier (Loc, Name_uInit),
14026 Selector_Name =>
14027 Duplicate_Subexpr_No_Checks
14028 (Expression (Handler))),
14029 Attribute_Name => Name_Access))));
14030 end;
14031 end if;
14032
14033 Next_Rep_Item (Ritem);
14034 end loop;
14035
14036 -- Append the table argument we just built
14037
14038 Append_To (Args, Make_Aggregate (Loc, Table));
14039
14040 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14041 -- call to the statements.
14042
14043 if Restricted then
14044 -- Call a simplified version of Install_Handlers to be used
14045 -- when the Ravenscar restrictions are in effect
14046 -- (Install_Restricted_Handlers).
14047
14048 Append_To (L,
14049 Make_Procedure_Call_Statement (Loc,
14050 Name =>
14051 New_Occurrence_Of
14052 (RTE (RE_Install_Restricted_Handlers), Loc),
14053 Parameter_Associations => Args));
14054
14055 else
14056 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14057
14058 -- First, prepends the _object argument
14059
14060 Prepend_To (Args,
14061 Make_Attribute_Reference (Loc,
14062 Prefix =>
14063 Make_Selected_Component (Loc,
14064 Prefix => Make_Identifier (Loc, Name_uInit),
14065 Selector_Name =>
14066 Make_Identifier (Loc, Name_uObject)),
14067 Attribute_Name => Name_Unchecked_Access));
14068 end if;
14069
14070 -- Then, insert call to Install_Handlers
14071
14072 Append_To (L,
14073 Make_Procedure_Call_Statement (Loc,
14074 Name =>
14075 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14076 Parameter_Associations => Args));
14077 end if;
14078 end;
14079 end if;
14080
14081 return L;
14082 end Make_Initialize_Protection;
14083
14084 ---------------------------
14085 -- Make_Task_Create_Call --
14086 ---------------------------
14087
14088 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14089 Loc : constant Source_Ptr := Sloc (Task_Rec);
14090 Args : List_Id;
14091 Ecount : Node_Id;
14092 Name : Node_Id;
14093 Tdec : Node_Id;
14094 Tdef : Node_Id;
14095 Tnam : Name_Id;
14096 Ttyp : Node_Id;
14097
14098 begin
14099 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14100 Tnam := Chars (Ttyp);
14101
14102 -- Get task declaration. In the case of a task type declaration, this is
14103 -- simply the parent of the task type entity. In the single task
14104 -- declaration, this parent will be the implicit type, and we can find
14105 -- the corresponding single task declaration by searching forward in the
14106 -- declaration list in the tree.
14107
14108 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14109 -- this type should have been removed during semantic analysis.
14110
14111 Tdec := Parent (Ttyp);
14112 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14113 N_Single_Task_Declaration)
14114 loop
14115 Next (Tdec);
14116 end loop;
14117
14118 -- Now we can find the task definition from this declaration
14119
14120 Tdef := Task_Definition (Tdec);
14121
14122 -- Build the parameter list for the call. Note that _Init is the name
14123 -- of the formal for the object to be initialized, which is the task
14124 -- value record itself.
14125
14126 Args := New_List;
14127
14128 -- Priority parameter. Set to Unspecified_Priority unless there is a
14129 -- Priority rep item, in which case we take the value from the rep item.
14130
14131 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14132 Append_To (Args,
14133 Make_Selected_Component (Loc,
14134 Prefix => Make_Identifier (Loc, Name_uInit),
14135 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14136 else
14137 Append_To (Args,
14138 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14139 end if;
14140
14141 -- Optional Stack parameter
14142
14143 if Restricted_Profile then
14144
14145 -- If the stack has been preallocated by the expander then
14146 -- pass its address. Otherwise, pass a null address.
14147
14148 if Preallocated_Stacks_On_Target then
14149 Append_To (Args,
14150 Make_Attribute_Reference (Loc,
14151 Prefix =>
14152 Make_Selected_Component (Loc,
14153 Prefix => Make_Identifier (Loc, Name_uInit),
14154 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14155 Attribute_Name => Name_Address));
14156
14157 else
14158 Append_To (Args,
14159 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14160 end if;
14161 end if;
14162
14163 -- Size parameter. If no Storage_Size pragma is present, then
14164 -- the size is taken from the taskZ variable for the type, which
14165 -- is either Unspecified_Size, or has been reset by the use of
14166 -- a Storage_Size attribute definition clause. If a pragma is
14167 -- present, then the size is taken from the _Size field of the
14168 -- task value record, which was set from the pragma value.
14169
14170 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14171 Append_To (Args,
14172 Make_Selected_Component (Loc,
14173 Prefix => Make_Identifier (Loc, Name_uInit),
14174 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14175
14176 else
14177 Append_To (Args,
14178 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14179 end if;
14180
14181 -- Secondary_Stack_Size parameter. Set Default_Secondary_Stack_Size
14182 -- unless there is a Secondary_Stack_Size rep item, in which case we
14183 -- take the value from the rep item. If the restriction
14184 -- No_Secondary_Stack is active then a size of 0 is passed regardless
14185 -- to prevent the allocation of the unused stack.
14186
14187 if Restriction_Active (No_Secondary_Stack) then
14188 Append_To (Args, Make_Integer_Literal (Loc, 0));
14189
14190 elsif Has_Rep_Item
14191 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14192 then
14193 Append_To (Args,
14194 Make_Selected_Component (Loc,
14195 Prefix => Make_Identifier (Loc, Name_uInit),
14196 Selector_Name =>
14197 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14198
14199 else
14200 Append_To (Args,
14201 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14202 end if;
14203
14204 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14205 -- Task_Info pragma, in which case we take the value from the pragma.
14206
14207 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14208 Append_To (Args,
14209 Make_Selected_Component (Loc,
14210 Prefix => Make_Identifier (Loc, Name_uInit),
14211 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14212
14213 else
14214 Append_To (Args,
14215 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14216 end if;
14217
14218 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14219 -- in which case we take the value from the rep item. The parameter is
14220 -- passed as an Integer because in the case of unspecified CPU the
14221 -- value is not in the range of CPU_Range.
14222
14223 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14224 Append_To (Args,
14225 Convert_To (Standard_Integer,
14226 Make_Selected_Component (Loc,
14227 Prefix => Make_Identifier (Loc, Name_uInit),
14228 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14229 else
14230 Append_To (Args,
14231 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14232 end if;
14233
14234 if not Restricted_Profile then
14235
14236 -- Deadline parameter. If no Relative_Deadline pragma is present,
14237 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14238 -- the deadline is taken from the _Relative_Deadline field of the
14239 -- task value record, which was set from the pragma value. Note that
14240 -- this parameter must not be generated for the restricted profiles
14241 -- since Ravenscar does not allow deadlines.
14242
14243 -- Case where pragma Relative_Deadline applies: use given value
14244
14245 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14246 Append_To (Args,
14247 Make_Selected_Component (Loc,
14248 Prefix => Make_Identifier (Loc, Name_uInit),
14249 Selector_Name =>
14250 Make_Identifier (Loc, Name_uRelative_Deadline)));
14251
14252 -- No pragma Relative_Deadline apply to the task
14253
14254 else
14255 Append_To (Args,
14256 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14257 end if;
14258
14259 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14260 -- present, then the dispatching domain is null. If a rep item is
14261 -- present, then the dispatching domain is taken from the
14262 -- _Dispatching_Domain field of the task value record, which was set
14263 -- from the rep item value.
14264
14265 -- Case where Dispatching_Domain rep item applies: use given value
14266
14267 if Has_Rep_Item
14268 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14269 then
14270 Append_To (Args,
14271 Make_Selected_Component (Loc,
14272 Prefix =>
14273 Make_Identifier (Loc, Name_uInit),
14274 Selector_Name =>
14275 Make_Identifier (Loc, Name_uDispatching_Domain)));
14276
14277 -- No pragma or aspect Dispatching_Domain applies to the task
14278
14279 else
14280 Append_To (Args, Make_Null (Loc));
14281 end if;
14282
14283 -- Number of entries. This is an expression of the form:
14284
14285 -- n + _Init.a'Length + _Init.a'B'Length + ...
14286
14287 -- where a,b... are the entry family names for the task definition
14288
14289 Ecount :=
14290 Build_Entry_Count_Expression
14291 (Ttyp,
14292 Component_Items
14293 (Component_List
14294 (Type_Definition
14295 (Parent (Corresponding_Record_Type (Ttyp))))),
14296 Loc);
14297 Append_To (Args, Ecount);
14298
14299 -- Master parameter. This is a reference to the _Master parameter of
14300 -- the initialization procedure, except in the case of the pragma
14301 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14302 -- System.Tasking.Library_Task_Level.
14303
14304 if Restriction_Active (No_Task_Hierarchy) = False then
14305 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14306 else
14307 Append_To (Args,
14308 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14309 end if;
14310 end if;
14311
14312 -- State parameter. This is a pointer to the task body procedure. The
14313 -- required value is obtained by taking 'Unrestricted_Access of the task
14314 -- body procedure and converting it (with an unchecked conversion) to
14315 -- the type required by the task kernel. For further details, see the
14316 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14317 -- than 'Address in order to avoid creating trampolines.
14318
14319 declare
14320 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14321 Subp_Ptr_Typ : constant Node_Id :=
14322 Create_Itype (E_Access_Subprogram_Type, Tdec);
14323 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14324
14325 begin
14326 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14327 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14328
14329 -- Be sure to freeze a reference to the access-to-subprogram type,
14330 -- otherwise gigi will complain that it's in the wrong scope, because
14331 -- it's actually inside the init procedure for the record type that
14332 -- corresponds to the task type.
14333
14334 Set_Itype (Ref, Subp_Ptr_Typ);
14335 Append_Freeze_Action (Task_Rec, Ref);
14336
14337 Append_To (Args,
14338 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14339 Make_Qualified_Expression (Loc,
14340 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14341 Expression =>
14342 Make_Attribute_Reference (Loc,
14343 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14344 Attribute_Name => Name_Unrestricted_Access))));
14345 end;
14346
14347 -- Discriminants parameter. This is just the address of the task
14348 -- value record itself (which contains the discriminant values
14349
14350 Append_To (Args,
14351 Make_Attribute_Reference (Loc,
14352 Prefix => Make_Identifier (Loc, Name_uInit),
14353 Attribute_Name => Name_Address));
14354
14355 -- Elaborated parameter. This is an access to the elaboration Boolean
14356
14357 Append_To (Args,
14358 Make_Attribute_Reference (Loc,
14359 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14360 Attribute_Name => Name_Unchecked_Access));
14361
14362 -- Add Chain parameter (not done for sequential elaboration policy, see
14363 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14364
14365 if Partition_Elaboration_Policy /= 'S' then
14366 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14367 end if;
14368
14369 -- Task name parameter. Take this from the _Task_Id parameter to the
14370 -- init call unless there is a Task_Name pragma, in which case we take
14371 -- the value from the pragma.
14372
14373 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14374 -- Copy expression in full, because it may be dynamic and have
14375 -- side effects.
14376
14377 Append_To (Args,
14378 New_Copy_Tree
14379 (Expression
14380 (First
14381 (Pragma_Argument_Associations
14382 (Get_Rep_Pragma
14383 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14384
14385 else
14386 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14387 end if;
14388
14389 -- Created_Task parameter. This is the _Task_Id field of the task
14390 -- record value
14391
14392 Append_To (Args,
14393 Make_Selected_Component (Loc,
14394 Prefix => Make_Identifier (Loc, Name_uInit),
14395 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14396
14397 declare
14398 Create_RE : RE_Id;
14399
14400 begin
14401 if Restricted_Profile then
14402 if Partition_Elaboration_Policy = 'S' then
14403 Create_RE := RE_Create_Restricted_Task_Sequential;
14404 else
14405 Create_RE := RE_Create_Restricted_Task;
14406 end if;
14407 else
14408 Create_RE := RE_Create_Task;
14409 end if;
14410
14411 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14412 end;
14413
14414 return
14415 Make_Procedure_Call_Statement (Loc,
14416 Name => Name,
14417 Parameter_Associations => Args);
14418 end Make_Task_Create_Call;
14419
14420 ------------------------------
14421 -- Next_Protected_Operation --
14422 ------------------------------
14423
14424 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14425 Next_Op : Node_Id;
14426
14427 begin
14428 -- Check whether there is a subsequent body for a protected operation
14429 -- in the current protected body. In Ada2012 that includes expression
14430 -- functions that are completions.
14431
14432 Next_Op := Next (N);
14433 while Present (Next_Op)
14434 and then not Nkind_In (Next_Op,
14435 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14436 loop
14437 Next (Next_Op);
14438 end loop;
14439
14440 return Next_Op;
14441 end Next_Protected_Operation;
14442
14443 ---------------------
14444 -- Null_Statements --
14445 ---------------------
14446
14447 function Null_Statements (Stats : List_Id) return Boolean is
14448 Stmt : Node_Id;
14449
14450 begin
14451 Stmt := First (Stats);
14452 while Nkind (Stmt) /= N_Empty
14453 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14454 or else
14455 (Nkind (Stmt) = N_Pragma
14456 and then
14457 Nam_In (Pragma_Name_Unmapped (Stmt),
14458 Name_Unreferenced,
14459 Name_Unmodified,
14460 Name_Warnings)))
14461 loop
14462 Next (Stmt);
14463 end loop;
14464
14465 return Nkind (Stmt) = N_Empty;
14466 end Null_Statements;
14467
14468 --------------------------
14469 -- Parameter_Block_Pack --
14470 --------------------------
14471
14472 function Parameter_Block_Pack
14473 (Loc : Source_Ptr;
14474 Blk_Typ : Entity_Id;
14475 Actuals : List_Id;
14476 Formals : List_Id;
14477 Decls : List_Id;
14478 Stmts : List_Id) return Node_Id
14479 is
14480 Actual : Entity_Id;
14481 Expr : Node_Id := Empty;
14482 Formal : Entity_Id;
14483 Has_Param : Boolean := False;
14484 P : Entity_Id;
14485 Params : List_Id;
14486 Temp_Asn : Node_Id;
14487 Temp_Nam : Node_Id;
14488
14489 begin
14490 Actual := First (Actuals);
14491 Formal := Defining_Identifier (First (Formals));
14492 Params := New_List;
14493 while Present (Actual) loop
14494 if Is_By_Copy_Type (Etype (Actual)) then
14495 -- Generate:
14496 -- Jnn : aliased <formal-type>
14497
14498 Temp_Nam := Make_Temporary (Loc, 'J');
14499
14500 Append_To (Decls,
14501 Make_Object_Declaration (Loc,
14502 Aliased_Present => True,
14503 Defining_Identifier => Temp_Nam,
14504 Object_Definition =>
14505 New_Occurrence_Of (Etype (Formal), Loc)));
14506
14507 if Ekind (Formal) /= E_Out_Parameter then
14508
14509 -- Generate:
14510 -- Jnn := <actual>
14511
14512 Temp_Asn :=
14513 New_Occurrence_Of (Temp_Nam, Loc);
14514
14515 Set_Assignment_OK (Temp_Asn);
14516
14517 Append_To (Stmts,
14518 Make_Assignment_Statement (Loc,
14519 Name => Temp_Asn,
14520 Expression => New_Copy_Tree (Actual)));
14521 end if;
14522
14523 -- Generate:
14524 -- Jnn'unchecked_access
14525
14526 Append_To (Params,
14527 Make_Attribute_Reference (Loc,
14528 Attribute_Name => Name_Unchecked_Access,
14529 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14530
14531 Has_Param := True;
14532
14533 -- The controlling parameter is omitted
14534
14535 else
14536 if not Is_Controlling_Actual (Actual) then
14537 Append_To (Params,
14538 Make_Reference (Loc, New_Copy_Tree (Actual)));
14539
14540 Has_Param := True;
14541 end if;
14542 end if;
14543
14544 Next_Actual (Actual);
14545 Next_Formal_With_Extras (Formal);
14546 end loop;
14547
14548 if Has_Param then
14549 Expr := Make_Aggregate (Loc, Params);
14550 end if;
14551
14552 -- Generate:
14553 -- P : Ann := (
14554 -- J1'unchecked_access;
14555 -- <actual2>'reference;
14556 -- ...);
14557
14558 P := Make_Temporary (Loc, 'P');
14559
14560 Append_To (Decls,
14561 Make_Object_Declaration (Loc,
14562 Defining_Identifier => P,
14563 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14564 Expression => Expr));
14565
14566 return P;
14567 end Parameter_Block_Pack;
14568
14569 ----------------------------
14570 -- Parameter_Block_Unpack --
14571 ----------------------------
14572
14573 function Parameter_Block_Unpack
14574 (Loc : Source_Ptr;
14575 P : Entity_Id;
14576 Actuals : List_Id;
14577 Formals : List_Id) return List_Id
14578 is
14579 Actual : Entity_Id;
14580 Asnmt : Node_Id;
14581 Formal : Entity_Id;
14582 Has_Asnmt : Boolean := False;
14583 Result : constant List_Id := New_List;
14584
14585 begin
14586 Actual := First (Actuals);
14587 Formal := Defining_Identifier (First (Formals));
14588 while Present (Actual) loop
14589 if Is_By_Copy_Type (Etype (Actual))
14590 and then Ekind (Formal) /= E_In_Parameter
14591 then
14592 -- Generate:
14593 -- <actual> := P.<formal>;
14594
14595 Asnmt :=
14596 Make_Assignment_Statement (Loc,
14597 Name =>
14598 New_Copy (Actual),
14599 Expression =>
14600 Make_Explicit_Dereference (Loc,
14601 Make_Selected_Component (Loc,
14602 Prefix =>
14603 New_Occurrence_Of (P, Loc),
14604 Selector_Name =>
14605 Make_Identifier (Loc, Chars (Formal)))));
14606
14607 Set_Assignment_OK (Name (Asnmt));
14608 Append_To (Result, Asnmt);
14609
14610 Has_Asnmt := True;
14611 end if;
14612
14613 Next_Actual (Actual);
14614 Next_Formal_With_Extras (Formal);
14615 end loop;
14616
14617 if Has_Asnmt then
14618 return Result;
14619 else
14620 return New_List (Make_Null_Statement (Loc));
14621 end if;
14622 end Parameter_Block_Unpack;
14623
14624 ----------------------
14625 -- Set_Discriminals --
14626 ----------------------
14627
14628 procedure Set_Discriminals (Dec : Node_Id) is
14629 D : Entity_Id;
14630 Pdef : Entity_Id;
14631 D_Minal : Entity_Id;
14632
14633 begin
14634 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14635 Pdef := Defining_Identifier (Dec);
14636
14637 if Has_Discriminants (Pdef) then
14638 D := First_Discriminant (Pdef);
14639 while Present (D) loop
14640 D_Minal :=
14641 Make_Defining_Identifier (Sloc (D),
14642 Chars => New_External_Name (Chars (D), 'D'));
14643
14644 Set_Ekind (D_Minal, E_Constant);
14645 Set_Etype (D_Minal, Etype (D));
14646 Set_Scope (D_Minal, Pdef);
14647 Set_Discriminal (D, D_Minal);
14648 Set_Discriminal_Link (D_Minal, D);
14649
14650 Next_Discriminant (D);
14651 end loop;
14652 end if;
14653 end Set_Discriminals;
14654
14655 -----------------------
14656 -- Trivial_Accept_OK --
14657 -----------------------
14658
14659 function Trivial_Accept_OK return Boolean is
14660 begin
14661 case Opt.Task_Dispatching_Policy is
14662
14663 -- If we have the default task dispatching policy in effect, we can
14664 -- definitely do the optimization (one way of looking at this is to
14665 -- think of the formal definition of the default policy being allowed
14666 -- to run any task it likes after a rendezvous, so even if notionally
14667 -- a full rescheduling occurs, we can say that our dispatching policy
14668 -- (i.e. the default dispatching policy) reorders the queue to be the
14669 -- same as just before the call.
14670
14671 when ' ' =>
14672 return True;
14673
14674 -- FIFO_Within_Priorities certainly does not permit this
14675 -- optimization since the Rendezvous is a scheduling action that may
14676 -- require some other task to be run.
14677
14678 when 'F' =>
14679 return False;
14680
14681 -- For now, disallow the optimization for all other policies. This
14682 -- may be over-conservative, but it is certainly not incorrect.
14683
14684 when others =>
14685 return False;
14686 end case;
14687 end Trivial_Accept_OK;
14688
14689 end Exp_Ch9;