PR sanitizer/65081
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob7f26a8cb1fe90c961058b551dc6fd3c1a2875d80
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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch3; use Exp_Ch3;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Dbug; use Exp_Dbug;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Sel; use Exp_Sel;
37 with Exp_Smem; use Exp_Smem;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
41 with Hostparm;
42 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch9; use Sem_Ch9;
55 with Sem_Ch11; use Sem_Ch11;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Exp_Ch9 is
70 -- The following constant establishes the upper bound for the index of
71 -- an entry family. It is used to limit the allocated size of protected
72 -- types with defaulted discriminant of an integer type, when the bound
73 -- of some entry family depends on a discriminant. The limitation to entry
74 -- families of 128K should be reasonable in all cases, and is a documented
75 -- implementation restriction.
77 Entry_Family_Bound : constant Int := 2**16;
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Actual_Index_Expression
84 (Sloc : Source_Ptr;
85 Ent : Entity_Id;
86 Index : Node_Id;
87 Tsk : Entity_Id) return Node_Id;
88 -- Compute the index position for an entry call. Tsk is the target task. If
89 -- the bounds of some entry family depend on discriminants, the expression
90 -- computed by this function uses the discriminants of the target task.
92 procedure Add_Object_Pointer
93 (Loc : Source_Ptr;
94 Conc_Typ : Entity_Id;
95 Decls : List_Id);
96 -- Prepend an object pointer declaration to the declaration list Decls.
97 -- This object pointer is initialized to a type conversion of the System.
98 -- Address pointer passed to entry barrier functions and entry body
99 -- procedures.
101 procedure Add_Formal_Renamings
102 (Spec : Node_Id;
103 Decls : List_Id;
104 Ent : Entity_Id;
105 Loc : Source_Ptr);
106 -- Create renaming declarations for the formals, inside the procedure that
107 -- implements an entry body. The renamings make the original names of the
108 -- formals accessible to gdb, and serve no other purpose.
109 -- Spec is the specification of the procedure being built.
110 -- Decls is the list of declarations to be enhanced.
111 -- Ent is the entity for the original entry body.
113 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
114 -- Transform accept statement into a block with added exception handler.
115 -- Used both for simple accept statements and for accept alternatives in
116 -- select statements. Astat is the accept statement.
118 function Build_Barrier_Function
119 (N : Node_Id;
120 Ent : Entity_Id;
121 Pid : Node_Id) return Node_Id;
122 -- Build the function body returning the value of the barrier expression
123 -- for the specified entry body.
125 function Build_Barrier_Function_Specification
126 (Loc : Source_Ptr;
127 Def_Id : Entity_Id) return Node_Id;
128 -- Build a specification for a function implementing the protected entry
129 -- barrier of the specified entry body.
131 function Build_Corresponding_Record
132 (N : Node_Id;
133 Ctyp : Node_Id;
134 Loc : Source_Ptr) return Node_Id;
135 -- Common to tasks and protected types. Copy discriminant specifications,
136 -- build record declaration. N is the type declaration, Ctyp is the
137 -- concurrent entity (task type or protected type).
139 function Build_Dispatching_Tag_Check
140 (K : Entity_Id;
141 N : Node_Id) return Node_Id;
142 -- Utility to create the tree to check whether the dispatching call in
143 -- a timed entry call, a conditional entry call, or an asynchronous
144 -- transfer of control is a call to a primitive of a non-synchronized type.
145 -- K is the temporary that holds the tagged kind of the target object, and
146 -- N is the enclosing construct.
148 function Build_Entry_Count_Expression
149 (Concurrent_Type : Node_Id;
150 Component_List : List_Id;
151 Loc : Source_Ptr) return Node_Id;
152 -- Compute number of entries for concurrent object. This is a count of
153 -- simple entries, followed by an expression that computes the length
154 -- of the range of each entry family. A single array with that size is
155 -- allocated for each concurrent object of the type.
157 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
158 -- Build the function that translates the entry index in the call
159 -- (which depends on the size of entry families) into an index into the
160 -- Entry_Bodies_Array, to determine the body and barrier function used
161 -- in a protected entry call. A pointer to this function appears in every
162 -- protected object.
164 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
165 -- Build subprogram declaration for previous one
167 function Build_Lock_Free_Protected_Subprogram_Body
168 (N : Node_Id;
169 Prot_Typ : Node_Id;
170 Unprot_Spec : Node_Id) return Node_Id;
171 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
172 -- the subprogram specification of the unprotected version of N. Transform
173 -- N such that it invokes the unprotected version of the body.
175 function Build_Lock_Free_Unprotected_Subprogram_Body
176 (N : Node_Id;
177 Prot_Typ : Node_Id) return Node_Id;
178 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
179 -- of N where the original statements of N are synchronized through atomic
180 -- actions such as compare and exchange. Prior to invoking this routine, it
181 -- has been established that N can be implemented in a lock-free fashion.
183 function Build_Parameter_Block
184 (Loc : Source_Ptr;
185 Actuals : List_Id;
186 Formals : List_Id;
187 Decls : List_Id) return Entity_Id;
188 -- Generate an access type for each actual parameter in the list Actuals.
189 -- Create an encapsulating record that contains all the actuals and return
190 -- its type. Generate:
191 -- type Ann1 is access all <actual1-type>
192 -- ...
193 -- type AnnN is access all <actualN-type>
194 -- type Pnn is record
195 -- <formal1> : Ann1;
196 -- ...
197 -- <formalN> : AnnN;
198 -- end record;
200 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
201 -- Build body of wrapper procedure for an entry or entry family that has
202 -- pre/postconditions. The body gathers the PPC's and expands them in the
203 -- usual way, and performs the entry call itself. This way preconditions
204 -- are evaluated before the call is queued. E is the entry in question,
205 -- and Decl is the enclosing synchronized type declaration at whose freeze
206 -- point the generated body is analyzed.
208 function Build_Protected_Entry
209 (N : Node_Id;
210 Ent : Entity_Id;
211 Pid : Node_Id) return Node_Id;
212 -- Build the procedure implementing the statement sequence of the specified
213 -- entry body.
215 function Build_Protected_Entry_Specification
216 (Loc : Source_Ptr;
217 Def_Id : Entity_Id;
218 Ent_Id : Entity_Id) return Node_Id;
219 -- Build a specification for the procedure implementing the statements of
220 -- the specified entry body. Add attributes associating it with the entry
221 -- defining identifier Ent_Id.
223 function Build_Protected_Spec
224 (N : Node_Id;
225 Obj_Type : Entity_Id;
226 Ident : Entity_Id;
227 Unprotected : Boolean := False) return List_Id;
228 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
229 -- Subprogram_Type. Builds signature of protected subprogram, adding the
230 -- formal that corresponds to the object itself. For an access to protected
231 -- subprogram, there is no object type to specify, so the parameter has
232 -- type Address and mode In. An indirect call through such a pointer will
233 -- convert the address to a reference to the actual object. The object is
234 -- a limited record and therefore a by_reference type.
236 function Build_Protected_Subprogram_Body
237 (N : Node_Id;
238 Pid : Node_Id;
239 N_Op_Spec : Node_Id) return Node_Id;
240 -- This function is used to construct the protected version of a protected
241 -- subprogram. Its statement sequence first defers abort, then locks the
242 -- associated protected object, and then enters a block that contains a
243 -- call to the unprotected version of the subprogram (for details, see
244 -- Build_Unprotected_Subprogram_Body). This block statement requires a
245 -- cleanup handler that unlocks the object in all cases. For details,
246 -- see Exp_Ch7.Expand_Cleanup_Actions.
248 function Build_Renamed_Formal_Declaration
249 (New_F : Entity_Id;
250 Formal : Entity_Id;
251 Comp : Entity_Id;
252 Renamed_Formal : Node_Id) return Node_Id;
253 -- Create a renaming declaration for a formal, within a protected entry
254 -- body or an accept body. The renamed object is a component of the
255 -- parameter block that is a parameter in the entry call.
257 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
258 -- does not dereference the corresponding component to prevent an illegal
259 -- use of the incomplete type (AI05-0151).
261 function Build_Selected_Name
262 (Prefix : Entity_Id;
263 Selector : Entity_Id;
264 Append_Char : Character := ' ') return Name_Id;
265 -- Build a name in the form of Prefix__Selector, with an optional character
266 -- appended. This is used for internal subprograms generated for operations
267 -- of protected types, including barrier functions. For the subprograms
268 -- generated for entry bodies and entry barriers, the generated name
269 -- includes a sequence number that makes names unique in the presence of
270 -- entry overloading. This is necessary because entry body procedures and
271 -- barrier functions all have the same signature.
273 procedure Build_Simple_Entry_Call
274 (N : Node_Id;
275 Concval : Node_Id;
276 Ename : Node_Id;
277 Index : Node_Id);
278 -- Some comments here would be useful ???
280 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
281 -- This routine constructs a specification for the procedure that we will
282 -- build for the task body for task type T. The spec has the form:
284 -- procedure tnameB (_Task : access tnameV);
286 -- where name is the character name taken from the task type entity that
287 -- is passed as the argument to the procedure, and tnameV is the task
288 -- value type that is associated with the task type.
290 function Build_Unprotected_Subprogram_Body
291 (N : Node_Id;
292 Pid : Node_Id) return Node_Id;
293 -- This routine constructs the unprotected version of a protected
294 -- subprogram body, which is contains all of the code in the original,
295 -- unexpanded body. This is the version of the protected subprogram that is
296 -- called from all protected operations on the same object, including the
297 -- protected version of the same subprogram.
299 procedure Build_Wrapper_Bodies
300 (Loc : Source_Ptr;
301 Typ : Entity_Id;
302 N : Node_Id);
303 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
304 -- record of a concurrent type. N is the insertion node where all bodies
305 -- will be placed. This routine builds the bodies of the subprograms which
306 -- serve as an indirection mechanism to overriding primitives of concurrent
307 -- types, entries and protected procedures. Any new body is analyzed.
309 procedure Build_Wrapper_Specs
310 (Loc : Source_Ptr;
311 Typ : Entity_Id;
312 N : in out Node_Id);
313 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
314 -- record of a concurrent type. N is the insertion node where all specs
315 -- will be placed. This routine builds the specs of the subprograms which
316 -- serve as an indirection mechanism to overriding primitives of concurrent
317 -- types, entries and protected procedures. Any new spec is analyzed.
319 procedure Collect_Entry_Families
320 (Loc : Source_Ptr;
321 Cdecls : List_Id;
322 Current_Node : in out Node_Id;
323 Conctyp : Entity_Id);
324 -- For each entry family in a concurrent type, create an anonymous array
325 -- type of the right size, and add a component to the corresponding_record.
327 function Concurrent_Object
328 (Spec_Id : Entity_Id;
329 Conc_Typ : Entity_Id) return Entity_Id;
330 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
331 -- the entity associated with the concurrent object in the Protected_Body_
332 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
333 -- denotes formal parameter _O, _object or _task.
335 function Copy_Result_Type (Res : Node_Id) return Node_Id;
336 -- Copy the result type of a function specification, when building the
337 -- internal operation corresponding to a protected function, or when
338 -- expanding an access to protected function. If the result is an anonymous
339 -- access to subprogram itself, we need to create a new signature with the
340 -- same parameter names and the same resolved types, but with new entities
341 -- for the formals.
343 procedure Debug_Private_Data_Declarations (Decls : List_Id);
344 -- Decls is a list which may contain the declarations created by Install_
345 -- Private_Data_Declarations. All generated entities are marked as needing
346 -- debug info and debug nodes are manually generation where necessary. This
347 -- step of the expansion must to be done after private data has been moved
348 -- to its final resting scope to ensure proper visibility of debug objects.
350 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
351 -- If control flow optimizations are suppressed, and Alt is an accept,
352 -- delay, or entry call alternative with no trailing statements, insert
353 -- a null trailing statement with the given Loc (which is the sloc of
354 -- the accept, delay, or entry call statement). There might not be any
355 -- generated code for the accept, delay, or entry call itself (the effect
356 -- of these statements is part of the general processsing done for the
357 -- enclosing selective accept, timed entry call, or asynchronous select),
358 -- and the null statement is there to carry the sloc of that statement to
359 -- the back-end for trace-based coverage analysis purposes.
361 procedure Extract_Dispatching_Call
362 (N : Node_Id;
363 Call_Ent : out Entity_Id;
364 Object : out Entity_Id;
365 Actuals : out List_Id;
366 Formals : out List_Id);
367 -- Given a dispatching call, extract the entity of the name of the call,
368 -- its actual dispatching object, its actual parameters and the formal
369 -- parameters of the overridden interface-level version. If the type of
370 -- the dispatching object is an access type then an explicit dereference
371 -- is returned in Object.
373 procedure Extract_Entry
374 (N : Node_Id;
375 Concval : out Node_Id;
376 Ename : out Node_Id;
377 Index : out Node_Id);
378 -- Given an entry call, returns the associated concurrent object, the entry
379 -- name, and the entry family index.
381 function Family_Offset
382 (Loc : Source_Ptr;
383 Hi : Node_Id;
384 Lo : Node_Id;
385 Ttyp : Entity_Id;
386 Cap : Boolean) return Node_Id;
387 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
388 -- accept statement, or the upper bound in the discrete subtype of an entry
389 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
390 -- type of the entry. If Cap is true, the result is capped according to
391 -- Entry_Family_Bound.
393 function Family_Size
394 (Loc : Source_Ptr;
395 Hi : Node_Id;
396 Lo : Node_Id;
397 Ttyp : Entity_Id;
398 Cap : Boolean) return Node_Id;
399 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
400 -- family, and handle properly the superflat case. This is equivalent to
401 -- the use of 'Length on the index type, but must use Family_Offset to
402 -- handle properly the case of bounds that depend on discriminants. If
403 -- Cap is true, the result is capped according to Entry_Family_Bound.
405 procedure Find_Enclosing_Context
406 (N : Node_Id;
407 Context : out Node_Id;
408 Context_Id : out Entity_Id;
409 Context_Decls : out List_Id);
410 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
411 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
412 -- nearest enclosing body, block, package or return statement and return
413 -- its constituents. Context is the enclosing construct, Context_Id is
414 -- the scope of Context_Id and Context_Decls is the declarative list of
415 -- Context.
417 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
418 -- Given a subprogram identifier, return the entity which is associated
419 -- with the protection entry index in the Protected_Body_Subprogram or
420 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
421 -- parameter _E.
423 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
424 -- Tell whether a given subprogram cannot raise an exception
426 function Is_Potentially_Large_Family
427 (Base_Index : Entity_Id;
428 Conctyp : Entity_Id;
429 Lo : Node_Id;
430 Hi : Node_Id) return Boolean;
432 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
433 -- Determine whether Id is a function or a procedure and is marked as a
434 -- private primitive.
436 function Null_Statements (Stats : List_Id) return Boolean;
437 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
438 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
439 -- to still count as null. Returns True for a null sequence. The argument
440 -- is the list of statements from the DO-END sequence.
442 function Parameter_Block_Pack
443 (Loc : Source_Ptr;
444 Blk_Typ : Entity_Id;
445 Actuals : List_Id;
446 Formals : List_Id;
447 Decls : List_Id;
448 Stmts : List_Id) return Entity_Id;
449 -- Set the components of the generated parameter block with the values
450 -- of the actual parameters. Generate aliased temporaries to capture the
451 -- values for types that are passed by copy. Otherwise generate a reference
452 -- to the actual's value. Return the address of the aggregate block.
453 -- Generate:
454 -- Jnn1 : alias <formal-type1>;
455 -- Jnn1 := <actual1>;
456 -- ...
457 -- P : Blk_Typ := (
458 -- Jnn1'unchecked_access;
459 -- <actual2>'reference;
460 -- ...);
462 function Parameter_Block_Unpack
463 (Loc : Source_Ptr;
464 P : Entity_Id;
465 Actuals : List_Id;
466 Formals : List_Id) return List_Id;
467 -- Retrieve the values of the components from the parameter block and
468 -- assign then to the original actual parameters. Generate:
469 -- <actual1> := P.<formal1>;
470 -- ...
471 -- <actualN> := P.<formalN>;
473 function Trivial_Accept_OK return Boolean;
474 -- If there is no DO-END block for an accept, or if the DO-END block has
475 -- only null statements, then it is possible to do the Rendezvous with much
476 -- less overhead using the Accept_Trivial routine in the run-time library.
477 -- However, this is not always a valid optimization. Whether it is valid or
478 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
479 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
480 -- a rescheduling is required, so this optimization is not allowed. This
481 -- function returns True if the optimization is permitted.
483 -----------------------------
484 -- Actual_Index_Expression --
485 -----------------------------
487 function Actual_Index_Expression
488 (Sloc : Source_Ptr;
489 Ent : Entity_Id;
490 Index : Node_Id;
491 Tsk : Entity_Id) return Node_Id
493 Ttyp : constant Entity_Id := Etype (Tsk);
494 Expr : Node_Id;
495 Num : Node_Id;
496 Lo : Node_Id;
497 Hi : Node_Id;
498 Prev : Entity_Id;
499 S : Node_Id;
501 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
502 -- Compute difference between bounds of entry family
504 --------------------------
505 -- Actual_Family_Offset --
506 --------------------------
508 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
510 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
511 -- Replace a reference to a discriminant with a selected component
512 -- denoting the discriminant of the target task.
514 -----------------------------
515 -- Actual_Discriminant_Ref --
516 -----------------------------
518 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
519 Typ : constant Entity_Id := Etype (Bound);
520 B : Node_Id;
522 begin
523 if not Is_Entity_Name (Bound)
524 or else Ekind (Entity (Bound)) /= E_Discriminant
525 then
526 if Nkind (Bound) = N_Attribute_Reference then
527 return Bound;
528 else
529 B := New_Copy_Tree (Bound);
530 end if;
532 else
533 B :=
534 Make_Selected_Component (Sloc,
535 Prefix => New_Copy_Tree (Tsk),
536 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
538 Analyze_And_Resolve (B, Typ);
539 end if;
541 return
542 Make_Attribute_Reference (Sloc,
543 Attribute_Name => Name_Pos,
544 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
545 Expressions => New_List (B));
546 end Actual_Discriminant_Ref;
548 -- Start of processing for Actual_Family_Offset
550 begin
551 return
552 Make_Op_Subtract (Sloc,
553 Left_Opnd => Actual_Discriminant_Ref (Hi),
554 Right_Opnd => Actual_Discriminant_Ref (Lo));
555 end Actual_Family_Offset;
557 -- Start of processing for Actual_Index_Expression
559 begin
560 -- The queues of entries and entry families appear in textual order in
561 -- the associated record. The entry index is computed as the sum of the
562 -- number of queues for all entries that precede the designated one, to
563 -- which is added the index expression, if this expression denotes a
564 -- member of a family.
566 -- The following is a place holder for the count of simple entries
568 Num := Make_Integer_Literal (Sloc, 1);
570 -- We construct an expression which is a series of addition operations.
571 -- See comments in Entry_Index_Expression, which is identical in
572 -- structure.
574 if Present (Index) then
575 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
577 Expr :=
578 Make_Op_Add (Sloc,
579 Left_Opnd => Num,
580 Right_Opnd =>
581 Actual_Family_Offset (
582 Make_Attribute_Reference (Sloc,
583 Attribute_Name => Name_Pos,
584 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
585 Expressions => New_List (Relocate_Node (Index))),
586 Type_Low_Bound (S)));
587 else
588 Expr := Num;
589 end if;
591 -- Now add lengths of preceding entries and entry families
593 Prev := First_Entity (Ttyp);
594 while Chars (Prev) /= Chars (Ent)
595 or else (Ekind (Prev) /= Ekind (Ent))
596 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
597 loop
598 if Ekind (Prev) = E_Entry then
599 Set_Intval (Num, Intval (Num) + 1);
601 elsif Ekind (Prev) = E_Entry_Family then
602 S :=
603 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
605 -- The need for the following full view retrieval stems from this
606 -- complex case of nested generics and tasking:
608 -- generic
609 -- type Formal_Index is range <>;
610 -- ...
611 -- package Outer is
612 -- type Index is private;
613 -- generic
614 -- ...
615 -- package Inner is
616 -- procedure P;
617 -- end Inner;
618 -- private
619 -- type Index is new Formal_Index range 1 .. 10;
620 -- end Outer;
622 -- package body Outer is
623 -- task type T is
624 -- entry Fam (Index); -- (2)
625 -- entry E;
626 -- end T;
627 -- package body Inner is -- (3)
628 -- procedure P is
629 -- begin
630 -- T.E; -- (1)
631 -- end P;
632 -- end Inner;
633 -- ...
635 -- We are currently building the index expression for the entry
636 -- call "T.E" (1). Part of the expansion must mention the range
637 -- of the discrete type "Index" (2) of entry family "Fam".
639 -- However only the private view of type "Index" is available to
640 -- the inner generic (3) because there was no prior mention of
641 -- the type inside "Inner". This visibility requirement is
642 -- implicit and cannot be detected during the construction of
643 -- the generic trees and needs special handling.
645 if In_Instance_Body
646 and then Is_Private_Type (S)
647 and then Present (Full_View (S))
648 then
649 S := Full_View (S);
650 end if;
652 Lo := Type_Low_Bound (S);
653 Hi := Type_High_Bound (S);
655 Expr :=
656 Make_Op_Add (Sloc,
657 Left_Opnd => Expr,
658 Right_Opnd =>
659 Make_Op_Add (Sloc,
660 Left_Opnd => Actual_Family_Offset (Hi, Lo),
661 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
663 -- Other components are anonymous types to be ignored
665 else
666 null;
667 end if;
669 Next_Entity (Prev);
670 end loop;
672 return Expr;
673 end Actual_Index_Expression;
675 --------------------------
676 -- Add_Formal_Renamings --
677 --------------------------
679 procedure Add_Formal_Renamings
680 (Spec : Node_Id;
681 Decls : List_Id;
682 Ent : Entity_Id;
683 Loc : Source_Ptr)
685 Ptr : constant Entity_Id :=
686 Defining_Identifier
687 (Next (First (Parameter_Specifications (Spec))));
688 -- The name of the formal that holds the address of the parameter block
689 -- for the call.
691 Comp : Entity_Id;
692 Decl : Node_Id;
693 Formal : Entity_Id;
694 New_F : Entity_Id;
695 Renamed_Formal : Node_Id;
697 begin
698 Formal := First_Formal (Ent);
699 while Present (Formal) loop
700 Comp := Entry_Component (Formal);
701 New_F :=
702 Make_Defining_Identifier (Sloc (Formal),
703 Chars => Chars (Formal));
704 Set_Etype (New_F, Etype (Formal));
705 Set_Scope (New_F, Ent);
707 -- Now we set debug info needed on New_F even though it does not come
708 -- from source, so that the debugger will get the right information
709 -- for these generated names.
711 Set_Debug_Info_Needed (New_F);
713 if Ekind (Formal) = E_In_Parameter then
714 Set_Ekind (New_F, E_Constant);
715 else
716 Set_Ekind (New_F, E_Variable);
717 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
718 end if;
720 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
722 Renamed_Formal :=
723 Make_Selected_Component (Loc,
724 Prefix =>
725 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
726 Make_Identifier (Loc, Chars (Ptr))),
727 Selector_Name => New_Occurrence_Of (Comp, Loc));
729 Decl :=
730 Build_Renamed_Formal_Declaration
731 (New_F, Formal, Comp, Renamed_Formal);
733 Append (Decl, Decls);
734 Set_Renamed_Object (Formal, New_F);
735 Next_Formal (Formal);
736 end loop;
737 end Add_Formal_Renamings;
739 ------------------------
740 -- Add_Object_Pointer --
741 ------------------------
743 procedure Add_Object_Pointer
744 (Loc : Source_Ptr;
745 Conc_Typ : Entity_Id;
746 Decls : List_Id)
748 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
749 Decl : Node_Id;
750 Obj_Ptr : Node_Id;
752 begin
753 -- Create the renaming declaration for the Protection object of a
754 -- protected type. _Object is used by Complete_Entry_Body.
755 -- ??? An attempt to make this a renaming was unsuccessful.
757 -- Build the entity for the access type
759 Obj_Ptr :=
760 Make_Defining_Identifier (Loc,
761 New_External_Name (Chars (Rec_Typ), 'P'));
763 -- Generate:
764 -- _object : poVP := poVP!O;
766 Decl :=
767 Make_Object_Declaration (Loc,
768 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
769 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
770 Expression =>
771 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
772 Set_Debug_Info_Needed (Defining_Identifier (Decl));
773 Prepend_To (Decls, Decl);
775 -- Generate:
776 -- type poVP is access poV;
778 Decl :=
779 Make_Full_Type_Declaration (Loc,
780 Defining_Identifier =>
781 Obj_Ptr,
782 Type_Definition =>
783 Make_Access_To_Object_Definition (Loc,
784 Subtype_Indication =>
785 New_Occurrence_Of (Rec_Typ, Loc)));
786 Set_Debug_Info_Needed (Defining_Identifier (Decl));
787 Prepend_To (Decls, Decl);
788 end Add_Object_Pointer;
790 -----------------------
791 -- Build_Accept_Body --
792 -----------------------
794 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
795 Loc : constant Source_Ptr := Sloc (Astat);
796 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
797 New_S : Node_Id;
798 Hand : Node_Id;
799 Call : Node_Id;
800 Ohandle : Node_Id;
802 begin
803 -- At the end of the statement sequence, Complete_Rendezvous is called.
804 -- A label skipping the Complete_Rendezvous, and all other accept
805 -- processing, has already been added for the expansion of requeue
806 -- statements. The Sloc is copied from the last statement since it
807 -- is really part of this last statement.
809 Call :=
810 Build_Runtime_Call
811 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
812 Insert_Before (Last (Statements (Stats)), Call);
813 Analyze (Call);
815 -- If exception handlers are present, then append Complete_Rendezvous
816 -- calls to the handlers, and construct the required outer block. As
817 -- above, the Sloc is copied from the last statement in the sequence.
819 if Present (Exception_Handlers (Stats)) then
820 Hand := First (Exception_Handlers (Stats));
821 while Present (Hand) loop
822 Call :=
823 Build_Runtime_Call
824 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
825 Append (Call, Statements (Hand));
826 Analyze (Call);
827 Next (Hand);
828 end loop;
830 New_S :=
831 Make_Handled_Sequence_Of_Statements (Loc,
832 Statements => New_List (
833 Make_Block_Statement (Loc,
834 Handled_Statement_Sequence => Stats)));
836 else
837 New_S := Stats;
838 end if;
840 -- At this stage we know that the new statement sequence does
841 -- not have an exception handler part, so we supply one to call
842 -- Exceptional_Complete_Rendezvous. This handler is
844 -- when all others =>
845 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
847 -- We handle Abort_Signal to make sure that we properly catch the abort
848 -- case and wake up the caller.
850 Ohandle := Make_Others_Choice (Loc);
851 Set_All_Others (Ohandle);
853 Set_Exception_Handlers (New_S,
854 New_List (
855 Make_Implicit_Exception_Handler (Loc,
856 Exception_Choices => New_List (Ohandle),
858 Statements => New_List (
859 Make_Procedure_Call_Statement (Sloc (Stats),
860 Name => New_Occurrence_Of (
861 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
862 Parameter_Associations => New_List (
863 Make_Function_Call (Sloc (Stats),
864 Name =>
865 New_Occurrence_Of
866 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
868 Set_Parent (New_S, Astat); -- temp parent for Analyze call
869 Analyze_Exception_Handlers (Exception_Handlers (New_S));
870 Expand_Exception_Handlers (New_S);
872 -- Exceptional_Complete_Rendezvous must be called with abort still
873 -- deferred, which is the case for a "when all others" handler.
875 return New_S;
876 end Build_Accept_Body;
878 -----------------------------------
879 -- Build_Activation_Chain_Entity --
880 -----------------------------------
882 procedure Build_Activation_Chain_Entity (N : Node_Id) is
883 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
884 -- Determine whether an extended return statement has activation chain
886 --------------------------
887 -- Has_Activation_Chain --
888 --------------------------
890 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
891 Decl : Node_Id;
893 begin
894 Decl := First (Return_Object_Declarations (Stmt));
895 while Present (Decl) loop
896 if Nkind (Decl) = N_Object_Declaration
897 and then Chars (Defining_Identifier (Decl)) = Name_uChain
898 then
899 return True;
900 end if;
902 Next (Decl);
903 end loop;
905 return False;
906 end Has_Activation_Chain;
908 -- Local variables
910 Context : Node_Id;
911 Context_Id : Entity_Id;
912 Decls : List_Id;
914 -- Start of processing for Build_Activation_Chain_Entity
916 begin
917 -- Activation chain is never used for sequential elaboration policy, see
918 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
920 if Partition_Elaboration_Policy = 'S' then
921 return;
922 end if;
924 Find_Enclosing_Context (N, Context, Context_Id, Decls);
926 -- If activation chain entity has not been declared already, create one
928 if Nkind (Context) = N_Extended_Return_Statement
929 or else No (Activation_Chain_Entity (Context))
930 then
931 -- Since extended return statements do not store the entity of the
932 -- chain, examine the return object declarations to avoid creating
933 -- a duplicate.
935 if Nkind (Context) = N_Extended_Return_Statement
936 and then Has_Activation_Chain (Context)
937 then
938 return;
939 end if;
941 declare
942 Loc : constant Source_Ptr := Sloc (Context);
943 Chain : Entity_Id;
944 Decl : Node_Id;
946 begin
947 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
949 -- Note: An extended return statement is not really a task
950 -- activator, but it does have an activation chain on which to
951 -- store the tasks temporarily. On successful return, the tasks
952 -- on this chain are moved to the chain passed in by the caller.
953 -- We do not build an Activation_Chain_Entity for an extended
954 -- return statement, because we do not want to build a call to
955 -- Activate_Tasks. Task activation is the responsibility of the
956 -- caller.
958 if Nkind (Context) /= N_Extended_Return_Statement then
959 Set_Activation_Chain_Entity (Context, Chain);
960 end if;
962 Decl :=
963 Make_Object_Declaration (Loc,
964 Defining_Identifier => Chain,
965 Aliased_Present => True,
966 Object_Definition =>
967 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
969 Prepend_To (Decls, Decl);
971 -- Ensure that _chain appears in the proper scope of the context
973 if Context_Id /= Current_Scope then
974 Push_Scope (Context_Id);
975 Analyze (Decl);
976 Pop_Scope;
977 else
978 Analyze (Decl);
979 end if;
980 end;
981 end if;
982 end Build_Activation_Chain_Entity;
984 ----------------------------
985 -- Build_Barrier_Function --
986 ----------------------------
988 function Build_Barrier_Function
989 (N : Node_Id;
990 Ent : Entity_Id;
991 Pid : Node_Id) return Node_Id
993 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
994 Cond : constant Node_Id := Condition (Ent_Formals);
995 Loc : constant Source_Ptr := Sloc (Cond);
996 Func_Id : constant Entity_Id := Barrier_Function (Ent);
997 Op_Decls : constant List_Id := New_List;
998 Stmt : Node_Id;
999 Func_Body : Node_Id;
1001 begin
1002 -- Add a declaration for the Protection object, renaming declarations
1003 -- for the discriminals and privals and finally a declaration for the
1004 -- entry family index (if applicable).
1006 Install_Private_Data_Declarations (Sloc (N),
1007 Spec_Id => Func_Id,
1008 Conc_Typ => Pid,
1009 Body_Nod => N,
1010 Decls => Op_Decls,
1011 Barrier => True,
1012 Family => Ekind (Ent) = E_Entry_Family);
1014 -- If compiling with -fpreserve-control-flow, make sure we insert an
1015 -- IF statement so that the back-end knows to generate a conditional
1016 -- branch instruction, even if the condition is just the name of a
1017 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1018 -- such redundant IF statements under -fpreserve-control-flow
1019 -- (whether coming from this routine, or directly from source).
1021 if Opt.Suppress_Control_Flow_Optimizations then
1022 Stmt := Make_Implicit_If_Statement (Cond,
1023 Condition => Cond,
1024 Then_Statements => New_List (
1025 Make_Simple_Return_Statement (Loc,
1026 New_Occurrence_Of (Standard_True, Loc))),
1027 Else_Statements => New_List (
1028 Make_Simple_Return_Statement (Loc,
1029 New_Occurrence_Of (Standard_False, Loc))));
1031 else
1032 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1033 end if;
1035 -- Note: the condition in the barrier function needs to be properly
1036 -- processed for the C/Fortran boolean possibility, but this happens
1037 -- automatically since the return statement does this normalization.
1039 Func_Body :=
1040 Make_Subprogram_Body (Loc,
1041 Specification =>
1042 Build_Barrier_Function_Specification (Loc,
1043 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1044 Declarations => Op_Decls,
1045 Handled_Statement_Sequence =>
1046 Make_Handled_Sequence_Of_Statements (Loc,
1047 Statements => New_List (Stmt)));
1048 Set_Is_Entry_Barrier_Function (Func_Body);
1050 return Func_Body;
1051 end Build_Barrier_Function;
1053 ------------------------------------------
1054 -- Build_Barrier_Function_Specification --
1055 ------------------------------------------
1057 function Build_Barrier_Function_Specification
1058 (Loc : Source_Ptr;
1059 Def_Id : Entity_Id) return Node_Id
1061 begin
1062 Set_Debug_Info_Needed (Def_Id);
1064 return Make_Function_Specification (Loc,
1065 Defining_Unit_Name => Def_Id,
1066 Parameter_Specifications => New_List (
1067 Make_Parameter_Specification (Loc,
1068 Defining_Identifier =>
1069 Make_Defining_Identifier (Loc, Name_uO),
1070 Parameter_Type =>
1071 New_Occurrence_Of (RTE (RE_Address), Loc)),
1073 Make_Parameter_Specification (Loc,
1074 Defining_Identifier =>
1075 Make_Defining_Identifier (Loc, Name_uE),
1076 Parameter_Type =>
1077 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1079 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc));
1080 end Build_Barrier_Function_Specification;
1082 --------------------------
1083 -- Build_Call_With_Task --
1084 --------------------------
1086 function Build_Call_With_Task
1087 (N : Node_Id;
1088 E : Entity_Id) return Node_Id
1090 Loc : constant Source_Ptr := Sloc (N);
1091 begin
1092 return
1093 Make_Function_Call (Loc,
1094 Name => New_Occurrence_Of (E, Loc),
1095 Parameter_Associations => New_List (Concurrent_Ref (N)));
1096 end Build_Call_With_Task;
1098 -----------------------------
1099 -- Build_Class_Wide_Master --
1100 -----------------------------
1102 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1103 Loc : constant Source_Ptr := Sloc (Typ);
1104 Master_Id : Entity_Id;
1105 Master_Scope : Entity_Id;
1106 Name_Id : Node_Id;
1107 Related_Node : Node_Id;
1108 Ren_Decl : Node_Id;
1110 begin
1111 -- Nothing to do if there is no task hierarchy
1113 if Restriction_Active (No_Task_Hierarchy) then
1114 return;
1115 end if;
1117 -- Find the declaration that created the access type, which is either a
1118 -- type declaration, or an object declaration with an access definition,
1119 -- in which case the type is anonymous.
1121 if Is_Itype (Typ) then
1122 Related_Node := Associated_Node_For_Itype (Typ);
1123 else
1124 Related_Node := Parent (Typ);
1125 end if;
1127 Master_Scope := Find_Master_Scope (Typ);
1129 -- Nothing to do if the master scope already contains a _master entity.
1130 -- The only exception to this is the following scenario:
1132 -- Source_Scope
1133 -- Transient_Scope_1
1134 -- _master
1136 -- Transient_Scope_2
1137 -- use of master
1139 -- In this case the source scope is marked as having the master entity
1140 -- even though the actual declaration appears inside an inner scope. If
1141 -- the second transient scope requires a _master, it cannot use the one
1142 -- already declared because the entity is not visible.
1144 Name_Id := Make_Identifier (Loc, Name_uMaster);
1146 if not Has_Master_Entity (Master_Scope)
1147 or else No (Current_Entity_In_Scope (Name_Id))
1148 then
1149 declare
1150 Master_Decl : Node_Id;
1151 begin
1152 Set_Has_Master_Entity (Master_Scope);
1154 -- Generate:
1155 -- _master : constant Integer := Current_Master.all;
1157 Master_Decl :=
1158 Make_Object_Declaration (Loc,
1159 Defining_Identifier =>
1160 Make_Defining_Identifier (Loc, Name_uMaster),
1161 Constant_Present => True,
1162 Object_Definition =>
1163 New_Occurrence_Of (Standard_Integer, Loc),
1164 Expression =>
1165 Make_Explicit_Dereference (Loc,
1166 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1168 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1169 Analyze (Master_Decl);
1171 -- Mark the containing scope as a task master. Masters associated
1172 -- with return statements are already marked at this stage (see
1173 -- Analyze_Subprogram_Body).
1175 if Ekind (Current_Scope) /= E_Return_Statement then
1176 declare
1177 Par : Node_Id := Related_Node;
1179 begin
1180 while Nkind (Par) /= N_Compilation_Unit loop
1181 Par := Parent (Par);
1183 -- If we fall off the top, we are at the outer level,
1184 -- and the environment task is our effective master,
1185 -- so nothing to mark.
1187 if Nkind_In (Par, N_Block_Statement,
1188 N_Subprogram_Body,
1189 N_Task_Body)
1190 then
1191 Set_Is_Task_Master (Par);
1192 exit;
1193 end if;
1194 end loop;
1195 end;
1196 end if;
1197 end;
1198 end if;
1200 Master_Id :=
1201 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1203 -- Generate:
1204 -- typeMnn renames _master;
1206 Ren_Decl :=
1207 Make_Object_Renaming_Declaration (Loc,
1208 Defining_Identifier => Master_Id,
1209 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1210 Name => Name_Id);
1212 Insert_Action (Related_Node, Ren_Decl);
1214 Set_Master_Id (Typ, Master_Id);
1215 end Build_Class_Wide_Master;
1217 --------------------------------
1218 -- Build_Corresponding_Record --
1219 --------------------------------
1221 function Build_Corresponding_Record
1222 (N : Node_Id;
1223 Ctyp : Entity_Id;
1224 Loc : Source_Ptr) return Node_Id
1226 Rec_Ent : constant Entity_Id :=
1227 Make_Defining_Identifier
1228 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1229 Disc : Entity_Id;
1230 Dlist : List_Id;
1231 New_Disc : Entity_Id;
1232 Cdecls : List_Id;
1234 begin
1235 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1236 Set_Ekind (Rec_Ent, E_Record_Type);
1237 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1238 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1239 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1240 Set_Stored_Constraint (Rec_Ent, No_Elist);
1241 Cdecls := New_List;
1243 -- Use discriminals to create list of discriminants for record, and
1244 -- create new discriminals for use in default expressions, etc. It is
1245 -- worth noting that a task discriminant gives rise to 5 entities;
1247 -- a) The original discriminant.
1248 -- b) The discriminal for use in the task.
1249 -- c) The discriminant of the corresponding record.
1250 -- d) The discriminal for the init proc of the corresponding record.
1251 -- e) The local variable that renames the discriminant in the procedure
1252 -- for the task body.
1254 -- In fact the discriminals b) are used in the renaming declarations
1255 -- for e). See details in einfo (Handling of Discriminants).
1257 if Present (Discriminant_Specifications (N)) then
1258 Dlist := New_List;
1259 Disc := First_Discriminant (Ctyp);
1261 while Present (Disc) loop
1262 New_Disc := CR_Discriminant (Disc);
1264 Append_To (Dlist,
1265 Make_Discriminant_Specification (Loc,
1266 Defining_Identifier => New_Disc,
1267 Discriminant_Type =>
1268 New_Occurrence_Of (Etype (Disc), Loc),
1269 Expression =>
1270 New_Copy (Discriminant_Default_Value (Disc))));
1272 Next_Discriminant (Disc);
1273 end loop;
1275 else
1276 Dlist := No_List;
1277 end if;
1279 -- Now we can construct the record type declaration. Note that this
1280 -- record is "limited tagged". It is "limited" to reflect the underlying
1281 -- limitedness of the task or protected object that it represents, and
1282 -- ensuring for example that it is properly passed by reference. It is
1283 -- "tagged" to give support to dispatching calls through interfaces. We
1284 -- propagate here the list of interfaces covered by the concurrent type
1285 -- (Ada 2005: AI-345).
1287 return
1288 Make_Full_Type_Declaration (Loc,
1289 Defining_Identifier => Rec_Ent,
1290 Discriminant_Specifications => Dlist,
1291 Type_Definition =>
1292 Make_Record_Definition (Loc,
1293 Component_List =>
1294 Make_Component_List (Loc, Component_Items => Cdecls),
1295 Tagged_Present =>
1296 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1297 Interface_List => Interface_List (N),
1298 Limited_Present => True));
1299 end Build_Corresponding_Record;
1301 ---------------------------------
1302 -- Build_Dispatching_Tag_Check --
1303 ---------------------------------
1305 function Build_Dispatching_Tag_Check
1306 (K : Entity_Id;
1307 N : Node_Id) return Node_Id
1309 Loc : constant Source_Ptr := Sloc (N);
1311 begin
1312 return
1313 Make_Op_Or (Loc,
1314 Make_Op_Eq (Loc,
1315 Left_Opnd =>
1316 New_Occurrence_Of (K, Loc),
1317 Right_Opnd =>
1318 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1320 Make_Op_Eq (Loc,
1321 Left_Opnd =>
1322 New_Occurrence_Of (K, Loc),
1323 Right_Opnd =>
1324 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1325 end Build_Dispatching_Tag_Check;
1327 ----------------------------------
1328 -- Build_Entry_Count_Expression --
1329 ----------------------------------
1331 function Build_Entry_Count_Expression
1332 (Concurrent_Type : Node_Id;
1333 Component_List : List_Id;
1334 Loc : Source_Ptr) return Node_Id
1336 Eindx : Nat;
1337 Ent : Entity_Id;
1338 Ecount : Node_Id;
1339 Comp : Node_Id;
1340 Lo : Node_Id;
1341 Hi : Node_Id;
1342 Typ : Entity_Id;
1343 Large : Boolean;
1345 begin
1346 -- Count number of non-family entries
1348 Eindx := 0;
1349 Ent := First_Entity (Concurrent_Type);
1350 while Present (Ent) loop
1351 if Ekind (Ent) = E_Entry then
1352 Eindx := Eindx + 1;
1353 end if;
1355 Next_Entity (Ent);
1356 end loop;
1358 Ecount := Make_Integer_Literal (Loc, Eindx);
1360 -- Loop through entry families building the addition nodes
1362 Ent := First_Entity (Concurrent_Type);
1363 Comp := First (Component_List);
1364 while Present (Ent) loop
1365 if Ekind (Ent) = E_Entry_Family then
1366 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1367 Next (Comp);
1368 end loop;
1370 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1371 Hi := Type_High_Bound (Typ);
1372 Lo := Type_Low_Bound (Typ);
1373 Large := Is_Potentially_Large_Family
1374 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1375 Ecount :=
1376 Make_Op_Add (Loc,
1377 Left_Opnd => Ecount,
1378 Right_Opnd =>
1379 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1380 end if;
1382 Next_Entity (Ent);
1383 end loop;
1385 return Ecount;
1386 end Build_Entry_Count_Expression;
1388 -----------------------
1389 -- Build_Entry_Names --
1390 -----------------------
1392 procedure Build_Entry_Names
1393 (Obj_Ref : Node_Id;
1394 Obj_Typ : Entity_Id;
1395 Stmts : List_Id)
1397 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1398 Data : Entity_Id := Empty;
1399 Index : Entity_Id := Empty;
1400 Typ : Entity_Id := Obj_Typ;
1402 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1403 -- Given an entry [family], create a static string which denotes the
1404 -- name of Comp_Id and assign it to the underlying data structure which
1405 -- contains the entry names of a concurrent object.
1407 function Object_Reference return Node_Id;
1408 -- Return a reference to field _object or _task_id depending on the
1409 -- concurrent object being processed.
1411 ----------------------
1412 -- Build_Entry_Name --
1413 ----------------------
1415 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1416 function Build_Range (Def : Node_Id) return Node_Id;
1417 -- Given a discrete subtype definition of an entry family, generate a
1418 -- range node which covers the range of Def's type.
1420 procedure Create_Index_And_Data;
1421 -- Generate the declarations of variables Index and Data. Subsequent
1422 -- calls do nothing.
1424 function Increment_Index return Node_Id;
1425 -- Increment the index used in the assignment of string names to the
1426 -- Data array.
1428 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1429 -- Given the name of a temporary variable, create the following
1430 -- declaration for it:
1432 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1434 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1435 -- Given the name of a temporary variable, place it in the array of
1436 -- string names. Generate:
1438 -- Data (Index) := Def_Id'Unchecked_Access;
1440 -----------------
1441 -- Build_Range --
1442 -----------------
1444 function Build_Range (Def : Node_Id) return Node_Id is
1445 High : Node_Id := Type_High_Bound (Etype (Def));
1446 Low : Node_Id := Type_Low_Bound (Etype (Def));
1448 begin
1449 -- If a bound references a discriminant, generate an identifier
1450 -- with the same name. Resolution will map it to the formals of
1451 -- the init proc.
1453 if Is_Entity_Name (Low)
1454 and then Ekind (Entity (Low)) = E_Discriminant
1455 then
1456 Low :=
1457 Make_Selected_Component (Loc,
1458 Prefix => New_Copy_Tree (Obj_Ref),
1459 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1460 else
1461 Low := New_Copy_Tree (Low);
1462 end if;
1464 if Is_Entity_Name (High)
1465 and then Ekind (Entity (High)) = E_Discriminant
1466 then
1467 High :=
1468 Make_Selected_Component (Loc,
1469 Prefix => New_Copy_Tree (Obj_Ref),
1470 Selector_Name => Make_Identifier (Loc, Chars (High)));
1471 else
1472 High := New_Copy_Tree (High);
1473 end if;
1475 return
1476 Make_Range (Loc,
1477 Low_Bound => Low,
1478 High_Bound => High);
1479 end Build_Range;
1481 ---------------------------
1482 -- Create_Index_And_Data --
1483 ---------------------------
1485 procedure Create_Index_And_Data is
1486 begin
1487 if No (Index) and then No (Data) then
1488 declare
1489 Count : RE_Id;
1490 Data_Typ : RE_Id;
1491 Size : Entity_Id;
1493 begin
1494 if Is_Protected_Type (Typ) then
1495 Count := RO_PE_Number_Of_Entries;
1496 Data_Typ := RE_Protected_Entry_Names_Array;
1497 else
1498 Count := RO_ST_Number_Of_Entries;
1499 Data_Typ := RE_Task_Entry_Names_Array;
1500 end if;
1502 -- Step 1: Generate the declaration of the index variable:
1504 -- Index : Entry_Index := 1;
1506 Index := Make_Temporary (Loc, 'I');
1508 Append_To (Stmts,
1509 Make_Object_Declaration (Loc,
1510 Defining_Identifier => Index,
1511 Object_Definition =>
1512 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1513 Expression => Make_Integer_Literal (Loc, 1)));
1515 -- Step 2: Generate the declaration of an array to house all
1516 -- names:
1518 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1519 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1521 Size := Make_Temporary (Loc, 'S');
1523 Append_To (Stmts,
1524 Make_Object_Declaration (Loc,
1525 Defining_Identifier => Size,
1526 Constant_Present => True,
1527 Object_Definition =>
1528 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1529 Expression =>
1530 Make_Function_Call (Loc,
1531 Name =>
1532 New_Occurrence_Of (RTE (Count), Loc),
1533 Parameter_Associations =>
1534 New_List (Object_Reference))));
1536 Data := Make_Temporary (Loc, 'A');
1538 Append_To (Stmts,
1539 Make_Object_Declaration (Loc,
1540 Defining_Identifier => Data,
1541 Aliased_Present => True,
1542 Object_Definition =>
1543 New_Occurrence_Of (RTE (Data_Typ), Loc),
1544 Expression =>
1545 Make_Aggregate (Loc,
1546 Component_Associations => New_List (
1547 Make_Component_Association (Loc,
1548 Choices => New_List (
1549 Make_Range (Loc,
1550 Low_Bound =>
1551 Make_Integer_Literal (Loc, 1),
1552 High_Bound =>
1553 New_Occurrence_Of (Size, Loc))),
1554 Expression => Make_Null (Loc))))));
1555 end;
1556 end if;
1557 end Create_Index_And_Data;
1559 ---------------------
1560 -- Increment_Index --
1561 ---------------------
1563 function Increment_Index return Node_Id is
1564 begin
1565 return
1566 Make_Assignment_Statement (Loc,
1567 Name => New_Occurrence_Of (Index, Loc),
1568 Expression =>
1569 Make_Op_Add (Loc,
1570 Left_Opnd => New_Occurrence_Of (Index, Loc),
1571 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1572 end Increment_Index;
1574 ----------------------
1575 -- Name_Declaration --
1576 ----------------------
1578 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1579 begin
1580 return
1581 Make_Object_Declaration (Loc,
1582 Defining_Identifier => Def_Id,
1583 Aliased_Present => True,
1584 Constant_Present => True,
1585 Object_Definition =>
1586 New_Occurrence_Of (Standard_String, Loc),
1587 Expression =>
1588 Make_String_Literal (Loc, String_From_Name_Buffer));
1589 end Name_Declaration;
1591 --------------------
1592 -- Set_Entry_Name --
1593 --------------------
1595 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1596 begin
1597 return
1598 Make_Assignment_Statement (Loc,
1599 Name =>
1600 Make_Indexed_Component (Loc,
1601 Prefix => New_Occurrence_Of (Data, Loc),
1602 Expressions => New_List (New_Occurrence_Of (Index, Loc))),
1604 Expression =>
1605 Make_Attribute_Reference (Loc,
1606 Prefix => New_Occurrence_Of (Def_Id, Loc),
1607 Attribute_Name => Name_Unchecked_Access));
1608 end Set_Entry_Name;
1610 -- Local variables
1612 Temp_Id : Entity_Id;
1613 Subt_Def : Node_Id;
1615 -- Start of processing for Build_Entry_Name
1617 begin
1618 if Ekind (Comp_Id) = E_Entry_Family then
1619 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1621 Create_Index_And_Data;
1623 -- Step 1: Create the string name of the entry family.
1624 -- Generate:
1625 -- Temp : aliased constant String := "name ()";
1627 Temp_Id := Make_Temporary (Loc, 'S');
1628 Get_Name_String (Chars (Comp_Id));
1629 Add_Char_To_Name_Buffer (' ');
1630 Add_Char_To_Name_Buffer ('(');
1631 Add_Char_To_Name_Buffer (')');
1633 Append_To (Stmts, Name_Declaration (Temp_Id));
1635 -- Generate:
1636 -- for Member in Family_Low .. Family_High loop
1637 -- Set_Entry_Name (...);
1638 -- Index := Index + 1;
1639 -- end loop;
1641 Append_To (Stmts,
1642 Make_Loop_Statement (Loc,
1643 Iteration_Scheme =>
1644 Make_Iteration_Scheme (Loc,
1645 Loop_Parameter_Specification =>
1646 Make_Loop_Parameter_Specification (Loc,
1647 Defining_Identifier =>
1648 Make_Temporary (Loc, 'L'),
1649 Discrete_Subtype_Definition =>
1650 Build_Range (Subt_Def))),
1652 Statements => New_List (
1653 Set_Entry_Name (Temp_Id),
1654 Increment_Index),
1655 End_Label => Empty));
1657 -- Entry
1659 else
1660 Create_Index_And_Data;
1662 -- Step 1: Create the string name of the entry. Generate:
1663 -- Temp : aliased constant String := "name";
1665 Temp_Id := Make_Temporary (Loc, 'S');
1666 Get_Name_String (Chars (Comp_Id));
1668 Append_To (Stmts, Name_Declaration (Temp_Id));
1670 -- Step 2: Associate the string name with the underlying data
1671 -- structure.
1673 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1674 Append_To (Stmts, Increment_Index);
1675 end if;
1676 end Build_Entry_Name;
1678 ----------------------
1679 -- Object_Reference --
1680 ----------------------
1682 function Object_Reference return Node_Id is
1683 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1684 Field : Name_Id;
1685 Ref : Node_Id;
1687 begin
1688 if Is_Protected_Type (Typ) then
1689 Field := Name_uObject;
1690 else
1691 Field := Name_uTask_Id;
1692 end if;
1694 Ref :=
1695 Make_Selected_Component (Loc,
1696 Prefix =>
1697 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1698 Selector_Name => Make_Identifier (Loc, Field));
1700 if Is_Protected_Type (Typ) then
1701 Ref :=
1702 Make_Attribute_Reference (Loc,
1703 Prefix => Ref,
1704 Attribute_Name => Name_Unchecked_Access);
1705 end if;
1707 return Ref;
1708 end Object_Reference;
1710 -- Local variables
1712 Comp : Node_Id;
1713 Proc : RE_Id;
1715 -- Start of processing for Build_Entry_Names
1717 begin
1718 -- Retrieve the original concurrent type
1720 if Is_Concurrent_Record_Type (Typ) then
1721 Typ := Corresponding_Concurrent_Type (Typ);
1722 end if;
1724 pragma Assert (Is_Concurrent_Type (Typ));
1726 -- Nothing to do if the type has no entries
1728 if not Has_Entries (Typ) then
1729 return;
1730 end if;
1732 -- Avoid generating entry names for a protected type with only one entry
1734 if Is_Protected_Type (Typ)
1735 and then Find_Protection_Type (Base_Type (Typ)) /=
1736 RTE (RE_Protection_Entries)
1737 then
1738 return;
1739 end if;
1741 -- Step 1: Populate the array with statically generated strings denoting
1742 -- entries and entry family names.
1744 Comp := First_Entity (Typ);
1745 while Present (Comp) loop
1746 if Comes_From_Source (Comp)
1747 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
1748 then
1749 Build_Entry_Name (Comp);
1750 end if;
1752 Next_Entity (Comp);
1753 end loop;
1755 -- Step 2: Associate the array with the related concurrent object:
1757 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
1759 if Present (Data) then
1760 if Is_Protected_Type (Typ) then
1761 Proc := RO_PE_Set_Entry_Names;
1762 else
1763 Proc := RO_ST_Set_Entry_Names;
1764 end if;
1766 Append_To (Stmts,
1767 Make_Procedure_Call_Statement (Loc,
1768 Name => New_Occurrence_Of (RTE (Proc), Loc),
1769 Parameter_Associations => New_List (
1770 Object_Reference,
1771 Make_Attribute_Reference (Loc,
1772 Prefix => New_Occurrence_Of (Data, Loc),
1773 Attribute_Name => Name_Unchecked_Access))));
1774 end if;
1775 end Build_Entry_Names;
1777 ---------------------------
1778 -- Build_Parameter_Block --
1779 ---------------------------
1781 function Build_Parameter_Block
1782 (Loc : Source_Ptr;
1783 Actuals : List_Id;
1784 Formals : List_Id;
1785 Decls : List_Id) return Entity_Id
1787 Actual : Entity_Id;
1788 Comp_Nam : Node_Id;
1789 Comps : List_Id;
1790 Formal : Entity_Id;
1791 Has_Comp : Boolean := False;
1792 Rec_Nam : Node_Id;
1794 begin
1795 Actual := First (Actuals);
1796 Comps := New_List;
1797 Formal := Defining_Identifier (First (Formals));
1799 while Present (Actual) loop
1800 if not Is_Controlling_Actual (Actual) then
1802 -- Generate:
1803 -- type Ann is access all <actual-type>
1805 Comp_Nam := Make_Temporary (Loc, 'A');
1807 Append_To (Decls,
1808 Make_Full_Type_Declaration (Loc,
1809 Defining_Identifier => Comp_Nam,
1810 Type_Definition =>
1811 Make_Access_To_Object_Definition (Loc,
1812 All_Present => True,
1813 Constant_Present => Ekind (Formal) = E_In_Parameter,
1814 Subtype_Indication =>
1815 New_Occurrence_Of (Etype (Actual), Loc))));
1817 -- Generate:
1818 -- Param : Ann;
1820 Append_To (Comps,
1821 Make_Component_Declaration (Loc,
1822 Defining_Identifier =>
1823 Make_Defining_Identifier (Loc, Chars (Formal)),
1824 Component_Definition =>
1825 Make_Component_Definition (Loc,
1826 Aliased_Present =>
1827 False,
1828 Subtype_Indication =>
1829 New_Occurrence_Of (Comp_Nam, Loc))));
1831 Has_Comp := True;
1832 end if;
1834 Next_Actual (Actual);
1835 Next_Formal_With_Extras (Formal);
1836 end loop;
1838 Rec_Nam := Make_Temporary (Loc, 'P');
1840 if Has_Comp then
1842 -- Generate:
1843 -- type Pnn is record
1844 -- Param1 : Ann1;
1845 -- ...
1846 -- ParamN : AnnN;
1848 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1849 -- the original parameter names and Ann1 .. AnnN are the access to
1850 -- actual types.
1852 Append_To (Decls,
1853 Make_Full_Type_Declaration (Loc,
1854 Defining_Identifier =>
1855 Rec_Nam,
1856 Type_Definition =>
1857 Make_Record_Definition (Loc,
1858 Component_List =>
1859 Make_Component_List (Loc, Comps))));
1860 else
1861 -- Generate:
1862 -- type Pnn is null record;
1864 Append_To (Decls,
1865 Make_Full_Type_Declaration (Loc,
1866 Defining_Identifier =>
1867 Rec_Nam,
1868 Type_Definition =>
1869 Make_Record_Definition (Loc,
1870 Null_Present => True,
1871 Component_List => Empty)));
1872 end if;
1874 return Rec_Nam;
1875 end Build_Parameter_Block;
1877 --------------------------------------
1878 -- Build_Renamed_Formal_Declaration --
1879 --------------------------------------
1881 function Build_Renamed_Formal_Declaration
1882 (New_F : Entity_Id;
1883 Formal : Entity_Id;
1884 Comp : Entity_Id;
1885 Renamed_Formal : Node_Id) return Node_Id
1887 Loc : constant Source_Ptr := Sloc (New_F);
1888 Decl : Node_Id;
1890 begin
1891 -- If the formal is a tagged incomplete type, it is already passed
1892 -- by reference, so it is sufficient to rename the pointer component
1893 -- that corresponds to the actual. Otherwise we need to dereference
1894 -- the pointer component to obtain the actual.
1896 if Is_Incomplete_Type (Etype (Formal))
1897 and then Is_Tagged_Type (Etype (Formal))
1898 then
1899 Decl :=
1900 Make_Object_Renaming_Declaration (Loc,
1901 Defining_Identifier => New_F,
1902 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1903 Name => Renamed_Formal);
1905 else
1906 Decl :=
1907 Make_Object_Renaming_Declaration (Loc,
1908 Defining_Identifier => New_F,
1909 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1910 Name =>
1911 Make_Explicit_Dereference (Loc, Renamed_Formal));
1912 end if;
1914 return Decl;
1915 end Build_Renamed_Formal_Declaration;
1917 -----------------------
1918 -- Build_PPC_Wrapper --
1919 -----------------------
1921 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1922 Loc : constant Source_Ptr := Sloc (E);
1923 Synch_Type : constant Entity_Id := Scope (E);
1925 Wrapper_Id : constant Entity_Id :=
1926 Make_Defining_Identifier (Loc,
1927 Chars => New_External_Name (Chars (E), 'E'));
1928 -- the wrapper procedure name
1930 Wrapper_Body : Node_Id;
1932 Synch_Id : constant Entity_Id :=
1933 Make_Defining_Identifier (Loc,
1934 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1935 -- The parameter that designates the synchronized object in the call
1937 Actuals : constant List_Id := New_List;
1938 -- The actuals in the entry call
1940 Decls : constant List_Id := New_List;
1942 Entry_Call : Node_Id;
1943 Entry_Name : Node_Id;
1945 Specs : List_Id;
1946 -- The specification of the wrapper procedure
1948 begin
1950 -- Only build the wrapper if entry has pre/postconditions.
1951 -- Should this be done unconditionally instead ???
1953 declare
1954 P : Node_Id;
1956 begin
1957 P := Pre_Post_Conditions (Contract (E));
1959 if No (P) then
1960 return;
1961 end if;
1963 -- Transfer ppc pragmas to the declarations of the wrapper
1965 while Present (P) loop
1966 if Nam_In (Pragma_Name (P), Name_Precondition,
1967 Name_Postcondition)
1968 then
1969 Append (Relocate_Node (P), Decls);
1970 Set_Analyzed (Last (Decls), False);
1971 end if;
1973 P := Next_Pragma (P);
1974 end loop;
1975 end;
1977 -- First formal is synchronized object
1979 Specs := New_List (
1980 Make_Parameter_Specification (Loc,
1981 Defining_Identifier => Synch_Id,
1982 Out_Present => True,
1983 In_Present => True,
1984 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1986 Entry_Name :=
1987 Make_Selected_Component (Loc,
1988 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1989 Selector_Name => New_Occurrence_Of (E, Loc));
1991 -- If entity is entry family, second formal is the corresponding index,
1992 -- and entry name is an indexed component.
1994 if Ekind (E) = E_Entry_Family then
1995 declare
1996 Index : constant Entity_Id :=
1997 Make_Defining_Identifier (Loc, Name_I);
1998 begin
1999 Append_To (Specs,
2000 Make_Parameter_Specification (Loc,
2001 Defining_Identifier => Index,
2002 Parameter_Type =>
2003 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
2005 Entry_Name :=
2006 Make_Indexed_Component (Loc,
2007 Prefix => Entry_Name,
2008 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
2009 end;
2010 end if;
2012 Entry_Call :=
2013 Make_Procedure_Call_Statement (Loc,
2014 Name => Entry_Name,
2015 Parameter_Associations => Actuals);
2017 -- Now add formals that match those of the entry, and build actuals for
2018 -- the nested entry call.
2020 declare
2021 Form : Entity_Id;
2022 New_Form : Entity_Id;
2023 Parm_Spec : Node_Id;
2025 begin
2026 Form := First_Formal (E);
2027 while Present (Form) loop
2028 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
2029 Parm_Spec :=
2030 Make_Parameter_Specification (Loc,
2031 Defining_Identifier => New_Form,
2032 Out_Present => Out_Present (Parent (Form)),
2033 In_Present => In_Present (Parent (Form)),
2034 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
2036 Append (Parm_Spec, Specs);
2037 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
2038 Next_Formal (Form);
2039 end loop;
2040 end;
2042 -- Add renaming declarations for the discriminants of the enclosing
2043 -- type, which may be visible in the preconditions.
2045 if Has_Discriminants (Synch_Type) then
2046 declare
2047 D : Entity_Id;
2048 Decl : Node_Id;
2050 begin
2051 D := First_Discriminant (Synch_Type);
2052 while Present (D) loop
2053 Decl :=
2054 Make_Object_Renaming_Declaration (Loc,
2055 Defining_Identifier =>
2056 Make_Defining_Identifier (Loc, Chars (D)),
2057 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
2058 Name =>
2059 Make_Selected_Component (Loc,
2060 Prefix => New_Occurrence_Of (Synch_Id, Loc),
2061 Selector_Name => Make_Identifier (Loc, Chars (D))));
2062 Prepend (Decl, Decls);
2063 Next_Discriminant (D);
2064 end loop;
2065 end;
2066 end if;
2068 Set_PPC_Wrapper (E, Wrapper_Id);
2069 Wrapper_Body :=
2070 Make_Subprogram_Body (Loc,
2071 Specification =>
2072 Make_Procedure_Specification (Loc,
2073 Defining_Unit_Name => Wrapper_Id,
2074 Parameter_Specifications => Specs),
2075 Declarations => Decls,
2076 Handled_Statement_Sequence =>
2077 Make_Handled_Sequence_Of_Statements (Loc,
2078 Statements => New_List (Entry_Call)));
2080 -- The wrapper body is analyzed when the enclosing type is frozen
2082 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
2083 end Build_PPC_Wrapper;
2085 --------------------------
2086 -- Build_Wrapper_Bodies --
2087 --------------------------
2089 procedure Build_Wrapper_Bodies
2090 (Loc : Source_Ptr;
2091 Typ : Entity_Id;
2092 N : Node_Id)
2094 Rec_Typ : Entity_Id;
2096 function Build_Wrapper_Body
2097 (Loc : Source_Ptr;
2098 Subp_Id : Entity_Id;
2099 Obj_Typ : Entity_Id;
2100 Formals : List_Id) return Node_Id;
2101 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2102 -- associated with a protected or task type. Subp_Id is the subprogram
2103 -- name which will be wrapped. Obj_Typ is the type of the new formal
2104 -- parameter which handles dispatching and object notation. Formals are
2105 -- the original formals of Subp_Id which will be explicitly replicated.
2107 ------------------------
2108 -- Build_Wrapper_Body --
2109 ------------------------
2111 function Build_Wrapper_Body
2112 (Loc : Source_Ptr;
2113 Subp_Id : Entity_Id;
2114 Obj_Typ : Entity_Id;
2115 Formals : List_Id) return Node_Id
2117 Body_Spec : Node_Id;
2119 begin
2120 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2122 -- The subprogram is not overriding or is not a primitive declared
2123 -- between two views.
2125 if No (Body_Spec) then
2126 return Empty;
2127 end if;
2129 declare
2130 Actuals : List_Id := No_List;
2131 Conv_Id : Node_Id;
2132 First_Form : Node_Id;
2133 Formal : Node_Id;
2134 Nam : Node_Id;
2136 begin
2137 -- Map formals to actuals. Use the list built for the wrapper
2138 -- spec, skipping the object notation parameter.
2140 First_Form := First (Parameter_Specifications (Body_Spec));
2142 Formal := First_Form;
2143 Next (Formal);
2145 if Present (Formal) then
2146 Actuals := New_List;
2147 while Present (Formal) loop
2148 Append_To (Actuals,
2149 Make_Identifier (Loc,
2150 Chars => Chars (Defining_Identifier (Formal))));
2151 Next (Formal);
2152 end loop;
2153 end if;
2155 -- Special processing for primitives declared between a private
2156 -- type and its completion: the wrapper needs a properly typed
2157 -- parameter if the wrapped operation has a controlling first
2158 -- parameter. Note that this might not be the case for a function
2159 -- with a controlling result.
2161 if Is_Private_Primitive_Subprogram (Subp_Id) then
2162 if No (Actuals) then
2163 Actuals := New_List;
2164 end if;
2166 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2167 Prepend_To (Actuals,
2168 Unchecked_Convert_To
2169 (Corresponding_Concurrent_Type (Obj_Typ),
2170 Make_Identifier (Loc, Name_uO)));
2172 else
2173 Prepend_To (Actuals,
2174 Make_Identifier (Loc,
2175 Chars => Chars (Defining_Identifier (First_Form))));
2176 end if;
2178 Nam := New_Occurrence_Of (Subp_Id, Loc);
2179 else
2180 -- An access-to-variable object parameter requires an explicit
2181 -- dereference in the unchecked conversion. This case occurs
2182 -- when a protected entry wrapper must override an interface
2183 -- level procedure with interface access as first parameter.
2185 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2187 if Nkind (Parameter_Type (First_Form)) =
2188 N_Access_Definition
2189 then
2190 Conv_Id :=
2191 Make_Explicit_Dereference (Loc,
2192 Prefix => Make_Identifier (Loc, Name_uO));
2193 else
2194 Conv_Id := Make_Identifier (Loc, Name_uO);
2195 end if;
2197 Nam :=
2198 Make_Selected_Component (Loc,
2199 Prefix =>
2200 Unchecked_Convert_To
2201 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2202 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
2203 end if;
2205 -- Create the subprogram body. For a function, the call to the
2206 -- actual subprogram has to be converted to the corresponding
2207 -- record if it is a controlling result.
2209 if Ekind (Subp_Id) = E_Function then
2210 declare
2211 Res : Node_Id;
2213 begin
2214 Res :=
2215 Make_Function_Call (Loc,
2216 Name => Nam,
2217 Parameter_Associations => Actuals);
2219 if Has_Controlling_Result (Subp_Id) then
2220 Res :=
2221 Unchecked_Convert_To
2222 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2223 end if;
2225 return
2226 Make_Subprogram_Body (Loc,
2227 Specification => Body_Spec,
2228 Declarations => Empty_List,
2229 Handled_Statement_Sequence =>
2230 Make_Handled_Sequence_Of_Statements (Loc,
2231 Statements => New_List (
2232 Make_Simple_Return_Statement (Loc, Res))));
2233 end;
2235 else
2236 return
2237 Make_Subprogram_Body (Loc,
2238 Specification => Body_Spec,
2239 Declarations => Empty_List,
2240 Handled_Statement_Sequence =>
2241 Make_Handled_Sequence_Of_Statements (Loc,
2242 Statements => New_List (
2243 Make_Procedure_Call_Statement (Loc,
2244 Name => Nam,
2245 Parameter_Associations => Actuals))));
2246 end if;
2247 end;
2248 end Build_Wrapper_Body;
2250 -- Start of processing for Build_Wrapper_Bodies
2252 begin
2253 if Is_Concurrent_Type (Typ) then
2254 Rec_Typ := Corresponding_Record_Type (Typ);
2255 else
2256 Rec_Typ := Typ;
2257 end if;
2259 -- Generate wrapper bodies for a concurrent type which implements an
2260 -- interface.
2262 if Present (Interfaces (Rec_Typ)) then
2263 declare
2264 Insert_Nod : Node_Id;
2265 Prim : Entity_Id;
2266 Prim_Elmt : Elmt_Id;
2267 Prim_Decl : Node_Id;
2268 Subp : Entity_Id;
2269 Wrap_Body : Node_Id;
2270 Wrap_Id : Entity_Id;
2272 begin
2273 Insert_Nod := N;
2275 -- Examine all primitive operations of the corresponding record
2276 -- type, looking for wrapper specs. Generate bodies in order to
2277 -- complete them.
2279 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2280 while Present (Prim_Elmt) loop
2281 Prim := Node (Prim_Elmt);
2283 if (Ekind (Prim) = E_Function
2284 or else Ekind (Prim) = E_Procedure)
2285 and then Is_Primitive_Wrapper (Prim)
2286 then
2287 Subp := Wrapped_Entity (Prim);
2288 Prim_Decl := Parent (Parent (Prim));
2290 Wrap_Body :=
2291 Build_Wrapper_Body (Loc,
2292 Subp_Id => Subp,
2293 Obj_Typ => Rec_Typ,
2294 Formals => Parameter_Specifications (Parent (Subp)));
2295 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2297 Set_Corresponding_Spec (Wrap_Body, Prim);
2298 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2300 Insert_After (Insert_Nod, Wrap_Body);
2301 Insert_Nod := Wrap_Body;
2303 Analyze (Wrap_Body);
2304 end if;
2306 Next_Elmt (Prim_Elmt);
2307 end loop;
2308 end;
2309 end if;
2310 end Build_Wrapper_Bodies;
2312 ------------------------
2313 -- Build_Wrapper_Spec --
2314 ------------------------
2316 function Build_Wrapper_Spec
2317 (Subp_Id : Entity_Id;
2318 Obj_Typ : Entity_Id;
2319 Formals : List_Id) return Node_Id
2321 Loc : constant Source_Ptr := Sloc (Subp_Id);
2322 First_Param : Node_Id;
2323 Iface : Entity_Id;
2324 Iface_Elmt : Elmt_Id;
2325 Iface_Op : Entity_Id;
2326 Iface_Op_Elmt : Elmt_Id;
2328 function Overriding_Possible
2329 (Iface_Op : Entity_Id;
2330 Wrapper : Entity_Id) return Boolean;
2331 -- Determine whether a primitive operation can be overridden by Wrapper.
2332 -- Iface_Op is the candidate primitive operation of an interface type,
2333 -- Wrapper is the generated entry wrapper.
2335 function Replicate_Formals
2336 (Loc : Source_Ptr;
2337 Formals : List_Id) return List_Id;
2338 -- An explicit parameter replication is required due to the Is_Entry_
2339 -- Formal flag being set for all the formals of an entry. The explicit
2340 -- replication removes the flag that would otherwise cause a different
2341 -- path of analysis.
2343 -------------------------
2344 -- Overriding_Possible --
2345 -------------------------
2347 function Overriding_Possible
2348 (Iface_Op : Entity_Id;
2349 Wrapper : Entity_Id) return Boolean
2351 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2352 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2354 function Type_Conformant_Parameters
2355 (Iface_Op_Params : List_Id;
2356 Wrapper_Params : List_Id) return Boolean;
2357 -- Determine whether the parameters of the generated entry wrapper
2358 -- and those of a primitive operation are type conformant. During
2359 -- this check, the first parameter of the primitive operation is
2360 -- skipped if it is a controlling argument: protected functions
2361 -- may have a controlling result.
2363 --------------------------------
2364 -- Type_Conformant_Parameters --
2365 --------------------------------
2367 function Type_Conformant_Parameters
2368 (Iface_Op_Params : List_Id;
2369 Wrapper_Params : List_Id) return Boolean
2371 Iface_Op_Param : Node_Id;
2372 Iface_Op_Typ : Entity_Id;
2373 Wrapper_Param : Node_Id;
2374 Wrapper_Typ : Entity_Id;
2376 begin
2377 -- Skip the first (controlling) parameter of primitive operation
2379 Iface_Op_Param := First (Iface_Op_Params);
2381 if Present (First_Formal (Iface_Op))
2382 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2383 then
2384 Iface_Op_Param := Next (Iface_Op_Param);
2385 end if;
2387 Wrapper_Param := First (Wrapper_Params);
2388 while Present (Iface_Op_Param)
2389 and then Present (Wrapper_Param)
2390 loop
2391 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2392 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2394 -- The two parameters must be mode conformant
2396 if not Conforming_Types
2397 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2398 then
2399 return False;
2400 end if;
2402 Next (Iface_Op_Param);
2403 Next (Wrapper_Param);
2404 end loop;
2406 -- One of the lists is longer than the other
2408 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2409 return False;
2410 end if;
2412 return True;
2413 end Type_Conformant_Parameters;
2415 -- Start of processing for Overriding_Possible
2417 begin
2418 if Chars (Iface_Op) /= Chars (Wrapper) then
2419 return False;
2420 end if;
2422 -- If an inherited subprogram is implemented by a protected procedure
2423 -- or an entry, then the first parameter of the inherited subprogram
2424 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2426 if Ekind (Iface_Op) = E_Procedure
2427 and then Present (Parameter_Specifications (Iface_Op_Spec))
2428 then
2429 declare
2430 Obj_Param : constant Node_Id :=
2431 First (Parameter_Specifications (Iface_Op_Spec));
2432 begin
2433 if not Out_Present (Obj_Param)
2434 and then Nkind (Parameter_Type (Obj_Param)) /=
2435 N_Access_Definition
2436 then
2437 return False;
2438 end if;
2439 end;
2440 end if;
2442 return
2443 Type_Conformant_Parameters (
2444 Parameter_Specifications (Iface_Op_Spec),
2445 Parameter_Specifications (Wrapper_Spec));
2446 end Overriding_Possible;
2448 -----------------------
2449 -- Replicate_Formals --
2450 -----------------------
2452 function Replicate_Formals
2453 (Loc : Source_Ptr;
2454 Formals : List_Id) return List_Id
2456 New_Formals : constant List_Id := New_List;
2457 Formal : Node_Id;
2458 Param_Type : Node_Id;
2460 begin
2461 Formal := First (Formals);
2463 -- Skip the object parameter when dealing with primitives declared
2464 -- between two views.
2466 if Is_Private_Primitive_Subprogram (Subp_Id)
2467 and then not Has_Controlling_Result (Subp_Id)
2468 then
2469 Formal := Next (Formal);
2470 end if;
2472 while Present (Formal) loop
2474 -- Create an explicit copy of the entry parameter
2476 -- When creating the wrapper subprogram for a primitive operation
2477 -- of a protected interface we must construct an equivalent
2478 -- signature to that of the overriding operation. For regular
2479 -- parameters we can just use the type of the formal, but for
2480 -- access to subprogram parameters we need to reanalyze the
2481 -- parameter type to create local entities for the signature of
2482 -- the subprogram type. Using the entities of the overriding
2483 -- subprogram will result in out-of-scope errors in the back-end.
2485 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2486 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2487 else
2488 Param_Type :=
2489 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2490 end if;
2492 Append_To (New_Formals,
2493 Make_Parameter_Specification (Loc,
2494 Defining_Identifier =>
2495 Make_Defining_Identifier (Loc,
2496 Chars => Chars
2497 (Defining_Identifier (Formal))),
2498 In_Present => In_Present (Formal),
2499 Out_Present => Out_Present (Formal),
2500 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2501 Parameter_Type => Param_Type));
2503 Next (Formal);
2504 end loop;
2506 return New_Formals;
2507 end Replicate_Formals;
2509 -- Start of processing for Build_Wrapper_Spec
2511 begin
2512 -- No point in building wrappers for untagged concurrent 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_Occurrence_Of (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_Occurrence_Of (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_Occurrence_Of (Obj_Typ, Loc),
2644 Null_Exclusion_Present =>
2645 Null_Exclusion_Present (Parameter_Type (First_Param)),
2646 Constant_Present =>
2647 Constant_Present (Parameter_Type (First_Param)));
2648 else
2649 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2650 end if;
2652 Obj_Param :=
2653 Make_Parameter_Specification (Loc,
2654 Defining_Identifier =>
2655 Make_Defining_Identifier (Loc,
2656 Chars => Name_uO),
2657 In_Present => In_Present (First_Param),
2658 Out_Present => Out_Present (First_Param),
2659 Parameter_Type => Obj_Param_Typ);
2661 Prepend_To (New_Formals, Obj_Param);
2663 -- If we are dealing with a primitive declared between two views,
2664 -- implemented by a synchronized operation, we need to create
2665 -- a default parameter. The mode of the parameter must match that
2666 -- of the primitive operation.
2668 else
2669 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2670 Obj_Param :=
2671 Make_Parameter_Specification (Loc,
2672 Defining_Identifier =>
2673 Make_Defining_Identifier (Loc, Name_uO),
2674 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2675 Out_Present => Ekind (Subp_Id) /= E_Function,
2676 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2677 Prepend_To (New_Formals, Obj_Param);
2678 end if;
2680 -- Build the final spec. If it is a function with a controlling
2681 -- result, it is a primitive operation of the corresponding
2682 -- record type, so mark the spec accordingly.
2684 if Ekind (Subp_Id) = E_Function then
2685 declare
2686 Res_Def : Node_Id;
2688 begin
2689 if Has_Controlling_Result (Subp_Id) then
2690 Res_Def :=
2691 New_Occurrence_Of
2692 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2693 else
2694 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2695 end if;
2697 return
2698 Make_Function_Specification (Loc,
2699 Defining_Unit_Name => Wrapper_Id,
2700 Parameter_Specifications => New_Formals,
2701 Result_Definition => Res_Def);
2702 end;
2703 else
2704 return
2705 Make_Procedure_Specification (Loc,
2706 Defining_Unit_Name => Wrapper_Id,
2707 Parameter_Specifications => New_Formals);
2708 end if;
2709 end;
2710 end Build_Wrapper_Spec;
2712 -------------------------
2713 -- Build_Wrapper_Specs --
2714 -------------------------
2716 procedure Build_Wrapper_Specs
2717 (Loc : Source_Ptr;
2718 Typ : Entity_Id;
2719 N : in out Node_Id)
2721 Def : Node_Id;
2722 Rec_Typ : Entity_Id;
2723 procedure Scan_Declarations (L : List_Id);
2724 -- Common processing for visible and private declarations
2725 -- of a protected type.
2727 procedure Scan_Declarations (L : List_Id) is
2728 Decl : Node_Id;
2729 Wrap_Decl : Node_Id;
2730 Wrap_Spec : Node_Id;
2732 begin
2733 if No (L) then
2734 return;
2735 end if;
2737 Decl := First (L);
2738 while Present (Decl) loop
2739 Wrap_Spec := Empty;
2741 if Nkind (Decl) = N_Entry_Declaration
2742 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2743 then
2744 Wrap_Spec :=
2745 Build_Wrapper_Spec
2746 (Subp_Id => Defining_Identifier (Decl),
2747 Obj_Typ => Rec_Typ,
2748 Formals => Parameter_Specifications (Decl));
2750 elsif Nkind (Decl) = N_Subprogram_Declaration then
2751 Wrap_Spec :=
2752 Build_Wrapper_Spec
2753 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2754 Obj_Typ => Rec_Typ,
2755 Formals =>
2756 Parameter_Specifications (Specification (Decl)));
2757 end if;
2759 if Present (Wrap_Spec) then
2760 Wrap_Decl :=
2761 Make_Subprogram_Declaration (Loc,
2762 Specification => Wrap_Spec);
2764 Insert_After (N, Wrap_Decl);
2765 N := Wrap_Decl;
2767 Analyze (Wrap_Decl);
2768 end if;
2770 Next (Decl);
2771 end loop;
2772 end Scan_Declarations;
2774 -- start of processing for Build_Wrapper_Specs
2776 begin
2777 if Is_Protected_Type (Typ) then
2778 Def := Protected_Definition (Parent (Typ));
2779 else pragma Assert (Is_Task_Type (Typ));
2780 Def := Task_Definition (Parent (Typ));
2781 end if;
2783 Rec_Typ := Corresponding_Record_Type (Typ);
2785 -- Generate wrapper specs for a concurrent type which implements an
2786 -- interface. Operations in both the visible and private parts may
2787 -- implement progenitor operations.
2789 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2790 Scan_Declarations (Visible_Declarations (Def));
2791 Scan_Declarations (Private_Declarations (Def));
2792 end if;
2793 end Build_Wrapper_Specs;
2795 ---------------------------
2796 -- Build_Find_Body_Index --
2797 ---------------------------
2799 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2800 Loc : constant Source_Ptr := Sloc (Typ);
2801 Ent : Entity_Id;
2802 E_Typ : Entity_Id;
2803 Has_F : Boolean := False;
2804 Index : Nat;
2805 If_St : Node_Id := Empty;
2806 Lo : Node_Id;
2807 Hi : Node_Id;
2808 Decls : List_Id := New_List;
2809 Ret : Node_Id;
2810 Spec : Node_Id;
2811 Siz : Node_Id := Empty;
2813 procedure Add_If_Clause (Expr : Node_Id);
2814 -- Add test for range of current entry
2816 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2817 -- If a bound of an entry is given by a discriminant, retrieve the
2818 -- actual value of the discriminant from the enclosing object.
2820 -------------------
2821 -- Add_If_Clause --
2822 -------------------
2824 procedure Add_If_Clause (Expr : Node_Id) is
2825 Cond : Node_Id;
2826 Stats : constant List_Id :=
2827 New_List (
2828 Make_Simple_Return_Statement (Loc,
2829 Expression => Make_Integer_Literal (Loc, Index + 1)));
2831 begin
2832 -- Index for current entry body
2834 Index := Index + 1;
2836 -- Compute total length of entry queues so far
2838 if No (Siz) then
2839 Siz := Expr;
2840 else
2841 Siz :=
2842 Make_Op_Add (Loc,
2843 Left_Opnd => Siz,
2844 Right_Opnd => Expr);
2845 end if;
2847 Cond :=
2848 Make_Op_Le (Loc,
2849 Left_Opnd => Make_Identifier (Loc, Name_uE),
2850 Right_Opnd => Siz);
2852 -- Map entry queue indexes in the range of the current family
2853 -- into the current index, that designates the entry body.
2855 if No (If_St) then
2856 If_St :=
2857 Make_Implicit_If_Statement (Typ,
2858 Condition => Cond,
2859 Then_Statements => Stats,
2860 Elsif_Parts => New_List);
2861 Ret := If_St;
2863 else
2864 Append_To (Elsif_Parts (If_St),
2865 Make_Elsif_Part (Loc,
2866 Condition => Cond,
2867 Then_Statements => Stats));
2868 end if;
2869 end Add_If_Clause;
2871 ------------------------------
2872 -- Convert_Discriminant_Ref --
2873 ------------------------------
2875 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2876 B : Node_Id;
2878 begin
2879 if Is_Entity_Name (Bound)
2880 and then Ekind (Entity (Bound)) = E_Discriminant
2881 then
2882 B :=
2883 Make_Selected_Component (Loc,
2884 Prefix =>
2885 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2886 Make_Explicit_Dereference (Loc,
2887 Make_Identifier (Loc, Name_uObject))),
2888 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2889 Set_Etype (B, Etype (Entity (Bound)));
2890 else
2891 B := New_Copy_Tree (Bound);
2892 end if;
2894 return B;
2895 end Convert_Discriminant_Ref;
2897 -- Start of processing for Build_Find_Body_Index
2899 begin
2900 Spec := Build_Find_Body_Index_Spec (Typ);
2902 Ent := First_Entity (Typ);
2903 while Present (Ent) loop
2904 if Ekind (Ent) = E_Entry_Family then
2905 Has_F := True;
2906 exit;
2907 end if;
2909 Next_Entity (Ent);
2910 end loop;
2912 if not Has_F then
2914 -- If the protected type has no entry families, there is a one-one
2915 -- correspondence between entry queue and entry body.
2917 Ret :=
2918 Make_Simple_Return_Statement (Loc,
2919 Expression => Make_Identifier (Loc, Name_uE));
2921 else
2922 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2923 -- the following:
2925 -- if E <= l1 then return 1;
2926 -- elsif E <= l1 + l2 then return 2;
2927 -- ...
2929 Index := 0;
2930 Siz := Empty;
2931 Ent := First_Entity (Typ);
2933 Add_Object_Pointer (Loc, Typ, Decls);
2935 while Present (Ent) loop
2936 if Ekind (Ent) = E_Entry then
2937 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2939 elsif Ekind (Ent) = E_Entry_Family then
2940 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2941 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2942 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2943 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2944 end if;
2946 Next_Entity (Ent);
2947 end loop;
2949 if Index = 1 then
2950 Decls := New_List;
2951 Ret :=
2952 Make_Simple_Return_Statement (Loc,
2953 Expression => Make_Integer_Literal (Loc, 1));
2955 elsif Nkind (Ret) = N_If_Statement then
2957 -- Ranges are in increasing order, so last one doesn't need guard
2959 declare
2960 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2961 begin
2962 Remove (Nod);
2963 Set_Else_Statements (Ret, Then_Statements (Nod));
2964 end;
2965 end if;
2966 end if;
2968 return
2969 Make_Subprogram_Body (Loc,
2970 Specification => Spec,
2971 Declarations => Decls,
2972 Handled_Statement_Sequence =>
2973 Make_Handled_Sequence_Of_Statements (Loc,
2974 Statements => New_List (Ret)));
2975 end Build_Find_Body_Index;
2977 --------------------------------
2978 -- Build_Find_Body_Index_Spec --
2979 --------------------------------
2981 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2982 Loc : constant Source_Ptr := Sloc (Typ);
2983 Id : constant Entity_Id :=
2984 Make_Defining_Identifier (Loc,
2985 Chars => New_External_Name (Chars (Typ), 'F'));
2986 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2987 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2989 begin
2990 return
2991 Make_Function_Specification (Loc,
2992 Defining_Unit_Name => Id,
2993 Parameter_Specifications => New_List (
2994 Make_Parameter_Specification (Loc,
2995 Defining_Identifier => Parm1,
2996 Parameter_Type =>
2997 New_Occurrence_Of (RTE (RE_Address), Loc)),
2999 Make_Parameter_Specification (Loc,
3000 Defining_Identifier => Parm2,
3001 Parameter_Type =>
3002 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
3004 Result_Definition => New_Occurrence_Of (
3005 RTE (RE_Protected_Entry_Index), Loc));
3006 end Build_Find_Body_Index_Spec;
3008 -----------------------------------------------
3009 -- Build_Lock_Free_Protected_Subprogram_Body --
3010 -----------------------------------------------
3012 function Build_Lock_Free_Protected_Subprogram_Body
3013 (N : Node_Id;
3014 Prot_Typ : Node_Id;
3015 Unprot_Spec : Node_Id) return Node_Id
3017 Actuals : constant List_Id := New_List;
3018 Loc : constant Source_Ptr := Sloc (N);
3019 Spec : constant Node_Id := Specification (N);
3020 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3021 Formal : Node_Id;
3022 Prot_Spec : Node_Id;
3023 Stmt : Node_Id;
3025 begin
3026 -- Create the protected version of the body
3028 Prot_Spec :=
3029 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3031 -- Build the actual parameters which appear in the call to the
3032 -- unprotected version of the body.
3034 Formal := First (Parameter_Specifications (Prot_Spec));
3035 while Present (Formal) loop
3036 Append_To (Actuals,
3037 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3039 Next (Formal);
3040 end loop;
3042 -- Function case, generate:
3043 -- return <Unprot_Func_Call>;
3045 if Nkind (Spec) = N_Function_Specification then
3046 Stmt :=
3047 Make_Simple_Return_Statement (Loc,
3048 Expression =>
3049 Make_Function_Call (Loc,
3050 Name =>
3051 Make_Identifier (Loc, Chars (Unprot_Id)),
3052 Parameter_Associations => Actuals));
3054 -- Procedure case, call the unprotected version
3056 else
3057 Stmt :=
3058 Make_Procedure_Call_Statement (Loc,
3059 Name =>
3060 Make_Identifier (Loc, Chars (Unprot_Id)),
3061 Parameter_Associations => Actuals);
3062 end if;
3064 return
3065 Make_Subprogram_Body (Loc,
3066 Declarations => Empty_List,
3067 Specification => Prot_Spec,
3068 Handled_Statement_Sequence =>
3069 Make_Handled_Sequence_Of_Statements (Loc,
3070 Statements => New_List (Stmt)));
3071 end Build_Lock_Free_Protected_Subprogram_Body;
3073 -------------------------------------------------
3074 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3075 -------------------------------------------------
3077 -- Procedures which meet the lock-free implementation requirements and
3078 -- reference a unique scalar component Comp are expanded in the following
3079 -- manner:
3081 -- procedure P (...) is
3082 -- Expected_Comp : constant Comp_Type :=
3083 -- Comp_Type
3084 -- (System.Atomic_Primitives.Lock_Free_Read_N
3085 -- (_Object.Comp'Address));
3086 -- begin
3087 -- loop
3088 -- declare
3089 -- <original declarations before the object renaming declaration
3090 -- of Comp>
3092 -- Desired_Comp : Comp_Type := Expected_Comp;
3093 -- Comp : Comp_Type renames Desired_Comp;
3095 -- <original delarations after the object renaming declaration
3096 -- of Comp>
3098 -- begin
3099 -- <original statements>
3100 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3101 -- (_Object.Comp'Address,
3102 -- Interfaces.Unsigned_N (Expected_Comp),
3103 -- Interfaces.Unsigned_N (Desired_Comp));
3104 -- end;
3105 -- end loop;
3106 -- end P;
3108 -- Each return and raise statement of P is transformed into an atomic
3109 -- status check:
3111 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3112 -- (_Object.Comp'Address,
3113 -- Interfaces.Unsigned_N (Expected_Comp),
3114 -- Interfaces.Unsigned_N (Desired_Comp));
3115 -- then
3116 -- <original statement>
3117 -- else
3118 -- goto L0;
3119 -- end if;
3121 -- Functions which meet the lock-free implementation requirements and
3122 -- reference a unique scalar component Comp are expanded in the following
3123 -- manner:
3125 -- function F (...) return ... is
3126 -- <original declarations before the object renaming declaration
3127 -- of Comp>
3129 -- Expected_Comp : constant Comp_Type :=
3130 -- Comp_Type
3131 -- (System.Atomic_Primitives.Lock_Free_Read_N
3132 -- (_Object.Comp'Address));
3133 -- Comp : Comp_Type renames Expected_Comp;
3135 -- <original delarations after the object renaming declaration of
3136 -- Comp>
3138 -- begin
3139 -- <original statements>
3140 -- end F;
3142 function Build_Lock_Free_Unprotected_Subprogram_Body
3143 (N : Node_Id;
3144 Prot_Typ : Node_Id) return Node_Id
3146 function Referenced_Component (N : Node_Id) return Entity_Id;
3147 -- Subprograms which meet the lock-free implementation criteria are
3148 -- allowed to reference only one unique component. Return the prival
3149 -- of the said component.
3151 --------------------------
3152 -- Referenced_Component --
3153 --------------------------
3155 function Referenced_Component (N : Node_Id) return Entity_Id is
3156 Comp : Entity_Id;
3157 Decl : Node_Id;
3158 Source_Comp : Entity_Id := Empty;
3160 begin
3161 -- Find the unique source component which N references in its
3162 -- statements.
3164 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3165 declare
3166 Element : Lock_Free_Subprogram renames
3167 Lock_Free_Subprogram_Table.Table (Index);
3168 begin
3169 if Element.Sub_Body = N then
3170 Source_Comp := Element.Comp_Id;
3171 exit;
3172 end if;
3173 end;
3174 end loop;
3176 if No (Source_Comp) then
3177 return Empty;
3178 end if;
3180 -- Find the prival which corresponds to the source component within
3181 -- the declarations of N.
3183 Decl := First (Declarations (N));
3184 while Present (Decl) loop
3186 -- Privals appear as object renamings
3188 if Nkind (Decl) = N_Object_Renaming_Declaration then
3189 Comp := Defining_Identifier (Decl);
3191 if Present (Prival_Link (Comp))
3192 and then Prival_Link (Comp) = Source_Comp
3193 then
3194 return Comp;
3195 end if;
3196 end if;
3198 Next (Decl);
3199 end loop;
3201 return Empty;
3202 end Referenced_Component;
3204 -- Local variables
3206 Comp : constant Entity_Id := Referenced_Component (N);
3207 Loc : constant Source_Ptr := Sloc (N);
3208 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3209 Decls : List_Id := Declarations (N);
3211 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3213 begin
3214 -- Add renamings for the protection object, discriminals, privals and
3215 -- the entry index constant for use by debugger.
3217 Debug_Private_Data_Declarations (Decls);
3219 -- Perform the lock-free expansion when the subprogram references a
3220 -- protected component.
3222 if Present (Comp) then
3223 Protected_Component_Ref : declare
3224 Comp_Decl : constant Node_Id := Parent (Comp);
3225 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3226 Comp_Type : constant Entity_Id := Etype (Comp);
3228 Is_Procedure : constant Boolean :=
3229 Ekind (Corresponding_Spec (N)) = E_Procedure;
3230 -- Indicates if N is a protected procedure body
3232 Block_Decls : List_Id;
3233 Try_Write : Entity_Id;
3234 Desired_Comp : Entity_Id;
3235 Decl : Node_Id;
3236 Label : Node_Id;
3237 Label_Id : Entity_Id := Empty;
3238 Read : Entity_Id;
3239 Expected_Comp : Entity_Id;
3240 Stmt : Node_Id;
3241 Stmts : List_Id :=
3242 New_Copy_List (Statements (Hand_Stmt_Seq));
3243 Typ_Size : Int;
3244 Unsigned : Entity_Id;
3246 function Process_Node (N : Node_Id) return Traverse_Result;
3247 -- Transform a single node if it is a return statement, a raise
3248 -- statement or a reference to Comp.
3250 procedure Process_Stmts (Stmts : List_Id);
3251 -- Given a statement sequence Stmts, wrap any return or raise
3252 -- statements in the following manner:
3254 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3255 -- (_Object.Comp'Address,
3256 -- Interfaces.Unsigned_N (Expected_Comp),
3257 -- Interfaces.Unsigned_N (Desired_Comp))
3258 -- then
3259 -- <Stmt>;
3260 -- else
3261 -- goto L0;
3262 -- end if;
3264 ------------------
3265 -- Process_Node --
3266 ------------------
3268 function Process_Node (N : Node_Id) return Traverse_Result is
3270 procedure Wrap_Statement (Stmt : Node_Id);
3271 -- Wrap an arbitrary statement inside an if statement where the
3272 -- condition does an atomic check on the state of the object.
3274 --------------------
3275 -- Wrap_Statement --
3276 --------------------
3278 procedure Wrap_Statement (Stmt : Node_Id) is
3279 begin
3280 -- The first time through, create the declaration of a label
3281 -- which is used to skip the remainder of source statements
3282 -- if the state of the object has changed.
3284 if No (Label_Id) then
3285 Label_Id :=
3286 Make_Identifier (Loc, New_External_Name ('L', 0));
3287 Set_Entity (Label_Id,
3288 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3289 end if;
3291 -- Generate:
3292 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3293 -- (_Object.Comp'Address,
3294 -- Interfaces.Unsigned_N (Expected_Comp),
3295 -- Interfaces.Unsigned_N (Desired_Comp))
3296 -- then
3297 -- <Stmt>;
3298 -- else
3299 -- goto L0;
3300 -- end if;
3302 Rewrite (Stmt,
3303 Make_Implicit_If_Statement (N,
3304 Condition =>
3305 Make_Function_Call (Loc,
3306 Name =>
3307 New_Occurrence_Of (Try_Write, Loc),
3308 Parameter_Associations => New_List (
3309 Make_Attribute_Reference (Loc,
3310 Prefix => Relocate_Node (Comp_Sel_Nam),
3311 Attribute_Name => Name_Address),
3313 Unchecked_Convert_To (Unsigned,
3314 New_Occurrence_Of (Expected_Comp, Loc)),
3316 Unchecked_Convert_To (Unsigned,
3317 New_Occurrence_Of (Desired_Comp, Loc)))),
3319 Then_Statements => New_List (Relocate_Node (Stmt)),
3321 Else_Statements => New_List (
3322 Make_Goto_Statement (Loc,
3323 Name =>
3324 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3325 end Wrap_Statement;
3327 -- Start of processing for Process_Node
3329 begin
3330 -- Wrap each return and raise statement that appear inside a
3331 -- procedure. Skip the last return statement which is added by
3332 -- default since it is transformed into an exit statement.
3334 if Is_Procedure
3335 and then ((Nkind (N) = N_Simple_Return_Statement
3336 and then N /= Last (Stmts))
3337 or else Nkind (N) = N_Extended_Return_Statement
3338 or else (Nkind_In (N, N_Raise_Constraint_Error,
3339 N_Raise_Program_Error,
3340 N_Raise_Statement,
3341 N_Raise_Storage_Error)
3342 and then Comes_From_Source (N)))
3343 then
3344 Wrap_Statement (N);
3345 return Skip;
3346 end if;
3348 -- Force reanalysis
3350 Set_Analyzed (N, False);
3352 return OK;
3353 end Process_Node;
3355 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3357 -------------------
3358 -- Process_Stmts --
3359 -------------------
3361 procedure Process_Stmts (Stmts : List_Id) is
3362 Stmt : Node_Id;
3363 begin
3364 Stmt := First (Stmts);
3365 while Present (Stmt) loop
3366 Process_Nodes (Stmt);
3367 Next (Stmt);
3368 end loop;
3369 end Process_Stmts;
3371 -- Start of processing for Protected_Component_Ref
3373 begin
3374 -- Get the type size
3376 if Known_Static_Esize (Comp_Type) then
3377 Typ_Size := UI_To_Int (Esize (Comp_Type));
3379 -- If the Esize (Object_Size) is unknown at compile time, look at
3380 -- the RM_Size (Value_Size) since it may have been set by an
3381 -- explicit representation clause.
3383 elsif Known_Static_RM_Size (Comp_Type) then
3384 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3386 -- Should not happen since this has already been checked in
3387 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3389 else
3390 raise Program_Error;
3391 end if;
3393 -- Retrieve all relevant atomic routines and types
3395 case Typ_Size is
3396 when 8 =>
3397 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3398 Read := RTE (RE_Lock_Free_Read_8);
3399 Unsigned := RTE (RE_Uint8);
3401 when 16 =>
3402 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3403 Read := RTE (RE_Lock_Free_Read_16);
3404 Unsigned := RTE (RE_Uint16);
3406 when 32 =>
3407 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3408 Read := RTE (RE_Lock_Free_Read_32);
3409 Unsigned := RTE (RE_Uint32);
3411 when 64 =>
3412 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3413 Read := RTE (RE_Lock_Free_Read_64);
3414 Unsigned := RTE (RE_Uint64);
3416 when others =>
3417 raise Program_Error;
3418 end case;
3420 -- Generate:
3421 -- Expected_Comp : constant Comp_Type :=
3422 -- Comp_Type
3423 -- (System.Atomic_Primitives.Lock_Free_Read_N
3424 -- (_Object.Comp'Address));
3426 Expected_Comp :=
3427 Make_Defining_Identifier (Loc,
3428 New_External_Name (Chars (Comp), Suffix => "_saved"));
3430 Decl :=
3431 Make_Object_Declaration (Loc,
3432 Defining_Identifier => Expected_Comp,
3433 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3434 Constant_Present => True,
3435 Expression =>
3436 Unchecked_Convert_To (Comp_Type,
3437 Make_Function_Call (Loc,
3438 Name => New_Occurrence_Of (Read, Loc),
3439 Parameter_Associations => New_List (
3440 Make_Attribute_Reference (Loc,
3441 Prefix => Relocate_Node (Comp_Sel_Nam),
3442 Attribute_Name => Name_Address)))));
3444 -- Protected procedures
3446 if Is_Procedure then
3447 -- Move the original declarations inside the generated block
3449 Block_Decls := Decls;
3451 -- Reset the declarations list of the protected procedure to
3452 -- contain only Decl.
3454 Decls := New_List (Decl);
3456 -- Generate:
3457 -- Desired_Comp : Comp_Type := Expected_Comp;
3459 Desired_Comp :=
3460 Make_Defining_Identifier (Loc,
3461 New_External_Name (Chars (Comp), Suffix => "_current"));
3463 -- Insert the declarations of Expected_Comp and Desired_Comp in
3464 -- the block declarations right before the renaming of the
3465 -- protected component.
3467 Insert_Before (Comp_Decl,
3468 Make_Object_Declaration (Loc,
3469 Defining_Identifier => Desired_Comp,
3470 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3471 Expression =>
3472 New_Occurrence_Of (Expected_Comp, Loc)));
3474 -- Protected function
3476 else
3477 Desired_Comp := Expected_Comp;
3479 -- Insert the declaration of Expected_Comp in the function
3480 -- declarations right before the renaming of the protected
3481 -- component.
3483 Insert_Before (Comp_Decl, Decl);
3484 end if;
3486 -- Rewrite the protected component renaming declaration to be a
3487 -- renaming of Desired_Comp.
3489 -- Generate:
3490 -- Comp : Comp_Type renames Desired_Comp;
3492 Rewrite (Comp_Decl,
3493 Make_Object_Renaming_Declaration (Loc,
3494 Defining_Identifier =>
3495 Defining_Identifier (Comp_Decl),
3496 Subtype_Mark =>
3497 New_Occurrence_Of (Comp_Type, Loc),
3498 Name =>
3499 New_Occurrence_Of (Desired_Comp, Loc)));
3501 -- Wrap any return or raise statements in Stmts in same the manner
3502 -- described in Process_Stmts.
3504 Process_Stmts (Stmts);
3506 -- Generate:
3507 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3508 -- (_Object.Comp'Address,
3509 -- Interfaces.Unsigned_N (Expected_Comp),
3510 -- Interfaces.Unsigned_N (Desired_Comp))
3512 if Is_Procedure then
3513 Stmt :=
3514 Make_Exit_Statement (Loc,
3515 Condition =>
3516 Make_Function_Call (Loc,
3517 Name =>
3518 New_Occurrence_Of (Try_Write, Loc),
3519 Parameter_Associations => New_List (
3520 Make_Attribute_Reference (Loc,
3521 Prefix => Relocate_Node (Comp_Sel_Nam),
3522 Attribute_Name => Name_Address),
3524 Unchecked_Convert_To (Unsigned,
3525 New_Occurrence_Of (Expected_Comp, Loc)),
3527 Unchecked_Convert_To (Unsigned,
3528 New_Occurrence_Of (Desired_Comp, Loc)))));
3530 -- Small optimization: transform the default return statement
3531 -- of a procedure into the atomic exit statement.
3533 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3534 Rewrite (Last (Stmts), Stmt);
3535 else
3536 Append_To (Stmts, Stmt);
3537 end if;
3538 end if;
3540 -- Create the declaration of the label used to skip the rest of
3541 -- the source statements when the object state changes.
3543 if Present (Label_Id) then
3544 Label := Make_Label (Loc, Label_Id);
3545 Append_To (Decls,
3546 Make_Implicit_Label_Declaration (Loc,
3547 Defining_Identifier => Entity (Label_Id),
3548 Label_Construct => Label));
3549 Append_To (Stmts, Label);
3550 end if;
3552 -- Generate:
3553 -- loop
3554 -- declare
3555 -- <Decls>
3556 -- begin
3557 -- <Stmts>
3558 -- end;
3559 -- end loop;
3561 if Is_Procedure then
3562 Stmts :=
3563 New_List (
3564 Make_Loop_Statement (Loc,
3565 Statements => New_List (
3566 Make_Block_Statement (Loc,
3567 Declarations => Block_Decls,
3568 Handled_Statement_Sequence =>
3569 Make_Handled_Sequence_Of_Statements (Loc,
3570 Statements => Stmts))),
3571 End_Label => Empty));
3572 end if;
3574 Hand_Stmt_Seq :=
3575 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3576 end Protected_Component_Ref;
3577 end if;
3579 -- Make an unprotected version of the subprogram for use within the same
3580 -- object, with new name and extra parameter representing the object.
3582 return
3583 Make_Subprogram_Body (Loc,
3584 Specification =>
3585 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3586 Declarations => Decls,
3587 Handled_Statement_Sequence => Hand_Stmt_Seq);
3588 end Build_Lock_Free_Unprotected_Subprogram_Body;
3590 -------------------------
3591 -- Build_Master_Entity --
3592 -------------------------
3594 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3595 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3596 Context : Node_Id;
3597 Context_Id : Entity_Id;
3598 Decl : Node_Id;
3599 Decls : List_Id;
3600 Par : Node_Id;
3602 begin
3603 if Is_Itype (Obj_Or_Typ) then
3604 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3605 else
3606 Par := Parent (Obj_Or_Typ);
3607 end if;
3609 -- When creating a master for a record component which is either a task
3610 -- or access-to-task, the enclosing record is the master scope and the
3611 -- proper insertion point is the component list.
3613 if Is_Record_Type (Current_Scope) then
3614 Context := Par;
3615 Context_Id := Current_Scope;
3616 Decls := List_Containing (Context);
3618 -- Default case for object declarations and access types. Note that the
3619 -- context is updated to the nearest enclosing body, block, package or
3620 -- return statement.
3622 else
3623 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3624 end if;
3626 -- Do not create a master if one already exists or there is no task
3627 -- hierarchy.
3629 if Has_Master_Entity (Context_Id)
3630 or else Restriction_Active (No_Task_Hierarchy)
3631 then
3632 return;
3633 end if;
3635 -- Create a master, generate:
3636 -- _Master : constant Master_Id := Current_Master.all;
3638 Decl :=
3639 Make_Object_Declaration (Loc,
3640 Defining_Identifier =>
3641 Make_Defining_Identifier (Loc, Name_uMaster),
3642 Constant_Present => True,
3643 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3644 Expression =>
3645 Make_Explicit_Dereference (Loc,
3646 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3648 -- The master is inserted at the start of the declarative list of the
3649 -- context.
3651 Prepend_To (Decls, Decl);
3653 -- In certain cases where transient scopes are involved, the immediate
3654 -- scope is not always the proper master scope. Ensure that the master
3655 -- declaration and entity appear in the same context.
3657 if Context_Id /= Current_Scope then
3658 Push_Scope (Context_Id);
3659 Analyze (Decl);
3660 Pop_Scope;
3661 else
3662 Analyze (Decl);
3663 end if;
3665 -- Mark the enclosing scope and its associated construct as being task
3666 -- masters.
3668 Set_Has_Master_Entity (Context_Id);
3670 while Present (Context)
3671 and then Nkind (Context) /= N_Compilation_Unit
3672 loop
3673 if Nkind_In (Context, N_Block_Statement,
3674 N_Subprogram_Body,
3675 N_Task_Body)
3676 then
3677 Set_Is_Task_Master (Context);
3678 exit;
3680 elsif Nkind (Parent (Context)) = N_Subunit then
3681 Context := Corresponding_Stub (Parent (Context));
3682 end if;
3684 Context := Parent (Context);
3685 end loop;
3686 end Build_Master_Entity;
3688 ---------------------------
3689 -- Build_Master_Renaming --
3690 ---------------------------
3692 procedure Build_Master_Renaming
3693 (Ptr_Typ : Entity_Id;
3694 Ins_Nod : Node_Id := Empty)
3696 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3697 Context : Node_Id;
3698 Master_Decl : Node_Id;
3699 Master_Id : Entity_Id;
3701 begin
3702 -- Nothing to do if there is no task hierarchy
3704 if Restriction_Active (No_Task_Hierarchy) then
3705 return;
3706 end if;
3708 -- Determine the proper context to insert the master renaming
3710 if Present (Ins_Nod) then
3711 Context := Ins_Nod;
3712 elsif Is_Itype (Ptr_Typ) then
3713 Context := Associated_Node_For_Itype (Ptr_Typ);
3714 else
3715 Context := Parent (Ptr_Typ);
3716 end if;
3718 -- Generate:
3719 -- <Ptr_Typ>M : Master_Id renames _Master;
3721 Master_Id :=
3722 Make_Defining_Identifier (Loc,
3723 New_External_Name (Chars (Ptr_Typ), 'M'));
3725 Master_Decl :=
3726 Make_Object_Renaming_Declaration (Loc,
3727 Defining_Identifier => Master_Id,
3728 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3729 Name => Make_Identifier (Loc, Name_uMaster));
3731 Insert_Action (Context, Master_Decl);
3733 -- The renamed master now services the access type
3735 Set_Master_Id (Ptr_Typ, Master_Id);
3736 end Build_Master_Renaming;
3738 -----------------------------------------
3739 -- Build_Private_Protected_Declaration --
3740 -----------------------------------------
3742 function Build_Private_Protected_Declaration
3743 (N : Node_Id) return Entity_Id
3745 Loc : constant Source_Ptr := Sloc (N);
3746 Body_Id : constant Entity_Id := Defining_Entity (N);
3747 Decl : Node_Id;
3748 Plist : List_Id;
3749 Formal : Entity_Id;
3750 New_Spec : Node_Id;
3751 Spec_Id : Entity_Id;
3753 begin
3754 Formal := First_Formal (Body_Id);
3756 -- The protected operation always has at least one formal, namely the
3757 -- object itself, but it is only placed in the parameter list if
3758 -- expansion is enabled.
3760 if Present (Formal) or else Expander_Active then
3761 Plist := Copy_Parameter_List (Body_Id);
3762 else
3763 Plist := No_List;
3764 end if;
3766 if Nkind (Specification (N)) = N_Procedure_Specification then
3767 New_Spec :=
3768 Make_Procedure_Specification (Loc,
3769 Defining_Unit_Name =>
3770 Make_Defining_Identifier (Sloc (Body_Id),
3771 Chars => Chars (Body_Id)),
3772 Parameter_Specifications =>
3773 Plist);
3774 else
3775 New_Spec :=
3776 Make_Function_Specification (Loc,
3777 Defining_Unit_Name =>
3778 Make_Defining_Identifier (Sloc (Body_Id),
3779 Chars => Chars (Body_Id)),
3780 Parameter_Specifications => Plist,
3781 Result_Definition =>
3782 New_Occurrence_Of (Etype (Body_Id), Loc));
3783 end if;
3785 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3786 Insert_Before (N, Decl);
3787 Spec_Id := Defining_Unit_Name (New_Spec);
3789 -- Indicate that the entity comes from source, to ensure that cross-
3790 -- reference information is properly generated. The body itself is
3791 -- rewritten during expansion, and the body entity will not appear in
3792 -- calls to the operation.
3794 Set_Comes_From_Source (Spec_Id, True);
3795 Analyze (Decl);
3796 Set_Has_Completion (Spec_Id);
3797 Set_Convention (Spec_Id, Convention_Protected);
3798 return Spec_Id;
3799 end Build_Private_Protected_Declaration;
3801 ---------------------------
3802 -- Build_Protected_Entry --
3803 ---------------------------
3805 function Build_Protected_Entry
3806 (N : Node_Id;
3807 Ent : Entity_Id;
3808 Pid : Node_Id) return Node_Id
3810 Loc : constant Source_Ptr := Sloc (N);
3812 Decls : constant List_Id := Declarations (N);
3813 End_Lab : constant Node_Id :=
3814 End_Label (Handled_Statement_Sequence (N));
3815 End_Loc : constant Source_Ptr :=
3816 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3817 -- Used for the generated call to Complete_Entry_Body
3819 Han_Loc : Source_Ptr;
3820 -- Used for the exception handler, inserted at end of the body
3822 Op_Decls : constant List_Id := New_List;
3823 Complete : Node_Id;
3824 Edef : Entity_Id;
3825 Espec : Node_Id;
3826 Ohandle : Node_Id;
3827 Op_Stats : List_Id;
3829 begin
3830 -- Set the source location on the exception handler only when debugging
3831 -- the expanded code (see Make_Implicit_Exception_Handler).
3833 if Debug_Generated_Code then
3834 Han_Loc := End_Loc;
3836 -- Otherwise the inserted code should not be visible to the debugger
3838 else
3839 Han_Loc := No_Location;
3840 end if;
3842 Edef :=
3843 Make_Defining_Identifier (Loc,
3844 Chars => Chars (Protected_Body_Subprogram (Ent)));
3845 Espec :=
3846 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3848 -- Add the following declarations:
3850 -- type poVP is access poV;
3851 -- _object : poVP := poVP (_O);
3853 -- where _O is the formal parameter associated with the concurrent
3854 -- object. These declarations are needed for Complete_Entry_Body.
3856 Add_Object_Pointer (Loc, Pid, Op_Decls);
3858 -- Add renamings for all formals, the Protection object, discriminals,
3859 -- privals and the entry index constant for use by debugger.
3861 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3862 Debug_Private_Data_Declarations (Decls);
3864 -- Put the declarations and the statements from the entry
3866 Op_Stats :=
3867 New_List (
3868 Make_Block_Statement (Loc,
3869 Declarations => Decls,
3870 Handled_Statement_Sequence =>
3871 Handled_Statement_Sequence (N)));
3873 case Corresponding_Runtime_Package (Pid) is
3874 when System_Tasking_Protected_Objects_Entries =>
3875 Append_To (Op_Stats,
3876 Make_Procedure_Call_Statement (End_Loc,
3877 Name =>
3878 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3879 Parameter_Associations => New_List (
3880 Make_Attribute_Reference (End_Loc,
3881 Prefix =>
3882 Make_Selected_Component (End_Loc,
3883 Prefix =>
3884 Make_Identifier (End_Loc, Name_uObject),
3885 Selector_Name =>
3886 Make_Identifier (End_Loc, Name_uObject)),
3887 Attribute_Name => Name_Unchecked_Access))));
3889 when System_Tasking_Protected_Objects_Single_Entry =>
3891 -- Historically, a call to Complete_Single_Entry_Body was
3892 -- inserted, but it was a null procedure.
3894 null;
3896 when others =>
3897 raise Program_Error;
3898 end case;
3900 -- When exceptions can not be propagated, we never need to call
3901 -- Exception_Complete_Entry_Body
3903 if No_Exception_Handlers_Set then
3904 return
3905 Make_Subprogram_Body (Loc,
3906 Specification => Espec,
3907 Declarations => Op_Decls,
3908 Handled_Statement_Sequence =>
3909 Make_Handled_Sequence_Of_Statements (Loc,
3910 Statements => Op_Stats,
3911 End_Label => End_Lab));
3913 else
3914 Ohandle := Make_Others_Choice (Loc);
3915 Set_All_Others (Ohandle);
3917 case Corresponding_Runtime_Package (Pid) is
3918 when System_Tasking_Protected_Objects_Entries =>
3919 Complete :=
3920 New_Occurrence_Of
3921 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3923 when System_Tasking_Protected_Objects_Single_Entry =>
3924 Complete :=
3925 New_Occurrence_Of
3926 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3928 when others =>
3929 raise Program_Error;
3930 end case;
3932 -- Establish link between subprogram body entity and source entry
3934 Set_Corresponding_Protected_Entry (Edef, Ent);
3936 -- Create body of entry procedure. The renaming declarations are
3937 -- placed ahead of the block that contains the actual entry body.
3939 return
3940 Make_Subprogram_Body (Loc,
3941 Specification => Espec,
3942 Declarations => Op_Decls,
3943 Handled_Statement_Sequence =>
3944 Make_Handled_Sequence_Of_Statements (Loc,
3945 Statements => Op_Stats,
3946 End_Label => End_Lab,
3947 Exception_Handlers => New_List (
3948 Make_Implicit_Exception_Handler (Han_Loc,
3949 Exception_Choices => New_List (Ohandle),
3951 Statements => New_List (
3952 Make_Procedure_Call_Statement (Han_Loc,
3953 Name => Complete,
3954 Parameter_Associations => New_List (
3955 Make_Attribute_Reference (Han_Loc,
3956 Prefix =>
3957 Make_Selected_Component (Han_Loc,
3958 Prefix =>
3959 Make_Identifier (Han_Loc, Name_uObject),
3960 Selector_Name =>
3961 Make_Identifier (Han_Loc, Name_uObject)),
3962 Attribute_Name => Name_Unchecked_Access),
3964 Make_Function_Call (Han_Loc,
3965 Name => New_Occurrence_Of (
3966 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3967 end if;
3968 end Build_Protected_Entry;
3970 -----------------------------------------
3971 -- Build_Protected_Entry_Specification --
3972 -----------------------------------------
3974 function Build_Protected_Entry_Specification
3975 (Loc : Source_Ptr;
3976 Def_Id : Entity_Id;
3977 Ent_Id : Entity_Id) return Node_Id
3979 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3981 begin
3982 Set_Debug_Info_Needed (Def_Id);
3984 if Present (Ent_Id) then
3985 Append_Elmt (P, Accept_Address (Ent_Id));
3986 end if;
3988 return
3989 Make_Procedure_Specification (Loc,
3990 Defining_Unit_Name => Def_Id,
3991 Parameter_Specifications => New_List (
3992 Make_Parameter_Specification (Loc,
3993 Defining_Identifier =>
3994 Make_Defining_Identifier (Loc, Name_uO),
3995 Parameter_Type =>
3996 New_Occurrence_Of (RTE (RE_Address), Loc)),
3998 Make_Parameter_Specification (Loc,
3999 Defining_Identifier => P,
4000 Parameter_Type =>
4001 New_Occurrence_Of (RTE (RE_Address), Loc)),
4003 Make_Parameter_Specification (Loc,
4004 Defining_Identifier =>
4005 Make_Defining_Identifier (Loc, Name_uE),
4006 Parameter_Type =>
4007 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
4008 end Build_Protected_Entry_Specification;
4010 --------------------------
4011 -- Build_Protected_Spec --
4012 --------------------------
4014 function Build_Protected_Spec
4015 (N : Node_Id;
4016 Obj_Type : Entity_Id;
4017 Ident : Entity_Id;
4018 Unprotected : Boolean := False) return List_Id
4020 Loc : constant Source_Ptr := Sloc (N);
4021 Decl : Node_Id;
4022 Formal : Entity_Id;
4023 New_Plist : List_Id;
4024 New_Param : Node_Id;
4026 begin
4027 New_Plist := New_List;
4029 Formal := First_Formal (Ident);
4030 while Present (Formal) loop
4031 New_Param :=
4032 Make_Parameter_Specification (Loc,
4033 Defining_Identifier =>
4034 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4035 In_Present => In_Present (Parent (Formal)),
4036 Out_Present => Out_Present (Parent (Formal)),
4037 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
4039 if Unprotected then
4040 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4041 end if;
4043 Append (New_Param, New_Plist);
4044 Next_Formal (Formal);
4045 end loop;
4047 -- If the subprogram is a procedure and the context is not an access
4048 -- to protected subprogram, the parameter is in-out. Otherwise it is
4049 -- an in parameter.
4051 Decl :=
4052 Make_Parameter_Specification (Loc,
4053 Defining_Identifier =>
4054 Make_Defining_Identifier (Loc, Name_uObject),
4055 In_Present => True,
4056 Out_Present =>
4057 (Etype (Ident) = Standard_Void_Type
4058 and then not Is_RTE (Obj_Type, RE_Address)),
4059 Parameter_Type =>
4060 New_Occurrence_Of (Obj_Type, Loc));
4061 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4062 Prepend_To (New_Plist, Decl);
4064 return New_Plist;
4065 end Build_Protected_Spec;
4067 ---------------------------------------
4068 -- Build_Protected_Sub_Specification --
4069 ---------------------------------------
4071 function Build_Protected_Sub_Specification
4072 (N : Node_Id;
4073 Prot_Typ : Entity_Id;
4074 Mode : Subprogram_Protection_Mode) return Node_Id
4076 Loc : constant Source_Ptr := Sloc (N);
4077 Decl : Node_Id;
4078 Def_Id : Entity_Id;
4079 New_Id : Entity_Id;
4080 New_Plist : List_Id;
4081 New_Spec : Node_Id;
4083 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4084 (Dispatching_Mode => ' ',
4085 Protected_Mode => 'P',
4086 Unprotected_Mode => 'N');
4088 begin
4089 if Ekind (Defining_Unit_Name (Specification (N))) =
4090 E_Subprogram_Body
4091 then
4092 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4093 else
4094 Decl := N;
4095 end if;
4097 Def_Id := Defining_Unit_Name (Specification (Decl));
4099 New_Plist :=
4100 Build_Protected_Spec
4101 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4102 Mode = Unprotected_Mode);
4103 New_Id :=
4104 Make_Defining_Identifier (Loc,
4105 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4107 -- The unprotected operation carries the user code, and debugging
4108 -- information must be generated for it, even though this spec does
4109 -- not come from source. It is also convenient to allow gdb to step
4110 -- into the protected operation, even though it only contains lock/
4111 -- unlock calls.
4113 Set_Debug_Info_Needed (New_Id);
4115 -- If a pragma Eliminate applies to the source entity, the internal
4116 -- subprograms will be eliminated as well.
4118 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4120 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4121 New_Spec :=
4122 Make_Procedure_Specification (Loc,
4123 Defining_Unit_Name => New_Id,
4124 Parameter_Specifications => New_Plist);
4126 -- Create a new specification for the anonymous subprogram type
4128 else
4129 New_Spec :=
4130 Make_Function_Specification (Loc,
4131 Defining_Unit_Name => New_Id,
4132 Parameter_Specifications => New_Plist,
4133 Result_Definition =>
4134 Copy_Result_Type (Result_Definition (Specification (Decl))));
4136 Set_Return_Present (Defining_Unit_Name (New_Spec));
4137 end if;
4139 return New_Spec;
4140 end Build_Protected_Sub_Specification;
4142 -------------------------------------
4143 -- Build_Protected_Subprogram_Body --
4144 -------------------------------------
4146 function Build_Protected_Subprogram_Body
4147 (N : Node_Id;
4148 Pid : Node_Id;
4149 N_Op_Spec : Node_Id) return Node_Id
4151 Loc : constant Source_Ptr := Sloc (N);
4152 Op_Spec : Node_Id;
4153 P_Op_Spec : Node_Id;
4154 Uactuals : List_Id;
4155 Pformal : Node_Id;
4156 Unprot_Call : Node_Id;
4157 Sub_Body : Node_Id;
4158 Lock_Name : Node_Id;
4159 Lock_Stmt : Node_Id;
4160 R : Node_Id;
4161 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4162 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4163 Stmts : List_Id;
4164 Object_Parm : Node_Id;
4165 Exc_Safe : Boolean;
4166 Lock_Kind : RE_Id;
4168 begin
4169 Op_Spec := Specification (N);
4170 Exc_Safe := Is_Exception_Safe (N);
4172 P_Op_Spec :=
4173 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4175 -- Build a list of the formal parameters of the protected version of
4176 -- the subprogram to use as the actual parameters of the unprotected
4177 -- version.
4179 Uactuals := New_List;
4180 Pformal := First (Parameter_Specifications (P_Op_Spec));
4181 while Present (Pformal) loop
4182 Append_To (Uactuals,
4183 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4184 Next (Pformal);
4185 end loop;
4187 -- Make a call to the unprotected version of the subprogram built above
4188 -- for use by the protected version built below.
4190 if Nkind (Op_Spec) = N_Function_Specification then
4191 if Exc_Safe then
4192 R := Make_Temporary (Loc, 'R');
4193 Unprot_Call :=
4194 Make_Object_Declaration (Loc,
4195 Defining_Identifier => R,
4196 Constant_Present => True,
4197 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4198 Expression =>
4199 Make_Function_Call (Loc,
4200 Name => Make_Identifier (Loc,
4201 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4202 Parameter_Associations => Uactuals));
4204 Return_Stmt :=
4205 Make_Simple_Return_Statement (Loc,
4206 Expression => New_Occurrence_Of (R, Loc));
4208 else
4209 Unprot_Call := Make_Simple_Return_Statement (Loc,
4210 Expression => Make_Function_Call (Loc,
4211 Name =>
4212 Make_Identifier (Loc,
4213 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4214 Parameter_Associations => Uactuals));
4215 end if;
4217 Lock_Kind := RE_Lock_Read_Only;
4219 else
4220 Unprot_Call :=
4221 Make_Procedure_Call_Statement (Loc,
4222 Name =>
4223 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4224 Parameter_Associations => Uactuals);
4226 Lock_Kind := RE_Lock;
4227 end if;
4229 -- Wrap call in block that will be covered by an at_end handler
4231 if not Exc_Safe then
4232 Unprot_Call := Make_Block_Statement (Loc,
4233 Handled_Statement_Sequence =>
4234 Make_Handled_Sequence_Of_Statements (Loc,
4235 Statements => New_List (Unprot_Call)));
4236 end if;
4238 -- Make the protected subprogram body. This locks the protected
4239 -- object and calls the unprotected version of the subprogram.
4241 case Corresponding_Runtime_Package (Pid) is
4242 when System_Tasking_Protected_Objects_Entries =>
4243 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
4245 when System_Tasking_Protected_Objects_Single_Entry =>
4246 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4248 when System_Tasking_Protected_Objects =>
4249 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4251 when others =>
4252 raise Program_Error;
4253 end case;
4255 Object_Parm :=
4256 Make_Attribute_Reference (Loc,
4257 Prefix =>
4258 Make_Selected_Component (Loc,
4259 Prefix => Make_Identifier (Loc, Name_uObject),
4260 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4261 Attribute_Name => Name_Unchecked_Access);
4263 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4264 Name => Lock_Name,
4265 Parameter_Associations => New_List (Object_Parm));
4267 if Abort_Allowed then
4268 Stmts := New_List (
4269 Make_Procedure_Call_Statement (Loc,
4270 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
4271 Parameter_Associations => Empty_List),
4272 Lock_Stmt);
4274 else
4275 Stmts := New_List (Lock_Stmt);
4276 end if;
4278 if not Exc_Safe then
4279 Append (Unprot_Call, Stmts);
4280 else
4281 if Nkind (Op_Spec) = N_Function_Specification then
4282 Pre_Stmts := Stmts;
4283 Stmts := Empty_List;
4284 else
4285 Append (Unprot_Call, Stmts);
4286 end if;
4288 -- Historical note: Previously, call the the cleanup was inserted
4289 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4290 -- which is also shared by the 'not Exc_Safe' path.
4292 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4294 if Nkind (Op_Spec) = N_Function_Specification then
4295 Append (Return_Stmt, Stmts);
4296 Append (Make_Block_Statement (Loc,
4297 Declarations => New_List (Unprot_Call),
4298 Handled_Statement_Sequence =>
4299 Make_Handled_Sequence_Of_Statements (Loc,
4300 Statements => Stmts)), Pre_Stmts);
4301 Stmts := Pre_Stmts;
4302 end if;
4303 end if;
4305 Sub_Body :=
4306 Make_Subprogram_Body (Loc,
4307 Declarations => Empty_List,
4308 Specification => P_Op_Spec,
4309 Handled_Statement_Sequence =>
4310 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4312 -- Mark this subprogram as a protected subprogram body so that the
4313 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4314 -- path as otherwise the cleanup has already been inserted.
4316 if not Exc_Safe then
4317 Set_Is_Protected_Subprogram_Body (Sub_Body);
4318 end if;
4320 return Sub_Body;
4321 end Build_Protected_Subprogram_Body;
4323 -------------------------------------
4324 -- Build_Protected_Subprogram_Call --
4325 -------------------------------------
4327 procedure Build_Protected_Subprogram_Call
4328 (N : Node_Id;
4329 Name : Node_Id;
4330 Rec : Node_Id;
4331 External : Boolean := True)
4333 Loc : constant Source_Ptr := Sloc (N);
4334 Sub : constant Entity_Id := Entity (Name);
4335 New_Sub : Node_Id;
4336 Params : List_Id;
4338 begin
4339 if External then
4340 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4341 else
4342 New_Sub :=
4343 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4344 end if;
4346 if Present (Parameter_Associations (N)) then
4347 Params := New_Copy_List_Tree (Parameter_Associations (N));
4348 else
4349 Params := New_List;
4350 end if;
4352 -- If the type is an untagged derived type, convert to the root type,
4353 -- which is the one on which the operations are defined.
4355 if Nkind (Rec) = N_Unchecked_Type_Conversion
4356 and then not Is_Tagged_Type (Etype (Rec))
4357 and then Is_Derived_Type (Etype (Rec))
4358 then
4359 Set_Etype (Rec, Root_Type (Etype (Rec)));
4360 Set_Subtype_Mark (Rec,
4361 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4362 end if;
4364 Prepend (Rec, Params);
4366 if Ekind (Sub) = E_Procedure then
4367 Rewrite (N,
4368 Make_Procedure_Call_Statement (Loc,
4369 Name => New_Sub,
4370 Parameter_Associations => Params));
4372 else
4373 pragma Assert (Ekind (Sub) = E_Function);
4374 Rewrite (N,
4375 Make_Function_Call (Loc,
4376 Name => New_Sub,
4377 Parameter_Associations => Params));
4379 -- Preserve type of call for subsequent processing (required for
4380 -- call to Wrap_Transient_Expression in the case of a shared passive
4381 -- protected).
4383 Set_Etype (N, Etype (New_Sub));
4384 end if;
4386 if External
4387 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4388 and then Is_Entity_Name (Expression (Rec))
4389 and then Is_Shared_Passive (Entity (Expression (Rec)))
4390 then
4391 Add_Shared_Var_Lock_Procs (N);
4392 end if;
4393 end Build_Protected_Subprogram_Call;
4395 ---------------------------------------------
4396 -- Build_Protected_Subprogram_Call_Cleanup --
4397 ---------------------------------------------
4399 procedure Build_Protected_Subprogram_Call_Cleanup
4400 (Op_Spec : Node_Id;
4401 Conc_Typ : Node_Id;
4402 Loc : Source_Ptr;
4403 Stmts : List_Id)
4405 Nam : Node_Id;
4407 begin
4408 -- If the associated protected object has entries, a protected
4409 -- procedure has to service entry queues. In this case generate:
4411 -- Service_Entries (_object._object'Access);
4413 if Nkind (Op_Spec) = N_Procedure_Specification
4414 and then Has_Entries (Conc_Typ)
4415 then
4416 case Corresponding_Runtime_Package (Conc_Typ) is
4417 when System_Tasking_Protected_Objects_Entries =>
4418 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4420 when System_Tasking_Protected_Objects_Single_Entry =>
4421 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4423 when others =>
4424 raise Program_Error;
4425 end case;
4427 Append_To (Stmts,
4428 Make_Procedure_Call_Statement (Loc,
4429 Name => Nam,
4430 Parameter_Associations => New_List (
4431 Make_Attribute_Reference (Loc,
4432 Prefix =>
4433 Make_Selected_Component (Loc,
4434 Prefix => Make_Identifier (Loc, Name_uObject),
4435 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4436 Attribute_Name => Name_Unchecked_Access))));
4438 else
4439 -- Generate:
4440 -- Unlock (_object._object'Access);
4442 case Corresponding_Runtime_Package (Conc_Typ) is
4443 when System_Tasking_Protected_Objects_Entries =>
4444 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4446 when System_Tasking_Protected_Objects_Single_Entry =>
4447 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4449 when System_Tasking_Protected_Objects =>
4450 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4452 when others =>
4453 raise Program_Error;
4454 end case;
4456 Append_To (Stmts,
4457 Make_Procedure_Call_Statement (Loc,
4458 Name => Nam,
4459 Parameter_Associations => New_List (
4460 Make_Attribute_Reference (Loc,
4461 Prefix =>
4462 Make_Selected_Component (Loc,
4463 Prefix => Make_Identifier (Loc, Name_uObject),
4464 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4465 Attribute_Name => Name_Unchecked_Access))));
4466 end if;
4468 -- Generate:
4469 -- Abort_Undefer;
4471 if Abort_Allowed then
4472 Append_To (Stmts,
4473 Make_Procedure_Call_Statement (Loc,
4474 Name =>
4475 New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
4476 Parameter_Associations => Empty_List));
4477 end if;
4478 end Build_Protected_Subprogram_Call_Cleanup;
4480 -------------------------
4481 -- Build_Selected_Name --
4482 -------------------------
4484 function Build_Selected_Name
4485 (Prefix : Entity_Id;
4486 Selector : Entity_Id;
4487 Append_Char : Character := ' ') return Name_Id
4489 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4490 Select_Len : Natural;
4492 begin
4493 Get_Name_String (Chars (Selector));
4494 Select_Len := Name_Len;
4495 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4496 Get_Name_String (Chars (Prefix));
4498 -- If scope is anonymous type, discard suffix to recover name of
4499 -- single protected object. Otherwise use protected type name.
4501 if Name_Buffer (Name_Len) = 'T' then
4502 Name_Len := Name_Len - 1;
4503 end if;
4505 Add_Str_To_Name_Buffer ("__");
4506 for J in 1 .. Select_Len loop
4507 Add_Char_To_Name_Buffer (Select_Buffer (J));
4508 end loop;
4510 -- Now add the Append_Char if specified. The encoding to follow
4511 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4512 -- then the entity is associated to a protected type subprogram.
4513 -- Otherwise, it is a protected type entry. For each case, the
4514 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4516 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4518 if Append_Char /= ' ' then
4519 if Append_Char = 'P' or Append_Char = 'N' then
4520 Add_Char_To_Name_Buffer (Append_Char);
4521 return Name_Find;
4522 else
4523 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4524 return New_External_Name (Name_Find, ' ', -1);
4525 end if;
4526 else
4527 return Name_Find;
4528 end if;
4529 end Build_Selected_Name;
4531 -----------------------------
4532 -- Build_Simple_Entry_Call --
4533 -----------------------------
4535 -- A task entry call is converted to a call to Call_Simple
4537 -- declare
4538 -- P : parms := (parm, parm, parm);
4539 -- begin
4540 -- Call_Simple (acceptor-task, entry-index, P'Address);
4541 -- parm := P.param;
4542 -- parm := P.param;
4543 -- ...
4544 -- end;
4546 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4547 -- the parameters, and the constructed aggregate value contains either the
4548 -- parameters or, in the case of non-elementary types, references to these
4549 -- parameters. Then the address of this aggregate is passed to the runtime
4550 -- routine, along with the task id value and the task entry index value.
4551 -- Pnn is only required if parameters are present.
4553 -- The assignments after the call are present only in the case of in-out
4554 -- or out parameters for elementary types, and are used to assign back the
4555 -- resulting values of such parameters.
4557 -- Note: the reason that we insert a block here is that in the context
4558 -- of selects, conditional entry calls etc. the entry call statement
4559 -- appears on its own, not as an element of a list.
4561 -- A protected entry call is converted to a Protected_Entry_Call:
4563 -- declare
4564 -- P : E1_Params := (param, param, param);
4565 -- Pnn : Boolean;
4566 -- Bnn : Communications_Block;
4568 -- declare
4569 -- P : E1_Params := (param, param, param);
4570 -- Bnn : Communications_Block;
4572 -- begin
4573 -- Protected_Entry_Call (
4574 -- Object => po._object'Access,
4575 -- E => <entry index>;
4576 -- Uninterpreted_Data => P'Address;
4577 -- Mode => Simple_Call;
4578 -- Block => Bnn);
4579 -- parm := P.param;
4580 -- parm := P.param;
4581 -- ...
4582 -- end;
4584 procedure Build_Simple_Entry_Call
4585 (N : Node_Id;
4586 Concval : Node_Id;
4587 Ename : Node_Id;
4588 Index : Node_Id)
4590 begin
4591 Expand_Call (N);
4593 -- If call has been inlined, nothing left to do
4595 if Nkind (N) = N_Block_Statement then
4596 return;
4597 end if;
4599 -- Convert entry call to Call_Simple call
4601 declare
4602 Loc : constant Source_Ptr := Sloc (N);
4603 Parms : constant List_Id := Parameter_Associations (N);
4604 Stats : constant List_Id := New_List;
4605 Actual : Node_Id;
4606 Call : Node_Id;
4607 Comm_Name : Entity_Id;
4608 Conctyp : Node_Id;
4609 Decls : List_Id;
4610 Ent : Entity_Id;
4611 Ent_Acc : Entity_Id;
4612 Formal : Node_Id;
4613 Iface_Tag : Entity_Id;
4614 Iface_Typ : Entity_Id;
4615 N_Node : Node_Id;
4616 N_Var : Node_Id;
4617 P : Entity_Id;
4618 Parm1 : Node_Id;
4619 Parm2 : Node_Id;
4620 Parm3 : Node_Id;
4621 Pdecl : Node_Id;
4622 Plist : List_Id;
4623 X : Entity_Id;
4624 Xdecl : Node_Id;
4626 begin
4627 -- Simple entry and entry family cases merge here
4629 Ent := Entity (Ename);
4630 Ent_Acc := Entry_Parameters_Type (Ent);
4631 Conctyp := Etype (Concval);
4633 -- If prefix is an access type, dereference to obtain the task type
4635 if Is_Access_Type (Conctyp) then
4636 Conctyp := Designated_Type (Conctyp);
4637 end if;
4639 -- Special case for protected subprogram calls
4641 if Is_Protected_Type (Conctyp)
4642 and then Is_Subprogram (Entity (Ename))
4643 then
4644 if not Is_Eliminated (Entity (Ename)) then
4645 Build_Protected_Subprogram_Call
4646 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4647 Analyze (N);
4648 end if;
4650 return;
4651 end if;
4653 -- First parameter is the Task_Id value from the task value or the
4654 -- Object from the protected object value, obtained by selecting
4655 -- the _Task_Id or _Object from the result of doing an unchecked
4656 -- conversion to convert the value to the corresponding record type.
4658 if Nkind (Concval) = N_Function_Call
4659 and then Is_Task_Type (Conctyp)
4660 and then Ada_Version >= Ada_2005
4661 then
4662 declare
4663 ExpR : constant Node_Id := Relocate_Node (Concval);
4664 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4665 Decl : Node_Id;
4667 begin
4668 Decl :=
4669 Make_Object_Declaration (Loc,
4670 Defining_Identifier => Obj,
4671 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4672 Expression => ExpR);
4673 Set_Etype (Obj, Conctyp);
4674 Decls := New_List (Decl);
4675 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4676 end;
4678 else
4679 Decls := New_List;
4680 end if;
4682 Parm1 := Concurrent_Ref (Concval);
4684 -- Second parameter is the entry index, computed by the routine
4685 -- provided for this purpose. The value of this expression is
4686 -- assigned to an intermediate variable to assure that any entry
4687 -- family index expressions are evaluated before the entry
4688 -- parameters.
4690 if not Is_Protected_Type (Conctyp)
4691 or else
4692 Corresponding_Runtime_Package (Conctyp) =
4693 System_Tasking_Protected_Objects_Entries
4694 then
4695 X := Make_Defining_Identifier (Loc, Name_uX);
4697 Xdecl :=
4698 Make_Object_Declaration (Loc,
4699 Defining_Identifier => X,
4700 Object_Definition =>
4701 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4702 Expression => Actual_Index_Expression (
4703 Loc, Entity (Ename), Index, Concval));
4705 Append_To (Decls, Xdecl);
4706 Parm2 := New_Occurrence_Of (X, Loc);
4708 else
4709 Xdecl := Empty;
4710 Parm2 := Empty;
4711 end if;
4713 -- The third parameter is the packaged parameters. If there are
4714 -- none, then it is just the null address, since nothing is passed.
4716 if No (Parms) then
4717 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4718 P := Empty;
4720 -- Case of parameters present, where third argument is the address
4721 -- of a packaged record containing the required parameter values.
4723 else
4724 -- First build a list of parameter values, which are references to
4725 -- objects of the parameter types.
4727 Plist := New_List;
4729 Actual := First_Actual (N);
4730 Formal := First_Formal (Ent);
4731 while Present (Actual) loop
4733 -- If it is a by_copy_type, copy it to a new variable. The
4734 -- packaged record has a field that points to this variable.
4736 if Is_By_Copy_Type (Etype (Actual)) then
4737 N_Node :=
4738 Make_Object_Declaration (Loc,
4739 Defining_Identifier => Make_Temporary (Loc, 'J'),
4740 Aliased_Present => True,
4741 Object_Definition =>
4742 New_Occurrence_Of (Etype (Formal), Loc));
4744 -- Mark the object as not needing initialization since the
4745 -- initialization is performed separately, avoiding errors
4746 -- on cases such as formals of null-excluding access types.
4748 Set_No_Initialization (N_Node);
4750 -- We must make an assignment statement separate for the
4751 -- case of limited type. We cannot assign it unless the
4752 -- Assignment_OK flag is set first. An out formal of an
4753 -- access type must also be initialized from the actual,
4754 -- as stated in RM 6.4.1 (13), but no constraint is applied
4755 -- before the call.
4757 if Ekind (Formal) /= E_Out_Parameter
4758 or else Is_Access_Type (Etype (Formal))
4759 then
4760 N_Var :=
4761 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4762 Set_Assignment_OK (N_Var);
4763 Append_To (Stats,
4764 Make_Assignment_Statement (Loc,
4765 Name => N_Var,
4766 Expression => Relocate_Node (Actual)));
4768 -- If actual is an out parameter of a null-excluding
4769 -- access type, there is access check on entry, so set
4770 -- Suppress_Assignment_Checks on the generated statement
4771 -- that assigns the actual to the parameter block
4773 Set_Suppress_Assignment_Checks (Last (Stats));
4774 end if;
4776 Append (N_Node, Decls);
4778 Append_To (Plist,
4779 Make_Attribute_Reference (Loc,
4780 Attribute_Name => Name_Unchecked_Access,
4781 Prefix =>
4782 New_Occurrence_Of (Defining_Identifier (N_Node), Loc)));
4784 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4786 elsif Is_VM_By_Copy_Actual (Actual) then
4787 N_Node :=
4788 Make_Object_Declaration (Loc,
4789 Defining_Identifier => Make_Temporary (Loc, 'J'),
4790 Aliased_Present => True,
4791 Object_Definition =>
4792 New_Occurrence_Of (Etype (Formal), Loc),
4793 Expression => New_Copy_Tree (Actual));
4794 Set_Assignment_OK (N_Node);
4796 Append (N_Node, Decls);
4798 Append_To (Plist,
4799 Make_Attribute_Reference (Loc,
4800 Attribute_Name => Name_Unchecked_Access,
4801 Prefix =>
4802 New_Occurrence_Of (Defining_Identifier (N_Node), Loc)));
4804 else
4805 -- Interface class-wide formal
4807 if Ada_Version >= Ada_2005
4808 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4809 and then Is_Interface (Etype (Formal))
4810 then
4811 Iface_Typ := Etype (Etype (Formal));
4813 -- Generate:
4814 -- formal_iface_type! (actual.iface_tag)'reference
4816 Iface_Tag :=
4817 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4818 pragma Assert (Present (Iface_Tag));
4820 Append_To (Plist,
4821 Make_Reference (Loc,
4822 Unchecked_Convert_To (Iface_Typ,
4823 Make_Selected_Component (Loc,
4824 Prefix =>
4825 Relocate_Node (Actual),
4826 Selector_Name =>
4827 New_Occurrence_Of (Iface_Tag, Loc)))));
4828 else
4829 -- Generate:
4830 -- actual'reference
4832 Append_To (Plist,
4833 Make_Reference (Loc, Relocate_Node (Actual)));
4834 end if;
4835 end if;
4837 Next_Actual (Actual);
4838 Next_Formal_With_Extras (Formal);
4839 end loop;
4841 -- Now build the declaration of parameters initialized with the
4842 -- aggregate containing this constructed parameter list.
4844 P := Make_Defining_Identifier (Loc, Name_uP);
4846 Pdecl :=
4847 Make_Object_Declaration (Loc,
4848 Defining_Identifier => P,
4849 Object_Definition =>
4850 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4851 Expression =>
4852 Make_Aggregate (Loc, Expressions => Plist));
4854 Parm3 :=
4855 Make_Attribute_Reference (Loc,
4856 Prefix => New_Occurrence_Of (P, Loc),
4857 Attribute_Name => Name_Address);
4859 Append (Pdecl, Decls);
4860 end if;
4862 -- Now we can create the call, case of protected type
4864 if Is_Protected_Type (Conctyp) then
4865 case Corresponding_Runtime_Package (Conctyp) is
4866 when System_Tasking_Protected_Objects_Entries =>
4868 -- Change the type of the index declaration
4870 Set_Object_Definition (Xdecl,
4871 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4873 -- Some additional declarations for protected entry calls
4875 if No (Decls) then
4876 Decls := New_List;
4877 end if;
4879 -- Bnn : Communications_Block;
4881 Comm_Name := Make_Temporary (Loc, 'B');
4883 Append_To (Decls,
4884 Make_Object_Declaration (Loc,
4885 Defining_Identifier => Comm_Name,
4886 Object_Definition =>
4887 New_Occurrence_Of
4888 (RTE (RE_Communication_Block), Loc)));
4890 -- Some additional statements for protected entry calls
4892 -- Protected_Entry_Call (
4893 -- Object => po._object'Access,
4894 -- E => <entry index>;
4895 -- Uninterpreted_Data => P'Address;
4896 -- Mode => Simple_Call;
4897 -- Block => Bnn);
4899 Call :=
4900 Make_Procedure_Call_Statement (Loc,
4901 Name =>
4902 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4904 Parameter_Associations => New_List (
4905 Make_Attribute_Reference (Loc,
4906 Attribute_Name => Name_Unchecked_Access,
4907 Prefix => Parm1),
4908 Parm2,
4909 Parm3,
4910 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4911 New_Occurrence_Of (Comm_Name, Loc)));
4913 when System_Tasking_Protected_Objects_Single_Entry =>
4914 -- Protected_Single_Entry_Call (
4915 -- Object => po._object'Access,
4916 -- Uninterpreted_Data => P'Address);
4918 Call :=
4919 Make_Procedure_Call_Statement (Loc,
4920 Name => New_Occurrence_Of (
4921 RTE (RE_Protected_Single_Entry_Call), Loc),
4923 Parameter_Associations => New_List (
4924 Make_Attribute_Reference (Loc,
4925 Attribute_Name => Name_Unchecked_Access,
4926 Prefix => Parm1),
4927 Parm3));
4929 when others =>
4930 raise Program_Error;
4931 end case;
4933 -- Case of task type
4935 else
4936 Call :=
4937 Make_Procedure_Call_Statement (Loc,
4938 Name => New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4939 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4941 end if;
4943 Append_To (Stats, Call);
4945 -- If there are out or in/out parameters by copy add assignment
4946 -- statements for the result values.
4948 if Present (Parms) then
4949 Actual := First_Actual (N);
4950 Formal := First_Formal (Ent);
4952 Set_Assignment_OK (Actual);
4953 while Present (Actual) loop
4954 if (Is_By_Copy_Type (Etype (Actual))
4955 or else Is_VM_By_Copy_Actual (Actual))
4956 and then Ekind (Formal) /= E_In_Parameter
4957 then
4958 N_Node :=
4959 Make_Assignment_Statement (Loc,
4960 Name => New_Copy (Actual),
4961 Expression =>
4962 Make_Explicit_Dereference (Loc,
4963 Make_Selected_Component (Loc,
4964 Prefix => New_Occurrence_Of (P, Loc),
4965 Selector_Name =>
4966 Make_Identifier (Loc, Chars (Formal)))));
4968 -- In all cases (including limited private types) we want
4969 -- the assignment to be valid.
4971 Set_Assignment_OK (Name (N_Node));
4973 -- If the call is the triggering alternative in an
4974 -- asynchronous select, or the entry_call alternative of a
4975 -- conditional entry call, the assignments for in-out
4976 -- parameters are incorporated into the statement list that
4977 -- follows, so that there are executed only if the entry
4978 -- call succeeds.
4980 if (Nkind (Parent (N)) = N_Triggering_Alternative
4981 and then N = Triggering_Statement (Parent (N)))
4982 or else
4983 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4984 and then N = Entry_Call_Statement (Parent (N)))
4985 then
4986 if No (Statements (Parent (N))) then
4987 Set_Statements (Parent (N), New_List);
4988 end if;
4990 Prepend (N_Node, Statements (Parent (N)));
4992 else
4993 Insert_After (Call, N_Node);
4994 end if;
4995 end if;
4997 Next_Actual (Actual);
4998 Next_Formal_With_Extras (Formal);
4999 end loop;
5000 end if;
5002 -- Finally, create block and analyze it
5004 Rewrite (N,
5005 Make_Block_Statement (Loc,
5006 Declarations => Decls,
5007 Handled_Statement_Sequence =>
5008 Make_Handled_Sequence_Of_Statements (Loc,
5009 Statements => Stats)));
5011 Analyze (N);
5012 end;
5013 end Build_Simple_Entry_Call;
5015 --------------------------------
5016 -- Build_Task_Activation_Call --
5017 --------------------------------
5019 procedure Build_Task_Activation_Call (N : Node_Id) is
5020 Loc : constant Source_Ptr := Sloc (N);
5021 Chain : Entity_Id;
5022 Call : Node_Id;
5023 Name : Node_Id;
5024 P : Node_Id;
5026 begin
5027 -- For sequential elaboration policy, all the tasks will be activated at
5028 -- the end of the elaboration.
5030 if Partition_Elaboration_Policy = 'S' then
5031 return;
5032 end if;
5034 -- Get the activation chain entity. Except in the case of a package
5035 -- body, this is in the node that was passed. For a package body, we
5036 -- have to find the corresponding package declaration node.
5038 if Nkind (N) = N_Package_Body then
5039 P := Corresponding_Spec (N);
5040 loop
5041 P := Parent (P);
5042 exit when Nkind (P) = N_Package_Declaration;
5043 end loop;
5045 Chain := Activation_Chain_Entity (P);
5047 else
5048 Chain := Activation_Chain_Entity (N);
5049 end if;
5051 if Present (Chain) then
5052 if Restricted_Profile then
5053 Name := New_Occurrence_Of
5054 (RTE (RE_Activate_Restricted_Tasks), Loc);
5055 else
5056 Name := New_Occurrence_Of
5057 (RTE (RE_Activate_Tasks), Loc);
5058 end if;
5060 Call :=
5061 Make_Procedure_Call_Statement (Loc,
5062 Name => Name,
5063 Parameter_Associations =>
5064 New_List (Make_Attribute_Reference (Loc,
5065 Prefix => New_Occurrence_Of (Chain, Loc),
5066 Attribute_Name => Name_Unchecked_Access)));
5068 if Nkind (N) = N_Package_Declaration then
5069 if Present (Corresponding_Body (N)) then
5070 null;
5072 elsif Present (Private_Declarations (Specification (N))) then
5073 Append (Call, Private_Declarations (Specification (N)));
5075 else
5076 Append (Call, Visible_Declarations (Specification (N)));
5077 end if;
5079 else
5080 if Present (Handled_Statement_Sequence (N)) then
5082 -- The call goes at the start of the statement sequence after
5083 -- the start of exception range label if one is present.
5085 declare
5086 Stm : Node_Id;
5088 begin
5089 Stm := First (Statements (Handled_Statement_Sequence (N)));
5091 -- A special case, skip exception range label if one is
5092 -- present (from front end zcx processing).
5094 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
5095 Next (Stm);
5096 end if;
5098 -- Another special case, if the first statement is a block
5099 -- from optimization of a local raise to a goto, then the
5100 -- call goes inside this block.
5102 if Nkind (Stm) = N_Block_Statement
5103 and then Exception_Junk (Stm)
5104 then
5105 Stm :=
5106 First (Statements (Handled_Statement_Sequence (Stm)));
5107 end if;
5109 -- Insertion point is after any exception label pushes,
5110 -- since we want it covered by any local handlers.
5112 while Nkind (Stm) in N_Push_xxx_Label loop
5113 Next (Stm);
5114 end loop;
5116 -- Now we have the proper insertion point
5118 Insert_Before (Stm, Call);
5119 end;
5121 else
5122 Set_Handled_Statement_Sequence (N,
5123 Make_Handled_Sequence_Of_Statements (Loc,
5124 Statements => New_List (Call)));
5125 end if;
5126 end if;
5128 Analyze (Call);
5129 Check_Task_Activation (N);
5130 end if;
5131 end Build_Task_Activation_Call;
5133 -------------------------------
5134 -- Build_Task_Allocate_Block --
5135 -------------------------------
5137 procedure Build_Task_Allocate_Block
5138 (Actions : List_Id;
5139 N : Node_Id;
5140 Args : List_Id)
5142 T : constant Entity_Id := Entity (Expression (N));
5143 Init : constant Entity_Id := Base_Init_Proc (T);
5144 Loc : constant Source_Ptr := Sloc (N);
5145 Chain : constant Entity_Id :=
5146 Make_Defining_Identifier (Loc, Name_uChain);
5147 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5148 Block : Node_Id;
5150 begin
5151 Block :=
5152 Make_Block_Statement (Loc,
5153 Identifier => New_Occurrence_Of (Blkent, Loc),
5154 Declarations => New_List (
5156 -- _Chain : Activation_Chain;
5158 Make_Object_Declaration (Loc,
5159 Defining_Identifier => Chain,
5160 Aliased_Present => True,
5161 Object_Definition =>
5162 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5164 Handled_Statement_Sequence =>
5165 Make_Handled_Sequence_Of_Statements (Loc,
5167 Statements => New_List (
5169 -- Init (Args);
5171 Make_Procedure_Call_Statement (Loc,
5172 Name => New_Occurrence_Of (Init, Loc),
5173 Parameter_Associations => Args),
5175 -- Activate_Tasks (_Chain);
5177 Make_Procedure_Call_Statement (Loc,
5178 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5179 Parameter_Associations => New_List (
5180 Make_Attribute_Reference (Loc,
5181 Prefix => New_Occurrence_Of (Chain, Loc),
5182 Attribute_Name => Name_Unchecked_Access))))),
5184 Has_Created_Identifier => True,
5185 Is_Task_Allocation_Block => True);
5187 Append_To (Actions,
5188 Make_Implicit_Label_Declaration (Loc,
5189 Defining_Identifier => Blkent,
5190 Label_Construct => Block));
5192 Append_To (Actions, Block);
5194 Set_Activation_Chain_Entity (Block, Chain);
5195 end Build_Task_Allocate_Block;
5197 -----------------------------------------------
5198 -- Build_Task_Allocate_Block_With_Init_Stmts --
5199 -----------------------------------------------
5201 procedure Build_Task_Allocate_Block_With_Init_Stmts
5202 (Actions : List_Id;
5203 N : Node_Id;
5204 Init_Stmts : List_Id)
5206 Loc : constant Source_Ptr := Sloc (N);
5207 Chain : constant Entity_Id :=
5208 Make_Defining_Identifier (Loc, Name_uChain);
5209 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5210 Block : Node_Id;
5212 begin
5213 Append_To (Init_Stmts,
5214 Make_Procedure_Call_Statement (Loc,
5215 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5216 Parameter_Associations => New_List (
5217 Make_Attribute_Reference (Loc,
5218 Prefix => New_Occurrence_Of (Chain, Loc),
5219 Attribute_Name => Name_Unchecked_Access))));
5221 Block :=
5222 Make_Block_Statement (Loc,
5223 Identifier => New_Occurrence_Of (Blkent, Loc),
5224 Declarations => New_List (
5226 -- _Chain : Activation_Chain;
5228 Make_Object_Declaration (Loc,
5229 Defining_Identifier => Chain,
5230 Aliased_Present => True,
5231 Object_Definition =>
5232 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5234 Handled_Statement_Sequence =>
5235 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5237 Has_Created_Identifier => True,
5238 Is_Task_Allocation_Block => True);
5240 Append_To (Actions,
5241 Make_Implicit_Label_Declaration (Loc,
5242 Defining_Identifier => Blkent,
5243 Label_Construct => Block));
5245 Append_To (Actions, Block);
5247 Set_Activation_Chain_Entity (Block, Chain);
5248 end Build_Task_Allocate_Block_With_Init_Stmts;
5250 -----------------------------------
5251 -- Build_Task_Proc_Specification --
5252 -----------------------------------
5254 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5255 Loc : constant Source_Ptr := Sloc (T);
5256 Spec_Id : Entity_Id;
5258 begin
5259 -- Case of explicit task type, suffix TB
5261 if Comes_From_Source (T) then
5262 Spec_Id :=
5263 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
5265 -- Case of anonymous task type, suffix B
5267 else
5268 Spec_Id :=
5269 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5270 end if;
5272 Set_Is_Internal (Spec_Id);
5274 -- Associate the procedure with the task, if this is the declaration
5275 -- (and not the body) of the procedure.
5277 if No (Task_Body_Procedure (T)) then
5278 Set_Task_Body_Procedure (T, Spec_Id);
5279 end if;
5281 return
5282 Make_Procedure_Specification (Loc,
5283 Defining_Unit_Name => Spec_Id,
5284 Parameter_Specifications => New_List (
5285 Make_Parameter_Specification (Loc,
5286 Defining_Identifier =>
5287 Make_Defining_Identifier (Loc, Name_uTask),
5288 Parameter_Type =>
5289 Make_Access_Definition (Loc,
5290 Subtype_Mark =>
5291 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5292 end Build_Task_Proc_Specification;
5294 ---------------------------------------
5295 -- Build_Unprotected_Subprogram_Body --
5296 ---------------------------------------
5298 function Build_Unprotected_Subprogram_Body
5299 (N : Node_Id;
5300 Pid : Node_Id) return Node_Id
5302 Decls : constant List_Id := Declarations (N);
5304 begin
5305 -- Add renamings for the Protection object, discriminals, privals and
5306 -- the entry index constant for use by debugger.
5308 Debug_Private_Data_Declarations (Decls);
5310 -- Make an unprotected version of the subprogram for use within the same
5311 -- object, with a new name and an additional parameter representing the
5312 -- object.
5314 return
5315 Make_Subprogram_Body (Sloc (N),
5316 Specification =>
5317 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5318 Declarations => Decls,
5319 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5320 end Build_Unprotected_Subprogram_Body;
5322 ----------------------------
5323 -- Collect_Entry_Families --
5324 ----------------------------
5326 procedure Collect_Entry_Families
5327 (Loc : Source_Ptr;
5328 Cdecls : List_Id;
5329 Current_Node : in out Node_Id;
5330 Conctyp : Entity_Id)
5332 Efam : Entity_Id;
5333 Efam_Decl : Node_Id;
5334 Efam_Type : Entity_Id;
5336 begin
5337 Efam := First_Entity (Conctyp);
5338 while Present (Efam) loop
5339 if Ekind (Efam) = E_Entry_Family then
5340 Efam_Type := Make_Temporary (Loc, 'F');
5342 declare
5343 Bas : Entity_Id :=
5344 Base_Type
5345 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5347 Bas_Decl : Node_Id := Empty;
5348 Lo, Hi : Node_Id;
5350 begin
5351 Get_Index_Bounds
5352 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5354 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5355 Bas := Make_Temporary (Loc, 'B');
5357 Bas_Decl :=
5358 Make_Subtype_Declaration (Loc,
5359 Defining_Identifier => Bas,
5360 Subtype_Indication =>
5361 Make_Subtype_Indication (Loc,
5362 Subtype_Mark =>
5363 New_Occurrence_Of (Standard_Integer, Loc),
5364 Constraint =>
5365 Make_Range_Constraint (Loc,
5366 Range_Expression => Make_Range (Loc,
5367 Make_Integer_Literal
5368 (Loc, -Entry_Family_Bound),
5369 Make_Integer_Literal
5370 (Loc, Entry_Family_Bound - 1)))));
5372 Insert_After (Current_Node, Bas_Decl);
5373 Current_Node := Bas_Decl;
5374 Analyze (Bas_Decl);
5375 end if;
5377 Efam_Decl :=
5378 Make_Full_Type_Declaration (Loc,
5379 Defining_Identifier => Efam_Type,
5380 Type_Definition =>
5381 Make_Unconstrained_Array_Definition (Loc,
5382 Subtype_Marks =>
5383 (New_List (New_Occurrence_Of (Bas, Loc))),
5385 Component_Definition =>
5386 Make_Component_Definition (Loc,
5387 Aliased_Present => False,
5388 Subtype_Indication =>
5389 New_Occurrence_Of (Standard_Character, Loc))));
5390 end;
5392 Insert_After (Current_Node, Efam_Decl);
5393 Current_Node := Efam_Decl;
5394 Analyze (Efam_Decl);
5396 Append_To (Cdecls,
5397 Make_Component_Declaration (Loc,
5398 Defining_Identifier =>
5399 Make_Defining_Identifier (Loc, Chars (Efam)),
5401 Component_Definition =>
5402 Make_Component_Definition (Loc,
5403 Aliased_Present => False,
5404 Subtype_Indication =>
5405 Make_Subtype_Indication (Loc,
5406 Subtype_Mark =>
5407 New_Occurrence_Of (Efam_Type, Loc),
5409 Constraint =>
5410 Make_Index_Or_Discriminant_Constraint (Loc,
5411 Constraints => New_List (
5412 New_Occurrence_Of
5413 (Etype (Discrete_Subtype_Definition
5414 (Parent (Efam))), Loc)))))));
5416 end if;
5418 Next_Entity (Efam);
5419 end loop;
5420 end Collect_Entry_Families;
5422 -----------------------
5423 -- Concurrent_Object --
5424 -----------------------
5426 function Concurrent_Object
5427 (Spec_Id : Entity_Id;
5428 Conc_Typ : Entity_Id) return Entity_Id
5430 begin
5431 -- Parameter _O or _object
5433 if Is_Protected_Type (Conc_Typ) then
5434 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5436 -- Parameter _task
5438 else
5439 pragma Assert (Is_Task_Type (Conc_Typ));
5440 return First_Formal (Task_Body_Procedure (Conc_Typ));
5441 end if;
5442 end Concurrent_Object;
5444 ----------------------
5445 -- Copy_Result_Type --
5446 ----------------------
5448 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5449 New_Res : constant Node_Id := New_Copy_Tree (Res);
5450 Par_Spec : Node_Id;
5451 Formal : Entity_Id;
5453 begin
5454 -- If the result type is an access_to_subprogram, we must create new
5455 -- entities for its spec.
5457 if Nkind (New_Res) = N_Access_Definition
5458 and then Present (Access_To_Subprogram_Definition (New_Res))
5459 then
5460 -- Provide new entities for the formals
5462 Par_Spec := First (Parameter_Specifications
5463 (Access_To_Subprogram_Definition (New_Res)));
5464 while Present (Par_Spec) loop
5465 Formal := Defining_Identifier (Par_Spec);
5466 Set_Defining_Identifier (Par_Spec,
5467 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5468 Next (Par_Spec);
5469 end loop;
5470 end if;
5472 return New_Res;
5473 end Copy_Result_Type;
5475 --------------------
5476 -- Concurrent_Ref --
5477 --------------------
5479 -- The expression returned for a reference to a concurrent object has the
5480 -- form:
5482 -- taskV!(name)._Task_Id
5484 -- for a task, and
5486 -- objectV!(name)._Object
5488 -- for a protected object. For the case of an access to a concurrent
5489 -- object, there is an extra explicit dereference:
5491 -- taskV!(name.all)._Task_Id
5492 -- objectV!(name.all)._Object
5494 -- here taskV and objectV are the types for the associated records, which
5495 -- contain the required _Task_Id and _Object fields for tasks and protected
5496 -- objects, respectively.
5498 -- For the case of a task type name, the expression is
5500 -- Self;
5502 -- i.e. a call to the Self function which returns precisely this Task_Id
5504 -- For the case of a protected type name, the expression is
5506 -- objectR
5508 -- which is a renaming of the _object field of the current object
5509 -- record, passed into protected operations as a parameter.
5511 function Concurrent_Ref (N : Node_Id) return Node_Id is
5512 Loc : constant Source_Ptr := Sloc (N);
5513 Ntyp : constant Entity_Id := Etype (N);
5514 Dtyp : Entity_Id;
5515 Sel : Name_Id;
5517 function Is_Current_Task (T : Entity_Id) return Boolean;
5518 -- Check whether the reference is to the immediately enclosing task
5519 -- type, or to an outer one (rare but legal).
5521 ---------------------
5522 -- Is_Current_Task --
5523 ---------------------
5525 function Is_Current_Task (T : Entity_Id) return Boolean is
5526 Scop : Entity_Id;
5528 begin
5529 Scop := Current_Scope;
5530 while Present (Scop) and then Scop /= Standard_Standard loop
5531 if Scop = T then
5532 return True;
5534 elsif Is_Task_Type (Scop) then
5535 return False;
5537 -- If this is a procedure nested within the task type, we must
5538 -- assume that it can be called from an inner task, and therefore
5539 -- cannot treat it as a local reference.
5541 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5542 return False;
5544 else
5545 Scop := Scope (Scop);
5546 end if;
5547 end loop;
5549 -- We know that we are within the task body, so should have found it
5550 -- in scope.
5552 raise Program_Error;
5553 end Is_Current_Task;
5555 -- Start of processing for Concurrent_Ref
5557 begin
5558 if Is_Access_Type (Ntyp) then
5559 Dtyp := Designated_Type (Ntyp);
5561 if Is_Protected_Type (Dtyp) then
5562 Sel := Name_uObject;
5563 else
5564 Sel := Name_uTask_Id;
5565 end if;
5567 return
5568 Make_Selected_Component (Loc,
5569 Prefix =>
5570 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5571 Make_Explicit_Dereference (Loc, N)),
5572 Selector_Name => Make_Identifier (Loc, Sel));
5574 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5575 if Is_Task_Type (Entity (N)) then
5577 if Is_Current_Task (Entity (N)) then
5578 return
5579 Make_Function_Call (Loc,
5580 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5582 else
5583 declare
5584 Decl : Node_Id;
5585 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5586 T_Body : constant Node_Id :=
5587 Parent (Corresponding_Body (Parent (Entity (N))));
5589 begin
5590 Decl :=
5591 Make_Object_Declaration (Loc,
5592 Defining_Identifier => T_Self,
5593 Object_Definition =>
5594 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5595 Expression =>
5596 Make_Function_Call (Loc,
5597 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5598 Prepend (Decl, Declarations (T_Body));
5599 Analyze (Decl);
5600 Set_Scope (T_Self, Entity (N));
5601 return New_Occurrence_Of (T_Self, Loc);
5602 end;
5603 end if;
5605 else
5606 pragma Assert (Is_Protected_Type (Entity (N)));
5608 return
5609 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5610 end if;
5612 else
5613 if Is_Protected_Type (Ntyp) then
5614 Sel := Name_uObject;
5616 elsif Is_Task_Type (Ntyp) then
5617 Sel := Name_uTask_Id;
5619 else
5620 raise Program_Error;
5621 end if;
5623 return
5624 Make_Selected_Component (Loc,
5625 Prefix =>
5626 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5627 New_Copy_Tree (N)),
5628 Selector_Name => Make_Identifier (Loc, Sel));
5629 end if;
5630 end Concurrent_Ref;
5632 ------------------------
5633 -- Convert_Concurrent --
5634 ------------------------
5636 function Convert_Concurrent
5637 (N : Node_Id;
5638 Typ : Entity_Id) return Node_Id
5640 begin
5641 if not Is_Concurrent_Type (Typ) then
5642 return N;
5643 else
5644 return
5645 Unchecked_Convert_To
5646 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5647 end if;
5648 end Convert_Concurrent;
5650 -------------------------------------
5651 -- Debug_Private_Data_Declarations --
5652 -------------------------------------
5654 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5655 Debug_Nod : Node_Id;
5656 Decl : Node_Id;
5658 begin
5659 Decl := First (Decls);
5660 while Present (Decl) and then not Comes_From_Source (Decl) loop
5662 -- Declaration for concurrent entity _object and its access type,
5663 -- along with the entry index subtype:
5664 -- type prot_typVP is access prot_typV;
5665 -- _object : prot_typVP := prot_typV (_O);
5666 -- subtype Jnn is <Type of Index> range Low .. High;
5668 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5669 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5671 -- Declaration for the Protection object, discriminals, privals and
5672 -- entry index constant:
5673 -- conc_typR : protection_typ renames _object._object;
5674 -- discr_nameD : discr_typ renames _object.discr_name;
5675 -- discr_nameD : discr_typ renames _task.discr_name;
5676 -- prival_name : comp_typ renames _object.comp_name;
5677 -- J : constant Jnn :=
5678 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5680 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5681 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5682 Debug_Nod := Debug_Renaming_Declaration (Decl);
5684 if Present (Debug_Nod) then
5685 Insert_After (Decl, Debug_Nod);
5686 end if;
5687 end if;
5689 Next (Decl);
5690 end loop;
5691 end Debug_Private_Data_Declarations;
5693 ------------------------------
5694 -- Ensure_Statement_Present --
5695 ------------------------------
5697 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5698 Stmt : Node_Id;
5700 begin
5701 if Opt.Suppress_Control_Flow_Optimizations
5702 and then Is_Empty_List (Statements (Alt))
5703 then
5704 Stmt := Make_Null_Statement (Loc);
5706 -- Mark NULL statement as coming from source so that it is not
5707 -- eliminated by GIGI.
5709 -- Another covert channel. If this is a requirement, it must be
5710 -- documented in sinfo/einfo ???
5712 Set_Comes_From_Source (Stmt, True);
5714 Set_Statements (Alt, New_List (Stmt));
5715 end if;
5716 end Ensure_Statement_Present;
5718 ----------------------------
5719 -- Entry_Index_Expression --
5720 ----------------------------
5722 function Entry_Index_Expression
5723 (Sloc : Source_Ptr;
5724 Ent : Entity_Id;
5725 Index : Node_Id;
5726 Ttyp : Entity_Id) return Node_Id
5728 Expr : Node_Id;
5729 Num : Node_Id;
5730 Lo : Node_Id;
5731 Hi : Node_Id;
5732 Prev : Entity_Id;
5733 S : Node_Id;
5735 begin
5736 -- The queues of entries and entry families appear in textual order in
5737 -- the associated record. The entry index is computed as the sum of the
5738 -- number of queues for all entries that precede the designated one, to
5739 -- which is added the index expression, if this expression denotes a
5740 -- member of a family.
5742 -- The following is a place holder for the count of simple entries
5744 Num := Make_Integer_Literal (Sloc, 1);
5746 -- We construct an expression which is a series of addition operations.
5747 -- The first operand is the number of single entries that precede this
5748 -- one, the second operand is the index value relative to the start of
5749 -- the referenced family, and the remaining operands are the lengths of
5750 -- the entry families that precede this entry, i.e. the constructed
5751 -- expression is:
5753 -- number_simple_entries +
5754 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5755 -- family'length + ...
5757 -- where index-value is the given index value, and s is the index
5758 -- subtype (we have to use pos because the subtype might be an
5759 -- enumeration type preventing direct subtraction). Note that the task
5760 -- entry array is one-indexed.
5762 -- The upper bound of the entry family may be a discriminant, so we
5763 -- retrieve the lower bound explicitly to compute offset, rather than
5764 -- using the index subtype which may mention a discriminant.
5766 if Present (Index) then
5767 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5769 Expr :=
5770 Make_Op_Add (Sloc,
5771 Left_Opnd => Num,
5772 Right_Opnd =>
5773 Family_Offset
5774 (Sloc,
5775 Make_Attribute_Reference (Sloc,
5776 Attribute_Name => Name_Pos,
5777 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5778 Expressions => New_List (Relocate_Node (Index))),
5779 Type_Low_Bound (S),
5780 Ttyp,
5781 False));
5782 else
5783 Expr := Num;
5784 end if;
5786 -- Now add lengths of preceding entries and entry families
5788 Prev := First_Entity (Ttyp);
5790 while Chars (Prev) /= Chars (Ent)
5791 or else (Ekind (Prev) /= Ekind (Ent))
5792 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5793 loop
5794 if Ekind (Prev) = E_Entry then
5795 Set_Intval (Num, Intval (Num) + 1);
5797 elsif Ekind (Prev) = E_Entry_Family then
5798 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5799 Lo := Type_Low_Bound (S);
5800 Hi := Type_High_Bound (S);
5802 Expr :=
5803 Make_Op_Add (Sloc,
5804 Left_Opnd => Expr,
5805 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5807 -- Other components are anonymous types to be ignored
5809 else
5810 null;
5811 end if;
5813 Next_Entity (Prev);
5814 end loop;
5816 return Expr;
5817 end Entry_Index_Expression;
5819 ---------------------------
5820 -- Establish_Task_Master --
5821 ---------------------------
5823 procedure Establish_Task_Master (N : Node_Id) is
5824 Call : Node_Id;
5826 begin
5827 if Restriction_Active (No_Task_Hierarchy) = False then
5828 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5830 -- The block may have no declarations (and nevertheless be a task
5831 -- master) if it contains a call that may return an object that
5832 -- contains tasks.
5834 if No (Declarations (N)) then
5835 Set_Declarations (N, New_List (Call));
5836 else
5837 Prepend_To (Declarations (N), Call);
5838 end if;
5840 Analyze (Call);
5841 end if;
5842 end Establish_Task_Master;
5844 --------------------------------
5845 -- Expand_Accept_Declarations --
5846 --------------------------------
5848 -- Part of the expansion of an accept statement involves the creation of
5849 -- a declaration that can be referenced from the statement sequence of
5850 -- the accept:
5852 -- Ann : Address;
5854 -- This declaration is inserted immediately before the accept statement
5855 -- and it is important that it be inserted before the statements of the
5856 -- statement sequence are analyzed. Thus it would be too late to create
5857 -- this declaration in the Expand_N_Accept_Statement routine, which is
5858 -- why there is a separate procedure to be called directly from Sem_Ch9.
5860 -- Ann is used to hold the address of the record containing the parameters
5861 -- (see Expand_N_Entry_Call for more details on how this record is built).
5862 -- References to the parameters do an unchecked conversion of this address
5863 -- to a pointer to the required record type, and then access the field that
5864 -- holds the value of the required parameter. The entity for the address
5865 -- variable is held as the top stack element (i.e. the last element) of the
5866 -- Accept_Address stack in the corresponding entry entity, and this element
5867 -- must be set in place before the statements are processed.
5869 -- The above description applies to the case of a stand alone accept
5870 -- statement, i.e. one not appearing as part of a select alternative.
5872 -- For the case of an accept that appears as part of a select alternative
5873 -- of a selective accept, we must still create the declaration right away,
5874 -- since Ann is needed immediately, but there is an important difference:
5876 -- The declaration is inserted before the selective accept, not before
5877 -- the accept statement (which is not part of a list anyway, and so would
5878 -- not accommodate inserted declarations)
5880 -- We only need one address variable for the entire selective accept. So
5881 -- the Ann declaration is created only for the first accept alternative,
5882 -- and subsequent accept alternatives reference the same Ann variable.
5884 -- We can distinguish the two cases by seeing whether the accept statement
5885 -- is part of a list. If not, then it must be in an accept alternative.
5887 -- To expand the requeue statement, a label is provided at the end of the
5888 -- accept statement or alternative of which it is a part, so that the
5889 -- statement can be skipped after the requeue is complete. This label is
5890 -- created here rather than during the expansion of the accept statement,
5891 -- because it will be needed by any requeue statements within the accept,
5892 -- which are expanded before the accept.
5894 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5895 Loc : constant Source_Ptr := Sloc (N);
5896 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5897 Ann : Entity_Id := Empty;
5898 Adecl : Node_Id;
5899 Lab : Node_Id;
5900 Ldecl : Node_Id;
5901 Ldecl2 : Node_Id;
5903 begin
5904 if Expander_Active then
5906 -- If we have no handled statement sequence, we may need to build
5907 -- a dummy sequence consisting of a null statement. This can be
5908 -- skipped if the trivial accept optimization is permitted.
5910 if not Trivial_Accept_OK
5911 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5912 then
5913 Set_Handled_Statement_Sequence (N,
5914 Make_Handled_Sequence_Of_Statements (Loc,
5915 Statements => New_List (Make_Null_Statement (Loc))));
5916 end if;
5918 -- Create and declare two labels to be placed at the end of the
5919 -- accept statement. The first label is used to allow requeues to
5920 -- skip the remainder of entry processing. The second label is used
5921 -- to skip the remainder of entry processing if the rendezvous
5922 -- completes in the middle of the accept body.
5924 if Present (Handled_Statement_Sequence (N)) then
5925 declare
5926 Ent : Entity_Id;
5928 begin
5929 Ent := Make_Temporary (Loc, 'L');
5930 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5931 Ldecl :=
5932 Make_Implicit_Label_Declaration (Loc,
5933 Defining_Identifier => Ent,
5934 Label_Construct => Lab);
5935 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5937 Ent := Make_Temporary (Loc, 'L');
5938 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5939 Ldecl2 :=
5940 Make_Implicit_Label_Declaration (Loc,
5941 Defining_Identifier => Ent,
5942 Label_Construct => Lab);
5943 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5944 end;
5946 else
5947 Ldecl := Empty;
5948 Ldecl2 := Empty;
5949 end if;
5951 -- Case of stand alone accept statement
5953 if Is_List_Member (N) then
5955 if Present (Handled_Statement_Sequence (N)) then
5956 Ann := Make_Temporary (Loc, 'A');
5958 Adecl :=
5959 Make_Object_Declaration (Loc,
5960 Defining_Identifier => Ann,
5961 Object_Definition =>
5962 New_Occurrence_Of (RTE (RE_Address), Loc));
5964 Insert_Before_And_Analyze (N, Adecl);
5965 Insert_Before_And_Analyze (N, Ldecl);
5966 Insert_Before_And_Analyze (N, Ldecl2);
5967 end if;
5969 -- Case of accept statement which is in an accept alternative
5971 else
5972 declare
5973 Acc_Alt : constant Node_Id := Parent (N);
5974 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5975 Alt : Node_Id;
5977 begin
5978 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5979 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5981 -- ??? Consider a single label for select statements
5983 if Present (Handled_Statement_Sequence (N)) then
5984 Prepend (Ldecl2,
5985 Statements (Handled_Statement_Sequence (N)));
5986 Analyze (Ldecl2);
5988 Prepend (Ldecl,
5989 Statements (Handled_Statement_Sequence (N)));
5990 Analyze (Ldecl);
5991 end if;
5993 -- Find first accept alternative of the selective accept. A
5994 -- valid selective accept must have at least one accept in it.
5996 Alt := First (Select_Alternatives (Sel_Acc));
5998 while Nkind (Alt) /= N_Accept_Alternative loop
5999 Next (Alt);
6000 end loop;
6002 -- If this is the first accept statement, then we have to
6003 -- create the Ann variable, as for the stand alone case, except
6004 -- that it is inserted before the selective accept. Similarly,
6005 -- a label for requeue expansion must be declared.
6007 if N = Accept_Statement (Alt) then
6008 Ann := Make_Temporary (Loc, 'A');
6009 Adecl :=
6010 Make_Object_Declaration (Loc,
6011 Defining_Identifier => Ann,
6012 Object_Definition =>
6013 New_Occurrence_Of (RTE (RE_Address), Loc));
6015 Insert_Before_And_Analyze (Sel_Acc, Adecl);
6017 -- If this is not the first accept statement, then find the Ann
6018 -- variable allocated by the first accept and use it.
6020 else
6021 Ann :=
6022 Node (Last_Elmt (Accept_Address
6023 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
6024 end if;
6025 end;
6026 end if;
6028 -- Merge here with Ann either created or referenced, and Adecl
6029 -- pointing to the corresponding declaration. Remaining processing
6030 -- is the same for the two cases.
6032 if Present (Ann) then
6033 Append_Elmt (Ann, Accept_Address (Ent));
6034 Set_Debug_Info_Needed (Ann);
6035 end if;
6037 -- Create renaming declarations for the entry formals. Each reference
6038 -- to a formal becomes a dereference of a component of the parameter
6039 -- block, whose address is held in Ann. These declarations are
6040 -- eventually inserted into the accept block, and analyzed there so
6041 -- that they have the proper scope for gdb and do not conflict with
6042 -- other declarations.
6044 if Present (Parameter_Specifications (N))
6045 and then Present (Handled_Statement_Sequence (N))
6046 then
6047 declare
6048 Comp : Entity_Id;
6049 Decl : Node_Id;
6050 Formal : Entity_Id;
6051 New_F : Entity_Id;
6052 Renamed_Formal : Node_Id;
6054 begin
6055 Push_Scope (Ent);
6056 Formal := First_Formal (Ent);
6058 while Present (Formal) loop
6059 Comp := Entry_Component (Formal);
6060 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
6062 Set_Etype (New_F, Etype (Formal));
6063 Set_Scope (New_F, Ent);
6065 -- Now we set debug info needed on New_F even though it does
6066 -- not come from source, so that the debugger will get the
6067 -- right information for these generated names.
6069 Set_Debug_Info_Needed (New_F);
6071 if Ekind (Formal) = E_In_Parameter then
6072 Set_Ekind (New_F, E_Constant);
6073 else
6074 Set_Ekind (New_F, E_Variable);
6075 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
6076 end if;
6078 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
6080 Renamed_Formal :=
6081 Make_Selected_Component (Loc,
6082 Prefix =>
6083 Unchecked_Convert_To (
6084 Entry_Parameters_Type (Ent),
6085 New_Occurrence_Of (Ann, Loc)),
6086 Selector_Name =>
6087 New_Occurrence_Of (Comp, Loc));
6089 Decl :=
6090 Build_Renamed_Formal_Declaration
6091 (New_F, Formal, Comp, Renamed_Formal);
6093 if No (Declarations (N)) then
6094 Set_Declarations (N, New_List);
6095 end if;
6097 Append (Decl, Declarations (N));
6098 Set_Renamed_Object (Formal, New_F);
6099 Next_Formal (Formal);
6100 end loop;
6102 End_Scope;
6103 end;
6104 end if;
6105 end if;
6106 end Expand_Accept_Declarations;
6108 ---------------------------------------------
6109 -- Expand_Access_Protected_Subprogram_Type --
6110 ---------------------------------------------
6112 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6113 Loc : constant Source_Ptr := Sloc (N);
6114 Comps : List_Id;
6115 T : constant Entity_Id := Defining_Identifier (N);
6116 D_T : constant Entity_Id := Designated_Type (T);
6117 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6118 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6119 P_List : constant List_Id := Build_Protected_Spec
6120 (N, RTE (RE_Address), D_T, False);
6121 Decl1 : Node_Id;
6122 Decl2 : Node_Id;
6123 Def1 : Node_Id;
6125 begin
6126 -- Create access to subprogram with full signature
6128 if Etype (D_T) /= Standard_Void_Type then
6129 Def1 :=
6130 Make_Access_Function_Definition (Loc,
6131 Parameter_Specifications => P_List,
6132 Result_Definition =>
6133 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6135 else
6136 Def1 :=
6137 Make_Access_Procedure_Definition (Loc,
6138 Parameter_Specifications => P_List);
6139 end if;
6141 Decl1 :=
6142 Make_Full_Type_Declaration (Loc,
6143 Defining_Identifier => D_T2,
6144 Type_Definition => Def1);
6146 Insert_After_And_Analyze (N, Decl1);
6148 -- Associate the access to subprogram with its original access to
6149 -- protected subprogram type. Needed by the backend to know that this
6150 -- type corresponds with an access to protected subprogram type.
6152 Set_Original_Access_Type (D_T2, T);
6154 -- Create Equivalent_Type, a record with two components for an access to
6155 -- object and an access to subprogram.
6157 Comps := New_List (
6158 Make_Component_Declaration (Loc,
6159 Defining_Identifier => Make_Temporary (Loc, 'P'),
6160 Component_Definition =>
6161 Make_Component_Definition (Loc,
6162 Aliased_Present => False,
6163 Subtype_Indication =>
6164 New_Occurrence_Of (RTE (RE_Address), Loc))),
6166 Make_Component_Declaration (Loc,
6167 Defining_Identifier => Make_Temporary (Loc, 'S'),
6168 Component_Definition =>
6169 Make_Component_Definition (Loc,
6170 Aliased_Present => False,
6171 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6173 Decl2 :=
6174 Make_Full_Type_Declaration (Loc,
6175 Defining_Identifier => E_T,
6176 Type_Definition =>
6177 Make_Record_Definition (Loc,
6178 Component_List =>
6179 Make_Component_List (Loc, Component_Items => Comps)));
6181 Insert_After_And_Analyze (Decl1, Decl2);
6182 Set_Equivalent_Type (T, E_T);
6183 end Expand_Access_Protected_Subprogram_Type;
6185 --------------------------
6186 -- Expand_Entry_Barrier --
6187 --------------------------
6189 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6190 Cond : constant Node_Id :=
6191 Condition (Entry_Body_Formal_Part (N));
6192 Prot : constant Entity_Id := Scope (Ent);
6193 Spec_Decl : constant Node_Id := Parent (Prot);
6194 Func : Entity_Id;
6195 B_F : Node_Id;
6196 Body_Decl : Node_Id;
6198 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
6199 -- Check whether entity in Barrier is external to protected type.
6200 -- If so, barrier may not be properly synchronized.
6202 ----------------------
6203 -- Is_Global_Entity --
6204 ----------------------
6206 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6207 E : Entity_Id;
6208 S : Entity_Id;
6210 begin
6211 if Is_Entity_Name (N) and then Present (Entity (N)) then
6212 E := Entity (N);
6213 S := Scope (E);
6215 if Ekind (E) = E_Variable then
6216 if Scope (E) = Func then
6217 null;
6219 -- A protected call from a barrier to another object is ok
6221 elsif Ekind (Etype (E)) = E_Protected_Type then
6222 null;
6224 -- If the variable is within the package body we consider
6225 -- this safe. This is a common (if dubious) idiom.
6227 elsif S = Scope (Prot)
6228 and then Ekind_In (S, E_Package, E_Generic_Package)
6229 and then Nkind (Parent (E)) = N_Object_Declaration
6230 and then Nkind (Parent (Parent (E))) = N_Package_Body
6231 then
6232 null;
6234 else
6235 Error_Msg_N ("potentially unsynchronized barrier??", N);
6236 Error_Msg_N ("\& should be private component of type??", N);
6237 end if;
6238 end if;
6239 end if;
6241 return OK;
6242 end Is_Global_Entity;
6244 procedure Check_Unprotected_Barrier is
6245 new Traverse_Proc (Is_Global_Entity);
6247 -- Start of processing for Expand_Entry_Barrier
6249 begin
6250 if No_Run_Time_Mode then
6251 Error_Msg_CRT ("entry barrier", N);
6252 return;
6253 end if;
6255 -- The body of the entry barrier must be analyzed in the context of the
6256 -- protected object, but its scope is external to it, just as any other
6257 -- unprotected version of a protected operation. The specification has
6258 -- been produced when the protected type declaration was elaborated. We
6259 -- build the body, insert it in the enclosing scope, but analyze it in
6260 -- the current context. A more uniform approach would be to treat the
6261 -- barrier just as a protected function, and discard the protected
6262 -- version of it because it is never called.
6264 if Expander_Active then
6265 B_F := Build_Barrier_Function (N, Ent, Prot);
6266 Func := Barrier_Function (Ent);
6267 Set_Corresponding_Spec (B_F, Func);
6269 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6271 if Nkind (Parent (Body_Decl)) = N_Subunit then
6272 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6273 end if;
6275 Insert_Before_And_Analyze (Body_Decl, B_F);
6277 Set_Discriminals (Spec_Decl);
6278 Set_Scope (Func, Scope (Prot));
6280 else
6281 Analyze_And_Resolve (Cond, Any_Boolean);
6282 end if;
6284 -- The Ravenscar profile restricts barriers to simple variables declared
6285 -- within the protected object. We also allow Boolean constants, since
6286 -- these appear in several published examples and are also allowed by
6287 -- other compilers.
6289 -- Note that after analysis variables in this context will be replaced
6290 -- by the corresponding prival, that is to say a renaming of a selected
6291 -- component of the form _Object.Var. If expansion is disabled, as
6292 -- within a generic, we check that the entity appears in the current
6293 -- scope.
6295 if Is_Entity_Name (Cond) then
6297 -- A small optimization of useless renamings. If the scope of the
6298 -- entity of the condition is not the barrier function, then the
6299 -- condition does not reference any of the generated renamings
6300 -- within the function.
6302 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6303 Set_Declarations (B_F, Empty_List);
6304 end if;
6306 if Entity (Cond) = Standard_False
6307 or else
6308 Entity (Cond) = Standard_True
6309 then
6310 return;
6312 elsif not Expander_Active
6313 and then Scope (Entity (Cond)) = Current_Scope
6314 then
6315 return;
6317 -- Check for case of _object.all.field (note that the explicit
6318 -- dereference gets inserted by analyze/expand of _object.field)
6320 elsif Present (Renamed_Object (Entity (Cond)))
6321 and then
6322 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
6323 and then
6324 Chars
6325 (Prefix
6326 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
6327 then
6328 return;
6329 end if;
6330 end if;
6332 -- It is not a boolean variable or literal, so check the restriction.
6333 -- Note that it is safe to be calling Check_Restriction from here, even
6334 -- though this is part of the expander, since Expand_Entry_Barrier is
6335 -- called from Sem_Ch9 even in -gnatc mode.
6337 Check_Restriction (Simple_Barriers, Cond);
6339 -- Emit warning if barrier contains global entities and is thus
6340 -- potentially unsynchronized.
6342 Check_Unprotected_Barrier (Cond);
6343 end Expand_Entry_Barrier;
6345 ------------------------------
6346 -- Expand_N_Abort_Statement --
6347 ------------------------------
6349 -- Expand abort T1, T2, .. Tn; into:
6350 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6352 procedure Expand_N_Abort_Statement (N : Node_Id) is
6353 Loc : constant Source_Ptr := Sloc (N);
6354 Tlist : constant List_Id := Names (N);
6355 Count : Nat;
6356 Aggr : Node_Id;
6357 Tasknm : Node_Id;
6359 begin
6360 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6361 Count := 0;
6363 Tasknm := First (Tlist);
6365 while Present (Tasknm) loop
6366 Count := Count + 1;
6368 -- A task interface class-wide type object is being aborted. Retrieve
6369 -- its _task_id by calling a dispatching routine.
6371 if Ada_Version >= Ada_2005
6372 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6373 and then Is_Interface (Etype (Tasknm))
6374 and then Is_Task_Interface (Etype (Tasknm))
6375 then
6376 Append_To (Component_Associations (Aggr),
6377 Make_Component_Association (Loc,
6378 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6379 Expression =>
6381 -- Task_Id (Tasknm._disp_get_task_id)
6383 Make_Unchecked_Type_Conversion (Loc,
6384 Subtype_Mark =>
6385 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6386 Expression =>
6387 Make_Selected_Component (Loc,
6388 Prefix => New_Copy_Tree (Tasknm),
6389 Selector_Name =>
6390 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6392 else
6393 Append_To (Component_Associations (Aggr),
6394 Make_Component_Association (Loc,
6395 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6396 Expression => Concurrent_Ref (Tasknm)));
6397 end if;
6399 Next (Tasknm);
6400 end loop;
6402 Rewrite (N,
6403 Make_Procedure_Call_Statement (Loc,
6404 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6405 Parameter_Associations => New_List (
6406 Make_Qualified_Expression (Loc,
6407 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6408 Expression => Aggr))));
6410 Analyze (N);
6411 end Expand_N_Abort_Statement;
6413 -------------------------------
6414 -- Expand_N_Accept_Statement --
6415 -------------------------------
6417 -- This procedure handles expansion of accept statements that stand alone,
6418 -- i.e. they are not part of an accept alternative. The expansion of
6419 -- accept statement in accept alternatives is handled by the routines
6420 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6421 -- following description applies only to stand alone accept statements.
6423 -- If there is no handled statement sequence, or only null statements, then
6424 -- this is called a trivial accept, and the expansion is:
6426 -- Accept_Trivial (entry-index)
6428 -- If there is a handled statement sequence, then the expansion is:
6430 -- Ann : Address;
6431 -- {Lnn : Label}
6433 -- begin
6434 -- begin
6435 -- Accept_Call (entry-index, Ann);
6436 -- Renaming_Declarations for formals
6437 -- <statement sequence from N_Accept_Statement node>
6438 -- Complete_Rendezvous;
6439 -- <<Lnn>>
6441 -- exception
6442 -- when ... =>
6443 -- <exception handler from N_Accept_Statement node>
6444 -- Complete_Rendezvous;
6445 -- when ... =>
6446 -- <exception handler from N_Accept_Statement node>
6447 -- Complete_Rendezvous;
6448 -- ...
6449 -- end;
6451 -- exception
6452 -- when all others =>
6453 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6454 -- end;
6456 -- The first three declarations were already inserted ahead of the accept
6457 -- statement by the Expand_Accept_Declarations procedure, which was called
6458 -- directly from the semantics during analysis of the accept statement,
6459 -- before analyzing its contained statements.
6461 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6462 -- from possible expansion activity (the original source of course does
6463 -- not have any declarations associated with the accept statement, since
6464 -- an accept statement has no declarative part). In particular, if the
6465 -- expander is active, the first such declaration is the declaration of
6466 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6468 -- The two blocks are merged into a single block if the inner block has
6469 -- no exception handlers, but otherwise two blocks are required, since
6470 -- exceptions might be raised in the exception handlers of the inner
6471 -- block, and Exceptional_Complete_Rendezvous must be called.
6473 procedure Expand_N_Accept_Statement (N : Node_Id) is
6474 Loc : constant Source_Ptr := Sloc (N);
6475 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6476 Ename : constant Node_Id := Entry_Direct_Name (N);
6477 Eindx : constant Node_Id := Entry_Index (N);
6478 Eent : constant Entity_Id := Entity (Ename);
6479 Acstack : constant Elist_Id := Accept_Address (Eent);
6480 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6481 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6482 Blkent : Entity_Id;
6483 Call : Node_Id;
6484 Block : Node_Id;
6486 begin
6487 -- If the accept statement is not part of a list, then its parent must
6488 -- be an accept alternative, and, as described above, we do not do any
6489 -- expansion for such accept statements at this level.
6491 if not Is_List_Member (N) then
6492 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6493 return;
6495 -- Trivial accept case (no statement sequence, or null statements).
6496 -- If the accept statement has declarations, then just insert them
6497 -- before the procedure call.
6499 elsif Trivial_Accept_OK
6500 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6501 then
6502 -- Remove declarations for renamings, because the parameter block
6503 -- will not be assigned.
6505 declare
6506 D : Node_Id;
6507 Next_D : Node_Id;
6509 begin
6510 D := First (Declarations (N));
6511 while Present (D) loop
6512 Next_D := Next (D);
6513 if Nkind (D) = N_Object_Renaming_Declaration then
6514 Remove (D);
6515 end if;
6517 D := Next_D;
6518 end loop;
6519 end;
6521 if Present (Declarations (N)) then
6522 Insert_Actions (N, Declarations (N));
6523 end if;
6525 Rewrite (N,
6526 Make_Procedure_Call_Statement (Loc,
6527 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6528 Parameter_Associations => New_List (
6529 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6531 Analyze (N);
6533 -- Discard Entry_Address that was created for it, so it will not be
6534 -- emitted if this accept statement is in the statement part of a
6535 -- delay alternative.
6537 if Present (Stats) then
6538 Remove_Last_Elmt (Acstack);
6539 end if;
6541 -- Case of statement sequence present
6543 else
6544 -- Construct the block, using the declarations from the accept
6545 -- statement if any to initialize the declarations of the block.
6547 Blkent := Make_Temporary (Loc, 'A');
6548 Set_Ekind (Blkent, E_Block);
6549 Set_Etype (Blkent, Standard_Void_Type);
6550 Set_Scope (Blkent, Current_Scope);
6552 Block :=
6553 Make_Block_Statement (Loc,
6554 Identifier => New_Occurrence_Of (Blkent, Loc),
6555 Declarations => Declarations (N),
6556 Handled_Statement_Sequence => Build_Accept_Body (N));
6558 -- For the analysis of the generated declarations, the parent node
6559 -- must be properly set.
6561 Set_Parent (Block, Parent (N));
6563 -- Prepend call to Accept_Call to main statement sequence If the
6564 -- accept has exception handlers, the statement sequence is wrapped
6565 -- in a block. Insert call and renaming declarations in the
6566 -- declarations of the block, so they are elaborated before the
6567 -- handlers.
6569 Call :=
6570 Make_Procedure_Call_Statement (Loc,
6571 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6572 Parameter_Associations => New_List (
6573 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6574 New_Occurrence_Of (Ann, Loc)));
6576 if Parent (Stats) = N then
6577 Prepend (Call, Statements (Stats));
6578 else
6579 Set_Declarations (Parent (Stats), New_List (Call));
6580 end if;
6582 Analyze (Call);
6584 Push_Scope (Blkent);
6586 declare
6587 D : Node_Id;
6588 Next_D : Node_Id;
6589 Typ : Entity_Id;
6591 begin
6592 D := First (Declarations (N));
6593 while Present (D) loop
6594 Next_D := Next (D);
6596 if Nkind (D) = N_Object_Renaming_Declaration then
6598 -- The renaming declarations for the formals were created
6599 -- during analysis of the accept statement, and attached to
6600 -- the list of declarations. Place them now in the context
6601 -- of the accept block or subprogram.
6603 Remove (D);
6604 Typ := Entity (Subtype_Mark (D));
6605 Insert_After (Call, D);
6606 Analyze (D);
6608 -- If the formal is class_wide, it does not have an actual
6609 -- subtype. The analysis of the renaming declaration creates
6610 -- one, but we need to retain the class-wide nature of the
6611 -- entity.
6613 if Is_Class_Wide_Type (Typ) then
6614 Set_Etype (Defining_Identifier (D), Typ);
6615 end if;
6617 end if;
6619 D := Next_D;
6620 end loop;
6621 end;
6623 End_Scope;
6625 -- Replace the accept statement by the new block
6627 Rewrite (N, Block);
6628 Analyze (N);
6630 -- Last step is to unstack the Accept_Address value
6632 Remove_Last_Elmt (Acstack);
6633 end if;
6634 end Expand_N_Accept_Statement;
6636 ----------------------------------
6637 -- Expand_N_Asynchronous_Select --
6638 ----------------------------------
6640 -- This procedure assumes that the trigger statement is an entry call or
6641 -- a dispatching procedure call. A delay alternative should already have
6642 -- been expanded into an entry call to the appropriate delay object Wait
6643 -- entry.
6645 -- If the trigger is a task entry call, the select is implemented with
6646 -- a Task_Entry_Call:
6648 -- declare
6649 -- B : Boolean;
6650 -- C : Boolean;
6651 -- P : parms := (parm, parm, parm);
6653 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6655 -- procedure _clean is
6656 -- begin
6657 -- ...
6658 -- Cancel_Task_Entry_Call (C);
6659 -- ...
6660 -- end _clean;
6662 -- begin
6663 -- Abort_Defer;
6664 -- Task_Entry_Call
6665 -- (<acceptor-task>, -- Acceptor
6666 -- <entry-index>, -- E
6667 -- P'Address, -- Uninterpreted_Data
6668 -- Asynchronous_Call, -- Mode
6669 -- B); -- Rendezvous_Successful
6671 -- begin
6672 -- begin
6673 -- Abort_Undefer;
6674 -- <abortable-part>
6675 -- at end
6676 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6677 -- end;
6678 -- exception
6679 -- when Abort_Signal => Abort_Undefer;
6680 -- end;
6682 -- parm := P.param;
6683 -- parm := P.param;
6684 -- ...
6685 -- if not C then
6686 -- <triggered-statements>
6687 -- end if;
6688 -- end;
6690 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6691 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6692 -- as follows:
6694 -- declare
6695 -- P : parms := (parm, parm, parm);
6696 -- begin
6697 -- Call_Simple (acceptor-task, entry-index, P'Address);
6698 -- parm := P.param;
6699 -- parm := P.param;
6700 -- ...
6701 -- end;
6703 -- so the task at hand is to convert the latter expansion into the former
6705 -- If the trigger is a protected entry call, the select is implemented
6706 -- with Protected_Entry_Call:
6708 -- declare
6709 -- P : E1_Params := (param, param, param);
6710 -- Bnn : Communications_Block;
6712 -- begin
6713 -- declare
6715 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6717 -- procedure _clean is
6718 -- begin
6719 -- ...
6720 -- if Enqueued (Bnn) then
6721 -- Cancel_Protected_Entry_Call (Bnn);
6722 -- end if;
6723 -- ...
6724 -- end _clean;
6726 -- begin
6727 -- begin
6728 -- Protected_Entry_Call
6729 -- (po._object'Access, -- Object
6730 -- <entry index>, -- E
6731 -- P'Address, -- Uninterpreted_Data
6732 -- Asynchronous_Call, -- Mode
6733 -- Bnn); -- Block
6735 -- if Enqueued (Bnn) then
6736 -- <abortable-part>
6737 -- end if;
6738 -- at end
6739 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6740 -- end;
6741 -- exception
6742 -- when Abort_Signal => Abort_Undefer;
6743 -- end;
6745 -- if not Cancelled (Bnn) then
6746 -- <triggered-statements>
6747 -- end if;
6748 -- end;
6750 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6751 -- entry call:
6753 -- declare
6754 -- P : E1_Params := (param, param, param);
6755 -- Bnn : Communications_Block;
6757 -- begin
6758 -- Protected_Entry_Call
6759 -- (po._object'Access, -- Object
6760 -- <entry index>, -- E
6761 -- P'Address, -- Uninterpreted_Data
6762 -- Simple_Call, -- Mode
6763 -- Bnn); -- Block
6764 -- parm := P.param;
6765 -- parm := P.param;
6766 -- ...
6767 -- end;
6769 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6770 -- expanded into:
6772 -- declare
6773 -- B : Boolean := False;
6774 -- Bnn : Communication_Block;
6775 -- C : Ada.Tags.Prim_Op_Kind;
6776 -- D : System.Storage_Elements.Dummy_Communication_Block;
6777 -- K : Ada.Tags.Tagged_Kind :=
6778 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6779 -- P : Parameters := (Param1 .. ParamN);
6780 -- S : Integer;
6781 -- U : Boolean;
6783 -- begin
6784 -- if K = Ada.Tags.TK_Limited_Tagged
6785 -- or else K = Ada.Tags.TK_Tagged
6786 -- then
6787 -- <dispatching-call>;
6788 -- <triggering-statements>;
6790 -- else
6791 -- S :=
6792 -- Ada.Tags.Get_Offset_Index
6793 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6795 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6797 -- if C = POK_Protected_Entry then
6798 -- declare
6799 -- procedure _clean is
6800 -- begin
6801 -- if Enqueued (Bnn) then
6802 -- Cancel_Protected_Entry_Call (Bnn);
6803 -- end if;
6804 -- end _clean;
6806 -- begin
6807 -- begin
6808 -- _Disp_Asynchronous_Select
6809 -- (<object>, S, P'Address, D, B);
6810 -- Bnn := Communication_Block (D);
6812 -- Param1 := P.Param1;
6813 -- ...
6814 -- ParamN := P.ParamN;
6816 -- if Enqueued (Bnn) then
6817 -- <abortable-statements>
6818 -- end if;
6819 -- at end
6820 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6821 -- end;
6822 -- exception
6823 -- when Abort_Signal => Abort_Undefer;
6824 -- end;
6826 -- if not Cancelled (Bnn) then
6827 -- <triggering-statements>
6828 -- end if;
6830 -- elsif C = POK_Task_Entry then
6831 -- declare
6832 -- procedure _clean is
6833 -- begin
6834 -- Cancel_Task_Entry_Call (U);
6835 -- end _clean;
6837 -- begin
6838 -- Abort_Defer;
6840 -- _Disp_Asynchronous_Select
6841 -- (<object>, S, P'Address, D, B);
6842 -- Bnn := Communication_Bloc (D);
6844 -- Param1 := P.Param1;
6845 -- ...
6846 -- ParamN := P.ParamN;
6848 -- begin
6849 -- begin
6850 -- Abort_Undefer;
6851 -- <abortable-statements>
6852 -- at end
6853 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6854 -- end;
6855 -- exception
6856 -- when Abort_Signal => Abort_Undefer;
6857 -- end;
6859 -- if not U then
6860 -- <triggering-statements>
6861 -- end if;
6862 -- end;
6864 -- else
6865 -- <dispatching-call>;
6866 -- <triggering-statements>
6867 -- end if;
6868 -- end if;
6869 -- end;
6871 -- The job is to convert this to the asynchronous form
6873 -- If the trigger is a delay statement, it will have been expanded into
6874 -- a call to one of the GNARL delay procedures. This routine will convert
6875 -- this into a protected entry call on a delay object and then continue
6876 -- processing as for a protected entry call trigger. This requires
6877 -- declaring a Delay_Block object and adding a pointer to this object to
6878 -- the parameter list of the delay procedure to form the parameter list of
6879 -- the entry call. This object is used by the runtime to queue the delay
6880 -- request.
6882 -- For a description of the use of P and the assignments after the call,
6883 -- see Expand_N_Entry_Call_Statement.
6885 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6886 Loc : constant Source_Ptr := Sloc (N);
6887 Abrt : constant Node_Id := Abortable_Part (N);
6888 Trig : constant Node_Id := Triggering_Alternative (N);
6890 Abort_Block_Ent : Entity_Id;
6891 Abortable_Block : Node_Id;
6892 Actuals : List_Id;
6893 Astats : List_Id;
6894 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6895 Blk_Typ : Entity_Id;
6896 Call : Node_Id;
6897 Call_Ent : Entity_Id;
6898 Cancel_Param : Entity_Id;
6899 Cleanup_Block : Node_Id;
6900 Cleanup_Block_Ent : Entity_Id;
6901 Cleanup_Stmts : List_Id;
6902 Conc_Typ_Stmts : List_Id;
6903 Concval : Node_Id;
6904 Dblock_Ent : Entity_Id;
6905 Decl : Node_Id;
6906 Decls : List_Id;
6907 Ecall : Node_Id;
6908 Ename : Node_Id;
6909 Enqueue_Call : Node_Id;
6910 Formals : List_Id;
6911 Hdle : List_Id;
6912 Handler_Stmt : Node_Id;
6913 Index : Node_Id;
6914 Lim_Typ_Stmts : List_Id;
6915 N_Orig : Node_Id;
6916 Obj : Entity_Id;
6917 Param : Node_Id;
6918 Params : List_Id;
6919 Pdef : Entity_Id;
6920 ProtE_Stmts : List_Id;
6921 ProtP_Stmts : List_Id;
6922 Stmt : Node_Id;
6923 Stmts : List_Id;
6924 TaskE_Stmts : List_Id;
6925 Tstats : List_Id;
6927 B : Entity_Id; -- Call status flag
6928 Bnn : Entity_Id; -- Communication block
6929 C : Entity_Id; -- Call kind
6930 K : Entity_Id; -- Tagged kind
6931 P : Entity_Id; -- Parameter block
6932 S : Entity_Id; -- Primitive operation slot
6933 T : Entity_Id; -- Additional status flag
6935 procedure Rewrite_Abortable_Part;
6936 -- If the trigger is a dispatching call, the expansion inserts multiple
6937 -- copies of the abortable part. This is both inefficient, and may lead
6938 -- to duplicate definitions that the back-end will reject, when the
6939 -- abortable part includes loops. This procedure rewrites the abortable
6940 -- part into a call to a generated procedure.
6942 ----------------------------
6943 -- Rewrite_Abortable_Part --
6944 ----------------------------
6946 procedure Rewrite_Abortable_Part is
6947 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6948 Decl : Node_Id;
6950 begin
6951 Decl :=
6952 Make_Subprogram_Body (Loc,
6953 Specification =>
6954 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6955 Declarations => New_List,
6956 Handled_Statement_Sequence =>
6957 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6958 Insert_Before (N, Decl);
6959 Analyze (Decl);
6961 -- Rewrite abortable part into a call to this procedure.
6963 Astats :=
6964 New_List (
6965 Make_Procedure_Call_Statement (Loc,
6966 Name => New_Occurrence_Of (Proc, Loc)));
6967 end Rewrite_Abortable_Part;
6969 begin
6970 Process_Statements_For_Controlled_Objects (Trig);
6971 Process_Statements_For_Controlled_Objects (Abrt);
6973 Ecall := Triggering_Statement (Trig);
6975 Ensure_Statement_Present (Sloc (Ecall), Trig);
6977 -- Retrieve Astats and Tstats now because the finalization machinery may
6978 -- wrap them in blocks.
6980 Astats := Statements (Abrt);
6981 Tstats := Statements (Trig);
6983 -- The arguments in the call may require dynamic allocation, and the
6984 -- call statement may have been transformed into a block. The block
6985 -- may contain additional declarations for internal entities, and the
6986 -- original call is found by sequential search.
6988 if Nkind (Ecall) = N_Block_Statement then
6989 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6990 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6991 N_Entry_Call_Statement)
6992 loop
6993 Next (Ecall);
6994 end loop;
6995 end if;
6997 -- This is either a dispatching call or a delay statement used as a
6998 -- trigger which was expanded into a procedure call.
7000 if Nkind (Ecall) = N_Procedure_Call_Statement then
7001 if Ada_Version >= Ada_2005
7002 and then
7003 (No (Original_Node (Ecall))
7004 or else not Nkind_In (Original_Node (Ecall),
7005 N_Delay_Relative_Statement,
7006 N_Delay_Until_Statement))
7007 then
7008 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
7010 Rewrite_Abortable_Part;
7011 Decls := New_List;
7012 Stmts := New_List;
7014 -- Call status flag processing, generate:
7015 -- B : Boolean := False;
7017 B := Build_B (Loc, Decls);
7019 -- Communication block processing, generate:
7020 -- Bnn : Communication_Block;
7022 Bnn := Make_Temporary (Loc, 'B');
7023 Append_To (Decls,
7024 Make_Object_Declaration (Loc,
7025 Defining_Identifier => Bnn,
7026 Object_Definition =>
7027 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
7029 -- Call kind processing, generate:
7030 -- C : Ada.Tags.Prim_Op_Kind;
7032 C := Build_C (Loc, Decls);
7034 -- Tagged kind processing, generate:
7035 -- K : Ada.Tags.Tagged_Kind :=
7036 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7038 -- Dummy communication block, generate:
7039 -- D : Dummy_Communication_Block;
7041 Append_To (Decls,
7042 Make_Object_Declaration (Loc,
7043 Defining_Identifier =>
7044 Make_Defining_Identifier (Loc, Name_uD),
7045 Object_Definition =>
7046 New_Occurrence_Of
7047 (RTE (RE_Dummy_Communication_Block), Loc)));
7049 K := Build_K (Loc, Decls, Obj);
7051 -- Parameter block processing
7053 Blk_Typ := Build_Parameter_Block
7054 (Loc, Actuals, Formals, Decls);
7055 P := Parameter_Block_Pack
7056 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7058 -- Dispatch table slot processing, generate:
7059 -- S : Integer;
7061 S := Build_S (Loc, Decls);
7063 -- Additional status flag processing, generate:
7064 -- Tnn : Boolean;
7066 T := Make_Temporary (Loc, 'T');
7067 Append_To (Decls,
7068 Make_Object_Declaration (Loc,
7069 Defining_Identifier => T,
7070 Object_Definition =>
7071 New_Occurrence_Of (Standard_Boolean, Loc)));
7073 ------------------------------
7074 -- Protected entry handling --
7075 ------------------------------
7077 -- Generate:
7078 -- Param1 := P.Param1;
7079 -- ...
7080 -- ParamN := P.ParamN;
7082 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7084 -- Generate:
7085 -- Bnn := Communication_Block (D);
7087 Prepend_To (Cleanup_Stmts,
7088 Make_Assignment_Statement (Loc,
7089 Name => New_Occurrence_Of (Bnn, Loc),
7090 Expression =>
7091 Make_Unchecked_Type_Conversion (Loc,
7092 Subtype_Mark =>
7093 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7094 Expression => Make_Identifier (Loc, Name_uD))));
7096 -- Generate:
7097 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7099 Prepend_To (Cleanup_Stmts,
7100 Make_Procedure_Call_Statement (Loc,
7101 Name =>
7102 New_Occurrence_Of
7103 (Find_Prim_Op
7104 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7105 Loc),
7106 Parameter_Associations =>
7107 New_List (
7108 New_Copy_Tree (Obj), -- <object>
7109 New_Occurrence_Of (S, Loc), -- S
7110 Make_Attribute_Reference (Loc, -- P'Address
7111 Prefix => New_Occurrence_Of (P, Loc),
7112 Attribute_Name => Name_Address),
7113 Make_Identifier (Loc, Name_uD), -- D
7114 New_Occurrence_Of (B, Loc)))); -- B
7116 -- Generate:
7117 -- if Enqueued (Bnn) then
7118 -- <abortable-statements>
7119 -- end if;
7121 Append_To (Cleanup_Stmts,
7122 Make_Implicit_If_Statement (N,
7123 Condition =>
7124 Make_Function_Call (Loc,
7125 Name =>
7126 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7127 Parameter_Associations =>
7128 New_List (New_Occurrence_Of (Bnn, Loc))),
7130 Then_Statements =>
7131 New_Copy_List_Tree (Astats)));
7133 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7134 -- will then generate a _clean for the communication block Bnn.
7136 -- Generate:
7137 -- declare
7138 -- procedure _clean is
7139 -- begin
7140 -- if Enqueued (Bnn) then
7141 -- Cancel_Protected_Entry_Call (Bnn);
7142 -- end if;
7143 -- end _clean;
7144 -- begin
7145 -- Cleanup_Stmts
7146 -- at end
7147 -- _clean;
7148 -- end;
7150 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7151 Cleanup_Block :=
7152 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7154 -- Wrap the cleanup block in an exception handling block
7156 -- Generate:
7157 -- begin
7158 -- Cleanup_Block
7159 -- exception
7160 -- when Abort_Signal => Abort_Undefer;
7161 -- end;
7163 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7164 ProtE_Stmts :=
7165 New_List (
7166 Make_Implicit_Label_Declaration (Loc,
7167 Defining_Identifier => Abort_Block_Ent),
7169 Build_Abort_Block
7170 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7172 -- Generate:
7173 -- if not Cancelled (Bnn) then
7174 -- <triggering-statements>
7175 -- end if;
7177 Append_To (ProtE_Stmts,
7178 Make_Implicit_If_Statement (N,
7179 Condition =>
7180 Make_Op_Not (Loc,
7181 Right_Opnd =>
7182 Make_Function_Call (Loc,
7183 Name =>
7184 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7185 Parameter_Associations =>
7186 New_List (New_Occurrence_Of (Bnn, Loc)))),
7188 Then_Statements =>
7189 New_Copy_List_Tree (Tstats)));
7191 -------------------------
7192 -- Task entry handling --
7193 -------------------------
7195 -- Generate:
7196 -- Param1 := P.Param1;
7197 -- ...
7198 -- ParamN := P.ParamN;
7200 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7202 -- Generate:
7203 -- Bnn := Communication_Block (D);
7205 Append_To (TaskE_Stmts,
7206 Make_Assignment_Statement (Loc,
7207 Name =>
7208 New_Occurrence_Of (Bnn, Loc),
7209 Expression =>
7210 Make_Unchecked_Type_Conversion (Loc,
7211 Subtype_Mark =>
7212 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7213 Expression => Make_Identifier (Loc, Name_uD))));
7215 -- Generate:
7216 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7218 Prepend_To (TaskE_Stmts,
7219 Make_Procedure_Call_Statement (Loc,
7220 Name =>
7221 New_Occurrence_Of (
7222 Find_Prim_Op (Etype (Etype (Obj)),
7223 Name_uDisp_Asynchronous_Select),
7224 Loc),
7226 Parameter_Associations =>
7227 New_List (
7228 New_Copy_Tree (Obj), -- <object>
7229 New_Occurrence_Of (S, Loc), -- S
7230 Make_Attribute_Reference (Loc, -- P'Address
7231 Prefix => New_Occurrence_Of (P, Loc),
7232 Attribute_Name => Name_Address),
7233 Make_Identifier (Loc, Name_uD), -- D
7234 New_Occurrence_Of (B, Loc)))); -- B
7236 -- Generate:
7237 -- Abort_Defer;
7239 Prepend_To (TaskE_Stmts,
7240 Make_Procedure_Call_Statement (Loc,
7241 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
7242 Parameter_Associations => No_List));
7244 -- Generate:
7245 -- Abort_Undefer;
7246 -- <abortable-statements>
7248 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7250 Prepend_To (Cleanup_Stmts,
7251 Make_Procedure_Call_Statement (Loc,
7252 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
7253 Parameter_Associations => No_List));
7255 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7256 -- will generate a _clean for the additional status flag.
7258 -- Generate:
7259 -- declare
7260 -- procedure _clean is
7261 -- begin
7262 -- Cancel_Task_Entry_Call (U);
7263 -- end _clean;
7264 -- begin
7265 -- Cleanup_Stmts
7266 -- at end
7267 -- _clean;
7268 -- end;
7270 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7271 Cleanup_Block :=
7272 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7274 -- Wrap the cleanup block in an exception handling block
7276 -- Generate:
7277 -- begin
7278 -- Cleanup_Block
7279 -- exception
7280 -- when Abort_Signal => Abort_Undefer;
7281 -- end;
7283 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7285 Append_To (TaskE_Stmts,
7286 Make_Implicit_Label_Declaration (Loc,
7287 Defining_Identifier => Abort_Block_Ent));
7289 Append_To (TaskE_Stmts,
7290 Build_Abort_Block
7291 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7293 -- Generate:
7294 -- if not T then
7295 -- <triggering-statements>
7296 -- end if;
7298 Append_To (TaskE_Stmts,
7299 Make_Implicit_If_Statement (N,
7300 Condition =>
7301 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7303 Then_Statements =>
7304 New_Copy_List_Tree (Tstats)));
7306 ----------------------------------
7307 -- Protected procedure handling --
7308 ----------------------------------
7310 -- Generate:
7311 -- <dispatching-call>;
7312 -- <triggering-statements>
7314 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7315 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7317 -- Generate:
7318 -- S := Ada.Tags.Get_Offset_Index
7319 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7321 Conc_Typ_Stmts :=
7322 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7324 -- Generate:
7325 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7327 Append_To (Conc_Typ_Stmts,
7328 Make_Procedure_Call_Statement (Loc,
7329 Name =>
7330 New_Occurrence_Of
7331 (Find_Prim_Op (Etype (Etype (Obj)),
7332 Name_uDisp_Get_Prim_Op_Kind),
7333 Loc),
7334 Parameter_Associations =>
7335 New_List (
7336 New_Copy_Tree (Obj),
7337 New_Occurrence_Of (S, Loc),
7338 New_Occurrence_Of (C, Loc))));
7340 -- Generate:
7341 -- if C = POK_Procedure_Entry then
7342 -- ProtE_Stmts
7343 -- elsif C = POK_Task_Entry then
7344 -- TaskE_Stmts
7345 -- else
7346 -- ProtP_Stmts
7347 -- end if;
7349 Append_To (Conc_Typ_Stmts,
7350 Make_Implicit_If_Statement (N,
7351 Condition =>
7352 Make_Op_Eq (Loc,
7353 Left_Opnd =>
7354 New_Occurrence_Of (C, Loc),
7355 Right_Opnd =>
7356 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7358 Then_Statements =>
7359 ProtE_Stmts,
7361 Elsif_Parts =>
7362 New_List (
7363 Make_Elsif_Part (Loc,
7364 Condition =>
7365 Make_Op_Eq (Loc,
7366 Left_Opnd =>
7367 New_Occurrence_Of (C, Loc),
7368 Right_Opnd =>
7369 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7371 Then_Statements =>
7372 TaskE_Stmts)),
7374 Else_Statements =>
7375 ProtP_Stmts));
7377 -- Generate:
7378 -- <dispatching-call>;
7379 -- <triggering-statements>
7381 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7382 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7384 -- Generate:
7385 -- if K = Ada.Tags.TK_Limited_Tagged
7386 -- or else K = Ada.Tags.TK_Tagged
7387 -- then
7388 -- Lim_Typ_Stmts
7389 -- else
7390 -- Conc_Typ_Stmts
7391 -- end if;
7393 Append_To (Stmts,
7394 Make_Implicit_If_Statement (N,
7395 Condition => Build_Dispatching_Tag_Check (K, N),
7396 Then_Statements => Lim_Typ_Stmts,
7397 Else_Statements => Conc_Typ_Stmts));
7399 Rewrite (N,
7400 Make_Block_Statement (Loc,
7401 Declarations =>
7402 Decls,
7403 Handled_Statement_Sequence =>
7404 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7406 Analyze (N);
7407 return;
7409 -- Delay triggering statement processing
7411 else
7412 -- Add a Delay_Block object to the parameter list of the delay
7413 -- procedure to form the parameter list of the Wait entry call.
7415 Dblock_Ent := Make_Temporary (Loc, 'D');
7417 Pdef := Entity (Name (Ecall));
7419 if Is_RTE (Pdef, RO_CA_Delay_For) then
7420 Enqueue_Call :=
7421 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7423 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7424 Enqueue_Call :=
7425 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7427 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7428 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7429 end if;
7431 Append_To (Parameter_Associations (Ecall),
7432 Make_Attribute_Reference (Loc,
7433 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7434 Attribute_Name => Name_Unchecked_Access));
7436 -- Create the inner block to protect the abortable part
7438 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7440 Prepend_To (Astats,
7441 Make_Procedure_Call_Statement (Loc,
7442 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc)));
7444 Abortable_Block :=
7445 Make_Block_Statement (Loc,
7446 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7447 Handled_Statement_Sequence =>
7448 Make_Handled_Sequence_Of_Statements (Loc,
7449 Statements => Astats),
7450 Has_Created_Identifier => True,
7451 Is_Asynchronous_Call_Block => True);
7453 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7455 Rewrite (Ecall,
7456 Make_Implicit_If_Statement (N,
7457 Condition =>
7458 Make_Function_Call (Loc,
7459 Name => Enqueue_Call,
7460 Parameter_Associations => Parameter_Associations (Ecall)),
7461 Then_Statements =>
7462 New_List (Make_Block_Statement (Loc,
7463 Handled_Statement_Sequence =>
7464 Make_Handled_Sequence_Of_Statements (Loc,
7465 Statements => New_List (
7466 Make_Implicit_Label_Declaration (Loc,
7467 Defining_Identifier => Blk_Ent,
7468 Label_Construct => Abortable_Block),
7469 Abortable_Block),
7470 Exception_Handlers => Hdle)))));
7472 Stmts := New_List (Ecall);
7474 -- Construct statement sequence for new block
7476 Append_To (Stmts,
7477 Make_Implicit_If_Statement (N,
7478 Condition =>
7479 Make_Function_Call (Loc,
7480 Name => New_Occurrence_Of (
7481 RTE (RE_Timed_Out), Loc),
7482 Parameter_Associations => New_List (
7483 Make_Attribute_Reference (Loc,
7484 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7485 Attribute_Name => Name_Unchecked_Access))),
7486 Then_Statements => Tstats));
7488 -- The result is the new block
7490 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7492 Rewrite (N,
7493 Make_Block_Statement (Loc,
7494 Declarations => New_List (
7495 Make_Object_Declaration (Loc,
7496 Defining_Identifier => Dblock_Ent,
7497 Aliased_Present => True,
7498 Object_Definition =>
7499 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7501 Handled_Statement_Sequence =>
7502 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7504 Analyze (N);
7505 return;
7506 end if;
7508 else
7509 N_Orig := N;
7510 end if;
7512 Extract_Entry (Ecall, Concval, Ename, Index);
7513 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7515 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7516 Decls := Declarations (Ecall);
7518 if Is_Protected_Type (Etype (Concval)) then
7520 -- Get the declarations of the block expanded from the entry call
7522 Decl := First (Decls);
7523 while Present (Decl)
7524 and then (Nkind (Decl) /= N_Object_Declaration
7525 or else not Is_RTE (Etype (Object_Definition (Decl)),
7526 RE_Communication_Block))
7527 loop
7528 Next (Decl);
7529 end loop;
7531 pragma Assert (Present (Decl));
7532 Cancel_Param := Defining_Identifier (Decl);
7534 -- Change the mode of the Protected_Entry_Call call
7536 -- Protected_Entry_Call (
7537 -- Object => po._object'Access,
7538 -- E => <entry index>;
7539 -- Uninterpreted_Data => P'Address;
7540 -- Mode => Asynchronous_Call;
7541 -- Block => Bnn);
7543 -- Skip assignments to temporaries created for in-out parameters
7545 -- This makes unwarranted assumptions about the shape of the expanded
7546 -- tree for the call, and should be cleaned up ???
7548 Stmt := First (Stmts);
7549 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7550 Next (Stmt);
7551 end loop;
7553 Call := Stmt;
7555 Param := First (Parameter_Associations (Call));
7556 while Present (Param)
7557 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7558 loop
7559 Next (Param);
7560 end loop;
7562 pragma Assert (Present (Param));
7563 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7564 Analyze (Param);
7566 -- Append an if statement to execute the abortable part
7568 -- Generate:
7569 -- if Enqueued (Bnn) then
7571 Append_To (Stmts,
7572 Make_Implicit_If_Statement (N,
7573 Condition =>
7574 Make_Function_Call (Loc,
7575 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7576 Parameter_Associations => New_List (
7577 New_Occurrence_Of (Cancel_Param, Loc))),
7578 Then_Statements => Astats));
7580 Abortable_Block :=
7581 Make_Block_Statement (Loc,
7582 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7583 Handled_Statement_Sequence =>
7584 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7585 Has_Created_Identifier => True,
7586 Is_Asynchronous_Call_Block => True);
7588 -- For the VM call Update_Exception instead of Abort_Undefer.
7589 -- See 4jexcept.ads for an explanation.
7591 if VM_Target = No_VM then
7592 if Exception_Mechanism = Back_End_Exceptions then
7594 -- Aborts are not deferred at beginning of exception handlers
7595 -- in ZCX.
7597 Handler_Stmt := Make_Null_Statement (Loc);
7599 else
7600 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7601 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc),
7602 Parameter_Associations => No_List);
7603 end if;
7604 else
7605 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7606 Name => New_Occurrence_Of (RTE (RE_Update_Exception), Loc),
7607 Parameter_Associations => New_List (
7608 Make_Function_Call (Loc,
7609 Name => New_Occurrence_Of
7610 (RTE (RE_Current_Target_Exception), Loc))));
7611 end if;
7613 Stmts := New_List (
7614 Make_Block_Statement (Loc,
7615 Handled_Statement_Sequence =>
7616 Make_Handled_Sequence_Of_Statements (Loc,
7617 Statements => New_List (
7618 Make_Implicit_Label_Declaration (Loc,
7619 Defining_Identifier => Blk_Ent,
7620 Label_Construct => Abortable_Block),
7621 Abortable_Block),
7623 -- exception
7625 Exception_Handlers => New_List (
7626 Make_Implicit_Exception_Handler (Loc,
7628 -- when Abort_Signal =>
7629 -- Abort_Undefer.all;
7631 Exception_Choices =>
7632 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7633 Statements => New_List (Handler_Stmt))))),
7635 -- if not Cancelled (Bnn) then
7636 -- triggered statements
7637 -- end if;
7639 Make_Implicit_If_Statement (N,
7640 Condition => Make_Op_Not (Loc,
7641 Right_Opnd =>
7642 Make_Function_Call (Loc,
7643 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7644 Parameter_Associations => New_List (
7645 New_Occurrence_Of (Cancel_Param, Loc)))),
7646 Then_Statements => Tstats));
7648 -- Asynchronous task entry call
7650 else
7651 if No (Decls) then
7652 Decls := New_List;
7653 end if;
7655 B := Make_Defining_Identifier (Loc, Name_uB);
7657 -- Insert declaration of B in declarations of existing block
7659 Prepend_To (Decls,
7660 Make_Object_Declaration (Loc,
7661 Defining_Identifier => B,
7662 Object_Definition =>
7663 New_Occurrence_Of (Standard_Boolean, Loc)));
7665 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7667 -- Insert declaration of C in declarations of existing block
7669 Prepend_To (Decls,
7670 Make_Object_Declaration (Loc,
7671 Defining_Identifier => Cancel_Param,
7672 Object_Definition =>
7673 New_Occurrence_Of (Standard_Boolean, Loc)));
7675 -- Remove and save the call to Call_Simple
7677 Stmt := First (Stmts);
7679 -- Skip assignments to temporaries created for in-out parameters.
7680 -- This makes unwarranted assumptions about the shape of the expanded
7681 -- tree for the call, and should be cleaned up ???
7683 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7684 Next (Stmt);
7685 end loop;
7687 Call := Stmt;
7689 -- Create the inner block to protect the abortable part
7691 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7693 Prepend_To (Astats,
7694 Make_Procedure_Call_Statement (Loc,
7695 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Loc)));
7697 Abortable_Block :=
7698 Make_Block_Statement (Loc,
7699 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7700 Handled_Statement_Sequence =>
7701 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7702 Has_Created_Identifier => True,
7703 Is_Asynchronous_Call_Block => True);
7705 Insert_After (Call,
7706 Make_Block_Statement (Loc,
7707 Handled_Statement_Sequence =>
7708 Make_Handled_Sequence_Of_Statements (Loc,
7709 Statements => New_List (
7710 Make_Implicit_Label_Declaration (Loc,
7711 Defining_Identifier => Blk_Ent,
7712 Label_Construct => Abortable_Block),
7713 Abortable_Block),
7714 Exception_Handlers => Hdle)));
7716 -- Create new call statement
7718 Params := Parameter_Associations (Call);
7720 Append_To (Params,
7721 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7722 Append_To (Params, New_Occurrence_Of (B, Loc));
7724 Rewrite (Call,
7725 Make_Procedure_Call_Statement (Loc,
7726 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7727 Parameter_Associations => Params));
7729 -- Construct statement sequence for new block
7731 Append_To (Stmts,
7732 Make_Implicit_If_Statement (N,
7733 Condition =>
7734 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7735 Then_Statements => Tstats));
7737 -- Protected the call against abort
7739 Prepend_To (Stmts,
7740 Make_Procedure_Call_Statement (Loc,
7741 Name => New_Occurrence_Of (RTE (RE_Abort_Defer), Loc),
7742 Parameter_Associations => Empty_List));
7743 end if;
7745 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7747 -- The result is the new block
7749 Rewrite (N_Orig,
7750 Make_Block_Statement (Loc,
7751 Declarations => Decls,
7752 Handled_Statement_Sequence =>
7753 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7755 Analyze (N_Orig);
7756 end Expand_N_Asynchronous_Select;
7758 -------------------------------------
7759 -- Expand_N_Conditional_Entry_Call --
7760 -------------------------------------
7762 -- The conditional task entry call is converted to a call to
7763 -- Task_Entry_Call:
7765 -- declare
7766 -- B : Boolean;
7767 -- P : parms := (parm, parm, parm);
7769 -- begin
7770 -- Task_Entry_Call
7771 -- (<acceptor-task>, -- Acceptor
7772 -- <entry-index>, -- E
7773 -- P'Address, -- Uninterpreted_Data
7774 -- Conditional_Call, -- Mode
7775 -- B); -- Rendezvous_Successful
7776 -- parm := P.param;
7777 -- parm := P.param;
7778 -- ...
7779 -- if B then
7780 -- normal-statements
7781 -- else
7782 -- else-statements
7783 -- end if;
7784 -- end;
7786 -- For a description of the use of P and the assignments after the call,
7787 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7788 -- conditional entry call has already been expanded (by the Expand_N_Entry
7789 -- _Call_Statement procedure) as follows:
7791 -- declare
7792 -- P : parms := (parm, parm, parm);
7793 -- begin
7794 -- ... info for in-out parameters
7795 -- Call_Simple (acceptor-task, entry-index, P'Address);
7796 -- parm := P.param;
7797 -- parm := P.param;
7798 -- ...
7799 -- end;
7801 -- so the task at hand is to convert the latter expansion into the former
7803 -- The conditional protected entry call is converted to a call to
7804 -- Protected_Entry_Call:
7806 -- declare
7807 -- P : parms := (parm, parm, parm);
7808 -- Bnn : Communications_Block;
7810 -- begin
7811 -- Protected_Entry_Call
7812 -- (po._object'Access, -- Object
7813 -- <entry index>, -- E
7814 -- P'Address, -- Uninterpreted_Data
7815 -- Conditional_Call, -- Mode
7816 -- Bnn); -- Block
7817 -- parm := P.param;
7818 -- parm := P.param;
7819 -- ...
7820 -- if Cancelled (Bnn) then
7821 -- else-statements
7822 -- else
7823 -- normal-statements
7824 -- end if;
7825 -- end;
7827 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7828 -- into:
7830 -- declare
7831 -- B : Boolean := False;
7832 -- C : Ada.Tags.Prim_Op_Kind;
7833 -- K : Ada.Tags.Tagged_Kind :=
7834 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7835 -- P : Parameters := (Param1 .. ParamN);
7836 -- S : Integer;
7838 -- begin
7839 -- if K = Ada.Tags.TK_Limited_Tagged
7840 -- or else K = Ada.Tags.TK_Tagged
7841 -- then
7842 -- <dispatching-call>;
7843 -- <triggering-statements>
7845 -- else
7846 -- S :=
7847 -- Ada.Tags.Get_Offset_Index
7848 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7850 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7852 -- if C = POK_Protected_Entry
7853 -- or else C = POK_Task_Entry
7854 -- then
7855 -- Param1 := P.Param1;
7856 -- ...
7857 -- ParamN := P.ParamN;
7858 -- end if;
7860 -- if B then
7861 -- if C = POK_Procedure
7862 -- or else C = POK_Protected_Procedure
7863 -- or else C = POK_Task_Procedure
7864 -- then
7865 -- <dispatching-call>;
7866 -- end if;
7868 -- <triggering-statements>
7869 -- else
7870 -- <else-statements>
7871 -- end if;
7872 -- end if;
7873 -- end;
7875 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7876 Loc : constant Source_Ptr := Sloc (N);
7877 Alt : constant Node_Id := Entry_Call_Alternative (N);
7878 Blk : Node_Id := Entry_Call_Statement (Alt);
7880 Actuals : List_Id;
7881 Blk_Typ : Entity_Id;
7882 Call : Node_Id;
7883 Call_Ent : Entity_Id;
7884 Conc_Typ_Stmts : List_Id;
7885 Decl : Node_Id;
7886 Decls : List_Id;
7887 Formals : List_Id;
7888 Lim_Typ_Stmts : List_Id;
7889 N_Stats : List_Id;
7890 Obj : Entity_Id;
7891 Param : Node_Id;
7892 Params : List_Id;
7893 Stmt : Node_Id;
7894 Stmts : List_Id;
7895 Transient_Blk : Node_Id;
7896 Unpack : List_Id;
7898 B : Entity_Id; -- Call status flag
7899 C : Entity_Id; -- Call kind
7900 K : Entity_Id; -- Tagged kind
7901 P : Entity_Id; -- Parameter block
7902 S : Entity_Id; -- Primitive operation slot
7904 begin
7905 Process_Statements_For_Controlled_Objects (N);
7907 if Ada_Version >= Ada_2005
7908 and then Nkind (Blk) = N_Procedure_Call_Statement
7909 then
7910 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7912 Decls := New_List;
7913 Stmts := New_List;
7915 -- Call status flag processing, generate:
7916 -- B : Boolean := False;
7918 B := Build_B (Loc, Decls);
7920 -- Call kind processing, generate:
7921 -- C : Ada.Tags.Prim_Op_Kind;
7923 C := Build_C (Loc, Decls);
7925 -- Tagged kind processing, generate:
7926 -- K : Ada.Tags.Tagged_Kind :=
7927 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7929 K := Build_K (Loc, Decls, Obj);
7931 -- Parameter block processing
7933 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7934 P := Parameter_Block_Pack
7935 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7937 -- Dispatch table slot processing, generate:
7938 -- S : Integer;
7940 S := Build_S (Loc, Decls);
7942 -- Generate:
7943 -- S := Ada.Tags.Get_Offset_Index
7944 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7946 Conc_Typ_Stmts :=
7947 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7949 -- Generate:
7950 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7952 Append_To (Conc_Typ_Stmts,
7953 Make_Procedure_Call_Statement (Loc,
7954 Name =>
7955 New_Occurrence_Of (
7956 Find_Prim_Op (Etype (Etype (Obj)),
7957 Name_uDisp_Conditional_Select),
7958 Loc),
7959 Parameter_Associations =>
7960 New_List (
7961 New_Copy_Tree (Obj), -- <object>
7962 New_Occurrence_Of (S, Loc), -- S
7963 Make_Attribute_Reference (Loc, -- P'Address
7964 Prefix => New_Occurrence_Of (P, Loc),
7965 Attribute_Name => Name_Address),
7966 New_Occurrence_Of (C, Loc), -- C
7967 New_Occurrence_Of (B, Loc)))); -- B
7969 -- Generate:
7970 -- if C = POK_Protected_Entry
7971 -- or else C = POK_Task_Entry
7972 -- then
7973 -- Param1 := P.Param1;
7974 -- ...
7975 -- ParamN := P.ParamN;
7976 -- end if;
7978 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7980 -- Generate the if statement only when the packed parameters need
7981 -- explicit assignments to their corresponding actuals.
7983 if Present (Unpack) then
7984 Append_To (Conc_Typ_Stmts,
7985 Make_Implicit_If_Statement (N,
7986 Condition =>
7987 Make_Or_Else (Loc,
7988 Left_Opnd =>
7989 Make_Op_Eq (Loc,
7990 Left_Opnd =>
7991 New_Occurrence_Of (C, Loc),
7992 Right_Opnd =>
7993 New_Occurrence_Of (RTE (
7994 RE_POK_Protected_Entry), Loc)),
7996 Right_Opnd =>
7997 Make_Op_Eq (Loc,
7998 Left_Opnd =>
7999 New_Occurrence_Of (C, Loc),
8000 Right_Opnd =>
8001 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
8003 Then_Statements => Unpack));
8004 end if;
8006 -- Generate:
8007 -- if B then
8008 -- if C = POK_Procedure
8009 -- or else C = POK_Protected_Procedure
8010 -- or else C = POK_Task_Procedure
8011 -- then
8012 -- <dispatching-call>
8013 -- end if;
8014 -- <normal-statements>
8015 -- else
8016 -- <else-statements>
8017 -- end if;
8019 N_Stats := New_Copy_List_Tree (Statements (Alt));
8021 Prepend_To (N_Stats,
8022 Make_Implicit_If_Statement (N,
8023 Condition =>
8024 Make_Or_Else (Loc,
8025 Left_Opnd =>
8026 Make_Op_Eq (Loc,
8027 Left_Opnd =>
8028 New_Occurrence_Of (C, Loc),
8029 Right_Opnd =>
8030 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
8032 Right_Opnd =>
8033 Make_Or_Else (Loc,
8034 Left_Opnd =>
8035 Make_Op_Eq (Loc,
8036 Left_Opnd =>
8037 New_Occurrence_Of (C, Loc),
8038 Right_Opnd =>
8039 New_Occurrence_Of (RTE (
8040 RE_POK_Protected_Procedure), Loc)),
8042 Right_Opnd =>
8043 Make_Op_Eq (Loc,
8044 Left_Opnd =>
8045 New_Occurrence_Of (C, Loc),
8046 Right_Opnd =>
8047 New_Occurrence_Of (RTE (
8048 RE_POK_Task_Procedure), Loc)))),
8050 Then_Statements =>
8051 New_List (Blk)));
8053 Append_To (Conc_Typ_Stmts,
8054 Make_Implicit_If_Statement (N,
8055 Condition => New_Occurrence_Of (B, Loc),
8056 Then_Statements => N_Stats,
8057 Else_Statements => Else_Statements (N)));
8059 -- Generate:
8060 -- <dispatching-call>;
8061 -- <triggering-statements>
8063 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8064 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8066 -- Generate:
8067 -- if K = Ada.Tags.TK_Limited_Tagged
8068 -- or else K = Ada.Tags.TK_Tagged
8069 -- then
8070 -- Lim_Typ_Stmts
8071 -- else
8072 -- Conc_Typ_Stmts
8073 -- end if;
8075 Append_To (Stmts,
8076 Make_Implicit_If_Statement (N,
8077 Condition => Build_Dispatching_Tag_Check (K, N),
8078 Then_Statements => Lim_Typ_Stmts,
8079 Else_Statements => Conc_Typ_Stmts));
8081 Rewrite (N,
8082 Make_Block_Statement (Loc,
8083 Declarations =>
8084 Decls,
8085 Handled_Statement_Sequence =>
8086 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8088 -- As described above, the entry alternative is transformed into a
8089 -- block that contains the gnulli call, and possibly assignment
8090 -- statements for in-out parameters. The gnulli call may itself be
8091 -- rewritten into a transient block if some unconstrained parameters
8092 -- require it. We need to retrieve the call to complete its parameter
8093 -- list.
8095 else
8096 Transient_Blk :=
8097 First_Real_Statement (Handled_Statement_Sequence (Blk));
8099 if Present (Transient_Blk)
8100 and then Nkind (Transient_Blk) = N_Block_Statement
8101 then
8102 Blk := Transient_Blk;
8103 end if;
8105 Stmts := Statements (Handled_Statement_Sequence (Blk));
8106 Stmt := First (Stmts);
8107 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8108 Next (Stmt);
8109 end loop;
8111 Call := Stmt;
8112 Params := Parameter_Associations (Call);
8114 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8116 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8118 Param := First (Params);
8119 while Present (Param)
8120 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8121 loop
8122 Next (Param);
8123 end loop;
8125 pragma Assert (Present (Param));
8126 Rewrite (Param,
8127 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8129 Analyze (Param);
8131 -- Find the Communication_Block parameter for the call to the
8132 -- Cancelled function.
8134 Decl := First (Declarations (Blk));
8135 while Present (Decl)
8136 and then not Is_RTE (Etype (Object_Definition (Decl)),
8137 RE_Communication_Block)
8138 loop
8139 Next (Decl);
8140 end loop;
8142 -- Add an if statement to execute the else part if the call
8143 -- does not succeed (as indicated by the Cancelled predicate).
8145 Append_To (Stmts,
8146 Make_Implicit_If_Statement (N,
8147 Condition => Make_Function_Call (Loc,
8148 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8149 Parameter_Associations => New_List (
8150 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8151 Then_Statements => Else_Statements (N),
8152 Else_Statements => Statements (Alt)));
8154 else
8155 B := Make_Defining_Identifier (Loc, Name_uB);
8157 -- Insert declaration of B in declarations of existing block
8159 if No (Declarations (Blk)) then
8160 Set_Declarations (Blk, New_List);
8161 end if;
8163 Prepend_To (Declarations (Blk),
8164 Make_Object_Declaration (Loc,
8165 Defining_Identifier => B,
8166 Object_Definition =>
8167 New_Occurrence_Of (Standard_Boolean, Loc)));
8169 -- Create new call statement
8171 Append_To (Params,
8172 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8173 Append_To (Params, New_Occurrence_Of (B, Loc));
8175 Rewrite (Call,
8176 Make_Procedure_Call_Statement (Loc,
8177 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8178 Parameter_Associations => Params));
8180 -- Construct statement sequence for new block
8182 Append_To (Stmts,
8183 Make_Implicit_If_Statement (N,
8184 Condition => New_Occurrence_Of (B, Loc),
8185 Then_Statements => Statements (Alt),
8186 Else_Statements => Else_Statements (N)));
8187 end if;
8189 -- The result is the new block
8191 Rewrite (N,
8192 Make_Block_Statement (Loc,
8193 Declarations => Declarations (Blk),
8194 Handled_Statement_Sequence =>
8195 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8196 end if;
8198 Analyze (N);
8199 end Expand_N_Conditional_Entry_Call;
8201 ---------------------------------------
8202 -- Expand_N_Delay_Relative_Statement --
8203 ---------------------------------------
8205 -- Delay statement is implemented as a procedure call to Delay_For
8206 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8207 -- simple delays imposed by the use of Protected Objects.
8209 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8210 Loc : constant Source_Ptr := Sloc (N);
8211 begin
8212 Rewrite (N,
8213 Make_Procedure_Call_Statement (Loc,
8214 Name => New_Occurrence_Of (RTE (RO_CA_Delay_For), Loc),
8215 Parameter_Associations => New_List (Expression (N))));
8216 Analyze (N);
8217 end Expand_N_Delay_Relative_Statement;
8219 ------------------------------------
8220 -- Expand_N_Delay_Until_Statement --
8221 ------------------------------------
8223 -- Delay Until statement is implemented as a procedure call to
8224 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8226 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8227 Loc : constant Source_Ptr := Sloc (N);
8228 Typ : Entity_Id;
8230 begin
8231 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8232 Typ := RTE (RO_CA_Delay_Until);
8233 else
8234 Typ := RTE (RO_RT_Delay_Until);
8235 end if;
8237 Rewrite (N,
8238 Make_Procedure_Call_Statement (Loc,
8239 Name => New_Occurrence_Of (Typ, Loc),
8240 Parameter_Associations => New_List (Expression (N))));
8242 Analyze (N);
8243 end Expand_N_Delay_Until_Statement;
8245 -------------------------
8246 -- Expand_N_Entry_Body --
8247 -------------------------
8249 procedure Expand_N_Entry_Body (N : Node_Id) is
8250 begin
8251 -- Associate discriminals with the next protected operation body to be
8252 -- expanded.
8254 if Present (Next_Protected_Operation (N)) then
8255 Set_Discriminals (Parent (Current_Scope));
8256 end if;
8257 end Expand_N_Entry_Body;
8259 -----------------------------------
8260 -- Expand_N_Entry_Call_Statement --
8261 -----------------------------------
8263 -- An entry call is expanded into GNARLI calls to implement a simple entry
8264 -- call (see Build_Simple_Entry_Call).
8266 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8267 Concval : Node_Id;
8268 Ename : Node_Id;
8269 Index : Node_Id;
8271 begin
8272 if No_Run_Time_Mode then
8273 Error_Msg_CRT ("entry call", N);
8274 return;
8275 end if;
8277 -- If this entry call is part of an asynchronous select, don't expand it
8278 -- here; it will be expanded with the select statement. Don't expand
8279 -- timed entry calls either, as they are translated into asynchronous
8280 -- entry calls.
8282 -- ??? This whole approach is questionable; it may be better to go back
8283 -- to allowing the expansion to take place and then attempting to fix it
8284 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8285 -- whether the expanded call is on a task or protected entry.
8287 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8288 or else N /= Triggering_Statement (Parent (N)))
8289 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8290 or else N /= Entry_Call_Statement (Parent (N))
8291 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8292 then
8293 Extract_Entry (N, Concval, Ename, Index);
8294 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8295 end if;
8296 end Expand_N_Entry_Call_Statement;
8298 --------------------------------
8299 -- Expand_N_Entry_Declaration --
8300 --------------------------------
8302 -- If there are parameters, then first, each of the formals is marked by
8303 -- setting Is_Entry_Formal. Next a record type is built which is used to
8304 -- hold the parameter values. The name of this record type is entryP where
8305 -- entry is the name of the entry, with an additional corresponding access
8306 -- type called entryPA. The record type has matching components for each
8307 -- formal (the component names are the same as the formal names). For
8308 -- elementary types, the component type matches the formal type. For
8309 -- composite types, an access type is declared (with the name formalA)
8310 -- which designates the formal type, and the type of the component is this
8311 -- access type. Finally the Entry_Component of each formal is set to
8312 -- reference the corresponding record component.
8314 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8315 Loc : constant Source_Ptr := Sloc (N);
8316 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8317 Components : List_Id;
8318 Formal : Node_Id;
8319 Ftype : Entity_Id;
8320 Last_Decl : Node_Id;
8321 Component : Entity_Id;
8322 Ctype : Entity_Id;
8323 Decl : Node_Id;
8324 Rec_Ent : Entity_Id;
8325 Acc_Ent : Entity_Id;
8327 begin
8328 Formal := First_Formal (Entry_Ent);
8329 Last_Decl := N;
8331 -- Most processing is done only if parameters are present
8333 if Present (Formal) then
8334 Components := New_List;
8336 -- Loop through formals
8338 while Present (Formal) loop
8339 Set_Is_Entry_Formal (Formal);
8340 Component :=
8341 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8342 Set_Entry_Component (Formal, Component);
8343 Set_Entry_Formal (Component, Formal);
8344 Ftype := Etype (Formal);
8346 -- Declare new access type and then append
8348 Ctype := Make_Temporary (Loc, 'A');
8350 Decl :=
8351 Make_Full_Type_Declaration (Loc,
8352 Defining_Identifier => Ctype,
8353 Type_Definition =>
8354 Make_Access_To_Object_Definition (Loc,
8355 All_Present => True,
8356 Constant_Present => Ekind (Formal) = E_In_Parameter,
8357 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8359 Insert_After (Last_Decl, Decl);
8360 Last_Decl := Decl;
8362 Append_To (Components,
8363 Make_Component_Declaration (Loc,
8364 Defining_Identifier => Component,
8365 Component_Definition =>
8366 Make_Component_Definition (Loc,
8367 Aliased_Present => False,
8368 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8370 Next_Formal_With_Extras (Formal);
8371 end loop;
8373 -- Create the Entry_Parameter_Record declaration
8375 Rec_Ent := Make_Temporary (Loc, 'P');
8377 Decl :=
8378 Make_Full_Type_Declaration (Loc,
8379 Defining_Identifier => Rec_Ent,
8380 Type_Definition =>
8381 Make_Record_Definition (Loc,
8382 Component_List =>
8383 Make_Component_List (Loc,
8384 Component_Items => Components)));
8386 Insert_After (Last_Decl, Decl);
8387 Last_Decl := Decl;
8389 -- Construct and link in the corresponding access type
8391 Acc_Ent := Make_Temporary (Loc, 'A');
8393 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8395 Decl :=
8396 Make_Full_Type_Declaration (Loc,
8397 Defining_Identifier => Acc_Ent,
8398 Type_Definition =>
8399 Make_Access_To_Object_Definition (Loc,
8400 All_Present => True,
8401 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8403 Insert_After (Last_Decl, Decl);
8404 end if;
8405 end Expand_N_Entry_Declaration;
8407 -----------------------------
8408 -- Expand_N_Protected_Body --
8409 -----------------------------
8411 -- Protected bodies are expanded to the completion of the subprograms
8412 -- created for the corresponding protected type. These are a protected and
8413 -- unprotected version of each protected subprogram in the object, a
8414 -- function to calculate each entry barrier, and a procedure to execute the
8415 -- sequence of statements of each protected entry body. For example, for
8416 -- protected type ptype:
8418 -- function entB
8419 -- (O : System.Address;
8420 -- E : Protected_Entry_Index)
8421 -- return Boolean
8422 -- is
8423 -- <discriminant renamings>
8424 -- <private object renamings>
8425 -- begin
8426 -- return <barrier expression>;
8427 -- end entB;
8429 -- procedure pprocN (_object : in out poV;...) is
8430 -- <discriminant renamings>
8431 -- <private object renamings>
8432 -- begin
8433 -- <sequence of statements>
8434 -- end pprocN;
8436 -- procedure pprocP (_object : in out poV;...) is
8437 -- procedure _clean is
8438 -- Pn : Boolean;
8439 -- begin
8440 -- ptypeS (_object, Pn);
8441 -- Unlock (_object._object'Access);
8442 -- Abort_Undefer.all;
8443 -- end _clean;
8445 -- begin
8446 -- Abort_Defer.all;
8447 -- Lock (_object._object'Access);
8448 -- pprocN (_object;...);
8449 -- at end
8450 -- _clean;
8451 -- end pproc;
8453 -- function pfuncN (_object : poV;...) return Return_Type is
8454 -- <discriminant renamings>
8455 -- <private object renamings>
8456 -- begin
8457 -- <sequence of statements>
8458 -- end pfuncN;
8460 -- function pfuncP (_object : poV) return Return_Type is
8461 -- procedure _clean is
8462 -- begin
8463 -- Unlock (_object._object'Access);
8464 -- Abort_Undefer.all;
8465 -- end _clean;
8467 -- begin
8468 -- Abort_Defer.all;
8469 -- Lock (_object._object'Access);
8470 -- return pfuncN (_object);
8472 -- at end
8473 -- _clean;
8474 -- end pfunc;
8476 -- procedure entE
8477 -- (O : System.Address;
8478 -- P : System.Address;
8479 -- E : Protected_Entry_Index)
8480 -- is
8481 -- <discriminant renamings>
8482 -- <private object renamings>
8483 -- type poVP is access poV;
8484 -- _Object : ptVP := ptVP!(O);
8486 -- begin
8487 -- begin
8488 -- <statement sequence>
8489 -- Complete_Entry_Body (_Object._Object);
8490 -- exception
8491 -- when all others =>
8492 -- Exceptional_Complete_Entry_Body (
8493 -- _Object._Object, Get_GNAT_Exception);
8494 -- end;
8495 -- end entE;
8497 -- The type poV is the record created for the protected type to hold
8498 -- the state of the protected object.
8500 procedure Expand_N_Protected_Body (N : Node_Id) is
8501 Loc : constant Source_Ptr := Sloc (N);
8502 Pid : constant Entity_Id := Corresponding_Spec (N);
8504 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8505 -- This flag indicates whether the lock free implementation is active
8507 Current_Node : Node_Id;
8508 Disp_Op_Body : Node_Id;
8509 New_Op_Body : Node_Id;
8510 Op_Body : Node_Id;
8511 Op_Id : Entity_Id;
8513 function Build_Dispatching_Subprogram_Body
8514 (N : Node_Id;
8515 Pid : Node_Id;
8516 Prot_Bod : Node_Id) return Node_Id;
8517 -- Build a dispatching version of the protected subprogram body. The
8518 -- newly generated subprogram contains a call to the original protected
8519 -- body. The following code is generated:
8521 -- function <protected-function-name> (Param1 .. ParamN) return
8522 -- <return-type> is
8523 -- begin
8524 -- return <protected-function-name>P (Param1 .. ParamN);
8525 -- end <protected-function-name>;
8527 -- or
8529 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8530 -- begin
8531 -- <protected-procedure-name>P (Param1 .. ParamN);
8532 -- end <protected-procedure-name>
8534 ---------------------------------------
8535 -- Build_Dispatching_Subprogram_Body --
8536 ---------------------------------------
8538 function Build_Dispatching_Subprogram_Body
8539 (N : Node_Id;
8540 Pid : Node_Id;
8541 Prot_Bod : Node_Id) return Node_Id
8543 Loc : constant Source_Ptr := Sloc (N);
8544 Actuals : List_Id;
8545 Formal : Node_Id;
8546 Spec : Node_Id;
8547 Stmts : List_Id;
8549 begin
8550 -- Generate a specification without a letter suffix in order to
8551 -- override an interface function or procedure.
8553 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8555 -- The formal parameters become the actuals of the protected function
8556 -- or procedure call.
8558 Actuals := New_List;
8559 Formal := First (Parameter_Specifications (Spec));
8560 while Present (Formal) loop
8561 Append_To (Actuals,
8562 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8563 Next (Formal);
8564 end loop;
8566 if Nkind (Spec) = N_Procedure_Specification then
8567 Stmts :=
8568 New_List (
8569 Make_Procedure_Call_Statement (Loc,
8570 Name =>
8571 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8572 Parameter_Associations => Actuals));
8574 else
8575 pragma Assert (Nkind (Spec) = N_Function_Specification);
8577 Stmts :=
8578 New_List (
8579 Make_Simple_Return_Statement (Loc,
8580 Expression =>
8581 Make_Function_Call (Loc,
8582 Name =>
8583 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8584 Parameter_Associations => Actuals)));
8585 end if;
8587 return
8588 Make_Subprogram_Body (Loc,
8589 Declarations => Empty_List,
8590 Specification => Spec,
8591 Handled_Statement_Sequence =>
8592 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8593 end Build_Dispatching_Subprogram_Body;
8595 -- Start of processing for Expand_N_Protected_Body
8597 begin
8598 if No_Run_Time_Mode then
8599 Error_Msg_CRT ("protected body", N);
8600 return;
8601 end if;
8603 -- This is the proper body corresponding to a stub. The declarations
8604 -- must be inserted at the point of the stub, which in turn is in the
8605 -- declarative part of the parent unit.
8607 if Nkind (Parent (N)) = N_Subunit then
8608 Current_Node := Corresponding_Stub (Parent (N));
8609 else
8610 Current_Node := N;
8611 end if;
8613 Op_Body := First (Declarations (N));
8615 -- The protected body is replaced with the bodies of its
8616 -- protected operations, and the declarations for internal objects
8617 -- that may have been created for entry family bounds.
8619 Rewrite (N, Make_Null_Statement (Sloc (N)));
8620 Analyze (N);
8622 while Present (Op_Body) loop
8623 case Nkind (Op_Body) is
8624 when N_Subprogram_Declaration =>
8625 null;
8627 when N_Subprogram_Body =>
8629 -- Do not create bodies for eliminated operations
8631 if not Is_Eliminated (Defining_Entity (Op_Body))
8632 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8633 then
8634 if Lock_Free_Active then
8635 New_Op_Body :=
8636 Build_Lock_Free_Unprotected_Subprogram_Body
8637 (Op_Body, Pid);
8638 else
8639 New_Op_Body :=
8640 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8641 end if;
8643 Insert_After (Current_Node, New_Op_Body);
8644 Current_Node := New_Op_Body;
8645 Analyze (New_Op_Body);
8647 -- Build the corresponding protected operation. It may
8648 -- appear that this is needed only if this is a visible
8649 -- operation of the type, or if it is an interrupt handler,
8650 -- and this was the strategy used previously in GNAT.
8652 -- However, the operation may be exported through a 'Access
8653 -- to an external caller. This is the common idiom in code
8654 -- that uses the Ada 2005 Timing_Events package. As a result
8655 -- we need to produce the protected body for both visible
8656 -- and private operations, as well as operations that only
8657 -- have a body in the source, and for which we create a
8658 -- declaration in the protected body itself.
8660 if Present (Corresponding_Spec (Op_Body)) then
8661 if Lock_Free_Active then
8662 New_Op_Body :=
8663 Build_Lock_Free_Protected_Subprogram_Body
8664 (Op_Body, Pid, Specification (New_Op_Body));
8665 else
8666 New_Op_Body :=
8667 Build_Protected_Subprogram_Body
8668 (Op_Body, Pid, Specification (New_Op_Body));
8669 end if;
8671 Insert_After (Current_Node, New_Op_Body);
8672 Analyze (New_Op_Body);
8674 Current_Node := New_Op_Body;
8676 -- Generate an overriding primitive operation body for
8677 -- this subprogram if the protected type implements an
8678 -- interface.
8680 if Ada_Version >= Ada_2005
8681 and then
8682 Present (Interfaces (Corresponding_Record_Type (Pid)))
8683 then
8684 Disp_Op_Body :=
8685 Build_Dispatching_Subprogram_Body
8686 (Op_Body, Pid, New_Op_Body);
8688 Insert_After (Current_Node, Disp_Op_Body);
8689 Analyze (Disp_Op_Body);
8691 Current_Node := Disp_Op_Body;
8692 end if;
8693 end if;
8694 end if;
8696 when N_Entry_Body =>
8697 Op_Id := Defining_Identifier (Op_Body);
8698 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8700 Insert_After (Current_Node, New_Op_Body);
8701 Current_Node := New_Op_Body;
8702 Analyze (New_Op_Body);
8704 when N_Implicit_Label_Declaration =>
8705 null;
8707 when N_Itype_Reference =>
8708 Insert_After (Current_Node, New_Copy (Op_Body));
8710 when N_Freeze_Entity =>
8711 New_Op_Body := New_Copy (Op_Body);
8713 if Present (Entity (Op_Body))
8714 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8715 then
8716 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8717 end if;
8719 Insert_After (Current_Node, New_Op_Body);
8720 Current_Node := New_Op_Body;
8721 Analyze (New_Op_Body);
8723 when N_Pragma =>
8724 New_Op_Body := New_Copy (Op_Body);
8725 Insert_After (Current_Node, New_Op_Body);
8726 Current_Node := New_Op_Body;
8727 Analyze (New_Op_Body);
8729 when N_Object_Declaration =>
8730 pragma Assert (not Comes_From_Source (Op_Body));
8731 New_Op_Body := New_Copy (Op_Body);
8732 Insert_After (Current_Node, New_Op_Body);
8733 Current_Node := New_Op_Body;
8734 Analyze (New_Op_Body);
8736 when others =>
8737 raise Program_Error;
8739 end case;
8741 Next (Op_Body);
8742 end loop;
8744 -- Finally, create the body of the function that maps an entry index
8745 -- into the corresponding body index, except when there is no entry, or
8746 -- in a Ravenscar-like profile.
8748 if Corresponding_Runtime_Package (Pid) =
8749 System_Tasking_Protected_Objects_Entries
8750 then
8751 New_Op_Body := Build_Find_Body_Index (Pid);
8752 Insert_After (Current_Node, New_Op_Body);
8753 Current_Node := New_Op_Body;
8754 Analyze (New_Op_Body);
8755 end if;
8757 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8758 -- protected body. At this point all wrapper specs have been created,
8759 -- frozen and included in the dispatch table for the protected type.
8761 if Ada_Version >= Ada_2005 then
8762 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8763 end if;
8764 end Expand_N_Protected_Body;
8766 -----------------------------------------
8767 -- Expand_N_Protected_Type_Declaration --
8768 -----------------------------------------
8770 -- First we create a corresponding record type declaration used to
8771 -- represent values of this protected type.
8772 -- The general form of this type declaration is
8774 -- type poV (discriminants) is record
8775 -- _Object : aliased <kind>Protection
8776 -- [(<entry count> [, <handler count>])];
8777 -- [entry_family : array (bounds) of Void;]
8778 -- <private data fields>
8779 -- end record;
8781 -- The discriminants are present only if the corresponding protected type
8782 -- has discriminants, and they exactly mirror the protected type
8783 -- discriminants. The private data fields similarly mirror the private
8784 -- declarations of the protected type.
8786 -- The Object field is always present. It contains RTS specific data used
8787 -- to control the protected object. It is declared as Aliased so that it
8788 -- can be passed as a pointer to the RTS. This allows the protected record
8789 -- to be referenced within RTS data structures. An appropriate Protection
8790 -- type and discriminant are generated.
8792 -- The Service field is present for protected objects with entries. It
8793 -- contains sufficient information to allow the entry service procedure for
8794 -- this object to be called when the object is not known till runtime.
8796 -- One entry_family component is present for each entry family in the
8797 -- task definition (see Expand_N_Task_Type_Declaration).
8799 -- When a protected object is declared, an instance of the protected type
8800 -- value record is created. The elaboration of this declaration creates the
8801 -- correct bounds for the entry families, and also evaluates the priority
8802 -- expression if needed. The initialization routine for the protected type
8803 -- itself then calls Initialize_Protection with appropriate parameters to
8804 -- initialize the value of the Task_Id field. Install_Handlers may be also
8805 -- called if a pragma Attach_Handler applies.
8807 -- Note: this record is passed to the subprograms created by the expansion
8808 -- of protected subprograms and entries. It is an in parameter to protected
8809 -- functions and an in out parameter to procedures and entry bodies. The
8810 -- Entity_Id for this created record type is placed in the
8811 -- Corresponding_Record_Type field of the associated protected type entity.
8813 -- Next we create a procedure specifications for protected subprograms and
8814 -- entry bodies. For each protected subprograms two subprograms are
8815 -- created, an unprotected and a protected version. The unprotected version
8816 -- is called from within other operations of the same protected object.
8818 -- We also build the call to register the procedure if a pragma
8819 -- Interrupt_Handler applies.
8821 -- A single subprogram is created to service all entry bodies; it has an
8822 -- additional boolean out parameter indicating that the previous entry call
8823 -- made by the current task was serviced immediately, i.e. not by proxy.
8824 -- The O parameter contains a pointer to a record object of the type
8825 -- described above. An untyped interface is used here to allow this
8826 -- procedure to be called in places where the type of the object to be
8827 -- serviced is not known. This must be done, for example, when a call that
8828 -- may have been requeued is cancelled; the corresponding object must be
8829 -- serviced, but which object that is not known till runtime.
8831 -- procedure ptypeS
8832 -- (O : System.Address; P : out Boolean);
8833 -- procedure pprocN (_object : in out poV);
8834 -- procedure pproc (_object : in out poV);
8835 -- function pfuncN (_object : poV);
8836 -- function pfunc (_object : poV);
8837 -- ...
8839 -- Note that this must come after the record type declaration, since
8840 -- the specs refer to this type.
8842 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8843 Loc : constant Source_Ptr := Sloc (N);
8844 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8846 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8847 -- This flag indicates whether the lock free implementation is active
8849 Pdef : constant Node_Id := Protected_Definition (N);
8850 -- This contains two lists; one for visible and one for private decls
8852 Rec_Decl : Node_Id;
8853 Cdecls : List_Id;
8854 Discr_Map : constant Elist_Id := New_Elmt_List;
8855 Priv : Node_Id;
8856 New_Priv : Node_Id;
8857 Comp : Node_Id;
8858 Comp_Id : Entity_Id;
8859 Sub : Node_Id;
8860 Current_Node : Node_Id := N;
8861 Entries_Aggr : Node_Id;
8862 Body_Id : Entity_Id;
8863 Body_Arr : Node_Id;
8864 E_Count : Int;
8865 Object_Comp : Node_Id;
8867 procedure Check_Inlining (Subp : Entity_Id);
8868 -- If the original operation has a pragma Inline, propagate the flag
8869 -- to the internal body, for possible inlining later on. The source
8870 -- operation is invisible to the back-end and is never actually called.
8872 function Discriminated_Size (Comp : Entity_Id) return Boolean;
8873 -- If a component size is not static then a warning will be emitted
8874 -- in Ravenscar or other restricted contexts. When a component is non-
8875 -- static because of a discriminant constraint we can specialize the
8876 -- warning by mentioning discriminants explicitly.
8878 procedure Expand_Entry_Declaration (Comp : Entity_Id);
8879 -- Create the subprograms for the barrier and for the body, and append
8880 -- then to Entry_Bodies_Array.
8882 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8883 -- When compiling under the Ravenscar profile, private components must
8884 -- have a static size, or else a protected object will require heap
8885 -- allocation, violating the corresponding restriction. It is preferable
8886 -- to make this check here, because it provides a better error message
8887 -- than the back-end, which refers to the object as a whole.
8889 procedure Register_Handler;
8890 -- For a protected operation that is an interrupt handler, add the
8891 -- freeze action that will register it as such.
8893 --------------------
8894 -- Check_Inlining --
8895 --------------------
8897 procedure Check_Inlining (Subp : Entity_Id) is
8898 begin
8899 if Is_Inlined (Subp) then
8900 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8901 Set_Is_Inlined (Subp, False);
8902 end if;
8903 end Check_Inlining;
8905 ------------------------
8906 -- Discriminated_Size --
8907 ------------------------
8909 function Discriminated_Size (Comp : Entity_Id) return Boolean is
8910 Typ : constant Entity_Id := Etype (Comp);
8911 Index : Node_Id;
8913 function Non_Static_Bound (Bound : Node_Id) return Boolean;
8914 -- Check whether the bound of an index is non-static and does denote
8915 -- a discriminant, in which case any protected object of the type
8916 -- will have a non-static size.
8918 ----------------------
8919 -- Non_Static_Bound --
8920 ----------------------
8922 function Non_Static_Bound (Bound : Node_Id) return Boolean is
8923 begin
8924 if Is_OK_Static_Expression (Bound) then
8925 return False;
8927 elsif Is_Entity_Name (Bound)
8928 and then Present (Discriminal_Link (Entity (Bound)))
8929 then
8930 return False;
8932 else
8933 return True;
8934 end if;
8935 end Non_Static_Bound;
8937 -- Start of processing for Discriminated_Size
8939 begin
8940 if not Is_Array_Type (Typ) then
8941 return False;
8942 end if;
8944 if Ekind (Typ) = E_Array_Subtype then
8945 Index := First_Index (Typ);
8946 while Present (Index) loop
8947 if Non_Static_Bound (Low_Bound (Index))
8948 or else Non_Static_Bound (High_Bound (Index))
8949 then
8950 return False;
8951 end if;
8953 Next_Index (Index);
8954 end loop;
8956 return True;
8957 end if;
8959 return False;
8960 end Discriminated_Size;
8962 ---------------------------
8963 -- Static_Component_Size --
8964 ---------------------------
8966 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8967 Typ : constant Entity_Id := Etype (Comp);
8968 C : Entity_Id;
8970 begin
8971 if Is_Scalar_Type (Typ) then
8972 return True;
8974 elsif Is_Array_Type (Typ) then
8975 return Compile_Time_Known_Bounds (Typ);
8977 elsif Is_Record_Type (Typ) then
8978 C := First_Component (Typ);
8979 while Present (C) loop
8980 if not Static_Component_Size (C) then
8981 return False;
8982 end if;
8984 Next_Component (C);
8985 end loop;
8987 return True;
8989 -- Any other type will be checked by the back-end
8991 else
8992 return True;
8993 end if;
8994 end Static_Component_Size;
8996 ------------------------------
8997 -- Expand_Entry_Declaration --
8998 ------------------------------
9000 procedure Expand_Entry_Declaration (Comp : Entity_Id) is
9001 Bdef : Entity_Id;
9002 Edef : Entity_Id;
9004 begin
9005 E_Count := E_Count + 1;
9006 Comp_Id := Defining_Identifier (Comp);
9008 Edef :=
9009 Make_Defining_Identifier (Loc,
9010 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9011 Sub :=
9012 Make_Subprogram_Declaration (Loc,
9013 Specification =>
9014 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9016 Insert_After (Current_Node, Sub);
9017 Analyze (Sub);
9019 -- Build wrapper procedure for pre/postconditions
9021 Build_PPC_Wrapper (Comp_Id, N);
9023 Set_Protected_Body_Subprogram
9024 (Defining_Identifier (Comp),
9025 Defining_Unit_Name (Specification (Sub)));
9027 Current_Node := Sub;
9029 Bdef :=
9030 Make_Defining_Identifier (Loc,
9031 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
9032 Sub :=
9033 Make_Subprogram_Declaration (Loc,
9034 Specification =>
9035 Build_Barrier_Function_Specification (Loc, Bdef));
9037 Insert_After (Current_Node, Sub);
9038 Analyze (Sub);
9039 Set_Protected_Body_Subprogram (Bdef, Bdef);
9040 Set_Barrier_Function (Comp_Id, Bdef);
9041 Set_Scope (Bdef, Scope (Comp_Id));
9042 Current_Node := Sub;
9044 -- Collect pointers to the protected subprogram and the barrier
9045 -- of the current entry, for insertion into Entry_Bodies_Array.
9047 Append_To (Expressions (Entries_Aggr),
9048 Make_Aggregate (Loc,
9049 Expressions => New_List (
9050 Make_Attribute_Reference (Loc,
9051 Prefix => New_Occurrence_Of (Bdef, Loc),
9052 Attribute_Name => Name_Unrestricted_Access),
9053 Make_Attribute_Reference (Loc,
9054 Prefix => New_Occurrence_Of (Edef, Loc),
9055 Attribute_Name => Name_Unrestricted_Access))));
9056 end Expand_Entry_Declaration;
9058 ----------------------
9059 -- Register_Handler --
9060 ----------------------
9062 procedure Register_Handler is
9064 -- All semantic checks already done in Sem_Prag
9066 Prot_Proc : constant Entity_Id :=
9067 Defining_Unit_Name (Specification (Current_Node));
9069 Proc_Address : constant Node_Id :=
9070 Make_Attribute_Reference (Loc,
9071 Prefix =>
9072 New_Occurrence_Of (Prot_Proc, Loc),
9073 Attribute_Name => Name_Address);
9075 RTS_Call : constant Entity_Id :=
9076 Make_Procedure_Call_Statement (Loc,
9077 Name =>
9078 New_Occurrence_Of
9079 (RTE (RE_Register_Interrupt_Handler), Loc),
9080 Parameter_Associations => New_List (Proc_Address));
9081 begin
9082 Append_Freeze_Action (Prot_Proc, RTS_Call);
9083 end Register_Handler;
9085 -- Start of processing for Expand_N_Protected_Type_Declaration
9087 begin
9088 if Present (Corresponding_Record_Type (Prot_Typ)) then
9089 return;
9090 else
9091 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9092 end if;
9094 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9096 Qualify_Entity_Names (N);
9098 -- If the type has discriminants, their occurrences in the declaration
9099 -- have been replaced by the corresponding discriminals. For components
9100 -- that are constrained by discriminants, their homologues in the
9101 -- corresponding record type must refer to the discriminants of that
9102 -- record, so we must apply a new renaming to subtypes_indications:
9104 -- protected discriminant => discriminal => record discriminant
9106 -- This replacement is not applied to default expressions, for which
9107 -- the discriminal is correct.
9109 if Has_Discriminants (Prot_Typ) then
9110 declare
9111 Disc : Entity_Id;
9112 Decl : Node_Id;
9114 begin
9115 Disc := First_Discriminant (Prot_Typ);
9116 Decl := First (Discriminant_Specifications (Rec_Decl));
9117 while Present (Disc) loop
9118 Append_Elmt (Discriminal (Disc), Discr_Map);
9119 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9120 Next_Discriminant (Disc);
9121 Next (Decl);
9122 end loop;
9123 end;
9124 end if;
9126 -- Fill in the component declarations
9128 -- Add components for entry families. For each entry family, create an
9129 -- anonymous type declaration with the same size, and analyze the type.
9131 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9133 pragma Assert (Present (Pdef));
9135 -- Add private field components
9137 if Present (Private_Declarations (Pdef)) then
9138 Priv := First (Private_Declarations (Pdef));
9139 while Present (Priv) loop
9140 if Nkind (Priv) = N_Component_Declaration then
9141 if not Static_Component_Size (Defining_Identifier (Priv)) then
9143 -- When compiling for a restricted profile, the private
9144 -- components must have a static size. If not, this is an
9145 -- error for a single protected declaration, and rates a
9146 -- warning on a protected type declaration.
9148 if not Comes_From_Source (Prot_Typ) then
9150 -- It's ok to be checking this restriction at expansion
9151 -- time, because this is only for the restricted profile,
9152 -- which is not subject to strict RM conformance, so it
9153 -- is OK to miss this check in -gnatc mode.
9155 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9157 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9158 if not Discriminated_Size (Defining_Identifier (Priv))
9159 then
9161 -- Any object of the type will be non-static.
9163 Error_Msg_N ("component has non-static size??", Priv);
9164 Error_Msg_NE
9165 ("\creation of protected object of type& will"
9166 & " violate restriction "
9167 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9168 else
9170 -- Object will be non-static if discriminants are.
9172 Error_Msg_NE
9173 ("creation of protected object of type& with "
9174 & "non-static discriminants will violate"
9175 & " restriction No_Implicit_Heap_Allocations??",
9176 Priv, Prot_Typ);
9177 end if;
9178 end if;
9179 end if;
9181 -- The component definition consists of a subtype indication,
9182 -- or (in Ada 2005) an access definition. Make a copy of the
9183 -- proper definition.
9185 declare
9186 Old_Comp : constant Node_Id := Component_Definition (Priv);
9187 Oent : constant Entity_Id := Defining_Identifier (Priv);
9188 New_Comp : Node_Id;
9189 Nent : constant Entity_Id :=
9190 Make_Defining_Identifier (Sloc (Oent),
9191 Chars => Chars (Oent));
9193 begin
9194 if Present (Subtype_Indication (Old_Comp)) then
9195 New_Comp :=
9196 Make_Component_Definition (Sloc (Oent),
9197 Aliased_Present => False,
9198 Subtype_Indication =>
9199 New_Copy_Tree (Subtype_Indication (Old_Comp),
9200 Discr_Map));
9201 else
9202 New_Comp :=
9203 Make_Component_Definition (Sloc (Oent),
9204 Aliased_Present => False,
9205 Access_Definition =>
9206 New_Copy_Tree (Access_Definition (Old_Comp),
9207 Discr_Map));
9208 end if;
9210 New_Priv :=
9211 Make_Component_Declaration (Loc,
9212 Defining_Identifier => Nent,
9213 Component_Definition => New_Comp,
9214 Expression => Expression (Priv));
9216 Set_Has_Per_Object_Constraint (Nent,
9217 Has_Per_Object_Constraint (Oent));
9219 Append_To (Cdecls, New_Priv);
9220 end;
9222 elsif Nkind (Priv) = N_Subprogram_Declaration then
9224 -- Make the unprotected version of the subprogram available
9225 -- for expansion of intra object calls. There is need for
9226 -- a protected version only if the subprogram is an interrupt
9227 -- handler, otherwise this operation can only be called from
9228 -- within the body.
9230 Sub :=
9231 Make_Subprogram_Declaration (Loc,
9232 Specification =>
9233 Build_Protected_Sub_Specification
9234 (Priv, Prot_Typ, Unprotected_Mode));
9236 Insert_After (Current_Node, Sub);
9237 Analyze (Sub);
9239 Set_Protected_Body_Subprogram
9240 (Defining_Unit_Name (Specification (Priv)),
9241 Defining_Unit_Name (Specification (Sub)));
9242 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9243 Current_Node := Sub;
9245 Sub :=
9246 Make_Subprogram_Declaration (Loc,
9247 Specification =>
9248 Build_Protected_Sub_Specification
9249 (Priv, Prot_Typ, Protected_Mode));
9251 Insert_After (Current_Node, Sub);
9252 Analyze (Sub);
9253 Current_Node := Sub;
9255 if Is_Interrupt_Handler
9256 (Defining_Unit_Name (Specification (Priv)))
9257 then
9258 if not Restricted_Profile then
9259 Register_Handler;
9260 end if;
9261 end if;
9262 end if;
9264 Next (Priv);
9265 end loop;
9266 end if;
9268 -- Except for the lock-free implementation, append the _Object field
9269 -- with the right type to the component list. We need to compute the
9270 -- number of entries, and in some cases the number of Attach_Handler
9271 -- pragmas.
9273 if not Lock_Free_Active then
9274 declare
9275 Ritem : Node_Id;
9276 Num_Attach_Handler : Int := 0;
9277 Protection_Subtype : Node_Id;
9278 Entry_Count_Expr : constant Node_Id :=
9279 Build_Entry_Count_Expression
9280 (Prot_Typ, Cdecls, Loc);
9282 begin
9283 if Has_Attach_Handler (Prot_Typ) then
9284 Ritem := First_Rep_Item (Prot_Typ);
9285 while Present (Ritem) loop
9286 if Nkind (Ritem) = N_Pragma
9287 and then Pragma_Name (Ritem) = Name_Attach_Handler
9288 then
9289 Num_Attach_Handler := Num_Attach_Handler + 1;
9290 end if;
9292 Next_Rep_Item (Ritem);
9293 end loop;
9294 end if;
9296 -- Determine the proper protection type. There are two special
9297 -- cases: 1) when the protected type has dynamic interrupt
9298 -- handlers, and 2) when it has static handlers and we use a
9299 -- restricted profile.
9301 if Has_Attach_Handler (Prot_Typ)
9302 and then not Restricted_Profile
9303 then
9304 Protection_Subtype :=
9305 Make_Subtype_Indication (Loc,
9306 Subtype_Mark =>
9307 New_Occurrence_Of
9308 (RTE (RE_Static_Interrupt_Protection), Loc),
9309 Constraint =>
9310 Make_Index_Or_Discriminant_Constraint (Loc,
9311 Constraints => New_List (
9312 Entry_Count_Expr,
9313 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9315 elsif Has_Interrupt_Handler (Prot_Typ)
9316 and then not Restriction_Active (No_Dynamic_Attachment)
9317 then
9318 Protection_Subtype :=
9319 Make_Subtype_Indication (Loc,
9320 Subtype_Mark =>
9321 New_Occurrence_Of
9322 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9323 Constraint =>
9324 Make_Index_Or_Discriminant_Constraint (Loc,
9325 Constraints => New_List (Entry_Count_Expr)));
9327 else
9328 case Corresponding_Runtime_Package (Prot_Typ) is
9329 when System_Tasking_Protected_Objects_Entries =>
9330 Protection_Subtype :=
9331 Make_Subtype_Indication (Loc,
9332 Subtype_Mark =>
9333 New_Occurrence_Of
9334 (RTE (RE_Protection_Entries), Loc),
9335 Constraint =>
9336 Make_Index_Or_Discriminant_Constraint (Loc,
9337 Constraints => New_List (Entry_Count_Expr)));
9339 when System_Tasking_Protected_Objects_Single_Entry =>
9340 Protection_Subtype :=
9341 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9343 when System_Tasking_Protected_Objects =>
9344 Protection_Subtype :=
9345 New_Occurrence_Of (RTE (RE_Protection), Loc);
9347 when others =>
9348 raise Program_Error;
9349 end case;
9350 end if;
9352 Object_Comp :=
9353 Make_Component_Declaration (Loc,
9354 Defining_Identifier =>
9355 Make_Defining_Identifier (Loc, Name_uObject),
9356 Component_Definition =>
9357 Make_Component_Definition (Loc,
9358 Aliased_Present => True,
9359 Subtype_Indication => Protection_Subtype));
9360 end;
9362 -- Put the _Object component after the private component so that it
9363 -- be finalized early as required by 9.4 (20)
9365 Append_To (Cdecls, Object_Comp);
9366 end if;
9368 Insert_After (Current_Node, Rec_Decl);
9369 Current_Node := Rec_Decl;
9371 -- Analyze the record declaration immediately after construction,
9372 -- because the initialization procedure is needed for single object
9373 -- declarations before the next entity is analyzed (the freeze call
9374 -- that generates this initialization procedure is found below).
9376 Analyze (Rec_Decl, Suppress => All_Checks);
9378 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9379 -- the corresponding record is frozen. If any wrappers are generated,
9380 -- Current_Node is updated accordingly.
9382 if Ada_Version >= Ada_2005 then
9383 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9384 end if;
9386 -- Collect pointers to entry bodies and their barriers, to be placed
9387 -- in the Entry_Bodies_Array for the type. For each entry/family we
9388 -- add an expression to the aggregate which is the initial value of
9389 -- this array. The array is declared after all protected subprograms.
9391 if Has_Entries (Prot_Typ) then
9392 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9393 else
9394 Entries_Aggr := Empty;
9395 end if;
9397 -- Build two new procedure specifications for each protected subprogram;
9398 -- one to call from outside the object and one to call from inside.
9399 -- Build a barrier function and an entry body action procedure
9400 -- specification for each protected entry. Initialize the entry body
9401 -- array. If subprogram is flagged as eliminated, do not generate any
9402 -- internal operations.
9404 E_Count := 0;
9405 Comp := First (Visible_Declarations (Pdef));
9406 while Present (Comp) loop
9407 if Nkind (Comp) = N_Subprogram_Declaration then
9408 Sub :=
9409 Make_Subprogram_Declaration (Loc,
9410 Specification =>
9411 Build_Protected_Sub_Specification
9412 (Comp, Prot_Typ, Unprotected_Mode));
9414 Insert_After (Current_Node, Sub);
9415 Analyze (Sub);
9417 Set_Protected_Body_Subprogram
9418 (Defining_Unit_Name (Specification (Comp)),
9419 Defining_Unit_Name (Specification (Sub)));
9420 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9422 -- Make the protected version of the subprogram available for
9423 -- expansion of external calls.
9425 Current_Node := Sub;
9427 Sub :=
9428 Make_Subprogram_Declaration (Loc,
9429 Specification =>
9430 Build_Protected_Sub_Specification
9431 (Comp, Prot_Typ, Protected_Mode));
9433 Insert_After (Current_Node, Sub);
9434 Analyze (Sub);
9436 Current_Node := Sub;
9438 -- Generate an overriding primitive operation specification for
9439 -- this subprogram if the protected type implements an interface.
9441 if Ada_Version >= Ada_2005
9442 and then
9443 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9444 then
9445 Sub :=
9446 Make_Subprogram_Declaration (Loc,
9447 Specification =>
9448 Build_Protected_Sub_Specification
9449 (Comp, Prot_Typ, Dispatching_Mode));
9451 Insert_After (Current_Node, Sub);
9452 Analyze (Sub);
9454 Current_Node := Sub;
9455 end if;
9457 -- If a pragma Interrupt_Handler applies, build and add a call to
9458 -- Register_Interrupt_Handler to the freezing actions of the
9459 -- protected version (Current_Node) of the subprogram:
9461 -- system.interrupts.register_interrupt_handler
9462 -- (prot_procP'address);
9464 if not Restricted_Profile
9465 and then Is_Interrupt_Handler
9466 (Defining_Unit_Name (Specification (Comp)))
9467 then
9468 Register_Handler;
9469 end if;
9471 elsif Nkind (Comp) = N_Entry_Declaration then
9473 Expand_Entry_Declaration (Comp);
9475 end if;
9477 Next (Comp);
9478 end loop;
9480 -- If there are some private entry declarations, expand it as if they
9481 -- were visible entries.
9483 if Present (Private_Declarations (Pdef)) then
9484 Comp := First (Private_Declarations (Pdef));
9485 while Present (Comp) loop
9486 if Nkind (Comp) = N_Entry_Declaration then
9487 Expand_Entry_Declaration (Comp);
9488 end if;
9490 Next (Comp);
9491 end loop;
9492 end if;
9494 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9495 -- all protected subprograms have been collected.
9497 if Has_Entries (Prot_Typ) then
9498 Body_Id :=
9499 Make_Defining_Identifier (Sloc (Prot_Typ),
9500 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9502 case Corresponding_Runtime_Package (Prot_Typ) is
9503 when System_Tasking_Protected_Objects_Entries =>
9504 Body_Arr := Make_Object_Declaration (Loc,
9505 Defining_Identifier => Body_Id,
9506 Aliased_Present => True,
9507 Object_Definition =>
9508 Make_Subtype_Indication (Loc,
9509 Subtype_Mark => New_Occurrence_Of (
9510 RTE (RE_Protected_Entry_Body_Array), Loc),
9511 Constraint =>
9512 Make_Index_Or_Discriminant_Constraint (Loc,
9513 Constraints => New_List (
9514 Make_Range (Loc,
9515 Make_Integer_Literal (Loc, 1),
9516 Make_Integer_Literal (Loc, E_Count))))),
9517 Expression => Entries_Aggr);
9519 when System_Tasking_Protected_Objects_Single_Entry =>
9520 Body_Arr := Make_Object_Declaration (Loc,
9521 Defining_Identifier => Body_Id,
9522 Aliased_Present => True,
9523 Object_Definition => New_Occurrence_Of
9524 (RTE (RE_Entry_Body), Loc),
9525 Expression => Remove_Head (Expressions (Entries_Aggr)));
9527 when others =>
9528 raise Program_Error;
9529 end case;
9531 -- A pointer to this array will be placed in the corresponding record
9532 -- by its initialization procedure so this needs to be analyzed here.
9534 Insert_After (Current_Node, Body_Arr);
9535 Current_Node := Body_Arr;
9536 Analyze (Body_Arr);
9538 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9540 -- Finally, build the function that maps an entry index into the
9541 -- corresponding body. A pointer to this function is placed in each
9542 -- object of the type. Except for a ravenscar-like profile (no abort,
9543 -- no entry queue, 1 entry)
9545 if Corresponding_Runtime_Package (Prot_Typ) =
9546 System_Tasking_Protected_Objects_Entries
9547 then
9548 Sub :=
9549 Make_Subprogram_Declaration (Loc,
9550 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9551 Insert_After (Current_Node, Sub);
9552 Analyze (Sub);
9553 end if;
9554 end if;
9555 end Expand_N_Protected_Type_Declaration;
9557 --------------------------------
9558 -- Expand_N_Requeue_Statement --
9559 --------------------------------
9561 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9562 -- operations, depending on the source and destination (task or protected
9563 -- object). A dispatching requeue statement is expanded into a call to the
9564 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9565 -- jump around the remainder of processing for the original entry and, if
9566 -- the destination is (different) protected object, to attempt to service
9567 -- it. The following illustrates the various cases:
9569 -- procedure entE
9570 -- (O : System.Address;
9571 -- P : System.Address;
9572 -- E : Protected_Entry_Index)
9573 -- is
9574 -- <discriminant renamings>
9575 -- <private object renamings>
9576 -- type poVP is access poV;
9577 -- _object : ptVP := ptVP!(O);
9579 -- begin
9580 -- begin
9581 -- <start of statement sequence for entry>
9583 -- -- Requeue from one protected entry body to another protected
9584 -- -- entry.
9586 -- Requeue_Protected_Entry (
9587 -- _object._object'Access,
9588 -- new._object'Access,
9589 -- E,
9590 -- Abort_Present);
9591 -- return;
9593 -- <some more of the statement sequence for entry>
9595 -- -- Requeue from an entry body to a task entry
9597 -- Requeue_Protected_To_Task_Entry (
9598 -- New._task_id,
9599 -- E,
9600 -- Abort_Present);
9601 -- return;
9603 -- <rest of statement sequence for entry>
9604 -- Complete_Entry_Body (_object._object);
9606 -- exception
9607 -- when all others =>
9608 -- Exceptional_Complete_Entry_Body (
9609 -- _object._object, Get_GNAT_Exception);
9610 -- end;
9611 -- end entE;
9613 -- Requeue of a task entry call to a task entry
9615 -- Accept_Call (E, Ann);
9616 -- <start of statement sequence for accept statement>
9617 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9618 -- goto Lnn;
9619 -- <rest of statement sequence for accept statement>
9620 -- <<Lnn>>
9621 -- Complete_Rendezvous;
9623 -- exception
9624 -- when all others =>
9625 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9627 -- Requeue of a task entry call to a protected entry
9629 -- Accept_Call (E, Ann);
9630 -- <start of statement sequence for accept statement>
9631 -- Requeue_Task_To_Protected_Entry (
9632 -- new._object'Access,
9633 -- E,
9634 -- Abort_Present);
9635 -- newS (new, Pnn);
9636 -- goto Lnn;
9637 -- <rest of statement sequence for accept statement>
9638 -- <<Lnn>>
9639 -- Complete_Rendezvous;
9641 -- exception
9642 -- when all others =>
9643 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9645 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9646 -- marked by pragma Implemented (XXX, By_Entry).
9648 -- The requeue is inside a protected entry:
9650 -- procedure entE
9651 -- (O : System.Address;
9652 -- P : System.Address;
9653 -- E : Protected_Entry_Index)
9654 -- is
9655 -- <discriminant renamings>
9656 -- <private object renamings>
9657 -- type poVP is access poV;
9658 -- _object : ptVP := ptVP!(O);
9660 -- begin
9661 -- begin
9662 -- <start of statement sequence for entry>
9664 -- _Disp_Requeue
9665 -- (<interface class-wide object>,
9666 -- True,
9667 -- _object'Address,
9668 -- Ada.Tags.Get_Offset_Index
9669 -- (Tag (_object),
9670 -- <interface dispatch table index of target entry>),
9671 -- Abort_Present);
9672 -- return;
9674 -- <rest of statement sequence for entry>
9675 -- Complete_Entry_Body (_object._object);
9677 -- exception
9678 -- when all others =>
9679 -- Exceptional_Complete_Entry_Body (
9680 -- _object._object, Get_GNAT_Exception);
9681 -- end;
9682 -- end entE;
9684 -- The requeue is inside a task entry:
9686 -- Accept_Call (E, Ann);
9687 -- <start of statement sequence for accept statement>
9688 -- _Disp_Requeue
9689 -- (<interface class-wide object>,
9690 -- False,
9691 -- null,
9692 -- Ada.Tags.Get_Offset_Index
9693 -- (Tag (_object),
9694 -- <interface dispatch table index of target entrt>),
9695 -- Abort_Present);
9696 -- newS (new, Pnn);
9697 -- goto Lnn;
9698 -- <rest of statement sequence for accept statement>
9699 -- <<Lnn>>
9700 -- Complete_Rendezvous;
9702 -- exception
9703 -- when all others =>
9704 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9706 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9707 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9708 -- statement is replaced by a dispatching call with actual parameters taken
9709 -- from the inner-most accept statement or entry body.
9711 -- Target.Primitive (Param1, ..., ParamN);
9713 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9714 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9715 -- at all.
9717 -- declare
9718 -- S : constant Offset_Index :=
9719 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9720 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9722 -- begin
9723 -- if C = POK_Protected_Entry
9724 -- or else C = POK_Task_Entry
9725 -- then
9726 -- <statements for dispatching requeue>
9728 -- elsif C = POK_Protected_Procedure then
9729 -- <dispatching call equivalent>
9731 -- else
9732 -- raise Program_Error;
9733 -- end if;
9734 -- end;
9736 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9737 Loc : constant Source_Ptr := Sloc (N);
9738 Conc_Typ : Entity_Id;
9739 Concval : Node_Id;
9740 Ename : Node_Id;
9741 Index : Node_Id;
9742 Old_Typ : Entity_Id;
9744 function Build_Dispatching_Call_Equivalent return Node_Id;
9745 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9746 -- the form Concval.Ename. It is statically known that Ename is allowed
9747 -- to be implemented by a protected procedure. Create a dispatching call
9748 -- equivalent of Concval.Ename taking the actual parameters from the
9749 -- inner-most accept statement or entry body.
9751 function Build_Dispatching_Requeue return Node_Id;
9752 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9753 -- the form Concval.Ename. It is statically known that Ename is allowed
9754 -- to be implemented by a protected or a task entry. Create a call to
9755 -- primitive _Disp_Requeue which handles the low-level actions.
9757 function Build_Dispatching_Requeue_To_Any return Node_Id;
9758 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9759 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9760 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9761 -- determines at runtime whether Ename denotes an entry or a procedure
9762 -- and perform the appropriate kind of dispatching select.
9764 function Build_Normal_Requeue return Node_Id;
9765 -- N denotes a non-dispatching requeue statement to either a task or a
9766 -- protected entry. Build the appropriate runtime call to perform the
9767 -- action.
9769 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9770 -- For a protected entry, create a return statement to skip the rest of
9771 -- the entry body. Otherwise, create a goto statement to skip the rest
9772 -- of a task accept statement. The lookup for the enclosing entry body
9773 -- or accept statement starts from Search.
9775 ---------------------------------------
9776 -- Build_Dispatching_Call_Equivalent --
9777 ---------------------------------------
9779 function Build_Dispatching_Call_Equivalent return Node_Id is
9780 Call_Ent : constant Entity_Id := Entity (Ename);
9781 Obj : constant Node_Id := Original_Node (Concval);
9782 Acc_Ent : Node_Id;
9783 Actuals : List_Id;
9784 Formal : Node_Id;
9785 Formals : List_Id;
9787 begin
9788 -- Climb the parent chain looking for the inner-most entry body or
9789 -- accept statement.
9791 Acc_Ent := N;
9792 while Present (Acc_Ent)
9793 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9794 N_Entry_Body)
9795 loop
9796 Acc_Ent := Parent (Acc_Ent);
9797 end loop;
9799 -- A requeue statement should be housed inside an entry body or an
9800 -- accept statement at some level. If this is not the case, then the
9801 -- tree is malformed.
9803 pragma Assert (Present (Acc_Ent));
9805 -- Recover the list of formal parameters
9807 if Nkind (Acc_Ent) = N_Entry_Body then
9808 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9809 end if;
9811 Formals := Parameter_Specifications (Acc_Ent);
9813 -- Create the actual parameters for the dispatching call. These are
9814 -- simply copies of the entry body or accept statement formals in the
9815 -- same order as they appear.
9817 Actuals := No_List;
9819 if Present (Formals) then
9820 Actuals := New_List;
9821 Formal := First (Formals);
9822 while Present (Formal) loop
9823 Append_To (Actuals,
9824 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9825 Next (Formal);
9826 end loop;
9827 end if;
9829 -- Generate:
9830 -- Obj.Call_Ent (Actuals);
9832 return
9833 Make_Procedure_Call_Statement (Loc,
9834 Name =>
9835 Make_Selected_Component (Loc,
9836 Prefix => Make_Identifier (Loc, Chars (Obj)),
9837 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9839 Parameter_Associations => Actuals);
9840 end Build_Dispatching_Call_Equivalent;
9842 -------------------------------
9843 -- Build_Dispatching_Requeue --
9844 -------------------------------
9846 function Build_Dispatching_Requeue return Node_Id is
9847 Params : constant List_Id := New_List;
9849 begin
9850 -- Process the "with abort" parameter
9852 Prepend_To (Params,
9853 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9855 -- Process the entry wrapper's position in the primary dispatch
9856 -- table parameter. Generate:
9858 -- Ada.Tags.Get_Entry_Index
9859 -- (T => To_Tag_Ptr (Obj'Address).all,
9860 -- Position =>
9861 -- Ada.Tags.Get_Offset_Index
9862 -- (Ada.Tags.Tag (Concval),
9863 -- <interface dispatch table position of Ename>));
9865 -- Note that Obj'Address is recursively expanded into a call to
9866 -- Base_Address (Obj).
9868 if Tagged_Type_Expansion then
9869 Prepend_To (Params,
9870 Make_Function_Call (Loc,
9871 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9872 Parameter_Associations => New_List (
9874 Make_Explicit_Dereference (Loc,
9875 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9876 Make_Attribute_Reference (Loc,
9877 Prefix => New_Copy_Tree (Concval),
9878 Attribute_Name => Name_Address))),
9880 Make_Function_Call (Loc,
9881 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9882 Parameter_Associations => New_List (
9883 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9884 Make_Integer_Literal (Loc,
9885 DT_Position (Entity (Ename))))))));
9887 -- VM targets
9889 else
9890 Prepend_To (Params,
9891 Make_Function_Call (Loc,
9892 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9893 Parameter_Associations => New_List (
9895 Make_Attribute_Reference (Loc,
9896 Prefix => Concval,
9897 Attribute_Name => Name_Tag),
9899 Make_Function_Call (Loc,
9900 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9902 Parameter_Associations => New_List (
9904 -- Obj_Tag
9906 Make_Attribute_Reference (Loc,
9907 Prefix => Concval,
9908 Attribute_Name => Name_Tag),
9910 -- Tag_Typ
9912 Make_Attribute_Reference (Loc,
9913 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9914 Attribute_Name => Name_Tag),
9916 -- Position
9918 Make_Integer_Literal (Loc,
9919 DT_Position (Entity (Ename))))))));
9920 end if;
9922 -- Specific actuals for protected to XXX requeue
9924 if Is_Protected_Type (Old_Typ) then
9925 Prepend_To (Params,
9926 Make_Attribute_Reference (Loc, -- _object'Address
9927 Prefix =>
9928 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9929 Attribute_Name => Name_Address));
9931 Prepend_To (Params, -- True
9932 New_Occurrence_Of (Standard_True, Loc));
9934 -- Specific actuals for task to XXX requeue
9936 else
9937 pragma Assert (Is_Task_Type (Old_Typ));
9939 Prepend_To (Params, -- null
9940 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
9942 Prepend_To (Params, -- False
9943 New_Occurrence_Of (Standard_False, Loc));
9944 end if;
9946 -- Add the object parameter
9948 Prepend_To (Params, New_Copy_Tree (Concval));
9950 -- Generate:
9951 -- _Disp_Requeue (<Params>);
9953 -- Find entity for Disp_Requeue operation, which belongs to
9954 -- the type and may not be directly visible.
9956 declare
9957 Elmt : Elmt_Id;
9958 Op : Entity_Id;
9960 begin
9961 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9962 while Present (Elmt) loop
9963 Op := Node (Elmt);
9964 exit when Chars (Op) = Name_uDisp_Requeue;
9965 Next_Elmt (Elmt);
9966 end loop;
9968 return
9969 Make_Procedure_Call_Statement (Loc,
9970 Name => New_Occurrence_Of (Op, Loc),
9971 Parameter_Associations => Params);
9972 end;
9973 end Build_Dispatching_Requeue;
9975 --------------------------------------
9976 -- Build_Dispatching_Requeue_To_Any --
9977 --------------------------------------
9979 function Build_Dispatching_Requeue_To_Any return Node_Id is
9980 Call_Ent : constant Entity_Id := Entity (Ename);
9981 Obj : constant Node_Id := Original_Node (Concval);
9982 Skip : constant Node_Id := Build_Skip_Statement (N);
9983 C : Entity_Id;
9984 Decls : List_Id;
9985 S : Entity_Id;
9986 Stmts : List_Id;
9988 begin
9989 Decls := New_List;
9990 Stmts := New_List;
9992 -- Dispatch table slot processing, generate:
9993 -- S : Integer;
9995 S := Build_S (Loc, Decls);
9997 -- Call kind processing, generate:
9998 -- C : Ada.Tags.Prim_Op_Kind;
10000 C := Build_C (Loc, Decls);
10002 -- Generate:
10003 -- S := Ada.Tags.Get_Offset_Index
10004 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10006 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10008 -- Generate:
10009 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10011 Append_To (Stmts,
10012 Make_Procedure_Call_Statement (Loc,
10013 Name =>
10014 New_Occurrence_Of (
10015 Find_Prim_Op (Etype (Etype (Obj)),
10016 Name_uDisp_Get_Prim_Op_Kind),
10017 Loc),
10018 Parameter_Associations => New_List (
10019 New_Copy_Tree (Obj),
10020 New_Occurrence_Of (S, Loc),
10021 New_Occurrence_Of (C, Loc))));
10023 Append_To (Stmts,
10025 -- if C = POK_Protected_Entry
10026 -- or else C = POK_Task_Entry
10027 -- then
10029 Make_Implicit_If_Statement (N,
10030 Condition =>
10031 Make_Op_Or (Loc,
10032 Left_Opnd =>
10033 Make_Op_Eq (Loc,
10034 Left_Opnd =>
10035 New_Occurrence_Of (C, Loc),
10036 Right_Opnd =>
10037 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10039 Right_Opnd =>
10040 Make_Op_Eq (Loc,
10041 Left_Opnd =>
10042 New_Occurrence_Of (C, Loc),
10043 Right_Opnd =>
10044 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10046 -- Dispatching requeue equivalent
10048 Then_Statements => New_List (
10049 Build_Dispatching_Requeue,
10050 Skip),
10052 -- elsif C = POK_Protected_Procedure then
10054 Elsif_Parts => New_List (
10055 Make_Elsif_Part (Loc,
10056 Condition =>
10057 Make_Op_Eq (Loc,
10058 Left_Opnd =>
10059 New_Occurrence_Of (C, Loc),
10060 Right_Opnd =>
10061 New_Occurrence_Of (
10062 RTE (RE_POK_Protected_Procedure), Loc)),
10064 -- Dispatching call equivalent
10066 Then_Statements => New_List (
10067 Build_Dispatching_Call_Equivalent))),
10069 -- else
10070 -- raise Program_Error;
10071 -- end if;
10073 Else_Statements => New_List (
10074 Make_Raise_Program_Error (Loc,
10075 Reason => PE_Explicit_Raise))));
10077 -- Wrap everything into a block
10079 return
10080 Make_Block_Statement (Loc,
10081 Declarations => Decls,
10082 Handled_Statement_Sequence =>
10083 Make_Handled_Sequence_Of_Statements (Loc,
10084 Statements => Stmts));
10085 end Build_Dispatching_Requeue_To_Any;
10087 --------------------------
10088 -- Build_Normal_Requeue --
10089 --------------------------
10091 function Build_Normal_Requeue return Node_Id is
10092 Params : constant List_Id := New_List;
10093 Param : Node_Id;
10094 RT_Call : Node_Id;
10096 begin
10097 -- Process the "with abort" parameter
10099 Prepend_To (Params,
10100 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10102 -- Add the index expression to the parameters. It is common among all
10103 -- four cases.
10105 Prepend_To (Params,
10106 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10108 if Is_Protected_Type (Old_Typ) then
10109 declare
10110 Self_Param : Node_Id;
10112 begin
10113 Self_Param :=
10114 Make_Attribute_Reference (Loc,
10115 Prefix =>
10116 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10117 Attribute_Name =>
10118 Name_Unchecked_Access);
10120 -- Protected to protected requeue
10122 if Is_Protected_Type (Conc_Typ) then
10123 RT_Call :=
10124 New_Occurrence_Of (
10125 RTE (RE_Requeue_Protected_Entry), Loc);
10127 Param :=
10128 Make_Attribute_Reference (Loc,
10129 Prefix =>
10130 Concurrent_Ref (Concval),
10131 Attribute_Name =>
10132 Name_Unchecked_Access);
10134 -- Protected to task requeue
10136 else pragma Assert (Is_Task_Type (Conc_Typ));
10137 RT_Call :=
10138 New_Occurrence_Of (
10139 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10141 Param := Concurrent_Ref (Concval);
10142 end if;
10144 Prepend_To (Params, Param);
10145 Prepend_To (Params, Self_Param);
10146 end;
10148 else pragma Assert (Is_Task_Type (Old_Typ));
10150 -- Task to protected requeue
10152 if Is_Protected_Type (Conc_Typ) then
10153 RT_Call :=
10154 New_Occurrence_Of (
10155 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10157 Param :=
10158 Make_Attribute_Reference (Loc,
10159 Prefix =>
10160 Concurrent_Ref (Concval),
10161 Attribute_Name =>
10162 Name_Unchecked_Access);
10164 -- Task to task requeue
10166 else pragma Assert (Is_Task_Type (Conc_Typ));
10167 RT_Call :=
10168 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10170 Param := Concurrent_Ref (Concval);
10171 end if;
10173 Prepend_To (Params, Param);
10174 end if;
10176 return
10177 Make_Procedure_Call_Statement (Loc,
10178 Name => RT_Call,
10179 Parameter_Associations => Params);
10180 end Build_Normal_Requeue;
10182 --------------------------
10183 -- Build_Skip_Statement --
10184 --------------------------
10186 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10187 Skip_Stmt : Node_Id;
10189 begin
10190 -- Build a return statement to skip the rest of the entire body
10192 if Is_Protected_Type (Old_Typ) then
10193 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10195 -- If the requeue is within a task, find the end label of the
10196 -- enclosing accept statement and create a goto statement to it.
10198 else
10199 declare
10200 Acc : Node_Id;
10201 Label : Node_Id;
10203 begin
10204 -- Climb the parent chain looking for the enclosing accept
10205 -- statement.
10207 Acc := Parent (Search);
10208 while Present (Acc)
10209 and then Nkind (Acc) /= N_Accept_Statement
10210 loop
10211 Acc := Parent (Acc);
10212 end loop;
10214 -- The last statement is the second label used for completing
10215 -- the rendezvous the usual way. The label we are looking for
10216 -- is right before it.
10218 Label :=
10219 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10221 pragma Assert (Nkind (Label) = N_Label);
10223 -- Generate a goto statement to skip the rest of the accept
10225 Skip_Stmt :=
10226 Make_Goto_Statement (Loc,
10227 Name =>
10228 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10229 end;
10230 end if;
10232 Set_Analyzed (Skip_Stmt);
10234 return Skip_Stmt;
10235 end Build_Skip_Statement;
10237 -- Start of processing for Expand_N_Requeue_Statement
10239 begin
10240 -- Extract the components of the entry call
10242 Extract_Entry (N, Concval, Ename, Index);
10243 Conc_Typ := Etype (Concval);
10245 -- If the prefix is an access to class-wide type, dereference to get
10246 -- object and entry type.
10248 if Is_Access_Type (Conc_Typ) then
10249 Conc_Typ := Designated_Type (Conc_Typ);
10250 Rewrite (Concval,
10251 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10252 Analyze_And_Resolve (Concval, Conc_Typ);
10253 end if;
10255 -- Examine the scope stack in order to find nearest enclosing protected
10256 -- or task type. This will constitute our invocation source.
10258 Old_Typ := Current_Scope;
10259 while Present (Old_Typ)
10260 and then not Is_Protected_Type (Old_Typ)
10261 and then not Is_Task_Type (Old_Typ)
10262 loop
10263 Old_Typ := Scope (Old_Typ);
10264 end loop;
10266 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10267 -- Concval.Ename where the type of Concval is class-wide concurrent
10268 -- interface.
10270 if Ada_Version >= Ada_2012
10271 and then Present (Concval)
10272 and then Is_Class_Wide_Type (Conc_Typ)
10273 and then Is_Concurrent_Interface (Conc_Typ)
10274 then
10275 declare
10276 Has_Impl : Boolean := False;
10277 Impl_Kind : Name_Id := No_Name;
10279 begin
10280 -- Check whether the Ename is flagged by pragma Implemented
10282 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10283 Has_Impl := True;
10284 Impl_Kind := Implementation_Kind (Entity (Ename));
10285 end if;
10287 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10288 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10290 if Has_Impl and then Impl_Kind = Name_By_Entry then
10291 Rewrite (N, Build_Dispatching_Requeue);
10292 Analyze (N);
10293 Insert_After (N, Build_Skip_Statement (N));
10295 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10296 -- a protected procedure. In this case the requeue is transformed
10297 -- into a dispatching call.
10299 elsif Has_Impl
10300 and then Impl_Kind = Name_By_Protected_Procedure
10301 then
10302 Rewrite (N, Build_Dispatching_Call_Equivalent);
10303 Analyze (N);
10305 -- The procedure_or_entry_NAME's implementation kind is either
10306 -- By_Any, Optional, or pragma Implemented was not applied at all.
10307 -- In this case a runtime test determines whether Ename denotes an
10308 -- entry or a protected procedure and performs the appropriate
10309 -- call.
10311 else
10312 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10313 Analyze (N);
10314 end if;
10315 end;
10317 -- Processing for regular (non-dispatching) requeues
10319 else
10320 Rewrite (N, Build_Normal_Requeue);
10321 Analyze (N);
10322 Insert_After (N, Build_Skip_Statement (N));
10323 end if;
10324 end Expand_N_Requeue_Statement;
10326 -------------------------------
10327 -- Expand_N_Selective_Accept --
10328 -------------------------------
10330 procedure Expand_N_Selective_Accept (N : Node_Id) is
10331 Loc : constant Source_Ptr := Sloc (N);
10332 Alts : constant List_Id := Select_Alternatives (N);
10334 -- Note: in the below declarations a lot of new lists are allocated
10335 -- unconditionally which may well not end up being used. That's not
10336 -- a good idea since it wastes space gratuitously ???
10338 Accept_Case : List_Id;
10339 Accept_List : constant List_Id := New_List;
10341 Alt : Node_Id;
10342 Alt_List : constant List_Id := New_List;
10343 Alt_Stats : List_Id;
10344 Ann : Entity_Id := Empty;
10346 Check_Guard : Boolean := True;
10348 Decls : constant List_Id := New_List;
10349 Stats : constant List_Id := New_List;
10350 Body_List : constant List_Id := New_List;
10351 Trailing_List : constant List_Id := New_List;
10353 Choices : List_Id;
10354 Else_Present : Boolean := False;
10355 Terminate_Alt : Node_Id := Empty;
10356 Select_Mode : Node_Id;
10358 Delay_Case : List_Id;
10359 Delay_Count : Integer := 0;
10360 Delay_Val : Entity_Id;
10361 Delay_Index : Entity_Id;
10362 Delay_Min : Entity_Id;
10363 Delay_Num : Int := 1;
10364 Delay_Alt_List : List_Id := New_List;
10365 Delay_List : constant List_Id := New_List;
10366 D : Entity_Id;
10367 M : Entity_Id;
10369 First_Delay : Boolean := True;
10370 Guard_Open : Entity_Id;
10372 End_Lab : Node_Id;
10373 Index : Int := 1;
10374 Lab : Node_Id;
10375 Num_Alts : Int;
10376 Num_Accept : Nat := 0;
10377 Proc : Node_Id;
10378 Time_Type : Entity_Id;
10379 Select_Call : Node_Id;
10381 Qnam : constant Entity_Id :=
10382 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10384 Xnam : constant Entity_Id :=
10385 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10387 -----------------------
10388 -- Local subprograms --
10389 -----------------------
10391 function Accept_Or_Raise return List_Id;
10392 -- For the rare case where delay alternatives all have guards, and
10393 -- all of them are closed, it is still possible that there were open
10394 -- accept alternatives with no callers. We must reexamine the
10395 -- Accept_List, and execute a selective wait with no else if some
10396 -- accept is open. If none, we raise program_error.
10398 procedure Add_Accept (Alt : Node_Id);
10399 -- Process a single accept statement in a select alternative. Build
10400 -- procedure for body of accept, and add entry to dispatch table with
10401 -- expression for guard, in preparation for call to run time select.
10403 function Make_And_Declare_Label (Num : Int) return Node_Id;
10404 -- Manufacture a label using Num as a serial number and declare it.
10405 -- The declaration is appended to Decls. The label marks the trailing
10406 -- statements of an accept or delay alternative.
10408 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10409 -- Build call to Selective_Wait runtime routine
10411 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10412 -- Add code to compare value of delay with previous values, and
10413 -- generate case entry for trailing statements.
10415 procedure Process_Accept_Alternative
10416 (Alt : Node_Id;
10417 Index : Int;
10418 Proc : Node_Id);
10419 -- Add code to call corresponding procedure, and branch to
10420 -- trailing statements, if any.
10422 ---------------------
10423 -- Accept_Or_Raise --
10424 ---------------------
10426 function Accept_Or_Raise return List_Id is
10427 Cond : Node_Id;
10428 Stats : List_Id;
10429 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10431 begin
10432 -- We generate the following:
10434 -- for J in q'range loop
10435 -- if q(J).S /=null_task_entry then
10436 -- selective_wait (simple_mode,...);
10437 -- done := True;
10438 -- exit;
10439 -- end if;
10440 -- end loop;
10442 -- if no rendez_vous then
10443 -- raise program_error;
10444 -- end if;
10446 -- Note that the code needs to know that the selector name
10447 -- in an Accept_Alternative is named S.
10449 Cond := Make_Op_Ne (Loc,
10450 Left_Opnd =>
10451 Make_Selected_Component (Loc,
10452 Prefix =>
10453 Make_Indexed_Component (Loc,
10454 Prefix => New_Occurrence_Of (Qnam, Loc),
10455 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10456 Selector_Name => Make_Identifier (Loc, Name_S)),
10457 Right_Opnd =>
10458 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10460 Stats := New_List (
10461 Make_Implicit_Loop_Statement (N,
10462 Iteration_Scheme =>
10463 Make_Iteration_Scheme (Loc,
10464 Loop_Parameter_Specification =>
10465 Make_Loop_Parameter_Specification (Loc,
10466 Defining_Identifier => J,
10467 Discrete_Subtype_Definition =>
10468 Make_Attribute_Reference (Loc,
10469 Prefix => New_Occurrence_Of (Qnam, Loc),
10470 Attribute_Name => Name_Range,
10471 Expressions => New_List (
10472 Make_Integer_Literal (Loc, 1))))),
10474 Statements => New_List (
10475 Make_Implicit_If_Statement (N,
10476 Condition => Cond,
10477 Then_Statements => New_List (
10478 Make_Select_Call (
10479 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10480 Make_Exit_Statement (Loc))))));
10482 Append_To (Stats,
10483 Make_Raise_Program_Error (Loc,
10484 Condition => Make_Op_Eq (Loc,
10485 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10486 Right_Opnd =>
10487 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10488 Reason => PE_All_Guards_Closed));
10490 return Stats;
10491 end Accept_Or_Raise;
10493 ----------------
10494 -- Add_Accept --
10495 ----------------
10497 procedure Add_Accept (Alt : Node_Id) is
10498 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10499 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10500 Eloc : constant Source_Ptr := Sloc (Ename);
10501 Eent : constant Entity_Id := Entity (Ename);
10502 Index : constant Node_Id := Entry_Index (Acc_Stm);
10503 Null_Body : Node_Id;
10504 Proc_Body : Node_Id;
10505 PB_Ent : Entity_Id;
10506 Expr : Node_Id;
10507 Call : Node_Id;
10509 begin
10510 if No (Ann) then
10511 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10512 end if;
10514 if Present (Condition (Alt)) then
10515 Expr :=
10516 Make_If_Expression (Eloc, New_List (
10517 Condition (Alt),
10518 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10519 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10520 else
10521 Expr :=
10522 Entry_Index_Expression
10523 (Eloc, Eent, Index, Scope (Eent));
10524 end if;
10526 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10527 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10529 -- Always add call to Abort_Undefer when generating code, since
10530 -- this is what the runtime expects (abort deferred in
10531 -- Selective_Wait). In CodePeer mode this only confuses the
10532 -- analysis with unknown calls, so don't do it.
10534 if not CodePeer_Mode then
10535 Call :=
10536 Make_Procedure_Call_Statement (Eloc,
10537 Name => New_Occurrence_Of (RTE (RE_Abort_Undefer), Eloc));
10538 Insert_Before
10539 (First (Statements (Handled_Statement_Sequence
10540 (Accept_Statement (Alt)))),
10541 Call);
10542 Analyze (Call);
10543 end if;
10545 PB_Ent :=
10546 Make_Defining_Identifier (Eloc,
10547 New_External_Name (Chars (Ename), 'A', Num_Accept));
10549 if Comes_From_Source (Alt) then
10550 Set_Debug_Info_Needed (PB_Ent);
10551 end if;
10553 Proc_Body :=
10554 Make_Subprogram_Body (Eloc,
10555 Specification =>
10556 Make_Procedure_Specification (Eloc,
10557 Defining_Unit_Name => PB_Ent),
10558 Declarations => Declarations (Acc_Stm),
10559 Handled_Statement_Sequence =>
10560 Build_Accept_Body (Accept_Statement (Alt)));
10562 -- During the analysis of the body of the accept statement, any
10563 -- zero cost exception handler records were collected in the
10564 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10565 -- This is where we move them to where they belong, namely the
10566 -- newly created procedure.
10568 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10569 Append (Proc_Body, Body_List);
10571 else
10572 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10574 -- if accept statement has declarations, insert above, given that
10575 -- we are not creating a body for the accept.
10577 if Present (Declarations (Acc_Stm)) then
10578 Insert_Actions (N, Declarations (Acc_Stm));
10579 end if;
10580 end if;
10582 Append_To (Accept_List,
10583 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10585 Num_Accept := Num_Accept + 1;
10586 end Add_Accept;
10588 ----------------------------
10589 -- Make_And_Declare_Label --
10590 ----------------------------
10592 function Make_And_Declare_Label (Num : Int) return Node_Id is
10593 Lab_Id : Node_Id;
10595 begin
10596 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10597 Lab :=
10598 Make_Label (Loc, Lab_Id);
10600 Append_To (Decls,
10601 Make_Implicit_Label_Declaration (Loc,
10602 Defining_Identifier =>
10603 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10604 Label_Construct => Lab));
10606 return Lab;
10607 end Make_And_Declare_Label;
10609 ----------------------
10610 -- Make_Select_Call --
10611 ----------------------
10613 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10614 Params : constant List_Id := New_List;
10616 begin
10617 Append_To (Params,
10618 Make_Attribute_Reference (Loc,
10619 Prefix => New_Occurrence_Of (Qnam, Loc),
10620 Attribute_Name => Name_Unchecked_Access));
10621 Append_To (Params, Select_Mode);
10622 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10623 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10625 return
10626 Make_Procedure_Call_Statement (Loc,
10627 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10628 Parameter_Associations => Params);
10629 end Make_Select_Call;
10631 --------------------------------
10632 -- Process_Accept_Alternative --
10633 --------------------------------
10635 procedure Process_Accept_Alternative
10636 (Alt : Node_Id;
10637 Index : Int;
10638 Proc : Node_Id)
10640 Astmt : constant Node_Id := Accept_Statement (Alt);
10641 Alt_Stats : List_Id;
10643 begin
10644 Adjust_Condition (Condition (Alt));
10646 -- Accept with body
10648 if Present (Handled_Statement_Sequence (Astmt)) then
10649 Alt_Stats :=
10650 New_List (
10651 Make_Procedure_Call_Statement (Sloc (Proc),
10652 Name =>
10653 New_Occurrence_Of
10654 (Defining_Unit_Name (Specification (Proc)),
10655 Sloc (Proc))));
10657 -- Accept with no body (followed by trailing statements)
10659 else
10660 Alt_Stats := Empty_List;
10661 end if;
10663 Ensure_Statement_Present (Sloc (Astmt), Alt);
10665 -- After the call, if any, branch to trailing statements, if any.
10666 -- We create a label for each, as well as the corresponding label
10667 -- declaration.
10669 if not Is_Empty_List (Statements (Alt)) then
10670 Lab := Make_And_Declare_Label (Index);
10671 Append (Lab, Trailing_List);
10672 Append_List (Statements (Alt), Trailing_List);
10673 Append_To (Trailing_List,
10674 Make_Goto_Statement (Loc,
10675 Name => New_Copy (Identifier (End_Lab))));
10677 else
10678 Lab := End_Lab;
10679 end if;
10681 Append_To (Alt_Stats,
10682 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10684 Append_To (Alt_List,
10685 Make_Case_Statement_Alternative (Loc,
10686 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10687 Statements => Alt_Stats));
10688 end Process_Accept_Alternative;
10690 -------------------------------
10691 -- Process_Delay_Alternative --
10692 -------------------------------
10694 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10695 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10696 Cond : Node_Id;
10697 Delay_Alt : List_Id;
10699 begin
10700 -- Deal with C/Fortran boolean as delay condition
10702 Adjust_Condition (Condition (Alt));
10704 -- Determine the smallest specified delay
10706 -- for each delay alternative generate:
10708 -- if guard-expression then
10709 -- Delay_Val := delay-expression;
10710 -- Guard_Open := True;
10711 -- if Delay_Val < Delay_Min then
10712 -- Delay_Min := Delay_Val;
10713 -- Delay_Index := Index;
10714 -- end if;
10715 -- end if;
10717 -- The enclosing if-statement is omitted if there is no guard
10719 if Delay_Count = 1 or else First_Delay then
10720 First_Delay := False;
10722 Delay_Alt := New_List (
10723 Make_Assignment_Statement (Loc,
10724 Name => New_Occurrence_Of (Delay_Min, Loc),
10725 Expression => Expression (Delay_Statement (Alt))));
10727 if Delay_Count > 1 then
10728 Append_To (Delay_Alt,
10729 Make_Assignment_Statement (Loc,
10730 Name => New_Occurrence_Of (Delay_Index, Loc),
10731 Expression => Make_Integer_Literal (Loc, Index)));
10732 end if;
10734 else
10735 Delay_Alt := New_List (
10736 Make_Assignment_Statement (Loc,
10737 Name => New_Occurrence_Of (Delay_Val, Loc),
10738 Expression => Expression (Delay_Statement (Alt))));
10740 if Time_Type = Standard_Duration then
10741 Cond :=
10742 Make_Op_Lt (Loc,
10743 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10744 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10746 else
10747 -- The scope of the time type must define a comparison
10748 -- operator. The scope itself may not be visible, so we
10749 -- construct a node with entity information to insure that
10750 -- semantic analysis can find the proper operator.
10752 Cond :=
10753 Make_Function_Call (Loc,
10754 Name => Make_Selected_Component (Loc,
10755 Prefix =>
10756 New_Occurrence_Of (Scope (Time_Type), Loc),
10757 Selector_Name =>
10758 Make_Operator_Symbol (Loc,
10759 Chars => Name_Op_Lt,
10760 Strval => No_String)),
10761 Parameter_Associations =>
10762 New_List (
10763 New_Occurrence_Of (Delay_Val, Loc),
10764 New_Occurrence_Of (Delay_Min, Loc)));
10766 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10767 end if;
10769 Append_To (Delay_Alt,
10770 Make_Implicit_If_Statement (N,
10771 Condition => Cond,
10772 Then_Statements => New_List (
10773 Make_Assignment_Statement (Loc,
10774 Name => New_Occurrence_Of (Delay_Min, Loc),
10775 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10777 Make_Assignment_Statement (Loc,
10778 Name => New_Occurrence_Of (Delay_Index, Loc),
10779 Expression => Make_Integer_Literal (Loc, Index)))));
10780 end if;
10782 if Check_Guard then
10783 Append_To (Delay_Alt,
10784 Make_Assignment_Statement (Loc,
10785 Name => New_Occurrence_Of (Guard_Open, Loc),
10786 Expression => New_Occurrence_Of (Standard_True, Loc)));
10787 end if;
10789 if Present (Condition (Alt)) then
10790 Delay_Alt := New_List (
10791 Make_Implicit_If_Statement (N,
10792 Condition => Condition (Alt),
10793 Then_Statements => Delay_Alt));
10794 end if;
10796 Append_List (Delay_Alt, Delay_List);
10798 Ensure_Statement_Present (Dloc, Alt);
10800 -- If the delay alternative has a statement part, add choice to the
10801 -- case statements for delays.
10803 if not Is_Empty_List (Statements (Alt)) then
10805 if Delay_Count = 1 then
10806 Append_List (Statements (Alt), Delay_Alt_List);
10808 else
10809 Append_To (Delay_Alt_List,
10810 Make_Case_Statement_Alternative (Loc,
10811 Discrete_Choices => New_List (
10812 Make_Integer_Literal (Loc, Index)),
10813 Statements => Statements (Alt)));
10814 end if;
10816 elsif Delay_Count = 1 then
10818 -- If the single delay has no trailing statements, add a branch
10819 -- to the exit label to the selective wait.
10821 Delay_Alt_List := New_List (
10822 Make_Goto_Statement (Loc,
10823 Name => New_Copy (Identifier (End_Lab))));
10825 end if;
10826 end Process_Delay_Alternative;
10828 -- Start of processing for Expand_N_Selective_Accept
10830 begin
10831 Process_Statements_For_Controlled_Objects (N);
10833 -- First insert some declarations before the select. The first is:
10835 -- Ann : Address
10837 -- This variable holds the parameters passed to the accept body. This
10838 -- declaration has already been inserted by the time we get here by
10839 -- a call to Expand_Accept_Declarations made from the semantics when
10840 -- processing the first accept statement contained in the select. We
10841 -- can find this entity as Accept_Address (E), where E is any of the
10842 -- entries references by contained accept statements.
10844 -- The first step is to scan the list of Selective_Accept_Statements
10845 -- to find this entity, and also count the number of accepts, and
10846 -- determine if terminated, delay or else is present:
10848 Num_Alts := 0;
10850 Alt := First (Alts);
10851 while Present (Alt) loop
10852 Process_Statements_For_Controlled_Objects (Alt);
10854 if Nkind (Alt) = N_Accept_Alternative then
10855 Add_Accept (Alt);
10857 elsif Nkind (Alt) = N_Delay_Alternative then
10858 Delay_Count := Delay_Count + 1;
10860 -- If the delays are relative delays, the delay expressions have
10861 -- type Standard_Duration. Otherwise they must have some time type
10862 -- recognized by GNAT.
10864 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10865 Time_Type := Standard_Duration;
10866 else
10867 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10869 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10870 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10871 then
10872 null;
10873 else
10874 Error_Msg_NE (
10875 "& is not a time type (RM 9.6(6))",
10876 Expression (Delay_Statement (Alt)), Time_Type);
10877 Time_Type := Standard_Duration;
10878 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10879 end if;
10880 end if;
10882 if No (Condition (Alt)) then
10884 -- This guard will always be open
10886 Check_Guard := False;
10887 end if;
10889 elsif Nkind (Alt) = N_Terminate_Alternative then
10890 Adjust_Condition (Condition (Alt));
10891 Terminate_Alt := Alt;
10892 end if;
10894 Num_Alts := Num_Alts + 1;
10895 Next (Alt);
10896 end loop;
10898 Else_Present := Present (Else_Statements (N));
10900 -- At the same time (see procedure Add_Accept) we build the accept list:
10902 -- Qnn : Accept_List (1 .. num-select) := (
10903 -- (null-body, entry-index),
10904 -- (null-body, entry-index),
10905 -- ..
10906 -- (null_body, entry-index));
10908 -- In the above declaration, null-body is True if the corresponding
10909 -- accept has no body, and false otherwise. The entry is either the
10910 -- entry index expression if there is no guard, or if a guard is
10911 -- present, then an if expression of the form:
10913 -- (if guard then entry-index else Null_Task_Entry)
10915 -- If a guard is statically known to be false, the entry can simply
10916 -- be omitted from the accept list.
10918 Append_To (Decls,
10919 Make_Object_Declaration (Loc,
10920 Defining_Identifier => Qnam,
10921 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10922 Aliased_Present => True,
10923 Expression =>
10924 Make_Qualified_Expression (Loc,
10925 Subtype_Mark =>
10926 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10927 Expression =>
10928 Make_Aggregate (Loc, Expressions => Accept_List))));
10930 -- Then we declare the variable that holds the index for the accept
10931 -- that will be selected for service:
10933 -- Xnn : Select_Index;
10935 Append_To (Decls,
10936 Make_Object_Declaration (Loc,
10937 Defining_Identifier => Xnam,
10938 Object_Definition =>
10939 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
10940 Expression =>
10941 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
10943 -- After this follow procedure declarations for each accept body
10945 -- procedure Pnn is
10946 -- begin
10947 -- ...
10948 -- end;
10950 -- where the ... are statements from the corresponding procedure body.
10951 -- No parameters are involved, since the parameters are passed via Ann
10952 -- and the parameter references have already been expanded to be direct
10953 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10954 -- any embedded tasking statements (which would normally be illegal in
10955 -- procedures), have been converted to calls to the tasking runtime so
10956 -- there is no problem in putting them into procedures.
10958 -- The original accept statement has been expanded into a block in
10959 -- the same fashion as for simple accepts (see Build_Accept_Body).
10961 -- Note: we don't really need to build these procedures for the case
10962 -- where no delay statement is present, but it is just as easy to
10963 -- build them unconditionally, and not significantly inefficient,
10964 -- since if they are short they will be inlined anyway.
10966 -- The procedure declarations have been assembled in Body_List
10968 -- If delays are present, we must compute the required delay.
10969 -- We first generate the declarations:
10971 -- Delay_Index : Boolean := 0;
10972 -- Delay_Min : Some_Time_Type.Time;
10973 -- Delay_Val : Some_Time_Type.Time;
10975 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10976 -- active delay that is actually chosen as the basis for the possible
10977 -- delay if an immediate rendez-vous is not possible.
10979 -- In the most common case there is a single delay statement, and this
10980 -- is handled specially.
10982 if Delay_Count > 0 then
10984 -- Generate the required declarations
10986 Delay_Val :=
10987 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10988 Delay_Index :=
10989 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10990 Delay_Min :=
10991 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10993 Append_To (Decls,
10994 Make_Object_Declaration (Loc,
10995 Defining_Identifier => Delay_Val,
10996 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
10998 Append_To (Decls,
10999 Make_Object_Declaration (Loc,
11000 Defining_Identifier => Delay_Index,
11001 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11002 Expression => Make_Integer_Literal (Loc, 0)));
11004 Append_To (Decls,
11005 Make_Object_Declaration (Loc,
11006 Defining_Identifier => Delay_Min,
11007 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11008 Expression =>
11009 Unchecked_Convert_To (Time_Type,
11010 Make_Attribute_Reference (Loc,
11011 Prefix =>
11012 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11013 Attribute_Name => Name_Last))));
11015 -- Create Duration and Delay_Mode objects used for passing a delay
11016 -- value to RTS
11018 D := Make_Temporary (Loc, 'D');
11019 M := Make_Temporary (Loc, 'M');
11021 declare
11022 Discr : Entity_Id;
11024 begin
11025 -- Note that these values are defined in s-osprim.ads and must
11026 -- be kept in sync:
11028 -- Relative : constant := 0;
11029 -- Absolute_Calendar : constant := 1;
11030 -- Absolute_RT : constant := 2;
11032 if Time_Type = Standard_Duration then
11033 Discr := Make_Integer_Literal (Loc, 0);
11035 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11036 Discr := Make_Integer_Literal (Loc, 1);
11038 else
11039 pragma Assert
11040 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11041 Discr := Make_Integer_Literal (Loc, 2);
11042 end if;
11044 Append_To (Decls,
11045 Make_Object_Declaration (Loc,
11046 Defining_Identifier => D,
11047 Object_Definition =>
11048 New_Occurrence_Of (Standard_Duration, Loc)));
11050 Append_To (Decls,
11051 Make_Object_Declaration (Loc,
11052 Defining_Identifier => M,
11053 Object_Definition =>
11054 New_Occurrence_Of (Standard_Integer, Loc),
11055 Expression => Discr));
11056 end;
11058 if Check_Guard then
11059 Guard_Open :=
11060 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11062 Append_To (Decls,
11063 Make_Object_Declaration (Loc,
11064 Defining_Identifier => Guard_Open,
11065 Object_Definition =>
11066 New_Occurrence_Of (Standard_Boolean, Loc),
11067 Expression =>
11068 New_Occurrence_Of (Standard_False, Loc)));
11069 end if;
11071 -- Delay_Count is zero, don't need M and D set (suppress warning)
11073 else
11074 M := Empty;
11075 D := Empty;
11076 end if;
11078 if Present (Terminate_Alt) then
11080 -- If the terminate alternative guard is False, use
11081 -- Simple_Mode; otherwise use Terminate_Mode.
11083 if Present (Condition (Terminate_Alt)) then
11084 Select_Mode := Make_If_Expression (Loc,
11085 New_List (Condition (Terminate_Alt),
11086 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11087 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11088 else
11089 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11090 end if;
11092 elsif Else_Present or Delay_Count > 0 then
11093 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11095 else
11096 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11097 end if;
11099 Select_Call := Make_Select_Call (Select_Mode);
11100 Append (Select_Call, Stats);
11102 -- Now generate code to act on the result. There is an entry
11103 -- in this case for each accept statement with a non-null body,
11104 -- followed by a branch to the statements that follow the Accept.
11105 -- In the absence of delay alternatives, we generate:
11107 -- case X is
11108 -- when No_Rendezvous => -- omitted if simple mode
11109 -- goto Lab0;
11111 -- when 1 =>
11112 -- P1n;
11113 -- goto Lab1;
11115 -- when 2 =>
11116 -- P2n;
11117 -- goto Lab2;
11119 -- when others =>
11120 -- goto Exit;
11121 -- end case;
11123 -- Lab0: Else_Statements;
11124 -- goto exit;
11126 -- Lab1: Trailing_Statements1;
11127 -- goto Exit;
11129 -- Lab2: Trailing_Statements2;
11130 -- goto Exit;
11131 -- ...
11132 -- Exit:
11134 -- Generate label for common exit
11136 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11138 -- First entry is the default case, when no rendezvous is possible
11140 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11142 if Else_Present then
11144 -- If no rendezvous is possible, the else part is executed
11146 Lab := Make_And_Declare_Label (0);
11147 Alt_Stats := New_List (
11148 Make_Goto_Statement (Loc,
11149 Name => New_Copy (Identifier (Lab))));
11151 Append (Lab, Trailing_List);
11152 Append_List (Else_Statements (N), Trailing_List);
11153 Append_To (Trailing_List,
11154 Make_Goto_Statement (Loc,
11155 Name => New_Copy (Identifier (End_Lab))));
11156 else
11157 Alt_Stats := New_List (
11158 Make_Goto_Statement (Loc,
11159 Name => New_Copy (Identifier (End_Lab))));
11160 end if;
11162 Append_To (Alt_List,
11163 Make_Case_Statement_Alternative (Loc,
11164 Discrete_Choices => Choices,
11165 Statements => Alt_Stats));
11167 -- We make use of the fact that Accept_Index is an integer type, and
11168 -- generate successive literals for entries for each accept. Only those
11169 -- for which there is a body or trailing statements get a case entry.
11171 Alt := First (Select_Alternatives (N));
11172 Proc := First (Body_List);
11173 while Present (Alt) loop
11175 if Nkind (Alt) = N_Accept_Alternative then
11176 Process_Accept_Alternative (Alt, Index, Proc);
11177 Index := Index + 1;
11179 if Present
11180 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11181 then
11182 Next (Proc);
11183 end if;
11185 elsif Nkind (Alt) = N_Delay_Alternative then
11186 Process_Delay_Alternative (Alt, Delay_Num);
11187 Delay_Num := Delay_Num + 1;
11188 end if;
11190 Next (Alt);
11191 end loop;
11193 -- An others choice is always added to the main case, as well
11194 -- as the delay case (to satisfy the compiler).
11196 Append_To (Alt_List,
11197 Make_Case_Statement_Alternative (Loc,
11198 Discrete_Choices =>
11199 New_List (Make_Others_Choice (Loc)),
11200 Statements =>
11201 New_List (Make_Goto_Statement (Loc,
11202 Name => New_Copy (Identifier (End_Lab))))));
11204 Accept_Case := New_List (
11205 Make_Case_Statement (Loc,
11206 Expression => New_Occurrence_Of (Xnam, Loc),
11207 Alternatives => Alt_List));
11209 Append_List (Trailing_List, Accept_Case);
11210 Append_List (Body_List, Decls);
11212 -- Construct case statement for trailing statements of delay
11213 -- alternatives, if there are several of them.
11215 if Delay_Count > 1 then
11216 Append_To (Delay_Alt_List,
11217 Make_Case_Statement_Alternative (Loc,
11218 Discrete_Choices =>
11219 New_List (Make_Others_Choice (Loc)),
11220 Statements =>
11221 New_List (Make_Null_Statement (Loc))));
11223 Delay_Case := New_List (
11224 Make_Case_Statement (Loc,
11225 Expression => New_Occurrence_Of (Delay_Index, Loc),
11226 Alternatives => Delay_Alt_List));
11227 else
11228 Delay_Case := Delay_Alt_List;
11229 end if;
11231 -- If there are no delay alternatives, we append the case statement
11232 -- to the statement list.
11234 if Delay_Count = 0 then
11235 Append_List (Accept_Case, Stats);
11237 -- Delay alternatives present
11239 else
11240 -- If delay alternatives are present we generate:
11242 -- find minimum delay.
11243 -- DX := minimum delay;
11244 -- M := <delay mode>;
11245 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11246 -- DX, MX, X);
11248 -- if X = No_Rendezvous then
11249 -- case statement for delay statements.
11250 -- else
11251 -- case statement for accept alternatives.
11252 -- end if;
11254 declare
11255 Cases : Node_Id;
11256 Stmt : Node_Id;
11257 Parms : List_Id;
11258 Parm : Node_Id;
11259 Conv : Node_Id;
11261 begin
11262 -- The type of the delay expression is known to be legal
11264 if Time_Type = Standard_Duration then
11265 Conv := New_Occurrence_Of (Delay_Min, Loc);
11267 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11268 Conv := Make_Function_Call (Loc,
11269 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11270 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11272 else
11273 pragma Assert
11274 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11276 Conv := Make_Function_Call (Loc,
11277 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11278 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11279 end if;
11281 Stmt := Make_Assignment_Statement (Loc,
11282 Name => New_Occurrence_Of (D, Loc),
11283 Expression => Conv);
11285 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11287 Parms := Parameter_Associations (Select_Call);
11289 Parm := First (Parms);
11290 while Present (Parm) and then Parm /= Select_Mode loop
11291 Next (Parm);
11292 end loop;
11294 pragma Assert (Present (Parm));
11295 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11296 Analyze (Parm);
11298 -- Prepare two new parameters of Duration and Delay_Mode type
11299 -- which represent the value and the mode of the minimum delay.
11301 Next (Parm);
11302 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11303 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11305 -- Create a call to RTS
11307 Rewrite (Select_Call,
11308 Make_Procedure_Call_Statement (Loc,
11309 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11310 Parameter_Associations => Parms));
11312 -- This new call should follow the calculation of the minimum
11313 -- delay.
11315 Insert_List_Before (Select_Call, Delay_List);
11317 if Check_Guard then
11318 Stmt :=
11319 Make_Implicit_If_Statement (N,
11320 Condition => New_Occurrence_Of (Guard_Open, Loc),
11321 Then_Statements => New_List (
11322 New_Copy_Tree (Stmt),
11323 New_Copy_Tree (Select_Call)),
11324 Else_Statements => Accept_Or_Raise);
11325 Rewrite (Select_Call, Stmt);
11326 else
11327 Insert_Before (Select_Call, Stmt);
11328 end if;
11330 Cases :=
11331 Make_Implicit_If_Statement (N,
11332 Condition => Make_Op_Eq (Loc,
11333 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11334 Right_Opnd =>
11335 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11337 Then_Statements => Delay_Case,
11338 Else_Statements => Accept_Case);
11340 Append (Cases, Stats);
11341 end;
11342 end if;
11344 Append (End_Lab, Stats);
11346 -- Replace accept statement with appropriate block
11348 Rewrite (N,
11349 Make_Block_Statement (Loc,
11350 Declarations => Decls,
11351 Handled_Statement_Sequence =>
11352 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11353 Analyze (N);
11355 -- Note: have to worry more about abort deferral in above code ???
11357 -- Final step is to unstack the Accept_Address entries for all accept
11358 -- statements appearing in accept alternatives in the select statement
11360 Alt := First (Alts);
11361 while Present (Alt) loop
11362 if Nkind (Alt) = N_Accept_Alternative then
11363 Remove_Last_Elmt (Accept_Address
11364 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11365 end if;
11367 Next (Alt);
11368 end loop;
11369 end Expand_N_Selective_Accept;
11371 --------------------------------------
11372 -- Expand_N_Single_Task_Declaration --
11373 --------------------------------------
11375 -- Single task declarations should never be present after semantic
11376 -- analysis, since we expect them to be replaced by a declaration of an
11377 -- anonymous task type, followed by a declaration of the task object. We
11378 -- include this routine to make sure that is happening.
11380 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11381 begin
11382 raise Program_Error;
11383 end Expand_N_Single_Task_Declaration;
11385 ------------------------
11386 -- Expand_N_Task_Body --
11387 ------------------------
11389 -- Given a task body
11391 -- task body tname is
11392 -- <declarations>
11393 -- begin
11394 -- <statements>
11395 -- end x;
11397 -- This expansion routine converts it into a procedure and sets the
11398 -- elaboration flag for the procedure to true, to represent the fact
11399 -- that the task body is now elaborated:
11401 -- procedure tnameB (_Task : access tnameV) is
11402 -- discriminal : dtype renames _Task.discriminant;
11404 -- procedure _clean is
11405 -- begin
11406 -- Abort_Defer.all;
11407 -- Complete_Task;
11408 -- Abort_Undefer.all;
11409 -- return;
11410 -- end _clean;
11412 -- begin
11413 -- Abort_Undefer.all;
11414 -- <declarations>
11415 -- System.Task_Stages.Complete_Activation;
11416 -- <statements>
11417 -- at end
11418 -- _clean;
11419 -- end tnameB;
11421 -- tnameE := True;
11423 -- In addition, if the task body is an activator, then a call to activate
11424 -- tasks is added at the start of the statements, before the call to
11425 -- Complete_Activation, and if in addition the task is a master then it
11426 -- must be established as a master. These calls are inserted and analyzed
11427 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11428 -- expanded.
11430 -- There is one discriminal declaration line generated for each
11431 -- discriminant that is present to provide an easy reference point for
11432 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11434 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11435 -- task body procedures have a profile (Arg : System.Address). That is
11436 -- needed because GNARLI has to use the same access-to-subprogram type
11437 -- for all task types. We depend here on knowing that in GNAT, passing
11438 -- an address argument by value is identical to passing a record value
11439 -- by access (in either case a single pointer is passed), so even though
11440 -- this procedure has the wrong profile. In fact it's all OK, since the
11441 -- callings sequence is identical.
11443 procedure Expand_N_Task_Body (N : Node_Id) is
11444 Loc : constant Source_Ptr := Sloc (N);
11445 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11446 Call : Node_Id;
11447 New_N : Node_Id;
11449 Insert_Nod : Node_Id;
11450 -- Used to determine the proper location of wrapper body insertions
11452 begin
11453 -- if no task body procedure, means we had an error in configurable
11454 -- run-time mode, and there is no point in proceeding further.
11456 if No (Task_Body_Procedure (Ttyp)) then
11457 return;
11458 end if;
11460 -- Add renaming declarations for discriminals and a declaration for the
11461 -- entry family index (if applicable).
11463 Install_Private_Data_Declarations
11464 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11466 -- Add a call to Abort_Undefer at the very beginning of the task
11467 -- body since this body is called with abort still deferred.
11469 if Abort_Allowed then
11470 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11471 Insert_Before
11472 (First (Statements (Handled_Statement_Sequence (N))), Call);
11473 Analyze (Call);
11474 end if;
11476 -- The statement part has already been protected with an at_end and
11477 -- cleanup actions. The call to Complete_Activation must be placed
11478 -- at the head of the sequence of statements of that block. The
11479 -- declarations have been merged in this sequence of statements but
11480 -- the first real statement is accessible from the First_Real_Statement
11481 -- field (which was set for exactly this purpose).
11483 if Restricted_Profile then
11484 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11485 else
11486 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11487 end if;
11489 Insert_Before
11490 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11491 Analyze (Call);
11493 New_N :=
11494 Make_Subprogram_Body (Loc,
11495 Specification => Build_Task_Proc_Specification (Ttyp),
11496 Declarations => Declarations (N),
11497 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11499 -- If the task contains generic instantiations, cleanup actions are
11500 -- delayed until after instantiation. Transfer the activation chain to
11501 -- the subprogram, to insure that the activation call is properly
11502 -- generated. It the task body contains inner tasks, indicate that the
11503 -- subprogram is a task master.
11505 if Delay_Cleanups (Ttyp) then
11506 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11507 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11508 end if;
11510 Rewrite (N, New_N);
11511 Analyze (N);
11513 -- Set elaboration flag immediately after task body. If the body is a
11514 -- subunit, the flag is set in the declarative part containing the stub.
11516 if Nkind (Parent (N)) /= N_Subunit then
11517 Insert_After (N,
11518 Make_Assignment_Statement (Loc,
11519 Name =>
11520 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11521 Expression => New_Occurrence_Of (Standard_True, Loc)));
11522 end if;
11524 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11525 -- the task body. At this point all wrapper specs have been created,
11526 -- frozen and included in the dispatch table for the task type.
11528 if Ada_Version >= Ada_2005 then
11529 if Nkind (Parent (N)) = N_Subunit then
11530 Insert_Nod := Corresponding_Stub (Parent (N));
11531 else
11532 Insert_Nod := N;
11533 end if;
11535 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11536 end if;
11537 end Expand_N_Task_Body;
11539 ------------------------------------
11540 -- Expand_N_Task_Type_Declaration --
11541 ------------------------------------
11543 -- We have several things to do. First we must create a Boolean flag used
11544 -- to mark if the body is elaborated yet. This variable gets set to True
11545 -- when the body of the task is elaborated (we can't rely on the normal
11546 -- ABE mechanism for the task body, since we need to pass an access to
11547 -- this elaboration boolean to the runtime routines).
11549 -- taskE : aliased Boolean := False;
11551 -- Next a variable is declared to hold the task stack size (either the
11552 -- default : Unspecified_Size, or a value that is set by a pragma
11553 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11554 -- the variable is initialized with this value:
11556 -- taskZ : Size_Type := Unspecified_Size;
11557 -- or
11558 -- taskZ : Size_Type := Size_Type (size_expression);
11560 -- Note: No variable is needed to hold the task relative deadline since
11561 -- its value would never be static because the parameter is of a private
11562 -- type (Ada.Real_Time.Time_Span).
11564 -- Next we create a corresponding record type declaration used to represent
11565 -- values of this task. The general form of this type declaration is
11567 -- type taskV (discriminants) is record
11568 -- _Task_Id : Task_Id;
11569 -- entry_family : array (bounds) of Void;
11570 -- _Priority : Integer := priority_expression;
11571 -- _Size : Size_Type := size_expression;
11572 -- _Task_Info : Task_Info_Type := task_info_expression;
11573 -- _CPU : Integer := cpu_range_expression;
11574 -- _Relative_Deadline : Time_Span := time_span_expression;
11575 -- _Domain : Dispatching_Domain := dd_expression;
11576 -- end record;
11578 -- The discriminants are present only if the corresponding task type has
11579 -- discriminants, and they exactly mirror the task type discriminants.
11581 -- The Id field is always present. It contains the Task_Id value, as set by
11582 -- the call to Create_Task. Note that although the task is limited, the
11583 -- task value record type is not limited, so there is no problem in passing
11584 -- this field as an out parameter to Create_Task.
11586 -- One entry_family component is present for each entry family in the task
11587 -- definition. The bounds correspond to the bounds of the entry family
11588 -- (which may depend on discriminants). The element type is void, since we
11589 -- only need the bounds information for determining the entry index. Note
11590 -- that the use of an anonymous array would normally be illegal in this
11591 -- context, but this is a parser check, and the semantics is quite prepared
11592 -- to handle such a case.
11594 -- The _Size field is present only if a Storage_Size pragma appears in the
11595 -- task definition. The expression captures the argument that was present
11596 -- in the pragma, and is used to override the task stack size otherwise
11597 -- associated with the task type.
11599 -- The _Priority field is present only if the task entity has a Priority or
11600 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11601 -- definition clause). It will be filled at the freeze point, when the
11602 -- record init proc is built, to capture the expression of the rep item
11603 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11604 -- here since aspect evaluations are delayed till the freeze point.
11606 -- The _Task_Info field is present only if a Task_Info pragma appears in
11607 -- the task definition. The expression captures the argument that was
11608 -- present in the pragma, and is used to provide the Task_Image parameter
11609 -- to the call to Create_Task.
11611 -- The _CPU field is present only if the task entity has a CPU rep item
11612 -- (pragma, aspect specification or attribute definition clause). It will
11613 -- be filled at the freeze point, when the record init proc is built, to
11614 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11615 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11616 -- are delayed till the freeze point.
11618 -- The _Relative_Deadline field is present only if a Relative_Deadline
11619 -- pragma appears in the task definition. The expression captures the
11620 -- argument that was present in the pragma, and is used to provide the
11621 -- Relative_Deadline parameter to the call to Create_Task.
11623 -- The _Domain field is present only if the task entity has a
11624 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11625 -- definition clause). It will be filled at the freeze point, when the
11626 -- record init proc is built, to capture the expression of the rep item
11627 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11628 -- here since aspect evaluations are delayed till the freeze point.
11630 -- When a task is declared, an instance of the task value record is
11631 -- created. The elaboration of this declaration creates the correct bounds
11632 -- for the entry families, and also evaluates the size, priority, and
11633 -- task_Info expressions if needed. The initialization routine for the task
11634 -- type itself then calls Create_Task with appropriate parameters to
11635 -- initialize the value of the Task_Id field.
11637 -- Note: the address of this record is passed as the "Discriminants"
11638 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11639 -- body procedure, it does not matter that it does not quite match the
11640 -- GNARLI model of what is being passed (the record contains more than just
11641 -- the discriminants, but the discriminants can be found from the record
11642 -- value).
11644 -- The Entity_Id for this created record type is placed in the
11645 -- Corresponding_Record_Type field of the associated task type entity.
11647 -- Next we create a procedure specification for the task body procedure:
11649 -- procedure taskB (_Task : access taskV);
11651 -- Note that this must come after the record type declaration, since
11652 -- the spec refers to this type. It turns out that the initialization
11653 -- procedure for the value type references the task body spec, but that's
11654 -- fine, since it won't be generated till the freeze point for the type,
11655 -- which is certainly after the task body spec declaration.
11657 -- Finally, we set the task index value field of the entry attribute in
11658 -- the case of a simple entry.
11660 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11661 Loc : constant Source_Ptr := Sloc (N);
11662 TaskId : constant Entity_Id := Defining_Identifier (N);
11663 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11664 Tasknm : constant Name_Id := Chars (Tasktyp);
11665 Taskdef : constant Node_Id := Task_Definition (N);
11667 Body_Decl : Node_Id;
11668 Cdecls : List_Id;
11669 Decl_Stack : Node_Id;
11670 Elab_Decl : Node_Id;
11671 Ent_Stack : Entity_Id;
11672 Proc_Spec : Node_Id;
11673 Rec_Decl : Node_Id;
11674 Rec_Ent : Entity_Id;
11675 Size_Decl : Entity_Id;
11676 Task_Size : Node_Id;
11678 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11679 -- Searches the task definition T for the first occurrence of the pragma
11680 -- Relative Deadline. The caller has ensured that the pragma is present
11681 -- in the task definition. Note that this routine cannot be implemented
11682 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11683 -- not chained because their expansion into a procedure call statement
11684 -- would cause a break in the chain.
11686 ----------------------------------
11687 -- Get_Relative_Deadline_Pragma --
11688 ----------------------------------
11690 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11691 N : Node_Id;
11693 begin
11694 N := First (Visible_Declarations (T));
11695 while Present (N) loop
11696 if Nkind (N) = N_Pragma
11697 and then Pragma_Name (N) = Name_Relative_Deadline
11698 then
11699 return N;
11700 end if;
11702 Next (N);
11703 end loop;
11705 N := First (Private_Declarations (T));
11706 while Present (N) loop
11707 if Nkind (N) = N_Pragma
11708 and then Pragma_Name (N) = Name_Relative_Deadline
11709 then
11710 return N;
11711 end if;
11713 Next (N);
11714 end loop;
11716 raise Program_Error;
11717 end Get_Relative_Deadline_Pragma;
11719 -- Start of processing for Expand_N_Task_Type_Declaration
11721 begin
11722 -- If already expanded, nothing to do
11724 if Present (Corresponding_Record_Type (Tasktyp)) then
11725 return;
11726 end if;
11728 -- Here we will do the expansion
11730 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11732 Rec_Ent := Defining_Identifier (Rec_Decl);
11733 Cdecls := Component_Items (Component_List
11734 (Type_Definition (Rec_Decl)));
11736 Qualify_Entity_Names (N);
11738 -- First create the elaboration variable
11740 Elab_Decl :=
11741 Make_Object_Declaration (Loc,
11742 Defining_Identifier =>
11743 Make_Defining_Identifier (Sloc (Tasktyp),
11744 Chars => New_External_Name (Tasknm, 'E')),
11745 Aliased_Present => True,
11746 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11747 Expression => New_Occurrence_Of (Standard_False, Loc));
11749 Insert_After (N, Elab_Decl);
11751 -- Next create the declaration of the size variable (tasknmZ)
11753 Set_Storage_Size_Variable (Tasktyp,
11754 Make_Defining_Identifier (Sloc (Tasktyp),
11755 Chars => New_External_Name (Tasknm, 'Z')));
11757 if Present (Taskdef)
11758 and then Has_Storage_Size_Pragma (Taskdef)
11759 and then
11760 Is_OK_Static_Expression
11761 (Expression
11762 (First (Pragma_Argument_Associations
11763 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11764 then
11765 Size_Decl :=
11766 Make_Object_Declaration (Loc,
11767 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11768 Object_Definition =>
11769 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11770 Expression =>
11771 Convert_To (RTE (RE_Size_Type),
11772 Relocate_Node
11773 (Expression (First (Pragma_Argument_Associations
11774 (Get_Rep_Pragma
11775 (TaskId, Name_Storage_Size)))))));
11777 else
11778 Size_Decl :=
11779 Make_Object_Declaration (Loc,
11780 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11781 Object_Definition =>
11782 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11783 Expression =>
11784 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11785 end if;
11787 Insert_After (Elab_Decl, Size_Decl);
11789 -- Next build the rest of the corresponding record declaration. This is
11790 -- done last, since the corresponding record initialization procedure
11791 -- will reference the previously created entities.
11793 -- Fill in the component declarations -- first the _Task_Id field
11795 Append_To (Cdecls,
11796 Make_Component_Declaration (Loc,
11797 Defining_Identifier =>
11798 Make_Defining_Identifier (Loc, Name_uTask_Id),
11799 Component_Definition =>
11800 Make_Component_Definition (Loc,
11801 Aliased_Present => False,
11802 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11803 Loc))));
11805 -- Declare static ATCB (that is, created by the expander) if we are
11806 -- using the Restricted run time.
11808 if Restricted_Profile then
11809 Append_To (Cdecls,
11810 Make_Component_Declaration (Loc,
11811 Defining_Identifier =>
11812 Make_Defining_Identifier (Loc, Name_uATCB),
11814 Component_Definition =>
11815 Make_Component_Definition (Loc,
11816 Aliased_Present => True,
11817 Subtype_Indication => Make_Subtype_Indication (Loc,
11818 Subtype_Mark =>
11819 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11821 Constraint =>
11822 Make_Index_Or_Discriminant_Constraint (Loc,
11823 Constraints =>
11824 New_List (Make_Integer_Literal (Loc, 0)))))));
11826 end if;
11828 -- Declare static stack (that is, created by the expander) if we are
11829 -- using the Restricted run time on a bare board configuration.
11831 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11833 -- First we need to extract the appropriate stack size
11835 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11837 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11838 declare
11839 Expr_N : constant Node_Id :=
11840 Expression (First (
11841 Pragma_Argument_Associations (
11842 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11843 Etyp : constant Entity_Id := Etype (Expr_N);
11844 P : constant Node_Id := Parent (Expr_N);
11846 begin
11847 -- The stack is defined inside the corresponding record.
11848 -- Therefore if the size of the stack is set by means of
11849 -- a discriminant, we must reference the discriminant of the
11850 -- corresponding record type.
11852 if Nkind (Expr_N) in N_Has_Entity
11853 and then Present (Discriminal_Link (Entity (Expr_N)))
11854 then
11855 Task_Size :=
11856 New_Occurrence_Of
11857 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11858 Loc);
11859 Set_Parent (Task_Size, P);
11860 Set_Etype (Task_Size, Etyp);
11861 Set_Analyzed (Task_Size);
11863 else
11864 Task_Size := Relocate_Node (Expr_N);
11865 end if;
11866 end;
11868 else
11869 Task_Size :=
11870 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11871 end if;
11873 Decl_Stack := Make_Component_Declaration (Loc,
11874 Defining_Identifier => Ent_Stack,
11876 Component_Definition =>
11877 Make_Component_Definition (Loc,
11878 Aliased_Present => True,
11879 Subtype_Indication => Make_Subtype_Indication (Loc,
11880 Subtype_Mark =>
11881 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11883 Constraint =>
11884 Make_Index_Or_Discriminant_Constraint (Loc,
11885 Constraints => New_List (Make_Range (Loc,
11886 Low_Bound => Make_Integer_Literal (Loc, 1),
11887 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11888 Task_Size)))))));
11890 Append_To (Cdecls, Decl_Stack);
11892 -- The appropriate alignment for the stack is ensured by the run-time
11893 -- code in charge of task creation.
11895 end if;
11897 -- Add components for entry families
11899 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11901 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11902 -- item is present.
11904 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11905 Append_To (Cdecls,
11906 Make_Component_Declaration (Loc,
11907 Defining_Identifier =>
11908 Make_Defining_Identifier (Loc, Name_uPriority),
11909 Component_Definition =>
11910 Make_Component_Definition (Loc,
11911 Aliased_Present => False,
11912 Subtype_Indication =>
11913 New_Occurrence_Of (Standard_Integer, Loc))));
11914 end if;
11916 -- Add the _Size component if a Storage_Size pragma is present
11918 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11919 Append_To (Cdecls,
11920 Make_Component_Declaration (Loc,
11921 Defining_Identifier =>
11922 Make_Defining_Identifier (Loc, Name_uSize),
11924 Component_Definition =>
11925 Make_Component_Definition (Loc,
11926 Aliased_Present => False,
11927 Subtype_Indication =>
11928 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
11930 Expression =>
11931 Convert_To (RTE (RE_Size_Type),
11932 Relocate_Node (
11933 Expression (First (
11934 Pragma_Argument_Associations (
11935 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11936 end if;
11938 -- Add the _Task_Info component if a Task_Info pragma is present
11940 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11941 Append_To (Cdecls,
11942 Make_Component_Declaration (Loc,
11943 Defining_Identifier =>
11944 Make_Defining_Identifier (Loc, Name_uTask_Info),
11946 Component_Definition =>
11947 Make_Component_Definition (Loc,
11948 Aliased_Present => False,
11949 Subtype_Indication =>
11950 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
11952 Expression => New_Copy (
11953 Expression (First (
11954 Pragma_Argument_Associations (
11955 Get_Rep_Pragma
11956 (TaskId, Name_Task_Info, Check_Parents => False)))))));
11957 end if;
11959 -- Add the _CPU component if a CPU rep item is present
11961 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
11962 Append_To (Cdecls,
11963 Make_Component_Declaration (Loc,
11964 Defining_Identifier =>
11965 Make_Defining_Identifier (Loc, Name_uCPU),
11967 Component_Definition =>
11968 Make_Component_Definition (Loc,
11969 Aliased_Present => False,
11970 Subtype_Indication =>
11971 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
11972 end if;
11974 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11975 -- present. If we are using a restricted run time this component will
11976 -- not be added (deadlines are not allowed by the Ravenscar profile).
11978 if not Restricted_Profile
11979 and then Present (Taskdef)
11980 and then Has_Relative_Deadline_Pragma (Taskdef)
11981 then
11982 Append_To (Cdecls,
11983 Make_Component_Declaration (Loc,
11984 Defining_Identifier =>
11985 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11987 Component_Definition =>
11988 Make_Component_Definition (Loc,
11989 Aliased_Present => False,
11990 Subtype_Indication =>
11991 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
11993 Expression =>
11994 Convert_To (RTE (RE_Time_Span),
11995 Relocate_Node (
11996 Expression (First (
11997 Pragma_Argument_Associations (
11998 Get_Relative_Deadline_Pragma (Taskdef))))))));
11999 end if;
12001 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12002 -- item is present. If we are using a restricted run time this component
12003 -- will not be added (dispatching domains are not allowed by the
12004 -- Ravenscar profile).
12006 if not Restricted_Profile
12007 and then
12008 Has_Rep_Item
12009 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12010 then
12011 Append_To (Cdecls,
12012 Make_Component_Declaration (Loc,
12013 Defining_Identifier =>
12014 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12016 Component_Definition =>
12017 Make_Component_Definition (Loc,
12018 Aliased_Present => False,
12019 Subtype_Indication =>
12020 New_Occurrence_Of
12021 (RTE (RE_Dispatching_Domain_Access), Loc))));
12022 end if;
12024 Insert_After (Size_Decl, Rec_Decl);
12026 -- Analyze the record declaration immediately after construction,
12027 -- because the initialization procedure is needed for single task
12028 -- declarations before the next entity is analyzed.
12030 Analyze (Rec_Decl);
12032 -- Create the declaration of the task body procedure
12034 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12035 Body_Decl :=
12036 Make_Subprogram_Declaration (Loc,
12037 Specification => Proc_Spec);
12039 Insert_After (Rec_Decl, Body_Decl);
12041 -- The subprogram does not comes from source, so we have to indicate the
12042 -- need for debugging information explicitly.
12044 if Comes_From_Source (Original_Node (N)) then
12045 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12046 end if;
12048 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12049 -- the corresponding record has been frozen.
12051 if Ada_Version >= Ada_2005 then
12052 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12053 end if;
12055 -- Ada 2005 (AI-345): We must defer freezing to allow further
12056 -- declaration of primitive subprograms covering task interfaces
12058 if Ada_Version <= Ada_95 then
12060 -- Now we can freeze the corresponding record. This needs manually
12061 -- freezing, since it is really part of the task type, and the task
12062 -- type is frozen at this stage. We of course need the initialization
12063 -- procedure for this corresponding record type and we won't get it
12064 -- in time if we don't freeze now.
12066 declare
12067 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12068 begin
12069 if Is_Non_Empty_List (L) then
12070 Insert_List_After (Body_Decl, L);
12071 end if;
12072 end;
12073 end if;
12075 -- Complete the expansion of access types to the current task type, if
12076 -- any were declared.
12078 Expand_Previous_Access_Type (Tasktyp);
12080 -- Create wrappers for entries that have pre/postconditions
12082 declare
12083 Ent : Entity_Id;
12085 begin
12086 Ent := First_Entity (Tasktyp);
12087 while Present (Ent) loop
12088 if Ekind_In (Ent, E_Entry, E_Entry_Family)
12089 and then Present (Pre_Post_Conditions (Contract (Ent)))
12090 then
12091 Build_PPC_Wrapper (Ent, N);
12092 end if;
12094 Next_Entity (Ent);
12095 end loop;
12096 end;
12097 end Expand_N_Task_Type_Declaration;
12099 -------------------------------
12100 -- Expand_N_Timed_Entry_Call --
12101 -------------------------------
12103 -- A timed entry call in normal case is not implemented using ATC mechanism
12104 -- anymore for efficiency reason.
12106 -- select
12107 -- T.E;
12108 -- S1;
12109 -- or
12110 -- delay D;
12111 -- S2;
12112 -- end select;
12114 -- is expanded as follows:
12116 -- 1) When T.E is a task entry_call;
12118 -- declare
12119 -- B : Boolean;
12120 -- X : Task_Entry_Index := <entry index>;
12121 -- DX : Duration := To_Duration (D);
12122 -- M : Delay_Mode := <discriminant>;
12123 -- P : parms := (parm, parm, parm);
12125 -- begin
12126 -- Timed_Protected_Entry_Call
12127 -- (<acceptor-task>, X, P'Address, DX, M, B);
12128 -- if B then
12129 -- S1;
12130 -- else
12131 -- S2;
12132 -- end if;
12133 -- end;
12135 -- 2) When T.E is a protected entry_call;
12137 -- declare
12138 -- B : Boolean;
12139 -- X : Protected_Entry_Index := <entry index>;
12140 -- DX : Duration := To_Duration (D);
12141 -- M : Delay_Mode := <discriminant>;
12142 -- P : parms := (parm, parm, parm);
12144 -- begin
12145 -- Timed_Protected_Entry_Call
12146 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12147 -- if B then
12148 -- S1;
12149 -- else
12150 -- S2;
12151 -- end if;
12152 -- end;
12154 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12155 -- is no delay and the triggering statements are executed. We first
12156 -- determine the kind of of the triggering call and then execute a
12157 -- synchronized operation or a direct call.
12159 -- declare
12160 -- B : Boolean := False;
12161 -- C : Ada.Tags.Prim_Op_Kind;
12162 -- DX : Duration := To_Duration (D)
12163 -- K : Ada.Tags.Tagged_Kind :=
12164 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12165 -- M : Integer :=...;
12166 -- P : Parameters := (Param1 .. ParamN);
12167 -- S : Integer;
12169 -- begin
12170 -- if K = Ada.Tags.TK_Limited_Tagged
12171 -- or else K = Ada.Tags.TK_Tagged
12172 -- then
12173 -- <dispatching-call>;
12174 -- B := True;
12176 -- else
12177 -- S :=
12178 -- Ada.Tags.Get_Offset_Index
12179 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12181 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12183 -- if C = POK_Protected_Entry
12184 -- or else C = POK_Task_Entry
12185 -- then
12186 -- Param1 := P.Param1;
12187 -- ...
12188 -- ParamN := P.ParamN;
12189 -- end if;
12191 -- if B then
12192 -- if C = POK_Procedure
12193 -- or else C = POK_Protected_Procedure
12194 -- or else C = POK_Task_Procedure
12195 -- then
12196 -- <dispatching-call>;
12197 -- end if;
12198 -- end if;
12199 -- end if;
12201 -- if B then
12202 -- <triggering-statements>
12203 -- else
12204 -- <timed-statements>
12205 -- end if;
12206 -- end;
12208 -- The triggering statement and the sequence of timed statements have not
12209 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12210 -- global references if within an instantiation.
12212 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12213 Loc : constant Source_Ptr := Sloc (N);
12215 Actuals : List_Id;
12216 Blk_Typ : Entity_Id;
12217 Call : Node_Id;
12218 Call_Ent : Entity_Id;
12219 Conc_Typ_Stmts : List_Id;
12220 Concval : Node_Id;
12221 D_Alt : constant Node_Id := Delay_Alternative (N);
12222 D_Conv : Node_Id;
12223 D_Disc : Node_Id;
12224 D_Stat : Node_Id := Delay_Statement (D_Alt);
12225 D_Stats : List_Id;
12226 D_Type : Entity_Id;
12227 Decls : List_Id;
12228 Dummy : Node_Id;
12229 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12230 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12231 E_Stats : List_Id;
12232 Ename : Node_Id;
12233 Formals : List_Id;
12234 Index : Node_Id;
12235 Is_Disp_Select : Boolean;
12236 Lim_Typ_Stmts : List_Id;
12237 N_Stats : List_Id;
12238 Obj : Entity_Id;
12239 Param : Node_Id;
12240 Params : List_Id;
12241 Stmt : Node_Id;
12242 Stmts : List_Id;
12243 Unpack : List_Id;
12245 B : Entity_Id; -- Call status flag
12246 C : Entity_Id; -- Call kind
12247 D : Entity_Id; -- Delay
12248 K : Entity_Id; -- Tagged kind
12249 M : Entity_Id; -- Delay mode
12250 P : Entity_Id; -- Parameter block
12251 S : Entity_Id; -- Primitive operation slot
12253 -- Start of processing for Expand_N_Timed_Entry_Call
12255 begin
12256 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12257 -- was already reported on spec, so do not attempt to expand the call.
12259 if Restriction_Active (No_Select_Statements) then
12260 return;
12261 end if;
12263 Process_Statements_For_Controlled_Objects (E_Alt);
12264 Process_Statements_For_Controlled_Objects (D_Alt);
12266 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12268 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12269 -- may wrap them in blocks.
12271 E_Stats := Statements (E_Alt);
12272 D_Stats := Statements (D_Alt);
12274 -- The arguments in the call may require dynamic allocation, and the
12275 -- call statement may have been transformed into a block. The block
12276 -- may contain additional declarations for internal entities, and the
12277 -- original call is found by sequential search.
12279 if Nkind (E_Call) = N_Block_Statement then
12280 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12281 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12282 N_Entry_Call_Statement)
12283 loop
12284 Next (E_Call);
12285 end loop;
12286 end if;
12288 Is_Disp_Select :=
12289 Ada_Version >= Ada_2005
12290 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12292 if Is_Disp_Select then
12293 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12294 Decls := New_List;
12296 Stmts := New_List;
12298 -- Generate:
12299 -- B : Boolean := False;
12301 B := Build_B (Loc, Decls);
12303 -- Generate:
12304 -- C : Ada.Tags.Prim_Op_Kind;
12306 C := Build_C (Loc, Decls);
12308 -- Because the analysis of all statements was disabled, manually
12309 -- analyze the delay statement.
12311 Analyze (D_Stat);
12312 D_Stat := Original_Node (D_Stat);
12314 else
12315 -- Build an entry call using Simple_Entry_Call
12317 Extract_Entry (E_Call, Concval, Ename, Index);
12318 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12320 Decls := Declarations (E_Call);
12321 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12323 if No (Decls) then
12324 Decls := New_List;
12325 end if;
12327 -- Generate:
12328 -- B : Boolean;
12330 B := Make_Defining_Identifier (Loc, Name_uB);
12332 Prepend_To (Decls,
12333 Make_Object_Declaration (Loc,
12334 Defining_Identifier => B,
12335 Object_Definition =>
12336 New_Occurrence_Of (Standard_Boolean, Loc)));
12337 end if;
12339 -- Duration and mode processing
12341 D_Type := Base_Type (Etype (Expression (D_Stat)));
12343 -- Use the type of the delay expression (Calendar or Real_Time) to
12344 -- generate the appropriate conversion.
12346 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12347 D_Disc := Make_Integer_Literal (Loc, 0);
12348 D_Conv := Relocate_Node (Expression (D_Stat));
12350 elsif Is_RTE (D_Type, RO_CA_Time) then
12351 D_Disc := Make_Integer_Literal (Loc, 1);
12352 D_Conv :=
12353 Make_Function_Call (Loc,
12354 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12355 Parameter_Associations =>
12356 New_List (New_Copy (Expression (D_Stat))));
12358 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12359 D_Disc := Make_Integer_Literal (Loc, 2);
12360 D_Conv :=
12361 Make_Function_Call (Loc,
12362 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12363 Parameter_Associations =>
12364 New_List (New_Copy (Expression (D_Stat))));
12365 end if;
12367 D := Make_Temporary (Loc, 'D');
12369 -- Generate:
12370 -- D : Duration;
12372 Append_To (Decls,
12373 Make_Object_Declaration (Loc,
12374 Defining_Identifier => D,
12375 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12377 M := Make_Temporary (Loc, 'M');
12379 -- Generate:
12380 -- M : Integer := (0 | 1 | 2);
12382 Append_To (Decls,
12383 Make_Object_Declaration (Loc,
12384 Defining_Identifier => M,
12385 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12386 Expression => D_Disc));
12388 -- Do the assignment at this stage only because the evaluation of the
12389 -- expression must not occur before (see ACVC C97302A).
12391 Append_To (Stmts,
12392 Make_Assignment_Statement (Loc,
12393 Name => New_Occurrence_Of (D, Loc),
12394 Expression => D_Conv));
12396 -- Parameter block processing
12398 -- Manually create the parameter block for dispatching calls. In the
12399 -- case of entries, the block has already been created during the call
12400 -- to Build_Simple_Entry_Call.
12402 if Is_Disp_Select then
12404 -- Tagged kind processing, generate:
12405 -- K : Ada.Tags.Tagged_Kind :=
12406 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12408 K := Build_K (Loc, Decls, Obj);
12410 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12411 P :=
12412 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12414 -- Dispatch table slot processing, generate:
12415 -- S : Integer;
12417 S := Build_S (Loc, Decls);
12419 -- Generate:
12420 -- S := Ada.Tags.Get_Offset_Index
12421 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12423 Conc_Typ_Stmts :=
12424 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12426 -- Generate:
12427 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12429 -- where Obj is the controlling formal parameter, S is the dispatch
12430 -- table slot number of the dispatching operation, P is the wrapped
12431 -- parameter block, D is the duration, M is the duration mode, C is
12432 -- the call kind and B is the call status.
12434 Params := New_List;
12436 Append_To (Params, New_Copy_Tree (Obj));
12437 Append_To (Params, New_Occurrence_Of (S, Loc));
12438 Append_To (Params,
12439 Make_Attribute_Reference (Loc,
12440 Prefix => New_Occurrence_Of (P, Loc),
12441 Attribute_Name => Name_Address));
12442 Append_To (Params, New_Occurrence_Of (D, Loc));
12443 Append_To (Params, New_Occurrence_Of (M, Loc));
12444 Append_To (Params, New_Occurrence_Of (C, Loc));
12445 Append_To (Params, New_Occurrence_Of (B, Loc));
12447 Append_To (Conc_Typ_Stmts,
12448 Make_Procedure_Call_Statement (Loc,
12449 Name =>
12450 New_Occurrence_Of
12451 (Find_Prim_Op
12452 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12453 Parameter_Associations => Params));
12455 -- Generate:
12456 -- if C = POK_Protected_Entry
12457 -- or else C = POK_Task_Entry
12458 -- then
12459 -- Param1 := P.Param1;
12460 -- ...
12461 -- ParamN := P.ParamN;
12462 -- end if;
12464 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12466 -- Generate the if statement only when the packed parameters need
12467 -- explicit assignments to their corresponding actuals.
12469 if Present (Unpack) then
12470 Append_To (Conc_Typ_Stmts,
12471 Make_Implicit_If_Statement (N,
12473 Condition =>
12474 Make_Or_Else (Loc,
12475 Left_Opnd =>
12476 Make_Op_Eq (Loc,
12477 Left_Opnd => New_Occurrence_Of (C, Loc),
12478 Right_Opnd =>
12479 New_Occurrence_Of
12480 (RTE (RE_POK_Protected_Entry), Loc)),
12482 Right_Opnd =>
12483 Make_Op_Eq (Loc,
12484 Left_Opnd => New_Occurrence_Of (C, Loc),
12485 Right_Opnd =>
12486 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12488 Then_Statements => Unpack));
12489 end if;
12491 -- Generate:
12493 -- if B then
12494 -- if C = POK_Procedure
12495 -- or else C = POK_Protected_Procedure
12496 -- or else C = POK_Task_Procedure
12497 -- then
12498 -- <dispatching-call>
12499 -- end if;
12500 -- end if;
12502 N_Stats := New_List (
12503 Make_Implicit_If_Statement (N,
12504 Condition =>
12505 Make_Or_Else (Loc,
12506 Left_Opnd =>
12507 Make_Op_Eq (Loc,
12508 Left_Opnd => New_Occurrence_Of (C, Loc),
12509 Right_Opnd =>
12510 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12512 Right_Opnd =>
12513 Make_Or_Else (Loc,
12514 Left_Opnd =>
12515 Make_Op_Eq (Loc,
12516 Left_Opnd => New_Occurrence_Of (C, Loc),
12517 Right_Opnd =>
12518 New_Occurrence_Of (RTE (
12519 RE_POK_Protected_Procedure), Loc)),
12520 Right_Opnd =>
12521 Make_Op_Eq (Loc,
12522 Left_Opnd => New_Occurrence_Of (C, Loc),
12523 Right_Opnd =>
12524 New_Occurrence_Of
12525 (RTE (RE_POK_Task_Procedure), Loc)))),
12527 Then_Statements => New_List (E_Call)));
12529 Append_To (Conc_Typ_Stmts,
12530 Make_Implicit_If_Statement (N,
12531 Condition => New_Occurrence_Of (B, Loc),
12532 Then_Statements => N_Stats));
12534 -- Generate:
12535 -- <dispatching-call>;
12536 -- B := True;
12538 Lim_Typ_Stmts :=
12539 New_List (New_Copy_Tree (E_Call),
12540 Make_Assignment_Statement (Loc,
12541 Name => New_Occurrence_Of (B, Loc),
12542 Expression => New_Occurrence_Of (Standard_True, Loc)));
12544 -- Generate:
12545 -- if K = Ada.Tags.TK_Limited_Tagged
12546 -- or else K = Ada.Tags.TK_Tagged
12547 -- then
12548 -- Lim_Typ_Stmts
12549 -- else
12550 -- Conc_Typ_Stmts
12551 -- end if;
12553 Append_To (Stmts,
12554 Make_Implicit_If_Statement (N,
12555 Condition => Build_Dispatching_Tag_Check (K, N),
12556 Then_Statements => Lim_Typ_Stmts,
12557 Else_Statements => Conc_Typ_Stmts));
12559 -- Generate:
12561 -- if B then
12562 -- <triggering-statements>
12563 -- else
12564 -- <timed-statements>
12565 -- end if;
12567 Append_To (Stmts,
12568 Make_Implicit_If_Statement (N,
12569 Condition => New_Occurrence_Of (B, Loc),
12570 Then_Statements => E_Stats,
12571 Else_Statements => D_Stats));
12573 else
12574 -- Simple case of a non-dispatching trigger. Skip assignments to
12575 -- temporaries created for in-out parameters.
12577 -- This makes unwarranted assumptions about the shape of the expanded
12578 -- tree for the call, and should be cleaned up ???
12580 Stmt := First (Stmts);
12581 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12582 Next (Stmt);
12583 end loop;
12585 -- Do the assignment at this stage only because the evaluation
12586 -- of the expression must not occur before (see ACVC C97302A).
12588 Insert_Before (Stmt,
12589 Make_Assignment_Statement (Loc,
12590 Name => New_Occurrence_Of (D, Loc),
12591 Expression => D_Conv));
12593 Call := Stmt;
12594 Params := Parameter_Associations (Call);
12596 -- For a protected type, we build a Timed_Protected_Entry_Call
12598 if Is_Protected_Type (Etype (Concval)) then
12600 -- Create a new call statement
12602 Param := First (Params);
12603 while Present (Param)
12604 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12605 loop
12606 Next (Param);
12607 end loop;
12609 Dummy := Remove_Next (Next (Param));
12611 -- Remove garbage is following the Cancel_Param if present
12613 Dummy := Next (Param);
12615 -- Remove the mode of the Protected_Entry_Call call, then remove
12616 -- the Communication_Block of the Protected_Entry_Call call, and
12617 -- finally add Duration and a Delay_Mode parameter
12619 pragma Assert (Present (Param));
12620 Rewrite (Param, New_Occurrence_Of (D, Loc));
12622 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12624 -- Add a Boolean flag for successful entry call
12626 Append_To (Params, New_Occurrence_Of (B, Loc));
12628 case Corresponding_Runtime_Package (Etype (Concval)) is
12629 when System_Tasking_Protected_Objects_Entries =>
12630 Rewrite (Call,
12631 Make_Procedure_Call_Statement (Loc,
12632 Name =>
12633 New_Occurrence_Of
12634 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12635 Parameter_Associations => Params));
12637 when others =>
12638 raise Program_Error;
12639 end case;
12641 -- For the task case, build a Timed_Task_Entry_Call
12643 else
12644 -- Create a new call statement
12646 Append_To (Params, New_Occurrence_Of (D, Loc));
12647 Append_To (Params, New_Occurrence_Of (M, Loc));
12648 Append_To (Params, New_Occurrence_Of (B, Loc));
12650 Rewrite (Call,
12651 Make_Procedure_Call_Statement (Loc,
12652 Name =>
12653 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12654 Parameter_Associations => Params));
12655 end if;
12657 Append_To (Stmts,
12658 Make_Implicit_If_Statement (N,
12659 Condition => New_Occurrence_Of (B, Loc),
12660 Then_Statements => E_Stats,
12661 Else_Statements => D_Stats));
12662 end if;
12664 Rewrite (N,
12665 Make_Block_Statement (Loc,
12666 Declarations => Decls,
12667 Handled_Statement_Sequence =>
12668 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12670 Analyze (N);
12671 end Expand_N_Timed_Entry_Call;
12673 ----------------------------------------
12674 -- Expand_Protected_Body_Declarations --
12675 ----------------------------------------
12677 procedure Expand_Protected_Body_Declarations
12678 (N : Node_Id;
12679 Spec_Id : Entity_Id)
12681 begin
12682 if No_Run_Time_Mode then
12683 Error_Msg_CRT ("protected body", N);
12684 return;
12686 elsif Expander_Active then
12688 -- Associate discriminals with the first subprogram or entry body to
12689 -- be expanded.
12691 if Present (First_Protected_Operation (Declarations (N))) then
12692 Set_Discriminals (Parent (Spec_Id));
12693 end if;
12694 end if;
12695 end Expand_Protected_Body_Declarations;
12697 -------------------------
12698 -- External_Subprogram --
12699 -------------------------
12701 function External_Subprogram (E : Entity_Id) return Entity_Id is
12702 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12704 begin
12705 -- The internal and external subprograms follow each other on the entity
12706 -- chain. Note that previously private operations had no separate
12707 -- external subprogram. We now create one in all cases, because a
12708 -- private operation may actually appear in an external call, through
12709 -- a 'Access reference used for a callback.
12711 -- If the operation is a function that returns an anonymous access type,
12712 -- the corresponding itype appears before the operation, and must be
12713 -- skipped.
12715 -- This mechanism is fragile, there should be a real link between the
12716 -- two versions of the operation, but there is no place to put it ???
12718 if Is_Access_Type (Next_Entity (Subp)) then
12719 return Next_Entity (Next_Entity (Subp));
12720 else
12721 return Next_Entity (Subp);
12722 end if;
12723 end External_Subprogram;
12725 ------------------------------
12726 -- Extract_Dispatching_Call --
12727 ------------------------------
12729 procedure Extract_Dispatching_Call
12730 (N : Node_Id;
12731 Call_Ent : out Entity_Id;
12732 Object : out Entity_Id;
12733 Actuals : out List_Id;
12734 Formals : out List_Id)
12736 Call_Nam : Node_Id;
12738 begin
12739 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12741 if Present (Original_Node (N)) then
12742 Call_Nam := Name (Original_Node (N));
12743 else
12744 Call_Nam := Name (N);
12745 end if;
12747 -- Retrieve the name of the dispatching procedure. It contains the
12748 -- dispatch table slot number.
12750 loop
12751 case Nkind (Call_Nam) is
12752 when N_Identifier =>
12753 exit;
12755 when N_Selected_Component =>
12756 Call_Nam := Selector_Name (Call_Nam);
12758 when others =>
12759 raise Program_Error;
12761 end case;
12762 end loop;
12764 Actuals := Parameter_Associations (N);
12765 Call_Ent := Entity (Call_Nam);
12766 Formals := Parameter_Specifications (Parent (Call_Ent));
12767 Object := First (Actuals);
12769 if Present (Original_Node (Object)) then
12770 Object := Original_Node (Object);
12771 end if;
12773 -- If the type of the dispatching object is an access type then return
12774 -- an explicit dereference.
12776 if Is_Access_Type (Etype (Object)) then
12777 Object := Make_Explicit_Dereference (Sloc (N), Object);
12778 Analyze (Object);
12779 end if;
12780 end Extract_Dispatching_Call;
12782 -------------------
12783 -- Extract_Entry --
12784 -------------------
12786 procedure Extract_Entry
12787 (N : Node_Id;
12788 Concval : out Node_Id;
12789 Ename : out Node_Id;
12790 Index : out Node_Id)
12792 Nam : constant Node_Id := Name (N);
12794 begin
12795 -- For a simple entry, the name is a selected component, with the
12796 -- prefix being the task value, and the selector being the entry.
12798 if Nkind (Nam) = N_Selected_Component then
12799 Concval := Prefix (Nam);
12800 Ename := Selector_Name (Nam);
12801 Index := Empty;
12803 -- For a member of an entry family, the name is an indexed component
12804 -- where the prefix is a selected component, whose prefix in turn is
12805 -- the task value, and whose selector is the entry family. The single
12806 -- expression in the expressions list of the indexed component is the
12807 -- subscript for the family.
12809 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12810 Concval := Prefix (Prefix (Nam));
12811 Ename := Selector_Name (Prefix (Nam));
12812 Index := First (Expressions (Nam));
12813 end if;
12815 -- Through indirection, the type may actually be a limited view of a
12816 -- concurrent type. When compiling a call, the non-limited view of the
12817 -- type is visible.
12819 if From_Limited_With (Etype (Concval)) then
12820 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
12821 end if;
12822 end Extract_Entry;
12824 -------------------
12825 -- Family_Offset --
12826 -------------------
12828 function Family_Offset
12829 (Loc : Source_Ptr;
12830 Hi : Node_Id;
12831 Lo : Node_Id;
12832 Ttyp : Entity_Id;
12833 Cap : Boolean) return Node_Id
12835 Ityp : Entity_Id;
12836 Real_Hi : Node_Id;
12837 Real_Lo : Node_Id;
12839 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12840 -- If one of the bounds is a reference to a discriminant, replace with
12841 -- corresponding discriminal of type. Within the body of a task retrieve
12842 -- the renamed discriminant by simple visibility, using its generated
12843 -- name. Within a protected object, find the original discriminant and
12844 -- replace it with the discriminal of the current protected operation.
12846 ------------------------------
12847 -- Convert_Discriminant_Ref --
12848 ------------------------------
12850 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12851 Loc : constant Source_Ptr := Sloc (Bound);
12852 B : Node_Id;
12853 D : Entity_Id;
12855 begin
12856 if Is_Entity_Name (Bound)
12857 and then Ekind (Entity (Bound)) = E_Discriminant
12858 then
12859 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
12860 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12861 Find_Direct_Name (B);
12863 elsif Is_Protected_Type (Ttyp) then
12864 D := First_Discriminant (Ttyp);
12865 while Chars (D) /= Chars (Entity (Bound)) loop
12866 Next_Discriminant (D);
12867 end loop;
12869 B := New_Occurrence_Of (Discriminal (D), Loc);
12871 else
12872 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
12873 end if;
12875 elsif Nkind (Bound) = N_Attribute_Reference then
12876 return Bound;
12878 else
12879 B := New_Copy_Tree (Bound);
12880 end if;
12882 return
12883 Make_Attribute_Reference (Loc,
12884 Attribute_Name => Name_Pos,
12885 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12886 Expressions => New_List (B));
12887 end Convert_Discriminant_Ref;
12889 -- Start of processing for Family_Offset
12891 begin
12892 Real_Hi := Convert_Discriminant_Ref (Hi);
12893 Real_Lo := Convert_Discriminant_Ref (Lo);
12895 if Cap then
12896 if Is_Task_Type (Ttyp) then
12897 Ityp := RTE (RE_Task_Entry_Index);
12898 else
12899 Ityp := RTE (RE_Protected_Entry_Index);
12900 end if;
12902 Real_Hi :=
12903 Make_Attribute_Reference (Loc,
12904 Prefix => New_Occurrence_Of (Ityp, Loc),
12905 Attribute_Name => Name_Min,
12906 Expressions => New_List (
12907 Real_Hi,
12908 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12910 Real_Lo :=
12911 Make_Attribute_Reference (Loc,
12912 Prefix => New_Occurrence_Of (Ityp, Loc),
12913 Attribute_Name => Name_Max,
12914 Expressions => New_List (
12915 Real_Lo,
12916 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12917 end if;
12919 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12920 end Family_Offset;
12922 -----------------
12923 -- Family_Size --
12924 -----------------
12926 function Family_Size
12927 (Loc : Source_Ptr;
12928 Hi : Node_Id;
12929 Lo : Node_Id;
12930 Ttyp : Entity_Id;
12931 Cap : Boolean) return Node_Id
12933 Ityp : Entity_Id;
12935 begin
12936 if Is_Task_Type (Ttyp) then
12937 Ityp := RTE (RE_Task_Entry_Index);
12938 else
12939 Ityp := RTE (RE_Protected_Entry_Index);
12940 end if;
12942 return
12943 Make_Attribute_Reference (Loc,
12944 Prefix => New_Occurrence_Of (Ityp, Loc),
12945 Attribute_Name => Name_Max,
12946 Expressions => New_List (
12947 Make_Op_Add (Loc,
12948 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12949 Right_Opnd => Make_Integer_Literal (Loc, 1)),
12950 Make_Integer_Literal (Loc, 0)));
12951 end Family_Size;
12953 ----------------------------
12954 -- Find_Enclosing_Context --
12955 ----------------------------
12957 procedure Find_Enclosing_Context
12958 (N : Node_Id;
12959 Context : out Node_Id;
12960 Context_Id : out Entity_Id;
12961 Context_Decls : out List_Id)
12963 begin
12964 -- Traverse the parent chain looking for an enclosing body, block,
12965 -- package or return statement.
12967 Context := Parent (N);
12968 while not Nkind_In (Context, N_Block_Statement,
12969 N_Entry_Body,
12970 N_Extended_Return_Statement,
12971 N_Package_Body,
12972 N_Package_Declaration,
12973 N_Subprogram_Body,
12974 N_Task_Body)
12975 loop
12976 Context := Parent (Context);
12977 end loop;
12979 -- Extract the constituents of the context
12981 if Nkind (Context) = N_Extended_Return_Statement then
12982 Context_Decls := Return_Object_Declarations (Context);
12983 Context_Id := Return_Statement_Entity (Context);
12985 -- Package declarations and bodies use a common library-level activation
12986 -- chain or task master, therefore return the package declaration as the
12987 -- proper carrier for the appropriate flag.
12989 elsif Nkind (Context) = N_Package_Body then
12990 Context_Decls := Declarations (Context);
12991 Context_Id := Corresponding_Spec (Context);
12992 Context := Parent (Context_Id);
12994 if Nkind (Context) = N_Defining_Program_Unit_Name then
12995 Context := Parent (Parent (Context));
12996 else
12997 Context := Parent (Context);
12998 end if;
13000 elsif Nkind (Context) = N_Package_Declaration then
13001 Context_Decls := Visible_Declarations (Specification (Context));
13002 Context_Id := Defining_Unit_Name (Specification (Context));
13004 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13005 Context_Id := Defining_Identifier (Context_Id);
13006 end if;
13008 else
13009 Context_Decls := Declarations (Context);
13011 if Nkind (Context) = N_Block_Statement then
13012 Context_Id := Entity (Identifier (Context));
13014 elsif Nkind (Context) = N_Entry_Body then
13015 Context_Id := Defining_Identifier (Context);
13017 elsif Nkind (Context) = N_Subprogram_Body then
13018 if Present (Corresponding_Spec (Context)) then
13019 Context_Id := Corresponding_Spec (Context);
13020 else
13021 Context_Id := Defining_Unit_Name (Specification (Context));
13023 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13024 Context_Id := Defining_Identifier (Context_Id);
13025 end if;
13026 end if;
13028 elsif Nkind (Context) = N_Task_Body then
13029 Context_Id := Corresponding_Spec (Context);
13031 else
13032 raise Program_Error;
13033 end if;
13034 end if;
13036 pragma Assert (Present (Context));
13037 pragma Assert (Present (Context_Id));
13038 pragma Assert (Present (Context_Decls));
13039 end Find_Enclosing_Context;
13041 -----------------------
13042 -- Find_Master_Scope --
13043 -----------------------
13045 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13046 S : Entity_Id;
13048 begin
13049 -- In Ada 2005, the master is the innermost enclosing scope that is not
13050 -- transient. If the enclosing block is the rewriting of a call or the
13051 -- scope is an extended return statement this is valid master. The
13052 -- master in an extended return is only used within the return, and is
13053 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13054 -- now before that overwriting occurs.
13056 S := Scope (E);
13058 if Ada_Version >= Ada_2005 then
13059 while Is_Internal (S) loop
13060 if Nkind (Parent (S)) = N_Block_Statement
13061 and then
13062 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13063 then
13064 exit;
13066 elsif Ekind (S) = E_Return_Statement then
13067 exit;
13069 else
13070 S := Scope (S);
13071 end if;
13072 end loop;
13073 end if;
13075 return S;
13076 end Find_Master_Scope;
13078 -------------------------------
13079 -- First_Protected_Operation --
13080 -------------------------------
13082 function First_Protected_Operation (D : List_Id) return Node_Id is
13083 First_Op : Node_Id;
13085 begin
13086 First_Op := First (D);
13087 while Present (First_Op)
13088 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13089 loop
13090 Next (First_Op);
13091 end loop;
13093 return First_Op;
13094 end First_Protected_Operation;
13096 ---------------------------------------
13097 -- Install_Private_Data_Declarations --
13098 ---------------------------------------
13100 procedure Install_Private_Data_Declarations
13101 (Loc : Source_Ptr;
13102 Spec_Id : Entity_Id;
13103 Conc_Typ : Entity_Id;
13104 Body_Nod : Node_Id;
13105 Decls : List_Id;
13106 Barrier : Boolean := False;
13107 Family : Boolean := False)
13109 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13110 Decl : Node_Id;
13111 Def : Node_Id;
13112 Insert_Node : Node_Id := Empty;
13113 Obj_Ent : Entity_Id;
13115 procedure Add (Decl : Node_Id);
13116 -- Add a single declaration after Insert_Node. If this is the first
13117 -- addition, Decl is added to the front of Decls and it becomes the
13118 -- insertion node.
13120 function Replace_Bound (Bound : Node_Id) return Node_Id;
13121 -- The bounds of an entry index may depend on discriminants, create a
13122 -- reference to the corresponding prival. Otherwise return a duplicate
13123 -- of the original bound.
13125 ---------
13126 -- Add --
13127 ---------
13129 procedure Add (Decl : Node_Id) is
13130 begin
13131 if No (Insert_Node) then
13132 Prepend_To (Decls, Decl);
13133 else
13134 Insert_After (Insert_Node, Decl);
13135 end if;
13137 Insert_Node := Decl;
13138 end Add;
13140 --------------------------
13141 -- Replace_Discriminant --
13142 --------------------------
13144 function Replace_Bound (Bound : Node_Id) return Node_Id is
13145 begin
13146 if Nkind (Bound) = N_Identifier
13147 and then Is_Discriminal (Entity (Bound))
13148 then
13149 return Make_Identifier (Loc, Chars (Entity (Bound)));
13150 else
13151 return Duplicate_Subexpr (Bound);
13152 end if;
13153 end Replace_Bound;
13155 -- Start of processing for Install_Private_Data_Declarations
13157 begin
13158 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13159 -- formal parameter _O, _object or _task depending on the context.
13161 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13163 -- Special processing of _O for barrier functions, protected entries
13164 -- and families.
13166 if Barrier
13167 or else
13168 (Is_Protected
13169 and then
13170 (Ekind (Spec_Id) = E_Entry
13171 or else Ekind (Spec_Id) = E_Entry_Family))
13172 then
13173 declare
13174 Conc_Rec : constant Entity_Id :=
13175 Corresponding_Record_Type (Conc_Typ);
13176 Typ_Id : constant Entity_Id :=
13177 Make_Defining_Identifier (Loc,
13178 New_External_Name (Chars (Conc_Rec), 'P'));
13179 begin
13180 -- Generate:
13181 -- type prot_typVP is access prot_typV;
13183 Decl :=
13184 Make_Full_Type_Declaration (Loc,
13185 Defining_Identifier => Typ_Id,
13186 Type_Definition =>
13187 Make_Access_To_Object_Definition (Loc,
13188 Subtype_Indication =>
13189 New_Occurrence_Of (Conc_Rec, Loc)));
13190 Add (Decl);
13192 -- Generate:
13193 -- _object : prot_typVP := prot_typV (_O);
13195 Decl :=
13196 Make_Object_Declaration (Loc,
13197 Defining_Identifier =>
13198 Make_Defining_Identifier (Loc, Name_uObject),
13199 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13200 Expression =>
13201 Unchecked_Convert_To (Typ_Id,
13202 New_Occurrence_Of (Obj_Ent, Loc)));
13203 Add (Decl);
13205 -- Set the reference to the concurrent object
13207 Obj_Ent := Defining_Identifier (Decl);
13208 end;
13209 end if;
13211 -- Step 2: Create the Protection object and build its declaration for
13212 -- any protected entry (family) of subprogram. Note for the lock-free
13213 -- implementation, the Protection object is not needed anymore.
13215 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13216 declare
13217 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13218 Prot_Typ : RE_Id;
13220 begin
13221 Set_Protection_Object (Spec_Id, Prot_Ent);
13223 -- Determine the proper protection type
13225 if Has_Attach_Handler (Conc_Typ)
13226 and then not Restricted_Profile
13227 then
13228 Prot_Typ := RE_Static_Interrupt_Protection;
13230 elsif Has_Interrupt_Handler (Conc_Typ)
13231 and then not Restriction_Active (No_Dynamic_Attachment)
13232 then
13233 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13235 else
13236 case Corresponding_Runtime_Package (Conc_Typ) is
13237 when System_Tasking_Protected_Objects_Entries =>
13238 Prot_Typ := RE_Protection_Entries;
13240 when System_Tasking_Protected_Objects_Single_Entry =>
13241 Prot_Typ := RE_Protection_Entry;
13243 when System_Tasking_Protected_Objects =>
13244 Prot_Typ := RE_Protection;
13246 when others =>
13247 raise Program_Error;
13248 end case;
13249 end if;
13251 -- Generate:
13252 -- conc_typR : protection_typ renames _object._object;
13254 Decl :=
13255 Make_Object_Renaming_Declaration (Loc,
13256 Defining_Identifier => Prot_Ent,
13257 Subtype_Mark =>
13258 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13259 Name =>
13260 Make_Selected_Component (Loc,
13261 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13262 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13263 Add (Decl);
13264 end;
13265 end if;
13267 -- Step 3: Add discriminant renamings (if any)
13269 if Has_Discriminants (Conc_Typ) then
13270 declare
13271 D : Entity_Id;
13273 begin
13274 D := First_Discriminant (Conc_Typ);
13275 while Present (D) loop
13277 -- Adjust the source location
13279 Set_Sloc (Discriminal (D), Loc);
13281 -- Generate:
13282 -- discr_name : discr_typ renames _object.discr_name;
13283 -- or
13284 -- discr_name : discr_typ renames _task.discr_name;
13286 Decl :=
13287 Make_Object_Renaming_Declaration (Loc,
13288 Defining_Identifier => Discriminal (D),
13289 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13290 Name =>
13291 Make_Selected_Component (Loc,
13292 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13293 Selector_Name => Make_Identifier (Loc, Chars (D))));
13294 Add (Decl);
13296 Next_Discriminant (D);
13297 end loop;
13298 end;
13299 end if;
13301 -- Step 4: Add private component renamings (if any)
13303 if Is_Protected then
13304 Def := Protected_Definition (Parent (Conc_Typ));
13306 if Present (Private_Declarations (Def)) then
13307 declare
13308 Comp : Node_Id;
13309 Comp_Id : Entity_Id;
13310 Decl_Id : Entity_Id;
13312 begin
13313 Comp := First (Private_Declarations (Def));
13314 while Present (Comp) loop
13315 if Nkind (Comp) = N_Component_Declaration then
13316 Comp_Id := Defining_Identifier (Comp);
13317 Decl_Id :=
13318 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13320 -- Minimal decoration
13322 if Ekind (Spec_Id) = E_Function then
13323 Set_Ekind (Decl_Id, E_Constant);
13324 else
13325 Set_Ekind (Decl_Id, E_Variable);
13326 end if;
13328 Set_Prival (Comp_Id, Decl_Id);
13329 Set_Prival_Link (Decl_Id, Comp_Id);
13330 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13332 -- Generate:
13333 -- comp_name : comp_typ renames _object.comp_name;
13335 Decl :=
13336 Make_Object_Renaming_Declaration (Loc,
13337 Defining_Identifier => Decl_Id,
13338 Subtype_Mark =>
13339 New_Occurrence_Of (Etype (Comp_Id), Loc),
13340 Name =>
13341 Make_Selected_Component (Loc,
13342 Prefix =>
13343 New_Occurrence_Of (Obj_Ent, Loc),
13344 Selector_Name =>
13345 Make_Identifier (Loc, Chars (Comp_Id))));
13346 Add (Decl);
13347 end if;
13349 Next (Comp);
13350 end loop;
13351 end;
13352 end if;
13353 end if;
13355 -- Step 5: Add the declaration of the entry index and the associated
13356 -- type for barrier functions and entry families.
13358 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13359 declare
13360 E : constant Entity_Id := Index_Object (Spec_Id);
13361 Index : constant Entity_Id :=
13362 Defining_Identifier
13363 (Entry_Index_Specification
13364 (Entry_Body_Formal_Part (Body_Nod)));
13365 Index_Con : constant Entity_Id :=
13366 Make_Defining_Identifier (Loc, Chars (Index));
13367 High : Node_Id;
13368 Index_Typ : Entity_Id;
13369 Low : Node_Id;
13371 begin
13372 -- Minimal decoration
13374 Set_Ekind (Index_Con, E_Constant);
13375 Set_Entry_Index_Constant (Index, Index_Con);
13376 Set_Discriminal_Link (Index_Con, Index);
13378 -- Retrieve the bounds of the entry family
13380 High := Type_High_Bound (Etype (Index));
13381 Low := Type_Low_Bound (Etype (Index));
13383 -- In the simple case the entry family is given by a subtype
13384 -- mark and the index constant has the same type.
13386 if Is_Entity_Name (Original_Node (
13387 Discrete_Subtype_Definition (Parent (Index))))
13388 then
13389 Index_Typ := Etype (Index);
13391 -- Otherwise a new subtype declaration is required
13393 else
13394 High := Replace_Bound (High);
13395 Low := Replace_Bound (Low);
13397 Index_Typ := Make_Temporary (Loc, 'J');
13399 -- Generate:
13400 -- subtype Jnn is <Etype of Index> range Low .. High;
13402 Decl :=
13403 Make_Subtype_Declaration (Loc,
13404 Defining_Identifier => Index_Typ,
13405 Subtype_Indication =>
13406 Make_Subtype_Indication (Loc,
13407 Subtype_Mark =>
13408 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13409 Constraint =>
13410 Make_Range_Constraint (Loc,
13411 Range_Expression =>
13412 Make_Range (Loc, Low, High))));
13413 Add (Decl);
13414 end if;
13416 Set_Etype (Index_Con, Index_Typ);
13418 -- Create the object which designates the index:
13419 -- J : constant Jnn :=
13420 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13422 -- where Jnn is the subtype created above or the original type of
13423 -- the index, _E is a formal of the protected body subprogram and
13424 -- <index expr> is the index of the first family member.
13426 Decl :=
13427 Make_Object_Declaration (Loc,
13428 Defining_Identifier => Index_Con,
13429 Constant_Present => True,
13430 Object_Definition =>
13431 New_Occurrence_Of (Index_Typ, Loc),
13433 Expression =>
13434 Make_Attribute_Reference (Loc,
13435 Prefix =>
13436 New_Occurrence_Of (Index_Typ, Loc),
13437 Attribute_Name => Name_Val,
13439 Expressions => New_List (
13441 Make_Op_Add (Loc,
13442 Left_Opnd =>
13443 Make_Op_Subtract (Loc,
13444 Left_Opnd => New_Occurrence_Of (E, Loc),
13445 Right_Opnd =>
13446 Entry_Index_Expression (Loc,
13447 Defining_Identifier (Body_Nod),
13448 Empty, Conc_Typ)),
13450 Right_Opnd =>
13451 Make_Attribute_Reference (Loc,
13452 Prefix =>
13453 New_Occurrence_Of (Index_Typ, Loc),
13454 Attribute_Name => Name_Pos,
13455 Expressions => New_List (
13456 Make_Attribute_Reference (Loc,
13457 Prefix =>
13458 New_Occurrence_Of (Index_Typ, Loc),
13459 Attribute_Name => Name_First)))))));
13460 Add (Decl);
13461 end;
13462 end if;
13463 end Install_Private_Data_Declarations;
13465 -----------------------
13466 -- Is_Exception_Safe --
13467 -----------------------
13469 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13471 function Has_Side_Effect (N : Node_Id) return Boolean;
13472 -- Return True whenever encountering a subprogram call or raise
13473 -- statement of any kind in the sequence of statements
13475 ---------------------
13476 -- Has_Side_Effect --
13477 ---------------------
13479 -- What is this doing buried two levels down in exp_ch9. It seems like a
13480 -- generally useful function, and indeed there may be code duplication
13481 -- going on here ???
13483 function Has_Side_Effect (N : Node_Id) return Boolean is
13484 Stmt : Node_Id;
13485 Expr : Node_Id;
13487 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13488 -- Indicate whether N is a subprogram call or a raise statement
13490 ----------------------
13491 -- Is_Call_Or_Raise --
13492 ----------------------
13494 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13495 begin
13496 return Nkind_In (N, N_Procedure_Call_Statement,
13497 N_Function_Call,
13498 N_Raise_Statement,
13499 N_Raise_Constraint_Error,
13500 N_Raise_Program_Error,
13501 N_Raise_Storage_Error);
13502 end Is_Call_Or_Raise;
13504 -- Start of processing for Has_Side_Effect
13506 begin
13507 Stmt := N;
13508 while Present (Stmt) loop
13509 if Is_Call_Or_Raise (Stmt) then
13510 return True;
13511 end if;
13513 -- An object declaration can also contain a function call or a
13514 -- raise statement.
13516 if Nkind (Stmt) = N_Object_Declaration then
13517 Expr := Expression (Stmt);
13519 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13520 return True;
13521 end if;
13522 end if;
13524 Next (Stmt);
13525 end loop;
13527 return False;
13528 end Has_Side_Effect;
13530 -- Start of processing for Is_Exception_Safe
13532 begin
13533 -- When exceptions can't be propagated, the subprogram returns normally
13535 if No_Exception_Handlers_Set then
13536 return True;
13537 end if;
13539 -- If the checks handled by the back end are not disabled, we cannot
13540 -- ensure that no exception will be raised.
13542 if not Access_Checks_Suppressed (Empty)
13543 or else not Discriminant_Checks_Suppressed (Empty)
13544 or else not Range_Checks_Suppressed (Empty)
13545 or else not Index_Checks_Suppressed (Empty)
13546 or else Opt.Stack_Checking_Enabled
13547 then
13548 return False;
13549 end if;
13551 if Has_Side_Effect (First (Declarations (Subprogram)))
13552 or else
13553 Has_Side_Effect
13554 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13555 then
13556 return False;
13557 else
13558 return True;
13559 end if;
13560 end Is_Exception_Safe;
13562 ---------------------------------
13563 -- Is_Potentially_Large_Family --
13564 ---------------------------------
13566 function Is_Potentially_Large_Family
13567 (Base_Index : Entity_Id;
13568 Conctyp : Entity_Id;
13569 Lo : Node_Id;
13570 Hi : Node_Id) return Boolean
13572 begin
13573 return Scope (Base_Index) = Standard_Standard
13574 and then Base_Index = Base_Type (Standard_Integer)
13575 and then Has_Discriminants (Conctyp)
13576 and then
13577 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13578 and then
13579 (Denotes_Discriminant (Lo, True)
13580 or else
13581 Denotes_Discriminant (Hi, True));
13582 end Is_Potentially_Large_Family;
13584 -------------------------------------
13585 -- Is_Private_Primitive_Subprogram --
13586 -------------------------------------
13588 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13589 begin
13590 return
13591 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13592 and then Is_Private_Primitive (Id);
13593 end Is_Private_Primitive_Subprogram;
13595 ------------------
13596 -- Index_Object --
13597 ------------------
13599 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13600 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13601 Formal : Entity_Id;
13603 begin
13604 Formal := First_Formal (Bod_Subp);
13605 while Present (Formal) loop
13607 -- Look for formal parameter _E
13609 if Chars (Formal) = Name_uE then
13610 return Formal;
13611 end if;
13613 Next_Formal (Formal);
13614 end loop;
13616 -- A protected body subprogram should always have the parameter in
13617 -- question.
13619 raise Program_Error;
13620 end Index_Object;
13622 --------------------------------
13623 -- Make_Initialize_Protection --
13624 --------------------------------
13626 function Make_Initialize_Protection
13627 (Protect_Rec : Entity_Id) return List_Id
13629 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13630 P_Arr : Entity_Id;
13631 Pdec : Node_Id;
13632 Ptyp : constant Node_Id :=
13633 Corresponding_Concurrent_Type (Protect_Rec);
13634 Args : List_Id;
13635 L : constant List_Id := New_List;
13636 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13637 Prio_Type : Entity_Id;
13638 Prio_Var : Entity_Id := Empty;
13639 Restricted : constant Boolean := Restricted_Profile;
13641 begin
13642 -- We may need two calls to properly initialize the object, one to
13643 -- Initialize_Protection, and possibly one to Install_Handlers if we
13644 -- have a pragma Attach_Handler.
13646 -- Get protected declaration. In the case of a task type declaration,
13647 -- this is simply the parent of the protected type entity. In the single
13648 -- protected object declaration, this parent will be the implicit type,
13649 -- and we can find the corresponding single protected object declaration
13650 -- by searching forward in the declaration list in the tree.
13652 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13653 -- of this type should have been removed during semantic analysis.
13655 Pdec := Parent (Ptyp);
13656 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13657 N_Single_Protected_Declaration)
13658 loop
13659 Next (Pdec);
13660 end loop;
13662 -- Build the parameter list for the call. Note that _Init is the name
13663 -- of the formal for the object to be initialized, which is the task
13664 -- value record itself.
13666 Args := New_List;
13668 -- For lock-free implementation, skip initializations of the Protection
13669 -- object.
13671 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13673 -- Object parameter. This is a pointer to the object of type
13674 -- Protection used by the GNARL to control the protected object.
13676 Append_To (Args,
13677 Make_Attribute_Reference (Loc,
13678 Prefix =>
13679 Make_Selected_Component (Loc,
13680 Prefix => Make_Identifier (Loc, Name_uInit),
13681 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13682 Attribute_Name => Name_Unchecked_Access));
13684 -- Priority parameter. Set to Unspecified_Priority unless there is a
13685 -- Priority rep item, in which case we take the value from the pragma
13686 -- or attribute definition clause, or there is an Interrupt_Priority
13687 -- rep item and no Priority rep item, and we set the ceiling to
13688 -- Interrupt_Priority'Last, an implementation-defined value, see
13689 -- (RM D.3(10)).
13691 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13692 declare
13693 Prio_Clause : constant Node_Id :=
13694 Get_Rep_Item
13695 (Ptyp, Name_Priority, Check_Parents => False);
13697 Prio : Node_Id;
13699 begin
13700 -- Pragma Priority
13702 if Nkind (Prio_Clause) = N_Pragma then
13703 Prio :=
13704 Expression
13705 (First (Pragma_Argument_Associations (Prio_Clause)));
13707 -- Get_Rep_Item returns either priority pragma.
13709 if Pragma_Name (Prio_Clause) = Name_Priority then
13710 Prio_Type := RTE (RE_Any_Priority);
13711 else
13712 Prio_Type := RTE (RE_Interrupt_Priority);
13713 end if;
13715 -- Attribute definition clause Priority
13717 else
13718 if Chars (Prio_Clause) = Name_Priority then
13719 Prio_Type := RTE (RE_Any_Priority);
13720 else
13721 Prio_Type := RTE (RE_Interrupt_Priority);
13722 end if;
13724 Prio := Expression (Prio_Clause);
13725 end if;
13727 -- Always create a locale variable to capture the priority.
13728 -- The priority is also passed to Install_Restriced_Handlers.
13729 -- Note that it is really necessary to create this variable
13730 -- explicitly. It might be thought that removing side effects
13731 -- would the appropriate approach, but that could generate
13732 -- declarations improperly placed in the enclosing scope.
13734 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13735 Append_To (L,
13736 Make_Object_Declaration (Loc,
13737 Defining_Identifier => Prio_Var,
13738 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13739 Expression => Relocate_Node (Prio)));
13741 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13742 end;
13744 -- When no priority is specified but an xx_Handler pragma is, we
13745 -- default to System.Interrupts.Default_Interrupt_Priority, see
13746 -- D.3(10).
13748 elsif Has_Attach_Handler (Ptyp)
13749 or else Has_Interrupt_Handler (Ptyp)
13750 then
13751 Append_To (Args,
13752 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13754 -- Normal case, no priority or xx_Handler specified, default priority
13756 else
13757 Append_To (Args,
13758 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13759 end if;
13761 -- Test for Compiler_Info parameter. This parameter allows entry body
13762 -- procedures and barrier functions to be called from the runtime. It
13763 -- is a pointer to the record generated by the compiler to represent
13764 -- the protected object.
13766 -- A protected type without entries that covers an interface and
13767 -- overrides the abstract routines with protected procedures is
13768 -- considered equivalent to a protected type with entries in the
13769 -- context of dispatching select statements.
13771 -- Protected types with interrupt handlers (when not using a
13772 -- restricted profile) are also considered equivalent to protected
13773 -- types with entries.
13775 -- The types which are used (Static_Interrupt_Protection and
13776 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13778 declare
13779 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13781 Called_Subp : RE_Id;
13783 begin
13784 case Pkg_Id is
13785 when System_Tasking_Protected_Objects_Entries =>
13786 Called_Subp := RE_Initialize_Protection_Entries;
13788 -- Argument Compiler_Info
13790 Append_To (Args,
13791 Make_Attribute_Reference (Loc,
13792 Prefix => Make_Identifier (Loc, Name_uInit),
13793 Attribute_Name => Name_Address));
13795 when System_Tasking_Protected_Objects_Single_Entry =>
13796 Called_Subp := RE_Initialize_Protection_Entry;
13798 -- Argument Compiler_Info
13800 Append_To (Args,
13801 Make_Attribute_Reference (Loc,
13802 Prefix => Make_Identifier (Loc, Name_uInit),
13803 Attribute_Name => Name_Address));
13805 when System_Tasking_Protected_Objects =>
13806 Called_Subp := RE_Initialize_Protection;
13808 when others =>
13809 raise Program_Error;
13810 end case;
13812 -- Entry_Bodies parameter. This is a pointer to an array of
13813 -- pointers to the entry body procedures and barrier functions of
13814 -- the object. If the protected type has no entries this object
13815 -- will not exist, in this case, pass a null (it can happen when
13816 -- there are protected interrupt handlers or interfaces).
13818 if Has_Entry then
13819 P_Arr := Entry_Bodies_Array (Ptyp);
13821 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13822 -- multiple entries).
13824 Append_To (Args,
13825 Make_Attribute_Reference (Loc,
13826 Prefix => New_Occurrence_Of (P_Arr, Loc),
13827 Attribute_Name => Name_Unrestricted_Access));
13829 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13831 -- Find index mapping function (clumsy but ok for now)
13833 while Ekind (P_Arr) /= E_Function loop
13834 Next_Entity (P_Arr);
13835 end loop;
13837 Append_To (Args,
13838 Make_Attribute_Reference (Loc,
13839 Prefix => New_Occurrence_Of (P_Arr, Loc),
13840 Attribute_Name => Name_Unrestricted_Access));
13841 end if;
13843 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13845 -- This is the case where we have a protected object with
13846 -- interfaces and no entries, and the single entry restriction
13847 -- is in effect. We pass a null pointer for the entry
13848 -- parameter because there is no actual entry.
13850 Append_To (Args, Make_Null (Loc));
13852 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13854 -- This is the case where we have a protected object with no
13855 -- entries and:
13856 -- - either interrupt handlers with non restricted profile,
13857 -- - or interfaces
13858 -- Note that the types which are used for interrupt handlers
13859 -- (Static/Dynamic_Interrupt_Protection) are derived from
13860 -- Protection_Entries. We pass two null pointers because there
13861 -- is no actual entry, and the initialization procedure needs
13862 -- both Entry_Bodies and Find_Body_Index.
13864 Append_To (Args, Make_Null (Loc));
13865 Append_To (Args, Make_Null (Loc));
13866 end if;
13868 Append_To (L,
13869 Make_Procedure_Call_Statement (Loc,
13870 Name =>
13871 New_Occurrence_Of (RTE (Called_Subp), Loc),
13872 Parameter_Associations => Args));
13873 end;
13874 end if;
13876 if Has_Attach_Handler (Ptyp) then
13878 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13879 -- make the following call:
13881 -- Install_Handlers (_object,
13882 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13884 -- or, in the case of Ravenscar:
13886 -- Install_Restricted_Handlers
13887 -- (Prio, (Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13889 declare
13890 Args : constant List_Id := New_List;
13891 Table : constant List_Id := New_List;
13892 Ritem : Node_Id := First_Rep_Item (Ptyp);
13894 begin
13895 -- Build the Priority parameter (only for ravenscar)
13897 if Restricted then
13899 -- Priority comes from a pragma
13901 if Present (Prio_Var) then
13902 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13904 -- Priority is the default one
13906 else
13907 Append_To (Args,
13908 New_Occurrence_Of
13909 (RTE (RE_Default_Interrupt_Priority), Loc));
13910 end if;
13911 end if;
13913 -- Build the Attach_Handler table argument
13915 while Present (Ritem) loop
13916 if Nkind (Ritem) = N_Pragma
13917 and then Pragma_Name (Ritem) = Name_Attach_Handler
13918 then
13919 declare
13920 Handler : constant Node_Id :=
13921 First (Pragma_Argument_Associations (Ritem));
13923 Interrupt : constant Node_Id := Next (Handler);
13924 Expr : constant Node_Id := Expression (Interrupt);
13926 begin
13927 Append_To (Table,
13928 Make_Aggregate (Loc, Expressions => New_List (
13929 Unchecked_Convert_To
13930 (RTE (RE_System_Interrupt_Id), Expr),
13931 Make_Attribute_Reference (Loc,
13932 Prefix =>
13933 Make_Selected_Component (Loc,
13934 Prefix =>
13935 Make_Identifier (Loc, Name_uInit),
13936 Selector_Name =>
13937 Duplicate_Subexpr_No_Checks
13938 (Expression (Handler))),
13939 Attribute_Name => Name_Access))));
13940 end;
13941 end if;
13943 Next_Rep_Item (Ritem);
13944 end loop;
13946 -- Append the table argument we just built
13948 Append_To (Args, Make_Aggregate (Loc, Table));
13950 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13951 -- call to the statements.
13953 if Restricted then
13954 -- Call a simplified version of Install_Handlers to be used
13955 -- when the Ravenscar restrictions are in effect
13956 -- (Install_Restricted_Handlers).
13958 Append_To (L,
13959 Make_Procedure_Call_Statement (Loc,
13960 Name =>
13961 New_Occurrence_Of
13962 (RTE (RE_Install_Restricted_Handlers), Loc),
13963 Parameter_Associations => Args));
13965 else
13966 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13968 -- First, prepends the _object argument
13970 Prepend_To (Args,
13971 Make_Attribute_Reference (Loc,
13972 Prefix =>
13973 Make_Selected_Component (Loc,
13974 Prefix => Make_Identifier (Loc, Name_uInit),
13975 Selector_Name =>
13976 Make_Identifier (Loc, Name_uObject)),
13977 Attribute_Name => Name_Unchecked_Access));
13978 end if;
13980 -- Then, insert call to Install_Handlers
13982 Append_To (L,
13983 Make_Procedure_Call_Statement (Loc,
13984 Name =>
13985 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
13986 Parameter_Associations => Args));
13987 end if;
13988 end;
13989 end if;
13991 return L;
13992 end Make_Initialize_Protection;
13994 ---------------------------
13995 -- Make_Task_Create_Call --
13996 ---------------------------
13998 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13999 Loc : constant Source_Ptr := Sloc (Task_Rec);
14000 Args : List_Id;
14001 Ecount : Node_Id;
14002 Name : Node_Id;
14003 Tdec : Node_Id;
14004 Tdef : Node_Id;
14005 Tnam : Name_Id;
14006 Ttyp : Node_Id;
14008 begin
14009 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14010 Tnam := Chars (Ttyp);
14012 -- Get task declaration. In the case of a task type declaration, this is
14013 -- simply the parent of the task type entity. In the single task
14014 -- declaration, this parent will be the implicit type, and we can find
14015 -- the corresponding single task declaration by searching forward in the
14016 -- declaration list in the tree.
14018 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14019 -- this type should have been removed during semantic analysis.
14021 Tdec := Parent (Ttyp);
14022 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14023 N_Single_Task_Declaration)
14024 loop
14025 Next (Tdec);
14026 end loop;
14028 -- Now we can find the task definition from this declaration
14030 Tdef := Task_Definition (Tdec);
14032 -- Build the parameter list for the call. Note that _Init is the name
14033 -- of the formal for the object to be initialized, which is the task
14034 -- value record itself.
14036 Args := New_List;
14038 -- Priority parameter. Set to Unspecified_Priority unless there is a
14039 -- Priority rep item, in which case we take the value from the rep item.
14041 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14042 Append_To (Args,
14043 Make_Selected_Component (Loc,
14044 Prefix => Make_Identifier (Loc, Name_uInit),
14045 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14046 else
14047 Append_To (Args,
14048 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14049 end if;
14051 -- Optional Stack parameter
14053 if Restricted_Profile then
14055 -- If the stack has been preallocated by the expander then
14056 -- pass its address. Otherwise, pass a null address.
14058 if Preallocated_Stacks_On_Target then
14059 Append_To (Args,
14060 Make_Attribute_Reference (Loc,
14061 Prefix =>
14062 Make_Selected_Component (Loc,
14063 Prefix => Make_Identifier (Loc, Name_uInit),
14064 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14065 Attribute_Name => Name_Address));
14067 else
14068 Append_To (Args,
14069 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14070 end if;
14071 end if;
14073 -- Size parameter. If no Storage_Size pragma is present, then
14074 -- the size is taken from the taskZ variable for the type, which
14075 -- is either Unspecified_Size, or has been reset by the use of
14076 -- a Storage_Size attribute definition clause. If a pragma is
14077 -- present, then the size is taken from the _Size field of the
14078 -- task value record, which was set from the pragma value.
14080 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14081 Append_To (Args,
14082 Make_Selected_Component (Loc,
14083 Prefix => Make_Identifier (Loc, Name_uInit),
14084 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14086 else
14087 Append_To (Args,
14088 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14089 end if;
14091 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14092 -- Task_Info pragma, in which case we take the value from the pragma.
14094 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14095 Append_To (Args,
14096 Make_Selected_Component (Loc,
14097 Prefix => Make_Identifier (Loc, Name_uInit),
14098 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14100 else
14101 Append_To (Args,
14102 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14103 end if;
14105 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14106 -- in which case we take the value from the rep item. The parameter is
14107 -- passed as an Integer because in the case of unspecified CPU the
14108 -- value is not in the range of CPU_Range.
14110 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14111 Append_To (Args,
14112 Convert_To (Standard_Integer,
14113 Make_Selected_Component (Loc,
14114 Prefix => Make_Identifier (Loc, Name_uInit),
14115 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14116 else
14117 Append_To (Args,
14118 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14119 end if;
14121 if not Restricted_Profile then
14123 -- Deadline parameter. If no Relative_Deadline pragma is present,
14124 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14125 -- the deadline is taken from the _Relative_Deadline field of the
14126 -- task value record, which was set from the pragma value. Note that
14127 -- this parameter must not be generated for the restricted profiles
14128 -- since Ravenscar does not allow deadlines.
14130 -- Case where pragma Relative_Deadline applies: use given value
14132 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14133 Append_To (Args,
14134 Make_Selected_Component (Loc,
14135 Prefix => Make_Identifier (Loc, Name_uInit),
14136 Selector_Name =>
14137 Make_Identifier (Loc, Name_uRelative_Deadline)));
14139 -- No pragma Relative_Deadline apply to the task
14141 else
14142 Append_To (Args,
14143 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14144 end if;
14146 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14147 -- present, then the dispatching domain is null. If a rep item is
14148 -- present, then the dispatching domain is taken from the
14149 -- _Dispatching_Domain field of the task value record, which was set
14150 -- from the rep item value.
14152 -- Case where Dispatching_Domain rep item applies: use given value
14154 if Has_Rep_Item
14155 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14156 then
14157 Append_To (Args,
14158 Make_Selected_Component (Loc,
14159 Prefix =>
14160 Make_Identifier (Loc, Name_uInit),
14161 Selector_Name =>
14162 Make_Identifier (Loc, Name_uDispatching_Domain)));
14164 -- No pragma or aspect Dispatching_Domain applies to the task
14166 else
14167 Append_To (Args, Make_Null (Loc));
14168 end if;
14170 -- Number of entries. This is an expression of the form:
14172 -- n + _Init.a'Length + _Init.a'B'Length + ...
14174 -- where a,b... are the entry family names for the task definition
14176 Ecount :=
14177 Build_Entry_Count_Expression
14178 (Ttyp,
14179 Component_Items
14180 (Component_List
14181 (Type_Definition
14182 (Parent (Corresponding_Record_Type (Ttyp))))),
14183 Loc);
14184 Append_To (Args, Ecount);
14186 -- Master parameter. This is a reference to the _Master parameter of
14187 -- the initialization procedure, except in the case of the pragma
14188 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14189 -- System.Tasking.Library_Task_Level.
14191 if Restriction_Active (No_Task_Hierarchy) = False then
14192 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14193 else
14194 Append_To (Args,
14195 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14196 end if;
14197 end if;
14199 -- State parameter. This is a pointer to the task body procedure. The
14200 -- required value is obtained by taking 'Unrestricted_Access of the task
14201 -- body procedure and converting it (with an unchecked conversion) to
14202 -- the type required by the task kernel. For further details, see the
14203 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14204 -- than 'Address in order to avoid creating trampolines.
14206 declare
14207 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14208 Subp_Ptr_Typ : constant Node_Id :=
14209 Create_Itype (E_Access_Subprogram_Type, Tdec);
14210 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14212 begin
14213 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14214 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14216 -- Be sure to freeze a reference to the access-to-subprogram type,
14217 -- otherwise gigi will complain that it's in the wrong scope, because
14218 -- it's actually inside the init procedure for the record type that
14219 -- corresponds to the task type.
14221 -- This processing is causing a crash in the .NET/JVM back ends that
14222 -- is not yet understood, so skip it in these cases ???
14224 if VM_Target = No_VM then
14225 Set_Itype (Ref, Subp_Ptr_Typ);
14226 Append_Freeze_Action (Task_Rec, Ref);
14228 Append_To (Args,
14229 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14230 Make_Qualified_Expression (Loc,
14231 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14232 Expression =>
14233 Make_Attribute_Reference (Loc,
14234 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14235 Attribute_Name => Name_Unrestricted_Access))));
14237 -- For the .NET/JVM cases revert to the original code below ???
14239 else
14240 Append_To (Args,
14241 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14242 Make_Attribute_Reference (Loc,
14243 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14244 Attribute_Name => Name_Address)));
14245 end if;
14246 end;
14248 -- Discriminants parameter. This is just the address of the task
14249 -- value record itself (which contains the discriminant values
14251 Append_To (Args,
14252 Make_Attribute_Reference (Loc,
14253 Prefix => Make_Identifier (Loc, Name_uInit),
14254 Attribute_Name => Name_Address));
14256 -- Elaborated parameter. This is an access to the elaboration Boolean
14258 Append_To (Args,
14259 Make_Attribute_Reference (Loc,
14260 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14261 Attribute_Name => Name_Unchecked_Access));
14263 -- Add Chain parameter (not done for sequential elaboration policy, see
14264 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14266 if Partition_Elaboration_Policy /= 'S' then
14267 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14268 end if;
14270 -- Task name parameter. Take this from the _Task_Id parameter to the
14271 -- init call unless there is a Task_Name pragma, in which case we take
14272 -- the value from the pragma.
14274 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14275 -- Copy expression in full, because it may be dynamic and have
14276 -- side effects.
14278 Append_To (Args,
14279 New_Copy_Tree
14280 (Expression
14281 (First
14282 (Pragma_Argument_Associations
14283 (Get_Rep_Pragma
14284 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14286 else
14287 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14288 end if;
14290 -- Created_Task parameter. This is the _Task_Id field of the task
14291 -- record value
14293 Append_To (Args,
14294 Make_Selected_Component (Loc,
14295 Prefix => Make_Identifier (Loc, Name_uInit),
14296 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14298 declare
14299 Create_RE : RE_Id;
14301 begin
14302 if Restricted_Profile then
14303 if Partition_Elaboration_Policy = 'S' then
14304 Create_RE := RE_Create_Restricted_Task_Sequential;
14305 else
14306 Create_RE := RE_Create_Restricted_Task;
14307 end if;
14308 else
14309 Create_RE := RE_Create_Task;
14310 end if;
14312 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14313 end;
14315 return
14316 Make_Procedure_Call_Statement (Loc,
14317 Name => Name,
14318 Parameter_Associations => Args);
14319 end Make_Task_Create_Call;
14321 ------------------------------
14322 -- Next_Protected_Operation --
14323 ------------------------------
14325 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14326 Next_Op : Node_Id;
14328 begin
14329 Next_Op := Next (N);
14330 while Present (Next_Op)
14331 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
14332 loop
14333 Next (Next_Op);
14334 end loop;
14336 return Next_Op;
14337 end Next_Protected_Operation;
14339 ---------------------
14340 -- Null_Statements --
14341 ---------------------
14343 function Null_Statements (Stats : List_Id) return Boolean is
14344 Stmt : Node_Id;
14346 begin
14347 Stmt := First (Stats);
14348 while Nkind (Stmt) /= N_Empty
14349 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14350 or else
14351 (Nkind (Stmt) = N_Pragma
14352 and then
14353 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14354 Name_Unmodified,
14355 Name_Warnings)))
14356 loop
14357 Next (Stmt);
14358 end loop;
14360 return Nkind (Stmt) = N_Empty;
14361 end Null_Statements;
14363 --------------------------
14364 -- Parameter_Block_Pack --
14365 --------------------------
14367 function Parameter_Block_Pack
14368 (Loc : Source_Ptr;
14369 Blk_Typ : Entity_Id;
14370 Actuals : List_Id;
14371 Formals : List_Id;
14372 Decls : List_Id;
14373 Stmts : List_Id) return Node_Id
14375 Actual : Entity_Id;
14376 Expr : Node_Id := Empty;
14377 Formal : Entity_Id;
14378 Has_Param : Boolean := False;
14379 P : Entity_Id;
14380 Params : List_Id;
14381 Temp_Asn : Node_Id;
14382 Temp_Nam : Node_Id;
14384 begin
14385 Actual := First (Actuals);
14386 Formal := Defining_Identifier (First (Formals));
14387 Params := New_List;
14388 while Present (Actual) loop
14389 if Is_By_Copy_Type (Etype (Actual)) then
14390 -- Generate:
14391 -- Jnn : aliased <formal-type>
14393 Temp_Nam := Make_Temporary (Loc, 'J');
14395 Append_To (Decls,
14396 Make_Object_Declaration (Loc,
14397 Aliased_Present => True,
14398 Defining_Identifier => Temp_Nam,
14399 Object_Definition =>
14400 New_Occurrence_Of (Etype (Formal), Loc)));
14402 if Ekind (Formal) /= E_Out_Parameter then
14404 -- Generate:
14405 -- Jnn := <actual>
14407 Temp_Asn :=
14408 New_Occurrence_Of (Temp_Nam, Loc);
14410 Set_Assignment_OK (Temp_Asn);
14412 Append_To (Stmts,
14413 Make_Assignment_Statement (Loc,
14414 Name => Temp_Asn,
14415 Expression => New_Copy_Tree (Actual)));
14416 end if;
14418 -- Generate:
14419 -- Jnn'unchecked_access
14421 Append_To (Params,
14422 Make_Attribute_Reference (Loc,
14423 Attribute_Name => Name_Unchecked_Access,
14424 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14426 Has_Param := True;
14428 -- The controlling parameter is omitted
14430 else
14431 if not Is_Controlling_Actual (Actual) then
14432 Append_To (Params,
14433 Make_Reference (Loc, New_Copy_Tree (Actual)));
14435 Has_Param := True;
14436 end if;
14437 end if;
14439 Next_Actual (Actual);
14440 Next_Formal_With_Extras (Formal);
14441 end loop;
14443 if Has_Param then
14444 Expr := Make_Aggregate (Loc, Params);
14445 end if;
14447 -- Generate:
14448 -- P : Ann := (
14449 -- J1'unchecked_access;
14450 -- <actual2>'reference;
14451 -- ...);
14453 P := Make_Temporary (Loc, 'P');
14455 Append_To (Decls,
14456 Make_Object_Declaration (Loc,
14457 Defining_Identifier => P,
14458 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14459 Expression => Expr));
14461 return P;
14462 end Parameter_Block_Pack;
14464 ----------------------------
14465 -- Parameter_Block_Unpack --
14466 ----------------------------
14468 function Parameter_Block_Unpack
14469 (Loc : Source_Ptr;
14470 P : Entity_Id;
14471 Actuals : List_Id;
14472 Formals : List_Id) return List_Id
14474 Actual : Entity_Id;
14475 Asnmt : Node_Id;
14476 Formal : Entity_Id;
14477 Has_Asnmt : Boolean := False;
14478 Result : constant List_Id := New_List;
14480 begin
14481 Actual := First (Actuals);
14482 Formal := Defining_Identifier (First (Formals));
14483 while Present (Actual) loop
14484 if Is_By_Copy_Type (Etype (Actual))
14485 and then Ekind (Formal) /= E_In_Parameter
14486 then
14487 -- Generate:
14488 -- <actual> := P.<formal>;
14490 Asnmt :=
14491 Make_Assignment_Statement (Loc,
14492 Name =>
14493 New_Copy (Actual),
14494 Expression =>
14495 Make_Explicit_Dereference (Loc,
14496 Make_Selected_Component (Loc,
14497 Prefix =>
14498 New_Occurrence_Of (P, Loc),
14499 Selector_Name =>
14500 Make_Identifier (Loc, Chars (Formal)))));
14502 Set_Assignment_OK (Name (Asnmt));
14503 Append_To (Result, Asnmt);
14505 Has_Asnmt := True;
14506 end if;
14508 Next_Actual (Actual);
14509 Next_Formal_With_Extras (Formal);
14510 end loop;
14512 if Has_Asnmt then
14513 return Result;
14514 else
14515 return New_List (Make_Null_Statement (Loc));
14516 end if;
14517 end Parameter_Block_Unpack;
14519 ----------------------
14520 -- Set_Discriminals --
14521 ----------------------
14523 procedure Set_Discriminals (Dec : Node_Id) is
14524 D : Entity_Id;
14525 Pdef : Entity_Id;
14526 D_Minal : Entity_Id;
14528 begin
14529 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14530 Pdef := Defining_Identifier (Dec);
14532 if Has_Discriminants (Pdef) then
14533 D := First_Discriminant (Pdef);
14534 while Present (D) loop
14535 D_Minal :=
14536 Make_Defining_Identifier (Sloc (D),
14537 Chars => New_External_Name (Chars (D), 'D'));
14539 Set_Ekind (D_Minal, E_Constant);
14540 Set_Etype (D_Minal, Etype (D));
14541 Set_Scope (D_Minal, Pdef);
14542 Set_Discriminal (D, D_Minal);
14543 Set_Discriminal_Link (D_Minal, D);
14545 Next_Discriminant (D);
14546 end loop;
14547 end if;
14548 end Set_Discriminals;
14550 -----------------------
14551 -- Trivial_Accept_OK --
14552 -----------------------
14554 function Trivial_Accept_OK return Boolean is
14555 begin
14556 case Opt.Task_Dispatching_Policy is
14558 -- If we have the default task dispatching policy in effect, we can
14559 -- definitely do the optimization (one way of looking at this is to
14560 -- think of the formal definition of the default policy being allowed
14561 -- to run any task it likes after a rendezvous, so even if notionally
14562 -- a full rescheduling occurs, we can say that our dispatching policy
14563 -- (i.e. the default dispatching policy) reorders the queue to be the
14564 -- same as just before the call.
14566 when ' ' =>
14567 return True;
14569 -- FIFO_Within_Priorities certainly does not permit this
14570 -- optimization since the Rendezvous is a scheduling action that may
14571 -- require some other task to be run.
14573 when 'F' =>
14574 return False;
14576 -- For now, disallow the optimization for all other policies. This
14577 -- may be over-conservative, but it is certainly not incorrect.
14579 when others =>
14580 return False;
14582 end case;
14583 end Trivial_Accept_OK;
14585 end Exp_Ch9;