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