]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch9.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / exp_ch9.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch3; use Exp_Ch3;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Dbug; use Exp_Dbug;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Sel; use Exp_Sel;
37 with Exp_Smem; use Exp_Smem;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
41 with Hostparm;
42 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch9; use Sem_Ch9;
55 with Sem_Ch11; use Sem_Ch11;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
67
68 package body Exp_Ch9 is
69
70 -- The following constant establishes the upper bound for the index of
71 -- an entry family. It is used to limit the allocated size of protected
72 -- types with defaulted discriminant of an integer type, when the bound
73 -- of some entry family depends on a discriminant. The limitation to entry
74 -- families of 128K should be reasonable in all cases, and is a documented
75 -- implementation restriction.
76
77 Entry_Family_Bound : constant Pos := 2**16;
78
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
82
83 function Actual_Index_Expression
84 (Sloc : Source_Ptr;
85 Ent : Entity_Id;
86 Index : Node_Id;
87 Tsk : Entity_Id) return Node_Id;
88 -- Compute the index position for an entry call. Tsk is the target task. If
89 -- the bounds of some entry family depend on discriminants, the expression
90 -- computed by this function uses the discriminants of the target task.
91
92 procedure Add_Object_Pointer
93 (Loc : Source_Ptr;
94 Conc_Typ : Entity_Id;
95 Decls : List_Id);
96 -- Prepend an object pointer declaration to the declaration list Decls.
97 -- This object pointer is initialized to a type conversion of the System.
98 -- Address pointer passed to entry barrier functions and entry body
99 -- procedures.
100
101 procedure Add_Formal_Renamings
102 (Spec : Node_Id;
103 Decls : List_Id;
104 Ent : Entity_Id;
105 Loc : Source_Ptr);
106 -- Create renaming declarations for the formals, inside the procedure that
107 -- implements an entry body. The renamings make the original names of the
108 -- formals accessible to gdb, and serve no other purpose.
109 -- Spec is the specification of the procedure being built.
110 -- Decls is the list of declarations to be enhanced.
111 -- Ent is the entity for the original entry body.
112
113 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
114 -- Transform accept statement into a block with added exception handler.
115 -- Used both for simple accept statements and for accept alternatives in
116 -- select statements. Astat is the accept statement.
117
118 function Build_Barrier_Function
119 (N : Node_Id;
120 Ent : Entity_Id;
121 Pid : Node_Id) return Node_Id;
122 -- Build the function body returning the value of the barrier expression
123 -- for the specified entry body.
124
125 function Build_Barrier_Function_Specification
126 (Loc : Source_Ptr;
127 Def_Id : Entity_Id) return Node_Id;
128 -- Build a specification for a function implementing the protected entry
129 -- barrier of the specified entry body.
130
131 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id);
132 -- Build the body of a wrapper procedure for an entry or entry family that
133 -- has contract cases, preconditions, or postconditions. The body gathers
134 -- the executable contract items and expands them in the usual way, and
135 -- performs the entry call itself. This way preconditions are evaluated
136 -- before the call is queued. E is the entry in question, and Decl is the
137 -- enclosing synchronized type declaration at whose freeze point the
138 -- generated body is analyzed.
139
140 function Build_Corresponding_Record
141 (N : Node_Id;
142 Ctyp : Node_Id;
143 Loc : Source_Ptr) return Node_Id;
144 -- Common to tasks and protected types. Copy discriminant specifications,
145 -- build record declaration. N is the type declaration, Ctyp is the
146 -- concurrent entity (task type or protected type).
147
148 function Build_Dispatching_Tag_Check
149 (K : Entity_Id;
150 N : Node_Id) return Node_Id;
151 -- Utility to create the tree to check whether the dispatching call in
152 -- a timed entry call, a conditional entry call, or an asynchronous
153 -- transfer of control is a call to a primitive of a non-synchronized type.
154 -- K is the temporary that holds the tagged kind of the target object, and
155 -- N is the enclosing construct.
156
157 function Build_Entry_Count_Expression
158 (Concurrent_Type : Node_Id;
159 Component_List : List_Id;
160 Loc : Source_Ptr) return Node_Id;
161 -- Compute number of entries for concurrent object. This is a count of
162 -- simple entries, followed by an expression that computes the length
163 -- of the range of each entry family. A single array with that size is
164 -- allocated for each concurrent object of the type.
165
166 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
167 -- Build the function that translates the entry index in the call
168 -- (which depends on the size of entry families) into an index into the
169 -- Entry_Bodies_Array, to determine the body and barrier function used
170 -- in a protected entry call. A pointer to this function appears in every
171 -- protected object.
172
173 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
174 -- Build subprogram declaration for previous one
175
176 function Build_Lock_Free_Protected_Subprogram_Body
177 (N : Node_Id;
178 Prot_Typ : Node_Id;
179 Unprot_Spec : Node_Id) return Node_Id;
180 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
181 -- the subprogram specification of the unprotected version of N. Transform
182 -- N such that it invokes the unprotected version of the body.
183
184 function Build_Lock_Free_Unprotected_Subprogram_Body
185 (N : Node_Id;
186 Prot_Typ : Node_Id) return Node_Id;
187 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
188 -- of N where the original statements of N are synchronized through atomic
189 -- actions such as compare and exchange. Prior to invoking this routine, it
190 -- has been established that N can be implemented in a lock-free fashion.
191
192 function Build_Parameter_Block
193 (Loc : Source_Ptr;
194 Actuals : List_Id;
195 Formals : List_Id;
196 Decls : List_Id) return Entity_Id;
197 -- Generate an access type for each actual parameter in the list Actuals.
198 -- Create an encapsulating record that contains all the actuals and return
199 -- its type. Generate:
200 -- type Ann1 is access all <actual1-type>
201 -- ...
202 -- type AnnN is access all <actualN-type>
203 -- type Pnn is record
204 -- <formal1> : Ann1;
205 -- ...
206 -- <formalN> : AnnN;
207 -- end record;
208
209 function Build_Protected_Entry
210 (N : Node_Id;
211 Ent : Entity_Id;
212 Pid : Node_Id) return Node_Id;
213 -- Build the procedure implementing the statement sequence of the specified
214 -- entry body.
215
216 function Build_Protected_Entry_Specification
217 (Loc : Source_Ptr;
218 Def_Id : Entity_Id;
219 Ent_Id : Entity_Id) return Node_Id;
220 -- Build a specification for the procedure implementing the statements of
221 -- the specified entry body. Add attributes associating it with the entry
222 -- defining identifier Ent_Id.
223
224 function Build_Protected_Spec
225 (N : Node_Id;
226 Obj_Type : Entity_Id;
227 Ident : Entity_Id;
228 Unprotected : Boolean := False) return List_Id;
229 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
230 -- Subprogram_Type. Builds signature of protected subprogram, adding the
231 -- formal that corresponds to the object itself. For an access to protected
232 -- subprogram, there is no object type to specify, so the parameter has
233 -- type Address and mode In. An indirect call through such a pointer will
234 -- convert the address to a reference to the actual object. The object is
235 -- a limited record and therefore a by_reference type.
236
237 function Build_Protected_Subprogram_Body
238 (N : Node_Id;
239 Pid : Node_Id;
240 N_Op_Spec : Node_Id) return Node_Id;
241 -- This function is used to construct the protected version of a protected
242 -- subprogram. Its statement sequence first defers abort, then locks the
243 -- associated protected object, and then enters a block that contains a
244 -- call to the unprotected version of the subprogram (for details, see
245 -- Build_Unprotected_Subprogram_Body). This block statement requires a
246 -- cleanup handler that unlocks the object in all cases. For details,
247 -- see Exp_Ch7.Expand_Cleanup_Actions.
248
249 function Build_Renamed_Formal_Declaration
250 (New_F : Entity_Id;
251 Formal : Entity_Id;
252 Comp : Entity_Id;
253 Renamed_Formal : Node_Id) return Node_Id;
254 -- Create a renaming declaration for a formal, within a protected entry
255 -- body or an accept body. The renamed object is a component of the
256 -- parameter block that is a parameter in the entry call.
257 --
258 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
259 -- does not dereference the corresponding component to prevent an illegal
260 -- use of the incomplete type (AI05-0151).
261
262 function Build_Selected_Name
263 (Prefix : Entity_Id;
264 Selector : Entity_Id;
265 Append_Char : Character := ' ') return Name_Id;
266 -- Build a name in the form of Prefix__Selector, with an optional character
267 -- appended. This is used for internal subprograms generated for operations
268 -- of protected types, including barrier functions. For the subprograms
269 -- generated for entry bodies and entry barriers, the generated name
270 -- includes a sequence number that makes names unique in the presence of
271 -- entry overloading. This is necessary because entry body procedures and
272 -- barrier functions all have the same signature.
273
274 procedure Build_Simple_Entry_Call
275 (N : Node_Id;
276 Concval : Node_Id;
277 Ename : Node_Id;
278 Index : Node_Id);
279 -- Some comments here would be useful ???
280
281 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
282 -- This routine constructs a specification for the procedure that we will
283 -- build for the task body for task type T. The spec has the form:
284 --
285 -- procedure tnameB (_Task : access tnameV);
286 --
287 -- where name is the character name taken from the task type entity that
288 -- is passed as the argument to the procedure, and tnameV is the task
289 -- value type that is associated with the task type.
290
291 function Build_Unprotected_Subprogram_Body
292 (N : Node_Id;
293 Pid : Node_Id) return Node_Id;
294 -- This routine constructs the unprotected version of a protected
295 -- subprogram body, which is contains all of the code in the original,
296 -- unexpanded body. This is the version of the protected subprogram that is
297 -- called from all protected operations on the same object, including the
298 -- protected version of the same subprogram.
299
300 procedure Build_Wrapper_Bodies
301 (Loc : Source_Ptr;
302 Typ : Entity_Id;
303 N : Node_Id);
304 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
305 -- record of a concurrent type. N is the insertion node where all bodies
306 -- will be placed. This routine builds the bodies of the subprograms which
307 -- serve as an indirection mechanism to overriding primitives of concurrent
308 -- types, entries and protected procedures. Any new body is analyzed.
309
310 procedure Build_Wrapper_Specs
311 (Loc : Source_Ptr;
312 Typ : Entity_Id;
313 N : in out Node_Id);
314 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
315 -- record of a concurrent type. N is the insertion node where all specs
316 -- will be placed. This routine builds the specs of the subprograms which
317 -- serve as an indirection mechanism to overriding primitives of concurrent
318 -- types, entries and protected procedures. Any new spec is analyzed.
319
320 procedure Collect_Entry_Families
321 (Loc : Source_Ptr;
322 Cdecls : List_Id;
323 Current_Node : in out Node_Id;
324 Conctyp : Entity_Id);
325 -- For each entry family in a concurrent type, create an anonymous array
326 -- type of the right size, and add a component to the corresponding_record.
327
328 function Concurrent_Object
329 (Spec_Id : Entity_Id;
330 Conc_Typ : Entity_Id) return Entity_Id;
331 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
332 -- the entity associated with the concurrent object in the Protected_Body_
333 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
334 -- denotes formal parameter _O, _object or _task.
335
336 function Copy_Result_Type (Res : Node_Id) return Node_Id;
337 -- Copy the result type of a function specification, when building the
338 -- internal operation corresponding to a protected function, or when
339 -- expanding an access to protected function. If the result is an anonymous
340 -- access to subprogram itself, we need to create a new signature with the
341 -- same parameter names and the same resolved types, but with new entities
342 -- for the formals.
343
344 procedure Debug_Private_Data_Declarations (Decls : List_Id);
345 -- Decls is a list which may contain the declarations created by Install_
346 -- Private_Data_Declarations. All generated entities are marked as needing
347 -- debug info and debug nodes are manually generation where necessary. This
348 -- step of the expansion must to be done after private data has been moved
349 -- to its final resting scope to ensure proper visibility of debug objects.
350
351 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
352 -- If control flow optimizations are suppressed, and Alt is an accept,
353 -- delay, or entry call alternative with no trailing statements, insert
354 -- a null trailing statement with the given Loc (which is the sloc of
355 -- the accept, delay, or entry call statement). There might not be any
356 -- generated code for the accept, delay, or entry call itself (the effect
357 -- of these statements is part of the general processsing done for the
358 -- enclosing selective accept, timed entry call, or asynchronous select),
359 -- and the null statement is there to carry the sloc of that statement to
360 -- the back-end for trace-based coverage analysis purposes.
361
362 procedure Extract_Dispatching_Call
363 (N : Node_Id;
364 Call_Ent : out Entity_Id;
365 Object : out Entity_Id;
366 Actuals : out List_Id;
367 Formals : out List_Id);
368 -- Given a dispatching call, extract the entity of the name of the call,
369 -- its actual dispatching object, its actual parameters and the formal
370 -- parameters of the overridden interface-level version. If the type of
371 -- the dispatching object is an access type then an explicit dereference
372 -- is returned in Object.
373
374 procedure Extract_Entry
375 (N : Node_Id;
376 Concval : out Node_Id;
377 Ename : out Node_Id;
378 Index : out Node_Id);
379 -- Given an entry call, returns the associated concurrent object, the entry
380 -- name, and the entry family index.
381
382 function Family_Offset
383 (Loc : Source_Ptr;
384 Hi : Node_Id;
385 Lo : Node_Id;
386 Ttyp : Entity_Id;
387 Cap : Boolean) return Node_Id;
388 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
389 -- accept statement, or the upper bound in the discrete subtype of an entry
390 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
391 -- type of the entry. If Cap is true, the result is capped according to
392 -- Entry_Family_Bound.
393
394 function Family_Size
395 (Loc : Source_Ptr;
396 Hi : Node_Id;
397 Lo : Node_Id;
398 Ttyp : Entity_Id;
399 Cap : Boolean) return Node_Id;
400 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
401 -- family, and handle properly the superflat case. This is equivalent to
402 -- the use of 'Length on the index type, but must use Family_Offset to
403 -- handle properly the case of bounds that depend on discriminants. If
404 -- Cap is true, the result is capped according to Entry_Family_Bound.
405
406 procedure Find_Enclosing_Context
407 (N : Node_Id;
408 Context : out Node_Id;
409 Context_Id : out Entity_Id;
410 Context_Decls : out List_Id);
411 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
412 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
413 -- nearest enclosing body, block, package, or return statement and return
414 -- its constituents. Context is the enclosing construct, Context_Id is
415 -- the scope of Context_Id and Context_Decls is the declarative list of
416 -- Context.
417
418 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
419 -- Given a subprogram identifier, return the entity which is associated
420 -- with the protection entry index in the Protected_Body_Subprogram or
421 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
422 -- parameter _E.
423
424 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
425 -- Tell whether a given subprogram cannot raise an exception
426
427 function Is_Potentially_Large_Family
428 (Base_Index : Entity_Id;
429 Conctyp : Entity_Id;
430 Lo : Node_Id;
431 Hi : Node_Id) return Boolean;
432
433 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
434 -- Determine whether Id is a function or a procedure and is marked as a
435 -- private primitive.
436
437 function Null_Statements (Stats : List_Id) return Boolean;
438 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
439 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
440 -- to still count as null. Returns True for a null sequence. The argument
441 -- is the list of statements from the DO-END sequence.
442
443 function Parameter_Block_Pack
444 (Loc : Source_Ptr;
445 Blk_Typ : Entity_Id;
446 Actuals : List_Id;
447 Formals : List_Id;
448 Decls : List_Id;
449 Stmts : List_Id) return Entity_Id;
450 -- Set the components of the generated parameter block with the values
451 -- of the actual parameters. Generate aliased temporaries to capture the
452 -- values for types that are passed by copy. Otherwise generate a reference
453 -- to the actual's value. Return the address of the aggregate block.
454 -- Generate:
455 -- Jnn1 : alias <formal-type1>;
456 -- Jnn1 := <actual1>;
457 -- ...
458 -- P : Blk_Typ := (
459 -- Jnn1'unchecked_access;
460 -- <actual2>'reference;
461 -- ...);
462
463 function Parameter_Block_Unpack
464 (Loc : Source_Ptr;
465 P : Entity_Id;
466 Actuals : List_Id;
467 Formals : List_Id) return List_Id;
468 -- Retrieve the values of the components from the parameter block and
469 -- assign then to the original actual parameters. Generate:
470 -- <actual1> := P.<formal1>;
471 -- ...
472 -- <actualN> := P.<formalN>;
473
474 function Trivial_Accept_OK return Boolean;
475 -- If there is no DO-END block for an accept, or if the DO-END block has
476 -- only null statements, then it is possible to do the Rendezvous with much
477 -- less overhead using the Accept_Trivial routine in the run-time library.
478 -- However, this is not always a valid optimization. Whether it is valid or
479 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
480 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
481 -- a rescheduling is required, so this optimization is not allowed. This
482 -- function returns True if the optimization is permitted.
483
484 -----------------------------
485 -- Actual_Index_Expression --
486 -----------------------------
487
488 function Actual_Index_Expression
489 (Sloc : Source_Ptr;
490 Ent : Entity_Id;
491 Index : Node_Id;
492 Tsk : Entity_Id) return Node_Id
493 is
494 Ttyp : constant Entity_Id := Etype (Tsk);
495 Expr : Node_Id;
496 Num : Node_Id;
497 Lo : Node_Id;
498 Hi : Node_Id;
499 Prev : Entity_Id;
500 S : Node_Id;
501
502 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
503 -- Compute difference between bounds of entry family
504
505 --------------------------
506 -- Actual_Family_Offset --
507 --------------------------
508
509 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
510
511 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
512 -- Replace a reference to a discriminant with a selected component
513 -- denoting the discriminant of the target task.
514
515 -----------------------------
516 -- Actual_Discriminant_Ref --
517 -----------------------------
518
519 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
520 Typ : constant Entity_Id := Etype (Bound);
521 B : Node_Id;
522
523 begin
524 if not Is_Entity_Name (Bound)
525 or else Ekind (Entity (Bound)) /= E_Discriminant
526 then
527 if Nkind (Bound) = N_Attribute_Reference then
528 return Bound;
529 else
530 B := New_Copy_Tree (Bound);
531 end if;
532
533 else
534 B :=
535 Make_Selected_Component (Sloc,
536 Prefix => New_Copy_Tree (Tsk),
537 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
538
539 Analyze_And_Resolve (B, Typ);
540 end if;
541
542 return
543 Make_Attribute_Reference (Sloc,
544 Attribute_Name => Name_Pos,
545 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
546 Expressions => New_List (B));
547 end Actual_Discriminant_Ref;
548
549 -- Start of processing for Actual_Family_Offset
550
551 begin
552 return
553 Make_Op_Subtract (Sloc,
554 Left_Opnd => Actual_Discriminant_Ref (Hi),
555 Right_Opnd => Actual_Discriminant_Ref (Lo));
556 end Actual_Family_Offset;
557
558 -- Start of processing for Actual_Index_Expression
559
560 begin
561 -- The queues of entries and entry families appear in textual order in
562 -- the associated record. The entry index is computed as the sum of the
563 -- number of queues for all entries that precede the designated one, to
564 -- which is added the index expression, if this expression denotes a
565 -- member of a family.
566
567 -- The following is a place holder for the count of simple entries
568
569 Num := Make_Integer_Literal (Sloc, 1);
570
571 -- We construct an expression which is a series of addition operations.
572 -- See comments in Entry_Index_Expression, which is identical in
573 -- structure.
574
575 if Present (Index) then
576 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
577
578 Expr :=
579 Make_Op_Add (Sloc,
580 Left_Opnd => Num,
581 Right_Opnd =>
582 Actual_Family_Offset (
583 Make_Attribute_Reference (Sloc,
584 Attribute_Name => Name_Pos,
585 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
586 Expressions => New_List (Relocate_Node (Index))),
587 Type_Low_Bound (S)));
588 else
589 Expr := Num;
590 end if;
591
592 -- Now add lengths of preceding entries and entry families
593
594 Prev := First_Entity (Ttyp);
595 while Chars (Prev) /= Chars (Ent)
596 or else (Ekind (Prev) /= Ekind (Ent))
597 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
598 loop
599 if Ekind (Prev) = E_Entry then
600 Set_Intval (Num, Intval (Num) + 1);
601
602 elsif Ekind (Prev) = E_Entry_Family then
603 S :=
604 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
605
606 -- The need for the following full view retrieval stems from this
607 -- complex case of nested generics and tasking:
608
609 -- generic
610 -- type Formal_Index is range <>;
611 -- ...
612 -- package Outer is
613 -- type Index is private;
614 -- generic
615 -- ...
616 -- package Inner is
617 -- procedure P;
618 -- end Inner;
619 -- private
620 -- type Index is new Formal_Index range 1 .. 10;
621 -- end Outer;
622
623 -- package body Outer is
624 -- task type T is
625 -- entry Fam (Index); -- (2)
626 -- entry E;
627 -- end T;
628 -- package body Inner is -- (3)
629 -- procedure P is
630 -- begin
631 -- T.E; -- (1)
632 -- end P;
633 -- end Inner;
634 -- ...
635
636 -- We are currently building the index expression for the entry
637 -- call "T.E" (1). Part of the expansion must mention the range
638 -- of the discrete type "Index" (2) of entry family "Fam".
639
640 -- However only the private view of type "Index" is available to
641 -- the inner generic (3) because there was no prior mention of
642 -- the type inside "Inner". This visibility requirement is
643 -- implicit and cannot be detected during the construction of
644 -- the generic trees and needs special handling.
645
646 if In_Instance_Body
647 and then Is_Private_Type (S)
648 and then Present (Full_View (S))
649 then
650 S := Full_View (S);
651 end if;
652
653 Lo := Type_Low_Bound (S);
654 Hi := Type_High_Bound (S);
655
656 Expr :=
657 Make_Op_Add (Sloc,
658 Left_Opnd => Expr,
659 Right_Opnd =>
660 Make_Op_Add (Sloc,
661 Left_Opnd => Actual_Family_Offset (Hi, Lo),
662 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
663
664 -- Other components are anonymous types to be ignored
665
666 else
667 null;
668 end if;
669
670 Next_Entity (Prev);
671 end loop;
672
673 return Expr;
674 end Actual_Index_Expression;
675
676 --------------------------
677 -- Add_Formal_Renamings --
678 --------------------------
679
680 procedure Add_Formal_Renamings
681 (Spec : Node_Id;
682 Decls : List_Id;
683 Ent : Entity_Id;
684 Loc : Source_Ptr)
685 is
686 Ptr : constant Entity_Id :=
687 Defining_Identifier
688 (Next (First (Parameter_Specifications (Spec))));
689 -- The name of the formal that holds the address of the parameter block
690 -- for the call.
691
692 Comp : Entity_Id;
693 Decl : Node_Id;
694 Formal : Entity_Id;
695 New_F : Entity_Id;
696 Renamed_Formal : Node_Id;
697
698 begin
699 Formal := First_Formal (Ent);
700 while Present (Formal) loop
701 Comp := Entry_Component (Formal);
702 New_F :=
703 Make_Defining_Identifier (Sloc (Formal),
704 Chars => Chars (Formal));
705 Set_Etype (New_F, Etype (Formal));
706 Set_Scope (New_F, Ent);
707
708 -- Now we set debug info needed on New_F even though it does not come
709 -- from source, so that the debugger will get the right information
710 -- for these generated names.
711
712 Set_Debug_Info_Needed (New_F);
713
714 if Ekind (Formal) = E_In_Parameter then
715 Set_Ekind (New_F, E_Constant);
716 else
717 Set_Ekind (New_F, E_Variable);
718 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
719 end if;
720
721 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
722
723 Renamed_Formal :=
724 Make_Selected_Component (Loc,
725 Prefix =>
726 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
727 Make_Identifier (Loc, Chars (Ptr))),
728 Selector_Name => New_Occurrence_Of (Comp, Loc));
729
730 Decl :=
731 Build_Renamed_Formal_Declaration
732 (New_F, Formal, Comp, Renamed_Formal);
733
734 Append (Decl, Decls);
735 Set_Renamed_Object (Formal, New_F);
736 Next_Formal (Formal);
737 end loop;
738 end Add_Formal_Renamings;
739
740 ------------------------
741 -- Add_Object_Pointer --
742 ------------------------
743
744 procedure Add_Object_Pointer
745 (Loc : Source_Ptr;
746 Conc_Typ : Entity_Id;
747 Decls : List_Id)
748 is
749 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
750 Decl : Node_Id;
751 Obj_Ptr : Node_Id;
752
753 begin
754 -- Create the renaming declaration for the Protection object of a
755 -- protected type. _Object is used by Complete_Entry_Body.
756 -- ??? An attempt to make this a renaming was unsuccessful.
757
758 -- Build the entity for the access type
759
760 Obj_Ptr :=
761 Make_Defining_Identifier (Loc,
762 New_External_Name (Chars (Rec_Typ), 'P'));
763
764 -- Generate:
765 -- _object : poVP := poVP!O;
766
767 Decl :=
768 Make_Object_Declaration (Loc,
769 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
770 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
771 Expression =>
772 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
773 Set_Debug_Info_Needed (Defining_Identifier (Decl));
774 Prepend_To (Decls, Decl);
775
776 -- Generate:
777 -- type poVP is access poV;
778
779 Decl :=
780 Make_Full_Type_Declaration (Loc,
781 Defining_Identifier =>
782 Obj_Ptr,
783 Type_Definition =>
784 Make_Access_To_Object_Definition (Loc,
785 Subtype_Indication =>
786 New_Occurrence_Of (Rec_Typ, Loc)));
787 Set_Debug_Info_Needed (Defining_Identifier (Decl));
788 Prepend_To (Decls, Decl);
789 end Add_Object_Pointer;
790
791 -----------------------
792 -- Build_Accept_Body --
793 -----------------------
794
795 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
796 Loc : constant Source_Ptr := Sloc (Astat);
797 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
798 New_S : Node_Id;
799 Hand : Node_Id;
800 Call : Node_Id;
801 Ohandle : Node_Id;
802
803 begin
804 -- At the end of the statement sequence, Complete_Rendezvous is called.
805 -- A label skipping the Complete_Rendezvous, and all other accept
806 -- processing, has already been added for the expansion of requeue
807 -- statements. The Sloc is copied from the last statement since it
808 -- is really part of this last statement.
809
810 Call :=
811 Build_Runtime_Call
812 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
813 Insert_Before (Last (Statements (Stats)), Call);
814 Analyze (Call);
815
816 -- If exception handlers are present, then append Complete_Rendezvous
817 -- calls to the handlers, and construct the required outer block. As
818 -- above, the Sloc is copied from the last statement in the sequence.
819
820 if Present (Exception_Handlers (Stats)) then
821 Hand := First (Exception_Handlers (Stats));
822 while Present (Hand) loop
823 Call :=
824 Build_Runtime_Call
825 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
826 Append (Call, Statements (Hand));
827 Analyze (Call);
828 Next (Hand);
829 end loop;
830
831 New_S :=
832 Make_Handled_Sequence_Of_Statements (Loc,
833 Statements => New_List (
834 Make_Block_Statement (Loc,
835 Handled_Statement_Sequence => Stats)));
836
837 else
838 New_S := Stats;
839 end if;
840
841 -- At this stage we know that the new statement sequence does
842 -- not have an exception handler part, so we supply one to call
843 -- Exceptional_Complete_Rendezvous. This handler is
844
845 -- when all others =>
846 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
847
848 -- We handle Abort_Signal to make sure that we properly catch the abort
849 -- case and wake up the caller.
850
851 Ohandle := Make_Others_Choice (Loc);
852 Set_All_Others (Ohandle);
853
854 Set_Exception_Handlers (New_S,
855 New_List (
856 Make_Implicit_Exception_Handler (Loc,
857 Exception_Choices => New_List (Ohandle),
858
859 Statements => New_List (
860 Make_Procedure_Call_Statement (Sloc (Stats),
861 Name => New_Occurrence_Of (
862 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
863 Parameter_Associations => New_List (
864 Make_Function_Call (Sloc (Stats),
865 Name =>
866 New_Occurrence_Of
867 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
868
869 Set_Parent (New_S, Astat); -- temp parent for Analyze call
870 Analyze_Exception_Handlers (Exception_Handlers (New_S));
871 Expand_Exception_Handlers (New_S);
872
873 -- Exceptional_Complete_Rendezvous must be called with abort still
874 -- deferred, which is the case for a "when all others" handler.
875
876 return New_S;
877 end Build_Accept_Body;
878
879 -----------------------------------
880 -- Build_Activation_Chain_Entity --
881 -----------------------------------
882
883 procedure Build_Activation_Chain_Entity (N : Node_Id) is
884 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
885 -- Determine whether an extended return statement has activation chain
886
887 --------------------------
888 -- Has_Activation_Chain --
889 --------------------------
890
891 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
892 Decl : Node_Id;
893
894 begin
895 Decl := First (Return_Object_Declarations (Stmt));
896 while Present (Decl) loop
897 if Nkind (Decl) = N_Object_Declaration
898 and then Chars (Defining_Identifier (Decl)) = Name_uChain
899 then
900 return True;
901 end if;
902
903 Next (Decl);
904 end loop;
905
906 return False;
907 end Has_Activation_Chain;
908
909 -- Local variables
910
911 Context : Node_Id;
912 Context_Id : Entity_Id;
913 Decls : List_Id;
914
915 -- Start of processing for Build_Activation_Chain_Entity
916
917 begin
918 -- Activation chain is never used for sequential elaboration policy, see
919 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
920
921 if Partition_Elaboration_Policy = 'S' then
922 return;
923 end if;
924
925 Find_Enclosing_Context (N, Context, Context_Id, Decls);
926
927 -- If activation chain entity has not been declared already, create one
928
929 if Nkind (Context) = N_Extended_Return_Statement
930 or else No (Activation_Chain_Entity (Context))
931 then
932 -- Since extended return statements do not store the entity of the
933 -- chain, examine the return object declarations to avoid creating
934 -- a duplicate.
935
936 if Nkind (Context) = N_Extended_Return_Statement
937 and then Has_Activation_Chain (Context)
938 then
939 return;
940 end if;
941
942 declare
943 Loc : constant Source_Ptr := Sloc (Context);
944 Chain : Entity_Id;
945 Decl : Node_Id;
946
947 begin
948 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
949
950 -- Note: An extended return statement is not really a task
951 -- activator, but it does have an activation chain on which to
952 -- store the tasks temporarily. On successful return, the tasks
953 -- on this chain are moved to the chain passed in by the caller.
954 -- We do not build an Activation_Chain_Entity for an extended
955 -- return statement, because we do not want to build a call to
956 -- Activate_Tasks. Task activation is the responsibility of the
957 -- caller.
958
959 if Nkind (Context) /= N_Extended_Return_Statement then
960 Set_Activation_Chain_Entity (Context, Chain);
961 end if;
962
963 Decl :=
964 Make_Object_Declaration (Loc,
965 Defining_Identifier => Chain,
966 Aliased_Present => True,
967 Object_Definition =>
968 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
969
970 Prepend_To (Decls, Decl);
971
972 -- Ensure that _chain appears in the proper scope of the context
973
974 if Context_Id /= Current_Scope then
975 Push_Scope (Context_Id);
976 Analyze (Decl);
977 Pop_Scope;
978 else
979 Analyze (Decl);
980 end if;
981 end;
982 end if;
983 end Build_Activation_Chain_Entity;
984
985 ----------------------------
986 -- Build_Barrier_Function --
987 ----------------------------
988
989 function Build_Barrier_Function
990 (N : Node_Id;
991 Ent : Entity_Id;
992 Pid : Node_Id) return Node_Id
993 is
994 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
995 Cond : constant Node_Id := Condition (Ent_Formals);
996 Loc : constant Source_Ptr := Sloc (Cond);
997 Func_Id : constant Entity_Id := Barrier_Function (Ent);
998 Op_Decls : constant List_Id := New_List;
999 Stmt : Node_Id;
1000 Func_Body : Node_Id;
1001
1002 begin
1003 -- Add a declaration for the Protection object, renaming declarations
1004 -- for the discriminals and privals and finally a declaration for the
1005 -- entry family index (if applicable).
1006
1007 Install_Private_Data_Declarations (Sloc (N),
1008 Spec_Id => Func_Id,
1009 Conc_Typ => Pid,
1010 Body_Nod => N,
1011 Decls => Op_Decls,
1012 Barrier => True,
1013 Family => Ekind (Ent) = E_Entry_Family);
1014
1015 -- If compiling with -fpreserve-control-flow, make sure we insert an
1016 -- IF statement so that the back-end knows to generate a conditional
1017 -- branch instruction, even if the condition is just the name of a
1018 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1019 -- such redundant IF statements under -fpreserve-control-flow
1020 -- (whether coming from this routine, or directly from source).
1021
1022 if Opt.Suppress_Control_Flow_Optimizations then
1023 Stmt :=
1024 Make_Implicit_If_Statement (Cond,
1025 Condition => Cond,
1026 Then_Statements => New_List (
1027 Make_Simple_Return_Statement (Loc,
1028 New_Occurrence_Of (Standard_True, Loc))),
1029
1030 Else_Statements => New_List (
1031 Make_Simple_Return_Statement (Loc,
1032 New_Occurrence_Of (Standard_False, Loc))));
1033
1034 else
1035 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1036 end if;
1037
1038 -- Note: the condition in the barrier function needs to be properly
1039 -- processed for the C/Fortran boolean possibility, but this happens
1040 -- automatically since the return statement does this normalization.
1041
1042 Func_Body :=
1043 Make_Subprogram_Body (Loc,
1044 Specification =>
1045 Build_Barrier_Function_Specification (Loc,
1046 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1047 Declarations => Op_Decls,
1048 Handled_Statement_Sequence =>
1049 Make_Handled_Sequence_Of_Statements (Loc,
1050 Statements => New_List (Stmt)));
1051 Set_Is_Entry_Barrier_Function (Func_Body);
1052
1053 return Func_Body;
1054 end Build_Barrier_Function;
1055
1056 ------------------------------------------
1057 -- Build_Barrier_Function_Specification --
1058 ------------------------------------------
1059
1060 function Build_Barrier_Function_Specification
1061 (Loc : Source_Ptr;
1062 Def_Id : Entity_Id) return Node_Id
1063 is
1064 begin
1065 Set_Debug_Info_Needed (Def_Id);
1066
1067 return
1068 Make_Function_Specification (Loc,
1069 Defining_Unit_Name => Def_Id,
1070 Parameter_Specifications => New_List (
1071 Make_Parameter_Specification (Loc,
1072 Defining_Identifier =>
1073 Make_Defining_Identifier (Loc, Name_uO),
1074 Parameter_Type =>
1075 New_Occurrence_Of (RTE (RE_Address), Loc)),
1076
1077 Make_Parameter_Specification (Loc,
1078 Defining_Identifier =>
1079 Make_Defining_Identifier (Loc, Name_uE),
1080 Parameter_Type =>
1081 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1082
1083 Result_Definition =>
1084 New_Occurrence_Of (Standard_Boolean, Loc));
1085 end Build_Barrier_Function_Specification;
1086
1087 --------------------------
1088 -- Build_Call_With_Task --
1089 --------------------------
1090
1091 function Build_Call_With_Task
1092 (N : Node_Id;
1093 E : Entity_Id) return Node_Id
1094 is
1095 Loc : constant Source_Ptr := Sloc (N);
1096 begin
1097 return
1098 Make_Function_Call (Loc,
1099 Name => New_Occurrence_Of (E, Loc),
1100 Parameter_Associations => New_List (Concurrent_Ref (N)));
1101 end Build_Call_With_Task;
1102
1103 -----------------------------
1104 -- Build_Class_Wide_Master --
1105 -----------------------------
1106
1107 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1108 Loc : constant Source_Ptr := Sloc (Typ);
1109 Master_Decl : Node_Id;
1110 Master_Id : Entity_Id;
1111 Master_Scope : Entity_Id;
1112 Name_Id : Node_Id;
1113 Related_Node : Node_Id;
1114 Ren_Decl : Node_Id;
1115
1116 begin
1117 -- Nothing to do if there is no task hierarchy
1118
1119 if Restriction_Active (No_Task_Hierarchy) then
1120 return;
1121 end if;
1122
1123 -- Find the declaration that created the access type, which is either a
1124 -- type declaration, or an object declaration with an access definition,
1125 -- in which case the type is anonymous.
1126
1127 if Is_Itype (Typ) then
1128 Related_Node := Associated_Node_For_Itype (Typ);
1129 else
1130 Related_Node := Parent (Typ);
1131 end if;
1132
1133 Master_Scope := Find_Master_Scope (Typ);
1134
1135 -- Nothing to do if the master scope already contains a _master entity.
1136 -- The only exception to this is the following scenario:
1137
1138 -- Source_Scope
1139 -- Transient_Scope_1
1140 -- _master
1141
1142 -- Transient_Scope_2
1143 -- use of master
1144
1145 -- In this case the source scope is marked as having the master entity
1146 -- even though the actual declaration appears inside an inner scope. If
1147 -- the second transient scope requires a _master, it cannot use the one
1148 -- already declared because the entity is not visible.
1149
1150 Name_Id := Make_Identifier (Loc, Name_uMaster);
1151 Master_Decl := Empty;
1152
1153 if not Has_Master_Entity (Master_Scope)
1154 or else No (Current_Entity_In_Scope (Name_Id))
1155 then
1156 begin
1157 Set_Has_Master_Entity (Master_Scope);
1158
1159 -- Generate:
1160 -- _master : constant Integer := Current_Master.all;
1161
1162 Master_Decl :=
1163 Make_Object_Declaration (Loc,
1164 Defining_Identifier =>
1165 Make_Defining_Identifier (Loc, Name_uMaster),
1166 Constant_Present => True,
1167 Object_Definition =>
1168 New_Occurrence_Of (Standard_Integer, Loc),
1169 Expression =>
1170 Make_Explicit_Dereference (Loc,
1171 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1172
1173 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1174 Analyze (Master_Decl);
1175
1176 -- Mark the containing scope as a task master. Masters associated
1177 -- with return statements are already marked at this stage (see
1178 -- Analyze_Subprogram_Body).
1179
1180 if Ekind (Current_Scope) /= E_Return_Statement then
1181 declare
1182 Par : Node_Id := Related_Node;
1183
1184 begin
1185 while Nkind (Par) /= N_Compilation_Unit loop
1186 Par := Parent (Par);
1187
1188 -- If we fall off the top, we are at the outer level,
1189 -- and the environment task is our effective master,
1190 -- so nothing to mark.
1191
1192 if Nkind_In (Par, N_Block_Statement,
1193 N_Subprogram_Body,
1194 N_Task_Body)
1195 then
1196 Set_Is_Task_Master (Par);
1197 exit;
1198 end if;
1199 end loop;
1200 end;
1201 end if;
1202 end;
1203 end if;
1204
1205 Master_Id :=
1206 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1207
1208 -- Generate:
1209 -- typeMnn renames _master;
1210
1211 Ren_Decl :=
1212 Make_Object_Renaming_Declaration (Loc,
1213 Defining_Identifier => Master_Id,
1214 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1215 Name => Name_Id);
1216
1217 -- If the master is declared locally, add the renaming declaration
1218 -- immediately after it, to prevent access-before-elaboration in the
1219 -- back-end.
1220
1221 if Present (Master_Decl) then
1222 Insert_After (Master_Decl, Ren_Decl);
1223 Analyze (Ren_Decl);
1224
1225 else
1226 Insert_Action (Related_Node, Ren_Decl);
1227 end if;
1228
1229 Set_Master_Id (Typ, Master_Id);
1230 end Build_Class_Wide_Master;
1231
1232 ----------------------------
1233 -- Build_Contract_Wrapper --
1234 ----------------------------
1235
1236 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1237 Conc_Typ : constant Entity_Id := Scope (E);
1238 Loc : constant Source_Ptr := Sloc (E);
1239
1240 procedure Add_Discriminant_Renamings
1241 (Obj_Id : Entity_Id;
1242 Decls : List_Id);
1243 -- Add renaming declarations for all discriminants of concurrent type
1244 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1245 -- represents the concurrent object.
1246
1247 procedure Add_Matching_Formals
1248 (Formals : List_Id;
1249 Actuals : in out List_Id);
1250 -- Add formal parameters that match those of entry E to list Formals.
1251 -- The routine also adds matching actuals for the new formals to list
1252 -- Actuals.
1253
1254 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1255 -- Relocate pragma Prag to list To. The routine creates a new list if
1256 -- To does not exist.
1257
1258 --------------------------------
1259 -- Add_Discriminant_Renamings --
1260 --------------------------------
1261
1262 procedure Add_Discriminant_Renamings
1263 (Obj_Id : Entity_Id;
1264 Decls : List_Id)
1265 is
1266 Discr : Entity_Id;
1267
1268 begin
1269 -- Inspect the discriminants of the concurrent type and generate a
1270 -- renaming for each one.
1271
1272 if Has_Discriminants (Conc_Typ) then
1273 Discr := First_Discriminant (Conc_Typ);
1274 while Present (Discr) loop
1275 Prepend_To (Decls,
1276 Make_Object_Renaming_Declaration (Loc,
1277 Defining_Identifier =>
1278 Make_Defining_Identifier (Loc, Chars (Discr)),
1279 Subtype_Mark =>
1280 New_Occurrence_Of (Etype (Discr), Loc),
1281 Name =>
1282 Make_Selected_Component (Loc,
1283 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1284 Selector_Name =>
1285 Make_Identifier (Loc, Chars (Discr)))));
1286
1287 Next_Discriminant (Discr);
1288 end loop;
1289 end if;
1290 end Add_Discriminant_Renamings;
1291
1292 --------------------------
1293 -- Add_Matching_Formals --
1294 --------------------------
1295
1296 procedure Add_Matching_Formals
1297 (Formals : List_Id;
1298 Actuals : in out List_Id)
1299 is
1300 Formal : Entity_Id;
1301 New_Formal : Entity_Id;
1302
1303 begin
1304 -- Inspect the formal parameters of the entry and generate a new
1305 -- matching formal with the same name for the wrapper. A reference
1306 -- to the new formal becomes an actual in the entry call.
1307
1308 Formal := First_Formal (E);
1309 while Present (Formal) loop
1310 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1311 Append_To (Formals,
1312 Make_Parameter_Specification (Loc,
1313 Defining_Identifier => New_Formal,
1314 In_Present => In_Present (Parent (Formal)),
1315 Out_Present => Out_Present (Parent (Formal)),
1316 Parameter_Type =>
1317 New_Occurrence_Of (Etype (Formal), Loc)));
1318
1319 if No (Actuals) then
1320 Actuals := New_List;
1321 end if;
1322
1323 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1324 Next_Formal (Formal);
1325 end loop;
1326 end Add_Matching_Formals;
1327
1328 ---------------------
1329 -- Transfer_Pragma --
1330 ---------------------
1331
1332 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1333 New_Prag : Node_Id;
1334
1335 begin
1336 if No (To) then
1337 To := New_List;
1338 end if;
1339
1340 New_Prag := Relocate_Node (Prag);
1341
1342 Set_Analyzed (New_Prag, False);
1343 Append (New_Prag, To);
1344 end Transfer_Pragma;
1345
1346 -- Local variables
1347
1348 Items : constant Node_Id := Contract (E);
1349 Actuals : List_Id := No_List;
1350 Call : Node_Id;
1351 Call_Nam : Node_Id;
1352 Decls : List_Id := No_List;
1353 Formals : List_Id;
1354 Has_Pragma : Boolean := False;
1355 Index_Id : Entity_Id;
1356 Obj_Id : Entity_Id;
1357 Prag : Node_Id;
1358 Wrapper_Id : Entity_Id;
1359
1360 -- Start of processing for Build_Contract_Wrapper
1361
1362 begin
1363 -- This routine generates a specialized wrapper for a protected or task
1364 -- entry [family] which implements precondition/postcondition semantics.
1365 -- Preconditions and case guards of contract cases are checked before
1366 -- the protected action or rendezvous takes place. Postconditions and
1367 -- consequences of contract cases are checked after the protected action
1368 -- or rendezvous takes place. The structure of the generated wrapper is
1369 -- as follows:
1370
1371 -- procedure Wrapper
1372 -- (Obj_Id : Conc_Typ; -- concurrent object
1373 -- [Index : Index_Typ;] -- index of entry family
1374 -- [Formal_1 : ...; -- parameters of original entry
1375 -- Formal_N : ...])
1376 -- is
1377 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1378 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1379
1380 -- <precondition checks>
1381 -- <case guard checks>
1382
1383 -- procedure _Postconditions is
1384 -- begin
1385 -- <postcondition checks>
1386 -- <consequence checks>
1387 -- end _Postconditions;
1388
1389 -- begin
1390 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1391 -- _Postconditions;
1392 -- end Wrapper;
1393
1394 -- Create the wrapper only when the entry has at least one executable
1395 -- contract item such as contract cases, precondition or postcondition.
1396
1397 if Present (Items) then
1398
1399 -- Inspect the list of pre/postconditions and transfer all available
1400 -- pragmas to the declarative list of the wrapper.
1401
1402 Prag := Pre_Post_Conditions (Items);
1403 while Present (Prag) loop
1404 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
1405 Name_Precondition)
1406 and then Is_Checked (Prag)
1407 then
1408 Has_Pragma := True;
1409 Transfer_Pragma (Prag, To => Decls);
1410 end if;
1411
1412 Prag := Next_Pragma (Prag);
1413 end loop;
1414
1415 -- Inspect the list of test/contract cases and transfer only contract
1416 -- cases pragmas to the declarative part of the wrapper.
1417
1418 Prag := Contract_Test_Cases (Items);
1419 while Present (Prag) loop
1420 if Pragma_Name (Prag) = Name_Contract_Cases
1421 and then Is_Checked (Prag)
1422 then
1423 Has_Pragma := True;
1424 Transfer_Pragma (Prag, To => Decls);
1425 end if;
1426
1427 Prag := Next_Pragma (Prag);
1428 end loop;
1429 end if;
1430
1431 -- The entry lacks executable contract items and a wrapper is not needed
1432
1433 if not Has_Pragma then
1434 return;
1435 end if;
1436
1437 -- Create the profile of the wrapper. The first formal parameter is the
1438 -- concurrent object.
1439
1440 Obj_Id :=
1441 Make_Defining_Identifier (Loc,
1442 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1443
1444 Formals := New_List (
1445 Make_Parameter_Specification (Loc,
1446 Defining_Identifier => Obj_Id,
1447 Out_Present => True,
1448 In_Present => True,
1449 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1450
1451 -- Construct the call to the original entry. The call will be gradually
1452 -- augmented with an optional entry index and extra parameters.
1453
1454 Call_Nam :=
1455 Make_Selected_Component (Loc,
1456 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1457 Selector_Name => New_Occurrence_Of (E, Loc));
1458
1459 -- When creating a wrapper for an entry family, the second formal is the
1460 -- entry index.
1461
1462 if Ekind (E) = E_Entry_Family then
1463 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1464
1465 Append_To (Formals,
1466 Make_Parameter_Specification (Loc,
1467 Defining_Identifier => Index_Id,
1468 Parameter_Type =>
1469 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1470
1471 -- The call to the original entry becomes an indexed component to
1472 -- accommodate the entry index.
1473
1474 Call_Nam :=
1475 Make_Indexed_Component (Loc,
1476 Prefix => Call_Nam,
1477 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1478 end if;
1479
1480 -- Add formal parameters to match those of the entry and build actuals
1481 -- for the entry call.
1482
1483 Add_Matching_Formals (Formals, Actuals);
1484
1485 Call :=
1486 Make_Procedure_Call_Statement (Loc,
1487 Name => Call_Nam,
1488 Parameter_Associations => Actuals);
1489
1490 -- Add renaming declarations for the discriminants of the enclosing type
1491 -- as the various contract items may reference them.
1492
1493 Add_Discriminant_Renamings (Obj_Id, Decls);
1494
1495 Wrapper_Id :=
1496 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1497 Set_Contract_Wrapper (E, Wrapper_Id);
1498 Set_Is_Entry_Wrapper (Wrapper_Id);
1499
1500 -- The wrapper body is analyzed when the enclosing type is frozen
1501
1502 Append_Freeze_Action (Defining_Entity (Decl),
1503 Make_Subprogram_Body (Loc,
1504 Specification =>
1505 Make_Procedure_Specification (Loc,
1506 Defining_Unit_Name => Wrapper_Id,
1507 Parameter_Specifications => Formals),
1508 Declarations => Decls,
1509 Handled_Statement_Sequence =>
1510 Make_Handled_Sequence_Of_Statements (Loc,
1511 Statements => New_List (Call))));
1512 end Build_Contract_Wrapper;
1513
1514 --------------------------------
1515 -- Build_Corresponding_Record --
1516 --------------------------------
1517
1518 function Build_Corresponding_Record
1519 (N : Node_Id;
1520 Ctyp : Entity_Id;
1521 Loc : Source_Ptr) return Node_Id
1522 is
1523 Rec_Ent : constant Entity_Id :=
1524 Make_Defining_Identifier
1525 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1526 Disc : Entity_Id;
1527 Dlist : List_Id;
1528 New_Disc : Entity_Id;
1529 Cdecls : List_Id;
1530
1531 begin
1532 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1533 Set_Ekind (Rec_Ent, E_Record_Type);
1534 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1535 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1536 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1537 Set_Stored_Constraint (Rec_Ent, No_Elist);
1538 Cdecls := New_List;
1539
1540 -- Use discriminals to create list of discriminants for record, and
1541 -- create new discriminals for use in default expressions, etc. It is
1542 -- worth noting that a task discriminant gives rise to 5 entities;
1543
1544 -- a) The original discriminant.
1545 -- b) The discriminal for use in the task.
1546 -- c) The discriminant of the corresponding record.
1547 -- d) The discriminal for the init proc of the corresponding record.
1548 -- e) The local variable that renames the discriminant in the procedure
1549 -- for the task body.
1550
1551 -- In fact the discriminals b) are used in the renaming declarations
1552 -- for e). See details in einfo (Handling of Discriminants).
1553
1554 if Present (Discriminant_Specifications (N)) then
1555 Dlist := New_List;
1556 Disc := First_Discriminant (Ctyp);
1557
1558 while Present (Disc) loop
1559 New_Disc := CR_Discriminant (Disc);
1560
1561 Append_To (Dlist,
1562 Make_Discriminant_Specification (Loc,
1563 Defining_Identifier => New_Disc,
1564 Discriminant_Type =>
1565 New_Occurrence_Of (Etype (Disc), Loc),
1566 Expression =>
1567 New_Copy (Discriminant_Default_Value (Disc))));
1568
1569 Next_Discriminant (Disc);
1570 end loop;
1571
1572 else
1573 Dlist := No_List;
1574 end if;
1575
1576 -- Now we can construct the record type declaration. Note that this
1577 -- record is "limited tagged". It is "limited" to reflect the underlying
1578 -- limitedness of the task or protected object that it represents, and
1579 -- ensuring for example that it is properly passed by reference. It is
1580 -- "tagged" to give support to dispatching calls through interfaces. We
1581 -- propagate here the list of interfaces covered by the concurrent type
1582 -- (Ada 2005: AI-345).
1583
1584 return
1585 Make_Full_Type_Declaration (Loc,
1586 Defining_Identifier => Rec_Ent,
1587 Discriminant_Specifications => Dlist,
1588 Type_Definition =>
1589 Make_Record_Definition (Loc,
1590 Component_List =>
1591 Make_Component_List (Loc, Component_Items => Cdecls),
1592 Tagged_Present =>
1593 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1594 Interface_List => Interface_List (N),
1595 Limited_Present => True));
1596 end Build_Corresponding_Record;
1597
1598 ---------------------------------
1599 -- Build_Dispatching_Tag_Check --
1600 ---------------------------------
1601
1602 function Build_Dispatching_Tag_Check
1603 (K : Entity_Id;
1604 N : Node_Id) return Node_Id
1605 is
1606 Loc : constant Source_Ptr := Sloc (N);
1607
1608 begin
1609 return
1610 Make_Op_Or (Loc,
1611 Make_Op_Eq (Loc,
1612 Left_Opnd =>
1613 New_Occurrence_Of (K, Loc),
1614 Right_Opnd =>
1615 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1616
1617 Make_Op_Eq (Loc,
1618 Left_Opnd =>
1619 New_Occurrence_Of (K, Loc),
1620 Right_Opnd =>
1621 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1622 end Build_Dispatching_Tag_Check;
1623
1624 ----------------------------------
1625 -- Build_Entry_Count_Expression --
1626 ----------------------------------
1627
1628 function Build_Entry_Count_Expression
1629 (Concurrent_Type : Node_Id;
1630 Component_List : List_Id;
1631 Loc : Source_Ptr) return Node_Id
1632 is
1633 Eindx : Nat;
1634 Ent : Entity_Id;
1635 Ecount : Node_Id;
1636 Comp : Node_Id;
1637 Lo : Node_Id;
1638 Hi : Node_Id;
1639 Typ : Entity_Id;
1640 Large : Boolean;
1641
1642 begin
1643 -- Count number of non-family entries
1644
1645 Eindx := 0;
1646 Ent := First_Entity (Concurrent_Type);
1647 while Present (Ent) loop
1648 if Ekind (Ent) = E_Entry then
1649 Eindx := Eindx + 1;
1650 end if;
1651
1652 Next_Entity (Ent);
1653 end loop;
1654
1655 Ecount := Make_Integer_Literal (Loc, Eindx);
1656
1657 -- Loop through entry families building the addition nodes
1658
1659 Ent := First_Entity (Concurrent_Type);
1660 Comp := First (Component_List);
1661 while Present (Ent) loop
1662 if Ekind (Ent) = E_Entry_Family then
1663 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1664 Next (Comp);
1665 end loop;
1666
1667 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1668 Hi := Type_High_Bound (Typ);
1669 Lo := Type_Low_Bound (Typ);
1670 Large := Is_Potentially_Large_Family
1671 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1672 Ecount :=
1673 Make_Op_Add (Loc,
1674 Left_Opnd => Ecount,
1675 Right_Opnd =>
1676 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1677 end if;
1678
1679 Next_Entity (Ent);
1680 end loop;
1681
1682 return Ecount;
1683 end Build_Entry_Count_Expression;
1684
1685 -----------------------
1686 -- Build_Entry_Names --
1687 -----------------------
1688
1689 procedure Build_Entry_Names
1690 (Obj_Ref : Node_Id;
1691 Obj_Typ : Entity_Id;
1692 Stmts : List_Id)
1693 is
1694 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1695 Data : Entity_Id := Empty;
1696 Index : Entity_Id := Empty;
1697 Typ : Entity_Id := Obj_Typ;
1698
1699 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1700 -- Given an entry [family], create a static string which denotes the
1701 -- name of Comp_Id and assign it to the underlying data structure which
1702 -- contains the entry names of a concurrent object.
1703
1704 function Object_Reference return Node_Id;
1705 -- Return a reference to field _object or _task_id depending on the
1706 -- concurrent object being processed.
1707
1708 ----------------------
1709 -- Build_Entry_Name --
1710 ----------------------
1711
1712 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1713 function Build_Range (Def : Node_Id) return Node_Id;
1714 -- Given a discrete subtype definition of an entry family, generate a
1715 -- range node which covers the range of Def's type.
1716
1717 procedure Create_Index_And_Data;
1718 -- Generate the declarations of variables Index and Data. Subsequent
1719 -- calls do nothing.
1720
1721 function Increment_Index return Node_Id;
1722 -- Increment the index used in the assignment of string names to the
1723 -- Data array.
1724
1725 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1726 -- Given the name of a temporary variable, create the following
1727 -- declaration for it:
1728 --
1729 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1730
1731 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1732 -- Given the name of a temporary variable, place it in the array of
1733 -- string names. Generate:
1734 --
1735 -- Data (Index) := Def_Id'Unchecked_Access;
1736
1737 -----------------
1738 -- Build_Range --
1739 -----------------
1740
1741 function Build_Range (Def : Node_Id) return Node_Id is
1742 High : Node_Id := Type_High_Bound (Etype (Def));
1743 Low : Node_Id := Type_Low_Bound (Etype (Def));
1744
1745 begin
1746 -- If a bound references a discriminant, generate an identifier
1747 -- with the same name. Resolution will map it to the formals of
1748 -- the init proc.
1749
1750 if Is_Entity_Name (Low)
1751 and then Ekind (Entity (Low)) = E_Discriminant
1752 then
1753 Low :=
1754 Make_Selected_Component (Loc,
1755 Prefix => New_Copy_Tree (Obj_Ref),
1756 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1757 else
1758 Low := New_Copy_Tree (Low);
1759 end if;
1760
1761 if Is_Entity_Name (High)
1762 and then Ekind (Entity (High)) = E_Discriminant
1763 then
1764 High :=
1765 Make_Selected_Component (Loc,
1766 Prefix => New_Copy_Tree (Obj_Ref),
1767 Selector_Name => Make_Identifier (Loc, Chars (High)));
1768 else
1769 High := New_Copy_Tree (High);
1770 end if;
1771
1772 return
1773 Make_Range (Loc,
1774 Low_Bound => Low,
1775 High_Bound => High);
1776 end Build_Range;
1777
1778 ---------------------------
1779 -- Create_Index_And_Data --
1780 ---------------------------
1781
1782 procedure Create_Index_And_Data is
1783 begin
1784 if No (Index) and then No (Data) then
1785 declare
1786 Count : RE_Id;
1787 Data_Typ : RE_Id;
1788 Size : Entity_Id;
1789
1790 begin
1791 if Is_Protected_Type (Typ) then
1792 Count := RO_PE_Number_Of_Entries;
1793 Data_Typ := RE_Protected_Entry_Names_Array;
1794 else
1795 Count := RO_ST_Number_Of_Entries;
1796 Data_Typ := RE_Task_Entry_Names_Array;
1797 end if;
1798
1799 -- Step 1: Generate the declaration of the index variable:
1800
1801 -- Index : Entry_Index := 1;
1802
1803 Index := Make_Temporary (Loc, 'I');
1804
1805 Append_To (Stmts,
1806 Make_Object_Declaration (Loc,
1807 Defining_Identifier => Index,
1808 Object_Definition =>
1809 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1810 Expression => Make_Integer_Literal (Loc, 1)));
1811
1812 -- Step 2: Generate the declaration of an array to house all
1813 -- names:
1814
1815 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1816 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1817
1818 Size := Make_Temporary (Loc, 'S');
1819
1820 Append_To (Stmts,
1821 Make_Object_Declaration (Loc,
1822 Defining_Identifier => Size,
1823 Constant_Present => True,
1824 Object_Definition =>
1825 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1826 Expression =>
1827 Make_Function_Call (Loc,
1828 Name =>
1829 New_Occurrence_Of (RTE (Count), Loc),
1830 Parameter_Associations =>
1831 New_List (Object_Reference))));
1832
1833 Data := Make_Temporary (Loc, 'A');
1834
1835 Append_To (Stmts,
1836 Make_Object_Declaration (Loc,
1837 Defining_Identifier => Data,
1838 Aliased_Present => True,
1839 Object_Definition =>
1840 New_Occurrence_Of (RTE (Data_Typ), Loc),
1841 Expression =>
1842 Make_Aggregate (Loc,
1843 Component_Associations => New_List (
1844 Make_Component_Association (Loc,
1845 Choices => New_List (
1846 Make_Range (Loc,
1847 Low_Bound =>
1848 Make_Integer_Literal (Loc, 1),
1849 High_Bound =>
1850 New_Occurrence_Of (Size, Loc))),
1851 Expression => Make_Null (Loc))))));
1852 end;
1853 end if;
1854 end Create_Index_And_Data;
1855
1856 ---------------------
1857 -- Increment_Index --
1858 ---------------------
1859
1860 function Increment_Index return Node_Id is
1861 begin
1862 return
1863 Make_Assignment_Statement (Loc,
1864 Name => New_Occurrence_Of (Index, Loc),
1865 Expression =>
1866 Make_Op_Add (Loc,
1867 Left_Opnd => New_Occurrence_Of (Index, Loc),
1868 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1869 end Increment_Index;
1870
1871 ----------------------
1872 -- Name_Declaration --
1873 ----------------------
1874
1875 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1876 begin
1877 return
1878 Make_Object_Declaration (Loc,
1879 Defining_Identifier => Def_Id,
1880 Aliased_Present => True,
1881 Constant_Present => True,
1882 Object_Definition =>
1883 New_Occurrence_Of (Standard_String, Loc),
1884 Expression =>
1885 Make_String_Literal (Loc, String_From_Name_Buffer));
1886 end Name_Declaration;
1887
1888 --------------------
1889 -- Set_Entry_Name --
1890 --------------------
1891
1892 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1893 begin
1894 return
1895 Make_Assignment_Statement (Loc,
1896 Name =>
1897 Make_Indexed_Component (Loc,
1898 Prefix => New_Occurrence_Of (Data, Loc),
1899 Expressions => New_List (New_Occurrence_Of (Index, Loc))),
1900
1901 Expression =>
1902 Make_Attribute_Reference (Loc,
1903 Prefix => New_Occurrence_Of (Def_Id, Loc),
1904 Attribute_Name => Name_Unchecked_Access));
1905 end Set_Entry_Name;
1906
1907 -- Local variables
1908
1909 Temp_Id : Entity_Id;
1910 Subt_Def : Node_Id;
1911
1912 -- Start of processing for Build_Entry_Name
1913
1914 begin
1915 if Ekind (Comp_Id) = E_Entry_Family then
1916 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1917
1918 Create_Index_And_Data;
1919
1920 -- Step 1: Create the string name of the entry family.
1921 -- Generate:
1922 -- Temp : aliased constant String := "name ()";
1923
1924 Temp_Id := Make_Temporary (Loc, 'S');
1925 Get_Name_String (Chars (Comp_Id));
1926 Add_Char_To_Name_Buffer (' ');
1927 Add_Char_To_Name_Buffer ('(');
1928 Add_Char_To_Name_Buffer (')');
1929
1930 Append_To (Stmts, Name_Declaration (Temp_Id));
1931
1932 -- Generate:
1933 -- for Member in Family_Low .. Family_High loop
1934 -- Set_Entry_Name (...);
1935 -- Index := Index + 1;
1936 -- end loop;
1937
1938 Append_To (Stmts,
1939 Make_Loop_Statement (Loc,
1940 Iteration_Scheme =>
1941 Make_Iteration_Scheme (Loc,
1942 Loop_Parameter_Specification =>
1943 Make_Loop_Parameter_Specification (Loc,
1944 Defining_Identifier =>
1945 Make_Temporary (Loc, 'L'),
1946 Discrete_Subtype_Definition =>
1947 Build_Range (Subt_Def))),
1948
1949 Statements => New_List (
1950 Set_Entry_Name (Temp_Id),
1951 Increment_Index),
1952 End_Label => Empty));
1953
1954 -- Entry
1955
1956 else
1957 Create_Index_And_Data;
1958
1959 -- Step 1: Create the string name of the entry. Generate:
1960 -- Temp : aliased constant String := "name";
1961
1962 Temp_Id := Make_Temporary (Loc, 'S');
1963 Get_Name_String (Chars (Comp_Id));
1964
1965 Append_To (Stmts, Name_Declaration (Temp_Id));
1966
1967 -- Step 2: Associate the string name with the underlying data
1968 -- structure.
1969
1970 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1971 Append_To (Stmts, Increment_Index);
1972 end if;
1973 end Build_Entry_Name;
1974
1975 ----------------------
1976 -- Object_Reference --
1977 ----------------------
1978
1979 function Object_Reference return Node_Id is
1980 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1981 Field : Name_Id;
1982 Ref : Node_Id;
1983
1984 begin
1985 if Is_Protected_Type (Typ) then
1986 Field := Name_uObject;
1987 else
1988 Field := Name_uTask_Id;
1989 end if;
1990
1991 Ref :=
1992 Make_Selected_Component (Loc,
1993 Prefix =>
1994 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1995 Selector_Name => Make_Identifier (Loc, Field));
1996
1997 if Is_Protected_Type (Typ) then
1998 Ref :=
1999 Make_Attribute_Reference (Loc,
2000 Prefix => Ref,
2001 Attribute_Name => Name_Unchecked_Access);
2002 end if;
2003
2004 return Ref;
2005 end Object_Reference;
2006
2007 -- Local variables
2008
2009 Comp : Node_Id;
2010 Proc : RE_Id;
2011
2012 -- Start of processing for Build_Entry_Names
2013
2014 begin
2015 -- Retrieve the original concurrent type
2016
2017 if Is_Concurrent_Record_Type (Typ) then
2018 Typ := Corresponding_Concurrent_Type (Typ);
2019 end if;
2020
2021 pragma Assert (Is_Concurrent_Type (Typ));
2022
2023 -- Nothing to do if the type has no entries
2024
2025 if not Has_Entries (Typ) then
2026 return;
2027 end if;
2028
2029 -- Avoid generating entry names for a protected type with only one entry
2030
2031 if Is_Protected_Type (Typ)
2032 and then Find_Protection_Type (Base_Type (Typ)) /=
2033 RTE (RE_Protection_Entries)
2034 then
2035 return;
2036 end if;
2037
2038 -- Step 1: Populate the array with statically generated strings denoting
2039 -- entries and entry family names.
2040
2041 Comp := First_Entity (Typ);
2042 while Present (Comp) loop
2043 if Comes_From_Source (Comp)
2044 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
2045 then
2046 Build_Entry_Name (Comp);
2047 end if;
2048
2049 Next_Entity (Comp);
2050 end loop;
2051
2052 -- Step 2: Associate the array with the related concurrent object:
2053
2054 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
2055
2056 if Present (Data) then
2057 if Is_Protected_Type (Typ) then
2058 Proc := RO_PE_Set_Entry_Names;
2059 else
2060 Proc := RO_ST_Set_Entry_Names;
2061 end if;
2062
2063 Append_To (Stmts,
2064 Make_Procedure_Call_Statement (Loc,
2065 Name => New_Occurrence_Of (RTE (Proc), Loc),
2066 Parameter_Associations => New_List (
2067 Object_Reference,
2068 Make_Attribute_Reference (Loc,
2069 Prefix => New_Occurrence_Of (Data, Loc),
2070 Attribute_Name => Name_Unchecked_Access))));
2071 end if;
2072 end Build_Entry_Names;
2073
2074 ---------------------------
2075 -- Build_Parameter_Block --
2076 ---------------------------
2077
2078 function Build_Parameter_Block
2079 (Loc : Source_Ptr;
2080 Actuals : List_Id;
2081 Formals : List_Id;
2082 Decls : List_Id) return Entity_Id
2083 is
2084 Actual : Entity_Id;
2085 Comp_Nam : Node_Id;
2086 Comps : List_Id;
2087 Formal : Entity_Id;
2088 Has_Comp : Boolean := False;
2089 Rec_Nam : Node_Id;
2090
2091 begin
2092 Actual := First (Actuals);
2093 Comps := New_List;
2094 Formal := Defining_Identifier (First (Formals));
2095
2096 while Present (Actual) loop
2097 if not Is_Controlling_Actual (Actual) then
2098
2099 -- Generate:
2100 -- type Ann is access all <actual-type>
2101
2102 Comp_Nam := Make_Temporary (Loc, 'A');
2103 Set_Is_Param_Block_Component_Type (Comp_Nam);
2104
2105 Append_To (Decls,
2106 Make_Full_Type_Declaration (Loc,
2107 Defining_Identifier => Comp_Nam,
2108 Type_Definition =>
2109 Make_Access_To_Object_Definition (Loc,
2110 All_Present => True,
2111 Constant_Present => Ekind (Formal) = E_In_Parameter,
2112 Subtype_Indication =>
2113 New_Occurrence_Of (Etype (Actual), Loc))));
2114
2115 -- Generate:
2116 -- Param : Ann;
2117
2118 Append_To (Comps,
2119 Make_Component_Declaration (Loc,
2120 Defining_Identifier =>
2121 Make_Defining_Identifier (Loc, Chars (Formal)),
2122 Component_Definition =>
2123 Make_Component_Definition (Loc,
2124 Aliased_Present =>
2125 False,
2126 Subtype_Indication =>
2127 New_Occurrence_Of (Comp_Nam, Loc))));
2128
2129 Has_Comp := True;
2130 end if;
2131
2132 Next_Actual (Actual);
2133 Next_Formal_With_Extras (Formal);
2134 end loop;
2135
2136 Rec_Nam := Make_Temporary (Loc, 'P');
2137
2138 if Has_Comp then
2139
2140 -- Generate:
2141 -- type Pnn is record
2142 -- Param1 : Ann1;
2143 -- ...
2144 -- ParamN : AnnN;
2145
2146 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
2147 -- the original parameter names and Ann1 .. AnnN are the access to
2148 -- actual types.
2149
2150 Append_To (Decls,
2151 Make_Full_Type_Declaration (Loc,
2152 Defining_Identifier =>
2153 Rec_Nam,
2154 Type_Definition =>
2155 Make_Record_Definition (Loc,
2156 Component_List =>
2157 Make_Component_List (Loc, Comps))));
2158 else
2159 -- Generate:
2160 -- type Pnn is null record;
2161
2162 Append_To (Decls,
2163 Make_Full_Type_Declaration (Loc,
2164 Defining_Identifier =>
2165 Rec_Nam,
2166 Type_Definition =>
2167 Make_Record_Definition (Loc,
2168 Null_Present => True,
2169 Component_List => Empty)));
2170 end if;
2171
2172 return Rec_Nam;
2173 end Build_Parameter_Block;
2174
2175 --------------------------------------
2176 -- Build_Renamed_Formal_Declaration --
2177 --------------------------------------
2178
2179 function Build_Renamed_Formal_Declaration
2180 (New_F : Entity_Id;
2181 Formal : Entity_Id;
2182 Comp : Entity_Id;
2183 Renamed_Formal : Node_Id) return Node_Id
2184 is
2185 Loc : constant Source_Ptr := Sloc (New_F);
2186 Decl : Node_Id;
2187
2188 begin
2189 -- If the formal is a tagged incomplete type, it is already passed
2190 -- by reference, so it is sufficient to rename the pointer component
2191 -- that corresponds to the actual. Otherwise we need to dereference
2192 -- the pointer component to obtain the actual.
2193
2194 if Is_Incomplete_Type (Etype (Formal))
2195 and then Is_Tagged_Type (Etype (Formal))
2196 then
2197 Decl :=
2198 Make_Object_Renaming_Declaration (Loc,
2199 Defining_Identifier => New_F,
2200 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
2201 Name => Renamed_Formal);
2202
2203 else
2204 Decl :=
2205 Make_Object_Renaming_Declaration (Loc,
2206 Defining_Identifier => New_F,
2207 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
2208 Name =>
2209 Make_Explicit_Dereference (Loc, Renamed_Formal));
2210 end if;
2211
2212 return Decl;
2213 end Build_Renamed_Formal_Declaration;
2214
2215 --------------------------
2216 -- Build_Wrapper_Bodies --
2217 --------------------------
2218
2219 procedure Build_Wrapper_Bodies
2220 (Loc : Source_Ptr;
2221 Typ : Entity_Id;
2222 N : Node_Id)
2223 is
2224 Rec_Typ : Entity_Id;
2225
2226 function Build_Wrapper_Body
2227 (Loc : Source_Ptr;
2228 Subp_Id : Entity_Id;
2229 Obj_Typ : Entity_Id;
2230 Formals : List_Id) return Node_Id;
2231 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2232 -- associated with a protected or task type. Subp_Id is the subprogram
2233 -- name which will be wrapped. Obj_Typ is the type of the new formal
2234 -- parameter which handles dispatching and object notation. Formals are
2235 -- the original formals of Subp_Id which will be explicitly replicated.
2236
2237 ------------------------
2238 -- Build_Wrapper_Body --
2239 ------------------------
2240
2241 function Build_Wrapper_Body
2242 (Loc : Source_Ptr;
2243 Subp_Id : Entity_Id;
2244 Obj_Typ : Entity_Id;
2245 Formals : List_Id) return Node_Id
2246 is
2247 Body_Spec : Node_Id;
2248
2249 begin
2250 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2251
2252 -- The subprogram is not overriding or is not a primitive declared
2253 -- between two views.
2254
2255 if No (Body_Spec) then
2256 return Empty;
2257 end if;
2258
2259 declare
2260 Actuals : List_Id := No_List;
2261 Conv_Id : Node_Id;
2262 First_Form : Node_Id;
2263 Formal : Node_Id;
2264 Nam : Node_Id;
2265
2266 begin
2267 -- Map formals to actuals. Use the list built for the wrapper
2268 -- spec, skipping the object notation parameter.
2269
2270 First_Form := First (Parameter_Specifications (Body_Spec));
2271
2272 Formal := First_Form;
2273 Next (Formal);
2274
2275 if Present (Formal) then
2276 Actuals := New_List;
2277 while Present (Formal) loop
2278 Append_To (Actuals,
2279 Make_Identifier (Loc,
2280 Chars => Chars (Defining_Identifier (Formal))));
2281 Next (Formal);
2282 end loop;
2283 end if;
2284
2285 -- Special processing for primitives declared between a private
2286 -- type and its completion: the wrapper needs a properly typed
2287 -- parameter if the wrapped operation has a controlling first
2288 -- parameter. Note that this might not be the case for a function
2289 -- with a controlling result.
2290
2291 if Is_Private_Primitive_Subprogram (Subp_Id) then
2292 if No (Actuals) then
2293 Actuals := New_List;
2294 end if;
2295
2296 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2297 Prepend_To (Actuals,
2298 Unchecked_Convert_To
2299 (Corresponding_Concurrent_Type (Obj_Typ),
2300 Make_Identifier (Loc, Name_uO)));
2301
2302 else
2303 Prepend_To (Actuals,
2304 Make_Identifier (Loc,
2305 Chars => Chars (Defining_Identifier (First_Form))));
2306 end if;
2307
2308 Nam := New_Occurrence_Of (Subp_Id, Loc);
2309 else
2310 -- An access-to-variable object parameter requires an explicit
2311 -- dereference in the unchecked conversion. This case occurs
2312 -- when a protected entry wrapper must override an interface
2313 -- level procedure with interface access as first parameter.
2314
2315 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2316
2317 if Nkind (Parameter_Type (First_Form)) =
2318 N_Access_Definition
2319 then
2320 Conv_Id :=
2321 Make_Explicit_Dereference (Loc,
2322 Prefix => Make_Identifier (Loc, Name_uO));
2323 else
2324 Conv_Id := Make_Identifier (Loc, Name_uO);
2325 end if;
2326
2327 Nam :=
2328 Make_Selected_Component (Loc,
2329 Prefix =>
2330 Unchecked_Convert_To
2331 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2332 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
2333 end if;
2334
2335 -- Create the subprogram body. For a function, the call to the
2336 -- actual subprogram has to be converted to the corresponding
2337 -- record if it is a controlling result.
2338
2339 if Ekind (Subp_Id) = E_Function then
2340 declare
2341 Res : Node_Id;
2342
2343 begin
2344 Res :=
2345 Make_Function_Call (Loc,
2346 Name => Nam,
2347 Parameter_Associations => Actuals);
2348
2349 if Has_Controlling_Result (Subp_Id) then
2350 Res :=
2351 Unchecked_Convert_To
2352 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2353 end if;
2354
2355 return
2356 Make_Subprogram_Body (Loc,
2357 Specification => Body_Spec,
2358 Declarations => Empty_List,
2359 Handled_Statement_Sequence =>
2360 Make_Handled_Sequence_Of_Statements (Loc,
2361 Statements => New_List (
2362 Make_Simple_Return_Statement (Loc, Res))));
2363 end;
2364
2365 else
2366 return
2367 Make_Subprogram_Body (Loc,
2368 Specification => Body_Spec,
2369 Declarations => Empty_List,
2370 Handled_Statement_Sequence =>
2371 Make_Handled_Sequence_Of_Statements (Loc,
2372 Statements => New_List (
2373 Make_Procedure_Call_Statement (Loc,
2374 Name => Nam,
2375 Parameter_Associations => Actuals))));
2376 end if;
2377 end;
2378 end Build_Wrapper_Body;
2379
2380 -- Start of processing for Build_Wrapper_Bodies
2381
2382 begin
2383 if Is_Concurrent_Type (Typ) then
2384 Rec_Typ := Corresponding_Record_Type (Typ);
2385 else
2386 Rec_Typ := Typ;
2387 end if;
2388
2389 -- Generate wrapper bodies for a concurrent type which implements an
2390 -- interface.
2391
2392 if Present (Interfaces (Rec_Typ)) then
2393 declare
2394 Insert_Nod : Node_Id;
2395 Prim : Entity_Id;
2396 Prim_Elmt : Elmt_Id;
2397 Prim_Decl : Node_Id;
2398 Subp : Entity_Id;
2399 Wrap_Body : Node_Id;
2400 Wrap_Id : Entity_Id;
2401
2402 begin
2403 Insert_Nod := N;
2404
2405 -- Examine all primitive operations of the corresponding record
2406 -- type, looking for wrapper specs. Generate bodies in order to
2407 -- complete them.
2408
2409 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2410 while Present (Prim_Elmt) loop
2411 Prim := Node (Prim_Elmt);
2412
2413 if (Ekind (Prim) = E_Function
2414 or else Ekind (Prim) = E_Procedure)
2415 and then Is_Primitive_Wrapper (Prim)
2416 then
2417 Subp := Wrapped_Entity (Prim);
2418 Prim_Decl := Parent (Parent (Prim));
2419
2420 Wrap_Body :=
2421 Build_Wrapper_Body (Loc,
2422 Subp_Id => Subp,
2423 Obj_Typ => Rec_Typ,
2424 Formals => Parameter_Specifications (Parent (Subp)));
2425 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2426
2427 Set_Corresponding_Spec (Wrap_Body, Prim);
2428 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2429
2430 Insert_After (Insert_Nod, Wrap_Body);
2431 Insert_Nod := Wrap_Body;
2432
2433 Analyze (Wrap_Body);
2434 end if;
2435
2436 Next_Elmt (Prim_Elmt);
2437 end loop;
2438 end;
2439 end if;
2440 end Build_Wrapper_Bodies;
2441
2442 ------------------------
2443 -- Build_Wrapper_Spec --
2444 ------------------------
2445
2446 function Build_Wrapper_Spec
2447 (Subp_Id : Entity_Id;
2448 Obj_Typ : Entity_Id;
2449 Formals : List_Id) return Node_Id
2450 is
2451 function Overriding_Possible
2452 (Iface_Op : Entity_Id;
2453 Wrapper : Entity_Id) return Boolean;
2454 -- Determine whether a primitive operation can be overridden by Wrapper.
2455 -- Iface_Op is the candidate primitive operation of an interface type,
2456 -- Wrapper is the generated entry wrapper.
2457
2458 function Replicate_Formals
2459 (Loc : Source_Ptr;
2460 Formals : List_Id) return List_Id;
2461 -- An explicit parameter replication is required due to the Is_Entry_
2462 -- Formal flag being set for all the formals of an entry. The explicit
2463 -- replication removes the flag that would otherwise cause a different
2464 -- path of analysis.
2465
2466 -------------------------
2467 -- Overriding_Possible --
2468 -------------------------
2469
2470 function Overriding_Possible
2471 (Iface_Op : Entity_Id;
2472 Wrapper : Entity_Id) return Boolean
2473 is
2474 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2475 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2476
2477 function Type_Conformant_Parameters
2478 (Iface_Op_Params : List_Id;
2479 Wrapper_Params : List_Id) return Boolean;
2480 -- Determine whether the parameters of the generated entry wrapper
2481 -- and those of a primitive operation are type conformant. During
2482 -- this check, the first parameter of the primitive operation is
2483 -- skipped if it is a controlling argument: protected functions
2484 -- may have a controlling result.
2485
2486 --------------------------------
2487 -- Type_Conformant_Parameters --
2488 --------------------------------
2489
2490 function Type_Conformant_Parameters
2491 (Iface_Op_Params : List_Id;
2492 Wrapper_Params : List_Id) return Boolean
2493 is
2494 Iface_Op_Param : Node_Id;
2495 Iface_Op_Typ : Entity_Id;
2496 Wrapper_Param : Node_Id;
2497 Wrapper_Typ : Entity_Id;
2498
2499 begin
2500 -- Skip the first (controlling) parameter of primitive operation
2501
2502 Iface_Op_Param := First (Iface_Op_Params);
2503
2504 if Present (First_Formal (Iface_Op))
2505 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2506 then
2507 Iface_Op_Param := Next (Iface_Op_Param);
2508 end if;
2509
2510 Wrapper_Param := First (Wrapper_Params);
2511 while Present (Iface_Op_Param)
2512 and then Present (Wrapper_Param)
2513 loop
2514 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2515 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2516
2517 -- The two parameters must be mode conformant
2518
2519 if not Conforming_Types
2520 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2521 then
2522 return False;
2523 end if;
2524
2525 Next (Iface_Op_Param);
2526 Next (Wrapper_Param);
2527 end loop;
2528
2529 -- One of the lists is longer than the other
2530
2531 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2532 return False;
2533 end if;
2534
2535 return True;
2536 end Type_Conformant_Parameters;
2537
2538 -- Start of processing for Overriding_Possible
2539
2540 begin
2541 if Chars (Iface_Op) /= Chars (Wrapper) then
2542 return False;
2543 end if;
2544
2545 -- If an inherited subprogram is implemented by a protected procedure
2546 -- or an entry, then the first parameter of the inherited subprogram
2547 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2548
2549 if Ekind (Iface_Op) = E_Procedure
2550 and then Present (Parameter_Specifications (Iface_Op_Spec))
2551 then
2552 declare
2553 Obj_Param : constant Node_Id :=
2554 First (Parameter_Specifications (Iface_Op_Spec));
2555 begin
2556 if not Out_Present (Obj_Param)
2557 and then Nkind (Parameter_Type (Obj_Param)) /=
2558 N_Access_Definition
2559 then
2560 return False;
2561 end if;
2562 end;
2563 end if;
2564
2565 return
2566 Type_Conformant_Parameters
2567 (Parameter_Specifications (Iface_Op_Spec),
2568 Parameter_Specifications (Wrapper_Spec));
2569 end Overriding_Possible;
2570
2571 -----------------------
2572 -- Replicate_Formals --
2573 -----------------------
2574
2575 function Replicate_Formals
2576 (Loc : Source_Ptr;
2577 Formals : List_Id) return List_Id
2578 is
2579 New_Formals : constant List_Id := New_List;
2580 Formal : Node_Id;
2581 Param_Type : Node_Id;
2582
2583 begin
2584 Formal := First (Formals);
2585
2586 -- Skip the object parameter when dealing with primitives declared
2587 -- between two views.
2588
2589 if Is_Private_Primitive_Subprogram (Subp_Id)
2590 and then not Has_Controlling_Result (Subp_Id)
2591 then
2592 Formal := Next (Formal);
2593 end if;
2594
2595 while Present (Formal) loop
2596
2597 -- Create an explicit copy of the entry parameter
2598
2599 -- When creating the wrapper subprogram for a primitive operation
2600 -- of a protected interface we must construct an equivalent
2601 -- signature to that of the overriding operation. For regular
2602 -- parameters we can just use the type of the formal, but for
2603 -- access to subprogram parameters we need to reanalyze the
2604 -- parameter type to create local entities for the signature of
2605 -- the subprogram type. Using the entities of the overriding
2606 -- subprogram will result in out-of-scope errors in the back-end.
2607
2608 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2609 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2610 else
2611 Param_Type :=
2612 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2613 end if;
2614
2615 Append_To (New_Formals,
2616 Make_Parameter_Specification (Loc,
2617 Defining_Identifier =>
2618 Make_Defining_Identifier (Loc,
2619 Chars => Chars (Defining_Identifier (Formal))),
2620 In_Present => In_Present (Formal),
2621 Out_Present => Out_Present (Formal),
2622 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2623 Parameter_Type => Param_Type));
2624
2625 Next (Formal);
2626 end loop;
2627
2628 return New_Formals;
2629 end Replicate_Formals;
2630
2631 -- Local variables
2632
2633 Loc : constant Source_Ptr := Sloc (Subp_Id);
2634 First_Param : Node_Id := Empty;
2635 Iface : Entity_Id;
2636 Iface_Elmt : Elmt_Id;
2637 Iface_Op : Entity_Id;
2638 Iface_Op_Elmt : Elmt_Id;
2639 Overridden_Subp : Entity_Id;
2640
2641 -- Start of processing for Build_Wrapper_Spec
2642
2643 begin
2644 -- No point in building wrappers for untagged concurrent types
2645
2646 pragma Assert (Is_Tagged_Type (Obj_Typ));
2647
2648 -- Check if this subprogram has a profile that matches some interface
2649 -- primitive.
2650
2651 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2652
2653 if Present (Overridden_Subp) then
2654 First_Param :=
2655 First (Parameter_Specifications (Parent (Overridden_Subp)));
2656
2657 -- An entry or a protected procedure can override a routine where the
2658 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2659 -- type. Since the wrapper must have the exact same signature as that of
2660 -- the overridden subprogram, we try to find the overriding candidate
2661 -- and use its controlling formal.
2662
2663 -- Check every implemented interface
2664
2665 elsif Present (Interfaces (Obj_Typ)) then
2666 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2667 Search : while Present (Iface_Elmt) loop
2668 Iface := Node (Iface_Elmt);
2669
2670 -- Check every interface primitive
2671
2672 if Present (Primitive_Operations (Iface)) then
2673 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2674 while Present (Iface_Op_Elmt) loop
2675 Iface_Op := Node (Iface_Op_Elmt);
2676
2677 -- Ignore predefined primitives
2678
2679 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2680 Iface_Op := Ultimate_Alias (Iface_Op);
2681
2682 -- The current primitive operation can be overridden by
2683 -- the generated entry wrapper.
2684
2685 if Overriding_Possible (Iface_Op, Subp_Id) then
2686 First_Param :=
2687 First (Parameter_Specifications (Parent (Iface_Op)));
2688
2689 exit Search;
2690 end if;
2691 end if;
2692
2693 Next_Elmt (Iface_Op_Elmt);
2694 end loop;
2695 end if;
2696
2697 Next_Elmt (Iface_Elmt);
2698 end loop Search;
2699 end if;
2700
2701 -- Do not generate the wrapper if no interface primitive is covered by
2702 -- the subprogram and it is not a primitive declared between two views
2703 -- (see Process_Full_View).
2704
2705 if No (First_Param)
2706 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2707 then
2708 return Empty;
2709 end if;
2710
2711 declare
2712 Wrapper_Id : constant Entity_Id :=
2713 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2714 New_Formals : List_Id;
2715 Obj_Param : Node_Id;
2716 Obj_Param_Typ : Entity_Id;
2717
2718 begin
2719 -- Minimum decoration is needed to catch the entity in
2720 -- Sem_Ch6.Override_Dispatching_Operation.
2721
2722 if Ekind (Subp_Id) = E_Function then
2723 Set_Ekind (Wrapper_Id, E_Function);
2724 else
2725 Set_Ekind (Wrapper_Id, E_Procedure);
2726 end if;
2727
2728 Set_Is_Primitive_Wrapper (Wrapper_Id);
2729 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2730 Set_Is_Private_Primitive (Wrapper_Id,
2731 Is_Private_Primitive_Subprogram (Subp_Id));
2732
2733 -- Process the formals
2734
2735 New_Formals := Replicate_Formals (Loc, Formals);
2736
2737 -- A function with a controlling result and no first controlling
2738 -- formal needs no additional parameter.
2739
2740 if Has_Controlling_Result (Subp_Id)
2741 and then
2742 (No (First_Formal (Subp_Id))
2743 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2744 then
2745 null;
2746
2747 -- Routine Subp_Id has been found to override an interface primitive.
2748 -- If the interface operation has an access parameter, create a copy
2749 -- of it, with the same null exclusion indicator if present.
2750
2751 elsif Present (First_Param) then
2752 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2753 Obj_Param_Typ :=
2754 Make_Access_Definition (Loc,
2755 Subtype_Mark =>
2756 New_Occurrence_Of (Obj_Typ, Loc),
2757 Null_Exclusion_Present =>
2758 Null_Exclusion_Present (Parameter_Type (First_Param)),
2759 Constant_Present =>
2760 Constant_Present (Parameter_Type (First_Param)));
2761 else
2762 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2763 end if;
2764
2765 Obj_Param :=
2766 Make_Parameter_Specification (Loc,
2767 Defining_Identifier =>
2768 Make_Defining_Identifier (Loc,
2769 Chars => Name_uO),
2770 In_Present => In_Present (First_Param),
2771 Out_Present => Out_Present (First_Param),
2772 Parameter_Type => Obj_Param_Typ);
2773
2774 Prepend_To (New_Formals, Obj_Param);
2775
2776 -- If we are dealing with a primitive declared between two views,
2777 -- implemented by a synchronized operation, we need to create
2778 -- a default parameter. The mode of the parameter must match that
2779 -- of the primitive operation.
2780
2781 else
2782 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2783
2784 Obj_Param :=
2785 Make_Parameter_Specification (Loc,
2786 Defining_Identifier =>
2787 Make_Defining_Identifier (Loc, Name_uO),
2788 In_Present =>
2789 In_Present (Parent (First_Entity (Subp_Id))),
2790 Out_Present => Ekind (Subp_Id) /= E_Function,
2791 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2792
2793 Prepend_To (New_Formals, Obj_Param);
2794 end if;
2795
2796 -- Build the final spec. If it is a function with a controlling
2797 -- result, it is a primitive operation of the corresponding
2798 -- record type, so mark the spec accordingly.
2799
2800 if Ekind (Subp_Id) = E_Function then
2801 declare
2802 Res_Def : Node_Id;
2803
2804 begin
2805 if Has_Controlling_Result (Subp_Id) then
2806 Res_Def :=
2807 New_Occurrence_Of
2808 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2809 else
2810 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2811 end if;
2812
2813 return
2814 Make_Function_Specification (Loc,
2815 Defining_Unit_Name => Wrapper_Id,
2816 Parameter_Specifications => New_Formals,
2817 Result_Definition => Res_Def);
2818 end;
2819 else
2820 return
2821 Make_Procedure_Specification (Loc,
2822 Defining_Unit_Name => Wrapper_Id,
2823 Parameter_Specifications => New_Formals);
2824 end if;
2825 end;
2826 end Build_Wrapper_Spec;
2827
2828 -------------------------
2829 -- Build_Wrapper_Specs --
2830 -------------------------
2831
2832 procedure Build_Wrapper_Specs
2833 (Loc : Source_Ptr;
2834 Typ : Entity_Id;
2835 N : in out Node_Id)
2836 is
2837 Def : Node_Id;
2838 Rec_Typ : Entity_Id;
2839 procedure Scan_Declarations (L : List_Id);
2840 -- Common processing for visible and private declarations
2841 -- of a protected type.
2842
2843 procedure Scan_Declarations (L : List_Id) is
2844 Decl : Node_Id;
2845 Wrap_Decl : Node_Id;
2846 Wrap_Spec : Node_Id;
2847
2848 begin
2849 if No (L) then
2850 return;
2851 end if;
2852
2853 Decl := First (L);
2854 while Present (Decl) loop
2855 Wrap_Spec := Empty;
2856
2857 if Nkind (Decl) = N_Entry_Declaration
2858 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2859 then
2860 Wrap_Spec :=
2861 Build_Wrapper_Spec
2862 (Subp_Id => Defining_Identifier (Decl),
2863 Obj_Typ => Rec_Typ,
2864 Formals => Parameter_Specifications (Decl));
2865
2866 elsif Nkind (Decl) = N_Subprogram_Declaration then
2867 Wrap_Spec :=
2868 Build_Wrapper_Spec
2869 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2870 Obj_Typ => Rec_Typ,
2871 Formals =>
2872 Parameter_Specifications (Specification (Decl)));
2873 end if;
2874
2875 if Present (Wrap_Spec) then
2876 Wrap_Decl :=
2877 Make_Subprogram_Declaration (Loc,
2878 Specification => Wrap_Spec);
2879
2880 Insert_After (N, Wrap_Decl);
2881 N := Wrap_Decl;
2882
2883 Analyze (Wrap_Decl);
2884 end if;
2885
2886 Next (Decl);
2887 end loop;
2888 end Scan_Declarations;
2889
2890 -- start of processing for Build_Wrapper_Specs
2891
2892 begin
2893 if Is_Protected_Type (Typ) then
2894 Def := Protected_Definition (Parent (Typ));
2895 else pragma Assert (Is_Task_Type (Typ));
2896 Def := Task_Definition (Parent (Typ));
2897 end if;
2898
2899 Rec_Typ := Corresponding_Record_Type (Typ);
2900
2901 -- Generate wrapper specs for a concurrent type which implements an
2902 -- interface. Operations in both the visible and private parts may
2903 -- implement progenitor operations.
2904
2905 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2906 Scan_Declarations (Visible_Declarations (Def));
2907 Scan_Declarations (Private_Declarations (Def));
2908 end if;
2909 end Build_Wrapper_Specs;
2910
2911 ---------------------------
2912 -- Build_Find_Body_Index --
2913 ---------------------------
2914
2915 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2916 Loc : constant Source_Ptr := Sloc (Typ);
2917 Ent : Entity_Id;
2918 E_Typ : Entity_Id;
2919 Has_F : Boolean := False;
2920 Index : Nat;
2921 If_St : Node_Id := Empty;
2922 Lo : Node_Id;
2923 Hi : Node_Id;
2924 Decls : List_Id := New_List;
2925 Ret : Node_Id;
2926 Spec : Node_Id;
2927 Siz : Node_Id := Empty;
2928
2929 procedure Add_If_Clause (Expr : Node_Id);
2930 -- Add test for range of current entry
2931
2932 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2933 -- If a bound of an entry is given by a discriminant, retrieve the
2934 -- actual value of the discriminant from the enclosing object.
2935
2936 -------------------
2937 -- Add_If_Clause --
2938 -------------------
2939
2940 procedure Add_If_Clause (Expr : Node_Id) is
2941 Cond : Node_Id;
2942 Stats : constant List_Id :=
2943 New_List (
2944 Make_Simple_Return_Statement (Loc,
2945 Expression => Make_Integer_Literal (Loc, Index + 1)));
2946
2947 begin
2948 -- Index for current entry body
2949
2950 Index := Index + 1;
2951
2952 -- Compute total length of entry queues so far
2953
2954 if No (Siz) then
2955 Siz := Expr;
2956 else
2957 Siz :=
2958 Make_Op_Add (Loc,
2959 Left_Opnd => Siz,
2960 Right_Opnd => Expr);
2961 end if;
2962
2963 Cond :=
2964 Make_Op_Le (Loc,
2965 Left_Opnd => Make_Identifier (Loc, Name_uE),
2966 Right_Opnd => Siz);
2967
2968 -- Map entry queue indexes in the range of the current family
2969 -- into the current index, that designates the entry body.
2970
2971 if No (If_St) then
2972 If_St :=
2973 Make_Implicit_If_Statement (Typ,
2974 Condition => Cond,
2975 Then_Statements => Stats,
2976 Elsif_Parts => New_List);
2977 Ret := If_St;
2978
2979 else
2980 Append_To (Elsif_Parts (If_St),
2981 Make_Elsif_Part (Loc,
2982 Condition => Cond,
2983 Then_Statements => Stats));
2984 end if;
2985 end Add_If_Clause;
2986
2987 ------------------------------
2988 -- Convert_Discriminant_Ref --
2989 ------------------------------
2990
2991 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2992 B : Node_Id;
2993
2994 begin
2995 if Is_Entity_Name (Bound)
2996 and then Ekind (Entity (Bound)) = E_Discriminant
2997 then
2998 B :=
2999 Make_Selected_Component (Loc,
3000 Prefix =>
3001 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
3002 Make_Explicit_Dereference (Loc,
3003 Make_Identifier (Loc, Name_uObject))),
3004 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
3005 Set_Etype (B, Etype (Entity (Bound)));
3006 else
3007 B := New_Copy_Tree (Bound);
3008 end if;
3009
3010 return B;
3011 end Convert_Discriminant_Ref;
3012
3013 -- Start of processing for Build_Find_Body_Index
3014
3015 begin
3016 Spec := Build_Find_Body_Index_Spec (Typ);
3017
3018 Ent := First_Entity (Typ);
3019 while Present (Ent) loop
3020 if Ekind (Ent) = E_Entry_Family then
3021 Has_F := True;
3022 exit;
3023 end if;
3024
3025 Next_Entity (Ent);
3026 end loop;
3027
3028 if not Has_F then
3029
3030 -- If the protected type has no entry families, there is a one-one
3031 -- correspondence between entry queue and entry body.
3032
3033 Ret :=
3034 Make_Simple_Return_Statement (Loc,
3035 Expression => Make_Identifier (Loc, Name_uE));
3036
3037 else
3038 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
3039 -- the following:
3040
3041 -- if E <= l1 then return 1;
3042 -- elsif E <= l1 + l2 then return 2;
3043 -- ...
3044
3045 Index := 0;
3046 Siz := Empty;
3047 Ent := First_Entity (Typ);
3048
3049 Add_Object_Pointer (Loc, Typ, Decls);
3050
3051 while Present (Ent) loop
3052 if Ekind (Ent) = E_Entry then
3053 Add_If_Clause (Make_Integer_Literal (Loc, 1));
3054
3055 elsif Ekind (Ent) = E_Entry_Family then
3056 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
3057 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
3058 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
3059 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
3060 end if;
3061
3062 Next_Entity (Ent);
3063 end loop;
3064
3065 if Index = 1 then
3066 Decls := New_List;
3067 Ret :=
3068 Make_Simple_Return_Statement (Loc,
3069 Expression => Make_Integer_Literal (Loc, 1));
3070
3071 elsif Nkind (Ret) = N_If_Statement then
3072
3073 -- Ranges are in increasing order, so last one doesn't need guard
3074
3075 declare
3076 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
3077 begin
3078 Remove (Nod);
3079 Set_Else_Statements (Ret, Then_Statements (Nod));
3080 end;
3081 end if;
3082 end if;
3083
3084 return
3085 Make_Subprogram_Body (Loc,
3086 Specification => Spec,
3087 Declarations => Decls,
3088 Handled_Statement_Sequence =>
3089 Make_Handled_Sequence_Of_Statements (Loc,
3090 Statements => New_List (Ret)));
3091 end Build_Find_Body_Index;
3092
3093 --------------------------------
3094 -- Build_Find_Body_Index_Spec --
3095 --------------------------------
3096
3097 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
3098 Loc : constant Source_Ptr := Sloc (Typ);
3099 Id : constant Entity_Id :=
3100 Make_Defining_Identifier (Loc,
3101 Chars => New_External_Name (Chars (Typ), 'F'));
3102 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
3103 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
3104
3105 begin
3106 return
3107 Make_Function_Specification (Loc,
3108 Defining_Unit_Name => Id,
3109 Parameter_Specifications => New_List (
3110 Make_Parameter_Specification (Loc,
3111 Defining_Identifier => Parm1,
3112 Parameter_Type =>
3113 New_Occurrence_Of (RTE (RE_Address), Loc)),
3114
3115 Make_Parameter_Specification (Loc,
3116 Defining_Identifier => Parm2,
3117 Parameter_Type =>
3118 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
3119
3120 Result_Definition => New_Occurrence_Of (
3121 RTE (RE_Protected_Entry_Index), Loc));
3122 end Build_Find_Body_Index_Spec;
3123
3124 -----------------------------------------------
3125 -- Build_Lock_Free_Protected_Subprogram_Body --
3126 -----------------------------------------------
3127
3128 function Build_Lock_Free_Protected_Subprogram_Body
3129 (N : Node_Id;
3130 Prot_Typ : Node_Id;
3131 Unprot_Spec : Node_Id) return Node_Id
3132 is
3133 Actuals : constant List_Id := New_List;
3134 Loc : constant Source_Ptr := Sloc (N);
3135 Spec : constant Node_Id := Specification (N);
3136 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3137 Formal : Node_Id;
3138 Prot_Spec : Node_Id;
3139 Stmt : Node_Id;
3140
3141 begin
3142 -- Create the protected version of the body
3143
3144 Prot_Spec :=
3145 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3146
3147 -- Build the actual parameters which appear in the call to the
3148 -- unprotected version of the body.
3149
3150 Formal := First (Parameter_Specifications (Prot_Spec));
3151 while Present (Formal) loop
3152 Append_To (Actuals,
3153 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3154
3155 Next (Formal);
3156 end loop;
3157
3158 -- Function case, generate:
3159 -- return <Unprot_Func_Call>;
3160
3161 if Nkind (Spec) = N_Function_Specification then
3162 Stmt :=
3163 Make_Simple_Return_Statement (Loc,
3164 Expression =>
3165 Make_Function_Call (Loc,
3166 Name =>
3167 Make_Identifier (Loc, Chars (Unprot_Id)),
3168 Parameter_Associations => Actuals));
3169
3170 -- Procedure case, call the unprotected version
3171
3172 else
3173 Stmt :=
3174 Make_Procedure_Call_Statement (Loc,
3175 Name =>
3176 Make_Identifier (Loc, Chars (Unprot_Id)),
3177 Parameter_Associations => Actuals);
3178 end if;
3179
3180 return
3181 Make_Subprogram_Body (Loc,
3182 Declarations => Empty_List,
3183 Specification => Prot_Spec,
3184 Handled_Statement_Sequence =>
3185 Make_Handled_Sequence_Of_Statements (Loc,
3186 Statements => New_List (Stmt)));
3187 end Build_Lock_Free_Protected_Subprogram_Body;
3188
3189 -------------------------------------------------
3190 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3191 -------------------------------------------------
3192
3193 -- Procedures which meet the lock-free implementation requirements and
3194 -- reference a unique scalar component Comp are expanded in the following
3195 -- manner:
3196
3197 -- procedure P (...) is
3198 -- Expected_Comp : constant Comp_Type :=
3199 -- Comp_Type
3200 -- (System.Atomic_Primitives.Lock_Free_Read_N
3201 -- (_Object.Comp'Address));
3202 -- begin
3203 -- loop
3204 -- declare
3205 -- <original declarations before the object renaming declaration
3206 -- of Comp>
3207 --
3208 -- Desired_Comp : Comp_Type := Expected_Comp;
3209 -- Comp : Comp_Type renames Desired_Comp;
3210 --
3211 -- <original delarations after the object renaming declaration
3212 -- of Comp>
3213 --
3214 -- begin
3215 -- <original statements>
3216 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3217 -- (_Object.Comp'Address,
3218 -- Interfaces.Unsigned_N (Expected_Comp),
3219 -- Interfaces.Unsigned_N (Desired_Comp));
3220 -- end;
3221 -- end loop;
3222 -- end P;
3223
3224 -- Each return and raise statement of P is transformed into an atomic
3225 -- status check:
3226
3227 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3228 -- (_Object.Comp'Address,
3229 -- Interfaces.Unsigned_N (Expected_Comp),
3230 -- Interfaces.Unsigned_N (Desired_Comp));
3231 -- then
3232 -- <original statement>
3233 -- else
3234 -- goto L0;
3235 -- end if;
3236
3237 -- Functions which meet the lock-free implementation requirements and
3238 -- reference a unique scalar component Comp are expanded in the following
3239 -- manner:
3240
3241 -- function F (...) return ... is
3242 -- <original declarations before the object renaming declaration
3243 -- of Comp>
3244 --
3245 -- Expected_Comp : constant Comp_Type :=
3246 -- Comp_Type
3247 -- (System.Atomic_Primitives.Lock_Free_Read_N
3248 -- (_Object.Comp'Address));
3249 -- Comp : Comp_Type renames Expected_Comp;
3250 --
3251 -- <original delarations after the object renaming declaration of
3252 -- Comp>
3253 --
3254 -- begin
3255 -- <original statements>
3256 -- end F;
3257
3258 function Build_Lock_Free_Unprotected_Subprogram_Body
3259 (N : Node_Id;
3260 Prot_Typ : Node_Id) return Node_Id
3261 is
3262 function Referenced_Component (N : Node_Id) return Entity_Id;
3263 -- Subprograms which meet the lock-free implementation criteria are
3264 -- allowed to reference only one unique component. Return the prival
3265 -- of the said component.
3266
3267 --------------------------
3268 -- Referenced_Component --
3269 --------------------------
3270
3271 function Referenced_Component (N : Node_Id) return Entity_Id is
3272 Comp : Entity_Id;
3273 Decl : Node_Id;
3274 Source_Comp : Entity_Id := Empty;
3275
3276 begin
3277 -- Find the unique source component which N references in its
3278 -- statements.
3279
3280 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3281 declare
3282 Element : Lock_Free_Subprogram renames
3283 Lock_Free_Subprogram_Table.Table (Index);
3284 begin
3285 if Element.Sub_Body = N then
3286 Source_Comp := Element.Comp_Id;
3287 exit;
3288 end if;
3289 end;
3290 end loop;
3291
3292 if No (Source_Comp) then
3293 return Empty;
3294 end if;
3295
3296 -- Find the prival which corresponds to the source component within
3297 -- the declarations of N.
3298
3299 Decl := First (Declarations (N));
3300 while Present (Decl) loop
3301
3302 -- Privals appear as object renamings
3303
3304 if Nkind (Decl) = N_Object_Renaming_Declaration then
3305 Comp := Defining_Identifier (Decl);
3306
3307 if Present (Prival_Link (Comp))
3308 and then Prival_Link (Comp) = Source_Comp
3309 then
3310 return Comp;
3311 end if;
3312 end if;
3313
3314 Next (Decl);
3315 end loop;
3316
3317 return Empty;
3318 end Referenced_Component;
3319
3320 -- Local variables
3321
3322 Comp : constant Entity_Id := Referenced_Component (N);
3323 Loc : constant Source_Ptr := Sloc (N);
3324 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3325 Decls : List_Id := Declarations (N);
3326
3327 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3328
3329 begin
3330 -- Add renamings for the protection object, discriminals, privals, and
3331 -- the entry index constant for use by debugger.
3332
3333 Debug_Private_Data_Declarations (Decls);
3334
3335 -- Perform the lock-free expansion when the subprogram references a
3336 -- protected component.
3337
3338 if Present (Comp) then
3339 Protected_Component_Ref : declare
3340 Comp_Decl : constant Node_Id := Parent (Comp);
3341 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3342 Comp_Type : constant Entity_Id := Etype (Comp);
3343
3344 Is_Procedure : constant Boolean :=
3345 Ekind (Corresponding_Spec (N)) = E_Procedure;
3346 -- Indicates if N is a protected procedure body
3347
3348 Block_Decls : List_Id;
3349 Try_Write : Entity_Id;
3350 Desired_Comp : Entity_Id;
3351 Decl : Node_Id;
3352 Label : Node_Id;
3353 Label_Id : Entity_Id := Empty;
3354 Read : Entity_Id;
3355 Expected_Comp : Entity_Id;
3356 Stmt : Node_Id;
3357 Stmts : List_Id :=
3358 New_Copy_List (Statements (Hand_Stmt_Seq));
3359 Typ_Size : Int;
3360 Unsigned : Entity_Id;
3361
3362 function Process_Node (N : Node_Id) return Traverse_Result;
3363 -- Transform a single node if it is a return statement, a raise
3364 -- statement or a reference to Comp.
3365
3366 procedure Process_Stmts (Stmts : List_Id);
3367 -- Given a statement sequence Stmts, wrap any return or raise
3368 -- statements in the following manner:
3369 --
3370 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3371 -- (_Object.Comp'Address,
3372 -- Interfaces.Unsigned_N (Expected_Comp),
3373 -- Interfaces.Unsigned_N (Desired_Comp))
3374 -- then
3375 -- <Stmt>;
3376 -- else
3377 -- goto L0;
3378 -- end if;
3379
3380 ------------------
3381 -- Process_Node --
3382 ------------------
3383
3384 function Process_Node (N : Node_Id) return Traverse_Result is
3385
3386 procedure Wrap_Statement (Stmt : Node_Id);
3387 -- Wrap an arbitrary statement inside an if statement where the
3388 -- condition does an atomic check on the state of the object.
3389
3390 --------------------
3391 -- Wrap_Statement --
3392 --------------------
3393
3394 procedure Wrap_Statement (Stmt : Node_Id) is
3395 begin
3396 -- The first time through, create the declaration of a label
3397 -- which is used to skip the remainder of source statements
3398 -- if the state of the object has changed.
3399
3400 if No (Label_Id) then
3401 Label_Id :=
3402 Make_Identifier (Loc, New_External_Name ('L', 0));
3403 Set_Entity (Label_Id,
3404 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3405 end if;
3406
3407 -- Generate:
3408 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3409 -- (_Object.Comp'Address,
3410 -- Interfaces.Unsigned_N (Expected_Comp),
3411 -- Interfaces.Unsigned_N (Desired_Comp))
3412 -- then
3413 -- <Stmt>;
3414 -- else
3415 -- goto L0;
3416 -- end if;
3417
3418 Rewrite (Stmt,
3419 Make_Implicit_If_Statement (N,
3420 Condition =>
3421 Make_Function_Call (Loc,
3422 Name =>
3423 New_Occurrence_Of (Try_Write, Loc),
3424 Parameter_Associations => New_List (
3425 Make_Attribute_Reference (Loc,
3426 Prefix => Relocate_Node (Comp_Sel_Nam),
3427 Attribute_Name => Name_Address),
3428
3429 Unchecked_Convert_To (Unsigned,
3430 New_Occurrence_Of (Expected_Comp, Loc)),
3431
3432 Unchecked_Convert_To (Unsigned,
3433 New_Occurrence_Of (Desired_Comp, Loc)))),
3434
3435 Then_Statements => New_List (Relocate_Node (Stmt)),
3436
3437 Else_Statements => New_List (
3438 Make_Goto_Statement (Loc,
3439 Name =>
3440 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3441 end Wrap_Statement;
3442
3443 -- Start of processing for Process_Node
3444
3445 begin
3446 -- Wrap each return and raise statement that appear inside a
3447 -- procedure. Skip the last return statement which is added by
3448 -- default since it is transformed into an exit statement.
3449
3450 if Is_Procedure
3451 and then ((Nkind (N) = N_Simple_Return_Statement
3452 and then N /= Last (Stmts))
3453 or else Nkind (N) = N_Extended_Return_Statement
3454 or else (Nkind_In (N, N_Raise_Constraint_Error,
3455 N_Raise_Program_Error,
3456 N_Raise_Statement,
3457 N_Raise_Storage_Error)
3458 and then Comes_From_Source (N)))
3459 then
3460 Wrap_Statement (N);
3461 return Skip;
3462 end if;
3463
3464 -- Force reanalysis
3465
3466 Set_Analyzed (N, False);
3467
3468 return OK;
3469 end Process_Node;
3470
3471 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3472
3473 -------------------
3474 -- Process_Stmts --
3475 -------------------
3476
3477 procedure Process_Stmts (Stmts : List_Id) is
3478 Stmt : Node_Id;
3479 begin
3480 Stmt := First (Stmts);
3481 while Present (Stmt) loop
3482 Process_Nodes (Stmt);
3483 Next (Stmt);
3484 end loop;
3485 end Process_Stmts;
3486
3487 -- Start of processing for Protected_Component_Ref
3488
3489 begin
3490 -- Get the type size
3491
3492 if Known_Static_Esize (Comp_Type) then
3493 Typ_Size := UI_To_Int (Esize (Comp_Type));
3494
3495 -- If the Esize (Object_Size) is unknown at compile time, look at
3496 -- the RM_Size (Value_Size) since it may have been set by an
3497 -- explicit representation clause.
3498
3499 elsif Known_Static_RM_Size (Comp_Type) then
3500 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3501
3502 -- Should not happen since this has already been checked in
3503 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3504
3505 else
3506 raise Program_Error;
3507 end if;
3508
3509 -- Retrieve all relevant atomic routines and types
3510
3511 case Typ_Size is
3512 when 8 =>
3513 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3514 Read := RTE (RE_Lock_Free_Read_8);
3515 Unsigned := RTE (RE_Uint8);
3516
3517 when 16 =>
3518 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3519 Read := RTE (RE_Lock_Free_Read_16);
3520 Unsigned := RTE (RE_Uint16);
3521
3522 when 32 =>
3523 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3524 Read := RTE (RE_Lock_Free_Read_32);
3525 Unsigned := RTE (RE_Uint32);
3526
3527 when 64 =>
3528 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3529 Read := RTE (RE_Lock_Free_Read_64);
3530 Unsigned := RTE (RE_Uint64);
3531
3532 when others =>
3533 raise Program_Error;
3534 end case;
3535
3536 -- Generate:
3537 -- Expected_Comp : constant Comp_Type :=
3538 -- Comp_Type
3539 -- (System.Atomic_Primitives.Lock_Free_Read_N
3540 -- (_Object.Comp'Address));
3541
3542 Expected_Comp :=
3543 Make_Defining_Identifier (Loc,
3544 New_External_Name (Chars (Comp), Suffix => "_saved"));
3545
3546 Decl :=
3547 Make_Object_Declaration (Loc,
3548 Defining_Identifier => Expected_Comp,
3549 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3550 Constant_Present => True,
3551 Expression =>
3552 Unchecked_Convert_To (Comp_Type,
3553 Make_Function_Call (Loc,
3554 Name => New_Occurrence_Of (Read, Loc),
3555 Parameter_Associations => New_List (
3556 Make_Attribute_Reference (Loc,
3557 Prefix => Relocate_Node (Comp_Sel_Nam),
3558 Attribute_Name => Name_Address)))));
3559
3560 -- Protected procedures
3561
3562 if Is_Procedure then
3563 -- Move the original declarations inside the generated block
3564
3565 Block_Decls := Decls;
3566
3567 -- Reset the declarations list of the protected procedure to
3568 -- contain only Decl.
3569
3570 Decls := New_List (Decl);
3571
3572 -- Generate:
3573 -- Desired_Comp : Comp_Type := Expected_Comp;
3574
3575 Desired_Comp :=
3576 Make_Defining_Identifier (Loc,
3577 New_External_Name (Chars (Comp), Suffix => "_current"));
3578
3579 -- Insert the declarations of Expected_Comp and Desired_Comp in
3580 -- the block declarations right before the renaming of the
3581 -- protected component.
3582
3583 Insert_Before (Comp_Decl,
3584 Make_Object_Declaration (Loc,
3585 Defining_Identifier => Desired_Comp,
3586 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3587 Expression =>
3588 New_Occurrence_Of (Expected_Comp, Loc)));
3589
3590 -- Protected function
3591
3592 else
3593 Desired_Comp := Expected_Comp;
3594
3595 -- Insert the declaration of Expected_Comp in the function
3596 -- declarations right before the renaming of the protected
3597 -- component.
3598
3599 Insert_Before (Comp_Decl, Decl);
3600 end if;
3601
3602 -- Rewrite the protected component renaming declaration to be a
3603 -- renaming of Desired_Comp.
3604
3605 -- Generate:
3606 -- Comp : Comp_Type renames Desired_Comp;
3607
3608 Rewrite (Comp_Decl,
3609 Make_Object_Renaming_Declaration (Loc,
3610 Defining_Identifier =>
3611 Defining_Identifier (Comp_Decl),
3612 Subtype_Mark =>
3613 New_Occurrence_Of (Comp_Type, Loc),
3614 Name =>
3615 New_Occurrence_Of (Desired_Comp, Loc)));
3616
3617 -- Wrap any return or raise statements in Stmts in same the manner
3618 -- described in Process_Stmts.
3619
3620 Process_Stmts (Stmts);
3621
3622 -- Generate:
3623 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3624 -- (_Object.Comp'Address,
3625 -- Interfaces.Unsigned_N (Expected_Comp),
3626 -- Interfaces.Unsigned_N (Desired_Comp))
3627
3628 if Is_Procedure then
3629 Stmt :=
3630 Make_Exit_Statement (Loc,
3631 Condition =>
3632 Make_Function_Call (Loc,
3633 Name =>
3634 New_Occurrence_Of (Try_Write, Loc),
3635 Parameter_Associations => New_List (
3636 Make_Attribute_Reference (Loc,
3637 Prefix => Relocate_Node (Comp_Sel_Nam),
3638 Attribute_Name => Name_Address),
3639
3640 Unchecked_Convert_To (Unsigned,
3641 New_Occurrence_Of (Expected_Comp, Loc)),
3642
3643 Unchecked_Convert_To (Unsigned,
3644 New_Occurrence_Of (Desired_Comp, Loc)))));
3645
3646 -- Small optimization: transform the default return statement
3647 -- of a procedure into the atomic exit statement.
3648
3649 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3650 Rewrite (Last (Stmts), Stmt);
3651 else
3652 Append_To (Stmts, Stmt);
3653 end if;
3654 end if;
3655
3656 -- Create the declaration of the label used to skip the rest of
3657 -- the source statements when the object state changes.
3658
3659 if Present (Label_Id) then
3660 Label := Make_Label (Loc, Label_Id);
3661 Append_To (Decls,
3662 Make_Implicit_Label_Declaration (Loc,
3663 Defining_Identifier => Entity (Label_Id),
3664 Label_Construct => Label));
3665 Append_To (Stmts, Label);
3666 end if;
3667
3668 -- Generate:
3669 -- loop
3670 -- declare
3671 -- <Decls>
3672 -- begin
3673 -- <Stmts>
3674 -- end;
3675 -- end loop;
3676
3677 if Is_Procedure then
3678 Stmts :=
3679 New_List (
3680 Make_Loop_Statement (Loc,
3681 Statements => New_List (
3682 Make_Block_Statement (Loc,
3683 Declarations => Block_Decls,
3684 Handled_Statement_Sequence =>
3685 Make_Handled_Sequence_Of_Statements (Loc,
3686 Statements => Stmts))),
3687 End_Label => Empty));
3688 end if;
3689
3690 Hand_Stmt_Seq :=
3691 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3692 end Protected_Component_Ref;
3693 end if;
3694
3695 -- Make an unprotected version of the subprogram for use within the same
3696 -- object, with new name and extra parameter representing the object.
3697
3698 return
3699 Make_Subprogram_Body (Loc,
3700 Specification =>
3701 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3702 Declarations => Decls,
3703 Handled_Statement_Sequence => Hand_Stmt_Seq);
3704 end Build_Lock_Free_Unprotected_Subprogram_Body;
3705
3706 -------------------------
3707 -- Build_Master_Entity --
3708 -------------------------
3709
3710 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3711 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3712 Context : Node_Id;
3713 Context_Id : Entity_Id;
3714 Decl : Node_Id;
3715 Decls : List_Id;
3716 Par : Node_Id;
3717
3718 begin
3719 if Is_Itype (Obj_Or_Typ) then
3720 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3721 else
3722 Par := Parent (Obj_Or_Typ);
3723 end if;
3724
3725 -- When creating a master for a record component which is either a task
3726 -- or access-to-task, the enclosing record is the master scope and the
3727 -- proper insertion point is the component list.
3728
3729 if Is_Record_Type (Current_Scope) then
3730 Context := Par;
3731 Context_Id := Current_Scope;
3732 Decls := List_Containing (Context);
3733
3734 -- Default case for object declarations and access types. Note that the
3735 -- context is updated to the nearest enclosing body, block, package, or
3736 -- return statement.
3737
3738 else
3739 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3740 end if;
3741
3742 -- Do not create a master if one already exists or there is no task
3743 -- hierarchy.
3744
3745 if Has_Master_Entity (Context_Id)
3746 or else Restriction_Active (No_Task_Hierarchy)
3747 then
3748 return;
3749 end if;
3750
3751 -- Create a master, generate:
3752 -- _Master : constant Master_Id := Current_Master.all;
3753
3754 Decl :=
3755 Make_Object_Declaration (Loc,
3756 Defining_Identifier =>
3757 Make_Defining_Identifier (Loc, Name_uMaster),
3758 Constant_Present => True,
3759 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3760 Expression =>
3761 Make_Explicit_Dereference (Loc,
3762 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3763
3764 -- The master is inserted at the start of the declarative list of the
3765 -- context.
3766
3767 Prepend_To (Decls, Decl);
3768
3769 -- In certain cases where transient scopes are involved, the immediate
3770 -- scope is not always the proper master scope. Ensure that the master
3771 -- declaration and entity appear in the same context.
3772
3773 if Context_Id /= Current_Scope then
3774 Push_Scope (Context_Id);
3775 Analyze (Decl);
3776 Pop_Scope;
3777 else
3778 Analyze (Decl);
3779 end if;
3780
3781 -- Mark the enclosing scope and its associated construct as being task
3782 -- masters.
3783
3784 Set_Has_Master_Entity (Context_Id);
3785
3786 while Present (Context)
3787 and then Nkind (Context) /= N_Compilation_Unit
3788 loop
3789 if Nkind_In (Context, N_Block_Statement,
3790 N_Subprogram_Body,
3791 N_Task_Body)
3792 then
3793 Set_Is_Task_Master (Context);
3794 exit;
3795
3796 elsif Nkind (Parent (Context)) = N_Subunit then
3797 Context := Corresponding_Stub (Parent (Context));
3798 end if;
3799
3800 Context := Parent (Context);
3801 end loop;
3802 end Build_Master_Entity;
3803
3804 ---------------------------
3805 -- Build_Master_Renaming --
3806 ---------------------------
3807
3808 procedure Build_Master_Renaming
3809 (Ptr_Typ : Entity_Id;
3810 Ins_Nod : Node_Id := Empty)
3811 is
3812 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3813 Context : Node_Id;
3814 Master_Decl : Node_Id;
3815 Master_Id : Entity_Id;
3816
3817 begin
3818 -- Nothing to do if there is no task hierarchy
3819
3820 if Restriction_Active (No_Task_Hierarchy) then
3821 return;
3822 end if;
3823
3824 -- Determine the proper context to insert the master renaming
3825
3826 if Present (Ins_Nod) then
3827 Context := Ins_Nod;
3828 elsif Is_Itype (Ptr_Typ) then
3829 Context := Associated_Node_For_Itype (Ptr_Typ);
3830 else
3831 Context := Parent (Ptr_Typ);
3832 end if;
3833
3834 -- Generate:
3835 -- <Ptr_Typ>M : Master_Id renames _Master;
3836
3837 Master_Id :=
3838 Make_Defining_Identifier (Loc,
3839 New_External_Name (Chars (Ptr_Typ), 'M'));
3840
3841 Master_Decl :=
3842 Make_Object_Renaming_Declaration (Loc,
3843 Defining_Identifier => Master_Id,
3844 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3845 Name => Make_Identifier (Loc, Name_uMaster));
3846
3847 Insert_Action (Context, Master_Decl);
3848
3849 -- The renamed master now services the access type
3850
3851 Set_Master_Id (Ptr_Typ, Master_Id);
3852 end Build_Master_Renaming;
3853
3854 -----------------------------------------
3855 -- Build_Private_Protected_Declaration --
3856 -----------------------------------------
3857
3858 function Build_Private_Protected_Declaration
3859 (N : Node_Id) return Entity_Id
3860 is
3861 Loc : constant Source_Ptr := Sloc (N);
3862 Body_Id : constant Entity_Id := Defining_Entity (N);
3863 Decl : Node_Id;
3864 Plist : List_Id;
3865 Formal : Entity_Id;
3866 New_Spec : Node_Id;
3867 Spec_Id : Entity_Id;
3868
3869 begin
3870 Formal := First_Formal (Body_Id);
3871
3872 -- The protected operation always has at least one formal, namely the
3873 -- object itself, but it is only placed in the parameter list if
3874 -- expansion is enabled.
3875
3876 if Present (Formal) or else Expander_Active then
3877 Plist := Copy_Parameter_List (Body_Id);
3878 else
3879 Plist := No_List;
3880 end if;
3881
3882 if Nkind (Specification (N)) = N_Procedure_Specification then
3883 New_Spec :=
3884 Make_Procedure_Specification (Loc,
3885 Defining_Unit_Name =>
3886 Make_Defining_Identifier (Sloc (Body_Id),
3887 Chars => Chars (Body_Id)),
3888 Parameter_Specifications =>
3889 Plist);
3890 else
3891 New_Spec :=
3892 Make_Function_Specification (Loc,
3893 Defining_Unit_Name =>
3894 Make_Defining_Identifier (Sloc (Body_Id),
3895 Chars => Chars (Body_Id)),
3896 Parameter_Specifications => Plist,
3897 Result_Definition =>
3898 New_Occurrence_Of (Etype (Body_Id), Loc));
3899 end if;
3900
3901 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3902 Insert_Before (N, Decl);
3903 Spec_Id := Defining_Unit_Name (New_Spec);
3904
3905 -- Indicate that the entity comes from source, to ensure that cross-
3906 -- reference information is properly generated. The body itself is
3907 -- rewritten during expansion, and the body entity will not appear in
3908 -- calls to the operation.
3909
3910 Set_Comes_From_Source (Spec_Id, True);
3911 Analyze (Decl);
3912 Set_Has_Completion (Spec_Id);
3913 Set_Convention (Spec_Id, Convention_Protected);
3914 return Spec_Id;
3915 end Build_Private_Protected_Declaration;
3916
3917 ---------------------------
3918 -- Build_Protected_Entry --
3919 ---------------------------
3920
3921 function Build_Protected_Entry
3922 (N : Node_Id;
3923 Ent : Entity_Id;
3924 Pid : Node_Id) return Node_Id
3925 is
3926 Bod_Decls : constant List_Id := New_List;
3927 Decls : constant List_Id := Declarations (N);
3928 End_Lab : constant Node_Id :=
3929 End_Label (Handled_Statement_Sequence (N));
3930 End_Loc : constant Source_Ptr :=
3931 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3932 -- Used for the generated call to Complete_Entry_Body
3933
3934 Loc : constant Source_Ptr := Sloc (N);
3935
3936 Bod_Id : Entity_Id;
3937 Bod_Spec : Node_Id;
3938 Bod_Stmts : List_Id;
3939 Complete : Node_Id;
3940 Ohandle : Node_Id;
3941
3942 EH_Loc : Source_Ptr;
3943 -- Used for the exception handler, inserted at end of the body
3944
3945 begin
3946 -- Set the source location on the exception handler only when debugging
3947 -- the expanded code (see Make_Implicit_Exception_Handler).
3948
3949 if Debug_Generated_Code then
3950 EH_Loc := End_Loc;
3951
3952 -- Otherwise the inserted code should not be visible to the debugger
3953
3954 else
3955 EH_Loc := No_Location;
3956 end if;
3957
3958 Bod_Id :=
3959 Make_Defining_Identifier (Loc,
3960 Chars => Chars (Protected_Body_Subprogram (Ent)));
3961 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3962
3963 -- Add the following declarations:
3964
3965 -- type poVP is access poV;
3966 -- _object : poVP := poVP (_O);
3967
3968 -- where _O is the formal parameter associated with the concurrent
3969 -- object. These declarations are needed for Complete_Entry_Body.
3970
3971 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3972
3973 -- Add renamings for all formals, the Protection object, discriminals,
3974 -- privals and the entry index constant for use by debugger.
3975
3976 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3977 Debug_Private_Data_Declarations (Decls);
3978
3979 -- Put the declarations and the statements from the entry
3980
3981 Bod_Stmts :=
3982 New_List (
3983 Make_Block_Statement (Loc,
3984 Declarations => Decls,
3985 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3986
3987 case Corresponding_Runtime_Package (Pid) is
3988 when System_Tasking_Protected_Objects_Entries =>
3989 Append_To (Bod_Stmts,
3990 Make_Procedure_Call_Statement (End_Loc,
3991 Name =>
3992 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3993 Parameter_Associations => New_List (
3994 Make_Attribute_Reference (End_Loc,
3995 Prefix =>
3996 Make_Selected_Component (End_Loc,
3997 Prefix =>
3998 Make_Identifier (End_Loc, Name_uObject),
3999 Selector_Name =>
4000 Make_Identifier (End_Loc, Name_uObject)),
4001 Attribute_Name => Name_Unchecked_Access))));
4002
4003 when System_Tasking_Protected_Objects_Single_Entry =>
4004
4005 -- Historically, a call to Complete_Single_Entry_Body was
4006 -- inserted, but it was a null procedure.
4007
4008 null;
4009
4010 when others =>
4011 raise Program_Error;
4012 end case;
4013
4014 -- When exceptions can not be propagated, we never need to call
4015 -- Exception_Complete_Entry_Body.
4016
4017 if No_Exception_Handlers_Set then
4018 return
4019 Make_Subprogram_Body (Loc,
4020 Specification => Bod_Spec,
4021 Declarations => Bod_Decls,
4022 Handled_Statement_Sequence =>
4023 Make_Handled_Sequence_Of_Statements (Loc,
4024 Statements => Bod_Stmts,
4025 End_Label => End_Lab));
4026
4027 else
4028 Ohandle := Make_Others_Choice (Loc);
4029 Set_All_Others (Ohandle);
4030
4031 case Corresponding_Runtime_Package (Pid) is
4032 when System_Tasking_Protected_Objects_Entries =>
4033 Complete :=
4034 New_Occurrence_Of
4035 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
4036
4037 when System_Tasking_Protected_Objects_Single_Entry =>
4038 Complete :=
4039 New_Occurrence_Of
4040 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
4041
4042 when others =>
4043 raise Program_Error;
4044 end case;
4045
4046 -- Establish link between subprogram body entity and source entry
4047
4048 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
4049
4050 -- Create body of entry procedure. The renaming declarations are
4051 -- placed ahead of the block that contains the actual entry body.
4052
4053 return
4054 Make_Subprogram_Body (Loc,
4055 Specification => Bod_Spec,
4056 Declarations => Bod_Decls,
4057 Handled_Statement_Sequence =>
4058 Make_Handled_Sequence_Of_Statements (Loc,
4059 Statements => Bod_Stmts,
4060 End_Label => End_Lab,
4061 Exception_Handlers => New_List (
4062 Make_Implicit_Exception_Handler (EH_Loc,
4063 Exception_Choices => New_List (Ohandle),
4064
4065 Statements => New_List (
4066 Make_Procedure_Call_Statement (EH_Loc,
4067 Name => Complete,
4068 Parameter_Associations => New_List (
4069 Make_Attribute_Reference (EH_Loc,
4070 Prefix =>
4071 Make_Selected_Component (EH_Loc,
4072 Prefix =>
4073 Make_Identifier (EH_Loc, Name_uObject),
4074 Selector_Name =>
4075 Make_Identifier (EH_Loc, Name_uObject)),
4076 Attribute_Name => Name_Unchecked_Access),
4077
4078 Make_Function_Call (EH_Loc,
4079 Name =>
4080 New_Occurrence_Of
4081 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
4082 end if;
4083 end Build_Protected_Entry;
4084
4085 -----------------------------------------
4086 -- Build_Protected_Entry_Specification --
4087 -----------------------------------------
4088
4089 function Build_Protected_Entry_Specification
4090 (Loc : Source_Ptr;
4091 Def_Id : Entity_Id;
4092 Ent_Id : Entity_Id) return Node_Id
4093 is
4094 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
4095
4096 begin
4097 Set_Debug_Info_Needed (Def_Id);
4098
4099 if Present (Ent_Id) then
4100 Append_Elmt (P, Accept_Address (Ent_Id));
4101 end if;
4102
4103 return
4104 Make_Procedure_Specification (Loc,
4105 Defining_Unit_Name => Def_Id,
4106 Parameter_Specifications => New_List (
4107 Make_Parameter_Specification (Loc,
4108 Defining_Identifier =>
4109 Make_Defining_Identifier (Loc, Name_uO),
4110 Parameter_Type =>
4111 New_Occurrence_Of (RTE (RE_Address), Loc)),
4112
4113 Make_Parameter_Specification (Loc,
4114 Defining_Identifier => P,
4115 Parameter_Type =>
4116 New_Occurrence_Of (RTE (RE_Address), Loc)),
4117
4118 Make_Parameter_Specification (Loc,
4119 Defining_Identifier =>
4120 Make_Defining_Identifier (Loc, Name_uE),
4121 Parameter_Type =>
4122 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
4123 end Build_Protected_Entry_Specification;
4124
4125 --------------------------
4126 -- Build_Protected_Spec --
4127 --------------------------
4128
4129 function Build_Protected_Spec
4130 (N : Node_Id;
4131 Obj_Type : Entity_Id;
4132 Ident : Entity_Id;
4133 Unprotected : Boolean := False) return List_Id
4134 is
4135 Loc : constant Source_Ptr := Sloc (N);
4136 Decl : Node_Id;
4137 Formal : Entity_Id;
4138 New_Plist : List_Id;
4139 New_Param : Node_Id;
4140
4141 begin
4142 New_Plist := New_List;
4143
4144 Formal := First_Formal (Ident);
4145 while Present (Formal) loop
4146 New_Param :=
4147 Make_Parameter_Specification (Loc,
4148 Defining_Identifier =>
4149 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4150 Aliased_Present => Aliased_Present (Parent (Formal)),
4151 In_Present => In_Present (Parent (Formal)),
4152 Out_Present => Out_Present (Parent (Formal)),
4153 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
4154
4155 if Unprotected then
4156 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4157 end if;
4158
4159 Append (New_Param, New_Plist);
4160 Next_Formal (Formal);
4161 end loop;
4162
4163 -- If the subprogram is a procedure and the context is not an access
4164 -- to protected subprogram, the parameter is in-out. Otherwise it is
4165 -- an in parameter.
4166
4167 Decl :=
4168 Make_Parameter_Specification (Loc,
4169 Defining_Identifier =>
4170 Make_Defining_Identifier (Loc, Name_uObject),
4171 In_Present => True,
4172 Out_Present =>
4173 (Etype (Ident) = Standard_Void_Type
4174 and then not Is_RTE (Obj_Type, RE_Address)),
4175 Parameter_Type =>
4176 New_Occurrence_Of (Obj_Type, Loc));
4177 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4178 Prepend_To (New_Plist, Decl);
4179
4180 return New_Plist;
4181 end Build_Protected_Spec;
4182
4183 ---------------------------------------
4184 -- Build_Protected_Sub_Specification --
4185 ---------------------------------------
4186
4187 function Build_Protected_Sub_Specification
4188 (N : Node_Id;
4189 Prot_Typ : Entity_Id;
4190 Mode : Subprogram_Protection_Mode) return Node_Id
4191 is
4192 Loc : constant Source_Ptr := Sloc (N);
4193 Decl : Node_Id;
4194 Def_Id : Entity_Id;
4195 New_Id : Entity_Id;
4196 New_Plist : List_Id;
4197 New_Spec : Node_Id;
4198
4199 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4200 (Dispatching_Mode => ' ',
4201 Protected_Mode => 'P',
4202 Unprotected_Mode => 'N');
4203
4204 begin
4205 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
4206 then
4207 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4208 else
4209 Decl := N;
4210 end if;
4211
4212 Def_Id := Defining_Unit_Name (Specification (Decl));
4213
4214 New_Plist :=
4215 Build_Protected_Spec
4216 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4217 Mode = Unprotected_Mode);
4218 New_Id :=
4219 Make_Defining_Identifier (Loc,
4220 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4221
4222 -- Reference the original nondispatching subprogram since the analysis
4223 -- of the object.operation notation may need its original name (see
4224 -- Sem_Ch4.Names_Match).
4225
4226 if Mode = Dispatching_Mode then
4227 Set_Ekind (New_Id, Ekind (Def_Id));
4228 Set_Original_Protected_Subprogram (New_Id, Def_Id);
4229 end if;
4230
4231 -- The unprotected operation carries the user code, and debugging
4232 -- information must be generated for it, even though this spec does
4233 -- not come from source. It is also convenient to allow gdb to step
4234 -- into the protected operation, even though it only contains lock/
4235 -- unlock calls.
4236
4237 Set_Debug_Info_Needed (New_Id);
4238
4239 -- If a pragma Eliminate applies to the source entity, the internal
4240 -- subprograms will be eliminated as well.
4241
4242 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4243
4244 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4245 New_Spec :=
4246 Make_Procedure_Specification (Loc,
4247 Defining_Unit_Name => New_Id,
4248 Parameter_Specifications => New_Plist);
4249
4250 -- Create a new specification for the anonymous subprogram type
4251
4252 else
4253 New_Spec :=
4254 Make_Function_Specification (Loc,
4255 Defining_Unit_Name => New_Id,
4256 Parameter_Specifications => New_Plist,
4257 Result_Definition =>
4258 Copy_Result_Type (Result_Definition (Specification (Decl))));
4259
4260 Set_Return_Present (Defining_Unit_Name (New_Spec));
4261 end if;
4262
4263 return New_Spec;
4264 end Build_Protected_Sub_Specification;
4265
4266 -------------------------------------
4267 -- Build_Protected_Subprogram_Body --
4268 -------------------------------------
4269
4270 function Build_Protected_Subprogram_Body
4271 (N : Node_Id;
4272 Pid : Node_Id;
4273 N_Op_Spec : Node_Id) return Node_Id
4274 is
4275 Loc : constant Source_Ptr := Sloc (N);
4276 Op_Spec : Node_Id;
4277 P_Op_Spec : Node_Id;
4278 Uactuals : List_Id;
4279 Pformal : Node_Id;
4280 Unprot_Call : Node_Id;
4281 Sub_Body : Node_Id;
4282 Lock_Name : Node_Id;
4283 Lock_Stmt : Node_Id;
4284 R : Node_Id;
4285 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4286 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4287 Stmts : List_Id;
4288 Object_Parm : Node_Id;
4289 Exc_Safe : Boolean;
4290 Lock_Kind : RE_Id;
4291
4292 begin
4293 Op_Spec := Specification (N);
4294 Exc_Safe := Is_Exception_Safe (N);
4295
4296 P_Op_Spec :=
4297 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4298
4299 -- Build a list of the formal parameters of the protected version of
4300 -- the subprogram to use as the actual parameters of the unprotected
4301 -- version.
4302
4303 Uactuals := New_List;
4304 Pformal := First (Parameter_Specifications (P_Op_Spec));
4305 while Present (Pformal) loop
4306 Append_To (Uactuals,
4307 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4308 Next (Pformal);
4309 end loop;
4310
4311 -- Make a call to the unprotected version of the subprogram built above
4312 -- for use by the protected version built below.
4313
4314 if Nkind (Op_Spec) = N_Function_Specification then
4315 if Exc_Safe then
4316 R := Make_Temporary (Loc, 'R');
4317
4318 Unprot_Call :=
4319 Make_Object_Declaration (Loc,
4320 Defining_Identifier => R,
4321 Constant_Present => True,
4322 Object_Definition =>
4323 New_Copy (Result_Definition (N_Op_Spec)),
4324 Expression =>
4325 Make_Function_Call (Loc,
4326 Name =>
4327 Make_Identifier (Loc,
4328 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4329 Parameter_Associations => Uactuals));
4330
4331 Return_Stmt :=
4332 Make_Simple_Return_Statement (Loc,
4333 Expression => New_Occurrence_Of (R, Loc));
4334
4335 else
4336 Unprot_Call :=
4337 Make_Simple_Return_Statement (Loc,
4338 Expression =>
4339 Make_Function_Call (Loc,
4340 Name =>
4341 Make_Identifier (Loc,
4342 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4343 Parameter_Associations => Uactuals));
4344 end if;
4345
4346 Lock_Kind := RE_Lock_Read_Only;
4347
4348 else
4349 Unprot_Call :=
4350 Make_Procedure_Call_Statement (Loc,
4351 Name =>
4352 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4353 Parameter_Associations => Uactuals);
4354
4355 Lock_Kind := RE_Lock;
4356 end if;
4357
4358 -- Wrap call in block that will be covered by an at_end handler
4359
4360 if not Exc_Safe then
4361 Unprot_Call :=
4362 Make_Block_Statement (Loc,
4363 Handled_Statement_Sequence =>
4364 Make_Handled_Sequence_Of_Statements (Loc,
4365 Statements => New_List (Unprot_Call)));
4366 end if;
4367
4368 -- Make the protected subprogram body. This locks the protected
4369 -- object and calls the unprotected version of the subprogram.
4370
4371 case Corresponding_Runtime_Package (Pid) is
4372 when System_Tasking_Protected_Objects_Entries =>
4373 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
4374
4375 when System_Tasking_Protected_Objects_Single_Entry =>
4376 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4377
4378 when System_Tasking_Protected_Objects =>
4379 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4380
4381 when others =>
4382 raise Program_Error;
4383 end case;
4384
4385 Object_Parm :=
4386 Make_Attribute_Reference (Loc,
4387 Prefix =>
4388 Make_Selected_Component (Loc,
4389 Prefix => Make_Identifier (Loc, Name_uObject),
4390 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4391 Attribute_Name => Name_Unchecked_Access);
4392
4393 Lock_Stmt :=
4394 Make_Procedure_Call_Statement (Loc,
4395 Name => Lock_Name,
4396 Parameter_Associations => New_List (Object_Parm));
4397
4398 if Abort_Allowed then
4399 Stmts := New_List (
4400 Build_Runtime_Call (Loc, RE_Abort_Defer),
4401 Lock_Stmt);
4402
4403 else
4404 Stmts := New_List (Lock_Stmt);
4405 end if;
4406
4407 if not Exc_Safe then
4408 Append (Unprot_Call, Stmts);
4409 else
4410 if Nkind (Op_Spec) = N_Function_Specification then
4411 Pre_Stmts := Stmts;
4412 Stmts := Empty_List;
4413 else
4414 Append (Unprot_Call, Stmts);
4415 end if;
4416
4417 -- Historical note: Previously, call to the cleanup was inserted
4418 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4419 -- which is also shared by the 'not Exc_Safe' path.
4420
4421 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4422
4423 if Nkind (Op_Spec) = N_Function_Specification then
4424 Append_To (Stmts, Return_Stmt);
4425 Append_To (Pre_Stmts,
4426 Make_Block_Statement (Loc,
4427 Declarations => New_List (Unprot_Call),
4428 Handled_Statement_Sequence =>
4429 Make_Handled_Sequence_Of_Statements (Loc,
4430 Statements => Stmts)));
4431 Stmts := Pre_Stmts;
4432 end if;
4433 end if;
4434
4435 Sub_Body :=
4436 Make_Subprogram_Body (Loc,
4437 Declarations => Empty_List,
4438 Specification => P_Op_Spec,
4439 Handled_Statement_Sequence =>
4440 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4441
4442 -- Mark this subprogram as a protected subprogram body so that the
4443 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4444 -- path as otherwise the cleanup has already been inserted.
4445
4446 if not Exc_Safe then
4447 Set_Is_Protected_Subprogram_Body (Sub_Body);
4448 end if;
4449
4450 return Sub_Body;
4451 end Build_Protected_Subprogram_Body;
4452
4453 -------------------------------------
4454 -- Build_Protected_Subprogram_Call --
4455 -------------------------------------
4456
4457 procedure Build_Protected_Subprogram_Call
4458 (N : Node_Id;
4459 Name : Node_Id;
4460 Rec : Node_Id;
4461 External : Boolean := True)
4462 is
4463 Loc : constant Source_Ptr := Sloc (N);
4464 Sub : constant Entity_Id := Entity (Name);
4465 New_Sub : Node_Id;
4466 Params : List_Id;
4467
4468 begin
4469 if External then
4470 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4471 else
4472 New_Sub :=
4473 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4474 end if;
4475
4476 if Present (Parameter_Associations (N)) then
4477 Params := New_Copy_List_Tree (Parameter_Associations (N));
4478 else
4479 Params := New_List;
4480 end if;
4481
4482 -- If the type is an untagged derived type, convert to the root type,
4483 -- which is the one on which the operations are defined.
4484
4485 if Nkind (Rec) = N_Unchecked_Type_Conversion
4486 and then not Is_Tagged_Type (Etype (Rec))
4487 and then Is_Derived_Type (Etype (Rec))
4488 then
4489 Set_Etype (Rec, Root_Type (Etype (Rec)));
4490 Set_Subtype_Mark (Rec,
4491 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4492 end if;
4493
4494 Prepend (Rec, Params);
4495
4496 if Ekind (Sub) = E_Procedure then
4497 Rewrite (N,
4498 Make_Procedure_Call_Statement (Loc,
4499 Name => New_Sub,
4500 Parameter_Associations => Params));
4501
4502 else
4503 pragma Assert (Ekind (Sub) = E_Function);
4504 Rewrite (N,
4505 Make_Function_Call (Loc,
4506 Name => New_Sub,
4507 Parameter_Associations => Params));
4508
4509 -- Preserve type of call for subsequent processing (required for
4510 -- call to Wrap_Transient_Expression in the case of a shared passive
4511 -- protected).
4512
4513 Set_Etype (N, Etype (New_Sub));
4514 end if;
4515
4516 if External
4517 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4518 and then Is_Entity_Name (Expression (Rec))
4519 and then Is_Shared_Passive (Entity (Expression (Rec)))
4520 then
4521 Add_Shared_Var_Lock_Procs (N);
4522 end if;
4523 end Build_Protected_Subprogram_Call;
4524
4525 ---------------------------------------------
4526 -- Build_Protected_Subprogram_Call_Cleanup --
4527 ---------------------------------------------
4528
4529 procedure Build_Protected_Subprogram_Call_Cleanup
4530 (Op_Spec : Node_Id;
4531 Conc_Typ : Node_Id;
4532 Loc : Source_Ptr;
4533 Stmts : List_Id)
4534 is
4535 Nam : Node_Id;
4536
4537 begin
4538 -- If the associated protected object has entries, a protected
4539 -- procedure has to service entry queues. In this case generate:
4540
4541 -- Service_Entries (_object._object'Access);
4542
4543 if Nkind (Op_Spec) = N_Procedure_Specification
4544 and then Has_Entries (Conc_Typ)
4545 then
4546 case Corresponding_Runtime_Package (Conc_Typ) is
4547 when System_Tasking_Protected_Objects_Entries =>
4548 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4549
4550 when System_Tasking_Protected_Objects_Single_Entry =>
4551 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4552
4553 when others =>
4554 raise Program_Error;
4555 end case;
4556
4557 Append_To (Stmts,
4558 Make_Procedure_Call_Statement (Loc,
4559 Name => Nam,
4560 Parameter_Associations => New_List (
4561 Make_Attribute_Reference (Loc,
4562 Prefix =>
4563 Make_Selected_Component (Loc,
4564 Prefix => Make_Identifier (Loc, Name_uObject),
4565 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4566 Attribute_Name => Name_Unchecked_Access))));
4567
4568 else
4569 -- Generate:
4570 -- Unlock (_object._object'Access);
4571
4572 case Corresponding_Runtime_Package (Conc_Typ) is
4573 when System_Tasking_Protected_Objects_Entries =>
4574 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4575
4576 when System_Tasking_Protected_Objects_Single_Entry =>
4577 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4578
4579 when System_Tasking_Protected_Objects =>
4580 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4581
4582 when others =>
4583 raise Program_Error;
4584 end case;
4585
4586 Append_To (Stmts,
4587 Make_Procedure_Call_Statement (Loc,
4588 Name => Nam,
4589 Parameter_Associations => New_List (
4590 Make_Attribute_Reference (Loc,
4591 Prefix =>
4592 Make_Selected_Component (Loc,
4593 Prefix => Make_Identifier (Loc, Name_uObject),
4594 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4595 Attribute_Name => Name_Unchecked_Access))));
4596 end if;
4597
4598 -- Generate:
4599 -- Abort_Undefer;
4600
4601 if Abort_Allowed then
4602 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4603 end if;
4604 end Build_Protected_Subprogram_Call_Cleanup;
4605
4606 -------------------------
4607 -- Build_Selected_Name --
4608 -------------------------
4609
4610 function Build_Selected_Name
4611 (Prefix : Entity_Id;
4612 Selector : Entity_Id;
4613 Append_Char : Character := ' ') return Name_Id
4614 is
4615 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4616 Select_Len : Natural;
4617
4618 begin
4619 Get_Name_String (Chars (Selector));
4620 Select_Len := Name_Len;
4621 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4622 Get_Name_String (Chars (Prefix));
4623
4624 -- If scope is anonymous type, discard suffix to recover name of
4625 -- single protected object. Otherwise use protected type name.
4626
4627 if Name_Buffer (Name_Len) = 'T' then
4628 Name_Len := Name_Len - 1;
4629 end if;
4630
4631 Add_Str_To_Name_Buffer ("__");
4632 for J in 1 .. Select_Len loop
4633 Add_Char_To_Name_Buffer (Select_Buffer (J));
4634 end loop;
4635
4636 -- Now add the Append_Char if specified. The encoding to follow
4637 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4638 -- then the entity is associated to a protected type subprogram.
4639 -- Otherwise, it is a protected type entry. For each case, the
4640 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4641
4642 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4643
4644 if Append_Char /= ' ' then
4645 if Append_Char = 'P' or Append_Char = 'N' then
4646 Add_Char_To_Name_Buffer (Append_Char);
4647 return Name_Find;
4648 else
4649 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4650 return New_External_Name (Name_Find, ' ', -1);
4651 end if;
4652 else
4653 return Name_Find;
4654 end if;
4655 end Build_Selected_Name;
4656
4657 -----------------------------
4658 -- Build_Simple_Entry_Call --
4659 -----------------------------
4660
4661 -- A task entry call is converted to a call to Call_Simple
4662
4663 -- declare
4664 -- P : parms := (parm, parm, parm);
4665 -- begin
4666 -- Call_Simple (acceptor-task, entry-index, P'Address);
4667 -- parm := P.param;
4668 -- parm := P.param;
4669 -- ...
4670 -- end;
4671
4672 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4673 -- the parameters, and the constructed aggregate value contains either the
4674 -- parameters or, in the case of non-elementary types, references to these
4675 -- parameters. Then the address of this aggregate is passed to the runtime
4676 -- routine, along with the task id value and the task entry index value.
4677 -- Pnn is only required if parameters are present.
4678
4679 -- The assignments after the call are present only in the case of in-out
4680 -- or out parameters for elementary types, and are used to assign back the
4681 -- resulting values of such parameters.
4682
4683 -- Note: the reason that we insert a block here is that in the context
4684 -- of selects, conditional entry calls etc. the entry call statement
4685 -- appears on its own, not as an element of a list.
4686
4687 -- A protected entry call is converted to a Protected_Entry_Call:
4688
4689 -- declare
4690 -- P : E1_Params := (param, param, param);
4691 -- Pnn : Boolean;
4692 -- Bnn : Communications_Block;
4693
4694 -- declare
4695 -- P : E1_Params := (param, param, param);
4696 -- Bnn : Communications_Block;
4697
4698 -- begin
4699 -- Protected_Entry_Call (
4700 -- Object => po._object'Access,
4701 -- E => <entry index>;
4702 -- Uninterpreted_Data => P'Address;
4703 -- Mode => Simple_Call;
4704 -- Block => Bnn);
4705 -- parm := P.param;
4706 -- parm := P.param;
4707 -- ...
4708 -- end;
4709
4710 procedure Build_Simple_Entry_Call
4711 (N : Node_Id;
4712 Concval : Node_Id;
4713 Ename : Node_Id;
4714 Index : Node_Id)
4715 is
4716 begin
4717 Expand_Call (N);
4718
4719 -- If call has been inlined, nothing left to do
4720
4721 if Nkind (N) = N_Block_Statement then
4722 return;
4723 end if;
4724
4725 -- Convert entry call to Call_Simple call
4726
4727 declare
4728 Loc : constant Source_Ptr := Sloc (N);
4729 Parms : constant List_Id := Parameter_Associations (N);
4730 Stats : constant List_Id := New_List;
4731 Actual : Node_Id;
4732 Call : Node_Id;
4733 Comm_Name : Entity_Id;
4734 Conctyp : Node_Id;
4735 Decls : List_Id;
4736 Ent : Entity_Id;
4737 Ent_Acc : Entity_Id;
4738 Formal : Node_Id;
4739 Iface_Tag : Entity_Id;
4740 Iface_Typ : Entity_Id;
4741 N_Node : Node_Id;
4742 N_Var : Node_Id;
4743 P : Entity_Id;
4744 Parm1 : Node_Id;
4745 Parm2 : Node_Id;
4746 Parm3 : Node_Id;
4747 Pdecl : Node_Id;
4748 Plist : List_Id;
4749 X : Entity_Id;
4750 Xdecl : Node_Id;
4751
4752 begin
4753 -- Simple entry and entry family cases merge here
4754
4755 Ent := Entity (Ename);
4756 Ent_Acc := Entry_Parameters_Type (Ent);
4757 Conctyp := Etype (Concval);
4758
4759 -- If prefix is an access type, dereference to obtain the task type
4760
4761 if Is_Access_Type (Conctyp) then
4762 Conctyp := Designated_Type (Conctyp);
4763 end if;
4764
4765 -- Special case for protected subprogram calls
4766
4767 if Is_Protected_Type (Conctyp)
4768 and then Is_Subprogram (Entity (Ename))
4769 then
4770 if not Is_Eliminated (Entity (Ename)) then
4771 Build_Protected_Subprogram_Call
4772 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4773 Analyze (N);
4774 end if;
4775
4776 return;
4777 end if;
4778
4779 -- First parameter is the Task_Id value from the task value or the
4780 -- Object from the protected object value, obtained by selecting
4781 -- the _Task_Id or _Object from the result of doing an unchecked
4782 -- conversion to convert the value to the corresponding record type.
4783
4784 if Nkind (Concval) = N_Function_Call
4785 and then Is_Task_Type (Conctyp)
4786 and then Ada_Version >= Ada_2005
4787 then
4788 declare
4789 ExpR : constant Node_Id := Relocate_Node (Concval);
4790 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4791 Decl : Node_Id;
4792
4793 begin
4794 Decl :=
4795 Make_Object_Declaration (Loc,
4796 Defining_Identifier => Obj,
4797 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4798 Expression => ExpR);
4799 Set_Etype (Obj, Conctyp);
4800 Decls := New_List (Decl);
4801 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4802 end;
4803
4804 else
4805 Decls := New_List;
4806 end if;
4807
4808 Parm1 := Concurrent_Ref (Concval);
4809
4810 -- Second parameter is the entry index, computed by the routine
4811 -- provided for this purpose. The value of this expression is
4812 -- assigned to an intermediate variable to assure that any entry
4813 -- family index expressions are evaluated before the entry
4814 -- parameters.
4815
4816 if not Is_Protected_Type (Conctyp)
4817 or else
4818 Corresponding_Runtime_Package (Conctyp) =
4819 System_Tasking_Protected_Objects_Entries
4820 then
4821 X := Make_Defining_Identifier (Loc, Name_uX);
4822
4823 Xdecl :=
4824 Make_Object_Declaration (Loc,
4825 Defining_Identifier => X,
4826 Object_Definition =>
4827 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4828 Expression => Actual_Index_Expression (
4829 Loc, Entity (Ename), Index, Concval));
4830
4831 Append_To (Decls, Xdecl);
4832 Parm2 := New_Occurrence_Of (X, Loc);
4833
4834 else
4835 Xdecl := Empty;
4836 Parm2 := Empty;
4837 end if;
4838
4839 -- The third parameter is the packaged parameters. If there are
4840 -- none, then it is just the null address, since nothing is passed.
4841
4842 if No (Parms) then
4843 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4844 P := Empty;
4845
4846 -- Case of parameters present, where third argument is the address
4847 -- of a packaged record containing the required parameter values.
4848
4849 else
4850 -- First build a list of parameter values, which are references to
4851 -- objects of the parameter types.
4852
4853 Plist := New_List;
4854
4855 Actual := First_Actual (N);
4856 Formal := First_Formal (Ent);
4857 while Present (Actual) loop
4858
4859 -- If it is a by-copy type, copy it to a new variable. The
4860 -- packaged record has a field that points to this variable.
4861
4862 if Is_By_Copy_Type (Etype (Actual)) then
4863 N_Node :=
4864 Make_Object_Declaration (Loc,
4865 Defining_Identifier => Make_Temporary (Loc, 'J'),
4866 Aliased_Present => True,
4867 Object_Definition =>
4868 New_Occurrence_Of (Etype (Formal), Loc));
4869
4870 -- Mark the object as not needing initialization since the
4871 -- initialization is performed separately, avoiding errors
4872 -- on cases such as formals of null-excluding access types.
4873
4874 Set_No_Initialization (N_Node);
4875
4876 -- We must make a separate assignment statement for the
4877 -- case of limited types. We cannot assign it unless the
4878 -- Assignment_OK flag is set first. An out formal of an
4879 -- access type or whose type has a Default_Value must also
4880 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4881 -- but no constraint, predicate, or null-exclusion check is
4882 -- applied before the call.
4883
4884 if Ekind (Formal) /= E_Out_Parameter
4885 or else Is_Access_Type (Etype (Formal))
4886 or else
4887 (Is_Scalar_Type (Etype (Formal))
4888 and then
4889 Present (Default_Aspect_Value (Etype (Formal))))
4890 then
4891 N_Var :=
4892 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4893 Set_Assignment_OK (N_Var);
4894 Append_To (Stats,
4895 Make_Assignment_Statement (Loc,
4896 Name => N_Var,
4897 Expression => Relocate_Node (Actual)));
4898
4899 -- Mark the object as internal, so we don't later reset
4900 -- No_Initialization flag in Default_Initialize_Object,
4901 -- which would lead to needless default initialization.
4902 -- We don't set this outside the if statement, because
4903 -- out scalar parameters without Default_Value do require
4904 -- default initialization if Initialize_Scalars applies.
4905
4906 Set_Is_Internal (Defining_Identifier (N_Node));
4907
4908 -- If actual is an out parameter of a null-excluding
4909 -- access type, there is access check on entry, so set
4910 -- Suppress_Assignment_Checks on the generated statement
4911 -- that assigns the actual to the parameter block
4912
4913 Set_Suppress_Assignment_Checks (Last (Stats));
4914 end if;
4915
4916 Append (N_Node, Decls);
4917
4918 Append_To (Plist,
4919 Make_Attribute_Reference (Loc,
4920 Attribute_Name => Name_Unchecked_Access,
4921 Prefix =>
4922 New_Occurrence_Of
4923 (Defining_Identifier (N_Node), Loc)));
4924
4925 else
4926 -- Interface class-wide formal
4927
4928 if Ada_Version >= Ada_2005
4929 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4930 and then Is_Interface (Etype (Formal))
4931 then
4932 Iface_Typ := Etype (Etype (Formal));
4933
4934 -- Generate:
4935 -- formal_iface_type! (actual.iface_tag)'reference
4936
4937 Iface_Tag :=
4938 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4939 pragma Assert (Present (Iface_Tag));
4940
4941 Append_To (Plist,
4942 Make_Reference (Loc,
4943 Unchecked_Convert_To (Iface_Typ,
4944 Make_Selected_Component (Loc,
4945 Prefix =>
4946 Relocate_Node (Actual),
4947 Selector_Name =>
4948 New_Occurrence_Of (Iface_Tag, Loc)))));
4949 else
4950 -- Generate:
4951 -- actual'reference
4952
4953 Append_To (Plist,
4954 Make_Reference (Loc, Relocate_Node (Actual)));
4955 end if;
4956 end if;
4957
4958 Next_Actual (Actual);
4959 Next_Formal_With_Extras (Formal);
4960 end loop;
4961
4962 -- Now build the declaration of parameters initialized with the
4963 -- aggregate containing this constructed parameter list.
4964
4965 P := Make_Defining_Identifier (Loc, Name_uP);
4966
4967 Pdecl :=
4968 Make_Object_Declaration (Loc,
4969 Defining_Identifier => P,
4970 Object_Definition =>
4971 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4972 Expression =>
4973 Make_Aggregate (Loc, Expressions => Plist));
4974
4975 Parm3 :=
4976 Make_Attribute_Reference (Loc,
4977 Prefix => New_Occurrence_Of (P, Loc),
4978 Attribute_Name => Name_Address);
4979
4980 Append (Pdecl, Decls);
4981 end if;
4982
4983 -- Now we can create the call, case of protected type
4984
4985 if Is_Protected_Type (Conctyp) then
4986 case Corresponding_Runtime_Package (Conctyp) is
4987 when System_Tasking_Protected_Objects_Entries =>
4988
4989 -- Change the type of the index declaration
4990
4991 Set_Object_Definition (Xdecl,
4992 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4993
4994 -- Some additional declarations for protected entry calls
4995
4996 if No (Decls) then
4997 Decls := New_List;
4998 end if;
4999
5000 -- Bnn : Communications_Block;
5001
5002 Comm_Name := Make_Temporary (Loc, 'B');
5003
5004 Append_To (Decls,
5005 Make_Object_Declaration (Loc,
5006 Defining_Identifier => Comm_Name,
5007 Object_Definition =>
5008 New_Occurrence_Of
5009 (RTE (RE_Communication_Block), Loc)));
5010
5011 -- Some additional statements for protected entry calls
5012
5013 -- Protected_Entry_Call (
5014 -- Object => po._object'Access,
5015 -- E => <entry index>;
5016 -- Uninterpreted_Data => P'Address;
5017 -- Mode => Simple_Call;
5018 -- Block => Bnn);
5019
5020 Call :=
5021 Make_Procedure_Call_Statement (Loc,
5022 Name =>
5023 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
5024
5025 Parameter_Associations => New_List (
5026 Make_Attribute_Reference (Loc,
5027 Attribute_Name => Name_Unchecked_Access,
5028 Prefix => Parm1),
5029 Parm2,
5030 Parm3,
5031 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
5032 New_Occurrence_Of (Comm_Name, Loc)));
5033
5034 when System_Tasking_Protected_Objects_Single_Entry =>
5035 -- Protected_Single_Entry_Call (
5036 -- Object => po._object'Access,
5037 -- Uninterpreted_Data => P'Address);
5038
5039 Call :=
5040 Make_Procedure_Call_Statement (Loc,
5041 Name =>
5042 New_Occurrence_Of
5043 (RTE (RE_Protected_Single_Entry_Call), Loc),
5044
5045 Parameter_Associations => New_List (
5046 Make_Attribute_Reference (Loc,
5047 Attribute_Name => Name_Unchecked_Access,
5048 Prefix => Parm1),
5049 Parm3));
5050
5051 when others =>
5052 raise Program_Error;
5053 end case;
5054
5055 -- Case of task type
5056
5057 else
5058 Call :=
5059 Make_Procedure_Call_Statement (Loc,
5060 Name =>
5061 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
5062 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
5063
5064 end if;
5065
5066 Append_To (Stats, Call);
5067
5068 -- If there are out or in/out parameters by copy add assignment
5069 -- statements for the result values.
5070
5071 if Present (Parms) then
5072 Actual := First_Actual (N);
5073 Formal := First_Formal (Ent);
5074
5075 Set_Assignment_OK (Actual);
5076 while Present (Actual) loop
5077 if Is_By_Copy_Type (Etype (Actual))
5078 and then Ekind (Formal) /= E_In_Parameter
5079 then
5080 N_Node :=
5081 Make_Assignment_Statement (Loc,
5082 Name => New_Copy (Actual),
5083 Expression =>
5084 Make_Explicit_Dereference (Loc,
5085 Make_Selected_Component (Loc,
5086 Prefix => New_Occurrence_Of (P, Loc),
5087 Selector_Name =>
5088 Make_Identifier (Loc, Chars (Formal)))));
5089
5090 -- In all cases (including limited private types) we want
5091 -- the assignment to be valid.
5092
5093 Set_Assignment_OK (Name (N_Node));
5094
5095 -- If the call is the triggering alternative in an
5096 -- asynchronous select, or the entry_call alternative of a
5097 -- conditional entry call, the assignments for in-out
5098 -- parameters are incorporated into the statement list that
5099 -- follows, so that there are executed only if the entry
5100 -- call succeeds.
5101
5102 if (Nkind (Parent (N)) = N_Triggering_Alternative
5103 and then N = Triggering_Statement (Parent (N)))
5104 or else
5105 (Nkind (Parent (N)) = N_Entry_Call_Alternative
5106 and then N = Entry_Call_Statement (Parent (N)))
5107 then
5108 if No (Statements (Parent (N))) then
5109 Set_Statements (Parent (N), New_List);
5110 end if;
5111
5112 Prepend (N_Node, Statements (Parent (N)));
5113
5114 else
5115 Insert_After (Call, N_Node);
5116 end if;
5117 end if;
5118
5119 Next_Actual (Actual);
5120 Next_Formal_With_Extras (Formal);
5121 end loop;
5122 end if;
5123
5124 -- Finally, create block and analyze it
5125
5126 Rewrite (N,
5127 Make_Block_Statement (Loc,
5128 Declarations => Decls,
5129 Handled_Statement_Sequence =>
5130 Make_Handled_Sequence_Of_Statements (Loc,
5131 Statements => Stats)));
5132
5133 Analyze (N);
5134 end;
5135 end Build_Simple_Entry_Call;
5136
5137 --------------------------------
5138 -- Build_Task_Activation_Call --
5139 --------------------------------
5140
5141 procedure Build_Task_Activation_Call (N : Node_Id) is
5142 Loc : constant Source_Ptr := Sloc (N);
5143 Chain : Entity_Id;
5144 Call : Node_Id;
5145 Name : Node_Id;
5146 P : Node_Id;
5147
5148 begin
5149 -- For sequential elaboration policy, all the tasks will be activated at
5150 -- the end of the elaboration.
5151
5152 if Partition_Elaboration_Policy = 'S' then
5153 return;
5154 end if;
5155
5156 -- Get the activation chain entity. Except in the case of a package
5157 -- body, this is in the node that was passed. For a package body, we
5158 -- have to find the corresponding package declaration node.
5159
5160 if Nkind (N) = N_Package_Body then
5161 P := Corresponding_Spec (N);
5162 loop
5163 P := Parent (P);
5164 exit when Nkind (P) = N_Package_Declaration;
5165 end loop;
5166
5167 Chain := Activation_Chain_Entity (P);
5168
5169 else
5170 Chain := Activation_Chain_Entity (N);
5171 end if;
5172
5173 if Present (Chain) then
5174 if Restricted_Profile then
5175 Name := New_Occurrence_Of
5176 (RTE (RE_Activate_Restricted_Tasks), Loc);
5177 else
5178 Name := New_Occurrence_Of
5179 (RTE (RE_Activate_Tasks), Loc);
5180 end if;
5181
5182 Call :=
5183 Make_Procedure_Call_Statement (Loc,
5184 Name => Name,
5185 Parameter_Associations =>
5186 New_List (Make_Attribute_Reference (Loc,
5187 Prefix => New_Occurrence_Of (Chain, Loc),
5188 Attribute_Name => Name_Unchecked_Access)));
5189
5190 if Nkind (N) = N_Package_Declaration then
5191 if Present (Corresponding_Body (N)) then
5192 null;
5193
5194 elsif Present (Private_Declarations (Specification (N))) then
5195 Append (Call, Private_Declarations (Specification (N)));
5196
5197 else
5198 Append (Call, Visible_Declarations (Specification (N)));
5199 end if;
5200
5201 else
5202 if Present (Handled_Statement_Sequence (N)) then
5203
5204 -- The call goes at the start of the statement sequence after
5205 -- the start of exception range label if one is present.
5206
5207 declare
5208 Stm : Node_Id;
5209
5210 begin
5211 Stm := First (Statements (Handled_Statement_Sequence (N)));
5212
5213 -- A special case, skip exception range label if one is
5214 -- present (from front end zcx processing).
5215
5216 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
5217 Next (Stm);
5218 end if;
5219
5220 -- Another special case, if the first statement is a block
5221 -- from optimization of a local raise to a goto, then the
5222 -- call goes inside this block.
5223
5224 if Nkind (Stm) = N_Block_Statement
5225 and then Exception_Junk (Stm)
5226 then
5227 Stm :=
5228 First (Statements (Handled_Statement_Sequence (Stm)));
5229 end if;
5230
5231 -- Insertion point is after any exception label pushes,
5232 -- since we want it covered by any local handlers.
5233
5234 while Nkind (Stm) in N_Push_xxx_Label loop
5235 Next (Stm);
5236 end loop;
5237
5238 -- Now we have the proper insertion point
5239
5240 Insert_Before (Stm, Call);
5241 end;
5242
5243 else
5244 Set_Handled_Statement_Sequence (N,
5245 Make_Handled_Sequence_Of_Statements (Loc,
5246 Statements => New_List (Call)));
5247 end if;
5248 end if;
5249
5250 Analyze (Call);
5251 Check_Task_Activation (N);
5252 end if;
5253 end Build_Task_Activation_Call;
5254
5255 -------------------------------
5256 -- Build_Task_Allocate_Block --
5257 -------------------------------
5258
5259 procedure Build_Task_Allocate_Block
5260 (Actions : List_Id;
5261 N : Node_Id;
5262 Args : List_Id)
5263 is
5264 T : constant Entity_Id := Entity (Expression (N));
5265 Init : constant Entity_Id := Base_Init_Proc (T);
5266 Loc : constant Source_Ptr := Sloc (N);
5267 Chain : constant Entity_Id :=
5268 Make_Defining_Identifier (Loc, Name_uChain);
5269 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5270 Block : Node_Id;
5271
5272 begin
5273 Block :=
5274 Make_Block_Statement (Loc,
5275 Identifier => New_Occurrence_Of (Blkent, Loc),
5276 Declarations => New_List (
5277
5278 -- _Chain : Activation_Chain;
5279
5280 Make_Object_Declaration (Loc,
5281 Defining_Identifier => Chain,
5282 Aliased_Present => True,
5283 Object_Definition =>
5284 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5285
5286 Handled_Statement_Sequence =>
5287 Make_Handled_Sequence_Of_Statements (Loc,
5288
5289 Statements => New_List (
5290
5291 -- Init (Args);
5292
5293 Make_Procedure_Call_Statement (Loc,
5294 Name => New_Occurrence_Of (Init, Loc),
5295 Parameter_Associations => Args),
5296
5297 -- Activate_Tasks (_Chain);
5298
5299 Make_Procedure_Call_Statement (Loc,
5300 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5301 Parameter_Associations => New_List (
5302 Make_Attribute_Reference (Loc,
5303 Prefix => New_Occurrence_Of (Chain, Loc),
5304 Attribute_Name => Name_Unchecked_Access))))),
5305
5306 Has_Created_Identifier => True,
5307 Is_Task_Allocation_Block => True);
5308
5309 Append_To (Actions,
5310 Make_Implicit_Label_Declaration (Loc,
5311 Defining_Identifier => Blkent,
5312 Label_Construct => Block));
5313
5314 Append_To (Actions, Block);
5315
5316 Set_Activation_Chain_Entity (Block, Chain);
5317 end Build_Task_Allocate_Block;
5318
5319 -----------------------------------------------
5320 -- Build_Task_Allocate_Block_With_Init_Stmts --
5321 -----------------------------------------------
5322
5323 procedure Build_Task_Allocate_Block_With_Init_Stmts
5324 (Actions : List_Id;
5325 N : Node_Id;
5326 Init_Stmts : List_Id)
5327 is
5328 Loc : constant Source_Ptr := Sloc (N);
5329 Chain : constant Entity_Id :=
5330 Make_Defining_Identifier (Loc, Name_uChain);
5331 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5332 Block : Node_Id;
5333
5334 begin
5335 Append_To (Init_Stmts,
5336 Make_Procedure_Call_Statement (Loc,
5337 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5338 Parameter_Associations => New_List (
5339 Make_Attribute_Reference (Loc,
5340 Prefix => New_Occurrence_Of (Chain, Loc),
5341 Attribute_Name => Name_Unchecked_Access))));
5342
5343 Block :=
5344 Make_Block_Statement (Loc,
5345 Identifier => New_Occurrence_Of (Blkent, Loc),
5346 Declarations => New_List (
5347
5348 -- _Chain : Activation_Chain;
5349
5350 Make_Object_Declaration (Loc,
5351 Defining_Identifier => Chain,
5352 Aliased_Present => True,
5353 Object_Definition =>
5354 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5355
5356 Handled_Statement_Sequence =>
5357 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5358
5359 Has_Created_Identifier => True,
5360 Is_Task_Allocation_Block => True);
5361
5362 Append_To (Actions,
5363 Make_Implicit_Label_Declaration (Loc,
5364 Defining_Identifier => Blkent,
5365 Label_Construct => Block));
5366
5367 Append_To (Actions, Block);
5368
5369 Set_Activation_Chain_Entity (Block, Chain);
5370 end Build_Task_Allocate_Block_With_Init_Stmts;
5371
5372 -----------------------------------
5373 -- Build_Task_Proc_Specification --
5374 -----------------------------------
5375
5376 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5377 Loc : constant Source_Ptr := Sloc (T);
5378 Spec_Id : Entity_Id;
5379
5380 begin
5381 -- Case of explicit task type, suffix TB
5382
5383 if Comes_From_Source (T) then
5384 Spec_Id :=
5385 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
5386
5387 -- Case of anonymous task type, suffix B
5388
5389 else
5390 Spec_Id :=
5391 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5392 end if;
5393
5394 Set_Is_Internal (Spec_Id);
5395
5396 -- Associate the procedure with the task, if this is the declaration
5397 -- (and not the body) of the procedure.
5398
5399 if No (Task_Body_Procedure (T)) then
5400 Set_Task_Body_Procedure (T, Spec_Id);
5401 end if;
5402
5403 return
5404 Make_Procedure_Specification (Loc,
5405 Defining_Unit_Name => Spec_Id,
5406 Parameter_Specifications => New_List (
5407 Make_Parameter_Specification (Loc,
5408 Defining_Identifier =>
5409 Make_Defining_Identifier (Loc, Name_uTask),
5410 Parameter_Type =>
5411 Make_Access_Definition (Loc,
5412 Subtype_Mark =>
5413 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5414 end Build_Task_Proc_Specification;
5415
5416 ---------------------------------------
5417 -- Build_Unprotected_Subprogram_Body --
5418 ---------------------------------------
5419
5420 function Build_Unprotected_Subprogram_Body
5421 (N : Node_Id;
5422 Pid : Node_Id) return Node_Id
5423 is
5424 Decls : constant List_Id := Declarations (N);
5425
5426 begin
5427 -- Add renamings for the Protection object, discriminals, privals, and
5428 -- the entry index constant for use by debugger.
5429
5430 Debug_Private_Data_Declarations (Decls);
5431
5432 -- Make an unprotected version of the subprogram for use within the same
5433 -- object, with a new name and an additional parameter representing the
5434 -- object.
5435
5436 return
5437 Make_Subprogram_Body (Sloc (N),
5438 Specification =>
5439 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5440 Declarations => Decls,
5441 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5442 end Build_Unprotected_Subprogram_Body;
5443
5444 ----------------------------
5445 -- Collect_Entry_Families --
5446 ----------------------------
5447
5448 procedure Collect_Entry_Families
5449 (Loc : Source_Ptr;
5450 Cdecls : List_Id;
5451 Current_Node : in out Node_Id;
5452 Conctyp : Entity_Id)
5453 is
5454 Efam : Entity_Id;
5455 Efam_Decl : Node_Id;
5456 Efam_Type : Entity_Id;
5457
5458 begin
5459 Efam := First_Entity (Conctyp);
5460 while Present (Efam) loop
5461 if Ekind (Efam) = E_Entry_Family then
5462 Efam_Type := Make_Temporary (Loc, 'F');
5463
5464 declare
5465 Bas : Entity_Id :=
5466 Base_Type
5467 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5468
5469 Bas_Decl : Node_Id := Empty;
5470 Lo, Hi : Node_Id;
5471
5472 begin
5473 Get_Index_Bounds
5474 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5475
5476 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5477 Bas := Make_Temporary (Loc, 'B');
5478
5479 Bas_Decl :=
5480 Make_Subtype_Declaration (Loc,
5481 Defining_Identifier => Bas,
5482 Subtype_Indication =>
5483 Make_Subtype_Indication (Loc,
5484 Subtype_Mark =>
5485 New_Occurrence_Of (Standard_Integer, Loc),
5486 Constraint =>
5487 Make_Range_Constraint (Loc,
5488 Range_Expression => Make_Range (Loc,
5489 Make_Integer_Literal
5490 (Loc, -Entry_Family_Bound),
5491 Make_Integer_Literal
5492 (Loc, Entry_Family_Bound - 1)))));
5493
5494 Insert_After (Current_Node, Bas_Decl);
5495 Current_Node := Bas_Decl;
5496 Analyze (Bas_Decl);
5497 end if;
5498
5499 Efam_Decl :=
5500 Make_Full_Type_Declaration (Loc,
5501 Defining_Identifier => Efam_Type,
5502 Type_Definition =>
5503 Make_Unconstrained_Array_Definition (Loc,
5504 Subtype_Marks =>
5505 (New_List (New_Occurrence_Of (Bas, Loc))),
5506
5507 Component_Definition =>
5508 Make_Component_Definition (Loc,
5509 Aliased_Present => False,
5510 Subtype_Indication =>
5511 New_Occurrence_Of (Standard_Character, Loc))));
5512 end;
5513
5514 Insert_After (Current_Node, Efam_Decl);
5515 Current_Node := Efam_Decl;
5516 Analyze (Efam_Decl);
5517
5518 Append_To (Cdecls,
5519 Make_Component_Declaration (Loc,
5520 Defining_Identifier =>
5521 Make_Defining_Identifier (Loc, Chars (Efam)),
5522
5523 Component_Definition =>
5524 Make_Component_Definition (Loc,
5525 Aliased_Present => False,
5526 Subtype_Indication =>
5527 Make_Subtype_Indication (Loc,
5528 Subtype_Mark =>
5529 New_Occurrence_Of (Efam_Type, Loc),
5530
5531 Constraint =>
5532 Make_Index_Or_Discriminant_Constraint (Loc,
5533 Constraints => New_List (
5534 New_Occurrence_Of
5535 (Etype (Discrete_Subtype_Definition
5536 (Parent (Efam))), Loc)))))));
5537
5538 end if;
5539
5540 Next_Entity (Efam);
5541 end loop;
5542 end Collect_Entry_Families;
5543
5544 -----------------------
5545 -- Concurrent_Object --
5546 -----------------------
5547
5548 function Concurrent_Object
5549 (Spec_Id : Entity_Id;
5550 Conc_Typ : Entity_Id) return Entity_Id
5551 is
5552 begin
5553 -- Parameter _O or _object
5554
5555 if Is_Protected_Type (Conc_Typ) then
5556 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5557
5558 -- Parameter _task
5559
5560 else
5561 pragma Assert (Is_Task_Type (Conc_Typ));
5562 return First_Formal (Task_Body_Procedure (Conc_Typ));
5563 end if;
5564 end Concurrent_Object;
5565
5566 ----------------------
5567 -- Copy_Result_Type --
5568 ----------------------
5569
5570 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5571 New_Res : constant Node_Id := New_Copy_Tree (Res);
5572 Par_Spec : Node_Id;
5573 Formal : Entity_Id;
5574
5575 begin
5576 -- If the result type is an access_to_subprogram, we must create new
5577 -- entities for its spec.
5578
5579 if Nkind (New_Res) = N_Access_Definition
5580 and then Present (Access_To_Subprogram_Definition (New_Res))
5581 then
5582 -- Provide new entities for the formals
5583
5584 Par_Spec := First (Parameter_Specifications
5585 (Access_To_Subprogram_Definition (New_Res)));
5586 while Present (Par_Spec) loop
5587 Formal := Defining_Identifier (Par_Spec);
5588 Set_Defining_Identifier (Par_Spec,
5589 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5590 Next (Par_Spec);
5591 end loop;
5592 end if;
5593
5594 return New_Res;
5595 end Copy_Result_Type;
5596
5597 --------------------
5598 -- Concurrent_Ref --
5599 --------------------
5600
5601 -- The expression returned for a reference to a concurrent object has the
5602 -- form:
5603
5604 -- taskV!(name)._Task_Id
5605
5606 -- for a task, and
5607
5608 -- objectV!(name)._Object
5609
5610 -- for a protected object. For the case of an access to a concurrent
5611 -- object, there is an extra explicit dereference:
5612
5613 -- taskV!(name.all)._Task_Id
5614 -- objectV!(name.all)._Object
5615
5616 -- here taskV and objectV are the types for the associated records, which
5617 -- contain the required _Task_Id and _Object fields for tasks and protected
5618 -- objects, respectively.
5619
5620 -- For the case of a task type name, the expression is
5621
5622 -- Self;
5623
5624 -- i.e. a call to the Self function which returns precisely this Task_Id
5625
5626 -- For the case of a protected type name, the expression is
5627
5628 -- objectR
5629
5630 -- which is a renaming of the _object field of the current object
5631 -- record, passed into protected operations as a parameter.
5632
5633 function Concurrent_Ref (N : Node_Id) return Node_Id is
5634 Loc : constant Source_Ptr := Sloc (N);
5635 Ntyp : constant Entity_Id := Etype (N);
5636 Dtyp : Entity_Id;
5637 Sel : Name_Id;
5638
5639 function Is_Current_Task (T : Entity_Id) return Boolean;
5640 -- Check whether the reference is to the immediately enclosing task
5641 -- type, or to an outer one (rare but legal).
5642
5643 ---------------------
5644 -- Is_Current_Task --
5645 ---------------------
5646
5647 function Is_Current_Task (T : Entity_Id) return Boolean is
5648 Scop : Entity_Id;
5649
5650 begin
5651 Scop := Current_Scope;
5652 while Present (Scop) and then Scop /= Standard_Standard loop
5653 if Scop = T then
5654 return True;
5655
5656 elsif Is_Task_Type (Scop) then
5657 return False;
5658
5659 -- If this is a procedure nested within the task type, we must
5660 -- assume that it can be called from an inner task, and therefore
5661 -- cannot treat it as a local reference.
5662
5663 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5664 return False;
5665
5666 else
5667 Scop := Scope (Scop);
5668 end if;
5669 end loop;
5670
5671 -- We know that we are within the task body, so should have found it
5672 -- in scope.
5673
5674 raise Program_Error;
5675 end Is_Current_Task;
5676
5677 -- Start of processing for Concurrent_Ref
5678
5679 begin
5680 if Is_Access_Type (Ntyp) then
5681 Dtyp := Designated_Type (Ntyp);
5682
5683 if Is_Protected_Type (Dtyp) then
5684 Sel := Name_uObject;
5685 else
5686 Sel := Name_uTask_Id;
5687 end if;
5688
5689 return
5690 Make_Selected_Component (Loc,
5691 Prefix =>
5692 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5693 Make_Explicit_Dereference (Loc, N)),
5694 Selector_Name => Make_Identifier (Loc, Sel));
5695
5696 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5697 if Is_Task_Type (Entity (N)) then
5698
5699 if Is_Current_Task (Entity (N)) then
5700 return
5701 Make_Function_Call (Loc,
5702 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5703
5704 else
5705 declare
5706 Decl : Node_Id;
5707 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5708 T_Body : constant Node_Id :=
5709 Parent (Corresponding_Body (Parent (Entity (N))));
5710
5711 begin
5712 Decl :=
5713 Make_Object_Declaration (Loc,
5714 Defining_Identifier => T_Self,
5715 Object_Definition =>
5716 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5717 Expression =>
5718 Make_Function_Call (Loc,
5719 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5720 Prepend (Decl, Declarations (T_Body));
5721 Analyze (Decl);
5722 Set_Scope (T_Self, Entity (N));
5723 return New_Occurrence_Of (T_Self, Loc);
5724 end;
5725 end if;
5726
5727 else
5728 pragma Assert (Is_Protected_Type (Entity (N)));
5729
5730 return
5731 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5732 end if;
5733
5734 else
5735 if Is_Protected_Type (Ntyp) then
5736 Sel := Name_uObject;
5737 elsif Is_Task_Type (Ntyp) then
5738 Sel := Name_uTask_Id;
5739 else
5740 raise Program_Error;
5741 end if;
5742
5743 return
5744 Make_Selected_Component (Loc,
5745 Prefix =>
5746 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5747 New_Copy_Tree (N)),
5748 Selector_Name => Make_Identifier (Loc, Sel));
5749 end if;
5750 end Concurrent_Ref;
5751
5752 ------------------------
5753 -- Convert_Concurrent --
5754 ------------------------
5755
5756 function Convert_Concurrent
5757 (N : Node_Id;
5758 Typ : Entity_Id) return Node_Id
5759 is
5760 begin
5761 if not Is_Concurrent_Type (Typ) then
5762 return N;
5763 else
5764 return
5765 Unchecked_Convert_To
5766 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5767 end if;
5768 end Convert_Concurrent;
5769
5770 -------------------------------------
5771 -- Debug_Private_Data_Declarations --
5772 -------------------------------------
5773
5774 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5775 Debug_Nod : Node_Id;
5776 Decl : Node_Id;
5777
5778 begin
5779 Decl := First (Decls);
5780 while Present (Decl) and then not Comes_From_Source (Decl) loop
5781
5782 -- Declaration for concurrent entity _object and its access type,
5783 -- along with the entry index subtype:
5784 -- type prot_typVP is access prot_typV;
5785 -- _object : prot_typVP := prot_typV (_O);
5786 -- subtype Jnn is <Type of Index> range Low .. High;
5787
5788 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5789 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5790
5791 -- Declaration for the Protection object, discriminals, privals, and
5792 -- entry index constant:
5793 -- conc_typR : protection_typ renames _object._object;
5794 -- discr_nameD : discr_typ renames _object.discr_name;
5795 -- discr_nameD : discr_typ renames _task.discr_name;
5796 -- prival_name : comp_typ renames _object.comp_name;
5797 -- J : constant Jnn :=
5798 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5799
5800 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5801 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5802 Debug_Nod := Debug_Renaming_Declaration (Decl);
5803
5804 if Present (Debug_Nod) then
5805 Insert_After (Decl, Debug_Nod);
5806 end if;
5807 end if;
5808
5809 Next (Decl);
5810 end loop;
5811 end Debug_Private_Data_Declarations;
5812
5813 ------------------------------
5814 -- Ensure_Statement_Present --
5815 ------------------------------
5816
5817 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5818 Stmt : Node_Id;
5819
5820 begin
5821 if Opt.Suppress_Control_Flow_Optimizations
5822 and then Is_Empty_List (Statements (Alt))
5823 then
5824 Stmt := Make_Null_Statement (Loc);
5825
5826 -- Mark NULL statement as coming from source so that it is not
5827 -- eliminated by GIGI.
5828
5829 -- Another covert channel. If this is a requirement, it must be
5830 -- documented in sinfo/einfo ???
5831
5832 Set_Comes_From_Source (Stmt, True);
5833
5834 Set_Statements (Alt, New_List (Stmt));
5835 end if;
5836 end Ensure_Statement_Present;
5837
5838 ----------------------------
5839 -- Entry_Index_Expression --
5840 ----------------------------
5841
5842 function Entry_Index_Expression
5843 (Sloc : Source_Ptr;
5844 Ent : Entity_Id;
5845 Index : Node_Id;
5846 Ttyp : Entity_Id) return Node_Id
5847 is
5848 Expr : Node_Id;
5849 Num : Node_Id;
5850 Lo : Node_Id;
5851 Hi : Node_Id;
5852 Prev : Entity_Id;
5853 S : Node_Id;
5854
5855 begin
5856 -- The queues of entries and entry families appear in textual order in
5857 -- the associated record. The entry index is computed as the sum of the
5858 -- number of queues for all entries that precede the designated one, to
5859 -- which is added the index expression, if this expression denotes a
5860 -- member of a family.
5861
5862 -- The following is a place holder for the count of simple entries
5863
5864 Num := Make_Integer_Literal (Sloc, 1);
5865
5866 -- We construct an expression which is a series of addition operations.
5867 -- The first operand is the number of single entries that precede this
5868 -- one, the second operand is the index value relative to the start of
5869 -- the referenced family, and the remaining operands are the lengths of
5870 -- the entry families that precede this entry, i.e. the constructed
5871 -- expression is:
5872
5873 -- number_simple_entries +
5874 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5875 -- family'length + ...
5876
5877 -- where index-value is the given index value, and s is the index
5878 -- subtype (we have to use pos because the subtype might be an
5879 -- enumeration type preventing direct subtraction). Note that the task
5880 -- entry array is one-indexed.
5881
5882 -- The upper bound of the entry family may be a discriminant, so we
5883 -- retrieve the lower bound explicitly to compute offset, rather than
5884 -- using the index subtype which may mention a discriminant.
5885
5886 if Present (Index) then
5887 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5888
5889 Expr :=
5890 Make_Op_Add (Sloc,
5891 Left_Opnd => Num,
5892 Right_Opnd =>
5893 Family_Offset
5894 (Sloc,
5895 Make_Attribute_Reference (Sloc,
5896 Attribute_Name => Name_Pos,
5897 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5898 Expressions => New_List (Relocate_Node (Index))),
5899 Type_Low_Bound (S),
5900 Ttyp,
5901 False));
5902 else
5903 Expr := Num;
5904 end if;
5905
5906 -- Now add lengths of preceding entries and entry families
5907
5908 Prev := First_Entity (Ttyp);
5909 while Chars (Prev) /= Chars (Ent)
5910 or else (Ekind (Prev) /= Ekind (Ent))
5911 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5912 loop
5913 if Ekind (Prev) = E_Entry then
5914 Set_Intval (Num, Intval (Num) + 1);
5915
5916 elsif Ekind (Prev) = E_Entry_Family then
5917 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5918 Lo := Type_Low_Bound (S);
5919 Hi := Type_High_Bound (S);
5920
5921 Expr :=
5922 Make_Op_Add (Sloc,
5923 Left_Opnd => Expr,
5924 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5925
5926 -- Other components are anonymous types to be ignored
5927
5928 else
5929 null;
5930 end if;
5931
5932 Next_Entity (Prev);
5933 end loop;
5934
5935 return Expr;
5936 end Entry_Index_Expression;
5937
5938 ---------------------------
5939 -- Establish_Task_Master --
5940 ---------------------------
5941
5942 procedure Establish_Task_Master (N : Node_Id) is
5943 Call : Node_Id;
5944
5945 begin
5946 if Restriction_Active (No_Task_Hierarchy) = False then
5947 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5948
5949 -- The block may have no declarations (and nevertheless be a task
5950 -- master) if it contains a call that may return an object that
5951 -- contains tasks.
5952
5953 if No (Declarations (N)) then
5954 Set_Declarations (N, New_List (Call));
5955 else
5956 Prepend_To (Declarations (N), Call);
5957 end if;
5958
5959 Analyze (Call);
5960 end if;
5961 end Establish_Task_Master;
5962
5963 --------------------------------
5964 -- Expand_Accept_Declarations --
5965 --------------------------------
5966
5967 -- Part of the expansion of an accept statement involves the creation of
5968 -- a declaration that can be referenced from the statement sequence of
5969 -- the accept:
5970
5971 -- Ann : Address;
5972
5973 -- This declaration is inserted immediately before the accept statement
5974 -- and it is important that it be inserted before the statements of the
5975 -- statement sequence are analyzed. Thus it would be too late to create
5976 -- this declaration in the Expand_N_Accept_Statement routine, which is
5977 -- why there is a separate procedure to be called directly from Sem_Ch9.
5978
5979 -- Ann is used to hold the address of the record containing the parameters
5980 -- (see Expand_N_Entry_Call for more details on how this record is built).
5981 -- References to the parameters do an unchecked conversion of this address
5982 -- to a pointer to the required record type, and then access the field that
5983 -- holds the value of the required parameter. The entity for the address
5984 -- variable is held as the top stack element (i.e. the last element) of the
5985 -- Accept_Address stack in the corresponding entry entity, and this element
5986 -- must be set in place before the statements are processed.
5987
5988 -- The above description applies to the case of a stand alone accept
5989 -- statement, i.e. one not appearing as part of a select alternative.
5990
5991 -- For the case of an accept that appears as part of a select alternative
5992 -- of a selective accept, we must still create the declaration right away,
5993 -- since Ann is needed immediately, but there is an important difference:
5994
5995 -- The declaration is inserted before the selective accept, not before
5996 -- the accept statement (which is not part of a list anyway, and so would
5997 -- not accommodate inserted declarations)
5998
5999 -- We only need one address variable for the entire selective accept. So
6000 -- the Ann declaration is created only for the first accept alternative,
6001 -- and subsequent accept alternatives reference the same Ann variable.
6002
6003 -- We can distinguish the two cases by seeing whether the accept statement
6004 -- is part of a list. If not, then it must be in an accept alternative.
6005
6006 -- To expand the requeue statement, a label is provided at the end of the
6007 -- accept statement or alternative of which it is a part, so that the
6008 -- statement can be skipped after the requeue is complete. This label is
6009 -- created here rather than during the expansion of the accept statement,
6010 -- because it will be needed by any requeue statements within the accept,
6011 -- which are expanded before the accept.
6012
6013 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
6014 Loc : constant Source_Ptr := Sloc (N);
6015 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6016 Ann : Entity_Id := Empty;
6017 Adecl : Node_Id;
6018 Lab : Node_Id;
6019 Ldecl : Node_Id;
6020 Ldecl2 : Node_Id;
6021
6022 begin
6023 if Expander_Active then
6024
6025 -- If we have no handled statement sequence, we may need to build
6026 -- a dummy sequence consisting of a null statement. This can be
6027 -- skipped if the trivial accept optimization is permitted.
6028
6029 if not Trivial_Accept_OK
6030 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6031 then
6032 Set_Handled_Statement_Sequence (N,
6033 Make_Handled_Sequence_Of_Statements (Loc,
6034 Statements => New_List (Make_Null_Statement (Loc))));
6035 end if;
6036
6037 -- Create and declare two labels to be placed at the end of the
6038 -- accept statement. The first label is used to allow requeues to
6039 -- skip the remainder of entry processing. The second label is used
6040 -- to skip the remainder of entry processing if the rendezvous
6041 -- completes in the middle of the accept body.
6042
6043 if Present (Handled_Statement_Sequence (N)) then
6044 declare
6045 Ent : Entity_Id;
6046
6047 begin
6048 Ent := Make_Temporary (Loc, 'L');
6049 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6050 Ldecl :=
6051 Make_Implicit_Label_Declaration (Loc,
6052 Defining_Identifier => Ent,
6053 Label_Construct => Lab);
6054 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6055
6056 Ent := Make_Temporary (Loc, 'L');
6057 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6058 Ldecl2 :=
6059 Make_Implicit_Label_Declaration (Loc,
6060 Defining_Identifier => Ent,
6061 Label_Construct => Lab);
6062 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6063 end;
6064
6065 else
6066 Ldecl := Empty;
6067 Ldecl2 := Empty;
6068 end if;
6069
6070 -- Case of stand alone accept statement
6071
6072 if Is_List_Member (N) then
6073
6074 if Present (Handled_Statement_Sequence (N)) then
6075 Ann := Make_Temporary (Loc, 'A');
6076
6077 Adecl :=
6078 Make_Object_Declaration (Loc,
6079 Defining_Identifier => Ann,
6080 Object_Definition =>
6081 New_Occurrence_Of (RTE (RE_Address), Loc));
6082
6083 Insert_Before_And_Analyze (N, Adecl);
6084 Insert_Before_And_Analyze (N, Ldecl);
6085 Insert_Before_And_Analyze (N, Ldecl2);
6086 end if;
6087
6088 -- Case of accept statement which is in an accept alternative
6089
6090 else
6091 declare
6092 Acc_Alt : constant Node_Id := Parent (N);
6093 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
6094 Alt : Node_Id;
6095
6096 begin
6097 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
6098 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
6099
6100 -- ??? Consider a single label for select statements
6101
6102 if Present (Handled_Statement_Sequence (N)) then
6103 Prepend (Ldecl2,
6104 Statements (Handled_Statement_Sequence (N)));
6105 Analyze (Ldecl2);
6106
6107 Prepend (Ldecl,
6108 Statements (Handled_Statement_Sequence (N)));
6109 Analyze (Ldecl);
6110 end if;
6111
6112 -- Find first accept alternative of the selective accept. A
6113 -- valid selective accept must have at least one accept in it.
6114
6115 Alt := First (Select_Alternatives (Sel_Acc));
6116
6117 while Nkind (Alt) /= N_Accept_Alternative loop
6118 Next (Alt);
6119 end loop;
6120
6121 -- If this is the first accept statement, then we have to
6122 -- create the Ann variable, as for the stand alone case, except
6123 -- that it is inserted before the selective accept. Similarly,
6124 -- a label for requeue expansion must be declared.
6125
6126 if N = Accept_Statement (Alt) then
6127 Ann := Make_Temporary (Loc, 'A');
6128 Adecl :=
6129 Make_Object_Declaration (Loc,
6130 Defining_Identifier => Ann,
6131 Object_Definition =>
6132 New_Occurrence_Of (RTE (RE_Address), Loc));
6133
6134 Insert_Before_And_Analyze (Sel_Acc, Adecl);
6135
6136 -- If this is not the first accept statement, then find the Ann
6137 -- variable allocated by the first accept and use it.
6138
6139 else
6140 Ann :=
6141 Node (Last_Elmt (Accept_Address
6142 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
6143 end if;
6144 end;
6145 end if;
6146
6147 -- Merge here with Ann either created or referenced, and Adecl
6148 -- pointing to the corresponding declaration. Remaining processing
6149 -- is the same for the two cases.
6150
6151 if Present (Ann) then
6152 Append_Elmt (Ann, Accept_Address (Ent));
6153 Set_Debug_Info_Needed (Ann);
6154 end if;
6155
6156 -- Create renaming declarations for the entry formals. Each reference
6157 -- to a formal becomes a dereference of a component of the parameter
6158 -- block, whose address is held in Ann. These declarations are
6159 -- eventually inserted into the accept block, and analyzed there so
6160 -- that they have the proper scope for gdb and do not conflict with
6161 -- other declarations.
6162
6163 if Present (Parameter_Specifications (N))
6164 and then Present (Handled_Statement_Sequence (N))
6165 then
6166 declare
6167 Comp : Entity_Id;
6168 Decl : Node_Id;
6169 Formal : Entity_Id;
6170 New_F : Entity_Id;
6171 Renamed_Formal : Node_Id;
6172
6173 begin
6174 Push_Scope (Ent);
6175 Formal := First_Formal (Ent);
6176
6177 while Present (Formal) loop
6178 Comp := Entry_Component (Formal);
6179 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
6180
6181 Set_Etype (New_F, Etype (Formal));
6182 Set_Scope (New_F, Ent);
6183
6184 -- Now we set debug info needed on New_F even though it does
6185 -- not come from source, so that the debugger will get the
6186 -- right information for these generated names.
6187
6188 Set_Debug_Info_Needed (New_F);
6189
6190 if Ekind (Formal) = E_In_Parameter then
6191 Set_Ekind (New_F, E_Constant);
6192 else
6193 Set_Ekind (New_F, E_Variable);
6194 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
6195 end if;
6196
6197 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
6198
6199 Renamed_Formal :=
6200 Make_Selected_Component (Loc,
6201 Prefix =>
6202 Unchecked_Convert_To (
6203 Entry_Parameters_Type (Ent),
6204 New_Occurrence_Of (Ann, Loc)),
6205 Selector_Name =>
6206 New_Occurrence_Of (Comp, Loc));
6207
6208 Decl :=
6209 Build_Renamed_Formal_Declaration
6210 (New_F, Formal, Comp, Renamed_Formal);
6211
6212 if No (Declarations (N)) then
6213 Set_Declarations (N, New_List);
6214 end if;
6215
6216 Append (Decl, Declarations (N));
6217 Set_Renamed_Object (Formal, New_F);
6218 Next_Formal (Formal);
6219 end loop;
6220
6221 End_Scope;
6222 end;
6223 end if;
6224 end if;
6225 end Expand_Accept_Declarations;
6226
6227 ---------------------------------------------
6228 -- Expand_Access_Protected_Subprogram_Type --
6229 ---------------------------------------------
6230
6231 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6232 Loc : constant Source_Ptr := Sloc (N);
6233 T : constant Entity_Id := Defining_Identifier (N);
6234 D_T : constant Entity_Id := Designated_Type (T);
6235 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6236 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6237 P_List : constant List_Id :=
6238 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
6239
6240 Comps : List_Id;
6241 Decl1 : Node_Id;
6242 Decl2 : Node_Id;
6243 Def1 : Node_Id;
6244
6245 begin
6246 -- Create access to subprogram with full signature
6247
6248 if Etype (D_T) /= Standard_Void_Type then
6249 Def1 :=
6250 Make_Access_Function_Definition (Loc,
6251 Parameter_Specifications => P_List,
6252 Result_Definition =>
6253 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6254
6255 else
6256 Def1 :=
6257 Make_Access_Procedure_Definition (Loc,
6258 Parameter_Specifications => P_List);
6259 end if;
6260
6261 Decl1 :=
6262 Make_Full_Type_Declaration (Loc,
6263 Defining_Identifier => D_T2,
6264 Type_Definition => Def1);
6265
6266 -- Declare the new types before the original one since the latter will
6267 -- refer to them through the Equivalent_Type slot.
6268
6269 Insert_Before_And_Analyze (N, Decl1);
6270
6271 -- Associate the access to subprogram with its original access to
6272 -- protected subprogram type. Needed by the backend to know that this
6273 -- type corresponds with an access to protected subprogram type.
6274
6275 Set_Original_Access_Type (D_T2, T);
6276
6277 -- Create Equivalent_Type, a record with two components for an access to
6278 -- object and an access to subprogram.
6279
6280 Comps := New_List (
6281 Make_Component_Declaration (Loc,
6282 Defining_Identifier => Make_Temporary (Loc, 'P'),
6283 Component_Definition =>
6284 Make_Component_Definition (Loc,
6285 Aliased_Present => False,
6286 Subtype_Indication =>
6287 New_Occurrence_Of (RTE (RE_Address), Loc))),
6288
6289 Make_Component_Declaration (Loc,
6290 Defining_Identifier => Make_Temporary (Loc, 'S'),
6291 Component_Definition =>
6292 Make_Component_Definition (Loc,
6293 Aliased_Present => False,
6294 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6295
6296 Decl2 :=
6297 Make_Full_Type_Declaration (Loc,
6298 Defining_Identifier => E_T,
6299 Type_Definition =>
6300 Make_Record_Definition (Loc,
6301 Component_List =>
6302 Make_Component_List (Loc, Component_Items => Comps)));
6303
6304 Insert_Before_And_Analyze (N, Decl2);
6305 Set_Equivalent_Type (T, E_T);
6306 end Expand_Access_Protected_Subprogram_Type;
6307
6308 --------------------------
6309 -- Expand_Entry_Barrier --
6310 --------------------------
6311
6312 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6313 Cond : constant Node_Id :=
6314 Condition (Entry_Body_Formal_Part (N));
6315 Prot : constant Entity_Id := Scope (Ent);
6316 Spec_Decl : constant Node_Id := Parent (Prot);
6317 Func : Entity_Id := Empty;
6318 B_F : Node_Id;
6319 Body_Decl : Node_Id;
6320
6321 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
6322 -- Check whether entity in Barrier is external to protected type.
6323 -- If so, barrier may not be properly synchronized.
6324
6325 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
6326 -- Check whether N follows the Pure_Barriers restriction. Return OK if
6327 -- so.
6328
6329 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
6330 -- Check whether entity name N denotes a component of the protected
6331 -- object. This is used to check the Simple_Barrier restriction.
6332
6333 ----------------------
6334 -- Is_Global_Entity --
6335 ----------------------
6336
6337 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6338 E : Entity_Id;
6339 S : Entity_Id;
6340
6341 begin
6342 if Is_Entity_Name (N) and then Present (Entity (N)) then
6343 E := Entity (N);
6344 S := Scope (E);
6345
6346 if Ekind (E) = E_Variable then
6347
6348 -- If the variable is local to the barrier function generated
6349 -- during expansion, it is ok. If expansion is not performed,
6350 -- then Func is Empty so this test cannot succeed.
6351
6352 if Scope (E) = Func then
6353 null;
6354
6355 -- A protected call from a barrier to another object is ok
6356
6357 elsif Ekind (Etype (E)) = E_Protected_Type then
6358 null;
6359
6360 -- If the variable is within the package body we consider
6361 -- this safe. This is a common (if dubious) idiom.
6362
6363 elsif S = Scope (Prot)
6364 and then Ekind_In (S, E_Package, E_Generic_Package)
6365 and then Nkind (Parent (E)) = N_Object_Declaration
6366 and then Nkind (Parent (Parent (E))) = N_Package_Body
6367 then
6368 null;
6369
6370 else
6371 Error_Msg_N ("potentially unsynchronized barrier??", N);
6372 Error_Msg_N ("\& should be private component of type??", N);
6373 end if;
6374 end if;
6375 end if;
6376
6377 return OK;
6378 end Is_Global_Entity;
6379
6380 procedure Check_Unprotected_Barrier is
6381 new Traverse_Proc (Is_Global_Entity);
6382
6383 ----------------------------
6384 -- Is_Simple_Barrier_Name --
6385 ----------------------------
6386
6387 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
6388 Renamed : Node_Id;
6389
6390 begin
6391 -- Check for case of _object.all.field (note that the explicit
6392 -- dereference gets inserted by analyze/expand of _object.field).
6393
6394 if Expander_Active then
6395 Renamed := Renamed_Object (Entity (N));
6396
6397 return
6398 Present (Renamed)
6399 and then Nkind (Renamed) = N_Selected_Component
6400 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6401 else
6402 return Scope (Entity (N)) = Current_Scope;
6403 end if;
6404 end Is_Simple_Barrier_Name;
6405
6406 ---------------------
6407 -- Is_Pure_Barrier --
6408 ---------------------
6409
6410 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6411 begin
6412 case Nkind (N) is
6413 when N_Expanded_Name |
6414 N_Identifier =>
6415 if No (Entity (N)) then
6416 return Abandon;
6417 end if;
6418
6419 case Ekind (Entity (N)) is
6420 when E_Constant |
6421 E_Discriminant |
6422 E_Named_Integer |
6423 E_Named_Real |
6424 E_Enumeration_Literal =>
6425 return OK;
6426
6427 when E_Component |
6428 E_Variable =>
6429
6430 -- A variable in the protected type is expanded as a
6431 -- component.
6432
6433 if Is_Simple_Barrier_Name (N) then
6434 return OK;
6435 end if;
6436
6437 when others =>
6438 null;
6439 end case;
6440
6441 when N_Integer_Literal |
6442 N_Real_Literal |
6443 N_Character_Literal =>
6444 return OK;
6445
6446 when N_Op_Boolean |
6447 N_Op_Not =>
6448 if Ekind (Entity (N)) = E_Operator then
6449 return OK;
6450 end if;
6451
6452 when N_Short_Circuit =>
6453 return OK;
6454
6455 when others =>
6456 null;
6457 end case;
6458
6459 return Abandon;
6460 end Is_Pure_Barrier;
6461
6462 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6463
6464 -- Start of processing for Expand_Entry_Barrier
6465
6466 begin
6467 if No_Run_Time_Mode then
6468 Error_Msg_CRT ("entry barrier", N);
6469 return;
6470 end if;
6471
6472 -- The body of the entry barrier must be analyzed in the context of the
6473 -- protected object, but its scope is external to it, just as any other
6474 -- unprotected version of a protected operation. The specification has
6475 -- been produced when the protected type declaration was elaborated. We
6476 -- build the body, insert it in the enclosing scope, but analyze it in
6477 -- the current context. A more uniform approach would be to treat the
6478 -- barrier just as a protected function, and discard the protected
6479 -- version of it because it is never called.
6480
6481 if Expander_Active then
6482 B_F := Build_Barrier_Function (N, Ent, Prot);
6483 Func := Barrier_Function (Ent);
6484 Set_Corresponding_Spec (B_F, Func);
6485
6486 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6487
6488 if Nkind (Parent (Body_Decl)) = N_Subunit then
6489 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6490 end if;
6491
6492 Insert_Before_And_Analyze (Body_Decl, B_F);
6493
6494 Set_Discriminals (Spec_Decl);
6495 Set_Scope (Func, Scope (Prot));
6496
6497 else
6498 Analyze_And_Resolve (Cond, Any_Boolean);
6499 end if;
6500
6501 -- Check Pure_Barriers restriction
6502
6503 if Check_Pure_Barriers (Cond) = Abandon then
6504 Check_Restriction (Pure_Barriers, Cond);
6505 end if;
6506
6507 -- The Ravenscar profile restricts barriers to simple variables declared
6508 -- within the protected object. We also allow Boolean constants, since
6509 -- these appear in several published examples and are also allowed by
6510 -- other compilers.
6511
6512 -- Note that after analysis variables in this context will be replaced
6513 -- by the corresponding prival, that is to say a renaming of a selected
6514 -- component of the form _Object.Var. If expansion is disabled, as
6515 -- within a generic, we check that the entity appears in the current
6516 -- scope.
6517
6518 if Is_Entity_Name (Cond) then
6519
6520 -- A small optimization of useless renamings. If the scope of the
6521 -- entity of the condition is not the barrier function, then the
6522 -- condition does not reference any of the generated renamings
6523 -- within the function.
6524
6525 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6526 Set_Declarations (B_F, Empty_List);
6527 end if;
6528
6529 if Entity (Cond) = Standard_False
6530 or else
6531 Entity (Cond) = Standard_True
6532 then
6533 return;
6534
6535 elsif Is_Simple_Barrier_Name (Cond) then
6536 return;
6537 end if;
6538 end if;
6539
6540 -- It is not a boolean variable or literal, so check the restriction.
6541 -- Note that it is safe to be calling Check_Restriction from here, even
6542 -- though this is part of the expander, since Expand_Entry_Barrier is
6543 -- called from Sem_Ch9 even in -gnatc mode.
6544
6545 Check_Restriction (Simple_Barriers, Cond);
6546
6547 -- Emit warning if barrier contains global entities and is thus
6548 -- potentially unsynchronized.
6549
6550 Check_Unprotected_Barrier (Cond);
6551 end Expand_Entry_Barrier;
6552
6553 ------------------------------
6554 -- Expand_N_Abort_Statement --
6555 ------------------------------
6556
6557 -- Expand abort T1, T2, .. Tn; into:
6558 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6559
6560 procedure Expand_N_Abort_Statement (N : Node_Id) is
6561 Loc : constant Source_Ptr := Sloc (N);
6562 Tlist : constant List_Id := Names (N);
6563 Count : Nat;
6564 Aggr : Node_Id;
6565 Tasknm : Node_Id;
6566
6567 begin
6568 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6569 Count := 0;
6570
6571 Tasknm := First (Tlist);
6572
6573 while Present (Tasknm) loop
6574 Count := Count + 1;
6575
6576 -- A task interface class-wide type object is being aborted. Retrieve
6577 -- its _task_id by calling a dispatching routine.
6578
6579 if Ada_Version >= Ada_2005
6580 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6581 and then Is_Interface (Etype (Tasknm))
6582 and then Is_Task_Interface (Etype (Tasknm))
6583 then
6584 Append_To (Component_Associations (Aggr),
6585 Make_Component_Association (Loc,
6586 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6587 Expression =>
6588
6589 -- Task_Id (Tasknm._disp_get_task_id)
6590
6591 Make_Unchecked_Type_Conversion (Loc,
6592 Subtype_Mark =>
6593 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6594 Expression =>
6595 Make_Selected_Component (Loc,
6596 Prefix => New_Copy_Tree (Tasknm),
6597 Selector_Name =>
6598 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6599
6600 else
6601 Append_To (Component_Associations (Aggr),
6602 Make_Component_Association (Loc,
6603 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6604 Expression => Concurrent_Ref (Tasknm)));
6605 end if;
6606
6607 Next (Tasknm);
6608 end loop;
6609
6610 Rewrite (N,
6611 Make_Procedure_Call_Statement (Loc,
6612 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6613 Parameter_Associations => New_List (
6614 Make_Qualified_Expression (Loc,
6615 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6616 Expression => Aggr))));
6617
6618 Analyze (N);
6619 end Expand_N_Abort_Statement;
6620
6621 -------------------------------
6622 -- Expand_N_Accept_Statement --
6623 -------------------------------
6624
6625 -- This procedure handles expansion of accept statements that stand alone,
6626 -- i.e. they are not part of an accept alternative. The expansion of
6627 -- accept statement in accept alternatives is handled by the routines
6628 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6629 -- following description applies only to stand alone accept statements.
6630
6631 -- If there is no handled statement sequence, or only null statements, then
6632 -- this is called a trivial accept, and the expansion is:
6633
6634 -- Accept_Trivial (entry-index)
6635
6636 -- If there is a handled statement sequence, then the expansion is:
6637
6638 -- Ann : Address;
6639 -- {Lnn : Label}
6640
6641 -- begin
6642 -- begin
6643 -- Accept_Call (entry-index, Ann);
6644 -- Renaming_Declarations for formals
6645 -- <statement sequence from N_Accept_Statement node>
6646 -- Complete_Rendezvous;
6647 -- <<Lnn>>
6648 --
6649 -- exception
6650 -- when ... =>
6651 -- <exception handler from N_Accept_Statement node>
6652 -- Complete_Rendezvous;
6653 -- when ... =>
6654 -- <exception handler from N_Accept_Statement node>
6655 -- Complete_Rendezvous;
6656 -- ...
6657 -- end;
6658
6659 -- exception
6660 -- when all others =>
6661 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6662 -- end;
6663
6664 -- The first three declarations were already inserted ahead of the accept
6665 -- statement by the Expand_Accept_Declarations procedure, which was called
6666 -- directly from the semantics during analysis of the accept statement,
6667 -- before analyzing its contained statements.
6668
6669 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6670 -- from possible expansion activity (the original source of course does
6671 -- not have any declarations associated with the accept statement, since
6672 -- an accept statement has no declarative part). In particular, if the
6673 -- expander is active, the first such declaration is the declaration of
6674 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6675
6676 -- The two blocks are merged into a single block if the inner block has
6677 -- no exception handlers, but otherwise two blocks are required, since
6678 -- exceptions might be raised in the exception handlers of the inner
6679 -- block, and Exceptional_Complete_Rendezvous must be called.
6680
6681 procedure Expand_N_Accept_Statement (N : Node_Id) is
6682 Loc : constant Source_Ptr := Sloc (N);
6683 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6684 Ename : constant Node_Id := Entry_Direct_Name (N);
6685 Eindx : constant Node_Id := Entry_Index (N);
6686 Eent : constant Entity_Id := Entity (Ename);
6687 Acstack : constant Elist_Id := Accept_Address (Eent);
6688 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6689 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6690 Blkent : Entity_Id;
6691 Call : Node_Id;
6692 Block : Node_Id;
6693
6694 begin
6695 -- If the accept statement is not part of a list, then its parent must
6696 -- be an accept alternative, and, as described above, we do not do any
6697 -- expansion for such accept statements at this level.
6698
6699 if not Is_List_Member (N) then
6700 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6701 return;
6702
6703 -- Trivial accept case (no statement sequence, or null statements).
6704 -- If the accept statement has declarations, then just insert them
6705 -- before the procedure call.
6706
6707 elsif Trivial_Accept_OK
6708 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6709 then
6710 -- Remove declarations for renamings, because the parameter block
6711 -- will not be assigned.
6712
6713 declare
6714 D : Node_Id;
6715 Next_D : Node_Id;
6716
6717 begin
6718 D := First (Declarations (N));
6719 while Present (D) loop
6720 Next_D := Next (D);
6721 if Nkind (D) = N_Object_Renaming_Declaration then
6722 Remove (D);
6723 end if;
6724
6725 D := Next_D;
6726 end loop;
6727 end;
6728
6729 if Present (Declarations (N)) then
6730 Insert_Actions (N, Declarations (N));
6731 end if;
6732
6733 Rewrite (N,
6734 Make_Procedure_Call_Statement (Loc,
6735 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6736 Parameter_Associations => New_List (
6737 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6738
6739 Analyze (N);
6740
6741 -- Discard Entry_Address that was created for it, so it will not be
6742 -- emitted if this accept statement is in the statement part of a
6743 -- delay alternative.
6744
6745 if Present (Stats) then
6746 Remove_Last_Elmt (Acstack);
6747 end if;
6748
6749 -- Case of statement sequence present
6750
6751 else
6752 -- Construct the block, using the declarations from the accept
6753 -- statement if any to initialize the declarations of the block.
6754
6755 Blkent := Make_Temporary (Loc, 'A');
6756 Set_Ekind (Blkent, E_Block);
6757 Set_Etype (Blkent, Standard_Void_Type);
6758 Set_Scope (Blkent, Current_Scope);
6759
6760 Block :=
6761 Make_Block_Statement (Loc,
6762 Identifier => New_Occurrence_Of (Blkent, Loc),
6763 Declarations => Declarations (N),
6764 Handled_Statement_Sequence => Build_Accept_Body (N));
6765
6766 -- For the analysis of the generated declarations, the parent node
6767 -- must be properly set.
6768
6769 Set_Parent (Block, Parent (N));
6770
6771 -- Prepend call to Accept_Call to main statement sequence If the
6772 -- accept has exception handlers, the statement sequence is wrapped
6773 -- in a block. Insert call and renaming declarations in the
6774 -- declarations of the block, so they are elaborated before the
6775 -- handlers.
6776
6777 Call :=
6778 Make_Procedure_Call_Statement (Loc,
6779 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6780 Parameter_Associations => New_List (
6781 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6782 New_Occurrence_Of (Ann, Loc)));
6783
6784 if Parent (Stats) = N then
6785 Prepend (Call, Statements (Stats));
6786 else
6787 Set_Declarations (Parent (Stats), New_List (Call));
6788 end if;
6789
6790 Analyze (Call);
6791
6792 Push_Scope (Blkent);
6793
6794 declare
6795 D : Node_Id;
6796 Next_D : Node_Id;
6797 Typ : Entity_Id;
6798
6799 begin
6800 D := First (Declarations (N));
6801 while Present (D) loop
6802 Next_D := Next (D);
6803
6804 if Nkind (D) = N_Object_Renaming_Declaration then
6805
6806 -- The renaming declarations for the formals were created
6807 -- during analysis of the accept statement, and attached to
6808 -- the list of declarations. Place them now in the context
6809 -- of the accept block or subprogram.
6810
6811 Remove (D);
6812 Typ := Entity (Subtype_Mark (D));
6813 Insert_After (Call, D);
6814 Analyze (D);
6815
6816 -- If the formal is class_wide, it does not have an actual
6817 -- subtype. The analysis of the renaming declaration creates
6818 -- one, but we need to retain the class-wide nature of the
6819 -- entity.
6820
6821 if Is_Class_Wide_Type (Typ) then
6822 Set_Etype (Defining_Identifier (D), Typ);
6823 end if;
6824
6825 end if;
6826
6827 D := Next_D;
6828 end loop;
6829 end;
6830
6831 End_Scope;
6832
6833 -- Replace the accept statement by the new block
6834
6835 Rewrite (N, Block);
6836 Analyze (N);
6837
6838 -- Last step is to unstack the Accept_Address value
6839
6840 Remove_Last_Elmt (Acstack);
6841 end if;
6842 end Expand_N_Accept_Statement;
6843
6844 ----------------------------------
6845 -- Expand_N_Asynchronous_Select --
6846 ----------------------------------
6847
6848 -- This procedure assumes that the trigger statement is an entry call or
6849 -- a dispatching procedure call. A delay alternative should already have
6850 -- been expanded into an entry call to the appropriate delay object Wait
6851 -- entry.
6852
6853 -- If the trigger is a task entry call, the select is implemented with
6854 -- a Task_Entry_Call:
6855
6856 -- declare
6857 -- B : Boolean;
6858 -- C : Boolean;
6859 -- P : parms := (parm, parm, parm);
6860
6861 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6862
6863 -- procedure _clean is
6864 -- begin
6865 -- ...
6866 -- Cancel_Task_Entry_Call (C);
6867 -- ...
6868 -- end _clean;
6869
6870 -- begin
6871 -- Abort_Defer;
6872 -- Task_Entry_Call
6873 -- (<acceptor-task>, -- Acceptor
6874 -- <entry-index>, -- E
6875 -- P'Address, -- Uninterpreted_Data
6876 -- Asynchronous_Call, -- Mode
6877 -- B); -- Rendezvous_Successful
6878
6879 -- begin
6880 -- begin
6881 -- Abort_Undefer;
6882 -- <abortable-part>
6883 -- at end
6884 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6885 -- end;
6886 -- exception
6887 -- when Abort_Signal => Abort_Undefer;
6888 -- end;
6889
6890 -- parm := P.param;
6891 -- parm := P.param;
6892 -- ...
6893 -- if not C then
6894 -- <triggered-statements>
6895 -- end if;
6896 -- end;
6897
6898 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6899 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6900 -- as follows:
6901
6902 -- declare
6903 -- P : parms := (parm, parm, parm);
6904 -- begin
6905 -- Call_Simple (acceptor-task, entry-index, P'Address);
6906 -- parm := P.param;
6907 -- parm := P.param;
6908 -- ...
6909 -- end;
6910
6911 -- so the task at hand is to convert the latter expansion into the former
6912
6913 -- If the trigger is a protected entry call, the select is implemented
6914 -- with Protected_Entry_Call:
6915
6916 -- declare
6917 -- P : E1_Params := (param, param, param);
6918 -- Bnn : Communications_Block;
6919
6920 -- begin
6921 -- declare
6922
6923 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6924
6925 -- procedure _clean is
6926 -- begin
6927 -- ...
6928 -- if Enqueued (Bnn) then
6929 -- Cancel_Protected_Entry_Call (Bnn);
6930 -- end if;
6931 -- ...
6932 -- end _clean;
6933
6934 -- begin
6935 -- begin
6936 -- Protected_Entry_Call
6937 -- (po._object'Access, -- Object
6938 -- <entry index>, -- E
6939 -- P'Address, -- Uninterpreted_Data
6940 -- Asynchronous_Call, -- Mode
6941 -- Bnn); -- Block
6942
6943 -- if Enqueued (Bnn) then
6944 -- <abortable-part>
6945 -- end if;
6946 -- at end
6947 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6948 -- end;
6949 -- exception
6950 -- when Abort_Signal => Abort_Undefer;
6951 -- end;
6952
6953 -- if not Cancelled (Bnn) then
6954 -- <triggered-statements>
6955 -- end if;
6956 -- end;
6957
6958 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6959 -- entry call:
6960
6961 -- declare
6962 -- P : E1_Params := (param, param, param);
6963 -- Bnn : Communications_Block;
6964
6965 -- begin
6966 -- Protected_Entry_Call
6967 -- (po._object'Access, -- Object
6968 -- <entry index>, -- E
6969 -- P'Address, -- Uninterpreted_Data
6970 -- Simple_Call, -- Mode
6971 -- Bnn); -- Block
6972 -- parm := P.param;
6973 -- parm := P.param;
6974 -- ...
6975 -- end;
6976
6977 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6978 -- expanded into:
6979
6980 -- declare
6981 -- B : Boolean := False;
6982 -- Bnn : Communication_Block;
6983 -- C : Ada.Tags.Prim_Op_Kind;
6984 -- D : System.Storage_Elements.Dummy_Communication_Block;
6985 -- K : Ada.Tags.Tagged_Kind :=
6986 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6987 -- P : Parameters := (Param1 .. ParamN);
6988 -- S : Integer;
6989 -- U : Boolean;
6990
6991 -- begin
6992 -- if K = Ada.Tags.TK_Limited_Tagged
6993 -- or else K = Ada.Tags.TK_Tagged
6994 -- then
6995 -- <dispatching-call>;
6996 -- <triggering-statements>;
6997
6998 -- else
6999 -- S :=
7000 -- Ada.Tags.Get_Offset_Index
7001 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7002
7003 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7004
7005 -- if C = POK_Protected_Entry then
7006 -- declare
7007 -- procedure _clean is
7008 -- begin
7009 -- if Enqueued (Bnn) then
7010 -- Cancel_Protected_Entry_Call (Bnn);
7011 -- end if;
7012 -- end _clean;
7013
7014 -- begin
7015 -- begin
7016 -- _Disp_Asynchronous_Select
7017 -- (<object>, S, P'Address, D, B);
7018 -- Bnn := Communication_Block (D);
7019
7020 -- Param1 := P.Param1;
7021 -- ...
7022 -- ParamN := P.ParamN;
7023
7024 -- if Enqueued (Bnn) then
7025 -- <abortable-statements>
7026 -- end if;
7027 -- at end
7028 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7029 -- end;
7030 -- exception
7031 -- when Abort_Signal => Abort_Undefer;
7032 -- end;
7033
7034 -- if not Cancelled (Bnn) then
7035 -- <triggering-statements>
7036 -- end if;
7037
7038 -- elsif C = POK_Task_Entry then
7039 -- declare
7040 -- procedure _clean is
7041 -- begin
7042 -- Cancel_Task_Entry_Call (U);
7043 -- end _clean;
7044
7045 -- begin
7046 -- Abort_Defer;
7047
7048 -- _Disp_Asynchronous_Select
7049 -- (<object>, S, P'Address, D, B);
7050 -- Bnn := Communication_Bloc (D);
7051
7052 -- Param1 := P.Param1;
7053 -- ...
7054 -- ParamN := P.ParamN;
7055
7056 -- begin
7057 -- begin
7058 -- Abort_Undefer;
7059 -- <abortable-statements>
7060 -- at end
7061 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7062 -- end;
7063 -- exception
7064 -- when Abort_Signal => Abort_Undefer;
7065 -- end;
7066
7067 -- if not U then
7068 -- <triggering-statements>
7069 -- end if;
7070 -- end;
7071
7072 -- else
7073 -- <dispatching-call>;
7074 -- <triggering-statements>
7075 -- end if;
7076 -- end if;
7077 -- end;
7078
7079 -- The job is to convert this to the asynchronous form
7080
7081 -- If the trigger is a delay statement, it will have been expanded into
7082 -- a call to one of the GNARL delay procedures. This routine will convert
7083 -- this into a protected entry call on a delay object and then continue
7084 -- processing as for a protected entry call trigger. This requires
7085 -- declaring a Delay_Block object and adding a pointer to this object to
7086 -- the parameter list of the delay procedure to form the parameter list of
7087 -- the entry call. This object is used by the runtime to queue the delay
7088 -- request.
7089
7090 -- For a description of the use of P and the assignments after the call,
7091 -- see Expand_N_Entry_Call_Statement.
7092
7093 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
7094 Loc : constant Source_Ptr := Sloc (N);
7095 Abrt : constant Node_Id := Abortable_Part (N);
7096 Trig : constant Node_Id := Triggering_Alternative (N);
7097
7098 Abort_Block_Ent : Entity_Id;
7099 Abortable_Block : Node_Id;
7100 Actuals : List_Id;
7101 Astats : List_Id;
7102 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
7103 Blk_Typ : Entity_Id;
7104 Call : Node_Id;
7105 Call_Ent : Entity_Id;
7106 Cancel_Param : Entity_Id;
7107 Cleanup_Block : Node_Id;
7108 Cleanup_Block_Ent : Entity_Id;
7109 Cleanup_Stmts : List_Id;
7110 Conc_Typ_Stmts : List_Id;
7111 Concval : Node_Id;
7112 Dblock_Ent : Entity_Id;
7113 Decl : Node_Id;
7114 Decls : List_Id;
7115 Ecall : Node_Id;
7116 Ename : Node_Id;
7117 Enqueue_Call : Node_Id;
7118 Formals : List_Id;
7119 Hdle : List_Id;
7120 Handler_Stmt : Node_Id;
7121 Index : Node_Id;
7122 Lim_Typ_Stmts : List_Id;
7123 N_Orig : Node_Id;
7124 Obj : Entity_Id;
7125 Param : Node_Id;
7126 Params : List_Id;
7127 Pdef : Entity_Id;
7128 ProtE_Stmts : List_Id;
7129 ProtP_Stmts : List_Id;
7130 Stmt : Node_Id;
7131 Stmts : List_Id;
7132 TaskE_Stmts : List_Id;
7133 Tstats : List_Id;
7134
7135 B : Entity_Id; -- Call status flag
7136 Bnn : Entity_Id; -- Communication block
7137 C : Entity_Id; -- Call kind
7138 K : Entity_Id; -- Tagged kind
7139 P : Entity_Id; -- Parameter block
7140 S : Entity_Id; -- Primitive operation slot
7141 T : Entity_Id; -- Additional status flag
7142
7143 procedure Rewrite_Abortable_Part;
7144 -- If the trigger is a dispatching call, the expansion inserts multiple
7145 -- copies of the abortable part. This is both inefficient, and may lead
7146 -- to duplicate definitions that the back-end will reject, when the
7147 -- abortable part includes loops. This procedure rewrites the abortable
7148 -- part into a call to a generated procedure.
7149
7150 ----------------------------
7151 -- Rewrite_Abortable_Part --
7152 ----------------------------
7153
7154 procedure Rewrite_Abortable_Part is
7155 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7156 Decl : Node_Id;
7157
7158 begin
7159 Decl :=
7160 Make_Subprogram_Body (Loc,
7161 Specification =>
7162 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
7163 Declarations => New_List,
7164 Handled_Statement_Sequence =>
7165 Make_Handled_Sequence_Of_Statements (Loc, Astats));
7166 Insert_Before (N, Decl);
7167 Analyze (Decl);
7168
7169 -- Rewrite abortable part into a call to this procedure.
7170
7171 Astats :=
7172 New_List (
7173 Make_Procedure_Call_Statement (Loc,
7174 Name => New_Occurrence_Of (Proc, Loc)));
7175 end Rewrite_Abortable_Part;
7176
7177 -- Start of processing for Expand_N_Asynchronous_Select
7178
7179 begin
7180 -- Asynchronous select is not supported on restricted runtimes. Don't
7181 -- try to expand.
7182
7183 if Restricted_Profile then
7184 return;
7185 end if;
7186
7187 Process_Statements_For_Controlled_Objects (Trig);
7188 Process_Statements_For_Controlled_Objects (Abrt);
7189
7190 Ecall := Triggering_Statement (Trig);
7191
7192 Ensure_Statement_Present (Sloc (Ecall), Trig);
7193
7194 -- Retrieve Astats and Tstats now because the finalization machinery may
7195 -- wrap them in blocks.
7196
7197 Astats := Statements (Abrt);
7198 Tstats := Statements (Trig);
7199
7200 -- The arguments in the call may require dynamic allocation, and the
7201 -- call statement may have been transformed into a block. The block
7202 -- may contain additional declarations for internal entities, and the
7203 -- original call is found by sequential search.
7204
7205 if Nkind (Ecall) = N_Block_Statement then
7206 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
7207 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
7208 N_Entry_Call_Statement)
7209 loop
7210 Next (Ecall);
7211 end loop;
7212 end if;
7213
7214 -- This is either a dispatching call or a delay statement used as a
7215 -- trigger which was expanded into a procedure call.
7216
7217 if Nkind (Ecall) = N_Procedure_Call_Statement then
7218 if Ada_Version >= Ada_2005
7219 and then
7220 (No (Original_Node (Ecall))
7221 or else not Nkind_In (Original_Node (Ecall),
7222 N_Delay_Relative_Statement,
7223 N_Delay_Until_Statement))
7224 then
7225 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
7226
7227 Rewrite_Abortable_Part;
7228 Decls := New_List;
7229 Stmts := New_List;
7230
7231 -- Call status flag processing, generate:
7232 -- B : Boolean := False;
7233
7234 B := Build_B (Loc, Decls);
7235
7236 -- Communication block processing, generate:
7237 -- Bnn : Communication_Block;
7238
7239 Bnn := Make_Temporary (Loc, 'B');
7240 Append_To (Decls,
7241 Make_Object_Declaration (Loc,
7242 Defining_Identifier => Bnn,
7243 Object_Definition =>
7244 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
7245
7246 -- Call kind processing, generate:
7247 -- C : Ada.Tags.Prim_Op_Kind;
7248
7249 C := Build_C (Loc, Decls);
7250
7251 -- Tagged kind processing, generate:
7252 -- K : Ada.Tags.Tagged_Kind :=
7253 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7254
7255 -- Dummy communication block, generate:
7256 -- D : Dummy_Communication_Block;
7257
7258 Append_To (Decls,
7259 Make_Object_Declaration (Loc,
7260 Defining_Identifier =>
7261 Make_Defining_Identifier (Loc, Name_uD),
7262 Object_Definition =>
7263 New_Occurrence_Of
7264 (RTE (RE_Dummy_Communication_Block), Loc)));
7265
7266 K := Build_K (Loc, Decls, Obj);
7267
7268 -- Parameter block processing
7269
7270 Blk_Typ := Build_Parameter_Block
7271 (Loc, Actuals, Formals, Decls);
7272 P := Parameter_Block_Pack
7273 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7274
7275 -- Dispatch table slot processing, generate:
7276 -- S : Integer;
7277
7278 S := Build_S (Loc, Decls);
7279
7280 -- Additional status flag processing, generate:
7281 -- Tnn : Boolean;
7282
7283 T := Make_Temporary (Loc, 'T');
7284 Append_To (Decls,
7285 Make_Object_Declaration (Loc,
7286 Defining_Identifier => T,
7287 Object_Definition =>
7288 New_Occurrence_Of (Standard_Boolean, Loc)));
7289
7290 ------------------------------
7291 -- Protected entry handling --
7292 ------------------------------
7293
7294 -- Generate:
7295 -- Param1 := P.Param1;
7296 -- ...
7297 -- ParamN := P.ParamN;
7298
7299 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7300
7301 -- Generate:
7302 -- Bnn := Communication_Block (D);
7303
7304 Prepend_To (Cleanup_Stmts,
7305 Make_Assignment_Statement (Loc,
7306 Name => New_Occurrence_Of (Bnn, Loc),
7307 Expression =>
7308 Make_Unchecked_Type_Conversion (Loc,
7309 Subtype_Mark =>
7310 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7311 Expression => Make_Identifier (Loc, Name_uD))));
7312
7313 -- Generate:
7314 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7315
7316 Prepend_To (Cleanup_Stmts,
7317 Make_Procedure_Call_Statement (Loc,
7318 Name =>
7319 New_Occurrence_Of
7320 (Find_Prim_Op
7321 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7322 Loc),
7323 Parameter_Associations =>
7324 New_List (
7325 New_Copy_Tree (Obj), -- <object>
7326 New_Occurrence_Of (S, Loc), -- S
7327 Make_Attribute_Reference (Loc, -- P'Address
7328 Prefix => New_Occurrence_Of (P, Loc),
7329 Attribute_Name => Name_Address),
7330 Make_Identifier (Loc, Name_uD), -- D
7331 New_Occurrence_Of (B, Loc)))); -- B
7332
7333 -- Generate:
7334 -- if Enqueued (Bnn) then
7335 -- <abortable-statements>
7336 -- end if;
7337
7338 Append_To (Cleanup_Stmts,
7339 Make_Implicit_If_Statement (N,
7340 Condition =>
7341 Make_Function_Call (Loc,
7342 Name =>
7343 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7344 Parameter_Associations =>
7345 New_List (New_Occurrence_Of (Bnn, Loc))),
7346
7347 Then_Statements =>
7348 New_Copy_List_Tree (Astats)));
7349
7350 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7351 -- will then generate a _clean for the communication block Bnn.
7352
7353 -- Generate:
7354 -- declare
7355 -- procedure _clean is
7356 -- begin
7357 -- if Enqueued (Bnn) then
7358 -- Cancel_Protected_Entry_Call (Bnn);
7359 -- end if;
7360 -- end _clean;
7361 -- begin
7362 -- Cleanup_Stmts
7363 -- at end
7364 -- _clean;
7365 -- end;
7366
7367 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7368 Cleanup_Block :=
7369 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7370
7371 -- Wrap the cleanup block in an exception handling block
7372
7373 -- Generate:
7374 -- begin
7375 -- Cleanup_Block
7376 -- exception
7377 -- when Abort_Signal => Abort_Undefer;
7378 -- end;
7379
7380 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7381 ProtE_Stmts :=
7382 New_List (
7383 Make_Implicit_Label_Declaration (Loc,
7384 Defining_Identifier => Abort_Block_Ent),
7385
7386 Build_Abort_Block
7387 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7388
7389 -- Generate:
7390 -- if not Cancelled (Bnn) then
7391 -- <triggering-statements>
7392 -- end if;
7393
7394 Append_To (ProtE_Stmts,
7395 Make_Implicit_If_Statement (N,
7396 Condition =>
7397 Make_Op_Not (Loc,
7398 Right_Opnd =>
7399 Make_Function_Call (Loc,
7400 Name =>
7401 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7402 Parameter_Associations =>
7403 New_List (New_Occurrence_Of (Bnn, Loc)))),
7404
7405 Then_Statements =>
7406 New_Copy_List_Tree (Tstats)));
7407
7408 -------------------------
7409 -- Task entry handling --
7410 -------------------------
7411
7412 -- Generate:
7413 -- Param1 := P.Param1;
7414 -- ...
7415 -- ParamN := P.ParamN;
7416
7417 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7418
7419 -- Generate:
7420 -- Bnn := Communication_Block (D);
7421
7422 Append_To (TaskE_Stmts,
7423 Make_Assignment_Statement (Loc,
7424 Name =>
7425 New_Occurrence_Of (Bnn, Loc),
7426 Expression =>
7427 Make_Unchecked_Type_Conversion (Loc,
7428 Subtype_Mark =>
7429 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7430 Expression => Make_Identifier (Loc, Name_uD))));
7431
7432 -- Generate:
7433 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7434
7435 Prepend_To (TaskE_Stmts,
7436 Make_Procedure_Call_Statement (Loc,
7437 Name =>
7438 New_Occurrence_Of (
7439 Find_Prim_Op (Etype (Etype (Obj)),
7440 Name_uDisp_Asynchronous_Select),
7441 Loc),
7442
7443 Parameter_Associations => New_List (
7444 New_Copy_Tree (Obj), -- <object>
7445 New_Occurrence_Of (S, Loc), -- S
7446 Make_Attribute_Reference (Loc, -- P'Address
7447 Prefix => New_Occurrence_Of (P, Loc),
7448 Attribute_Name => Name_Address),
7449 Make_Identifier (Loc, Name_uD), -- D
7450 New_Occurrence_Of (B, Loc)))); -- B
7451
7452 -- Generate:
7453 -- Abort_Defer;
7454
7455 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7456
7457 -- Generate:
7458 -- Abort_Undefer;
7459 -- <abortable-statements>
7460
7461 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7462
7463 Prepend_To
7464 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7465
7466 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7467 -- will generate a _clean for the additional status flag.
7468
7469 -- Generate:
7470 -- declare
7471 -- procedure _clean is
7472 -- begin
7473 -- Cancel_Task_Entry_Call (U);
7474 -- end _clean;
7475 -- begin
7476 -- Cleanup_Stmts
7477 -- at end
7478 -- _clean;
7479 -- end;
7480
7481 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7482 Cleanup_Block :=
7483 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7484
7485 -- Wrap the cleanup block in an exception handling block
7486
7487 -- Generate:
7488 -- begin
7489 -- Cleanup_Block
7490 -- exception
7491 -- when Abort_Signal => Abort_Undefer;
7492 -- end;
7493
7494 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7495
7496 Append_To (TaskE_Stmts,
7497 Make_Implicit_Label_Declaration (Loc,
7498 Defining_Identifier => Abort_Block_Ent));
7499
7500 Append_To (TaskE_Stmts,
7501 Build_Abort_Block
7502 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7503
7504 -- Generate:
7505 -- if not T then
7506 -- <triggering-statements>
7507 -- end if;
7508
7509 Append_To (TaskE_Stmts,
7510 Make_Implicit_If_Statement (N,
7511 Condition =>
7512 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7513
7514 Then_Statements =>
7515 New_Copy_List_Tree (Tstats)));
7516
7517 ----------------------------------
7518 -- Protected procedure handling --
7519 ----------------------------------
7520
7521 -- Generate:
7522 -- <dispatching-call>;
7523 -- <triggering-statements>
7524
7525 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7526 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7527
7528 -- Generate:
7529 -- S := Ada.Tags.Get_Offset_Index
7530 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7531
7532 Conc_Typ_Stmts :=
7533 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7534
7535 -- Generate:
7536 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7537
7538 Append_To (Conc_Typ_Stmts,
7539 Make_Procedure_Call_Statement (Loc,
7540 Name =>
7541 New_Occurrence_Of
7542 (Find_Prim_Op (Etype (Etype (Obj)),
7543 Name_uDisp_Get_Prim_Op_Kind),
7544 Loc),
7545 Parameter_Associations =>
7546 New_List (
7547 New_Copy_Tree (Obj),
7548 New_Occurrence_Of (S, Loc),
7549 New_Occurrence_Of (C, Loc))));
7550
7551 -- Generate:
7552 -- if C = POK_Procedure_Entry then
7553 -- ProtE_Stmts
7554 -- elsif C = POK_Task_Entry then
7555 -- TaskE_Stmts
7556 -- else
7557 -- ProtP_Stmts
7558 -- end if;
7559
7560 Append_To (Conc_Typ_Stmts,
7561 Make_Implicit_If_Statement (N,
7562 Condition =>
7563 Make_Op_Eq (Loc,
7564 Left_Opnd =>
7565 New_Occurrence_Of (C, Loc),
7566 Right_Opnd =>
7567 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7568
7569 Then_Statements =>
7570 ProtE_Stmts,
7571
7572 Elsif_Parts =>
7573 New_List (
7574 Make_Elsif_Part (Loc,
7575 Condition =>
7576 Make_Op_Eq (Loc,
7577 Left_Opnd =>
7578 New_Occurrence_Of (C, Loc),
7579 Right_Opnd =>
7580 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7581
7582 Then_Statements =>
7583 TaskE_Stmts)),
7584
7585 Else_Statements =>
7586 ProtP_Stmts));
7587
7588 -- Generate:
7589 -- <dispatching-call>;
7590 -- <triggering-statements>
7591
7592 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7593 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7594
7595 -- Generate:
7596 -- if K = Ada.Tags.TK_Limited_Tagged
7597 -- or else K = Ada.Tags.TK_Tagged
7598 -- then
7599 -- Lim_Typ_Stmts
7600 -- else
7601 -- Conc_Typ_Stmts
7602 -- end if;
7603
7604 Append_To (Stmts,
7605 Make_Implicit_If_Statement (N,
7606 Condition => Build_Dispatching_Tag_Check (K, N),
7607 Then_Statements => Lim_Typ_Stmts,
7608 Else_Statements => Conc_Typ_Stmts));
7609
7610 Rewrite (N,
7611 Make_Block_Statement (Loc,
7612 Declarations =>
7613 Decls,
7614 Handled_Statement_Sequence =>
7615 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7616
7617 Analyze (N);
7618 return;
7619
7620 -- Delay triggering statement processing
7621
7622 else
7623 -- Add a Delay_Block object to the parameter list of the delay
7624 -- procedure to form the parameter list of the Wait entry call.
7625
7626 Dblock_Ent := Make_Temporary (Loc, 'D');
7627
7628 Pdef := Entity (Name (Ecall));
7629
7630 if Is_RTE (Pdef, RO_CA_Delay_For) then
7631 Enqueue_Call :=
7632 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7633
7634 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7635 Enqueue_Call :=
7636 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7637
7638 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7639 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7640 end if;
7641
7642 Append_To (Parameter_Associations (Ecall),
7643 Make_Attribute_Reference (Loc,
7644 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7645 Attribute_Name => Name_Unchecked_Access));
7646
7647 -- Create the inner block to protect the abortable part
7648
7649 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7650
7651 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7652
7653 Abortable_Block :=
7654 Make_Block_Statement (Loc,
7655 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7656 Handled_Statement_Sequence =>
7657 Make_Handled_Sequence_Of_Statements (Loc,
7658 Statements => Astats),
7659 Has_Created_Identifier => True,
7660 Is_Asynchronous_Call_Block => True);
7661
7662 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7663
7664 Rewrite (Ecall,
7665 Make_Implicit_If_Statement (N,
7666 Condition =>
7667 Make_Function_Call (Loc,
7668 Name => Enqueue_Call,
7669 Parameter_Associations => Parameter_Associations (Ecall)),
7670 Then_Statements =>
7671 New_List (Make_Block_Statement (Loc,
7672 Handled_Statement_Sequence =>
7673 Make_Handled_Sequence_Of_Statements (Loc,
7674 Statements => New_List (
7675 Make_Implicit_Label_Declaration (Loc,
7676 Defining_Identifier => Blk_Ent,
7677 Label_Construct => Abortable_Block),
7678 Abortable_Block),
7679 Exception_Handlers => Hdle)))));
7680
7681 Stmts := New_List (Ecall);
7682
7683 -- Construct statement sequence for new block
7684
7685 Append_To (Stmts,
7686 Make_Implicit_If_Statement (N,
7687 Condition =>
7688 Make_Function_Call (Loc,
7689 Name => New_Occurrence_Of (
7690 RTE (RE_Timed_Out), Loc),
7691 Parameter_Associations => New_List (
7692 Make_Attribute_Reference (Loc,
7693 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7694 Attribute_Name => Name_Unchecked_Access))),
7695 Then_Statements => Tstats));
7696
7697 -- The result is the new block
7698
7699 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7700
7701 Rewrite (N,
7702 Make_Block_Statement (Loc,
7703 Declarations => New_List (
7704 Make_Object_Declaration (Loc,
7705 Defining_Identifier => Dblock_Ent,
7706 Aliased_Present => True,
7707 Object_Definition =>
7708 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7709
7710 Handled_Statement_Sequence =>
7711 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7712
7713 Analyze (N);
7714 return;
7715 end if;
7716
7717 else
7718 N_Orig := N;
7719 end if;
7720
7721 Extract_Entry (Ecall, Concval, Ename, Index);
7722 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7723
7724 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7725 Decls := Declarations (Ecall);
7726
7727 if Is_Protected_Type (Etype (Concval)) then
7728
7729 -- Get the declarations of the block expanded from the entry call
7730
7731 Decl := First (Decls);
7732 while Present (Decl)
7733 and then (Nkind (Decl) /= N_Object_Declaration
7734 or else not Is_RTE (Etype (Object_Definition (Decl)),
7735 RE_Communication_Block))
7736 loop
7737 Next (Decl);
7738 end loop;
7739
7740 pragma Assert (Present (Decl));
7741 Cancel_Param := Defining_Identifier (Decl);
7742
7743 -- Change the mode of the Protected_Entry_Call call
7744
7745 -- Protected_Entry_Call (
7746 -- Object => po._object'Access,
7747 -- E => <entry index>;
7748 -- Uninterpreted_Data => P'Address;
7749 -- Mode => Asynchronous_Call;
7750 -- Block => Bnn);
7751
7752 -- Skip assignments to temporaries created for in-out parameters
7753
7754 -- This makes unwarranted assumptions about the shape of the expanded
7755 -- tree for the call, and should be cleaned up ???
7756
7757 Stmt := First (Stmts);
7758 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7759 Next (Stmt);
7760 end loop;
7761
7762 Call := Stmt;
7763
7764 Param := First (Parameter_Associations (Call));
7765 while Present (Param)
7766 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7767 loop
7768 Next (Param);
7769 end loop;
7770
7771 pragma Assert (Present (Param));
7772 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7773 Analyze (Param);
7774
7775 -- Append an if statement to execute the abortable part
7776
7777 -- Generate:
7778 -- if Enqueued (Bnn) then
7779
7780 Append_To (Stmts,
7781 Make_Implicit_If_Statement (N,
7782 Condition =>
7783 Make_Function_Call (Loc,
7784 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7785 Parameter_Associations => New_List (
7786 New_Occurrence_Of (Cancel_Param, Loc))),
7787 Then_Statements => Astats));
7788
7789 Abortable_Block :=
7790 Make_Block_Statement (Loc,
7791 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7792 Handled_Statement_Sequence =>
7793 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7794 Has_Created_Identifier => True,
7795 Is_Asynchronous_Call_Block => True);
7796
7797 -- Aborts are not deferred at beginning of exception handlers in
7798 -- ZCX mode.
7799
7800 if ZCX_Exceptions then
7801 Handler_Stmt := Make_Null_Statement (Loc);
7802
7803 else
7804 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7805 end if;
7806
7807 Stmts := New_List (
7808 Make_Block_Statement (Loc,
7809 Handled_Statement_Sequence =>
7810 Make_Handled_Sequence_Of_Statements (Loc,
7811 Statements => New_List (
7812 Make_Implicit_Label_Declaration (Loc,
7813 Defining_Identifier => Blk_Ent,
7814 Label_Construct => Abortable_Block),
7815 Abortable_Block),
7816
7817 -- exception
7818
7819 Exception_Handlers => New_List (
7820 Make_Implicit_Exception_Handler (Loc,
7821
7822 -- when Abort_Signal =>
7823 -- Abort_Undefer.all;
7824
7825 Exception_Choices =>
7826 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7827 Statements => New_List (Handler_Stmt))))),
7828
7829 -- if not Cancelled (Bnn) then
7830 -- triggered statements
7831 -- end if;
7832
7833 Make_Implicit_If_Statement (N,
7834 Condition => Make_Op_Not (Loc,
7835 Right_Opnd =>
7836 Make_Function_Call (Loc,
7837 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7838 Parameter_Associations => New_List (
7839 New_Occurrence_Of (Cancel_Param, Loc)))),
7840 Then_Statements => Tstats));
7841
7842 -- Asynchronous task entry call
7843
7844 else
7845 if No (Decls) then
7846 Decls := New_List;
7847 end if;
7848
7849 B := Make_Defining_Identifier (Loc, Name_uB);
7850
7851 -- Insert declaration of B in declarations of existing block
7852
7853 Prepend_To (Decls,
7854 Make_Object_Declaration (Loc,
7855 Defining_Identifier => B,
7856 Object_Definition =>
7857 New_Occurrence_Of (Standard_Boolean, Loc)));
7858
7859 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7860
7861 -- Insert declaration of C in declarations of existing block
7862
7863 Prepend_To (Decls,
7864 Make_Object_Declaration (Loc,
7865 Defining_Identifier => Cancel_Param,
7866 Object_Definition =>
7867 New_Occurrence_Of (Standard_Boolean, Loc)));
7868
7869 -- Remove and save the call to Call_Simple
7870
7871 Stmt := First (Stmts);
7872
7873 -- Skip assignments to temporaries created for in-out parameters.
7874 -- This makes unwarranted assumptions about the shape of the expanded
7875 -- tree for the call, and should be cleaned up ???
7876
7877 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7878 Next (Stmt);
7879 end loop;
7880
7881 Call := Stmt;
7882
7883 -- Create the inner block to protect the abortable part
7884
7885 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7886
7887 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7888
7889 Abortable_Block :=
7890 Make_Block_Statement (Loc,
7891 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7892 Handled_Statement_Sequence =>
7893 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7894 Has_Created_Identifier => True,
7895 Is_Asynchronous_Call_Block => True);
7896
7897 Insert_After (Call,
7898 Make_Block_Statement (Loc,
7899 Handled_Statement_Sequence =>
7900 Make_Handled_Sequence_Of_Statements (Loc,
7901 Statements => New_List (
7902 Make_Implicit_Label_Declaration (Loc,
7903 Defining_Identifier => Blk_Ent,
7904 Label_Construct => Abortable_Block),
7905 Abortable_Block),
7906 Exception_Handlers => Hdle)));
7907
7908 -- Create new call statement
7909
7910 Params := Parameter_Associations (Call);
7911
7912 Append_To (Params,
7913 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7914 Append_To (Params, New_Occurrence_Of (B, Loc));
7915
7916 Rewrite (Call,
7917 Make_Procedure_Call_Statement (Loc,
7918 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7919 Parameter_Associations => Params));
7920
7921 -- Construct statement sequence for new block
7922
7923 Append_To (Stmts,
7924 Make_Implicit_If_Statement (N,
7925 Condition =>
7926 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7927 Then_Statements => Tstats));
7928
7929 -- Protected the call against abort
7930
7931 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7932 end if;
7933
7934 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7935
7936 -- The result is the new block
7937
7938 Rewrite (N_Orig,
7939 Make_Block_Statement (Loc,
7940 Declarations => Decls,
7941 Handled_Statement_Sequence =>
7942 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7943
7944 Analyze (N_Orig);
7945 end Expand_N_Asynchronous_Select;
7946
7947 -------------------------------------
7948 -- Expand_N_Conditional_Entry_Call --
7949 -------------------------------------
7950
7951 -- The conditional task entry call is converted to a call to
7952 -- Task_Entry_Call:
7953
7954 -- declare
7955 -- B : Boolean;
7956 -- P : parms := (parm, parm, parm);
7957
7958 -- begin
7959 -- Task_Entry_Call
7960 -- (<acceptor-task>, -- Acceptor
7961 -- <entry-index>, -- E
7962 -- P'Address, -- Uninterpreted_Data
7963 -- Conditional_Call, -- Mode
7964 -- B); -- Rendezvous_Successful
7965 -- parm := P.param;
7966 -- parm := P.param;
7967 -- ...
7968 -- if B then
7969 -- normal-statements
7970 -- else
7971 -- else-statements
7972 -- end if;
7973 -- end;
7974
7975 -- For a description of the use of P and the assignments after the call,
7976 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7977 -- conditional entry call has already been expanded (by the Expand_N_Entry
7978 -- _Call_Statement procedure) as follows:
7979
7980 -- declare
7981 -- P : parms := (parm, parm, parm);
7982 -- begin
7983 -- ... info for in-out parameters
7984 -- Call_Simple (acceptor-task, entry-index, P'Address);
7985 -- parm := P.param;
7986 -- parm := P.param;
7987 -- ...
7988 -- end;
7989
7990 -- so the task at hand is to convert the latter expansion into the former
7991
7992 -- The conditional protected entry call is converted to a call to
7993 -- Protected_Entry_Call:
7994
7995 -- declare
7996 -- P : parms := (parm, parm, parm);
7997 -- Bnn : Communications_Block;
7998
7999 -- begin
8000 -- Protected_Entry_Call
8001 -- (po._object'Access, -- Object
8002 -- <entry index>, -- E
8003 -- P'Address, -- Uninterpreted_Data
8004 -- Conditional_Call, -- Mode
8005 -- Bnn); -- Block
8006 -- parm := P.param;
8007 -- parm := P.param;
8008 -- ...
8009 -- if Cancelled (Bnn) then
8010 -- else-statements
8011 -- else
8012 -- normal-statements
8013 -- end if;
8014 -- end;
8015
8016 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
8017 -- into:
8018
8019 -- declare
8020 -- B : Boolean := False;
8021 -- C : Ada.Tags.Prim_Op_Kind;
8022 -- K : Ada.Tags.Tagged_Kind :=
8023 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8024 -- P : Parameters := (Param1 .. ParamN);
8025 -- S : Integer;
8026
8027 -- begin
8028 -- if K = Ada.Tags.TK_Limited_Tagged
8029 -- or else K = Ada.Tags.TK_Tagged
8030 -- then
8031 -- <dispatching-call>;
8032 -- <triggering-statements>
8033
8034 -- else
8035 -- S :=
8036 -- Ada.Tags.Get_Offset_Index
8037 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
8038
8039 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8040
8041 -- if C = POK_Protected_Entry
8042 -- or else C = POK_Task_Entry
8043 -- then
8044 -- Param1 := P.Param1;
8045 -- ...
8046 -- ParamN := P.ParamN;
8047 -- end if;
8048
8049 -- if B then
8050 -- if C = POK_Procedure
8051 -- or else C = POK_Protected_Procedure
8052 -- or else C = POK_Task_Procedure
8053 -- then
8054 -- <dispatching-call>;
8055 -- end if;
8056
8057 -- <triggering-statements>
8058 -- else
8059 -- <else-statements>
8060 -- end if;
8061 -- end if;
8062 -- end;
8063
8064 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
8065 Loc : constant Source_Ptr := Sloc (N);
8066 Alt : constant Node_Id := Entry_Call_Alternative (N);
8067 Blk : Node_Id := Entry_Call_Statement (Alt);
8068
8069 Actuals : List_Id;
8070 Blk_Typ : Entity_Id;
8071 Call : Node_Id;
8072 Call_Ent : Entity_Id;
8073 Conc_Typ_Stmts : List_Id;
8074 Decl : Node_Id;
8075 Decls : List_Id;
8076 Formals : List_Id;
8077 Lim_Typ_Stmts : List_Id;
8078 N_Stats : List_Id;
8079 Obj : Entity_Id;
8080 Param : Node_Id;
8081 Params : List_Id;
8082 Stmt : Node_Id;
8083 Stmts : List_Id;
8084 Transient_Blk : Node_Id;
8085 Unpack : List_Id;
8086
8087 B : Entity_Id; -- Call status flag
8088 C : Entity_Id; -- Call kind
8089 K : Entity_Id; -- Tagged kind
8090 P : Entity_Id; -- Parameter block
8091 S : Entity_Id; -- Primitive operation slot
8092
8093 begin
8094 Process_Statements_For_Controlled_Objects (N);
8095
8096 if Ada_Version >= Ada_2005
8097 and then Nkind (Blk) = N_Procedure_Call_Statement
8098 then
8099 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
8100
8101 Decls := New_List;
8102 Stmts := New_List;
8103
8104 -- Call status flag processing, generate:
8105 -- B : Boolean := False;
8106
8107 B := Build_B (Loc, Decls);
8108
8109 -- Call kind processing, generate:
8110 -- C : Ada.Tags.Prim_Op_Kind;
8111
8112 C := Build_C (Loc, Decls);
8113
8114 -- Tagged kind processing, generate:
8115 -- K : Ada.Tags.Tagged_Kind :=
8116 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8117
8118 K := Build_K (Loc, Decls, Obj);
8119
8120 -- Parameter block processing
8121
8122 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
8123 P := Parameter_Block_Pack
8124 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
8125
8126 -- Dispatch table slot processing, generate:
8127 -- S : Integer;
8128
8129 S := Build_S (Loc, Decls);
8130
8131 -- Generate:
8132 -- S := Ada.Tags.Get_Offset_Index
8133 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
8134
8135 Conc_Typ_Stmts :=
8136 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
8137
8138 -- Generate:
8139 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8140
8141 Append_To (Conc_Typ_Stmts,
8142 Make_Procedure_Call_Statement (Loc,
8143 Name =>
8144 New_Occurrence_Of (
8145 Find_Prim_Op (Etype (Etype (Obj)),
8146 Name_uDisp_Conditional_Select),
8147 Loc),
8148 Parameter_Associations =>
8149 New_List (
8150 New_Copy_Tree (Obj), -- <object>
8151 New_Occurrence_Of (S, Loc), -- S
8152 Make_Attribute_Reference (Loc, -- P'Address
8153 Prefix => New_Occurrence_Of (P, Loc),
8154 Attribute_Name => Name_Address),
8155 New_Occurrence_Of (C, Loc), -- C
8156 New_Occurrence_Of (B, Loc)))); -- B
8157
8158 -- Generate:
8159 -- if C = POK_Protected_Entry
8160 -- or else C = POK_Task_Entry
8161 -- then
8162 -- Param1 := P.Param1;
8163 -- ...
8164 -- ParamN := P.ParamN;
8165 -- end if;
8166
8167 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
8168
8169 -- Generate the if statement only when the packed parameters need
8170 -- explicit assignments to their corresponding actuals.
8171
8172 if Present (Unpack) then
8173 Append_To (Conc_Typ_Stmts,
8174 Make_Implicit_If_Statement (N,
8175 Condition =>
8176 Make_Or_Else (Loc,
8177 Left_Opnd =>
8178 Make_Op_Eq (Loc,
8179 Left_Opnd =>
8180 New_Occurrence_Of (C, Loc),
8181 Right_Opnd =>
8182 New_Occurrence_Of (RTE (
8183 RE_POK_Protected_Entry), Loc)),
8184
8185 Right_Opnd =>
8186 Make_Op_Eq (Loc,
8187 Left_Opnd =>
8188 New_Occurrence_Of (C, Loc),
8189 Right_Opnd =>
8190 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
8191
8192 Then_Statements => Unpack));
8193 end if;
8194
8195 -- Generate:
8196 -- if B then
8197 -- if C = POK_Procedure
8198 -- or else C = POK_Protected_Procedure
8199 -- or else C = POK_Task_Procedure
8200 -- then
8201 -- <dispatching-call>
8202 -- end if;
8203 -- <normal-statements>
8204 -- else
8205 -- <else-statements>
8206 -- end if;
8207
8208 N_Stats := New_Copy_List_Tree (Statements (Alt));
8209
8210 Prepend_To (N_Stats,
8211 Make_Implicit_If_Statement (N,
8212 Condition =>
8213 Make_Or_Else (Loc,
8214 Left_Opnd =>
8215 Make_Op_Eq (Loc,
8216 Left_Opnd =>
8217 New_Occurrence_Of (C, Loc),
8218 Right_Opnd =>
8219 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
8220
8221 Right_Opnd =>
8222 Make_Or_Else (Loc,
8223 Left_Opnd =>
8224 Make_Op_Eq (Loc,
8225 Left_Opnd =>
8226 New_Occurrence_Of (C, Loc),
8227 Right_Opnd =>
8228 New_Occurrence_Of (RTE (
8229 RE_POK_Protected_Procedure), Loc)),
8230
8231 Right_Opnd =>
8232 Make_Op_Eq (Loc,
8233 Left_Opnd =>
8234 New_Occurrence_Of (C, Loc),
8235 Right_Opnd =>
8236 New_Occurrence_Of (RTE (
8237 RE_POK_Task_Procedure), Loc)))),
8238
8239 Then_Statements =>
8240 New_List (Blk)));
8241
8242 Append_To (Conc_Typ_Stmts,
8243 Make_Implicit_If_Statement (N,
8244 Condition => New_Occurrence_Of (B, Loc),
8245 Then_Statements => N_Stats,
8246 Else_Statements => Else_Statements (N)));
8247
8248 -- Generate:
8249 -- <dispatching-call>;
8250 -- <triggering-statements>
8251
8252 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8253 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8254
8255 -- Generate:
8256 -- if K = Ada.Tags.TK_Limited_Tagged
8257 -- or else K = Ada.Tags.TK_Tagged
8258 -- then
8259 -- Lim_Typ_Stmts
8260 -- else
8261 -- Conc_Typ_Stmts
8262 -- end if;
8263
8264 Append_To (Stmts,
8265 Make_Implicit_If_Statement (N,
8266 Condition => Build_Dispatching_Tag_Check (K, N),
8267 Then_Statements => Lim_Typ_Stmts,
8268 Else_Statements => Conc_Typ_Stmts));
8269
8270 Rewrite (N,
8271 Make_Block_Statement (Loc,
8272 Declarations =>
8273 Decls,
8274 Handled_Statement_Sequence =>
8275 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8276
8277 -- As described above, the entry alternative is transformed into a
8278 -- block that contains the gnulli call, and possibly assignment
8279 -- statements for in-out parameters. The gnulli call may itself be
8280 -- rewritten into a transient block if some unconstrained parameters
8281 -- require it. We need to retrieve the call to complete its parameter
8282 -- list.
8283
8284 else
8285 Transient_Blk :=
8286 First_Real_Statement (Handled_Statement_Sequence (Blk));
8287
8288 if Present (Transient_Blk)
8289 and then Nkind (Transient_Blk) = N_Block_Statement
8290 then
8291 Blk := Transient_Blk;
8292 end if;
8293
8294 Stmts := Statements (Handled_Statement_Sequence (Blk));
8295 Stmt := First (Stmts);
8296 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8297 Next (Stmt);
8298 end loop;
8299
8300 Call := Stmt;
8301 Params := Parameter_Associations (Call);
8302
8303 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8304
8305 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8306
8307 Param := First (Params);
8308 while Present (Param)
8309 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8310 loop
8311 Next (Param);
8312 end loop;
8313
8314 pragma Assert (Present (Param));
8315 Rewrite (Param,
8316 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8317
8318 Analyze (Param);
8319
8320 -- Find the Communication_Block parameter for the call to the
8321 -- Cancelled function.
8322
8323 Decl := First (Declarations (Blk));
8324 while Present (Decl)
8325 and then not Is_RTE (Etype (Object_Definition (Decl)),
8326 RE_Communication_Block)
8327 loop
8328 Next (Decl);
8329 end loop;
8330
8331 -- Add an if statement to execute the else part if the call
8332 -- does not succeed (as indicated by the Cancelled predicate).
8333
8334 Append_To (Stmts,
8335 Make_Implicit_If_Statement (N,
8336 Condition => Make_Function_Call (Loc,
8337 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8338 Parameter_Associations => New_List (
8339 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8340 Then_Statements => Else_Statements (N),
8341 Else_Statements => Statements (Alt)));
8342
8343 else
8344 B := Make_Defining_Identifier (Loc, Name_uB);
8345
8346 -- Insert declaration of B in declarations of existing block
8347
8348 if No (Declarations (Blk)) then
8349 Set_Declarations (Blk, New_List);
8350 end if;
8351
8352 Prepend_To (Declarations (Blk),
8353 Make_Object_Declaration (Loc,
8354 Defining_Identifier => B,
8355 Object_Definition =>
8356 New_Occurrence_Of (Standard_Boolean, Loc)));
8357
8358 -- Create new call statement
8359
8360 Append_To (Params,
8361 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8362 Append_To (Params, New_Occurrence_Of (B, Loc));
8363
8364 Rewrite (Call,
8365 Make_Procedure_Call_Statement (Loc,
8366 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8367 Parameter_Associations => Params));
8368
8369 -- Construct statement sequence for new block
8370
8371 Append_To (Stmts,
8372 Make_Implicit_If_Statement (N,
8373 Condition => New_Occurrence_Of (B, Loc),
8374 Then_Statements => Statements (Alt),
8375 Else_Statements => Else_Statements (N)));
8376 end if;
8377
8378 -- The result is the new block
8379
8380 Rewrite (N,
8381 Make_Block_Statement (Loc,
8382 Declarations => Declarations (Blk),
8383 Handled_Statement_Sequence =>
8384 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8385 end if;
8386
8387 Analyze (N);
8388 end Expand_N_Conditional_Entry_Call;
8389
8390 ---------------------------------------
8391 -- Expand_N_Delay_Relative_Statement --
8392 ---------------------------------------
8393
8394 -- Delay statement is implemented as a procedure call to Delay_For
8395 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8396 -- simple delays imposed by the use of Protected Objects.
8397
8398 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8399 Loc : constant Source_Ptr := Sloc (N);
8400 Proc : Entity_Id;
8401
8402 begin
8403 -- Try to use System.Relative_Delays.Delay_For only if available. This
8404 -- is the implementation used on restricted platforms when Ada.Calendar
8405 -- is not available.
8406
8407 if RTE_Available (RO_RD_Delay_For) then
8408 Proc := RTE (RO_RD_Delay_For);
8409
8410 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8411 -- message if not available.
8412
8413 else
8414 Proc := RTE (RO_CA_Delay_For);
8415 end if;
8416
8417 Rewrite (N,
8418 Make_Procedure_Call_Statement (Loc,
8419 Name => New_Occurrence_Of (Proc, Loc),
8420 Parameter_Associations => New_List (Expression (N))));
8421 Analyze (N);
8422 end Expand_N_Delay_Relative_Statement;
8423
8424 ------------------------------------
8425 -- Expand_N_Delay_Until_Statement --
8426 ------------------------------------
8427
8428 -- Delay Until statement is implemented as a procedure call to
8429 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8430
8431 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8432 Loc : constant Source_Ptr := Sloc (N);
8433 Typ : Entity_Id;
8434
8435 begin
8436 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8437 Typ := RTE (RO_CA_Delay_Until);
8438 else
8439 Typ := RTE (RO_RT_Delay_Until);
8440 end if;
8441
8442 Rewrite (N,
8443 Make_Procedure_Call_Statement (Loc,
8444 Name => New_Occurrence_Of (Typ, Loc),
8445 Parameter_Associations => New_List (Expression (N))));
8446
8447 Analyze (N);
8448 end Expand_N_Delay_Until_Statement;
8449
8450 -------------------------
8451 -- Expand_N_Entry_Body --
8452 -------------------------
8453
8454 procedure Expand_N_Entry_Body (N : Node_Id) is
8455 begin
8456 -- Associate discriminals with the next protected operation body to be
8457 -- expanded.
8458
8459 if Present (Next_Protected_Operation (N)) then
8460 Set_Discriminals (Parent (Current_Scope));
8461 end if;
8462 end Expand_N_Entry_Body;
8463
8464 -----------------------------------
8465 -- Expand_N_Entry_Call_Statement --
8466 -----------------------------------
8467
8468 -- An entry call is expanded into GNARLI calls to implement a simple entry
8469 -- call (see Build_Simple_Entry_Call).
8470
8471 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8472 Concval : Node_Id;
8473 Ename : Node_Id;
8474 Index : Node_Id;
8475
8476 begin
8477 if No_Run_Time_Mode then
8478 Error_Msg_CRT ("entry call", N);
8479 return;
8480 end if;
8481
8482 -- If this entry call is part of an asynchronous select, don't expand it
8483 -- here; it will be expanded with the select statement. Don't expand
8484 -- timed entry calls either, as they are translated into asynchronous
8485 -- entry calls.
8486
8487 -- ??? This whole approach is questionable; it may be better to go back
8488 -- to allowing the expansion to take place and then attempting to fix it
8489 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8490 -- whether the expanded call is on a task or protected entry.
8491
8492 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8493 or else N /= Triggering_Statement (Parent (N)))
8494 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8495 or else N /= Entry_Call_Statement (Parent (N))
8496 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8497 then
8498 Extract_Entry (N, Concval, Ename, Index);
8499 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8500 end if;
8501 end Expand_N_Entry_Call_Statement;
8502
8503 --------------------------------
8504 -- Expand_N_Entry_Declaration --
8505 --------------------------------
8506
8507 -- If there are parameters, then first, each of the formals is marked by
8508 -- setting Is_Entry_Formal. Next a record type is built which is used to
8509 -- hold the parameter values. The name of this record type is entryP where
8510 -- entry is the name of the entry, with an additional corresponding access
8511 -- type called entryPA. The record type has matching components for each
8512 -- formal (the component names are the same as the formal names). For
8513 -- elementary types, the component type matches the formal type. For
8514 -- composite types, an access type is declared (with the name formalA)
8515 -- which designates the formal type, and the type of the component is this
8516 -- access type. Finally the Entry_Component of each formal is set to
8517 -- reference the corresponding record component.
8518
8519 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8520 Loc : constant Source_Ptr := Sloc (N);
8521 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8522 Components : List_Id;
8523 Formal : Node_Id;
8524 Ftype : Entity_Id;
8525 Last_Decl : Node_Id;
8526 Component : Entity_Id;
8527 Ctype : Entity_Id;
8528 Decl : Node_Id;
8529 Rec_Ent : Entity_Id;
8530 Acc_Ent : Entity_Id;
8531
8532 begin
8533 Formal := First_Formal (Entry_Ent);
8534 Last_Decl := N;
8535
8536 -- Most processing is done only if parameters are present
8537
8538 if Present (Formal) then
8539 Components := New_List;
8540
8541 -- Loop through formals
8542
8543 while Present (Formal) loop
8544 Set_Is_Entry_Formal (Formal);
8545 Component :=
8546 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8547 Set_Entry_Component (Formal, Component);
8548 Set_Entry_Formal (Component, Formal);
8549 Ftype := Etype (Formal);
8550
8551 -- Declare new access type and then append
8552
8553 Ctype := Make_Temporary (Loc, 'A');
8554 Set_Is_Param_Block_Component_Type (Ctype);
8555
8556 Decl :=
8557 Make_Full_Type_Declaration (Loc,
8558 Defining_Identifier => Ctype,
8559 Type_Definition =>
8560 Make_Access_To_Object_Definition (Loc,
8561 All_Present => True,
8562 Constant_Present => Ekind (Formal) = E_In_Parameter,
8563 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8564
8565 Insert_After (Last_Decl, Decl);
8566 Last_Decl := Decl;
8567
8568 Append_To (Components,
8569 Make_Component_Declaration (Loc,
8570 Defining_Identifier => Component,
8571 Component_Definition =>
8572 Make_Component_Definition (Loc,
8573 Aliased_Present => False,
8574 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8575
8576 Next_Formal_With_Extras (Formal);
8577 end loop;
8578
8579 -- Create the Entry_Parameter_Record declaration
8580
8581 Rec_Ent := Make_Temporary (Loc, 'P');
8582
8583 Decl :=
8584 Make_Full_Type_Declaration (Loc,
8585 Defining_Identifier => Rec_Ent,
8586 Type_Definition =>
8587 Make_Record_Definition (Loc,
8588 Component_List =>
8589 Make_Component_List (Loc,
8590 Component_Items => Components)));
8591
8592 Insert_After (Last_Decl, Decl);
8593 Last_Decl := Decl;
8594
8595 -- Construct and link in the corresponding access type
8596
8597 Acc_Ent := Make_Temporary (Loc, 'A');
8598
8599 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8600
8601 Decl :=
8602 Make_Full_Type_Declaration (Loc,
8603 Defining_Identifier => Acc_Ent,
8604 Type_Definition =>
8605 Make_Access_To_Object_Definition (Loc,
8606 All_Present => True,
8607 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8608
8609 Insert_After (Last_Decl, Decl);
8610 end if;
8611 end Expand_N_Entry_Declaration;
8612
8613 -----------------------------
8614 -- Expand_N_Protected_Body --
8615 -----------------------------
8616
8617 -- Protected bodies are expanded to the completion of the subprograms
8618 -- created for the corresponding protected type. These are a protected and
8619 -- unprotected version of each protected subprogram in the object, a
8620 -- function to calculate each entry barrier, and a procedure to execute the
8621 -- sequence of statements of each protected entry body. For example, for
8622 -- protected type ptype:
8623
8624 -- function entB
8625 -- (O : System.Address;
8626 -- E : Protected_Entry_Index)
8627 -- return Boolean
8628 -- is
8629 -- <discriminant renamings>
8630 -- <private object renamings>
8631 -- begin
8632 -- return <barrier expression>;
8633 -- end entB;
8634
8635 -- procedure pprocN (_object : in out poV;...) is
8636 -- <discriminant renamings>
8637 -- <private object renamings>
8638 -- begin
8639 -- <sequence of statements>
8640 -- end pprocN;
8641
8642 -- procedure pprocP (_object : in out poV;...) is
8643 -- procedure _clean is
8644 -- Pn : Boolean;
8645 -- begin
8646 -- ptypeS (_object, Pn);
8647 -- Unlock (_object._object'Access);
8648 -- Abort_Undefer.all;
8649 -- end _clean;
8650
8651 -- begin
8652 -- Abort_Defer.all;
8653 -- Lock (_object._object'Access);
8654 -- pprocN (_object;...);
8655 -- at end
8656 -- _clean;
8657 -- end pproc;
8658
8659 -- function pfuncN (_object : poV;...) return Return_Type is
8660 -- <discriminant renamings>
8661 -- <private object renamings>
8662 -- begin
8663 -- <sequence of statements>
8664 -- end pfuncN;
8665
8666 -- function pfuncP (_object : poV) return Return_Type is
8667 -- procedure _clean is
8668 -- begin
8669 -- Unlock (_object._object'Access);
8670 -- Abort_Undefer.all;
8671 -- end _clean;
8672
8673 -- begin
8674 -- Abort_Defer.all;
8675 -- Lock (_object._object'Access);
8676 -- return pfuncN (_object);
8677
8678 -- at end
8679 -- _clean;
8680 -- end pfunc;
8681
8682 -- procedure entE
8683 -- (O : System.Address;
8684 -- P : System.Address;
8685 -- E : Protected_Entry_Index)
8686 -- is
8687 -- <discriminant renamings>
8688 -- <private object renamings>
8689 -- type poVP is access poV;
8690 -- _Object : ptVP := ptVP!(O);
8691
8692 -- begin
8693 -- begin
8694 -- <statement sequence>
8695 -- Complete_Entry_Body (_Object._Object);
8696 -- exception
8697 -- when all others =>
8698 -- Exceptional_Complete_Entry_Body (
8699 -- _Object._Object, Get_GNAT_Exception);
8700 -- end;
8701 -- end entE;
8702
8703 -- The type poV is the record created for the protected type to hold
8704 -- the state of the protected object.
8705
8706 procedure Expand_N_Protected_Body (N : Node_Id) is
8707 Loc : constant Source_Ptr := Sloc (N);
8708 Pid : constant Entity_Id := Corresponding_Spec (N);
8709
8710 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8711 -- This flag indicates whether the lock free implementation is active
8712
8713 Current_Node : Node_Id;
8714 Disp_Op_Body : Node_Id;
8715 New_Op_Body : Node_Id;
8716 Op_Body : Node_Id;
8717 Op_Id : Entity_Id;
8718
8719 function Build_Dispatching_Subprogram_Body
8720 (N : Node_Id;
8721 Pid : Node_Id;
8722 Prot_Bod : Node_Id) return Node_Id;
8723 -- Build a dispatching version of the protected subprogram body. The
8724 -- newly generated subprogram contains a call to the original protected
8725 -- body. The following code is generated:
8726 --
8727 -- function <protected-function-name> (Param1 .. ParamN) return
8728 -- <return-type> is
8729 -- begin
8730 -- return <protected-function-name>P (Param1 .. ParamN);
8731 -- end <protected-function-name>;
8732 --
8733 -- or
8734 --
8735 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8736 -- begin
8737 -- <protected-procedure-name>P (Param1 .. ParamN);
8738 -- end <protected-procedure-name>
8739
8740 ---------------------------------------
8741 -- Build_Dispatching_Subprogram_Body --
8742 ---------------------------------------
8743
8744 function Build_Dispatching_Subprogram_Body
8745 (N : Node_Id;
8746 Pid : Node_Id;
8747 Prot_Bod : Node_Id) return Node_Id
8748 is
8749 Loc : constant Source_Ptr := Sloc (N);
8750 Actuals : List_Id;
8751 Formal : Node_Id;
8752 Spec : Node_Id;
8753 Stmts : List_Id;
8754
8755 begin
8756 -- Generate a specification without a letter suffix in order to
8757 -- override an interface function or procedure.
8758
8759 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8760
8761 -- The formal parameters become the actuals of the protected function
8762 -- or procedure call.
8763
8764 Actuals := New_List;
8765 Formal := First (Parameter_Specifications (Spec));
8766 while Present (Formal) loop
8767 Append_To (Actuals,
8768 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8769 Next (Formal);
8770 end loop;
8771
8772 if Nkind (Spec) = N_Procedure_Specification then
8773 Stmts :=
8774 New_List (
8775 Make_Procedure_Call_Statement (Loc,
8776 Name =>
8777 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8778 Parameter_Associations => Actuals));
8779
8780 else
8781 pragma Assert (Nkind (Spec) = N_Function_Specification);
8782
8783 Stmts :=
8784 New_List (
8785 Make_Simple_Return_Statement (Loc,
8786 Expression =>
8787 Make_Function_Call (Loc,
8788 Name =>
8789 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8790 Parameter_Associations => Actuals)));
8791 end if;
8792
8793 return
8794 Make_Subprogram_Body (Loc,
8795 Declarations => Empty_List,
8796 Specification => Spec,
8797 Handled_Statement_Sequence =>
8798 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8799 end Build_Dispatching_Subprogram_Body;
8800
8801 -- Start of processing for Expand_N_Protected_Body
8802
8803 begin
8804 if No_Run_Time_Mode then
8805 Error_Msg_CRT ("protected body", N);
8806 return;
8807 end if;
8808
8809 -- This is the proper body corresponding to a stub. The declarations
8810 -- must be inserted at the point of the stub, which in turn is in the
8811 -- declarative part of the parent unit.
8812
8813 if Nkind (Parent (N)) = N_Subunit then
8814 Current_Node := Corresponding_Stub (Parent (N));
8815 else
8816 Current_Node := N;
8817 end if;
8818
8819 Op_Body := First (Declarations (N));
8820
8821 -- The protected body is replaced with the bodies of its
8822 -- protected operations, and the declarations for internal objects
8823 -- that may have been created for entry family bounds.
8824
8825 Rewrite (N, Make_Null_Statement (Sloc (N)));
8826 Analyze (N);
8827
8828 while Present (Op_Body) loop
8829 case Nkind (Op_Body) is
8830 when N_Subprogram_Declaration =>
8831 null;
8832
8833 when N_Subprogram_Body =>
8834
8835 -- Do not create bodies for eliminated operations
8836
8837 if not Is_Eliminated (Defining_Entity (Op_Body))
8838 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8839 then
8840 if Lock_Free_Active then
8841 New_Op_Body :=
8842 Build_Lock_Free_Unprotected_Subprogram_Body
8843 (Op_Body, Pid);
8844 else
8845 New_Op_Body :=
8846 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8847 end if;
8848
8849 Insert_After (Current_Node, New_Op_Body);
8850 Current_Node := New_Op_Body;
8851 Analyze (New_Op_Body);
8852
8853 -- Build the corresponding protected operation. It may
8854 -- appear that this is needed only if this is a visible
8855 -- operation of the type, or if it is an interrupt handler,
8856 -- and this was the strategy used previously in GNAT.
8857
8858 -- However, the operation may be exported through a 'Access
8859 -- to an external caller. This is the common idiom in code
8860 -- that uses the Ada 2005 Timing_Events package. As a result
8861 -- we need to produce the protected body for both visible
8862 -- and private operations, as well as operations that only
8863 -- have a body in the source, and for which we create a
8864 -- declaration in the protected body itself.
8865
8866 if Present (Corresponding_Spec (Op_Body)) then
8867 if Lock_Free_Active then
8868 New_Op_Body :=
8869 Build_Lock_Free_Protected_Subprogram_Body
8870 (Op_Body, Pid, Specification (New_Op_Body));
8871 else
8872 New_Op_Body :=
8873 Build_Protected_Subprogram_Body
8874 (Op_Body, Pid, Specification (New_Op_Body));
8875 end if;
8876
8877 Insert_After (Current_Node, New_Op_Body);
8878 Analyze (New_Op_Body);
8879
8880 Current_Node := New_Op_Body;
8881
8882 -- Generate an overriding primitive operation body for
8883 -- this subprogram if the protected type implements an
8884 -- interface.
8885
8886 if Ada_Version >= Ada_2005
8887 and then
8888 Present (Interfaces (Corresponding_Record_Type (Pid)))
8889 then
8890 Disp_Op_Body :=
8891 Build_Dispatching_Subprogram_Body
8892 (Op_Body, Pid, New_Op_Body);
8893
8894 Insert_After (Current_Node, Disp_Op_Body);
8895 Analyze (Disp_Op_Body);
8896
8897 Current_Node := Disp_Op_Body;
8898 end if;
8899 end if;
8900 end if;
8901
8902 when N_Entry_Body =>
8903 Op_Id := Defining_Identifier (Op_Body);
8904 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8905
8906 Insert_After (Current_Node, New_Op_Body);
8907 Current_Node := New_Op_Body;
8908 Analyze (New_Op_Body);
8909
8910 when N_Implicit_Label_Declaration =>
8911 null;
8912
8913 when N_Itype_Reference =>
8914 Insert_After (Current_Node, New_Copy (Op_Body));
8915
8916 when N_Freeze_Entity =>
8917 New_Op_Body := New_Copy (Op_Body);
8918
8919 if Present (Entity (Op_Body))
8920 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8921 then
8922 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8923 end if;
8924
8925 Insert_After (Current_Node, New_Op_Body);
8926 Current_Node := New_Op_Body;
8927 Analyze (New_Op_Body);
8928
8929 when N_Pragma =>
8930 New_Op_Body := New_Copy (Op_Body);
8931 Insert_After (Current_Node, New_Op_Body);
8932 Current_Node := New_Op_Body;
8933 Analyze (New_Op_Body);
8934
8935 when N_Object_Declaration =>
8936 pragma Assert (not Comes_From_Source (Op_Body));
8937 New_Op_Body := New_Copy (Op_Body);
8938 Insert_After (Current_Node, New_Op_Body);
8939 Current_Node := New_Op_Body;
8940 Analyze (New_Op_Body);
8941
8942 when others =>
8943 raise Program_Error;
8944
8945 end case;
8946
8947 Next (Op_Body);
8948 end loop;
8949
8950 -- Finally, create the body of the function that maps an entry index
8951 -- into the corresponding body index, except when there is no entry, or
8952 -- in a Ravenscar-like profile.
8953
8954 if Corresponding_Runtime_Package (Pid) =
8955 System_Tasking_Protected_Objects_Entries
8956 then
8957 New_Op_Body := Build_Find_Body_Index (Pid);
8958 Insert_After (Current_Node, New_Op_Body);
8959 Current_Node := New_Op_Body;
8960 Analyze (New_Op_Body);
8961 end if;
8962
8963 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8964 -- protected body. At this point all wrapper specs have been created,
8965 -- frozen and included in the dispatch table for the protected type.
8966
8967 if Ada_Version >= Ada_2005 then
8968 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8969 end if;
8970 end Expand_N_Protected_Body;
8971
8972 -----------------------------------------
8973 -- Expand_N_Protected_Type_Declaration --
8974 -----------------------------------------
8975
8976 -- First we create a corresponding record type declaration used to
8977 -- represent values of this protected type.
8978 -- The general form of this type declaration is
8979
8980 -- type poV (discriminants) is record
8981 -- _Object : aliased <kind>Protection
8982 -- [(<entry count> [, <handler count>])];
8983 -- [entry_family : array (bounds) of Void;]
8984 -- <private data fields>
8985 -- end record;
8986
8987 -- The discriminants are present only if the corresponding protected type
8988 -- has discriminants, and they exactly mirror the protected type
8989 -- discriminants. The private data fields similarly mirror the private
8990 -- declarations of the protected type.
8991
8992 -- The Object field is always present. It contains RTS specific data used
8993 -- to control the protected object. It is declared as Aliased so that it
8994 -- can be passed as a pointer to the RTS. This allows the protected record
8995 -- to be referenced within RTS data structures. An appropriate Protection
8996 -- type and discriminant are generated.
8997
8998 -- The Service field is present for protected objects with entries. It
8999 -- contains sufficient information to allow the entry service procedure for
9000 -- this object to be called when the object is not known till runtime.
9001
9002 -- One entry_family component is present for each entry family in the
9003 -- task definition (see Expand_N_Task_Type_Declaration).
9004
9005 -- When a protected object is declared, an instance of the protected type
9006 -- value record is created. The elaboration of this declaration creates the
9007 -- correct bounds for the entry families, and also evaluates the priority
9008 -- expression if needed. The initialization routine for the protected type
9009 -- itself then calls Initialize_Protection with appropriate parameters to
9010 -- initialize the value of the Task_Id field. Install_Handlers may be also
9011 -- called if a pragma Attach_Handler applies.
9012
9013 -- Note: this record is passed to the subprograms created by the expansion
9014 -- of protected subprograms and entries. It is an in parameter to protected
9015 -- functions and an in out parameter to procedures and entry bodies. The
9016 -- Entity_Id for this created record type is placed in the
9017 -- Corresponding_Record_Type field of the associated protected type entity.
9018
9019 -- Next we create a procedure specifications for protected subprograms and
9020 -- entry bodies. For each protected subprograms two subprograms are
9021 -- created, an unprotected and a protected version. The unprotected version
9022 -- is called from within other operations of the same protected object.
9023
9024 -- We also build the call to register the procedure if a pragma
9025 -- Interrupt_Handler applies.
9026
9027 -- A single subprogram is created to service all entry bodies; it has an
9028 -- additional boolean out parameter indicating that the previous entry call
9029 -- made by the current task was serviced immediately, i.e. not by proxy.
9030 -- The O parameter contains a pointer to a record object of the type
9031 -- described above. An untyped interface is used here to allow this
9032 -- procedure to be called in places where the type of the object to be
9033 -- serviced is not known. This must be done, for example, when a call that
9034 -- may have been requeued is cancelled; the corresponding object must be
9035 -- serviced, but which object that is not known till runtime.
9036
9037 -- procedure ptypeS
9038 -- (O : System.Address; P : out Boolean);
9039 -- procedure pprocN (_object : in out poV);
9040 -- procedure pproc (_object : in out poV);
9041 -- function pfuncN (_object : poV);
9042 -- function pfunc (_object : poV);
9043 -- ...
9044
9045 -- Note that this must come after the record type declaration, since
9046 -- the specs refer to this type.
9047
9048 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
9049 Discr_Map : constant Elist_Id := New_Elmt_List;
9050 Loc : constant Source_Ptr := Sloc (N);
9051 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
9052
9053 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
9054 -- This flag indicates whether the lock free implementation is active
9055
9056 Pdef : constant Node_Id := Protected_Definition (N);
9057 -- This contains two lists; one for visible and one for private decls
9058
9059 Current_Node : Node_Id := N;
9060 E_Count : Int;
9061 Entries_Aggr : Node_Id;
9062
9063 procedure Check_Inlining (Subp : Entity_Id);
9064 -- If the original operation has a pragma Inline, propagate the flag
9065 -- to the internal body, for possible inlining later on. The source
9066 -- operation is invisible to the back-end and is never actually called.
9067
9068 function Discriminated_Size (Comp : Entity_Id) return Boolean;
9069 -- If a component size is not static then a warning will be emitted
9070 -- in Ravenscar or other restricted contexts. When a component is non-
9071 -- static because of a discriminant constraint we can specialize the
9072 -- warning by mentioning discriminants explicitly.
9073
9074 procedure Expand_Entry_Declaration (Decl : Node_Id);
9075 -- Create the entry barrier and the procedure body for entry declaration
9076 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
9077
9078 function Static_Component_Size (Comp : Entity_Id) return Boolean;
9079 -- When compiling under the Ravenscar profile, private components must
9080 -- have a static size, or else a protected object will require heap
9081 -- allocation, violating the corresponding restriction. It is preferable
9082 -- to make this check here, because it provides a better error message
9083 -- than the back-end, which refers to the object as a whole.
9084
9085 procedure Register_Handler;
9086 -- For a protected operation that is an interrupt handler, add the
9087 -- freeze action that will register it as such.
9088
9089 --------------------
9090 -- Check_Inlining --
9091 --------------------
9092
9093 procedure Check_Inlining (Subp : Entity_Id) is
9094 begin
9095 if Is_Inlined (Subp) then
9096 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
9097 Set_Is_Inlined (Subp, False);
9098 end if;
9099 end Check_Inlining;
9100
9101 ------------------------
9102 -- Discriminated_Size --
9103 ------------------------
9104
9105 function Discriminated_Size (Comp : Entity_Id) return Boolean is
9106 Typ : constant Entity_Id := Etype (Comp);
9107 Index : Node_Id;
9108
9109 function Non_Static_Bound (Bound : Node_Id) return Boolean;
9110 -- Check whether the bound of an index is non-static and does denote
9111 -- a discriminant, in which case any protected object of the type
9112 -- will have a non-static size.
9113
9114 ----------------------
9115 -- Non_Static_Bound --
9116 ----------------------
9117
9118 function Non_Static_Bound (Bound : Node_Id) return Boolean is
9119 begin
9120 if Is_OK_Static_Expression (Bound) then
9121 return False;
9122
9123 elsif Is_Entity_Name (Bound)
9124 and then Present (Discriminal_Link (Entity (Bound)))
9125 then
9126 return False;
9127
9128 else
9129 return True;
9130 end if;
9131 end Non_Static_Bound;
9132
9133 -- Start of processing for Discriminated_Size
9134
9135 begin
9136 if not Is_Array_Type (Typ) then
9137 return False;
9138 end if;
9139
9140 if Ekind (Typ) = E_Array_Subtype then
9141 Index := First_Index (Typ);
9142 while Present (Index) loop
9143 if Non_Static_Bound (Low_Bound (Index))
9144 or else Non_Static_Bound (High_Bound (Index))
9145 then
9146 return False;
9147 end if;
9148
9149 Next_Index (Index);
9150 end loop;
9151
9152 return True;
9153 end if;
9154
9155 return False;
9156 end Discriminated_Size;
9157
9158 ---------------------------
9159 -- Static_Component_Size --
9160 ---------------------------
9161
9162 function Static_Component_Size (Comp : Entity_Id) return Boolean is
9163 Typ : constant Entity_Id := Etype (Comp);
9164 C : Entity_Id;
9165
9166 begin
9167 if Is_Scalar_Type (Typ) then
9168 return True;
9169
9170 elsif Is_Array_Type (Typ) then
9171 return Compile_Time_Known_Bounds (Typ);
9172
9173 elsif Is_Record_Type (Typ) then
9174 C := First_Component (Typ);
9175 while Present (C) loop
9176 if not Static_Component_Size (C) then
9177 return False;
9178 end if;
9179
9180 Next_Component (C);
9181 end loop;
9182
9183 return True;
9184
9185 -- Any other type will be checked by the back-end
9186
9187 else
9188 return True;
9189 end if;
9190 end Static_Component_Size;
9191
9192 ------------------------------
9193 -- Expand_Entry_Declaration --
9194 ------------------------------
9195
9196 procedure Expand_Entry_Declaration (Decl : Node_Id) is
9197 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
9198 Bar_Id : Entity_Id;
9199 Bod_Id : Entity_Id;
9200 Subp : Node_Id;
9201
9202 begin
9203 E_Count := E_Count + 1;
9204
9205 -- Create the protected body subprogram
9206
9207 Bod_Id :=
9208 Make_Defining_Identifier (Loc,
9209 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
9210 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
9211
9212 Subp :=
9213 Make_Subprogram_Declaration (Loc,
9214 Specification =>
9215 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
9216
9217 Insert_After (Current_Node, Subp);
9218 Current_Node := Subp;
9219
9220 Analyze (Subp);
9221
9222 -- Build a wrapper procedure to handle contract cases, preconditions,
9223 -- and postconditions.
9224
9225 Build_Contract_Wrapper (Ent_Id, N);
9226
9227 -- Create the barrier function
9228
9229 Bar_Id :=
9230 Make_Defining_Identifier (Loc,
9231 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
9232 Set_Barrier_Function (Ent_Id, Bar_Id);
9233
9234 Subp :=
9235 Make_Subprogram_Declaration (Loc,
9236 Specification =>
9237 Build_Barrier_Function_Specification (Loc, Bar_Id));
9238 Set_Is_Entry_Barrier_Function (Subp);
9239
9240 Insert_After (Current_Node, Subp);
9241 Current_Node := Subp;
9242
9243 Analyze (Subp);
9244
9245 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
9246 Set_Scope (Bar_Id, Scope (Ent_Id));
9247
9248 -- Collect pointers to the protected subprogram and the barrier
9249 -- of the current entry, for insertion into Entry_Bodies_Array.
9250
9251 Append_To (Expressions (Entries_Aggr),
9252 Make_Aggregate (Loc,
9253 Expressions => New_List (
9254 Make_Attribute_Reference (Loc,
9255 Prefix => New_Occurrence_Of (Bar_Id, Loc),
9256 Attribute_Name => Name_Unrestricted_Access),
9257 Make_Attribute_Reference (Loc,
9258 Prefix => New_Occurrence_Of (Bod_Id, Loc),
9259 Attribute_Name => Name_Unrestricted_Access))));
9260 end Expand_Entry_Declaration;
9261
9262 ----------------------
9263 -- Register_Handler --
9264 ----------------------
9265
9266 procedure Register_Handler is
9267
9268 -- All semantic checks already done in Sem_Prag
9269
9270 Prot_Proc : constant Entity_Id :=
9271 Defining_Unit_Name (Specification (Current_Node));
9272
9273 Proc_Address : constant Node_Id :=
9274 Make_Attribute_Reference (Loc,
9275 Prefix =>
9276 New_Occurrence_Of (Prot_Proc, Loc),
9277 Attribute_Name => Name_Address);
9278
9279 RTS_Call : constant Entity_Id :=
9280 Make_Procedure_Call_Statement (Loc,
9281 Name =>
9282 New_Occurrence_Of
9283 (RTE (RE_Register_Interrupt_Handler), Loc),
9284 Parameter_Associations => New_List (Proc_Address));
9285 begin
9286 Append_Freeze_Action (Prot_Proc, RTS_Call);
9287 end Register_Handler;
9288
9289 -- Local variables
9290
9291 Body_Arr : Node_Id;
9292 Body_Id : Entity_Id;
9293 Cdecls : List_Id;
9294 Comp : Node_Id;
9295 Expr : Node_Id;
9296 New_Priv : Node_Id;
9297 Obj_Def : Node_Id;
9298 Object_Comp : Node_Id;
9299 Priv : Node_Id;
9300 Rec_Decl : Node_Id;
9301 Sub : Node_Id;
9302
9303 -- Start of processing for Expand_N_Protected_Type_Declaration
9304
9305 begin
9306 if Present (Corresponding_Record_Type (Prot_Typ)) then
9307 return;
9308 else
9309 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9310 end if;
9311
9312 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9313
9314 Qualify_Entity_Names (N);
9315
9316 -- If the type has discriminants, their occurrences in the declaration
9317 -- have been replaced by the corresponding discriminals. For components
9318 -- that are constrained by discriminants, their homologues in the
9319 -- corresponding record type must refer to the discriminants of that
9320 -- record, so we must apply a new renaming to subtypes_indications:
9321
9322 -- protected discriminant => discriminal => record discriminant
9323
9324 -- This replacement is not applied to default expressions, for which
9325 -- the discriminal is correct.
9326
9327 if Has_Discriminants (Prot_Typ) then
9328 declare
9329 Disc : Entity_Id;
9330 Decl : Node_Id;
9331
9332 begin
9333 Disc := First_Discriminant (Prot_Typ);
9334 Decl := First (Discriminant_Specifications (Rec_Decl));
9335 while Present (Disc) loop
9336 Append_Elmt (Discriminal (Disc), Discr_Map);
9337 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9338 Next_Discriminant (Disc);
9339 Next (Decl);
9340 end loop;
9341 end;
9342 end if;
9343
9344 -- Fill in the component declarations
9345
9346 -- Add components for entry families. For each entry family, create an
9347 -- anonymous type declaration with the same size, and analyze the type.
9348
9349 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9350
9351 pragma Assert (Present (Pdef));
9352
9353 Insert_After (Current_Node, Rec_Decl);
9354 Current_Node := Rec_Decl;
9355
9356 -- Add private field components
9357
9358 if Present (Private_Declarations (Pdef)) then
9359 Priv := First (Private_Declarations (Pdef));
9360 while Present (Priv) loop
9361 if Nkind (Priv) = N_Component_Declaration then
9362 if not Static_Component_Size (Defining_Identifier (Priv)) then
9363
9364 -- When compiling for a restricted profile, the private
9365 -- components must have a static size. If not, this is an
9366 -- error for a single protected declaration, and rates a
9367 -- warning on a protected type declaration.
9368
9369 if not Comes_From_Source (Prot_Typ) then
9370
9371 -- It's ok to be checking this restriction at expansion
9372 -- time, because this is only for the restricted profile,
9373 -- which is not subject to strict RM conformance, so it
9374 -- is OK to miss this check in -gnatc mode.
9375
9376 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9377 Check_Restriction
9378 (No_Implicit_Protected_Object_Allocations, Priv);
9379
9380 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9381 if not Discriminated_Size (Defining_Identifier (Priv))
9382 then
9383 -- Any object of the type will be non-static.
9384
9385 Error_Msg_N ("component has non-static size??", Priv);
9386 Error_Msg_NE
9387 ("\creation of protected object of type& will "
9388 & "violate restriction "
9389 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9390 else
9391
9392 -- Object will be non-static if discriminants are.
9393
9394 Error_Msg_NE
9395 ("creation of protected object of type& with "
9396 & "non-static discriminants will violate"
9397 & " restriction No_Implicit_Heap_Allocations??",
9398 Priv, Prot_Typ);
9399 end if;
9400
9401 -- Likewise for No_Implicit_Protected_Object_Allocations
9402
9403 elsif Restriction_Active
9404 (No_Implicit_Protected_Object_Allocations)
9405 then
9406 if not Discriminated_Size (Defining_Identifier (Priv))
9407 then
9408 -- Any object of the type will be non-static.
9409
9410 Error_Msg_N ("component has non-static size??", Priv);
9411 Error_Msg_NE
9412 ("\creation of protected object of type& will "
9413 & "violate restriction "
9414 & "No_Implicit_Protected_Object_Allocations??",
9415 Priv, Prot_Typ);
9416 else
9417 -- Object will be non-static if discriminants are.
9418
9419 Error_Msg_NE
9420 ("creation of protected object of type& with "
9421 & "non-static discriminants will violate "
9422 & "restriction "
9423 & "No_Implicit_Protected_Object_Allocations??",
9424 Priv, Prot_Typ);
9425 end if;
9426 end if;
9427 end if;
9428
9429 -- The component definition consists of a subtype indication,
9430 -- or (in Ada 2005) an access definition. Make a copy of the
9431 -- proper definition.
9432
9433 declare
9434 Old_Comp : constant Node_Id := Component_Definition (Priv);
9435 Oent : constant Entity_Id := Defining_Identifier (Priv);
9436 Nent : constant Entity_Id :=
9437 Make_Defining_Identifier (Sloc (Oent),
9438 Chars => Chars (Oent));
9439 New_Comp : Node_Id;
9440
9441 begin
9442 if Present (Subtype_Indication (Old_Comp)) then
9443 New_Comp :=
9444 Make_Component_Definition (Sloc (Oent),
9445 Aliased_Present => False,
9446 Subtype_Indication =>
9447 New_Copy_Tree
9448 (Subtype_Indication (Old_Comp), Discr_Map));
9449 else
9450 New_Comp :=
9451 Make_Component_Definition (Sloc (Oent),
9452 Aliased_Present => False,
9453 Access_Definition =>
9454 New_Copy_Tree
9455 (Access_Definition (Old_Comp), Discr_Map));
9456 end if;
9457
9458 New_Priv :=
9459 Make_Component_Declaration (Loc,
9460 Defining_Identifier => Nent,
9461 Component_Definition => New_Comp,
9462 Expression => Expression (Priv));
9463
9464 Set_Has_Per_Object_Constraint (Nent,
9465 Has_Per_Object_Constraint (Oent));
9466
9467 Append_To (Cdecls, New_Priv);
9468 end;
9469
9470 elsif Nkind (Priv) = N_Subprogram_Declaration then
9471
9472 -- Make the unprotected version of the subprogram available
9473 -- for expansion of intra object calls. There is need for
9474 -- a protected version only if the subprogram is an interrupt
9475 -- handler, otherwise this operation can only be called from
9476 -- within the body.
9477
9478 Sub :=
9479 Make_Subprogram_Declaration (Loc,
9480 Specification =>
9481 Build_Protected_Sub_Specification
9482 (Priv, Prot_Typ, Unprotected_Mode));
9483
9484 Insert_After (Current_Node, Sub);
9485 Analyze (Sub);
9486
9487 Set_Protected_Body_Subprogram
9488 (Defining_Unit_Name (Specification (Priv)),
9489 Defining_Unit_Name (Specification (Sub)));
9490 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9491 Current_Node := Sub;
9492
9493 Sub :=
9494 Make_Subprogram_Declaration (Loc,
9495 Specification =>
9496 Build_Protected_Sub_Specification
9497 (Priv, Prot_Typ, Protected_Mode));
9498
9499 Insert_After (Current_Node, Sub);
9500 Analyze (Sub);
9501 Current_Node := Sub;
9502
9503 if Is_Interrupt_Handler
9504 (Defining_Unit_Name (Specification (Priv)))
9505 then
9506 if not Restricted_Profile then
9507 Register_Handler;
9508 end if;
9509 end if;
9510 end if;
9511
9512 Next (Priv);
9513 end loop;
9514 end if;
9515
9516 -- Except for the lock-free implementation, append the _Object field
9517 -- with the right type to the component list. We need to compute the
9518 -- number of entries, and in some cases the number of Attach_Handler
9519 -- pragmas.
9520
9521 if not Lock_Free_Active then
9522 declare
9523 Entry_Count_Expr : constant Node_Id :=
9524 Build_Entry_Count_Expression
9525 (Prot_Typ, Cdecls, Loc);
9526 Num_Attach_Handler : Nat := 0;
9527 Protection_Subtype : Node_Id;
9528 Ritem : Node_Id;
9529
9530 begin
9531 if Has_Attach_Handler (Prot_Typ) then
9532 Ritem := First_Rep_Item (Prot_Typ);
9533 while Present (Ritem) loop
9534 if Nkind (Ritem) = N_Pragma
9535 and then Pragma_Name (Ritem) = Name_Attach_Handler
9536 then
9537 Num_Attach_Handler := Num_Attach_Handler + 1;
9538 end if;
9539
9540 Next_Rep_Item (Ritem);
9541 end loop;
9542 end if;
9543
9544 -- Determine the proper protection type. There are two special
9545 -- cases: 1) when the protected type has dynamic interrupt
9546 -- handlers, and 2) when it has static handlers and we use a
9547 -- restricted profile.
9548
9549 if Has_Attach_Handler (Prot_Typ)
9550 and then not Restricted_Profile
9551 then
9552 Protection_Subtype :=
9553 Make_Subtype_Indication (Loc,
9554 Subtype_Mark =>
9555 New_Occurrence_Of
9556 (RTE (RE_Static_Interrupt_Protection), Loc),
9557 Constraint =>
9558 Make_Index_Or_Discriminant_Constraint (Loc,
9559 Constraints => New_List (
9560 Entry_Count_Expr,
9561 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9562
9563 elsif Has_Interrupt_Handler (Prot_Typ)
9564 and then not Restriction_Active (No_Dynamic_Attachment)
9565 then
9566 Protection_Subtype :=
9567 Make_Subtype_Indication (Loc,
9568 Subtype_Mark =>
9569 New_Occurrence_Of
9570 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9571 Constraint =>
9572 Make_Index_Or_Discriminant_Constraint (Loc,
9573 Constraints => New_List (Entry_Count_Expr)));
9574
9575 else
9576 case Corresponding_Runtime_Package (Prot_Typ) is
9577 when System_Tasking_Protected_Objects_Entries =>
9578 Protection_Subtype :=
9579 Make_Subtype_Indication (Loc,
9580 Subtype_Mark =>
9581 New_Occurrence_Of
9582 (RTE (RE_Protection_Entries), Loc),
9583 Constraint =>
9584 Make_Index_Or_Discriminant_Constraint (Loc,
9585 Constraints => New_List (Entry_Count_Expr)));
9586
9587 when System_Tasking_Protected_Objects_Single_Entry =>
9588 Protection_Subtype :=
9589 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9590
9591 when System_Tasking_Protected_Objects =>
9592 Protection_Subtype :=
9593 New_Occurrence_Of (RTE (RE_Protection), Loc);
9594
9595 when others =>
9596 raise Program_Error;
9597 end case;
9598 end if;
9599
9600 Object_Comp :=
9601 Make_Component_Declaration (Loc,
9602 Defining_Identifier =>
9603 Make_Defining_Identifier (Loc, Name_uObject),
9604 Component_Definition =>
9605 Make_Component_Definition (Loc,
9606 Aliased_Present => True,
9607 Subtype_Indication => Protection_Subtype));
9608 end;
9609
9610 -- Put the _Object component after the private component so that it
9611 -- be finalized early as required by 9.4 (20)
9612
9613 Append_To (Cdecls, Object_Comp);
9614 end if;
9615
9616 -- Analyze the record declaration immediately after construction,
9617 -- because the initialization procedure is needed for single object
9618 -- declarations before the next entity is analyzed (the freeze call
9619 -- that generates this initialization procedure is found below).
9620
9621 Analyze (Rec_Decl, Suppress => All_Checks);
9622
9623 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9624 -- the corresponding record is frozen. If any wrappers are generated,
9625 -- Current_Node is updated accordingly.
9626
9627 if Ada_Version >= Ada_2005 then
9628 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9629 end if;
9630
9631 -- Collect pointers to entry bodies and their barriers, to be placed
9632 -- in the Entry_Bodies_Array for the type. For each entry/family we
9633 -- add an expression to the aggregate which is the initial value of
9634 -- this array. The array is declared after all protected subprograms.
9635
9636 if Has_Entries (Prot_Typ) then
9637 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9638 else
9639 Entries_Aggr := Empty;
9640 end if;
9641
9642 -- Build two new procedure specifications for each protected subprogram;
9643 -- one to call from outside the object and one to call from inside.
9644 -- Build a barrier function and an entry body action procedure
9645 -- specification for each protected entry. Initialize the entry body
9646 -- array. If subprogram is flagged as eliminated, do not generate any
9647 -- internal operations.
9648
9649 E_Count := 0;
9650 Comp := First (Visible_Declarations (Pdef));
9651 while Present (Comp) loop
9652 if Nkind (Comp) = N_Subprogram_Declaration then
9653 Sub :=
9654 Make_Subprogram_Declaration (Loc,
9655 Specification =>
9656 Build_Protected_Sub_Specification
9657 (Comp, Prot_Typ, Unprotected_Mode));
9658
9659 Insert_After (Current_Node, Sub);
9660 Analyze (Sub);
9661
9662 Set_Protected_Body_Subprogram
9663 (Defining_Unit_Name (Specification (Comp)),
9664 Defining_Unit_Name (Specification (Sub)));
9665 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9666
9667 -- Make the protected version of the subprogram available for
9668 -- expansion of external calls.
9669
9670 Current_Node := Sub;
9671
9672 Sub :=
9673 Make_Subprogram_Declaration (Loc,
9674 Specification =>
9675 Build_Protected_Sub_Specification
9676 (Comp, Prot_Typ, Protected_Mode));
9677
9678 Insert_After (Current_Node, Sub);
9679 Analyze (Sub);
9680
9681 Current_Node := Sub;
9682
9683 -- Generate an overriding primitive operation specification for
9684 -- this subprogram if the protected type implements an interface
9685 -- and Build_Wrapper_Spec did not generate its wrapper.
9686
9687 if Ada_Version >= Ada_2005
9688 and then
9689 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9690 then
9691 declare
9692 Found : Boolean := False;
9693 Prim_Elmt : Elmt_Id;
9694 Prim_Op : Node_Id;
9695
9696 begin
9697 Prim_Elmt :=
9698 First_Elmt
9699 (Primitive_Operations
9700 (Corresponding_Record_Type (Prot_Typ)));
9701
9702 while Present (Prim_Elmt) loop
9703 Prim_Op := Node (Prim_Elmt);
9704
9705 if Is_Primitive_Wrapper (Prim_Op)
9706 and then Wrapped_Entity (Prim_Op) =
9707 Defining_Entity (Specification (Comp))
9708 then
9709 Found := True;
9710 exit;
9711 end if;
9712
9713 Next_Elmt (Prim_Elmt);
9714 end loop;
9715
9716 if not Found then
9717 Sub :=
9718 Make_Subprogram_Declaration (Loc,
9719 Specification =>
9720 Build_Protected_Sub_Specification
9721 (Comp, Prot_Typ, Dispatching_Mode));
9722
9723 Insert_After (Current_Node, Sub);
9724 Analyze (Sub);
9725
9726 Current_Node := Sub;
9727 end if;
9728 end;
9729 end if;
9730
9731 -- If a pragma Interrupt_Handler applies, build and add a call to
9732 -- Register_Interrupt_Handler to the freezing actions of the
9733 -- protected version (Current_Node) of the subprogram:
9734
9735 -- system.interrupts.register_interrupt_handler
9736 -- (prot_procP'address);
9737
9738 if not Restricted_Profile
9739 and then Is_Interrupt_Handler
9740 (Defining_Unit_Name (Specification (Comp)))
9741 then
9742 Register_Handler;
9743 end if;
9744
9745 elsif Nkind (Comp) = N_Entry_Declaration then
9746 Expand_Entry_Declaration (Comp);
9747 end if;
9748
9749 Next (Comp);
9750 end loop;
9751
9752 -- If there are some private entry declarations, expand it as if they
9753 -- were visible entries.
9754
9755 if Present (Private_Declarations (Pdef)) then
9756 Comp := First (Private_Declarations (Pdef));
9757 while Present (Comp) loop
9758 if Nkind (Comp) = N_Entry_Declaration then
9759 Expand_Entry_Declaration (Comp);
9760 end if;
9761
9762 Next (Comp);
9763 end loop;
9764 end if;
9765
9766 -- Create the declaration of an array object which contains the values
9767 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9768 -- type. This object is later passed to the appropriate protected object
9769 -- initialization routine.
9770
9771 if Has_Entries (Prot_Typ)
9772 and then Corresponding_Runtime_Package (Prot_Typ) =
9773 System_Tasking_Protected_Objects_Entries
9774 then
9775 declare
9776 Count : Int;
9777 Item : Entity_Id;
9778 Max_Vals : Node_Id;
9779 Maxes : List_Id;
9780 Maxes_Id : Entity_Id;
9781 Need_Array : Boolean := False;
9782
9783 begin
9784 -- First check if there is any Max_Queue_Length pragma
9785
9786 Item := First_Entity (Prot_Typ);
9787 while Present (Item) loop
9788 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9789 Need_Array := True;
9790 exit;
9791 end if;
9792
9793 Next_Entity (Item);
9794 end loop;
9795
9796 -- Gather the Max_Queue_Length values of all entries in a list. A
9797 -- value of zero indicates that the entry has no limitation on its
9798 -- queue length.
9799
9800 if Need_Array then
9801 Count := 0;
9802 Item := First_Entity (Prot_Typ);
9803 Maxes := New_List;
9804 while Present (Item) loop
9805 if Is_Entry (Item) then
9806 Count := Count + 1;
9807 Append_To (Maxes,
9808 Make_Integer_Literal
9809 (Loc, Get_Max_Queue_Length (Item)));
9810 end if;
9811
9812 Next_Entity (Item);
9813 end loop;
9814
9815 -- Create the declaration of the array object. Generate:
9816
9817 -- Maxes_Id : aliased Protected_Entry_Queue_Max_Array
9818 -- (1 .. Count) := (..., ...);
9819
9820 Maxes_Id :=
9821 Make_Defining_Identifier (Loc,
9822 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9823
9824 Max_Vals :=
9825 Make_Object_Declaration (Loc,
9826 Defining_Identifier => Maxes_Id,
9827 Aliased_Present => True,
9828 Constant_Present => True,
9829 Object_Definition =>
9830 Make_Subtype_Indication (Loc,
9831 Subtype_Mark =>
9832 New_Occurrence_Of
9833 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9834 Constraint =>
9835 Make_Index_Or_Discriminant_Constraint (Loc,
9836 Constraints => New_List (
9837 Make_Range (Loc,
9838 Make_Integer_Literal (Loc, 1),
9839 Make_Integer_Literal (Loc, Count))))),
9840 Expression => Make_Aggregate (Loc, Maxes));
9841
9842 -- A pointer to this array will be placed in the corresponding
9843 -- record by its initialization procedure so this needs to be
9844 -- analyzed here.
9845
9846 Insert_After (Current_Node, Max_Vals);
9847 Current_Node := Max_Vals;
9848 Analyze (Max_Vals);
9849
9850 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9851 end if;
9852 end;
9853 end if;
9854
9855 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9856 -- all protected subprograms have been collected.
9857
9858 if Has_Entries (Prot_Typ) then
9859 Body_Id :=
9860 Make_Defining_Identifier (Sloc (Prot_Typ),
9861 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9862
9863 case Corresponding_Runtime_Package (Prot_Typ) is
9864 when System_Tasking_Protected_Objects_Entries =>
9865 Expr := Entries_Aggr;
9866 Obj_Def :=
9867 Make_Subtype_Indication (Loc,
9868 Subtype_Mark =>
9869 New_Occurrence_Of
9870 (RTE (RE_Protected_Entry_Body_Array), Loc),
9871 Constraint =>
9872 Make_Index_Or_Discriminant_Constraint (Loc,
9873 Constraints => New_List (
9874 Make_Range (Loc,
9875 Make_Integer_Literal (Loc, 1),
9876 Make_Integer_Literal (Loc, E_Count)))));
9877
9878 when System_Tasking_Protected_Objects_Single_Entry =>
9879 Expr := Remove_Head (Expressions (Entries_Aggr));
9880 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9881
9882 when others =>
9883 raise Program_Error;
9884 end case;
9885
9886 Body_Arr :=
9887 Make_Object_Declaration (Loc,
9888 Defining_Identifier => Body_Id,
9889 Aliased_Present => True,
9890 Object_Definition => Obj_Def,
9891 Expression => Expr,
9892 Constant_Present => True);
9893
9894 -- A pointer to this array will be placed in the corresponding record
9895 -- by its initialization procedure so this needs to be analyzed here.
9896
9897 Insert_After (Current_Node, Body_Arr);
9898 Current_Node := Body_Arr;
9899 Analyze (Body_Arr);
9900
9901 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9902
9903 -- Finally, build the function that maps an entry index into the
9904 -- corresponding body. A pointer to this function is placed in each
9905 -- object of the type. Except for a ravenscar-like profile (no abort,
9906 -- no entry queue, 1 entry)
9907
9908 if Corresponding_Runtime_Package (Prot_Typ) =
9909 System_Tasking_Protected_Objects_Entries
9910 then
9911 Sub :=
9912 Make_Subprogram_Declaration (Loc,
9913 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9914
9915 Insert_After (Current_Node, Sub);
9916 Analyze (Sub);
9917 end if;
9918 end if;
9919 end Expand_N_Protected_Type_Declaration;
9920
9921 --------------------------------
9922 -- Expand_N_Requeue_Statement --
9923 --------------------------------
9924
9925 -- A nondispatching requeue statement is expanded into one of four GNARLI
9926 -- operations, depending on the source and destination (task or protected
9927 -- object). A dispatching requeue statement is expanded into a call to the
9928 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9929 -- jump around the remainder of processing for the original entry and, if
9930 -- the destination is (different) protected object, to attempt to service
9931 -- it. The following illustrates the various cases:
9932
9933 -- procedure entE
9934 -- (O : System.Address;
9935 -- P : System.Address;
9936 -- E : Protected_Entry_Index)
9937 -- is
9938 -- <discriminant renamings>
9939 -- <private object renamings>
9940 -- type poVP is access poV;
9941 -- _object : ptVP := ptVP!(O);
9942
9943 -- begin
9944 -- begin
9945 -- <start of statement sequence for entry>
9946
9947 -- -- Requeue from one protected entry body to another protected
9948 -- -- entry.
9949
9950 -- Requeue_Protected_Entry (
9951 -- _object._object'Access,
9952 -- new._object'Access,
9953 -- E,
9954 -- Abort_Present);
9955 -- return;
9956
9957 -- <some more of the statement sequence for entry>
9958
9959 -- -- Requeue from an entry body to a task entry
9960
9961 -- Requeue_Protected_To_Task_Entry (
9962 -- New._task_id,
9963 -- E,
9964 -- Abort_Present);
9965 -- return;
9966
9967 -- <rest of statement sequence for entry>
9968 -- Complete_Entry_Body (_object._object);
9969
9970 -- exception
9971 -- when all others =>
9972 -- Exceptional_Complete_Entry_Body (
9973 -- _object._object, Get_GNAT_Exception);
9974 -- end;
9975 -- end entE;
9976
9977 -- Requeue of a task entry call to a task entry
9978
9979 -- Accept_Call (E, Ann);
9980 -- <start of statement sequence for accept statement>
9981 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9982 -- goto Lnn;
9983 -- <rest of statement sequence for accept statement>
9984 -- <<Lnn>>
9985 -- Complete_Rendezvous;
9986
9987 -- exception
9988 -- when all others =>
9989 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9990
9991 -- Requeue of a task entry call to a protected entry
9992
9993 -- Accept_Call (E, Ann);
9994 -- <start of statement sequence for accept statement>
9995 -- Requeue_Task_To_Protected_Entry (
9996 -- new._object'Access,
9997 -- E,
9998 -- Abort_Present);
9999 -- newS (new, Pnn);
10000 -- goto Lnn;
10001 -- <rest of statement sequence for accept statement>
10002 -- <<Lnn>>
10003 -- Complete_Rendezvous;
10004
10005 -- exception
10006 -- when all others =>
10007 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10008
10009 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10010 -- marked by pragma Implemented (XXX, By_Entry).
10011
10012 -- The requeue is inside a protected entry:
10013
10014 -- procedure entE
10015 -- (O : System.Address;
10016 -- P : System.Address;
10017 -- E : Protected_Entry_Index)
10018 -- is
10019 -- <discriminant renamings>
10020 -- <private object renamings>
10021 -- type poVP is access poV;
10022 -- _object : ptVP := ptVP!(O);
10023
10024 -- begin
10025 -- begin
10026 -- <start of statement sequence for entry>
10027
10028 -- _Disp_Requeue
10029 -- (<interface class-wide object>,
10030 -- True,
10031 -- _object'Address,
10032 -- Ada.Tags.Get_Offset_Index
10033 -- (Tag (_object),
10034 -- <interface dispatch table index of target entry>),
10035 -- Abort_Present);
10036 -- return;
10037
10038 -- <rest of statement sequence for entry>
10039 -- Complete_Entry_Body (_object._object);
10040
10041 -- exception
10042 -- when all others =>
10043 -- Exceptional_Complete_Entry_Body (
10044 -- _object._object, Get_GNAT_Exception);
10045 -- end;
10046 -- end entE;
10047
10048 -- The requeue is inside a task entry:
10049
10050 -- Accept_Call (E, Ann);
10051 -- <start of statement sequence for accept statement>
10052 -- _Disp_Requeue
10053 -- (<interface class-wide object>,
10054 -- False,
10055 -- null,
10056 -- Ada.Tags.Get_Offset_Index
10057 -- (Tag (_object),
10058 -- <interface dispatch table index of target entrt>),
10059 -- Abort_Present);
10060 -- newS (new, Pnn);
10061 -- goto Lnn;
10062 -- <rest of statement sequence for accept statement>
10063 -- <<Lnn>>
10064 -- Complete_Rendezvous;
10065
10066 -- exception
10067 -- when all others =>
10068 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10069
10070 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10071 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
10072 -- statement is replaced by a dispatching call with actual parameters taken
10073 -- from the inner-most accept statement or entry body.
10074
10075 -- Target.Primitive (Param1, ..., ParamN);
10076
10077 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10078 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
10079 -- at all.
10080
10081 -- declare
10082 -- S : constant Offset_Index :=
10083 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
10084 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
10085
10086 -- begin
10087 -- if C = POK_Protected_Entry
10088 -- or else C = POK_Task_Entry
10089 -- then
10090 -- <statements for dispatching requeue>
10091
10092 -- elsif C = POK_Protected_Procedure then
10093 -- <dispatching call equivalent>
10094
10095 -- else
10096 -- raise Program_Error;
10097 -- end if;
10098 -- end;
10099
10100 procedure Expand_N_Requeue_Statement (N : Node_Id) is
10101 Loc : constant Source_Ptr := Sloc (N);
10102 Conc_Typ : Entity_Id;
10103 Concval : Node_Id;
10104 Ename : Node_Id;
10105 Index : Node_Id;
10106 Old_Typ : Entity_Id;
10107
10108 function Build_Dispatching_Call_Equivalent return Node_Id;
10109 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10110 -- the form Concval.Ename. It is statically known that Ename is allowed
10111 -- to be implemented by a protected procedure. Create a dispatching call
10112 -- equivalent of Concval.Ename taking the actual parameters from the
10113 -- inner-most accept statement or entry body.
10114
10115 function Build_Dispatching_Requeue return Node_Id;
10116 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10117 -- the form Concval.Ename. It is statically known that Ename is allowed
10118 -- to be implemented by a protected or a task entry. Create a call to
10119 -- primitive _Disp_Requeue which handles the low-level actions.
10120
10121 function Build_Dispatching_Requeue_To_Any return Node_Id;
10122 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10123 -- the form Concval.Ename. Ename is either marked by pragma Implemented
10124 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
10125 -- determines at runtime whether Ename denotes an entry or a procedure
10126 -- and perform the appropriate kind of dispatching select.
10127
10128 function Build_Normal_Requeue return Node_Id;
10129 -- N denotes a nondispatching requeue statement to either a task or a
10130 -- protected entry. Build the appropriate runtime call to perform the
10131 -- action.
10132
10133 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
10134 -- For a protected entry, create a return statement to skip the rest of
10135 -- the entry body. Otherwise, create a goto statement to skip the rest
10136 -- of a task accept statement. The lookup for the enclosing entry body
10137 -- or accept statement starts from Search.
10138
10139 ---------------------------------------
10140 -- Build_Dispatching_Call_Equivalent --
10141 ---------------------------------------
10142
10143 function Build_Dispatching_Call_Equivalent return Node_Id is
10144 Call_Ent : constant Entity_Id := Entity (Ename);
10145 Obj : constant Node_Id := Original_Node (Concval);
10146 Acc_Ent : Node_Id;
10147 Actuals : List_Id;
10148 Formal : Node_Id;
10149 Formals : List_Id;
10150
10151 begin
10152 -- Climb the parent chain looking for the inner-most entry body or
10153 -- accept statement.
10154
10155 Acc_Ent := N;
10156 while Present (Acc_Ent)
10157 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
10158 N_Entry_Body)
10159 loop
10160 Acc_Ent := Parent (Acc_Ent);
10161 end loop;
10162
10163 -- A requeue statement should be housed inside an entry body or an
10164 -- accept statement at some level. If this is not the case, then the
10165 -- tree is malformed.
10166
10167 pragma Assert (Present (Acc_Ent));
10168
10169 -- Recover the list of formal parameters
10170
10171 if Nkind (Acc_Ent) = N_Entry_Body then
10172 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
10173 end if;
10174
10175 Formals := Parameter_Specifications (Acc_Ent);
10176
10177 -- Create the actual parameters for the dispatching call. These are
10178 -- simply copies of the entry body or accept statement formals in the
10179 -- same order as they appear.
10180
10181 Actuals := No_List;
10182
10183 if Present (Formals) then
10184 Actuals := New_List;
10185 Formal := First (Formals);
10186 while Present (Formal) loop
10187 Append_To (Actuals,
10188 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
10189 Next (Formal);
10190 end loop;
10191 end if;
10192
10193 -- Generate:
10194 -- Obj.Call_Ent (Actuals);
10195
10196 return
10197 Make_Procedure_Call_Statement (Loc,
10198 Name =>
10199 Make_Selected_Component (Loc,
10200 Prefix => Make_Identifier (Loc, Chars (Obj)),
10201 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
10202
10203 Parameter_Associations => Actuals);
10204 end Build_Dispatching_Call_Equivalent;
10205
10206 -------------------------------
10207 -- Build_Dispatching_Requeue --
10208 -------------------------------
10209
10210 function Build_Dispatching_Requeue return Node_Id is
10211 Params : constant List_Id := New_List;
10212
10213 begin
10214 -- Process the "with abort" parameter
10215
10216 Prepend_To (Params,
10217 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10218
10219 -- Process the entry wrapper's position in the primary dispatch
10220 -- table parameter. Generate:
10221
10222 -- Ada.Tags.Get_Entry_Index
10223 -- (T => To_Tag_Ptr (Obj'Address).all,
10224 -- Position =>
10225 -- Ada.Tags.Get_Offset_Index
10226 -- (Ada.Tags.Tag (Concval),
10227 -- <interface dispatch table position of Ename>));
10228
10229 -- Note that Obj'Address is recursively expanded into a call to
10230 -- Base_Address (Obj).
10231
10232 if Tagged_Type_Expansion then
10233 Prepend_To (Params,
10234 Make_Function_Call (Loc,
10235 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10236 Parameter_Associations => New_List (
10237
10238 Make_Explicit_Dereference (Loc,
10239 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
10240 Make_Attribute_Reference (Loc,
10241 Prefix => New_Copy_Tree (Concval),
10242 Attribute_Name => Name_Address))),
10243
10244 Make_Function_Call (Loc,
10245 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10246 Parameter_Associations => New_List (
10247 Unchecked_Convert_To (RTE (RE_Tag), Concval),
10248 Make_Integer_Literal (Loc,
10249 DT_Position (Entity (Ename))))))));
10250
10251 -- VM targets
10252
10253 else
10254 Prepend_To (Params,
10255 Make_Function_Call (Loc,
10256 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10257 Parameter_Associations => New_List (
10258
10259 Make_Attribute_Reference (Loc,
10260 Prefix => Concval,
10261 Attribute_Name => Name_Tag),
10262
10263 Make_Function_Call (Loc,
10264 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10265
10266 Parameter_Associations => New_List (
10267
10268 -- Obj_Tag
10269
10270 Make_Attribute_Reference (Loc,
10271 Prefix => Concval,
10272 Attribute_Name => Name_Tag),
10273
10274 -- Tag_Typ
10275
10276 Make_Attribute_Reference (Loc,
10277 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
10278 Attribute_Name => Name_Tag),
10279
10280 -- Position
10281
10282 Make_Integer_Literal (Loc,
10283 DT_Position (Entity (Ename))))))));
10284 end if;
10285
10286 -- Specific actuals for protected to XXX requeue
10287
10288 if Is_Protected_Type (Old_Typ) then
10289 Prepend_To (Params,
10290 Make_Attribute_Reference (Loc, -- _object'Address
10291 Prefix =>
10292 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10293 Attribute_Name => Name_Address));
10294
10295 Prepend_To (Params, -- True
10296 New_Occurrence_Of (Standard_True, Loc));
10297
10298 -- Specific actuals for task to XXX requeue
10299
10300 else
10301 pragma Assert (Is_Task_Type (Old_Typ));
10302
10303 Prepend_To (Params, -- null
10304 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10305
10306 Prepend_To (Params, -- False
10307 New_Occurrence_Of (Standard_False, Loc));
10308 end if;
10309
10310 -- Add the object parameter
10311
10312 Prepend_To (Params, New_Copy_Tree (Concval));
10313
10314 -- Generate:
10315 -- _Disp_Requeue (<Params>);
10316
10317 -- Find entity for Disp_Requeue operation, which belongs to
10318 -- the type and may not be directly visible.
10319
10320 declare
10321 Elmt : Elmt_Id;
10322 Op : Entity_Id;
10323
10324 begin
10325 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10326 while Present (Elmt) loop
10327 Op := Node (Elmt);
10328 exit when Chars (Op) = Name_uDisp_Requeue;
10329 Next_Elmt (Elmt);
10330 end loop;
10331
10332 return
10333 Make_Procedure_Call_Statement (Loc,
10334 Name => New_Occurrence_Of (Op, Loc),
10335 Parameter_Associations => Params);
10336 end;
10337 end Build_Dispatching_Requeue;
10338
10339 --------------------------------------
10340 -- Build_Dispatching_Requeue_To_Any --
10341 --------------------------------------
10342
10343 function Build_Dispatching_Requeue_To_Any return Node_Id is
10344 Call_Ent : constant Entity_Id := Entity (Ename);
10345 Obj : constant Node_Id := Original_Node (Concval);
10346 Skip : constant Node_Id := Build_Skip_Statement (N);
10347 C : Entity_Id;
10348 Decls : List_Id;
10349 S : Entity_Id;
10350 Stmts : List_Id;
10351
10352 begin
10353 Decls := New_List;
10354 Stmts := New_List;
10355
10356 -- Dispatch table slot processing, generate:
10357 -- S : Integer;
10358
10359 S := Build_S (Loc, Decls);
10360
10361 -- Call kind processing, generate:
10362 -- C : Ada.Tags.Prim_Op_Kind;
10363
10364 C := Build_C (Loc, Decls);
10365
10366 -- Generate:
10367 -- S := Ada.Tags.Get_Offset_Index
10368 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10369
10370 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10371
10372 -- Generate:
10373 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10374
10375 Append_To (Stmts,
10376 Make_Procedure_Call_Statement (Loc,
10377 Name =>
10378 New_Occurrence_Of (
10379 Find_Prim_Op (Etype (Etype (Obj)),
10380 Name_uDisp_Get_Prim_Op_Kind),
10381 Loc),
10382 Parameter_Associations => New_List (
10383 New_Copy_Tree (Obj),
10384 New_Occurrence_Of (S, Loc),
10385 New_Occurrence_Of (C, Loc))));
10386
10387 Append_To (Stmts,
10388
10389 -- if C = POK_Protected_Entry
10390 -- or else C = POK_Task_Entry
10391 -- then
10392
10393 Make_Implicit_If_Statement (N,
10394 Condition =>
10395 Make_Op_Or (Loc,
10396 Left_Opnd =>
10397 Make_Op_Eq (Loc,
10398 Left_Opnd =>
10399 New_Occurrence_Of (C, Loc),
10400 Right_Opnd =>
10401 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10402
10403 Right_Opnd =>
10404 Make_Op_Eq (Loc,
10405 Left_Opnd =>
10406 New_Occurrence_Of (C, Loc),
10407 Right_Opnd =>
10408 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10409
10410 -- Dispatching requeue equivalent
10411
10412 Then_Statements => New_List (
10413 Build_Dispatching_Requeue,
10414 Skip),
10415
10416 -- elsif C = POK_Protected_Procedure then
10417
10418 Elsif_Parts => New_List (
10419 Make_Elsif_Part (Loc,
10420 Condition =>
10421 Make_Op_Eq (Loc,
10422 Left_Opnd =>
10423 New_Occurrence_Of (C, Loc),
10424 Right_Opnd =>
10425 New_Occurrence_Of (
10426 RTE (RE_POK_Protected_Procedure), Loc)),
10427
10428 -- Dispatching call equivalent
10429
10430 Then_Statements => New_List (
10431 Build_Dispatching_Call_Equivalent))),
10432
10433 -- else
10434 -- raise Program_Error;
10435 -- end if;
10436
10437 Else_Statements => New_List (
10438 Make_Raise_Program_Error (Loc,
10439 Reason => PE_Explicit_Raise))));
10440
10441 -- Wrap everything into a block
10442
10443 return
10444 Make_Block_Statement (Loc,
10445 Declarations => Decls,
10446 Handled_Statement_Sequence =>
10447 Make_Handled_Sequence_Of_Statements (Loc,
10448 Statements => Stmts));
10449 end Build_Dispatching_Requeue_To_Any;
10450
10451 --------------------------
10452 -- Build_Normal_Requeue --
10453 --------------------------
10454
10455 function Build_Normal_Requeue return Node_Id is
10456 Params : constant List_Id := New_List;
10457 Param : Node_Id;
10458 RT_Call : Node_Id;
10459
10460 begin
10461 -- Process the "with abort" parameter
10462
10463 Prepend_To (Params,
10464 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10465
10466 -- Add the index expression to the parameters. It is common among all
10467 -- four cases.
10468
10469 Prepend_To (Params,
10470 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10471
10472 if Is_Protected_Type (Old_Typ) then
10473 declare
10474 Self_Param : Node_Id;
10475
10476 begin
10477 Self_Param :=
10478 Make_Attribute_Reference (Loc,
10479 Prefix =>
10480 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10481 Attribute_Name =>
10482 Name_Unchecked_Access);
10483
10484 -- Protected to protected requeue
10485
10486 if Is_Protected_Type (Conc_Typ) then
10487 RT_Call :=
10488 New_Occurrence_Of (
10489 RTE (RE_Requeue_Protected_Entry), Loc);
10490
10491 Param :=
10492 Make_Attribute_Reference (Loc,
10493 Prefix =>
10494 Concurrent_Ref (Concval),
10495 Attribute_Name =>
10496 Name_Unchecked_Access);
10497
10498 -- Protected to task requeue
10499
10500 else pragma Assert (Is_Task_Type (Conc_Typ));
10501 RT_Call :=
10502 New_Occurrence_Of (
10503 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10504
10505 Param := Concurrent_Ref (Concval);
10506 end if;
10507
10508 Prepend_To (Params, Param);
10509 Prepend_To (Params, Self_Param);
10510 end;
10511
10512 else pragma Assert (Is_Task_Type (Old_Typ));
10513
10514 -- Task to protected requeue
10515
10516 if Is_Protected_Type (Conc_Typ) then
10517 RT_Call :=
10518 New_Occurrence_Of (
10519 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10520
10521 Param :=
10522 Make_Attribute_Reference (Loc,
10523 Prefix =>
10524 Concurrent_Ref (Concval),
10525 Attribute_Name =>
10526 Name_Unchecked_Access);
10527
10528 -- Task to task requeue
10529
10530 else pragma Assert (Is_Task_Type (Conc_Typ));
10531 RT_Call :=
10532 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10533
10534 Param := Concurrent_Ref (Concval);
10535 end if;
10536
10537 Prepend_To (Params, Param);
10538 end if;
10539
10540 return
10541 Make_Procedure_Call_Statement (Loc,
10542 Name => RT_Call,
10543 Parameter_Associations => Params);
10544 end Build_Normal_Requeue;
10545
10546 --------------------------
10547 -- Build_Skip_Statement --
10548 --------------------------
10549
10550 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10551 Skip_Stmt : Node_Id;
10552
10553 begin
10554 -- Build a return statement to skip the rest of the entire body
10555
10556 if Is_Protected_Type (Old_Typ) then
10557 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10558
10559 -- If the requeue is within a task, find the end label of the
10560 -- enclosing accept statement and create a goto statement to it.
10561
10562 else
10563 declare
10564 Acc : Node_Id;
10565 Label : Node_Id;
10566
10567 begin
10568 -- Climb the parent chain looking for the enclosing accept
10569 -- statement.
10570
10571 Acc := Parent (Search);
10572 while Present (Acc)
10573 and then Nkind (Acc) /= N_Accept_Statement
10574 loop
10575 Acc := Parent (Acc);
10576 end loop;
10577
10578 -- The last statement is the second label used for completing
10579 -- the rendezvous the usual way. The label we are looking for
10580 -- is right before it.
10581
10582 Label :=
10583 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10584
10585 pragma Assert (Nkind (Label) = N_Label);
10586
10587 -- Generate a goto statement to skip the rest of the accept
10588
10589 Skip_Stmt :=
10590 Make_Goto_Statement (Loc,
10591 Name =>
10592 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10593 end;
10594 end if;
10595
10596 Set_Analyzed (Skip_Stmt);
10597
10598 return Skip_Stmt;
10599 end Build_Skip_Statement;
10600
10601 -- Start of processing for Expand_N_Requeue_Statement
10602
10603 begin
10604 -- Extract the components of the entry call
10605
10606 Extract_Entry (N, Concval, Ename, Index);
10607 Conc_Typ := Etype (Concval);
10608
10609 -- If the prefix is an access to class-wide type, dereference to get
10610 -- object and entry type.
10611
10612 if Is_Access_Type (Conc_Typ) then
10613 Conc_Typ := Designated_Type (Conc_Typ);
10614 Rewrite (Concval,
10615 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10616 Analyze_And_Resolve (Concval, Conc_Typ);
10617 end if;
10618
10619 -- Examine the scope stack in order to find nearest enclosing protected
10620 -- or task type. This will constitute our invocation source.
10621
10622 Old_Typ := Current_Scope;
10623 while Present (Old_Typ)
10624 and then not Is_Protected_Type (Old_Typ)
10625 and then not Is_Task_Type (Old_Typ)
10626 loop
10627 Old_Typ := Scope (Old_Typ);
10628 end loop;
10629
10630 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10631 -- Concval.Ename where the type of Concval is class-wide concurrent
10632 -- interface.
10633
10634 if Ada_Version >= Ada_2012
10635 and then Present (Concval)
10636 and then Is_Class_Wide_Type (Conc_Typ)
10637 and then Is_Concurrent_Interface (Conc_Typ)
10638 then
10639 declare
10640 Has_Impl : Boolean := False;
10641 Impl_Kind : Name_Id := No_Name;
10642
10643 begin
10644 -- Check whether the Ename is flagged by pragma Implemented
10645
10646 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10647 Has_Impl := True;
10648 Impl_Kind := Implementation_Kind (Entity (Ename));
10649 end if;
10650
10651 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10652 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10653
10654 if Has_Impl and then Impl_Kind = Name_By_Entry then
10655 Rewrite (N, Build_Dispatching_Requeue);
10656 Analyze (N);
10657 Insert_After (N, Build_Skip_Statement (N));
10658
10659 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10660 -- a protected procedure. In this case the requeue is transformed
10661 -- into a dispatching call.
10662
10663 elsif Has_Impl
10664 and then Impl_Kind = Name_By_Protected_Procedure
10665 then
10666 Rewrite (N, Build_Dispatching_Call_Equivalent);
10667 Analyze (N);
10668
10669 -- The procedure_or_entry_NAME's implementation kind is either
10670 -- By_Any, Optional, or pragma Implemented was not applied at all.
10671 -- In this case a runtime test determines whether Ename denotes an
10672 -- entry or a protected procedure and performs the appropriate
10673 -- call.
10674
10675 else
10676 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10677 Analyze (N);
10678 end if;
10679 end;
10680
10681 -- Processing for regular (nondispatching) requeues
10682
10683 else
10684 Rewrite (N, Build_Normal_Requeue);
10685 Analyze (N);
10686 Insert_After (N, Build_Skip_Statement (N));
10687 end if;
10688 end Expand_N_Requeue_Statement;
10689
10690 -------------------------------
10691 -- Expand_N_Selective_Accept --
10692 -------------------------------
10693
10694 procedure Expand_N_Selective_Accept (N : Node_Id) is
10695 Loc : constant Source_Ptr := Sloc (N);
10696 Alts : constant List_Id := Select_Alternatives (N);
10697
10698 -- Note: in the below declarations a lot of new lists are allocated
10699 -- unconditionally which may well not end up being used. That's not
10700 -- a good idea since it wastes space gratuitously ???
10701
10702 Accept_Case : List_Id;
10703 Accept_List : constant List_Id := New_List;
10704
10705 Alt : Node_Id;
10706 Alt_List : constant List_Id := New_List;
10707 Alt_Stats : List_Id;
10708 Ann : Entity_Id := Empty;
10709
10710 Check_Guard : Boolean := True;
10711
10712 Decls : constant List_Id := New_List;
10713 Stats : constant List_Id := New_List;
10714 Body_List : constant List_Id := New_List;
10715 Trailing_List : constant List_Id := New_List;
10716
10717 Choices : List_Id;
10718 Else_Present : Boolean := False;
10719 Terminate_Alt : Node_Id := Empty;
10720 Select_Mode : Node_Id;
10721
10722 Delay_Case : List_Id;
10723 Delay_Count : Integer := 0;
10724 Delay_Val : Entity_Id;
10725 Delay_Index : Entity_Id;
10726 Delay_Min : Entity_Id;
10727 Delay_Num : Pos := 1;
10728 Delay_Alt_List : List_Id := New_List;
10729 Delay_List : constant List_Id := New_List;
10730 D : Entity_Id;
10731 M : Entity_Id;
10732
10733 First_Delay : Boolean := True;
10734 Guard_Open : Entity_Id;
10735
10736 End_Lab : Node_Id;
10737 Index : Pos := 1;
10738 Lab : Node_Id;
10739 Num_Alts : Nat;
10740 Num_Accept : Nat := 0;
10741 Proc : Node_Id;
10742 Time_Type : Entity_Id;
10743 Select_Call : Node_Id;
10744
10745 Qnam : constant Entity_Id :=
10746 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10747
10748 Xnam : constant Entity_Id :=
10749 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10750
10751 -----------------------
10752 -- Local subprograms --
10753 -----------------------
10754
10755 function Accept_Or_Raise return List_Id;
10756 -- For the rare case where delay alternatives all have guards, and
10757 -- all of them are closed, it is still possible that there were open
10758 -- accept alternatives with no callers. We must reexamine the
10759 -- Accept_List, and execute a selective wait with no else if some
10760 -- accept is open. If none, we raise program_error.
10761
10762 procedure Add_Accept (Alt : Node_Id);
10763 -- Process a single accept statement in a select alternative. Build
10764 -- procedure for body of accept, and add entry to dispatch table with
10765 -- expression for guard, in preparation for call to run time select.
10766
10767 function Make_And_Declare_Label (Num : Int) return Node_Id;
10768 -- Manufacture a label using Num as a serial number and declare it.
10769 -- The declaration is appended to Decls. The label marks the trailing
10770 -- statements of an accept or delay alternative.
10771
10772 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10773 -- Build call to Selective_Wait runtime routine
10774
10775 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10776 -- Add code to compare value of delay with previous values, and
10777 -- generate case entry for trailing statements.
10778
10779 procedure Process_Accept_Alternative
10780 (Alt : Node_Id;
10781 Index : Int;
10782 Proc : Node_Id);
10783 -- Add code to call corresponding procedure, and branch to
10784 -- trailing statements, if any.
10785
10786 ---------------------
10787 -- Accept_Or_Raise --
10788 ---------------------
10789
10790 function Accept_Or_Raise return List_Id is
10791 Cond : Node_Id;
10792 Stats : List_Id;
10793 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10794
10795 begin
10796 -- We generate the following:
10797
10798 -- for J in q'range loop
10799 -- if q(J).S /=null_task_entry then
10800 -- selective_wait (simple_mode,...);
10801 -- done := True;
10802 -- exit;
10803 -- end if;
10804 -- end loop;
10805 --
10806 -- if no rendez_vous then
10807 -- raise program_error;
10808 -- end if;
10809
10810 -- Note that the code needs to know that the selector name
10811 -- in an Accept_Alternative is named S.
10812
10813 Cond := Make_Op_Ne (Loc,
10814 Left_Opnd =>
10815 Make_Selected_Component (Loc,
10816 Prefix =>
10817 Make_Indexed_Component (Loc,
10818 Prefix => New_Occurrence_Of (Qnam, Loc),
10819 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10820 Selector_Name => Make_Identifier (Loc, Name_S)),
10821 Right_Opnd =>
10822 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10823
10824 Stats := New_List (
10825 Make_Implicit_Loop_Statement (N,
10826 Iteration_Scheme =>
10827 Make_Iteration_Scheme (Loc,
10828 Loop_Parameter_Specification =>
10829 Make_Loop_Parameter_Specification (Loc,
10830 Defining_Identifier => J,
10831 Discrete_Subtype_Definition =>
10832 Make_Attribute_Reference (Loc,
10833 Prefix => New_Occurrence_Of (Qnam, Loc),
10834 Attribute_Name => Name_Range,
10835 Expressions => New_List (
10836 Make_Integer_Literal (Loc, 1))))),
10837
10838 Statements => New_List (
10839 Make_Implicit_If_Statement (N,
10840 Condition => Cond,
10841 Then_Statements => New_List (
10842 Make_Select_Call (
10843 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10844 Make_Exit_Statement (Loc))))));
10845
10846 Append_To (Stats,
10847 Make_Raise_Program_Error (Loc,
10848 Condition => Make_Op_Eq (Loc,
10849 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10850 Right_Opnd =>
10851 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10852 Reason => PE_All_Guards_Closed));
10853
10854 return Stats;
10855 end Accept_Or_Raise;
10856
10857 ----------------
10858 -- Add_Accept --
10859 ----------------
10860
10861 procedure Add_Accept (Alt : Node_Id) is
10862 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10863 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10864 Eloc : constant Source_Ptr := Sloc (Ename);
10865 Eent : constant Entity_Id := Entity (Ename);
10866 Index : constant Node_Id := Entry_Index (Acc_Stm);
10867 Null_Body : Node_Id;
10868 Proc_Body : Node_Id;
10869 PB_Ent : Entity_Id;
10870 Expr : Node_Id;
10871 Call : Node_Id;
10872
10873 begin
10874 if No (Ann) then
10875 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10876 end if;
10877
10878 if Present (Condition (Alt)) then
10879 Expr :=
10880 Make_If_Expression (Eloc, New_List (
10881 Condition (Alt),
10882 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10883 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10884 else
10885 Expr :=
10886 Entry_Index_Expression
10887 (Eloc, Eent, Index, Scope (Eent));
10888 end if;
10889
10890 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10891 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10892
10893 -- Always add call to Abort_Undefer when generating code, since
10894 -- this is what the runtime expects (abort deferred in
10895 -- Selective_Wait). In CodePeer mode this only confuses the
10896 -- analysis with unknown calls, so don't do it.
10897
10898 if not CodePeer_Mode then
10899 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10900 Insert_Before
10901 (First (Statements (Handled_Statement_Sequence
10902 (Accept_Statement (Alt)))),
10903 Call);
10904 Analyze (Call);
10905 end if;
10906
10907 PB_Ent :=
10908 Make_Defining_Identifier (Eloc,
10909 New_External_Name (Chars (Ename), 'A', Num_Accept));
10910
10911 if Comes_From_Source (Alt) then
10912 Set_Debug_Info_Needed (PB_Ent);
10913 end if;
10914
10915 Proc_Body :=
10916 Make_Subprogram_Body (Eloc,
10917 Specification =>
10918 Make_Procedure_Specification (Eloc,
10919 Defining_Unit_Name => PB_Ent),
10920 Declarations => Declarations (Acc_Stm),
10921 Handled_Statement_Sequence =>
10922 Build_Accept_Body (Accept_Statement (Alt)));
10923
10924 -- During the analysis of the body of the accept statement, any
10925 -- zero cost exception handler records were collected in the
10926 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10927 -- This is where we move them to where they belong, namely the
10928 -- newly created procedure.
10929
10930 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10931 Append (Proc_Body, Body_List);
10932
10933 else
10934 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10935
10936 -- if accept statement has declarations, insert above, given that
10937 -- we are not creating a body for the accept.
10938
10939 if Present (Declarations (Acc_Stm)) then
10940 Insert_Actions (N, Declarations (Acc_Stm));
10941 end if;
10942 end if;
10943
10944 Append_To (Accept_List,
10945 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10946
10947 Num_Accept := Num_Accept + 1;
10948 end Add_Accept;
10949
10950 ----------------------------
10951 -- Make_And_Declare_Label --
10952 ----------------------------
10953
10954 function Make_And_Declare_Label (Num : Int) return Node_Id is
10955 Lab_Id : Node_Id;
10956
10957 begin
10958 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10959 Lab :=
10960 Make_Label (Loc, Lab_Id);
10961
10962 Append_To (Decls,
10963 Make_Implicit_Label_Declaration (Loc,
10964 Defining_Identifier =>
10965 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10966 Label_Construct => Lab));
10967
10968 return Lab;
10969 end Make_And_Declare_Label;
10970
10971 ----------------------
10972 -- Make_Select_Call --
10973 ----------------------
10974
10975 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10976 Params : constant List_Id := New_List;
10977
10978 begin
10979 Append_To (Params,
10980 Make_Attribute_Reference (Loc,
10981 Prefix => New_Occurrence_Of (Qnam, Loc),
10982 Attribute_Name => Name_Unchecked_Access));
10983 Append_To (Params, Select_Mode);
10984 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10985 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10986
10987 return
10988 Make_Procedure_Call_Statement (Loc,
10989 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10990 Parameter_Associations => Params);
10991 end Make_Select_Call;
10992
10993 --------------------------------
10994 -- Process_Accept_Alternative --
10995 --------------------------------
10996
10997 procedure Process_Accept_Alternative
10998 (Alt : Node_Id;
10999 Index : Int;
11000 Proc : Node_Id)
11001 is
11002 Astmt : constant Node_Id := Accept_Statement (Alt);
11003 Alt_Stats : List_Id;
11004
11005 begin
11006 Adjust_Condition (Condition (Alt));
11007
11008 -- Accept with body
11009
11010 if Present (Handled_Statement_Sequence (Astmt)) then
11011 Alt_Stats :=
11012 New_List (
11013 Make_Procedure_Call_Statement (Sloc (Proc),
11014 Name =>
11015 New_Occurrence_Of
11016 (Defining_Unit_Name (Specification (Proc)),
11017 Sloc (Proc))));
11018
11019 -- Accept with no body (followed by trailing statements)
11020
11021 else
11022 Alt_Stats := Empty_List;
11023 end if;
11024
11025 Ensure_Statement_Present (Sloc (Astmt), Alt);
11026
11027 -- After the call, if any, branch to trailing statements, if any.
11028 -- We create a label for each, as well as the corresponding label
11029 -- declaration.
11030
11031 if not Is_Empty_List (Statements (Alt)) then
11032 Lab := Make_And_Declare_Label (Index);
11033 Append (Lab, Trailing_List);
11034 Append_List (Statements (Alt), Trailing_List);
11035 Append_To (Trailing_List,
11036 Make_Goto_Statement (Loc,
11037 Name => New_Copy (Identifier (End_Lab))));
11038
11039 else
11040 Lab := End_Lab;
11041 end if;
11042
11043 Append_To (Alt_Stats,
11044 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
11045
11046 Append_To (Alt_List,
11047 Make_Case_Statement_Alternative (Loc,
11048 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
11049 Statements => Alt_Stats));
11050 end Process_Accept_Alternative;
11051
11052 -------------------------------
11053 -- Process_Delay_Alternative --
11054 -------------------------------
11055
11056 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
11057 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
11058 Cond : Node_Id;
11059 Delay_Alt : List_Id;
11060
11061 begin
11062 -- Deal with C/Fortran boolean as delay condition
11063
11064 Adjust_Condition (Condition (Alt));
11065
11066 -- Determine the smallest specified delay
11067
11068 -- for each delay alternative generate:
11069
11070 -- if guard-expression then
11071 -- Delay_Val := delay-expression;
11072 -- Guard_Open := True;
11073 -- if Delay_Val < Delay_Min then
11074 -- Delay_Min := Delay_Val;
11075 -- Delay_Index := Index;
11076 -- end if;
11077 -- end if;
11078
11079 -- The enclosing if-statement is omitted if there is no guard
11080
11081 if Delay_Count = 1 or else First_Delay then
11082 First_Delay := False;
11083
11084 Delay_Alt := New_List (
11085 Make_Assignment_Statement (Loc,
11086 Name => New_Occurrence_Of (Delay_Min, Loc),
11087 Expression => Expression (Delay_Statement (Alt))));
11088
11089 if Delay_Count > 1 then
11090 Append_To (Delay_Alt,
11091 Make_Assignment_Statement (Loc,
11092 Name => New_Occurrence_Of (Delay_Index, Loc),
11093 Expression => Make_Integer_Literal (Loc, Index)));
11094 end if;
11095
11096 else
11097 Delay_Alt := New_List (
11098 Make_Assignment_Statement (Loc,
11099 Name => New_Occurrence_Of (Delay_Val, Loc),
11100 Expression => Expression (Delay_Statement (Alt))));
11101
11102 if Time_Type = Standard_Duration then
11103 Cond :=
11104 Make_Op_Lt (Loc,
11105 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
11106 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
11107
11108 else
11109 -- The scope of the time type must define a comparison
11110 -- operator. The scope itself may not be visible, so we
11111 -- construct a node with entity information to insure that
11112 -- semantic analysis can find the proper operator.
11113
11114 Cond :=
11115 Make_Function_Call (Loc,
11116 Name => Make_Selected_Component (Loc,
11117 Prefix =>
11118 New_Occurrence_Of (Scope (Time_Type), Loc),
11119 Selector_Name =>
11120 Make_Operator_Symbol (Loc,
11121 Chars => Name_Op_Lt,
11122 Strval => No_String)),
11123 Parameter_Associations =>
11124 New_List (
11125 New_Occurrence_Of (Delay_Val, Loc),
11126 New_Occurrence_Of (Delay_Min, Loc)));
11127
11128 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
11129 end if;
11130
11131 Append_To (Delay_Alt,
11132 Make_Implicit_If_Statement (N,
11133 Condition => Cond,
11134 Then_Statements => New_List (
11135 Make_Assignment_Statement (Loc,
11136 Name => New_Occurrence_Of (Delay_Min, Loc),
11137 Expression => New_Occurrence_Of (Delay_Val, Loc)),
11138
11139 Make_Assignment_Statement (Loc,
11140 Name => New_Occurrence_Of (Delay_Index, Loc),
11141 Expression => Make_Integer_Literal (Loc, Index)))));
11142 end if;
11143
11144 if Check_Guard then
11145 Append_To (Delay_Alt,
11146 Make_Assignment_Statement (Loc,
11147 Name => New_Occurrence_Of (Guard_Open, Loc),
11148 Expression => New_Occurrence_Of (Standard_True, Loc)));
11149 end if;
11150
11151 if Present (Condition (Alt)) then
11152 Delay_Alt := New_List (
11153 Make_Implicit_If_Statement (N,
11154 Condition => Condition (Alt),
11155 Then_Statements => Delay_Alt));
11156 end if;
11157
11158 Append_List (Delay_Alt, Delay_List);
11159
11160 Ensure_Statement_Present (Dloc, Alt);
11161
11162 -- If the delay alternative has a statement part, add choice to the
11163 -- case statements for delays.
11164
11165 if not Is_Empty_List (Statements (Alt)) then
11166
11167 if Delay_Count = 1 then
11168 Append_List (Statements (Alt), Delay_Alt_List);
11169
11170 else
11171 Append_To (Delay_Alt_List,
11172 Make_Case_Statement_Alternative (Loc,
11173 Discrete_Choices => New_List (
11174 Make_Integer_Literal (Loc, Index)),
11175 Statements => Statements (Alt)));
11176 end if;
11177
11178 elsif Delay_Count = 1 then
11179
11180 -- If the single delay has no trailing statements, add a branch
11181 -- to the exit label to the selective wait.
11182
11183 Delay_Alt_List := New_List (
11184 Make_Goto_Statement (Loc,
11185 Name => New_Copy (Identifier (End_Lab))));
11186
11187 end if;
11188 end Process_Delay_Alternative;
11189
11190 -- Start of processing for Expand_N_Selective_Accept
11191
11192 begin
11193 Process_Statements_For_Controlled_Objects (N);
11194
11195 -- First insert some declarations before the select. The first is:
11196
11197 -- Ann : Address
11198
11199 -- This variable holds the parameters passed to the accept body. This
11200 -- declaration has already been inserted by the time we get here by
11201 -- a call to Expand_Accept_Declarations made from the semantics when
11202 -- processing the first accept statement contained in the select. We
11203 -- can find this entity as Accept_Address (E), where E is any of the
11204 -- entries references by contained accept statements.
11205
11206 -- The first step is to scan the list of Selective_Accept_Statements
11207 -- to find this entity, and also count the number of accepts, and
11208 -- determine if terminated, delay or else is present:
11209
11210 Num_Alts := 0;
11211
11212 Alt := First (Alts);
11213 while Present (Alt) loop
11214 Process_Statements_For_Controlled_Objects (Alt);
11215
11216 if Nkind (Alt) = N_Accept_Alternative then
11217 Add_Accept (Alt);
11218
11219 elsif Nkind (Alt) = N_Delay_Alternative then
11220 Delay_Count := Delay_Count + 1;
11221
11222 -- If the delays are relative delays, the delay expressions have
11223 -- type Standard_Duration. Otherwise they must have some time type
11224 -- recognized by GNAT.
11225
11226 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
11227 Time_Type := Standard_Duration;
11228 else
11229 Time_Type := Etype (Expression (Delay_Statement (Alt)));
11230
11231 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
11232 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
11233 then
11234 null;
11235 else
11236 Error_Msg_NE (
11237 "& is not a time type (RM 9.6(6))",
11238 Expression (Delay_Statement (Alt)), Time_Type);
11239 Time_Type := Standard_Duration;
11240 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
11241 end if;
11242 end if;
11243
11244 if No (Condition (Alt)) then
11245
11246 -- This guard will always be open
11247
11248 Check_Guard := False;
11249 end if;
11250
11251 elsif Nkind (Alt) = N_Terminate_Alternative then
11252 Adjust_Condition (Condition (Alt));
11253 Terminate_Alt := Alt;
11254 end if;
11255
11256 Num_Alts := Num_Alts + 1;
11257 Next (Alt);
11258 end loop;
11259
11260 Else_Present := Present (Else_Statements (N));
11261
11262 -- At the same time (see procedure Add_Accept) we build the accept list:
11263
11264 -- Qnn : Accept_List (1 .. num-select) := (
11265 -- (null-body, entry-index),
11266 -- (null-body, entry-index),
11267 -- ..
11268 -- (null_body, entry-index));
11269
11270 -- In the above declaration, null-body is True if the corresponding
11271 -- accept has no body, and false otherwise. The entry is either the
11272 -- entry index expression if there is no guard, or if a guard is
11273 -- present, then an if expression of the form:
11274
11275 -- (if guard then entry-index else Null_Task_Entry)
11276
11277 -- If a guard is statically known to be false, the entry can simply
11278 -- be omitted from the accept list.
11279
11280 Append_To (Decls,
11281 Make_Object_Declaration (Loc,
11282 Defining_Identifier => Qnam,
11283 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11284 Aliased_Present => True,
11285 Expression =>
11286 Make_Qualified_Expression (Loc,
11287 Subtype_Mark =>
11288 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11289 Expression =>
11290 Make_Aggregate (Loc, Expressions => Accept_List))));
11291
11292 -- Then we declare the variable that holds the index for the accept
11293 -- that will be selected for service:
11294
11295 -- Xnn : Select_Index;
11296
11297 Append_To (Decls,
11298 Make_Object_Declaration (Loc,
11299 Defining_Identifier => Xnam,
11300 Object_Definition =>
11301 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11302 Expression =>
11303 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11304
11305 -- After this follow procedure declarations for each accept body
11306
11307 -- procedure Pnn is
11308 -- begin
11309 -- ...
11310 -- end;
11311
11312 -- where the ... are statements from the corresponding procedure body.
11313 -- No parameters are involved, since the parameters are passed via Ann
11314 -- and the parameter references have already been expanded to be direct
11315 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11316 -- any embedded tasking statements (which would normally be illegal in
11317 -- procedures), have been converted to calls to the tasking runtime so
11318 -- there is no problem in putting them into procedures.
11319
11320 -- The original accept statement has been expanded into a block in
11321 -- the same fashion as for simple accepts (see Build_Accept_Body).
11322
11323 -- Note: we don't really need to build these procedures for the case
11324 -- where no delay statement is present, but it is just as easy to
11325 -- build them unconditionally, and not significantly inefficient,
11326 -- since if they are short they will be inlined anyway.
11327
11328 -- The procedure declarations have been assembled in Body_List
11329
11330 -- If delays are present, we must compute the required delay.
11331 -- We first generate the declarations:
11332
11333 -- Delay_Index : Boolean := 0;
11334 -- Delay_Min : Some_Time_Type.Time;
11335 -- Delay_Val : Some_Time_Type.Time;
11336
11337 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11338 -- active delay that is actually chosen as the basis for the possible
11339 -- delay if an immediate rendez-vous is not possible.
11340
11341 -- In the most common case there is a single delay statement, and this
11342 -- is handled specially.
11343
11344 if Delay_Count > 0 then
11345
11346 -- Generate the required declarations
11347
11348 Delay_Val :=
11349 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11350 Delay_Index :=
11351 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11352 Delay_Min :=
11353 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11354
11355 Append_To (Decls,
11356 Make_Object_Declaration (Loc,
11357 Defining_Identifier => Delay_Val,
11358 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11359
11360 Append_To (Decls,
11361 Make_Object_Declaration (Loc,
11362 Defining_Identifier => Delay_Index,
11363 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11364 Expression => Make_Integer_Literal (Loc, 0)));
11365
11366 Append_To (Decls,
11367 Make_Object_Declaration (Loc,
11368 Defining_Identifier => Delay_Min,
11369 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11370 Expression =>
11371 Unchecked_Convert_To (Time_Type,
11372 Make_Attribute_Reference (Loc,
11373 Prefix =>
11374 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11375 Attribute_Name => Name_Last))));
11376
11377 -- Create Duration and Delay_Mode objects used for passing a delay
11378 -- value to RTS
11379
11380 D := Make_Temporary (Loc, 'D');
11381 M := Make_Temporary (Loc, 'M');
11382
11383 declare
11384 Discr : Entity_Id;
11385
11386 begin
11387 -- Note that these values are defined in s-osprim.ads and must
11388 -- be kept in sync:
11389 --
11390 -- Relative : constant := 0;
11391 -- Absolute_Calendar : constant := 1;
11392 -- Absolute_RT : constant := 2;
11393
11394 if Time_Type = Standard_Duration then
11395 Discr := Make_Integer_Literal (Loc, 0);
11396
11397 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11398 Discr := Make_Integer_Literal (Loc, 1);
11399
11400 else
11401 pragma Assert
11402 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11403 Discr := Make_Integer_Literal (Loc, 2);
11404 end if;
11405
11406 Append_To (Decls,
11407 Make_Object_Declaration (Loc,
11408 Defining_Identifier => D,
11409 Object_Definition =>
11410 New_Occurrence_Of (Standard_Duration, Loc)));
11411
11412 Append_To (Decls,
11413 Make_Object_Declaration (Loc,
11414 Defining_Identifier => M,
11415 Object_Definition =>
11416 New_Occurrence_Of (Standard_Integer, Loc),
11417 Expression => Discr));
11418 end;
11419
11420 if Check_Guard then
11421 Guard_Open :=
11422 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11423
11424 Append_To (Decls,
11425 Make_Object_Declaration (Loc,
11426 Defining_Identifier => Guard_Open,
11427 Object_Definition =>
11428 New_Occurrence_Of (Standard_Boolean, Loc),
11429 Expression =>
11430 New_Occurrence_Of (Standard_False, Loc)));
11431 end if;
11432
11433 -- Delay_Count is zero, don't need M and D set (suppress warning)
11434
11435 else
11436 M := Empty;
11437 D := Empty;
11438 end if;
11439
11440 if Present (Terminate_Alt) then
11441
11442 -- If the terminate alternative guard is False, use
11443 -- Simple_Mode; otherwise use Terminate_Mode.
11444
11445 if Present (Condition (Terminate_Alt)) then
11446 Select_Mode := Make_If_Expression (Loc,
11447 New_List (Condition (Terminate_Alt),
11448 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11449 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11450 else
11451 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11452 end if;
11453
11454 elsif Else_Present or Delay_Count > 0 then
11455 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11456
11457 else
11458 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11459 end if;
11460
11461 Select_Call := Make_Select_Call (Select_Mode);
11462 Append (Select_Call, Stats);
11463
11464 -- Now generate code to act on the result. There is an entry
11465 -- in this case for each accept statement with a non-null body,
11466 -- followed by a branch to the statements that follow the Accept.
11467 -- In the absence of delay alternatives, we generate:
11468
11469 -- case X is
11470 -- when No_Rendezvous => -- omitted if simple mode
11471 -- goto Lab0;
11472
11473 -- when 1 =>
11474 -- P1n;
11475 -- goto Lab1;
11476
11477 -- when 2 =>
11478 -- P2n;
11479 -- goto Lab2;
11480
11481 -- when others =>
11482 -- goto Exit;
11483 -- end case;
11484 --
11485 -- Lab0: Else_Statements;
11486 -- goto exit;
11487
11488 -- Lab1: Trailing_Statements1;
11489 -- goto Exit;
11490 --
11491 -- Lab2: Trailing_Statements2;
11492 -- goto Exit;
11493 -- ...
11494 -- Exit:
11495
11496 -- Generate label for common exit
11497
11498 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11499
11500 -- First entry is the default case, when no rendezvous is possible
11501
11502 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11503
11504 if Else_Present then
11505
11506 -- If no rendezvous is possible, the else part is executed
11507
11508 Lab := Make_And_Declare_Label (0);
11509 Alt_Stats := New_List (
11510 Make_Goto_Statement (Loc,
11511 Name => New_Copy (Identifier (Lab))));
11512
11513 Append (Lab, Trailing_List);
11514 Append_List (Else_Statements (N), Trailing_List);
11515 Append_To (Trailing_List,
11516 Make_Goto_Statement (Loc,
11517 Name => New_Copy (Identifier (End_Lab))));
11518 else
11519 Alt_Stats := New_List (
11520 Make_Goto_Statement (Loc,
11521 Name => New_Copy (Identifier (End_Lab))));
11522 end if;
11523
11524 Append_To (Alt_List,
11525 Make_Case_Statement_Alternative (Loc,
11526 Discrete_Choices => Choices,
11527 Statements => Alt_Stats));
11528
11529 -- We make use of the fact that Accept_Index is an integer type, and
11530 -- generate successive literals for entries for each accept. Only those
11531 -- for which there is a body or trailing statements get a case entry.
11532
11533 Alt := First (Select_Alternatives (N));
11534 Proc := First (Body_List);
11535 while Present (Alt) loop
11536
11537 if Nkind (Alt) = N_Accept_Alternative then
11538 Process_Accept_Alternative (Alt, Index, Proc);
11539 Index := Index + 1;
11540
11541 if Present
11542 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11543 then
11544 Next (Proc);
11545 end if;
11546
11547 elsif Nkind (Alt) = N_Delay_Alternative then
11548 Process_Delay_Alternative (Alt, Delay_Num);
11549 Delay_Num := Delay_Num + 1;
11550 end if;
11551
11552 Next (Alt);
11553 end loop;
11554
11555 -- An others choice is always added to the main case, as well
11556 -- as the delay case (to satisfy the compiler).
11557
11558 Append_To (Alt_List,
11559 Make_Case_Statement_Alternative (Loc,
11560 Discrete_Choices =>
11561 New_List (Make_Others_Choice (Loc)),
11562 Statements =>
11563 New_List (Make_Goto_Statement (Loc,
11564 Name => New_Copy (Identifier (End_Lab))))));
11565
11566 Accept_Case := New_List (
11567 Make_Case_Statement (Loc,
11568 Expression => New_Occurrence_Of (Xnam, Loc),
11569 Alternatives => Alt_List));
11570
11571 Append_List (Trailing_List, Accept_Case);
11572 Append_List (Body_List, Decls);
11573
11574 -- Construct case statement for trailing statements of delay
11575 -- alternatives, if there are several of them.
11576
11577 if Delay_Count > 1 then
11578 Append_To (Delay_Alt_List,
11579 Make_Case_Statement_Alternative (Loc,
11580 Discrete_Choices =>
11581 New_List (Make_Others_Choice (Loc)),
11582 Statements =>
11583 New_List (Make_Null_Statement (Loc))));
11584
11585 Delay_Case := New_List (
11586 Make_Case_Statement (Loc,
11587 Expression => New_Occurrence_Of (Delay_Index, Loc),
11588 Alternatives => Delay_Alt_List));
11589 else
11590 Delay_Case := Delay_Alt_List;
11591 end if;
11592
11593 -- If there are no delay alternatives, we append the case statement
11594 -- to the statement list.
11595
11596 if Delay_Count = 0 then
11597 Append_List (Accept_Case, Stats);
11598
11599 -- Delay alternatives present
11600
11601 else
11602 -- If delay alternatives are present we generate:
11603
11604 -- find minimum delay.
11605 -- DX := minimum delay;
11606 -- M := <delay mode>;
11607 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11608 -- DX, MX, X);
11609 --
11610 -- if X = No_Rendezvous then
11611 -- case statement for delay statements.
11612 -- else
11613 -- case statement for accept alternatives.
11614 -- end if;
11615
11616 declare
11617 Cases : Node_Id;
11618 Stmt : Node_Id;
11619 Parms : List_Id;
11620 Parm : Node_Id;
11621 Conv : Node_Id;
11622
11623 begin
11624 -- The type of the delay expression is known to be legal
11625
11626 if Time_Type = Standard_Duration then
11627 Conv := New_Occurrence_Of (Delay_Min, Loc);
11628
11629 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11630 Conv := Make_Function_Call (Loc,
11631 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11632 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11633
11634 else
11635 pragma Assert
11636 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11637
11638 Conv := Make_Function_Call (Loc,
11639 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11640 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11641 end if;
11642
11643 Stmt := Make_Assignment_Statement (Loc,
11644 Name => New_Occurrence_Of (D, Loc),
11645 Expression => Conv);
11646
11647 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11648
11649 Parms := Parameter_Associations (Select_Call);
11650
11651 Parm := First (Parms);
11652 while Present (Parm) and then Parm /= Select_Mode loop
11653 Next (Parm);
11654 end loop;
11655
11656 pragma Assert (Present (Parm));
11657 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11658 Analyze (Parm);
11659
11660 -- Prepare two new parameters of Duration and Delay_Mode type
11661 -- which represent the value and the mode of the minimum delay.
11662
11663 Next (Parm);
11664 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11665 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11666
11667 -- Create a call to RTS
11668
11669 Rewrite (Select_Call,
11670 Make_Procedure_Call_Statement (Loc,
11671 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11672 Parameter_Associations => Parms));
11673
11674 -- This new call should follow the calculation of the minimum
11675 -- delay.
11676
11677 Insert_List_Before (Select_Call, Delay_List);
11678
11679 if Check_Guard then
11680 Stmt :=
11681 Make_Implicit_If_Statement (N,
11682 Condition => New_Occurrence_Of (Guard_Open, Loc),
11683 Then_Statements => New_List (
11684 New_Copy_Tree (Stmt),
11685 New_Copy_Tree (Select_Call)),
11686 Else_Statements => Accept_Or_Raise);
11687 Rewrite (Select_Call, Stmt);
11688 else
11689 Insert_Before (Select_Call, Stmt);
11690 end if;
11691
11692 Cases :=
11693 Make_Implicit_If_Statement (N,
11694 Condition => Make_Op_Eq (Loc,
11695 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11696 Right_Opnd =>
11697 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11698
11699 Then_Statements => Delay_Case,
11700 Else_Statements => Accept_Case);
11701
11702 Append (Cases, Stats);
11703 end;
11704 end if;
11705
11706 Append (End_Lab, Stats);
11707
11708 -- Replace accept statement with appropriate block
11709
11710 Rewrite (N,
11711 Make_Block_Statement (Loc,
11712 Declarations => Decls,
11713 Handled_Statement_Sequence =>
11714 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11715 Analyze (N);
11716
11717 -- Note: have to worry more about abort deferral in above code ???
11718
11719 -- Final step is to unstack the Accept_Address entries for all accept
11720 -- statements appearing in accept alternatives in the select statement
11721
11722 Alt := First (Alts);
11723 while Present (Alt) loop
11724 if Nkind (Alt) = N_Accept_Alternative then
11725 Remove_Last_Elmt (Accept_Address
11726 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11727 end if;
11728
11729 Next (Alt);
11730 end loop;
11731 end Expand_N_Selective_Accept;
11732
11733 -------------------------------------------
11734 -- Expand_N_Single_Protected_Declaration --
11735 -------------------------------------------
11736
11737 -- A single protected declaration should never be present after semantic
11738 -- analysis because it is transformed into a protected type declaration
11739 -- and an accompanying anonymous object. This routine ensures that the
11740 -- transformation takes place.
11741
11742 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11743 begin
11744 raise Program_Error;
11745 end Expand_N_Single_Protected_Declaration;
11746
11747 --------------------------------------
11748 -- Expand_N_Single_Task_Declaration --
11749 --------------------------------------
11750
11751 -- A single task declaration should never be present after semantic
11752 -- analysis because it is transformed into a task type declaration and
11753 -- an accompanying anonymous object. This routine ensures that the
11754 -- transformation takes place.
11755
11756 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11757 begin
11758 raise Program_Error;
11759 end Expand_N_Single_Task_Declaration;
11760
11761 ------------------------
11762 -- Expand_N_Task_Body --
11763 ------------------------
11764
11765 -- Given a task body
11766
11767 -- task body tname is
11768 -- <declarations>
11769 -- begin
11770 -- <statements>
11771 -- end x;
11772
11773 -- This expansion routine converts it into a procedure and sets the
11774 -- elaboration flag for the procedure to true, to represent the fact
11775 -- that the task body is now elaborated:
11776
11777 -- procedure tnameB (_Task : access tnameV) is
11778 -- discriminal : dtype renames _Task.discriminant;
11779
11780 -- procedure _clean is
11781 -- begin
11782 -- Abort_Defer.all;
11783 -- Complete_Task;
11784 -- Abort_Undefer.all;
11785 -- return;
11786 -- end _clean;
11787
11788 -- begin
11789 -- Abort_Undefer.all;
11790 -- <declarations>
11791 -- System.Task_Stages.Complete_Activation;
11792 -- <statements>
11793 -- at end
11794 -- _clean;
11795 -- end tnameB;
11796
11797 -- tnameE := True;
11798
11799 -- In addition, if the task body is an activator, then a call to activate
11800 -- tasks is added at the start of the statements, before the call to
11801 -- Complete_Activation, and if in addition the task is a master then it
11802 -- must be established as a master. These calls are inserted and analyzed
11803 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11804 -- expanded.
11805
11806 -- There is one discriminal declaration line generated for each
11807 -- discriminant that is present to provide an easy reference point for
11808 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11809
11810 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11811 -- task body procedures have a profile (Arg : System.Address). That is
11812 -- needed because GNARLI has to use the same access-to-subprogram type
11813 -- for all task types. We depend here on knowing that in GNAT, passing
11814 -- an address argument by value is identical to passing a record value
11815 -- by access (in either case a single pointer is passed), so even though
11816 -- this procedure has the wrong profile. In fact it's all OK, since the
11817 -- callings sequence is identical.
11818
11819 procedure Expand_N_Task_Body (N : Node_Id) is
11820 Loc : constant Source_Ptr := Sloc (N);
11821 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11822 Call : Node_Id;
11823 New_N : Node_Id;
11824
11825 Insert_Nod : Node_Id;
11826 -- Used to determine the proper location of wrapper body insertions
11827
11828 begin
11829 -- if no task body procedure, means we had an error in configurable
11830 -- run-time mode, and there is no point in proceeding further.
11831
11832 if No (Task_Body_Procedure (Ttyp)) then
11833 return;
11834 end if;
11835
11836 -- Add renaming declarations for discriminals and a declaration for the
11837 -- entry family index (if applicable).
11838
11839 Install_Private_Data_Declarations
11840 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11841
11842 -- Add a call to Abort_Undefer at the very beginning of the task
11843 -- body since this body is called with abort still deferred.
11844
11845 if Abort_Allowed then
11846 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11847 Insert_Before
11848 (First (Statements (Handled_Statement_Sequence (N))), Call);
11849 Analyze (Call);
11850 end if;
11851
11852 -- The statement part has already been protected with an at_end and
11853 -- cleanup actions. The call to Complete_Activation must be placed
11854 -- at the head of the sequence of statements of that block. The
11855 -- declarations have been merged in this sequence of statements but
11856 -- the first real statement is accessible from the First_Real_Statement
11857 -- field (which was set for exactly this purpose).
11858
11859 if Restricted_Profile then
11860 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11861 else
11862 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11863 end if;
11864
11865 Insert_Before
11866 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11867 Analyze (Call);
11868
11869 New_N :=
11870 Make_Subprogram_Body (Loc,
11871 Specification => Build_Task_Proc_Specification (Ttyp),
11872 Declarations => Declarations (N),
11873 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11874 Set_Is_Task_Body_Procedure (New_N);
11875
11876 -- If the task contains generic instantiations, cleanup actions are
11877 -- delayed until after instantiation. Transfer the activation chain to
11878 -- the subprogram, to insure that the activation call is properly
11879 -- generated. It the task body contains inner tasks, indicate that the
11880 -- subprogram is a task master.
11881
11882 if Delay_Cleanups (Ttyp) then
11883 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11884 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11885 end if;
11886
11887 Rewrite (N, New_N);
11888 Analyze (N);
11889
11890 -- Set elaboration flag immediately after task body. If the body is a
11891 -- subunit, the flag is set in the declarative part containing the stub.
11892
11893 if Nkind (Parent (N)) /= N_Subunit then
11894 Insert_After (N,
11895 Make_Assignment_Statement (Loc,
11896 Name =>
11897 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11898 Expression => New_Occurrence_Of (Standard_True, Loc)));
11899 end if;
11900
11901 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11902 -- the task body. At this point all wrapper specs have been created,
11903 -- frozen and included in the dispatch table for the task type.
11904
11905 if Ada_Version >= Ada_2005 then
11906 if Nkind (Parent (N)) = N_Subunit then
11907 Insert_Nod := Corresponding_Stub (Parent (N));
11908 else
11909 Insert_Nod := N;
11910 end if;
11911
11912 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11913 end if;
11914 end Expand_N_Task_Body;
11915
11916 ------------------------------------
11917 -- Expand_N_Task_Type_Declaration --
11918 ------------------------------------
11919
11920 -- We have several things to do. First we must create a Boolean flag used
11921 -- to mark if the body is elaborated yet. This variable gets set to True
11922 -- when the body of the task is elaborated (we can't rely on the normal
11923 -- ABE mechanism for the task body, since we need to pass an access to
11924 -- this elaboration boolean to the runtime routines).
11925
11926 -- taskE : aliased Boolean := False;
11927
11928 -- Next a variable is declared to hold the task stack size (either the
11929 -- default : Unspecified_Size, or a value that is set by a pragma
11930 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11931 -- the variable is initialized with this value:
11932
11933 -- taskZ : Size_Type := Unspecified_Size;
11934 -- or
11935 -- taskZ : Size_Type := Size_Type (size_expression);
11936
11937 -- Note: No variable is needed to hold the task relative deadline since
11938 -- its value would never be static because the parameter is of a private
11939 -- type (Ada.Real_Time.Time_Span).
11940
11941 -- Next we create a corresponding record type declaration used to represent
11942 -- values of this task. The general form of this type declaration is
11943
11944 -- type taskV (discriminants) is record
11945 -- _Task_Id : Task_Id;
11946 -- entry_family : array (bounds) of Void;
11947 -- _Priority : Integer := priority_expression;
11948 -- _Size : Size_Type := size_expression;
11949 -- _Task_Info : Task_Info_Type := task_info_expression;
11950 -- _CPU : Integer := cpu_range_expression;
11951 -- _Relative_Deadline : Time_Span := time_span_expression;
11952 -- _Domain : Dispatching_Domain := dd_expression;
11953 -- end record;
11954
11955 -- The discriminants are present only if the corresponding task type has
11956 -- discriminants, and they exactly mirror the task type discriminants.
11957
11958 -- The Id field is always present. It contains the Task_Id value, as set by
11959 -- the call to Create_Task. Note that although the task is limited, the
11960 -- task value record type is not limited, so there is no problem in passing
11961 -- this field as an out parameter to Create_Task.
11962
11963 -- One entry_family component is present for each entry family in the task
11964 -- definition. The bounds correspond to the bounds of the entry family
11965 -- (which may depend on discriminants). The element type is void, since we
11966 -- only need the bounds information for determining the entry index. Note
11967 -- that the use of an anonymous array would normally be illegal in this
11968 -- context, but this is a parser check, and the semantics is quite prepared
11969 -- to handle such a case.
11970
11971 -- The _Size field is present only if a Storage_Size pragma appears in the
11972 -- task definition. The expression captures the argument that was present
11973 -- in the pragma, and is used to override the task stack size otherwise
11974 -- associated with the task type.
11975
11976 -- The _Priority field is present only if the task entity has a Priority or
11977 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11978 -- definition clause). It will be filled at the freeze point, when the
11979 -- record init proc is built, to capture the expression of the rep item
11980 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11981 -- here since aspect evaluations are delayed till the freeze point.
11982
11983 -- The _Task_Info field is present only if a Task_Info pragma appears in
11984 -- the task definition. The expression captures the argument that was
11985 -- present in the pragma, and is used to provide the Task_Image parameter
11986 -- to the call to Create_Task.
11987
11988 -- The _CPU field is present only if the task entity has a CPU rep item
11989 -- (pragma, aspect specification or attribute definition clause). It will
11990 -- be filled at the freeze point, when the record init proc is built, to
11991 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11992 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11993 -- are delayed till the freeze point.
11994
11995 -- The _Relative_Deadline field is present only if a Relative_Deadline
11996 -- pragma appears in the task definition. The expression captures the
11997 -- argument that was present in the pragma, and is used to provide the
11998 -- Relative_Deadline parameter to the call to Create_Task.
11999
12000 -- The _Domain field is present only if the task entity has a
12001 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
12002 -- definition clause). It will be filled at the freeze point, when the
12003 -- record init proc is built, to capture the expression of the rep item
12004 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
12005 -- here since aspect evaluations are delayed till the freeze point.
12006
12007 -- When a task is declared, an instance of the task value record is
12008 -- created. The elaboration of this declaration creates the correct bounds
12009 -- for the entry families, and also evaluates the size, priority, and
12010 -- task_Info expressions if needed. The initialization routine for the task
12011 -- type itself then calls Create_Task with appropriate parameters to
12012 -- initialize the value of the Task_Id field.
12013
12014 -- Note: the address of this record is passed as the "Discriminants"
12015 -- parameter for Create_Task. Since Create_Task merely passes this onto the
12016 -- body procedure, it does not matter that it does not quite match the
12017 -- GNARLI model of what is being passed (the record contains more than just
12018 -- the discriminants, but the discriminants can be found from the record
12019 -- value).
12020
12021 -- The Entity_Id for this created record type is placed in the
12022 -- Corresponding_Record_Type field of the associated task type entity.
12023
12024 -- Next we create a procedure specification for the task body procedure:
12025
12026 -- procedure taskB (_Task : access taskV);
12027
12028 -- Note that this must come after the record type declaration, since
12029 -- the spec refers to this type. It turns out that the initialization
12030 -- procedure for the value type references the task body spec, but that's
12031 -- fine, since it won't be generated till the freeze point for the type,
12032 -- which is certainly after the task body spec declaration.
12033
12034 -- Finally, we set the task index value field of the entry attribute in
12035 -- the case of a simple entry.
12036
12037 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
12038 Loc : constant Source_Ptr := Sloc (N);
12039 TaskId : constant Entity_Id := Defining_Identifier (N);
12040 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
12041 Tasknm : constant Name_Id := Chars (Tasktyp);
12042 Taskdef : constant Node_Id := Task_Definition (N);
12043
12044 Body_Decl : Node_Id;
12045 Cdecls : List_Id;
12046 Decl_Stack : Node_Id;
12047 Elab_Decl : Node_Id;
12048 Ent_Stack : Entity_Id;
12049 Proc_Spec : Node_Id;
12050 Rec_Decl : Node_Id;
12051 Rec_Ent : Entity_Id;
12052 Size_Decl : Entity_Id;
12053 Task_Size : Node_Id;
12054
12055 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
12056 -- Searches the task definition T for the first occurrence of the pragma
12057 -- Relative Deadline. The caller has ensured that the pragma is present
12058 -- in the task definition. Note that this routine cannot be implemented
12059 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
12060 -- not chained because their expansion into a procedure call statement
12061 -- would cause a break in the chain.
12062
12063 ----------------------------------
12064 -- Get_Relative_Deadline_Pragma --
12065 ----------------------------------
12066
12067 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
12068 N : Node_Id;
12069
12070 begin
12071 N := First (Visible_Declarations (T));
12072 while Present (N) loop
12073 if Nkind (N) = N_Pragma
12074 and then Pragma_Name (N) = Name_Relative_Deadline
12075 then
12076 return N;
12077 end if;
12078
12079 Next (N);
12080 end loop;
12081
12082 N := First (Private_Declarations (T));
12083 while Present (N) loop
12084 if Nkind (N) = N_Pragma
12085 and then Pragma_Name (N) = Name_Relative_Deadline
12086 then
12087 return N;
12088 end if;
12089
12090 Next (N);
12091 end loop;
12092
12093 raise Program_Error;
12094 end Get_Relative_Deadline_Pragma;
12095
12096 -- Start of processing for Expand_N_Task_Type_Declaration
12097
12098 begin
12099 -- If already expanded, nothing to do
12100
12101 if Present (Corresponding_Record_Type (Tasktyp)) then
12102 return;
12103 end if;
12104
12105 -- Here we will do the expansion
12106
12107 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
12108
12109 Rec_Ent := Defining_Identifier (Rec_Decl);
12110 Cdecls := Component_Items (Component_List
12111 (Type_Definition (Rec_Decl)));
12112
12113 Qualify_Entity_Names (N);
12114
12115 -- First create the elaboration variable
12116
12117 Elab_Decl :=
12118 Make_Object_Declaration (Loc,
12119 Defining_Identifier =>
12120 Make_Defining_Identifier (Sloc (Tasktyp),
12121 Chars => New_External_Name (Tasknm, 'E')),
12122 Aliased_Present => True,
12123 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
12124 Expression => New_Occurrence_Of (Standard_False, Loc));
12125
12126 Insert_After (N, Elab_Decl);
12127
12128 -- Next create the declaration of the size variable (tasknmZ)
12129
12130 Set_Storage_Size_Variable (Tasktyp,
12131 Make_Defining_Identifier (Sloc (Tasktyp),
12132 Chars => New_External_Name (Tasknm, 'Z')));
12133
12134 if Present (Taskdef)
12135 and then Has_Storage_Size_Pragma (Taskdef)
12136 and then
12137 Is_OK_Static_Expression
12138 (Expression
12139 (First (Pragma_Argument_Associations
12140 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
12141 then
12142 Size_Decl :=
12143 Make_Object_Declaration (Loc,
12144 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12145 Object_Definition =>
12146 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12147 Expression =>
12148 Convert_To (RTE (RE_Size_Type),
12149 Relocate_Node
12150 (Expression (First (Pragma_Argument_Associations
12151 (Get_Rep_Pragma
12152 (TaskId, Name_Storage_Size)))))));
12153
12154 else
12155 Size_Decl :=
12156 Make_Object_Declaration (Loc,
12157 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12158 Object_Definition =>
12159 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12160 Expression =>
12161 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
12162 end if;
12163
12164 Insert_After (Elab_Decl, Size_Decl);
12165
12166 -- Next build the rest of the corresponding record declaration. This is
12167 -- done last, since the corresponding record initialization procedure
12168 -- will reference the previously created entities.
12169
12170 -- Fill in the component declarations -- first the _Task_Id field
12171
12172 Append_To (Cdecls,
12173 Make_Component_Declaration (Loc,
12174 Defining_Identifier =>
12175 Make_Defining_Identifier (Loc, Name_uTask_Id),
12176 Component_Definition =>
12177 Make_Component_Definition (Loc,
12178 Aliased_Present => False,
12179 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
12180 Loc))));
12181
12182 -- Declare static ATCB (that is, created by the expander) if we are
12183 -- using the Restricted run time.
12184
12185 if Restricted_Profile then
12186 Append_To (Cdecls,
12187 Make_Component_Declaration (Loc,
12188 Defining_Identifier =>
12189 Make_Defining_Identifier (Loc, Name_uATCB),
12190
12191 Component_Definition =>
12192 Make_Component_Definition (Loc,
12193 Aliased_Present => True,
12194 Subtype_Indication => Make_Subtype_Indication (Loc,
12195 Subtype_Mark =>
12196 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
12197
12198 Constraint =>
12199 Make_Index_Or_Discriminant_Constraint (Loc,
12200 Constraints =>
12201 New_List (Make_Integer_Literal (Loc, 0)))))));
12202
12203 end if;
12204
12205 -- Declare static stack (that is, created by the expander) if we are
12206 -- using the Restricted run time on a bare board configuration.
12207
12208 if Restricted_Profile and then Preallocated_Stacks_On_Target then
12209
12210 -- First we need to extract the appropriate stack size
12211
12212 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
12213
12214 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12215 declare
12216 Expr_N : constant Node_Id :=
12217 Expression (First (
12218 Pragma_Argument_Associations (
12219 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
12220 Etyp : constant Entity_Id := Etype (Expr_N);
12221 P : constant Node_Id := Parent (Expr_N);
12222
12223 begin
12224 -- The stack is defined inside the corresponding record.
12225 -- Therefore if the size of the stack is set by means of
12226 -- a discriminant, we must reference the discriminant of the
12227 -- corresponding record type.
12228
12229 if Nkind (Expr_N) in N_Has_Entity
12230 and then Present (Discriminal_Link (Entity (Expr_N)))
12231 then
12232 Task_Size :=
12233 New_Occurrence_Of
12234 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
12235 Loc);
12236 Set_Parent (Task_Size, P);
12237 Set_Etype (Task_Size, Etyp);
12238 Set_Analyzed (Task_Size);
12239
12240 else
12241 Task_Size := Relocate_Node (Expr_N);
12242 end if;
12243 end;
12244
12245 else
12246 Task_Size :=
12247 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
12248 end if;
12249
12250 Decl_Stack := Make_Component_Declaration (Loc,
12251 Defining_Identifier => Ent_Stack,
12252
12253 Component_Definition =>
12254 Make_Component_Definition (Loc,
12255 Aliased_Present => True,
12256 Subtype_Indication => Make_Subtype_Indication (Loc,
12257 Subtype_Mark =>
12258 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
12259
12260 Constraint =>
12261 Make_Index_Or_Discriminant_Constraint (Loc,
12262 Constraints => New_List (Make_Range (Loc,
12263 Low_Bound => Make_Integer_Literal (Loc, 1),
12264 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12265 Task_Size)))))));
12266
12267 Append_To (Cdecls, Decl_Stack);
12268
12269 -- The appropriate alignment for the stack is ensured by the run-time
12270 -- code in charge of task creation.
12271
12272 end if;
12273
12274 -- Add components for entry families
12275
12276 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12277
12278 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12279 -- item is present.
12280
12281 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12282 Append_To (Cdecls,
12283 Make_Component_Declaration (Loc,
12284 Defining_Identifier =>
12285 Make_Defining_Identifier (Loc, Name_uPriority),
12286 Component_Definition =>
12287 Make_Component_Definition (Loc,
12288 Aliased_Present => False,
12289 Subtype_Indication =>
12290 New_Occurrence_Of (Standard_Integer, Loc))));
12291 end if;
12292
12293 -- Add the _Size component if a Storage_Size pragma is present
12294
12295 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12296 Append_To (Cdecls,
12297 Make_Component_Declaration (Loc,
12298 Defining_Identifier =>
12299 Make_Defining_Identifier (Loc, Name_uSize),
12300
12301 Component_Definition =>
12302 Make_Component_Definition (Loc,
12303 Aliased_Present => False,
12304 Subtype_Indication =>
12305 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12306
12307 Expression =>
12308 Convert_To (RTE (RE_Size_Type),
12309 Relocate_Node (
12310 Expression (First (
12311 Pragma_Argument_Associations (
12312 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12313 end if;
12314
12315 -- Add the _Task_Info component if a Task_Info pragma is present
12316
12317 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12318 Append_To (Cdecls,
12319 Make_Component_Declaration (Loc,
12320 Defining_Identifier =>
12321 Make_Defining_Identifier (Loc, Name_uTask_Info),
12322
12323 Component_Definition =>
12324 Make_Component_Definition (Loc,
12325 Aliased_Present => False,
12326 Subtype_Indication =>
12327 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12328
12329 Expression => New_Copy (
12330 Expression (First (
12331 Pragma_Argument_Associations (
12332 Get_Rep_Pragma
12333 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12334 end if;
12335
12336 -- Add the _CPU component if a CPU rep item is present
12337
12338 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12339 Append_To (Cdecls,
12340 Make_Component_Declaration (Loc,
12341 Defining_Identifier =>
12342 Make_Defining_Identifier (Loc, Name_uCPU),
12343
12344 Component_Definition =>
12345 Make_Component_Definition (Loc,
12346 Aliased_Present => False,
12347 Subtype_Indication =>
12348 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12349 end if;
12350
12351 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12352 -- present. If we are using a restricted run time this component will
12353 -- not be added (deadlines are not allowed by the Ravenscar profile).
12354
12355 if not Restricted_Profile
12356 and then Present (Taskdef)
12357 and then Has_Relative_Deadline_Pragma (Taskdef)
12358 then
12359 Append_To (Cdecls,
12360 Make_Component_Declaration (Loc,
12361 Defining_Identifier =>
12362 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12363
12364 Component_Definition =>
12365 Make_Component_Definition (Loc,
12366 Aliased_Present => False,
12367 Subtype_Indication =>
12368 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12369
12370 Expression =>
12371 Convert_To (RTE (RE_Time_Span),
12372 Relocate_Node (
12373 Expression (First (
12374 Pragma_Argument_Associations (
12375 Get_Relative_Deadline_Pragma (Taskdef))))))));
12376 end if;
12377
12378 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12379 -- item is present. If we are using a restricted run time this component
12380 -- will not be added (dispatching domains are not allowed by the
12381 -- Ravenscar profile).
12382
12383 if not Restricted_Profile
12384 and then
12385 Has_Rep_Item
12386 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12387 then
12388 Append_To (Cdecls,
12389 Make_Component_Declaration (Loc,
12390 Defining_Identifier =>
12391 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12392
12393 Component_Definition =>
12394 Make_Component_Definition (Loc,
12395 Aliased_Present => False,
12396 Subtype_Indication =>
12397 New_Occurrence_Of
12398 (RTE (RE_Dispatching_Domain_Access), Loc))));
12399 end if;
12400
12401 Insert_After (Size_Decl, Rec_Decl);
12402
12403 -- Analyze the record declaration immediately after construction,
12404 -- because the initialization procedure is needed for single task
12405 -- declarations before the next entity is analyzed.
12406
12407 Analyze (Rec_Decl);
12408
12409 -- Create the declaration of the task body procedure
12410
12411 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12412 Body_Decl :=
12413 Make_Subprogram_Declaration (Loc,
12414 Specification => Proc_Spec);
12415 Set_Is_Task_Body_Procedure (Body_Decl);
12416
12417 Insert_After (Rec_Decl, Body_Decl);
12418
12419 -- The subprogram does not comes from source, so we have to indicate the
12420 -- need for debugging information explicitly.
12421
12422 if Comes_From_Source (Original_Node (N)) then
12423 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12424 end if;
12425
12426 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12427 -- the corresponding record has been frozen.
12428
12429 if Ada_Version >= Ada_2005 then
12430 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12431 end if;
12432
12433 -- Ada 2005 (AI-345): We must defer freezing to allow further
12434 -- declaration of primitive subprograms covering task interfaces
12435
12436 if Ada_Version <= Ada_95 then
12437
12438 -- Now we can freeze the corresponding record. This needs manually
12439 -- freezing, since it is really part of the task type, and the task
12440 -- type is frozen at this stage. We of course need the initialization
12441 -- procedure for this corresponding record type and we won't get it
12442 -- in time if we don't freeze now.
12443
12444 declare
12445 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12446 begin
12447 if Is_Non_Empty_List (L) then
12448 Insert_List_After (Body_Decl, L);
12449 end if;
12450 end;
12451 end if;
12452
12453 -- Complete the expansion of access types to the current task type, if
12454 -- any were declared.
12455
12456 Expand_Previous_Access_Type (Tasktyp);
12457
12458 -- Create wrappers for entries that have contract cases, preconditions
12459 -- and postconditions.
12460
12461 declare
12462 Ent : Entity_Id;
12463
12464 begin
12465 Ent := First_Entity (Tasktyp);
12466 while Present (Ent) loop
12467 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12468 Build_Contract_Wrapper (Ent, N);
12469 end if;
12470
12471 Next_Entity (Ent);
12472 end loop;
12473 end;
12474 end Expand_N_Task_Type_Declaration;
12475
12476 -------------------------------
12477 -- Expand_N_Timed_Entry_Call --
12478 -------------------------------
12479
12480 -- A timed entry call in normal case is not implemented using ATC mechanism
12481 -- anymore for efficiency reason.
12482
12483 -- select
12484 -- T.E;
12485 -- S1;
12486 -- or
12487 -- delay D;
12488 -- S2;
12489 -- end select;
12490
12491 -- is expanded as follows:
12492
12493 -- 1) When T.E is a task entry_call;
12494
12495 -- declare
12496 -- B : Boolean;
12497 -- X : Task_Entry_Index := <entry index>;
12498 -- DX : Duration := To_Duration (D);
12499 -- M : Delay_Mode := <discriminant>;
12500 -- P : parms := (parm, parm, parm);
12501
12502 -- begin
12503 -- Timed_Protected_Entry_Call
12504 -- (<acceptor-task>, X, P'Address, DX, M, B);
12505 -- if B then
12506 -- S1;
12507 -- else
12508 -- S2;
12509 -- end if;
12510 -- end;
12511
12512 -- 2) When T.E is a protected entry_call;
12513
12514 -- declare
12515 -- B : Boolean;
12516 -- X : Protected_Entry_Index := <entry index>;
12517 -- DX : Duration := To_Duration (D);
12518 -- M : Delay_Mode := <discriminant>;
12519 -- P : parms := (parm, parm, parm);
12520
12521 -- begin
12522 -- Timed_Protected_Entry_Call
12523 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12524 -- if B then
12525 -- S1;
12526 -- else
12527 -- S2;
12528 -- end if;
12529 -- end;
12530
12531 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12532 -- is no delay and the triggering statements are executed. We first
12533 -- determine the kind of the triggering call and then execute a
12534 -- synchronized operation or a direct call.
12535
12536 -- declare
12537 -- B : Boolean := False;
12538 -- C : Ada.Tags.Prim_Op_Kind;
12539 -- DX : Duration := To_Duration (D)
12540 -- K : Ada.Tags.Tagged_Kind :=
12541 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12542 -- M : Integer :=...;
12543 -- P : Parameters := (Param1 .. ParamN);
12544 -- S : Integer;
12545
12546 -- begin
12547 -- if K = Ada.Tags.TK_Limited_Tagged
12548 -- or else K = Ada.Tags.TK_Tagged
12549 -- then
12550 -- <dispatching-call>;
12551 -- B := True;
12552
12553 -- else
12554 -- S :=
12555 -- Ada.Tags.Get_Offset_Index
12556 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12557
12558 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12559
12560 -- if C = POK_Protected_Entry
12561 -- or else C = POK_Task_Entry
12562 -- then
12563 -- Param1 := P.Param1;
12564 -- ...
12565 -- ParamN := P.ParamN;
12566 -- end if;
12567
12568 -- if B then
12569 -- if C = POK_Procedure
12570 -- or else C = POK_Protected_Procedure
12571 -- or else C = POK_Task_Procedure
12572 -- then
12573 -- <dispatching-call>;
12574 -- end if;
12575 -- end if;
12576 -- end if;
12577
12578 -- if B then
12579 -- <triggering-statements>
12580 -- else
12581 -- <timed-statements>
12582 -- end if;
12583 -- end;
12584
12585 -- The triggering statement and the sequence of timed statements have not
12586 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12587 -- global references if within an instantiation.
12588
12589 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12590 Loc : constant Source_Ptr := Sloc (N);
12591
12592 Actuals : List_Id;
12593 Blk_Typ : Entity_Id;
12594 Call : Node_Id;
12595 Call_Ent : Entity_Id;
12596 Conc_Typ_Stmts : List_Id;
12597 Concval : Node_Id;
12598 D_Alt : constant Node_Id := Delay_Alternative (N);
12599 D_Conv : Node_Id;
12600 D_Disc : Node_Id;
12601 D_Stat : Node_Id := Delay_Statement (D_Alt);
12602 D_Stats : List_Id;
12603 D_Type : Entity_Id;
12604 Decls : List_Id;
12605 Dummy : Node_Id;
12606 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12607 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12608 E_Stats : List_Id;
12609 Ename : Node_Id;
12610 Formals : List_Id;
12611 Index : Node_Id;
12612 Is_Disp_Select : Boolean;
12613 Lim_Typ_Stmts : List_Id;
12614 N_Stats : List_Id;
12615 Obj : Entity_Id;
12616 Param : Node_Id;
12617 Params : List_Id;
12618 Stmt : Node_Id;
12619 Stmts : List_Id;
12620 Unpack : List_Id;
12621
12622 B : Entity_Id; -- Call status flag
12623 C : Entity_Id; -- Call kind
12624 D : Entity_Id; -- Delay
12625 K : Entity_Id; -- Tagged kind
12626 M : Entity_Id; -- Delay mode
12627 P : Entity_Id; -- Parameter block
12628 S : Entity_Id; -- Primitive operation slot
12629
12630 -- Start of processing for Expand_N_Timed_Entry_Call
12631
12632 begin
12633 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12634 -- was already reported on spec, so do not attempt to expand the call.
12635
12636 if Restriction_Active (No_Select_Statements) then
12637 return;
12638 end if;
12639
12640 Process_Statements_For_Controlled_Objects (E_Alt);
12641 Process_Statements_For_Controlled_Objects (D_Alt);
12642
12643 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12644
12645 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12646 -- may wrap them in blocks.
12647
12648 E_Stats := Statements (E_Alt);
12649 D_Stats := Statements (D_Alt);
12650
12651 -- The arguments in the call may require dynamic allocation, and the
12652 -- call statement may have been transformed into a block. The block
12653 -- may contain additional declarations for internal entities, and the
12654 -- original call is found by sequential search.
12655
12656 if Nkind (E_Call) = N_Block_Statement then
12657 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12658 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12659 N_Entry_Call_Statement)
12660 loop
12661 Next (E_Call);
12662 end loop;
12663 end if;
12664
12665 Is_Disp_Select :=
12666 Ada_Version >= Ada_2005
12667 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12668
12669 if Is_Disp_Select then
12670 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12671 Decls := New_List;
12672
12673 Stmts := New_List;
12674
12675 -- Generate:
12676 -- B : Boolean := False;
12677
12678 B := Build_B (Loc, Decls);
12679
12680 -- Generate:
12681 -- C : Ada.Tags.Prim_Op_Kind;
12682
12683 C := Build_C (Loc, Decls);
12684
12685 -- Because the analysis of all statements was disabled, manually
12686 -- analyze the delay statement.
12687
12688 Analyze (D_Stat);
12689 D_Stat := Original_Node (D_Stat);
12690
12691 else
12692 -- Build an entry call using Simple_Entry_Call
12693
12694 Extract_Entry (E_Call, Concval, Ename, Index);
12695 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12696
12697 Decls := Declarations (E_Call);
12698 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12699
12700 if No (Decls) then
12701 Decls := New_List;
12702 end if;
12703
12704 -- Generate:
12705 -- B : Boolean;
12706
12707 B := Make_Defining_Identifier (Loc, Name_uB);
12708
12709 Prepend_To (Decls,
12710 Make_Object_Declaration (Loc,
12711 Defining_Identifier => B,
12712 Object_Definition =>
12713 New_Occurrence_Of (Standard_Boolean, Loc)));
12714 end if;
12715
12716 -- Duration and mode processing
12717
12718 D_Type := Base_Type (Etype (Expression (D_Stat)));
12719
12720 -- Use the type of the delay expression (Calendar or Real_Time) to
12721 -- generate the appropriate conversion.
12722
12723 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12724 D_Disc := Make_Integer_Literal (Loc, 0);
12725 D_Conv := Relocate_Node (Expression (D_Stat));
12726
12727 elsif Is_RTE (D_Type, RO_CA_Time) then
12728 D_Disc := Make_Integer_Literal (Loc, 1);
12729 D_Conv :=
12730 Make_Function_Call (Loc,
12731 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12732 Parameter_Associations =>
12733 New_List (New_Copy (Expression (D_Stat))));
12734
12735 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12736 D_Disc := Make_Integer_Literal (Loc, 2);
12737 D_Conv :=
12738 Make_Function_Call (Loc,
12739 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12740 Parameter_Associations =>
12741 New_List (New_Copy (Expression (D_Stat))));
12742 end if;
12743
12744 D := Make_Temporary (Loc, 'D');
12745
12746 -- Generate:
12747 -- D : Duration;
12748
12749 Append_To (Decls,
12750 Make_Object_Declaration (Loc,
12751 Defining_Identifier => D,
12752 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12753
12754 M := Make_Temporary (Loc, 'M');
12755
12756 -- Generate:
12757 -- M : Integer := (0 | 1 | 2);
12758
12759 Append_To (Decls,
12760 Make_Object_Declaration (Loc,
12761 Defining_Identifier => M,
12762 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12763 Expression => D_Disc));
12764
12765 -- Do the assignment at this stage only because the evaluation of the
12766 -- expression must not occur before (see ACVC C97302A).
12767
12768 Append_To (Stmts,
12769 Make_Assignment_Statement (Loc,
12770 Name => New_Occurrence_Of (D, Loc),
12771 Expression => D_Conv));
12772
12773 -- Parameter block processing
12774
12775 -- Manually create the parameter block for dispatching calls. In the
12776 -- case of entries, the block has already been created during the call
12777 -- to Build_Simple_Entry_Call.
12778
12779 if Is_Disp_Select then
12780
12781 -- Tagged kind processing, generate:
12782 -- K : Ada.Tags.Tagged_Kind :=
12783 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12784
12785 K := Build_K (Loc, Decls, Obj);
12786
12787 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12788 P :=
12789 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12790
12791 -- Dispatch table slot processing, generate:
12792 -- S : Integer;
12793
12794 S := Build_S (Loc, Decls);
12795
12796 -- Generate:
12797 -- S := Ada.Tags.Get_Offset_Index
12798 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12799
12800 Conc_Typ_Stmts :=
12801 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12802
12803 -- Generate:
12804 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12805
12806 -- where Obj is the controlling formal parameter, S is the dispatch
12807 -- table slot number of the dispatching operation, P is the wrapped
12808 -- parameter block, D is the duration, M is the duration mode, C is
12809 -- the call kind and B is the call status.
12810
12811 Params := New_List;
12812
12813 Append_To (Params, New_Copy_Tree (Obj));
12814 Append_To (Params, New_Occurrence_Of (S, Loc));
12815 Append_To (Params,
12816 Make_Attribute_Reference (Loc,
12817 Prefix => New_Occurrence_Of (P, Loc),
12818 Attribute_Name => Name_Address));
12819 Append_To (Params, New_Occurrence_Of (D, Loc));
12820 Append_To (Params, New_Occurrence_Of (M, Loc));
12821 Append_To (Params, New_Occurrence_Of (C, Loc));
12822 Append_To (Params, New_Occurrence_Of (B, Loc));
12823
12824 Append_To (Conc_Typ_Stmts,
12825 Make_Procedure_Call_Statement (Loc,
12826 Name =>
12827 New_Occurrence_Of
12828 (Find_Prim_Op
12829 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12830 Parameter_Associations => Params));
12831
12832 -- Generate:
12833 -- if C = POK_Protected_Entry
12834 -- or else C = POK_Task_Entry
12835 -- then
12836 -- Param1 := P.Param1;
12837 -- ...
12838 -- ParamN := P.ParamN;
12839 -- end if;
12840
12841 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12842
12843 -- Generate the if statement only when the packed parameters need
12844 -- explicit assignments to their corresponding actuals.
12845
12846 if Present (Unpack) then
12847 Append_To (Conc_Typ_Stmts,
12848 Make_Implicit_If_Statement (N,
12849
12850 Condition =>
12851 Make_Or_Else (Loc,
12852 Left_Opnd =>
12853 Make_Op_Eq (Loc,
12854 Left_Opnd => New_Occurrence_Of (C, Loc),
12855 Right_Opnd =>
12856 New_Occurrence_Of
12857 (RTE (RE_POK_Protected_Entry), Loc)),
12858
12859 Right_Opnd =>
12860 Make_Op_Eq (Loc,
12861 Left_Opnd => New_Occurrence_Of (C, Loc),
12862 Right_Opnd =>
12863 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12864
12865 Then_Statements => Unpack));
12866 end if;
12867
12868 -- Generate:
12869
12870 -- if B then
12871 -- if C = POK_Procedure
12872 -- or else C = POK_Protected_Procedure
12873 -- or else C = POK_Task_Procedure
12874 -- then
12875 -- <dispatching-call>
12876 -- end if;
12877 -- end if;
12878
12879 N_Stats := New_List (
12880 Make_Implicit_If_Statement (N,
12881 Condition =>
12882 Make_Or_Else (Loc,
12883 Left_Opnd =>
12884 Make_Op_Eq (Loc,
12885 Left_Opnd => New_Occurrence_Of (C, Loc),
12886 Right_Opnd =>
12887 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12888
12889 Right_Opnd =>
12890 Make_Or_Else (Loc,
12891 Left_Opnd =>
12892 Make_Op_Eq (Loc,
12893 Left_Opnd => New_Occurrence_Of (C, Loc),
12894 Right_Opnd =>
12895 New_Occurrence_Of (RTE (
12896 RE_POK_Protected_Procedure), Loc)),
12897 Right_Opnd =>
12898 Make_Op_Eq (Loc,
12899 Left_Opnd => New_Occurrence_Of (C, Loc),
12900 Right_Opnd =>
12901 New_Occurrence_Of
12902 (RTE (RE_POK_Task_Procedure), Loc)))),
12903
12904 Then_Statements => New_List (E_Call)));
12905
12906 Append_To (Conc_Typ_Stmts,
12907 Make_Implicit_If_Statement (N,
12908 Condition => New_Occurrence_Of (B, Loc),
12909 Then_Statements => N_Stats));
12910
12911 -- Generate:
12912 -- <dispatching-call>;
12913 -- B := True;
12914
12915 Lim_Typ_Stmts :=
12916 New_List (New_Copy_Tree (E_Call),
12917 Make_Assignment_Statement (Loc,
12918 Name => New_Occurrence_Of (B, Loc),
12919 Expression => New_Occurrence_Of (Standard_True, Loc)));
12920
12921 -- Generate:
12922 -- if K = Ada.Tags.TK_Limited_Tagged
12923 -- or else K = Ada.Tags.TK_Tagged
12924 -- then
12925 -- Lim_Typ_Stmts
12926 -- else
12927 -- Conc_Typ_Stmts
12928 -- end if;
12929
12930 Append_To (Stmts,
12931 Make_Implicit_If_Statement (N,
12932 Condition => Build_Dispatching_Tag_Check (K, N),
12933 Then_Statements => Lim_Typ_Stmts,
12934 Else_Statements => Conc_Typ_Stmts));
12935
12936 -- Generate:
12937
12938 -- if B then
12939 -- <triggering-statements>
12940 -- else
12941 -- <timed-statements>
12942 -- end if;
12943
12944 Append_To (Stmts,
12945 Make_Implicit_If_Statement (N,
12946 Condition => New_Occurrence_Of (B, Loc),
12947 Then_Statements => E_Stats,
12948 Else_Statements => D_Stats));
12949
12950 else
12951 -- Simple case of a nondispatching trigger. Skip assignments to
12952 -- temporaries created for in-out parameters.
12953
12954 -- This makes unwarranted assumptions about the shape of the expanded
12955 -- tree for the call, and should be cleaned up ???
12956
12957 Stmt := First (Stmts);
12958 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12959 Next (Stmt);
12960 end loop;
12961
12962 -- Do the assignment at this stage only because the evaluation
12963 -- of the expression must not occur before (see ACVC C97302A).
12964
12965 Insert_Before (Stmt,
12966 Make_Assignment_Statement (Loc,
12967 Name => New_Occurrence_Of (D, Loc),
12968 Expression => D_Conv));
12969
12970 Call := Stmt;
12971 Params := Parameter_Associations (Call);
12972
12973 -- For a protected type, we build a Timed_Protected_Entry_Call
12974
12975 if Is_Protected_Type (Etype (Concval)) then
12976
12977 -- Create a new call statement
12978
12979 Param := First (Params);
12980 while Present (Param)
12981 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12982 loop
12983 Next (Param);
12984 end loop;
12985
12986 Dummy := Remove_Next (Next (Param));
12987
12988 -- Remove garbage is following the Cancel_Param if present
12989
12990 Dummy := Next (Param);
12991
12992 -- Remove the mode of the Protected_Entry_Call call, then remove
12993 -- the Communication_Block of the Protected_Entry_Call call, and
12994 -- finally add Duration and a Delay_Mode parameter
12995
12996 pragma Assert (Present (Param));
12997 Rewrite (Param, New_Occurrence_Of (D, Loc));
12998
12999 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
13000
13001 -- Add a Boolean flag for successful entry call
13002
13003 Append_To (Params, New_Occurrence_Of (B, Loc));
13004
13005 case Corresponding_Runtime_Package (Etype (Concval)) is
13006 when System_Tasking_Protected_Objects_Entries =>
13007 Rewrite (Call,
13008 Make_Procedure_Call_Statement (Loc,
13009 Name =>
13010 New_Occurrence_Of
13011 (RTE (RE_Timed_Protected_Entry_Call), Loc),
13012 Parameter_Associations => Params));
13013
13014 when others =>
13015 raise Program_Error;
13016 end case;
13017
13018 -- For the task case, build a Timed_Task_Entry_Call
13019
13020 else
13021 -- Create a new call statement
13022
13023 Append_To (Params, New_Occurrence_Of (D, Loc));
13024 Append_To (Params, New_Occurrence_Of (M, Loc));
13025 Append_To (Params, New_Occurrence_Of (B, Loc));
13026
13027 Rewrite (Call,
13028 Make_Procedure_Call_Statement (Loc,
13029 Name =>
13030 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
13031 Parameter_Associations => Params));
13032 end if;
13033
13034 Append_To (Stmts,
13035 Make_Implicit_If_Statement (N,
13036 Condition => New_Occurrence_Of (B, Loc),
13037 Then_Statements => E_Stats,
13038 Else_Statements => D_Stats));
13039 end if;
13040
13041 Rewrite (N,
13042 Make_Block_Statement (Loc,
13043 Declarations => Decls,
13044 Handled_Statement_Sequence =>
13045 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
13046
13047 Analyze (N);
13048 end Expand_N_Timed_Entry_Call;
13049
13050 ----------------------------------------
13051 -- Expand_Protected_Body_Declarations --
13052 ----------------------------------------
13053
13054 procedure Expand_Protected_Body_Declarations
13055 (N : Node_Id;
13056 Spec_Id : Entity_Id)
13057 is
13058 begin
13059 if No_Run_Time_Mode then
13060 Error_Msg_CRT ("protected body", N);
13061 return;
13062
13063 elsif Expander_Active then
13064
13065 -- Associate discriminals with the first subprogram or entry body to
13066 -- be expanded.
13067
13068 if Present (First_Protected_Operation (Declarations (N))) then
13069 Set_Discriminals (Parent (Spec_Id));
13070 end if;
13071 end if;
13072 end Expand_Protected_Body_Declarations;
13073
13074 -------------------------
13075 -- External_Subprogram --
13076 -------------------------
13077
13078 function External_Subprogram (E : Entity_Id) return Entity_Id is
13079 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
13080
13081 begin
13082 -- The internal and external subprograms follow each other on the entity
13083 -- chain. Note that previously private operations had no separate
13084 -- external subprogram. We now create one in all cases, because a
13085 -- private operation may actually appear in an external call, through
13086 -- a 'Access reference used for a callback.
13087
13088 -- If the operation is a function that returns an anonymous access type,
13089 -- the corresponding itype appears before the operation, and must be
13090 -- skipped.
13091
13092 -- This mechanism is fragile, there should be a real link between the
13093 -- two versions of the operation, but there is no place to put it ???
13094
13095 if Is_Access_Type (Next_Entity (Subp)) then
13096 return Next_Entity (Next_Entity (Subp));
13097 else
13098 return Next_Entity (Subp);
13099 end if;
13100 end External_Subprogram;
13101
13102 ------------------------------
13103 -- Extract_Dispatching_Call --
13104 ------------------------------
13105
13106 procedure Extract_Dispatching_Call
13107 (N : Node_Id;
13108 Call_Ent : out Entity_Id;
13109 Object : out Entity_Id;
13110 Actuals : out List_Id;
13111 Formals : out List_Id)
13112 is
13113 Call_Nam : Node_Id;
13114
13115 begin
13116 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
13117
13118 if Present (Original_Node (N)) then
13119 Call_Nam := Name (Original_Node (N));
13120 else
13121 Call_Nam := Name (N);
13122 end if;
13123
13124 -- Retrieve the name of the dispatching procedure. It contains the
13125 -- dispatch table slot number.
13126
13127 loop
13128 case Nkind (Call_Nam) is
13129 when N_Identifier =>
13130 exit;
13131
13132 when N_Selected_Component =>
13133 Call_Nam := Selector_Name (Call_Nam);
13134
13135 when others =>
13136 raise Program_Error;
13137
13138 end case;
13139 end loop;
13140
13141 Actuals := Parameter_Associations (N);
13142 Call_Ent := Entity (Call_Nam);
13143 Formals := Parameter_Specifications (Parent (Call_Ent));
13144 Object := First (Actuals);
13145
13146 if Present (Original_Node (Object)) then
13147 Object := Original_Node (Object);
13148 end if;
13149
13150 -- If the type of the dispatching object is an access type then return
13151 -- an explicit dereference.
13152
13153 if Is_Access_Type (Etype (Object)) then
13154 Object := Make_Explicit_Dereference (Sloc (N), Object);
13155 Analyze (Object);
13156 end if;
13157 end Extract_Dispatching_Call;
13158
13159 -------------------
13160 -- Extract_Entry --
13161 -------------------
13162
13163 procedure Extract_Entry
13164 (N : Node_Id;
13165 Concval : out Node_Id;
13166 Ename : out Node_Id;
13167 Index : out Node_Id)
13168 is
13169 Nam : constant Node_Id := Name (N);
13170
13171 begin
13172 -- For a simple entry, the name is a selected component, with the
13173 -- prefix being the task value, and the selector being the entry.
13174
13175 if Nkind (Nam) = N_Selected_Component then
13176 Concval := Prefix (Nam);
13177 Ename := Selector_Name (Nam);
13178 Index := Empty;
13179
13180 -- For a member of an entry family, the name is an indexed component
13181 -- where the prefix is a selected component, whose prefix in turn is
13182 -- the task value, and whose selector is the entry family. The single
13183 -- expression in the expressions list of the indexed component is the
13184 -- subscript for the family.
13185
13186 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13187 Concval := Prefix (Prefix (Nam));
13188 Ename := Selector_Name (Prefix (Nam));
13189 Index := First (Expressions (Nam));
13190 end if;
13191
13192 -- Through indirection, the type may actually be a limited view of a
13193 -- concurrent type. When compiling a call, the non-limited view of the
13194 -- type is visible.
13195
13196 if From_Limited_With (Etype (Concval)) then
13197 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13198 end if;
13199 end Extract_Entry;
13200
13201 -------------------
13202 -- Family_Offset --
13203 -------------------
13204
13205 function Family_Offset
13206 (Loc : Source_Ptr;
13207 Hi : Node_Id;
13208 Lo : Node_Id;
13209 Ttyp : Entity_Id;
13210 Cap : Boolean) return Node_Id
13211 is
13212 Ityp : Entity_Id;
13213 Real_Hi : Node_Id;
13214 Real_Lo : Node_Id;
13215
13216 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13217 -- If one of the bounds is a reference to a discriminant, replace with
13218 -- corresponding discriminal of type. Within the body of a task retrieve
13219 -- the renamed discriminant by simple visibility, using its generated
13220 -- name. Within a protected object, find the original discriminant and
13221 -- replace it with the discriminal of the current protected operation.
13222
13223 ------------------------------
13224 -- Convert_Discriminant_Ref --
13225 ------------------------------
13226
13227 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13228 Loc : constant Source_Ptr := Sloc (Bound);
13229 B : Node_Id;
13230 D : Entity_Id;
13231
13232 begin
13233 if Is_Entity_Name (Bound)
13234 and then Ekind (Entity (Bound)) = E_Discriminant
13235 then
13236 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13237 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13238 Find_Direct_Name (B);
13239
13240 elsif Is_Protected_Type (Ttyp) then
13241 D := First_Discriminant (Ttyp);
13242 while Chars (D) /= Chars (Entity (Bound)) loop
13243 Next_Discriminant (D);
13244 end loop;
13245
13246 B := New_Occurrence_Of (Discriminal (D), Loc);
13247
13248 else
13249 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13250 end if;
13251
13252 elsif Nkind (Bound) = N_Attribute_Reference then
13253 return Bound;
13254
13255 else
13256 B := New_Copy_Tree (Bound);
13257 end if;
13258
13259 return
13260 Make_Attribute_Reference (Loc,
13261 Attribute_Name => Name_Pos,
13262 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13263 Expressions => New_List (B));
13264 end Convert_Discriminant_Ref;
13265
13266 -- Start of processing for Family_Offset
13267
13268 begin
13269 Real_Hi := Convert_Discriminant_Ref (Hi);
13270 Real_Lo := Convert_Discriminant_Ref (Lo);
13271
13272 if Cap then
13273 if Is_Task_Type (Ttyp) then
13274 Ityp := RTE (RE_Task_Entry_Index);
13275 else
13276 Ityp := RTE (RE_Protected_Entry_Index);
13277 end if;
13278
13279 Real_Hi :=
13280 Make_Attribute_Reference (Loc,
13281 Prefix => New_Occurrence_Of (Ityp, Loc),
13282 Attribute_Name => Name_Min,
13283 Expressions => New_List (
13284 Real_Hi,
13285 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13286
13287 Real_Lo :=
13288 Make_Attribute_Reference (Loc,
13289 Prefix => New_Occurrence_Of (Ityp, Loc),
13290 Attribute_Name => Name_Max,
13291 Expressions => New_List (
13292 Real_Lo,
13293 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13294 end if;
13295
13296 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13297 end Family_Offset;
13298
13299 -----------------
13300 -- Family_Size --
13301 -----------------
13302
13303 function Family_Size
13304 (Loc : Source_Ptr;
13305 Hi : Node_Id;
13306 Lo : Node_Id;
13307 Ttyp : Entity_Id;
13308 Cap : Boolean) return Node_Id
13309 is
13310 Ityp : Entity_Id;
13311
13312 begin
13313 if Is_Task_Type (Ttyp) then
13314 Ityp := RTE (RE_Task_Entry_Index);
13315 else
13316 Ityp := RTE (RE_Protected_Entry_Index);
13317 end if;
13318
13319 return
13320 Make_Attribute_Reference (Loc,
13321 Prefix => New_Occurrence_Of (Ityp, Loc),
13322 Attribute_Name => Name_Max,
13323 Expressions => New_List (
13324 Make_Op_Add (Loc,
13325 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13326 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13327 Make_Integer_Literal (Loc, 0)));
13328 end Family_Size;
13329
13330 ----------------------------
13331 -- Find_Enclosing_Context --
13332 ----------------------------
13333
13334 procedure Find_Enclosing_Context
13335 (N : Node_Id;
13336 Context : out Node_Id;
13337 Context_Id : out Entity_Id;
13338 Context_Decls : out List_Id)
13339 is
13340 begin
13341 -- Traverse the parent chain looking for an enclosing body, block,
13342 -- package or return statement.
13343
13344 Context := Parent (N);
13345 while Present (Context) loop
13346 if Nkind_In (Context, N_Entry_Body,
13347 N_Extended_Return_Statement,
13348 N_Package_Body,
13349 N_Package_Declaration,
13350 N_Subprogram_Body,
13351 N_Task_Body)
13352 then
13353 exit;
13354
13355 -- Do not consider block created to protect a list of statements with
13356 -- an Abort_Defer / Abort_Undefer_Direct pair.
13357
13358 elsif Nkind (Context) = N_Block_Statement
13359 and then not Is_Abort_Block (Context)
13360 then
13361 exit;
13362 end if;
13363
13364 Context := Parent (Context);
13365 end loop;
13366
13367 pragma Assert (Present (Context));
13368
13369 -- Extract the constituents of the context
13370
13371 if Nkind (Context) = N_Extended_Return_Statement then
13372 Context_Decls := Return_Object_Declarations (Context);
13373 Context_Id := Return_Statement_Entity (Context);
13374
13375 -- Package declarations and bodies use a common library-level activation
13376 -- chain or task master, therefore return the package declaration as the
13377 -- proper carrier for the appropriate flag.
13378
13379 elsif Nkind (Context) = N_Package_Body then
13380 Context_Decls := Declarations (Context);
13381 Context_Id := Corresponding_Spec (Context);
13382 Context := Parent (Context_Id);
13383
13384 if Nkind (Context) = N_Defining_Program_Unit_Name then
13385 Context := Parent (Parent (Context));
13386 else
13387 Context := Parent (Context);
13388 end if;
13389
13390 elsif Nkind (Context) = N_Package_Declaration then
13391 Context_Decls := Visible_Declarations (Specification (Context));
13392 Context_Id := Defining_Unit_Name (Specification (Context));
13393
13394 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13395 Context_Id := Defining_Identifier (Context_Id);
13396 end if;
13397
13398 else
13399 if Nkind (Context) = N_Block_Statement then
13400 Context_Id := Entity (Identifier (Context));
13401
13402 elsif Nkind (Context) = N_Entry_Body then
13403 Context_Id := Defining_Identifier (Context);
13404
13405 elsif Nkind (Context) = N_Subprogram_Body then
13406 if Present (Corresponding_Spec (Context)) then
13407 Context_Id := Corresponding_Spec (Context);
13408 else
13409 Context_Id := Defining_Unit_Name (Specification (Context));
13410
13411 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13412 Context_Id := Defining_Identifier (Context_Id);
13413 end if;
13414 end if;
13415
13416 elsif Nkind (Context) = N_Task_Body then
13417 Context_Id := Corresponding_Spec (Context);
13418
13419 else
13420 raise Program_Error;
13421 end if;
13422
13423 Context_Decls := Declarations (Context);
13424 end if;
13425
13426 pragma Assert (Present (Context_Id));
13427 pragma Assert (Present (Context_Decls));
13428 end Find_Enclosing_Context;
13429
13430 -----------------------
13431 -- Find_Master_Scope --
13432 -----------------------
13433
13434 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13435 S : Entity_Id;
13436
13437 begin
13438 -- In Ada 2005, the master is the innermost enclosing scope that is not
13439 -- transient. If the enclosing block is the rewriting of a call or the
13440 -- scope is an extended return statement this is valid master. The
13441 -- master in an extended return is only used within the return, and is
13442 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13443 -- now before that overwriting occurs.
13444
13445 S := Scope (E);
13446
13447 if Ada_Version >= Ada_2005 then
13448 while Is_Internal (S) loop
13449 if Nkind (Parent (S)) = N_Block_Statement
13450 and then
13451 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13452 then
13453 exit;
13454
13455 elsif Ekind (S) = E_Return_Statement then
13456 exit;
13457
13458 else
13459 S := Scope (S);
13460 end if;
13461 end loop;
13462 end if;
13463
13464 return S;
13465 end Find_Master_Scope;
13466
13467 -------------------------------
13468 -- First_Protected_Operation --
13469 -------------------------------
13470
13471 function First_Protected_Operation (D : List_Id) return Node_Id is
13472 First_Op : Node_Id;
13473
13474 begin
13475 First_Op := First (D);
13476 while Present (First_Op)
13477 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13478 loop
13479 Next (First_Op);
13480 end loop;
13481
13482 return First_Op;
13483 end First_Protected_Operation;
13484
13485 ---------------------------------------
13486 -- Install_Private_Data_Declarations --
13487 ---------------------------------------
13488
13489 procedure Install_Private_Data_Declarations
13490 (Loc : Source_Ptr;
13491 Spec_Id : Entity_Id;
13492 Conc_Typ : Entity_Id;
13493 Body_Nod : Node_Id;
13494 Decls : List_Id;
13495 Barrier : Boolean := False;
13496 Family : Boolean := False)
13497 is
13498 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13499 Decl : Node_Id;
13500 Def : Node_Id;
13501 Insert_Node : Node_Id := Empty;
13502 Obj_Ent : Entity_Id;
13503
13504 procedure Add (Decl : Node_Id);
13505 -- Add a single declaration after Insert_Node. If this is the first
13506 -- addition, Decl is added to the front of Decls and it becomes the
13507 -- insertion node.
13508
13509 function Replace_Bound (Bound : Node_Id) return Node_Id;
13510 -- The bounds of an entry index may depend on discriminants, create a
13511 -- reference to the corresponding prival. Otherwise return a duplicate
13512 -- of the original bound.
13513
13514 ---------
13515 -- Add --
13516 ---------
13517
13518 procedure Add (Decl : Node_Id) is
13519 begin
13520 if No (Insert_Node) then
13521 Prepend_To (Decls, Decl);
13522 else
13523 Insert_After (Insert_Node, Decl);
13524 end if;
13525
13526 Insert_Node := Decl;
13527 end Add;
13528
13529 --------------------------
13530 -- Replace_Discriminant --
13531 --------------------------
13532
13533 function Replace_Bound (Bound : Node_Id) return Node_Id is
13534 begin
13535 if Nkind (Bound) = N_Identifier
13536 and then Is_Discriminal (Entity (Bound))
13537 then
13538 return Make_Identifier (Loc, Chars (Entity (Bound)));
13539 else
13540 return Duplicate_Subexpr (Bound);
13541 end if;
13542 end Replace_Bound;
13543
13544 -- Start of processing for Install_Private_Data_Declarations
13545
13546 begin
13547 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13548 -- formal parameter _O, _object or _task depending on the context.
13549
13550 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13551
13552 -- Special processing of _O for barrier functions, protected entries
13553 -- and families.
13554
13555 if Barrier
13556 or else
13557 (Is_Protected
13558 and then
13559 (Ekind (Spec_Id) = E_Entry
13560 or else Ekind (Spec_Id) = E_Entry_Family))
13561 then
13562 declare
13563 Conc_Rec : constant Entity_Id :=
13564 Corresponding_Record_Type (Conc_Typ);
13565 Typ_Id : constant Entity_Id :=
13566 Make_Defining_Identifier (Loc,
13567 New_External_Name (Chars (Conc_Rec), 'P'));
13568 begin
13569 -- Generate:
13570 -- type prot_typVP is access prot_typV;
13571
13572 Decl :=
13573 Make_Full_Type_Declaration (Loc,
13574 Defining_Identifier => Typ_Id,
13575 Type_Definition =>
13576 Make_Access_To_Object_Definition (Loc,
13577 Subtype_Indication =>
13578 New_Occurrence_Of (Conc_Rec, Loc)));
13579 Add (Decl);
13580
13581 -- Generate:
13582 -- _object : prot_typVP := prot_typV (_O);
13583
13584 Decl :=
13585 Make_Object_Declaration (Loc,
13586 Defining_Identifier =>
13587 Make_Defining_Identifier (Loc, Name_uObject),
13588 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13589 Expression =>
13590 Unchecked_Convert_To (Typ_Id,
13591 New_Occurrence_Of (Obj_Ent, Loc)));
13592 Add (Decl);
13593
13594 -- Set the reference to the concurrent object
13595
13596 Obj_Ent := Defining_Identifier (Decl);
13597 end;
13598 end if;
13599
13600 -- Step 2: Create the Protection object and build its declaration for
13601 -- any protected entry (family) of subprogram. Note for the lock-free
13602 -- implementation, the Protection object is not needed anymore.
13603
13604 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13605 declare
13606 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13607 Prot_Typ : RE_Id;
13608
13609 begin
13610 Set_Protection_Object (Spec_Id, Prot_Ent);
13611
13612 -- Determine the proper protection type
13613
13614 if Has_Attach_Handler (Conc_Typ)
13615 and then not Restricted_Profile
13616 then
13617 Prot_Typ := RE_Static_Interrupt_Protection;
13618
13619 elsif Has_Interrupt_Handler (Conc_Typ)
13620 and then not Restriction_Active (No_Dynamic_Attachment)
13621 then
13622 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13623
13624 else
13625 case Corresponding_Runtime_Package (Conc_Typ) is
13626 when System_Tasking_Protected_Objects_Entries =>
13627 Prot_Typ := RE_Protection_Entries;
13628
13629 when System_Tasking_Protected_Objects_Single_Entry =>
13630 Prot_Typ := RE_Protection_Entry;
13631
13632 when System_Tasking_Protected_Objects =>
13633 Prot_Typ := RE_Protection;
13634
13635 when others =>
13636 raise Program_Error;
13637 end case;
13638 end if;
13639
13640 -- Generate:
13641 -- conc_typR : protection_typ renames _object._object;
13642
13643 Decl :=
13644 Make_Object_Renaming_Declaration (Loc,
13645 Defining_Identifier => Prot_Ent,
13646 Subtype_Mark =>
13647 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13648 Name =>
13649 Make_Selected_Component (Loc,
13650 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13651 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13652 Add (Decl);
13653 end;
13654 end if;
13655
13656 -- Step 3: Add discriminant renamings (if any)
13657
13658 if Has_Discriminants (Conc_Typ) then
13659 declare
13660 D : Entity_Id;
13661
13662 begin
13663 D := First_Discriminant (Conc_Typ);
13664 while Present (D) loop
13665
13666 -- Adjust the source location
13667
13668 Set_Sloc (Discriminal (D), Loc);
13669
13670 -- Generate:
13671 -- discr_name : discr_typ renames _object.discr_name;
13672 -- or
13673 -- discr_name : discr_typ renames _task.discr_name;
13674
13675 Decl :=
13676 Make_Object_Renaming_Declaration (Loc,
13677 Defining_Identifier => Discriminal (D),
13678 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13679 Name =>
13680 Make_Selected_Component (Loc,
13681 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13682 Selector_Name => Make_Identifier (Loc, Chars (D))));
13683 Add (Decl);
13684
13685 Next_Discriminant (D);
13686 end loop;
13687 end;
13688 end if;
13689
13690 -- Step 4: Add private component renamings (if any)
13691
13692 if Is_Protected then
13693 Def := Protected_Definition (Parent (Conc_Typ));
13694
13695 if Present (Private_Declarations (Def)) then
13696 declare
13697 Comp : Node_Id;
13698 Comp_Id : Entity_Id;
13699 Decl_Id : Entity_Id;
13700
13701 begin
13702 Comp := First (Private_Declarations (Def));
13703 while Present (Comp) loop
13704 if Nkind (Comp) = N_Component_Declaration then
13705 Comp_Id := Defining_Identifier (Comp);
13706 Decl_Id :=
13707 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13708
13709 -- Minimal decoration
13710
13711 if Ekind (Spec_Id) = E_Function then
13712 Set_Ekind (Decl_Id, E_Constant);
13713 else
13714 Set_Ekind (Decl_Id, E_Variable);
13715 end if;
13716
13717 Set_Prival (Comp_Id, Decl_Id);
13718 Set_Prival_Link (Decl_Id, Comp_Id);
13719 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13720
13721 -- Generate:
13722 -- comp_name : comp_typ renames _object.comp_name;
13723
13724 Decl :=
13725 Make_Object_Renaming_Declaration (Loc,
13726 Defining_Identifier => Decl_Id,
13727 Subtype_Mark =>
13728 New_Occurrence_Of (Etype (Comp_Id), Loc),
13729 Name =>
13730 Make_Selected_Component (Loc,
13731 Prefix =>
13732 New_Occurrence_Of (Obj_Ent, Loc),
13733 Selector_Name =>
13734 Make_Identifier (Loc, Chars (Comp_Id))));
13735 Add (Decl);
13736 end if;
13737
13738 Next (Comp);
13739 end loop;
13740 end;
13741 end if;
13742 end if;
13743
13744 -- Step 5: Add the declaration of the entry index and the associated
13745 -- type for barrier functions and entry families.
13746
13747 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13748 declare
13749 E : constant Entity_Id := Index_Object (Spec_Id);
13750 Index : constant Entity_Id :=
13751 Defining_Identifier
13752 (Entry_Index_Specification
13753 (Entry_Body_Formal_Part (Body_Nod)));
13754 Index_Con : constant Entity_Id :=
13755 Make_Defining_Identifier (Loc, Chars (Index));
13756 High : Node_Id;
13757 Index_Typ : Entity_Id;
13758 Low : Node_Id;
13759
13760 begin
13761 -- Minimal decoration
13762
13763 Set_Ekind (Index_Con, E_Constant);
13764 Set_Entry_Index_Constant (Index, Index_Con);
13765 Set_Discriminal_Link (Index_Con, Index);
13766
13767 -- Retrieve the bounds of the entry family
13768
13769 High := Type_High_Bound (Etype (Index));
13770 Low := Type_Low_Bound (Etype (Index));
13771
13772 -- In the simple case the entry family is given by a subtype
13773 -- mark and the index constant has the same type.
13774
13775 if Is_Entity_Name (Original_Node (
13776 Discrete_Subtype_Definition (Parent (Index))))
13777 then
13778 Index_Typ := Etype (Index);
13779
13780 -- Otherwise a new subtype declaration is required
13781
13782 else
13783 High := Replace_Bound (High);
13784 Low := Replace_Bound (Low);
13785
13786 Index_Typ := Make_Temporary (Loc, 'J');
13787
13788 -- Generate:
13789 -- subtype Jnn is <Etype of Index> range Low .. High;
13790
13791 Decl :=
13792 Make_Subtype_Declaration (Loc,
13793 Defining_Identifier => Index_Typ,
13794 Subtype_Indication =>
13795 Make_Subtype_Indication (Loc,
13796 Subtype_Mark =>
13797 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13798 Constraint =>
13799 Make_Range_Constraint (Loc,
13800 Range_Expression =>
13801 Make_Range (Loc, Low, High))));
13802 Add (Decl);
13803 end if;
13804
13805 Set_Etype (Index_Con, Index_Typ);
13806
13807 -- Create the object which designates the index:
13808 -- J : constant Jnn :=
13809 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13810 --
13811 -- where Jnn is the subtype created above or the original type of
13812 -- the index, _E is a formal of the protected body subprogram and
13813 -- <index expr> is the index of the first family member.
13814
13815 Decl :=
13816 Make_Object_Declaration (Loc,
13817 Defining_Identifier => Index_Con,
13818 Constant_Present => True,
13819 Object_Definition =>
13820 New_Occurrence_Of (Index_Typ, Loc),
13821
13822 Expression =>
13823 Make_Attribute_Reference (Loc,
13824 Prefix =>
13825 New_Occurrence_Of (Index_Typ, Loc),
13826 Attribute_Name => Name_Val,
13827
13828 Expressions => New_List (
13829
13830 Make_Op_Add (Loc,
13831 Left_Opnd =>
13832 Make_Op_Subtract (Loc,
13833 Left_Opnd => New_Occurrence_Of (E, Loc),
13834 Right_Opnd =>
13835 Entry_Index_Expression (Loc,
13836 Defining_Identifier (Body_Nod),
13837 Empty, Conc_Typ)),
13838
13839 Right_Opnd =>
13840 Make_Attribute_Reference (Loc,
13841 Prefix =>
13842 New_Occurrence_Of (Index_Typ, Loc),
13843 Attribute_Name => Name_Pos,
13844 Expressions => New_List (
13845 Make_Attribute_Reference (Loc,
13846 Prefix =>
13847 New_Occurrence_Of (Index_Typ, Loc),
13848 Attribute_Name => Name_First)))))));
13849 Add (Decl);
13850 end;
13851 end if;
13852 end Install_Private_Data_Declarations;
13853
13854 -----------------------
13855 -- Is_Exception_Safe --
13856 -----------------------
13857
13858 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13859
13860 function Has_Side_Effect (N : Node_Id) return Boolean;
13861 -- Return True whenever encountering a subprogram call or raise
13862 -- statement of any kind in the sequence of statements
13863
13864 ---------------------
13865 -- Has_Side_Effect --
13866 ---------------------
13867
13868 -- What is this doing buried two levels down in exp_ch9. It seems like a
13869 -- generally useful function, and indeed there may be code duplication
13870 -- going on here ???
13871
13872 function Has_Side_Effect (N : Node_Id) return Boolean is
13873 Stmt : Node_Id;
13874 Expr : Node_Id;
13875
13876 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13877 -- Indicate whether N is a subprogram call or a raise statement
13878
13879 ----------------------
13880 -- Is_Call_Or_Raise --
13881 ----------------------
13882
13883 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13884 begin
13885 return Nkind_In (N, N_Procedure_Call_Statement,
13886 N_Function_Call,
13887 N_Raise_Statement,
13888 N_Raise_Constraint_Error,
13889 N_Raise_Program_Error,
13890 N_Raise_Storage_Error);
13891 end Is_Call_Or_Raise;
13892
13893 -- Start of processing for Has_Side_Effect
13894
13895 begin
13896 Stmt := N;
13897 while Present (Stmt) loop
13898 if Is_Call_Or_Raise (Stmt) then
13899 return True;
13900 end if;
13901
13902 -- An object declaration can also contain a function call or a
13903 -- raise statement.
13904
13905 if Nkind (Stmt) = N_Object_Declaration then
13906 Expr := Expression (Stmt);
13907
13908 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13909 return True;
13910 end if;
13911 end if;
13912
13913 Next (Stmt);
13914 end loop;
13915
13916 return False;
13917 end Has_Side_Effect;
13918
13919 -- Start of processing for Is_Exception_Safe
13920
13921 begin
13922 -- When exceptions can't be propagated, the subprogram returns normally
13923
13924 if No_Exception_Handlers_Set then
13925 return True;
13926 end if;
13927
13928 -- If the checks handled by the back end are not disabled, we cannot
13929 -- ensure that no exception will be raised.
13930
13931 if not Access_Checks_Suppressed (Empty)
13932 or else not Discriminant_Checks_Suppressed (Empty)
13933 or else not Range_Checks_Suppressed (Empty)
13934 or else not Index_Checks_Suppressed (Empty)
13935 or else Opt.Stack_Checking_Enabled
13936 then
13937 return False;
13938 end if;
13939
13940 if Has_Side_Effect (First (Declarations (Subprogram)))
13941 or else
13942 Has_Side_Effect
13943 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13944 then
13945 return False;
13946 else
13947 return True;
13948 end if;
13949 end Is_Exception_Safe;
13950
13951 ---------------------------------
13952 -- Is_Potentially_Large_Family --
13953 ---------------------------------
13954
13955 function Is_Potentially_Large_Family
13956 (Base_Index : Entity_Id;
13957 Conctyp : Entity_Id;
13958 Lo : Node_Id;
13959 Hi : Node_Id) return Boolean
13960 is
13961 begin
13962 return Scope (Base_Index) = Standard_Standard
13963 and then Base_Index = Base_Type (Standard_Integer)
13964 and then Has_Discriminants (Conctyp)
13965 and then
13966 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13967 and then
13968 (Denotes_Discriminant (Lo, True)
13969 or else
13970 Denotes_Discriminant (Hi, True));
13971 end Is_Potentially_Large_Family;
13972
13973 -------------------------------------
13974 -- Is_Private_Primitive_Subprogram --
13975 -------------------------------------
13976
13977 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13978 begin
13979 return
13980 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13981 and then Is_Private_Primitive (Id);
13982 end Is_Private_Primitive_Subprogram;
13983
13984 ------------------
13985 -- Index_Object --
13986 ------------------
13987
13988 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13989 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13990 Formal : Entity_Id;
13991
13992 begin
13993 Formal := First_Formal (Bod_Subp);
13994 while Present (Formal) loop
13995
13996 -- Look for formal parameter _E
13997
13998 if Chars (Formal) = Name_uE then
13999 return Formal;
14000 end if;
14001
14002 Next_Formal (Formal);
14003 end loop;
14004
14005 -- A protected body subprogram should always have the parameter in
14006 -- question.
14007
14008 raise Program_Error;
14009 end Index_Object;
14010
14011 --------------------------------
14012 -- Make_Initialize_Protection --
14013 --------------------------------
14014
14015 function Make_Initialize_Protection
14016 (Protect_Rec : Entity_Id) return List_Id
14017 is
14018 Loc : constant Source_Ptr := Sloc (Protect_Rec);
14019 P_Arr : Entity_Id;
14020 Pdec : Node_Id;
14021 Ptyp : constant Node_Id :=
14022 Corresponding_Concurrent_Type (Protect_Rec);
14023 Args : List_Id;
14024 L : constant List_Id := New_List;
14025 Has_Entry : constant Boolean := Has_Entries (Ptyp);
14026 Prio_Type : Entity_Id;
14027 Prio_Var : Entity_Id := Empty;
14028 Restricted : constant Boolean := Restricted_Profile;
14029
14030 begin
14031 -- We may need two calls to properly initialize the object, one to
14032 -- Initialize_Protection, and possibly one to Install_Handlers if we
14033 -- have a pragma Attach_Handler.
14034
14035 -- Get protected declaration. In the case of a task type declaration,
14036 -- this is simply the parent of the protected type entity. In the single
14037 -- protected object declaration, this parent will be the implicit type,
14038 -- and we can find the corresponding single protected object declaration
14039 -- by searching forward in the declaration list in the tree.
14040
14041 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
14042 -- of this type should have been removed during semantic analysis.
14043
14044 Pdec := Parent (Ptyp);
14045 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
14046 N_Single_Protected_Declaration)
14047 loop
14048 Next (Pdec);
14049 end loop;
14050
14051 -- Build the parameter list for the call. Note that _Init is the name
14052 -- of the formal for the object to be initialized, which is the task
14053 -- value record itself.
14054
14055 Args := New_List;
14056
14057 -- For lock-free implementation, skip initializations of the Protection
14058 -- object.
14059
14060 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14061
14062 -- Object parameter. This is a pointer to the object of type
14063 -- Protection used by the GNARL to control the protected object.
14064
14065 Append_To (Args,
14066 Make_Attribute_Reference (Loc,
14067 Prefix =>
14068 Make_Selected_Component (Loc,
14069 Prefix => Make_Identifier (Loc, Name_uInit),
14070 Selector_Name => Make_Identifier (Loc, Name_uObject)),
14071 Attribute_Name => Name_Unchecked_Access));
14072
14073 -- Priority parameter. Set to Unspecified_Priority unless there is a
14074 -- Priority rep item, in which case we take the value from the pragma
14075 -- or attribute definition clause, or there is an Interrupt_Priority
14076 -- rep item and no Priority rep item, and we set the ceiling to
14077 -- Interrupt_Priority'Last, an implementation-defined value, see
14078 -- (RM D.3(10)).
14079
14080 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
14081 declare
14082 Prio_Clause : constant Node_Id :=
14083 Get_Rep_Item
14084 (Ptyp, Name_Priority, Check_Parents => False);
14085
14086 Prio : Node_Id;
14087
14088 begin
14089 -- Pragma Priority
14090
14091 if Nkind (Prio_Clause) = N_Pragma then
14092 Prio :=
14093 Expression
14094 (First (Pragma_Argument_Associations (Prio_Clause)));
14095
14096 -- Get_Rep_Item returns either priority pragma.
14097
14098 if Pragma_Name (Prio_Clause) = Name_Priority then
14099 Prio_Type := RTE (RE_Any_Priority);
14100 else
14101 Prio_Type := RTE (RE_Interrupt_Priority);
14102 end if;
14103
14104 -- Attribute definition clause Priority
14105
14106 else
14107 if Chars (Prio_Clause) = Name_Priority then
14108 Prio_Type := RTE (RE_Any_Priority);
14109 else
14110 Prio_Type := RTE (RE_Interrupt_Priority);
14111 end if;
14112
14113 Prio := Expression (Prio_Clause);
14114 end if;
14115
14116 -- Always create a locale variable to capture the priority.
14117 -- The priority is also passed to Install_Restriced_Handlers.
14118 -- Note that it is really necessary to create this variable
14119 -- explicitly. It might be thought that removing side effects
14120 -- would the appropriate approach, but that could generate
14121 -- declarations improperly placed in the enclosing scope.
14122
14123 Prio_Var := Make_Temporary (Loc, 'R', Prio);
14124 Append_To (L,
14125 Make_Object_Declaration (Loc,
14126 Defining_Identifier => Prio_Var,
14127 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
14128 Expression => Relocate_Node (Prio)));
14129
14130 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14131 end;
14132
14133 -- When no priority is specified but an xx_Handler pragma is, we
14134 -- default to System.Interrupts.Default_Interrupt_Priority, see
14135 -- D.3(10).
14136
14137 elsif Has_Attach_Handler (Ptyp)
14138 or else Has_Interrupt_Handler (Ptyp)
14139 then
14140 Append_To (Args,
14141 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
14142
14143 -- Normal case, no priority or xx_Handler specified, default priority
14144
14145 else
14146 Append_To (Args,
14147 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14148 end if;
14149
14150 -- Test for Compiler_Info parameter. This parameter allows entry body
14151 -- procedures and barrier functions to be called from the runtime. It
14152 -- is a pointer to the record generated by the compiler to represent
14153 -- the protected object.
14154
14155 -- A protected type without entries that covers an interface and
14156 -- overrides the abstract routines with protected procedures is
14157 -- considered equivalent to a protected type with entries in the
14158 -- context of dispatching select statements.
14159
14160 -- Protected types with interrupt handlers (when not using a
14161 -- restricted profile) are also considered equivalent to protected
14162 -- types with entries.
14163
14164 -- The types which are used (Static_Interrupt_Protection and
14165 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
14166
14167 declare
14168 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
14169
14170 Called_Subp : RE_Id;
14171
14172 begin
14173 case Pkg_Id is
14174 when System_Tasking_Protected_Objects_Entries =>
14175 Called_Subp := RE_Initialize_Protection_Entries;
14176
14177 -- Argument Compiler_Info
14178
14179 Append_To (Args,
14180 Make_Attribute_Reference (Loc,
14181 Prefix => Make_Identifier (Loc, Name_uInit),
14182 Attribute_Name => Name_Address));
14183
14184 when System_Tasking_Protected_Objects_Single_Entry =>
14185 Called_Subp := RE_Initialize_Protection_Entry;
14186
14187 -- Argument Compiler_Info
14188
14189 Append_To (Args,
14190 Make_Attribute_Reference (Loc,
14191 Prefix => Make_Identifier (Loc, Name_uInit),
14192 Attribute_Name => Name_Address));
14193
14194 when System_Tasking_Protected_Objects =>
14195 Called_Subp := RE_Initialize_Protection;
14196
14197 when others =>
14198 raise Program_Error;
14199 end case;
14200
14201 -- Entry_Queue_Maxes parameter. This is an access to an array of
14202 -- naturals representing the entry queue maximums for each entry
14203 -- in the protected type. Zero represents no max. The access is
14204 -- null if there is no limit for all entries (usual case).
14205
14206 if Has_Entry
14207 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
14208 then
14209 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
14210 Append_To (Args,
14211 Make_Attribute_Reference (Loc,
14212 Prefix =>
14213 New_Occurrence_Of
14214 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
14215 Attribute_Name => Name_Unrestricted_Access));
14216 else
14217 Append_To (Args, Make_Null (Loc));
14218 end if;
14219
14220 -- Edge cases exist where entry initialization functions are
14221 -- called, but no entries exist, so null is appended.
14222
14223 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14224 Append_To (Args, Make_Null (Loc));
14225 end if;
14226
14227 -- Entry_Bodies parameter. This is a pointer to an array of
14228 -- pointers to the entry body procedures and barrier functions of
14229 -- the object. If the protected type has no entries this object
14230 -- will not exist, in this case, pass a null (it can happen when
14231 -- there are protected interrupt handlers or interfaces).
14232
14233 if Has_Entry then
14234 P_Arr := Entry_Bodies_Array (Ptyp);
14235
14236 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14237 -- multiple entries).
14238
14239 Append_To (Args,
14240 Make_Attribute_Reference (Loc,
14241 Prefix => New_Occurrence_Of (P_Arr, Loc),
14242 Attribute_Name => Name_Unrestricted_Access));
14243
14244 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14245
14246 -- Find index mapping function (clumsy but ok for now)
14247
14248 while Ekind (P_Arr) /= E_Function loop
14249 Next_Entity (P_Arr);
14250 end loop;
14251
14252 Append_To (Args,
14253 Make_Attribute_Reference (Loc,
14254 Prefix => New_Occurrence_Of (P_Arr, Loc),
14255 Attribute_Name => Name_Unrestricted_Access));
14256 end if;
14257
14258 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14259
14260 -- This is the case where we have a protected object with
14261 -- interfaces and no entries, and the single entry restriction
14262 -- is in effect. We pass a null pointer for the entry
14263 -- parameter because there is no actual entry.
14264
14265 Append_To (Args, Make_Null (Loc));
14266
14267 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14268
14269 -- This is the case where we have a protected object with no
14270 -- entries and:
14271 -- - either interrupt handlers with non restricted profile,
14272 -- - or interfaces
14273 -- Note that the types which are used for interrupt handlers
14274 -- (Static/Dynamic_Interrupt_Protection) are derived from
14275 -- Protection_Entries. We pass two null pointers because there
14276 -- is no actual entry, and the initialization procedure needs
14277 -- both Entry_Bodies and Find_Body_Index.
14278
14279 Append_To (Args, Make_Null (Loc));
14280 Append_To (Args, Make_Null (Loc));
14281 end if;
14282
14283 Append_To (L,
14284 Make_Procedure_Call_Statement (Loc,
14285 Name =>
14286 New_Occurrence_Of (RTE (Called_Subp), Loc),
14287 Parameter_Associations => Args));
14288 end;
14289 end if;
14290
14291 if Has_Attach_Handler (Ptyp) then
14292
14293 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14294 -- make the following call:
14295
14296 -- Install_Handlers (_object,
14297 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14298
14299 -- or, in the case of Ravenscar:
14300
14301 -- Install_Restricted_Handlers
14302 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14303
14304 declare
14305 Args : constant List_Id := New_List;
14306 Table : constant List_Id := New_List;
14307 Ritem : Node_Id := First_Rep_Item (Ptyp);
14308
14309 begin
14310 -- Build the Priority parameter (only for ravenscar)
14311
14312 if Restricted then
14313
14314 -- Priority comes from a pragma
14315
14316 if Present (Prio_Var) then
14317 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14318
14319 -- Priority is the default one
14320
14321 else
14322 Append_To (Args,
14323 New_Occurrence_Of
14324 (RTE (RE_Default_Interrupt_Priority), Loc));
14325 end if;
14326 end if;
14327
14328 -- Build the Attach_Handler table argument
14329
14330 while Present (Ritem) loop
14331 if Nkind (Ritem) = N_Pragma
14332 and then Pragma_Name (Ritem) = Name_Attach_Handler
14333 then
14334 declare
14335 Handler : constant Node_Id :=
14336 First (Pragma_Argument_Associations (Ritem));
14337
14338 Interrupt : constant Node_Id := Next (Handler);
14339 Expr : constant Node_Id := Expression (Interrupt);
14340
14341 begin
14342 Append_To (Table,
14343 Make_Aggregate (Loc, Expressions => New_List (
14344 Unchecked_Convert_To
14345 (RTE (RE_System_Interrupt_Id), Expr),
14346 Make_Attribute_Reference (Loc,
14347 Prefix =>
14348 Make_Selected_Component (Loc,
14349 Prefix =>
14350 Make_Identifier (Loc, Name_uInit),
14351 Selector_Name =>
14352 Duplicate_Subexpr_No_Checks
14353 (Expression (Handler))),
14354 Attribute_Name => Name_Access))));
14355 end;
14356 end if;
14357
14358 Next_Rep_Item (Ritem);
14359 end loop;
14360
14361 -- Append the table argument we just built
14362
14363 Append_To (Args, Make_Aggregate (Loc, Table));
14364
14365 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14366 -- call to the statements.
14367
14368 if Restricted then
14369 -- Call a simplified version of Install_Handlers to be used
14370 -- when the Ravenscar restrictions are in effect
14371 -- (Install_Restricted_Handlers).
14372
14373 Append_To (L,
14374 Make_Procedure_Call_Statement (Loc,
14375 Name =>
14376 New_Occurrence_Of
14377 (RTE (RE_Install_Restricted_Handlers), Loc),
14378 Parameter_Associations => Args));
14379
14380 else
14381 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14382
14383 -- First, prepends the _object argument
14384
14385 Prepend_To (Args,
14386 Make_Attribute_Reference (Loc,
14387 Prefix =>
14388 Make_Selected_Component (Loc,
14389 Prefix => Make_Identifier (Loc, Name_uInit),
14390 Selector_Name =>
14391 Make_Identifier (Loc, Name_uObject)),
14392 Attribute_Name => Name_Unchecked_Access));
14393 end if;
14394
14395 -- Then, insert call to Install_Handlers
14396
14397 Append_To (L,
14398 Make_Procedure_Call_Statement (Loc,
14399 Name =>
14400 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14401 Parameter_Associations => Args));
14402 end if;
14403 end;
14404 end if;
14405
14406 return L;
14407 end Make_Initialize_Protection;
14408
14409 ---------------------------
14410 -- Make_Task_Create_Call --
14411 ---------------------------
14412
14413 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14414 Loc : constant Source_Ptr := Sloc (Task_Rec);
14415 Args : List_Id;
14416 Ecount : Node_Id;
14417 Name : Node_Id;
14418 Tdec : Node_Id;
14419 Tdef : Node_Id;
14420 Tnam : Name_Id;
14421 Ttyp : Node_Id;
14422
14423 begin
14424 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14425 Tnam := Chars (Ttyp);
14426
14427 -- Get task declaration. In the case of a task type declaration, this is
14428 -- simply the parent of the task type entity. In the single task
14429 -- declaration, this parent will be the implicit type, and we can find
14430 -- the corresponding single task declaration by searching forward in the
14431 -- declaration list in the tree.
14432
14433 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14434 -- this type should have been removed during semantic analysis.
14435
14436 Tdec := Parent (Ttyp);
14437 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14438 N_Single_Task_Declaration)
14439 loop
14440 Next (Tdec);
14441 end loop;
14442
14443 -- Now we can find the task definition from this declaration
14444
14445 Tdef := Task_Definition (Tdec);
14446
14447 -- Build the parameter list for the call. Note that _Init is the name
14448 -- of the formal for the object to be initialized, which is the task
14449 -- value record itself.
14450
14451 Args := New_List;
14452
14453 -- Priority parameter. Set to Unspecified_Priority unless there is a
14454 -- Priority rep item, in which case we take the value from the rep item.
14455
14456 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14457 Append_To (Args,
14458 Make_Selected_Component (Loc,
14459 Prefix => Make_Identifier (Loc, Name_uInit),
14460 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14461 else
14462 Append_To (Args,
14463 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14464 end if;
14465
14466 -- Optional Stack parameter
14467
14468 if Restricted_Profile then
14469
14470 -- If the stack has been preallocated by the expander then
14471 -- pass its address. Otherwise, pass a null address.
14472
14473 if Preallocated_Stacks_On_Target then
14474 Append_To (Args,
14475 Make_Attribute_Reference (Loc,
14476 Prefix =>
14477 Make_Selected_Component (Loc,
14478 Prefix => Make_Identifier (Loc, Name_uInit),
14479 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14480 Attribute_Name => Name_Address));
14481
14482 else
14483 Append_To (Args,
14484 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14485 end if;
14486 end if;
14487
14488 -- Size parameter. If no Storage_Size pragma is present, then
14489 -- the size is taken from the taskZ variable for the type, which
14490 -- is either Unspecified_Size, or has been reset by the use of
14491 -- a Storage_Size attribute definition clause. If a pragma is
14492 -- present, then the size is taken from the _Size field of the
14493 -- task value record, which was set from the pragma value.
14494
14495 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14496 Append_To (Args,
14497 Make_Selected_Component (Loc,
14498 Prefix => Make_Identifier (Loc, Name_uInit),
14499 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14500
14501 else
14502 Append_To (Args,
14503 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14504 end if;
14505
14506 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14507 -- Task_Info pragma, in which case we take the value from the pragma.
14508
14509 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14510 Append_To (Args,
14511 Make_Selected_Component (Loc,
14512 Prefix => Make_Identifier (Loc, Name_uInit),
14513 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14514
14515 else
14516 Append_To (Args,
14517 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14518 end if;
14519
14520 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14521 -- in which case we take the value from the rep item. The parameter is
14522 -- passed as an Integer because in the case of unspecified CPU the
14523 -- value is not in the range of CPU_Range.
14524
14525 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14526 Append_To (Args,
14527 Convert_To (Standard_Integer,
14528 Make_Selected_Component (Loc,
14529 Prefix => Make_Identifier (Loc, Name_uInit),
14530 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14531 else
14532 Append_To (Args,
14533 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14534 end if;
14535
14536 if not Restricted_Profile then
14537
14538 -- Deadline parameter. If no Relative_Deadline pragma is present,
14539 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14540 -- the deadline is taken from the _Relative_Deadline field of the
14541 -- task value record, which was set from the pragma value. Note that
14542 -- this parameter must not be generated for the restricted profiles
14543 -- since Ravenscar does not allow deadlines.
14544
14545 -- Case where pragma Relative_Deadline applies: use given value
14546
14547 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14548 Append_To (Args,
14549 Make_Selected_Component (Loc,
14550 Prefix => Make_Identifier (Loc, Name_uInit),
14551 Selector_Name =>
14552 Make_Identifier (Loc, Name_uRelative_Deadline)));
14553
14554 -- No pragma Relative_Deadline apply to the task
14555
14556 else
14557 Append_To (Args,
14558 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14559 end if;
14560
14561 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14562 -- present, then the dispatching domain is null. If a rep item is
14563 -- present, then the dispatching domain is taken from the
14564 -- _Dispatching_Domain field of the task value record, which was set
14565 -- from the rep item value.
14566
14567 -- Case where Dispatching_Domain rep item applies: use given value
14568
14569 if Has_Rep_Item
14570 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14571 then
14572 Append_To (Args,
14573 Make_Selected_Component (Loc,
14574 Prefix =>
14575 Make_Identifier (Loc, Name_uInit),
14576 Selector_Name =>
14577 Make_Identifier (Loc, Name_uDispatching_Domain)));
14578
14579 -- No pragma or aspect Dispatching_Domain applies to the task
14580
14581 else
14582 Append_To (Args, Make_Null (Loc));
14583 end if;
14584
14585 -- Number of entries. This is an expression of the form:
14586
14587 -- n + _Init.a'Length + _Init.a'B'Length + ...
14588
14589 -- where a,b... are the entry family names for the task definition
14590
14591 Ecount :=
14592 Build_Entry_Count_Expression
14593 (Ttyp,
14594 Component_Items
14595 (Component_List
14596 (Type_Definition
14597 (Parent (Corresponding_Record_Type (Ttyp))))),
14598 Loc);
14599 Append_To (Args, Ecount);
14600
14601 -- Master parameter. This is a reference to the _Master parameter of
14602 -- the initialization procedure, except in the case of the pragma
14603 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14604 -- System.Tasking.Library_Task_Level.
14605
14606 if Restriction_Active (No_Task_Hierarchy) = False then
14607 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14608 else
14609 Append_To (Args,
14610 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14611 end if;
14612 end if;
14613
14614 -- State parameter. This is a pointer to the task body procedure. The
14615 -- required value is obtained by taking 'Unrestricted_Access of the task
14616 -- body procedure and converting it (with an unchecked conversion) to
14617 -- the type required by the task kernel. For further details, see the
14618 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14619 -- than 'Address in order to avoid creating trampolines.
14620
14621 declare
14622 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14623 Subp_Ptr_Typ : constant Node_Id :=
14624 Create_Itype (E_Access_Subprogram_Type, Tdec);
14625 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14626
14627 begin
14628 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14629 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14630
14631 -- Be sure to freeze a reference to the access-to-subprogram type,
14632 -- otherwise gigi will complain that it's in the wrong scope, because
14633 -- it's actually inside the init procedure for the record type that
14634 -- corresponds to the task type.
14635
14636 Set_Itype (Ref, Subp_Ptr_Typ);
14637 Append_Freeze_Action (Task_Rec, Ref);
14638
14639 Append_To (Args,
14640 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14641 Make_Qualified_Expression (Loc,
14642 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14643 Expression =>
14644 Make_Attribute_Reference (Loc,
14645 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14646 Attribute_Name => Name_Unrestricted_Access))));
14647 end;
14648
14649 -- Discriminants parameter. This is just the address of the task
14650 -- value record itself (which contains the discriminant values
14651
14652 Append_To (Args,
14653 Make_Attribute_Reference (Loc,
14654 Prefix => Make_Identifier (Loc, Name_uInit),
14655 Attribute_Name => Name_Address));
14656
14657 -- Elaborated parameter. This is an access to the elaboration Boolean
14658
14659 Append_To (Args,
14660 Make_Attribute_Reference (Loc,
14661 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14662 Attribute_Name => Name_Unchecked_Access));
14663
14664 -- Add Chain parameter (not done for sequential elaboration policy, see
14665 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14666
14667 if Partition_Elaboration_Policy /= 'S' then
14668 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14669 end if;
14670
14671 -- Task name parameter. Take this from the _Task_Id parameter to the
14672 -- init call unless there is a Task_Name pragma, in which case we take
14673 -- the value from the pragma.
14674
14675 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14676 -- Copy expression in full, because it may be dynamic and have
14677 -- side effects.
14678
14679 Append_To (Args,
14680 New_Copy_Tree
14681 (Expression
14682 (First
14683 (Pragma_Argument_Associations
14684 (Get_Rep_Pragma
14685 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14686
14687 else
14688 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14689 end if;
14690
14691 -- Created_Task parameter. This is the _Task_Id field of the task
14692 -- record value
14693
14694 Append_To (Args,
14695 Make_Selected_Component (Loc,
14696 Prefix => Make_Identifier (Loc, Name_uInit),
14697 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14698
14699 declare
14700 Create_RE : RE_Id;
14701
14702 begin
14703 if Restricted_Profile then
14704 if Partition_Elaboration_Policy = 'S' then
14705 Create_RE := RE_Create_Restricted_Task_Sequential;
14706 else
14707 Create_RE := RE_Create_Restricted_Task;
14708 end if;
14709 else
14710 Create_RE := RE_Create_Task;
14711 end if;
14712
14713 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14714 end;
14715
14716 return
14717 Make_Procedure_Call_Statement (Loc,
14718 Name => Name,
14719 Parameter_Associations => Args);
14720 end Make_Task_Create_Call;
14721
14722 ------------------------------
14723 -- Next_Protected_Operation --
14724 ------------------------------
14725
14726 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14727 Next_Op : Node_Id;
14728
14729 begin
14730 -- Check whether there is a subsequent body for a protected operation
14731 -- in the current protected body. In Ada2012 that includes expression
14732 -- functions that are completions.
14733
14734 Next_Op := Next (N);
14735 while Present (Next_Op)
14736 and then not Nkind_In (Next_Op,
14737 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14738 loop
14739 Next (Next_Op);
14740 end loop;
14741
14742 return Next_Op;
14743 end Next_Protected_Operation;
14744
14745 ---------------------
14746 -- Null_Statements --
14747 ---------------------
14748
14749 function Null_Statements (Stats : List_Id) return Boolean is
14750 Stmt : Node_Id;
14751
14752 begin
14753 Stmt := First (Stats);
14754 while Nkind (Stmt) /= N_Empty
14755 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14756 or else
14757 (Nkind (Stmt) = N_Pragma
14758 and then
14759 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14760 Name_Unmodified,
14761 Name_Warnings)))
14762 loop
14763 Next (Stmt);
14764 end loop;
14765
14766 return Nkind (Stmt) = N_Empty;
14767 end Null_Statements;
14768
14769 --------------------------
14770 -- Parameter_Block_Pack --
14771 --------------------------
14772
14773 function Parameter_Block_Pack
14774 (Loc : Source_Ptr;
14775 Blk_Typ : Entity_Id;
14776 Actuals : List_Id;
14777 Formals : List_Id;
14778 Decls : List_Id;
14779 Stmts : List_Id) return Node_Id
14780 is
14781 Actual : Entity_Id;
14782 Expr : Node_Id := Empty;
14783 Formal : Entity_Id;
14784 Has_Param : Boolean := False;
14785 P : Entity_Id;
14786 Params : List_Id;
14787 Temp_Asn : Node_Id;
14788 Temp_Nam : Node_Id;
14789
14790 begin
14791 Actual := First (Actuals);
14792 Formal := Defining_Identifier (First (Formals));
14793 Params := New_List;
14794 while Present (Actual) loop
14795 if Is_By_Copy_Type (Etype (Actual)) then
14796 -- Generate:
14797 -- Jnn : aliased <formal-type>
14798
14799 Temp_Nam := Make_Temporary (Loc, 'J');
14800
14801 Append_To (Decls,
14802 Make_Object_Declaration (Loc,
14803 Aliased_Present => True,
14804 Defining_Identifier => Temp_Nam,
14805 Object_Definition =>
14806 New_Occurrence_Of (Etype (Formal), Loc)));
14807
14808 if Ekind (Formal) /= E_Out_Parameter then
14809
14810 -- Generate:
14811 -- Jnn := <actual>
14812
14813 Temp_Asn :=
14814 New_Occurrence_Of (Temp_Nam, Loc);
14815
14816 Set_Assignment_OK (Temp_Asn);
14817
14818 Append_To (Stmts,
14819 Make_Assignment_Statement (Loc,
14820 Name => Temp_Asn,
14821 Expression => New_Copy_Tree (Actual)));
14822 end if;
14823
14824 -- Generate:
14825 -- Jnn'unchecked_access
14826
14827 Append_To (Params,
14828 Make_Attribute_Reference (Loc,
14829 Attribute_Name => Name_Unchecked_Access,
14830 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14831
14832 Has_Param := True;
14833
14834 -- The controlling parameter is omitted
14835
14836 else
14837 if not Is_Controlling_Actual (Actual) then
14838 Append_To (Params,
14839 Make_Reference (Loc, New_Copy_Tree (Actual)));
14840
14841 Has_Param := True;
14842 end if;
14843 end if;
14844
14845 Next_Actual (Actual);
14846 Next_Formal_With_Extras (Formal);
14847 end loop;
14848
14849 if Has_Param then
14850 Expr := Make_Aggregate (Loc, Params);
14851 end if;
14852
14853 -- Generate:
14854 -- P : Ann := (
14855 -- J1'unchecked_access;
14856 -- <actual2>'reference;
14857 -- ...);
14858
14859 P := Make_Temporary (Loc, 'P');
14860
14861 Append_To (Decls,
14862 Make_Object_Declaration (Loc,
14863 Defining_Identifier => P,
14864 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14865 Expression => Expr));
14866
14867 return P;
14868 end Parameter_Block_Pack;
14869
14870 ----------------------------
14871 -- Parameter_Block_Unpack --
14872 ----------------------------
14873
14874 function Parameter_Block_Unpack
14875 (Loc : Source_Ptr;
14876 P : Entity_Id;
14877 Actuals : List_Id;
14878 Formals : List_Id) return List_Id
14879 is
14880 Actual : Entity_Id;
14881 Asnmt : Node_Id;
14882 Formal : Entity_Id;
14883 Has_Asnmt : Boolean := False;
14884 Result : constant List_Id := New_List;
14885
14886 begin
14887 Actual := First (Actuals);
14888 Formal := Defining_Identifier (First (Formals));
14889 while Present (Actual) loop
14890 if Is_By_Copy_Type (Etype (Actual))
14891 and then Ekind (Formal) /= E_In_Parameter
14892 then
14893 -- Generate:
14894 -- <actual> := P.<formal>;
14895
14896 Asnmt :=
14897 Make_Assignment_Statement (Loc,
14898 Name =>
14899 New_Copy (Actual),
14900 Expression =>
14901 Make_Explicit_Dereference (Loc,
14902 Make_Selected_Component (Loc,
14903 Prefix =>
14904 New_Occurrence_Of (P, Loc),
14905 Selector_Name =>
14906 Make_Identifier (Loc, Chars (Formal)))));
14907
14908 Set_Assignment_OK (Name (Asnmt));
14909 Append_To (Result, Asnmt);
14910
14911 Has_Asnmt := True;
14912 end if;
14913
14914 Next_Actual (Actual);
14915 Next_Formal_With_Extras (Formal);
14916 end loop;
14917
14918 if Has_Asnmt then
14919 return Result;
14920 else
14921 return New_List (Make_Null_Statement (Loc));
14922 end if;
14923 end Parameter_Block_Unpack;
14924
14925 ----------------------
14926 -- Set_Discriminals --
14927 ----------------------
14928
14929 procedure Set_Discriminals (Dec : Node_Id) is
14930 D : Entity_Id;
14931 Pdef : Entity_Id;
14932 D_Minal : Entity_Id;
14933
14934 begin
14935 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14936 Pdef := Defining_Identifier (Dec);
14937
14938 if Has_Discriminants (Pdef) then
14939 D := First_Discriminant (Pdef);
14940 while Present (D) loop
14941 D_Minal :=
14942 Make_Defining_Identifier (Sloc (D),
14943 Chars => New_External_Name (Chars (D), 'D'));
14944
14945 Set_Ekind (D_Minal, E_Constant);
14946 Set_Etype (D_Minal, Etype (D));
14947 Set_Scope (D_Minal, Pdef);
14948 Set_Discriminal (D, D_Minal);
14949 Set_Discriminal_Link (D_Minal, D);
14950
14951 Next_Discriminant (D);
14952 end loop;
14953 end if;
14954 end Set_Discriminals;
14955
14956 -----------------------
14957 -- Trivial_Accept_OK --
14958 -----------------------
14959
14960 function Trivial_Accept_OK return Boolean is
14961 begin
14962 case Opt.Task_Dispatching_Policy is
14963
14964 -- If we have the default task dispatching policy in effect, we can
14965 -- definitely do the optimization (one way of looking at this is to
14966 -- think of the formal definition of the default policy being allowed
14967 -- to run any task it likes after a rendezvous, so even if notionally
14968 -- a full rescheduling occurs, we can say that our dispatching policy
14969 -- (i.e. the default dispatching policy) reorders the queue to be the
14970 -- same as just before the call.
14971
14972 when ' ' =>
14973 return True;
14974
14975 -- FIFO_Within_Priorities certainly does not permit this
14976 -- optimization since the Rendezvous is a scheduling action that may
14977 -- require some other task to be run.
14978
14979 when 'F' =>
14980 return False;
14981
14982 -- For now, disallow the optimization for all other policies. This
14983 -- may be over-conservative, but it is certainly not incorrect.
14984
14985 when others =>
14986 return False;
14987
14988 end case;
14989 end Trivial_Accept_OK;
14990
14991 end Exp_Ch9;