2014-01-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob0557995c563c7fd9363130654c4406cea2a9fa9f
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-2013, 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
295 -- original, unexpanded body. This is the version of the protected
296 -- subprogram that is called from all protected operations on the same
297 -- object, including the 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_Reference_To (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);
596 while Chars (Prev) /= Chars (Ent)
597 or else (Ekind (Prev) /= Ekind (Ent))
598 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
599 loop
600 if Ekind (Prev) = E_Entry then
601 Set_Intval (Num, Intval (Num) + 1);
603 elsif Ekind (Prev) = E_Entry_Family then
604 S :=
605 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
607 -- The need for the following full view retrieval stems from this
608 -- complex case of nested generics and tasking:
610 -- generic
611 -- type Formal_Index is range <>;
612 -- ...
613 -- package Outer is
614 -- type Index is private;
615 -- generic
616 -- ...
617 -- package Inner is
618 -- procedure P;
619 -- end Inner;
620 -- private
621 -- type Index is new Formal_Index range 1 .. 10;
622 -- end Outer;
624 -- package body Outer is
625 -- task type T is
626 -- entry Fam (Index); -- (2)
627 -- entry E;
628 -- end T;
629 -- package body Inner is -- (3)
630 -- procedure P is
631 -- begin
632 -- T.E; -- (1)
633 -- end P;
634 -- end Inner;
635 -- ...
637 -- We are currently building the index expression for the entry
638 -- call "T.E" (1). Part of the expansion must mention the range
639 -- of the discrete type "Index" (2) of entry family "Fam".
641 -- However only the private view of type "Index" is available to
642 -- the inner generic (3) because there was no prior mention of
643 -- the type inside "Inner". This visibility requirement is
644 -- implicit and cannot be detected during the construction of
645 -- the generic trees and needs special handling.
647 if In_Instance_Body
648 and then Is_Private_Type (S)
649 and then Present (Full_View (S))
650 then
651 S := Full_View (S);
652 end if;
654 Lo := Type_Low_Bound (S);
655 Hi := Type_High_Bound (S);
657 Expr :=
658 Make_Op_Add (Sloc,
659 Left_Opnd => Expr,
660 Right_Opnd =>
661 Make_Op_Add (Sloc,
662 Left_Opnd =>
663 Actual_Family_Offset (Hi, Lo),
664 Right_Opnd =>
665 Make_Integer_Literal (Sloc, 1)));
667 -- Other components are anonymous types to be ignored
669 else
670 null;
671 end if;
673 Next_Entity (Prev);
674 end loop;
676 return Expr;
677 end Actual_Index_Expression;
679 --------------------------
680 -- Add_Formal_Renamings --
681 --------------------------
683 procedure Add_Formal_Renamings
684 (Spec : Node_Id;
685 Decls : List_Id;
686 Ent : Entity_Id;
687 Loc : Source_Ptr)
689 Ptr : constant Entity_Id :=
690 Defining_Identifier
691 (Next (First (Parameter_Specifications (Spec))));
692 -- The name of the formal that holds the address of the parameter block
693 -- for the call.
695 Comp : Entity_Id;
696 Decl : Node_Id;
697 Formal : Entity_Id;
698 New_F : Entity_Id;
699 Renamed_Formal : Node_Id;
701 begin
702 Formal := First_Formal (Ent);
703 while Present (Formal) loop
704 Comp := Entry_Component (Formal);
705 New_F :=
706 Make_Defining_Identifier (Sloc (Formal),
707 Chars => Chars (Formal));
708 Set_Etype (New_F, Etype (Formal));
709 Set_Scope (New_F, Ent);
711 -- Now we set debug info needed on New_F even though it does not come
712 -- from source, so that the debugger will get the right information
713 -- for these generated names.
715 Set_Debug_Info_Needed (New_F);
717 if Ekind (Formal) = E_In_Parameter then
718 Set_Ekind (New_F, E_Constant);
719 else
720 Set_Ekind (New_F, E_Variable);
721 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
722 end if;
724 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
726 Renamed_Formal :=
727 Make_Selected_Component (Loc,
728 Prefix =>
729 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
730 Make_Identifier (Loc, Chars (Ptr))),
731 Selector_Name => New_Reference_To (Comp, Loc));
733 Decl :=
734 Build_Renamed_Formal_Declaration
735 (New_F, Formal, Comp, Renamed_Formal);
737 Append (Decl, Decls);
738 Set_Renamed_Object (Formal, New_F);
739 Next_Formal (Formal);
740 end loop;
741 end Add_Formal_Renamings;
743 ------------------------
744 -- Add_Object_Pointer --
745 ------------------------
747 procedure Add_Object_Pointer
748 (Loc : Source_Ptr;
749 Conc_Typ : Entity_Id;
750 Decls : List_Id)
752 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
753 Decl : Node_Id;
754 Obj_Ptr : Node_Id;
756 begin
757 -- Create the renaming declaration for the Protection object of a
758 -- protected type. _Object is used by Complete_Entry_Body.
759 -- ??? An attempt to make this a renaming was unsuccessful.
761 -- Build the entity for the access type
763 Obj_Ptr :=
764 Make_Defining_Identifier (Loc,
765 New_External_Name (Chars (Rec_Typ), 'P'));
767 -- Generate:
768 -- _object : poVP := poVP!O;
770 Decl :=
771 Make_Object_Declaration (Loc,
772 Defining_Identifier =>
773 Make_Defining_Identifier (Loc, Name_uObject),
774 Object_Definition =>
775 New_Reference_To (Obj_Ptr, Loc),
776 Expression =>
777 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
778 Set_Debug_Info_Needed (Defining_Identifier (Decl));
779 Prepend_To (Decls, Decl);
781 -- Generate:
782 -- type poVP is access poV;
784 Decl :=
785 Make_Full_Type_Declaration (Loc,
786 Defining_Identifier =>
787 Obj_Ptr,
788 Type_Definition =>
789 Make_Access_To_Object_Definition (Loc,
790 Subtype_Indication =>
791 New_Reference_To (Rec_Typ, Loc)));
792 Set_Debug_Info_Needed (Defining_Identifier (Decl));
793 Prepend_To (Decls, Decl);
794 end Add_Object_Pointer;
796 -----------------------
797 -- Build_Accept_Body --
798 -----------------------
800 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
801 Loc : constant Source_Ptr := Sloc (Astat);
802 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
803 New_S : Node_Id;
804 Hand : Node_Id;
805 Call : Node_Id;
806 Ohandle : Node_Id;
808 begin
809 -- At the end of the statement sequence, Complete_Rendezvous is called.
810 -- A label skipping the Complete_Rendezvous, and all other accept
811 -- processing, has already been added for the expansion of requeue
812 -- statements. The Sloc is copied from the last statement since it
813 -- is really part of this last statement.
815 Call :=
816 Build_Runtime_Call
817 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
818 Insert_Before (Last (Statements (Stats)), Call);
819 Analyze (Call);
821 -- If exception handlers are present, then append Complete_Rendezvous
822 -- calls to the handlers, and construct the required outer block. As
823 -- above, the Sloc is copied from the last statement in the sequence.
825 if Present (Exception_Handlers (Stats)) then
826 Hand := First (Exception_Handlers (Stats));
827 while Present (Hand) loop
828 Call :=
829 Build_Runtime_Call
830 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
831 Append (Call, Statements (Hand));
832 Analyze (Call);
833 Next (Hand);
834 end loop;
836 New_S :=
837 Make_Handled_Sequence_Of_Statements (Loc,
838 Statements => New_List (
839 Make_Block_Statement (Loc,
840 Handled_Statement_Sequence => Stats)));
842 else
843 New_S := Stats;
844 end if;
846 -- At this stage we know that the new statement sequence does
847 -- not have an exception handler part, so we supply one to call
848 -- Exceptional_Complete_Rendezvous. This handler is
850 -- when all others =>
851 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
853 -- We handle Abort_Signal to make sure that we properly catch the abort
854 -- case and wake up the caller.
856 Ohandle := Make_Others_Choice (Loc);
857 Set_All_Others (Ohandle);
859 Set_Exception_Handlers (New_S,
860 New_List (
861 Make_Implicit_Exception_Handler (Loc,
862 Exception_Choices => New_List (Ohandle),
864 Statements => New_List (
865 Make_Procedure_Call_Statement (Sloc (Stats),
866 Name => New_Reference_To (
867 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
868 Parameter_Associations => New_List (
869 Make_Function_Call (Sloc (Stats),
870 Name => New_Reference_To (
871 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
873 Set_Parent (New_S, Astat); -- temp parent for Analyze call
874 Analyze_Exception_Handlers (Exception_Handlers (New_S));
875 Expand_Exception_Handlers (New_S);
877 -- Exceptional_Complete_Rendezvous must be called with abort
878 -- still deferred, which is the case for a "when all others" handler.
880 return New_S;
881 end Build_Accept_Body;
883 -----------------------------------
884 -- Build_Activation_Chain_Entity --
885 -----------------------------------
887 procedure Build_Activation_Chain_Entity (N : Node_Id) is
888 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
889 -- Determine whether an extended return statement has an activation
890 -- chain.
892 --------------------------
893 -- Has_Activation_Chain --
894 --------------------------
896 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
897 Decl : Node_Id;
899 begin
900 Decl := First (Return_Object_Declarations (Stmt));
901 while Present (Decl) loop
902 if Nkind (Decl) = N_Object_Declaration
903 and then Chars (Defining_Identifier (Decl)) = Name_uChain
904 then
905 return True;
906 end if;
908 Next (Decl);
909 end loop;
911 return False;
912 end Has_Activation_Chain;
914 -- Local variables
916 Context : Node_Id;
917 Context_Id : Entity_Id;
918 Decls : List_Id;
920 -- Start of processing for Build_Activation_Chain_Entity
922 begin
923 -- Activation chain is never used for sequential elaboration policy, see
924 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
926 if Partition_Elaboration_Policy = 'S' then
927 return;
928 end if;
930 Find_Enclosing_Context (N, Context, Context_Id, Decls);
932 -- If activation chain entity has not been declared already, create one
934 if Nkind (Context) = N_Extended_Return_Statement
935 or else No (Activation_Chain_Entity (Context))
936 then
937 -- Since extended return statements do not store the entity of the
938 -- chain, examine the return object declarations to avoid creating
939 -- a duplicate.
941 if Nkind (Context) = N_Extended_Return_Statement
942 and then Has_Activation_Chain (Context)
943 then
944 return;
945 end if;
947 declare
948 Loc : constant Source_Ptr := Sloc (Context);
949 Chain : Entity_Id;
950 Decl : Node_Id;
952 begin
953 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
955 -- Note: An extended return statement is not really a task
956 -- activator, but it does have an activation chain on which to
957 -- store the tasks temporarily. On successful return, the tasks
958 -- on this chain are moved to the chain passed in by the caller.
959 -- We do not build an Activation_Chain_Entity for an extended
960 -- return statement, because we do not want to build a call to
961 -- Activate_Tasks. Task activation is the responsibility of the
962 -- caller.
964 if Nkind (Context) /= N_Extended_Return_Statement then
965 Set_Activation_Chain_Entity (Context, Chain);
966 end if;
968 Decl :=
969 Make_Object_Declaration (Loc,
970 Defining_Identifier => Chain,
971 Aliased_Present => True,
972 Object_Definition =>
973 New_Reference_To (RTE (RE_Activation_Chain), Loc));
975 Prepend_To (Decls, Decl);
977 -- Ensure that _chain appears in the proper scope of the context
979 if Context_Id /= Current_Scope then
980 Push_Scope (Context_Id);
981 Analyze (Decl);
982 Pop_Scope;
983 else
984 Analyze (Decl);
985 end if;
986 end;
987 end if;
988 end Build_Activation_Chain_Entity;
990 ----------------------------
991 -- Build_Barrier_Function --
992 ----------------------------
994 function Build_Barrier_Function
995 (N : Node_Id;
996 Ent : Entity_Id;
997 Pid : Node_Id) return Node_Id
999 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
1000 Cond : constant Node_Id := Condition (Ent_Formals);
1001 Loc : constant Source_Ptr := Sloc (Cond);
1002 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1003 Op_Decls : constant List_Id := New_List;
1004 Stmt : Node_Id;
1005 Func_Body : Node_Id;
1007 begin
1008 -- Add a declaration for the Protection object, renaming declarations
1009 -- for the discriminals and privals and finally a declaration for the
1010 -- entry family index (if applicable).
1012 Install_Private_Data_Declarations (Sloc (N),
1013 Spec_Id => Func_Id,
1014 Conc_Typ => Pid,
1015 Body_Nod => N,
1016 Decls => Op_Decls,
1017 Barrier => True,
1018 Family => Ekind (Ent) = E_Entry_Family);
1020 -- If compiling with -fpreserve-control-flow, make sure we insert an
1021 -- IF statement so that the back-end knows to generate a conditional
1022 -- branch instruction, even if the condition is just the name of a
1023 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1024 -- such redundant IF statements under -fpreserve-control-flow
1025 -- (whether coming from this routine, or directly from source).
1027 if Opt.Suppress_Control_Flow_Optimizations then
1028 Stmt := Make_Implicit_If_Statement (Cond,
1029 Condition => Cond,
1030 Then_Statements => New_List (
1031 Make_Simple_Return_Statement (Loc,
1032 New_Occurrence_Of (Standard_True, Loc))),
1033 Else_Statements => New_List (
1034 Make_Simple_Return_Statement (Loc,
1035 New_Occurrence_Of (Standard_False, Loc))));
1037 else
1038 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1039 end if;
1041 -- Note: the condition in the barrier function needs to be properly
1042 -- processed for the C/Fortran boolean possibility, but this happens
1043 -- automatically since the return statement does this normalization.
1045 Func_Body :=
1046 Make_Subprogram_Body (Loc,
1047 Specification =>
1048 Build_Barrier_Function_Specification (Loc,
1049 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1050 Declarations => Op_Decls,
1051 Handled_Statement_Sequence =>
1052 Make_Handled_Sequence_Of_Statements (Loc,
1053 Statements => New_List (Stmt)));
1054 Set_Is_Entry_Barrier_Function (Func_Body);
1056 return Func_Body;
1057 end Build_Barrier_Function;
1059 ------------------------------------------
1060 -- Build_Barrier_Function_Specification --
1061 ------------------------------------------
1063 function Build_Barrier_Function_Specification
1064 (Loc : Source_Ptr;
1065 Def_Id : Entity_Id) return Node_Id
1067 begin
1068 Set_Debug_Info_Needed (Def_Id);
1070 return Make_Function_Specification (Loc,
1071 Defining_Unit_Name => Def_Id,
1072 Parameter_Specifications => New_List (
1073 Make_Parameter_Specification (Loc,
1074 Defining_Identifier =>
1075 Make_Defining_Identifier (Loc, Name_uO),
1076 Parameter_Type =>
1077 New_Reference_To (RTE (RE_Address), Loc)),
1079 Make_Parameter_Specification (Loc,
1080 Defining_Identifier =>
1081 Make_Defining_Identifier (Loc, Name_uE),
1082 Parameter_Type =>
1083 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1085 Result_Definition =>
1086 New_Reference_To (Standard_Boolean, Loc));
1087 end Build_Barrier_Function_Specification;
1089 --------------------------
1090 -- Build_Call_With_Task --
1091 --------------------------
1093 function Build_Call_With_Task
1094 (N : Node_Id;
1095 E : Entity_Id) return Node_Id
1097 Loc : constant Source_Ptr := Sloc (N);
1098 begin
1099 return
1100 Make_Function_Call (Loc,
1101 Name => New_Reference_To (E, Loc),
1102 Parameter_Associations => New_List (Concurrent_Ref (N)));
1103 end Build_Call_With_Task;
1105 -----------------------------
1106 -- Build_Class_Wide_Master --
1107 -----------------------------
1109 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1110 Loc : constant Source_Ptr := Sloc (Typ);
1111 Master_Id : Entity_Id;
1112 Master_Scope : Entity_Id;
1113 Name_Id : Node_Id;
1114 Related_Node : Node_Id;
1115 Ren_Decl : Node_Id;
1117 begin
1118 -- Nothing to do if there is no task hierarchy
1120 if Restriction_Active (No_Task_Hierarchy) then
1121 return;
1122 end if;
1124 -- Find the declaration that created the access type. It is either a
1125 -- type declaration, or an object declaration with an access definition,
1126 -- in which case the type is anonymous.
1128 if Is_Itype (Typ) then
1129 Related_Node := Associated_Node_For_Itype (Typ);
1130 else
1131 Related_Node := Parent (Typ);
1132 end if;
1134 Master_Scope := Find_Master_Scope (Typ);
1136 -- Nothing to do if the master scope already contains a _master entity.
1137 -- The only exception to this is the following scenario:
1139 -- Source_Scope
1140 -- Transient_Scope_1
1141 -- _master
1143 -- Transient_Scope_2
1144 -- use of master
1146 -- In this case the source scope is marked as having the master entity
1147 -- even though the actual declaration appears inside an inner scope. If
1148 -- the second transient scope requires a _master, it cannot use the one
1149 -- already declared because the entity is not visible.
1151 Name_Id := Make_Identifier (Loc, Name_uMaster);
1153 if not Has_Master_Entity (Master_Scope)
1154 or else No (Current_Entity_In_Scope (Name_Id))
1155 then
1156 declare
1157 Master_Decl : Node_Id;
1159 begin
1160 Set_Has_Master_Entity (Master_Scope);
1162 -- Generate:
1163 -- _master : constant Integer := Current_Master.all;
1165 Master_Decl :=
1166 Make_Object_Declaration (Loc,
1167 Defining_Identifier =>
1168 Make_Defining_Identifier (Loc, Name_uMaster),
1169 Constant_Present => True,
1170 Object_Definition =>
1171 New_Reference_To (Standard_Integer, Loc),
1172 Expression =>
1173 Make_Explicit_Dereference (Loc,
1174 New_Reference_To (RTE (RE_Current_Master), Loc)));
1176 Insert_Action (Related_Node, Master_Decl);
1177 Analyze (Master_Decl);
1179 -- Mark the containing scope as a task master. Masters associated
1180 -- with return statements are already marked at this stage (see
1181 -- Analyze_Subprogram_Body).
1183 if Ekind (Current_Scope) /= E_Return_Statement then
1184 declare
1185 Par : Node_Id := Related_Node;
1187 begin
1188 while Nkind (Par) /= N_Compilation_Unit loop
1189 Par := Parent (Par);
1191 -- If we fall off the top, we are at the outer level,
1192 -- and the environment task is our effective master,
1193 -- so nothing to mark.
1195 if Nkind_In (Par, N_Block_Statement,
1196 N_Subprogram_Body,
1197 N_Task_Body)
1198 then
1199 Set_Is_Task_Master (Par);
1200 exit;
1201 end if;
1202 end loop;
1203 end;
1204 end if;
1205 end;
1206 end if;
1208 Master_Id :=
1209 Make_Defining_Identifier (Loc,
1210 New_External_Name (Chars (Typ), 'M'));
1212 -- Generate:
1213 -- Mnn renames _master;
1215 Ren_Decl :=
1216 Make_Object_Renaming_Declaration (Loc,
1217 Defining_Identifier => Master_Id,
1218 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
1219 Name => Name_Id);
1221 Insert_Action (Related_Node, Ren_Decl);
1223 Set_Master_Id (Typ, Master_Id);
1224 end Build_Class_Wide_Master;
1226 --------------------------------
1227 -- Build_Corresponding_Record --
1228 --------------------------------
1230 function Build_Corresponding_Record
1231 (N : Node_Id;
1232 Ctyp : Entity_Id;
1233 Loc : Source_Ptr) return Node_Id
1235 Rec_Ent : constant Entity_Id :=
1236 Make_Defining_Identifier
1237 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1238 Disc : Entity_Id;
1239 Dlist : List_Id;
1240 New_Disc : Entity_Id;
1241 Cdecls : List_Id;
1243 begin
1244 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1245 Set_Ekind (Rec_Ent, E_Record_Type);
1246 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1247 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1248 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1249 Set_Stored_Constraint (Rec_Ent, No_Elist);
1250 Cdecls := New_List;
1252 -- Use discriminals to create list of discriminants for record, and
1253 -- create new discriminals for use in default expressions, etc. It is
1254 -- worth noting that a task discriminant gives rise to 5 entities;
1256 -- a) The original discriminant.
1257 -- b) The discriminal for use in the task.
1258 -- c) The discriminant of the corresponding record.
1259 -- d) The discriminal for the init proc of the corresponding record.
1260 -- e) The local variable that renames the discriminant in the procedure
1261 -- for the task body.
1263 -- In fact the discriminals b) are used in the renaming declarations
1264 -- for e). See details in einfo (Handling of Discriminants).
1266 if Present (Discriminant_Specifications (N)) then
1267 Dlist := New_List;
1268 Disc := First_Discriminant (Ctyp);
1270 while Present (Disc) loop
1271 New_Disc := CR_Discriminant (Disc);
1273 Append_To (Dlist,
1274 Make_Discriminant_Specification (Loc,
1275 Defining_Identifier => New_Disc,
1276 Discriminant_Type =>
1277 New_Occurrence_Of (Etype (Disc), Loc),
1278 Expression =>
1279 New_Copy (Discriminant_Default_Value (Disc))));
1281 Next_Discriminant (Disc);
1282 end loop;
1284 else
1285 Dlist := No_List;
1286 end if;
1288 -- Now we can construct the record type declaration. Note that this
1289 -- record is "limited tagged". It is "limited" to reflect the underlying
1290 -- limitedness of the task or protected object that it represents, and
1291 -- ensuring for example that it is properly passed by reference. It is
1292 -- "tagged" to give support to dispatching calls through interfaces. We
1293 -- propagate here the list of interfaces covered by the concurrent type
1294 -- (Ada 2005: AI-345).
1296 return
1297 Make_Full_Type_Declaration (Loc,
1298 Defining_Identifier => Rec_Ent,
1299 Discriminant_Specifications => Dlist,
1300 Type_Definition =>
1301 Make_Record_Definition (Loc,
1302 Component_List =>
1303 Make_Component_List (Loc,
1304 Component_Items => Cdecls),
1305 Tagged_Present =>
1306 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1307 Interface_List => Interface_List (N),
1308 Limited_Present => True));
1309 end Build_Corresponding_Record;
1311 ---------------------------------
1312 -- Build_Dispatching_Tag_Check --
1313 ---------------------------------
1315 function Build_Dispatching_Tag_Check
1316 (K : Entity_Id;
1317 N : Node_Id) return Node_Id
1319 Loc : constant Source_Ptr := Sloc (N);
1320 begin
1321 return
1322 Make_Op_Or (Loc,
1323 Make_Op_Eq (Loc,
1324 Left_Opnd => New_Reference_To (K, Loc),
1325 Right_Opnd => New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
1326 Make_Op_Eq (Loc,
1327 Left_Opnd => New_Reference_To (K, Loc),
1328 Right_Opnd => New_Reference_To (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_Reference_To (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_Reference_To (RTE (RE_Entry_Index), Loc),
1533 Expression =>
1534 Make_Function_Call (Loc,
1535 Name =>
1536 New_Reference_To (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_Reference_To (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 => Make_Integer_Literal (Loc, 1),
1555 High_Bound => New_Reference_To (Size, Loc))),
1556 Expression => Make_Null (Loc))))));
1557 end;
1558 end if;
1559 end Create_Index_And_Data;
1561 ---------------------
1562 -- Increment_Index --
1563 ---------------------
1565 function Increment_Index return Node_Id is
1566 begin
1567 return
1568 Make_Assignment_Statement (Loc,
1569 Name => New_Reference_To (Index, Loc),
1570 Expression =>
1571 Make_Op_Add (Loc,
1572 Left_Opnd => New_Reference_To (Index, Loc),
1573 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1574 end Increment_Index;
1576 ----------------------
1577 -- Name_Declaration --
1578 ----------------------
1580 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1581 begin
1582 return
1583 Make_Object_Declaration (Loc,
1584 Defining_Identifier => Def_Id,
1585 Aliased_Present => True,
1586 Constant_Present => True,
1587 Object_Definition => New_Reference_To (Standard_String, Loc),
1588 Expression =>
1589 Make_String_Literal (Loc, String_From_Name_Buffer));
1590 end Name_Declaration;
1592 --------------------
1593 -- Set_Entry_Name --
1594 --------------------
1596 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1597 begin
1598 return
1599 Make_Assignment_Statement (Loc,
1600 Name =>
1601 Make_Indexed_Component (Loc,
1602 Prefix => New_Reference_To (Data, Loc),
1603 Expressions => New_List (New_Reference_To (Index, Loc))),
1605 Expression =>
1606 Make_Attribute_Reference (Loc,
1607 Prefix => New_Reference_To (Def_Id, Loc),
1608 Attribute_Name => Name_Unchecked_Access));
1609 end Set_Entry_Name;
1611 -- Local variables
1613 Temp_Id : Entity_Id;
1614 Subt_Def : Node_Id;
1616 -- Start of processing for Build_Entry_Name
1618 begin
1619 if Ekind (Comp_Id) = E_Entry_Family then
1620 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1622 Create_Index_And_Data;
1624 -- Step 1: Create the string name of the entry family.
1625 -- Generate:
1626 -- Temp : aliased constant String := "name ()";
1628 Temp_Id := Make_Temporary (Loc, 'S');
1629 Get_Name_String (Chars (Comp_Id));
1630 Add_Char_To_Name_Buffer (' ');
1631 Add_Char_To_Name_Buffer ('(');
1632 Add_Char_To_Name_Buffer (')');
1634 Append_To (Stmts, Name_Declaration (Temp_Id));
1636 -- Generate:
1637 -- for Member in Family_Low .. Family_High loop
1638 -- Set_Entry_Name (...);
1639 -- Index := Index + 1;
1640 -- end loop;
1642 Append_To (Stmts,
1643 Make_Loop_Statement (Loc,
1644 Iteration_Scheme =>
1645 Make_Iteration_Scheme (Loc,
1646 Loop_Parameter_Specification =>
1647 Make_Loop_Parameter_Specification (Loc,
1648 Defining_Identifier =>
1649 Make_Temporary (Loc, 'L'),
1650 Discrete_Subtype_Definition =>
1651 Build_Range (Subt_Def))),
1653 Statements => New_List (
1654 Set_Entry_Name (Temp_Id),
1655 Increment_Index),
1656 End_Label => Empty));
1658 -- Entry
1660 else
1661 Create_Index_And_Data;
1663 -- Step 1: Create the string name of the entry. Generate:
1664 -- Temp : aliased constant String := "name";
1666 Temp_Id := Make_Temporary (Loc, 'S');
1667 Get_Name_String (Chars (Comp_Id));
1669 Append_To (Stmts, Name_Declaration (Temp_Id));
1671 -- Step 2: Associate the string name with the underlying data
1672 -- structure.
1674 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1675 Append_To (Stmts, Increment_Index);
1676 end if;
1677 end Build_Entry_Name;
1679 ----------------------
1680 -- Object_Reference --
1681 ----------------------
1683 function Object_Reference return Node_Id is
1684 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1685 Field : Name_Id;
1686 Ref : Node_Id;
1688 begin
1689 if Is_Protected_Type (Typ) then
1690 Field := Name_uObject;
1691 else
1692 Field := Name_uTask_Id;
1693 end if;
1695 Ref :=
1696 Make_Selected_Component (Loc,
1697 Prefix =>
1698 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1699 Selector_Name => Make_Identifier (Loc, Field));
1701 if Is_Protected_Type (Typ) then
1702 Ref :=
1703 Make_Attribute_Reference (Loc,
1704 Prefix => Ref,
1705 Attribute_Name => Name_Unchecked_Access);
1706 end if;
1708 return Ref;
1709 end Object_Reference;
1711 -- Local variables
1713 Comp : Node_Id;
1714 Proc : RE_Id;
1716 -- Start of processing for Build_Entry_Names
1718 begin
1719 -- Retrieve the original concurrent type
1721 if Is_Concurrent_Record_Type (Typ) then
1722 Typ := Corresponding_Concurrent_Type (Typ);
1723 end if;
1725 pragma Assert (Is_Concurrent_Type (Typ));
1727 -- Nothing to do if the type has no entries
1729 if not Has_Entries (Typ) then
1730 return;
1731 end if;
1733 -- Avoid generating entry names for a protected type with only one entry
1735 if Is_Protected_Type (Typ)
1736 and then Find_Protection_Type (Base_Type (Typ)) /=
1737 RTE (RE_Protection_Entries)
1738 then
1739 return;
1740 end if;
1742 -- Step 1: Populate the array with statically generated strings denoting
1743 -- entries and entry family names.
1745 Comp := First_Entity (Typ);
1746 while Present (Comp) loop
1747 if Comes_From_Source (Comp)
1748 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
1749 then
1750 Build_Entry_Name (Comp);
1751 end if;
1753 Next_Entity (Comp);
1754 end loop;
1756 -- Step 2: Associate the array with the related concurrent object:
1758 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
1760 if Present (Data) then
1761 if Is_Protected_Type (Typ) then
1762 Proc := RO_PE_Set_Entry_Names;
1763 else
1764 Proc := RO_ST_Set_Entry_Names;
1765 end if;
1767 Append_To (Stmts,
1768 Make_Procedure_Call_Statement (Loc,
1769 Name => New_Reference_To (RTE (Proc), Loc),
1770 Parameter_Associations => New_List (
1771 Object_Reference,
1772 Make_Attribute_Reference (Loc,
1773 Prefix => New_Reference_To (Data, Loc),
1774 Attribute_Name => Name_Unchecked_Access))));
1775 end if;
1776 end Build_Entry_Names;
1778 ---------------------------
1779 -- Build_Parameter_Block --
1780 ---------------------------
1782 function Build_Parameter_Block
1783 (Loc : Source_Ptr;
1784 Actuals : List_Id;
1785 Formals : List_Id;
1786 Decls : List_Id) return Entity_Id
1788 Actual : Entity_Id;
1789 Comp_Nam : Node_Id;
1790 Comps : List_Id;
1791 Formal : Entity_Id;
1792 Has_Comp : Boolean := False;
1793 Rec_Nam : Node_Id;
1795 begin
1796 Actual := First (Actuals);
1797 Comps := New_List;
1798 Formal := Defining_Identifier (First (Formals));
1800 while Present (Actual) loop
1801 if not Is_Controlling_Actual (Actual) then
1803 -- Generate:
1804 -- type Ann is access all <actual-type>
1806 Comp_Nam := Make_Temporary (Loc, 'A');
1808 Append_To (Decls,
1809 Make_Full_Type_Declaration (Loc,
1810 Defining_Identifier => Comp_Nam,
1811 Type_Definition =>
1812 Make_Access_To_Object_Definition (Loc,
1813 All_Present => True,
1814 Constant_Present => Ekind (Formal) = E_In_Parameter,
1815 Subtype_Indication =>
1816 New_Reference_To (Etype (Actual), Loc))));
1818 -- Generate:
1819 -- Param : Ann;
1821 Append_To (Comps,
1822 Make_Component_Declaration (Loc,
1823 Defining_Identifier =>
1824 Make_Defining_Identifier (Loc, Chars (Formal)),
1825 Component_Definition =>
1826 Make_Component_Definition (Loc,
1827 Aliased_Present =>
1828 False,
1829 Subtype_Indication =>
1830 New_Reference_To (Comp_Nam, Loc))));
1832 Has_Comp := True;
1833 end if;
1835 Next_Actual (Actual);
1836 Next_Formal_With_Extras (Formal);
1837 end loop;
1839 Rec_Nam := Make_Temporary (Loc, 'P');
1841 if Has_Comp then
1843 -- Generate:
1844 -- type Pnn is record
1845 -- Param1 : Ann1;
1846 -- ...
1847 -- ParamN : AnnN;
1849 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1850 -- the original parameter names and Ann1 .. AnnN are the access to
1851 -- actual types.
1853 Append_To (Decls,
1854 Make_Full_Type_Declaration (Loc,
1855 Defining_Identifier =>
1856 Rec_Nam,
1857 Type_Definition =>
1858 Make_Record_Definition (Loc,
1859 Component_List =>
1860 Make_Component_List (Loc, Comps))));
1861 else
1862 -- Generate:
1863 -- type Pnn is null record;
1865 Append_To (Decls,
1866 Make_Full_Type_Declaration (Loc,
1867 Defining_Identifier =>
1868 Rec_Nam,
1869 Type_Definition =>
1870 Make_Record_Definition (Loc,
1871 Null_Present => True,
1872 Component_List => Empty)));
1873 end if;
1875 return Rec_Nam;
1876 end Build_Parameter_Block;
1878 --------------------------------------
1879 -- Build_Renamed_Formal_Declaration --
1880 --------------------------------------
1882 function Build_Renamed_Formal_Declaration
1883 (New_F : Entity_Id;
1884 Formal : Entity_Id;
1885 Comp : Entity_Id;
1886 Renamed_Formal : Node_Id) return Node_Id
1888 Loc : constant Source_Ptr := Sloc (New_F);
1889 Decl : Node_Id;
1891 begin
1892 -- If the formal is a tagged incomplete type, it is already passed
1893 -- by reference, so it is sufficient to rename the pointer component
1894 -- that corresponds to the actual. Otherwise we need to dereference
1895 -- the pointer component to obtain the actual.
1897 if Is_Incomplete_Type (Etype (Formal))
1898 and then Is_Tagged_Type (Etype (Formal))
1899 then
1900 Decl :=
1901 Make_Object_Renaming_Declaration (Loc,
1902 Defining_Identifier => New_F,
1903 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1904 Name => Renamed_Formal);
1906 else
1907 Decl :=
1908 Make_Object_Renaming_Declaration (Loc,
1909 Defining_Identifier => New_F,
1910 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1911 Name =>
1912 Make_Explicit_Dereference (Loc, Renamed_Formal));
1913 end if;
1915 return Decl;
1916 end Build_Renamed_Formal_Declaration;
1918 -----------------------
1919 -- Build_PPC_Wrapper --
1920 -----------------------
1922 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1923 Loc : constant Source_Ptr := Sloc (E);
1924 Synch_Type : constant Entity_Id := Scope (E);
1926 Wrapper_Id : constant Entity_Id :=
1927 Make_Defining_Identifier (Loc,
1928 Chars => New_External_Name (Chars (E), 'E'));
1929 -- the wrapper procedure name
1931 Wrapper_Body : Node_Id;
1933 Synch_Id : constant Entity_Id :=
1934 Make_Defining_Identifier (Loc,
1935 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1936 -- The parameter that designates the synchronized object in the call
1938 Actuals : constant List_Id := New_List;
1939 -- The actuals in the entry call
1941 Decls : constant List_Id := New_List;
1943 Entry_Call : Node_Id;
1944 Entry_Name : Node_Id;
1946 Specs : List_Id;
1947 -- The specification of the wrapper procedure
1949 begin
1951 -- Only build the wrapper if entry has pre/postconditions.
1952 -- Should this be done unconditionally instead ???
1954 declare
1955 P : Node_Id;
1957 begin
1958 P := Pre_Post_Conditions (Contract (E));
1960 if No (P) then
1961 return;
1962 end if;
1964 -- Transfer ppc pragmas to the declarations of the wrapper
1966 while Present (P) loop
1967 if Nam_In (Pragma_Name (P), Name_Precondition,
1968 Name_Postcondition)
1969 then
1970 Append (Relocate_Node (P), Decls);
1971 Set_Analyzed (Last (Decls), False);
1972 end if;
1974 P := Next_Pragma (P);
1975 end loop;
1976 end;
1978 -- First formal is synchronized object
1980 Specs := New_List (
1981 Make_Parameter_Specification (Loc,
1982 Defining_Identifier => Synch_Id,
1983 Out_Present => True,
1984 In_Present => True,
1985 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1987 Entry_Name :=
1988 Make_Selected_Component (Loc,
1989 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1990 Selector_Name => New_Occurrence_Of (E, Loc));
1992 -- If entity is entry family, second formal is the corresponding index,
1993 -- and entry name is an indexed component.
1995 if Ekind (E) = E_Entry_Family then
1996 declare
1997 Index : constant Entity_Id :=
1998 Make_Defining_Identifier (Loc, Name_I);
1999 begin
2000 Append_To (Specs,
2001 Make_Parameter_Specification (Loc,
2002 Defining_Identifier => Index,
2003 Parameter_Type =>
2004 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
2006 Entry_Name :=
2007 Make_Indexed_Component (Loc,
2008 Prefix => Entry_Name,
2009 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
2010 end;
2011 end if;
2013 Entry_Call :=
2014 Make_Procedure_Call_Statement (Loc,
2015 Name => Entry_Name,
2016 Parameter_Associations => Actuals);
2018 -- Now add formals that match those of the entry, and build actuals for
2019 -- the nested entry call.
2021 declare
2022 Form : Entity_Id;
2023 New_Form : Entity_Id;
2024 Parm_Spec : Node_Id;
2026 begin
2027 Form := First_Formal (E);
2028 while Present (Form) loop
2029 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
2030 Parm_Spec :=
2031 Make_Parameter_Specification (Loc,
2032 Defining_Identifier => New_Form,
2033 Out_Present => Out_Present (Parent (Form)),
2034 In_Present => In_Present (Parent (Form)),
2035 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
2037 Append (Parm_Spec, Specs);
2038 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
2039 Next_Formal (Form);
2040 end loop;
2041 end;
2043 -- Add renaming declarations for the discriminants of the enclosing
2044 -- type, which may be visible in the preconditions.
2046 if Has_Discriminants (Synch_Type) then
2047 declare
2048 D : Entity_Id;
2049 Decl : Node_Id;
2051 begin
2052 D := First_Discriminant (Synch_Type);
2053 while Present (D) loop
2054 Decl :=
2055 Make_Object_Renaming_Declaration (Loc,
2056 Defining_Identifier =>
2057 Make_Defining_Identifier (Loc, Chars (D)),
2058 Subtype_Mark => New_Reference_To (Etype (D), Loc),
2059 Name =>
2060 Make_Selected_Component (Loc,
2061 Prefix => New_Reference_To (Synch_Id, Loc),
2062 Selector_Name => Make_Identifier (Loc, Chars (D))));
2063 Prepend (Decl, Decls);
2064 Next_Discriminant (D);
2065 end loop;
2066 end;
2067 end if;
2069 Set_PPC_Wrapper (E, Wrapper_Id);
2070 Wrapper_Body :=
2071 Make_Subprogram_Body (Loc,
2072 Specification =>
2073 Make_Procedure_Specification (Loc,
2074 Defining_Unit_Name => Wrapper_Id,
2075 Parameter_Specifications => Specs),
2076 Declarations => Decls,
2077 Handled_Statement_Sequence =>
2078 Make_Handled_Sequence_Of_Statements (Loc,
2079 Statements => New_List (Entry_Call)));
2081 -- The wrapper body is analyzed when the enclosing type is frozen
2083 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
2084 end Build_PPC_Wrapper;
2086 --------------------------
2087 -- Build_Wrapper_Bodies --
2088 --------------------------
2090 procedure Build_Wrapper_Bodies
2091 (Loc : Source_Ptr;
2092 Typ : Entity_Id;
2093 N : Node_Id)
2095 Rec_Typ : Entity_Id;
2097 function Build_Wrapper_Body
2098 (Loc : Source_Ptr;
2099 Subp_Id : Entity_Id;
2100 Obj_Typ : Entity_Id;
2101 Formals : List_Id) return Node_Id;
2102 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2103 -- associated with a protected or task type. Subp_Id is the subprogram
2104 -- name which will be wrapped. Obj_Typ is the type of the new formal
2105 -- parameter which handles dispatching and object notation. Formals are
2106 -- the original formals of Subp_Id which will be explicitly replicated.
2108 ------------------------
2109 -- Build_Wrapper_Body --
2110 ------------------------
2112 function Build_Wrapper_Body
2113 (Loc : Source_Ptr;
2114 Subp_Id : Entity_Id;
2115 Obj_Typ : Entity_Id;
2116 Formals : List_Id) return Node_Id
2118 Body_Spec : Node_Id;
2120 begin
2121 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2123 -- The subprogram is not overriding or is not a primitive declared
2124 -- between two views.
2126 if No (Body_Spec) then
2127 return Empty;
2128 end if;
2130 declare
2131 Actuals : List_Id := No_List;
2132 Conv_Id : Node_Id;
2133 First_Form : Node_Id;
2134 Formal : Node_Id;
2135 Nam : Node_Id;
2137 begin
2138 -- Map formals to actuals. Use the list built for the wrapper
2139 -- spec, skipping the object notation parameter.
2141 First_Form := First (Parameter_Specifications (Body_Spec));
2143 Formal := First_Form;
2144 Next (Formal);
2146 if Present (Formal) then
2147 Actuals := New_List;
2148 while Present (Formal) loop
2149 Append_To (Actuals,
2150 Make_Identifier (Loc,
2151 Chars => Chars (Defining_Identifier (Formal))));
2152 Next (Formal);
2153 end loop;
2154 end if;
2156 -- Special processing for primitives declared between a private
2157 -- type and its completion: the wrapper needs a properly typed
2158 -- parameter if the wrapped operation has a controlling first
2159 -- parameter. Note that this might not be the case for a function
2160 -- with a controlling result.
2162 if Is_Private_Primitive_Subprogram (Subp_Id) then
2163 if No (Actuals) then
2164 Actuals := New_List;
2165 end if;
2167 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2168 Prepend_To (Actuals,
2169 Unchecked_Convert_To
2170 (Corresponding_Concurrent_Type (Obj_Typ),
2171 Make_Identifier (Loc, Name_uO)));
2173 else
2174 Prepend_To (Actuals,
2175 Make_Identifier (Loc,
2176 Chars => Chars (Defining_Identifier (First_Form))));
2177 end if;
2179 Nam := New_Reference_To (Subp_Id, Loc);
2180 else
2181 -- An access-to-variable object parameter requires an explicit
2182 -- dereference in the unchecked conversion. This case occurs
2183 -- when a protected entry wrapper must override an interface
2184 -- level procedure with interface access as first parameter.
2186 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2188 if Nkind (Parameter_Type (First_Form)) =
2189 N_Access_Definition
2190 then
2191 Conv_Id :=
2192 Make_Explicit_Dereference (Loc,
2193 Prefix => Make_Identifier (Loc, Name_uO));
2194 else
2195 Conv_Id := Make_Identifier (Loc, Name_uO);
2196 end if;
2198 Nam :=
2199 Make_Selected_Component (Loc,
2200 Prefix =>
2201 Unchecked_Convert_To
2202 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2203 Selector_Name => New_Reference_To (Subp_Id, Loc));
2204 end if;
2206 -- Create the subprogram body. For a function, the call to the
2207 -- actual subprogram has to be converted to the corresponding
2208 -- record if it is a controlling result.
2210 if Ekind (Subp_Id) = E_Function then
2211 declare
2212 Res : Node_Id;
2214 begin
2215 Res :=
2216 Make_Function_Call (Loc,
2217 Name => Nam,
2218 Parameter_Associations => Actuals);
2220 if Has_Controlling_Result (Subp_Id) then
2221 Res :=
2222 Unchecked_Convert_To
2223 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2224 end if;
2226 return
2227 Make_Subprogram_Body (Loc,
2228 Specification => Body_Spec,
2229 Declarations => Empty_List,
2230 Handled_Statement_Sequence =>
2231 Make_Handled_Sequence_Of_Statements (Loc,
2232 Statements => New_List (
2233 Make_Simple_Return_Statement (Loc, Res))));
2234 end;
2236 else
2237 return
2238 Make_Subprogram_Body (Loc,
2239 Specification => Body_Spec,
2240 Declarations => Empty_List,
2241 Handled_Statement_Sequence =>
2242 Make_Handled_Sequence_Of_Statements (Loc,
2243 Statements => New_List (
2244 Make_Procedure_Call_Statement (Loc,
2245 Name => Nam,
2246 Parameter_Associations => Actuals))));
2247 end if;
2248 end;
2249 end Build_Wrapper_Body;
2251 -- Start of processing for Build_Wrapper_Bodies
2253 begin
2254 if Is_Concurrent_Type (Typ) then
2255 Rec_Typ := Corresponding_Record_Type (Typ);
2256 else
2257 Rec_Typ := Typ;
2258 end if;
2260 -- Generate wrapper bodies for a concurrent type which implements an
2261 -- interface.
2263 if Present (Interfaces (Rec_Typ)) then
2264 declare
2265 Insert_Nod : Node_Id;
2266 Prim : Entity_Id;
2267 Prim_Elmt : Elmt_Id;
2268 Prim_Decl : Node_Id;
2269 Subp : Entity_Id;
2270 Wrap_Body : Node_Id;
2271 Wrap_Id : Entity_Id;
2273 begin
2274 Insert_Nod := N;
2276 -- Examine all primitive operations of the corresponding record
2277 -- type, looking for wrapper specs. Generate bodies in order to
2278 -- complete them.
2280 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2281 while Present (Prim_Elmt) loop
2282 Prim := Node (Prim_Elmt);
2284 if (Ekind (Prim) = E_Function
2285 or else Ekind (Prim) = E_Procedure)
2286 and then Is_Primitive_Wrapper (Prim)
2287 then
2288 Subp := Wrapped_Entity (Prim);
2289 Prim_Decl := Parent (Parent (Prim));
2291 Wrap_Body :=
2292 Build_Wrapper_Body (Loc,
2293 Subp_Id => Subp,
2294 Obj_Typ => Rec_Typ,
2295 Formals => Parameter_Specifications (Parent (Subp)));
2296 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2298 Set_Corresponding_Spec (Wrap_Body, Prim);
2299 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2301 Insert_After (Insert_Nod, Wrap_Body);
2302 Insert_Nod := Wrap_Body;
2304 Analyze (Wrap_Body);
2305 end if;
2307 Next_Elmt (Prim_Elmt);
2308 end loop;
2309 end;
2310 end if;
2311 end Build_Wrapper_Bodies;
2313 ------------------------
2314 -- Build_Wrapper_Spec --
2315 ------------------------
2317 function Build_Wrapper_Spec
2318 (Subp_Id : Entity_Id;
2319 Obj_Typ : Entity_Id;
2320 Formals : List_Id) return Node_Id
2322 Loc : constant Source_Ptr := Sloc (Subp_Id);
2323 First_Param : Node_Id;
2324 Iface : Entity_Id;
2325 Iface_Elmt : Elmt_Id;
2326 Iface_Op : Entity_Id;
2327 Iface_Op_Elmt : Elmt_Id;
2329 function Overriding_Possible
2330 (Iface_Op : Entity_Id;
2331 Wrapper : Entity_Id) return Boolean;
2332 -- Determine whether a primitive operation can be overridden by Wrapper.
2333 -- Iface_Op is the candidate primitive operation of an interface type,
2334 -- Wrapper is the generated entry wrapper.
2336 function Replicate_Formals
2337 (Loc : Source_Ptr;
2338 Formals : List_Id) return List_Id;
2339 -- An explicit parameter replication is required due to the Is_Entry_
2340 -- Formal flag being set for all the formals of an entry. The explicit
2341 -- replication removes the flag that would otherwise cause a different
2342 -- path of analysis.
2344 -------------------------
2345 -- Overriding_Possible --
2346 -------------------------
2348 function Overriding_Possible
2349 (Iface_Op : Entity_Id;
2350 Wrapper : Entity_Id) return Boolean
2352 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2353 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2355 function Type_Conformant_Parameters
2356 (Iface_Op_Params : List_Id;
2357 Wrapper_Params : List_Id) return Boolean;
2358 -- Determine whether the parameters of the generated entry wrapper
2359 -- and those of a primitive operation are type conformant. During
2360 -- this check, the first parameter of the primitive operation is
2361 -- skipped if it is a controlling argument: protected functions
2362 -- may have a controlling result.
2364 --------------------------------
2365 -- Type_Conformant_Parameters --
2366 --------------------------------
2368 function Type_Conformant_Parameters
2369 (Iface_Op_Params : List_Id;
2370 Wrapper_Params : List_Id) return Boolean
2372 Iface_Op_Param : Node_Id;
2373 Iface_Op_Typ : Entity_Id;
2374 Wrapper_Param : Node_Id;
2375 Wrapper_Typ : Entity_Id;
2377 begin
2378 -- Skip the first (controlling) parameter of primitive operation
2380 Iface_Op_Param := First (Iface_Op_Params);
2382 if Present (First_Formal (Iface_Op))
2383 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2384 then
2385 Iface_Op_Param := Next (Iface_Op_Param);
2386 end if;
2388 Wrapper_Param := First (Wrapper_Params);
2389 while Present (Iface_Op_Param)
2390 and then Present (Wrapper_Param)
2391 loop
2392 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2393 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2395 -- The two parameters must be mode conformant
2397 if not Conforming_Types
2398 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2399 then
2400 return False;
2401 end if;
2403 Next (Iface_Op_Param);
2404 Next (Wrapper_Param);
2405 end loop;
2407 -- One of the lists is longer than the other
2409 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2410 return False;
2411 end if;
2413 return True;
2414 end Type_Conformant_Parameters;
2416 -- Start of processing for Overriding_Possible
2418 begin
2419 if Chars (Iface_Op) /= Chars (Wrapper) then
2420 return False;
2421 end if;
2423 -- If an inherited subprogram is implemented by a protected procedure
2424 -- or an entry, then the first parameter of the inherited subprogram
2425 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2427 if Ekind (Iface_Op) = E_Procedure
2428 and then Present (Parameter_Specifications (Iface_Op_Spec))
2429 then
2430 declare
2431 Obj_Param : constant Node_Id :=
2432 First (Parameter_Specifications (Iface_Op_Spec));
2433 begin
2434 if not Out_Present (Obj_Param)
2435 and then Nkind (Parameter_Type (Obj_Param)) /=
2436 N_Access_Definition
2437 then
2438 return False;
2439 end if;
2440 end;
2441 end if;
2443 return
2444 Type_Conformant_Parameters (
2445 Parameter_Specifications (Iface_Op_Spec),
2446 Parameter_Specifications (Wrapper_Spec));
2447 end Overriding_Possible;
2449 -----------------------
2450 -- Replicate_Formals --
2451 -----------------------
2453 function Replicate_Formals
2454 (Loc : Source_Ptr;
2455 Formals : List_Id) return List_Id
2457 New_Formals : constant List_Id := New_List;
2458 Formal : Node_Id;
2459 Param_Type : Node_Id;
2461 begin
2462 Formal := First (Formals);
2464 -- Skip the object parameter when dealing with primitives declared
2465 -- between two views.
2467 if Is_Private_Primitive_Subprogram (Subp_Id)
2468 and then not Has_Controlling_Result (Subp_Id)
2469 then
2470 Formal := Next (Formal);
2471 end if;
2473 while Present (Formal) loop
2475 -- Create an explicit copy of the entry parameter
2477 -- When creating the wrapper subprogram for a primitive operation
2478 -- of a protected interface we must construct an equivalent
2479 -- signature to that of the overriding operation. For regular
2480 -- parameters we can just use the type of the formal, but for
2481 -- access to subprogram parameters we need to reanalyze the
2482 -- parameter type to create local entities for the signature of
2483 -- the subprogram type. Using the entities of the overriding
2484 -- subprogram will result in out-of-scope errors in the back-end.
2486 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2487 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2488 else
2489 Param_Type :=
2490 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2491 end if;
2493 Append_To (New_Formals,
2494 Make_Parameter_Specification (Loc,
2495 Defining_Identifier =>
2496 Make_Defining_Identifier (Loc,
2497 Chars => Chars (Defining_Identifier (Formal))),
2498 In_Present => In_Present (Formal),
2499 Out_Present => Out_Present (Formal),
2500 Parameter_Type => Param_Type));
2502 Next (Formal);
2503 end loop;
2505 return New_Formals;
2506 end Replicate_Formals;
2508 -- Start of processing for Build_Wrapper_Spec
2510 begin
2511 -- There is no point in building wrappers for non-tagged concurrent
2512 -- types.
2514 pragma Assert (Is_Tagged_Type (Obj_Typ));
2516 -- An entry or a protected procedure can override a routine where the
2517 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2518 -- type. Since the wrapper must have the exact same signature as that of
2519 -- the overridden subprogram, we try to find the overriding candidate
2520 -- and use its controlling formal.
2522 First_Param := Empty;
2524 -- Check every implemented interface
2526 if Present (Interfaces (Obj_Typ)) then
2527 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2528 Search : while Present (Iface_Elmt) loop
2529 Iface := Node (Iface_Elmt);
2531 -- Check every interface primitive
2533 if Present (Primitive_Operations (Iface)) then
2534 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2535 while Present (Iface_Op_Elmt) loop
2536 Iface_Op := Node (Iface_Op_Elmt);
2538 -- Ignore predefined primitives
2540 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2541 Iface_Op := Ultimate_Alias (Iface_Op);
2543 -- The current primitive operation can be overridden by
2544 -- the generated entry wrapper.
2546 if Overriding_Possible (Iface_Op, Subp_Id) then
2547 First_Param :=
2548 First (Parameter_Specifications (Parent (Iface_Op)));
2550 exit Search;
2551 end if;
2552 end if;
2554 Next_Elmt (Iface_Op_Elmt);
2555 end loop;
2556 end if;
2558 Next_Elmt (Iface_Elmt);
2559 end loop Search;
2560 end if;
2562 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2563 -- this subprogram and this is not a primitive declared between two
2564 -- views then force the generation of a wrapper. As an optimization,
2565 -- previous versions of the frontend avoid generating the wrapper;
2566 -- however, the wrapper facilitates locating and reporting an error
2567 -- when a duplicate declaration is found later. See example in
2568 -- AI05-0090-1.
2570 if No (First_Param)
2571 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2572 then
2573 if Is_Task_Type
2574 (Corresponding_Concurrent_Type (Obj_Typ))
2575 then
2576 First_Param :=
2577 Make_Parameter_Specification (Loc,
2578 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2579 In_Present => True,
2580 Out_Present => False,
2581 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2583 -- For entries and procedures of protected types the mode of
2584 -- the controlling argument must be in-out.
2586 else
2587 First_Param :=
2588 Make_Parameter_Specification (Loc,
2589 Defining_Identifier =>
2590 Make_Defining_Identifier (Loc,
2591 Chars => Name_uO),
2592 In_Present => True,
2593 Out_Present => (Ekind (Subp_Id) /= E_Function),
2594 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2595 end if;
2596 end if;
2598 declare
2599 Wrapper_Id : constant Entity_Id :=
2600 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2601 New_Formals : List_Id;
2602 Obj_Param : Node_Id;
2603 Obj_Param_Typ : Entity_Id;
2605 begin
2606 -- Minimum decoration is needed to catch the entity in
2607 -- Sem_Ch6.Override_Dispatching_Operation.
2609 if Ekind (Subp_Id) = E_Function then
2610 Set_Ekind (Wrapper_Id, E_Function);
2611 else
2612 Set_Ekind (Wrapper_Id, E_Procedure);
2613 end if;
2615 Set_Is_Primitive_Wrapper (Wrapper_Id);
2616 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2617 Set_Is_Private_Primitive (Wrapper_Id,
2618 Is_Private_Primitive_Subprogram (Subp_Id));
2620 -- Process the formals
2622 New_Formals := Replicate_Formals (Loc, Formals);
2624 -- A function with a controlling result and no first controlling
2625 -- formal needs no additional parameter.
2627 if Has_Controlling_Result (Subp_Id)
2628 and then
2629 (No (First_Formal (Subp_Id))
2630 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2631 then
2632 null;
2634 -- Routine Subp_Id has been found to override an interface primitive.
2635 -- If the interface operation has an access parameter, create a copy
2636 -- of it, with the same null exclusion indicator if present.
2638 elsif Present (First_Param) then
2639 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2640 Obj_Param_Typ :=
2641 Make_Access_Definition (Loc,
2642 Subtype_Mark =>
2643 New_Reference_To (Obj_Typ, Loc));
2644 Set_Null_Exclusion_Present (Obj_Param_Typ,
2645 Null_Exclusion_Present (Parameter_Type (First_Param)));
2647 else
2648 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2649 end if;
2651 Obj_Param :=
2652 Make_Parameter_Specification (Loc,
2653 Defining_Identifier =>
2654 Make_Defining_Identifier (Loc,
2655 Chars => Name_uO),
2656 In_Present => In_Present (First_Param),
2657 Out_Present => Out_Present (First_Param),
2658 Parameter_Type => Obj_Param_Typ);
2660 Prepend_To (New_Formals, Obj_Param);
2662 -- If we are dealing with a primitive declared between two views,
2663 -- implemented by a synchronized operation, we need to create
2664 -- a default parameter. The mode of the parameter must match that
2665 -- of the primitive operation.
2667 else
2668 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2669 Obj_Param :=
2670 Make_Parameter_Specification (Loc,
2671 Defining_Identifier =>
2672 Make_Defining_Identifier (Loc, Name_uO),
2673 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2674 Out_Present => Ekind (Subp_Id) /= E_Function,
2675 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2676 Prepend_To (New_Formals, Obj_Param);
2677 end if;
2679 -- Build the final spec. If it is a function with a controlling
2680 -- result, it is a primitive operation of the corresponding
2681 -- record type, so mark the spec accordingly.
2683 if Ekind (Subp_Id) = E_Function then
2684 declare
2685 Res_Def : Node_Id;
2687 begin
2688 if Has_Controlling_Result (Subp_Id) then
2689 Res_Def :=
2690 New_Occurrence_Of
2691 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2692 else
2693 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2694 end if;
2696 return
2697 Make_Function_Specification (Loc,
2698 Defining_Unit_Name => Wrapper_Id,
2699 Parameter_Specifications => New_Formals,
2700 Result_Definition => Res_Def);
2701 end;
2702 else
2703 return
2704 Make_Procedure_Specification (Loc,
2705 Defining_Unit_Name => Wrapper_Id,
2706 Parameter_Specifications => New_Formals);
2707 end if;
2708 end;
2709 end Build_Wrapper_Spec;
2711 -------------------------
2712 -- Build_Wrapper_Specs --
2713 -------------------------
2715 procedure Build_Wrapper_Specs
2716 (Loc : Source_Ptr;
2717 Typ : Entity_Id;
2718 N : in out Node_Id)
2720 Def : Node_Id;
2721 Rec_Typ : Entity_Id;
2722 procedure Scan_Declarations (L : List_Id);
2723 -- Common processing for visible and private declarations
2724 -- of a protected type.
2726 procedure Scan_Declarations (L : List_Id) is
2727 Decl : Node_Id;
2728 Wrap_Decl : Node_Id;
2729 Wrap_Spec : Node_Id;
2731 begin
2732 if No (L) then
2733 return;
2734 end if;
2736 Decl := First (L);
2737 while Present (Decl) loop
2738 Wrap_Spec := Empty;
2740 if Nkind (Decl) = N_Entry_Declaration
2741 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2742 then
2743 Wrap_Spec :=
2744 Build_Wrapper_Spec
2745 (Subp_Id => Defining_Identifier (Decl),
2746 Obj_Typ => Rec_Typ,
2747 Formals => Parameter_Specifications (Decl));
2749 elsif Nkind (Decl) = N_Subprogram_Declaration then
2750 Wrap_Spec :=
2751 Build_Wrapper_Spec
2752 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2753 Obj_Typ => Rec_Typ,
2754 Formals =>
2755 Parameter_Specifications (Specification (Decl)));
2756 end if;
2758 if Present (Wrap_Spec) then
2759 Wrap_Decl :=
2760 Make_Subprogram_Declaration (Loc,
2761 Specification => Wrap_Spec);
2763 Insert_After (N, Wrap_Decl);
2764 N := Wrap_Decl;
2766 Analyze (Wrap_Decl);
2767 end if;
2769 Next (Decl);
2770 end loop;
2771 end Scan_Declarations;
2773 -- start of processing for Build_Wrapper_Specs
2775 begin
2776 if Is_Protected_Type (Typ) then
2777 Def := Protected_Definition (Parent (Typ));
2778 else pragma Assert (Is_Task_Type (Typ));
2779 Def := Task_Definition (Parent (Typ));
2780 end if;
2782 Rec_Typ := Corresponding_Record_Type (Typ);
2784 -- Generate wrapper specs for a concurrent type which implements an
2785 -- interface. Operations in both the visible and private parts may
2786 -- implement progenitor operations.
2788 if Present (Interfaces (Rec_Typ))
2789 and then Present (Def)
2790 then
2791 Scan_Declarations (Visible_Declarations (Def));
2792 Scan_Declarations (Private_Declarations (Def));
2793 end if;
2794 end Build_Wrapper_Specs;
2796 ---------------------------
2797 -- Build_Find_Body_Index --
2798 ---------------------------
2800 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2801 Loc : constant Source_Ptr := Sloc (Typ);
2802 Ent : Entity_Id;
2803 E_Typ : Entity_Id;
2804 Has_F : Boolean := False;
2805 Index : Nat;
2806 If_St : Node_Id := Empty;
2807 Lo : Node_Id;
2808 Hi : Node_Id;
2809 Decls : List_Id := New_List;
2810 Ret : Node_Id;
2811 Spec : Node_Id;
2812 Siz : Node_Id := Empty;
2814 procedure Add_If_Clause (Expr : Node_Id);
2815 -- Add test for range of current entry
2817 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2818 -- If a bound of an entry is given by a discriminant, retrieve the
2819 -- actual value of the discriminant from the enclosing object.
2821 -------------------
2822 -- Add_If_Clause --
2823 -------------------
2825 procedure Add_If_Clause (Expr : Node_Id) is
2826 Cond : Node_Id;
2827 Stats : constant List_Id :=
2828 New_List (
2829 Make_Simple_Return_Statement (Loc,
2830 Expression => Make_Integer_Literal (Loc, Index + 1)));
2832 begin
2833 -- Index for current entry body
2835 Index := Index + 1;
2837 -- Compute total length of entry queues so far
2839 if No (Siz) then
2840 Siz := Expr;
2841 else
2842 Siz :=
2843 Make_Op_Add (Loc,
2844 Left_Opnd => Siz,
2845 Right_Opnd => Expr);
2846 end if;
2848 Cond :=
2849 Make_Op_Le (Loc,
2850 Left_Opnd => Make_Identifier (Loc, Name_uE),
2851 Right_Opnd => Siz);
2853 -- Map entry queue indexes in the range of the current family
2854 -- into the current index, that designates the entry body.
2856 if No (If_St) then
2857 If_St :=
2858 Make_Implicit_If_Statement (Typ,
2859 Condition => Cond,
2860 Then_Statements => Stats,
2861 Elsif_Parts => New_List);
2862 Ret := If_St;
2864 else
2865 Append_To (Elsif_Parts (If_St),
2866 Make_Elsif_Part (Loc,
2867 Condition => Cond,
2868 Then_Statements => Stats));
2869 end if;
2870 end Add_If_Clause;
2872 ------------------------------
2873 -- Convert_Discriminant_Ref --
2874 ------------------------------
2876 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2877 B : Node_Id;
2879 begin
2880 if Is_Entity_Name (Bound)
2881 and then Ekind (Entity (Bound)) = E_Discriminant
2882 then
2883 B :=
2884 Make_Selected_Component (Loc,
2885 Prefix =>
2886 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2887 Make_Explicit_Dereference (Loc,
2888 Make_Identifier (Loc, Name_uObject))),
2889 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2890 Set_Etype (B, Etype (Entity (Bound)));
2891 else
2892 B := New_Copy_Tree (Bound);
2893 end if;
2895 return B;
2896 end Convert_Discriminant_Ref;
2898 -- Start of processing for Build_Find_Body_Index
2900 begin
2901 Spec := Build_Find_Body_Index_Spec (Typ);
2903 Ent := First_Entity (Typ);
2904 while Present (Ent) loop
2905 if Ekind (Ent) = E_Entry_Family then
2906 Has_F := True;
2907 exit;
2908 end if;
2910 Next_Entity (Ent);
2911 end loop;
2913 if not Has_F then
2915 -- If the protected type has no entry families, there is a one-one
2916 -- correspondence between entry queue and entry body.
2918 Ret :=
2919 Make_Simple_Return_Statement (Loc,
2920 Expression => Make_Identifier (Loc, Name_uE));
2922 else
2923 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2924 -- the following:
2926 -- if E <= l1 then return 1;
2927 -- elsif E <= l1 + l2 then return 2;
2928 -- ...
2930 Index := 0;
2931 Siz := Empty;
2932 Ent := First_Entity (Typ);
2934 Add_Object_Pointer (Loc, Typ, Decls);
2936 while Present (Ent) loop
2937 if Ekind (Ent) = E_Entry then
2938 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2940 elsif Ekind (Ent) = E_Entry_Family then
2941 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2942 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2943 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2944 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2945 end if;
2947 Next_Entity (Ent);
2948 end loop;
2950 if Index = 1 then
2951 Decls := New_List;
2952 Ret :=
2953 Make_Simple_Return_Statement (Loc,
2954 Expression => Make_Integer_Literal (Loc, 1));
2956 elsif Nkind (Ret) = N_If_Statement then
2958 -- Ranges are in increasing order, so last one doesn't need guard
2960 declare
2961 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2962 begin
2963 Remove (Nod);
2964 Set_Else_Statements (Ret, Then_Statements (Nod));
2965 end;
2966 end if;
2967 end if;
2969 return
2970 Make_Subprogram_Body (Loc,
2971 Specification => Spec,
2972 Declarations => Decls,
2973 Handled_Statement_Sequence =>
2974 Make_Handled_Sequence_Of_Statements (Loc,
2975 Statements => New_List (Ret)));
2976 end Build_Find_Body_Index;
2978 --------------------------------
2979 -- Build_Find_Body_Index_Spec --
2980 --------------------------------
2982 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2983 Loc : constant Source_Ptr := Sloc (Typ);
2984 Id : constant Entity_Id :=
2985 Make_Defining_Identifier (Loc,
2986 Chars => New_External_Name (Chars (Typ), 'F'));
2987 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2988 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2990 begin
2991 return
2992 Make_Function_Specification (Loc,
2993 Defining_Unit_Name => Id,
2994 Parameter_Specifications => New_List (
2995 Make_Parameter_Specification (Loc,
2996 Defining_Identifier => Parm1,
2997 Parameter_Type =>
2998 New_Reference_To (RTE (RE_Address), Loc)),
3000 Make_Parameter_Specification (Loc,
3001 Defining_Identifier => Parm2,
3002 Parameter_Type =>
3003 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
3005 Result_Definition => New_Occurrence_Of (
3006 RTE (RE_Protected_Entry_Index), Loc));
3007 end Build_Find_Body_Index_Spec;
3009 -----------------------------------------------
3010 -- Build_Lock_Free_Protected_Subprogram_Body --
3011 -----------------------------------------------
3013 function Build_Lock_Free_Protected_Subprogram_Body
3014 (N : Node_Id;
3015 Prot_Typ : Node_Id;
3016 Unprot_Spec : Node_Id) return Node_Id
3018 Actuals : constant List_Id := New_List;
3019 Loc : constant Source_Ptr := Sloc (N);
3020 Spec : constant Node_Id := Specification (N);
3021 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3022 Formal : Node_Id;
3023 Prot_Spec : Node_Id;
3024 Stmt : Node_Id;
3026 begin
3027 -- Create the protected version of the body
3029 Prot_Spec :=
3030 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3032 -- Build the actual parameters which appear in the call to the
3033 -- unprotected version of the body.
3035 Formal := First (Parameter_Specifications (Prot_Spec));
3036 while Present (Formal) loop
3037 Append_To (Actuals,
3038 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3040 Next (Formal);
3041 end loop;
3043 -- Function case, generate:
3044 -- return <Unprot_Func_Call>;
3046 if Nkind (Spec) = N_Function_Specification then
3047 Stmt :=
3048 Make_Simple_Return_Statement (Loc,
3049 Expression =>
3050 Make_Function_Call (Loc,
3051 Name =>
3052 Make_Identifier (Loc, Chars (Unprot_Id)),
3053 Parameter_Associations => Actuals));
3055 -- Procedure case, call the unprotected version
3057 else
3058 Stmt :=
3059 Make_Procedure_Call_Statement (Loc,
3060 Name =>
3061 Make_Identifier (Loc, Chars (Unprot_Id)),
3062 Parameter_Associations => Actuals);
3063 end if;
3065 return
3066 Make_Subprogram_Body (Loc,
3067 Declarations => Empty_List,
3068 Specification => Prot_Spec,
3069 Handled_Statement_Sequence =>
3070 Make_Handled_Sequence_Of_Statements (Loc,
3071 Statements => New_List (Stmt)));
3072 end Build_Lock_Free_Protected_Subprogram_Body;
3074 -------------------------------------------------
3075 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3076 -------------------------------------------------
3078 -- Procedures which meet the lock-free implementation requirements and
3079 -- reference a unique scalar component Comp are expanded in the following
3080 -- manner:
3082 -- procedure P (...) is
3083 -- Expected_Comp : constant Comp_Type :=
3084 -- Comp_Type
3085 -- (System.Atomic_Primitives.Lock_Free_Read_N
3086 -- (_Object.Comp'Address));
3087 -- begin
3088 -- loop
3089 -- declare
3090 -- <original declarations before the object renaming declaration
3091 -- of Comp>
3093 -- Desired_Comp : Comp_Type := Expected_Comp;
3094 -- Comp : Comp_Type renames Desired_Comp;
3096 -- <original delarations after the object renaming declaration
3097 -- of Comp>
3099 -- begin
3100 -- <original statements>
3101 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3102 -- (_Object.Comp'Address,
3103 -- Interfaces.Unsigned_N (Expected_Comp),
3104 -- Interfaces.Unsigned_N (Desired_Comp));
3105 -- end;
3106 -- end loop;
3107 -- end P;
3109 -- Each return and raise statement of P is transformed into an atomic
3110 -- status check:
3112 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3113 -- (_Object.Comp'Address,
3114 -- Interfaces.Unsigned_N (Expected_Comp),
3115 -- Interfaces.Unsigned_N (Desired_Comp));
3116 -- then
3117 -- <original statement>
3118 -- else
3119 -- goto L0;
3120 -- end if;
3122 -- Functions which meet the lock-free implementation requirements and
3123 -- reference a unique scalar component Comp are expanded in the following
3124 -- manner:
3126 -- function F (...) return ... is
3127 -- <original declarations before the object renaming declaration
3128 -- of Comp>
3130 -- Expected_Comp : constant Comp_Type :=
3131 -- Comp_Type
3132 -- (System.Atomic_Primitives.Lock_Free_Read_N
3133 -- (_Object.Comp'Address));
3134 -- Comp : Comp_Type renames Expected_Comp;
3136 -- <original delarations after the object renaming declaration of
3137 -- Comp>
3139 -- begin
3140 -- <original statements>
3141 -- end F;
3143 function Build_Lock_Free_Unprotected_Subprogram_Body
3144 (N : Node_Id;
3145 Prot_Typ : Node_Id) return Node_Id
3147 function Referenced_Component (N : Node_Id) return Entity_Id;
3148 -- Subprograms which meet the lock-free implementation criteria are
3149 -- allowed to reference only one unique component. Return the prival
3150 -- of the said component.
3152 --------------------------
3153 -- Referenced_Component --
3154 --------------------------
3156 function Referenced_Component (N : Node_Id) return Entity_Id is
3157 Comp : Entity_Id;
3158 Decl : Node_Id;
3159 Source_Comp : Entity_Id := Empty;
3161 begin
3162 -- Find the unique source component which N references in its
3163 -- statements.
3165 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3166 declare
3167 Element : Lock_Free_Subprogram renames
3168 Lock_Free_Subprogram_Table.Table (Index);
3169 begin
3170 if Element.Sub_Body = N then
3171 Source_Comp := Element.Comp_Id;
3172 exit;
3173 end if;
3174 end;
3175 end loop;
3177 if No (Source_Comp) then
3178 return Empty;
3179 end if;
3181 -- Find the prival which corresponds to the source component within
3182 -- the declarations of N.
3184 Decl := First (Declarations (N));
3185 while Present (Decl) loop
3187 -- Privals appear as object renamings
3189 if Nkind (Decl) = N_Object_Renaming_Declaration then
3190 Comp := Defining_Identifier (Decl);
3192 if Present (Prival_Link (Comp))
3193 and then Prival_Link (Comp) = Source_Comp
3194 then
3195 return Comp;
3196 end if;
3197 end if;
3199 Next (Decl);
3200 end loop;
3202 return Empty;
3203 end Referenced_Component;
3205 -- Local variables
3207 Comp : constant Entity_Id := Referenced_Component (N);
3208 Loc : constant Source_Ptr := Sloc (N);
3209 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3210 Decls : List_Id := Declarations (N);
3212 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3214 begin
3215 -- Add renamings for the protection object, discriminals, privals and
3216 -- the entry index constant for use by debugger.
3218 Debug_Private_Data_Declarations (Decls);
3220 -- Perform the lock-free expansion when the subprogram references a
3221 -- protected component.
3223 if Present (Comp) then
3224 Protected_Component_Ref : declare
3225 Comp_Decl : constant Node_Id := Parent (Comp);
3226 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3227 Comp_Type : constant Entity_Id := Etype (Comp);
3229 Is_Procedure : constant Boolean :=
3230 Ekind (Corresponding_Spec (N)) = E_Procedure;
3231 -- Indicates if N is a protected procedure body
3233 Block_Decls : List_Id;
3234 Try_Write : Entity_Id;
3235 Desired_Comp : Entity_Id;
3236 Decl : Node_Id;
3237 Label : Node_Id;
3238 Label_Id : Entity_Id := Empty;
3239 Read : Entity_Id;
3240 Expected_Comp : Entity_Id;
3241 Stmt : Node_Id;
3242 Stmts : List_Id :=
3243 New_Copy_List (Statements (Hand_Stmt_Seq));
3244 Typ_Size : Int;
3245 Unsigned : Entity_Id;
3247 function Process_Node (N : Node_Id) return Traverse_Result;
3248 -- Transform a single node if it is a return statement, a raise
3249 -- statement or a reference to Comp.
3251 procedure Process_Stmts (Stmts : List_Id);
3252 -- Given a statement sequence Stmts, wrap any return or raise
3253 -- statements in the following manner:
3255 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3256 -- (_Object.Comp'Address,
3257 -- Interfaces.Unsigned_N (Expected_Comp),
3258 -- Interfaces.Unsigned_N (Desired_Comp))
3259 -- then
3260 -- <Stmt>;
3261 -- else
3262 -- goto L0;
3263 -- end if;
3265 ------------------
3266 -- Process_Node --
3267 ------------------
3269 function Process_Node (N : Node_Id) return Traverse_Result is
3271 procedure Wrap_Statement (Stmt : Node_Id);
3272 -- Wrap an arbitrary statement inside an if statement where the
3273 -- condition does an atomic check on the state of the object.
3275 --------------------
3276 -- Wrap_Statement --
3277 --------------------
3279 procedure Wrap_Statement (Stmt : Node_Id) is
3280 begin
3281 -- The first time through, create the declaration of a label
3282 -- which is used to skip the remainder of source statements
3283 -- if the state of the object has changed.
3285 if No (Label_Id) then
3286 Label_Id :=
3287 Make_Identifier (Loc, New_External_Name ('L', 0));
3288 Set_Entity (Label_Id,
3289 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3290 end if;
3292 -- Generate:
3293 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3294 -- (_Object.Comp'Address,
3295 -- Interfaces.Unsigned_N (Expected_Comp),
3296 -- Interfaces.Unsigned_N (Desired_Comp))
3297 -- then
3298 -- <Stmt>;
3299 -- else
3300 -- goto L0;
3301 -- end if;
3303 Rewrite (Stmt,
3304 Make_Implicit_If_Statement (N,
3305 Condition =>
3306 Make_Function_Call (Loc,
3307 Name =>
3308 New_Reference_To (Try_Write, Loc),
3309 Parameter_Associations => New_List (
3310 Make_Attribute_Reference (Loc,
3311 Prefix => Relocate_Node (Comp_Sel_Nam),
3312 Attribute_Name => Name_Address),
3314 Unchecked_Convert_To (Unsigned,
3315 New_Reference_To (Expected_Comp, Loc)),
3317 Unchecked_Convert_To (Unsigned,
3318 New_Reference_To (Desired_Comp, Loc)))),
3320 Then_Statements => New_List (Relocate_Node (Stmt)),
3322 Else_Statements => New_List (
3323 Make_Goto_Statement (Loc,
3324 Name =>
3325 New_Reference_To (Entity (Label_Id), Loc)))));
3326 end Wrap_Statement;
3328 -- Start of processing for Process_Node
3330 begin
3331 -- Wrap each return and raise statement that appear inside a
3332 -- procedure. Skip the last return statement which is added by
3333 -- default since it is transformed into an exit statement.
3335 if Is_Procedure
3336 and then ((Nkind (N) = N_Simple_Return_Statement
3337 and then N /= Last (Stmts))
3338 or else Nkind (N) = N_Extended_Return_Statement
3339 or else (Nkind_In (N, N_Raise_Constraint_Error,
3340 N_Raise_Program_Error,
3341 N_Raise_Statement,
3342 N_Raise_Storage_Error)
3343 and then Comes_From_Source (N)))
3344 then
3345 Wrap_Statement (N);
3346 return Skip;
3347 end if;
3349 -- Force reanalysis
3351 Set_Analyzed (N, False);
3353 return OK;
3354 end Process_Node;
3356 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3358 -------------------
3359 -- Process_Stmts --
3360 -------------------
3362 procedure Process_Stmts (Stmts : List_Id) is
3363 Stmt : Node_Id;
3364 begin
3365 Stmt := First (Stmts);
3366 while Present (Stmt) loop
3367 Process_Nodes (Stmt);
3368 Next (Stmt);
3369 end loop;
3370 end Process_Stmts;
3372 -- Start of processing for Protected_Component_Ref
3374 begin
3375 -- Get the type size
3377 if Known_Static_Esize (Comp_Type) then
3378 Typ_Size := UI_To_Int (Esize (Comp_Type));
3380 -- If the Esize (Object_Size) is unknown at compile time, look at
3381 -- the RM_Size (Value_Size) since it may have been set by an
3382 -- explicit representation clause.
3384 elsif Known_Static_RM_Size (Comp_Type) then
3385 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3387 -- Should not happen since this has already been checked in
3388 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3390 else
3391 raise Program_Error;
3392 end if;
3394 -- Retrieve all relevant atomic routines and types
3396 case Typ_Size is
3397 when 8 =>
3398 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3399 Read := RTE (RE_Lock_Free_Read_8);
3400 Unsigned := RTE (RE_Uint8);
3402 when 16 =>
3403 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3404 Read := RTE (RE_Lock_Free_Read_16);
3405 Unsigned := RTE (RE_Uint16);
3407 when 32 =>
3408 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3409 Read := RTE (RE_Lock_Free_Read_32);
3410 Unsigned := RTE (RE_Uint32);
3412 when 64 =>
3413 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3414 Read := RTE (RE_Lock_Free_Read_64);
3415 Unsigned := RTE (RE_Uint64);
3417 when others =>
3418 raise Program_Error;
3419 end case;
3421 -- Generate:
3422 -- Expected_Comp : constant Comp_Type :=
3423 -- Comp_Type
3424 -- (System.Atomic_Primitives.Lock_Free_Read_N
3425 -- (_Object.Comp'Address));
3427 Expected_Comp :=
3428 Make_Defining_Identifier (Loc,
3429 New_External_Name (Chars (Comp), Suffix => "_saved"));
3431 Decl :=
3432 Make_Object_Declaration (Loc,
3433 Defining_Identifier => Expected_Comp,
3434 Object_Definition => New_Reference_To (Comp_Type, Loc),
3435 Constant_Present => True,
3436 Expression =>
3437 Unchecked_Convert_To (Comp_Type,
3438 Make_Function_Call (Loc,
3439 Name => New_Reference_To (Read, Loc),
3440 Parameter_Associations => New_List (
3441 Make_Attribute_Reference (Loc,
3442 Prefix => Relocate_Node (Comp_Sel_Nam),
3443 Attribute_Name => Name_Address)))));
3445 -- Protected procedures
3447 if Is_Procedure then
3448 -- Move the original declarations inside the generated block
3450 Block_Decls := Decls;
3452 -- Reset the declarations list of the protected procedure to
3453 -- contain only Decl.
3455 Decls := New_List (Decl);
3457 -- Generate:
3458 -- Desired_Comp : Comp_Type := Expected_Comp;
3460 Desired_Comp :=
3461 Make_Defining_Identifier (Loc,
3462 New_External_Name (Chars (Comp), Suffix => "_current"));
3464 -- Insert the declarations of Expected_Comp and Desired_Comp in
3465 -- the block declarations right before the renaming of the
3466 -- protected component.
3468 Insert_Before (Comp_Decl,
3469 Make_Object_Declaration (Loc,
3470 Defining_Identifier => Desired_Comp,
3471 Object_Definition => New_Reference_To (Comp_Type, Loc),
3472 Expression =>
3473 New_Reference_To (Expected_Comp, Loc)));
3475 -- Protected function
3477 else
3478 Desired_Comp := Expected_Comp;
3480 -- Insert the declaration of Expected_Comp in the function
3481 -- declarations right before the renaming of the protected
3482 -- component.
3484 Insert_Before (Comp_Decl, Decl);
3485 end if;
3487 -- Rewrite the protected component renaming declaration to be a
3488 -- renaming of Desired_Comp.
3490 -- Generate:
3491 -- Comp : Comp_Type renames Desired_Comp;
3493 Rewrite (Comp_Decl,
3494 Make_Object_Renaming_Declaration (Loc,
3495 Defining_Identifier =>
3496 Defining_Identifier (Comp_Decl),
3497 Subtype_Mark =>
3498 New_Occurrence_Of (Comp_Type, Loc),
3499 Name =>
3500 New_Reference_To (Desired_Comp, Loc)));
3502 -- Wrap any return or raise statements in Stmts in same the manner
3503 -- described in Process_Stmts.
3505 Process_Stmts (Stmts);
3507 -- Generate:
3508 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3509 -- (_Object.Comp'Address,
3510 -- Interfaces.Unsigned_N (Expected_Comp),
3511 -- Interfaces.Unsigned_N (Desired_Comp))
3513 if Is_Procedure then
3514 Stmt :=
3515 Make_Exit_Statement (Loc,
3516 Condition =>
3517 Make_Function_Call (Loc,
3518 Name =>
3519 New_Reference_To (Try_Write, Loc),
3520 Parameter_Associations => New_List (
3521 Make_Attribute_Reference (Loc,
3522 Prefix => Relocate_Node (Comp_Sel_Nam),
3523 Attribute_Name => Name_Address),
3525 Unchecked_Convert_To (Unsigned,
3526 New_Reference_To (Expected_Comp, Loc)),
3528 Unchecked_Convert_To (Unsigned,
3529 New_Reference_To (Desired_Comp, Loc)))));
3531 -- Small optimization: transform the default return statement
3532 -- of a procedure into the atomic exit statement.
3534 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3535 Rewrite (Last (Stmts), Stmt);
3536 else
3537 Append_To (Stmts, Stmt);
3538 end if;
3539 end if;
3541 -- Create the declaration of the label used to skip the rest of
3542 -- the source statements when the object state changes.
3544 if Present (Label_Id) then
3545 Label := Make_Label (Loc, Label_Id);
3546 Append_To (Decls,
3547 Make_Implicit_Label_Declaration (Loc,
3548 Defining_Identifier => Entity (Label_Id),
3549 Label_Construct => Label));
3550 Append_To (Stmts, Label);
3551 end if;
3553 -- Generate:
3554 -- loop
3555 -- declare
3556 -- <Decls>
3557 -- begin
3558 -- <Stmts>
3559 -- end;
3560 -- end loop;
3562 if Is_Procedure then
3563 Stmts :=
3564 New_List (
3565 Make_Loop_Statement (Loc,
3566 Statements => New_List (
3567 Make_Block_Statement (Loc,
3568 Declarations => Block_Decls,
3569 Handled_Statement_Sequence =>
3570 Make_Handled_Sequence_Of_Statements (Loc,
3571 Statements => Stmts))),
3572 End_Label => Empty));
3573 end if;
3575 Hand_Stmt_Seq :=
3576 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3577 end Protected_Component_Ref;
3578 end if;
3580 -- Make an unprotected version of the subprogram for use within the same
3581 -- object, with new name and extra parameter representing the object.
3583 return
3584 Make_Subprogram_Body (Loc,
3585 Specification =>
3586 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3587 Declarations => Decls,
3588 Handled_Statement_Sequence => Hand_Stmt_Seq);
3589 end Build_Lock_Free_Unprotected_Subprogram_Body;
3591 -------------------------
3592 -- Build_Master_Entity --
3593 -------------------------
3595 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3596 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3597 Context : Node_Id;
3598 Context_Id : Entity_Id;
3599 Decl : Node_Id;
3600 Decls : List_Id;
3601 Par : Node_Id;
3603 begin
3604 if Is_Itype (Obj_Or_Typ) then
3605 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3606 else
3607 Par := Parent (Obj_Or_Typ);
3608 end if;
3610 -- When creating a master for a record component which is either a task
3611 -- or access-to-task, the enclosing record is the master scope and the
3612 -- proper insertion point is the component list.
3614 if Is_Record_Type (Current_Scope) then
3615 Context := Par;
3616 Context_Id := Current_Scope;
3617 Decls := List_Containing (Context);
3619 -- Default case for object declarations and access types. Note that the
3620 -- context is updated to the nearest enclosing body, block, package or
3621 -- return statement.
3623 else
3624 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3625 end if;
3627 -- Do not create a master if one already exists or there is no task
3628 -- hierarchy.
3630 if Has_Master_Entity (Context_Id)
3631 or else Restriction_Active (No_Task_Hierarchy)
3632 then
3633 return;
3634 end if;
3636 -- Create a master, generate:
3637 -- _Master : constant Master_Id := Current_Master.all;
3639 Decl :=
3640 Make_Object_Declaration (Loc,
3641 Defining_Identifier =>
3642 Make_Defining_Identifier (Loc, Name_uMaster),
3643 Constant_Present => True,
3644 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
3645 Expression =>
3646 Make_Explicit_Dereference (Loc,
3647 New_Reference_To (RTE (RE_Current_Master), Loc)));
3649 -- The master is inserted at the start of the declarative list of the
3650 -- context.
3652 Prepend_To (Decls, Decl);
3654 -- In certain cases where transient scopes are involved, the immediate
3655 -- scope is not always the proper master scope. Ensure that the master
3656 -- declaration and entity appear in the same context.
3658 if Context_Id /= Current_Scope then
3659 Push_Scope (Context_Id);
3660 Analyze (Decl);
3661 Pop_Scope;
3662 else
3663 Analyze (Decl);
3664 end if;
3666 -- Mark the enclosing scope and its associated construct as being task
3667 -- masters.
3669 Set_Has_Master_Entity (Context_Id);
3671 while Present (Context)
3672 and then Nkind (Context) /= N_Compilation_Unit
3673 loop
3674 if Nkind_In (Context, N_Block_Statement,
3675 N_Subprogram_Body,
3676 N_Task_Body)
3677 then
3678 Set_Is_Task_Master (Context);
3679 exit;
3681 elsif Nkind (Parent (Context)) = N_Subunit then
3682 Context := Corresponding_Stub (Parent (Context));
3683 end if;
3685 Context := Parent (Context);
3686 end loop;
3687 end Build_Master_Entity;
3689 ---------------------------
3690 -- Build_Master_Renaming --
3691 ---------------------------
3693 procedure Build_Master_Renaming
3694 (Ptr_Typ : Entity_Id;
3695 Ins_Nod : Node_Id := Empty)
3697 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3698 Context : Node_Id;
3699 Master_Decl : Node_Id;
3700 Master_Id : Entity_Id;
3702 begin
3703 -- Nothing to do if there is no task hierarchy
3705 if Restriction_Active (No_Task_Hierarchy) then
3706 return;
3707 end if;
3709 -- Determine the proper context to insert the master renaming
3711 if Present (Ins_Nod) then
3712 Context := Ins_Nod;
3713 elsif Is_Itype (Ptr_Typ) then
3714 Context := Associated_Node_For_Itype (Ptr_Typ);
3715 else
3716 Context := Parent (Ptr_Typ);
3717 end if;
3719 -- Generate:
3720 -- <Ptr_Typ>M : Master_Id renames _Master;
3722 Master_Id :=
3723 Make_Defining_Identifier (Loc,
3724 New_External_Name (Chars (Ptr_Typ), 'M'));
3726 Master_Decl :=
3727 Make_Object_Renaming_Declaration (Loc,
3728 Defining_Identifier => Master_Id,
3729 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
3730 Name => Make_Identifier (Loc, Name_uMaster));
3732 Insert_Action (Context, Master_Decl);
3734 -- The renamed master now services the access type
3736 Set_Master_Id (Ptr_Typ, Master_Id);
3737 end Build_Master_Renaming;
3739 -----------------------------------------
3740 -- Build_Private_Protected_Declaration --
3741 -----------------------------------------
3743 function Build_Private_Protected_Declaration
3744 (N : Node_Id) return Entity_Id
3746 Loc : constant Source_Ptr := Sloc (N);
3747 Body_Id : constant Entity_Id := Defining_Entity (N);
3748 Decl : Node_Id;
3749 Plist : List_Id;
3750 Formal : Entity_Id;
3751 New_Spec : Node_Id;
3752 Spec_Id : Entity_Id;
3754 begin
3755 Formal := First_Formal (Body_Id);
3757 -- The protected operation always has at least one formal, namely the
3758 -- object itself, but it is only placed in the parameter list if
3759 -- expansion is enabled.
3761 if Present (Formal) or else Expander_Active then
3762 Plist := Copy_Parameter_List (Body_Id);
3763 else
3764 Plist := No_List;
3765 end if;
3767 if Nkind (Specification (N)) = N_Procedure_Specification then
3768 New_Spec :=
3769 Make_Procedure_Specification (Loc,
3770 Defining_Unit_Name =>
3771 Make_Defining_Identifier (Sloc (Body_Id),
3772 Chars => Chars (Body_Id)),
3773 Parameter_Specifications =>
3774 Plist);
3775 else
3776 New_Spec :=
3777 Make_Function_Specification (Loc,
3778 Defining_Unit_Name =>
3779 Make_Defining_Identifier (Sloc (Body_Id),
3780 Chars => Chars (Body_Id)),
3781 Parameter_Specifications => Plist,
3782 Result_Definition =>
3783 New_Occurrence_Of (Etype (Body_Id), Loc));
3784 end if;
3786 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3787 Insert_Before (N, Decl);
3788 Spec_Id := Defining_Unit_Name (New_Spec);
3790 -- Indicate that the entity comes from source, to ensure that cross-
3791 -- reference information is properly generated. The body itself is
3792 -- rewritten during expansion, and the body entity will not appear in
3793 -- calls to the operation.
3795 Set_Comes_From_Source (Spec_Id, True);
3796 Analyze (Decl);
3797 Set_Has_Completion (Spec_Id);
3798 Set_Convention (Spec_Id, Convention_Protected);
3799 return Spec_Id;
3800 end Build_Private_Protected_Declaration;
3802 ---------------------------
3803 -- Build_Protected_Entry --
3804 ---------------------------
3806 function Build_Protected_Entry
3807 (N : Node_Id;
3808 Ent : Entity_Id;
3809 Pid : Node_Id) return Node_Id
3811 Loc : constant Source_Ptr := Sloc (N);
3813 Decls : constant List_Id := Declarations (N);
3814 End_Lab : constant Node_Id :=
3815 End_Label (Handled_Statement_Sequence (N));
3816 End_Loc : constant Source_Ptr :=
3817 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3818 -- Used for the generated call to Complete_Entry_Body
3820 Han_Loc : Source_Ptr;
3821 -- Used for the exception handler, inserted at end of the body
3823 Op_Decls : constant List_Id := New_List;
3824 Complete : Node_Id;
3825 Edef : Entity_Id;
3826 Espec : Node_Id;
3827 Ohandle : Node_Id;
3828 Op_Stats : List_Id;
3830 begin
3831 -- Set the source location on the exception handler only when debugging
3832 -- the expanded code (see Make_Implicit_Exception_Handler).
3834 if Debug_Generated_Code then
3835 Han_Loc := End_Loc;
3837 -- Otherwise the inserted code should not be visible to the debugger
3839 else
3840 Han_Loc := No_Location;
3841 end if;
3843 Edef :=
3844 Make_Defining_Identifier (Loc,
3845 Chars => Chars (Protected_Body_Subprogram (Ent)));
3846 Espec :=
3847 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3849 -- Add the following declarations:
3851 -- type poVP is access poV;
3852 -- _object : poVP := poVP (_O);
3854 -- where _O is the formal parameter associated with the concurrent
3855 -- object. These declarations are needed for Complete_Entry_Body.
3857 Add_Object_Pointer (Loc, Pid, Op_Decls);
3859 -- Add renamings for all formals, the Protection object, discriminals,
3860 -- privals and the entry index constant for use by debugger.
3862 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3863 Debug_Private_Data_Declarations (Decls);
3865 -- Put the declarations and the statements from the entry
3867 Op_Stats :=
3868 New_List (
3869 Make_Block_Statement (Loc,
3870 Declarations => Decls,
3871 Handled_Statement_Sequence =>
3872 Handled_Statement_Sequence (N)));
3874 case Corresponding_Runtime_Package (Pid) is
3875 when System_Tasking_Protected_Objects_Entries =>
3876 Append_To (Op_Stats,
3877 Make_Procedure_Call_Statement (End_Loc,
3878 Name =>
3879 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
3880 Parameter_Associations => New_List (
3881 Make_Attribute_Reference (End_Loc,
3882 Prefix =>
3883 Make_Selected_Component (End_Loc,
3884 Prefix =>
3885 Make_Identifier (End_Loc, Name_uObject),
3886 Selector_Name =>
3887 Make_Identifier (End_Loc, Name_uObject)),
3888 Attribute_Name => Name_Unchecked_Access))));
3890 when System_Tasking_Protected_Objects_Single_Entry =>
3892 -- Historically, a call to Complete_Single_Entry_Body was
3893 -- inserted, but it was a null procedure.
3895 null;
3897 when others =>
3898 raise Program_Error;
3899 end case;
3901 -- When exceptions can not be propagated, we never need to call
3902 -- Exception_Complete_Entry_Body
3904 if No_Exception_Handlers_Set then
3905 return
3906 Make_Subprogram_Body (Loc,
3907 Specification => Espec,
3908 Declarations => Op_Decls,
3909 Handled_Statement_Sequence =>
3910 Make_Handled_Sequence_Of_Statements (Loc,
3911 Statements => Op_Stats,
3912 End_Label => End_Lab));
3914 else
3915 Ohandle := Make_Others_Choice (Loc);
3916 Set_All_Others (Ohandle);
3918 case Corresponding_Runtime_Package (Pid) is
3919 when System_Tasking_Protected_Objects_Entries =>
3920 Complete :=
3921 New_Reference_To
3922 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3924 when System_Tasking_Protected_Objects_Single_Entry =>
3925 Complete :=
3926 New_Reference_To
3927 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3929 when others =>
3930 raise Program_Error;
3931 end case;
3933 -- Establish link between subprogram body entity and source entry
3935 Set_Corresponding_Protected_Entry (Edef, Ent);
3937 -- Create body of entry procedure. The renaming declarations are
3938 -- placed ahead of the block that contains the actual entry body.
3940 return
3941 Make_Subprogram_Body (Loc,
3942 Specification => Espec,
3943 Declarations => Op_Decls,
3944 Handled_Statement_Sequence =>
3945 Make_Handled_Sequence_Of_Statements (Loc,
3946 Statements => Op_Stats,
3947 End_Label => End_Lab,
3948 Exception_Handlers => New_List (
3949 Make_Implicit_Exception_Handler (Han_Loc,
3950 Exception_Choices => New_List (Ohandle),
3952 Statements => New_List (
3953 Make_Procedure_Call_Statement (Han_Loc,
3954 Name => Complete,
3955 Parameter_Associations => New_List (
3956 Make_Attribute_Reference (Han_Loc,
3957 Prefix =>
3958 Make_Selected_Component (Han_Loc,
3959 Prefix =>
3960 Make_Identifier (Han_Loc, Name_uObject),
3961 Selector_Name =>
3962 Make_Identifier (Han_Loc, Name_uObject)),
3963 Attribute_Name => Name_Unchecked_Access),
3965 Make_Function_Call (Han_Loc,
3966 Name => New_Reference_To (
3967 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3968 end if;
3969 end Build_Protected_Entry;
3971 -----------------------------------------
3972 -- Build_Protected_Entry_Specification --
3973 -----------------------------------------
3975 function Build_Protected_Entry_Specification
3976 (Loc : Source_Ptr;
3977 Def_Id : Entity_Id;
3978 Ent_Id : Entity_Id) return Node_Id
3980 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3982 begin
3983 Set_Debug_Info_Needed (Def_Id);
3985 if Present (Ent_Id) then
3986 Append_Elmt (P, Accept_Address (Ent_Id));
3987 end if;
3989 return
3990 Make_Procedure_Specification (Loc,
3991 Defining_Unit_Name => Def_Id,
3992 Parameter_Specifications => New_List (
3993 Make_Parameter_Specification (Loc,
3994 Defining_Identifier =>
3995 Make_Defining_Identifier (Loc, Name_uO),
3996 Parameter_Type =>
3997 New_Reference_To (RTE (RE_Address), Loc)),
3999 Make_Parameter_Specification (Loc,
4000 Defining_Identifier => P,
4001 Parameter_Type =>
4002 New_Reference_To (RTE (RE_Address), Loc)),
4004 Make_Parameter_Specification (Loc,
4005 Defining_Identifier =>
4006 Make_Defining_Identifier (Loc, Name_uE),
4007 Parameter_Type =>
4008 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
4009 end Build_Protected_Entry_Specification;
4011 --------------------------
4012 -- Build_Protected_Spec --
4013 --------------------------
4015 function Build_Protected_Spec
4016 (N : Node_Id;
4017 Obj_Type : Entity_Id;
4018 Ident : Entity_Id;
4019 Unprotected : Boolean := False) return List_Id
4021 Loc : constant Source_Ptr := Sloc (N);
4022 Decl : Node_Id;
4023 Formal : Entity_Id;
4024 New_Plist : List_Id;
4025 New_Param : Node_Id;
4027 begin
4028 New_Plist := New_List;
4030 Formal := First_Formal (Ident);
4031 while Present (Formal) loop
4032 New_Param :=
4033 Make_Parameter_Specification (Loc,
4034 Defining_Identifier =>
4035 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4036 In_Present => In_Present (Parent (Formal)),
4037 Out_Present => Out_Present (Parent (Formal)),
4038 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
4040 if Unprotected then
4041 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4042 end if;
4044 Append (New_Param, New_Plist);
4045 Next_Formal (Formal);
4046 end loop;
4048 -- If the subprogram is a procedure and the context is not an access
4049 -- to protected subprogram, the parameter is in-out. Otherwise it is
4050 -- an in parameter.
4052 Decl :=
4053 Make_Parameter_Specification (Loc,
4054 Defining_Identifier =>
4055 Make_Defining_Identifier (Loc, Name_uObject),
4056 In_Present => True,
4057 Out_Present =>
4058 (Etype (Ident) = Standard_Void_Type
4059 and then not Is_RTE (Obj_Type, RE_Address)),
4060 Parameter_Type =>
4061 New_Reference_To (Obj_Type, Loc));
4062 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4063 Prepend_To (New_Plist, Decl);
4065 return New_Plist;
4066 end Build_Protected_Spec;
4068 ---------------------------------------
4069 -- Build_Protected_Sub_Specification --
4070 ---------------------------------------
4072 function Build_Protected_Sub_Specification
4073 (N : Node_Id;
4074 Prot_Typ : Entity_Id;
4075 Mode : Subprogram_Protection_Mode) return Node_Id
4077 Loc : constant Source_Ptr := Sloc (N);
4078 Decl : Node_Id;
4079 Def_Id : Entity_Id;
4080 New_Id : Entity_Id;
4081 New_Plist : List_Id;
4082 New_Spec : Node_Id;
4084 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4085 (Dispatching_Mode => ' ',
4086 Protected_Mode => 'P',
4087 Unprotected_Mode => 'N');
4089 begin
4090 if Ekind (Defining_Unit_Name (Specification (N))) =
4091 E_Subprogram_Body
4092 then
4093 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4094 else
4095 Decl := N;
4096 end if;
4098 Def_Id := Defining_Unit_Name (Specification (Decl));
4100 New_Plist :=
4101 Build_Protected_Spec
4102 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4103 Mode = Unprotected_Mode);
4104 New_Id :=
4105 Make_Defining_Identifier (Loc,
4106 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4108 -- The unprotected operation carries the user code, and debugging
4109 -- information must be generated for it, even though this spec does
4110 -- not come from source. It is also convenient to allow gdb to step
4111 -- into the protected operation, even though it only contains lock/
4112 -- unlock calls.
4114 Set_Debug_Info_Needed (New_Id);
4116 -- If a pragma Eliminate applies to the source entity, the internal
4117 -- subprograms will be eliminated as well.
4119 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4121 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4122 New_Spec :=
4123 Make_Procedure_Specification (Loc,
4124 Defining_Unit_Name => New_Id,
4125 Parameter_Specifications => New_Plist);
4127 -- Create a new specification for the anonymous subprogram type
4129 else
4130 New_Spec :=
4131 Make_Function_Specification (Loc,
4132 Defining_Unit_Name => New_Id,
4133 Parameter_Specifications => New_Plist,
4134 Result_Definition =>
4135 Copy_Result_Type (Result_Definition (Specification (Decl))));
4137 Set_Return_Present (Defining_Unit_Name (New_Spec));
4138 end if;
4140 return New_Spec;
4141 end Build_Protected_Sub_Specification;
4143 -------------------------------------
4144 -- Build_Protected_Subprogram_Body --
4145 -------------------------------------
4147 function Build_Protected_Subprogram_Body
4148 (N : Node_Id;
4149 Pid : Node_Id;
4150 N_Op_Spec : Node_Id) return Node_Id
4152 Loc : constant Source_Ptr := Sloc (N);
4153 Op_Spec : Node_Id;
4154 P_Op_Spec : Node_Id;
4155 Uactuals : List_Id;
4156 Pformal : Node_Id;
4157 Unprot_Call : Node_Id;
4158 Sub_Body : Node_Id;
4159 Lock_Name : Node_Id;
4160 Lock_Stmt : Node_Id;
4161 R : Node_Id;
4162 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4163 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4164 Stmts : List_Id;
4165 Object_Parm : Node_Id;
4166 Exc_Safe : Boolean;
4167 Lock_Kind : RE_Id;
4169 begin
4170 Op_Spec := Specification (N);
4171 Exc_Safe := Is_Exception_Safe (N);
4173 P_Op_Spec :=
4174 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4176 -- Build a list of the formal parameters of the protected version of
4177 -- the subprogram to use as the actual parameters of the unprotected
4178 -- version.
4180 Uactuals := New_List;
4181 Pformal := First (Parameter_Specifications (P_Op_Spec));
4182 while Present (Pformal) loop
4183 Append_To (Uactuals,
4184 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4185 Next (Pformal);
4186 end loop;
4188 -- Make a call to the unprotected version of the subprogram built above
4189 -- for use by the protected version built below.
4191 if Nkind (Op_Spec) = N_Function_Specification then
4192 if Exc_Safe then
4193 R := Make_Temporary (Loc, 'R');
4194 Unprot_Call :=
4195 Make_Object_Declaration (Loc,
4196 Defining_Identifier => R,
4197 Constant_Present => True,
4198 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4199 Expression =>
4200 Make_Function_Call (Loc,
4201 Name => Make_Identifier (Loc,
4202 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4203 Parameter_Associations => Uactuals));
4205 Return_Stmt :=
4206 Make_Simple_Return_Statement (Loc,
4207 Expression => New_Reference_To (R, Loc));
4209 else
4210 Unprot_Call := Make_Simple_Return_Statement (Loc,
4211 Expression => Make_Function_Call (Loc,
4212 Name =>
4213 Make_Identifier (Loc,
4214 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4215 Parameter_Associations => Uactuals));
4216 end if;
4218 Lock_Kind := RE_Lock_Read_Only;
4220 else
4221 Unprot_Call :=
4222 Make_Procedure_Call_Statement (Loc,
4223 Name =>
4224 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4225 Parameter_Associations => Uactuals);
4227 Lock_Kind := RE_Lock;
4228 end if;
4230 -- Wrap call in block that will be covered by an at_end handler
4232 if not Exc_Safe then
4233 Unprot_Call := Make_Block_Statement (Loc,
4234 Handled_Statement_Sequence =>
4235 Make_Handled_Sequence_Of_Statements (Loc,
4236 Statements => New_List (Unprot_Call)));
4237 end if;
4239 -- Make the protected subprogram body. This locks the protected
4240 -- object and calls the unprotected version of the subprogram.
4242 case Corresponding_Runtime_Package (Pid) is
4243 when System_Tasking_Protected_Objects_Entries =>
4244 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
4246 when System_Tasking_Protected_Objects_Single_Entry =>
4247 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
4249 when System_Tasking_Protected_Objects =>
4250 Lock_Name := New_Reference_To (RTE (Lock_Kind), Loc);
4252 when others =>
4253 raise Program_Error;
4254 end case;
4256 Object_Parm :=
4257 Make_Attribute_Reference (Loc,
4258 Prefix =>
4259 Make_Selected_Component (Loc,
4260 Prefix => Make_Identifier (Loc, Name_uObject),
4261 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4262 Attribute_Name => Name_Unchecked_Access);
4264 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4265 Name => Lock_Name,
4266 Parameter_Associations => New_List (Object_Parm));
4268 if Abort_Allowed then
4269 Stmts := New_List (
4270 Make_Procedure_Call_Statement (Loc,
4271 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4272 Parameter_Associations => Empty_List),
4273 Lock_Stmt);
4275 else
4276 Stmts := New_List (Lock_Stmt);
4277 end if;
4279 if not Exc_Safe then
4280 Append (Unprot_Call, Stmts);
4281 else
4282 if Nkind (Op_Spec) = N_Function_Specification then
4283 Pre_Stmts := Stmts;
4284 Stmts := Empty_List;
4285 else
4286 Append (Unprot_Call, Stmts);
4287 end if;
4289 -- Historical note: Previously, call the the cleanup was inserted
4290 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4291 -- which is also shared by the 'not Exc_Safe' path.
4293 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4295 if Nkind (Op_Spec) = N_Function_Specification then
4296 Append (Return_Stmt, Stmts);
4297 Append (Make_Block_Statement (Loc,
4298 Declarations => New_List (Unprot_Call),
4299 Handled_Statement_Sequence =>
4300 Make_Handled_Sequence_Of_Statements (Loc,
4301 Statements => Stmts)), Pre_Stmts);
4302 Stmts := Pre_Stmts;
4303 end if;
4304 end if;
4306 Sub_Body :=
4307 Make_Subprogram_Body (Loc,
4308 Declarations => Empty_List,
4309 Specification => P_Op_Spec,
4310 Handled_Statement_Sequence =>
4311 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4313 -- Mark this subprogram as a protected subprogram body so that the
4314 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4315 -- path as otherwise the cleanup has already been inserted.
4317 if not Exc_Safe then
4318 Set_Is_Protected_Subprogram_Body (Sub_Body);
4319 end if;
4321 return Sub_Body;
4322 end Build_Protected_Subprogram_Body;
4324 -------------------------------------
4325 -- Build_Protected_Subprogram_Call --
4326 -------------------------------------
4328 procedure Build_Protected_Subprogram_Call
4329 (N : Node_Id;
4330 Name : Node_Id;
4331 Rec : Node_Id;
4332 External : Boolean := True)
4334 Loc : constant Source_Ptr := Sloc (N);
4335 Sub : constant Entity_Id := Entity (Name);
4336 New_Sub : Node_Id;
4337 Params : List_Id;
4339 begin
4340 if External then
4341 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4342 else
4343 New_Sub :=
4344 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4345 end if;
4347 if Present (Parameter_Associations (N)) then
4348 Params := New_Copy_List_Tree (Parameter_Associations (N));
4349 else
4350 Params := New_List;
4351 end if;
4353 -- If the type is an untagged derived type, convert to the root type,
4354 -- which is the one on which the operations are defined.
4356 if Nkind (Rec) = N_Unchecked_Type_Conversion
4357 and then not Is_Tagged_Type (Etype (Rec))
4358 and then Is_Derived_Type (Etype (Rec))
4359 then
4360 Set_Etype (Rec, Root_Type (Etype (Rec)));
4361 Set_Subtype_Mark (Rec,
4362 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4363 end if;
4365 Prepend (Rec, Params);
4367 if Ekind (Sub) = E_Procedure then
4368 Rewrite (N,
4369 Make_Procedure_Call_Statement (Loc,
4370 Name => New_Sub,
4371 Parameter_Associations => Params));
4373 else
4374 pragma Assert (Ekind (Sub) = E_Function);
4375 Rewrite (N,
4376 Make_Function_Call (Loc,
4377 Name => New_Sub,
4378 Parameter_Associations => Params));
4379 end if;
4381 if External
4382 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4383 and then Is_Entity_Name (Expression (Rec))
4384 and then Is_Shared_Passive (Entity (Expression (Rec)))
4385 then
4386 Add_Shared_Var_Lock_Procs (N);
4387 end if;
4388 end Build_Protected_Subprogram_Call;
4390 ---------------------------------------------
4391 -- Build_Protected_Subprogram_Call_Cleanup --
4392 ---------------------------------------------
4394 procedure Build_Protected_Subprogram_Call_Cleanup
4395 (Op_Spec : Node_Id;
4396 Conc_Typ : Node_Id;
4397 Loc : Source_Ptr;
4398 Stmts : List_Id)
4400 Nam : Node_Id;
4402 begin
4403 -- If the associated protected object has entries, a protected
4404 -- procedure has to service entry queues. In this case generate:
4406 -- Service_Entries (_object._object'Access);
4408 if Nkind (Op_Spec) = N_Procedure_Specification
4409 and then Has_Entries (Conc_Typ)
4410 then
4411 case Corresponding_Runtime_Package (Conc_Typ) is
4412 when System_Tasking_Protected_Objects_Entries =>
4413 Nam := New_Reference_To (RTE (RE_Service_Entries), Loc);
4415 when System_Tasking_Protected_Objects_Single_Entry =>
4416 Nam := New_Reference_To (RTE (RE_Service_Entry), Loc);
4418 when others =>
4419 raise Program_Error;
4420 end case;
4422 Append_To (Stmts,
4423 Make_Procedure_Call_Statement (Loc,
4424 Name => Nam,
4425 Parameter_Associations => New_List (
4426 Make_Attribute_Reference (Loc,
4427 Prefix =>
4428 Make_Selected_Component (Loc,
4429 Prefix => Make_Identifier (Loc, Name_uObject),
4430 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4431 Attribute_Name => Name_Unchecked_Access))));
4433 else
4434 -- Generate:
4435 -- Unlock (_object._object'Access);
4437 case Corresponding_Runtime_Package (Conc_Typ) is
4438 when System_Tasking_Protected_Objects_Entries =>
4439 Nam := New_Reference_To (RTE (RE_Unlock_Entries), Loc);
4441 when System_Tasking_Protected_Objects_Single_Entry =>
4442 Nam := New_Reference_To (RTE (RE_Unlock_Entry), Loc);
4444 when System_Tasking_Protected_Objects =>
4445 Nam := New_Reference_To (RTE (RE_Unlock), Loc);
4447 when others =>
4448 raise Program_Error;
4449 end case;
4451 Append_To (Stmts,
4452 Make_Procedure_Call_Statement (Loc,
4453 Name => Nam,
4454 Parameter_Associations => New_List (
4455 Make_Attribute_Reference (Loc,
4456 Prefix =>
4457 Make_Selected_Component (Loc,
4458 Prefix => Make_Identifier (Loc, Name_uObject),
4459 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4460 Attribute_Name => Name_Unchecked_Access))));
4461 end if;
4463 -- Generate:
4464 -- Abort_Undefer;
4466 if Abort_Allowed then
4467 Append_To (Stmts,
4468 Make_Procedure_Call_Statement (Loc,
4469 Name =>
4470 New_Reference_To (RTE (RE_Abort_Undefer), Loc),
4471 Parameter_Associations => Empty_List));
4472 end if;
4473 end Build_Protected_Subprogram_Call_Cleanup;
4475 -------------------------
4476 -- Build_Selected_Name --
4477 -------------------------
4479 function Build_Selected_Name
4480 (Prefix : Entity_Id;
4481 Selector : Entity_Id;
4482 Append_Char : Character := ' ') return Name_Id
4484 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4485 Select_Len : Natural;
4487 begin
4488 Get_Name_String (Chars (Selector));
4489 Select_Len := Name_Len;
4490 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4491 Get_Name_String (Chars (Prefix));
4493 -- If scope is anonymous type, discard suffix to recover name of
4494 -- single protected object. Otherwise use protected type name.
4496 if Name_Buffer (Name_Len) = 'T' then
4497 Name_Len := Name_Len - 1;
4498 end if;
4500 Add_Str_To_Name_Buffer ("__");
4501 for J in 1 .. Select_Len loop
4502 Add_Char_To_Name_Buffer (Select_Buffer (J));
4503 end loop;
4505 -- Now add the Append_Char if specified. The encoding to follow
4506 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4507 -- then the entity is associated to a protected type subprogram.
4508 -- Otherwise, it is a protected type entry. For each case, the
4509 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4511 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4513 if Append_Char /= ' ' then
4514 if Append_Char = 'P' or Append_Char = 'N' then
4515 Add_Char_To_Name_Buffer (Append_Char);
4516 return Name_Find;
4517 else
4518 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4519 return New_External_Name (Name_Find, ' ', -1);
4520 end if;
4521 else
4522 return Name_Find;
4523 end if;
4524 end Build_Selected_Name;
4526 -----------------------------
4527 -- Build_Simple_Entry_Call --
4528 -----------------------------
4530 -- A task entry call is converted to a call to Call_Simple
4532 -- declare
4533 -- P : parms := (parm, parm, parm);
4534 -- begin
4535 -- Call_Simple (acceptor-task, entry-index, P'Address);
4536 -- parm := P.param;
4537 -- parm := P.param;
4538 -- ...
4539 -- end;
4541 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4542 -- the parameters, and the constructed aggregate value contains either the
4543 -- parameters or, in the case of non-elementary types, references to these
4544 -- parameters. Then the address of this aggregate is passed to the runtime
4545 -- routine, along with the task id value and the task entry index value.
4546 -- Pnn is only required if parameters are present.
4548 -- The assignments after the call are present only in the case of in-out
4549 -- or out parameters for elementary types, and are used to assign back the
4550 -- resulting values of such parameters.
4552 -- Note: the reason that we insert a block here is that in the context
4553 -- of selects, conditional entry calls etc. the entry call statement
4554 -- appears on its own, not as an element of a list.
4556 -- A protected entry call is converted to a Protected_Entry_Call:
4558 -- declare
4559 -- P : E1_Params := (param, param, param);
4560 -- Pnn : Boolean;
4561 -- Bnn : Communications_Block;
4563 -- declare
4564 -- P : E1_Params := (param, param, param);
4565 -- Bnn : Communications_Block;
4567 -- begin
4568 -- Protected_Entry_Call (
4569 -- Object => po._object'Access,
4570 -- E => <entry index>;
4571 -- Uninterpreted_Data => P'Address;
4572 -- Mode => Simple_Call;
4573 -- Block => Bnn);
4574 -- parm := P.param;
4575 -- parm := P.param;
4576 -- ...
4577 -- end;
4579 procedure Build_Simple_Entry_Call
4580 (N : Node_Id;
4581 Concval : Node_Id;
4582 Ename : Node_Id;
4583 Index : Node_Id)
4585 begin
4586 Expand_Call (N);
4588 -- If call has been inlined, nothing left to do
4590 if Nkind (N) = N_Block_Statement then
4591 return;
4592 end if;
4594 -- Convert entry call to Call_Simple call
4596 declare
4597 Loc : constant Source_Ptr := Sloc (N);
4598 Parms : constant List_Id := Parameter_Associations (N);
4599 Stats : constant List_Id := New_List;
4600 Actual : Node_Id;
4601 Call : Node_Id;
4602 Comm_Name : Entity_Id;
4603 Conctyp : Node_Id;
4604 Decls : List_Id;
4605 Ent : Entity_Id;
4606 Ent_Acc : Entity_Id;
4607 Formal : Node_Id;
4608 Iface_Tag : Entity_Id;
4609 Iface_Typ : Entity_Id;
4610 N_Node : Node_Id;
4611 N_Var : Node_Id;
4612 P : Entity_Id;
4613 Parm1 : Node_Id;
4614 Parm2 : Node_Id;
4615 Parm3 : Node_Id;
4616 Pdecl : Node_Id;
4617 Plist : List_Id;
4618 X : Entity_Id;
4619 Xdecl : Node_Id;
4621 begin
4622 -- Simple entry and entry family cases merge here
4624 Ent := Entity (Ename);
4625 Ent_Acc := Entry_Parameters_Type (Ent);
4626 Conctyp := Etype (Concval);
4628 -- If prefix is an access type, dereference to obtain the task type
4630 if Is_Access_Type (Conctyp) then
4631 Conctyp := Designated_Type (Conctyp);
4632 end if;
4634 -- Special case for protected subprogram calls
4636 if Is_Protected_Type (Conctyp)
4637 and then Is_Subprogram (Entity (Ename))
4638 then
4639 if not Is_Eliminated (Entity (Ename)) then
4640 Build_Protected_Subprogram_Call
4641 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4642 Analyze (N);
4643 end if;
4645 return;
4646 end if;
4648 -- First parameter is the Task_Id value from the task value or the
4649 -- Object from the protected object value, obtained by selecting
4650 -- the _Task_Id or _Object from the result of doing an unchecked
4651 -- conversion to convert the value to the corresponding record type.
4653 if Nkind (Concval) = N_Function_Call
4654 and then Is_Task_Type (Conctyp)
4655 and then Ada_Version >= Ada_2005
4656 then
4657 declare
4658 ExpR : constant Node_Id := Relocate_Node (Concval);
4659 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4660 Decl : Node_Id;
4662 begin
4663 Decl :=
4664 Make_Object_Declaration (Loc,
4665 Defining_Identifier => Obj,
4666 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4667 Expression => ExpR);
4668 Set_Etype (Obj, Conctyp);
4669 Decls := New_List (Decl);
4670 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4671 end;
4673 else
4674 Decls := New_List;
4675 end if;
4677 Parm1 := Concurrent_Ref (Concval);
4679 -- Second parameter is the entry index, computed by the routine
4680 -- provided for this purpose. The value of this expression is
4681 -- assigned to an intermediate variable to assure that any entry
4682 -- family index expressions are evaluated before the entry
4683 -- parameters.
4685 if Abort_Allowed
4686 or else Restriction_Active (No_Entry_Queue) = False
4687 or else not Is_Protected_Type (Conctyp)
4688 or else Number_Entries (Conctyp) > 1
4689 or else (Has_Attach_Handler (Conctyp)
4690 and then not Restricted_Profile)
4691 then
4692 X := Make_Defining_Identifier (Loc, Name_uX);
4694 Xdecl :=
4695 Make_Object_Declaration (Loc,
4696 Defining_Identifier => X,
4697 Object_Definition =>
4698 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
4699 Expression => Actual_Index_Expression (
4700 Loc, Entity (Ename), Index, Concval));
4702 Append_To (Decls, Xdecl);
4703 Parm2 := New_Reference_To (X, Loc);
4705 else
4706 Xdecl := Empty;
4707 Parm2 := Empty;
4708 end if;
4710 -- The third parameter is the packaged parameters. If there are
4711 -- none, then it is just the null address, since nothing is passed.
4713 if No (Parms) then
4714 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
4715 P := Empty;
4717 -- Case of parameters present, where third argument is the address
4718 -- of a packaged record containing the required parameter values.
4720 else
4721 -- First build a list of parameter values, which are references to
4722 -- objects of the parameter types.
4724 Plist := New_List;
4726 Actual := First_Actual (N);
4727 Formal := First_Formal (Ent);
4729 while Present (Actual) loop
4731 -- If it is a by_copy_type, copy it to a new variable. The
4732 -- packaged record has a field that points to this variable.
4734 if Is_By_Copy_Type (Etype (Actual)) then
4735 N_Node :=
4736 Make_Object_Declaration (Loc,
4737 Defining_Identifier => Make_Temporary (Loc, 'J'),
4738 Aliased_Present => True,
4739 Object_Definition =>
4740 New_Reference_To (Etype (Formal), Loc));
4742 -- Mark the object as not needing initialization since the
4743 -- initialization is performed separately, avoiding errors
4744 -- on cases such as formals of null-excluding access types.
4746 Set_No_Initialization (N_Node);
4748 -- We must make an assignment statement separate for the
4749 -- case of limited type. We cannot assign it unless the
4750 -- Assignment_OK flag is set first. An out formal of an
4751 -- access type must also be initialized from the actual,
4752 -- as stated in RM 6.4.1 (13).
4754 if Ekind (Formal) /= E_Out_Parameter
4755 or else Is_Access_Type (Etype (Formal))
4756 then
4757 N_Var :=
4758 New_Reference_To (Defining_Identifier (N_Node), Loc);
4759 Set_Assignment_OK (N_Var);
4760 Append_To (Stats,
4761 Make_Assignment_Statement (Loc,
4762 Name => N_Var,
4763 Expression => Relocate_Node (Actual)));
4764 end if;
4766 Append (N_Node, Decls);
4768 Append_To (Plist,
4769 Make_Attribute_Reference (Loc,
4770 Attribute_Name => Name_Unchecked_Access,
4771 Prefix =>
4772 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4774 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4776 elsif Is_VM_By_Copy_Actual (Actual) then
4777 N_Node :=
4778 Make_Object_Declaration (Loc,
4779 Defining_Identifier => Make_Temporary (Loc, 'J'),
4780 Aliased_Present => True,
4781 Object_Definition =>
4782 New_Reference_To (Etype (Formal), Loc),
4783 Expression => New_Copy_Tree (Actual));
4784 Set_Assignment_OK (N_Node);
4786 Append (N_Node, Decls);
4788 Append_To (Plist,
4789 Make_Attribute_Reference (Loc,
4790 Attribute_Name => Name_Unchecked_Access,
4791 Prefix =>
4792 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4794 else
4795 -- Interface class-wide formal
4797 if Ada_Version >= Ada_2005
4798 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4799 and then Is_Interface (Etype (Formal))
4800 then
4801 Iface_Typ := Etype (Etype (Formal));
4803 -- Generate:
4804 -- formal_iface_type! (actual.iface_tag)'reference
4806 Iface_Tag :=
4807 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4808 pragma Assert (Present (Iface_Tag));
4810 Append_To (Plist,
4811 Make_Reference (Loc,
4812 Unchecked_Convert_To (Iface_Typ,
4813 Make_Selected_Component (Loc,
4814 Prefix =>
4815 Relocate_Node (Actual),
4816 Selector_Name =>
4817 New_Reference_To (Iface_Tag, Loc)))));
4818 else
4819 -- Generate:
4820 -- actual'reference
4822 Append_To (Plist,
4823 Make_Reference (Loc, Relocate_Node (Actual)));
4824 end if;
4825 end if;
4827 Next_Actual (Actual);
4828 Next_Formal_With_Extras (Formal);
4829 end loop;
4831 -- Now build the declaration of parameters initialized with the
4832 -- aggregate containing this constructed parameter list.
4834 P := Make_Defining_Identifier (Loc, Name_uP);
4836 Pdecl :=
4837 Make_Object_Declaration (Loc,
4838 Defining_Identifier => P,
4839 Object_Definition =>
4840 New_Reference_To (Designated_Type (Ent_Acc), Loc),
4841 Expression =>
4842 Make_Aggregate (Loc, Expressions => Plist));
4844 Parm3 :=
4845 Make_Attribute_Reference (Loc,
4846 Prefix => New_Reference_To (P, Loc),
4847 Attribute_Name => Name_Address);
4849 Append (Pdecl, Decls);
4850 end if;
4852 -- Now we can create the call, case of protected type
4854 if Is_Protected_Type (Conctyp) then
4855 case Corresponding_Runtime_Package (Conctyp) is
4856 when System_Tasking_Protected_Objects_Entries =>
4858 -- Change the type of the index declaration
4860 Set_Object_Definition (Xdecl,
4861 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
4863 -- Some additional declarations for protected entry calls
4865 if No (Decls) then
4866 Decls := New_List;
4867 end if;
4869 -- Bnn : Communications_Block;
4871 Comm_Name := Make_Temporary (Loc, 'B');
4873 Append_To (Decls,
4874 Make_Object_Declaration (Loc,
4875 Defining_Identifier => Comm_Name,
4876 Object_Definition =>
4877 New_Reference_To (RTE (RE_Communication_Block), Loc)));
4879 -- Some additional statements for protected entry calls
4881 -- Protected_Entry_Call (
4882 -- Object => po._object'Access,
4883 -- E => <entry index>;
4884 -- Uninterpreted_Data => P'Address;
4885 -- Mode => Simple_Call;
4886 -- Block => Bnn);
4888 Call :=
4889 Make_Procedure_Call_Statement (Loc,
4890 Name =>
4891 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
4893 Parameter_Associations => New_List (
4894 Make_Attribute_Reference (Loc,
4895 Attribute_Name => Name_Unchecked_Access,
4896 Prefix => Parm1),
4897 Parm2,
4898 Parm3,
4899 New_Reference_To (RTE (RE_Simple_Call), Loc),
4900 New_Occurrence_Of (Comm_Name, Loc)));
4902 when System_Tasking_Protected_Objects_Single_Entry =>
4903 -- Protected_Single_Entry_Call (
4904 -- Object => po._object'Access,
4905 -- Uninterpreted_Data => P'Address;
4906 -- Mode => Simple_Call);
4908 Call :=
4909 Make_Procedure_Call_Statement (Loc,
4910 Name => New_Reference_To (
4911 RTE (RE_Protected_Single_Entry_Call), Loc),
4913 Parameter_Associations => New_List (
4914 Make_Attribute_Reference (Loc,
4915 Attribute_Name => Name_Unchecked_Access,
4916 Prefix => Parm1),
4917 Parm3,
4918 New_Reference_To (RTE (RE_Simple_Call), Loc)));
4920 when others =>
4921 raise Program_Error;
4922 end case;
4924 -- Case of task type
4926 else
4927 Call :=
4928 Make_Procedure_Call_Statement (Loc,
4929 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
4930 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4932 end if;
4934 Append_To (Stats, Call);
4936 -- If there are out or in/out parameters by copy add assignment
4937 -- statements for the result values.
4939 if Present (Parms) then
4940 Actual := First_Actual (N);
4941 Formal := First_Formal (Ent);
4943 Set_Assignment_OK (Actual);
4944 while Present (Actual) loop
4945 if (Is_By_Copy_Type (Etype (Actual))
4946 or else Is_VM_By_Copy_Actual (Actual))
4947 and then Ekind (Formal) /= E_In_Parameter
4948 then
4949 N_Node :=
4950 Make_Assignment_Statement (Loc,
4951 Name => New_Copy (Actual),
4952 Expression =>
4953 Make_Explicit_Dereference (Loc,
4954 Make_Selected_Component (Loc,
4955 Prefix => New_Reference_To (P, Loc),
4956 Selector_Name =>
4957 Make_Identifier (Loc, Chars (Formal)))));
4959 -- In all cases (including limited private types) we want
4960 -- the assignment to be valid.
4962 Set_Assignment_OK (Name (N_Node));
4964 -- If the call is the triggering alternative in an
4965 -- asynchronous select, or the entry_call alternative of a
4966 -- conditional entry call, the assignments for in-out
4967 -- parameters are incorporated into the statement list that
4968 -- follows, so that there are executed only if the entry
4969 -- call succeeds.
4971 if (Nkind (Parent (N)) = N_Triggering_Alternative
4972 and then N = Triggering_Statement (Parent (N)))
4973 or else
4974 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4975 and then N = Entry_Call_Statement (Parent (N)))
4976 then
4977 if No (Statements (Parent (N))) then
4978 Set_Statements (Parent (N), New_List);
4979 end if;
4981 Prepend (N_Node, Statements (Parent (N)));
4983 else
4984 Insert_After (Call, N_Node);
4985 end if;
4986 end if;
4988 Next_Actual (Actual);
4989 Next_Formal_With_Extras (Formal);
4990 end loop;
4991 end if;
4993 -- Finally, create block and analyze it
4995 Rewrite (N,
4996 Make_Block_Statement (Loc,
4997 Declarations => Decls,
4998 Handled_Statement_Sequence =>
4999 Make_Handled_Sequence_Of_Statements (Loc,
5000 Statements => Stats)));
5002 Analyze (N);
5003 end;
5004 end Build_Simple_Entry_Call;
5006 --------------------------------
5007 -- Build_Task_Activation_Call --
5008 --------------------------------
5010 procedure Build_Task_Activation_Call (N : Node_Id) is
5011 Loc : constant Source_Ptr := Sloc (N);
5012 Chain : Entity_Id;
5013 Call : Node_Id;
5014 Name : Node_Id;
5015 P : Node_Id;
5017 begin
5018 -- For sequential elaboration policy, all the tasks will be activated at
5019 -- the end of the elaboration.
5021 if Partition_Elaboration_Policy = 'S' then
5022 return;
5023 end if;
5025 -- Get the activation chain entity. Except in the case of a package
5026 -- body, this is in the node that was passed. For a package body, we
5027 -- have to find the corresponding package declaration node.
5029 if Nkind (N) = N_Package_Body then
5030 P := Corresponding_Spec (N);
5031 loop
5032 P := Parent (P);
5033 exit when Nkind (P) = N_Package_Declaration;
5034 end loop;
5036 Chain := Activation_Chain_Entity (P);
5038 else
5039 Chain := Activation_Chain_Entity (N);
5040 end if;
5042 if Present (Chain) then
5043 if Restricted_Profile then
5044 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
5045 else
5046 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
5047 end if;
5049 Call :=
5050 Make_Procedure_Call_Statement (Loc,
5051 Name => Name,
5052 Parameter_Associations =>
5053 New_List (Make_Attribute_Reference (Loc,
5054 Prefix => New_Occurrence_Of (Chain, Loc),
5055 Attribute_Name => Name_Unchecked_Access)));
5057 if Nkind (N) = N_Package_Declaration then
5058 if Present (Corresponding_Body (N)) then
5059 null;
5061 elsif Present (Private_Declarations (Specification (N))) then
5062 Append (Call, Private_Declarations (Specification (N)));
5064 else
5065 Append (Call, Visible_Declarations (Specification (N)));
5066 end if;
5068 else
5069 if Present (Handled_Statement_Sequence (N)) then
5071 -- The call goes at the start of the statement sequence
5072 -- after the start of exception range label if one is present.
5074 declare
5075 Stm : Node_Id;
5077 begin
5078 Stm := First (Statements (Handled_Statement_Sequence (N)));
5080 -- A special case, skip exception range label if one is
5081 -- present (from front end zcx processing).
5083 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
5084 Next (Stm);
5085 end if;
5087 -- Another special case, if the first statement is a block
5088 -- from optimization of a local raise to a goto, then the
5089 -- call goes inside this block.
5091 if Nkind (Stm) = N_Block_Statement
5092 and then Exception_Junk (Stm)
5093 then
5094 Stm :=
5095 First (Statements (Handled_Statement_Sequence (Stm)));
5096 end if;
5098 -- Insertion point is after any exception label pushes,
5099 -- since we want it covered by any local handlers.
5101 while Nkind (Stm) in N_Push_xxx_Label loop
5102 Next (Stm);
5103 end loop;
5105 -- Now we have the proper insertion point
5107 Insert_Before (Stm, Call);
5108 end;
5110 else
5111 Set_Handled_Statement_Sequence (N,
5112 Make_Handled_Sequence_Of_Statements (Loc,
5113 Statements => New_List (Call)));
5114 end if;
5115 end if;
5117 Analyze (Call);
5118 Check_Task_Activation (N);
5119 end if;
5120 end Build_Task_Activation_Call;
5122 -------------------------------
5123 -- Build_Task_Allocate_Block --
5124 -------------------------------
5126 procedure Build_Task_Allocate_Block
5127 (Actions : List_Id;
5128 N : Node_Id;
5129 Args : List_Id)
5131 T : constant Entity_Id := Entity (Expression (N));
5132 Init : constant Entity_Id := Base_Init_Proc (T);
5133 Loc : constant Source_Ptr := Sloc (N);
5134 Chain : constant Entity_Id :=
5135 Make_Defining_Identifier (Loc, Name_uChain);
5136 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5137 Block : Node_Id;
5139 begin
5140 Block :=
5141 Make_Block_Statement (Loc,
5142 Identifier => New_Reference_To (Blkent, Loc),
5143 Declarations => New_List (
5145 -- _Chain : Activation_Chain;
5147 Make_Object_Declaration (Loc,
5148 Defining_Identifier => Chain,
5149 Aliased_Present => True,
5150 Object_Definition =>
5151 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5153 Handled_Statement_Sequence =>
5154 Make_Handled_Sequence_Of_Statements (Loc,
5156 Statements => New_List (
5158 -- Init (Args);
5160 Make_Procedure_Call_Statement (Loc,
5161 Name => New_Reference_To (Init, Loc),
5162 Parameter_Associations => Args),
5164 -- Activate_Tasks (_Chain);
5166 Make_Procedure_Call_Statement (Loc,
5167 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5168 Parameter_Associations => New_List (
5169 Make_Attribute_Reference (Loc,
5170 Prefix => New_Reference_To (Chain, Loc),
5171 Attribute_Name => Name_Unchecked_Access))))),
5173 Has_Created_Identifier => True,
5174 Is_Task_Allocation_Block => True);
5176 Append_To (Actions,
5177 Make_Implicit_Label_Declaration (Loc,
5178 Defining_Identifier => Blkent,
5179 Label_Construct => Block));
5181 Append_To (Actions, Block);
5183 Set_Activation_Chain_Entity (Block, Chain);
5184 end Build_Task_Allocate_Block;
5186 -----------------------------------------------
5187 -- Build_Task_Allocate_Block_With_Init_Stmts --
5188 -----------------------------------------------
5190 procedure Build_Task_Allocate_Block_With_Init_Stmts
5191 (Actions : List_Id;
5192 N : Node_Id;
5193 Init_Stmts : List_Id)
5195 Loc : constant Source_Ptr := Sloc (N);
5196 Chain : constant Entity_Id :=
5197 Make_Defining_Identifier (Loc, Name_uChain);
5198 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5199 Block : Node_Id;
5201 begin
5202 Append_To (Init_Stmts,
5203 Make_Procedure_Call_Statement (Loc,
5204 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5205 Parameter_Associations => New_List (
5206 Make_Attribute_Reference (Loc,
5207 Prefix => New_Reference_To (Chain, Loc),
5208 Attribute_Name => Name_Unchecked_Access))));
5210 Block :=
5211 Make_Block_Statement (Loc,
5212 Identifier => New_Reference_To (Blkent, Loc),
5213 Declarations => New_List (
5215 -- _Chain : Activation_Chain;
5217 Make_Object_Declaration (Loc,
5218 Defining_Identifier => Chain,
5219 Aliased_Present => True,
5220 Object_Definition =>
5221 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5223 Handled_Statement_Sequence =>
5224 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5226 Has_Created_Identifier => True,
5227 Is_Task_Allocation_Block => True);
5229 Append_To (Actions,
5230 Make_Implicit_Label_Declaration (Loc,
5231 Defining_Identifier => Blkent,
5232 Label_Construct => Block));
5234 Append_To (Actions, Block);
5236 Set_Activation_Chain_Entity (Block, Chain);
5237 end Build_Task_Allocate_Block_With_Init_Stmts;
5239 -----------------------------------
5240 -- Build_Task_Proc_Specification --
5241 -----------------------------------
5243 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5244 Loc : constant Source_Ptr := Sloc (T);
5245 Spec_Id : Entity_Id;
5247 begin
5248 -- Case of explicit task type, suffix TB
5250 if Comes_From_Source (T) then
5251 Spec_Id :=
5252 Make_Defining_Identifier (Loc,
5253 Chars => New_External_Name (Chars (T), "TB"));
5255 -- Case of anonymous task type, suffix B
5257 else
5258 Spec_Id :=
5259 Make_Defining_Identifier (Loc,
5260 Chars => New_External_Name (Chars (T), 'B'));
5261 end if;
5263 Set_Is_Internal (Spec_Id);
5265 -- Associate the procedure with the task, if this is the declaration
5266 -- (and not the body) of the procedure.
5268 if No (Task_Body_Procedure (T)) then
5269 Set_Task_Body_Procedure (T, Spec_Id);
5270 end if;
5272 return
5273 Make_Procedure_Specification (Loc,
5274 Defining_Unit_Name => Spec_Id,
5275 Parameter_Specifications => New_List (
5276 Make_Parameter_Specification (Loc,
5277 Defining_Identifier =>
5278 Make_Defining_Identifier (Loc, Name_uTask),
5279 Parameter_Type =>
5280 Make_Access_Definition (Loc,
5281 Subtype_Mark =>
5282 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
5283 end Build_Task_Proc_Specification;
5285 ---------------------------------------
5286 -- Build_Unprotected_Subprogram_Body --
5287 ---------------------------------------
5289 function Build_Unprotected_Subprogram_Body
5290 (N : Node_Id;
5291 Pid : Node_Id) return Node_Id
5293 Decls : constant List_Id := Declarations (N);
5295 begin
5296 -- Add renamings for the Protection object, discriminals, privals and
5297 -- the entry index constant for use by debugger.
5299 Debug_Private_Data_Declarations (Decls);
5301 -- Make an unprotected version of the subprogram for use within the same
5302 -- object, with a new name and an additional parameter representing the
5303 -- object.
5305 return
5306 Make_Subprogram_Body (Sloc (N),
5307 Specification =>
5308 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5309 Declarations => Decls,
5310 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5311 end Build_Unprotected_Subprogram_Body;
5313 ----------------------------
5314 -- Collect_Entry_Families --
5315 ----------------------------
5317 procedure Collect_Entry_Families
5318 (Loc : Source_Ptr;
5319 Cdecls : List_Id;
5320 Current_Node : in out Node_Id;
5321 Conctyp : Entity_Id)
5323 Efam : Entity_Id;
5324 Efam_Decl : Node_Id;
5325 Efam_Type : Entity_Id;
5327 begin
5328 Efam := First_Entity (Conctyp);
5329 while Present (Efam) loop
5330 if Ekind (Efam) = E_Entry_Family then
5331 Efam_Type := Make_Temporary (Loc, 'F');
5333 declare
5334 Bas : Entity_Id :=
5335 Base_Type
5336 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5338 Bas_Decl : Node_Id := Empty;
5339 Lo, Hi : Node_Id;
5341 begin
5342 Get_Index_Bounds
5343 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5345 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5346 Bas := Make_Temporary (Loc, 'B');
5348 Bas_Decl :=
5349 Make_Subtype_Declaration (Loc,
5350 Defining_Identifier => Bas,
5351 Subtype_Indication =>
5352 Make_Subtype_Indication (Loc,
5353 Subtype_Mark =>
5354 New_Occurrence_Of (Standard_Integer, Loc),
5355 Constraint =>
5356 Make_Range_Constraint (Loc,
5357 Range_Expression => Make_Range (Loc,
5358 Make_Integer_Literal
5359 (Loc, -Entry_Family_Bound),
5360 Make_Integer_Literal
5361 (Loc, Entry_Family_Bound - 1)))));
5363 Insert_After (Current_Node, Bas_Decl);
5364 Current_Node := Bas_Decl;
5365 Analyze (Bas_Decl);
5366 end if;
5368 Efam_Decl :=
5369 Make_Full_Type_Declaration (Loc,
5370 Defining_Identifier => Efam_Type,
5371 Type_Definition =>
5372 Make_Unconstrained_Array_Definition (Loc,
5373 Subtype_Marks =>
5374 (New_List (New_Occurrence_Of (Bas, Loc))),
5376 Component_Definition =>
5377 Make_Component_Definition (Loc,
5378 Aliased_Present => False,
5379 Subtype_Indication =>
5380 New_Reference_To (Standard_Character, Loc))));
5381 end;
5383 Insert_After (Current_Node, Efam_Decl);
5384 Current_Node := Efam_Decl;
5385 Analyze (Efam_Decl);
5387 Append_To (Cdecls,
5388 Make_Component_Declaration (Loc,
5389 Defining_Identifier =>
5390 Make_Defining_Identifier (Loc, Chars (Efam)),
5392 Component_Definition =>
5393 Make_Component_Definition (Loc,
5394 Aliased_Present => False,
5395 Subtype_Indication =>
5396 Make_Subtype_Indication (Loc,
5397 Subtype_Mark =>
5398 New_Occurrence_Of (Efam_Type, Loc),
5400 Constraint =>
5401 Make_Index_Or_Discriminant_Constraint (Loc,
5402 Constraints => New_List (
5403 New_Occurrence_Of
5404 (Etype (Discrete_Subtype_Definition
5405 (Parent (Efam))), Loc)))))));
5407 end if;
5409 Next_Entity (Efam);
5410 end loop;
5411 end Collect_Entry_Families;
5413 -----------------------
5414 -- Concurrent_Object --
5415 -----------------------
5417 function Concurrent_Object
5418 (Spec_Id : Entity_Id;
5419 Conc_Typ : Entity_Id) return Entity_Id
5421 begin
5422 -- Parameter _O or _object
5424 if Is_Protected_Type (Conc_Typ) then
5425 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5427 -- Parameter _task
5429 else
5430 pragma Assert (Is_Task_Type (Conc_Typ));
5431 return First_Formal (Task_Body_Procedure (Conc_Typ));
5432 end if;
5433 end Concurrent_Object;
5435 ----------------------
5436 -- Copy_Result_Type --
5437 ----------------------
5439 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5440 New_Res : constant Node_Id := New_Copy_Tree (Res);
5441 Par_Spec : Node_Id;
5442 Formal : Entity_Id;
5444 begin
5445 -- If the result type is an access_to_subprogram, we must create new
5446 -- entities for its spec.
5448 if Nkind (New_Res) = N_Access_Definition
5449 and then Present (Access_To_Subprogram_Definition (New_Res))
5450 then
5451 -- Provide new entities for the formals
5453 Par_Spec := First (Parameter_Specifications
5454 (Access_To_Subprogram_Definition (New_Res)));
5455 while Present (Par_Spec) loop
5456 Formal := Defining_Identifier (Par_Spec);
5457 Set_Defining_Identifier (Par_Spec,
5458 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5459 Next (Par_Spec);
5460 end loop;
5461 end if;
5463 return New_Res;
5464 end Copy_Result_Type;
5466 --------------------
5467 -- Concurrent_Ref --
5468 --------------------
5470 -- The expression returned for a reference to a concurrent object has the
5471 -- form:
5473 -- taskV!(name)._Task_Id
5475 -- for a task, and
5477 -- objectV!(name)._Object
5479 -- for a protected object. For the case of an access to a concurrent
5480 -- object, there is an extra explicit dereference:
5482 -- taskV!(name.all)._Task_Id
5483 -- objectV!(name.all)._Object
5485 -- here taskV and objectV are the types for the associated records, which
5486 -- contain the required _Task_Id and _Object fields for tasks and protected
5487 -- objects, respectively.
5489 -- For the case of a task type name, the expression is
5491 -- Self;
5493 -- i.e. a call to the Self function which returns precisely this Task_Id
5495 -- For the case of a protected type name, the expression is
5497 -- objectR
5499 -- which is a renaming of the _object field of the current object
5500 -- record, passed into protected operations as a parameter.
5502 function Concurrent_Ref (N : Node_Id) return Node_Id is
5503 Loc : constant Source_Ptr := Sloc (N);
5504 Ntyp : constant Entity_Id := Etype (N);
5505 Dtyp : Entity_Id;
5506 Sel : Name_Id;
5508 function Is_Current_Task (T : Entity_Id) return Boolean;
5509 -- Check whether the reference is to the immediately enclosing task
5510 -- type, or to an outer one (rare but legal).
5512 ---------------------
5513 -- Is_Current_Task --
5514 ---------------------
5516 function Is_Current_Task (T : Entity_Id) return Boolean is
5517 Scop : Entity_Id;
5519 begin
5520 Scop := Current_Scope;
5521 while Present (Scop)
5522 and then Scop /= Standard_Standard
5523 loop
5525 if Scop = T then
5526 return True;
5528 elsif Is_Task_Type (Scop) then
5529 return False;
5531 -- If this is a procedure nested within the task type, we must
5532 -- assume that it can be called from an inner task, and therefore
5533 -- cannot treat it as a local reference.
5535 elsif Is_Overloadable (Scop)
5536 and then In_Open_Scopes (T)
5537 then
5538 return False;
5540 else
5541 Scop := Scope (Scop);
5542 end if;
5543 end loop;
5545 -- We know that we are within the task body, so should have found it
5546 -- in scope.
5548 raise Program_Error;
5549 end Is_Current_Task;
5551 -- Start of processing for Concurrent_Ref
5553 begin
5554 if Is_Access_Type (Ntyp) then
5555 Dtyp := Designated_Type (Ntyp);
5557 if Is_Protected_Type (Dtyp) then
5558 Sel := Name_uObject;
5559 else
5560 Sel := Name_uTask_Id;
5561 end if;
5563 return
5564 Make_Selected_Component (Loc,
5565 Prefix =>
5566 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5567 Make_Explicit_Dereference (Loc, N)),
5568 Selector_Name => Make_Identifier (Loc, Sel));
5570 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5571 if Is_Task_Type (Entity (N)) then
5573 if Is_Current_Task (Entity (N)) then
5574 return
5575 Make_Function_Call (Loc,
5576 Name => New_Reference_To (RTE (RE_Self), Loc));
5578 else
5579 declare
5580 Decl : Node_Id;
5581 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5582 T_Body : constant Node_Id :=
5583 Parent (Corresponding_Body (Parent (Entity (N))));
5585 begin
5586 Decl :=
5587 Make_Object_Declaration (Loc,
5588 Defining_Identifier => T_Self,
5589 Object_Definition =>
5590 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5591 Expression =>
5592 Make_Function_Call (Loc,
5593 Name => New_Reference_To (RTE (RE_Self), Loc)));
5594 Prepend (Decl, Declarations (T_Body));
5595 Analyze (Decl);
5596 Set_Scope (T_Self, Entity (N));
5597 return New_Occurrence_Of (T_Self, Loc);
5598 end;
5599 end if;
5601 else
5602 pragma Assert (Is_Protected_Type (Entity (N)));
5604 return
5605 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
5606 end if;
5608 else
5609 if Is_Protected_Type (Ntyp) then
5610 Sel := Name_uObject;
5612 elsif Is_Task_Type (Ntyp) then
5613 Sel := Name_uTask_Id;
5615 else
5616 raise Program_Error;
5617 end if;
5619 return
5620 Make_Selected_Component (Loc,
5621 Prefix =>
5622 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5623 New_Copy_Tree (N)),
5624 Selector_Name => Make_Identifier (Loc, Sel));
5625 end if;
5626 end Concurrent_Ref;
5628 ------------------------
5629 -- Convert_Concurrent --
5630 ------------------------
5632 function Convert_Concurrent
5633 (N : Node_Id;
5634 Typ : Entity_Id) return Node_Id
5636 begin
5637 if not Is_Concurrent_Type (Typ) then
5638 return N;
5639 else
5640 return
5641 Unchecked_Convert_To
5642 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5643 end if;
5644 end Convert_Concurrent;
5646 -------------------------------------
5647 -- Debug_Private_Data_Declarations --
5648 -------------------------------------
5650 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5651 Debug_Nod : Node_Id;
5652 Decl : Node_Id;
5654 begin
5655 Decl := First (Decls);
5656 while Present (Decl) and then not Comes_From_Source (Decl) loop
5657 -- Declaration for concurrent entity _object and its access type,
5658 -- along with the entry index subtype:
5659 -- type prot_typVP is access prot_typV;
5660 -- _object : prot_typVP := prot_typV (_O);
5661 -- subtype Jnn is <Type of Index> range Low .. High;
5663 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5664 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5666 -- Declaration for the Protection object, discriminals, privals and
5667 -- entry index constant:
5668 -- conc_typR : protection_typ renames _object._object;
5669 -- discr_nameD : discr_typ renames _object.discr_name;
5670 -- discr_nameD : discr_typ renames _task.discr_name;
5671 -- prival_name : comp_typ renames _object.comp_name;
5672 -- J : constant Jnn :=
5673 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5675 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5676 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5677 Debug_Nod := Debug_Renaming_Declaration (Decl);
5679 if Present (Debug_Nod) then
5680 Insert_After (Decl, Debug_Nod);
5681 end if;
5682 end if;
5684 Next (Decl);
5685 end loop;
5686 end Debug_Private_Data_Declarations;
5688 ------------------------------
5689 -- Ensure_Statement_Present --
5690 ------------------------------
5692 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5693 Stmt : Node_Id;
5695 begin
5696 if Opt.Suppress_Control_Flow_Optimizations
5697 and then Is_Empty_List (Statements (Alt))
5698 then
5699 Stmt := Make_Null_Statement (Loc);
5701 -- Mark NULL statement as coming from source so that it is not
5702 -- eliminated by GIGI.
5704 -- Another covert channel. If this is a requirement, it must be
5705 -- documented in sinfo/einfo ???
5707 Set_Comes_From_Source (Stmt, True);
5709 Set_Statements (Alt, New_List (Stmt));
5710 end if;
5711 end Ensure_Statement_Present;
5713 ----------------------------
5714 -- Entry_Index_Expression --
5715 ----------------------------
5717 function Entry_Index_Expression
5718 (Sloc : Source_Ptr;
5719 Ent : Entity_Id;
5720 Index : Node_Id;
5721 Ttyp : Entity_Id) return Node_Id
5723 Expr : Node_Id;
5724 Num : Node_Id;
5725 Lo : Node_Id;
5726 Hi : Node_Id;
5727 Prev : Entity_Id;
5728 S : Node_Id;
5730 begin
5731 -- The queues of entries and entry families appear in textual order in
5732 -- the associated record. The entry index is computed as the sum of the
5733 -- number of queues for all entries that precede the designated one, to
5734 -- which is added the index expression, if this expression denotes a
5735 -- member of a family.
5737 -- The following is a place holder for the count of simple entries
5739 Num := Make_Integer_Literal (Sloc, 1);
5741 -- We construct an expression which is a series of addition operations.
5742 -- The first operand is the number of single entries that precede this
5743 -- one, the second operand is the index value relative to the start of
5744 -- the referenced family, and the remaining operands are the lengths of
5745 -- the entry families that precede this entry, i.e. the constructed
5746 -- expression is:
5748 -- number_simple_entries +
5749 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5750 -- family'length + ...
5752 -- where index-value is the given index value, and s is the index
5753 -- subtype (we have to use pos because the subtype might be an
5754 -- enumeration type preventing direct subtraction). Note that the task
5755 -- entry array is one-indexed.
5757 -- The upper bound of the entry family may be a discriminant, so we
5758 -- retrieve the lower bound explicitly to compute offset, rather than
5759 -- using the index subtype which may mention a discriminant.
5761 if Present (Index) then
5762 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5764 Expr :=
5765 Make_Op_Add (Sloc,
5766 Left_Opnd => Num,
5768 Right_Opnd =>
5769 Family_Offset (
5770 Sloc,
5771 Make_Attribute_Reference (Sloc,
5772 Attribute_Name => Name_Pos,
5773 Prefix => New_Reference_To (Base_Type (S), Sloc),
5774 Expressions => New_List (Relocate_Node (Index))),
5775 Type_Low_Bound (S),
5776 Ttyp,
5777 False));
5778 else
5779 Expr := Num;
5780 end if;
5782 -- Now add lengths of preceding entries and entry families
5784 Prev := First_Entity (Ttyp);
5786 while Chars (Prev) /= Chars (Ent)
5787 or else (Ekind (Prev) /= Ekind (Ent))
5788 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5789 loop
5790 if Ekind (Prev) = E_Entry then
5791 Set_Intval (Num, Intval (Num) + 1);
5793 elsif Ekind (Prev) = E_Entry_Family then
5794 S :=
5795 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5796 Lo := Type_Low_Bound (S);
5797 Hi := Type_High_Bound (S);
5799 Expr :=
5800 Make_Op_Add (Sloc,
5801 Left_Opnd => Expr,
5802 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5804 -- Other components are anonymous types to be ignored
5806 else
5807 null;
5808 end if;
5810 Next_Entity (Prev);
5811 end loop;
5813 return Expr;
5814 end Entry_Index_Expression;
5816 ---------------------------
5817 -- Establish_Task_Master --
5818 ---------------------------
5820 procedure Establish_Task_Master (N : Node_Id) is
5821 Call : Node_Id;
5823 begin
5824 if Restriction_Active (No_Task_Hierarchy) = False then
5825 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5827 -- The block may have no declarations, and nevertheless be a task
5828 -- master, if it contains a call that may return an object that
5829 -- contains tasks.
5831 if No (Declarations (N)) then
5832 Set_Declarations (N, New_List (Call));
5833 else
5834 Prepend_To (Declarations (N), Call);
5835 end if;
5837 Analyze (Call);
5838 end if;
5839 end Establish_Task_Master;
5841 --------------------------------
5842 -- Expand_Accept_Declarations --
5843 --------------------------------
5845 -- Part of the expansion of an accept statement involves the creation of
5846 -- a declaration that can be referenced from the statement sequence of
5847 -- the accept:
5849 -- Ann : Address;
5851 -- This declaration is inserted immediately before the accept statement
5852 -- and it is important that it be inserted before the statements of the
5853 -- statement sequence are analyzed. Thus it would be too late to create
5854 -- this declaration in the Expand_N_Accept_Statement routine, which is
5855 -- why there is a separate procedure to be called directly from Sem_Ch9.
5857 -- Ann is used to hold the address of the record containing the parameters
5858 -- (see Expand_N_Entry_Call for more details on how this record is built).
5859 -- References to the parameters do an unchecked conversion of this address
5860 -- to a pointer to the required record type, and then access the field that
5861 -- holds the value of the required parameter. The entity for the address
5862 -- variable is held as the top stack element (i.e. the last element) of the
5863 -- Accept_Address stack in the corresponding entry entity, and this element
5864 -- must be set in place before the statements are processed.
5866 -- The above description applies to the case of a stand alone accept
5867 -- statement, i.e. one not appearing as part of a select alternative.
5869 -- For the case of an accept that appears as part of a select alternative
5870 -- of a selective accept, we must still create the declaration right away,
5871 -- since Ann is needed immediately, but there is an important difference:
5873 -- The declaration is inserted before the selective accept, not before
5874 -- the accept statement (which is not part of a list anyway, and so would
5875 -- not accommodate inserted declarations)
5877 -- We only need one address variable for the entire selective accept. So
5878 -- the Ann declaration is created only for the first accept alternative,
5879 -- and subsequent accept alternatives reference the same Ann variable.
5881 -- We can distinguish the two cases by seeing whether the accept statement
5882 -- is part of a list. If not, then it must be in an accept alternative.
5884 -- To expand the requeue statement, a label is provided at the end of the
5885 -- accept statement or alternative of which it is a part, so that the
5886 -- statement can be skipped after the requeue is complete. This label is
5887 -- created here rather than during the expansion of the accept statement,
5888 -- because it will be needed by any requeue statements within the accept,
5889 -- which are expanded before the accept.
5891 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5892 Loc : constant Source_Ptr := Sloc (N);
5893 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5894 Ann : Entity_Id := Empty;
5895 Adecl : Node_Id;
5896 Lab : Node_Id;
5897 Ldecl : Node_Id;
5898 Ldecl2 : Node_Id;
5900 begin
5901 if Expander_Active then
5903 -- If we have no handled statement sequence, we may need to build
5904 -- a dummy sequence consisting of a null statement. This can be
5905 -- skipped if the trivial accept optimization is permitted.
5907 if not Trivial_Accept_OK
5908 and then
5909 (No (Stats) or else Null_Statements (Statements (Stats)))
5910 then
5911 Set_Handled_Statement_Sequence (N,
5912 Make_Handled_Sequence_Of_Statements (Loc,
5913 Statements => New_List (Make_Null_Statement (Loc))));
5914 end if;
5916 -- Create and declare two labels to be placed at the end of the
5917 -- accept statement. The first label is used to allow requeues to
5918 -- skip the remainder of entry processing. The second label is used
5919 -- to skip the remainder of entry processing if the rendezvous
5920 -- completes in the middle of the accept body.
5922 if Present (Handled_Statement_Sequence (N)) then
5923 declare
5924 Ent : Entity_Id;
5926 begin
5927 Ent := Make_Temporary (Loc, 'L');
5928 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5929 Ldecl :=
5930 Make_Implicit_Label_Declaration (Loc,
5931 Defining_Identifier => Ent,
5932 Label_Construct => Lab);
5933 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5935 Ent := Make_Temporary (Loc, 'L');
5936 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5937 Ldecl2 :=
5938 Make_Implicit_Label_Declaration (Loc,
5939 Defining_Identifier => Ent,
5940 Label_Construct => Lab);
5941 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5942 end;
5944 else
5945 Ldecl := Empty;
5946 Ldecl2 := Empty;
5947 end if;
5949 -- Case of stand alone accept statement
5951 if Is_List_Member (N) then
5953 if Present (Handled_Statement_Sequence (N)) then
5954 Ann := Make_Temporary (Loc, 'A');
5956 Adecl :=
5957 Make_Object_Declaration (Loc,
5958 Defining_Identifier => Ann,
5959 Object_Definition =>
5960 New_Reference_To (RTE (RE_Address), Loc));
5962 Insert_Before_And_Analyze (N, Adecl);
5963 Insert_Before_And_Analyze (N, Ldecl);
5964 Insert_Before_And_Analyze (N, Ldecl2);
5965 end if;
5967 -- Case of accept statement which is in an accept alternative
5969 else
5970 declare
5971 Acc_Alt : constant Node_Id := Parent (N);
5972 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5973 Alt : Node_Id;
5975 begin
5976 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5977 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5979 -- ??? Consider a single label for select statements
5981 if Present (Handled_Statement_Sequence (N)) then
5982 Prepend (Ldecl2,
5983 Statements (Handled_Statement_Sequence (N)));
5984 Analyze (Ldecl2);
5986 Prepend (Ldecl,
5987 Statements (Handled_Statement_Sequence (N)));
5988 Analyze (Ldecl);
5989 end if;
5991 -- Find first accept alternative of the selective accept. A
5992 -- valid selective accept must have at least one accept in it.
5994 Alt := First (Select_Alternatives (Sel_Acc));
5996 while Nkind (Alt) /= N_Accept_Alternative loop
5997 Next (Alt);
5998 end loop;
6000 -- If we are the first accept statement, then we have to create
6001 -- the Ann variable, as for the stand alone case, except that
6002 -- it is inserted before the selective accept. Similarly, a
6003 -- label for requeue expansion must be declared.
6005 if N = Accept_Statement (Alt) then
6006 Ann := Make_Temporary (Loc, 'A');
6007 Adecl :=
6008 Make_Object_Declaration (Loc,
6009 Defining_Identifier => Ann,
6010 Object_Definition =>
6011 New_Reference_To (RTE (RE_Address), Loc));
6013 Insert_Before_And_Analyze (Sel_Acc, Adecl);
6015 -- If we are not the first accept statement, then find the Ann
6016 -- variable allocated by the first accept and use it.
6018 else
6019 Ann :=
6020 Node (Last_Elmt (Accept_Address
6021 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
6022 end if;
6023 end;
6024 end if;
6026 -- Merge here with Ann either created or referenced, and Adecl
6027 -- pointing to the corresponding declaration. Remaining processing
6028 -- is the same for the two cases.
6030 if Present (Ann) then
6031 Append_Elmt (Ann, Accept_Address (Ent));
6032 Set_Debug_Info_Needed (Ann);
6033 end if;
6035 -- Create renaming declarations for the entry formals. Each reference
6036 -- to a formal becomes a dereference of a component of the parameter
6037 -- block, whose address is held in Ann. These declarations are
6038 -- eventually inserted into the accept block, and analyzed there so
6039 -- that they have the proper scope for gdb and do not conflict with
6040 -- other declarations.
6042 if Present (Parameter_Specifications (N))
6043 and then Present (Handled_Statement_Sequence (N))
6044 then
6045 declare
6046 Comp : Entity_Id;
6047 Decl : Node_Id;
6048 Formal : Entity_Id;
6049 New_F : Entity_Id;
6050 Renamed_Formal : Node_Id;
6052 begin
6053 Push_Scope (Ent);
6054 Formal := First_Formal (Ent);
6056 while Present (Formal) loop
6057 Comp := Entry_Component (Formal);
6058 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
6060 Set_Etype (New_F, Etype (Formal));
6061 Set_Scope (New_F, Ent);
6063 -- Now we set debug info needed on New_F even though it does
6064 -- not come from source, so that the debugger will get the
6065 -- right information for these generated names.
6067 Set_Debug_Info_Needed (New_F);
6069 if Ekind (Formal) = E_In_Parameter then
6070 Set_Ekind (New_F, E_Constant);
6071 else
6072 Set_Ekind (New_F, E_Variable);
6073 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
6074 end if;
6076 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
6078 Renamed_Formal :=
6079 Make_Selected_Component (Loc,
6080 Prefix =>
6081 Unchecked_Convert_To (
6082 Entry_Parameters_Type (Ent),
6083 New_Reference_To (Ann, Loc)),
6084 Selector_Name =>
6085 New_Reference_To (Comp, Loc));
6087 Decl :=
6088 Build_Renamed_Formal_Declaration
6089 (New_F, Formal, Comp, Renamed_Formal);
6091 if No (Declarations (N)) then
6092 Set_Declarations (N, New_List);
6093 end if;
6095 Append (Decl, Declarations (N));
6096 Set_Renamed_Object (Formal, New_F);
6097 Next_Formal (Formal);
6098 end loop;
6100 End_Scope;
6101 end;
6102 end if;
6103 end if;
6104 end Expand_Accept_Declarations;
6106 ---------------------------------------------
6107 -- Expand_Access_Protected_Subprogram_Type --
6108 ---------------------------------------------
6110 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6111 Loc : constant Source_Ptr := Sloc (N);
6112 Comps : List_Id;
6113 T : constant Entity_Id := Defining_Identifier (N);
6114 D_T : constant Entity_Id := Designated_Type (T);
6115 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6116 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6117 P_List : constant List_Id := Build_Protected_Spec
6118 (N, RTE (RE_Address), D_T, False);
6119 Decl1 : Node_Id;
6120 Decl2 : Node_Id;
6121 Def1 : Node_Id;
6123 begin
6124 -- Create access to subprogram with full signature
6126 if Etype (D_T) /= Standard_Void_Type then
6127 Def1 :=
6128 Make_Access_Function_Definition (Loc,
6129 Parameter_Specifications => P_List,
6130 Result_Definition =>
6131 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6133 else
6134 Def1 :=
6135 Make_Access_Procedure_Definition (Loc,
6136 Parameter_Specifications => P_List);
6137 end if;
6139 Decl1 :=
6140 Make_Full_Type_Declaration (Loc,
6141 Defining_Identifier => D_T2,
6142 Type_Definition => Def1);
6144 Insert_After_And_Analyze (N, Decl1);
6146 -- Associate the access to subprogram with its original access to
6147 -- protected subprogram type. Needed by the backend to know that this
6148 -- type corresponds with an access to protected subprogram type.
6150 Set_Original_Access_Type (D_T2, T);
6152 -- Create Equivalent_Type, a record with two components for an access to
6153 -- object and an access to subprogram.
6155 Comps := New_List (
6156 Make_Component_Declaration (Loc,
6157 Defining_Identifier => Make_Temporary (Loc, 'P'),
6158 Component_Definition =>
6159 Make_Component_Definition (Loc,
6160 Aliased_Present => False,
6161 Subtype_Indication =>
6162 New_Occurrence_Of (RTE (RE_Address), Loc))),
6164 Make_Component_Declaration (Loc,
6165 Defining_Identifier => Make_Temporary (Loc, 'S'),
6166 Component_Definition =>
6167 Make_Component_Definition (Loc,
6168 Aliased_Present => False,
6169 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6171 Decl2 :=
6172 Make_Full_Type_Declaration (Loc,
6173 Defining_Identifier => E_T,
6174 Type_Definition =>
6175 Make_Record_Definition (Loc,
6176 Component_List =>
6177 Make_Component_List (Loc, Component_Items => Comps)));
6179 Insert_After_And_Analyze (Decl1, Decl2);
6180 Set_Equivalent_Type (T, E_T);
6181 end Expand_Access_Protected_Subprogram_Type;
6183 --------------------------
6184 -- Expand_Entry_Barrier --
6185 --------------------------
6187 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6188 Cond : constant Node_Id :=
6189 Condition (Entry_Body_Formal_Part (N));
6190 Prot : constant Entity_Id := Scope (Ent);
6191 Spec_Decl : constant Node_Id := Parent (Prot);
6192 Func : Node_Id;
6193 B_F : Node_Id;
6194 Body_Decl : Node_Id;
6196 begin
6197 if No_Run_Time_Mode then
6198 Error_Msg_CRT ("entry barrier", N);
6199 return;
6200 end if;
6202 -- The body of the entry barrier must be analyzed in the context of the
6203 -- protected object, but its scope is external to it, just as any other
6204 -- unprotected version of a protected operation. The specification has
6205 -- been produced when the protected type declaration was elaborated. We
6206 -- build the body, insert it in the enclosing scope, but analyze it in
6207 -- the current context. A more uniform approach would be to treat the
6208 -- barrier just as a protected function, and discard the protected
6209 -- version of it because it is never called.
6211 if Expander_Active then
6212 B_F := Build_Barrier_Function (N, Ent, Prot);
6213 Func := Barrier_Function (Ent);
6214 Set_Corresponding_Spec (B_F, Func);
6216 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6218 if Nkind (Parent (Body_Decl)) = N_Subunit then
6219 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6220 end if;
6222 Insert_Before_And_Analyze (Body_Decl, B_F);
6224 Set_Discriminals (Spec_Decl);
6225 Set_Scope (Func, Scope (Prot));
6227 else
6228 Analyze_And_Resolve (Cond, Any_Boolean);
6229 end if;
6231 -- The Ravenscar profile restricts barriers to simple variables declared
6232 -- within the protected object. We also allow Boolean constants, since
6233 -- these appear in several published examples and are also allowed by
6234 -- the Aonix compiler.
6236 -- Note that after analysis variables in this context will be replaced
6237 -- by the corresponding prival, that is to say a renaming of a selected
6238 -- component of the form _Object.Var. If expansion is disabled, as
6239 -- within a generic, we check that the entity appears in the current
6240 -- scope.
6242 if Is_Entity_Name (Cond) then
6244 -- A small optimization of useless renamings. If the scope of the
6245 -- entity of the condition is not the barrier function, then the
6246 -- condition does not reference any of the generated renamings
6247 -- within the function.
6249 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6250 Set_Declarations (B_F, Empty_List);
6251 end if;
6253 if Entity (Cond) = Standard_False
6254 or else
6255 Entity (Cond) = Standard_True
6256 then
6257 return;
6259 elsif not Expander_Active
6260 and then Scope (Entity (Cond)) = Current_Scope
6261 then
6262 return;
6264 -- Check for case of _object.all.field (note that the explicit
6265 -- dereference gets inserted by analyze/expand of _object.field)
6267 elsif Present (Renamed_Object (Entity (Cond)))
6268 and then
6269 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
6270 and then
6271 Chars
6272 (Prefix
6273 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
6274 then
6275 return;
6276 end if;
6277 end if;
6279 -- It is not a boolean variable or literal, so check the restriction
6281 Check_Restriction (Simple_Barriers, Cond);
6282 end Expand_Entry_Barrier;
6284 ------------------------------
6285 -- Expand_N_Abort_Statement --
6286 ------------------------------
6288 -- Expand abort T1, T2, .. Tn; into:
6289 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6291 procedure Expand_N_Abort_Statement (N : Node_Id) is
6292 Loc : constant Source_Ptr := Sloc (N);
6293 Tlist : constant List_Id := Names (N);
6294 Count : Nat;
6295 Aggr : Node_Id;
6296 Tasknm : Node_Id;
6298 begin
6299 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6300 Count := 0;
6302 Tasknm := First (Tlist);
6304 while Present (Tasknm) loop
6305 Count := Count + 1;
6307 -- A task interface class-wide type object is being aborted.
6308 -- Retrieve its _task_id by calling a dispatching routine.
6310 if Ada_Version >= Ada_2005
6311 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6312 and then Is_Interface (Etype (Tasknm))
6313 and then Is_Task_Interface (Etype (Tasknm))
6314 then
6315 Append_To (Component_Associations (Aggr),
6316 Make_Component_Association (Loc,
6317 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6318 Expression =>
6320 -- Task_Id (Tasknm._disp_get_task_id)
6322 Make_Unchecked_Type_Conversion (Loc,
6323 Subtype_Mark =>
6324 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
6325 Expression =>
6326 Make_Selected_Component (Loc,
6327 Prefix => New_Copy_Tree (Tasknm),
6328 Selector_Name =>
6329 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6331 else
6332 Append_To (Component_Associations (Aggr),
6333 Make_Component_Association (Loc,
6334 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6335 Expression => Concurrent_Ref (Tasknm)));
6336 end if;
6338 Next (Tasknm);
6339 end loop;
6341 Rewrite (N,
6342 Make_Procedure_Call_Statement (Loc,
6343 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
6344 Parameter_Associations => New_List (
6345 Make_Qualified_Expression (Loc,
6346 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
6347 Expression => Aggr))));
6349 Analyze (N);
6350 end Expand_N_Abort_Statement;
6352 -------------------------------
6353 -- Expand_N_Accept_Statement --
6354 -------------------------------
6356 -- This procedure handles expansion of accept statements that stand
6357 -- alone, i.e. they are not part of an accept alternative. The expansion
6358 -- of accept statement in accept alternatives is handled by the routines
6359 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6360 -- following description applies only to stand alone accept statements.
6362 -- If there is no handled statement sequence, or only null statements,
6363 -- then this is called a trivial accept, and the expansion is:
6365 -- Accept_Trivial (entry-index)
6367 -- If there is a handled statement sequence, then the expansion is:
6369 -- Ann : Address;
6370 -- {Lnn : Label}
6372 -- begin
6373 -- begin
6374 -- Accept_Call (entry-index, Ann);
6375 -- Renaming_Declarations for formals
6376 -- <statement sequence from N_Accept_Statement node>
6377 -- Complete_Rendezvous;
6378 -- <<Lnn>>
6380 -- exception
6381 -- when ... =>
6382 -- <exception handler from N_Accept_Statement node>
6383 -- Complete_Rendezvous;
6384 -- when ... =>
6385 -- <exception handler from N_Accept_Statement node>
6386 -- Complete_Rendezvous;
6387 -- ...
6388 -- end;
6390 -- exception
6391 -- when all others =>
6392 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6393 -- end;
6395 -- The first three declarations were already inserted ahead of the accept
6396 -- statement by the Expand_Accept_Declarations procedure, which was called
6397 -- directly from the semantics during analysis of the accept statement,
6398 -- before analyzing its contained statements.
6400 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6401 -- from possible expansion activity (the original source of course does
6402 -- not have any declarations associated with the accept statement, since
6403 -- an accept statement has no declarative part). In particular, if the
6404 -- expander is active, the first such declaration is the declaration of
6405 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6407 -- The two blocks are merged into a single block if the inner block has
6408 -- no exception handlers, but otherwise two blocks are required, since
6409 -- exceptions might be raised in the exception handlers of the inner
6410 -- block, and Exceptional_Complete_Rendezvous must be called.
6412 procedure Expand_N_Accept_Statement (N : Node_Id) is
6413 Loc : constant Source_Ptr := Sloc (N);
6414 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6415 Ename : constant Node_Id := Entry_Direct_Name (N);
6416 Eindx : constant Node_Id := Entry_Index (N);
6417 Eent : constant Entity_Id := Entity (Ename);
6418 Acstack : constant Elist_Id := Accept_Address (Eent);
6419 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6420 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6421 Blkent : Entity_Id;
6422 Call : Node_Id;
6423 Block : Node_Id;
6425 begin
6426 -- If the accept statement is not part of a list, then its parent must
6427 -- be an accept alternative, and, as described above, we do not do any
6428 -- expansion for such accept statements at this level.
6430 if not Is_List_Member (N) then
6431 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6432 return;
6434 -- Trivial accept case (no statement sequence, or null statements).
6435 -- If the accept statement has declarations, then just insert them
6436 -- before the procedure call.
6438 elsif Trivial_Accept_OK
6439 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6440 then
6441 -- Remove declarations for renamings, because the parameter block
6442 -- will not be assigned.
6444 declare
6445 D : Node_Id;
6446 Next_D : Node_Id;
6448 begin
6449 D := First (Declarations (N));
6451 while Present (D) loop
6452 Next_D := Next (D);
6453 if Nkind (D) = N_Object_Renaming_Declaration then
6454 Remove (D);
6455 end if;
6457 D := Next_D;
6458 end loop;
6459 end;
6461 if Present (Declarations (N)) then
6462 Insert_Actions (N, Declarations (N));
6463 end if;
6465 Rewrite (N,
6466 Make_Procedure_Call_Statement (Loc,
6467 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
6468 Parameter_Associations => New_List (
6469 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6471 Analyze (N);
6473 -- Discard Entry_Address that was created for it, so it will not be
6474 -- emitted if this accept statement is in the statement part of a
6475 -- delay alternative.
6477 if Present (Stats) then
6478 Remove_Last_Elmt (Acstack);
6479 end if;
6481 -- Case of statement sequence present
6483 else
6484 -- Construct the block, using the declarations from the accept
6485 -- statement if any to initialize the declarations of the block.
6487 Blkent := Make_Temporary (Loc, 'A');
6488 Set_Ekind (Blkent, E_Block);
6489 Set_Etype (Blkent, Standard_Void_Type);
6490 Set_Scope (Blkent, Current_Scope);
6492 Block :=
6493 Make_Block_Statement (Loc,
6494 Identifier => New_Reference_To (Blkent, Loc),
6495 Declarations => Declarations (N),
6496 Handled_Statement_Sequence => Build_Accept_Body (N));
6498 -- For the analysis of the generated declarations, the parent node
6499 -- must be properly set.
6501 Set_Parent (Block, Parent (N));
6503 -- Prepend call to Accept_Call to main statement sequence If the
6504 -- accept has exception handlers, the statement sequence is wrapped
6505 -- in a block. Insert call and renaming declarations in the
6506 -- declarations of the block, so they are elaborated before the
6507 -- handlers.
6509 Call :=
6510 Make_Procedure_Call_Statement (Loc,
6511 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
6512 Parameter_Associations => New_List (
6513 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6514 New_Reference_To (Ann, Loc)));
6516 if Parent (Stats) = N then
6517 Prepend (Call, Statements (Stats));
6518 else
6519 Set_Declarations (Parent (Stats), New_List (Call));
6520 end if;
6522 Analyze (Call);
6524 Push_Scope (Blkent);
6526 declare
6527 D : Node_Id;
6528 Next_D : Node_Id;
6529 Typ : Entity_Id;
6531 begin
6532 D := First (Declarations (N));
6533 while Present (D) loop
6534 Next_D := Next (D);
6536 if Nkind (D) = N_Object_Renaming_Declaration then
6538 -- The renaming declarations for the formals were created
6539 -- during analysis of the accept statement, and attached to
6540 -- the list of declarations. Place them now in the context
6541 -- of the accept block or subprogram.
6543 Remove (D);
6544 Typ := Entity (Subtype_Mark (D));
6545 Insert_After (Call, D);
6546 Analyze (D);
6548 -- If the formal is class_wide, it does not have an actual
6549 -- subtype. The analysis of the renaming declaration creates
6550 -- one, but we need to retain the class-wide nature of the
6551 -- entity.
6553 if Is_Class_Wide_Type (Typ) then
6554 Set_Etype (Defining_Identifier (D), Typ);
6555 end if;
6557 end if;
6559 D := Next_D;
6560 end loop;
6561 end;
6563 End_Scope;
6565 -- Replace the accept statement by the new block
6567 Rewrite (N, Block);
6568 Analyze (N);
6570 -- Last step is to unstack the Accept_Address value
6572 Remove_Last_Elmt (Acstack);
6573 end if;
6574 end Expand_N_Accept_Statement;
6576 ----------------------------------
6577 -- Expand_N_Asynchronous_Select --
6578 ----------------------------------
6580 -- This procedure assumes that the trigger statement is an entry call or
6581 -- a dispatching procedure call. A delay alternative should already have
6582 -- been expanded into an entry call to the appropriate delay object Wait
6583 -- entry.
6585 -- If the trigger is a task entry call, the select is implemented with
6586 -- a Task_Entry_Call:
6588 -- declare
6589 -- B : Boolean;
6590 -- C : Boolean;
6591 -- P : parms := (parm, parm, parm);
6593 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6595 -- procedure _clean is
6596 -- begin
6597 -- ...
6598 -- Cancel_Task_Entry_Call (C);
6599 -- ...
6600 -- end _clean;
6602 -- begin
6603 -- Abort_Defer;
6604 -- Task_Entry_Call
6605 -- (<acceptor-task>, -- Acceptor
6606 -- <entry-index>, -- E
6607 -- P'Address, -- Uninterpreted_Data
6608 -- Asynchronous_Call, -- Mode
6609 -- B); -- Rendezvous_Successful
6611 -- begin
6612 -- begin
6613 -- Abort_Undefer;
6614 -- <abortable-part>
6615 -- at end
6616 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6617 -- end;
6618 -- exception
6619 -- when Abort_Signal => Abort_Undefer;
6620 -- end;
6622 -- parm := P.param;
6623 -- parm := P.param;
6624 -- ...
6625 -- if not C then
6626 -- <triggered-statements>
6627 -- end if;
6628 -- end;
6630 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6631 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6632 -- as follows:
6634 -- declare
6635 -- P : parms := (parm, parm, parm);
6636 -- begin
6637 -- Call_Simple (acceptor-task, entry-index, P'Address);
6638 -- parm := P.param;
6639 -- parm := P.param;
6640 -- ...
6641 -- end;
6643 -- so the task at hand is to convert the latter expansion into the former
6645 -- If the trigger is a protected entry call, the select is implemented
6646 -- with Protected_Entry_Call:
6648 -- declare
6649 -- P : E1_Params := (param, param, param);
6650 -- Bnn : Communications_Block;
6652 -- begin
6653 -- declare
6655 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6657 -- procedure _clean is
6658 -- begin
6659 -- ...
6660 -- if Enqueued (Bnn) then
6661 -- Cancel_Protected_Entry_Call (Bnn);
6662 -- end if;
6663 -- ...
6664 -- end _clean;
6666 -- begin
6667 -- begin
6668 -- Protected_Entry_Call
6669 -- (po._object'Access, -- Object
6670 -- <entry index>, -- E
6671 -- P'Address, -- Uninterpreted_Data
6672 -- Asynchronous_Call, -- Mode
6673 -- Bnn); -- Block
6675 -- if Enqueued (Bnn) then
6676 -- <abortable-part>
6677 -- end if;
6678 -- at end
6679 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6680 -- end;
6681 -- exception
6682 -- when Abort_Signal => Abort_Undefer;
6683 -- end;
6685 -- if not Cancelled (Bnn) then
6686 -- <triggered-statements>
6687 -- end if;
6688 -- end;
6690 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6691 -- entry call:
6693 -- declare
6694 -- P : E1_Params := (param, param, param);
6695 -- Bnn : Communications_Block;
6697 -- begin
6698 -- Protected_Entry_Call
6699 -- (po._object'Access, -- Object
6700 -- <entry index>, -- E
6701 -- P'Address, -- Uninterpreted_Data
6702 -- Simple_Call, -- Mode
6703 -- Bnn); -- Block
6704 -- parm := P.param;
6705 -- parm := P.param;
6706 -- ...
6707 -- end;
6709 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6710 -- expanded into:
6712 -- declare
6713 -- B : Boolean := False;
6714 -- Bnn : Communication_Block;
6715 -- C : Ada.Tags.Prim_Op_Kind;
6716 -- D : System.Storage_Elements.Dummy_Communication_Block;
6717 -- K : Ada.Tags.Tagged_Kind :=
6718 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6719 -- P : Parameters := (Param1 .. ParamN);
6720 -- S : Integer;
6721 -- U : Boolean;
6723 -- begin
6724 -- if K = Ada.Tags.TK_Limited_Tagged
6725 -- or else K = Ada.Tags.TK_Tagged
6726 -- then
6727 -- <dispatching-call>;
6728 -- <triggering-statements>;
6730 -- else
6731 -- S :=
6732 -- Ada.Tags.Get_Offset_Index
6733 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6735 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6737 -- if C = POK_Protected_Entry then
6738 -- declare
6739 -- procedure _clean is
6740 -- begin
6741 -- if Enqueued (Bnn) then
6742 -- Cancel_Protected_Entry_Call (Bnn);
6743 -- end if;
6744 -- end _clean;
6746 -- begin
6747 -- begin
6748 -- _Disp_Asynchronous_Select
6749 -- (<object>, S, P'Address, D, B);
6750 -- Bnn := Communication_Block (D);
6752 -- Param1 := P.Param1;
6753 -- ...
6754 -- ParamN := P.ParamN;
6756 -- if Enqueued (Bnn) then
6757 -- <abortable-statements>
6758 -- end if;
6759 -- at end
6760 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6761 -- end;
6762 -- exception
6763 -- when Abort_Signal => Abort_Undefer;
6764 -- end;
6766 -- if not Cancelled (Bnn) then
6767 -- <triggering-statements>
6768 -- end if;
6770 -- elsif C = POK_Task_Entry then
6771 -- declare
6772 -- procedure _clean is
6773 -- begin
6774 -- Cancel_Task_Entry_Call (U);
6775 -- end _clean;
6777 -- begin
6778 -- Abort_Defer;
6780 -- _Disp_Asynchronous_Select
6781 -- (<object>, S, P'Address, D, B);
6782 -- Bnn := Communication_Bloc (D);
6784 -- Param1 := P.Param1;
6785 -- ...
6786 -- ParamN := P.ParamN;
6788 -- begin
6789 -- begin
6790 -- Abort_Undefer;
6791 -- <abortable-statements>
6792 -- at end
6793 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6794 -- end;
6795 -- exception
6796 -- when Abort_Signal => Abort_Undefer;
6797 -- end;
6799 -- if not U then
6800 -- <triggering-statements>
6801 -- end if;
6802 -- end;
6804 -- else
6805 -- <dispatching-call>;
6806 -- <triggering-statements>
6807 -- end if;
6808 -- end if;
6809 -- end;
6811 -- The job is to convert this to the asynchronous form
6813 -- If the trigger is a delay statement, it will have been expanded into a
6814 -- call to one of the GNARL delay procedures. This routine will convert
6815 -- this into a protected entry call on a delay object and then continue
6816 -- processing as for a protected entry call trigger. This requires
6817 -- declaring a Delay_Block object and adding a pointer to this object to
6818 -- the parameter list of the delay procedure to form the parameter list of
6819 -- the entry call. This object is used by the runtime to queue the delay
6820 -- request.
6822 -- For a description of the use of P and the assignments after the call,
6823 -- see Expand_N_Entry_Call_Statement.
6825 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6826 Loc : constant Source_Ptr := Sloc (N);
6827 Abrt : constant Node_Id := Abortable_Part (N);
6828 Trig : constant Node_Id := Triggering_Alternative (N);
6830 Abort_Block_Ent : Entity_Id;
6831 Abortable_Block : Node_Id;
6832 Actuals : List_Id;
6833 Astats : List_Id;
6834 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6835 Blk_Typ : Entity_Id;
6836 Call : Node_Id;
6837 Call_Ent : Entity_Id;
6838 Cancel_Param : Entity_Id;
6839 Cleanup_Block : Node_Id;
6840 Cleanup_Block_Ent : Entity_Id;
6841 Cleanup_Stmts : List_Id;
6842 Conc_Typ_Stmts : List_Id;
6843 Concval : Node_Id;
6844 Dblock_Ent : Entity_Id;
6845 Decl : Node_Id;
6846 Decls : List_Id;
6847 Ecall : Node_Id;
6848 Ename : Node_Id;
6849 Enqueue_Call : Node_Id;
6850 Formals : List_Id;
6851 Hdle : List_Id;
6852 Handler_Stmt : Node_Id;
6853 Index : Node_Id;
6854 Lim_Typ_Stmts : List_Id;
6855 N_Orig : Node_Id;
6856 Obj : Entity_Id;
6857 Param : Node_Id;
6858 Params : List_Id;
6859 Pdef : Entity_Id;
6860 ProtE_Stmts : List_Id;
6861 ProtP_Stmts : List_Id;
6862 Stmt : Node_Id;
6863 Stmts : List_Id;
6864 TaskE_Stmts : List_Id;
6865 Tstats : List_Id;
6867 B : Entity_Id; -- Call status flag
6868 Bnn : Entity_Id; -- Communication block
6869 C : Entity_Id; -- Call kind
6870 K : Entity_Id; -- Tagged kind
6871 P : Entity_Id; -- Parameter block
6872 S : Entity_Id; -- Primitive operation slot
6873 T : Entity_Id; -- Additional status flag
6875 procedure Rewrite_Abortable_Part;
6876 -- If the trigger is a dispatching call, the expansion inserts multiple
6877 -- copies of the abortable part. This is both inefficient, and may lead
6878 -- to duplicate definitions that the back-end will reject, when the
6879 -- abortable part includes loops. This procedure rewrites the abortable
6880 -- part into a call to a generated procedure.
6882 ----------------------------
6883 -- Rewrite_Abortable_Part --
6884 ----------------------------
6886 procedure Rewrite_Abortable_Part is
6887 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6888 Decl : Node_Id;
6890 begin
6891 Decl :=
6892 Make_Subprogram_Body (Loc,
6893 Specification =>
6894 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6895 Declarations => New_List,
6896 Handled_Statement_Sequence =>
6897 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6898 Insert_Before (N, Decl);
6899 Analyze (Decl);
6901 -- Rewrite abortable part into a call to this procedure.
6903 Astats :=
6904 New_List (
6905 Make_Procedure_Call_Statement (Loc,
6906 Name => New_Occurrence_Of (Proc, Loc)));
6907 end Rewrite_Abortable_Part;
6909 begin
6910 Process_Statements_For_Controlled_Objects (Trig);
6911 Process_Statements_For_Controlled_Objects (Abrt);
6913 Ecall := Triggering_Statement (Trig);
6915 Ensure_Statement_Present (Sloc (Ecall), Trig);
6917 -- Retrieve Astats and Tstats now because the finalization machinery may
6918 -- wrap them in blocks.
6920 Astats := Statements (Abrt);
6921 Tstats := Statements (Trig);
6923 -- The arguments in the call may require dynamic allocation, and the
6924 -- call statement may have been transformed into a block. The block
6925 -- may contain additional declarations for internal entities, and the
6926 -- original call is found by sequential search.
6928 if Nkind (Ecall) = N_Block_Statement then
6929 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6930 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6931 N_Entry_Call_Statement)
6932 loop
6933 Next (Ecall);
6934 end loop;
6935 end if;
6937 -- This is either a dispatching call or a delay statement used as a
6938 -- trigger which was expanded into a procedure call.
6940 if Nkind (Ecall) = N_Procedure_Call_Statement then
6941 if Ada_Version >= Ada_2005
6942 and then
6943 (No (Original_Node (Ecall))
6944 or else not Nkind_In (Original_Node (Ecall),
6945 N_Delay_Relative_Statement,
6946 N_Delay_Until_Statement))
6947 then
6948 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6950 Rewrite_Abortable_Part;
6951 Decls := New_List;
6952 Stmts := New_List;
6954 -- Call status flag processing, generate:
6955 -- B : Boolean := False;
6957 B := Build_B (Loc, Decls);
6959 -- Communication block processing, generate:
6960 -- Bnn : Communication_Block;
6962 Bnn := Make_Temporary (Loc, 'B');
6963 Append_To (Decls,
6964 Make_Object_Declaration (Loc,
6965 Defining_Identifier => Bnn,
6966 Object_Definition =>
6967 New_Reference_To (RTE (RE_Communication_Block), Loc)));
6969 -- Call kind processing, generate:
6970 -- C : Ada.Tags.Prim_Op_Kind;
6972 C := Build_C (Loc, Decls);
6974 -- Tagged kind processing, generate:
6975 -- K : Ada.Tags.Tagged_Kind :=
6976 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6978 -- Dummy communication block, generate:
6979 -- D : Dummy_Communication_Block;
6981 Append_To (Decls,
6982 Make_Object_Declaration (Loc,
6983 Defining_Identifier =>
6984 Make_Defining_Identifier (Loc, Name_uD),
6985 Object_Definition =>
6986 New_Reference_To
6987 (RTE (RE_Dummy_Communication_Block), Loc)));
6989 K := Build_K (Loc, Decls, Obj);
6991 -- Parameter block processing
6993 Blk_Typ := Build_Parameter_Block
6994 (Loc, Actuals, Formals, Decls);
6995 P := Parameter_Block_Pack
6996 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6998 -- Dispatch table slot processing, generate:
6999 -- S : Integer;
7001 S := Build_S (Loc, Decls);
7003 -- Additional status flag processing, generate:
7004 -- Tnn : Boolean;
7006 T := Make_Temporary (Loc, 'T');
7007 Append_To (Decls,
7008 Make_Object_Declaration (Loc,
7009 Defining_Identifier => T,
7010 Object_Definition =>
7011 New_Reference_To (Standard_Boolean, Loc)));
7013 ------------------------------
7014 -- Protected entry handling --
7015 ------------------------------
7017 -- Generate:
7018 -- Param1 := P.Param1;
7019 -- ...
7020 -- ParamN := P.ParamN;
7022 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7024 -- Generate:
7025 -- Bnn := Communication_Block (D);
7027 Prepend_To (Cleanup_Stmts,
7028 Make_Assignment_Statement (Loc,
7029 Name => New_Reference_To (Bnn, Loc),
7030 Expression =>
7031 Make_Unchecked_Type_Conversion (Loc,
7032 Subtype_Mark =>
7033 New_Reference_To (RTE (RE_Communication_Block), Loc),
7034 Expression => Make_Identifier (Loc, Name_uD))));
7036 -- Generate:
7037 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7039 Prepend_To (Cleanup_Stmts,
7040 Make_Procedure_Call_Statement (Loc,
7041 Name =>
7042 New_Reference_To
7043 (Find_Prim_Op
7044 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7045 Loc),
7046 Parameter_Associations =>
7047 New_List (
7048 New_Copy_Tree (Obj), -- <object>
7049 New_Reference_To (S, Loc), -- S
7050 Make_Attribute_Reference (Loc, -- P'Address
7051 Prefix => New_Reference_To (P, Loc),
7052 Attribute_Name => Name_Address),
7053 Make_Identifier (Loc, Name_uD), -- D
7054 New_Reference_To (B, Loc)))); -- B
7056 -- Generate:
7057 -- if Enqueued (Bnn) then
7058 -- <abortable-statements>
7059 -- end if;
7061 Append_To (Cleanup_Stmts,
7062 Make_Implicit_If_Statement (N,
7063 Condition =>
7064 Make_Function_Call (Loc,
7065 Name =>
7066 New_Reference_To (RTE (RE_Enqueued), Loc),
7067 Parameter_Associations =>
7068 New_List (New_Reference_To (Bnn, Loc))),
7070 Then_Statements =>
7071 New_Copy_List_Tree (Astats)));
7073 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7074 -- will then generate a _clean for the communication block Bnn.
7076 -- Generate:
7077 -- declare
7078 -- procedure _clean is
7079 -- begin
7080 -- if Enqueued (Bnn) then
7081 -- Cancel_Protected_Entry_Call (Bnn);
7082 -- end if;
7083 -- end _clean;
7084 -- begin
7085 -- Cleanup_Stmts
7086 -- at end
7087 -- _clean;
7088 -- end;
7090 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7091 Cleanup_Block :=
7092 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7094 -- Wrap the cleanup block in an exception handling block
7096 -- Generate:
7097 -- begin
7098 -- Cleanup_Block
7099 -- exception
7100 -- when Abort_Signal => Abort_Undefer;
7101 -- end;
7103 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7104 ProtE_Stmts :=
7105 New_List (
7106 Make_Implicit_Label_Declaration (Loc,
7107 Defining_Identifier => Abort_Block_Ent),
7109 Build_Abort_Block
7110 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7112 -- Generate:
7113 -- if not Cancelled (Bnn) then
7114 -- <triggering-statements>
7115 -- end if;
7117 Append_To (ProtE_Stmts,
7118 Make_Implicit_If_Statement (N,
7119 Condition =>
7120 Make_Op_Not (Loc,
7121 Right_Opnd =>
7122 Make_Function_Call (Loc,
7123 Name =>
7124 New_Reference_To (RTE (RE_Cancelled), Loc),
7125 Parameter_Associations =>
7126 New_List (New_Reference_To (Bnn, Loc)))),
7128 Then_Statements =>
7129 New_Copy_List_Tree (Tstats)));
7131 -------------------------
7132 -- Task entry handling --
7133 -------------------------
7135 -- Generate:
7136 -- Param1 := P.Param1;
7137 -- ...
7138 -- ParamN := P.ParamN;
7140 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7142 -- Generate:
7143 -- Bnn := Communication_Block (D);
7145 Append_To (TaskE_Stmts,
7146 Make_Assignment_Statement (Loc,
7147 Name =>
7148 New_Reference_To (Bnn, Loc),
7149 Expression =>
7150 Make_Unchecked_Type_Conversion (Loc,
7151 Subtype_Mark =>
7152 New_Reference_To (RTE (RE_Communication_Block), Loc),
7153 Expression => Make_Identifier (Loc, Name_uD))));
7155 -- Generate:
7156 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7158 Prepend_To (TaskE_Stmts,
7159 Make_Procedure_Call_Statement (Loc,
7160 Name =>
7161 New_Reference_To (
7162 Find_Prim_Op (Etype (Etype (Obj)),
7163 Name_uDisp_Asynchronous_Select),
7164 Loc),
7166 Parameter_Associations =>
7167 New_List (
7168 New_Copy_Tree (Obj), -- <object>
7169 New_Reference_To (S, Loc), -- S
7170 Make_Attribute_Reference (Loc, -- P'Address
7171 Prefix => New_Reference_To (P, Loc),
7172 Attribute_Name => Name_Address),
7173 Make_Identifier (Loc, Name_uD), -- D
7174 New_Reference_To (B, Loc)))); -- B
7176 -- Generate:
7177 -- Abort_Defer;
7179 Prepend_To (TaskE_Stmts,
7180 Make_Procedure_Call_Statement (Loc,
7181 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7182 Parameter_Associations => No_List));
7184 -- Generate:
7185 -- Abort_Undefer;
7186 -- <abortable-statements>
7188 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7190 Prepend_To (Cleanup_Stmts,
7191 Make_Procedure_Call_Statement (Loc,
7192 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7193 Parameter_Associations => No_List));
7195 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7196 -- will generate a _clean for the additional status flag.
7198 -- Generate:
7199 -- declare
7200 -- procedure _clean is
7201 -- begin
7202 -- Cancel_Task_Entry_Call (U);
7203 -- end _clean;
7204 -- begin
7205 -- Cleanup_Stmts
7206 -- at end
7207 -- _clean;
7208 -- end;
7210 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7211 Cleanup_Block :=
7212 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7214 -- Wrap the cleanup block in an exception handling block
7216 -- Generate:
7217 -- begin
7218 -- Cleanup_Block
7219 -- exception
7220 -- when Abort_Signal => Abort_Undefer;
7221 -- end;
7223 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7225 Append_To (TaskE_Stmts,
7226 Make_Implicit_Label_Declaration (Loc,
7227 Defining_Identifier => Abort_Block_Ent));
7229 Append_To (TaskE_Stmts,
7230 Build_Abort_Block
7231 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7233 -- Generate:
7234 -- if not T then
7235 -- <triggering-statements>
7236 -- end if;
7238 Append_To (TaskE_Stmts,
7239 Make_Implicit_If_Statement (N,
7240 Condition =>
7241 Make_Op_Not (Loc, Right_Opnd => New_Reference_To (T, Loc)),
7243 Then_Statements =>
7244 New_Copy_List_Tree (Tstats)));
7246 ----------------------------------
7247 -- Protected procedure handling --
7248 ----------------------------------
7250 -- Generate:
7251 -- <dispatching-call>;
7252 -- <triggering-statements>
7254 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7255 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7257 -- Generate:
7258 -- S := Ada.Tags.Get_Offset_Index
7259 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7261 Conc_Typ_Stmts :=
7262 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7264 -- Generate:
7265 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7267 Append_To (Conc_Typ_Stmts,
7268 Make_Procedure_Call_Statement (Loc,
7269 Name =>
7270 New_Reference_To
7271 (Find_Prim_Op (Etype (Etype (Obj)),
7272 Name_uDisp_Get_Prim_Op_Kind),
7273 Loc),
7274 Parameter_Associations =>
7275 New_List (
7276 New_Copy_Tree (Obj),
7277 New_Reference_To (S, Loc),
7278 New_Reference_To (C, Loc))));
7280 -- Generate:
7281 -- if C = POK_Procedure_Entry then
7282 -- ProtE_Stmts
7283 -- elsif C = POK_Task_Entry then
7284 -- TaskE_Stmts
7285 -- else
7286 -- ProtP_Stmts
7287 -- end if;
7289 Append_To (Conc_Typ_Stmts,
7290 Make_Implicit_If_Statement (N,
7291 Condition =>
7292 Make_Op_Eq (Loc,
7293 Left_Opnd =>
7294 New_Reference_To (C, Loc),
7295 Right_Opnd =>
7296 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
7298 Then_Statements =>
7299 ProtE_Stmts,
7301 Elsif_Parts =>
7302 New_List (
7303 Make_Elsif_Part (Loc,
7304 Condition =>
7305 Make_Op_Eq (Loc,
7306 Left_Opnd =>
7307 New_Reference_To (C, Loc),
7308 Right_Opnd =>
7309 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
7311 Then_Statements =>
7312 TaskE_Stmts)),
7314 Else_Statements =>
7315 ProtP_Stmts));
7317 -- Generate:
7318 -- <dispatching-call>;
7319 -- <triggering-statements>
7321 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7322 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7324 -- Generate:
7325 -- if K = Ada.Tags.TK_Limited_Tagged
7326 -- or else K = Ada.Tags.TK_Tagged
7327 -- then
7328 -- Lim_Typ_Stmts
7329 -- else
7330 -- Conc_Typ_Stmts
7331 -- end if;
7333 Append_To (Stmts,
7334 Make_Implicit_If_Statement (N,
7335 Condition => Build_Dispatching_Tag_Check (K, N),
7336 Then_Statements => Lim_Typ_Stmts,
7337 Else_Statements => Conc_Typ_Stmts));
7339 Rewrite (N,
7340 Make_Block_Statement (Loc,
7341 Declarations =>
7342 Decls,
7343 Handled_Statement_Sequence =>
7344 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7346 Analyze (N);
7347 return;
7349 -- Delay triggering statement processing
7351 else
7352 -- Add a Delay_Block object to the parameter list of the delay
7353 -- procedure to form the parameter list of the Wait entry call.
7355 Dblock_Ent := Make_Temporary (Loc, 'D');
7357 Pdef := Entity (Name (Ecall));
7359 if Is_RTE (Pdef, RO_CA_Delay_For) then
7360 Enqueue_Call :=
7361 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
7363 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7364 Enqueue_Call :=
7365 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
7367 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7368 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
7369 end if;
7371 Append_To (Parameter_Associations (Ecall),
7372 Make_Attribute_Reference (Loc,
7373 Prefix => New_Reference_To (Dblock_Ent, Loc),
7374 Attribute_Name => Name_Unchecked_Access));
7376 -- Create the inner block to protect the abortable part
7378 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7380 Prepend_To (Astats,
7381 Make_Procedure_Call_Statement (Loc,
7382 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7384 Abortable_Block :=
7385 Make_Block_Statement (Loc,
7386 Identifier => New_Reference_To (Blk_Ent, Loc),
7387 Handled_Statement_Sequence =>
7388 Make_Handled_Sequence_Of_Statements (Loc,
7389 Statements => Astats),
7390 Has_Created_Identifier => True,
7391 Is_Asynchronous_Call_Block => True);
7393 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7395 Rewrite (Ecall,
7396 Make_Implicit_If_Statement (N,
7397 Condition =>
7398 Make_Function_Call (Loc,
7399 Name => Enqueue_Call,
7400 Parameter_Associations => Parameter_Associations (Ecall)),
7401 Then_Statements =>
7402 New_List (Make_Block_Statement (Loc,
7403 Handled_Statement_Sequence =>
7404 Make_Handled_Sequence_Of_Statements (Loc,
7405 Statements => New_List (
7406 Make_Implicit_Label_Declaration (Loc,
7407 Defining_Identifier => Blk_Ent,
7408 Label_Construct => Abortable_Block),
7409 Abortable_Block),
7410 Exception_Handlers => Hdle)))));
7412 Stmts := New_List (Ecall);
7414 -- Construct statement sequence for new block
7416 Append_To (Stmts,
7417 Make_Implicit_If_Statement (N,
7418 Condition =>
7419 Make_Function_Call (Loc,
7420 Name => New_Reference_To (
7421 RTE (RE_Timed_Out), Loc),
7422 Parameter_Associations => New_List (
7423 Make_Attribute_Reference (Loc,
7424 Prefix => New_Reference_To (Dblock_Ent, Loc),
7425 Attribute_Name => Name_Unchecked_Access))),
7426 Then_Statements => Tstats));
7428 -- The result is the new block
7430 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7432 Rewrite (N,
7433 Make_Block_Statement (Loc,
7434 Declarations => New_List (
7435 Make_Object_Declaration (Loc,
7436 Defining_Identifier => Dblock_Ent,
7437 Aliased_Present => True,
7438 Object_Definition =>
7439 New_Reference_To (RTE (RE_Delay_Block), Loc))),
7441 Handled_Statement_Sequence =>
7442 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7444 Analyze (N);
7445 return;
7446 end if;
7448 else
7449 N_Orig := N;
7450 end if;
7452 Extract_Entry (Ecall, Concval, Ename, Index);
7453 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7455 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7456 Decls := Declarations (Ecall);
7458 if Is_Protected_Type (Etype (Concval)) then
7460 -- Get the declarations of the block expanded from the entry call
7462 Decl := First (Decls);
7463 while Present (Decl)
7464 and then (Nkind (Decl) /= N_Object_Declaration
7465 or else not Is_RTE (Etype (Object_Definition (Decl)),
7466 RE_Communication_Block))
7467 loop
7468 Next (Decl);
7469 end loop;
7471 pragma Assert (Present (Decl));
7472 Cancel_Param := Defining_Identifier (Decl);
7474 -- Change the mode of the Protected_Entry_Call call
7476 -- Protected_Entry_Call (
7477 -- Object => po._object'Access,
7478 -- E => <entry index>;
7479 -- Uninterpreted_Data => P'Address;
7480 -- Mode => Asynchronous_Call;
7481 -- Block => Bnn);
7483 -- Skip assignments to temporaries created for in-out parameters
7485 -- This makes unwarranted assumptions about the shape of the expanded
7486 -- tree for the call, and should be cleaned up ???
7488 Stmt := First (Stmts);
7489 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7490 Next (Stmt);
7491 end loop;
7493 Call := Stmt;
7495 Param := First (Parameter_Associations (Call));
7496 while Present (Param)
7497 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7498 loop
7499 Next (Param);
7500 end loop;
7502 pragma Assert (Present (Param));
7503 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7504 Analyze (Param);
7506 -- Append an if statement to execute the abortable part
7508 -- Generate:
7509 -- if Enqueued (Bnn) then
7511 Append_To (Stmts,
7512 Make_Implicit_If_Statement (N,
7513 Condition =>
7514 Make_Function_Call (Loc,
7515 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
7516 Parameter_Associations => New_List (
7517 New_Reference_To (Cancel_Param, Loc))),
7518 Then_Statements => Astats));
7520 Abortable_Block :=
7521 Make_Block_Statement (Loc,
7522 Identifier => New_Reference_To (Blk_Ent, Loc),
7523 Handled_Statement_Sequence =>
7524 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7525 Has_Created_Identifier => True,
7526 Is_Asynchronous_Call_Block => True);
7528 -- For the VM call Update_Exception instead of Abort_Undefer.
7529 -- See 4jexcept.ads for an explanation.
7531 if VM_Target = No_VM then
7532 if Exception_Mechanism = Back_End_Exceptions then
7534 -- Aborts are not deferred at beginning of exception handlers
7535 -- in ZCX.
7537 Handler_Stmt := Make_Null_Statement (Loc);
7539 else
7540 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7541 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7542 Parameter_Associations => No_List);
7543 end if;
7544 else
7545 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7546 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
7547 Parameter_Associations => New_List (
7548 Make_Function_Call (Loc,
7549 Name => New_Occurrence_Of
7550 (RTE (RE_Current_Target_Exception), Loc))));
7551 end if;
7553 Stmts := New_List (
7554 Make_Block_Statement (Loc,
7555 Handled_Statement_Sequence =>
7556 Make_Handled_Sequence_Of_Statements (Loc,
7557 Statements => New_List (
7558 Make_Implicit_Label_Declaration (Loc,
7559 Defining_Identifier => Blk_Ent,
7560 Label_Construct => Abortable_Block),
7561 Abortable_Block),
7563 -- exception
7565 Exception_Handlers => New_List (
7566 Make_Implicit_Exception_Handler (Loc,
7568 -- when Abort_Signal =>
7569 -- Abort_Undefer.all;
7571 Exception_Choices =>
7572 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
7573 Statements => New_List (Handler_Stmt))))),
7575 -- if not Cancelled (Bnn) then
7576 -- triggered statements
7577 -- end if;
7579 Make_Implicit_If_Statement (N,
7580 Condition => Make_Op_Not (Loc,
7581 Right_Opnd =>
7582 Make_Function_Call (Loc,
7583 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7584 Parameter_Associations => New_List (
7585 New_Occurrence_Of (Cancel_Param, Loc)))),
7586 Then_Statements => Tstats));
7588 -- Asynchronous task entry call
7590 else
7591 if No (Decls) then
7592 Decls := New_List;
7593 end if;
7595 B := Make_Defining_Identifier (Loc, Name_uB);
7597 -- Insert declaration of B in declarations of existing block
7599 Prepend_To (Decls,
7600 Make_Object_Declaration (Loc,
7601 Defining_Identifier => B,
7602 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7604 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7606 -- Insert declaration of C in declarations of existing block
7608 Prepend_To (Decls,
7609 Make_Object_Declaration (Loc,
7610 Defining_Identifier => Cancel_Param,
7611 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7613 -- Remove and save the call to Call_Simple
7615 Stmt := First (Stmts);
7617 -- Skip assignments to temporaries created for in-out parameters.
7618 -- This makes unwarranted assumptions about the shape of the expanded
7619 -- tree for the call, and should be cleaned up ???
7621 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7622 Next (Stmt);
7623 end loop;
7625 Call := Stmt;
7627 -- Create the inner block to protect the abortable part
7629 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7631 Prepend_To (Astats,
7632 Make_Procedure_Call_Statement (Loc,
7633 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7635 Abortable_Block :=
7636 Make_Block_Statement (Loc,
7637 Identifier => New_Reference_To (Blk_Ent, Loc),
7638 Handled_Statement_Sequence =>
7639 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7640 Has_Created_Identifier => True,
7641 Is_Asynchronous_Call_Block => True);
7643 Insert_After (Call,
7644 Make_Block_Statement (Loc,
7645 Handled_Statement_Sequence =>
7646 Make_Handled_Sequence_Of_Statements (Loc,
7647 Statements => New_List (
7648 Make_Implicit_Label_Declaration (Loc,
7649 Defining_Identifier => Blk_Ent,
7650 Label_Construct => Abortable_Block),
7651 Abortable_Block),
7652 Exception_Handlers => Hdle)));
7654 -- Create new call statement
7656 Params := Parameter_Associations (Call);
7658 Append_To (Params,
7659 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7660 Append_To (Params, New_Reference_To (B, Loc));
7662 Rewrite (Call,
7663 Make_Procedure_Call_Statement (Loc,
7664 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7665 Parameter_Associations => Params));
7667 -- Construct statement sequence for new block
7669 Append_To (Stmts,
7670 Make_Implicit_If_Statement (N,
7671 Condition =>
7672 Make_Op_Not (Loc, New_Reference_To (Cancel_Param, Loc)),
7673 Then_Statements => Tstats));
7675 -- Protected the call against abort
7677 Prepend_To (Stmts,
7678 Make_Procedure_Call_Statement (Loc,
7679 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7680 Parameter_Associations => Empty_List));
7681 end if;
7683 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7685 -- The result is the new block
7687 Rewrite (N_Orig,
7688 Make_Block_Statement (Loc,
7689 Declarations => Decls,
7690 Handled_Statement_Sequence =>
7691 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7693 Analyze (N_Orig);
7694 end Expand_N_Asynchronous_Select;
7696 -------------------------------------
7697 -- Expand_N_Conditional_Entry_Call --
7698 -------------------------------------
7700 -- The conditional task entry call is converted to a call to
7701 -- Task_Entry_Call:
7703 -- declare
7704 -- B : Boolean;
7705 -- P : parms := (parm, parm, parm);
7707 -- begin
7708 -- Task_Entry_Call
7709 -- (<acceptor-task>, -- Acceptor
7710 -- <entry-index>, -- E
7711 -- P'Address, -- Uninterpreted_Data
7712 -- Conditional_Call, -- Mode
7713 -- B); -- Rendezvous_Successful
7714 -- parm := P.param;
7715 -- parm := P.param;
7716 -- ...
7717 -- if B then
7718 -- normal-statements
7719 -- else
7720 -- else-statements
7721 -- end if;
7722 -- end;
7724 -- For a description of the use of P and the assignments after the call,
7725 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7726 -- conditional entry call has already been expanded (by the Expand_N_Entry
7727 -- _Call_Statement procedure) as follows:
7729 -- declare
7730 -- P : parms := (parm, parm, parm);
7731 -- begin
7732 -- ... info for in-out parameters
7733 -- Call_Simple (acceptor-task, entry-index, P'Address);
7734 -- parm := P.param;
7735 -- parm := P.param;
7736 -- ...
7737 -- end;
7739 -- so the task at hand is to convert the latter expansion into the former
7741 -- The conditional protected entry call is converted to a call to
7742 -- Protected_Entry_Call:
7744 -- declare
7745 -- P : parms := (parm, parm, parm);
7746 -- Bnn : Communications_Block;
7748 -- begin
7749 -- Protected_Entry_Call
7750 -- (po._object'Access, -- Object
7751 -- <entry index>, -- E
7752 -- P'Address, -- Uninterpreted_Data
7753 -- Conditional_Call, -- Mode
7754 -- Bnn); -- Block
7755 -- parm := P.param;
7756 -- parm := P.param;
7757 -- ...
7758 -- if Cancelled (Bnn) then
7759 -- else-statements
7760 -- else
7761 -- normal-statements
7762 -- end if;
7763 -- end;
7765 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7766 -- into:
7768 -- declare
7769 -- B : Boolean := False;
7770 -- C : Ada.Tags.Prim_Op_Kind;
7771 -- K : Ada.Tags.Tagged_Kind :=
7772 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7773 -- P : Parameters := (Param1 .. ParamN);
7774 -- S : Integer;
7776 -- begin
7777 -- if K = Ada.Tags.TK_Limited_Tagged
7778 -- or else K = Ada.Tags.TK_Tagged
7779 -- then
7780 -- <dispatching-call>;
7781 -- <triggering-statements>
7783 -- else
7784 -- S :=
7785 -- Ada.Tags.Get_Offset_Index
7786 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7788 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7790 -- if C = POK_Protected_Entry
7791 -- or else C = POK_Task_Entry
7792 -- then
7793 -- Param1 := P.Param1;
7794 -- ...
7795 -- ParamN := P.ParamN;
7796 -- end if;
7798 -- if B then
7799 -- if C = POK_Procedure
7800 -- or else C = POK_Protected_Procedure
7801 -- or else C = POK_Task_Procedure
7802 -- then
7803 -- <dispatching-call>;
7804 -- end if;
7806 -- <triggering-statements>
7807 -- else
7808 -- <else-statements>
7809 -- end if;
7810 -- end if;
7811 -- end;
7813 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7814 Loc : constant Source_Ptr := Sloc (N);
7815 Alt : constant Node_Id := Entry_Call_Alternative (N);
7816 Blk : Node_Id := Entry_Call_Statement (Alt);
7818 Actuals : List_Id;
7819 Blk_Typ : Entity_Id;
7820 Call : Node_Id;
7821 Call_Ent : Entity_Id;
7822 Conc_Typ_Stmts : List_Id;
7823 Decl : Node_Id;
7824 Decls : List_Id;
7825 Formals : List_Id;
7826 Lim_Typ_Stmts : List_Id;
7827 N_Stats : List_Id;
7828 Obj : Entity_Id;
7829 Param : Node_Id;
7830 Params : List_Id;
7831 Stmt : Node_Id;
7832 Stmts : List_Id;
7833 Transient_Blk : Node_Id;
7834 Unpack : List_Id;
7836 B : Entity_Id; -- Call status flag
7837 C : Entity_Id; -- Call kind
7838 K : Entity_Id; -- Tagged kind
7839 P : Entity_Id; -- Parameter block
7840 S : Entity_Id; -- Primitive operation slot
7842 begin
7843 Process_Statements_For_Controlled_Objects (N);
7845 if Ada_Version >= Ada_2005
7846 and then Nkind (Blk) = N_Procedure_Call_Statement
7847 then
7848 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7850 Decls := New_List;
7851 Stmts := New_List;
7853 -- Call status flag processing, generate:
7854 -- B : Boolean := False;
7856 B := Build_B (Loc, Decls);
7858 -- Call kind processing, generate:
7859 -- C : Ada.Tags.Prim_Op_Kind;
7861 C := Build_C (Loc, Decls);
7863 -- Tagged kind processing, generate:
7864 -- K : Ada.Tags.Tagged_Kind :=
7865 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7867 K := Build_K (Loc, Decls, Obj);
7869 -- Parameter block processing
7871 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7872 P := Parameter_Block_Pack
7873 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7875 -- Dispatch table slot processing, generate:
7876 -- S : Integer;
7878 S := Build_S (Loc, Decls);
7880 -- Generate:
7881 -- S := Ada.Tags.Get_Offset_Index
7882 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7884 Conc_Typ_Stmts :=
7885 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7887 -- Generate:
7888 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7890 Append_To (Conc_Typ_Stmts,
7891 Make_Procedure_Call_Statement (Loc,
7892 Name =>
7893 New_Reference_To (
7894 Find_Prim_Op (Etype (Etype (Obj)),
7895 Name_uDisp_Conditional_Select),
7896 Loc),
7897 Parameter_Associations =>
7898 New_List (
7899 New_Copy_Tree (Obj), -- <object>
7900 New_Reference_To (S, Loc), -- S
7901 Make_Attribute_Reference (Loc, -- P'Address
7902 Prefix => New_Reference_To (P, Loc),
7903 Attribute_Name => Name_Address),
7904 New_Reference_To (C, Loc), -- C
7905 New_Reference_To (B, Loc)))); -- B
7907 -- Generate:
7908 -- if C = POK_Protected_Entry
7909 -- or else C = POK_Task_Entry
7910 -- then
7911 -- Param1 := P.Param1;
7912 -- ...
7913 -- ParamN := P.ParamN;
7914 -- end if;
7916 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7918 -- Generate the if statement only when the packed parameters need
7919 -- explicit assignments to their corresponding actuals.
7921 if Present (Unpack) then
7922 Append_To (Conc_Typ_Stmts,
7923 Make_Implicit_If_Statement (N,
7924 Condition =>
7925 Make_Or_Else (Loc,
7926 Left_Opnd =>
7927 Make_Op_Eq (Loc,
7928 Left_Opnd =>
7929 New_Reference_To (C, Loc),
7930 Right_Opnd =>
7931 New_Reference_To (RTE (
7932 RE_POK_Protected_Entry), Loc)),
7934 Right_Opnd =>
7935 Make_Op_Eq (Loc,
7936 Left_Opnd =>
7937 New_Reference_To (C, Loc),
7938 Right_Opnd =>
7939 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
7941 Then_Statements => Unpack));
7942 end if;
7944 -- Generate:
7945 -- if B then
7946 -- if C = POK_Procedure
7947 -- or else C = POK_Protected_Procedure
7948 -- or else C = POK_Task_Procedure
7949 -- then
7950 -- <dispatching-call>
7951 -- end if;
7952 -- <normal-statements>
7953 -- else
7954 -- <else-statements>
7955 -- end if;
7957 N_Stats := New_Copy_List_Tree (Statements (Alt));
7959 Prepend_To (N_Stats,
7960 Make_Implicit_If_Statement (N,
7961 Condition =>
7962 Make_Or_Else (Loc,
7963 Left_Opnd =>
7964 Make_Op_Eq (Loc,
7965 Left_Opnd =>
7966 New_Reference_To (C, Loc),
7967 Right_Opnd =>
7968 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
7970 Right_Opnd =>
7971 Make_Or_Else (Loc,
7972 Left_Opnd =>
7973 Make_Op_Eq (Loc,
7974 Left_Opnd =>
7975 New_Reference_To (C, Loc),
7976 Right_Opnd =>
7977 New_Reference_To (RTE (
7978 RE_POK_Protected_Procedure), Loc)),
7980 Right_Opnd =>
7981 Make_Op_Eq (Loc,
7982 Left_Opnd =>
7983 New_Reference_To (C, Loc),
7984 Right_Opnd =>
7985 New_Reference_To (RTE (
7986 RE_POK_Task_Procedure), Loc)))),
7988 Then_Statements =>
7989 New_List (Blk)));
7991 Append_To (Conc_Typ_Stmts,
7992 Make_Implicit_If_Statement (N,
7993 Condition => New_Reference_To (B, Loc),
7994 Then_Statements => N_Stats,
7995 Else_Statements => Else_Statements (N)));
7997 -- Generate:
7998 -- <dispatching-call>;
7999 -- <triggering-statements>
8001 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8002 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8004 -- Generate:
8005 -- if K = Ada.Tags.TK_Limited_Tagged
8006 -- or else K = Ada.Tags.TK_Tagged
8007 -- then
8008 -- Lim_Typ_Stmts
8009 -- else
8010 -- Conc_Typ_Stmts
8011 -- end if;
8013 Append_To (Stmts,
8014 Make_Implicit_If_Statement (N,
8015 Condition => Build_Dispatching_Tag_Check (K, N),
8016 Then_Statements => Lim_Typ_Stmts,
8017 Else_Statements => Conc_Typ_Stmts));
8019 Rewrite (N,
8020 Make_Block_Statement (Loc,
8021 Declarations =>
8022 Decls,
8023 Handled_Statement_Sequence =>
8024 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8026 -- As described above, the entry alternative is transformed into a
8027 -- block that contains the gnulli call, and possibly assignment
8028 -- statements for in-out parameters. The gnulli call may itself be
8029 -- rewritten into a transient block if some unconstrained parameters
8030 -- require it. We need to retrieve the call to complete its parameter
8031 -- list.
8033 else
8034 Transient_Blk :=
8035 First_Real_Statement (Handled_Statement_Sequence (Blk));
8037 if Present (Transient_Blk)
8038 and then Nkind (Transient_Blk) = N_Block_Statement
8039 then
8040 Blk := Transient_Blk;
8041 end if;
8043 Stmts := Statements (Handled_Statement_Sequence (Blk));
8044 Stmt := First (Stmts);
8045 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8046 Next (Stmt);
8047 end loop;
8049 Call := Stmt;
8050 Params := Parameter_Associations (Call);
8052 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8054 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8056 Param := First (Params);
8057 while Present (Param)
8058 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8059 loop
8060 Next (Param);
8061 end loop;
8063 pragma Assert (Present (Param));
8064 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
8066 Analyze (Param);
8068 -- Find the Communication_Block parameter for the call to the
8069 -- Cancelled function.
8071 Decl := First (Declarations (Blk));
8072 while Present (Decl)
8073 and then not Is_RTE (Etype (Object_Definition (Decl)),
8074 RE_Communication_Block)
8075 loop
8076 Next (Decl);
8077 end loop;
8079 -- Add an if statement to execute the else part if the call
8080 -- does not succeed (as indicated by the Cancelled predicate).
8082 Append_To (Stmts,
8083 Make_Implicit_If_Statement (N,
8084 Condition => Make_Function_Call (Loc,
8085 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
8086 Parameter_Associations => New_List (
8087 New_Reference_To (Defining_Identifier (Decl), Loc))),
8088 Then_Statements => Else_Statements (N),
8089 Else_Statements => Statements (Alt)));
8091 else
8092 B := Make_Defining_Identifier (Loc, Name_uB);
8094 -- Insert declaration of B in declarations of existing block
8096 if No (Declarations (Blk)) then
8097 Set_Declarations (Blk, New_List);
8098 end if;
8100 Prepend_To (Declarations (Blk),
8101 Make_Object_Declaration (Loc,
8102 Defining_Identifier => B,
8103 Object_Definition =>
8104 New_Reference_To (Standard_Boolean, Loc)));
8106 -- Create new call statement
8108 Append_To (Params,
8109 New_Reference_To (RTE (RE_Conditional_Call), Loc));
8110 Append_To (Params, New_Reference_To (B, Loc));
8112 Rewrite (Call,
8113 Make_Procedure_Call_Statement (Loc,
8114 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
8115 Parameter_Associations => Params));
8117 -- Construct statement sequence for new block
8119 Append_To (Stmts,
8120 Make_Implicit_If_Statement (N,
8121 Condition => New_Reference_To (B, Loc),
8122 Then_Statements => Statements (Alt),
8123 Else_Statements => Else_Statements (N)));
8124 end if;
8126 -- The result is the new block
8128 Rewrite (N,
8129 Make_Block_Statement (Loc,
8130 Declarations => Declarations (Blk),
8131 Handled_Statement_Sequence =>
8132 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8133 end if;
8135 Analyze (N);
8136 end Expand_N_Conditional_Entry_Call;
8138 ---------------------------------------
8139 -- Expand_N_Delay_Relative_Statement --
8140 ---------------------------------------
8142 -- Delay statement is implemented as a procedure call to Delay_For
8143 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8144 -- simple delays imposed by the use of Protected Objects.
8146 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8147 Loc : constant Source_Ptr := Sloc (N);
8148 begin
8149 Rewrite (N,
8150 Make_Procedure_Call_Statement (Loc,
8151 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
8152 Parameter_Associations => New_List (Expression (N))));
8153 Analyze (N);
8154 end Expand_N_Delay_Relative_Statement;
8156 ------------------------------------
8157 -- Expand_N_Delay_Until_Statement --
8158 ------------------------------------
8160 -- Delay Until statement is implemented as a procedure call to
8161 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8163 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8164 Loc : constant Source_Ptr := Sloc (N);
8165 Typ : Entity_Id;
8167 begin
8168 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8169 Typ := RTE (RO_CA_Delay_Until);
8170 else
8171 Typ := RTE (RO_RT_Delay_Until);
8172 end if;
8174 Rewrite (N,
8175 Make_Procedure_Call_Statement (Loc,
8176 Name => New_Reference_To (Typ, Loc),
8177 Parameter_Associations => New_List (Expression (N))));
8179 Analyze (N);
8180 end Expand_N_Delay_Until_Statement;
8182 -------------------------
8183 -- Expand_N_Entry_Body --
8184 -------------------------
8186 procedure Expand_N_Entry_Body (N : Node_Id) is
8187 begin
8188 -- Associate discriminals with the next protected operation body to be
8189 -- expanded.
8191 if Present (Next_Protected_Operation (N)) then
8192 Set_Discriminals (Parent (Current_Scope));
8193 end if;
8194 end Expand_N_Entry_Body;
8196 -----------------------------------
8197 -- Expand_N_Entry_Call_Statement --
8198 -----------------------------------
8200 -- An entry call is expanded into GNARLI calls to implement a simple entry
8201 -- call (see Build_Simple_Entry_Call).
8203 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8204 Concval : Node_Id;
8205 Ename : Node_Id;
8206 Index : Node_Id;
8208 begin
8209 if No_Run_Time_Mode then
8210 Error_Msg_CRT ("entry call", N);
8211 return;
8212 end if;
8214 -- If this entry call is part of an asynchronous select, don't expand it
8215 -- here; it will be expanded with the select statement. Don't expand
8216 -- timed entry calls either, as they are translated into asynchronous
8217 -- entry calls.
8219 -- ??? This whole approach is questionable; it may be better to go back
8220 -- to allowing the expansion to take place and then attempting to fix it
8221 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8222 -- whether the expanded call is on a task or protected entry.
8224 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8225 or else N /= Triggering_Statement (Parent (N)))
8226 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8227 or else N /= Entry_Call_Statement (Parent (N))
8228 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8229 then
8230 Extract_Entry (N, Concval, Ename, Index);
8231 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8232 end if;
8233 end Expand_N_Entry_Call_Statement;
8235 --------------------------------
8236 -- Expand_N_Entry_Declaration --
8237 --------------------------------
8239 -- If there are parameters, then first, each of the formals is marked by
8240 -- setting Is_Entry_Formal. Next a record type is built which is used to
8241 -- hold the parameter values. The name of this record type is entryP where
8242 -- entry is the name of the entry, with an additional corresponding access
8243 -- type called entryPA. The record type has matching components for each
8244 -- formal (the component names are the same as the formal names). For
8245 -- elementary types, the component type matches the formal type. For
8246 -- composite types, an access type is declared (with the name formalA)
8247 -- which designates the formal type, and the type of the component is this
8248 -- access type. Finally the Entry_Component of each formal is set to
8249 -- reference the corresponding record component.
8251 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8252 Loc : constant Source_Ptr := Sloc (N);
8253 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8254 Components : List_Id;
8255 Formal : Node_Id;
8256 Ftype : Entity_Id;
8257 Last_Decl : Node_Id;
8258 Component : Entity_Id;
8259 Ctype : Entity_Id;
8260 Decl : Node_Id;
8261 Rec_Ent : Entity_Id;
8262 Acc_Ent : Entity_Id;
8264 begin
8265 Formal := First_Formal (Entry_Ent);
8266 Last_Decl := N;
8268 -- Most processing is done only if parameters are present
8270 if Present (Formal) then
8271 Components := New_List;
8273 -- Loop through formals
8275 while Present (Formal) loop
8276 Set_Is_Entry_Formal (Formal);
8277 Component :=
8278 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8279 Set_Entry_Component (Formal, Component);
8280 Set_Entry_Formal (Component, Formal);
8281 Ftype := Etype (Formal);
8283 -- Declare new access type and then append
8285 Ctype := Make_Temporary (Loc, 'A');
8287 Decl :=
8288 Make_Full_Type_Declaration (Loc,
8289 Defining_Identifier => Ctype,
8290 Type_Definition =>
8291 Make_Access_To_Object_Definition (Loc,
8292 All_Present => True,
8293 Constant_Present => Ekind (Formal) = E_In_Parameter,
8294 Subtype_Indication => New_Reference_To (Ftype, Loc)));
8296 Insert_After (Last_Decl, Decl);
8297 Last_Decl := Decl;
8299 Append_To (Components,
8300 Make_Component_Declaration (Loc,
8301 Defining_Identifier => Component,
8302 Component_Definition =>
8303 Make_Component_Definition (Loc,
8304 Aliased_Present => False,
8305 Subtype_Indication => New_Reference_To (Ctype, Loc))));
8307 Next_Formal_With_Extras (Formal);
8308 end loop;
8310 -- Create the Entry_Parameter_Record declaration
8312 Rec_Ent := Make_Temporary (Loc, 'P');
8314 Decl :=
8315 Make_Full_Type_Declaration (Loc,
8316 Defining_Identifier => Rec_Ent,
8317 Type_Definition =>
8318 Make_Record_Definition (Loc,
8319 Component_List =>
8320 Make_Component_List (Loc,
8321 Component_Items => Components)));
8323 Insert_After (Last_Decl, Decl);
8324 Last_Decl := Decl;
8326 -- Construct and link in the corresponding access type
8328 Acc_Ent := Make_Temporary (Loc, 'A');
8330 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8332 Decl :=
8333 Make_Full_Type_Declaration (Loc,
8334 Defining_Identifier => Acc_Ent,
8335 Type_Definition =>
8336 Make_Access_To_Object_Definition (Loc,
8337 All_Present => True,
8338 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
8340 Insert_After (Last_Decl, Decl);
8341 end if;
8342 end Expand_N_Entry_Declaration;
8344 -----------------------------
8345 -- Expand_N_Protected_Body --
8346 -----------------------------
8348 -- Protected bodies are expanded to the completion of the subprograms
8349 -- created for the corresponding protected type. These are a protected and
8350 -- unprotected version of each protected subprogram in the object, a
8351 -- function to calculate each entry barrier, and a procedure to execute the
8352 -- sequence of statements of each protected entry body. For example, for
8353 -- protected type ptype:
8355 -- function entB
8356 -- (O : System.Address;
8357 -- E : Protected_Entry_Index)
8358 -- return Boolean
8359 -- is
8360 -- <discriminant renamings>
8361 -- <private object renamings>
8362 -- begin
8363 -- return <barrier expression>;
8364 -- end entB;
8366 -- procedure pprocN (_object : in out poV;...) is
8367 -- <discriminant renamings>
8368 -- <private object renamings>
8369 -- begin
8370 -- <sequence of statements>
8371 -- end pprocN;
8373 -- procedure pprocP (_object : in out poV;...) is
8374 -- procedure _clean is
8375 -- Pn : Boolean;
8376 -- begin
8377 -- ptypeS (_object, Pn);
8378 -- Unlock (_object._object'Access);
8379 -- Abort_Undefer.all;
8380 -- end _clean;
8382 -- begin
8383 -- Abort_Defer.all;
8384 -- Lock (_object._object'Access);
8385 -- pprocN (_object;...);
8386 -- at end
8387 -- _clean;
8388 -- end pproc;
8390 -- function pfuncN (_object : poV;...) return Return_Type is
8391 -- <discriminant renamings>
8392 -- <private object renamings>
8393 -- begin
8394 -- <sequence of statements>
8395 -- end pfuncN;
8397 -- function pfuncP (_object : poV) return Return_Type is
8398 -- procedure _clean is
8399 -- begin
8400 -- Unlock (_object._object'Access);
8401 -- Abort_Undefer.all;
8402 -- end _clean;
8404 -- begin
8405 -- Abort_Defer.all;
8406 -- Lock (_object._object'Access);
8407 -- return pfuncN (_object);
8409 -- at end
8410 -- _clean;
8411 -- end pfunc;
8413 -- procedure entE
8414 -- (O : System.Address;
8415 -- P : System.Address;
8416 -- E : Protected_Entry_Index)
8417 -- is
8418 -- <discriminant renamings>
8419 -- <private object renamings>
8420 -- type poVP is access poV;
8421 -- _Object : ptVP := ptVP!(O);
8423 -- begin
8424 -- begin
8425 -- <statement sequence>
8426 -- Complete_Entry_Body (_Object._Object);
8427 -- exception
8428 -- when all others =>
8429 -- Exceptional_Complete_Entry_Body (
8430 -- _Object._Object, Get_GNAT_Exception);
8431 -- end;
8432 -- end entE;
8434 -- The type poV is the record created for the protected type to hold
8435 -- the state of the protected object.
8437 procedure Expand_N_Protected_Body (N : Node_Id) is
8438 Loc : constant Source_Ptr := Sloc (N);
8439 Pid : constant Entity_Id := Corresponding_Spec (N);
8441 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8442 -- This flag indicates whether the lock free implementation is active
8444 Current_Node : Node_Id;
8445 Disp_Op_Body : Node_Id;
8446 New_Op_Body : Node_Id;
8447 Op_Body : Node_Id;
8448 Op_Id : Entity_Id;
8450 function Build_Dispatching_Subprogram_Body
8451 (N : Node_Id;
8452 Pid : Node_Id;
8453 Prot_Bod : Node_Id) return Node_Id;
8454 -- Build a dispatching version of the protected subprogram body. The
8455 -- newly generated subprogram contains a call to the original protected
8456 -- body. The following code is generated:
8458 -- function <protected-function-name> (Param1 .. ParamN) return
8459 -- <return-type> is
8460 -- begin
8461 -- return <protected-function-name>P (Param1 .. ParamN);
8462 -- end <protected-function-name>;
8464 -- or
8466 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8467 -- begin
8468 -- <protected-procedure-name>P (Param1 .. ParamN);
8469 -- end <protected-procedure-name>
8471 ---------------------------------------
8472 -- Build_Dispatching_Subprogram_Body --
8473 ---------------------------------------
8475 function Build_Dispatching_Subprogram_Body
8476 (N : Node_Id;
8477 Pid : Node_Id;
8478 Prot_Bod : Node_Id) return Node_Id
8480 Loc : constant Source_Ptr := Sloc (N);
8481 Actuals : List_Id;
8482 Formal : Node_Id;
8483 Spec : Node_Id;
8484 Stmts : List_Id;
8486 begin
8487 -- Generate a specification without a letter suffix in order to
8488 -- override an interface function or procedure.
8490 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8492 -- The formal parameters become the actuals of the protected function
8493 -- or procedure call.
8495 Actuals := New_List;
8496 Formal := First (Parameter_Specifications (Spec));
8497 while Present (Formal) loop
8498 Append_To (Actuals,
8499 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8500 Next (Formal);
8501 end loop;
8503 if Nkind (Spec) = N_Procedure_Specification then
8504 Stmts :=
8505 New_List (
8506 Make_Procedure_Call_Statement (Loc,
8507 Name =>
8508 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8509 Parameter_Associations => Actuals));
8511 else
8512 pragma Assert (Nkind (Spec) = N_Function_Specification);
8514 Stmts :=
8515 New_List (
8516 Make_Simple_Return_Statement (Loc,
8517 Expression =>
8518 Make_Function_Call (Loc,
8519 Name =>
8520 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8521 Parameter_Associations => Actuals)));
8522 end if;
8524 return
8525 Make_Subprogram_Body (Loc,
8526 Declarations => Empty_List,
8527 Specification => Spec,
8528 Handled_Statement_Sequence =>
8529 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8530 end Build_Dispatching_Subprogram_Body;
8532 -- Start of processing for Expand_N_Protected_Body
8534 begin
8535 if No_Run_Time_Mode then
8536 Error_Msg_CRT ("protected body", N);
8537 return;
8538 end if;
8540 -- This is the proper body corresponding to a stub. The declarations
8541 -- must be inserted at the point of the stub, which in turn is in the
8542 -- declarative part of the parent unit.
8544 if Nkind (Parent (N)) = N_Subunit then
8545 Current_Node := Corresponding_Stub (Parent (N));
8546 else
8547 Current_Node := N;
8548 end if;
8550 Op_Body := First (Declarations (N));
8552 -- The protected body is replaced with the bodies of its
8553 -- protected operations, and the declarations for internal objects
8554 -- that may have been created for entry family bounds.
8556 Rewrite (N, Make_Null_Statement (Sloc (N)));
8557 Analyze (N);
8559 while Present (Op_Body) loop
8560 case Nkind (Op_Body) is
8561 when N_Subprogram_Declaration =>
8562 null;
8564 when N_Subprogram_Body =>
8566 -- Do not create bodies for eliminated operations
8568 if not Is_Eliminated (Defining_Entity (Op_Body))
8569 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8570 then
8571 if Lock_Free_Active then
8572 New_Op_Body :=
8573 Build_Lock_Free_Unprotected_Subprogram_Body
8574 (Op_Body, Pid);
8575 else
8576 New_Op_Body :=
8577 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8578 end if;
8580 Insert_After (Current_Node, New_Op_Body);
8581 Current_Node := New_Op_Body;
8582 Analyze (New_Op_Body);
8584 -- Build the corresponding protected operation. It may
8585 -- appear that this is needed only if this is a visible
8586 -- operation of the type, or if it is an interrupt handler,
8587 -- and this was the strategy used previously in GNAT.
8589 -- However, the operation may be exported through a 'Access
8590 -- to an external caller. This is the common idiom in code
8591 -- that uses the Ada 2005 Timing_Events package. As a result
8592 -- we need to produce the protected body for both visible
8593 -- and private operations, as well as operations that only
8594 -- have a body in the source, and for which we create a
8595 -- declaration in the protected body itself.
8597 if Present (Corresponding_Spec (Op_Body)) then
8598 if Lock_Free_Active then
8599 New_Op_Body :=
8600 Build_Lock_Free_Protected_Subprogram_Body
8601 (Op_Body, Pid, Specification (New_Op_Body));
8602 else
8603 New_Op_Body :=
8604 Build_Protected_Subprogram_Body
8605 (Op_Body, Pid, Specification (New_Op_Body));
8606 end if;
8608 Insert_After (Current_Node, New_Op_Body);
8609 Analyze (New_Op_Body);
8611 Current_Node := New_Op_Body;
8613 -- Generate an overriding primitive operation body for
8614 -- this subprogram if the protected type implements an
8615 -- interface.
8617 if Ada_Version >= Ada_2005
8618 and then
8619 Present (Interfaces (Corresponding_Record_Type (Pid)))
8620 then
8621 Disp_Op_Body :=
8622 Build_Dispatching_Subprogram_Body
8623 (Op_Body, Pid, New_Op_Body);
8625 Insert_After (Current_Node, Disp_Op_Body);
8626 Analyze (Disp_Op_Body);
8628 Current_Node := Disp_Op_Body;
8629 end if;
8630 end if;
8631 end if;
8633 when N_Entry_Body =>
8634 Op_Id := Defining_Identifier (Op_Body);
8635 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8637 Insert_After (Current_Node, New_Op_Body);
8638 Current_Node := New_Op_Body;
8639 Analyze (New_Op_Body);
8641 when N_Implicit_Label_Declaration =>
8642 null;
8644 when N_Itype_Reference =>
8645 Insert_After (Current_Node, New_Copy (Op_Body));
8647 when N_Freeze_Entity =>
8648 New_Op_Body := New_Copy (Op_Body);
8650 if Present (Entity (Op_Body))
8651 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8652 then
8653 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8654 end if;
8656 Insert_After (Current_Node, New_Op_Body);
8657 Current_Node := New_Op_Body;
8658 Analyze (New_Op_Body);
8660 when N_Pragma =>
8661 New_Op_Body := New_Copy (Op_Body);
8662 Insert_After (Current_Node, New_Op_Body);
8663 Current_Node := New_Op_Body;
8664 Analyze (New_Op_Body);
8666 when N_Object_Declaration =>
8667 pragma Assert (not Comes_From_Source (Op_Body));
8668 New_Op_Body := New_Copy (Op_Body);
8669 Insert_After (Current_Node, New_Op_Body);
8670 Current_Node := New_Op_Body;
8671 Analyze (New_Op_Body);
8673 when others =>
8674 raise Program_Error;
8676 end case;
8678 Next (Op_Body);
8679 end loop;
8681 -- Finally, create the body of the function that maps an entry index
8682 -- into the corresponding body index, except when there is no entry, or
8683 -- in a Ravenscar-like profile.
8685 if Corresponding_Runtime_Package (Pid) =
8686 System_Tasking_Protected_Objects_Entries
8687 then
8688 New_Op_Body := Build_Find_Body_Index (Pid);
8689 Insert_After (Current_Node, New_Op_Body);
8690 Current_Node := New_Op_Body;
8691 Analyze (New_Op_Body);
8692 end if;
8694 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8695 -- protected body. At this point all wrapper specs have been created,
8696 -- frozen and included in the dispatch table for the protected type.
8698 if Ada_Version >= Ada_2005 then
8699 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8700 end if;
8701 end Expand_N_Protected_Body;
8703 -----------------------------------------
8704 -- Expand_N_Protected_Type_Declaration --
8705 -----------------------------------------
8707 -- First we create a corresponding record type declaration used to
8708 -- represent values of this protected type.
8709 -- The general form of this type declaration is
8711 -- type poV (discriminants) is record
8712 -- _Object : aliased <kind>Protection
8713 -- [(<entry count> [, <handler count>])];
8714 -- [entry_family : array (bounds) of Void;]
8715 -- <private data fields>
8716 -- end record;
8718 -- The discriminants are present only if the corresponding protected type
8719 -- has discriminants, and they exactly mirror the protected type
8720 -- discriminants. The private data fields similarly mirror the private
8721 -- declarations of the protected type.
8723 -- The Object field is always present. It contains RTS specific data used
8724 -- to control the protected object. It is declared as Aliased so that it
8725 -- can be passed as a pointer to the RTS. This allows the protected record
8726 -- to be referenced within RTS data structures. An appropriate Protection
8727 -- type and discriminant are generated.
8729 -- The Service field is present for protected objects with entries. It
8730 -- contains sufficient information to allow the entry service procedure for
8731 -- this object to be called when the object is not known till runtime.
8733 -- One entry_family component is present for each entry family in the
8734 -- task definition (see Expand_N_Task_Type_Declaration).
8736 -- When a protected object is declared, an instance of the protected type
8737 -- value record is created. The elaboration of this declaration creates the
8738 -- correct bounds for the entry families, and also evaluates the priority
8739 -- expression if needed. The initialization routine for the protected type
8740 -- itself then calls Initialize_Protection with appropriate parameters to
8741 -- initialize the value of the Task_Id field. Install_Handlers may be also
8742 -- called if a pragma Attach_Handler applies.
8744 -- Note: this record is passed to the subprograms created by the expansion
8745 -- of protected subprograms and entries. It is an in parameter to protected
8746 -- functions and an in out parameter to procedures and entry bodies. The
8747 -- Entity_Id for this created record type is placed in the
8748 -- Corresponding_Record_Type field of the associated protected type entity.
8750 -- Next we create a procedure specifications for protected subprograms and
8751 -- entry bodies. For each protected subprograms two subprograms are
8752 -- created, an unprotected and a protected version. The unprotected version
8753 -- is called from within other operations of the same protected object.
8755 -- We also build the call to register the procedure if a pragma
8756 -- Interrupt_Handler applies.
8758 -- A single subprogram is created to service all entry bodies; it has an
8759 -- additional boolean out parameter indicating that the previous entry call
8760 -- made by the current task was serviced immediately, i.e. not by proxy.
8761 -- The O parameter contains a pointer to a record object of the type
8762 -- described above. An untyped interface is used here to allow this
8763 -- procedure to be called in places where the type of the object to be
8764 -- serviced is not known. This must be done, for example, when a call that
8765 -- may have been requeued is cancelled; the corresponding object must be
8766 -- serviced, but which object that is not known till runtime.
8768 -- procedure ptypeS
8769 -- (O : System.Address; P : out Boolean);
8770 -- procedure pprocN (_object : in out poV);
8771 -- procedure pproc (_object : in out poV);
8772 -- function pfuncN (_object : poV);
8773 -- function pfunc (_object : poV);
8774 -- ...
8776 -- Note that this must come after the record type declaration, since
8777 -- the specs refer to this type.
8779 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8780 Loc : constant Source_Ptr := Sloc (N);
8781 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8783 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8784 -- This flag indicates whether the lock free implementation is active
8786 Pdef : constant Node_Id := Protected_Definition (N);
8787 -- This contains two lists; one for visible and one for private decls
8789 Rec_Decl : Node_Id;
8790 Cdecls : List_Id;
8791 Discr_Map : constant Elist_Id := New_Elmt_List;
8792 Priv : Node_Id;
8793 New_Priv : Node_Id;
8794 Comp : Node_Id;
8795 Comp_Id : Entity_Id;
8796 Sub : Node_Id;
8797 Current_Node : Node_Id := N;
8798 Entries_Aggr : Node_Id;
8799 Body_Id : Entity_Id;
8800 Body_Arr : Node_Id;
8801 E_Count : Int;
8802 Object_Comp : Node_Id;
8804 procedure Check_Inlining (Subp : Entity_Id);
8805 -- If the original operation has a pragma Inline, propagate the flag
8806 -- to the internal body, for possible inlining later on. The source
8807 -- operation is invisible to the back-end and is never actually called.
8809 procedure Expand_Entry_Declaration (Comp : Entity_Id);
8810 -- Create the subprograms for the barrier and for the body, and append
8811 -- then to Entry_Bodies_Array.
8813 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8814 -- When compiling under the Ravenscar profile, private components must
8815 -- have a static size, or else a protected object will require heap
8816 -- allocation, violating the corresponding restriction. It is preferable
8817 -- to make this check here, because it provides a better error message
8818 -- than the back-end, which refers to the object as a whole.
8820 procedure Register_Handler;
8821 -- For a protected operation that is an interrupt handler, add the
8822 -- freeze action that will register it as such.
8824 --------------------
8825 -- Check_Inlining --
8826 --------------------
8828 procedure Check_Inlining (Subp : Entity_Id) is
8829 begin
8830 if Is_Inlined (Subp) then
8831 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8832 Set_Is_Inlined (Subp, False);
8833 end if;
8834 end Check_Inlining;
8836 ---------------------------------
8837 -- Check_Static_Component_Size --
8838 ---------------------------------
8840 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8841 Typ : constant Entity_Id := Etype (Comp);
8842 C : Entity_Id;
8844 begin
8845 if Is_Scalar_Type (Typ) then
8846 return True;
8848 elsif Is_Array_Type (Typ) then
8849 return Compile_Time_Known_Bounds (Typ);
8851 elsif Is_Record_Type (Typ) then
8852 C := First_Component (Typ);
8853 while Present (C) loop
8854 if not Static_Component_Size (C) then
8855 return False;
8856 end if;
8858 Next_Component (C);
8859 end loop;
8861 return True;
8863 -- Any other type will be checked by the back-end
8865 else
8866 return True;
8867 end if;
8868 end Static_Component_Size;
8870 ------------------------------
8871 -- Expand_Entry_Declaration --
8872 ------------------------------
8874 procedure Expand_Entry_Declaration (Comp : Entity_Id) is
8875 Bdef : Entity_Id;
8876 Edef : Entity_Id;
8878 begin
8879 E_Count := E_Count + 1;
8880 Comp_Id := Defining_Identifier (Comp);
8882 Edef :=
8883 Make_Defining_Identifier (Loc,
8884 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8885 Sub :=
8886 Make_Subprogram_Declaration (Loc,
8887 Specification =>
8888 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
8890 Insert_After (Current_Node, Sub);
8891 Analyze (Sub);
8893 -- Build wrapper procedure for pre/postconditions
8895 Build_PPC_Wrapper (Comp_Id, N);
8897 Set_Protected_Body_Subprogram
8898 (Defining_Identifier (Comp),
8899 Defining_Unit_Name (Specification (Sub)));
8901 Current_Node := Sub;
8903 Bdef :=
8904 Make_Defining_Identifier (Loc,
8905 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
8906 Sub :=
8907 Make_Subprogram_Declaration (Loc,
8908 Specification =>
8909 Build_Barrier_Function_Specification (Loc, Bdef));
8911 Insert_After (Current_Node, Sub);
8912 Analyze (Sub);
8913 Set_Protected_Body_Subprogram (Bdef, Bdef);
8914 Set_Barrier_Function (Comp_Id, Bdef);
8915 Set_Scope (Bdef, Scope (Comp_Id));
8916 Current_Node := Sub;
8918 -- Collect pointers to the protected subprogram and the barrier
8919 -- of the current entry, for insertion into Entry_Bodies_Array.
8921 Append_To (Expressions (Entries_Aggr),
8922 Make_Aggregate (Loc,
8923 Expressions => New_List (
8924 Make_Attribute_Reference (Loc,
8925 Prefix => New_Reference_To (Bdef, Loc),
8926 Attribute_Name => Name_Unrestricted_Access),
8927 Make_Attribute_Reference (Loc,
8928 Prefix => New_Reference_To (Edef, Loc),
8929 Attribute_Name => Name_Unrestricted_Access))));
8930 end Expand_Entry_Declaration;
8932 ----------------------
8933 -- Register_Handler --
8934 ----------------------
8936 procedure Register_Handler is
8938 -- All semantic checks already done in Sem_Prag
8940 Prot_Proc : constant Entity_Id :=
8941 Defining_Unit_Name (Specification (Current_Node));
8943 Proc_Address : constant Node_Id :=
8944 Make_Attribute_Reference (Loc,
8945 Prefix =>
8946 New_Reference_To (Prot_Proc, Loc),
8947 Attribute_Name => Name_Address);
8949 RTS_Call : constant Entity_Id :=
8950 Make_Procedure_Call_Statement (Loc,
8951 Name =>
8952 New_Reference_To
8953 (RTE (RE_Register_Interrupt_Handler), Loc),
8954 Parameter_Associations => New_List (Proc_Address));
8955 begin
8956 Append_Freeze_Action (Prot_Proc, RTS_Call);
8957 end Register_Handler;
8959 -- Start of processing for Expand_N_Protected_Type_Declaration
8961 begin
8962 if Present (Corresponding_Record_Type (Prot_Typ)) then
8963 return;
8964 else
8965 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8966 end if;
8968 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8970 Qualify_Entity_Names (N);
8972 -- If the type has discriminants, their occurrences in the declaration
8973 -- have been replaced by the corresponding discriminals. For components
8974 -- that are constrained by discriminants, their homologues in the
8975 -- corresponding record type must refer to the discriminants of that
8976 -- record, so we must apply a new renaming to subtypes_indications:
8978 -- protected discriminant => discriminal => record discriminant
8980 -- This replacement is not applied to default expressions, for which
8981 -- the discriminal is correct.
8983 if Has_Discriminants (Prot_Typ) then
8984 declare
8985 Disc : Entity_Id;
8986 Decl : Node_Id;
8988 begin
8989 Disc := First_Discriminant (Prot_Typ);
8990 Decl := First (Discriminant_Specifications (Rec_Decl));
8991 while Present (Disc) loop
8992 Append_Elmt (Discriminal (Disc), Discr_Map);
8993 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8994 Next_Discriminant (Disc);
8995 Next (Decl);
8996 end loop;
8997 end;
8998 end if;
9000 -- Fill in the component declarations
9002 -- Add components for entry families. For each entry family, create an
9003 -- anonymous type declaration with the same size, and analyze the type.
9005 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9007 pragma Assert (Present (Pdef));
9009 -- Add private field components
9011 if Present (Private_Declarations (Pdef)) then
9012 Priv := First (Private_Declarations (Pdef));
9013 while Present (Priv) loop
9014 if Nkind (Priv) = N_Component_Declaration then
9015 if not Static_Component_Size (Defining_Identifier (Priv)) then
9017 -- When compiling for a restricted profile, the private
9018 -- components must have a static size. If not, this is an
9019 -- error for a single protected declaration, and rates a
9020 -- warning on a protected type declaration.
9022 if not Comes_From_Source (Prot_Typ) then
9023 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9025 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9026 Error_Msg_N ("component has non-static size??", Priv);
9027 Error_Msg_NE
9028 ("\creation of protected object of type& will violate"
9029 & " restriction No_Implicit_Heap_Allocations??",
9030 Priv, Prot_Typ);
9031 end if;
9032 end if;
9034 -- The component definition consists of a subtype indication,
9035 -- or (in Ada 2005) an access definition. Make a copy of the
9036 -- proper definition.
9038 declare
9039 Old_Comp : constant Node_Id := Component_Definition (Priv);
9040 Oent : constant Entity_Id := Defining_Identifier (Priv);
9041 New_Comp : Node_Id;
9042 Nent : constant Entity_Id :=
9043 Make_Defining_Identifier (Sloc (Oent),
9044 Chars => Chars (Oent));
9046 begin
9047 if Present (Subtype_Indication (Old_Comp)) then
9048 New_Comp :=
9049 Make_Component_Definition (Sloc (Oent),
9050 Aliased_Present => False,
9051 Subtype_Indication =>
9052 New_Copy_Tree (Subtype_Indication (Old_Comp),
9053 Discr_Map));
9054 else
9055 New_Comp :=
9056 Make_Component_Definition (Sloc (Oent),
9057 Aliased_Present => False,
9058 Access_Definition =>
9059 New_Copy_Tree (Access_Definition (Old_Comp),
9060 Discr_Map));
9061 end if;
9063 New_Priv :=
9064 Make_Component_Declaration (Loc,
9065 Defining_Identifier => Nent,
9066 Component_Definition => New_Comp,
9067 Expression => Expression (Priv));
9069 Set_Has_Per_Object_Constraint (Nent,
9070 Has_Per_Object_Constraint (Oent));
9072 Append_To (Cdecls, New_Priv);
9073 end;
9075 elsif Nkind (Priv) = N_Subprogram_Declaration then
9077 -- Make the unprotected version of the subprogram available
9078 -- for expansion of intra object calls. There is need for
9079 -- a protected version only if the subprogram is an interrupt
9080 -- handler, otherwise this operation can only be called from
9081 -- within the body.
9083 Sub :=
9084 Make_Subprogram_Declaration (Loc,
9085 Specification =>
9086 Build_Protected_Sub_Specification
9087 (Priv, Prot_Typ, Unprotected_Mode));
9089 Insert_After (Current_Node, Sub);
9090 Analyze (Sub);
9092 Set_Protected_Body_Subprogram
9093 (Defining_Unit_Name (Specification (Priv)),
9094 Defining_Unit_Name (Specification (Sub)));
9095 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9096 Current_Node := Sub;
9098 Sub :=
9099 Make_Subprogram_Declaration (Loc,
9100 Specification =>
9101 Build_Protected_Sub_Specification
9102 (Priv, Prot_Typ, Protected_Mode));
9104 Insert_After (Current_Node, Sub);
9105 Analyze (Sub);
9106 Current_Node := Sub;
9108 if Is_Interrupt_Handler
9109 (Defining_Unit_Name (Specification (Priv)))
9110 then
9111 if not Restricted_Profile then
9112 Register_Handler;
9113 end if;
9114 end if;
9115 end if;
9117 Next (Priv);
9118 end loop;
9119 end if;
9121 -- Except for the lock-free implementation, append the _Object field
9122 -- with the right type to the component list. We need to compute the
9123 -- number of entries, and in some cases the number of Attach_Handler
9124 -- pragmas.
9126 if not Lock_Free_Active then
9127 declare
9128 Ritem : Node_Id;
9129 Num_Attach_Handler : Int := 0;
9130 Protection_Subtype : Node_Id;
9131 Entry_Count_Expr : constant Node_Id :=
9132 Build_Entry_Count_Expression
9133 (Prot_Typ, Cdecls, Loc);
9135 begin
9136 if Has_Attach_Handler (Prot_Typ) then
9137 Ritem := First_Rep_Item (Prot_Typ);
9138 while Present (Ritem) loop
9139 if Nkind (Ritem) = N_Pragma
9140 and then Pragma_Name (Ritem) = Name_Attach_Handler
9141 then
9142 Num_Attach_Handler := Num_Attach_Handler + 1;
9143 end if;
9145 Next_Rep_Item (Ritem);
9146 end loop;
9147 end if;
9149 -- Determine the proper protection type. There are two special
9150 -- cases: 1) when the protected type has dynamic interrupt
9151 -- handlers, and 2) when it has static handlers and we use a
9152 -- restricted profile.
9154 if Has_Attach_Handler (Prot_Typ)
9155 and then not Restricted_Profile
9156 then
9157 Protection_Subtype :=
9158 Make_Subtype_Indication (Loc,
9159 Subtype_Mark =>
9160 New_Reference_To
9161 (RTE (RE_Static_Interrupt_Protection), Loc),
9162 Constraint =>
9163 Make_Index_Or_Discriminant_Constraint (Loc,
9164 Constraints => New_List (
9165 Entry_Count_Expr,
9166 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9168 elsif Has_Interrupt_Handler (Prot_Typ)
9169 and then not Restriction_Active (No_Dynamic_Attachment)
9170 then
9171 Protection_Subtype :=
9172 Make_Subtype_Indication (Loc,
9173 Subtype_Mark =>
9174 New_Reference_To
9175 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9176 Constraint =>
9177 Make_Index_Or_Discriminant_Constraint (Loc,
9178 Constraints => New_List (Entry_Count_Expr)));
9180 else
9181 case Corresponding_Runtime_Package (Prot_Typ) is
9182 when System_Tasking_Protected_Objects_Entries =>
9183 Protection_Subtype :=
9184 Make_Subtype_Indication (Loc,
9185 Subtype_Mark =>
9186 New_Reference_To
9187 (RTE (RE_Protection_Entries), Loc),
9188 Constraint =>
9189 Make_Index_Or_Discriminant_Constraint (Loc,
9190 Constraints => New_List (Entry_Count_Expr)));
9192 when System_Tasking_Protected_Objects_Single_Entry =>
9193 Protection_Subtype :=
9194 New_Reference_To (RTE (RE_Protection_Entry), Loc);
9196 when System_Tasking_Protected_Objects =>
9197 Protection_Subtype :=
9198 New_Reference_To (RTE (RE_Protection), Loc);
9200 when others =>
9201 raise Program_Error;
9202 end case;
9203 end if;
9205 Object_Comp :=
9206 Make_Component_Declaration (Loc,
9207 Defining_Identifier =>
9208 Make_Defining_Identifier (Loc, Name_uObject),
9209 Component_Definition =>
9210 Make_Component_Definition (Loc,
9211 Aliased_Present => True,
9212 Subtype_Indication => Protection_Subtype));
9213 end;
9215 -- Put the _Object component after the private component so that it
9216 -- be finalized early as required by 9.4 (20)
9218 Append_To (Cdecls, Object_Comp);
9219 end if;
9221 Insert_After (Current_Node, Rec_Decl);
9222 Current_Node := Rec_Decl;
9224 -- Analyze the record declaration immediately after construction,
9225 -- because the initialization procedure is needed for single object
9226 -- declarations before the next entity is analyzed (the freeze call
9227 -- that generates this initialization procedure is found below).
9229 Analyze (Rec_Decl, Suppress => All_Checks);
9231 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9232 -- the corresponding record is frozen. If any wrappers are generated,
9233 -- Current_Node is updated accordingly.
9235 if Ada_Version >= Ada_2005 then
9236 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9237 end if;
9239 -- Collect pointers to entry bodies and their barriers, to be placed
9240 -- in the Entry_Bodies_Array for the type. For each entry/family we
9241 -- add an expression to the aggregate which is the initial value of
9242 -- this array. The array is declared after all protected subprograms.
9244 if Has_Entries (Prot_Typ) then
9245 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9246 else
9247 Entries_Aggr := Empty;
9248 end if;
9250 -- Build two new procedure specifications for each protected subprogram;
9251 -- one to call from outside the object and one to call from inside.
9252 -- Build a barrier function and an entry body action procedure
9253 -- specification for each protected entry. Initialize the entry body
9254 -- array. If subprogram is flagged as eliminated, do not generate any
9255 -- internal operations.
9257 E_Count := 0;
9258 Comp := First (Visible_Declarations (Pdef));
9259 while Present (Comp) loop
9260 if Nkind (Comp) = N_Subprogram_Declaration then
9261 Sub :=
9262 Make_Subprogram_Declaration (Loc,
9263 Specification =>
9264 Build_Protected_Sub_Specification
9265 (Comp, Prot_Typ, Unprotected_Mode));
9267 Insert_After (Current_Node, Sub);
9268 Analyze (Sub);
9270 Set_Protected_Body_Subprogram
9271 (Defining_Unit_Name (Specification (Comp)),
9272 Defining_Unit_Name (Specification (Sub)));
9273 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9275 -- Make the protected version of the subprogram available for
9276 -- expansion of external calls.
9278 Current_Node := Sub;
9280 Sub :=
9281 Make_Subprogram_Declaration (Loc,
9282 Specification =>
9283 Build_Protected_Sub_Specification
9284 (Comp, Prot_Typ, Protected_Mode));
9286 Insert_After (Current_Node, Sub);
9287 Analyze (Sub);
9289 Current_Node := Sub;
9291 -- Generate an overriding primitive operation specification for
9292 -- this subprogram if the protected type implements an interface.
9294 if Ada_Version >= Ada_2005
9295 and then
9296 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9297 then
9298 Sub :=
9299 Make_Subprogram_Declaration (Loc,
9300 Specification =>
9301 Build_Protected_Sub_Specification
9302 (Comp, Prot_Typ, Dispatching_Mode));
9304 Insert_After (Current_Node, Sub);
9305 Analyze (Sub);
9307 Current_Node := Sub;
9308 end if;
9310 -- If a pragma Interrupt_Handler applies, build and add a call to
9311 -- Register_Interrupt_Handler to the freezing actions of the
9312 -- protected version (Current_Node) of the subprogram:
9314 -- system.interrupts.register_interrupt_handler
9315 -- (prot_procP'address);
9317 if not Restricted_Profile
9318 and then Is_Interrupt_Handler
9319 (Defining_Unit_Name (Specification (Comp)))
9320 then
9321 Register_Handler;
9322 end if;
9324 elsif Nkind (Comp) = N_Entry_Declaration then
9326 Expand_Entry_Declaration (Comp);
9328 end if;
9330 Next (Comp);
9331 end loop;
9333 -- If there are some private entry declarations, expand it as if they
9334 -- were visible entries.
9336 if Present (Private_Declarations (Pdef)) then
9337 Comp := First (Private_Declarations (Pdef));
9338 while Present (Comp) loop
9339 if Nkind (Comp) = N_Entry_Declaration then
9340 Expand_Entry_Declaration (Comp);
9341 end if;
9343 Next (Comp);
9344 end loop;
9345 end if;
9347 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9348 -- all protected subprograms have been collected.
9350 if Has_Entries (Prot_Typ) then
9351 Body_Id :=
9352 Make_Defining_Identifier (Sloc (Prot_Typ),
9353 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9355 case Corresponding_Runtime_Package (Prot_Typ) is
9356 when System_Tasking_Protected_Objects_Entries =>
9357 Body_Arr := Make_Object_Declaration (Loc,
9358 Defining_Identifier => Body_Id,
9359 Aliased_Present => True,
9360 Object_Definition =>
9361 Make_Subtype_Indication (Loc,
9362 Subtype_Mark => New_Reference_To (
9363 RTE (RE_Protected_Entry_Body_Array), Loc),
9364 Constraint =>
9365 Make_Index_Or_Discriminant_Constraint (Loc,
9366 Constraints => New_List (
9367 Make_Range (Loc,
9368 Make_Integer_Literal (Loc, 1),
9369 Make_Integer_Literal (Loc, E_Count))))),
9370 Expression => Entries_Aggr);
9372 when System_Tasking_Protected_Objects_Single_Entry =>
9373 Body_Arr := Make_Object_Declaration (Loc,
9374 Defining_Identifier => Body_Id,
9375 Aliased_Present => True,
9376 Object_Definition => New_Reference_To
9377 (RTE (RE_Entry_Body), Loc),
9378 Expression => Remove_Head (Expressions (Entries_Aggr)));
9380 when others =>
9381 raise Program_Error;
9382 end case;
9384 -- A pointer to this array will be placed in the corresponding record
9385 -- by its initialization procedure so this needs to be analyzed here.
9387 Insert_After (Current_Node, Body_Arr);
9388 Current_Node := Body_Arr;
9389 Analyze (Body_Arr);
9391 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9393 -- Finally, build the function that maps an entry index into the
9394 -- corresponding body. A pointer to this function is placed in each
9395 -- object of the type. Except for a ravenscar-like profile (no abort,
9396 -- no entry queue, 1 entry)
9398 if Corresponding_Runtime_Package (Prot_Typ) =
9399 System_Tasking_Protected_Objects_Entries
9400 then
9401 Sub :=
9402 Make_Subprogram_Declaration (Loc,
9403 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9404 Insert_After (Current_Node, Sub);
9405 Analyze (Sub);
9406 end if;
9407 end if;
9408 end Expand_N_Protected_Type_Declaration;
9410 --------------------------------
9411 -- Expand_N_Requeue_Statement --
9412 --------------------------------
9414 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9415 -- operations, depending on the source and destination (task or protected
9416 -- object). A dispatching requeue statement is expanded into a call to the
9417 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9418 -- jump around the remainder of processing for the original entry and, if
9419 -- the destination is (different) protected object, to attempt to service
9420 -- it. The following illustrates the various cases:
9422 -- procedure entE
9423 -- (O : System.Address;
9424 -- P : System.Address;
9425 -- E : Protected_Entry_Index)
9426 -- is
9427 -- <discriminant renamings>
9428 -- <private object renamings>
9429 -- type poVP is access poV;
9430 -- _object : ptVP := ptVP!(O);
9432 -- begin
9433 -- begin
9434 -- <start of statement sequence for entry>
9436 -- -- Requeue from one protected entry body to another protected
9437 -- -- entry.
9439 -- Requeue_Protected_Entry (
9440 -- _object._object'Access,
9441 -- new._object'Access,
9442 -- E,
9443 -- Abort_Present);
9444 -- return;
9446 -- <some more of the statement sequence for entry>
9448 -- -- Requeue from an entry body to a task entry
9450 -- Requeue_Protected_To_Task_Entry (
9451 -- New._task_id,
9452 -- E,
9453 -- Abort_Present);
9454 -- return;
9456 -- <rest of statement sequence for entry>
9457 -- Complete_Entry_Body (_object._object);
9459 -- exception
9460 -- when all others =>
9461 -- Exceptional_Complete_Entry_Body (
9462 -- _object._object, Get_GNAT_Exception);
9463 -- end;
9464 -- end entE;
9466 -- Requeue of a task entry call to a task entry
9468 -- Accept_Call (E, Ann);
9469 -- <start of statement sequence for accept statement>
9470 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9471 -- goto Lnn;
9472 -- <rest of statement sequence for accept statement>
9473 -- <<Lnn>>
9474 -- Complete_Rendezvous;
9476 -- exception
9477 -- when all others =>
9478 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9480 -- Requeue of a task entry call to a protected entry
9482 -- Accept_Call (E, Ann);
9483 -- <start of statement sequence for accept statement>
9484 -- Requeue_Task_To_Protected_Entry (
9485 -- new._object'Access,
9486 -- E,
9487 -- Abort_Present);
9488 -- newS (new, Pnn);
9489 -- goto Lnn;
9490 -- <rest of statement sequence for accept statement>
9491 -- <<Lnn>>
9492 -- Complete_Rendezvous;
9494 -- exception
9495 -- when all others =>
9496 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9498 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9499 -- marked by pragma Implemented (XXX, By_Entry).
9501 -- The requeue is inside a protected entry:
9503 -- procedure entE
9504 -- (O : System.Address;
9505 -- P : System.Address;
9506 -- E : Protected_Entry_Index)
9507 -- is
9508 -- <discriminant renamings>
9509 -- <private object renamings>
9510 -- type poVP is access poV;
9511 -- _object : ptVP := ptVP!(O);
9513 -- begin
9514 -- begin
9515 -- <start of statement sequence for entry>
9517 -- _Disp_Requeue
9518 -- (<interface class-wide object>,
9519 -- True,
9520 -- _object'Address,
9521 -- Ada.Tags.Get_Offset_Index
9522 -- (Tag (_object),
9523 -- <interface dispatch table index of target entry>),
9524 -- Abort_Present);
9525 -- return;
9527 -- <rest of statement sequence for entry>
9528 -- Complete_Entry_Body (_object._object);
9530 -- exception
9531 -- when all others =>
9532 -- Exceptional_Complete_Entry_Body (
9533 -- _object._object, Get_GNAT_Exception);
9534 -- end;
9535 -- end entE;
9537 -- The requeue is inside a task entry:
9539 -- Accept_Call (E, Ann);
9540 -- <start of statement sequence for accept statement>
9541 -- _Disp_Requeue
9542 -- (<interface class-wide object>,
9543 -- False,
9544 -- null,
9545 -- Ada.Tags.Get_Offset_Index
9546 -- (Tag (_object),
9547 -- <interface dispatch table index of target entrt>),
9548 -- Abort_Present);
9549 -- newS (new, Pnn);
9550 -- goto Lnn;
9551 -- <rest of statement sequence for accept statement>
9552 -- <<Lnn>>
9553 -- Complete_Rendezvous;
9555 -- exception
9556 -- when all others =>
9557 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9559 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9560 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9561 -- statement is replaced by a dispatching call with actual parameters taken
9562 -- from the inner-most accept statement or entry body.
9564 -- Target.Primitive (Param1, ..., ParamN);
9566 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9567 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9568 -- at all.
9570 -- declare
9571 -- S : constant Offset_Index :=
9572 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9573 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9575 -- begin
9576 -- if C = POK_Protected_Entry
9577 -- or else C = POK_Task_Entry
9578 -- then
9579 -- <statements for dispatching requeue>
9581 -- elsif C = POK_Protected_Procedure then
9582 -- <dispatching call equivalent>
9584 -- else
9585 -- raise Program_Error;
9586 -- end if;
9587 -- end;
9589 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9590 Loc : constant Source_Ptr := Sloc (N);
9591 Conc_Typ : Entity_Id;
9592 Concval : Node_Id;
9593 Ename : Node_Id;
9594 Index : Node_Id;
9595 Old_Typ : Entity_Id;
9597 function Build_Dispatching_Call_Equivalent return Node_Id;
9598 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9599 -- the form Concval.Ename. It is statically known that Ename is allowed
9600 -- to be implemented by a protected procedure. Create a dispatching call
9601 -- equivalent of Concval.Ename taking the actual parameters from the
9602 -- inner-most accept statement or entry body.
9604 function Build_Dispatching_Requeue return Node_Id;
9605 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9606 -- the form Concval.Ename. It is statically known that Ename is allowed
9607 -- to be implemented by a protected or a task entry. Create a call to
9608 -- primitive _Disp_Requeue which handles the low-level actions.
9610 function Build_Dispatching_Requeue_To_Any return Node_Id;
9611 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9612 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9613 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9614 -- determines at runtime whether Ename denotes an entry or a procedure
9615 -- and perform the appropriate kind of dispatching select.
9617 function Build_Normal_Requeue return Node_Id;
9618 -- N denotes a non-dispatching requeue statement to either a task or a
9619 -- protected entry. Build the appropriate runtime call to perform the
9620 -- action.
9622 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9623 -- For a protected entry, create a return statement to skip the rest of
9624 -- the entry body. Otherwise, create a goto statement to skip the rest
9625 -- of a task accept statement. The lookup for the enclosing entry body
9626 -- or accept statement starts from Search.
9628 ---------------------------------------
9629 -- Build_Dispatching_Call_Equivalent --
9630 ---------------------------------------
9632 function Build_Dispatching_Call_Equivalent return Node_Id is
9633 Call_Ent : constant Entity_Id := Entity (Ename);
9634 Obj : constant Node_Id := Original_Node (Concval);
9635 Acc_Ent : Node_Id;
9636 Actuals : List_Id;
9637 Formal : Node_Id;
9638 Formals : List_Id;
9640 begin
9641 -- Climb the parent chain looking for the inner-most entry body or
9642 -- accept statement.
9644 Acc_Ent := N;
9645 while Present (Acc_Ent)
9646 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9647 N_Entry_Body)
9648 loop
9649 Acc_Ent := Parent (Acc_Ent);
9650 end loop;
9652 -- A requeue statement should be housed inside an entry body or an
9653 -- accept statement at some level. If this is not the case, then the
9654 -- tree is malformed.
9656 pragma Assert (Present (Acc_Ent));
9658 -- Recover the list of formal parameters
9660 if Nkind (Acc_Ent) = N_Entry_Body then
9661 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9662 end if;
9664 Formals := Parameter_Specifications (Acc_Ent);
9666 -- Create the actual parameters for the dispatching call. These are
9667 -- simply copies of the entry body or accept statement formals in the
9668 -- same order as they appear.
9670 Actuals := No_List;
9672 if Present (Formals) then
9673 Actuals := New_List;
9674 Formal := First (Formals);
9675 while Present (Formal) loop
9676 Append_To (Actuals,
9677 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9678 Next (Formal);
9679 end loop;
9680 end if;
9682 -- Generate:
9683 -- Obj.Call_Ent (Actuals);
9685 return
9686 Make_Procedure_Call_Statement (Loc,
9687 Name =>
9688 Make_Selected_Component (Loc,
9689 Prefix => Make_Identifier (Loc, Chars (Obj)),
9690 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9692 Parameter_Associations => Actuals);
9693 end Build_Dispatching_Call_Equivalent;
9695 -------------------------------
9696 -- Build_Dispatching_Requeue --
9697 -------------------------------
9699 function Build_Dispatching_Requeue return Node_Id is
9700 Params : constant List_Id := New_List;
9702 begin
9703 -- Process the "with abort" parameter
9705 Prepend_To (Params,
9706 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9708 -- Process the entry wrapper's position in the primary dispatch
9709 -- table parameter. Generate:
9711 -- Ada.Tags.Get_Entry_Index
9712 -- (T => To_Tag_Ptr (Obj'Address).all,
9713 -- Position =>
9714 -- Ada.Tags.Get_Offset_Index
9715 -- (Ada.Tags.Tag (Concval),
9716 -- <interface dispatch table position of Ename>));
9718 -- Note that Obj'Address is recursively expanded into a call to
9719 -- Base_Address (Obj).
9721 if Tagged_Type_Expansion then
9722 Prepend_To (Params,
9723 Make_Function_Call (Loc,
9724 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9725 Parameter_Associations => New_List (
9727 Make_Explicit_Dereference (Loc,
9728 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9729 Make_Attribute_Reference (Loc,
9730 Prefix => New_Copy_Tree (Concval),
9731 Attribute_Name => Name_Address))),
9733 Make_Function_Call (Loc,
9734 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9735 Parameter_Associations => New_List (
9736 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9737 Make_Integer_Literal (Loc,
9738 DT_Position (Entity (Ename))))))));
9740 -- VM targets
9742 else
9743 Prepend_To (Params,
9744 Make_Function_Call (Loc,
9745 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9746 Parameter_Associations => New_List (
9748 Make_Attribute_Reference (Loc,
9749 Prefix => Concval,
9750 Attribute_Name => Name_Tag),
9752 Make_Function_Call (Loc,
9753 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9755 Parameter_Associations => New_List (
9757 -- Obj_Tag
9759 Make_Attribute_Reference (Loc,
9760 Prefix => Concval,
9761 Attribute_Name => Name_Tag),
9763 -- Tag_Typ
9765 Make_Attribute_Reference (Loc,
9766 Prefix => New_Reference_To (Etype (Concval), Loc),
9767 Attribute_Name => Name_Tag),
9769 -- Position
9771 Make_Integer_Literal (Loc,
9772 DT_Position (Entity (Ename))))))));
9773 end if;
9775 -- Specific actuals for protected to XXX requeue
9777 if Is_Protected_Type (Old_Typ) then
9778 Prepend_To (Params,
9779 Make_Attribute_Reference (Loc, -- _object'Address
9780 Prefix =>
9781 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9782 Attribute_Name => Name_Address));
9784 Prepend_To (Params, -- True
9785 New_Reference_To (Standard_True, Loc));
9787 -- Specific actuals for task to XXX requeue
9789 else
9790 pragma Assert (Is_Task_Type (Old_Typ));
9792 Prepend_To (Params, -- null
9793 New_Reference_To (RTE (RE_Null_Address), Loc));
9795 Prepend_To (Params, -- False
9796 New_Reference_To (Standard_False, Loc));
9797 end if;
9799 -- Add the object parameter
9801 Prepend_To (Params, New_Copy_Tree (Concval));
9803 -- Generate:
9804 -- _Disp_Requeue (<Params>);
9806 -- Find entity for Disp_Requeue operation, which belongs to
9807 -- the type and may not be directly visible.
9809 declare
9810 Elmt : Elmt_Id;
9811 Op : Entity_Id;
9813 begin
9814 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9815 while Present (Elmt) loop
9816 Op := Node (Elmt);
9817 exit when Chars (Op) = Name_uDisp_Requeue;
9818 Next_Elmt (Elmt);
9819 end loop;
9821 return
9822 Make_Procedure_Call_Statement (Loc,
9823 Name => New_Occurrence_Of (Op, Loc),
9824 Parameter_Associations => Params);
9825 end;
9826 end Build_Dispatching_Requeue;
9828 --------------------------------------
9829 -- Build_Dispatching_Requeue_To_Any --
9830 --------------------------------------
9832 function Build_Dispatching_Requeue_To_Any return Node_Id is
9833 Call_Ent : constant Entity_Id := Entity (Ename);
9834 Obj : constant Node_Id := Original_Node (Concval);
9835 Skip : constant Node_Id := Build_Skip_Statement (N);
9836 C : Entity_Id;
9837 Decls : List_Id;
9838 S : Entity_Id;
9839 Stmts : List_Id;
9841 begin
9842 Decls := New_List;
9843 Stmts := New_List;
9845 -- Dispatch table slot processing, generate:
9846 -- S : Integer;
9848 S := Build_S (Loc, Decls);
9850 -- Call kind processing, generate:
9851 -- C : Ada.Tags.Prim_Op_Kind;
9853 C := Build_C (Loc, Decls);
9855 -- Generate:
9856 -- S := Ada.Tags.Get_Offset_Index
9857 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9859 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9861 -- Generate:
9862 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9864 Append_To (Stmts,
9865 Make_Procedure_Call_Statement (Loc,
9866 Name =>
9867 New_Reference_To (
9868 Find_Prim_Op (Etype (Etype (Obj)),
9869 Name_uDisp_Get_Prim_Op_Kind),
9870 Loc),
9871 Parameter_Associations => New_List (
9872 New_Copy_Tree (Obj),
9873 New_Reference_To (S, Loc),
9874 New_Reference_To (C, Loc))));
9876 Append_To (Stmts,
9878 -- if C = POK_Protected_Entry
9879 -- or else C = POK_Task_Entry
9880 -- then
9882 Make_Implicit_If_Statement (N,
9883 Condition =>
9884 Make_Op_Or (Loc,
9885 Left_Opnd =>
9886 Make_Op_Eq (Loc,
9887 Left_Opnd =>
9888 New_Reference_To (C, Loc),
9889 Right_Opnd =>
9890 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
9892 Right_Opnd =>
9893 Make_Op_Eq (Loc,
9894 Left_Opnd =>
9895 New_Reference_To (C, Loc),
9896 Right_Opnd =>
9897 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
9899 -- Dispatching requeue equivalent
9901 Then_Statements => New_List (
9902 Build_Dispatching_Requeue,
9903 Skip),
9905 -- elsif C = POK_Protected_Procedure then
9907 Elsif_Parts => New_List (
9908 Make_Elsif_Part (Loc,
9909 Condition =>
9910 Make_Op_Eq (Loc,
9911 Left_Opnd =>
9912 New_Reference_To (C, Loc),
9913 Right_Opnd =>
9914 New_Reference_To (
9915 RTE (RE_POK_Protected_Procedure), Loc)),
9917 -- Dispatching call equivalent
9919 Then_Statements => New_List (
9920 Build_Dispatching_Call_Equivalent))),
9922 -- else
9923 -- raise Program_Error;
9924 -- end if;
9926 Else_Statements => New_List (
9927 Make_Raise_Program_Error (Loc,
9928 Reason => PE_Explicit_Raise))));
9930 -- Wrap everything into a block
9932 return
9933 Make_Block_Statement (Loc,
9934 Declarations => Decls,
9935 Handled_Statement_Sequence =>
9936 Make_Handled_Sequence_Of_Statements (Loc,
9937 Statements => Stmts));
9938 end Build_Dispatching_Requeue_To_Any;
9940 --------------------------
9941 -- Build_Normal_Requeue --
9942 --------------------------
9944 function Build_Normal_Requeue return Node_Id is
9945 Params : constant List_Id := New_List;
9946 Param : Node_Id;
9947 RT_Call : Node_Id;
9949 begin
9950 -- Process the "with abort" parameter
9952 Prepend_To (Params,
9953 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9955 -- Add the index expression to the parameters. It is common among all
9956 -- four cases.
9958 Prepend_To (Params,
9959 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
9961 if Is_Protected_Type (Old_Typ) then
9962 declare
9963 Self_Param : Node_Id;
9965 begin
9966 Self_Param :=
9967 Make_Attribute_Reference (Loc,
9968 Prefix =>
9969 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9970 Attribute_Name =>
9971 Name_Unchecked_Access);
9973 -- Protected to protected requeue
9975 if Is_Protected_Type (Conc_Typ) then
9976 RT_Call :=
9977 New_Reference_To (
9978 RTE (RE_Requeue_Protected_Entry), Loc);
9980 Param :=
9981 Make_Attribute_Reference (Loc,
9982 Prefix =>
9983 Concurrent_Ref (Concval),
9984 Attribute_Name =>
9985 Name_Unchecked_Access);
9987 -- Protected to task requeue
9989 else pragma Assert (Is_Task_Type (Conc_Typ));
9990 RT_Call :=
9991 New_Reference_To (
9992 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9994 Param := Concurrent_Ref (Concval);
9995 end if;
9997 Prepend_To (Params, Param);
9998 Prepend_To (Params, Self_Param);
9999 end;
10001 else pragma Assert (Is_Task_Type (Old_Typ));
10003 -- Task to protected requeue
10005 if Is_Protected_Type (Conc_Typ) then
10006 RT_Call :=
10007 New_Reference_To (
10008 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10010 Param :=
10011 Make_Attribute_Reference (Loc,
10012 Prefix =>
10013 Concurrent_Ref (Concval),
10014 Attribute_Name =>
10015 Name_Unchecked_Access);
10017 -- Task to task requeue
10019 else pragma Assert (Is_Task_Type (Conc_Typ));
10020 RT_Call :=
10021 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
10023 Param := Concurrent_Ref (Concval);
10024 end if;
10026 Prepend_To (Params, Param);
10027 end if;
10029 return
10030 Make_Procedure_Call_Statement (Loc,
10031 Name => RT_Call,
10032 Parameter_Associations => Params);
10033 end Build_Normal_Requeue;
10035 --------------------------
10036 -- Build_Skip_Statement --
10037 --------------------------
10039 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10040 Skip_Stmt : Node_Id;
10042 begin
10043 -- Build a return statement to skip the rest of the entire body
10045 if Is_Protected_Type (Old_Typ) then
10046 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10048 -- If the requeue is within a task, find the end label of the
10049 -- enclosing accept statement and create a goto statement to it.
10051 else
10052 declare
10053 Acc : Node_Id;
10054 Label : Node_Id;
10056 begin
10057 -- Climb the parent chain looking for the enclosing accept
10058 -- statement.
10060 Acc := Parent (Search);
10061 while Present (Acc)
10062 and then Nkind (Acc) /= N_Accept_Statement
10063 loop
10064 Acc := Parent (Acc);
10065 end loop;
10067 -- The last statement is the second label used for completing
10068 -- the rendezvous the usual way. The label we are looking for
10069 -- is right before it.
10071 Label :=
10072 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10074 pragma Assert (Nkind (Label) = N_Label);
10076 -- Generate a goto statement to skip the rest of the accept
10078 Skip_Stmt :=
10079 Make_Goto_Statement (Loc,
10080 Name =>
10081 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10082 end;
10083 end if;
10085 Set_Analyzed (Skip_Stmt);
10087 return Skip_Stmt;
10088 end Build_Skip_Statement;
10090 -- Start of processing for Expand_N_Requeue_Statement
10092 begin
10093 -- Extract the components of the entry call
10095 Extract_Entry (N, Concval, Ename, Index);
10096 Conc_Typ := Etype (Concval);
10098 -- If the prefix is an access to class-wide type, dereference to get
10099 -- object and entry type.
10101 if Is_Access_Type (Conc_Typ) then
10102 Conc_Typ := Designated_Type (Conc_Typ);
10103 Rewrite (Concval,
10104 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10105 Analyze_And_Resolve (Concval, Conc_Typ);
10106 end if;
10108 -- Examine the scope stack in order to find nearest enclosing protected
10109 -- or task type. This will constitute our invocation source.
10111 Old_Typ := Current_Scope;
10112 while Present (Old_Typ)
10113 and then not Is_Protected_Type (Old_Typ)
10114 and then not Is_Task_Type (Old_Typ)
10115 loop
10116 Old_Typ := Scope (Old_Typ);
10117 end loop;
10119 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10120 -- Concval.Ename where the type of Concval is class-wide concurrent
10121 -- interface.
10123 if Ada_Version >= Ada_2012
10124 and then Present (Concval)
10125 and then Is_Class_Wide_Type (Conc_Typ)
10126 and then Is_Concurrent_Interface (Conc_Typ)
10127 then
10128 declare
10129 Has_Impl : Boolean := False;
10130 Impl_Kind : Name_Id := No_Name;
10132 begin
10133 -- Check whether the Ename is flagged by pragma Implemented
10135 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10136 Has_Impl := True;
10137 Impl_Kind := Implementation_Kind (Entity (Ename));
10138 end if;
10140 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10141 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10143 if Has_Impl
10144 and then Impl_Kind = Name_By_Entry
10145 then
10146 Rewrite (N, Build_Dispatching_Requeue);
10147 Analyze (N);
10148 Insert_After (N, Build_Skip_Statement (N));
10150 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10151 -- a protected procedure. In this case the requeue is transformed
10152 -- into a dispatching call.
10154 elsif Has_Impl
10155 and then Impl_Kind = Name_By_Protected_Procedure
10156 then
10157 Rewrite (N, Build_Dispatching_Call_Equivalent);
10158 Analyze (N);
10160 -- The procedure_or_entry_NAME's implementation kind is either
10161 -- By_Any, Optional, or pragma Implemented was not applied at all.
10162 -- In this case a runtime test determines whether Ename denotes an
10163 -- entry or a protected procedure and performs the appropriate
10164 -- call.
10166 else
10167 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10168 Analyze (N);
10169 end if;
10170 end;
10172 -- Processing for regular (non-dispatching) requeues
10174 else
10175 Rewrite (N, Build_Normal_Requeue);
10176 Analyze (N);
10177 Insert_After (N, Build_Skip_Statement (N));
10178 end if;
10179 end Expand_N_Requeue_Statement;
10181 -------------------------------
10182 -- Expand_N_Selective_Accept --
10183 -------------------------------
10185 procedure Expand_N_Selective_Accept (N : Node_Id) is
10186 Loc : constant Source_Ptr := Sloc (N);
10187 Alts : constant List_Id := Select_Alternatives (N);
10189 -- Note: in the below declarations a lot of new lists are allocated
10190 -- unconditionally which may well not end up being used. That's not
10191 -- a good idea since it wastes space gratuitously ???
10193 Accept_Case : List_Id;
10194 Accept_List : constant List_Id := New_List;
10196 Alt : Node_Id;
10197 Alt_List : constant List_Id := New_List;
10198 Alt_Stats : List_Id;
10199 Ann : Entity_Id := Empty;
10201 Check_Guard : Boolean := True;
10203 Decls : constant List_Id := New_List;
10204 Stats : constant List_Id := New_List;
10205 Body_List : constant List_Id := New_List;
10206 Trailing_List : constant List_Id := New_List;
10208 Choices : List_Id;
10209 Else_Present : Boolean := False;
10210 Terminate_Alt : Node_Id := Empty;
10211 Select_Mode : Node_Id;
10213 Delay_Case : List_Id;
10214 Delay_Count : Integer := 0;
10215 Delay_Val : Entity_Id;
10216 Delay_Index : Entity_Id;
10217 Delay_Min : Entity_Id;
10218 Delay_Num : Int := 1;
10219 Delay_Alt_List : List_Id := New_List;
10220 Delay_List : constant List_Id := New_List;
10221 D : Entity_Id;
10222 M : Entity_Id;
10224 First_Delay : Boolean := True;
10225 Guard_Open : Entity_Id;
10227 End_Lab : Node_Id;
10228 Index : Int := 1;
10229 Lab : Node_Id;
10230 Num_Alts : Int;
10231 Num_Accept : Nat := 0;
10232 Proc : Node_Id;
10233 Time_Type : Entity_Id;
10234 Select_Call : Node_Id;
10236 Qnam : constant Entity_Id :=
10237 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10239 Xnam : constant Entity_Id :=
10240 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10242 -----------------------
10243 -- Local subprograms --
10244 -----------------------
10246 function Accept_Or_Raise return List_Id;
10247 -- For the rare case where delay alternatives all have guards, and
10248 -- all of them are closed, it is still possible that there were open
10249 -- accept alternatives with no callers. We must reexamine the
10250 -- Accept_List, and execute a selective wait with no else if some
10251 -- accept is open. If none, we raise program_error.
10253 procedure Add_Accept (Alt : Node_Id);
10254 -- Process a single accept statement in a select alternative. Build
10255 -- procedure for body of accept, and add entry to dispatch table with
10256 -- expression for guard, in preparation for call to run time select.
10258 function Make_And_Declare_Label (Num : Int) return Node_Id;
10259 -- Manufacture a label using Num as a serial number and declare it.
10260 -- The declaration is appended to Decls. The label marks the trailing
10261 -- statements of an accept or delay alternative.
10263 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10264 -- Build call to Selective_Wait runtime routine
10266 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10267 -- Add code to compare value of delay with previous values, and
10268 -- generate case entry for trailing statements.
10270 procedure Process_Accept_Alternative
10271 (Alt : Node_Id;
10272 Index : Int;
10273 Proc : Node_Id);
10274 -- Add code to call corresponding procedure, and branch to
10275 -- trailing statements, if any.
10277 ---------------------
10278 -- Accept_Or_Raise --
10279 ---------------------
10281 function Accept_Or_Raise return List_Id is
10282 Cond : Node_Id;
10283 Stats : List_Id;
10284 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10286 begin
10287 -- We generate the following:
10289 -- for J in q'range loop
10290 -- if q(J).S /=null_task_entry then
10291 -- selective_wait (simple_mode,...);
10292 -- done := True;
10293 -- exit;
10294 -- end if;
10295 -- end loop;
10297 -- if no rendez_vous then
10298 -- raise program_error;
10299 -- end if;
10301 -- Note that the code needs to know that the selector name
10302 -- in an Accept_Alternative is named S.
10304 Cond := Make_Op_Ne (Loc,
10305 Left_Opnd =>
10306 Make_Selected_Component (Loc,
10307 Prefix =>
10308 Make_Indexed_Component (Loc,
10309 Prefix => New_Reference_To (Qnam, Loc),
10310 Expressions => New_List (New_Reference_To (J, Loc))),
10311 Selector_Name => Make_Identifier (Loc, Name_S)),
10312 Right_Opnd =>
10313 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
10315 Stats := New_List (
10316 Make_Implicit_Loop_Statement (N,
10317 Iteration_Scheme =>
10318 Make_Iteration_Scheme (Loc,
10319 Loop_Parameter_Specification =>
10320 Make_Loop_Parameter_Specification (Loc,
10321 Defining_Identifier => J,
10322 Discrete_Subtype_Definition =>
10323 Make_Attribute_Reference (Loc,
10324 Prefix => New_Reference_To (Qnam, Loc),
10325 Attribute_Name => Name_Range,
10326 Expressions => New_List (
10327 Make_Integer_Literal (Loc, 1))))),
10329 Statements => New_List (
10330 Make_Implicit_If_Statement (N,
10331 Condition => Cond,
10332 Then_Statements => New_List (
10333 Make_Select_Call (
10334 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
10335 Make_Exit_Statement (Loc))))));
10337 Append_To (Stats,
10338 Make_Raise_Program_Error (Loc,
10339 Condition => Make_Op_Eq (Loc,
10340 Left_Opnd => New_Reference_To (Xnam, Loc),
10341 Right_Opnd =>
10342 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10343 Reason => PE_All_Guards_Closed));
10345 return Stats;
10346 end Accept_Or_Raise;
10348 ----------------
10349 -- Add_Accept --
10350 ----------------
10352 procedure Add_Accept (Alt : Node_Id) is
10353 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10354 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10355 Eloc : constant Source_Ptr := Sloc (Ename);
10356 Eent : constant Entity_Id := Entity (Ename);
10357 Index : constant Node_Id := Entry_Index (Acc_Stm);
10358 Null_Body : Node_Id;
10359 Proc_Body : Node_Id;
10360 PB_Ent : Entity_Id;
10361 Expr : Node_Id;
10362 Call : Node_Id;
10364 begin
10365 if No (Ann) then
10366 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10367 end if;
10369 if Present (Condition (Alt)) then
10370 Expr :=
10371 Make_If_Expression (Eloc, New_List (
10372 Condition (Alt),
10373 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10374 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
10375 else
10376 Expr :=
10377 Entry_Index_Expression
10378 (Eloc, Eent, Index, Scope (Eent));
10379 end if;
10381 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10382 Null_Body := New_Reference_To (Standard_False, Eloc);
10384 -- Always add call to Abort_Undefer when generating code, since
10385 -- this is what the runtime expects (abort deferred in
10386 -- Selective_Wait). In CodePeer mode this only confuses the
10387 -- analysis with unknown calls, so don't do it.
10389 if not CodePeer_Mode then
10390 Call :=
10391 Make_Procedure_Call_Statement (Eloc,
10392 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
10393 Insert_Before
10394 (First (Statements (Handled_Statement_Sequence
10395 (Accept_Statement (Alt)))),
10396 Call);
10397 Analyze (Call);
10398 end if;
10400 PB_Ent :=
10401 Make_Defining_Identifier (Eloc,
10402 New_External_Name (Chars (Ename), 'A', Num_Accept));
10404 if Comes_From_Source (Alt) then
10405 Set_Debug_Info_Needed (PB_Ent);
10406 end if;
10408 Proc_Body :=
10409 Make_Subprogram_Body (Eloc,
10410 Specification =>
10411 Make_Procedure_Specification (Eloc,
10412 Defining_Unit_Name => PB_Ent),
10413 Declarations => Declarations (Acc_Stm),
10414 Handled_Statement_Sequence =>
10415 Build_Accept_Body (Accept_Statement (Alt)));
10417 -- During the analysis of the body of the accept statement, any
10418 -- zero cost exception handler records were collected in the
10419 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10420 -- This is where we move them to where they belong, namely the
10421 -- newly created procedure.
10423 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10424 Append (Proc_Body, Body_List);
10426 else
10427 Null_Body := New_Reference_To (Standard_True, Eloc);
10429 -- if accept statement has declarations, insert above, given that
10430 -- we are not creating a body for the accept.
10432 if Present (Declarations (Acc_Stm)) then
10433 Insert_Actions (N, Declarations (Acc_Stm));
10434 end if;
10435 end if;
10437 Append_To (Accept_List,
10438 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10440 Num_Accept := Num_Accept + 1;
10441 end Add_Accept;
10443 ----------------------------
10444 -- Make_And_Declare_Label --
10445 ----------------------------
10447 function Make_And_Declare_Label (Num : Int) return Node_Id is
10448 Lab_Id : Node_Id;
10450 begin
10451 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10452 Lab :=
10453 Make_Label (Loc, Lab_Id);
10455 Append_To (Decls,
10456 Make_Implicit_Label_Declaration (Loc,
10457 Defining_Identifier =>
10458 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10459 Label_Construct => Lab));
10461 return Lab;
10462 end Make_And_Declare_Label;
10464 ----------------------
10465 -- Make_Select_Call --
10466 ----------------------
10468 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10469 Params : constant List_Id := New_List;
10471 begin
10472 Append (
10473 Make_Attribute_Reference (Loc,
10474 Prefix => New_Reference_To (Qnam, Loc),
10475 Attribute_Name => Name_Unchecked_Access),
10476 Params);
10477 Append (Select_Mode, Params);
10478 Append (New_Reference_To (Ann, Loc), Params);
10479 Append (New_Reference_To (Xnam, Loc), Params);
10481 return
10482 Make_Procedure_Call_Statement (Loc,
10483 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
10484 Parameter_Associations => Params);
10485 end Make_Select_Call;
10487 --------------------------------
10488 -- Process_Accept_Alternative --
10489 --------------------------------
10491 procedure Process_Accept_Alternative
10492 (Alt : Node_Id;
10493 Index : Int;
10494 Proc : Node_Id)
10496 Astmt : constant Node_Id := Accept_Statement (Alt);
10497 Alt_Stats : List_Id;
10499 begin
10500 Adjust_Condition (Condition (Alt));
10502 -- Accept with body
10504 if Present (Handled_Statement_Sequence (Astmt)) then
10505 Alt_Stats :=
10506 New_List (
10507 Make_Procedure_Call_Statement (Sloc (Proc),
10508 Name =>
10509 New_Reference_To
10510 (Defining_Unit_Name (Specification (Proc)),
10511 Sloc (Proc))));
10513 -- Accept with no body (followed by trailing statements)
10515 else
10516 Alt_Stats := Empty_List;
10517 end if;
10519 Ensure_Statement_Present (Sloc (Astmt), Alt);
10521 -- After the call, if any, branch to trailing statements, if any.
10522 -- We create a label for each, as well as the corresponding label
10523 -- declaration.
10525 if not Is_Empty_List (Statements (Alt)) then
10526 Lab := Make_And_Declare_Label (Index);
10527 Append (Lab, Trailing_List);
10528 Append_List (Statements (Alt), Trailing_List);
10529 Append_To (Trailing_List,
10530 Make_Goto_Statement (Loc,
10531 Name => New_Copy (Identifier (End_Lab))));
10533 else
10534 Lab := End_Lab;
10535 end if;
10537 Append_To (Alt_Stats,
10538 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10540 Append_To (Alt_List,
10541 Make_Case_Statement_Alternative (Loc,
10542 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10543 Statements => Alt_Stats));
10544 end Process_Accept_Alternative;
10546 -------------------------------
10547 -- Process_Delay_Alternative --
10548 -------------------------------
10550 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10551 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10552 Cond : Node_Id;
10553 Delay_Alt : List_Id;
10555 begin
10556 -- Deal with C/Fortran boolean as delay condition
10558 Adjust_Condition (Condition (Alt));
10560 -- Determine the smallest specified delay
10562 -- for each delay alternative generate:
10564 -- if guard-expression then
10565 -- Delay_Val := delay-expression;
10566 -- Guard_Open := True;
10567 -- if Delay_Val < Delay_Min then
10568 -- Delay_Min := Delay_Val;
10569 -- Delay_Index := Index;
10570 -- end if;
10571 -- end if;
10573 -- The enclosing if-statement is omitted if there is no guard
10575 if Delay_Count = 1 or else First_Delay then
10576 First_Delay := False;
10578 Delay_Alt := New_List (
10579 Make_Assignment_Statement (Loc,
10580 Name => New_Reference_To (Delay_Min, Loc),
10581 Expression => Expression (Delay_Statement (Alt))));
10583 if Delay_Count > 1 then
10584 Append_To (Delay_Alt,
10585 Make_Assignment_Statement (Loc,
10586 Name => New_Reference_To (Delay_Index, Loc),
10587 Expression => Make_Integer_Literal (Loc, Index)));
10588 end if;
10590 else
10591 Delay_Alt := New_List (
10592 Make_Assignment_Statement (Loc,
10593 Name => New_Reference_To (Delay_Val, Loc),
10594 Expression => Expression (Delay_Statement (Alt))));
10596 if Time_Type = Standard_Duration then
10597 Cond :=
10598 Make_Op_Lt (Loc,
10599 Left_Opnd => New_Reference_To (Delay_Val, Loc),
10600 Right_Opnd => New_Reference_To (Delay_Min, Loc));
10602 else
10603 -- The scope of the time type must define a comparison
10604 -- operator. The scope itself may not be visible, so we
10605 -- construct a node with entity information to insure that
10606 -- semantic analysis can find the proper operator.
10608 Cond :=
10609 Make_Function_Call (Loc,
10610 Name => Make_Selected_Component (Loc,
10611 Prefix =>
10612 New_Reference_To (Scope (Time_Type), Loc),
10613 Selector_Name =>
10614 Make_Operator_Symbol (Loc,
10615 Chars => Name_Op_Lt,
10616 Strval => No_String)),
10617 Parameter_Associations =>
10618 New_List (
10619 New_Reference_To (Delay_Val, Loc),
10620 New_Reference_To (Delay_Min, Loc)));
10622 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10623 end if;
10625 Append_To (Delay_Alt,
10626 Make_Implicit_If_Statement (N,
10627 Condition => Cond,
10628 Then_Statements => New_List (
10629 Make_Assignment_Statement (Loc,
10630 Name => New_Reference_To (Delay_Min, Loc),
10631 Expression => New_Reference_To (Delay_Val, Loc)),
10633 Make_Assignment_Statement (Loc,
10634 Name => New_Reference_To (Delay_Index, Loc),
10635 Expression => Make_Integer_Literal (Loc, Index)))));
10636 end if;
10638 if Check_Guard then
10639 Append_To (Delay_Alt,
10640 Make_Assignment_Statement (Loc,
10641 Name => New_Reference_To (Guard_Open, Loc),
10642 Expression => New_Reference_To (Standard_True, Loc)));
10643 end if;
10645 if Present (Condition (Alt)) then
10646 Delay_Alt := New_List (
10647 Make_Implicit_If_Statement (N,
10648 Condition => Condition (Alt),
10649 Then_Statements => Delay_Alt));
10650 end if;
10652 Append_List (Delay_Alt, Delay_List);
10654 Ensure_Statement_Present (Dloc, Alt);
10656 -- If the delay alternative has a statement part, add choice to the
10657 -- case statements for delays.
10659 if not Is_Empty_List (Statements (Alt)) then
10661 if Delay_Count = 1 then
10662 Append_List (Statements (Alt), Delay_Alt_List);
10664 else
10665 Append_To (Delay_Alt_List,
10666 Make_Case_Statement_Alternative (Loc,
10667 Discrete_Choices => New_List (
10668 Make_Integer_Literal (Loc, Index)),
10669 Statements => Statements (Alt)));
10670 end if;
10672 elsif Delay_Count = 1 then
10674 -- If the single delay has no trailing statements, add a branch
10675 -- to the exit label to the selective wait.
10677 Delay_Alt_List := New_List (
10678 Make_Goto_Statement (Loc,
10679 Name => New_Copy (Identifier (End_Lab))));
10681 end if;
10682 end Process_Delay_Alternative;
10684 -- Start of processing for Expand_N_Selective_Accept
10686 begin
10687 Process_Statements_For_Controlled_Objects (N);
10689 -- First insert some declarations before the select. The first is:
10691 -- Ann : Address
10693 -- This variable holds the parameters passed to the accept body. This
10694 -- declaration has already been inserted by the time we get here by
10695 -- a call to Expand_Accept_Declarations made from the semantics when
10696 -- processing the first accept statement contained in the select. We
10697 -- can find this entity as Accept_Address (E), where E is any of the
10698 -- entries references by contained accept statements.
10700 -- The first step is to scan the list of Selective_Accept_Statements
10701 -- to find this entity, and also count the number of accepts, and
10702 -- determine if terminated, delay or else is present:
10704 Num_Alts := 0;
10706 Alt := First (Alts);
10707 while Present (Alt) loop
10708 Process_Statements_For_Controlled_Objects (Alt);
10710 if Nkind (Alt) = N_Accept_Alternative then
10711 Add_Accept (Alt);
10713 elsif Nkind (Alt) = N_Delay_Alternative then
10714 Delay_Count := Delay_Count + 1;
10716 -- If the delays are relative delays, the delay expressions have
10717 -- type Standard_Duration. Otherwise they must have some time type
10718 -- recognized by GNAT.
10720 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10721 Time_Type := Standard_Duration;
10722 else
10723 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10725 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10726 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10727 then
10728 null;
10729 else
10730 Error_Msg_NE (
10731 "& is not a time type (RM 9.6(6))",
10732 Expression (Delay_Statement (Alt)), Time_Type);
10733 Time_Type := Standard_Duration;
10734 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10735 end if;
10736 end if;
10738 if No (Condition (Alt)) then
10740 -- This guard will always be open
10742 Check_Guard := False;
10743 end if;
10745 elsif Nkind (Alt) = N_Terminate_Alternative then
10746 Adjust_Condition (Condition (Alt));
10747 Terminate_Alt := Alt;
10748 end if;
10750 Num_Alts := Num_Alts + 1;
10751 Next (Alt);
10752 end loop;
10754 Else_Present := Present (Else_Statements (N));
10756 -- At the same time (see procedure Add_Accept) we build the accept list:
10758 -- Qnn : Accept_List (1 .. num-select) := (
10759 -- (null-body, entry-index),
10760 -- (null-body, entry-index),
10761 -- ..
10762 -- (null_body, entry-index));
10764 -- In the above declaration, null-body is True if the corresponding
10765 -- accept has no body, and false otherwise. The entry is either the
10766 -- entry index expression if there is no guard, or if a guard is
10767 -- present, then an if expression of the form:
10769 -- (if guard then entry-index else Null_Task_Entry)
10771 -- If a guard is statically known to be false, the entry can simply
10772 -- be omitted from the accept list.
10774 Append_To (Decls,
10775 Make_Object_Declaration (Loc,
10776 Defining_Identifier => Qnam,
10777 Object_Definition => New_Reference_To (RTE (RE_Accept_List), Loc),
10778 Aliased_Present => True,
10779 Expression =>
10780 Make_Qualified_Expression (Loc,
10781 Subtype_Mark =>
10782 New_Reference_To (RTE (RE_Accept_List), Loc),
10783 Expression =>
10784 Make_Aggregate (Loc, Expressions => Accept_List))));
10786 -- Then we declare the variable that holds the index for the accept
10787 -- that will be selected for service:
10789 -- Xnn : Select_Index;
10791 Append_To (Decls,
10792 Make_Object_Declaration (Loc,
10793 Defining_Identifier => Xnam,
10794 Object_Definition =>
10795 New_Reference_To (RTE (RE_Select_Index), Loc),
10796 Expression =>
10797 New_Reference_To (RTE (RE_No_Rendezvous), Loc)));
10799 -- After this follow procedure declarations for each accept body
10801 -- procedure Pnn is
10802 -- begin
10803 -- ...
10804 -- end;
10806 -- where the ... are statements from the corresponding procedure body.
10807 -- No parameters are involved, since the parameters are passed via Ann
10808 -- and the parameter references have already been expanded to be direct
10809 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10810 -- any embedded tasking statements (which would normally be illegal in
10811 -- procedures), have been converted to calls to the tasking runtime so
10812 -- there is no problem in putting them into procedures.
10814 -- The original accept statement has been expanded into a block in
10815 -- the same fashion as for simple accepts (see Build_Accept_Body).
10817 -- Note: we don't really need to build these procedures for the case
10818 -- where no delay statement is present, but it is just as easy to
10819 -- build them unconditionally, and not significantly inefficient,
10820 -- since if they are short they will be inlined anyway.
10822 -- The procedure declarations have been assembled in Body_List
10824 -- If delays are present, we must compute the required delay.
10825 -- We first generate the declarations:
10827 -- Delay_Index : Boolean := 0;
10828 -- Delay_Min : Some_Time_Type.Time;
10829 -- Delay_Val : Some_Time_Type.Time;
10831 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10832 -- active delay that is actually chosen as the basis for the possible
10833 -- delay if an immediate rendez-vous is not possible.
10835 -- In the most common case there is a single delay statement, and this
10836 -- is handled specially.
10838 if Delay_Count > 0 then
10840 -- Generate the required declarations
10842 Delay_Val :=
10843 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10844 Delay_Index :=
10845 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10846 Delay_Min :=
10847 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10849 Append_To (Decls,
10850 Make_Object_Declaration (Loc,
10851 Defining_Identifier => Delay_Val,
10852 Object_Definition => New_Reference_To (Time_Type, Loc)));
10854 Append_To (Decls,
10855 Make_Object_Declaration (Loc,
10856 Defining_Identifier => Delay_Index,
10857 Object_Definition => New_Reference_To (Standard_Integer, Loc),
10858 Expression => Make_Integer_Literal (Loc, 0)));
10860 Append_To (Decls,
10861 Make_Object_Declaration (Loc,
10862 Defining_Identifier => Delay_Min,
10863 Object_Definition => New_Reference_To (Time_Type, Loc),
10864 Expression =>
10865 Unchecked_Convert_To (Time_Type,
10866 Make_Attribute_Reference (Loc,
10867 Prefix =>
10868 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10869 Attribute_Name => Name_Last))));
10871 -- Create Duration and Delay_Mode objects used for passing a delay
10872 -- value to RTS
10874 D := Make_Temporary (Loc, 'D');
10875 M := Make_Temporary (Loc, 'M');
10877 declare
10878 Discr : Entity_Id;
10880 begin
10881 -- Note that these values are defined in s-osprim.ads and must
10882 -- be kept in sync:
10884 -- Relative : constant := 0;
10885 -- Absolute_Calendar : constant := 1;
10886 -- Absolute_RT : constant := 2;
10888 if Time_Type = Standard_Duration then
10889 Discr := Make_Integer_Literal (Loc, 0);
10891 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10892 Discr := Make_Integer_Literal (Loc, 1);
10894 else
10895 pragma Assert
10896 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10897 Discr := Make_Integer_Literal (Loc, 2);
10898 end if;
10900 Append_To (Decls,
10901 Make_Object_Declaration (Loc,
10902 Defining_Identifier => D,
10903 Object_Definition =>
10904 New_Reference_To (Standard_Duration, Loc)));
10906 Append_To (Decls,
10907 Make_Object_Declaration (Loc,
10908 Defining_Identifier => M,
10909 Object_Definition =>
10910 New_Reference_To (Standard_Integer, Loc),
10911 Expression => Discr));
10912 end;
10914 if Check_Guard then
10915 Guard_Open :=
10916 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
10918 Append_To (Decls,
10919 Make_Object_Declaration (Loc,
10920 Defining_Identifier => Guard_Open,
10921 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10922 Expression => New_Reference_To (Standard_False, Loc)));
10923 end if;
10925 -- Delay_Count is zero, don't need M and D set (suppress warning)
10927 else
10928 M := Empty;
10929 D := Empty;
10930 end if;
10932 if Present (Terminate_Alt) then
10934 -- If the terminate alternative guard is False, use
10935 -- Simple_Mode; otherwise use Terminate_Mode.
10937 if Present (Condition (Terminate_Alt)) then
10938 Select_Mode := Make_If_Expression (Loc,
10939 New_List (Condition (Terminate_Alt),
10940 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
10941 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
10942 else
10943 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
10944 end if;
10946 elsif Else_Present or Delay_Count > 0 then
10947 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
10949 else
10950 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
10951 end if;
10953 Select_Call := Make_Select_Call (Select_Mode);
10954 Append (Select_Call, Stats);
10956 -- Now generate code to act on the result. There is an entry
10957 -- in this case for each accept statement with a non-null body,
10958 -- followed by a branch to the statements that follow the Accept.
10959 -- In the absence of delay alternatives, we generate:
10961 -- case X is
10962 -- when No_Rendezvous => -- omitted if simple mode
10963 -- goto Lab0;
10965 -- when 1 =>
10966 -- P1n;
10967 -- goto Lab1;
10969 -- when 2 =>
10970 -- P2n;
10971 -- goto Lab2;
10973 -- when others =>
10974 -- goto Exit;
10975 -- end case;
10977 -- Lab0: Else_Statements;
10978 -- goto exit;
10980 -- Lab1: Trailing_Statements1;
10981 -- goto Exit;
10983 -- Lab2: Trailing_Statements2;
10984 -- goto Exit;
10985 -- ...
10986 -- Exit:
10988 -- Generate label for common exit
10990 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
10992 -- First entry is the default case, when no rendezvous is possible
10994 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10996 if Else_Present then
10998 -- If no rendezvous is possible, the else part is executed
11000 Lab := Make_And_Declare_Label (0);
11001 Alt_Stats := New_List (
11002 Make_Goto_Statement (Loc,
11003 Name => New_Copy (Identifier (Lab))));
11005 Append (Lab, Trailing_List);
11006 Append_List (Else_Statements (N), Trailing_List);
11007 Append_To (Trailing_List,
11008 Make_Goto_Statement (Loc,
11009 Name => New_Copy (Identifier (End_Lab))));
11010 else
11011 Alt_Stats := New_List (
11012 Make_Goto_Statement (Loc,
11013 Name => New_Copy (Identifier (End_Lab))));
11014 end if;
11016 Append_To (Alt_List,
11017 Make_Case_Statement_Alternative (Loc,
11018 Discrete_Choices => Choices,
11019 Statements => Alt_Stats));
11021 -- We make use of the fact that Accept_Index is an integer type, and
11022 -- generate successive literals for entries for each accept. Only those
11023 -- for which there is a body or trailing statements get a case entry.
11025 Alt := First (Select_Alternatives (N));
11026 Proc := First (Body_List);
11027 while Present (Alt) loop
11029 if Nkind (Alt) = N_Accept_Alternative then
11030 Process_Accept_Alternative (Alt, Index, Proc);
11031 Index := Index + 1;
11033 if Present
11034 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11035 then
11036 Next (Proc);
11037 end if;
11039 elsif Nkind (Alt) = N_Delay_Alternative then
11040 Process_Delay_Alternative (Alt, Delay_Num);
11041 Delay_Num := Delay_Num + 1;
11042 end if;
11044 Next (Alt);
11045 end loop;
11047 -- An others choice is always added to the main case, as well
11048 -- as the delay case (to satisfy the compiler).
11050 Append_To (Alt_List,
11051 Make_Case_Statement_Alternative (Loc,
11052 Discrete_Choices =>
11053 New_List (Make_Others_Choice (Loc)),
11054 Statements =>
11055 New_List (Make_Goto_Statement (Loc,
11056 Name => New_Copy (Identifier (End_Lab))))));
11058 Accept_Case := New_List (
11059 Make_Case_Statement (Loc,
11060 Expression => New_Reference_To (Xnam, Loc),
11061 Alternatives => Alt_List));
11063 Append_List (Trailing_List, Accept_Case);
11064 Append_List (Body_List, Decls);
11066 -- Construct case statement for trailing statements of delay
11067 -- alternatives, if there are several of them.
11069 if Delay_Count > 1 then
11070 Append_To (Delay_Alt_List,
11071 Make_Case_Statement_Alternative (Loc,
11072 Discrete_Choices =>
11073 New_List (Make_Others_Choice (Loc)),
11074 Statements =>
11075 New_List (Make_Null_Statement (Loc))));
11077 Delay_Case := New_List (
11078 Make_Case_Statement (Loc,
11079 Expression => New_Reference_To (Delay_Index, Loc),
11080 Alternatives => Delay_Alt_List));
11081 else
11082 Delay_Case := Delay_Alt_List;
11083 end if;
11085 -- If there are no delay alternatives, we append the case statement
11086 -- to the statement list.
11088 if Delay_Count = 0 then
11089 Append_List (Accept_Case, Stats);
11091 -- Delay alternatives present
11093 else
11094 -- If delay alternatives are present we generate:
11096 -- find minimum delay.
11097 -- DX := minimum delay;
11098 -- M := <delay mode>;
11099 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11100 -- DX, MX, X);
11102 -- if X = No_Rendezvous then
11103 -- case statement for delay statements.
11104 -- else
11105 -- case statement for accept alternatives.
11106 -- end if;
11108 declare
11109 Cases : Node_Id;
11110 Stmt : Node_Id;
11111 Parms : List_Id;
11112 Parm : Node_Id;
11113 Conv : Node_Id;
11115 begin
11116 -- The type of the delay expression is known to be legal
11118 if Time_Type = Standard_Duration then
11119 Conv := New_Reference_To (Delay_Min, Loc);
11121 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11122 Conv := Make_Function_Call (Loc,
11123 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
11124 New_List (New_Reference_To (Delay_Min, Loc)));
11126 else
11127 pragma Assert
11128 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11130 Conv := Make_Function_Call (Loc,
11131 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
11132 New_List (New_Reference_To (Delay_Min, Loc)));
11133 end if;
11135 Stmt := Make_Assignment_Statement (Loc,
11136 Name => New_Reference_To (D, Loc),
11137 Expression => Conv);
11139 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11141 Parms := Parameter_Associations (Select_Call);
11142 Parm := First (Parms);
11144 while Present (Parm) and then Parm /= Select_Mode loop
11145 Next (Parm);
11146 end loop;
11148 pragma Assert (Present (Parm));
11149 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
11150 Analyze (Parm);
11152 -- Prepare two new parameters of Duration and Delay_Mode type
11153 -- which represent the value and the mode of the minimum delay.
11155 Next (Parm);
11156 Insert_After (Parm, New_Reference_To (M, Loc));
11157 Insert_After (Parm, New_Reference_To (D, Loc));
11159 -- Create a call to RTS
11161 Rewrite (Select_Call,
11162 Make_Procedure_Call_Statement (Loc,
11163 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
11164 Parameter_Associations => Parms));
11166 -- This new call should follow the calculation of the minimum
11167 -- delay.
11169 Insert_List_Before (Select_Call, Delay_List);
11171 if Check_Guard then
11172 Stmt :=
11173 Make_Implicit_If_Statement (N,
11174 Condition => New_Reference_To (Guard_Open, Loc),
11175 Then_Statements => New_List (
11176 New_Copy_Tree (Stmt),
11177 New_Copy_Tree (Select_Call)),
11178 Else_Statements => Accept_Or_Raise);
11179 Rewrite (Select_Call, Stmt);
11180 else
11181 Insert_Before (Select_Call, Stmt);
11182 end if;
11184 Cases :=
11185 Make_Implicit_If_Statement (N,
11186 Condition => Make_Op_Eq (Loc,
11187 Left_Opnd => New_Reference_To (Xnam, Loc),
11188 Right_Opnd =>
11189 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
11191 Then_Statements => Delay_Case,
11192 Else_Statements => Accept_Case);
11194 Append (Cases, Stats);
11195 end;
11196 end if;
11197 Append (End_Lab, Stats);
11199 -- Replace accept statement with appropriate block
11201 Rewrite (N,
11202 Make_Block_Statement (Loc,
11203 Declarations => Decls,
11204 Handled_Statement_Sequence =>
11205 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11206 Analyze (N);
11208 -- Note: have to worry more about abort deferral in above code ???
11210 -- Final step is to unstack the Accept_Address entries for all accept
11211 -- statements appearing in accept alternatives in the select statement
11213 Alt := First (Alts);
11214 while Present (Alt) loop
11215 if Nkind (Alt) = N_Accept_Alternative then
11216 Remove_Last_Elmt (Accept_Address
11217 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11218 end if;
11220 Next (Alt);
11221 end loop;
11222 end Expand_N_Selective_Accept;
11224 --------------------------------------
11225 -- Expand_N_Single_Task_Declaration --
11226 --------------------------------------
11228 -- Single task declarations should never be present after semantic
11229 -- analysis, since we expect them to be replaced by a declaration of an
11230 -- anonymous task type, followed by a declaration of the task object. We
11231 -- include this routine to make sure that is happening.
11233 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11234 begin
11235 raise Program_Error;
11236 end Expand_N_Single_Task_Declaration;
11238 ------------------------
11239 -- Expand_N_Task_Body --
11240 ------------------------
11242 -- Given a task body
11244 -- task body tname is
11245 -- <declarations>
11246 -- begin
11247 -- <statements>
11248 -- end x;
11250 -- This expansion routine converts it into a procedure and sets the
11251 -- elaboration flag for the procedure to true, to represent the fact
11252 -- that the task body is now elaborated:
11254 -- procedure tnameB (_Task : access tnameV) is
11255 -- discriminal : dtype renames _Task.discriminant;
11257 -- procedure _clean is
11258 -- begin
11259 -- Abort_Defer.all;
11260 -- Complete_Task;
11261 -- Abort_Undefer.all;
11262 -- return;
11263 -- end _clean;
11265 -- begin
11266 -- Abort_Undefer.all;
11267 -- <declarations>
11268 -- System.Task_Stages.Complete_Activation;
11269 -- <statements>
11270 -- at end
11271 -- _clean;
11272 -- end tnameB;
11274 -- tnameE := True;
11276 -- In addition, if the task body is an activator, then a call to activate
11277 -- tasks is added at the start of the statements, before the call to
11278 -- Complete_Activation, and if in addition the task is a master then it
11279 -- must be established as a master. These calls are inserted and analyzed
11280 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11281 -- expanded.
11283 -- There is one discriminal declaration line generated for each
11284 -- discriminant that is present to provide an easy reference point for
11285 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11287 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11288 -- task body procedures have a profile (Arg : System.Address). That is
11289 -- needed because GNARLI has to use the same access-to-subprogram type
11290 -- for all task types. We depend here on knowing that in GNAT, passing
11291 -- an address argument by value is identical to passing a record value
11292 -- by access (in either case a single pointer is passed), so even though
11293 -- this procedure has the wrong profile. In fact it's all OK, since the
11294 -- callings sequence is identical.
11296 procedure Expand_N_Task_Body (N : Node_Id) is
11297 Loc : constant Source_Ptr := Sloc (N);
11298 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11299 Call : Node_Id;
11300 New_N : Node_Id;
11302 Insert_Nod : Node_Id;
11303 -- Used to determine the proper location of wrapper body insertions
11305 begin
11306 -- Add renaming declarations for discriminals and a declaration for the
11307 -- entry family index (if applicable).
11309 Install_Private_Data_Declarations
11310 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11312 -- Add a call to Abort_Undefer at the very beginning of the task
11313 -- body since this body is called with abort still deferred.
11315 if Abort_Allowed then
11316 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11317 Insert_Before
11318 (First (Statements (Handled_Statement_Sequence (N))), Call);
11319 Analyze (Call);
11320 end if;
11322 -- The statement part has already been protected with an at_end and
11323 -- cleanup actions. The call to Complete_Activation must be placed
11324 -- at the head of the sequence of statements of that block. The
11325 -- declarations have been merged in this sequence of statements but
11326 -- the first real statement is accessible from the First_Real_Statement
11327 -- field (which was set for exactly this purpose).
11329 if Restricted_Profile then
11330 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11331 else
11332 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11333 end if;
11335 Insert_Before
11336 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11337 Analyze (Call);
11339 New_N :=
11340 Make_Subprogram_Body (Loc,
11341 Specification => Build_Task_Proc_Specification (Ttyp),
11342 Declarations => Declarations (N),
11343 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11345 -- If the task contains generic instantiations, cleanup actions are
11346 -- delayed until after instantiation. Transfer the activation chain to
11347 -- the subprogram, to insure that the activation call is properly
11348 -- generated. It the task body contains inner tasks, indicate that the
11349 -- subprogram is a task master.
11351 if Delay_Cleanups (Ttyp) then
11352 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11353 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11354 end if;
11356 Rewrite (N, New_N);
11357 Analyze (N);
11359 -- Set elaboration flag immediately after task body. If the body is a
11360 -- subunit, the flag is set in the declarative part containing the stub.
11362 if Nkind (Parent (N)) /= N_Subunit then
11363 Insert_After (N,
11364 Make_Assignment_Statement (Loc,
11365 Name =>
11366 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11367 Expression => New_Reference_To (Standard_True, Loc)));
11368 end if;
11370 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11371 -- the task body. At this point all wrapper specs have been created,
11372 -- frozen and included in the dispatch table for the task type.
11374 if Ada_Version >= Ada_2005 then
11375 if Nkind (Parent (N)) = N_Subunit then
11376 Insert_Nod := Corresponding_Stub (Parent (N));
11377 else
11378 Insert_Nod := N;
11379 end if;
11381 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11382 end if;
11383 end Expand_N_Task_Body;
11385 ------------------------------------
11386 -- Expand_N_Task_Type_Declaration --
11387 ------------------------------------
11389 -- We have several things to do. First we must create a Boolean flag used
11390 -- to mark if the body is elaborated yet. This variable gets set to True
11391 -- when the body of the task is elaborated (we can't rely on the normal
11392 -- ABE mechanism for the task body, since we need to pass an access to
11393 -- this elaboration boolean to the runtime routines).
11395 -- taskE : aliased Boolean := False;
11397 -- Next a variable is declared to hold the task stack size (either the
11398 -- default : Unspecified_Size, or a value that is set by a pragma
11399 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11400 -- the variable is initialized with this value:
11402 -- taskZ : Size_Type := Unspecified_Size;
11403 -- or
11404 -- taskZ : Size_Type := Size_Type (size_expression);
11406 -- Note: No variable is needed to hold the task relative deadline since
11407 -- its value would never be static because the parameter is of a private
11408 -- type (Ada.Real_Time.Time_Span).
11410 -- Next we create a corresponding record type declaration used to represent
11411 -- values of this task. The general form of this type declaration is
11413 -- type taskV (discriminants) is record
11414 -- _Task_Id : Task_Id;
11415 -- entry_family : array (bounds) of Void;
11416 -- _Priority : Integer := priority_expression;
11417 -- _Size : Size_Type := size_expression;
11418 -- _Task_Info : Task_Info_Type := task_info_expression;
11419 -- _CPU : Integer := cpu_range_expression;
11420 -- _Relative_Deadline : Time_Span := time_span_expression;
11421 -- _Domain : Dispatching_Domain := dd_expression;
11422 -- end record;
11424 -- The discriminants are present only if the corresponding task type has
11425 -- discriminants, and they exactly mirror the task type discriminants.
11427 -- The Id field is always present. It contains the Task_Id value, as set by
11428 -- the call to Create_Task. Note that although the task is limited, the
11429 -- task value record type is not limited, so there is no problem in passing
11430 -- this field as an out parameter to Create_Task.
11432 -- One entry_family component is present for each entry family in the task
11433 -- definition. The bounds correspond to the bounds of the entry family
11434 -- (which may depend on discriminants). The element type is void, since we
11435 -- only need the bounds information for determining the entry index. Note
11436 -- that the use of an anonymous array would normally be illegal in this
11437 -- context, but this is a parser check, and the semantics is quite prepared
11438 -- to handle such a case.
11440 -- The _Size field is present only if a Storage_Size pragma appears in the
11441 -- task definition. The expression captures the argument that was present
11442 -- in the pragma, and is used to override the task stack size otherwise
11443 -- associated with the task type.
11445 -- The _Priority field is present only if the task entity has a Priority or
11446 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11447 -- definition clause). It will be filled at the freeze point, when the
11448 -- record init proc is built, to capture the expression of the rep item
11449 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11450 -- here since aspect evaluations are delayed till the freeze point.
11452 -- The _Task_Info field is present only if a Task_Info pragma appears in
11453 -- the task definition. The expression captures the argument that was
11454 -- present in the pragma, and is used to provide the Task_Image parameter
11455 -- to the call to Create_Task.
11457 -- The _CPU field is present only if the task entity has a CPU rep item
11458 -- (pragma, aspect specification or attribute definition clause). It will
11459 -- be filled at the freeze point, when the record init proc is built, to
11460 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11461 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11462 -- are delayed till the freeze point.
11464 -- The _Relative_Deadline field is present only if a Relative_Deadline
11465 -- pragma appears in the task definition. The expression captures the
11466 -- argument that was present in the pragma, and is used to provide the
11467 -- Relative_Deadline parameter to the call to Create_Task.
11469 -- The _Domain field is present only if the task entity has a
11470 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11471 -- definition clause). It will be filled at the freeze point, when the
11472 -- record init proc is built, to capture the expression of the rep item
11473 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11474 -- here since aspect evaluations are delayed till the freeze point.
11476 -- When a task is declared, an instance of the task value record is
11477 -- created. The elaboration of this declaration creates the correct bounds
11478 -- for the entry families, and also evaluates the size, priority, and
11479 -- task_Info expressions if needed. The initialization routine for the task
11480 -- type itself then calls Create_Task with appropriate parameters to
11481 -- initialize the value of the Task_Id field.
11483 -- Note: the address of this record is passed as the "Discriminants"
11484 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11485 -- body procedure, it does not matter that it does not quite match the
11486 -- GNARLI model of what is being passed (the record contains more than just
11487 -- the discriminants, but the discriminants can be found from the record
11488 -- value).
11490 -- The Entity_Id for this created record type is placed in the
11491 -- Corresponding_Record_Type field of the associated task type entity.
11493 -- Next we create a procedure specification for the task body procedure:
11495 -- procedure taskB (_Task : access taskV);
11497 -- Note that this must come after the record type declaration, since
11498 -- the spec refers to this type. It turns out that the initialization
11499 -- procedure for the value type references the task body spec, but that's
11500 -- fine, since it won't be generated till the freeze point for the type,
11501 -- which is certainly after the task body spec declaration.
11503 -- Finally, we set the task index value field of the entry attribute in
11504 -- the case of a simple entry.
11506 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11507 Loc : constant Source_Ptr := Sloc (N);
11508 TaskId : constant Entity_Id := Defining_Identifier (N);
11509 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11510 Tasknm : constant Name_Id := Chars (Tasktyp);
11511 Taskdef : constant Node_Id := Task_Definition (N);
11513 Body_Decl : Node_Id;
11514 Cdecls : List_Id;
11515 Decl_Stack : Node_Id;
11516 Elab_Decl : Node_Id;
11517 Ent_Stack : Entity_Id;
11518 Proc_Spec : Node_Id;
11519 Rec_Decl : Node_Id;
11520 Rec_Ent : Entity_Id;
11521 Size_Decl : Entity_Id;
11522 Task_Size : Node_Id;
11524 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11525 -- Searches the task definition T for the first occurrence of the pragma
11526 -- Relative Deadline. The caller has ensured that the pragma is present
11527 -- in the task definition. Note that this routine cannot be implemented
11528 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11529 -- not chained because their expansion into a procedure call statement
11530 -- would cause a break in the chain.
11532 ----------------------------------
11533 -- Get_Relative_Deadline_Pragma --
11534 ----------------------------------
11536 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11537 N : Node_Id;
11539 begin
11540 N := First (Visible_Declarations (T));
11541 while Present (N) loop
11542 if Nkind (N) = N_Pragma
11543 and then Pragma_Name (N) = Name_Relative_Deadline
11544 then
11545 return N;
11546 end if;
11548 Next (N);
11549 end loop;
11551 N := First (Private_Declarations (T));
11552 while Present (N) loop
11553 if Nkind (N) = N_Pragma
11554 and then Pragma_Name (N) = Name_Relative_Deadline
11555 then
11556 return N;
11557 end if;
11559 Next (N);
11560 end loop;
11562 raise Program_Error;
11563 end Get_Relative_Deadline_Pragma;
11565 -- Start of processing for Expand_N_Task_Type_Declaration
11567 begin
11568 -- If already expanded, nothing to do
11570 if Present (Corresponding_Record_Type (Tasktyp)) then
11571 return;
11572 end if;
11574 -- Here we will do the expansion
11576 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11578 Rec_Ent := Defining_Identifier (Rec_Decl);
11579 Cdecls := Component_Items (Component_List
11580 (Type_Definition (Rec_Decl)));
11582 Qualify_Entity_Names (N);
11584 -- First create the elaboration variable
11586 Elab_Decl :=
11587 Make_Object_Declaration (Loc,
11588 Defining_Identifier =>
11589 Make_Defining_Identifier (Sloc (Tasktyp),
11590 Chars => New_External_Name (Tasknm, 'E')),
11591 Aliased_Present => True,
11592 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
11593 Expression => New_Reference_To (Standard_False, Loc));
11595 Insert_After (N, Elab_Decl);
11597 -- Next create the declaration of the size variable (tasknmZ)
11599 Set_Storage_Size_Variable (Tasktyp,
11600 Make_Defining_Identifier (Sloc (Tasktyp),
11601 Chars => New_External_Name (Tasknm, 'Z')));
11603 if Present (Taskdef)
11604 and then Has_Storage_Size_Pragma (Taskdef)
11605 and then
11606 Is_Static_Expression
11607 (Expression
11608 (First (Pragma_Argument_Associations
11609 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11610 then
11611 Size_Decl :=
11612 Make_Object_Declaration (Loc,
11613 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11614 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
11615 Expression =>
11616 Convert_To (RTE (RE_Size_Type),
11617 Relocate_Node
11618 (Expression (First (Pragma_Argument_Associations
11619 (Get_Rep_Pragma
11620 (TaskId, Name_Storage_Size)))))));
11622 else
11623 Size_Decl :=
11624 Make_Object_Declaration (Loc,
11625 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11626 Object_Definition =>
11627 New_Reference_To (RTE (RE_Size_Type), Loc),
11628 Expression =>
11629 New_Reference_To (RTE (RE_Unspecified_Size), Loc));
11630 end if;
11632 Insert_After (Elab_Decl, Size_Decl);
11634 -- Next build the rest of the corresponding record declaration. This is
11635 -- done last, since the corresponding record initialization procedure
11636 -- will reference the previously created entities.
11638 -- Fill in the component declarations -- first the _Task_Id field
11640 Append_To (Cdecls,
11641 Make_Component_Declaration (Loc,
11642 Defining_Identifier =>
11643 Make_Defining_Identifier (Loc, Name_uTask_Id),
11644 Component_Definition =>
11645 Make_Component_Definition (Loc,
11646 Aliased_Present => False,
11647 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
11648 Loc))));
11650 -- Declare static ATCB (that is, created by the expander) if we are
11651 -- using the Restricted run time.
11653 if Restricted_Profile then
11654 Append_To (Cdecls,
11655 Make_Component_Declaration (Loc,
11656 Defining_Identifier =>
11657 Make_Defining_Identifier (Loc, Name_uATCB),
11659 Component_Definition =>
11660 Make_Component_Definition (Loc,
11661 Aliased_Present => True,
11662 Subtype_Indication => Make_Subtype_Indication (Loc,
11663 Subtype_Mark =>
11664 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11666 Constraint =>
11667 Make_Index_Or_Discriminant_Constraint (Loc,
11668 Constraints =>
11669 New_List (Make_Integer_Literal (Loc, 0)))))));
11671 end if;
11673 -- Declare static stack (that is, created by the expander) if we are
11674 -- using the Restricted run time on a bare board configuration.
11676 if Restricted_Profile
11677 and then Preallocated_Stacks_On_Target
11678 then
11679 -- First we need to extract the appropriate stack size
11681 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11683 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11684 declare
11685 Expr_N : constant Node_Id :=
11686 Expression (First (
11687 Pragma_Argument_Associations (
11688 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11689 Etyp : constant Entity_Id := Etype (Expr_N);
11690 P : constant Node_Id := Parent (Expr_N);
11692 begin
11693 -- The stack is defined inside the corresponding record.
11694 -- Therefore if the size of the stack is set by means of
11695 -- a discriminant, we must reference the discriminant of the
11696 -- corresponding record type.
11698 if Nkind (Expr_N) in N_Has_Entity
11699 and then Present (Discriminal_Link (Entity (Expr_N)))
11700 then
11701 Task_Size :=
11702 New_Reference_To
11703 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11704 Loc);
11705 Set_Parent (Task_Size, P);
11706 Set_Etype (Task_Size, Etyp);
11707 Set_Analyzed (Task_Size);
11709 else
11710 Task_Size := Relocate_Node (Expr_N);
11711 end if;
11712 end;
11714 else
11715 Task_Size :=
11716 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
11717 end if;
11719 Decl_Stack := Make_Component_Declaration (Loc,
11720 Defining_Identifier => Ent_Stack,
11722 Component_Definition =>
11723 Make_Component_Definition (Loc,
11724 Aliased_Present => True,
11725 Subtype_Indication => Make_Subtype_Indication (Loc,
11726 Subtype_Mark =>
11727 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11729 Constraint =>
11730 Make_Index_Or_Discriminant_Constraint (Loc,
11731 Constraints => New_List (Make_Range (Loc,
11732 Low_Bound => Make_Integer_Literal (Loc, 1),
11733 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11734 Task_Size)))))));
11736 Append_To (Cdecls, Decl_Stack);
11738 -- The appropriate alignment for the stack is ensured by the run-time
11739 -- code in charge of task creation.
11741 end if;
11743 -- Add components for entry families
11745 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11747 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11748 -- item is present.
11750 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11751 Append_To (Cdecls,
11752 Make_Component_Declaration (Loc,
11753 Defining_Identifier =>
11754 Make_Defining_Identifier (Loc, Name_uPriority),
11755 Component_Definition =>
11756 Make_Component_Definition (Loc,
11757 Aliased_Present => False,
11758 Subtype_Indication =>
11759 New_Reference_To (Standard_Integer, Loc))));
11760 end if;
11762 -- Add the _Size component if a Storage_Size pragma is present
11764 if Present (Taskdef)
11765 and then Has_Storage_Size_Pragma (Taskdef)
11766 then
11767 Append_To (Cdecls,
11768 Make_Component_Declaration (Loc,
11769 Defining_Identifier =>
11770 Make_Defining_Identifier (Loc, Name_uSize),
11772 Component_Definition =>
11773 Make_Component_Definition (Loc,
11774 Aliased_Present => False,
11775 Subtype_Indication =>
11776 New_Reference_To (RTE (RE_Size_Type), Loc)),
11778 Expression =>
11779 Convert_To (RTE (RE_Size_Type),
11780 Relocate_Node (
11781 Expression (First (
11782 Pragma_Argument_Associations (
11783 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11784 end if;
11786 -- Add the _Task_Info component if a Task_Info pragma is present
11788 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11789 Append_To (Cdecls,
11790 Make_Component_Declaration (Loc,
11791 Defining_Identifier =>
11792 Make_Defining_Identifier (Loc, Name_uTask_Info),
11794 Component_Definition =>
11795 Make_Component_Definition (Loc,
11796 Aliased_Present => False,
11797 Subtype_Indication =>
11798 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
11800 Expression => New_Copy (
11801 Expression (First (
11802 Pragma_Argument_Associations (
11803 Get_Rep_Pragma
11804 (TaskId, Name_Task_Info, Check_Parents => False)))))));
11805 end if;
11807 -- Add the _CPU component if a CPU rep item is present
11809 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
11810 Append_To (Cdecls,
11811 Make_Component_Declaration (Loc,
11812 Defining_Identifier =>
11813 Make_Defining_Identifier (Loc, Name_uCPU),
11815 Component_Definition =>
11816 Make_Component_Definition (Loc,
11817 Aliased_Present => False,
11818 Subtype_Indication =>
11819 New_Reference_To (RTE (RE_CPU_Range), Loc))));
11820 end if;
11822 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11823 -- present. If we are using a restricted run time this component will
11824 -- not be added (deadlines are not allowed by the Ravenscar profile).
11826 if not Restricted_Profile
11827 and then Present (Taskdef)
11828 and then Has_Relative_Deadline_Pragma (Taskdef)
11829 then
11830 Append_To (Cdecls,
11831 Make_Component_Declaration (Loc,
11832 Defining_Identifier =>
11833 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11835 Component_Definition =>
11836 Make_Component_Definition (Loc,
11837 Aliased_Present => False,
11838 Subtype_Indication =>
11839 New_Reference_To (RTE (RE_Time_Span), Loc)),
11841 Expression =>
11842 Convert_To (RTE (RE_Time_Span),
11843 Relocate_Node (
11844 Expression (First (
11845 Pragma_Argument_Associations (
11846 Get_Relative_Deadline_Pragma (Taskdef))))))));
11847 end if;
11849 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
11850 -- item is present. If we are using a restricted run time this component
11851 -- will not be added (dispatching domains are not allowed by the
11852 -- Ravenscar profile).
11854 if not Restricted_Profile
11855 and then
11856 Has_Rep_Item
11857 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
11858 then
11859 Append_To (Cdecls,
11860 Make_Component_Declaration (Loc,
11861 Defining_Identifier =>
11862 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
11864 Component_Definition =>
11865 Make_Component_Definition (Loc,
11866 Aliased_Present => False,
11867 Subtype_Indication =>
11868 New_Reference_To
11869 (RTE (RE_Dispatching_Domain_Access), Loc))));
11870 end if;
11872 Insert_After (Size_Decl, Rec_Decl);
11874 -- Analyze the record declaration immediately after construction,
11875 -- because the initialization procedure is needed for single task
11876 -- declarations before the next entity is analyzed.
11878 Analyze (Rec_Decl);
11880 -- Create the declaration of the task body procedure
11882 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
11883 Body_Decl :=
11884 Make_Subprogram_Declaration (Loc,
11885 Specification => Proc_Spec);
11887 Insert_After (Rec_Decl, Body_Decl);
11889 -- The subprogram does not comes from source, so we have to indicate the
11890 -- need for debugging information explicitly.
11892 if Comes_From_Source (Original_Node (N)) then
11893 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
11894 end if;
11896 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
11897 -- the corresponding record has been frozen.
11899 if Ada_Version >= Ada_2005 then
11900 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
11901 end if;
11903 -- Ada 2005 (AI-345): We must defer freezing to allow further
11904 -- declaration of primitive subprograms covering task interfaces
11906 if Ada_Version <= Ada_95 then
11908 -- Now we can freeze the corresponding record. This needs manually
11909 -- freezing, since it is really part of the task type, and the task
11910 -- type is frozen at this stage. We of course need the initialization
11911 -- procedure for this corresponding record type and we won't get it
11912 -- in time if we don't freeze now.
11914 declare
11915 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
11916 begin
11917 if Is_Non_Empty_List (L) then
11918 Insert_List_After (Body_Decl, L);
11919 end if;
11920 end;
11921 end if;
11923 -- Complete the expansion of access types to the current task type, if
11924 -- any were declared.
11926 Expand_Previous_Access_Type (Tasktyp);
11928 -- Create wrappers for entries that have pre/postconditions
11930 declare
11931 Ent : Entity_Id;
11933 begin
11934 Ent := First_Entity (Tasktyp);
11935 while Present (Ent) loop
11936 if Ekind_In (Ent, E_Entry, E_Entry_Family)
11937 and then Present (Pre_Post_Conditions (Contract (Ent)))
11938 then
11939 Build_PPC_Wrapper (Ent, N);
11940 end if;
11942 Next_Entity (Ent);
11943 end loop;
11944 end;
11945 end Expand_N_Task_Type_Declaration;
11947 -------------------------------
11948 -- Expand_N_Timed_Entry_Call --
11949 -------------------------------
11951 -- A timed entry call in normal case is not implemented using ATC mechanism
11952 -- anymore for efficiency reason.
11954 -- select
11955 -- T.E;
11956 -- S1;
11957 -- or
11958 -- delay D;
11959 -- S2;
11960 -- end select;
11962 -- is expanded as follows:
11964 -- 1) When T.E is a task entry_call;
11966 -- declare
11967 -- B : Boolean;
11968 -- X : Task_Entry_Index := <entry index>;
11969 -- DX : Duration := To_Duration (D);
11970 -- M : Delay_Mode := <discriminant>;
11971 -- P : parms := (parm, parm, parm);
11973 -- begin
11974 -- Timed_Protected_Entry_Call
11975 -- (<acceptor-task>, X, P'Address, DX, M, B);
11976 -- if B then
11977 -- S1;
11978 -- else
11979 -- S2;
11980 -- end if;
11981 -- end;
11983 -- 2) When T.E is a protected entry_call;
11985 -- declare
11986 -- B : Boolean;
11987 -- X : Protected_Entry_Index := <entry index>;
11988 -- DX : Duration := To_Duration (D);
11989 -- M : Delay_Mode := <discriminant>;
11990 -- P : parms := (parm, parm, parm);
11992 -- begin
11993 -- Timed_Protected_Entry_Call
11994 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
11995 -- if B then
11996 -- S1;
11997 -- else
11998 -- S2;
11999 -- end if;
12000 -- end;
12002 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12003 -- is no delay and the triggering statements are executed. We first
12004 -- determine the kind of of the triggering call and then execute a
12005 -- synchronized operation or a direct call.
12007 -- declare
12008 -- B : Boolean := False;
12009 -- C : Ada.Tags.Prim_Op_Kind;
12010 -- DX : Duration := To_Duration (D)
12011 -- K : Ada.Tags.Tagged_Kind :=
12012 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12013 -- M : Integer :=...;
12014 -- P : Parameters := (Param1 .. ParamN);
12015 -- S : Integer;
12017 -- begin
12018 -- if K = Ada.Tags.TK_Limited_Tagged
12019 -- or else K = Ada.Tags.TK_Tagged
12020 -- then
12021 -- <dispatching-call>;
12022 -- B := True;
12024 -- else
12025 -- S :=
12026 -- Ada.Tags.Get_Offset_Index
12027 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12029 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12031 -- if C = POK_Protected_Entry
12032 -- or else C = POK_Task_Entry
12033 -- then
12034 -- Param1 := P.Param1;
12035 -- ...
12036 -- ParamN := P.ParamN;
12037 -- end if;
12039 -- if B then
12040 -- if C = POK_Procedure
12041 -- or else C = POK_Protected_Procedure
12042 -- or else C = POK_Task_Procedure
12043 -- then
12044 -- <dispatching-call>;
12045 -- end if;
12046 -- end if;
12047 -- end if;
12049 -- if B then
12050 -- <triggering-statements>
12051 -- else
12052 -- <timed-statements>
12053 -- end if;
12054 -- end;
12056 -- The triggering statement and the sequence of timed statements have not
12057 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12058 -- global references if within an instantiation.
12060 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12061 Loc : constant Source_Ptr := Sloc (N);
12063 Actuals : List_Id;
12064 Blk_Typ : Entity_Id;
12065 Call : Node_Id;
12066 Call_Ent : Entity_Id;
12067 Conc_Typ_Stmts : List_Id;
12068 Concval : Node_Id;
12069 D_Alt : constant Node_Id := Delay_Alternative (N);
12070 D_Conv : Node_Id;
12071 D_Disc : Node_Id;
12072 D_Stat : Node_Id := Delay_Statement (D_Alt);
12073 D_Stats : List_Id;
12074 D_Type : Entity_Id;
12075 Decls : List_Id;
12076 Dummy : Node_Id;
12077 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12078 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12079 E_Stats : List_Id;
12080 Ename : Node_Id;
12081 Formals : List_Id;
12082 Index : Node_Id;
12083 Is_Disp_Select : Boolean;
12084 Lim_Typ_Stmts : List_Id;
12085 N_Stats : List_Id;
12086 Obj : Entity_Id;
12087 Param : Node_Id;
12088 Params : List_Id;
12089 Stmt : Node_Id;
12090 Stmts : List_Id;
12091 Unpack : List_Id;
12093 B : Entity_Id; -- Call status flag
12094 C : Entity_Id; -- Call kind
12095 D : Entity_Id; -- Delay
12096 K : Entity_Id; -- Tagged kind
12097 M : Entity_Id; -- Delay mode
12098 P : Entity_Id; -- Parameter block
12099 S : Entity_Id; -- Primitive operation slot
12101 -- Start of processing for Expand_N_Timed_Entry_Call
12103 begin
12104 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12105 -- was already reported on spec, so do not attempt to expand the call.
12107 if Restriction_Active (No_Select_Statements) then
12108 return;
12109 end if;
12111 Process_Statements_For_Controlled_Objects (E_Alt);
12112 Process_Statements_For_Controlled_Objects (D_Alt);
12114 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12116 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12117 -- may wrap them in blocks.
12119 E_Stats := Statements (E_Alt);
12120 D_Stats := Statements (D_Alt);
12122 -- The arguments in the call may require dynamic allocation, and the
12123 -- call statement may have been transformed into a block. The block
12124 -- may contain additional declarations for internal entities, and the
12125 -- original call is found by sequential search.
12127 if Nkind (E_Call) = N_Block_Statement then
12128 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12129 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12130 N_Entry_Call_Statement)
12131 loop
12132 Next (E_Call);
12133 end loop;
12134 end if;
12136 Is_Disp_Select :=
12137 Ada_Version >= Ada_2005
12138 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12140 if Is_Disp_Select then
12141 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12142 Decls := New_List;
12144 Stmts := New_List;
12146 -- Generate:
12147 -- B : Boolean := False;
12149 B := Build_B (Loc, Decls);
12151 -- Generate:
12152 -- C : Ada.Tags.Prim_Op_Kind;
12154 C := Build_C (Loc, Decls);
12156 -- Because the analysis of all statements was disabled, manually
12157 -- analyze the delay statement.
12159 Analyze (D_Stat);
12160 D_Stat := Original_Node (D_Stat);
12162 else
12163 -- Build an entry call using Simple_Entry_Call
12165 Extract_Entry (E_Call, Concval, Ename, Index);
12166 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12168 Decls := Declarations (E_Call);
12169 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12171 if No (Decls) then
12172 Decls := New_List;
12173 end if;
12175 -- Generate:
12176 -- B : Boolean;
12178 B := Make_Defining_Identifier (Loc, Name_uB);
12180 Prepend_To (Decls,
12181 Make_Object_Declaration (Loc,
12182 Defining_Identifier => B,
12183 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
12184 end if;
12186 -- Duration and mode processing
12188 D_Type := Base_Type (Etype (Expression (D_Stat)));
12190 -- Use the type of the delay expression (Calendar or Real_Time) to
12191 -- generate the appropriate conversion.
12193 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12194 D_Disc := Make_Integer_Literal (Loc, 0);
12195 D_Conv := Relocate_Node (Expression (D_Stat));
12197 elsif Is_RTE (D_Type, RO_CA_Time) then
12198 D_Disc := Make_Integer_Literal (Loc, 1);
12199 D_Conv :=
12200 Make_Function_Call (Loc,
12201 Name => New_Reference_To (RTE (RO_CA_To_Duration), Loc),
12202 Parameter_Associations =>
12203 New_List (New_Copy (Expression (D_Stat))));
12205 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12206 D_Disc := Make_Integer_Literal (Loc, 2);
12207 D_Conv :=
12208 Make_Function_Call (Loc,
12209 Name => New_Reference_To (RTE (RO_RT_To_Duration), Loc),
12210 Parameter_Associations =>
12211 New_List (New_Copy (Expression (D_Stat))));
12212 end if;
12214 D := Make_Temporary (Loc, 'D');
12216 -- Generate:
12217 -- D : Duration;
12219 Append_To (Decls,
12220 Make_Object_Declaration (Loc,
12221 Defining_Identifier => D,
12222 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
12224 M := Make_Temporary (Loc, 'M');
12226 -- Generate:
12227 -- M : Integer := (0 | 1 | 2);
12229 Append_To (Decls,
12230 Make_Object_Declaration (Loc,
12231 Defining_Identifier => M,
12232 Object_Definition => New_Reference_To (Standard_Integer, Loc),
12233 Expression => D_Disc));
12235 -- Do the assignment at this stage only because the evaluation of the
12236 -- expression must not occur before (see ACVC C97302A).
12238 Append_To (Stmts,
12239 Make_Assignment_Statement (Loc,
12240 Name => New_Reference_To (D, Loc),
12241 Expression => D_Conv));
12243 -- Parameter block processing
12245 -- Manually create the parameter block for dispatching calls. In the
12246 -- case of entries, the block has already been created during the call
12247 -- to Build_Simple_Entry_Call.
12249 if Is_Disp_Select then
12251 -- Tagged kind processing, generate:
12252 -- K : Ada.Tags.Tagged_Kind :=
12253 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12255 K := Build_K (Loc, Decls, Obj);
12257 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12258 P :=
12259 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12261 -- Dispatch table slot processing, generate:
12262 -- S : Integer;
12264 S := Build_S (Loc, Decls);
12266 -- Generate:
12267 -- S := Ada.Tags.Get_Offset_Index
12268 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12270 Conc_Typ_Stmts :=
12271 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12273 -- Generate:
12274 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12276 -- where Obj is the controlling formal parameter, S is the dispatch
12277 -- table slot number of the dispatching operation, P is the wrapped
12278 -- parameter block, D is the duration, M is the duration mode, C is
12279 -- the call kind and B is the call status.
12281 Params := New_List;
12283 Append_To (Params, New_Copy_Tree (Obj));
12284 Append_To (Params, New_Reference_To (S, Loc));
12285 Append_To (Params,
12286 Make_Attribute_Reference (Loc,
12287 Prefix => New_Reference_To (P, Loc),
12288 Attribute_Name => Name_Address));
12289 Append_To (Params, New_Reference_To (D, Loc));
12290 Append_To (Params, New_Reference_To (M, Loc));
12291 Append_To (Params, New_Reference_To (C, Loc));
12292 Append_To (Params, New_Reference_To (B, Loc));
12294 Append_To (Conc_Typ_Stmts,
12295 Make_Procedure_Call_Statement (Loc,
12296 Name =>
12297 New_Reference_To
12298 (Find_Prim_Op
12299 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12300 Parameter_Associations => Params));
12302 -- Generate:
12303 -- if C = POK_Protected_Entry
12304 -- or else C = POK_Task_Entry
12305 -- then
12306 -- Param1 := P.Param1;
12307 -- ...
12308 -- ParamN := P.ParamN;
12309 -- end if;
12311 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12313 -- Generate the if statement only when the packed parameters need
12314 -- explicit assignments to their corresponding actuals.
12316 if Present (Unpack) then
12317 Append_To (Conc_Typ_Stmts,
12318 Make_Implicit_If_Statement (N,
12320 Condition =>
12321 Make_Or_Else (Loc,
12322 Left_Opnd =>
12323 Make_Op_Eq (Loc,
12324 Left_Opnd => New_Reference_To (C, Loc),
12325 Right_Opnd =>
12326 New_Reference_To
12327 (RTE (RE_POK_Protected_Entry), Loc)),
12329 Right_Opnd =>
12330 Make_Op_Eq (Loc,
12331 Left_Opnd => New_Reference_To (C, Loc),
12332 Right_Opnd =>
12333 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
12335 Then_Statements => Unpack));
12336 end if;
12338 -- Generate:
12340 -- if B then
12341 -- if C = POK_Procedure
12342 -- or else C = POK_Protected_Procedure
12343 -- or else C = POK_Task_Procedure
12344 -- then
12345 -- <dispatching-call>
12346 -- end if;
12347 -- end if;
12349 N_Stats := New_List (
12350 Make_Implicit_If_Statement (N,
12351 Condition =>
12352 Make_Or_Else (Loc,
12353 Left_Opnd =>
12354 Make_Op_Eq (Loc,
12355 Left_Opnd => New_Reference_To (C, Loc),
12356 Right_Opnd =>
12357 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
12359 Right_Opnd =>
12360 Make_Or_Else (Loc,
12361 Left_Opnd =>
12362 Make_Op_Eq (Loc,
12363 Left_Opnd => New_Reference_To (C, Loc),
12364 Right_Opnd =>
12365 New_Reference_To (RTE (
12366 RE_POK_Protected_Procedure), Loc)),
12367 Right_Opnd =>
12368 Make_Op_Eq (Loc,
12369 Left_Opnd => New_Reference_To (C, Loc),
12370 Right_Opnd =>
12371 New_Reference_To
12372 (RTE (RE_POK_Task_Procedure), Loc)))),
12374 Then_Statements => New_List (E_Call)));
12376 Append_To (Conc_Typ_Stmts,
12377 Make_Implicit_If_Statement (N,
12378 Condition => New_Reference_To (B, Loc),
12379 Then_Statements => N_Stats));
12381 -- Generate:
12382 -- <dispatching-call>;
12383 -- B := True;
12385 Lim_Typ_Stmts :=
12386 New_List (New_Copy_Tree (E_Call),
12387 Make_Assignment_Statement (Loc,
12388 Name => New_Occurrence_Of (B, Loc),
12389 Expression => New_Occurrence_Of (Standard_True, Loc)));
12391 -- Generate:
12392 -- if K = Ada.Tags.TK_Limited_Tagged
12393 -- or else K = Ada.Tags.TK_Tagged
12394 -- then
12395 -- Lim_Typ_Stmts
12396 -- else
12397 -- Conc_Typ_Stmts
12398 -- end if;
12400 Append_To (Stmts,
12401 Make_Implicit_If_Statement (N,
12402 Condition => Build_Dispatching_Tag_Check (K, N),
12403 Then_Statements => Lim_Typ_Stmts,
12404 Else_Statements => Conc_Typ_Stmts));
12406 -- Generate:
12408 -- if B then
12409 -- <triggering-statements>
12410 -- else
12411 -- <timed-statements>
12412 -- end if;
12414 Append_To (Stmts,
12415 Make_Implicit_If_Statement (N,
12416 Condition => New_Occurrence_Of (B, Loc),
12417 Then_Statements => E_Stats,
12418 Else_Statements => D_Stats));
12420 else
12421 -- Simple case of a non-dispatching trigger. Skip assignments to
12422 -- temporaries created for in-out parameters.
12424 -- This makes unwarranted assumptions about the shape of the expanded
12425 -- tree for the call, and should be cleaned up ???
12427 Stmt := First (Stmts);
12428 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12429 Next (Stmt);
12430 end loop;
12432 -- Do the assignment at this stage only because the evaluation
12433 -- of the expression must not occur before (see ACVC C97302A).
12435 Insert_Before (Stmt,
12436 Make_Assignment_Statement (Loc,
12437 Name => New_Reference_To (D, Loc),
12438 Expression => D_Conv));
12440 Call := Stmt;
12441 Params := Parameter_Associations (Call);
12443 -- For a protected type, we build a Timed_Protected_Entry_Call
12445 if Is_Protected_Type (Etype (Concval)) then
12447 -- Create a new call statement
12449 Param := First (Params);
12450 while Present (Param)
12451 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12452 loop
12453 Next (Param);
12454 end loop;
12456 Dummy := Remove_Next (Next (Param));
12458 -- Remove garbage is following the Cancel_Param if present
12460 Dummy := Next (Param);
12462 -- Remove the mode of the Protected_Entry_Call call, then remove
12463 -- the Communication_Block of the Protected_Entry_Call call, and
12464 -- finally add Duration and a Delay_Mode parameter
12466 pragma Assert (Present (Param));
12467 Rewrite (Param, New_Reference_To (D, Loc));
12469 Rewrite (Dummy, New_Reference_To (M, Loc));
12471 -- Add a Boolean flag for successful entry call
12473 Append_To (Params, New_Reference_To (B, Loc));
12475 case Corresponding_Runtime_Package (Etype (Concval)) is
12476 when System_Tasking_Protected_Objects_Entries =>
12477 Rewrite (Call,
12478 Make_Procedure_Call_Statement (Loc,
12479 Name =>
12480 New_Reference_To
12481 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12482 Parameter_Associations => Params));
12484 when System_Tasking_Protected_Objects_Single_Entry =>
12485 Param := First (Params);
12486 while Present (Param)
12487 and then not
12488 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
12489 loop
12490 Next (Param);
12491 end loop;
12493 Remove (Param);
12495 Rewrite (Call,
12496 Make_Procedure_Call_Statement (Loc,
12497 Name =>
12498 New_Reference_To
12499 (RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
12500 Parameter_Associations => Params));
12502 when others =>
12503 raise Program_Error;
12504 end case;
12506 -- For the task case, build a Timed_Task_Entry_Call
12508 else
12509 -- Create a new call statement
12511 Append_To (Params, New_Reference_To (D, Loc));
12512 Append_To (Params, New_Reference_To (M, Loc));
12513 Append_To (Params, New_Reference_To (B, Loc));
12515 Rewrite (Call,
12516 Make_Procedure_Call_Statement (Loc,
12517 Name =>
12518 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
12519 Parameter_Associations => Params));
12520 end if;
12522 Append_To (Stmts,
12523 Make_Implicit_If_Statement (N,
12524 Condition => New_Reference_To (B, Loc),
12525 Then_Statements => E_Stats,
12526 Else_Statements => D_Stats));
12527 end if;
12529 Rewrite (N,
12530 Make_Block_Statement (Loc,
12531 Declarations => Decls,
12532 Handled_Statement_Sequence =>
12533 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12535 Analyze (N);
12536 end Expand_N_Timed_Entry_Call;
12538 ----------------------------------------
12539 -- Expand_Protected_Body_Declarations --
12540 ----------------------------------------
12542 procedure Expand_Protected_Body_Declarations
12543 (N : Node_Id;
12544 Spec_Id : Entity_Id)
12546 begin
12547 if No_Run_Time_Mode then
12548 Error_Msg_CRT ("protected body", N);
12549 return;
12551 elsif Expander_Active then
12553 -- Associate discriminals with the first subprogram or entry body to
12554 -- be expanded.
12556 if Present (First_Protected_Operation (Declarations (N))) then
12557 Set_Discriminals (Parent (Spec_Id));
12558 end if;
12559 end if;
12560 end Expand_Protected_Body_Declarations;
12562 -------------------------
12563 -- External_Subprogram --
12564 -------------------------
12566 function External_Subprogram (E : Entity_Id) return Entity_Id is
12567 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12569 begin
12570 -- The internal and external subprograms follow each other on the entity
12571 -- chain. Note that previously private operations had no separate
12572 -- external subprogram. We now create one in all cases, because a
12573 -- private operation may actually appear in an external call, through
12574 -- a 'Access reference used for a callback.
12576 -- If the operation is a function that returns an anonymous access type,
12577 -- the corresponding itype appears before the operation, and must be
12578 -- skipped.
12580 -- This mechanism is fragile, there should be a real link between the
12581 -- two versions of the operation, but there is no place to put it ???
12583 if Is_Access_Type (Next_Entity (Subp)) then
12584 return Next_Entity (Next_Entity (Subp));
12585 else
12586 return Next_Entity (Subp);
12587 end if;
12588 end External_Subprogram;
12590 ------------------------------
12591 -- Extract_Dispatching_Call --
12592 ------------------------------
12594 procedure Extract_Dispatching_Call
12595 (N : Node_Id;
12596 Call_Ent : out Entity_Id;
12597 Object : out Entity_Id;
12598 Actuals : out List_Id;
12599 Formals : out List_Id)
12601 Call_Nam : Node_Id;
12603 begin
12604 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12606 if Present (Original_Node (N)) then
12607 Call_Nam := Name (Original_Node (N));
12608 else
12609 Call_Nam := Name (N);
12610 end if;
12612 -- Retrieve the name of the dispatching procedure. It contains the
12613 -- dispatch table slot number.
12615 loop
12616 case Nkind (Call_Nam) is
12617 when N_Identifier =>
12618 exit;
12620 when N_Selected_Component =>
12621 Call_Nam := Selector_Name (Call_Nam);
12623 when others =>
12624 raise Program_Error;
12626 end case;
12627 end loop;
12629 Actuals := Parameter_Associations (N);
12630 Call_Ent := Entity (Call_Nam);
12631 Formals := Parameter_Specifications (Parent (Call_Ent));
12632 Object := First (Actuals);
12634 if Present (Original_Node (Object)) then
12635 Object := Original_Node (Object);
12636 end if;
12638 -- If the type of the dispatching object is an access type then return
12639 -- an explicit dereference.
12641 if Is_Access_Type (Etype (Object)) then
12642 Object := Make_Explicit_Dereference (Sloc (N), Object);
12643 Analyze (Object);
12644 end if;
12645 end Extract_Dispatching_Call;
12647 -------------------
12648 -- Extract_Entry --
12649 -------------------
12651 procedure Extract_Entry
12652 (N : Node_Id;
12653 Concval : out Node_Id;
12654 Ename : out Node_Id;
12655 Index : out Node_Id)
12657 Nam : constant Node_Id := Name (N);
12659 begin
12660 -- For a simple entry, the name is a selected component, with the
12661 -- prefix being the task value, and the selector being the entry.
12663 if Nkind (Nam) = N_Selected_Component then
12664 Concval := Prefix (Nam);
12665 Ename := Selector_Name (Nam);
12666 Index := Empty;
12668 -- For a member of an entry family, the name is an indexed component
12669 -- where the prefix is a selected component, whose prefix in turn is
12670 -- the task value, and whose selector is the entry family. The single
12671 -- expression in the expressions list of the indexed component is the
12672 -- subscript for the family.
12674 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12675 Concval := Prefix (Prefix (Nam));
12676 Ename := Selector_Name (Prefix (Nam));
12677 Index := First (Expressions (Nam));
12678 end if;
12679 end Extract_Entry;
12681 -------------------
12682 -- Family_Offset --
12683 -------------------
12685 function Family_Offset
12686 (Loc : Source_Ptr;
12687 Hi : Node_Id;
12688 Lo : Node_Id;
12689 Ttyp : Entity_Id;
12690 Cap : Boolean) return Node_Id
12692 Ityp : Entity_Id;
12693 Real_Hi : Node_Id;
12694 Real_Lo : Node_Id;
12696 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12697 -- If one of the bounds is a reference to a discriminant, replace with
12698 -- corresponding discriminal of type. Within the body of a task retrieve
12699 -- the renamed discriminant by simple visibility, using its generated
12700 -- name. Within a protected object, find the original discriminant and
12701 -- replace it with the discriminal of the current protected operation.
12703 ------------------------------
12704 -- Convert_Discriminant_Ref --
12705 ------------------------------
12707 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12708 Loc : constant Source_Ptr := Sloc (Bound);
12709 B : Node_Id;
12710 D : Entity_Id;
12712 begin
12713 if Is_Entity_Name (Bound)
12714 and then Ekind (Entity (Bound)) = E_Discriminant
12715 then
12716 if Is_Task_Type (Ttyp)
12717 and then Has_Completion (Ttyp)
12718 then
12719 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12720 Find_Direct_Name (B);
12722 elsif Is_Protected_Type (Ttyp) then
12723 D := First_Discriminant (Ttyp);
12724 while Chars (D) /= Chars (Entity (Bound)) loop
12725 Next_Discriminant (D);
12726 end loop;
12728 B := New_Reference_To (Discriminal (D), Loc);
12730 else
12731 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
12732 end if;
12734 elsif Nkind (Bound) = N_Attribute_Reference then
12735 return Bound;
12737 else
12738 B := New_Copy_Tree (Bound);
12739 end if;
12741 return
12742 Make_Attribute_Reference (Loc,
12743 Attribute_Name => Name_Pos,
12744 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12745 Expressions => New_List (B));
12746 end Convert_Discriminant_Ref;
12748 -- Start of processing for Family_Offset
12750 begin
12751 Real_Hi := Convert_Discriminant_Ref (Hi);
12752 Real_Lo := Convert_Discriminant_Ref (Lo);
12754 if Cap then
12755 if Is_Task_Type (Ttyp) then
12756 Ityp := RTE (RE_Task_Entry_Index);
12757 else
12758 Ityp := RTE (RE_Protected_Entry_Index);
12759 end if;
12761 Real_Hi :=
12762 Make_Attribute_Reference (Loc,
12763 Prefix => New_Reference_To (Ityp, Loc),
12764 Attribute_Name => Name_Min,
12765 Expressions => New_List (
12766 Real_Hi,
12767 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12769 Real_Lo :=
12770 Make_Attribute_Reference (Loc,
12771 Prefix => New_Reference_To (Ityp, Loc),
12772 Attribute_Name => Name_Max,
12773 Expressions => New_List (
12774 Real_Lo,
12775 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12776 end if;
12778 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12779 end Family_Offset;
12781 -----------------
12782 -- Family_Size --
12783 -----------------
12785 function Family_Size
12786 (Loc : Source_Ptr;
12787 Hi : Node_Id;
12788 Lo : Node_Id;
12789 Ttyp : Entity_Id;
12790 Cap : Boolean) return Node_Id
12792 Ityp : Entity_Id;
12794 begin
12795 if Is_Task_Type (Ttyp) then
12796 Ityp := RTE (RE_Task_Entry_Index);
12797 else
12798 Ityp := RTE (RE_Protected_Entry_Index);
12799 end if;
12801 return
12802 Make_Attribute_Reference (Loc,
12803 Prefix => New_Reference_To (Ityp, Loc),
12804 Attribute_Name => Name_Max,
12805 Expressions => New_List (
12806 Make_Op_Add (Loc,
12807 Left_Opnd =>
12808 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12809 Right_Opnd =>
12810 Make_Integer_Literal (Loc, 1)),
12811 Make_Integer_Literal (Loc, 0)));
12812 end Family_Size;
12814 ----------------------------
12815 -- Find_Enclosing_Context --
12816 ----------------------------
12818 procedure Find_Enclosing_Context
12819 (N : Node_Id;
12820 Context : out Node_Id;
12821 Context_Id : out Entity_Id;
12822 Context_Decls : out List_Id)
12824 begin
12825 -- Traverse the parent chain looking for an enclosing body, block,
12826 -- package or return statement.
12828 Context := Parent (N);
12829 while not Nkind_In (Context, N_Block_Statement,
12830 N_Entry_Body,
12831 N_Extended_Return_Statement,
12832 N_Package_Body,
12833 N_Package_Declaration,
12834 N_Subprogram_Body,
12835 N_Task_Body)
12836 loop
12837 Context := Parent (Context);
12838 end loop;
12840 -- Extract the constituents of the context
12842 if Nkind (Context) = N_Extended_Return_Statement then
12843 Context_Decls := Return_Object_Declarations (Context);
12844 Context_Id := Return_Statement_Entity (Context);
12846 -- Package declarations and bodies use a common library-level activation
12847 -- chain or task master, therefore return the package declaration as the
12848 -- proper carrier for the appropriate flag.
12850 elsif Nkind (Context) = N_Package_Body then
12851 Context_Decls := Declarations (Context);
12852 Context_Id := Corresponding_Spec (Context);
12853 Context := Parent (Context_Id);
12855 if Nkind (Context) = N_Defining_Program_Unit_Name then
12856 Context := Parent (Parent (Context));
12857 else
12858 Context := Parent (Context);
12859 end if;
12861 elsif Nkind (Context) = N_Package_Declaration then
12862 Context_Decls := Visible_Declarations (Specification (Context));
12863 Context_Id := Defining_Unit_Name (Specification (Context));
12865 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12866 Context_Id := Defining_Identifier (Context_Id);
12867 end if;
12869 else
12870 Context_Decls := Declarations (Context);
12872 if Nkind (Context) = N_Block_Statement then
12873 Context_Id := Entity (Identifier (Context));
12875 elsif Nkind (Context) = N_Entry_Body then
12876 Context_Id := Defining_Identifier (Context);
12878 elsif Nkind (Context) = N_Subprogram_Body then
12879 if Present (Corresponding_Spec (Context)) then
12880 Context_Id := Corresponding_Spec (Context);
12881 else
12882 Context_Id := Defining_Unit_Name (Specification (Context));
12884 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12885 Context_Id := Defining_Identifier (Context_Id);
12886 end if;
12887 end if;
12889 elsif Nkind (Context) = N_Task_Body then
12890 Context_Id := Corresponding_Spec (Context);
12892 else
12893 raise Program_Error;
12894 end if;
12895 end if;
12897 pragma Assert (Present (Context));
12898 pragma Assert (Present (Context_Id));
12899 pragma Assert (Present (Context_Decls));
12900 end Find_Enclosing_Context;
12902 -----------------------
12903 -- Find_Master_Scope --
12904 -----------------------
12906 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
12907 S : Entity_Id;
12909 begin
12910 -- In Ada 2005, the master is the innermost enclosing scope that is not
12911 -- transient. If the enclosing block is the rewriting of a call or the
12912 -- scope is an extended return statement this is valid master. The
12913 -- master in an extended return is only used within the return, and is
12914 -- subsequently overwritten in Move_Activation_Chain, but it must exist
12915 -- now before that overwriting occurs.
12917 S := Scope (E);
12919 if Ada_Version >= Ada_2005 then
12920 while Is_Internal (S) loop
12921 if Nkind (Parent (S)) = N_Block_Statement
12922 and then
12923 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
12924 then
12925 exit;
12927 elsif Ekind (S) = E_Return_Statement then
12928 exit;
12930 else
12931 S := Scope (S);
12932 end if;
12933 end loop;
12934 end if;
12936 return S;
12937 end Find_Master_Scope;
12939 -------------------------------
12940 -- First_Protected_Operation --
12941 -------------------------------
12943 function First_Protected_Operation (D : List_Id) return Node_Id is
12944 First_Op : Node_Id;
12946 begin
12947 First_Op := First (D);
12948 while Present (First_Op)
12949 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
12950 loop
12951 Next (First_Op);
12952 end loop;
12954 return First_Op;
12955 end First_Protected_Operation;
12957 ---------------------------------------
12958 -- Install_Private_Data_Declarations --
12959 ---------------------------------------
12961 procedure Install_Private_Data_Declarations
12962 (Loc : Source_Ptr;
12963 Spec_Id : Entity_Id;
12964 Conc_Typ : Entity_Id;
12965 Body_Nod : Node_Id;
12966 Decls : List_Id;
12967 Barrier : Boolean := False;
12968 Family : Boolean := False)
12970 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
12971 Decl : Node_Id;
12972 Def : Node_Id;
12973 Insert_Node : Node_Id := Empty;
12974 Obj_Ent : Entity_Id;
12976 procedure Add (Decl : Node_Id);
12977 -- Add a single declaration after Insert_Node. If this is the first
12978 -- addition, Decl is added to the front of Decls and it becomes the
12979 -- insertion node.
12981 function Replace_Bound (Bound : Node_Id) return Node_Id;
12982 -- The bounds of an entry index may depend on discriminants, create a
12983 -- reference to the corresponding prival. Otherwise return a duplicate
12984 -- of the original bound.
12986 ---------
12987 -- Add --
12988 ---------
12990 procedure Add (Decl : Node_Id) is
12991 begin
12992 if No (Insert_Node) then
12993 Prepend_To (Decls, Decl);
12994 else
12995 Insert_After (Insert_Node, Decl);
12996 end if;
12998 Insert_Node := Decl;
12999 end Add;
13001 --------------------------
13002 -- Replace_Discriminant --
13003 --------------------------
13005 function Replace_Bound (Bound : Node_Id) return Node_Id is
13006 begin
13007 if Nkind (Bound) = N_Identifier
13008 and then Is_Discriminal (Entity (Bound))
13009 then
13010 return Make_Identifier (Loc, Chars (Entity (Bound)));
13011 else
13012 return Duplicate_Subexpr (Bound);
13013 end if;
13014 end Replace_Bound;
13016 -- Start of processing for Install_Private_Data_Declarations
13018 begin
13019 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13020 -- formal parameter _O, _object or _task depending on the context.
13022 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13024 -- Special processing of _O for barrier functions, protected entries
13025 -- and families.
13027 if Barrier
13028 or else
13029 (Is_Protected
13030 and then
13031 (Ekind (Spec_Id) = E_Entry
13032 or else Ekind (Spec_Id) = E_Entry_Family))
13033 then
13034 declare
13035 Conc_Rec : constant Entity_Id :=
13036 Corresponding_Record_Type (Conc_Typ);
13037 Typ_Id : constant Entity_Id :=
13038 Make_Defining_Identifier (Loc,
13039 New_External_Name (Chars (Conc_Rec), 'P'));
13040 begin
13041 -- Generate:
13042 -- type prot_typVP is access prot_typV;
13044 Decl :=
13045 Make_Full_Type_Declaration (Loc,
13046 Defining_Identifier => Typ_Id,
13047 Type_Definition =>
13048 Make_Access_To_Object_Definition (Loc,
13049 Subtype_Indication =>
13050 New_Reference_To (Conc_Rec, Loc)));
13051 Add (Decl);
13053 -- Generate:
13054 -- _object : prot_typVP := prot_typV (_O);
13056 Decl :=
13057 Make_Object_Declaration (Loc,
13058 Defining_Identifier =>
13059 Make_Defining_Identifier (Loc, Name_uObject),
13060 Object_Definition => New_Reference_To (Typ_Id, Loc),
13061 Expression =>
13062 Unchecked_Convert_To (Typ_Id,
13063 New_Reference_To (Obj_Ent, Loc)));
13064 Add (Decl);
13066 -- Set the reference to the concurrent object
13068 Obj_Ent := Defining_Identifier (Decl);
13069 end;
13070 end if;
13072 -- Step 2: Create the Protection object and build its declaration for
13073 -- any protected entry (family) of subprogram. Note for the lock-free
13074 -- implementation, the Protection object is not needed anymore.
13076 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13077 declare
13078 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13079 Prot_Typ : RE_Id;
13081 begin
13082 Set_Protection_Object (Spec_Id, Prot_Ent);
13084 -- Determine the proper protection type
13086 if Has_Attach_Handler (Conc_Typ)
13087 and then not Restricted_Profile
13088 then
13089 Prot_Typ := RE_Static_Interrupt_Protection;
13091 elsif Has_Interrupt_Handler (Conc_Typ)
13092 and then not Restriction_Active (No_Dynamic_Attachment)
13093 then
13094 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13096 else
13097 case Corresponding_Runtime_Package (Conc_Typ) is
13098 when System_Tasking_Protected_Objects_Entries =>
13099 Prot_Typ := RE_Protection_Entries;
13101 when System_Tasking_Protected_Objects_Single_Entry =>
13102 Prot_Typ := RE_Protection_Entry;
13104 when System_Tasking_Protected_Objects =>
13105 Prot_Typ := RE_Protection;
13107 when others =>
13108 raise Program_Error;
13109 end case;
13110 end if;
13112 -- Generate:
13113 -- conc_typR : protection_typ renames _object._object;
13115 Decl :=
13116 Make_Object_Renaming_Declaration (Loc,
13117 Defining_Identifier => Prot_Ent,
13118 Subtype_Mark =>
13119 New_Reference_To (RTE (Prot_Typ), Loc),
13120 Name =>
13121 Make_Selected_Component (Loc,
13122 Prefix => New_Reference_To (Obj_Ent, Loc),
13123 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13124 Add (Decl);
13125 end;
13126 end if;
13128 -- Step 3: Add discriminant renamings (if any)
13130 if Has_Discriminants (Conc_Typ) then
13131 declare
13132 D : Entity_Id;
13134 begin
13135 D := First_Discriminant (Conc_Typ);
13136 while Present (D) loop
13138 -- Adjust the source location
13140 Set_Sloc (Discriminal (D), Loc);
13142 -- Generate:
13143 -- discr_name : discr_typ renames _object.discr_name;
13144 -- or
13145 -- discr_name : discr_typ renames _task.discr_name;
13147 Decl :=
13148 Make_Object_Renaming_Declaration (Loc,
13149 Defining_Identifier => Discriminal (D),
13150 Subtype_Mark => New_Reference_To (Etype (D), Loc),
13151 Name =>
13152 Make_Selected_Component (Loc,
13153 Prefix => New_Reference_To (Obj_Ent, Loc),
13154 Selector_Name => Make_Identifier (Loc, Chars (D))));
13155 Add (Decl);
13157 Next_Discriminant (D);
13158 end loop;
13159 end;
13160 end if;
13162 -- Step 4: Add private component renamings (if any)
13164 if Is_Protected then
13165 Def := Protected_Definition (Parent (Conc_Typ));
13167 if Present (Private_Declarations (Def)) then
13168 declare
13169 Comp : Node_Id;
13170 Comp_Id : Entity_Id;
13171 Decl_Id : Entity_Id;
13173 begin
13174 Comp := First (Private_Declarations (Def));
13175 while Present (Comp) loop
13176 if Nkind (Comp) = N_Component_Declaration then
13177 Comp_Id := Defining_Identifier (Comp);
13178 Decl_Id :=
13179 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13181 -- Minimal decoration
13183 if Ekind (Spec_Id) = E_Function then
13184 Set_Ekind (Decl_Id, E_Constant);
13185 else
13186 Set_Ekind (Decl_Id, E_Variable);
13187 end if;
13189 Set_Prival (Comp_Id, Decl_Id);
13190 Set_Prival_Link (Decl_Id, Comp_Id);
13191 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13193 -- Generate:
13194 -- comp_name : comp_typ renames _object.comp_name;
13196 Decl :=
13197 Make_Object_Renaming_Declaration (Loc,
13198 Defining_Identifier => Decl_Id,
13199 Subtype_Mark =>
13200 New_Reference_To (Etype (Comp_Id), Loc),
13201 Name =>
13202 Make_Selected_Component (Loc,
13203 Prefix =>
13204 New_Reference_To (Obj_Ent, Loc),
13205 Selector_Name =>
13206 Make_Identifier (Loc, Chars (Comp_Id))));
13207 Add (Decl);
13208 end if;
13210 Next (Comp);
13211 end loop;
13212 end;
13213 end if;
13214 end if;
13216 -- Step 5: Add the declaration of the entry index and the associated
13217 -- type for barrier functions and entry families.
13219 if (Barrier and then Family)
13220 or else Ekind (Spec_Id) = E_Entry_Family
13221 then
13222 declare
13223 E : constant Entity_Id := Index_Object (Spec_Id);
13224 Index : constant Entity_Id :=
13225 Defining_Identifier (
13226 Entry_Index_Specification (
13227 Entry_Body_Formal_Part (Body_Nod)));
13228 Index_Con : constant Entity_Id :=
13229 Make_Defining_Identifier (Loc, Chars (Index));
13230 High : Node_Id;
13231 Index_Typ : Entity_Id;
13232 Low : Node_Id;
13234 begin
13235 -- Minimal decoration
13237 Set_Ekind (Index_Con, E_Constant);
13238 Set_Entry_Index_Constant (Index, Index_Con);
13239 Set_Discriminal_Link (Index_Con, Index);
13241 -- Retrieve the bounds of the entry family
13243 High := Type_High_Bound (Etype (Index));
13244 Low := Type_Low_Bound (Etype (Index));
13246 -- In the simple case the entry family is given by a subtype
13247 -- mark and the index constant has the same type.
13249 if Is_Entity_Name (Original_Node (
13250 Discrete_Subtype_Definition (Parent (Index))))
13251 then
13252 Index_Typ := Etype (Index);
13254 -- Otherwise a new subtype declaration is required
13256 else
13257 High := Replace_Bound (High);
13258 Low := Replace_Bound (Low);
13260 Index_Typ := Make_Temporary (Loc, 'J');
13262 -- Generate:
13263 -- subtype Jnn is <Etype of Index> range Low .. High;
13265 Decl :=
13266 Make_Subtype_Declaration (Loc,
13267 Defining_Identifier => Index_Typ,
13268 Subtype_Indication =>
13269 Make_Subtype_Indication (Loc,
13270 Subtype_Mark =>
13271 New_Reference_To (Base_Type (Etype (Index)), Loc),
13272 Constraint =>
13273 Make_Range_Constraint (Loc,
13274 Range_Expression =>
13275 Make_Range (Loc, Low, High))));
13276 Add (Decl);
13277 end if;
13279 Set_Etype (Index_Con, Index_Typ);
13281 -- Create the object which designates the index:
13282 -- J : constant Jnn :=
13283 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13285 -- where Jnn is the subtype created above or the original type of
13286 -- the index, _E is a formal of the protected body subprogram and
13287 -- <index expr> is the index of the first family member.
13289 Decl :=
13290 Make_Object_Declaration (Loc,
13291 Defining_Identifier => Index_Con,
13292 Constant_Present => True,
13293 Object_Definition =>
13294 New_Reference_To (Index_Typ, Loc),
13296 Expression =>
13297 Make_Attribute_Reference (Loc,
13298 Prefix =>
13299 New_Reference_To (Index_Typ, Loc),
13300 Attribute_Name => Name_Val,
13302 Expressions => New_List (
13304 Make_Op_Add (Loc,
13305 Left_Opnd =>
13306 Make_Op_Subtract (Loc,
13307 Left_Opnd =>
13308 New_Reference_To (E, Loc),
13309 Right_Opnd =>
13310 Entry_Index_Expression (Loc,
13311 Defining_Identifier (Body_Nod),
13312 Empty, Conc_Typ)),
13314 Right_Opnd =>
13315 Make_Attribute_Reference (Loc,
13316 Prefix =>
13317 New_Reference_To (Index_Typ, Loc),
13318 Attribute_Name => Name_Pos,
13319 Expressions => New_List (
13320 Make_Attribute_Reference (Loc,
13321 Prefix =>
13322 New_Reference_To (Index_Typ, Loc),
13323 Attribute_Name => Name_First)))))));
13324 Add (Decl);
13325 end;
13326 end if;
13327 end Install_Private_Data_Declarations;
13329 -----------------------
13330 -- Is_Exception_Safe --
13331 -----------------------
13333 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13335 function Has_Side_Effect (N : Node_Id) return Boolean;
13336 -- Return True whenever encountering a subprogram call or raise
13337 -- statement of any kind in the sequence of statements
13339 ---------------------
13340 -- Has_Side_Effect --
13341 ---------------------
13343 -- What is this doing buried two levels down in exp_ch9. It seems like a
13344 -- generally useful function, and indeed there may be code duplication
13345 -- going on here ???
13347 function Has_Side_Effect (N : Node_Id) return Boolean is
13348 Stmt : Node_Id;
13349 Expr : Node_Id;
13351 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13352 -- Indicate whether N is a subprogram call or a raise statement
13354 ----------------------
13355 -- Is_Call_Or_Raise --
13356 ----------------------
13358 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13359 begin
13360 return Nkind_In (N, N_Procedure_Call_Statement,
13361 N_Function_Call,
13362 N_Raise_Statement,
13363 N_Raise_Constraint_Error,
13364 N_Raise_Program_Error,
13365 N_Raise_Storage_Error);
13366 end Is_Call_Or_Raise;
13368 -- Start of processing for Has_Side_Effect
13370 begin
13371 Stmt := N;
13372 while Present (Stmt) loop
13373 if Is_Call_Or_Raise (Stmt) then
13374 return True;
13375 end if;
13377 -- An object declaration can also contain a function call or a
13378 -- raise statement.
13380 if Nkind (Stmt) = N_Object_Declaration then
13381 Expr := Expression (Stmt);
13383 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13384 return True;
13385 end if;
13386 end if;
13388 Next (Stmt);
13389 end loop;
13391 return False;
13392 end Has_Side_Effect;
13394 -- Start of processing for Is_Exception_Safe
13396 begin
13397 -- When exceptions can't be propagated, the subprogram returns normally
13399 if No_Exception_Handlers_Set then
13400 return True;
13401 end if;
13403 -- If the checks handled by the back end are not disabled, we cannot
13404 -- ensure that no exception will be raised.
13406 if not Access_Checks_Suppressed (Empty)
13407 or else not Discriminant_Checks_Suppressed (Empty)
13408 or else not Range_Checks_Suppressed (Empty)
13409 or else not Index_Checks_Suppressed (Empty)
13410 or else Opt.Stack_Checking_Enabled
13411 then
13412 return False;
13413 end if;
13415 if Has_Side_Effect (First (Declarations (Subprogram)))
13416 or else
13417 Has_Side_Effect
13418 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13419 then
13420 return False;
13421 else
13422 return True;
13423 end if;
13424 end Is_Exception_Safe;
13426 ---------------------------------
13427 -- Is_Potentially_Large_Family --
13428 ---------------------------------
13430 function Is_Potentially_Large_Family
13431 (Base_Index : Entity_Id;
13432 Conctyp : Entity_Id;
13433 Lo : Node_Id;
13434 Hi : Node_Id) return Boolean
13436 begin
13437 return Scope (Base_Index) = Standard_Standard
13438 and then Base_Index = Base_Type (Standard_Integer)
13439 and then Has_Discriminants (Conctyp)
13440 and then
13441 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13442 and then
13443 (Denotes_Discriminant (Lo, True)
13444 or else
13445 Denotes_Discriminant (Hi, True));
13446 end Is_Potentially_Large_Family;
13448 -------------------------------------
13449 -- Is_Private_Primitive_Subprogram --
13450 -------------------------------------
13452 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13453 begin
13454 return
13455 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13456 and then Is_Private_Primitive (Id);
13457 end Is_Private_Primitive_Subprogram;
13459 ------------------
13460 -- Index_Object --
13461 ------------------
13463 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13464 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13465 Formal : Entity_Id;
13467 begin
13468 Formal := First_Formal (Bod_Subp);
13469 while Present (Formal) loop
13471 -- Look for formal parameter _E
13473 if Chars (Formal) = Name_uE then
13474 return Formal;
13475 end if;
13477 Next_Formal (Formal);
13478 end loop;
13480 -- A protected body subprogram should always have the parameter in
13481 -- question.
13483 raise Program_Error;
13484 end Index_Object;
13486 --------------------------------
13487 -- Make_Initialize_Protection --
13488 --------------------------------
13490 function Make_Initialize_Protection
13491 (Protect_Rec : Entity_Id) return List_Id
13493 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13494 P_Arr : Entity_Id;
13495 Pdec : Node_Id;
13496 Ptyp : constant Node_Id :=
13497 Corresponding_Concurrent_Type (Protect_Rec);
13498 Args : List_Id;
13499 L : constant List_Id := New_List;
13500 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13501 Prio_Type : Entity_Id;
13502 Prio_Var : Entity_Id := Empty;
13503 Restricted : constant Boolean := Restricted_Profile;
13505 begin
13506 -- We may need two calls to properly initialize the object, one to
13507 -- Initialize_Protection, and possibly one to Install_Handlers if we
13508 -- have a pragma Attach_Handler.
13510 -- Get protected declaration. In the case of a task type declaration,
13511 -- this is simply the parent of the protected type entity. In the single
13512 -- protected object declaration, this parent will be the implicit type,
13513 -- and we can find the corresponding single protected object declaration
13514 -- by searching forward in the declaration list in the tree.
13516 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13517 -- of this type should have been removed during semantic analysis.
13519 Pdec := Parent (Ptyp);
13520 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13521 N_Single_Protected_Declaration)
13522 loop
13523 Next (Pdec);
13524 end loop;
13526 -- Build the parameter list for the call. Note that _Init is the name
13527 -- of the formal for the object to be initialized, which is the task
13528 -- value record itself.
13530 Args := New_List;
13532 -- For lock-free implementation, skip initializations of the Protection
13533 -- object.
13535 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13536 -- Object parameter. This is a pointer to the object of type
13537 -- Protection used by the GNARL to control the protected object.
13539 Append_To (Args,
13540 Make_Attribute_Reference (Loc,
13541 Prefix =>
13542 Make_Selected_Component (Loc,
13543 Prefix => Make_Identifier (Loc, Name_uInit),
13544 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13545 Attribute_Name => Name_Unchecked_Access));
13547 -- Priority parameter. Set to Unspecified_Priority unless there is a
13548 -- Priority rep item, in which case we take the value from the pragma
13549 -- or attribute definition clause, or there is an Interrupt_Priority
13550 -- rep item and no Priority rep item, and we set the ceiling to
13551 -- Interrupt_Priority'Last, an implementation-defined value, see
13552 -- (RM D.3(10)).
13554 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13555 declare
13556 Prio_Clause : constant Node_Id :=
13557 Get_Rep_Item
13558 (Ptyp, Name_Priority, Check_Parents => False);
13560 Prio : Node_Id;
13562 begin
13563 -- Pragma Priority
13565 if Nkind (Prio_Clause) = N_Pragma then
13566 Prio :=
13567 Expression
13568 (First (Pragma_Argument_Associations (Prio_Clause)));
13570 -- Get_Rep_Item returns either priority pragma.
13572 if Pragma_Name (Prio_Clause) = Name_Priority then
13573 Prio_Type := RTE (RE_Any_Priority);
13574 else
13575 Prio_Type := RTE (RE_Interrupt_Priority);
13576 end if;
13578 -- Attribute definition clause Priority
13580 else
13581 if Chars (Prio_Clause) = Name_Priority then
13582 Prio_Type := RTE (RE_Any_Priority);
13583 else
13584 Prio_Type := RTE (RE_Interrupt_Priority);
13585 end if;
13587 Prio := Expression (Prio_Clause);
13588 end if;
13590 -- Always create a locale variable to capture the priority.
13591 -- The priority is also passed to Install_Restriced_Handlers.
13592 -- Note that it is really necessary to create this variable
13593 -- explicitly. It might be thought that removing side effects
13594 -- would the appropriate approach, but that could generate
13595 -- declarations improperly placed in the enclosing scope.
13597 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13598 Append_To (L,
13599 Make_Object_Declaration (Loc,
13600 Defining_Identifier => Prio_Var,
13601 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13602 Expression => Relocate_Node (Prio)));
13604 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13605 end;
13607 -- When no priority is specified but an xx_Handler pragma is, we
13608 -- default to System.Interrupts.Default_Interrupt_Priority, see
13609 -- D.3(10).
13611 elsif Has_Attach_Handler (Ptyp)
13612 or else Has_Interrupt_Handler (Ptyp)
13613 then
13614 Append_To (Args,
13615 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
13617 -- Normal case, no priority or xx_Handler specified, default priority
13619 else
13620 Append_To (Args,
13621 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13622 end if;
13624 -- Test for Compiler_Info parameter. This parameter allows entry body
13625 -- procedures and barrier functions to be called from the runtime. It
13626 -- is a pointer to the record generated by the compiler to represent
13627 -- the protected object.
13629 -- A protected type without entries that covers an interface and
13630 -- overrides the abstract routines with protected procedures is
13631 -- considered equivalent to a protected type with entries in the
13632 -- context of dispatching select statements.
13634 -- Protected types with interrupt handlers (when not using a
13635 -- restricted profile) are also considered equivalent to protected
13636 -- types with entries.
13638 -- The types which are used (Static_Interrupt_Protection and
13639 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13641 declare
13642 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13644 Called_Subp : RE_Id;
13646 begin
13647 case Pkg_Id is
13648 when System_Tasking_Protected_Objects_Entries =>
13649 Called_Subp := RE_Initialize_Protection_Entries;
13651 -- Argument Compiler_Info
13653 Append_To (Args,
13654 Make_Attribute_Reference (Loc,
13655 Prefix => Make_Identifier (Loc, Name_uInit),
13656 Attribute_Name => Name_Address));
13658 when System_Tasking_Protected_Objects_Single_Entry =>
13659 Called_Subp := RE_Initialize_Protection_Entry;
13661 -- Argument Compiler_Info
13663 Append_To (Args,
13664 Make_Attribute_Reference (Loc,
13665 Prefix => Make_Identifier (Loc, Name_uInit),
13666 Attribute_Name => Name_Address));
13668 when System_Tasking_Protected_Objects =>
13669 Called_Subp := RE_Initialize_Protection;
13671 when others =>
13672 raise Program_Error;
13673 end case;
13675 -- Entry_Bodies parameter. This is a pointer to an array of
13676 -- pointers to the entry body procedures and barrier functions of
13677 -- the object. If the protected type has no entries this object
13678 -- will not exist, in this case, pass a null (it can happen when
13679 -- there are protected interrupt handlers or interfaces).
13681 if Has_Entry then
13682 P_Arr := Entry_Bodies_Array (Ptyp);
13684 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13685 -- multiple entries).
13687 Append_To (Args,
13688 Make_Attribute_Reference (Loc,
13689 Prefix => New_Reference_To (P_Arr, Loc),
13690 Attribute_Name => Name_Unrestricted_Access));
13692 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13694 -- Find index mapping function (clumsy but ok for now)
13696 while Ekind (P_Arr) /= E_Function loop
13697 Next_Entity (P_Arr);
13698 end loop;
13700 Append_To (Args,
13701 Make_Attribute_Reference (Loc,
13702 Prefix => New_Reference_To (P_Arr, Loc),
13703 Attribute_Name => Name_Unrestricted_Access));
13704 end if;
13706 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13708 -- This is the case where we have a protected object with
13709 -- interfaces and no entries, and the single entry restriction
13710 -- is in effect. We pass a null pointer for the entry
13711 -- parameter because there is no actual entry.
13713 Append_To (Args, Make_Null (Loc));
13715 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13717 -- This is the case where we have a protected object with no
13718 -- entries and:
13719 -- - either interrupt handlers with non restricted profile,
13720 -- - or interfaces
13721 -- Note that the types which are used for interrupt handlers
13722 -- (Static/Dynamic_Interrupt_Protection) are derived from
13723 -- Protection_Entries. We pass two null pointers because there
13724 -- is no actual entry, and the initialization procedure needs
13725 -- both Entry_Bodies and Find_Body_Index.
13727 Append_To (Args, Make_Null (Loc));
13728 Append_To (Args, Make_Null (Loc));
13729 end if;
13731 Append_To (L,
13732 Make_Procedure_Call_Statement (Loc,
13733 Name => New_Reference_To (RTE (Called_Subp), Loc),
13734 Parameter_Associations => Args));
13735 end;
13736 end if;
13738 if Has_Attach_Handler (Ptyp) then
13740 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13741 -- make the following call:
13743 -- Install_Handlers (_object,
13744 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13746 -- or, in the case of Ravenscar:
13748 -- Install_Restricted_Handlers
13749 -- (Prio, (Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13751 declare
13752 Args : constant List_Id := New_List;
13753 Table : constant List_Id := New_List;
13754 Ritem : Node_Id := First_Rep_Item (Ptyp);
13756 begin
13757 -- Build the Priority parameter (only for ravenscar)
13759 if Restricted then
13761 -- Priority comes from a pragma
13763 if Present (Prio_Var) then
13764 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13766 -- Priority is the default one
13768 else
13769 Append_To (Args,
13770 New_Reference_To
13771 (RTE (RE_Default_Interrupt_Priority), Loc));
13772 end if;
13773 end if;
13775 -- Build the Attach_Handler table argument
13777 while Present (Ritem) loop
13778 if Nkind (Ritem) = N_Pragma
13779 and then Pragma_Name (Ritem) = Name_Attach_Handler
13780 then
13781 declare
13782 Handler : constant Node_Id :=
13783 First (Pragma_Argument_Associations (Ritem));
13785 Interrupt : constant Node_Id := Next (Handler);
13786 Expr : constant Node_Id := Expression (Interrupt);
13788 begin
13789 Append_To (Table,
13790 Make_Aggregate (Loc, Expressions => New_List (
13791 Unchecked_Convert_To
13792 (RTE (RE_System_Interrupt_Id), Expr),
13793 Make_Attribute_Reference (Loc,
13794 Prefix => Make_Selected_Component (Loc,
13795 Make_Identifier (Loc, Name_uInit),
13796 Duplicate_Subexpr_No_Checks
13797 (Expression (Handler))),
13798 Attribute_Name => Name_Access))));
13799 end;
13800 end if;
13802 Next_Rep_Item (Ritem);
13803 end loop;
13805 -- Append the table argument we just built
13807 Append_To (Args, Make_Aggregate (Loc, Table));
13809 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13810 -- call to the statements.
13812 if Restricted then
13813 -- Call a simplified version of Install_Handlers to be used
13814 -- when the Ravenscar restrictions are in effect
13815 -- (Install_Restricted_Handlers).
13817 Append_To (L,
13818 Make_Procedure_Call_Statement (Loc,
13819 Name =>
13820 New_Reference_To
13821 (RTE (RE_Install_Restricted_Handlers), Loc),
13822 Parameter_Associations => Args));
13824 else
13825 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13826 -- First, prepends the _object argument
13828 Prepend_To (Args,
13829 Make_Attribute_Reference (Loc,
13830 Prefix =>
13831 Make_Selected_Component (Loc,
13832 Prefix => Make_Identifier (Loc, Name_uInit),
13833 Selector_Name =>
13834 Make_Identifier (Loc, Name_uObject)),
13835 Attribute_Name => Name_Unchecked_Access));
13836 end if;
13838 -- Then, insert call to Install_Handlers
13840 Append_To (L,
13841 Make_Procedure_Call_Statement (Loc,
13842 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
13843 Parameter_Associations => Args));
13844 end if;
13845 end;
13846 end if;
13848 return L;
13849 end Make_Initialize_Protection;
13851 ---------------------------
13852 -- Make_Task_Create_Call --
13853 ---------------------------
13855 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13856 Loc : constant Source_Ptr := Sloc (Task_Rec);
13857 Args : List_Id;
13858 Ecount : Node_Id;
13859 Name : Node_Id;
13860 Tdec : Node_Id;
13861 Tdef : Node_Id;
13862 Tnam : Name_Id;
13863 Ttyp : Node_Id;
13865 begin
13866 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
13867 Tnam := Chars (Ttyp);
13869 -- Get task declaration. In the case of a task type declaration, this is
13870 -- simply the parent of the task type entity. In the single task
13871 -- declaration, this parent will be the implicit type, and we can find
13872 -- the corresponding single task declaration by searching forward in the
13873 -- declaration list in the tree.
13875 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
13876 -- this type should have been removed during semantic analysis.
13878 Tdec := Parent (Ttyp);
13879 while not Nkind_In (Tdec, N_Task_Type_Declaration,
13880 N_Single_Task_Declaration)
13881 loop
13882 Next (Tdec);
13883 end loop;
13885 -- Now we can find the task definition from this declaration
13887 Tdef := Task_Definition (Tdec);
13889 -- Build the parameter list for the call. Note that _Init is the name
13890 -- of the formal for the object to be initialized, which is the task
13891 -- value record itself.
13893 Args := New_List;
13895 -- Priority parameter. Set to Unspecified_Priority unless there is a
13896 -- Priority rep item, in which case we take the value from the rep item.
13898 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
13899 Append_To (Args,
13900 Make_Selected_Component (Loc,
13901 Prefix => Make_Identifier (Loc, Name_uInit),
13902 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
13903 else
13904 Append_To (Args,
13905 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13906 end if;
13908 -- Optional Stack parameter
13910 if Restricted_Profile then
13912 -- If the stack has been preallocated by the expander then
13913 -- pass its address. Otherwise, pass a null address.
13915 if Preallocated_Stacks_On_Target then
13916 Append_To (Args,
13917 Make_Attribute_Reference (Loc,
13918 Prefix =>
13919 Make_Selected_Component (Loc,
13920 Prefix => Make_Identifier (Loc, Name_uInit),
13921 Selector_Name => Make_Identifier (Loc, Name_uStack)),
13922 Attribute_Name => Name_Address));
13924 else
13925 Append_To (Args,
13926 New_Reference_To (RTE (RE_Null_Address), Loc));
13927 end if;
13928 end if;
13930 -- Size parameter. If no Storage_Size pragma is present, then
13931 -- the size is taken from the taskZ variable for the type, which
13932 -- is either Unspecified_Size, or has been reset by the use of
13933 -- a Storage_Size attribute definition clause. If a pragma is
13934 -- present, then the size is taken from the _Size field of the
13935 -- task value record, which was set from the pragma value.
13937 if Present (Tdef)
13938 and then Has_Storage_Size_Pragma (Tdef)
13939 then
13940 Append_To (Args,
13941 Make_Selected_Component (Loc,
13942 Prefix => Make_Identifier (Loc, Name_uInit),
13943 Selector_Name => Make_Identifier (Loc, Name_uSize)));
13945 else
13946 Append_To (Args,
13947 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
13948 end if;
13950 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
13951 -- Task_Info pragma, in which case we take the value from the pragma.
13953 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
13954 Append_To (Args,
13955 Make_Selected_Component (Loc,
13956 Prefix => Make_Identifier (Loc, Name_uInit),
13957 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
13959 else
13960 Append_To (Args,
13961 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
13962 end if;
13964 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
13965 -- in which case we take the value from the rep item. The parameter is
13966 -- passed as an Integer because in the case of unspecified CPU the
13967 -- value is not in the range of CPU_Range.
13969 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
13970 Append_To (Args,
13971 Convert_To (Standard_Integer,
13972 Make_Selected_Component (Loc,
13973 Prefix => Make_Identifier (Loc, Name_uInit),
13974 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
13975 else
13976 Append_To (Args,
13977 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
13978 end if;
13980 if not Restricted_Profile then
13982 -- Deadline parameter. If no Relative_Deadline pragma is present,
13983 -- then the deadline is Time_Span_Zero. If a pragma is present, then
13984 -- the deadline is taken from the _Relative_Deadline field of the
13985 -- task value record, which was set from the pragma value. Note that
13986 -- this parameter must not be generated for the restricted profiles
13987 -- since Ravenscar does not allow deadlines.
13989 -- Case where pragma Relative_Deadline applies: use given value
13991 if Present (Tdef)
13992 and then Has_Relative_Deadline_Pragma (Tdef)
13993 then
13994 Append_To (Args,
13995 Make_Selected_Component (Loc,
13996 Prefix =>
13997 Make_Identifier (Loc, Name_uInit),
13998 Selector_Name =>
13999 Make_Identifier (Loc, Name_uRelative_Deadline)));
14001 -- No pragma Relative_Deadline apply to the task
14003 else
14004 Append_To (Args,
14005 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
14006 end if;
14008 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14009 -- present, then the dispatching domain is null. If a rep item is
14010 -- present, then the dispatching domain is taken from the
14011 -- _Dispatching_Domain field of the task value record, which was set
14012 -- from the rep item value. Note that this parameter must not be
14013 -- generated for the restricted profiles since Ravenscar does not
14014 -- allow dispatching domains.
14016 -- Case where Dispatching_Domain rep item applies: use given value
14018 if Has_Rep_Item
14019 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14020 then
14021 Append_To (Args,
14022 Make_Selected_Component (Loc,
14023 Prefix =>
14024 Make_Identifier (Loc, Name_uInit),
14025 Selector_Name =>
14026 Make_Identifier (Loc, Name_uDispatching_Domain)));
14028 -- No pragma or aspect Dispatching_Domain apply to the task
14030 else
14031 Append_To (Args, Make_Null (Loc));
14032 end if;
14034 -- Number of entries. This is an expression of the form:
14036 -- n + _Init.a'Length + _Init.a'B'Length + ...
14038 -- where a,b... are the entry family names for the task definition
14040 Ecount :=
14041 Build_Entry_Count_Expression
14042 (Ttyp,
14043 Component_Items
14044 (Component_List
14045 (Type_Definition
14046 (Parent (Corresponding_Record_Type (Ttyp))))),
14047 Loc);
14048 Append_To (Args, Ecount);
14050 -- Master parameter. This is a reference to the _Master parameter of
14051 -- the initialization procedure, except in the case of the pragma
14052 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14053 -- System.Tasking.Library_Task_Level.
14055 if Restriction_Active (No_Task_Hierarchy) = False then
14056 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14057 else
14058 Append_To (Args,
14059 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14060 end if;
14061 end if;
14063 -- State parameter. This is a pointer to the task body procedure. The
14064 -- required value is obtained by taking 'Unrestricted_Access of the task
14065 -- body procedure and converting it (with an unchecked conversion) to
14066 -- the type required by the task kernel. For further details, see the
14067 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14068 -- than 'Address in order to avoid creating trampolines.
14070 declare
14071 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14072 Subp_Ptr_Typ : constant Node_Id :=
14073 Create_Itype (E_Access_Subprogram_Type, Tdec);
14074 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14076 begin
14077 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14078 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14080 -- Be sure to freeze a reference to the access-to-subprogram type,
14081 -- otherwise gigi will complain that it's in the wrong scope, because
14082 -- it's actually inside the init procedure for the record type that
14083 -- corresponds to the task type.
14085 -- This processing is causing a crash in the .NET/JVM back ends that
14086 -- is not yet understood, so skip it in these cases ???
14088 if VM_Target = No_VM then
14089 Set_Itype (Ref, Subp_Ptr_Typ);
14090 Append_Freeze_Action (Task_Rec, Ref);
14092 Append_To (Args,
14093 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14094 Make_Qualified_Expression (Loc,
14095 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
14096 Expression =>
14097 Make_Attribute_Reference (Loc,
14098 Prefix =>
14099 New_Occurrence_Of (Body_Proc, Loc),
14100 Attribute_Name => Name_Unrestricted_Access))));
14102 -- For the .NET/JVM cases revert to the original code below ???
14104 else
14105 Append_To (Args,
14106 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14107 Make_Attribute_Reference (Loc,
14108 Prefix =>
14109 New_Occurrence_Of (Body_Proc, Loc),
14110 Attribute_Name => Name_Address)));
14111 end if;
14112 end;
14114 -- Discriminants parameter. This is just the address of the task
14115 -- value record itself (which contains the discriminant values
14117 Append_To (Args,
14118 Make_Attribute_Reference (Loc,
14119 Prefix => Make_Identifier (Loc, Name_uInit),
14120 Attribute_Name => Name_Address));
14122 -- Elaborated parameter. This is an access to the elaboration Boolean
14124 Append_To (Args,
14125 Make_Attribute_Reference (Loc,
14126 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14127 Attribute_Name => Name_Unchecked_Access));
14129 -- Add Chain parameter (not done for sequential elaboration policy, see
14130 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14132 if Partition_Elaboration_Policy /= 'S' then
14133 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14134 end if;
14136 -- Task name parameter. Take this from the _Task_Id parameter to the
14137 -- init call unless there is a Task_Name pragma, in which case we take
14138 -- the value from the pragma.
14140 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14141 -- Copy expression in full, because it may be dynamic and have
14142 -- side effects.
14144 Append_To (Args,
14145 New_Copy_Tree
14146 (Expression
14147 (First
14148 (Pragma_Argument_Associations
14149 (Get_Rep_Pragma
14150 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14152 else
14153 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14154 end if;
14156 -- Created_Task parameter. This is the _Task_Id field of the task
14157 -- record value
14159 Append_To (Args,
14160 Make_Selected_Component (Loc,
14161 Prefix => Make_Identifier (Loc, Name_uInit),
14162 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14164 declare
14165 Create_RE : RE_Id;
14167 begin
14168 if Restricted_Profile then
14169 if Partition_Elaboration_Policy = 'S' then
14170 Create_RE := RE_Create_Restricted_Task_Sequential;
14171 else
14172 Create_RE := RE_Create_Restricted_Task;
14173 end if;
14174 else
14175 Create_RE := RE_Create_Task;
14176 end if;
14178 Name := New_Reference_To (RTE (Create_RE), Loc);
14179 end;
14181 return
14182 Make_Procedure_Call_Statement (Loc,
14183 Name => Name,
14184 Parameter_Associations => Args);
14185 end Make_Task_Create_Call;
14187 ------------------------------
14188 -- Next_Protected_Operation --
14189 ------------------------------
14191 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14192 Next_Op : Node_Id;
14194 begin
14195 Next_Op := Next (N);
14196 while Present (Next_Op)
14197 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
14198 loop
14199 Next (Next_Op);
14200 end loop;
14202 return Next_Op;
14203 end Next_Protected_Operation;
14205 ---------------------
14206 -- Null_Statements --
14207 ---------------------
14209 function Null_Statements (Stats : List_Id) return Boolean is
14210 Stmt : Node_Id;
14212 begin
14213 Stmt := First (Stats);
14214 while Nkind (Stmt) /= N_Empty
14215 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14216 or else
14217 (Nkind (Stmt) = N_Pragma
14218 and then
14219 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14220 Name_Unmodified,
14221 Name_Warnings)))
14222 loop
14223 Next (Stmt);
14224 end loop;
14226 return Nkind (Stmt) = N_Empty;
14227 end Null_Statements;
14229 --------------------------
14230 -- Parameter_Block_Pack --
14231 --------------------------
14233 function Parameter_Block_Pack
14234 (Loc : Source_Ptr;
14235 Blk_Typ : Entity_Id;
14236 Actuals : List_Id;
14237 Formals : List_Id;
14238 Decls : List_Id;
14239 Stmts : List_Id) return Node_Id
14241 Actual : Entity_Id;
14242 Expr : Node_Id := Empty;
14243 Formal : Entity_Id;
14244 Has_Param : Boolean := False;
14245 P : Entity_Id;
14246 Params : List_Id;
14247 Temp_Asn : Node_Id;
14248 Temp_Nam : Node_Id;
14250 begin
14251 Actual := First (Actuals);
14252 Formal := Defining_Identifier (First (Formals));
14253 Params := New_List;
14255 while Present (Actual) loop
14256 if Is_By_Copy_Type (Etype (Actual)) then
14257 -- Generate:
14258 -- Jnn : aliased <formal-type>
14260 Temp_Nam := Make_Temporary (Loc, 'J');
14262 Append_To (Decls,
14263 Make_Object_Declaration (Loc,
14264 Aliased_Present =>
14265 True,
14266 Defining_Identifier =>
14267 Temp_Nam,
14268 Object_Definition =>
14269 New_Reference_To (Etype (Formal), Loc)));
14271 if Ekind (Formal) /= E_Out_Parameter then
14273 -- Generate:
14274 -- Jnn := <actual>
14276 Temp_Asn :=
14277 New_Reference_To (Temp_Nam, Loc);
14279 Set_Assignment_OK (Temp_Asn);
14281 Append_To (Stmts,
14282 Make_Assignment_Statement (Loc,
14283 Name =>
14284 Temp_Asn,
14285 Expression =>
14286 New_Copy_Tree (Actual)));
14287 end if;
14289 -- Generate:
14290 -- Jnn'unchecked_access
14292 Append_To (Params,
14293 Make_Attribute_Reference (Loc,
14294 Attribute_Name =>
14295 Name_Unchecked_Access,
14296 Prefix =>
14297 New_Reference_To (Temp_Nam, Loc)));
14299 Has_Param := True;
14301 -- The controlling parameter is omitted
14303 else
14304 if not Is_Controlling_Actual (Actual) then
14305 Append_To (Params,
14306 Make_Reference (Loc, New_Copy_Tree (Actual)));
14308 Has_Param := True;
14309 end if;
14310 end if;
14312 Next_Actual (Actual);
14313 Next_Formal_With_Extras (Formal);
14314 end loop;
14316 if Has_Param then
14317 Expr := Make_Aggregate (Loc, Params);
14318 end if;
14320 -- Generate:
14321 -- P : Ann := (
14322 -- J1'unchecked_access;
14323 -- <actual2>'reference;
14324 -- ...);
14326 P := Make_Temporary (Loc, 'P');
14328 Append_To (Decls,
14329 Make_Object_Declaration (Loc,
14330 Defining_Identifier =>
14332 Object_Definition =>
14333 New_Reference_To (Blk_Typ, Loc),
14334 Expression =>
14335 Expr));
14337 return P;
14338 end Parameter_Block_Pack;
14340 ----------------------------
14341 -- Parameter_Block_Unpack --
14342 ----------------------------
14344 function Parameter_Block_Unpack
14345 (Loc : Source_Ptr;
14346 P : Entity_Id;
14347 Actuals : List_Id;
14348 Formals : List_Id) return List_Id
14350 Actual : Entity_Id;
14351 Asnmt : Node_Id;
14352 Formal : Entity_Id;
14353 Has_Asnmt : Boolean := False;
14354 Result : constant List_Id := New_List;
14356 begin
14357 Actual := First (Actuals);
14358 Formal := Defining_Identifier (First (Formals));
14359 while Present (Actual) loop
14360 if Is_By_Copy_Type (Etype (Actual))
14361 and then Ekind (Formal) /= E_In_Parameter
14362 then
14363 -- Generate:
14364 -- <actual> := P.<formal>;
14366 Asnmt :=
14367 Make_Assignment_Statement (Loc,
14368 Name =>
14369 New_Copy (Actual),
14370 Expression =>
14371 Make_Explicit_Dereference (Loc,
14372 Make_Selected_Component (Loc,
14373 Prefix =>
14374 New_Reference_To (P, Loc),
14375 Selector_Name =>
14376 Make_Identifier (Loc, Chars (Formal)))));
14378 Set_Assignment_OK (Name (Asnmt));
14379 Append_To (Result, Asnmt);
14381 Has_Asnmt := True;
14382 end if;
14384 Next_Actual (Actual);
14385 Next_Formal_With_Extras (Formal);
14386 end loop;
14388 if Has_Asnmt then
14389 return Result;
14390 else
14391 return New_List (Make_Null_Statement (Loc));
14392 end if;
14393 end Parameter_Block_Unpack;
14395 ----------------------
14396 -- Set_Discriminals --
14397 ----------------------
14399 procedure Set_Discriminals (Dec : Node_Id) is
14400 D : Entity_Id;
14401 Pdef : Entity_Id;
14402 D_Minal : Entity_Id;
14404 begin
14405 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14406 Pdef := Defining_Identifier (Dec);
14408 if Has_Discriminants (Pdef) then
14409 D := First_Discriminant (Pdef);
14410 while Present (D) loop
14411 D_Minal :=
14412 Make_Defining_Identifier (Sloc (D),
14413 Chars => New_External_Name (Chars (D), 'D'));
14415 Set_Ekind (D_Minal, E_Constant);
14416 Set_Etype (D_Minal, Etype (D));
14417 Set_Scope (D_Minal, Pdef);
14418 Set_Discriminal (D, D_Minal);
14419 Set_Discriminal_Link (D_Minal, D);
14421 Next_Discriminant (D);
14422 end loop;
14423 end if;
14424 end Set_Discriminals;
14426 -----------------------
14427 -- Trivial_Accept_OK --
14428 -----------------------
14430 function Trivial_Accept_OK return Boolean is
14431 begin
14432 case Opt.Task_Dispatching_Policy is
14434 -- If we have the default task dispatching policy in effect, we can
14435 -- definitely do the optimization (one way of looking at this is to
14436 -- think of the formal definition of the default policy being allowed
14437 -- to run any task it likes after a rendezvous, so even if notionally
14438 -- a full rescheduling occurs, we can say that our dispatching policy
14439 -- (i.e. the default dispatching policy) reorders the queue to be the
14440 -- same as just before the call.
14442 when ' ' =>
14443 return True;
14445 -- FIFO_Within_Priorities certainly does not permit this
14446 -- optimization since the Rendezvous is a scheduling action that may
14447 -- require some other task to be run.
14449 when 'F' =>
14450 return False;
14452 -- For now, disallow the optimization for all other policies. This
14453 -- may be over-conservative, but it is certainly not incorrect.
14455 when others =>
14456 return False;
14458 end case;
14459 end Trivial_Accept_OK;
14461 end Exp_Ch9;