* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob49e7efeba6e97645da4d5cca549069a93296a90e
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-2012, 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
74 -- entry families of 128K should be reasonable in all cases, and is a
75 -- documented 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_Entry_Count_Expression
140 (Concurrent_Type : Node_Id;
141 Component_List : List_Id;
142 Loc : Source_Ptr) return Node_Id;
143 -- Compute number of entries for concurrent object. This is a count of
144 -- simple entries, followed by an expression that computes the length
145 -- of the range of each entry family. A single array with that size is
146 -- allocated for each concurrent object of the type.
148 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
149 -- Build the function that translates the entry index in the call
150 -- (which depends on the size of entry families) into an index into the
151 -- Entry_Bodies_Array, to determine the body and barrier function used
152 -- in a protected entry call. A pointer to this function appears in every
153 -- protected object.
155 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
156 -- Build subprogram declaration for previous one
158 function Build_Lock_Free_Protected_Subprogram_Body
159 (N : Node_Id;
160 Prot_Typ : Node_Id;
161 Unprot_Spec : Node_Id) return Node_Id;
162 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
163 -- the subprogram specification of the unprotected version of N. Transform
164 -- N such that it invokes the unprotected version of the body.
166 function Build_Lock_Free_Unprotected_Subprogram_Body
167 (N : Node_Id;
168 Prot_Typ : Node_Id) return Node_Id;
169 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
170 -- of N where the original statements of N are synchronized through atomic
171 -- actions such as compare and exchange. Prior to invoking this routine, it
172 -- has been established that N can be implemented in a lock-free fashion.
174 function Build_Parameter_Block
175 (Loc : Source_Ptr;
176 Actuals : List_Id;
177 Formals : List_Id;
178 Decls : List_Id) return Entity_Id;
179 -- Generate an access type for each actual parameter in the list Actuals.
180 -- Create an encapsulating record that contains all the actuals and return
181 -- its type. Generate:
182 -- type Ann1 is access all <actual1-type>
183 -- ...
184 -- type AnnN is access all <actualN-type>
185 -- type Pnn is record
186 -- <formal1> : Ann1;
187 -- ...
188 -- <formalN> : AnnN;
189 -- end record;
191 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
192 -- Build body of wrapper procedure for an entry or entry family that has
193 -- pre/postconditions. The body gathers the PPC's and expands them in the
194 -- usual way, and performs the entry call itself. This way preconditions
195 -- are evaluated before the call is queued. E is the entry in question,
196 -- and Decl is the enclosing synchronized type declaration at whose
197 -- freeze point the generated body is analyzed.
199 function Build_Protected_Entry
200 (N : Node_Id;
201 Ent : Entity_Id;
202 Pid : Node_Id) return Node_Id;
203 -- Build the procedure implementing the statement sequence of the specified
204 -- entry body.
206 function Build_Protected_Entry_Specification
207 (Loc : Source_Ptr;
208 Def_Id : Entity_Id;
209 Ent_Id : Entity_Id) return Node_Id;
210 -- Build a specification for the procedure implementing the statements of
211 -- the specified entry body. Add attributes associating it with the entry
212 -- defining identifier Ent_Id.
214 function Build_Protected_Spec
215 (N : Node_Id;
216 Obj_Type : Entity_Id;
217 Ident : Entity_Id;
218 Unprotected : Boolean := False) return List_Id;
219 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
220 -- Subprogram_Type. Builds signature of protected subprogram, adding the
221 -- formal that corresponds to the object itself. For an access to protected
222 -- subprogram, there is no object type to specify, so the parameter has
223 -- type Address and mode In. An indirect call through such a pointer will
224 -- convert the address to a reference to the actual object. The object is
225 -- a limited record and therefore a by_reference type.
227 function Build_Protected_Subprogram_Body
228 (N : Node_Id;
229 Pid : Node_Id;
230 N_Op_Spec : Node_Id) return Node_Id;
231 -- This function is used to construct the protected version of a protected
232 -- subprogram. Its statement sequence first defers abort, then locks
233 -- the associated protected object, and then enters a block that contains
234 -- a call to the unprotected version of the subprogram (for details, see
235 -- Build_Unprotected_Subprogram_Body). This block statement requires
236 -- a cleanup handler that unlocks the object in all cases.
237 -- (see Exp_Ch7.Expand_Cleanup_Actions).
239 function Build_Renamed_Formal_Declaration
240 (New_F : Entity_Id;
241 Formal : Entity_Id;
242 Comp : Entity_Id;
243 Renamed_Formal : Node_Id) return Node_Id;
244 -- Create a renaming declaration for a formal, within a protected entry
245 -- body or an accept body. The renamed object is a component of the
246 -- parameter block that is a parameter in the entry call.
248 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
249 -- does not dereference the corresponding component to prevent an illegal
250 -- use of the incomplete type (AI05-0151).
252 function Build_Selected_Name
253 (Prefix : Entity_Id;
254 Selector : Entity_Id;
255 Append_Char : Character := ' ') return Name_Id;
256 -- Build a name in the form of Prefix__Selector, with an optional
257 -- character appended. This is used for internal subprograms generated
258 -- for operations of protected types, including barrier functions.
259 -- For the subprograms generated for entry bodies and entry barriers,
260 -- the generated name includes a sequence number that makes names
261 -- unique in the presence of entry overloading. This is necessary
262 -- because entry body procedures and barrier functions all have the
263 -- same signature.
265 procedure Build_Simple_Entry_Call
266 (N : Node_Id;
267 Concval : Node_Id;
268 Ename : Node_Id;
269 Index : Node_Id);
270 -- Some comments here would be useful ???
272 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
273 -- This routine constructs a specification for the procedure that we will
274 -- build for the task body for task type T. The spec has the form:
276 -- procedure tnameB (_Task : access tnameV);
278 -- where name is the character name taken from the task type entity that
279 -- is passed as the argument to the procedure, and tnameV is the task
280 -- value type that is associated with the task type.
282 function Build_Unprotected_Subprogram_Body
283 (N : Node_Id;
284 Pid : Node_Id) return Node_Id;
285 -- This routine constructs the unprotected version of a protected
286 -- subprogram body, which is contains all of the code in the
287 -- original, unexpanded body. This is the version of the protected
288 -- subprogram that is called from all protected operations on the same
289 -- object, including the protected version of the same subprogram.
291 procedure Build_Wrapper_Bodies
292 (Loc : Source_Ptr;
293 Typ : Entity_Id;
294 N : Node_Id);
295 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
296 -- record of a concurrent type. N is the insertion node where all bodies
297 -- will be placed. This routine builds the bodies of the subprograms which
298 -- serve as an indirection mechanism to overriding primitives of concurrent
299 -- types, entries and protected procedures. Any new body is analyzed.
301 procedure Build_Wrapper_Specs
302 (Loc : Source_Ptr;
303 Typ : Entity_Id;
304 N : in out Node_Id);
305 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
306 -- record of a concurrent type. N is the insertion node where all specs
307 -- will be placed. This routine builds the specs of the subprograms which
308 -- serve as an indirection mechanism to overriding primitives of concurrent
309 -- types, entries and protected procedures. Any new spec is analyzed.
311 procedure Collect_Entry_Families
312 (Loc : Source_Ptr;
313 Cdecls : List_Id;
314 Current_Node : in out Node_Id;
315 Conctyp : Entity_Id);
316 -- For each entry family in a concurrent type, create an anonymous array
317 -- type of the right size, and add a component to the corresponding_record.
319 function Concurrent_Object
320 (Spec_Id : Entity_Id;
321 Conc_Typ : Entity_Id) return Entity_Id;
322 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
323 -- the entity associated with the concurrent object in the Protected_Body_
324 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
325 -- denotes formal parameter _O, _object or _task.
327 function Copy_Result_Type (Res : Node_Id) return Node_Id;
328 -- Copy the result type of a function specification, when building the
329 -- internal operation corresponding to a protected function, or when
330 -- expanding an access to protected function. If the result is an anonymous
331 -- access to subprogram itself, we need to create a new signature with the
332 -- same parameter names and the same resolved types, but with new entities
333 -- for the formals.
335 procedure Debug_Private_Data_Declarations (Decls : List_Id);
336 -- Decls is a list which may contain the declarations created by Install_
337 -- Private_Data_Declarations. All generated entities are marked as needing
338 -- debug info and debug nodes are manually generation where necessary. This
339 -- step of the expansion must to be done after private data has been moved
340 -- to its final resting scope to ensure proper visibility of debug objects.
342 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
343 -- If control flow optimizations are suppressed, and Alt is an accept,
344 -- delay, or entry call alternative with no trailing statements, insert a
345 -- null trailing statement with the given Loc (which is the sloc of the
346 -- accept, delay, or entry call statement). There might not be any
347 -- generated code for the accept, delay, or entry call itself (the
348 -- effect of these statements is part of the general processsing done
349 -- for the enclosing selective accept, timed entry call, or asynchronous
350 -- select), and the null statement is there to carry the sloc of that
351 -- statement to the back-end for trace-based coverage analysis purposes.
353 procedure Extract_Dispatching_Call
354 (N : Node_Id;
355 Call_Ent : out Entity_Id;
356 Object : out Entity_Id;
357 Actuals : out List_Id;
358 Formals : out List_Id);
359 -- Given a dispatching call, extract the entity of the name of the call,
360 -- its actual dispatching object, its actual parameters and the formal
361 -- parameters of the overridden interface-level version. If the type of
362 -- the dispatching object is an access type then an explicit dereference
363 -- is returned in Object.
365 procedure Extract_Entry
366 (N : Node_Id;
367 Concval : out Node_Id;
368 Ename : out Node_Id;
369 Index : out Node_Id);
370 -- Given an entry call, returns the associated concurrent object,
371 -- the entry name, and the entry family index.
373 function Family_Offset
374 (Loc : Source_Ptr;
375 Hi : Node_Id;
376 Lo : Node_Id;
377 Ttyp : Entity_Id;
378 Cap : Boolean) return Node_Id;
379 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in
380 -- an accept statement, or the upper bound in the discrete subtype of
381 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
382 -- the concurrent type of the entry. If Cap is true, the result is
383 -- capped according to Entry_Family_Bound.
385 function Family_Size
386 (Loc : Source_Ptr;
387 Hi : Node_Id;
388 Lo : Node_Id;
389 Ttyp : Entity_Id;
390 Cap : Boolean) return Node_Id;
391 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
392 -- a family, and handle properly the superflat case. This is equivalent
393 -- to the use of 'Length on the index type, but must use Family_Offset
394 -- to handle properly the case of bounds that depend on discriminants.
395 -- If Cap is true, the result is capped according to Entry_Family_Bound.
397 procedure Find_Enclosing_Context
398 (N : Node_Id;
399 Context : out Node_Id;
400 Context_Id : out Entity_Id;
401 Context_Decls : out List_Id);
402 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
403 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
404 -- nearest enclosing body, block, package or return statement and return
405 -- its constituents. Context is the enclosing construct, Context_Id is
406 -- the scope of Context_Id and Context_Decls is the declarative list of
407 -- Context.
409 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
410 -- Given a subprogram identifier, return the entity which is associated
411 -- with the protection entry index in the Protected_Body_Subprogram or the
412 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
413 -- parameter _E.
415 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
416 -- Tell whether a given subprogram cannot raise an exception
418 function Is_Potentially_Large_Family
419 (Base_Index : Entity_Id;
420 Conctyp : Entity_Id;
421 Lo : Node_Id;
422 Hi : Node_Id) return Boolean;
424 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
425 -- Determine whether Id is a function or a procedure and is marked as a
426 -- private primitive.
428 function Null_Statements (Stats : List_Id) return Boolean;
429 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
430 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
431 -- well to still count as null. Returns True for a null sequence. The
432 -- argument is the list of statements from the DO-END sequence.
434 function Parameter_Block_Pack
435 (Loc : Source_Ptr;
436 Blk_Typ : Entity_Id;
437 Actuals : List_Id;
438 Formals : List_Id;
439 Decls : List_Id;
440 Stmts : List_Id) return Entity_Id;
441 -- Set the components of the generated parameter block with the values of
442 -- the actual parameters. Generate aliased temporaries to capture the
443 -- values for types that are passed by copy. Otherwise generate a reference
444 -- to the actual's value. Return the address of the aggregate block.
445 -- Generate:
446 -- Jnn1 : alias <formal-type1>;
447 -- Jnn1 := <actual1>;
448 -- ...
449 -- P : Blk_Typ := (
450 -- Jnn1'unchecked_access;
451 -- <actual2>'reference;
452 -- ...);
454 function Parameter_Block_Unpack
455 (Loc : Source_Ptr;
456 P : Entity_Id;
457 Actuals : List_Id;
458 Formals : List_Id) return List_Id;
459 -- Retrieve the values of the components from the parameter block and
460 -- assign then to the original actual parameters. Generate:
461 -- <actual1> := P.<formal1>;
462 -- ...
463 -- <actualN> := P.<formalN>;
465 function Trivial_Accept_OK return Boolean;
466 -- If there is no DO-END block for an accept, or if the DO-END block has
467 -- only null statements, then it is possible to do the Rendezvous with much
468 -- less overhead using the Accept_Trivial routine in the run-time library.
469 -- However, this is not always a valid optimization. Whether it is valid or
470 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
471 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
472 -- a rescheduling is required, so this optimization is not allowed. This
473 -- function returns True if the optimization is permitted.
475 -----------------------------
476 -- Actual_Index_Expression --
477 -----------------------------
479 function Actual_Index_Expression
480 (Sloc : Source_Ptr;
481 Ent : Entity_Id;
482 Index : Node_Id;
483 Tsk : Entity_Id) return Node_Id
485 Ttyp : constant Entity_Id := Etype (Tsk);
486 Expr : Node_Id;
487 Num : Node_Id;
488 Lo : Node_Id;
489 Hi : Node_Id;
490 Prev : Entity_Id;
491 S : Node_Id;
493 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
494 -- Compute difference between bounds of entry family
496 --------------------------
497 -- Actual_Family_Offset --
498 --------------------------
500 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
502 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
503 -- Replace a reference to a discriminant with a selected component
504 -- denoting the discriminant of the target task.
506 -----------------------------
507 -- Actual_Discriminant_Ref --
508 -----------------------------
510 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
511 Typ : constant Entity_Id := Etype (Bound);
512 B : Node_Id;
514 begin
515 if not Is_Entity_Name (Bound)
516 or else Ekind (Entity (Bound)) /= E_Discriminant
517 then
518 if Nkind (Bound) = N_Attribute_Reference then
519 return Bound;
520 else
521 B := New_Copy_Tree (Bound);
522 end if;
524 else
525 B :=
526 Make_Selected_Component (Sloc,
527 Prefix => New_Copy_Tree (Tsk),
528 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
530 Analyze_And_Resolve (B, Typ);
531 end if;
533 return
534 Make_Attribute_Reference (Sloc,
535 Attribute_Name => Name_Pos,
536 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
537 Expressions => New_List (B));
538 end Actual_Discriminant_Ref;
540 -- Start of processing for Actual_Family_Offset
542 begin
543 return
544 Make_Op_Subtract (Sloc,
545 Left_Opnd => Actual_Discriminant_Ref (Hi),
546 Right_Opnd => Actual_Discriminant_Ref (Lo));
547 end Actual_Family_Offset;
549 -- Start of processing for Actual_Index_Expression
551 begin
552 -- The queues of entries and entry families appear in textual order in
553 -- the associated record. The entry index is computed as the sum of the
554 -- number of queues for all entries that precede the designated one, to
555 -- which is added the index expression, if this expression denotes a
556 -- member of a family.
558 -- The following is a place holder for the count of simple entries
560 Num := Make_Integer_Literal (Sloc, 1);
562 -- We construct an expression which is a series of addition operations.
563 -- See comments in Entry_Index_Expression, which is identical in
564 -- structure.
566 if Present (Index) then
567 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
569 Expr :=
570 Make_Op_Add (Sloc,
571 Left_Opnd => Num,
573 Right_Opnd =>
574 Actual_Family_Offset (
575 Make_Attribute_Reference (Sloc,
576 Attribute_Name => Name_Pos,
577 Prefix => New_Reference_To (Base_Type (S), Sloc),
578 Expressions => New_List (Relocate_Node (Index))),
579 Type_Low_Bound (S)));
580 else
581 Expr := Num;
582 end if;
584 -- Now add lengths of preceding entries and entry families
586 Prev := First_Entity (Ttyp);
588 while Chars (Prev) /= Chars (Ent)
589 or else (Ekind (Prev) /= Ekind (Ent))
590 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
591 loop
592 if Ekind (Prev) = E_Entry then
593 Set_Intval (Num, Intval (Num) + 1);
595 elsif Ekind (Prev) = E_Entry_Family then
596 S :=
597 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
599 -- The need for the following full view retrieval stems from
600 -- this complex case of nested generics and tasking:
602 -- generic
603 -- type Formal_Index is range <>;
604 -- ...
605 -- package Outer is
606 -- type Index is private;
607 -- generic
608 -- ...
609 -- package Inner is
610 -- procedure P;
611 -- end Inner;
612 -- private
613 -- type Index is new Formal_Index range 1 .. 10;
614 -- end Outer;
616 -- package body Outer is
617 -- task type T is
618 -- entry Fam (Index); -- (2)
619 -- entry E;
620 -- end T;
621 -- package body Inner is -- (3)
622 -- procedure P is
623 -- begin
624 -- T.E; -- (1)
625 -- end P;
626 -- end Inner;
627 -- ...
629 -- We are currently building the index expression for the entry
630 -- call "T.E" (1). Part of the expansion must mention the range
631 -- of the discrete type "Index" (2) of entry family "Fam".
632 -- However only the private view of type "Index" is available to
633 -- the inner generic (3) because there was no prior mention of
634 -- the type inside "Inner". This visibility requirement is
635 -- implicit and cannot be detected during the construction of
636 -- the generic trees and needs special handling.
638 if In_Instance_Body
639 and then Is_Private_Type (S)
640 and then Present (Full_View (S))
641 then
642 S := Full_View (S);
643 end if;
645 Lo := Type_Low_Bound (S);
646 Hi := Type_High_Bound (S);
648 Expr :=
649 Make_Op_Add (Sloc,
650 Left_Opnd => Expr,
651 Right_Opnd =>
652 Make_Op_Add (Sloc,
653 Left_Opnd =>
654 Actual_Family_Offset (Hi, Lo),
655 Right_Opnd =>
656 Make_Integer_Literal (Sloc, 1)));
658 -- Other components are anonymous types to be ignored
660 else
661 null;
662 end if;
664 Next_Entity (Prev);
665 end loop;
667 return Expr;
668 end Actual_Index_Expression;
670 --------------------------
671 -- Add_Formal_Renamings --
672 --------------------------
674 procedure Add_Formal_Renamings
675 (Spec : Node_Id;
676 Decls : List_Id;
677 Ent : Entity_Id;
678 Loc : Source_Ptr)
680 Ptr : constant Entity_Id :=
681 Defining_Identifier
682 (Next (First (Parameter_Specifications (Spec))));
683 -- The name of the formal that holds the address of the parameter block
684 -- for the call.
686 Comp : Entity_Id;
687 Decl : Node_Id;
688 Formal : Entity_Id;
689 New_F : Entity_Id;
690 Renamed_Formal : Node_Id;
692 begin
693 Formal := First_Formal (Ent);
694 while Present (Formal) loop
695 Comp := Entry_Component (Formal);
696 New_F :=
697 Make_Defining_Identifier (Sloc (Formal),
698 Chars => Chars (Formal));
699 Set_Etype (New_F, Etype (Formal));
700 Set_Scope (New_F, Ent);
702 -- Now we set debug info needed on New_F even though it does not
703 -- come from source, so that the debugger will get the right
704 -- information for these generated names.
706 Set_Debug_Info_Needed (New_F);
708 if Ekind (Formal) = E_In_Parameter then
709 Set_Ekind (New_F, E_Constant);
710 else
711 Set_Ekind (New_F, E_Variable);
712 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
713 end if;
715 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
717 Renamed_Formal :=
718 Make_Selected_Component (Loc,
719 Prefix =>
720 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
721 Make_Identifier (Loc, Chars (Ptr))),
722 Selector_Name => New_Reference_To (Comp, Loc));
724 Decl :=
725 Build_Renamed_Formal_Declaration
726 (New_F, Formal, Comp, Renamed_Formal);
728 Append (Decl, Decls);
729 Set_Renamed_Object (Formal, New_F);
730 Next_Formal (Formal);
731 end loop;
732 end Add_Formal_Renamings;
734 ------------------------
735 -- Add_Object_Pointer --
736 ------------------------
738 procedure Add_Object_Pointer
739 (Loc : Source_Ptr;
740 Conc_Typ : Entity_Id;
741 Decls : List_Id)
743 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
744 Decl : Node_Id;
745 Obj_Ptr : Node_Id;
747 begin
748 -- Create the renaming declaration for the Protection object of a
749 -- protected type. _Object is used by Complete_Entry_Body.
750 -- ??? An attempt to make this a renaming was unsuccessful.
752 -- Build the entity for the access type
754 Obj_Ptr :=
755 Make_Defining_Identifier (Loc,
756 New_External_Name (Chars (Rec_Typ), 'P'));
758 -- Generate:
759 -- _object : poVP := poVP!O;
761 Decl :=
762 Make_Object_Declaration (Loc,
763 Defining_Identifier =>
764 Make_Defining_Identifier (Loc, Name_uObject),
765 Object_Definition =>
766 New_Reference_To (Obj_Ptr, Loc),
767 Expression =>
768 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
769 Set_Debug_Info_Needed (Defining_Identifier (Decl));
770 Prepend_To (Decls, Decl);
772 -- Generate:
773 -- type poVP is access poV;
775 Decl :=
776 Make_Full_Type_Declaration (Loc,
777 Defining_Identifier =>
778 Obj_Ptr,
779 Type_Definition =>
780 Make_Access_To_Object_Definition (Loc,
781 Subtype_Indication =>
782 New_Reference_To (Rec_Typ, Loc)));
783 Set_Debug_Info_Needed (Defining_Identifier (Decl));
784 Prepend_To (Decls, Decl);
785 end Add_Object_Pointer;
787 -----------------------
788 -- Build_Accept_Body --
789 -----------------------
791 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
792 Loc : constant Source_Ptr := Sloc (Astat);
793 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
794 New_S : Node_Id;
795 Hand : Node_Id;
796 Call : Node_Id;
797 Ohandle : Node_Id;
799 begin
800 -- At the end of the statement sequence, Complete_Rendezvous is called.
801 -- A label skipping the Complete_Rendezvous, and all other accept
802 -- processing, has already been added for the expansion of requeue
803 -- statements. The Sloc is copied from the last statement since it
804 -- is really part of this last statement.
806 Call :=
807 Build_Runtime_Call
808 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
809 Insert_Before (Last (Statements (Stats)), Call);
810 Analyze (Call);
812 -- If exception handlers are present, then append Complete_Rendezvous
813 -- calls to the handlers, and construct the required outer block. As
814 -- above, the Sloc is copied from the last statement in the sequence.
816 if Present (Exception_Handlers (Stats)) then
817 Hand := First (Exception_Handlers (Stats));
818 while Present (Hand) loop
819 Call :=
820 Build_Runtime_Call
821 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
822 Append (Call, Statements (Hand));
823 Analyze (Call);
824 Next (Hand);
825 end loop;
827 New_S :=
828 Make_Handled_Sequence_Of_Statements (Loc,
829 Statements => New_List (
830 Make_Block_Statement (Loc,
831 Handled_Statement_Sequence => Stats)));
833 else
834 New_S := Stats;
835 end if;
837 -- At this stage we know that the new statement sequence does not
838 -- have an exception handler part, so we supply one to call
839 -- Exceptional_Complete_Rendezvous. This handler is
841 -- when all others =>
842 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
844 -- We handle Abort_Signal to make sure that we properly catch the abort
845 -- case and wake up the caller.
847 Ohandle := Make_Others_Choice (Loc);
848 Set_All_Others (Ohandle);
850 Set_Exception_Handlers (New_S,
851 New_List (
852 Make_Implicit_Exception_Handler (Loc,
853 Exception_Choices => New_List (Ohandle),
855 Statements => New_List (
856 Make_Procedure_Call_Statement (Sloc (Stats),
857 Name => New_Reference_To (
858 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
859 Parameter_Associations => New_List (
860 Make_Function_Call (Sloc (Stats),
861 Name => New_Reference_To (
862 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
864 Set_Parent (New_S, Astat); -- temp parent for Analyze call
865 Analyze_Exception_Handlers (Exception_Handlers (New_S));
866 Expand_Exception_Handlers (New_S);
868 -- Exceptional_Complete_Rendezvous must be called with abort
869 -- still deferred, which is the case for a "when all others" handler.
871 return New_S;
872 end Build_Accept_Body;
874 -----------------------------------
875 -- Build_Activation_Chain_Entity --
876 -----------------------------------
878 procedure Build_Activation_Chain_Entity (N : Node_Id) is
879 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
880 -- Determine whether an extended return statement has an activation
881 -- chain.
883 --------------------------
884 -- Has_Activation_Chain --
885 --------------------------
887 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
888 Decl : Node_Id;
890 begin
891 Decl := First (Return_Object_Declarations (Stmt));
892 while Present (Decl) loop
893 if Nkind (Decl) = N_Object_Declaration
894 and then Chars (Defining_Identifier (Decl)) = Name_uChain
895 then
896 return True;
897 end if;
899 Next (Decl);
900 end loop;
902 return False;
903 end Has_Activation_Chain;
905 -- Local variables
907 Context : Node_Id;
908 Context_Id : Entity_Id;
909 Decls : List_Id;
911 -- Start of processing for Build_Activation_Chain_Entity
913 begin
914 -- Activation chain is never used for sequential elaboration policy, see
915 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
917 if Partition_Elaboration_Policy = 'S' then
918 return;
919 end if;
921 Find_Enclosing_Context (N, Context, Context_Id, Decls);
923 -- If activation chain entity has not been declared already, create one
925 if Nkind (Context) = N_Extended_Return_Statement
926 or else No (Activation_Chain_Entity (Context))
927 then
928 -- Since extended return statements do not store the entity of the
929 -- chain, examine the return object declarations to avoid creating
930 -- a duplicate.
932 if Nkind (Context) = N_Extended_Return_Statement
933 and then Has_Activation_Chain (Context)
934 then
935 return;
936 end if;
938 declare
939 Loc : constant Source_Ptr := Sloc (Context);
940 Chain : Entity_Id;
941 Decl : Node_Id;
943 begin
944 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
946 -- Note: An extended return statement is not really a task
947 -- activator, but it does have an activation chain on which to
948 -- store the tasks temporarily. On successful return, the tasks
949 -- on this chain are moved to the chain passed in by the caller.
950 -- We do not build an Activation_Chain_Entity for an extended
951 -- return statement, because we do not want to build a call to
952 -- Activate_Tasks. Task activation is the responsibility of the
953 -- caller.
955 if Nkind (Context) /= N_Extended_Return_Statement then
956 Set_Activation_Chain_Entity (Context, Chain);
957 end if;
959 Decl :=
960 Make_Object_Declaration (Loc,
961 Defining_Identifier => Chain,
962 Aliased_Present => True,
963 Object_Definition =>
964 New_Reference_To (RTE (RE_Activation_Chain), Loc));
966 Prepend_To (Decls, Decl);
968 -- Ensure that the _chain appears in the proper scope of the
969 -- context.
971 if Context_Id /= Current_Scope then
972 Push_Scope (Context_Id);
973 Analyze (Decl);
974 Pop_Scope;
975 else
976 Analyze (Decl);
977 end if;
978 end;
979 end if;
980 end Build_Activation_Chain_Entity;
982 ----------------------------
983 -- Build_Barrier_Function --
984 ----------------------------
986 function Build_Barrier_Function
987 (N : Node_Id;
988 Ent : Entity_Id;
989 Pid : Node_Id) return Node_Id
991 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
992 Cond : constant Node_Id := Condition (Ent_Formals);
993 Loc : constant Source_Ptr := Sloc (Cond);
994 Func_Id : constant Entity_Id := Barrier_Function (Ent);
995 Op_Decls : constant List_Id := New_List;
996 Stmt : Node_Id;
997 Func_Body : Node_Id;
999 begin
1000 -- Add a declaration for the Protection object, renaming declarations
1001 -- for the discriminals and privals and finally a declaration for the
1002 -- entry family index (if applicable).
1004 Install_Private_Data_Declarations (Sloc (N),
1005 Spec_Id => Func_Id,
1006 Conc_Typ => Pid,
1007 Body_Nod => N,
1008 Decls => Op_Decls,
1009 Barrier => True,
1010 Family => Ekind (Ent) = E_Entry_Family);
1012 -- If compiling with -fpreserve-control-flow, make sure we insert an
1013 -- IF statement so that the back-end knows to generate a conditional
1014 -- branch instruction, even if the condition is just the name of a
1015 -- boolean object.
1017 if Opt.Suppress_Control_Flow_Optimizations then
1018 Stmt := Make_Implicit_If_Statement (Cond,
1019 Condition => Cond,
1020 Then_Statements => New_List (
1021 Make_Simple_Return_Statement (Loc,
1022 New_Occurrence_Of (Standard_True, Loc))),
1023 Else_Statements => New_List (
1024 Make_Simple_Return_Statement (Loc,
1025 New_Occurrence_Of (Standard_False, Loc))));
1027 else
1028 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1029 end if;
1031 -- Note: the condition in the barrier function needs to be properly
1032 -- processed for the C/Fortran boolean possibility, but this happens
1033 -- automatically since the return statement does this normalization.
1035 Func_Body :=
1036 Make_Subprogram_Body (Loc,
1037 Specification =>
1038 Build_Barrier_Function_Specification (Loc,
1039 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1040 Declarations => Op_Decls,
1041 Handled_Statement_Sequence =>
1042 Make_Handled_Sequence_Of_Statements (Loc,
1043 Statements => New_List (Stmt)));
1044 Set_Is_Entry_Barrier_Function (Func_Body);
1046 return Func_Body;
1047 end Build_Barrier_Function;
1049 ------------------------------------------
1050 -- Build_Barrier_Function_Specification --
1051 ------------------------------------------
1053 function Build_Barrier_Function_Specification
1054 (Loc : Source_Ptr;
1055 Def_Id : Entity_Id) return Node_Id
1057 begin
1058 Set_Debug_Info_Needed (Def_Id);
1060 return Make_Function_Specification (Loc,
1061 Defining_Unit_Name => Def_Id,
1062 Parameter_Specifications => New_List (
1063 Make_Parameter_Specification (Loc,
1064 Defining_Identifier =>
1065 Make_Defining_Identifier (Loc, Name_uO),
1066 Parameter_Type =>
1067 New_Reference_To (RTE (RE_Address), Loc)),
1069 Make_Parameter_Specification (Loc,
1070 Defining_Identifier =>
1071 Make_Defining_Identifier (Loc, Name_uE),
1072 Parameter_Type =>
1073 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1075 Result_Definition =>
1076 New_Reference_To (Standard_Boolean, Loc));
1077 end Build_Barrier_Function_Specification;
1079 --------------------------
1080 -- Build_Call_With_Task --
1081 --------------------------
1083 function Build_Call_With_Task
1084 (N : Node_Id;
1085 E : Entity_Id) return Node_Id
1087 Loc : constant Source_Ptr := Sloc (N);
1088 begin
1089 return
1090 Make_Function_Call (Loc,
1091 Name => New_Reference_To (E, Loc),
1092 Parameter_Associations => New_List (Concurrent_Ref (N)));
1093 end Build_Call_With_Task;
1095 -----------------------------
1096 -- Build_Class_Wide_Master --
1097 -----------------------------
1099 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1100 Loc : constant Source_Ptr := Sloc (Typ);
1101 Master_Id : Entity_Id;
1102 Master_Scope : Entity_Id;
1103 Name_Id : Node_Id;
1104 Related_Node : Node_Id;
1105 Ren_Decl : Node_Id;
1107 begin
1108 -- Nothing to do if there is no task hierarchy
1110 if Restriction_Active (No_Task_Hierarchy) then
1111 return;
1112 end if;
1114 -- Find the declaration that created the access type. It is either a
1115 -- type declaration, or an object declaration with an access definition,
1116 -- in which case the type is anonymous.
1118 if Is_Itype (Typ) then
1119 Related_Node := Associated_Node_For_Itype (Typ);
1120 else
1121 Related_Node := Parent (Typ);
1122 end if;
1124 Master_Scope := Find_Master_Scope (Typ);
1126 -- Nothing to do if the master scope already contains a _master entity.
1127 -- The only exception to this is the following scenario:
1129 -- Source_Scope
1130 -- Transient_Scope_1
1131 -- _master
1133 -- Transient_Scope_2
1134 -- use of master
1136 -- In this case the source scope is marked as having the master entity
1137 -- even though the actual declaration appears inside an inner scope. If
1138 -- the second transient scope requires a _master, it cannot use the one
1139 -- already declared because the entity is not visible.
1141 Name_Id := Make_Identifier (Loc, Name_uMaster);
1143 if not Has_Master_Entity (Master_Scope)
1144 or else No (Current_Entity_In_Scope (Name_Id))
1145 then
1146 declare
1147 Master_Decl : Node_Id;
1149 begin
1150 Set_Has_Master_Entity (Master_Scope);
1152 -- Generate:
1153 -- _master : constant Integer := Current_Master.all;
1155 Master_Decl :=
1156 Make_Object_Declaration (Loc,
1157 Defining_Identifier =>
1158 Make_Defining_Identifier (Loc, Name_uMaster),
1159 Constant_Present => True,
1160 Object_Definition =>
1161 New_Reference_To (Standard_Integer, Loc),
1162 Expression =>
1163 Make_Explicit_Dereference (Loc,
1164 New_Reference_To (RTE (RE_Current_Master), Loc)));
1166 Insert_Action (Related_Node, Master_Decl);
1167 Analyze (Master_Decl);
1169 -- Mark the containing scope as a task master. Masters associated
1170 -- with return statements are already marked at this stage (see
1171 -- Analyze_Subprogram_Body).
1173 if Ekind (Current_Scope) /= E_Return_Statement then
1174 declare
1175 Par : Node_Id := Related_Node;
1177 begin
1178 while Nkind (Par) /= N_Compilation_Unit loop
1179 Par := Parent (Par);
1181 -- If we fall off the top, we are at the outer level, and
1182 -- the environment task is our effective master, so
1183 -- nothing to mark.
1185 if Nkind_In (Par, N_Block_Statement,
1186 N_Subprogram_Body,
1187 N_Task_Body)
1188 then
1189 Set_Is_Task_Master (Par);
1190 exit;
1191 end if;
1192 end loop;
1193 end;
1194 end if;
1195 end;
1196 end if;
1198 Master_Id :=
1199 Make_Defining_Identifier (Loc,
1200 New_External_Name (Chars (Typ), 'M'));
1202 -- Generate:
1203 -- Mnn renames _master;
1205 Ren_Decl :=
1206 Make_Object_Renaming_Declaration (Loc,
1207 Defining_Identifier => Master_Id,
1208 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
1209 Name => Name_Id);
1211 Insert_Action (Related_Node, Ren_Decl);
1213 Set_Master_Id (Typ, Master_Id);
1214 end Build_Class_Wide_Master;
1216 --------------------------------
1217 -- Build_Corresponding_Record --
1218 --------------------------------
1220 function Build_Corresponding_Record
1221 (N : Node_Id;
1222 Ctyp : Entity_Id;
1223 Loc : Source_Ptr) return Node_Id
1225 Rec_Ent : constant Entity_Id :=
1226 Make_Defining_Identifier
1227 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1228 Disc : Entity_Id;
1229 Dlist : List_Id;
1230 New_Disc : Entity_Id;
1231 Cdecls : List_Id;
1233 begin
1234 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1235 Set_Ekind (Rec_Ent, E_Record_Type);
1236 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1237 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1238 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1239 Set_Stored_Constraint (Rec_Ent, No_Elist);
1240 Cdecls := New_List;
1242 -- Use discriminals to create list of discriminants for record, and
1243 -- create new discriminals for use in default expressions, etc. It is
1244 -- worth noting that a task discriminant gives rise to 5 entities;
1246 -- a) The original discriminant.
1247 -- b) The discriminal for use in the task.
1248 -- c) The discriminant of the corresponding record.
1249 -- d) The discriminal for the init proc of the corresponding record.
1250 -- e) The local variable that renames the discriminant in the procedure
1251 -- for the task body.
1253 -- In fact the discriminals b) are used in the renaming declarations
1254 -- for e). See details in einfo (Handling of Discriminants).
1256 if Present (Discriminant_Specifications (N)) then
1257 Dlist := New_List;
1258 Disc := First_Discriminant (Ctyp);
1260 while Present (Disc) loop
1261 New_Disc := CR_Discriminant (Disc);
1263 Append_To (Dlist,
1264 Make_Discriminant_Specification (Loc,
1265 Defining_Identifier => New_Disc,
1266 Discriminant_Type =>
1267 New_Occurrence_Of (Etype (Disc), Loc),
1268 Expression =>
1269 New_Copy (Discriminant_Default_Value (Disc))));
1271 Next_Discriminant (Disc);
1272 end loop;
1274 else
1275 Dlist := No_List;
1276 end if;
1278 -- Now we can construct the record type declaration. Note that this
1279 -- record is "limited tagged". It is "limited" to reflect the underlying
1280 -- limitedness of the task or protected object that it represents, and
1281 -- ensuring for example that it is properly passed by reference. It is
1282 -- "tagged" to give support to dispatching calls through interfaces. We
1283 -- propagate here the list of interfaces covered by the concurrent type
1284 -- (Ada 2005: AI-345).
1286 return
1287 Make_Full_Type_Declaration (Loc,
1288 Defining_Identifier => Rec_Ent,
1289 Discriminant_Specifications => Dlist,
1290 Type_Definition =>
1291 Make_Record_Definition (Loc,
1292 Component_List =>
1293 Make_Component_List (Loc,
1294 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_Entry_Count_Expression --
1303 ----------------------------------
1305 function Build_Entry_Count_Expression
1306 (Concurrent_Type : Node_Id;
1307 Component_List : List_Id;
1308 Loc : Source_Ptr) return Node_Id
1310 Eindx : Nat;
1311 Ent : Entity_Id;
1312 Ecount : Node_Id;
1313 Comp : Node_Id;
1314 Lo : Node_Id;
1315 Hi : Node_Id;
1316 Typ : Entity_Id;
1317 Large : Boolean;
1319 begin
1320 -- Count number of non-family entries
1322 Eindx := 0;
1323 Ent := First_Entity (Concurrent_Type);
1324 while Present (Ent) loop
1325 if Ekind (Ent) = E_Entry then
1326 Eindx := Eindx + 1;
1327 end if;
1329 Next_Entity (Ent);
1330 end loop;
1332 Ecount := Make_Integer_Literal (Loc, Eindx);
1334 -- Loop through entry families building the addition nodes
1336 Ent := First_Entity (Concurrent_Type);
1337 Comp := First (Component_List);
1338 while Present (Ent) loop
1339 if Ekind (Ent) = E_Entry_Family then
1340 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1341 Next (Comp);
1342 end loop;
1344 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1345 Hi := Type_High_Bound (Typ);
1346 Lo := Type_Low_Bound (Typ);
1347 Large := Is_Potentially_Large_Family
1348 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1349 Ecount :=
1350 Make_Op_Add (Loc,
1351 Left_Opnd => Ecount,
1352 Right_Opnd => Family_Size
1353 (Loc, Hi, Lo, Concurrent_Type, Large));
1354 end if;
1356 Next_Entity (Ent);
1357 end loop;
1359 return Ecount;
1360 end Build_Entry_Count_Expression;
1362 -----------------------
1363 -- Build_Entry_Names --
1364 -----------------------
1366 procedure Build_Entry_Names
1367 (Obj_Ref : Node_Id;
1368 Obj_Typ : Entity_Id;
1369 Stmts : List_Id)
1371 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1372 Data : Entity_Id := Empty;
1373 Index : Entity_Id := Empty;
1374 Typ : Entity_Id := Obj_Typ;
1376 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1377 -- Given an entry [family], create a static string which denotes the
1378 -- name of Comp_Id and assign it to the underlying data structure which
1379 -- contains the entry names of a concurrent object.
1381 function Object_Reference return Node_Id;
1382 -- Return a reference to field _object or _task_id depending on the
1383 -- concurrent object being processed.
1385 ----------------------
1386 -- Build_Entry_Name --
1387 ----------------------
1389 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1390 function Build_Range (Def : Node_Id) return Node_Id;
1391 -- Given a discrete subtype definition of an entry family, generate a
1392 -- range node which covers the range of Def's type.
1394 procedure Create_Index_And_Data;
1395 -- Generate the declarations of variables Index and Data. Subsequent
1396 -- calls do nothing.
1398 function Increment_Index return Node_Id;
1399 -- Increment the index used in the assignment of string names to the
1400 -- Data array.
1402 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1403 -- Given the name of a temporary variable, create the following
1404 -- declaration for it:
1406 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1408 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1409 -- Given the name of a temporary variable, place it in the array of
1410 -- string names. Generate:
1412 -- Data (Index) := Def_Id'Unchecked_Access;
1414 -----------------
1415 -- Build_Range --
1416 -----------------
1418 function Build_Range (Def : Node_Id) return Node_Id is
1419 High : Node_Id := Type_High_Bound (Etype (Def));
1420 Low : Node_Id := Type_Low_Bound (Etype (Def));
1422 begin
1423 -- If a bound references a discriminant, generate an identifier
1424 -- with the same name. Resolution will map it to the formals of
1425 -- the init proc.
1427 if Is_Entity_Name (Low)
1428 and then Ekind (Entity (Low)) = E_Discriminant
1429 then
1430 Low :=
1431 Make_Selected_Component (Loc,
1432 Prefix => New_Copy_Tree (Obj_Ref),
1433 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1434 else
1435 Low := New_Copy_Tree (Low);
1436 end if;
1438 if Is_Entity_Name (High)
1439 and then Ekind (Entity (High)) = E_Discriminant
1440 then
1441 High :=
1442 Make_Selected_Component (Loc,
1443 Prefix => New_Copy_Tree (Obj_Ref),
1444 Selector_Name => Make_Identifier (Loc, Chars (High)));
1445 else
1446 High := New_Copy_Tree (High);
1447 end if;
1449 return
1450 Make_Range (Loc,
1451 Low_Bound => Low,
1452 High_Bound => High);
1453 end Build_Range;
1455 ---------------------------
1456 -- Create_Index_And_Data --
1457 ---------------------------
1459 procedure Create_Index_And_Data is
1460 begin
1461 if No (Index) and then No (Data) then
1462 declare
1463 Count : RE_Id;
1464 Data_Typ : RE_Id;
1465 Size : Entity_Id;
1467 begin
1468 if Is_Protected_Type (Typ) then
1469 Count := RO_PE_Number_Of_Entries;
1470 Data_Typ := RE_Protected_Entry_Names_Array;
1471 else
1472 Count := RO_ST_Number_Of_Entries;
1473 Data_Typ := RE_Task_Entry_Names_Array;
1474 end if;
1476 -- Step 1: Generate the declaration of the index variable:
1478 -- Index : Entry_Index := 1;
1480 Index := Make_Temporary (Loc, 'I');
1482 Append_To (Stmts,
1483 Make_Object_Declaration (Loc,
1484 Defining_Identifier => Index,
1485 Object_Definition =>
1486 New_Reference_To (RTE (RE_Entry_Index), Loc),
1487 Expression => Make_Integer_Literal (Loc, 1)));
1489 -- Step 2: Generate the declaration of an array to house all
1490 -- names:
1492 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1493 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1495 Size := Make_Temporary (Loc, 'S');
1497 Append_To (Stmts,
1498 Make_Object_Declaration (Loc,
1499 Defining_Identifier => Size,
1500 Constant_Present => True,
1501 Object_Definition =>
1502 New_Reference_To (RTE (RE_Entry_Index), Loc),
1503 Expression =>
1504 Make_Function_Call (Loc,
1505 Name =>
1506 New_Reference_To (RTE (Count), Loc),
1507 Parameter_Associations =>
1508 New_List (Object_Reference))));
1510 Data := Make_Temporary (Loc, 'A');
1512 Append_To (Stmts,
1513 Make_Object_Declaration (Loc,
1514 Defining_Identifier => Data,
1515 Aliased_Present => True,
1516 Object_Definition =>
1517 New_Reference_To (RTE (Data_Typ), Loc),
1518 Expression =>
1519 Make_Aggregate (Loc,
1520 Component_Associations => New_List (
1521 Make_Component_Association (Loc,
1522 Choices => New_List (
1523 Make_Range (Loc,
1524 Low_Bound => Make_Integer_Literal (Loc, 1),
1525 High_Bound => New_Reference_To (Size, Loc))),
1526 Expression => Make_Null (Loc))))));
1527 end;
1528 end if;
1529 end Create_Index_And_Data;
1531 ---------------------
1532 -- Increment_Index --
1533 ---------------------
1535 function Increment_Index return Node_Id is
1536 begin
1537 return
1538 Make_Assignment_Statement (Loc,
1539 Name => New_Reference_To (Index, Loc),
1540 Expression =>
1541 Make_Op_Add (Loc,
1542 Left_Opnd => New_Reference_To (Index, Loc),
1543 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1544 end Increment_Index;
1546 ----------------------
1547 -- Name_Declaration --
1548 ----------------------
1550 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1551 begin
1552 return
1553 Make_Object_Declaration (Loc,
1554 Defining_Identifier => Def_Id,
1555 Aliased_Present => True,
1556 Constant_Present => True,
1557 Object_Definition => New_Reference_To (Standard_String, Loc),
1558 Expression =>
1559 Make_String_Literal (Loc, String_From_Name_Buffer));
1560 end Name_Declaration;
1562 --------------------
1563 -- Set_Entry_Name --
1564 --------------------
1566 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1567 begin
1568 return
1569 Make_Assignment_Statement (Loc,
1570 Name =>
1571 Make_Indexed_Component (Loc,
1572 Prefix => New_Reference_To (Data, Loc),
1573 Expressions => New_List (New_Reference_To (Index, Loc))),
1575 Expression =>
1576 Make_Attribute_Reference (Loc,
1577 Prefix => New_Reference_To (Def_Id, Loc),
1578 Attribute_Name => Name_Unchecked_Access));
1579 end Set_Entry_Name;
1581 -- Local variables
1583 Temp_Id : Entity_Id;
1584 Subt_Def : Node_Id;
1586 -- Start of processing for Build_Entry_Name
1588 begin
1589 if Ekind (Comp_Id) = E_Entry_Family then
1590 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1592 Create_Index_And_Data;
1594 -- Step 1: Create the string name of the entry family.
1595 -- Generate:
1596 -- Temp : aliased constant String := "name ()";
1598 Temp_Id := Make_Temporary (Loc, 'S');
1599 Get_Name_String (Chars (Comp_Id));
1600 Add_Char_To_Name_Buffer (' ');
1601 Add_Char_To_Name_Buffer ('(');
1602 Add_Char_To_Name_Buffer (')');
1604 Append_To (Stmts, Name_Declaration (Temp_Id));
1606 -- Generate:
1607 -- for Member in Family_Low .. Family_High loop
1608 -- Set_Entry_Name (...);
1609 -- Index := Index + 1;
1610 -- end loop;
1612 Append_To (Stmts,
1613 Make_Loop_Statement (Loc,
1614 Iteration_Scheme =>
1615 Make_Iteration_Scheme (Loc,
1616 Loop_Parameter_Specification =>
1617 Make_Loop_Parameter_Specification (Loc,
1618 Defining_Identifier =>
1619 Make_Temporary (Loc, 'L'),
1620 Discrete_Subtype_Definition =>
1621 Build_Range (Subt_Def))),
1623 Statements => New_List (
1624 Set_Entry_Name (Temp_Id),
1625 Increment_Index),
1626 End_Label => Empty));
1628 -- Entry
1630 else
1631 Create_Index_And_Data;
1633 -- Step 1: Create the string name of the entry. Generate:
1634 -- Temp : aliased constant String := "name";
1636 Temp_Id := Make_Temporary (Loc, 'S');
1637 Get_Name_String (Chars (Comp_Id));
1639 Append_To (Stmts, Name_Declaration (Temp_Id));
1641 -- Step 2: Associate the string name with the underlying data
1642 -- structure.
1644 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1645 Append_To (Stmts, Increment_Index);
1646 end if;
1647 end Build_Entry_Name;
1649 ----------------------
1650 -- Object_Reference --
1651 ----------------------
1653 function Object_Reference return Node_Id is
1654 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1655 Field : Name_Id;
1656 Ref : Node_Id;
1658 begin
1659 if Is_Protected_Type (Typ) then
1660 Field := Name_uObject;
1661 else
1662 Field := Name_uTask_Id;
1663 end if;
1665 Ref :=
1666 Make_Selected_Component (Loc,
1667 Prefix =>
1668 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1669 Selector_Name => Make_Identifier (Loc, Field));
1671 if Is_Protected_Type (Typ) then
1672 Ref :=
1673 Make_Attribute_Reference (Loc,
1674 Prefix => Ref,
1675 Attribute_Name => Name_Unchecked_Access);
1676 end if;
1678 return Ref;
1679 end Object_Reference;
1681 -- Local variables
1683 Comp : Node_Id;
1684 Proc : RE_Id;
1686 -- Start of processing for Build_Entry_Names
1688 begin
1689 -- Retrieve the original concurrent type
1691 if Is_Concurrent_Record_Type (Typ) then
1692 Typ := Corresponding_Concurrent_Type (Typ);
1693 end if;
1695 pragma Assert (Is_Concurrent_Type (Typ));
1697 -- Nothing to do if the type has no entries
1699 if not Has_Entries (Typ) then
1700 return;
1701 end if;
1703 -- Avoid generating entry names for a protected type with only one entry
1705 if Is_Protected_Type (Typ)
1706 and then Find_Protection_Type (Base_Type (Typ)) /=
1707 RTE (RE_Protection_Entries)
1708 then
1709 return;
1710 end if;
1712 -- Step 1: Populate the array with statically generated strings denoting
1713 -- entries and entry family names.
1715 Comp := First_Entity (Typ);
1716 while Present (Comp) loop
1717 if Comes_From_Source (Comp)
1718 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
1719 then
1720 Build_Entry_Name (Comp);
1721 end if;
1723 Next_Entity (Comp);
1724 end loop;
1726 -- Step 2: Associate the array with the related concurrent object:
1728 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
1730 if Present (Data) then
1731 if Is_Protected_Type (Typ) then
1732 Proc := RO_PE_Set_Entry_Names;
1733 else
1734 Proc := RO_ST_Set_Entry_Names;
1735 end if;
1737 Append_To (Stmts,
1738 Make_Procedure_Call_Statement (Loc,
1739 Name => New_Reference_To (RTE (Proc), Loc),
1740 Parameter_Associations => New_List (
1741 Object_Reference,
1742 Make_Attribute_Reference (Loc,
1743 Prefix => New_Reference_To (Data, Loc),
1744 Attribute_Name => Name_Unchecked_Access))));
1745 end if;
1746 end Build_Entry_Names;
1748 ---------------------------
1749 -- Build_Parameter_Block --
1750 ---------------------------
1752 function Build_Parameter_Block
1753 (Loc : Source_Ptr;
1754 Actuals : List_Id;
1755 Formals : List_Id;
1756 Decls : List_Id) return Entity_Id
1758 Actual : Entity_Id;
1759 Comp_Nam : Node_Id;
1760 Comps : List_Id;
1761 Formal : Entity_Id;
1762 Has_Comp : Boolean := False;
1763 Rec_Nam : Node_Id;
1765 begin
1766 Actual := First (Actuals);
1767 Comps := New_List;
1768 Formal := Defining_Identifier (First (Formals));
1770 while Present (Actual) loop
1771 if not Is_Controlling_Actual (Actual) then
1773 -- Generate:
1774 -- type Ann is access all <actual-type>
1776 Comp_Nam := Make_Temporary (Loc, 'A');
1778 Append_To (Decls,
1779 Make_Full_Type_Declaration (Loc,
1780 Defining_Identifier => Comp_Nam,
1781 Type_Definition =>
1782 Make_Access_To_Object_Definition (Loc,
1783 All_Present => True,
1784 Constant_Present => Ekind (Formal) = E_In_Parameter,
1785 Subtype_Indication =>
1786 New_Reference_To (Etype (Actual), Loc))));
1788 -- Generate:
1789 -- Param : Ann;
1791 Append_To (Comps,
1792 Make_Component_Declaration (Loc,
1793 Defining_Identifier =>
1794 Make_Defining_Identifier (Loc, Chars (Formal)),
1795 Component_Definition =>
1796 Make_Component_Definition (Loc,
1797 Aliased_Present =>
1798 False,
1799 Subtype_Indication =>
1800 New_Reference_To (Comp_Nam, Loc))));
1802 Has_Comp := True;
1803 end if;
1805 Next_Actual (Actual);
1806 Next_Formal_With_Extras (Formal);
1807 end loop;
1809 Rec_Nam := Make_Temporary (Loc, 'P');
1811 if Has_Comp then
1813 -- Generate:
1814 -- type Pnn is record
1815 -- Param1 : Ann1;
1816 -- ...
1817 -- ParamN : AnnN;
1819 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1820 -- the original parameter names and Ann1 .. AnnN are the access to
1821 -- actual types.
1823 Append_To (Decls,
1824 Make_Full_Type_Declaration (Loc,
1825 Defining_Identifier =>
1826 Rec_Nam,
1827 Type_Definition =>
1828 Make_Record_Definition (Loc,
1829 Component_List =>
1830 Make_Component_List (Loc, Comps))));
1831 else
1832 -- Generate:
1833 -- type Pnn is null record;
1835 Append_To (Decls,
1836 Make_Full_Type_Declaration (Loc,
1837 Defining_Identifier =>
1838 Rec_Nam,
1839 Type_Definition =>
1840 Make_Record_Definition (Loc,
1841 Null_Present => True,
1842 Component_List => Empty)));
1843 end if;
1845 return Rec_Nam;
1846 end Build_Parameter_Block;
1848 --------------------------------------
1849 -- Build_Renamed_Formal_Declaration --
1850 --------------------------------------
1852 function Build_Renamed_Formal_Declaration
1853 (New_F : Entity_Id;
1854 Formal : Entity_Id;
1855 Comp : Entity_Id;
1856 Renamed_Formal : Node_Id) return Node_Id
1858 Loc : constant Source_Ptr := Sloc (New_F);
1859 Decl : Node_Id;
1861 begin
1862 -- If the formal is a tagged incomplete type, it is already passed
1863 -- by reference, so it is sufficient to rename the pointer component
1864 -- that corresponds to the actual. Otherwise we need to dereference
1865 -- the pointer component to obtain the actual.
1867 if Is_Incomplete_Type (Etype (Formal))
1868 and then Is_Tagged_Type (Etype (Formal))
1869 then
1870 Decl :=
1871 Make_Object_Renaming_Declaration (Loc,
1872 Defining_Identifier => New_F,
1873 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1874 Name => Renamed_Formal);
1876 else
1877 Decl :=
1878 Make_Object_Renaming_Declaration (Loc,
1879 Defining_Identifier => New_F,
1880 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1881 Name =>
1882 Make_Explicit_Dereference (Loc, Renamed_Formal));
1883 end if;
1885 return Decl;
1886 end Build_Renamed_Formal_Declaration;
1888 -----------------------
1889 -- Build_PPC_Wrapper --
1890 -----------------------
1892 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1893 Loc : constant Source_Ptr := Sloc (E);
1894 Synch_Type : constant Entity_Id := Scope (E);
1896 Wrapper_Id : constant Entity_Id :=
1897 Make_Defining_Identifier (Loc,
1898 Chars => New_External_Name (Chars (E), 'E'));
1899 -- the wrapper procedure name
1901 Wrapper_Body : Node_Id;
1903 Synch_Id : constant Entity_Id :=
1904 Make_Defining_Identifier (Loc,
1905 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1906 -- The parameter that designates the synchronized object in the call
1908 Actuals : constant List_Id := New_List;
1909 -- The actuals in the entry call
1911 Decls : constant List_Id := New_List;
1913 Entry_Call : Node_Id;
1914 Entry_Name : Node_Id;
1916 Specs : List_Id;
1917 -- The specification of the wrapper procedure
1919 begin
1921 -- Only build the wrapper if entry has pre/postconditions.
1922 -- Should this be done unconditionally instead ???
1924 declare
1925 P : Node_Id;
1927 begin
1928 P := Spec_PPC_List (Contract (E));
1929 if No (P) then
1930 return;
1931 end if;
1933 -- Transfer ppc pragmas to the declarations of the wrapper
1935 while Present (P) loop
1936 if Pragma_Name (P) = Name_Precondition
1937 or else Pragma_Name (P) = Name_Postcondition
1938 then
1939 Append (Relocate_Node (P), Decls);
1940 Set_Analyzed (Last (Decls), False);
1941 end if;
1943 P := Next_Pragma (P);
1944 end loop;
1945 end;
1947 -- First formal is synchronized object
1949 Specs := New_List (
1950 Make_Parameter_Specification (Loc,
1951 Defining_Identifier => Synch_Id,
1952 Out_Present => True,
1953 In_Present => True,
1954 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1956 Entry_Name :=
1957 Make_Selected_Component (Loc,
1958 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1959 Selector_Name => New_Occurrence_Of (E, Loc));
1961 -- If entity is entry family, second formal is the corresponding index,
1962 -- and entry name is an indexed component.
1964 if Ekind (E) = E_Entry_Family then
1965 declare
1966 Index : constant Entity_Id :=
1967 Make_Defining_Identifier (Loc, Name_I);
1968 begin
1969 Append_To (Specs,
1970 Make_Parameter_Specification (Loc,
1971 Defining_Identifier => Index,
1972 Parameter_Type =>
1973 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1975 Entry_Name :=
1976 Make_Indexed_Component (Loc,
1977 Prefix => Entry_Name,
1978 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
1979 end;
1980 end if;
1982 Entry_Call :=
1983 Make_Procedure_Call_Statement (Loc,
1984 Name => Entry_Name,
1985 Parameter_Associations => Actuals);
1987 -- Now add formals that match those of the entry, and build actuals for
1988 -- the nested entry call.
1990 declare
1991 Form : Entity_Id;
1992 New_Form : Entity_Id;
1993 Parm_Spec : Node_Id;
1995 begin
1996 Form := First_Formal (E);
1997 while Present (Form) loop
1998 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
1999 Parm_Spec :=
2000 Make_Parameter_Specification (Loc,
2001 Defining_Identifier => New_Form,
2002 Out_Present => Out_Present (Parent (Form)),
2003 In_Present => In_Present (Parent (Form)),
2004 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
2006 Append (Parm_Spec, Specs);
2007 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
2008 Next_Formal (Form);
2009 end loop;
2010 end;
2012 -- Add renaming declarations for the discriminants of the enclosing
2013 -- type, which may be visible in the preconditions.
2015 if Has_Discriminants (Synch_Type) then
2016 declare
2017 D : Entity_Id;
2018 Decl : Node_Id;
2020 begin
2021 D := First_Discriminant (Synch_Type);
2022 while Present (D) loop
2023 Decl :=
2024 Make_Object_Renaming_Declaration (Loc,
2025 Defining_Identifier =>
2026 Make_Defining_Identifier (Loc, Chars (D)),
2027 Subtype_Mark => New_Reference_To (Etype (D), Loc),
2028 Name =>
2029 Make_Selected_Component (Loc,
2030 Prefix => New_Reference_To (Synch_Id, Loc),
2031 Selector_Name => Make_Identifier (Loc, Chars (D))));
2032 Prepend (Decl, Decls);
2033 Next_Discriminant (D);
2034 end loop;
2035 end;
2036 end if;
2038 Set_PPC_Wrapper (E, Wrapper_Id);
2039 Wrapper_Body :=
2040 Make_Subprogram_Body (Loc,
2041 Specification =>
2042 Make_Procedure_Specification (Loc,
2043 Defining_Unit_Name => Wrapper_Id,
2044 Parameter_Specifications => Specs),
2045 Declarations => Decls,
2046 Handled_Statement_Sequence =>
2047 Make_Handled_Sequence_Of_Statements (Loc,
2048 Statements => New_List (Entry_Call)));
2050 -- The wrapper body is analyzed when the enclosing type is frozen
2052 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
2053 end Build_PPC_Wrapper;
2055 --------------------------
2056 -- Build_Wrapper_Bodies --
2057 --------------------------
2059 procedure Build_Wrapper_Bodies
2060 (Loc : Source_Ptr;
2061 Typ : Entity_Id;
2062 N : Node_Id)
2064 Rec_Typ : Entity_Id;
2066 function Build_Wrapper_Body
2067 (Loc : Source_Ptr;
2068 Subp_Id : Entity_Id;
2069 Obj_Typ : Entity_Id;
2070 Formals : List_Id) return Node_Id;
2071 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2072 -- associated with a protected or task type. Subp_Id is the subprogram
2073 -- name which will be wrapped. Obj_Typ is the type of the new formal
2074 -- parameter which handles dispatching and object notation. Formals are
2075 -- the original formals of Subp_Id which will be explicitly replicated.
2077 ------------------------
2078 -- Build_Wrapper_Body --
2079 ------------------------
2081 function Build_Wrapper_Body
2082 (Loc : Source_Ptr;
2083 Subp_Id : Entity_Id;
2084 Obj_Typ : Entity_Id;
2085 Formals : List_Id) return Node_Id
2087 Body_Spec : Node_Id;
2089 begin
2090 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2092 -- The subprogram is not overriding or is not a primitive declared
2093 -- between two views.
2095 if No (Body_Spec) then
2096 return Empty;
2097 end if;
2099 declare
2100 Actuals : List_Id := No_List;
2101 Conv_Id : Node_Id;
2102 First_Form : Node_Id;
2103 Formal : Node_Id;
2104 Nam : Node_Id;
2106 begin
2107 -- Map formals to actuals. Use the list built for the wrapper
2108 -- spec, skipping the object notation parameter.
2110 First_Form := First (Parameter_Specifications (Body_Spec));
2112 Formal := First_Form;
2113 Next (Formal);
2115 if Present (Formal) then
2116 Actuals := New_List;
2117 while Present (Formal) loop
2118 Append_To (Actuals,
2119 Make_Identifier (Loc,
2120 Chars => Chars (Defining_Identifier (Formal))));
2121 Next (Formal);
2122 end loop;
2123 end if;
2125 -- Special processing for primitives declared between a private
2126 -- type and its completion: the wrapper needs a properly typed
2127 -- parameter if the wrapped operation has a controlling first
2128 -- parameter. Note that this might not be the case for a function
2129 -- with a controlling result.
2131 if Is_Private_Primitive_Subprogram (Subp_Id) then
2132 if No (Actuals) then
2133 Actuals := New_List;
2134 end if;
2136 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2137 Prepend_To (Actuals,
2138 Unchecked_Convert_To
2139 (Corresponding_Concurrent_Type (Obj_Typ),
2140 Make_Identifier (Loc, Name_uO)));
2142 else
2143 Prepend_To (Actuals,
2144 Make_Identifier (Loc,
2145 Chars => Chars (Defining_Identifier (First_Form))));
2146 end if;
2148 Nam := New_Reference_To (Subp_Id, Loc);
2149 else
2150 -- An access-to-variable object parameter requires an explicit
2151 -- dereference in the unchecked conversion. This case occurs
2152 -- when a protected entry wrapper must override an interface
2153 -- level procedure with interface access as first parameter.
2155 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2157 if Nkind (Parameter_Type (First_Form)) =
2158 N_Access_Definition
2159 then
2160 Conv_Id :=
2161 Make_Explicit_Dereference (Loc,
2162 Prefix => Make_Identifier (Loc, Name_uO));
2163 else
2164 Conv_Id := Make_Identifier (Loc, Name_uO);
2165 end if;
2167 Nam :=
2168 Make_Selected_Component (Loc,
2169 Prefix =>
2170 Unchecked_Convert_To
2171 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2172 Selector_Name => New_Reference_To (Subp_Id, Loc));
2173 end if;
2175 -- Create the subprogram body. For a function, the call to the
2176 -- actual subprogram has to be converted to the corresponding
2177 -- record if it is a controlling result.
2179 if Ekind (Subp_Id) = E_Function then
2180 declare
2181 Res : Node_Id;
2183 begin
2184 Res :=
2185 Make_Function_Call (Loc,
2186 Name => Nam,
2187 Parameter_Associations => Actuals);
2189 if Has_Controlling_Result (Subp_Id) then
2190 Res :=
2191 Unchecked_Convert_To
2192 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2193 end if;
2195 return
2196 Make_Subprogram_Body (Loc,
2197 Specification => Body_Spec,
2198 Declarations => Empty_List,
2199 Handled_Statement_Sequence =>
2200 Make_Handled_Sequence_Of_Statements (Loc,
2201 Statements => New_List (
2202 Make_Simple_Return_Statement (Loc, Res))));
2203 end;
2205 else
2206 return
2207 Make_Subprogram_Body (Loc,
2208 Specification => Body_Spec,
2209 Declarations => Empty_List,
2210 Handled_Statement_Sequence =>
2211 Make_Handled_Sequence_Of_Statements (Loc,
2212 Statements => New_List (
2213 Make_Procedure_Call_Statement (Loc,
2214 Name => Nam,
2215 Parameter_Associations => Actuals))));
2216 end if;
2217 end;
2218 end Build_Wrapper_Body;
2220 -- Start of processing for Build_Wrapper_Bodies
2222 begin
2223 if Is_Concurrent_Type (Typ) then
2224 Rec_Typ := Corresponding_Record_Type (Typ);
2225 else
2226 Rec_Typ := Typ;
2227 end if;
2229 -- Generate wrapper bodies for a concurrent type which implements an
2230 -- interface.
2232 if Present (Interfaces (Rec_Typ)) then
2233 declare
2234 Insert_Nod : Node_Id;
2235 Prim : Entity_Id;
2236 Prim_Elmt : Elmt_Id;
2237 Prim_Decl : Node_Id;
2238 Subp : Entity_Id;
2239 Wrap_Body : Node_Id;
2240 Wrap_Id : Entity_Id;
2242 begin
2243 Insert_Nod := N;
2245 -- Examine all primitive operations of the corresponding record
2246 -- type, looking for wrapper specs. Generate bodies in order to
2247 -- complete them.
2249 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2250 while Present (Prim_Elmt) loop
2251 Prim := Node (Prim_Elmt);
2253 if (Ekind (Prim) = E_Function
2254 or else Ekind (Prim) = E_Procedure)
2255 and then Is_Primitive_Wrapper (Prim)
2256 then
2257 Subp := Wrapped_Entity (Prim);
2258 Prim_Decl := Parent (Parent (Prim));
2260 Wrap_Body :=
2261 Build_Wrapper_Body (Loc,
2262 Subp_Id => Subp,
2263 Obj_Typ => Rec_Typ,
2264 Formals => Parameter_Specifications (Parent (Subp)));
2265 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2267 Set_Corresponding_Spec (Wrap_Body, Prim);
2268 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2270 Insert_After (Insert_Nod, Wrap_Body);
2271 Insert_Nod := Wrap_Body;
2273 Analyze (Wrap_Body);
2274 end if;
2276 Next_Elmt (Prim_Elmt);
2277 end loop;
2278 end;
2279 end if;
2280 end Build_Wrapper_Bodies;
2282 ------------------------
2283 -- Build_Wrapper_Spec --
2284 ------------------------
2286 function Build_Wrapper_Spec
2287 (Subp_Id : Entity_Id;
2288 Obj_Typ : Entity_Id;
2289 Formals : List_Id) return Node_Id
2291 Loc : constant Source_Ptr := Sloc (Subp_Id);
2292 First_Param : Node_Id;
2293 Iface : Entity_Id;
2294 Iface_Elmt : Elmt_Id;
2295 Iface_Op : Entity_Id;
2296 Iface_Op_Elmt : Elmt_Id;
2298 function Overriding_Possible
2299 (Iface_Op : Entity_Id;
2300 Wrapper : Entity_Id) return Boolean;
2301 -- Determine whether a primitive operation can be overridden by Wrapper.
2302 -- Iface_Op is the candidate primitive operation of an interface type,
2303 -- Wrapper is the generated entry wrapper.
2305 function Replicate_Formals
2306 (Loc : Source_Ptr;
2307 Formals : List_Id) return List_Id;
2308 -- An explicit parameter replication is required due to the Is_Entry_
2309 -- Formal flag being set for all the formals of an entry. The explicit
2310 -- replication removes the flag that would otherwise cause a different
2311 -- path of analysis.
2313 -------------------------
2314 -- Overriding_Possible --
2315 -------------------------
2317 function Overriding_Possible
2318 (Iface_Op : Entity_Id;
2319 Wrapper : Entity_Id) return Boolean
2321 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2322 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2324 function Type_Conformant_Parameters
2325 (Iface_Op_Params : List_Id;
2326 Wrapper_Params : List_Id) return Boolean;
2327 -- Determine whether the parameters of the generated entry wrapper
2328 -- and those of a primitive operation are type conformant. During
2329 -- this check, the first parameter of the primitive operation is
2330 -- skipped if it is a controlling argument: protected functions
2331 -- may have a controlling result.
2333 --------------------------------
2334 -- Type_Conformant_Parameters --
2335 --------------------------------
2337 function Type_Conformant_Parameters
2338 (Iface_Op_Params : List_Id;
2339 Wrapper_Params : List_Id) return Boolean
2341 Iface_Op_Param : Node_Id;
2342 Iface_Op_Typ : Entity_Id;
2343 Wrapper_Param : Node_Id;
2344 Wrapper_Typ : Entity_Id;
2346 begin
2347 -- Skip the first (controlling) parameter of primitive operation
2349 Iface_Op_Param := First (Iface_Op_Params);
2351 if Present (First_Formal (Iface_Op))
2352 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2353 then
2354 Iface_Op_Param := Next (Iface_Op_Param);
2355 end if;
2357 Wrapper_Param := First (Wrapper_Params);
2358 while Present (Iface_Op_Param)
2359 and then Present (Wrapper_Param)
2360 loop
2361 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2362 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2364 -- The two parameters must be mode conformant
2366 if not Conforming_Types
2367 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2368 then
2369 return False;
2370 end if;
2372 Next (Iface_Op_Param);
2373 Next (Wrapper_Param);
2374 end loop;
2376 -- One of the lists is longer than the other
2378 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2379 return False;
2380 end if;
2382 return True;
2383 end Type_Conformant_Parameters;
2385 -- Start of processing for Overriding_Possible
2387 begin
2388 if Chars (Iface_Op) /= Chars (Wrapper) then
2389 return False;
2390 end if;
2392 -- If an inherited subprogram is implemented by a protected procedure
2393 -- or an entry, then the first parameter of the inherited subprogram
2394 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2396 if Ekind (Iface_Op) = E_Procedure
2397 and then Present (Parameter_Specifications (Iface_Op_Spec))
2398 then
2399 declare
2400 Obj_Param : constant Node_Id :=
2401 First (Parameter_Specifications (Iface_Op_Spec));
2402 begin
2403 if not Out_Present (Obj_Param)
2404 and then Nkind (Parameter_Type (Obj_Param)) /=
2405 N_Access_Definition
2406 then
2407 return False;
2408 end if;
2409 end;
2410 end if;
2412 return
2413 Type_Conformant_Parameters (
2414 Parameter_Specifications (Iface_Op_Spec),
2415 Parameter_Specifications (Wrapper_Spec));
2416 end Overriding_Possible;
2418 -----------------------
2419 -- Replicate_Formals --
2420 -----------------------
2422 function Replicate_Formals
2423 (Loc : Source_Ptr;
2424 Formals : List_Id) return List_Id
2426 New_Formals : constant List_Id := New_List;
2427 Formal : Node_Id;
2428 Param_Type : Node_Id;
2430 begin
2431 Formal := First (Formals);
2433 -- Skip the object parameter when dealing with primitives declared
2434 -- between two views.
2436 if Is_Private_Primitive_Subprogram (Subp_Id)
2437 and then not Has_Controlling_Result (Subp_Id)
2438 then
2439 Formal := Next (Formal);
2440 end if;
2442 while Present (Formal) loop
2444 -- Create an explicit copy of the entry parameter
2446 -- When creating the wrapper subprogram for a primitive operation
2447 -- of a protected interface we must construct an equivalent
2448 -- signature to that of the overriding operation. For regular
2449 -- parameters we can just use the type of the formal, but for
2450 -- access to subprogram parameters we need to reanalyze the
2451 -- parameter type to create local entities for the signature of
2452 -- the subprogram type. Using the entities of the overriding
2453 -- subprogram will result in out-of-scope errors in the back-end.
2455 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2456 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2457 else
2458 Param_Type :=
2459 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2460 end if;
2462 Append_To (New_Formals,
2463 Make_Parameter_Specification (Loc,
2464 Defining_Identifier =>
2465 Make_Defining_Identifier (Loc,
2466 Chars => Chars (Defining_Identifier (Formal))),
2467 In_Present => In_Present (Formal),
2468 Out_Present => Out_Present (Formal),
2469 Parameter_Type => Param_Type));
2471 Next (Formal);
2472 end loop;
2474 return New_Formals;
2475 end Replicate_Formals;
2477 -- Start of processing for Build_Wrapper_Spec
2479 begin
2480 -- There is no point in building wrappers for non-tagged concurrent
2481 -- types.
2483 pragma Assert (Is_Tagged_Type (Obj_Typ));
2485 -- An entry or a protected procedure can override a routine where the
2486 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2487 -- type. Since the wrapper must have the exact same signature as that of
2488 -- the overridden subprogram, we try to find the overriding candidate
2489 -- and use its controlling formal.
2491 First_Param := Empty;
2493 -- Check every implemented interface
2495 if Present (Interfaces (Obj_Typ)) then
2496 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2497 Search : while Present (Iface_Elmt) loop
2498 Iface := Node (Iface_Elmt);
2500 -- Check every interface primitive
2502 if Present (Primitive_Operations (Iface)) then
2503 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2504 while Present (Iface_Op_Elmt) loop
2505 Iface_Op := Node (Iface_Op_Elmt);
2507 -- Ignore predefined primitives
2509 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2510 Iface_Op := Ultimate_Alias (Iface_Op);
2512 -- The current primitive operation can be overridden by
2513 -- the generated entry wrapper.
2515 if Overriding_Possible (Iface_Op, Subp_Id) then
2516 First_Param :=
2517 First (Parameter_Specifications (Parent (Iface_Op)));
2519 exit Search;
2520 end if;
2521 end if;
2523 Next_Elmt (Iface_Op_Elmt);
2524 end loop;
2525 end if;
2527 Next_Elmt (Iface_Elmt);
2528 end loop Search;
2529 end if;
2531 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2532 -- this subprogram and this is not a primitive declared between two
2533 -- views then force the generation of a wrapper. As an optimization,
2534 -- previous versions of the frontend avoid generating the wrapper;
2535 -- however, the wrapper facilitates locating and reporting an error
2536 -- when a duplicate declaration is found later. See example in
2537 -- AI05-0090-1.
2539 if No (First_Param)
2540 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2541 then
2542 if Is_Task_Type
2543 (Corresponding_Concurrent_Type (Obj_Typ))
2544 then
2545 First_Param :=
2546 Make_Parameter_Specification (Loc,
2547 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2548 In_Present => True,
2549 Out_Present => False,
2550 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2552 -- For entries and procedures of protected types the mode of
2553 -- the controlling argument must be in-out.
2555 else
2556 First_Param :=
2557 Make_Parameter_Specification (Loc,
2558 Defining_Identifier =>
2559 Make_Defining_Identifier (Loc,
2560 Chars => Name_uO),
2561 In_Present => True,
2562 Out_Present => (Ekind (Subp_Id) /= E_Function),
2563 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2564 end if;
2565 end if;
2567 declare
2568 Wrapper_Id : constant Entity_Id :=
2569 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2570 New_Formals : List_Id;
2571 Obj_Param : Node_Id;
2572 Obj_Param_Typ : Entity_Id;
2574 begin
2575 -- Minimum decoration is needed to catch the entity in
2576 -- Sem_Ch6.Override_Dispatching_Operation.
2578 if Ekind (Subp_Id) = E_Function then
2579 Set_Ekind (Wrapper_Id, E_Function);
2580 else
2581 Set_Ekind (Wrapper_Id, E_Procedure);
2582 end if;
2584 Set_Is_Primitive_Wrapper (Wrapper_Id);
2585 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2586 Set_Is_Private_Primitive (Wrapper_Id,
2587 Is_Private_Primitive_Subprogram (Subp_Id));
2589 -- Process the formals
2591 New_Formals := Replicate_Formals (Loc, Formals);
2593 -- A function with a controlling result and no first controlling
2594 -- formal needs no additional parameter.
2596 if Has_Controlling_Result (Subp_Id)
2597 and then
2598 (No (First_Formal (Subp_Id))
2599 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2600 then
2601 null;
2603 -- Routine Subp_Id has been found to override an interface primitive.
2604 -- If the interface operation has an access parameter, create a copy
2605 -- of it, with the same null exclusion indicator if present.
2607 elsif Present (First_Param) then
2608 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2609 Obj_Param_Typ :=
2610 Make_Access_Definition (Loc,
2611 Subtype_Mark =>
2612 New_Reference_To (Obj_Typ, Loc));
2613 Set_Null_Exclusion_Present (Obj_Param_Typ,
2614 Null_Exclusion_Present (Parameter_Type (First_Param)));
2616 else
2617 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2618 end if;
2620 Obj_Param :=
2621 Make_Parameter_Specification (Loc,
2622 Defining_Identifier =>
2623 Make_Defining_Identifier (Loc,
2624 Chars => Name_uO),
2625 In_Present => In_Present (First_Param),
2626 Out_Present => Out_Present (First_Param),
2627 Parameter_Type => Obj_Param_Typ);
2629 Prepend_To (New_Formals, Obj_Param);
2631 -- If we are dealing with a primitive declared between two views,
2632 -- implemented by a synchronized operation, we need to create
2633 -- a default parameter. The mode of the parameter must match that
2634 -- of the primitive operation.
2636 else
2637 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2638 Obj_Param :=
2639 Make_Parameter_Specification (Loc,
2640 Defining_Identifier =>
2641 Make_Defining_Identifier (Loc, Name_uO),
2642 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2643 Out_Present => Ekind (Subp_Id) /= E_Function,
2644 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2645 Prepend_To (New_Formals, Obj_Param);
2646 end if;
2648 -- Build the final spec. If it is a function with a controlling
2649 -- result, it is a primitive operation of the corresponding
2650 -- record type, so mark the spec accordingly.
2652 if Ekind (Subp_Id) = E_Function then
2653 declare
2654 Res_Def : Node_Id;
2656 begin
2657 if Has_Controlling_Result (Subp_Id) then
2658 Res_Def :=
2659 New_Occurrence_Of
2660 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2661 else
2662 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2663 end if;
2665 return
2666 Make_Function_Specification (Loc,
2667 Defining_Unit_Name => Wrapper_Id,
2668 Parameter_Specifications => New_Formals,
2669 Result_Definition => Res_Def);
2670 end;
2671 else
2672 return
2673 Make_Procedure_Specification (Loc,
2674 Defining_Unit_Name => Wrapper_Id,
2675 Parameter_Specifications => New_Formals);
2676 end if;
2677 end;
2678 end Build_Wrapper_Spec;
2680 -------------------------
2681 -- Build_Wrapper_Specs --
2682 -------------------------
2684 procedure Build_Wrapper_Specs
2685 (Loc : Source_Ptr;
2686 Typ : Entity_Id;
2687 N : in out Node_Id)
2689 Def : Node_Id;
2690 Rec_Typ : Entity_Id;
2691 procedure Scan_Declarations (L : List_Id);
2692 -- Common processing for visible and private declarations
2693 -- of a protected type.
2695 procedure Scan_Declarations (L : List_Id) is
2696 Decl : Node_Id;
2697 Wrap_Decl : Node_Id;
2698 Wrap_Spec : Node_Id;
2700 begin
2701 if No (L) then
2702 return;
2703 end if;
2705 Decl := First (L);
2706 while Present (Decl) loop
2707 Wrap_Spec := Empty;
2709 if Nkind (Decl) = N_Entry_Declaration
2710 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2711 then
2712 Wrap_Spec :=
2713 Build_Wrapper_Spec
2714 (Subp_Id => Defining_Identifier (Decl),
2715 Obj_Typ => Rec_Typ,
2716 Formals => Parameter_Specifications (Decl));
2718 elsif Nkind (Decl) = N_Subprogram_Declaration then
2719 Wrap_Spec :=
2720 Build_Wrapper_Spec
2721 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2722 Obj_Typ => Rec_Typ,
2723 Formals =>
2724 Parameter_Specifications (Specification (Decl)));
2725 end if;
2727 if Present (Wrap_Spec) then
2728 Wrap_Decl :=
2729 Make_Subprogram_Declaration (Loc,
2730 Specification => Wrap_Spec);
2732 Insert_After (N, Wrap_Decl);
2733 N := Wrap_Decl;
2735 Analyze (Wrap_Decl);
2736 end if;
2738 Next (Decl);
2739 end loop;
2740 end Scan_Declarations;
2742 -- start of processing for Build_Wrapper_Specs
2744 begin
2745 if Is_Protected_Type (Typ) then
2746 Def := Protected_Definition (Parent (Typ));
2747 else pragma Assert (Is_Task_Type (Typ));
2748 Def := Task_Definition (Parent (Typ));
2749 end if;
2751 Rec_Typ := Corresponding_Record_Type (Typ);
2753 -- Generate wrapper specs for a concurrent type which implements an
2754 -- interface. Operations in both the visible and private parts may
2755 -- implement progenitor operations.
2757 if Present (Interfaces (Rec_Typ))
2758 and then Present (Def)
2759 then
2760 Scan_Declarations (Visible_Declarations (Def));
2761 Scan_Declarations (Private_Declarations (Def));
2762 end if;
2763 end Build_Wrapper_Specs;
2765 ---------------------------
2766 -- Build_Find_Body_Index --
2767 ---------------------------
2769 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2770 Loc : constant Source_Ptr := Sloc (Typ);
2771 Ent : Entity_Id;
2772 E_Typ : Entity_Id;
2773 Has_F : Boolean := False;
2774 Index : Nat;
2775 If_St : Node_Id := Empty;
2776 Lo : Node_Id;
2777 Hi : Node_Id;
2778 Decls : List_Id := New_List;
2779 Ret : Node_Id;
2780 Spec : Node_Id;
2781 Siz : Node_Id := Empty;
2783 procedure Add_If_Clause (Expr : Node_Id);
2784 -- Add test for range of current entry
2786 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2787 -- If a bound of an entry is given by a discriminant, retrieve the
2788 -- actual value of the discriminant from the enclosing object.
2790 -------------------
2791 -- Add_If_Clause --
2792 -------------------
2794 procedure Add_If_Clause (Expr : Node_Id) is
2795 Cond : Node_Id;
2796 Stats : constant List_Id :=
2797 New_List (
2798 Make_Simple_Return_Statement (Loc,
2799 Expression => Make_Integer_Literal (Loc, Index + 1)));
2801 begin
2802 -- Index for current entry body
2804 Index := Index + 1;
2806 -- Compute total length of entry queues so far
2808 if No (Siz) then
2809 Siz := Expr;
2810 else
2811 Siz :=
2812 Make_Op_Add (Loc,
2813 Left_Opnd => Siz,
2814 Right_Opnd => Expr);
2815 end if;
2817 Cond :=
2818 Make_Op_Le (Loc,
2819 Left_Opnd => Make_Identifier (Loc, Name_uE),
2820 Right_Opnd => Siz);
2822 -- Map entry queue indexes in the range of the current family
2823 -- into the current index, that designates the entry body.
2825 if No (If_St) then
2826 If_St :=
2827 Make_Implicit_If_Statement (Typ,
2828 Condition => Cond,
2829 Then_Statements => Stats,
2830 Elsif_Parts => New_List);
2831 Ret := If_St;
2833 else
2834 Append_To (Elsif_Parts (If_St),
2835 Make_Elsif_Part (Loc,
2836 Condition => Cond,
2837 Then_Statements => Stats));
2838 end if;
2839 end Add_If_Clause;
2841 ------------------------------
2842 -- Convert_Discriminant_Ref --
2843 ------------------------------
2845 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2846 B : Node_Id;
2848 begin
2849 if Is_Entity_Name (Bound)
2850 and then Ekind (Entity (Bound)) = E_Discriminant
2851 then
2852 B :=
2853 Make_Selected_Component (Loc,
2854 Prefix =>
2855 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2856 Make_Explicit_Dereference (Loc,
2857 Make_Identifier (Loc, Name_uObject))),
2858 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2859 Set_Etype (B, Etype (Entity (Bound)));
2860 else
2861 B := New_Copy_Tree (Bound);
2862 end if;
2864 return B;
2865 end Convert_Discriminant_Ref;
2867 -- Start of processing for Build_Find_Body_Index
2869 begin
2870 Spec := Build_Find_Body_Index_Spec (Typ);
2872 Ent := First_Entity (Typ);
2873 while Present (Ent) loop
2874 if Ekind (Ent) = E_Entry_Family then
2875 Has_F := True;
2876 exit;
2877 end if;
2879 Next_Entity (Ent);
2880 end loop;
2882 if not Has_F then
2884 -- If the protected type has no entry families, there is a one-one
2885 -- correspondence between entry queue and entry body.
2887 Ret :=
2888 Make_Simple_Return_Statement (Loc,
2889 Expression => Make_Identifier (Loc, Name_uE));
2891 else
2892 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2893 -- the following:
2895 -- if E <= l1 then return 1;
2896 -- elsif E <= l1 + l2 then return 2;
2897 -- ...
2899 Index := 0;
2900 Siz := Empty;
2901 Ent := First_Entity (Typ);
2903 Add_Object_Pointer (Loc, Typ, Decls);
2905 while Present (Ent) loop
2906 if Ekind (Ent) = E_Entry then
2907 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2909 elsif Ekind (Ent) = E_Entry_Family then
2910 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2911 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2912 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2913 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2914 end if;
2916 Next_Entity (Ent);
2917 end loop;
2919 if Index = 1 then
2920 Decls := New_List;
2921 Ret :=
2922 Make_Simple_Return_Statement (Loc,
2923 Expression => Make_Integer_Literal (Loc, 1));
2925 elsif Nkind (Ret) = N_If_Statement then
2927 -- Ranges are in increasing order, so last one doesn't need guard
2929 declare
2930 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2931 begin
2932 Remove (Nod);
2933 Set_Else_Statements (Ret, Then_Statements (Nod));
2934 end;
2935 end if;
2936 end if;
2938 return
2939 Make_Subprogram_Body (Loc,
2940 Specification => Spec,
2941 Declarations => Decls,
2942 Handled_Statement_Sequence =>
2943 Make_Handled_Sequence_Of_Statements (Loc,
2944 Statements => New_List (Ret)));
2945 end Build_Find_Body_Index;
2947 --------------------------------
2948 -- Build_Find_Body_Index_Spec --
2949 --------------------------------
2951 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2952 Loc : constant Source_Ptr := Sloc (Typ);
2953 Id : constant Entity_Id :=
2954 Make_Defining_Identifier (Loc,
2955 Chars => New_External_Name (Chars (Typ), 'F'));
2956 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2957 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2959 begin
2960 return
2961 Make_Function_Specification (Loc,
2962 Defining_Unit_Name => Id,
2963 Parameter_Specifications => New_List (
2964 Make_Parameter_Specification (Loc,
2965 Defining_Identifier => Parm1,
2966 Parameter_Type =>
2967 New_Reference_To (RTE (RE_Address), Loc)),
2969 Make_Parameter_Specification (Loc,
2970 Defining_Identifier => Parm2,
2971 Parameter_Type =>
2972 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
2974 Result_Definition => New_Occurrence_Of (
2975 RTE (RE_Protected_Entry_Index), Loc));
2976 end Build_Find_Body_Index_Spec;
2978 -----------------------------------------------
2979 -- Build_Lock_Free_Protected_Subprogram_Body --
2980 -----------------------------------------------
2982 function Build_Lock_Free_Protected_Subprogram_Body
2983 (N : Node_Id;
2984 Prot_Typ : Node_Id;
2985 Unprot_Spec : Node_Id) return Node_Id
2987 Actuals : constant List_Id := New_List;
2988 Loc : constant Source_Ptr := Sloc (N);
2989 Spec : constant Node_Id := Specification (N);
2990 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2991 Formal : Node_Id;
2992 Prot_Spec : Node_Id;
2993 Stmt : Node_Id;
2995 begin
2996 -- Create the protected version of the body
2998 Prot_Spec :=
2999 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3001 -- Build the actual parameters which appear in the call to the
3002 -- unprotected version of the body.
3004 Formal := First (Parameter_Specifications (Prot_Spec));
3005 while Present (Formal) loop
3006 Append_To (Actuals,
3007 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3009 Next (Formal);
3010 end loop;
3012 -- Function case, generate:
3013 -- return <Unprot_Func_Call>;
3015 if Nkind (Spec) = N_Function_Specification then
3016 Stmt :=
3017 Make_Simple_Return_Statement (Loc,
3018 Expression =>
3019 Make_Function_Call (Loc,
3020 Name =>
3021 Make_Identifier (Loc, Chars (Unprot_Id)),
3022 Parameter_Associations => Actuals));
3024 -- Procedure case, call the unprotected version
3026 else
3027 Stmt :=
3028 Make_Procedure_Call_Statement (Loc,
3029 Name =>
3030 Make_Identifier (Loc, Chars (Unprot_Id)),
3031 Parameter_Associations => Actuals);
3032 end if;
3034 return
3035 Make_Subprogram_Body (Loc,
3036 Declarations => Empty_List,
3037 Specification => Prot_Spec,
3038 Handled_Statement_Sequence =>
3039 Make_Handled_Sequence_Of_Statements (Loc,
3040 Statements => New_List (Stmt)));
3041 end Build_Lock_Free_Protected_Subprogram_Body;
3043 -------------------------------------------------
3044 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3045 -------------------------------------------------
3047 -- Procedures which meet the lock-free implementation requirements and
3048 -- reference a unique scalar component Comp are expanded in the following
3049 -- manner:
3051 -- procedure P (...) is
3052 -- Expected_Comp : constant Comp_Type :=
3053 -- Comp_Type
3054 -- (System.Atomic_Primitives.Lock_Free_Read_N
3055 -- (_Object.Comp'Address));
3056 -- begin
3057 -- loop
3058 -- declare
3059 -- <original declarations before the object renaming declaration
3060 -- of Comp>
3062 -- Desired_Comp : Comp_Type := Expected_Comp;
3063 -- Comp : Comp_Type renames Desired_Comp;
3065 -- <original delarations after the object renaming declaration
3066 -- of Comp>
3068 -- begin
3069 -- <original statements>
3070 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3071 -- (_Object.Comp'Address,
3072 -- Interfaces.Unsigned_N (Expected_Comp),
3073 -- Interfaces.Unsigned_N (Desired_Comp));
3074 -- end;
3075 -- end loop;
3076 -- end P;
3078 -- Each return and raise statement of P is transformed into an atomic
3079 -- status check:
3081 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3082 -- (_Object.Comp'Address,
3083 -- Interfaces.Unsigned_N (Expected_Comp),
3084 -- Interfaces.Unsigned_N (Desired_Comp));
3085 -- then
3086 -- <original statement>
3087 -- else
3088 -- goto L0;
3089 -- end if;
3091 -- Functions which meet the lock-free implementation requirements and
3092 -- reference a unique scalar component Comp are expanded in the following
3093 -- manner:
3095 -- function F (...) return ... is
3096 -- <original declarations before the object renaming declaration
3097 -- of Comp>
3099 -- Expected_Comp : constant Comp_Type :=
3100 -- Comp_Type
3101 -- (System.Atomic_Primitives.Lock_Free_Read_N
3102 -- (_Object.Comp'Address));
3103 -- Comp : Comp_Type renames Expected_Comp;
3105 -- <original delarations after the object renaming declaration of
3106 -- Comp>
3108 -- begin
3109 -- <original statements>
3110 -- end F;
3112 function Build_Lock_Free_Unprotected_Subprogram_Body
3113 (N : Node_Id;
3114 Prot_Typ : Node_Id) return Node_Id
3116 function Referenced_Component (N : Node_Id) return Entity_Id;
3117 -- Subprograms which meet the lock-free implementation criteria are
3118 -- allowed to reference only one unique component. Return the prival
3119 -- of the said component.
3121 --------------------------
3122 -- Referenced_Component --
3123 --------------------------
3125 function Referenced_Component (N : Node_Id) return Entity_Id is
3126 Comp : Entity_Id;
3127 Decl : Node_Id;
3128 Source_Comp : Entity_Id := Empty;
3130 begin
3131 -- Find the unique source component which N references in its
3132 -- statements.
3134 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3135 declare
3136 Element : Lock_Free_Subprogram renames
3137 Lock_Free_Subprogram_Table.Table (Index);
3138 begin
3139 if Element.Sub_Body = N then
3140 Source_Comp := Element.Comp_Id;
3141 exit;
3142 end if;
3143 end;
3144 end loop;
3146 if No (Source_Comp) then
3147 return Empty;
3148 end if;
3150 -- Find the prival which corresponds to the source component within
3151 -- the declarations of N.
3153 Decl := First (Declarations (N));
3154 while Present (Decl) loop
3156 -- Privals appear as object renamings
3158 if Nkind (Decl) = N_Object_Renaming_Declaration then
3159 Comp := Defining_Identifier (Decl);
3161 if Present (Prival_Link (Comp))
3162 and then Prival_Link (Comp) = Source_Comp
3163 then
3164 return Comp;
3165 end if;
3166 end if;
3168 Next (Decl);
3169 end loop;
3171 return Empty;
3172 end Referenced_Component;
3174 -- Local variables
3176 Comp : constant Entity_Id := Referenced_Component (N);
3177 Loc : constant Source_Ptr := Sloc (N);
3178 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3179 Decls : List_Id := Declarations (N);
3181 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3183 begin
3184 -- Add renamings for the protection object, discriminals, privals and
3185 -- the entry index constant for use by debugger.
3187 Debug_Private_Data_Declarations (Decls);
3189 -- Perform the lock-free expansion when the subprogram references a
3190 -- protected component.
3192 if Present (Comp) then
3193 Protected_Component_Ref : declare
3194 Comp_Decl : constant Node_Id := Parent (Comp);
3195 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3196 Comp_Type : constant Entity_Id := Etype (Comp);
3198 Is_Procedure : constant Boolean :=
3199 Ekind (Corresponding_Spec (N)) = E_Procedure;
3200 -- Indicates if N is a protected procedure body
3202 Block_Decls : List_Id;
3203 Try_Write : Entity_Id;
3204 Desired_Comp : Entity_Id;
3205 Decl : Node_Id;
3206 Label : Node_Id;
3207 Label_Id : Entity_Id := Empty;
3208 Read : Entity_Id;
3209 Expected_Comp : Entity_Id;
3210 Stmt : Node_Id;
3211 Stmts : List_Id :=
3212 New_Copy_List (Statements (Hand_Stmt_Seq));
3213 Typ_Size : Int;
3214 Unsigned : Entity_Id;
3216 function Process_Node (N : Node_Id) return Traverse_Result;
3217 -- Transform a single node if it is a return statement, a raise
3218 -- statement or a reference to Comp.
3220 procedure Process_Stmts (Stmts : List_Id);
3221 -- Given a statement sequence Stmts, wrap any return or raise
3222 -- statements in the following manner:
3224 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3225 -- (_Object.Comp'Address,
3226 -- Interfaces.Unsigned_N (Expected_Comp),
3227 -- Interfaces.Unsigned_N (Desired_Comp))
3228 -- then
3229 -- <Stmt>;
3230 -- else
3231 -- goto L0;
3232 -- end if;
3234 ------------------
3235 -- Process_Node --
3236 ------------------
3238 function Process_Node (N : Node_Id) return Traverse_Result is
3240 procedure Wrap_Statement (Stmt : Node_Id);
3241 -- Wrap an arbitrary statement inside an if statement where the
3242 -- condition does an atomic check on the state of the object.
3244 --------------------
3245 -- Wrap_Statement --
3246 --------------------
3248 procedure Wrap_Statement (Stmt : Node_Id) is
3249 begin
3250 -- The first time through, create the declaration of a label
3251 -- which is used to skip the remainder of source statements
3252 -- if the state of the object has changed.
3254 if No (Label_Id) then
3255 Label_Id :=
3256 Make_Identifier (Loc, New_External_Name ('L', 0));
3257 Set_Entity (Label_Id,
3258 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3259 end if;
3261 -- Generate:
3262 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3263 -- (_Object.Comp'Address,
3264 -- Interfaces.Unsigned_N (Expected_Comp),
3265 -- Interfaces.Unsigned_N (Desired_Comp))
3266 -- then
3267 -- <Stmt>;
3268 -- else
3269 -- goto L0;
3270 -- end if;
3272 Rewrite (Stmt,
3273 Make_Implicit_If_Statement (N,
3274 Condition =>
3275 Make_Function_Call (Loc,
3276 Name =>
3277 New_Reference_To (Try_Write, Loc),
3278 Parameter_Associations => New_List (
3279 Make_Attribute_Reference (Loc,
3280 Prefix => Relocate_Node (Comp_Sel_Nam),
3281 Attribute_Name => Name_Address),
3283 Unchecked_Convert_To (Unsigned,
3284 New_Reference_To (Expected_Comp, Loc)),
3286 Unchecked_Convert_To (Unsigned,
3287 New_Reference_To (Desired_Comp, Loc)))),
3289 Then_Statements => New_List (Relocate_Node (Stmt)),
3291 Else_Statements => New_List (
3292 Make_Goto_Statement (Loc,
3293 Name =>
3294 New_Reference_To (Entity (Label_Id), Loc)))));
3295 end Wrap_Statement;
3297 -- Start of processing for Process_Node
3299 begin
3300 -- Wrap each return and raise statement that appear inside a
3301 -- procedure. Skip the last return statement which is added by
3302 -- default since it is transformed into an exit statement.
3304 if Is_Procedure
3305 and then ((Nkind (N) = N_Simple_Return_Statement
3306 and then N /= Last (Stmts))
3307 or else Nkind (N) = N_Extended_Return_Statement
3308 or else (Nkind_In (N, N_Raise_Constraint_Error,
3309 N_Raise_Program_Error,
3310 N_Raise_Statement,
3311 N_Raise_Storage_Error)
3312 and then Comes_From_Source (N)))
3313 then
3314 Wrap_Statement (N);
3315 return Skip;
3316 end if;
3318 -- Force reanalysis
3320 Set_Analyzed (N, False);
3322 return OK;
3323 end Process_Node;
3325 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3327 -------------------
3328 -- Process_Stmts --
3329 -------------------
3331 procedure Process_Stmts (Stmts : List_Id) is
3332 Stmt : Node_Id;
3333 begin
3334 Stmt := First (Stmts);
3335 while Present (Stmt) loop
3336 Process_Nodes (Stmt);
3337 Next (Stmt);
3338 end loop;
3339 end Process_Stmts;
3341 -- Start of processing for Protected_Component_Ref
3343 begin
3344 -- Get the type size
3346 if Known_Static_Esize (Comp_Type) then
3347 Typ_Size := UI_To_Int (Esize (Comp_Type));
3349 -- If the Esize (Object_Size) is unknown at compile-time, look at
3350 -- the RM_Size (Value_Size) since it may have been set by an
3351 -- explicit representation clause.
3353 elsif Known_Static_RM_Size (Comp_Type) then
3354 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3356 -- Should not happen since this has already been checked in
3357 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3359 else
3360 raise Program_Error;
3361 end if;
3363 -- Retrieve all relevant atomic routines and types
3365 case Typ_Size is
3366 when 8 =>
3367 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3368 Read := RTE (RE_Lock_Free_Read_8);
3369 Unsigned := RTE (RE_Uint8);
3371 when 16 =>
3372 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3373 Read := RTE (RE_Lock_Free_Read_16);
3374 Unsigned := RTE (RE_Uint16);
3376 when 32 =>
3377 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3378 Read := RTE (RE_Lock_Free_Read_32);
3379 Unsigned := RTE (RE_Uint32);
3381 when 64 =>
3382 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3383 Read := RTE (RE_Lock_Free_Read_64);
3384 Unsigned := RTE (RE_Uint64);
3386 when others =>
3387 raise Program_Error;
3388 end case;
3390 -- Generate:
3391 -- Expected_Comp : constant Comp_Type :=
3392 -- Comp_Type
3393 -- (System.Atomic_Primitives.Lock_Free_Read_N
3394 -- (_Object.Comp'Address));
3396 Expected_Comp :=
3397 Make_Defining_Identifier (Loc,
3398 New_External_Name (Chars (Comp), Suffix => "_saved"));
3400 Decl :=
3401 Make_Object_Declaration (Loc,
3402 Defining_Identifier => Expected_Comp,
3403 Object_Definition => New_Reference_To (Comp_Type, Loc),
3404 Constant_Present => True,
3405 Expression =>
3406 Unchecked_Convert_To (Comp_Type,
3407 Make_Function_Call (Loc,
3408 Name => New_Reference_To (Read, Loc),
3409 Parameter_Associations => New_List (
3410 Make_Attribute_Reference (Loc,
3411 Prefix => Relocate_Node (Comp_Sel_Nam),
3412 Attribute_Name => Name_Address)))));
3414 -- Protected procedures
3416 if Is_Procedure then
3417 -- Move the original declarations inside the generated block
3419 Block_Decls := Decls;
3421 -- Reset the declarations list of the protected procedure to
3422 -- contain only Decl.
3424 Decls := New_List (Decl);
3426 -- Generate:
3427 -- Desired_Comp : Comp_Type := Expected_Comp;
3429 Desired_Comp :=
3430 Make_Defining_Identifier (Loc,
3431 New_External_Name (Chars (Comp), Suffix => "_current"));
3433 -- Insert the declarations of Expected_Comp and Desired_Comp in
3434 -- the block declarations right before the renaming of the
3435 -- protected component.
3437 Insert_Before (Comp_Decl,
3438 Make_Object_Declaration (Loc,
3439 Defining_Identifier => Desired_Comp,
3440 Object_Definition => New_Reference_To (Comp_Type, Loc),
3441 Expression =>
3442 New_Reference_To (Expected_Comp, Loc)));
3444 -- Protected function
3446 else
3447 Desired_Comp := Expected_Comp;
3449 -- Insert the declaration of Expected_Comp in the function
3450 -- declarations right before the renaming of the protected
3451 -- component.
3453 Insert_Before (Comp_Decl, Decl);
3454 end if;
3456 -- Rewrite the protected component renaming declaration to be a
3457 -- renaming of Desired_Comp.
3459 -- Generate:
3460 -- Comp : Comp_Type renames Desired_Comp;
3462 Rewrite (Comp_Decl,
3463 Make_Object_Renaming_Declaration (Loc,
3464 Defining_Identifier =>
3465 Defining_Identifier (Comp_Decl),
3466 Subtype_Mark =>
3467 New_Occurrence_Of (Comp_Type, Loc),
3468 Name =>
3469 New_Reference_To (Desired_Comp, Loc)));
3471 -- Wrap any return or raise statements in Stmts in same the manner
3472 -- described in Process_Stmts.
3474 Process_Stmts (Stmts);
3476 -- Generate:
3477 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3478 -- (_Object.Comp'Address,
3479 -- Interfaces.Unsigned_N (Expected_Comp),
3480 -- Interfaces.Unsigned_N (Desired_Comp))
3482 if Is_Procedure then
3483 Stmt :=
3484 Make_Exit_Statement (Loc,
3485 Condition =>
3486 Make_Function_Call (Loc,
3487 Name =>
3488 New_Reference_To (Try_Write, Loc),
3489 Parameter_Associations => New_List (
3490 Make_Attribute_Reference (Loc,
3491 Prefix => Relocate_Node (Comp_Sel_Nam),
3492 Attribute_Name => Name_Address),
3494 Unchecked_Convert_To (Unsigned,
3495 New_Reference_To (Expected_Comp, Loc)),
3497 Unchecked_Convert_To (Unsigned,
3498 New_Reference_To (Desired_Comp, Loc)))));
3500 -- Small optimization: transform the default return statement
3501 -- of a procedure into the atomic exit statement.
3503 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3504 Rewrite (Last (Stmts), Stmt);
3505 else
3506 Append_To (Stmts, Stmt);
3507 end if;
3508 end if;
3510 -- Create the declaration of the label used to skip the rest of
3511 -- the source statements when the object state changes.
3513 if Present (Label_Id) then
3514 Label := Make_Label (Loc, Label_Id);
3515 Append_To (Decls,
3516 Make_Implicit_Label_Declaration (Loc,
3517 Defining_Identifier => Entity (Label_Id),
3518 Label_Construct => Label));
3519 Append_To (Stmts, Label);
3520 end if;
3522 -- Generate:
3523 -- loop
3524 -- declare
3525 -- <Decls>
3526 -- begin
3527 -- <Stmts>
3528 -- end;
3529 -- end loop;
3531 if Is_Procedure then
3532 Stmts :=
3533 New_List (
3534 Make_Loop_Statement (Loc,
3535 Statements => New_List (
3536 Make_Block_Statement (Loc,
3537 Declarations => Block_Decls,
3538 Handled_Statement_Sequence =>
3539 Make_Handled_Sequence_Of_Statements (Loc,
3540 Statements => Stmts))),
3541 End_Label => Empty));
3542 end if;
3544 Hand_Stmt_Seq :=
3545 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3546 end Protected_Component_Ref;
3547 end if;
3549 -- Make an unprotected version of the subprogram for use within the same
3550 -- object, with new name and extra parameter representing the object.
3552 return
3553 Make_Subprogram_Body (Loc,
3554 Specification =>
3555 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3556 Declarations => Decls,
3557 Handled_Statement_Sequence => Hand_Stmt_Seq);
3558 end Build_Lock_Free_Unprotected_Subprogram_Body;
3560 -------------------------
3561 -- Build_Master_Entity --
3562 -------------------------
3564 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3565 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3566 Context : Node_Id;
3567 Context_Id : Entity_Id;
3568 Decl : Node_Id;
3569 Decls : List_Id;
3570 Par : Node_Id;
3572 begin
3573 if Is_Itype (Obj_Or_Typ) then
3574 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3575 else
3576 Par := Parent (Obj_Or_Typ);
3577 end if;
3579 -- When creating a master for a record component which is either a task
3580 -- or access-to-task, the enclosing record is the master scope and the
3581 -- proper insertion point is the component list.
3583 if Is_Record_Type (Current_Scope) then
3584 Context := Par;
3585 Context_Id := Current_Scope;
3586 Decls := List_Containing (Context);
3588 -- Default case for object declarations and access types. Note that the
3589 -- context is updated to the nearest enclosing body, block, package or
3590 -- return statement.
3592 else
3593 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3594 end if;
3596 -- Do not create a master if one already exists or there is no task
3597 -- hierarchy.
3599 if Has_Master_Entity (Context_Id)
3600 or else Restriction_Active (No_Task_Hierarchy)
3601 then
3602 return;
3603 end if;
3605 -- Create a master, generate:
3606 -- _Master : constant Master_Id := Current_Master.all;
3608 Decl :=
3609 Make_Object_Declaration (Loc,
3610 Defining_Identifier =>
3611 Make_Defining_Identifier (Loc, Name_uMaster),
3612 Constant_Present => True,
3613 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
3614 Expression =>
3615 Make_Explicit_Dereference (Loc,
3616 New_Reference_To (RTE (RE_Current_Master), Loc)));
3618 -- The master is inserted at the start of the declarative list of the
3619 -- context.
3621 Prepend_To (Decls, Decl);
3623 -- In certain cases where transient scopes are involved, the immediate
3624 -- scope is not always the proper master scope. Ensure that the master
3625 -- declaration and entity appear in the same context.
3627 if Context_Id /= Current_Scope then
3628 Push_Scope (Context_Id);
3629 Analyze (Decl);
3630 Pop_Scope;
3631 else
3632 Analyze (Decl);
3633 end if;
3635 -- Mark the enclosing scope and its associated construct as being task
3636 -- masters.
3638 Set_Has_Master_Entity (Context_Id);
3640 while Present (Context)
3641 and then Nkind (Context) /= N_Compilation_Unit
3642 loop
3643 if Nkind_In (Context, N_Block_Statement,
3644 N_Subprogram_Body,
3645 N_Task_Body)
3646 then
3647 Set_Is_Task_Master (Context);
3648 exit;
3650 elsif Nkind (Parent (Context)) = N_Subunit then
3651 Context := Corresponding_Stub (Parent (Context));
3652 end if;
3654 Context := Parent (Context);
3655 end loop;
3656 end Build_Master_Entity;
3658 ---------------------------
3659 -- Build_Master_Renaming --
3660 ---------------------------
3662 procedure Build_Master_Renaming
3663 (Ptr_Typ : Entity_Id;
3664 Ins_Nod : Node_Id := Empty)
3666 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3667 Context : Node_Id;
3668 Master_Decl : Node_Id;
3669 Master_Id : Entity_Id;
3671 begin
3672 -- Nothing to do if there is no task hierarchy
3674 if Restriction_Active (No_Task_Hierarchy) then
3675 return;
3676 end if;
3678 -- Determine the proper context to insert the master renaming
3680 if Present (Ins_Nod) then
3681 Context := Ins_Nod;
3682 elsif Is_Itype (Ptr_Typ) then
3683 Context := Associated_Node_For_Itype (Ptr_Typ);
3684 else
3685 Context := Parent (Ptr_Typ);
3686 end if;
3688 -- Generate:
3689 -- <Ptr_Typ>M : Master_Id renames _Master;
3691 Master_Id :=
3692 Make_Defining_Identifier (Loc,
3693 New_External_Name (Chars (Ptr_Typ), 'M'));
3695 Master_Decl :=
3696 Make_Object_Renaming_Declaration (Loc,
3697 Defining_Identifier => Master_Id,
3698 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
3699 Name => Make_Identifier (Loc, Name_uMaster));
3701 Insert_Action (Context, Master_Decl);
3703 -- The renamed master now services the access type
3705 Set_Master_Id (Ptr_Typ, Master_Id);
3706 end Build_Master_Renaming;
3708 -----------------------------------------
3709 -- Build_Private_Protected_Declaration --
3710 -----------------------------------------
3712 function Build_Private_Protected_Declaration
3713 (N : Node_Id) return Entity_Id
3715 Loc : constant Source_Ptr := Sloc (N);
3716 Body_Id : constant Entity_Id := Defining_Entity (N);
3717 Decl : Node_Id;
3718 Plist : List_Id;
3719 Formal : Entity_Id;
3720 New_Spec : Node_Id;
3721 Spec_Id : Entity_Id;
3723 begin
3724 Formal := First_Formal (Body_Id);
3726 -- The protected operation always has at least one formal, namely the
3727 -- object itself, but it is only placed in the parameter list if
3728 -- expansion is enabled.
3730 if Present (Formal) or else Expander_Active then
3731 Plist := Copy_Parameter_List (Body_Id);
3732 else
3733 Plist := No_List;
3734 end if;
3736 if Nkind (Specification (N)) = N_Procedure_Specification then
3737 New_Spec :=
3738 Make_Procedure_Specification (Loc,
3739 Defining_Unit_Name =>
3740 Make_Defining_Identifier (Sloc (Body_Id),
3741 Chars => Chars (Body_Id)),
3742 Parameter_Specifications =>
3743 Plist);
3744 else
3745 New_Spec :=
3746 Make_Function_Specification (Loc,
3747 Defining_Unit_Name =>
3748 Make_Defining_Identifier (Sloc (Body_Id),
3749 Chars => Chars (Body_Id)),
3750 Parameter_Specifications => Plist,
3751 Result_Definition =>
3752 New_Occurrence_Of (Etype (Body_Id), Loc));
3753 end if;
3755 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3756 Insert_Before (N, Decl);
3757 Spec_Id := Defining_Unit_Name (New_Spec);
3759 -- Indicate that the entity comes from source, to ensure that cross-
3760 -- reference information is properly generated. The body itself is
3761 -- rewritten during expansion, and the body entity will not appear in
3762 -- calls to the operation.
3764 Set_Comes_From_Source (Spec_Id, True);
3765 Analyze (Decl);
3766 Set_Has_Completion (Spec_Id);
3767 Set_Convention (Spec_Id, Convention_Protected);
3768 return Spec_Id;
3769 end Build_Private_Protected_Declaration;
3771 ---------------------------
3772 -- Build_Protected_Entry --
3773 ---------------------------
3775 function Build_Protected_Entry
3776 (N : Node_Id;
3777 Ent : Entity_Id;
3778 Pid : Node_Id) return Node_Id
3780 Loc : constant Source_Ptr := Sloc (N);
3782 Decls : constant List_Id := Declarations (N);
3783 End_Lab : constant Node_Id :=
3784 End_Label (Handled_Statement_Sequence (N));
3785 End_Loc : constant Source_Ptr :=
3786 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3787 -- Used for the generated call to Complete_Entry_Body
3789 Han_Loc : Source_Ptr;
3790 -- Used for the exception handler, inserted at end of the body
3792 Op_Decls : constant List_Id := New_List;
3793 Complete : Node_Id;
3794 Edef : Entity_Id;
3795 Espec : Node_Id;
3796 Ohandle : Node_Id;
3797 Op_Stats : List_Id;
3799 begin
3800 -- Set the source location on the exception handler only when debugging
3801 -- the expanded code (see Make_Implicit_Exception_Handler).
3803 if Debug_Generated_Code then
3804 Han_Loc := End_Loc;
3806 -- Otherwise the inserted code should not be visible to the debugger
3808 else
3809 Han_Loc := No_Location;
3810 end if;
3812 Edef :=
3813 Make_Defining_Identifier (Loc,
3814 Chars => Chars (Protected_Body_Subprogram (Ent)));
3815 Espec :=
3816 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3818 -- Add the following declarations:
3819 -- type poVP is access poV;
3820 -- _object : poVP := poVP (_O);
3822 -- where _O is the formal parameter associated with the concurrent
3823 -- object. These declarations are needed for Complete_Entry_Body.
3825 Add_Object_Pointer (Loc, Pid, Op_Decls);
3827 -- Add renamings for all formals, the Protection object, discriminals,
3828 -- privals and the entry index constant for use by debugger.
3830 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3831 Debug_Private_Data_Declarations (Decls);
3833 case Corresponding_Runtime_Package (Pid) is
3834 when System_Tasking_Protected_Objects_Entries =>
3835 Complete :=
3836 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
3838 when System_Tasking_Protected_Objects_Single_Entry =>
3839 Complete :=
3840 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
3842 when others =>
3843 raise Program_Error;
3844 end case;
3846 Op_Stats := New_List (
3847 Make_Block_Statement (Loc,
3848 Declarations => Decls,
3849 Handled_Statement_Sequence =>
3850 Handled_Statement_Sequence (N)),
3852 Make_Procedure_Call_Statement (End_Loc,
3853 Name => Complete,
3854 Parameter_Associations => New_List (
3855 Make_Attribute_Reference (End_Loc,
3856 Prefix =>
3857 Make_Selected_Component (End_Loc,
3858 Prefix => Make_Identifier (End_Loc, Name_uObject),
3859 Selector_Name => Make_Identifier (End_Loc, Name_uObject)),
3860 Attribute_Name => Name_Unchecked_Access))));
3862 -- When exceptions can not be propagated, we never need to call
3863 -- Exception_Complete_Entry_Body
3865 if No_Exception_Handlers_Set then
3866 return
3867 Make_Subprogram_Body (Loc,
3868 Specification => Espec,
3869 Declarations => Op_Decls,
3870 Handled_Statement_Sequence =>
3871 Make_Handled_Sequence_Of_Statements (Loc,
3872 Statements => Op_Stats,
3873 End_Label => End_Lab));
3875 else
3876 Ohandle := Make_Others_Choice (Loc);
3877 Set_All_Others (Ohandle);
3879 case Corresponding_Runtime_Package (Pid) is
3880 when System_Tasking_Protected_Objects_Entries =>
3881 Complete :=
3882 New_Reference_To
3883 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3885 when System_Tasking_Protected_Objects_Single_Entry =>
3886 Complete :=
3887 New_Reference_To
3888 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3890 when others =>
3891 raise Program_Error;
3892 end case;
3894 -- Establish link between subprogram body entity and source entry
3896 Set_Corresponding_Protected_Entry (Edef, Ent);
3898 -- Create body of entry procedure. The renaming declarations are
3899 -- placed ahead of the block that contains the actual entry body.
3901 return
3902 Make_Subprogram_Body (Loc,
3903 Specification => Espec,
3904 Declarations => Op_Decls,
3905 Handled_Statement_Sequence =>
3906 Make_Handled_Sequence_Of_Statements (Loc,
3907 Statements => Op_Stats,
3908 End_Label => End_Lab,
3909 Exception_Handlers => New_List (
3910 Make_Implicit_Exception_Handler (Han_Loc,
3911 Exception_Choices => New_List (Ohandle),
3913 Statements => New_List (
3914 Make_Procedure_Call_Statement (Han_Loc,
3915 Name => Complete,
3916 Parameter_Associations => New_List (
3917 Make_Attribute_Reference (Han_Loc,
3918 Prefix =>
3919 Make_Selected_Component (Han_Loc,
3920 Prefix =>
3921 Make_Identifier (Han_Loc, Name_uObject),
3922 Selector_Name =>
3923 Make_Identifier (Han_Loc, Name_uObject)),
3924 Attribute_Name => Name_Unchecked_Access),
3926 Make_Function_Call (Han_Loc,
3927 Name => New_Reference_To (
3928 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3929 end if;
3930 end Build_Protected_Entry;
3932 -----------------------------------------
3933 -- Build_Protected_Entry_Specification --
3934 -----------------------------------------
3936 function Build_Protected_Entry_Specification
3937 (Loc : Source_Ptr;
3938 Def_Id : Entity_Id;
3939 Ent_Id : Entity_Id) return Node_Id
3941 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3943 begin
3944 Set_Debug_Info_Needed (Def_Id);
3946 if Present (Ent_Id) then
3947 Append_Elmt (P, Accept_Address (Ent_Id));
3948 end if;
3950 return
3951 Make_Procedure_Specification (Loc,
3952 Defining_Unit_Name => Def_Id,
3953 Parameter_Specifications => New_List (
3954 Make_Parameter_Specification (Loc,
3955 Defining_Identifier =>
3956 Make_Defining_Identifier (Loc, Name_uO),
3957 Parameter_Type =>
3958 New_Reference_To (RTE (RE_Address), Loc)),
3960 Make_Parameter_Specification (Loc,
3961 Defining_Identifier => P,
3962 Parameter_Type =>
3963 New_Reference_To (RTE (RE_Address), Loc)),
3965 Make_Parameter_Specification (Loc,
3966 Defining_Identifier =>
3967 Make_Defining_Identifier (Loc, Name_uE),
3968 Parameter_Type =>
3969 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
3970 end Build_Protected_Entry_Specification;
3972 --------------------------
3973 -- Build_Protected_Spec --
3974 --------------------------
3976 function Build_Protected_Spec
3977 (N : Node_Id;
3978 Obj_Type : Entity_Id;
3979 Ident : Entity_Id;
3980 Unprotected : Boolean := False) return List_Id
3982 Loc : constant Source_Ptr := Sloc (N);
3983 Decl : Node_Id;
3984 Formal : Entity_Id;
3985 New_Plist : List_Id;
3986 New_Param : Node_Id;
3988 begin
3989 New_Plist := New_List;
3991 Formal := First_Formal (Ident);
3992 while Present (Formal) loop
3993 New_Param :=
3994 Make_Parameter_Specification (Loc,
3995 Defining_Identifier =>
3996 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3997 In_Present => In_Present (Parent (Formal)),
3998 Out_Present => Out_Present (Parent (Formal)),
3999 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
4001 if Unprotected then
4002 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4003 end if;
4005 Append (New_Param, New_Plist);
4006 Next_Formal (Formal);
4007 end loop;
4009 -- If the subprogram is a procedure and the context is not an access
4010 -- to protected subprogram, the parameter is in-out. Otherwise it is
4011 -- an in parameter.
4013 Decl :=
4014 Make_Parameter_Specification (Loc,
4015 Defining_Identifier =>
4016 Make_Defining_Identifier (Loc, Name_uObject),
4017 In_Present => True,
4018 Out_Present =>
4019 (Etype (Ident) = Standard_Void_Type
4020 and then not Is_RTE (Obj_Type, RE_Address)),
4021 Parameter_Type =>
4022 New_Reference_To (Obj_Type, Loc));
4023 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4024 Prepend_To (New_Plist, Decl);
4026 return New_Plist;
4027 end Build_Protected_Spec;
4029 ---------------------------------------
4030 -- Build_Protected_Sub_Specification --
4031 ---------------------------------------
4033 function Build_Protected_Sub_Specification
4034 (N : Node_Id;
4035 Prot_Typ : Entity_Id;
4036 Mode : Subprogram_Protection_Mode) return Node_Id
4038 Loc : constant Source_Ptr := Sloc (N);
4039 Decl : Node_Id;
4040 Def_Id : Entity_Id;
4041 New_Id : Entity_Id;
4042 New_Plist : List_Id;
4043 New_Spec : Node_Id;
4045 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4046 (Dispatching_Mode => ' ',
4047 Protected_Mode => 'P',
4048 Unprotected_Mode => 'N');
4050 begin
4051 if Ekind (Defining_Unit_Name (Specification (N))) =
4052 E_Subprogram_Body
4053 then
4054 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4055 else
4056 Decl := N;
4057 end if;
4059 Def_Id := Defining_Unit_Name (Specification (Decl));
4061 New_Plist :=
4062 Build_Protected_Spec
4063 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4064 Mode = Unprotected_Mode);
4065 New_Id :=
4066 Make_Defining_Identifier (Loc,
4067 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4069 -- The unprotected operation carries the user code, and debugging
4070 -- information must be generated for it, even though this spec does
4071 -- not come from source. It is also convenient to allow gdb to step
4072 -- into the protected operation, even though it only contains lock/
4073 -- unlock calls.
4075 Set_Debug_Info_Needed (New_Id);
4077 -- If a pragma Eliminate applies to the source entity, the internal
4078 -- subprograms will be eliminated as well.
4080 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4082 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4083 New_Spec :=
4084 Make_Procedure_Specification (Loc,
4085 Defining_Unit_Name => New_Id,
4086 Parameter_Specifications => New_Plist);
4088 -- Create a new specification for the anonymous subprogram type
4090 else
4091 New_Spec :=
4092 Make_Function_Specification (Loc,
4093 Defining_Unit_Name => New_Id,
4094 Parameter_Specifications => New_Plist,
4095 Result_Definition =>
4096 Copy_Result_Type (Result_Definition (Specification (Decl))));
4098 Set_Return_Present (Defining_Unit_Name (New_Spec));
4099 end if;
4101 return New_Spec;
4102 end Build_Protected_Sub_Specification;
4104 -------------------------------------
4105 -- Build_Protected_Subprogram_Body --
4106 -------------------------------------
4108 function Build_Protected_Subprogram_Body
4109 (N : Node_Id;
4110 Pid : Node_Id;
4111 N_Op_Spec : Node_Id) return Node_Id
4113 Loc : constant Source_Ptr := Sloc (N);
4114 Op_Spec : Node_Id;
4115 P_Op_Spec : Node_Id;
4116 Uactuals : List_Id;
4117 Pformal : Node_Id;
4118 Unprot_Call : Node_Id;
4119 Sub_Body : Node_Id;
4120 Lock_Name : Node_Id;
4121 Lock_Stmt : Node_Id;
4122 Service_Name : Node_Id;
4123 R : Node_Id;
4124 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4125 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4126 Stmts : List_Id;
4127 Object_Parm : Node_Id;
4128 Exc_Safe : Boolean;
4129 Lock_Kind : RE_Id;
4131 begin
4132 Op_Spec := Specification (N);
4133 Exc_Safe := Is_Exception_Safe (N);
4135 P_Op_Spec :=
4136 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4138 -- Build a list of the formal parameters of the protected version of
4139 -- the subprogram to use as the actual parameters of the unprotected
4140 -- version.
4142 Uactuals := New_List;
4143 Pformal := First (Parameter_Specifications (P_Op_Spec));
4144 while Present (Pformal) loop
4145 Append_To (Uactuals,
4146 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4147 Next (Pformal);
4148 end loop;
4150 -- Make a call to the unprotected version of the subprogram built above
4151 -- for use by the protected version built below.
4153 if Nkind (Op_Spec) = N_Function_Specification then
4154 if Exc_Safe then
4155 R := Make_Temporary (Loc, 'R');
4156 Unprot_Call :=
4157 Make_Object_Declaration (Loc,
4158 Defining_Identifier => R,
4159 Constant_Present => True,
4160 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4161 Expression =>
4162 Make_Function_Call (Loc,
4163 Name => Make_Identifier (Loc,
4164 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4165 Parameter_Associations => Uactuals));
4167 Return_Stmt :=
4168 Make_Simple_Return_Statement (Loc,
4169 Expression => New_Reference_To (R, Loc));
4171 else
4172 Unprot_Call := Make_Simple_Return_Statement (Loc,
4173 Expression => Make_Function_Call (Loc,
4174 Name =>
4175 Make_Identifier (Loc,
4176 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4177 Parameter_Associations => Uactuals));
4178 end if;
4180 Lock_Kind := RE_Lock_Read_Only;
4182 else
4183 Unprot_Call :=
4184 Make_Procedure_Call_Statement (Loc,
4185 Name =>
4186 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4187 Parameter_Associations => Uactuals);
4189 Lock_Kind := RE_Lock;
4190 end if;
4192 -- Wrap call in block that will be covered by an at_end handler
4194 if not Exc_Safe then
4195 Unprot_Call := Make_Block_Statement (Loc,
4196 Handled_Statement_Sequence =>
4197 Make_Handled_Sequence_Of_Statements (Loc,
4198 Statements => New_List (Unprot_Call)));
4199 end if;
4201 -- Make the protected subprogram body. This locks the protected
4202 -- object and calls the unprotected version of the subprogram.
4204 case Corresponding_Runtime_Package (Pid) is
4205 when System_Tasking_Protected_Objects_Entries =>
4206 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
4207 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
4209 when System_Tasking_Protected_Objects_Single_Entry =>
4210 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
4211 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
4213 when System_Tasking_Protected_Objects =>
4214 Lock_Name := New_Reference_To (RTE (Lock_Kind), Loc);
4215 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
4217 when others =>
4218 raise Program_Error;
4219 end case;
4221 Object_Parm :=
4222 Make_Attribute_Reference (Loc,
4223 Prefix =>
4224 Make_Selected_Component (Loc,
4225 Prefix => Make_Identifier (Loc, Name_uObject),
4226 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4227 Attribute_Name => Name_Unchecked_Access);
4229 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4230 Name => Lock_Name,
4231 Parameter_Associations => New_List (Object_Parm));
4233 if Abort_Allowed then
4234 Stmts := New_List (
4235 Make_Procedure_Call_Statement (Loc,
4236 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4237 Parameter_Associations => Empty_List),
4238 Lock_Stmt);
4240 else
4241 Stmts := New_List (Lock_Stmt);
4242 end if;
4244 if not Exc_Safe then
4245 Append (Unprot_Call, Stmts);
4246 else
4247 if Nkind (Op_Spec) = N_Function_Specification then
4248 Pre_Stmts := Stmts;
4249 Stmts := Empty_List;
4250 else
4251 Append (Unprot_Call, Stmts);
4252 end if;
4254 Append (
4255 Make_Procedure_Call_Statement (Loc,
4256 Name => Service_Name,
4257 Parameter_Associations =>
4258 New_List (New_Copy_Tree (Object_Parm))),
4259 Stmts);
4261 if Abort_Allowed then
4262 Append (
4263 Make_Procedure_Call_Statement (Loc,
4264 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
4265 Parameter_Associations => Empty_List),
4266 Stmts);
4267 end if;
4269 if Nkind (Op_Spec) = N_Function_Specification then
4270 Append (Return_Stmt, Stmts);
4271 Append (Make_Block_Statement (Loc,
4272 Declarations => New_List (Unprot_Call),
4273 Handled_Statement_Sequence =>
4274 Make_Handled_Sequence_Of_Statements (Loc,
4275 Statements => Stmts)), Pre_Stmts);
4276 Stmts := Pre_Stmts;
4277 end if;
4278 end if;
4280 Sub_Body :=
4281 Make_Subprogram_Body (Loc,
4282 Declarations => Empty_List,
4283 Specification => P_Op_Spec,
4284 Handled_Statement_Sequence =>
4285 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4287 if not Exc_Safe then
4288 Set_Is_Protected_Subprogram_Body (Sub_Body);
4289 end if;
4291 return Sub_Body;
4292 end Build_Protected_Subprogram_Body;
4294 -------------------------------------
4295 -- Build_Protected_Subprogram_Call --
4296 -------------------------------------
4298 procedure Build_Protected_Subprogram_Call
4299 (N : Node_Id;
4300 Name : Node_Id;
4301 Rec : Node_Id;
4302 External : Boolean := True)
4304 Loc : constant Source_Ptr := Sloc (N);
4305 Sub : constant Entity_Id := Entity (Name);
4306 New_Sub : Node_Id;
4307 Params : List_Id;
4309 begin
4310 if External then
4311 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4312 else
4313 New_Sub :=
4314 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4315 end if;
4317 if Present (Parameter_Associations (N)) then
4318 Params := New_Copy_List_Tree (Parameter_Associations (N));
4319 else
4320 Params := New_List;
4321 end if;
4323 -- If the type is an untagged derived type, convert to the root type,
4324 -- which is the one on which the operations are defined.
4326 if Nkind (Rec) = N_Unchecked_Type_Conversion
4327 and then not Is_Tagged_Type (Etype (Rec))
4328 and then Is_Derived_Type (Etype (Rec))
4329 then
4330 Set_Etype (Rec, Root_Type (Etype (Rec)));
4331 Set_Subtype_Mark (Rec,
4332 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4333 end if;
4335 Prepend (Rec, Params);
4337 if Ekind (Sub) = E_Procedure then
4338 Rewrite (N,
4339 Make_Procedure_Call_Statement (Loc,
4340 Name => New_Sub,
4341 Parameter_Associations => Params));
4343 else
4344 pragma Assert (Ekind (Sub) = E_Function);
4345 Rewrite (N,
4346 Make_Function_Call (Loc,
4347 Name => New_Sub,
4348 Parameter_Associations => Params));
4349 end if;
4351 if External
4352 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4353 and then Is_Entity_Name (Expression (Rec))
4354 and then Is_Shared_Passive (Entity (Expression (Rec)))
4355 then
4356 Add_Shared_Var_Lock_Procs (N);
4357 end if;
4358 end Build_Protected_Subprogram_Call;
4360 -------------------------
4361 -- Build_Selected_Name --
4362 -------------------------
4364 function Build_Selected_Name
4365 (Prefix : Entity_Id;
4366 Selector : Entity_Id;
4367 Append_Char : Character := ' ') return Name_Id
4369 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4370 Select_Len : Natural;
4372 begin
4373 Get_Name_String (Chars (Selector));
4374 Select_Len := Name_Len;
4375 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4376 Get_Name_String (Chars (Prefix));
4378 -- If scope is anonymous type, discard suffix to recover name of
4379 -- single protected object. Otherwise use protected type name.
4381 if Name_Buffer (Name_Len) = 'T' then
4382 Name_Len := Name_Len - 1;
4383 end if;
4385 Add_Str_To_Name_Buffer ("__");
4386 for J in 1 .. Select_Len loop
4387 Add_Char_To_Name_Buffer (Select_Buffer (J));
4388 end loop;
4390 -- Now add the Append_Char if specified. The encoding to follow
4391 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4392 -- then the entity is associated to a protected type subprogram.
4393 -- Otherwise, it is a protected type entry. For each case, the
4394 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4396 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4398 if Append_Char /= ' ' then
4399 if Append_Char = 'P' or Append_Char = 'N' then
4400 Add_Char_To_Name_Buffer (Append_Char);
4401 return Name_Find;
4402 else
4403 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4404 return New_External_Name (Name_Find, ' ', -1);
4405 end if;
4406 else
4407 return Name_Find;
4408 end if;
4409 end Build_Selected_Name;
4411 -----------------------------
4412 -- Build_Simple_Entry_Call --
4413 -----------------------------
4415 -- A task entry call is converted to a call to Call_Simple
4417 -- declare
4418 -- P : parms := (parm, parm, parm);
4419 -- begin
4420 -- Call_Simple (acceptor-task, entry-index, P'Address);
4421 -- parm := P.param;
4422 -- parm := P.param;
4423 -- ...
4424 -- end;
4426 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4427 -- the parameters, and the constructed aggregate value contains either the
4428 -- parameters or, in the case of non-elementary types, references to these
4429 -- parameters. Then the address of this aggregate is passed to the runtime
4430 -- routine, along with the task id value and the task entry index value.
4431 -- Pnn is only required if parameters are present.
4433 -- The assignments after the call are present only in the case of in-out
4434 -- or out parameters for elementary types, and are used to assign back the
4435 -- resulting values of such parameters.
4437 -- Note: the reason that we insert a block here is that in the context
4438 -- of selects, conditional entry calls etc. the entry call statement
4439 -- appears on its own, not as an element of a list.
4441 -- A protected entry call is converted to a Protected_Entry_Call:
4443 -- declare
4444 -- P : E1_Params := (param, param, param);
4445 -- Pnn : Boolean;
4446 -- Bnn : Communications_Block;
4448 -- declare
4449 -- P : E1_Params := (param, param, param);
4450 -- Bnn : Communications_Block;
4452 -- begin
4453 -- Protected_Entry_Call (
4454 -- Object => po._object'Access,
4455 -- E => <entry index>;
4456 -- Uninterpreted_Data => P'Address;
4457 -- Mode => Simple_Call;
4458 -- Block => Bnn);
4459 -- parm := P.param;
4460 -- parm := P.param;
4461 -- ...
4462 -- end;
4464 procedure Build_Simple_Entry_Call
4465 (N : Node_Id;
4466 Concval : Node_Id;
4467 Ename : Node_Id;
4468 Index : Node_Id)
4470 begin
4471 Expand_Call (N);
4473 -- If call has been inlined, nothing left to do
4475 if Nkind (N) = N_Block_Statement then
4476 return;
4477 end if;
4479 -- Convert entry call to Call_Simple call
4481 declare
4482 Loc : constant Source_Ptr := Sloc (N);
4483 Parms : constant List_Id := Parameter_Associations (N);
4484 Stats : constant List_Id := New_List;
4485 Actual : Node_Id;
4486 Call : Node_Id;
4487 Comm_Name : Entity_Id;
4488 Conctyp : Node_Id;
4489 Decls : List_Id;
4490 Ent : Entity_Id;
4491 Ent_Acc : Entity_Id;
4492 Formal : Node_Id;
4493 Iface_Tag : Entity_Id;
4494 Iface_Typ : Entity_Id;
4495 N_Node : Node_Id;
4496 N_Var : Node_Id;
4497 P : Entity_Id;
4498 Parm1 : Node_Id;
4499 Parm2 : Node_Id;
4500 Parm3 : Node_Id;
4501 Pdecl : Node_Id;
4502 Plist : List_Id;
4503 X : Entity_Id;
4504 Xdecl : Node_Id;
4506 begin
4507 -- Simple entry and entry family cases merge here
4509 Ent := Entity (Ename);
4510 Ent_Acc := Entry_Parameters_Type (Ent);
4511 Conctyp := Etype (Concval);
4513 -- If prefix is an access type, dereference to obtain the task type
4515 if Is_Access_Type (Conctyp) then
4516 Conctyp := Designated_Type (Conctyp);
4517 end if;
4519 -- Special case for protected subprogram calls
4521 if Is_Protected_Type (Conctyp)
4522 and then Is_Subprogram (Entity (Ename))
4523 then
4524 if not Is_Eliminated (Entity (Ename)) then
4525 Build_Protected_Subprogram_Call
4526 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4527 Analyze (N);
4528 end if;
4530 return;
4531 end if;
4533 -- First parameter is the Task_Id value from the task value or the
4534 -- Object from the protected object value, obtained by selecting
4535 -- the _Task_Id or _Object from the result of doing an unchecked
4536 -- conversion to convert the value to the corresponding record type.
4538 if Nkind (Concval) = N_Function_Call
4539 and then Is_Task_Type (Conctyp)
4540 and then Ada_Version >= Ada_2005
4541 then
4542 declare
4543 ExpR : constant Node_Id := Relocate_Node (Concval);
4544 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4545 Decl : Node_Id;
4547 begin
4548 Decl :=
4549 Make_Object_Declaration (Loc,
4550 Defining_Identifier => Obj,
4551 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4552 Expression => ExpR);
4553 Set_Etype (Obj, Conctyp);
4554 Decls := New_List (Decl);
4555 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4556 end;
4558 else
4559 Decls := New_List;
4560 end if;
4562 Parm1 := Concurrent_Ref (Concval);
4564 -- Second parameter is the entry index, computed by the routine
4565 -- provided for this purpose. The value of this expression is
4566 -- assigned to an intermediate variable to assure that any entry
4567 -- family index expressions are evaluated before the entry
4568 -- parameters.
4570 if Abort_Allowed
4571 or else Restriction_Active (No_Entry_Queue) = False
4572 or else not Is_Protected_Type (Conctyp)
4573 or else Number_Entries (Conctyp) > 1
4574 or else (Has_Attach_Handler (Conctyp)
4575 and then not Restricted_Profile)
4576 then
4577 X := Make_Defining_Identifier (Loc, Name_uX);
4579 Xdecl :=
4580 Make_Object_Declaration (Loc,
4581 Defining_Identifier => X,
4582 Object_Definition =>
4583 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
4584 Expression => Actual_Index_Expression (
4585 Loc, Entity (Ename), Index, Concval));
4587 Append_To (Decls, Xdecl);
4588 Parm2 := New_Reference_To (X, Loc);
4590 else
4591 Xdecl := Empty;
4592 Parm2 := Empty;
4593 end if;
4595 -- The third parameter is the packaged parameters. If there are
4596 -- none, then it is just the null address, since nothing is passed.
4598 if No (Parms) then
4599 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
4600 P := Empty;
4602 -- Case of parameters present, where third argument is the address
4603 -- of a packaged record containing the required parameter values.
4605 else
4606 -- First build a list of parameter values, which are references to
4607 -- objects of the parameter types.
4609 Plist := New_List;
4611 Actual := First_Actual (N);
4612 Formal := First_Formal (Ent);
4614 while Present (Actual) loop
4616 -- If it is a by_copy_type, copy it to a new variable. The
4617 -- packaged record has a field that points to this variable.
4619 if Is_By_Copy_Type (Etype (Actual)) then
4620 N_Node :=
4621 Make_Object_Declaration (Loc,
4622 Defining_Identifier => Make_Temporary (Loc, 'J'),
4623 Aliased_Present => True,
4624 Object_Definition =>
4625 New_Reference_To (Etype (Formal), Loc));
4627 -- Mark the object as not needing initialization since the
4628 -- initialization is performed separately, avoiding errors
4629 -- on cases such as formals of null-excluding access types.
4631 Set_No_Initialization (N_Node);
4633 -- We must make an assignment statement separate for the
4634 -- case of limited type. We cannot assign it unless the
4635 -- Assignment_OK flag is set first. An out formal of an
4636 -- access type must also be initialized from the actual,
4637 -- as stated in RM 6.4.1 (13).
4639 if Ekind (Formal) /= E_Out_Parameter
4640 or else Is_Access_Type (Etype (Formal))
4641 then
4642 N_Var :=
4643 New_Reference_To (Defining_Identifier (N_Node), Loc);
4644 Set_Assignment_OK (N_Var);
4645 Append_To (Stats,
4646 Make_Assignment_Statement (Loc,
4647 Name => N_Var,
4648 Expression => Relocate_Node (Actual)));
4649 end if;
4651 Append (N_Node, Decls);
4653 Append_To (Plist,
4654 Make_Attribute_Reference (Loc,
4655 Attribute_Name => Name_Unchecked_Access,
4656 Prefix =>
4657 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4659 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4661 elsif Is_VM_By_Copy_Actual (Actual) then
4662 N_Node :=
4663 Make_Object_Declaration (Loc,
4664 Defining_Identifier => Make_Temporary (Loc, 'J'),
4665 Aliased_Present => True,
4666 Object_Definition =>
4667 New_Reference_To (Etype (Formal), Loc),
4668 Expression => New_Copy_Tree (Actual));
4669 Set_Assignment_OK (N_Node);
4671 Append (N_Node, Decls);
4673 Append_To (Plist,
4674 Make_Attribute_Reference (Loc,
4675 Attribute_Name => Name_Unchecked_Access,
4676 Prefix =>
4677 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4679 else
4680 -- Interface class-wide formal
4682 if Ada_Version >= Ada_2005
4683 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4684 and then Is_Interface (Etype (Formal))
4685 then
4686 Iface_Typ := Etype (Etype (Formal));
4688 -- Generate:
4689 -- formal_iface_type! (actual.iface_tag)'reference
4691 Iface_Tag :=
4692 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4693 pragma Assert (Present (Iface_Tag));
4695 Append_To (Plist,
4696 Make_Reference (Loc,
4697 Unchecked_Convert_To (Iface_Typ,
4698 Make_Selected_Component (Loc,
4699 Prefix =>
4700 Relocate_Node (Actual),
4701 Selector_Name =>
4702 New_Reference_To (Iface_Tag, Loc)))));
4703 else
4704 -- Generate:
4705 -- actual'reference
4707 Append_To (Plist,
4708 Make_Reference (Loc, Relocate_Node (Actual)));
4709 end if;
4710 end if;
4712 Next_Actual (Actual);
4713 Next_Formal_With_Extras (Formal);
4714 end loop;
4716 -- Now build the declaration of parameters initialized with the
4717 -- aggregate containing this constructed parameter list.
4719 P := Make_Defining_Identifier (Loc, Name_uP);
4721 Pdecl :=
4722 Make_Object_Declaration (Loc,
4723 Defining_Identifier => P,
4724 Object_Definition =>
4725 New_Reference_To (Designated_Type (Ent_Acc), Loc),
4726 Expression =>
4727 Make_Aggregate (Loc, Expressions => Plist));
4729 Parm3 :=
4730 Make_Attribute_Reference (Loc,
4731 Prefix => New_Reference_To (P, Loc),
4732 Attribute_Name => Name_Address);
4734 Append (Pdecl, Decls);
4735 end if;
4737 -- Now we can create the call, case of protected type
4739 if Is_Protected_Type (Conctyp) then
4740 case Corresponding_Runtime_Package (Conctyp) is
4741 when System_Tasking_Protected_Objects_Entries =>
4743 -- Change the type of the index declaration
4745 Set_Object_Definition (Xdecl,
4746 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
4748 -- Some additional declarations for protected entry calls
4750 if No (Decls) then
4751 Decls := New_List;
4752 end if;
4754 -- Bnn : Communications_Block;
4756 Comm_Name := Make_Temporary (Loc, 'B');
4758 Append_To (Decls,
4759 Make_Object_Declaration (Loc,
4760 Defining_Identifier => Comm_Name,
4761 Object_Definition =>
4762 New_Reference_To (RTE (RE_Communication_Block), Loc)));
4764 -- Some additional statements for protected entry calls
4766 -- Protected_Entry_Call (
4767 -- Object => po._object'Access,
4768 -- E => <entry index>;
4769 -- Uninterpreted_Data => P'Address;
4770 -- Mode => Simple_Call;
4771 -- Block => Bnn);
4773 Call :=
4774 Make_Procedure_Call_Statement (Loc,
4775 Name =>
4776 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
4778 Parameter_Associations => New_List (
4779 Make_Attribute_Reference (Loc,
4780 Attribute_Name => Name_Unchecked_Access,
4781 Prefix => Parm1),
4782 Parm2,
4783 Parm3,
4784 New_Reference_To (RTE (RE_Simple_Call), Loc),
4785 New_Occurrence_Of (Comm_Name, Loc)));
4787 when System_Tasking_Protected_Objects_Single_Entry =>
4788 -- Protected_Single_Entry_Call (
4789 -- Object => po._object'Access,
4790 -- Uninterpreted_Data => P'Address;
4791 -- Mode => Simple_Call);
4793 Call :=
4794 Make_Procedure_Call_Statement (Loc,
4795 Name => New_Reference_To (
4796 RTE (RE_Protected_Single_Entry_Call), Loc),
4798 Parameter_Associations => New_List (
4799 Make_Attribute_Reference (Loc,
4800 Attribute_Name => Name_Unchecked_Access,
4801 Prefix => Parm1),
4802 Parm3,
4803 New_Reference_To (RTE (RE_Simple_Call), Loc)));
4805 when others =>
4806 raise Program_Error;
4807 end case;
4809 -- Case of task type
4811 else
4812 Call :=
4813 Make_Procedure_Call_Statement (Loc,
4814 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
4815 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4817 end if;
4819 Append_To (Stats, Call);
4821 -- If there are out or in/out parameters by copy add assignment
4822 -- statements for the result values.
4824 if Present (Parms) then
4825 Actual := First_Actual (N);
4826 Formal := First_Formal (Ent);
4828 Set_Assignment_OK (Actual);
4829 while Present (Actual) loop
4830 if (Is_By_Copy_Type (Etype (Actual))
4831 or else Is_VM_By_Copy_Actual (Actual))
4832 and then Ekind (Formal) /= E_In_Parameter
4833 then
4834 N_Node :=
4835 Make_Assignment_Statement (Loc,
4836 Name => New_Copy (Actual),
4837 Expression =>
4838 Make_Explicit_Dereference (Loc,
4839 Make_Selected_Component (Loc,
4840 Prefix => New_Reference_To (P, Loc),
4841 Selector_Name =>
4842 Make_Identifier (Loc, Chars (Formal)))));
4844 -- In all cases (including limited private types) we want
4845 -- the assignment to be valid.
4847 Set_Assignment_OK (Name (N_Node));
4849 -- If the call is the triggering alternative in an
4850 -- asynchronous select, or the entry_call alternative of a
4851 -- conditional entry call, the assignments for in-out
4852 -- parameters are incorporated into the statement list that
4853 -- follows, so that there are executed only if the entry
4854 -- call succeeds.
4856 if (Nkind (Parent (N)) = N_Triggering_Alternative
4857 and then N = Triggering_Statement (Parent (N)))
4858 or else
4859 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4860 and then N = Entry_Call_Statement (Parent (N)))
4861 then
4862 if No (Statements (Parent (N))) then
4863 Set_Statements (Parent (N), New_List);
4864 end if;
4866 Prepend (N_Node, Statements (Parent (N)));
4868 else
4869 Insert_After (Call, N_Node);
4870 end if;
4871 end if;
4873 Next_Actual (Actual);
4874 Next_Formal_With_Extras (Formal);
4875 end loop;
4876 end if;
4878 -- Finally, create block and analyze it
4880 Rewrite (N,
4881 Make_Block_Statement (Loc,
4882 Declarations => Decls,
4883 Handled_Statement_Sequence =>
4884 Make_Handled_Sequence_Of_Statements (Loc,
4885 Statements => Stats)));
4887 Analyze (N);
4888 end;
4889 end Build_Simple_Entry_Call;
4891 --------------------------------
4892 -- Build_Task_Activation_Call --
4893 --------------------------------
4895 procedure Build_Task_Activation_Call (N : Node_Id) is
4896 Loc : constant Source_Ptr := Sloc (N);
4897 Chain : Entity_Id;
4898 Call : Node_Id;
4899 Name : Node_Id;
4900 P : Node_Id;
4902 begin
4903 -- For sequential elaboration policy, all the tasks will be activated at
4904 -- the end of the elaboration.
4906 if Partition_Elaboration_Policy = 'S' then
4907 return;
4908 end if;
4910 -- Get the activation chain entity. Except in the case of a package
4911 -- body, this is in the node that was passed. For a package body, we
4912 -- have to find the corresponding package declaration node.
4914 if Nkind (N) = N_Package_Body then
4915 P := Corresponding_Spec (N);
4916 loop
4917 P := Parent (P);
4918 exit when Nkind (P) = N_Package_Declaration;
4919 end loop;
4921 Chain := Activation_Chain_Entity (P);
4923 else
4924 Chain := Activation_Chain_Entity (N);
4925 end if;
4927 if Present (Chain) then
4928 if Restricted_Profile then
4929 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
4930 else
4931 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
4932 end if;
4934 Call :=
4935 Make_Procedure_Call_Statement (Loc,
4936 Name => Name,
4937 Parameter_Associations =>
4938 New_List (Make_Attribute_Reference (Loc,
4939 Prefix => New_Occurrence_Of (Chain, Loc),
4940 Attribute_Name => Name_Unchecked_Access)));
4942 if Nkind (N) = N_Package_Declaration then
4943 if Present (Corresponding_Body (N)) then
4944 null;
4946 elsif Present (Private_Declarations (Specification (N))) then
4947 Append (Call, Private_Declarations (Specification (N)));
4949 else
4950 Append (Call, Visible_Declarations (Specification (N)));
4951 end if;
4953 else
4954 if Present (Handled_Statement_Sequence (N)) then
4956 -- The call goes at the start of the statement sequence
4957 -- after the start of exception range label if one is present.
4959 declare
4960 Stm : Node_Id;
4962 begin
4963 Stm := First (Statements (Handled_Statement_Sequence (N)));
4965 -- A special case, skip exception range label if one is
4966 -- present (from front end zcx processing).
4968 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4969 Next (Stm);
4970 end if;
4972 -- Another special case, if the first statement is a block
4973 -- from optimization of a local raise to a goto, then the
4974 -- call goes inside this block.
4976 if Nkind (Stm) = N_Block_Statement
4977 and then Exception_Junk (Stm)
4978 then
4979 Stm :=
4980 First (Statements (Handled_Statement_Sequence (Stm)));
4981 end if;
4983 -- Insertion point is after any exception label pushes,
4984 -- since we want it covered by any local handlers.
4986 while Nkind (Stm) in N_Push_xxx_Label loop
4987 Next (Stm);
4988 end loop;
4990 -- Now we have the proper insertion point
4992 Insert_Before (Stm, Call);
4993 end;
4995 else
4996 Set_Handled_Statement_Sequence (N,
4997 Make_Handled_Sequence_Of_Statements (Loc,
4998 Statements => New_List (Call)));
4999 end if;
5000 end if;
5002 Analyze (Call);
5003 Check_Task_Activation (N);
5004 end if;
5005 end Build_Task_Activation_Call;
5007 -------------------------------
5008 -- Build_Task_Allocate_Block --
5009 -------------------------------
5011 procedure Build_Task_Allocate_Block
5012 (Actions : List_Id;
5013 N : Node_Id;
5014 Args : List_Id)
5016 T : constant Entity_Id := Entity (Expression (N));
5017 Init : constant Entity_Id := Base_Init_Proc (T);
5018 Loc : constant Source_Ptr := Sloc (N);
5019 Chain : constant Entity_Id :=
5020 Make_Defining_Identifier (Loc, Name_uChain);
5021 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5022 Block : Node_Id;
5024 begin
5025 Block :=
5026 Make_Block_Statement (Loc,
5027 Identifier => New_Reference_To (Blkent, Loc),
5028 Declarations => New_List (
5030 -- _Chain : Activation_Chain;
5032 Make_Object_Declaration (Loc,
5033 Defining_Identifier => Chain,
5034 Aliased_Present => True,
5035 Object_Definition =>
5036 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5038 Handled_Statement_Sequence =>
5039 Make_Handled_Sequence_Of_Statements (Loc,
5041 Statements => New_List (
5043 -- Init (Args);
5045 Make_Procedure_Call_Statement (Loc,
5046 Name => New_Reference_To (Init, Loc),
5047 Parameter_Associations => Args),
5049 -- Activate_Tasks (_Chain);
5051 Make_Procedure_Call_Statement (Loc,
5052 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5053 Parameter_Associations => New_List (
5054 Make_Attribute_Reference (Loc,
5055 Prefix => New_Reference_To (Chain, Loc),
5056 Attribute_Name => Name_Unchecked_Access))))),
5058 Has_Created_Identifier => True,
5059 Is_Task_Allocation_Block => True);
5061 Append_To (Actions,
5062 Make_Implicit_Label_Declaration (Loc,
5063 Defining_Identifier => Blkent,
5064 Label_Construct => Block));
5066 Append_To (Actions, Block);
5068 Set_Activation_Chain_Entity (Block, Chain);
5069 end Build_Task_Allocate_Block;
5071 -----------------------------------------------
5072 -- Build_Task_Allocate_Block_With_Init_Stmts --
5073 -----------------------------------------------
5075 procedure Build_Task_Allocate_Block_With_Init_Stmts
5076 (Actions : List_Id;
5077 N : Node_Id;
5078 Init_Stmts : List_Id)
5080 Loc : constant Source_Ptr := Sloc (N);
5081 Chain : constant Entity_Id :=
5082 Make_Defining_Identifier (Loc, Name_uChain);
5083 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5084 Block : Node_Id;
5086 begin
5087 Append_To (Init_Stmts,
5088 Make_Procedure_Call_Statement (Loc,
5089 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
5090 Parameter_Associations => New_List (
5091 Make_Attribute_Reference (Loc,
5092 Prefix => New_Reference_To (Chain, Loc),
5093 Attribute_Name => Name_Unchecked_Access))));
5095 Block :=
5096 Make_Block_Statement (Loc,
5097 Identifier => New_Reference_To (Blkent, Loc),
5098 Declarations => New_List (
5100 -- _Chain : Activation_Chain;
5102 Make_Object_Declaration (Loc,
5103 Defining_Identifier => Chain,
5104 Aliased_Present => True,
5105 Object_Definition =>
5106 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
5108 Handled_Statement_Sequence =>
5109 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5111 Has_Created_Identifier => True,
5112 Is_Task_Allocation_Block => True);
5114 Append_To (Actions,
5115 Make_Implicit_Label_Declaration (Loc,
5116 Defining_Identifier => Blkent,
5117 Label_Construct => Block));
5119 Append_To (Actions, Block);
5121 Set_Activation_Chain_Entity (Block, Chain);
5122 end Build_Task_Allocate_Block_With_Init_Stmts;
5124 -----------------------------------
5125 -- Build_Task_Proc_Specification --
5126 -----------------------------------
5128 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5129 Loc : constant Source_Ptr := Sloc (T);
5130 Spec_Id : Entity_Id;
5132 begin
5133 -- Case of explicit task type, suffix TB
5135 if Comes_From_Source (T) then
5136 Spec_Id :=
5137 Make_Defining_Identifier (Loc,
5138 Chars => New_External_Name (Chars (T), "TB"));
5140 -- Case of anonymous task type, suffix B
5142 else
5143 Spec_Id :=
5144 Make_Defining_Identifier (Loc,
5145 Chars => New_External_Name (Chars (T), 'B'));
5146 end if;
5148 Set_Is_Internal (Spec_Id);
5150 -- Associate the procedure with the task, if this is the declaration
5151 -- (and not the body) of the procedure.
5153 if No (Task_Body_Procedure (T)) then
5154 Set_Task_Body_Procedure (T, Spec_Id);
5155 end if;
5157 return
5158 Make_Procedure_Specification (Loc,
5159 Defining_Unit_Name => Spec_Id,
5160 Parameter_Specifications => New_List (
5161 Make_Parameter_Specification (Loc,
5162 Defining_Identifier =>
5163 Make_Defining_Identifier (Loc, Name_uTask),
5164 Parameter_Type =>
5165 Make_Access_Definition (Loc,
5166 Subtype_Mark =>
5167 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
5168 end Build_Task_Proc_Specification;
5170 ---------------------------------------
5171 -- Build_Unprotected_Subprogram_Body --
5172 ---------------------------------------
5174 function Build_Unprotected_Subprogram_Body
5175 (N : Node_Id;
5176 Pid : Node_Id) return Node_Id
5178 Decls : constant List_Id := Declarations (N);
5180 begin
5181 -- Add renamings for the Protection object, discriminals, privals and
5182 -- the entry index constant for use by debugger.
5184 Debug_Private_Data_Declarations (Decls);
5186 -- Make an unprotected version of the subprogram for use within the same
5187 -- object, with a new name and an additional parameter representing the
5188 -- object.
5190 return
5191 Make_Subprogram_Body (Sloc (N),
5192 Specification =>
5193 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5194 Declarations => Decls,
5195 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5196 end Build_Unprotected_Subprogram_Body;
5198 ----------------------------
5199 -- Collect_Entry_Families --
5200 ----------------------------
5202 procedure Collect_Entry_Families
5203 (Loc : Source_Ptr;
5204 Cdecls : List_Id;
5205 Current_Node : in out Node_Id;
5206 Conctyp : Entity_Id)
5208 Efam : Entity_Id;
5209 Efam_Decl : Node_Id;
5210 Efam_Type : Entity_Id;
5212 begin
5213 Efam := First_Entity (Conctyp);
5214 while Present (Efam) loop
5215 if Ekind (Efam) = E_Entry_Family then
5216 Efam_Type := Make_Temporary (Loc, 'F');
5218 declare
5219 Bas : Entity_Id :=
5220 Base_Type
5221 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5223 Bas_Decl : Node_Id := Empty;
5224 Lo, Hi : Node_Id;
5226 begin
5227 Get_Index_Bounds
5228 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5230 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5231 Bas := Make_Temporary (Loc, 'B');
5233 Bas_Decl :=
5234 Make_Subtype_Declaration (Loc,
5235 Defining_Identifier => Bas,
5236 Subtype_Indication =>
5237 Make_Subtype_Indication (Loc,
5238 Subtype_Mark =>
5239 New_Occurrence_Of (Standard_Integer, Loc),
5240 Constraint =>
5241 Make_Range_Constraint (Loc,
5242 Range_Expression => Make_Range (Loc,
5243 Make_Integer_Literal
5244 (Loc, -Entry_Family_Bound),
5245 Make_Integer_Literal
5246 (Loc, Entry_Family_Bound - 1)))));
5248 Insert_After (Current_Node, Bas_Decl);
5249 Current_Node := Bas_Decl;
5250 Analyze (Bas_Decl);
5251 end if;
5253 Efam_Decl :=
5254 Make_Full_Type_Declaration (Loc,
5255 Defining_Identifier => Efam_Type,
5256 Type_Definition =>
5257 Make_Unconstrained_Array_Definition (Loc,
5258 Subtype_Marks =>
5259 (New_List (New_Occurrence_Of (Bas, Loc))),
5261 Component_Definition =>
5262 Make_Component_Definition (Loc,
5263 Aliased_Present => False,
5264 Subtype_Indication =>
5265 New_Reference_To (Standard_Character, Loc))));
5266 end;
5268 Insert_After (Current_Node, Efam_Decl);
5269 Current_Node := Efam_Decl;
5270 Analyze (Efam_Decl);
5272 Append_To (Cdecls,
5273 Make_Component_Declaration (Loc,
5274 Defining_Identifier =>
5275 Make_Defining_Identifier (Loc, Chars (Efam)),
5277 Component_Definition =>
5278 Make_Component_Definition (Loc,
5279 Aliased_Present => False,
5280 Subtype_Indication =>
5281 Make_Subtype_Indication (Loc,
5282 Subtype_Mark =>
5283 New_Occurrence_Of (Efam_Type, Loc),
5285 Constraint =>
5286 Make_Index_Or_Discriminant_Constraint (Loc,
5287 Constraints => New_List (
5288 New_Occurrence_Of
5289 (Etype (Discrete_Subtype_Definition
5290 (Parent (Efam))), Loc)))))));
5292 end if;
5294 Next_Entity (Efam);
5295 end loop;
5296 end Collect_Entry_Families;
5298 -----------------------
5299 -- Concurrent_Object --
5300 -----------------------
5302 function Concurrent_Object
5303 (Spec_Id : Entity_Id;
5304 Conc_Typ : Entity_Id) return Entity_Id
5306 begin
5307 -- Parameter _O or _object
5309 if Is_Protected_Type (Conc_Typ) then
5310 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5312 -- Parameter _task
5314 else
5315 pragma Assert (Is_Task_Type (Conc_Typ));
5316 return First_Formal (Task_Body_Procedure (Conc_Typ));
5317 end if;
5318 end Concurrent_Object;
5320 ----------------------
5321 -- Copy_Result_Type --
5322 ----------------------
5324 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5325 New_Res : constant Node_Id := New_Copy_Tree (Res);
5326 Par_Spec : Node_Id;
5327 Formal : Entity_Id;
5329 begin
5330 -- If the result type is an access_to_subprogram, we must create new
5331 -- entities for its spec.
5333 if Nkind (New_Res) = N_Access_Definition
5334 and then Present (Access_To_Subprogram_Definition (New_Res))
5335 then
5336 -- Provide new entities for the formals
5338 Par_Spec := First (Parameter_Specifications
5339 (Access_To_Subprogram_Definition (New_Res)));
5340 while Present (Par_Spec) loop
5341 Formal := Defining_Identifier (Par_Spec);
5342 Set_Defining_Identifier (Par_Spec,
5343 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5344 Next (Par_Spec);
5345 end loop;
5346 end if;
5348 return New_Res;
5349 end Copy_Result_Type;
5351 --------------------
5352 -- Concurrent_Ref --
5353 --------------------
5355 -- The expression returned for a reference to a concurrent object has the
5356 -- form:
5358 -- taskV!(name)._Task_Id
5360 -- for a task, and
5362 -- objectV!(name)._Object
5364 -- for a protected object. For the case of an access to a concurrent
5365 -- object, there is an extra explicit dereference:
5367 -- taskV!(name.all)._Task_Id
5368 -- objectV!(name.all)._Object
5370 -- here taskV and objectV are the types for the associated records, which
5371 -- contain the required _Task_Id and _Object fields for tasks and protected
5372 -- objects, respectively.
5374 -- For the case of a task type name, the expression is
5376 -- Self;
5378 -- i.e. a call to the Self function which returns precisely this Task_Id
5380 -- For the case of a protected type name, the expression is
5382 -- objectR
5384 -- which is a renaming of the _object field of the current object
5385 -- record, passed into protected operations as a parameter.
5387 function Concurrent_Ref (N : Node_Id) return Node_Id is
5388 Loc : constant Source_Ptr := Sloc (N);
5389 Ntyp : constant Entity_Id := Etype (N);
5390 Dtyp : Entity_Id;
5391 Sel : Name_Id;
5393 function Is_Current_Task (T : Entity_Id) return Boolean;
5394 -- Check whether the reference is to the immediately enclosing task
5395 -- type, or to an outer one (rare but legal).
5397 ---------------------
5398 -- Is_Current_Task --
5399 ---------------------
5401 function Is_Current_Task (T : Entity_Id) return Boolean is
5402 Scop : Entity_Id;
5404 begin
5405 Scop := Current_Scope;
5406 while Present (Scop)
5407 and then Scop /= Standard_Standard
5408 loop
5410 if Scop = T then
5411 return True;
5413 elsif Is_Task_Type (Scop) then
5414 return False;
5416 -- If this is a procedure nested within the task type, we must
5417 -- assume that it can be called from an inner task, and therefore
5418 -- cannot treat it as a local reference.
5420 elsif Is_Overloadable (Scop)
5421 and then In_Open_Scopes (T)
5422 then
5423 return False;
5425 else
5426 Scop := Scope (Scop);
5427 end if;
5428 end loop;
5430 -- We know that we are within the task body, so should have found it
5431 -- in scope.
5433 raise Program_Error;
5434 end Is_Current_Task;
5436 -- Start of processing for Concurrent_Ref
5438 begin
5439 if Is_Access_Type (Ntyp) then
5440 Dtyp := Designated_Type (Ntyp);
5442 if Is_Protected_Type (Dtyp) then
5443 Sel := Name_uObject;
5444 else
5445 Sel := Name_uTask_Id;
5446 end if;
5448 return
5449 Make_Selected_Component (Loc,
5450 Prefix =>
5451 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5452 Make_Explicit_Dereference (Loc, N)),
5453 Selector_Name => Make_Identifier (Loc, Sel));
5455 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5456 if Is_Task_Type (Entity (N)) then
5458 if Is_Current_Task (Entity (N)) then
5459 return
5460 Make_Function_Call (Loc,
5461 Name => New_Reference_To (RTE (RE_Self), Loc));
5463 else
5464 declare
5465 Decl : Node_Id;
5466 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5467 T_Body : constant Node_Id :=
5468 Parent (Corresponding_Body (Parent (Entity (N))));
5470 begin
5471 Decl :=
5472 Make_Object_Declaration (Loc,
5473 Defining_Identifier => T_Self,
5474 Object_Definition =>
5475 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5476 Expression =>
5477 Make_Function_Call (Loc,
5478 Name => New_Reference_To (RTE (RE_Self), Loc)));
5479 Prepend (Decl, Declarations (T_Body));
5480 Analyze (Decl);
5481 Set_Scope (T_Self, Entity (N));
5482 return New_Occurrence_Of (T_Self, Loc);
5483 end;
5484 end if;
5486 else
5487 pragma Assert (Is_Protected_Type (Entity (N)));
5489 return
5490 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
5491 end if;
5493 else
5494 if Is_Protected_Type (Ntyp) then
5495 Sel := Name_uObject;
5497 elsif Is_Task_Type (Ntyp) then
5498 Sel := Name_uTask_Id;
5500 else
5501 raise Program_Error;
5502 end if;
5504 return
5505 Make_Selected_Component (Loc,
5506 Prefix =>
5507 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5508 New_Copy_Tree (N)),
5509 Selector_Name => Make_Identifier (Loc, Sel));
5510 end if;
5511 end Concurrent_Ref;
5513 ------------------------
5514 -- Convert_Concurrent --
5515 ------------------------
5517 function Convert_Concurrent
5518 (N : Node_Id;
5519 Typ : Entity_Id) return Node_Id
5521 begin
5522 if not Is_Concurrent_Type (Typ) then
5523 return N;
5524 else
5525 return
5526 Unchecked_Convert_To
5527 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5528 end if;
5529 end Convert_Concurrent;
5531 -------------------------------------
5532 -- Debug_Private_Data_Declarations --
5533 -------------------------------------
5535 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5536 Debug_Nod : Node_Id;
5537 Decl : Node_Id;
5539 begin
5540 Decl := First (Decls);
5541 while Present (Decl) and then not Comes_From_Source (Decl) loop
5542 -- Declaration for concurrent entity _object and its access type,
5543 -- along with the entry index subtype:
5544 -- type prot_typVP is access prot_typV;
5545 -- _object : prot_typVP := prot_typV (_O);
5546 -- subtype Jnn is <Type of Index> range Low .. High;
5548 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5549 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5551 -- Declaration for the Protection object, discriminals, privals and
5552 -- entry index constant:
5553 -- conc_typR : protection_typ renames _object._object;
5554 -- discr_nameD : discr_typ renames _object.discr_name;
5555 -- discr_nameD : discr_typ renames _task.discr_name;
5556 -- prival_name : comp_typ renames _object.comp_name;
5557 -- J : constant Jnn :=
5558 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5560 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5561 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5562 Debug_Nod := Debug_Renaming_Declaration (Decl);
5564 if Present (Debug_Nod) then
5565 Insert_After (Decl, Debug_Nod);
5566 end if;
5567 end if;
5569 Next (Decl);
5570 end loop;
5571 end Debug_Private_Data_Declarations;
5573 ------------------------------
5574 -- Ensure_Statement_Present --
5575 ------------------------------
5577 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5578 Stmt : Node_Id;
5580 begin
5581 if Opt.Suppress_Control_Flow_Optimizations
5582 and then Is_Empty_List (Statements (Alt))
5583 then
5584 Stmt := Make_Null_Statement (Loc);
5586 -- Mark NULL statement as coming from source so that it is not
5587 -- eliminated by GIGI.
5589 -- Another covert channel! If this is a requirement, it must be
5590 -- documented in sinfo/einfo ???
5592 Set_Comes_From_Source (Stmt, True);
5594 Set_Statements (Alt, New_List (Stmt));
5595 end if;
5596 end Ensure_Statement_Present;
5598 ----------------------------
5599 -- Entry_Index_Expression --
5600 ----------------------------
5602 function Entry_Index_Expression
5603 (Sloc : Source_Ptr;
5604 Ent : Entity_Id;
5605 Index : Node_Id;
5606 Ttyp : Entity_Id) return Node_Id
5608 Expr : Node_Id;
5609 Num : Node_Id;
5610 Lo : Node_Id;
5611 Hi : Node_Id;
5612 Prev : Entity_Id;
5613 S : Node_Id;
5615 begin
5616 -- The queues of entries and entry families appear in textual order in
5617 -- the associated record. The entry index is computed as the sum of the
5618 -- number of queues for all entries that precede the designated one, to
5619 -- which is added the index expression, if this expression denotes a
5620 -- member of a family.
5622 -- The following is a place holder for the count of simple entries
5624 Num := Make_Integer_Literal (Sloc, 1);
5626 -- We construct an expression which is a series of addition operations.
5627 -- The first operand is the number of single entries that precede this
5628 -- one, the second operand is the index value relative to the start of
5629 -- the referenced family, and the remaining operands are the lengths of
5630 -- the entry families that precede this entry, i.e. the constructed
5631 -- expression is:
5633 -- number_simple_entries +
5634 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5635 -- family'length + ...
5637 -- where index-value is the given index value, and s is the index
5638 -- subtype (we have to use pos because the subtype might be an
5639 -- enumeration type preventing direct subtraction). Note that the task
5640 -- entry array is one-indexed.
5642 -- The upper bound of the entry family may be a discriminant, so we
5643 -- retrieve the lower bound explicitly to compute offset, rather than
5644 -- using the index subtype which may mention a discriminant.
5646 if Present (Index) then
5647 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5649 Expr :=
5650 Make_Op_Add (Sloc,
5651 Left_Opnd => Num,
5653 Right_Opnd =>
5654 Family_Offset (
5655 Sloc,
5656 Make_Attribute_Reference (Sloc,
5657 Attribute_Name => Name_Pos,
5658 Prefix => New_Reference_To (Base_Type (S), Sloc),
5659 Expressions => New_List (Relocate_Node (Index))),
5660 Type_Low_Bound (S),
5661 Ttyp,
5662 False));
5663 else
5664 Expr := Num;
5665 end if;
5667 -- Now add lengths of preceding entries and entry families
5669 Prev := First_Entity (Ttyp);
5671 while Chars (Prev) /= Chars (Ent)
5672 or else (Ekind (Prev) /= Ekind (Ent))
5673 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5674 loop
5675 if Ekind (Prev) = E_Entry then
5676 Set_Intval (Num, Intval (Num) + 1);
5678 elsif Ekind (Prev) = E_Entry_Family then
5679 S :=
5680 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5681 Lo := Type_Low_Bound (S);
5682 Hi := Type_High_Bound (S);
5684 Expr :=
5685 Make_Op_Add (Sloc,
5686 Left_Opnd => Expr,
5687 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5689 -- Other components are anonymous types to be ignored
5691 else
5692 null;
5693 end if;
5695 Next_Entity (Prev);
5696 end loop;
5698 return Expr;
5699 end Entry_Index_Expression;
5701 ---------------------------
5702 -- Establish_Task_Master --
5703 ---------------------------
5705 procedure Establish_Task_Master (N : Node_Id) is
5706 Call : Node_Id;
5708 begin
5709 if Restriction_Active (No_Task_Hierarchy) = False then
5710 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5712 -- The block may have no declarations, and nevertheless be a task
5713 -- master, if it contains a call that may return an object that
5714 -- contains tasks.
5716 if No (Declarations (N)) then
5717 Set_Declarations (N, New_List (Call));
5718 else
5719 Prepend_To (Declarations (N), Call);
5720 end if;
5722 Analyze (Call);
5723 end if;
5724 end Establish_Task_Master;
5726 --------------------------------
5727 -- Expand_Accept_Declarations --
5728 --------------------------------
5730 -- Part of the expansion of an accept statement involves the creation of
5731 -- a declaration that can be referenced from the statement sequence of
5732 -- the accept:
5734 -- Ann : Address;
5736 -- This declaration is inserted immediately before the accept statement
5737 -- and it is important that it be inserted before the statements of the
5738 -- statement sequence are analyzed. Thus it would be too late to create
5739 -- this declaration in the Expand_N_Accept_Statement routine, which is
5740 -- why there is a separate procedure to be called directly from Sem_Ch9.
5742 -- Ann is used to hold the address of the record containing the parameters
5743 -- (see Expand_N_Entry_Call for more details on how this record is built).
5744 -- References to the parameters do an unchecked conversion of this address
5745 -- to a pointer to the required record type, and then access the field that
5746 -- holds the value of the required parameter. The entity for the address
5747 -- variable is held as the top stack element (i.e. the last element) of the
5748 -- Accept_Address stack in the corresponding entry entity, and this element
5749 -- must be set in place before the statements are processed.
5751 -- The above description applies to the case of a stand alone accept
5752 -- statement, i.e. one not appearing as part of a select alternative.
5754 -- For the case of an accept that appears as part of a select alternative
5755 -- of a selective accept, we must still create the declaration right away,
5756 -- since Ann is needed immediately, but there is an important difference:
5758 -- The declaration is inserted before the selective accept, not before
5759 -- the accept statement (which is not part of a list anyway, and so would
5760 -- not accommodate inserted declarations)
5762 -- We only need one address variable for the entire selective accept. So
5763 -- the Ann declaration is created only for the first accept alternative,
5764 -- and subsequent accept alternatives reference the same Ann variable.
5766 -- We can distinguish the two cases by seeing whether the accept statement
5767 -- is part of a list. If not, then it must be in an accept alternative.
5769 -- To expand the requeue statement, a label is provided at the end of the
5770 -- accept statement or alternative of which it is a part, so that the
5771 -- statement can be skipped after the requeue is complete. This label is
5772 -- created here rather than during the expansion of the accept statement,
5773 -- because it will be needed by any requeue statements within the accept,
5774 -- which are expanded before the accept.
5776 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5777 Loc : constant Source_Ptr := Sloc (N);
5778 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5779 Ann : Entity_Id := Empty;
5780 Adecl : Node_Id;
5781 Lab : Node_Id;
5782 Ldecl : Node_Id;
5783 Ldecl2 : Node_Id;
5785 begin
5786 if Full_Expander_Active then
5788 -- If we have no handled statement sequence, we may need to build
5789 -- a dummy sequence consisting of a null statement. This can be
5790 -- skipped if the trivial accept optimization is permitted.
5792 if not Trivial_Accept_OK
5793 and then
5794 (No (Stats) or else Null_Statements (Statements (Stats)))
5795 then
5796 Set_Handled_Statement_Sequence (N,
5797 Make_Handled_Sequence_Of_Statements (Loc,
5798 Statements => New_List (Make_Null_Statement (Loc))));
5799 end if;
5801 -- Create and declare two labels to be placed at the end of the
5802 -- accept statement. The first label is used to allow requeues to
5803 -- skip the remainder of entry processing. The second label is used
5804 -- to skip the remainder of entry processing if the rendezvous
5805 -- completes in the middle of the accept body.
5807 if Present (Handled_Statement_Sequence (N)) then
5808 declare
5809 Ent : Entity_Id;
5811 begin
5812 Ent := Make_Temporary (Loc, 'L');
5813 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5814 Ldecl :=
5815 Make_Implicit_Label_Declaration (Loc,
5816 Defining_Identifier => Ent,
5817 Label_Construct => Lab);
5818 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5820 Ent := Make_Temporary (Loc, 'L');
5821 Lab := Make_Label (Loc, New_Reference_To (Ent, Loc));
5822 Ldecl2 :=
5823 Make_Implicit_Label_Declaration (Loc,
5824 Defining_Identifier => Ent,
5825 Label_Construct => Lab);
5826 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5827 end;
5829 else
5830 Ldecl := Empty;
5831 Ldecl2 := Empty;
5832 end if;
5834 -- Case of stand alone accept statement
5836 if Is_List_Member (N) then
5838 if Present (Handled_Statement_Sequence (N)) then
5839 Ann := Make_Temporary (Loc, 'A');
5841 Adecl :=
5842 Make_Object_Declaration (Loc,
5843 Defining_Identifier => Ann,
5844 Object_Definition =>
5845 New_Reference_To (RTE (RE_Address), Loc));
5847 Insert_Before_And_Analyze (N, Adecl);
5848 Insert_Before_And_Analyze (N, Ldecl);
5849 Insert_Before_And_Analyze (N, Ldecl2);
5850 end if;
5852 -- Case of accept statement which is in an accept alternative
5854 else
5855 declare
5856 Acc_Alt : constant Node_Id := Parent (N);
5857 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5858 Alt : Node_Id;
5860 begin
5861 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5862 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5864 -- ??? Consider a single label for select statements
5866 if Present (Handled_Statement_Sequence (N)) then
5867 Prepend (Ldecl2,
5868 Statements (Handled_Statement_Sequence (N)));
5869 Analyze (Ldecl2);
5871 Prepend (Ldecl,
5872 Statements (Handled_Statement_Sequence (N)));
5873 Analyze (Ldecl);
5874 end if;
5876 -- Find first accept alternative of the selective accept. A
5877 -- valid selective accept must have at least one accept in it.
5879 Alt := First (Select_Alternatives (Sel_Acc));
5881 while Nkind (Alt) /= N_Accept_Alternative loop
5882 Next (Alt);
5883 end loop;
5885 -- If we are the first accept statement, then we have to create
5886 -- the Ann variable, as for the stand alone case, except that
5887 -- it is inserted before the selective accept. Similarly, a
5888 -- label for requeue expansion must be declared.
5890 if N = Accept_Statement (Alt) then
5891 Ann := Make_Temporary (Loc, 'A');
5892 Adecl :=
5893 Make_Object_Declaration (Loc,
5894 Defining_Identifier => Ann,
5895 Object_Definition =>
5896 New_Reference_To (RTE (RE_Address), Loc));
5898 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5900 -- If we are not the first accept statement, then find the Ann
5901 -- variable allocated by the first accept and use it.
5903 else
5904 Ann :=
5905 Node (Last_Elmt (Accept_Address
5906 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5907 end if;
5908 end;
5909 end if;
5911 -- Merge here with Ann either created or referenced, and Adecl
5912 -- pointing to the corresponding declaration. Remaining processing
5913 -- is the same for the two cases.
5915 if Present (Ann) then
5916 Append_Elmt (Ann, Accept_Address (Ent));
5917 Set_Debug_Info_Needed (Ann);
5918 end if;
5920 -- Create renaming declarations for the entry formals. Each reference
5921 -- to a formal becomes a dereference of a component of the parameter
5922 -- block, whose address is held in Ann. These declarations are
5923 -- eventually inserted into the accept block, and analyzed there so
5924 -- that they have the proper scope for gdb and do not conflict with
5925 -- other declarations.
5927 if Present (Parameter_Specifications (N))
5928 and then Present (Handled_Statement_Sequence (N))
5929 then
5930 declare
5931 Comp : Entity_Id;
5932 Decl : Node_Id;
5933 Formal : Entity_Id;
5934 New_F : Entity_Id;
5935 Renamed_Formal : Node_Id;
5937 begin
5938 Push_Scope (Ent);
5939 Formal := First_Formal (Ent);
5941 while Present (Formal) loop
5942 Comp := Entry_Component (Formal);
5943 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5945 Set_Etype (New_F, Etype (Formal));
5946 Set_Scope (New_F, Ent);
5948 -- Now we set debug info needed on New_F even though it does
5949 -- not come from source, so that the debugger will get the
5950 -- right information for these generated names.
5952 Set_Debug_Info_Needed (New_F);
5954 if Ekind (Formal) = E_In_Parameter then
5955 Set_Ekind (New_F, E_Constant);
5956 else
5957 Set_Ekind (New_F, E_Variable);
5958 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5959 end if;
5961 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5963 Renamed_Formal :=
5964 Make_Selected_Component (Loc,
5965 Prefix =>
5966 Unchecked_Convert_To (
5967 Entry_Parameters_Type (Ent),
5968 New_Reference_To (Ann, Loc)),
5969 Selector_Name =>
5970 New_Reference_To (Comp, Loc));
5972 Decl :=
5973 Build_Renamed_Formal_Declaration
5974 (New_F, Formal, Comp, Renamed_Formal);
5976 if No (Declarations (N)) then
5977 Set_Declarations (N, New_List);
5978 end if;
5980 Append (Decl, Declarations (N));
5981 Set_Renamed_Object (Formal, New_F);
5982 Next_Formal (Formal);
5983 end loop;
5985 End_Scope;
5986 end;
5987 end if;
5988 end if;
5989 end Expand_Accept_Declarations;
5991 ---------------------------------------------
5992 -- Expand_Access_Protected_Subprogram_Type --
5993 ---------------------------------------------
5995 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5996 Loc : constant Source_Ptr := Sloc (N);
5997 Comps : List_Id;
5998 T : constant Entity_Id := Defining_Identifier (N);
5999 D_T : constant Entity_Id := Designated_Type (T);
6000 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6001 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6002 P_List : constant List_Id := Build_Protected_Spec
6003 (N, RTE (RE_Address), D_T, False);
6004 Decl1 : Node_Id;
6005 Decl2 : Node_Id;
6006 Def1 : Node_Id;
6008 begin
6009 -- Create access to subprogram with full signature
6011 if Etype (D_T) /= Standard_Void_Type then
6012 Def1 :=
6013 Make_Access_Function_Definition (Loc,
6014 Parameter_Specifications => P_List,
6015 Result_Definition =>
6016 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6018 else
6019 Def1 :=
6020 Make_Access_Procedure_Definition (Loc,
6021 Parameter_Specifications => P_List);
6022 end if;
6024 Decl1 :=
6025 Make_Full_Type_Declaration (Loc,
6026 Defining_Identifier => D_T2,
6027 Type_Definition => Def1);
6029 Insert_After_And_Analyze (N, Decl1);
6031 -- Associate the access to subprogram with its original access to
6032 -- protected subprogram type. Needed by the backend to know that this
6033 -- type corresponds with an access to protected subprogram type.
6035 Set_Original_Access_Type (D_T2, T);
6037 -- Create Equivalent_Type, a record with two components for an access to
6038 -- object and an access to subprogram.
6040 Comps := New_List (
6041 Make_Component_Declaration (Loc,
6042 Defining_Identifier => Make_Temporary (Loc, 'P'),
6043 Component_Definition =>
6044 Make_Component_Definition (Loc,
6045 Aliased_Present => False,
6046 Subtype_Indication =>
6047 New_Occurrence_Of (RTE (RE_Address), Loc))),
6049 Make_Component_Declaration (Loc,
6050 Defining_Identifier => Make_Temporary (Loc, 'S'),
6051 Component_Definition =>
6052 Make_Component_Definition (Loc,
6053 Aliased_Present => False,
6054 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6056 Decl2 :=
6057 Make_Full_Type_Declaration (Loc,
6058 Defining_Identifier => E_T,
6059 Type_Definition =>
6060 Make_Record_Definition (Loc,
6061 Component_List =>
6062 Make_Component_List (Loc, Component_Items => Comps)));
6064 Insert_After_And_Analyze (Decl1, Decl2);
6065 Set_Equivalent_Type (T, E_T);
6066 end Expand_Access_Protected_Subprogram_Type;
6068 --------------------------
6069 -- Expand_Entry_Barrier --
6070 --------------------------
6072 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6073 Cond : constant Node_Id :=
6074 Condition (Entry_Body_Formal_Part (N));
6075 Prot : constant Entity_Id := Scope (Ent);
6076 Spec_Decl : constant Node_Id := Parent (Prot);
6077 Func : Node_Id;
6078 B_F : Node_Id;
6079 Body_Decl : Node_Id;
6081 begin
6082 if No_Run_Time_Mode then
6083 Error_Msg_CRT ("entry barrier", N);
6084 return;
6085 end if;
6087 -- The body of the entry barrier must be analyzed in the context of the
6088 -- protected object, but its scope is external to it, just as any other
6089 -- unprotected version of a protected operation. The specification has
6090 -- been produced when the protected type declaration was elaborated. We
6091 -- build the body, insert it in the enclosing scope, but analyze it in
6092 -- the current context. A more uniform approach would be to treat the
6093 -- barrier just as a protected function, and discard the protected
6094 -- version of it because it is never called.
6096 if Full_Expander_Active then
6097 B_F := Build_Barrier_Function (N, Ent, Prot);
6098 Func := Barrier_Function (Ent);
6099 Set_Corresponding_Spec (B_F, Func);
6101 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6103 if Nkind (Parent (Body_Decl)) = N_Subunit then
6104 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6105 end if;
6107 Insert_Before_And_Analyze (Body_Decl, B_F);
6109 Set_Discriminals (Spec_Decl);
6110 Set_Scope (Func, Scope (Prot));
6112 else
6113 Analyze_And_Resolve (Cond, Any_Boolean);
6114 end if;
6116 -- The Ravenscar profile restricts barriers to simple variables declared
6117 -- within the protected object. We also allow Boolean constants, since
6118 -- these appear in several published examples and are also allowed by
6119 -- the Aonix compiler.
6121 -- Note that after analysis variables in this context will be replaced
6122 -- by the corresponding prival, that is to say a renaming of a selected
6123 -- component of the form _Object.Var. If expansion is disabled, as
6124 -- within a generic, we check that the entity appears in the current
6125 -- scope.
6127 if Is_Entity_Name (Cond) then
6129 -- A small optimization of useless renamings. If the scope of the
6130 -- entity of the condition is not the barrier function, then the
6131 -- condition does not reference any of the generated renamings
6132 -- within the function.
6134 if Full_Expander_Active and then Scope (Entity (Cond)) /= Func then
6135 Set_Declarations (B_F, Empty_List);
6136 end if;
6138 if Entity (Cond) = Standard_False
6139 or else
6140 Entity (Cond) = Standard_True
6141 then
6142 return;
6144 elsif not Expander_Active
6145 and then Scope (Entity (Cond)) = Current_Scope
6146 then
6147 return;
6149 -- Check for case of _object.all.field (note that the explicit
6150 -- dereference gets inserted by analyze/expand of _object.field)
6152 elsif Present (Renamed_Object (Entity (Cond)))
6153 and then
6154 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
6155 and then
6156 Chars
6157 (Prefix
6158 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
6159 then
6160 return;
6161 end if;
6162 end if;
6164 -- It is not a boolean variable or literal, so check the restriction
6166 Check_Restriction (Simple_Barriers, Cond);
6167 end Expand_Entry_Barrier;
6169 ------------------------------
6170 -- Expand_N_Abort_Statement --
6171 ------------------------------
6173 -- Expand abort T1, T2, .. Tn; into:
6174 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6176 procedure Expand_N_Abort_Statement (N : Node_Id) is
6177 Loc : constant Source_Ptr := Sloc (N);
6178 Tlist : constant List_Id := Names (N);
6179 Count : Nat;
6180 Aggr : Node_Id;
6181 Tasknm : Node_Id;
6183 begin
6184 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6185 Count := 0;
6187 Tasknm := First (Tlist);
6189 while Present (Tasknm) loop
6190 Count := Count + 1;
6192 -- A task interface class-wide type object is being aborted.
6193 -- Retrieve its _task_id by calling a dispatching routine.
6195 if Ada_Version >= Ada_2005
6196 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6197 and then Is_Interface (Etype (Tasknm))
6198 and then Is_Task_Interface (Etype (Tasknm))
6199 then
6200 Append_To (Component_Associations (Aggr),
6201 Make_Component_Association (Loc,
6202 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6203 Expression =>
6205 -- Task_Id (Tasknm._disp_get_task_id)
6207 Make_Unchecked_Type_Conversion (Loc,
6208 Subtype_Mark =>
6209 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
6210 Expression =>
6211 Make_Selected_Component (Loc,
6212 Prefix => New_Copy_Tree (Tasknm),
6213 Selector_Name =>
6214 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6216 else
6217 Append_To (Component_Associations (Aggr),
6218 Make_Component_Association (Loc,
6219 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6220 Expression => Concurrent_Ref (Tasknm)));
6221 end if;
6223 Next (Tasknm);
6224 end loop;
6226 Rewrite (N,
6227 Make_Procedure_Call_Statement (Loc,
6228 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
6229 Parameter_Associations => New_List (
6230 Make_Qualified_Expression (Loc,
6231 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
6232 Expression => Aggr))));
6234 Analyze (N);
6235 end Expand_N_Abort_Statement;
6237 -------------------------------
6238 -- Expand_N_Accept_Statement --
6239 -------------------------------
6241 -- This procedure handles expansion of accept statements that stand
6242 -- alone, i.e. they are not part of an accept alternative. The expansion
6243 -- of accept statement in accept alternatives is handled by the routines
6244 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6245 -- following description applies only to stand alone accept statements.
6247 -- If there is no handled statement sequence, or only null statements,
6248 -- then this is called a trivial accept, and the expansion is:
6250 -- Accept_Trivial (entry-index)
6252 -- If there is a handled statement sequence, then the expansion is:
6254 -- Ann : Address;
6255 -- {Lnn : Label}
6257 -- begin
6258 -- begin
6259 -- Accept_Call (entry-index, Ann);
6260 -- Renaming_Declarations for formals
6261 -- <statement sequence from N_Accept_Statement node>
6262 -- Complete_Rendezvous;
6263 -- <<Lnn>>
6265 -- exception
6266 -- when ... =>
6267 -- <exception handler from N_Accept_Statement node>
6268 -- Complete_Rendezvous;
6269 -- when ... =>
6270 -- <exception handler from N_Accept_Statement node>
6271 -- Complete_Rendezvous;
6272 -- ...
6273 -- end;
6275 -- exception
6276 -- when all others =>
6277 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6278 -- end;
6280 -- The first three declarations were already inserted ahead of the accept
6281 -- statement by the Expand_Accept_Declarations procedure, which was called
6282 -- directly from the semantics during analysis of the accept statement,
6283 -- before analyzing its contained statements.
6285 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6286 -- from possible expansion activity (the original source of course does
6287 -- not have any declarations associated with the accept statement, since
6288 -- an accept statement has no declarative part). In particular, if the
6289 -- expander is active, the first such declaration is the declaration of
6290 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6292 -- The two blocks are merged into a single block if the inner block has
6293 -- no exception handlers, but otherwise two blocks are required, since
6294 -- exceptions might be raised in the exception handlers of the inner
6295 -- block, and Exceptional_Complete_Rendezvous must be called.
6297 procedure Expand_N_Accept_Statement (N : Node_Id) is
6298 Loc : constant Source_Ptr := Sloc (N);
6299 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6300 Ename : constant Node_Id := Entry_Direct_Name (N);
6301 Eindx : constant Node_Id := Entry_Index (N);
6302 Eent : constant Entity_Id := Entity (Ename);
6303 Acstack : constant Elist_Id := Accept_Address (Eent);
6304 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6305 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6306 Blkent : Entity_Id;
6307 Call : Node_Id;
6308 Block : Node_Id;
6310 begin
6311 -- If the accept statement is not part of a list, then its parent must
6312 -- be an accept alternative, and, as described above, we do not do any
6313 -- expansion for such accept statements at this level.
6315 if not Is_List_Member (N) then
6316 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6317 return;
6319 -- Trivial accept case (no statement sequence, or null statements).
6320 -- If the accept statement has declarations, then just insert them
6321 -- before the procedure call.
6323 elsif Trivial_Accept_OK
6324 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6325 then
6326 -- Remove declarations for renamings, because the parameter block
6327 -- will not be assigned.
6329 declare
6330 D : Node_Id;
6331 Next_D : Node_Id;
6333 begin
6334 D := First (Declarations (N));
6336 while Present (D) loop
6337 Next_D := Next (D);
6338 if Nkind (D) = N_Object_Renaming_Declaration then
6339 Remove (D);
6340 end if;
6342 D := Next_D;
6343 end loop;
6344 end;
6346 if Present (Declarations (N)) then
6347 Insert_Actions (N, Declarations (N));
6348 end if;
6350 Rewrite (N,
6351 Make_Procedure_Call_Statement (Loc,
6352 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
6353 Parameter_Associations => New_List (
6354 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6356 Analyze (N);
6358 -- Discard Entry_Address that was created for it, so it will not be
6359 -- emitted if this accept statement is in the statement part of a
6360 -- delay alternative.
6362 if Present (Stats) then
6363 Remove_Last_Elmt (Acstack);
6364 end if;
6366 -- Case of statement sequence present
6368 else
6369 -- Construct the block, using the declarations from the accept
6370 -- statement if any to initialize the declarations of the block.
6372 Blkent := Make_Temporary (Loc, 'A');
6373 Set_Ekind (Blkent, E_Block);
6374 Set_Etype (Blkent, Standard_Void_Type);
6375 Set_Scope (Blkent, Current_Scope);
6377 Block :=
6378 Make_Block_Statement (Loc,
6379 Identifier => New_Reference_To (Blkent, Loc),
6380 Declarations => Declarations (N),
6381 Handled_Statement_Sequence => Build_Accept_Body (N));
6383 -- For the analysis of the generated declarations, the parent node
6384 -- must be properly set.
6386 Set_Parent (Block, Parent (N));
6388 -- Prepend call to Accept_Call to main statement sequence If the
6389 -- accept has exception handlers, the statement sequence is wrapped
6390 -- in a block. Insert call and renaming declarations in the
6391 -- declarations of the block, so they are elaborated before the
6392 -- handlers.
6394 Call :=
6395 Make_Procedure_Call_Statement (Loc,
6396 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
6397 Parameter_Associations => New_List (
6398 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6399 New_Reference_To (Ann, Loc)));
6401 if Parent (Stats) = N then
6402 Prepend (Call, Statements (Stats));
6403 else
6404 Set_Declarations (Parent (Stats), New_List (Call));
6405 end if;
6407 Analyze (Call);
6409 Push_Scope (Blkent);
6411 declare
6412 D : Node_Id;
6413 Next_D : Node_Id;
6414 Typ : Entity_Id;
6416 begin
6417 D := First (Declarations (N));
6418 while Present (D) loop
6419 Next_D := Next (D);
6421 if Nkind (D) = N_Object_Renaming_Declaration then
6423 -- The renaming declarations for the formals were created
6424 -- during analysis of the accept statement, and attached to
6425 -- the list of declarations. Place them now in the context
6426 -- of the accept block or subprogram.
6428 Remove (D);
6429 Typ := Entity (Subtype_Mark (D));
6430 Insert_After (Call, D);
6431 Analyze (D);
6433 -- If the formal is class_wide, it does not have an actual
6434 -- subtype. The analysis of the renaming declaration creates
6435 -- one, but we need to retain the class-wide nature of the
6436 -- entity.
6438 if Is_Class_Wide_Type (Typ) then
6439 Set_Etype (Defining_Identifier (D), Typ);
6440 end if;
6442 end if;
6444 D := Next_D;
6445 end loop;
6446 end;
6448 End_Scope;
6450 -- Replace the accept statement by the new block
6452 Rewrite (N, Block);
6453 Analyze (N);
6455 -- Last step is to unstack the Accept_Address value
6457 Remove_Last_Elmt (Acstack);
6458 end if;
6459 end Expand_N_Accept_Statement;
6461 ----------------------------------
6462 -- Expand_N_Asynchronous_Select --
6463 ----------------------------------
6465 -- This procedure assumes that the trigger statement is an entry call or
6466 -- a dispatching procedure call. A delay alternative should already have
6467 -- been expanded into an entry call to the appropriate delay object Wait
6468 -- entry.
6470 -- If the trigger is a task entry call, the select is implemented with
6471 -- a Task_Entry_Call:
6473 -- declare
6474 -- B : Boolean;
6475 -- C : Boolean;
6476 -- P : parms := (parm, parm, parm);
6478 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6480 -- procedure _clean is
6481 -- begin
6482 -- ...
6483 -- Cancel_Task_Entry_Call (C);
6484 -- ...
6485 -- end _clean;
6487 -- begin
6488 -- Abort_Defer;
6489 -- Task_Entry_Call
6490 -- (<acceptor-task>, -- Acceptor
6491 -- <entry-index>, -- E
6492 -- P'Address, -- Uninterpreted_Data
6493 -- Asynchronous_Call, -- Mode
6494 -- B); -- Rendezvous_Successful
6496 -- begin
6497 -- begin
6498 -- Abort_Undefer;
6499 -- <abortable-part>
6500 -- at end
6501 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6502 -- end;
6503 -- exception
6504 -- when Abort_Signal => Abort_Undefer;
6505 -- end;
6507 -- parm := P.param;
6508 -- parm := P.param;
6509 -- ...
6510 -- if not C then
6511 -- <triggered-statements>
6512 -- end if;
6513 -- end;
6515 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6516 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6517 -- as follows:
6519 -- declare
6520 -- P : parms := (parm, parm, parm);
6521 -- begin
6522 -- Call_Simple (acceptor-task, entry-index, P'Address);
6523 -- parm := P.param;
6524 -- parm := P.param;
6525 -- ...
6526 -- end;
6528 -- so the task at hand is to convert the latter expansion into the former
6530 -- If the trigger is a protected entry call, the select is implemented
6531 -- with Protected_Entry_Call:
6533 -- declare
6534 -- P : E1_Params := (param, param, param);
6535 -- Bnn : Communications_Block;
6537 -- begin
6538 -- declare
6540 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6542 -- procedure _clean is
6543 -- begin
6544 -- ...
6545 -- if Enqueued (Bnn) then
6546 -- Cancel_Protected_Entry_Call (Bnn);
6547 -- end if;
6548 -- ...
6549 -- end _clean;
6551 -- begin
6552 -- begin
6553 -- Protected_Entry_Call
6554 -- (po._object'Access, -- Object
6555 -- <entry index>, -- E
6556 -- P'Address, -- Uninterpreted_Data
6557 -- Asynchronous_Call, -- Mode
6558 -- Bnn); -- Block
6560 -- if Enqueued (Bnn) then
6561 -- <abortable-part>
6562 -- end if;
6563 -- at end
6564 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6565 -- end;
6566 -- exception
6567 -- when Abort_Signal => Abort_Undefer;
6568 -- end;
6570 -- if not Cancelled (Bnn) then
6571 -- <triggered-statements>
6572 -- end if;
6573 -- end;
6575 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6576 -- entry call:
6578 -- declare
6579 -- P : E1_Params := (param, param, param);
6580 -- Bnn : Communications_Block;
6582 -- begin
6583 -- Protected_Entry_Call
6584 -- (po._object'Access, -- Object
6585 -- <entry index>, -- E
6586 -- P'Address, -- Uninterpreted_Data
6587 -- Simple_Call, -- Mode
6588 -- Bnn); -- Block
6589 -- parm := P.param;
6590 -- parm := P.param;
6591 -- ...
6592 -- end;
6594 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6595 -- expanded into:
6597 -- declare
6598 -- B : Boolean := False;
6599 -- Bnn : Communication_Block;
6600 -- C : Ada.Tags.Prim_Op_Kind;
6601 -- D : System.Storage_Elements.Dummy_Communication_Block;
6602 -- K : Ada.Tags.Tagged_Kind :=
6603 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6604 -- P : Parameters := (Param1 .. ParamN);
6605 -- S : Integer;
6606 -- U : Boolean;
6608 -- begin
6609 -- if K = Ada.Tags.TK_Limited_Tagged then
6610 -- <dispatching-call>;
6611 -- <triggering-statements>;
6613 -- else
6614 -- S :=
6615 -- Ada.Tags.Get_Offset_Index
6616 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6618 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6620 -- if C = POK_Protected_Entry then
6621 -- declare
6622 -- procedure _clean is
6623 -- begin
6624 -- if Enqueued (Bnn) then
6625 -- Cancel_Protected_Entry_Call (Bnn);
6626 -- end if;
6627 -- end _clean;
6629 -- begin
6630 -- begin
6631 -- _Disp_Asynchronous_Select
6632 -- (<object>, S, P'Address, D, B);
6633 -- Bnn := Communication_Block (D);
6635 -- Param1 := P.Param1;
6636 -- ...
6637 -- ParamN := P.ParamN;
6639 -- if Enqueued (Bnn) then
6640 -- <abortable-statements>
6641 -- end if;
6642 -- at end
6643 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6644 -- end;
6645 -- exception
6646 -- when Abort_Signal => Abort_Undefer;
6647 -- end;
6649 -- if not Cancelled (Bnn) then
6650 -- <triggering-statements>
6651 -- end if;
6653 -- elsif C = POK_Task_Entry then
6654 -- declare
6655 -- procedure _clean is
6656 -- begin
6657 -- Cancel_Task_Entry_Call (U);
6658 -- end _clean;
6660 -- begin
6661 -- Abort_Defer;
6663 -- _Disp_Asynchronous_Select
6664 -- (<object>, S, P'Address, D, B);
6665 -- Bnn := Communication_Bloc (D);
6667 -- Param1 := P.Param1;
6668 -- ...
6669 -- ParamN := P.ParamN;
6671 -- begin
6672 -- begin
6673 -- Abort_Undefer;
6674 -- <abortable-statements>
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 -- if not U then
6683 -- <triggering-statements>
6684 -- end if;
6685 -- end;
6687 -- else
6688 -- <dispatching-call>;
6689 -- <triggering-statements>
6690 -- end if;
6691 -- end if;
6692 -- end;
6694 -- The job is to convert this to the asynchronous form
6696 -- If the trigger is a delay statement, it will have been expanded into a
6697 -- call to one of the GNARL delay procedures. This routine will convert
6698 -- this into a protected entry call on a delay object and then continue
6699 -- processing as for a protected entry call trigger. This requires
6700 -- declaring a Delay_Block object and adding a pointer to this object to
6701 -- the parameter list of the delay procedure to form the parameter list of
6702 -- the entry call. This object is used by the runtime to queue the delay
6703 -- request.
6705 -- For a description of the use of P and the assignments after the call,
6706 -- see Expand_N_Entry_Call_Statement.
6708 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6709 Loc : constant Source_Ptr := Sloc (N);
6710 Abrt : constant Node_Id := Abortable_Part (N);
6711 Trig : constant Node_Id := Triggering_Alternative (N);
6713 Abort_Block_Ent : Entity_Id;
6714 Abortable_Block : Node_Id;
6715 Actuals : List_Id;
6716 Astats : List_Id;
6717 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6718 Blk_Typ : Entity_Id;
6719 Call : Node_Id;
6720 Call_Ent : Entity_Id;
6721 Cancel_Param : Entity_Id;
6722 Cleanup_Block : Node_Id;
6723 Cleanup_Block_Ent : Entity_Id;
6724 Cleanup_Stmts : List_Id;
6725 Conc_Typ_Stmts : List_Id;
6726 Concval : Node_Id;
6727 Dblock_Ent : Entity_Id;
6728 Decl : Node_Id;
6729 Decls : List_Id;
6730 Ecall : Node_Id;
6731 Ename : Node_Id;
6732 Enqueue_Call : Node_Id;
6733 Formals : List_Id;
6734 Hdle : List_Id;
6735 Handler_Stmt : Node_Id;
6736 Index : Node_Id;
6737 Lim_Typ_Stmts : List_Id;
6738 N_Orig : Node_Id;
6739 Obj : Entity_Id;
6740 Param : Node_Id;
6741 Params : List_Id;
6742 Pdef : Entity_Id;
6743 ProtE_Stmts : List_Id;
6744 ProtP_Stmts : List_Id;
6745 Stmt : Node_Id;
6746 Stmts : List_Id;
6747 TaskE_Stmts : List_Id;
6748 Tstats : List_Id;
6750 B : Entity_Id; -- Call status flag
6751 Bnn : Entity_Id; -- Communication block
6752 C : Entity_Id; -- Call kind
6753 K : Entity_Id; -- Tagged kind
6754 P : Entity_Id; -- Parameter block
6755 S : Entity_Id; -- Primitive operation slot
6756 T : Entity_Id; -- Additional status flag
6758 begin
6759 Process_Statements_For_Controlled_Objects (Trig);
6760 Process_Statements_For_Controlled_Objects (Abrt);
6762 Ecall := Triggering_Statement (Trig);
6764 Ensure_Statement_Present (Sloc (Ecall), Trig);
6766 -- Retrieve Astats and Tstats now because the finalization machinery may
6767 -- wrap them in blocks.
6769 Astats := Statements (Abrt);
6770 Tstats := Statements (Trig);
6772 -- The arguments in the call may require dynamic allocation, and the
6773 -- call statement may have been transformed into a block. The block
6774 -- may contain additional declarations for internal entities, and the
6775 -- original call is found by sequential search.
6777 if Nkind (Ecall) = N_Block_Statement then
6778 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6779 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6780 N_Entry_Call_Statement)
6781 loop
6782 Next (Ecall);
6783 end loop;
6784 end if;
6786 -- This is either a dispatching call or a delay statement used as a
6787 -- trigger which was expanded into a procedure call.
6789 if Nkind (Ecall) = N_Procedure_Call_Statement then
6790 if Ada_Version >= Ada_2005
6791 and then
6792 (No (Original_Node (Ecall))
6793 or else not Nkind_In (Original_Node (Ecall),
6794 N_Delay_Relative_Statement,
6795 N_Delay_Until_Statement))
6796 then
6797 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6799 Decls := New_List;
6800 Stmts := New_List;
6802 -- Call status flag processing, generate:
6803 -- B : Boolean := False;
6805 B := Build_B (Loc, Decls);
6807 -- Communication block processing, generate:
6808 -- Bnn : Communication_Block;
6810 Bnn := Make_Temporary (Loc, 'B');
6811 Append_To (Decls,
6812 Make_Object_Declaration (Loc,
6813 Defining_Identifier => Bnn,
6814 Object_Definition =>
6815 New_Reference_To (RTE (RE_Communication_Block), Loc)));
6817 -- Call kind processing, generate:
6818 -- C : Ada.Tags.Prim_Op_Kind;
6820 C := Build_C (Loc, Decls);
6822 -- Tagged kind processing, generate:
6823 -- K : Ada.Tags.Tagged_Kind :=
6824 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6826 -- Dummy communication block, generate:
6827 -- D : Dummy_Communication_Block;
6829 Append_To (Decls,
6830 Make_Object_Declaration (Loc,
6831 Defining_Identifier =>
6832 Make_Defining_Identifier (Loc, Name_uD),
6833 Object_Definition =>
6834 New_Reference_To (
6835 RTE (RE_Dummy_Communication_Block), Loc)));
6837 K := Build_K (Loc, Decls, Obj);
6839 -- Parameter block processing
6841 Blk_Typ := Build_Parameter_Block
6842 (Loc, Actuals, Formals, Decls);
6843 P := Parameter_Block_Pack
6844 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6846 -- Dispatch table slot processing, generate:
6847 -- S : Integer;
6849 S := Build_S (Loc, Decls);
6851 -- Additional status flag processing, generate:
6852 -- Tnn : Boolean;
6854 T := Make_Temporary (Loc, 'T');
6855 Append_To (Decls,
6856 Make_Object_Declaration (Loc,
6857 Defining_Identifier => T,
6858 Object_Definition =>
6859 New_Reference_To (Standard_Boolean, Loc)));
6861 ------------------------------
6862 -- Protected entry handling --
6863 ------------------------------
6865 -- Generate:
6866 -- Param1 := P.Param1;
6867 -- ...
6868 -- ParamN := P.ParamN;
6870 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6872 -- Generate:
6873 -- Bnn := Communication_Block (D);
6875 Prepend_To (Cleanup_Stmts,
6876 Make_Assignment_Statement (Loc,
6877 Name =>
6878 New_Reference_To (Bnn, Loc),
6879 Expression =>
6880 Make_Unchecked_Type_Conversion (Loc,
6881 Subtype_Mark =>
6882 New_Reference_To (RTE (RE_Communication_Block), Loc),
6883 Expression => Make_Identifier (Loc, Name_uD))));
6885 -- Generate:
6886 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6888 Prepend_To (Cleanup_Stmts,
6889 Make_Procedure_Call_Statement (Loc,
6890 Name =>
6891 New_Reference_To (
6892 Find_Prim_Op (Etype (Etype (Obj)),
6893 Name_uDisp_Asynchronous_Select),
6894 Loc),
6895 Parameter_Associations =>
6896 New_List (
6897 New_Copy_Tree (Obj), -- <object>
6898 New_Reference_To (S, Loc), -- S
6899 Make_Attribute_Reference (Loc, -- P'Address
6900 Prefix => New_Reference_To (P, Loc),
6901 Attribute_Name => Name_Address),
6902 Make_Identifier (Loc, Name_uD), -- D
6903 New_Reference_To (B, Loc)))); -- B
6905 -- Generate:
6906 -- if Enqueued (Bnn) then
6907 -- <abortable-statements>
6908 -- end if;
6910 Append_To (Cleanup_Stmts,
6911 Make_Implicit_If_Statement (N,
6912 Condition =>
6913 Make_Function_Call (Loc,
6914 Name =>
6915 New_Reference_To (RTE (RE_Enqueued), Loc),
6916 Parameter_Associations =>
6917 New_List (New_Reference_To (Bnn, Loc))),
6919 Then_Statements =>
6920 New_Copy_List_Tree (Astats)));
6922 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6923 -- will then generate a _clean for the communication block Bnn.
6925 -- Generate:
6926 -- declare
6927 -- procedure _clean is
6928 -- begin
6929 -- if Enqueued (Bnn) then
6930 -- Cancel_Protected_Entry_Call (Bnn);
6931 -- end if;
6932 -- end _clean;
6933 -- begin
6934 -- Cleanup_Stmts
6935 -- at end
6936 -- _clean;
6937 -- end;
6939 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6940 Cleanup_Block :=
6941 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
6943 -- Wrap the cleanup block in an exception handling block
6945 -- Generate:
6946 -- begin
6947 -- Cleanup_Block
6948 -- exception
6949 -- when Abort_Signal => Abort_Undefer;
6950 -- end;
6952 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6953 ProtE_Stmts :=
6954 New_List (
6955 Make_Implicit_Label_Declaration (Loc,
6956 Defining_Identifier => Abort_Block_Ent),
6958 Build_Abort_Block
6959 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6961 -- Generate:
6962 -- if not Cancelled (Bnn) then
6963 -- <triggering-statements>
6964 -- end if;
6966 Append_To (ProtE_Stmts,
6967 Make_Implicit_If_Statement (N,
6968 Condition =>
6969 Make_Op_Not (Loc,
6970 Right_Opnd =>
6971 Make_Function_Call (Loc,
6972 Name =>
6973 New_Reference_To (RTE (RE_Cancelled), Loc),
6974 Parameter_Associations =>
6975 New_List (New_Reference_To (Bnn, Loc)))),
6977 Then_Statements =>
6978 New_Copy_List_Tree (Tstats)));
6980 -------------------------
6981 -- Task entry handling --
6982 -------------------------
6984 -- Generate:
6985 -- Param1 := P.Param1;
6986 -- ...
6987 -- ParamN := P.ParamN;
6989 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6991 -- Generate:
6992 -- Bnn := Communication_Block (D);
6994 Append_To (TaskE_Stmts,
6995 Make_Assignment_Statement (Loc,
6996 Name =>
6997 New_Reference_To (Bnn, Loc),
6998 Expression =>
6999 Make_Unchecked_Type_Conversion (Loc,
7000 Subtype_Mark =>
7001 New_Reference_To (RTE (RE_Communication_Block), Loc),
7002 Expression => Make_Identifier (Loc, Name_uD))));
7004 -- Generate:
7005 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7007 Prepend_To (TaskE_Stmts,
7008 Make_Procedure_Call_Statement (Loc,
7009 Name =>
7010 New_Reference_To (
7011 Find_Prim_Op (Etype (Etype (Obj)),
7012 Name_uDisp_Asynchronous_Select),
7013 Loc),
7015 Parameter_Associations =>
7016 New_List (
7017 New_Copy_Tree (Obj), -- <object>
7018 New_Reference_To (S, Loc), -- S
7019 Make_Attribute_Reference (Loc, -- P'Address
7020 Prefix => New_Reference_To (P, Loc),
7021 Attribute_Name => Name_Address),
7022 Make_Identifier (Loc, Name_uD), -- D
7023 New_Reference_To (B, Loc)))); -- B
7025 -- Generate:
7026 -- Abort_Defer;
7028 Prepend_To (TaskE_Stmts,
7029 Make_Procedure_Call_Statement (Loc,
7030 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7031 Parameter_Associations => No_List));
7033 -- Generate:
7034 -- Abort_Undefer;
7035 -- <abortable-statements>
7037 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7039 Prepend_To (Cleanup_Stmts,
7040 Make_Procedure_Call_Statement (Loc,
7041 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7042 Parameter_Associations => No_List));
7044 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7045 -- will generate a _clean for the additional status flag.
7047 -- Generate:
7048 -- declare
7049 -- procedure _clean is
7050 -- begin
7051 -- Cancel_Task_Entry_Call (U);
7052 -- end _clean;
7053 -- begin
7054 -- Cleanup_Stmts
7055 -- at end
7056 -- _clean;
7057 -- end;
7059 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7060 Cleanup_Block :=
7061 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7063 -- Wrap the cleanup block in an exception handling block
7065 -- Generate:
7066 -- begin
7067 -- Cleanup_Block
7068 -- exception
7069 -- when Abort_Signal => Abort_Undefer;
7070 -- end;
7072 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7074 Append_To (TaskE_Stmts,
7075 Make_Implicit_Label_Declaration (Loc,
7076 Defining_Identifier => Abort_Block_Ent));
7078 Append_To (TaskE_Stmts,
7079 Build_Abort_Block
7080 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7082 -- Generate:
7083 -- if not T then
7084 -- <triggering-statements>
7085 -- end if;
7087 Append_To (TaskE_Stmts,
7088 Make_Implicit_If_Statement (N,
7089 Condition =>
7090 Make_Op_Not (Loc, Right_Opnd => New_Reference_To (T, Loc)),
7092 Then_Statements =>
7093 New_Copy_List_Tree (Tstats)));
7095 ----------------------------------
7096 -- Protected procedure handling --
7097 ----------------------------------
7099 -- Generate:
7100 -- <dispatching-call>;
7101 -- <triggering-statements>
7103 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7104 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7106 -- Generate:
7107 -- S := Ada.Tags.Get_Offset_Index
7108 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7110 Conc_Typ_Stmts :=
7111 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7113 -- Generate:
7114 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7116 Append_To (Conc_Typ_Stmts,
7117 Make_Procedure_Call_Statement (Loc,
7118 Name =>
7119 New_Reference_To (
7120 Find_Prim_Op (Etype (Etype (Obj)),
7121 Name_uDisp_Get_Prim_Op_Kind),
7122 Loc),
7123 Parameter_Associations =>
7124 New_List (
7125 New_Copy_Tree (Obj),
7126 New_Reference_To (S, Loc),
7127 New_Reference_To (C, Loc))));
7129 -- Generate:
7130 -- if C = POK_Procedure_Entry then
7131 -- ProtE_Stmts
7132 -- elsif C = POK_Task_Entry then
7133 -- TaskE_Stmts
7134 -- else
7135 -- ProtP_Stmts
7136 -- end if;
7138 Append_To (Conc_Typ_Stmts,
7139 Make_Implicit_If_Statement (N,
7140 Condition =>
7141 Make_Op_Eq (Loc,
7142 Left_Opnd =>
7143 New_Reference_To (C, Loc),
7144 Right_Opnd =>
7145 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
7147 Then_Statements =>
7148 ProtE_Stmts,
7150 Elsif_Parts =>
7151 New_List (
7152 Make_Elsif_Part (Loc,
7153 Condition =>
7154 Make_Op_Eq (Loc,
7155 Left_Opnd =>
7156 New_Reference_To (C, Loc),
7157 Right_Opnd =>
7158 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
7160 Then_Statements =>
7161 TaskE_Stmts)),
7163 Else_Statements =>
7164 ProtP_Stmts));
7166 -- Generate:
7167 -- <dispatching-call>;
7168 -- <triggering-statements>
7170 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7171 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7173 -- Generate:
7174 -- if K = Ada.Tags.TK_Limited_Tagged then
7175 -- Lim_Typ_Stmts
7176 -- else
7177 -- Conc_Typ_Stmts
7178 -- end if;
7180 Append_To (Stmts,
7181 Make_Implicit_If_Statement (N,
7182 Condition =>
7183 Make_Op_Eq (Loc,
7184 Left_Opnd =>
7185 New_Reference_To (K, Loc),
7186 Right_Opnd =>
7187 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7189 Then_Statements =>
7190 Lim_Typ_Stmts,
7192 Else_Statements =>
7193 Conc_Typ_Stmts));
7195 Rewrite (N,
7196 Make_Block_Statement (Loc,
7197 Declarations =>
7198 Decls,
7199 Handled_Statement_Sequence =>
7200 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7202 Analyze (N);
7203 return;
7205 -- Delay triggering statement processing
7207 else
7208 -- Add a Delay_Block object to the parameter list of the delay
7209 -- procedure to form the parameter list of the Wait entry call.
7211 Dblock_Ent := Make_Temporary (Loc, 'D');
7213 Pdef := Entity (Name (Ecall));
7215 if Is_RTE (Pdef, RO_CA_Delay_For) then
7216 Enqueue_Call :=
7217 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
7219 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7220 Enqueue_Call :=
7221 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
7223 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7224 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
7225 end if;
7227 Append_To (Parameter_Associations (Ecall),
7228 Make_Attribute_Reference (Loc,
7229 Prefix => New_Reference_To (Dblock_Ent, Loc),
7230 Attribute_Name => Name_Unchecked_Access));
7232 -- Create the inner block to protect the abortable part
7234 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7236 Prepend_To (Astats,
7237 Make_Procedure_Call_Statement (Loc,
7238 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7240 Abortable_Block :=
7241 Make_Block_Statement (Loc,
7242 Identifier => New_Reference_To (Blk_Ent, Loc),
7243 Handled_Statement_Sequence =>
7244 Make_Handled_Sequence_Of_Statements (Loc,
7245 Statements => Astats),
7246 Has_Created_Identifier => True,
7247 Is_Asynchronous_Call_Block => True);
7249 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7251 Rewrite (Ecall,
7252 Make_Implicit_If_Statement (N,
7253 Condition =>
7254 Make_Function_Call (Loc,
7255 Name => Enqueue_Call,
7256 Parameter_Associations => Parameter_Associations (Ecall)),
7257 Then_Statements =>
7258 New_List (Make_Block_Statement (Loc,
7259 Handled_Statement_Sequence =>
7260 Make_Handled_Sequence_Of_Statements (Loc,
7261 Statements => New_List (
7262 Make_Implicit_Label_Declaration (Loc,
7263 Defining_Identifier => Blk_Ent,
7264 Label_Construct => Abortable_Block),
7265 Abortable_Block),
7266 Exception_Handlers => Hdle)))));
7268 Stmts := New_List (Ecall);
7270 -- Construct statement sequence for new block
7272 Append_To (Stmts,
7273 Make_Implicit_If_Statement (N,
7274 Condition =>
7275 Make_Function_Call (Loc,
7276 Name => New_Reference_To (
7277 RTE (RE_Timed_Out), Loc),
7278 Parameter_Associations => New_List (
7279 Make_Attribute_Reference (Loc,
7280 Prefix => New_Reference_To (Dblock_Ent, Loc),
7281 Attribute_Name => Name_Unchecked_Access))),
7282 Then_Statements => Tstats));
7284 -- The result is the new block
7286 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7288 Rewrite (N,
7289 Make_Block_Statement (Loc,
7290 Declarations => New_List (
7291 Make_Object_Declaration (Loc,
7292 Defining_Identifier => Dblock_Ent,
7293 Aliased_Present => True,
7294 Object_Definition => New_Reference_To (
7295 RTE (RE_Delay_Block), Loc))),
7297 Handled_Statement_Sequence =>
7298 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7300 Analyze (N);
7301 return;
7302 end if;
7304 else
7305 N_Orig := N;
7306 end if;
7308 Extract_Entry (Ecall, Concval, Ename, Index);
7309 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7311 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7312 Decls := Declarations (Ecall);
7314 if Is_Protected_Type (Etype (Concval)) then
7316 -- Get the declarations of the block expanded from the entry call
7318 Decl := First (Decls);
7319 while Present (Decl)
7320 and then
7321 (Nkind (Decl) /= N_Object_Declaration
7322 or else not Is_RTE (Etype (Object_Definition (Decl)),
7323 RE_Communication_Block))
7324 loop
7325 Next (Decl);
7326 end loop;
7328 pragma Assert (Present (Decl));
7329 Cancel_Param := Defining_Identifier (Decl);
7331 -- Change the mode of the Protected_Entry_Call call
7333 -- Protected_Entry_Call (
7334 -- Object => po._object'Access,
7335 -- E => <entry index>;
7336 -- Uninterpreted_Data => P'Address;
7337 -- Mode => Asynchronous_Call;
7338 -- Block => Bnn);
7340 Stmt := First (Stmts);
7342 -- Skip assignments to temporaries created for in-out parameters
7344 -- This makes unwarranted assumptions about the shape of the expanded
7345 -- tree for the call, and should be cleaned up ???
7347 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7348 Next (Stmt);
7349 end loop;
7351 Call := Stmt;
7353 Param := First (Parameter_Associations (Call));
7354 while Present (Param)
7355 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7356 loop
7357 Next (Param);
7358 end loop;
7360 pragma Assert (Present (Param));
7361 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7362 Analyze (Param);
7364 -- Append an if statement to execute the abortable part
7366 -- Generate:
7367 -- if Enqueued (Bnn) then
7369 Append_To (Stmts,
7370 Make_Implicit_If_Statement (N,
7371 Condition =>
7372 Make_Function_Call (Loc,
7373 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
7374 Parameter_Associations => New_List (
7375 New_Reference_To (Cancel_Param, Loc))),
7376 Then_Statements => Astats));
7378 Abortable_Block :=
7379 Make_Block_Statement (Loc,
7380 Identifier => New_Reference_To (Blk_Ent, Loc),
7381 Handled_Statement_Sequence =>
7382 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7383 Has_Created_Identifier => True,
7384 Is_Asynchronous_Call_Block => True);
7386 -- For the VM call Update_Exception instead of Abort_Undefer.
7387 -- See 4jexcept.ads for an explanation.
7389 if VM_Target = No_VM then
7390 if Exception_Mechanism = Back_End_Exceptions then
7392 -- Aborts are not deferred at beginning of exception handlers
7393 -- in ZCX.
7395 Handler_Stmt := Make_Null_Statement (Loc);
7397 else
7398 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7399 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7400 Parameter_Associations => No_List);
7401 end if;
7402 else
7403 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7404 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
7405 Parameter_Associations => New_List (
7406 Make_Function_Call (Loc,
7407 Name => New_Occurrence_Of
7408 (RTE (RE_Current_Target_Exception), Loc))));
7409 end if;
7411 Stmts := New_List (
7412 Make_Block_Statement (Loc,
7413 Handled_Statement_Sequence =>
7414 Make_Handled_Sequence_Of_Statements (Loc,
7415 Statements => New_List (
7416 Make_Implicit_Label_Declaration (Loc,
7417 Defining_Identifier => Blk_Ent,
7418 Label_Construct => Abortable_Block),
7419 Abortable_Block),
7421 -- exception
7423 Exception_Handlers => New_List (
7424 Make_Implicit_Exception_Handler (Loc,
7426 -- when Abort_Signal =>
7427 -- Abort_Undefer.all;
7429 Exception_Choices =>
7430 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
7431 Statements => New_List (Handler_Stmt))))),
7433 -- if not Cancelled (Bnn) then
7434 -- triggered statements
7435 -- end if;
7437 Make_Implicit_If_Statement (N,
7438 Condition => Make_Op_Not (Loc,
7439 Right_Opnd =>
7440 Make_Function_Call (Loc,
7441 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7442 Parameter_Associations => New_List (
7443 New_Occurrence_Of (Cancel_Param, Loc)))),
7444 Then_Statements => Tstats));
7446 -- Asynchronous task entry call
7448 else
7449 if No (Decls) then
7450 Decls := New_List;
7451 end if;
7453 B := Make_Defining_Identifier (Loc, Name_uB);
7455 -- Insert declaration of B in declarations of existing block
7457 Prepend_To (Decls,
7458 Make_Object_Declaration (Loc,
7459 Defining_Identifier => B,
7460 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7462 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7464 -- Insert declaration of C in declarations of existing block
7466 Prepend_To (Decls,
7467 Make_Object_Declaration (Loc,
7468 Defining_Identifier => Cancel_Param,
7469 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7471 -- Remove and save the call to Call_Simple
7473 Stmt := First (Stmts);
7475 -- Skip assignments to temporaries created for in-out parameters.
7476 -- This makes unwarranted assumptions about the shape of the expanded
7477 -- tree for the call, and should be cleaned up ???
7479 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7480 Next (Stmt);
7481 end loop;
7483 Call := Stmt;
7485 -- Create the inner block to protect the abortable part
7487 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7489 Prepend_To (Astats,
7490 Make_Procedure_Call_Statement (Loc,
7491 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7493 Abortable_Block :=
7494 Make_Block_Statement (Loc,
7495 Identifier => New_Reference_To (Blk_Ent, Loc),
7496 Handled_Statement_Sequence =>
7497 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7498 Has_Created_Identifier => True,
7499 Is_Asynchronous_Call_Block => True);
7501 Insert_After (Call,
7502 Make_Block_Statement (Loc,
7503 Handled_Statement_Sequence =>
7504 Make_Handled_Sequence_Of_Statements (Loc,
7505 Statements => New_List (
7506 Make_Implicit_Label_Declaration (Loc,
7507 Defining_Identifier => Blk_Ent,
7508 Label_Construct => Abortable_Block),
7509 Abortable_Block),
7510 Exception_Handlers => Hdle)));
7512 -- Create new call statement
7514 Params := Parameter_Associations (Call);
7516 Append_To (Params,
7517 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7518 Append_To (Params, New_Reference_To (B, Loc));
7520 Rewrite (Call,
7521 Make_Procedure_Call_Statement (Loc,
7522 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7523 Parameter_Associations => Params));
7525 -- Construct statement sequence for new block
7527 Append_To (Stmts,
7528 Make_Implicit_If_Statement (N,
7529 Condition =>
7530 Make_Op_Not (Loc, New_Reference_To (Cancel_Param, Loc)),
7531 Then_Statements => Tstats));
7533 -- Protected the call against abort
7535 Prepend_To (Stmts,
7536 Make_Procedure_Call_Statement (Loc,
7537 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7538 Parameter_Associations => Empty_List));
7539 end if;
7541 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7543 -- The result is the new block
7545 Rewrite (N_Orig,
7546 Make_Block_Statement (Loc,
7547 Declarations => Decls,
7548 Handled_Statement_Sequence =>
7549 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7551 Analyze (N_Orig);
7552 end Expand_N_Asynchronous_Select;
7554 -------------------------------------
7555 -- Expand_N_Conditional_Entry_Call --
7556 -------------------------------------
7558 -- The conditional task entry call is converted to a call to
7559 -- Task_Entry_Call:
7561 -- declare
7562 -- B : Boolean;
7563 -- P : parms := (parm, parm, parm);
7565 -- begin
7566 -- Task_Entry_Call
7567 -- (<acceptor-task>, -- Acceptor
7568 -- <entry-index>, -- E
7569 -- P'Address, -- Uninterpreted_Data
7570 -- Conditional_Call, -- Mode
7571 -- B); -- Rendezvous_Successful
7572 -- parm := P.param;
7573 -- parm := P.param;
7574 -- ...
7575 -- if B then
7576 -- normal-statements
7577 -- else
7578 -- else-statements
7579 -- end if;
7580 -- end;
7582 -- For a description of the use of P and the assignments after the call,
7583 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7584 -- conditional entry call has already been expanded (by the Expand_N_Entry
7585 -- _Call_Statement procedure) as follows:
7587 -- declare
7588 -- P : parms := (parm, parm, parm);
7589 -- begin
7590 -- ... info for in-out parameters
7591 -- Call_Simple (acceptor-task, entry-index, P'Address);
7592 -- parm := P.param;
7593 -- parm := P.param;
7594 -- ...
7595 -- end;
7597 -- so the task at hand is to convert the latter expansion into the former
7599 -- The conditional protected entry call is converted to a call to
7600 -- Protected_Entry_Call:
7602 -- declare
7603 -- P : parms := (parm, parm, parm);
7604 -- Bnn : Communications_Block;
7606 -- begin
7607 -- Protected_Entry_Call
7608 -- (po._object'Access, -- Object
7609 -- <entry index>, -- E
7610 -- P'Address, -- Uninterpreted_Data
7611 -- Conditional_Call, -- Mode
7612 -- Bnn); -- Block
7613 -- parm := P.param;
7614 -- parm := P.param;
7615 -- ...
7616 -- if Cancelled (Bnn) then
7617 -- else-statements
7618 -- else
7619 -- normal-statements
7620 -- end if;
7621 -- end;
7623 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7624 -- into:
7626 -- declare
7627 -- B : Boolean := False;
7628 -- C : Ada.Tags.Prim_Op_Kind;
7629 -- K : Ada.Tags.Tagged_Kind :=
7630 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7631 -- P : Parameters := (Param1 .. ParamN);
7632 -- S : Integer;
7634 -- begin
7635 -- if K = Ada.Tags.TK_Limited_Tagged then
7636 -- <dispatching-call>;
7637 -- <triggering-statements>
7639 -- else
7640 -- S :=
7641 -- Ada.Tags.Get_Offset_Index
7642 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7644 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7646 -- if C = POK_Protected_Entry
7647 -- or else C = POK_Task_Entry
7648 -- then
7649 -- Param1 := P.Param1;
7650 -- ...
7651 -- ParamN := P.ParamN;
7652 -- end if;
7654 -- if B then
7655 -- if C = POK_Procedure
7656 -- or else C = POK_Protected_Procedure
7657 -- or else C = POK_Task_Procedure
7658 -- then
7659 -- <dispatching-call>;
7660 -- end if;
7662 -- <triggering-statements>
7663 -- else
7664 -- <else-statements>
7665 -- end if;
7666 -- end if;
7667 -- end;
7669 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7670 Loc : constant Source_Ptr := Sloc (N);
7671 Alt : constant Node_Id := Entry_Call_Alternative (N);
7672 Blk : Node_Id := Entry_Call_Statement (Alt);
7674 Actuals : List_Id;
7675 Blk_Typ : Entity_Id;
7676 Call : Node_Id;
7677 Call_Ent : Entity_Id;
7678 Conc_Typ_Stmts : List_Id;
7679 Decl : Node_Id;
7680 Decls : List_Id;
7681 Formals : List_Id;
7682 Lim_Typ_Stmts : List_Id;
7683 N_Stats : List_Id;
7684 Obj : Entity_Id;
7685 Param : Node_Id;
7686 Params : List_Id;
7687 Stmt : Node_Id;
7688 Stmts : List_Id;
7689 Transient_Blk : Node_Id;
7690 Unpack : List_Id;
7692 B : Entity_Id; -- Call status flag
7693 C : Entity_Id; -- Call kind
7694 K : Entity_Id; -- Tagged kind
7695 P : Entity_Id; -- Parameter block
7696 S : Entity_Id; -- Primitive operation slot
7698 begin
7699 Process_Statements_For_Controlled_Objects (N);
7701 if Ada_Version >= Ada_2005
7702 and then Nkind (Blk) = N_Procedure_Call_Statement
7703 then
7704 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7706 Decls := New_List;
7707 Stmts := New_List;
7709 -- Call status flag processing, generate:
7710 -- B : Boolean := False;
7712 B := Build_B (Loc, Decls);
7714 -- Call kind processing, generate:
7715 -- C : Ada.Tags.Prim_Op_Kind;
7717 C := Build_C (Loc, Decls);
7719 -- Tagged kind processing, generate:
7720 -- K : Ada.Tags.Tagged_Kind :=
7721 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7723 K := Build_K (Loc, Decls, Obj);
7725 -- Parameter block processing
7727 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7728 P := Parameter_Block_Pack
7729 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7731 -- Dispatch table slot processing, generate:
7732 -- S : Integer;
7734 S := Build_S (Loc, Decls);
7736 -- Generate:
7737 -- S := Ada.Tags.Get_Offset_Index
7738 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7740 Conc_Typ_Stmts :=
7741 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7743 -- Generate:
7744 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7746 Append_To (Conc_Typ_Stmts,
7747 Make_Procedure_Call_Statement (Loc,
7748 Name =>
7749 New_Reference_To (
7750 Find_Prim_Op (Etype (Etype (Obj)),
7751 Name_uDisp_Conditional_Select),
7752 Loc),
7753 Parameter_Associations =>
7754 New_List (
7755 New_Copy_Tree (Obj), -- <object>
7756 New_Reference_To (S, Loc), -- S
7757 Make_Attribute_Reference (Loc, -- P'Address
7758 Prefix => New_Reference_To (P, Loc),
7759 Attribute_Name => Name_Address),
7760 New_Reference_To (C, Loc), -- C
7761 New_Reference_To (B, Loc)))); -- B
7763 -- Generate:
7764 -- if C = POK_Protected_Entry
7765 -- or else C = POK_Task_Entry
7766 -- then
7767 -- Param1 := P.Param1;
7768 -- ...
7769 -- ParamN := P.ParamN;
7770 -- end if;
7772 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7774 -- Generate the if statement only when the packed parameters need
7775 -- explicit assignments to their corresponding actuals.
7777 if Present (Unpack) then
7778 Append_To (Conc_Typ_Stmts,
7779 Make_Implicit_If_Statement (N,
7780 Condition =>
7781 Make_Or_Else (Loc,
7782 Left_Opnd =>
7783 Make_Op_Eq (Loc,
7784 Left_Opnd =>
7785 New_Reference_To (C, Loc),
7786 Right_Opnd =>
7787 New_Reference_To (RTE (
7788 RE_POK_Protected_Entry), Loc)),
7790 Right_Opnd =>
7791 Make_Op_Eq (Loc,
7792 Left_Opnd =>
7793 New_Reference_To (C, Loc),
7794 Right_Opnd =>
7795 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
7797 Then_Statements => Unpack));
7798 end if;
7800 -- Generate:
7801 -- if B then
7802 -- if C = POK_Procedure
7803 -- or else C = POK_Protected_Procedure
7804 -- or else C = POK_Task_Procedure
7805 -- then
7806 -- <dispatching-call>
7807 -- end if;
7808 -- <normal-statements>
7809 -- else
7810 -- <else-statements>
7811 -- end if;
7813 N_Stats := New_Copy_List_Tree (Statements (Alt));
7815 Prepend_To (N_Stats,
7816 Make_Implicit_If_Statement (N,
7817 Condition =>
7818 Make_Or_Else (Loc,
7819 Left_Opnd =>
7820 Make_Op_Eq (Loc,
7821 Left_Opnd =>
7822 New_Reference_To (C, Loc),
7823 Right_Opnd =>
7824 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
7826 Right_Opnd =>
7827 Make_Or_Else (Loc,
7828 Left_Opnd =>
7829 Make_Op_Eq (Loc,
7830 Left_Opnd =>
7831 New_Reference_To (C, Loc),
7832 Right_Opnd =>
7833 New_Reference_To (RTE (
7834 RE_POK_Protected_Procedure), Loc)),
7836 Right_Opnd =>
7837 Make_Op_Eq (Loc,
7838 Left_Opnd =>
7839 New_Reference_To (C, Loc),
7840 Right_Opnd =>
7841 New_Reference_To (RTE (
7842 RE_POK_Task_Procedure), Loc)))),
7844 Then_Statements =>
7845 New_List (Blk)));
7847 Append_To (Conc_Typ_Stmts,
7848 Make_Implicit_If_Statement (N,
7849 Condition => New_Reference_To (B, Loc),
7850 Then_Statements => N_Stats,
7851 Else_Statements => Else_Statements (N)));
7853 -- Generate:
7854 -- <dispatching-call>;
7855 -- <triggering-statements>
7857 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7858 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7860 -- Generate:
7861 -- if K = Ada.Tags.TK_Limited_Tagged then
7862 -- Lim_Typ_Stmts
7863 -- else
7864 -- Conc_Typ_Stmts
7865 -- end if;
7867 Append_To (Stmts,
7868 Make_Implicit_If_Statement (N,
7869 Condition =>
7870 Make_Op_Eq (Loc,
7871 Left_Opnd =>
7872 New_Reference_To (K, Loc),
7873 Right_Opnd =>
7874 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7876 Then_Statements =>
7877 Lim_Typ_Stmts,
7879 Else_Statements =>
7880 Conc_Typ_Stmts));
7882 Rewrite (N,
7883 Make_Block_Statement (Loc,
7884 Declarations =>
7885 Decls,
7886 Handled_Statement_Sequence =>
7887 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7889 -- As described above, the entry alternative is transformed into a
7890 -- block that contains the gnulli call, and possibly assignment
7891 -- statements for in-out parameters. The gnulli call may itself be
7892 -- rewritten into a transient block if some unconstrained parameters
7893 -- require it. We need to retrieve the call to complete its parameter
7894 -- list.
7896 else
7897 Transient_Blk :=
7898 First_Real_Statement (Handled_Statement_Sequence (Blk));
7900 if Present (Transient_Blk)
7901 and then Nkind (Transient_Blk) = N_Block_Statement
7902 then
7903 Blk := Transient_Blk;
7904 end if;
7906 Stmts := Statements (Handled_Statement_Sequence (Blk));
7907 Stmt := First (Stmts);
7908 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7909 Next (Stmt);
7910 end loop;
7912 Call := Stmt;
7913 Params := Parameter_Associations (Call);
7915 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7917 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7919 Param := First (Params);
7920 while Present (Param)
7921 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7922 loop
7923 Next (Param);
7924 end loop;
7926 pragma Assert (Present (Param));
7927 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
7929 Analyze (Param);
7931 -- Find the Communication_Block parameter for the call to the
7932 -- Cancelled function.
7934 Decl := First (Declarations (Blk));
7935 while Present (Decl)
7936 and then not Is_RTE (Etype (Object_Definition (Decl)),
7937 RE_Communication_Block)
7938 loop
7939 Next (Decl);
7940 end loop;
7942 -- Add an if statement to execute the else part if the call
7943 -- does not succeed (as indicated by the Cancelled predicate).
7945 Append_To (Stmts,
7946 Make_Implicit_If_Statement (N,
7947 Condition => Make_Function_Call (Loc,
7948 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
7949 Parameter_Associations => New_List (
7950 New_Reference_To (Defining_Identifier (Decl), Loc))),
7951 Then_Statements => Else_Statements (N),
7952 Else_Statements => Statements (Alt)));
7954 else
7955 B := Make_Defining_Identifier (Loc, Name_uB);
7957 -- Insert declaration of B in declarations of existing block
7959 if No (Declarations (Blk)) then
7960 Set_Declarations (Blk, New_List);
7961 end if;
7963 Prepend_To (Declarations (Blk),
7964 Make_Object_Declaration (Loc,
7965 Defining_Identifier => B,
7966 Object_Definition =>
7967 New_Reference_To (Standard_Boolean, Loc)));
7969 -- Create new call statement
7971 Append_To (Params,
7972 New_Reference_To (RTE (RE_Conditional_Call), Loc));
7973 Append_To (Params, New_Reference_To (B, Loc));
7975 Rewrite (Call,
7976 Make_Procedure_Call_Statement (Loc,
7977 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7978 Parameter_Associations => Params));
7980 -- Construct statement sequence for new block
7982 Append_To (Stmts,
7983 Make_Implicit_If_Statement (N,
7984 Condition => New_Reference_To (B, Loc),
7985 Then_Statements => Statements (Alt),
7986 Else_Statements => Else_Statements (N)));
7987 end if;
7989 -- The result is the new block
7991 Rewrite (N,
7992 Make_Block_Statement (Loc,
7993 Declarations => Declarations (Blk),
7994 Handled_Statement_Sequence =>
7995 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7996 end if;
7998 Analyze (N);
7999 end Expand_N_Conditional_Entry_Call;
8001 ---------------------------------------
8002 -- Expand_N_Delay_Relative_Statement --
8003 ---------------------------------------
8005 -- Delay statement is implemented as a procedure call to Delay_For
8006 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8007 -- simple delays imposed by the use of Protected Objects.
8009 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8010 Loc : constant Source_Ptr := Sloc (N);
8011 begin
8012 Rewrite (N,
8013 Make_Procedure_Call_Statement (Loc,
8014 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
8015 Parameter_Associations => New_List (Expression (N))));
8016 Analyze (N);
8017 end Expand_N_Delay_Relative_Statement;
8019 ------------------------------------
8020 -- Expand_N_Delay_Until_Statement --
8021 ------------------------------------
8023 -- Delay Until statement is implemented as a procedure call to
8024 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8026 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8027 Loc : constant Source_Ptr := Sloc (N);
8028 Typ : Entity_Id;
8030 begin
8031 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8032 Typ := RTE (RO_CA_Delay_Until);
8033 else
8034 Typ := RTE (RO_RT_Delay_Until);
8035 end if;
8037 Rewrite (N,
8038 Make_Procedure_Call_Statement (Loc,
8039 Name => New_Reference_To (Typ, Loc),
8040 Parameter_Associations => New_List (Expression (N))));
8042 Analyze (N);
8043 end Expand_N_Delay_Until_Statement;
8045 -------------------------
8046 -- Expand_N_Entry_Body --
8047 -------------------------
8049 procedure Expand_N_Entry_Body (N : Node_Id) is
8050 begin
8051 -- Associate discriminals with the next protected operation body to be
8052 -- expanded.
8054 if Present (Next_Protected_Operation (N)) then
8055 Set_Discriminals (Parent (Current_Scope));
8056 end if;
8057 end Expand_N_Entry_Body;
8059 -----------------------------------
8060 -- Expand_N_Entry_Call_Statement --
8061 -----------------------------------
8063 -- An entry call is expanded into GNARLI calls to implement a simple entry
8064 -- call (see Build_Simple_Entry_Call).
8066 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8067 Concval : Node_Id;
8068 Ename : Node_Id;
8069 Index : Node_Id;
8071 begin
8072 if No_Run_Time_Mode then
8073 Error_Msg_CRT ("entry call", N);
8074 return;
8075 end if;
8077 -- If this entry call is part of an asynchronous select, don't expand it
8078 -- here; it will be expanded with the select statement. Don't expand
8079 -- timed entry calls either, as they are translated into asynchronous
8080 -- entry calls.
8082 -- ??? This whole approach is questionable; it may be better to go back
8083 -- to allowing the expansion to take place and then attempting to fix it
8084 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8085 -- whether the expanded call is on a task or protected entry.
8087 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8088 or else N /= Triggering_Statement (Parent (N)))
8089 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8090 or else N /= Entry_Call_Statement (Parent (N))
8091 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8092 then
8093 Extract_Entry (N, Concval, Ename, Index);
8094 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8095 end if;
8096 end Expand_N_Entry_Call_Statement;
8098 --------------------------------
8099 -- Expand_N_Entry_Declaration --
8100 --------------------------------
8102 -- If there are parameters, then first, each of the formals is marked by
8103 -- setting Is_Entry_Formal. Next a record type is built which is used to
8104 -- hold the parameter values. The name of this record type is entryP where
8105 -- entry is the name of the entry, with an additional corresponding access
8106 -- type called entryPA. The record type has matching components for each
8107 -- formal (the component names are the same as the formal names). For
8108 -- elementary types, the component type matches the formal type. For
8109 -- composite types, an access type is declared (with the name formalA)
8110 -- which designates the formal type, and the type of the component is this
8111 -- access type. Finally the Entry_Component of each formal is set to
8112 -- reference the corresponding record component.
8114 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8115 Loc : constant Source_Ptr := Sloc (N);
8116 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8117 Components : List_Id;
8118 Formal : Node_Id;
8119 Ftype : Entity_Id;
8120 Last_Decl : Node_Id;
8121 Component : Entity_Id;
8122 Ctype : Entity_Id;
8123 Decl : Node_Id;
8124 Rec_Ent : Entity_Id;
8125 Acc_Ent : Entity_Id;
8127 begin
8128 Formal := First_Formal (Entry_Ent);
8129 Last_Decl := N;
8131 -- Most processing is done only if parameters are present
8133 if Present (Formal) then
8134 Components := New_List;
8136 -- Loop through formals
8138 while Present (Formal) loop
8139 Set_Is_Entry_Formal (Formal);
8140 Component :=
8141 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8142 Set_Entry_Component (Formal, Component);
8143 Set_Entry_Formal (Component, Formal);
8144 Ftype := Etype (Formal);
8146 -- Declare new access type and then append
8148 Ctype := Make_Temporary (Loc, 'A');
8150 Decl :=
8151 Make_Full_Type_Declaration (Loc,
8152 Defining_Identifier => Ctype,
8153 Type_Definition =>
8154 Make_Access_To_Object_Definition (Loc,
8155 All_Present => True,
8156 Constant_Present => Ekind (Formal) = E_In_Parameter,
8157 Subtype_Indication => New_Reference_To (Ftype, Loc)));
8159 Insert_After (Last_Decl, Decl);
8160 Last_Decl := Decl;
8162 Append_To (Components,
8163 Make_Component_Declaration (Loc,
8164 Defining_Identifier => Component,
8165 Component_Definition =>
8166 Make_Component_Definition (Loc,
8167 Aliased_Present => False,
8168 Subtype_Indication => New_Reference_To (Ctype, Loc))));
8170 Next_Formal_With_Extras (Formal);
8171 end loop;
8173 -- Create the Entry_Parameter_Record declaration
8175 Rec_Ent := Make_Temporary (Loc, 'P');
8177 Decl :=
8178 Make_Full_Type_Declaration (Loc,
8179 Defining_Identifier => Rec_Ent,
8180 Type_Definition =>
8181 Make_Record_Definition (Loc,
8182 Component_List =>
8183 Make_Component_List (Loc,
8184 Component_Items => Components)));
8186 Insert_After (Last_Decl, Decl);
8187 Last_Decl := Decl;
8189 -- Construct and link in the corresponding access type
8191 Acc_Ent := Make_Temporary (Loc, 'A');
8193 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8195 Decl :=
8196 Make_Full_Type_Declaration (Loc,
8197 Defining_Identifier => Acc_Ent,
8198 Type_Definition =>
8199 Make_Access_To_Object_Definition (Loc,
8200 All_Present => True,
8201 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
8203 Insert_After (Last_Decl, Decl);
8204 end if;
8205 end Expand_N_Entry_Declaration;
8207 -----------------------------
8208 -- Expand_N_Protected_Body --
8209 -----------------------------
8211 -- Protected bodies are expanded to the completion of the subprograms
8212 -- created for the corresponding protected type. These are a protected and
8213 -- unprotected version of each protected subprogram in the object, a
8214 -- function to calculate each entry barrier, and a procedure to execute the
8215 -- sequence of statements of each protected entry body. For example, for
8216 -- protected type ptype:
8218 -- function entB
8219 -- (O : System.Address;
8220 -- E : Protected_Entry_Index)
8221 -- return Boolean
8222 -- is
8223 -- <discriminant renamings>
8224 -- <private object renamings>
8225 -- begin
8226 -- return <barrier expression>;
8227 -- end entB;
8229 -- procedure pprocN (_object : in out poV;...) is
8230 -- <discriminant renamings>
8231 -- <private object renamings>
8232 -- begin
8233 -- <sequence of statements>
8234 -- end pprocN;
8236 -- procedure pprocP (_object : in out poV;...) is
8237 -- procedure _clean is
8238 -- Pn : Boolean;
8239 -- begin
8240 -- ptypeS (_object, Pn);
8241 -- Unlock (_object._object'Access);
8242 -- Abort_Undefer.all;
8243 -- end _clean;
8245 -- begin
8246 -- Abort_Defer.all;
8247 -- Lock (_object._object'Access);
8248 -- pprocN (_object;...);
8249 -- at end
8250 -- _clean;
8251 -- end pproc;
8253 -- function pfuncN (_object : poV;...) return Return_Type is
8254 -- <discriminant renamings>
8255 -- <private object renamings>
8256 -- begin
8257 -- <sequence of statements>
8258 -- end pfuncN;
8260 -- function pfuncP (_object : poV) return Return_Type is
8261 -- procedure _clean is
8262 -- begin
8263 -- Unlock (_object._object'Access);
8264 -- Abort_Undefer.all;
8265 -- end _clean;
8267 -- begin
8268 -- Abort_Defer.all;
8269 -- Lock (_object._object'Access);
8270 -- return pfuncN (_object);
8272 -- at end
8273 -- _clean;
8274 -- end pfunc;
8276 -- procedure entE
8277 -- (O : System.Address;
8278 -- P : System.Address;
8279 -- E : Protected_Entry_Index)
8280 -- is
8281 -- <discriminant renamings>
8282 -- <private object renamings>
8283 -- type poVP is access poV;
8284 -- _Object : ptVP := ptVP!(O);
8286 -- begin
8287 -- begin
8288 -- <statement sequence>
8289 -- Complete_Entry_Body (_Object._Object);
8290 -- exception
8291 -- when all others =>
8292 -- Exceptional_Complete_Entry_Body (
8293 -- _Object._Object, Get_GNAT_Exception);
8294 -- end;
8295 -- end entE;
8297 -- The type poV is the record created for the protected type to hold
8298 -- the state of the protected object.
8300 procedure Expand_N_Protected_Body (N : Node_Id) is
8301 Loc : constant Source_Ptr := Sloc (N);
8302 Pid : constant Entity_Id := Corresponding_Spec (N);
8304 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8305 -- This flag indicates whether the lock free implementation is active
8307 Current_Node : Node_Id;
8308 Disp_Op_Body : Node_Id;
8309 New_Op_Body : Node_Id;
8310 Num_Entries : Natural := 0;
8311 Op_Body : Node_Id;
8312 Op_Id : Entity_Id;
8314 function Build_Dispatching_Subprogram_Body
8315 (N : Node_Id;
8316 Pid : Node_Id;
8317 Prot_Bod : Node_Id) return Node_Id;
8318 -- Build a dispatching version of the protected subprogram body. The
8319 -- newly generated subprogram contains a call to the original protected
8320 -- body. The following code is generated:
8322 -- function <protected-function-name> (Param1 .. ParamN) return
8323 -- <return-type> is
8324 -- begin
8325 -- return <protected-function-name>P (Param1 .. ParamN);
8326 -- end <protected-function-name>;
8328 -- or
8330 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8331 -- begin
8332 -- <protected-procedure-name>P (Param1 .. ParamN);
8333 -- end <protected-procedure-name>
8335 ---------------------------------------
8336 -- Build_Dispatching_Subprogram_Body --
8337 ---------------------------------------
8339 function Build_Dispatching_Subprogram_Body
8340 (N : Node_Id;
8341 Pid : Node_Id;
8342 Prot_Bod : Node_Id) return Node_Id
8344 Loc : constant Source_Ptr := Sloc (N);
8345 Actuals : List_Id;
8346 Formal : Node_Id;
8347 Spec : Node_Id;
8348 Stmts : List_Id;
8350 begin
8351 -- Generate a specification without a letter suffix in order to
8352 -- override an interface function or procedure.
8354 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8356 -- The formal parameters become the actuals of the protected function
8357 -- or procedure call.
8359 Actuals := New_List;
8360 Formal := First (Parameter_Specifications (Spec));
8361 while Present (Formal) loop
8362 Append_To (Actuals,
8363 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8364 Next (Formal);
8365 end loop;
8367 if Nkind (Spec) = N_Procedure_Specification then
8368 Stmts :=
8369 New_List (
8370 Make_Procedure_Call_Statement (Loc,
8371 Name =>
8372 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8373 Parameter_Associations => Actuals));
8375 else
8376 pragma Assert (Nkind (Spec) = N_Function_Specification);
8378 Stmts :=
8379 New_List (
8380 Make_Simple_Return_Statement (Loc,
8381 Expression =>
8382 Make_Function_Call (Loc,
8383 Name =>
8384 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8385 Parameter_Associations => Actuals)));
8386 end if;
8388 return
8389 Make_Subprogram_Body (Loc,
8390 Declarations => Empty_List,
8391 Specification => Spec,
8392 Handled_Statement_Sequence =>
8393 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8394 end Build_Dispatching_Subprogram_Body;
8396 -- Start of processing for Expand_N_Protected_Body
8398 begin
8399 if No_Run_Time_Mode then
8400 Error_Msg_CRT ("protected body", N);
8401 return;
8402 end if;
8404 -- This is the proper body corresponding to a stub. The declarations
8405 -- must be inserted at the point of the stub, which in turn is in the
8406 -- declarative part of the parent unit.
8408 if Nkind (Parent (N)) = N_Subunit then
8409 Current_Node := Corresponding_Stub (Parent (N));
8410 else
8411 Current_Node := N;
8412 end if;
8414 Op_Body := First (Declarations (N));
8416 -- The protected body is replaced with the bodies of its
8417 -- protected operations, and the declarations for internal objects
8418 -- that may have been created for entry family bounds.
8420 Rewrite (N, Make_Null_Statement (Sloc (N)));
8421 Analyze (N);
8423 while Present (Op_Body) loop
8424 case Nkind (Op_Body) is
8425 when N_Subprogram_Declaration =>
8426 null;
8428 when N_Subprogram_Body =>
8430 -- Do not create bodies for eliminated operations
8432 if not Is_Eliminated (Defining_Entity (Op_Body))
8433 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8434 then
8435 if Lock_Free_Active then
8436 New_Op_Body :=
8437 Build_Lock_Free_Unprotected_Subprogram_Body
8438 (Op_Body, Pid);
8439 else
8440 New_Op_Body :=
8441 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8442 end if;
8444 Insert_After (Current_Node, New_Op_Body);
8445 Current_Node := New_Op_Body;
8446 Analyze (New_Op_Body);
8448 -- Build the corresponding protected operation. It may
8449 -- appear that this is needed only if this is a visible
8450 -- operation of the type, or if it is an interrupt handler,
8451 -- and this was the strategy used previously in GNAT.
8453 -- However, the operation may be exported through a 'Access
8454 -- to an external caller. This is the common idiom in code
8455 -- that uses the Ada 2005 Timing_Events package. As a result
8456 -- we need to produce the protected body for both visible
8457 -- and private operations, as well as operations that only
8458 -- have a body in the source, and for which we create a
8459 -- declaration in the protected body itself.
8461 if Present (Corresponding_Spec (Op_Body)) then
8462 if Lock_Free_Active then
8463 New_Op_Body :=
8464 Build_Lock_Free_Protected_Subprogram_Body
8465 (Op_Body, Pid, Specification (New_Op_Body));
8466 else
8467 New_Op_Body :=
8468 Build_Protected_Subprogram_Body
8469 (Op_Body, Pid, Specification (New_Op_Body));
8470 end if;
8472 Insert_After (Current_Node, New_Op_Body);
8473 Analyze (New_Op_Body);
8475 Current_Node := New_Op_Body;
8477 -- Generate an overriding primitive operation body for
8478 -- this subprogram if the protected type implements an
8479 -- interface.
8481 if Ada_Version >= Ada_2005
8482 and then
8483 Present (Interfaces (Corresponding_Record_Type (Pid)))
8484 then
8485 Disp_Op_Body :=
8486 Build_Dispatching_Subprogram_Body
8487 (Op_Body, Pid, New_Op_Body);
8489 Insert_After (Current_Node, Disp_Op_Body);
8490 Analyze (Disp_Op_Body);
8492 Current_Node := Disp_Op_Body;
8493 end if;
8494 end if;
8495 end if;
8497 when N_Entry_Body =>
8498 Op_Id := Defining_Identifier (Op_Body);
8499 Num_Entries := Num_Entries + 1;
8501 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8503 Insert_After (Current_Node, New_Op_Body);
8504 Current_Node := New_Op_Body;
8505 Analyze (New_Op_Body);
8507 when N_Implicit_Label_Declaration =>
8508 null;
8510 when N_Itype_Reference =>
8511 Insert_After (Current_Node, New_Copy (Op_Body));
8513 when N_Freeze_Entity =>
8514 New_Op_Body := New_Copy (Op_Body);
8516 if Present (Entity (Op_Body))
8517 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8518 then
8519 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8520 end if;
8522 Insert_After (Current_Node, New_Op_Body);
8523 Current_Node := New_Op_Body;
8524 Analyze (New_Op_Body);
8526 when N_Pragma =>
8527 New_Op_Body := New_Copy (Op_Body);
8528 Insert_After (Current_Node, New_Op_Body);
8529 Current_Node := New_Op_Body;
8530 Analyze (New_Op_Body);
8532 when N_Object_Declaration =>
8533 pragma Assert (not Comes_From_Source (Op_Body));
8534 New_Op_Body := New_Copy (Op_Body);
8535 Insert_After (Current_Node, New_Op_Body);
8536 Current_Node := New_Op_Body;
8537 Analyze (New_Op_Body);
8539 when others =>
8540 raise Program_Error;
8542 end case;
8544 Next (Op_Body);
8545 end loop;
8547 -- Finally, create the body of the function that maps an entry index
8548 -- into the corresponding body index, except when there is no entry, or
8549 -- in a Ravenscar-like profile.
8551 if Corresponding_Runtime_Package (Pid) =
8552 System_Tasking_Protected_Objects_Entries
8553 then
8554 New_Op_Body := Build_Find_Body_Index (Pid);
8555 Insert_After (Current_Node, New_Op_Body);
8556 Current_Node := New_Op_Body;
8557 Analyze (New_Op_Body);
8558 end if;
8560 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8561 -- protected body. At this point all wrapper specs have been created,
8562 -- frozen and included in the dispatch table for the protected type.
8564 if Ada_Version >= Ada_2005 then
8565 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8566 end if;
8567 end Expand_N_Protected_Body;
8569 -----------------------------------------
8570 -- Expand_N_Protected_Type_Declaration --
8571 -----------------------------------------
8573 -- First we create a corresponding record type declaration used to
8574 -- represent values of this protected type.
8575 -- The general form of this type declaration is
8577 -- type poV (discriminants) is record
8578 -- _Object : aliased <kind>Protection
8579 -- [(<entry count> [, <handler count>])];
8580 -- [entry_family : array (bounds) of Void;]
8581 -- <private data fields>
8582 -- end record;
8584 -- The discriminants are present only if the corresponding protected type
8585 -- has discriminants, and they exactly mirror the protected type
8586 -- discriminants. The private data fields similarly mirror the private
8587 -- declarations of the protected type.
8589 -- The Object field is always present. It contains RTS specific data used
8590 -- to control the protected object. It is declared as Aliased so that it
8591 -- can be passed as a pointer to the RTS. This allows the protected record
8592 -- to be referenced within RTS data structures. An appropriate Protection
8593 -- type and discriminant are generated.
8595 -- The Service field is present for protected objects with entries. It
8596 -- contains sufficient information to allow the entry service procedure for
8597 -- this object to be called when the object is not known till runtime.
8599 -- One entry_family component is present for each entry family in the
8600 -- task definition (see Expand_N_Task_Type_Declaration).
8602 -- When a protected object is declared, an instance of the protected type
8603 -- value record is created. The elaboration of this declaration creates the
8604 -- correct bounds for the entry families, and also evaluates the priority
8605 -- expression if needed. The initialization routine for the protected type
8606 -- itself then calls Initialize_Protection with appropriate parameters to
8607 -- initialize the value of the Task_Id field. Install_Handlers may be also
8608 -- called if a pragma Attach_Handler applies.
8610 -- Note: this record is passed to the subprograms created by the expansion
8611 -- of protected subprograms and entries. It is an in parameter to protected
8612 -- functions and an in out parameter to procedures and entry bodies. The
8613 -- Entity_Id for this created record type is placed in the
8614 -- Corresponding_Record_Type field of the associated protected type entity.
8616 -- Next we create a procedure specifications for protected subprograms and
8617 -- entry bodies. For each protected subprograms two subprograms are
8618 -- created, an unprotected and a protected version. The unprotected version
8619 -- is called from within other operations of the same protected object.
8621 -- We also build the call to register the procedure if a pragma
8622 -- Interrupt_Handler applies.
8624 -- A single subprogram is created to service all entry bodies; it has an
8625 -- additional boolean out parameter indicating that the previous entry call
8626 -- made by the current task was serviced immediately, i.e. not by proxy.
8627 -- The O parameter contains a pointer to a record object of the type
8628 -- described above. An untyped interface is used here to allow this
8629 -- procedure to be called in places where the type of the object to be
8630 -- serviced is not known. This must be done, for example, when a call that
8631 -- may have been requeued is cancelled; the corresponding object must be
8632 -- serviced, but which object that is not known till runtime.
8634 -- procedure ptypeS
8635 -- (O : System.Address; P : out Boolean);
8636 -- procedure pprocN (_object : in out poV);
8637 -- procedure pproc (_object : in out poV);
8638 -- function pfuncN (_object : poV);
8639 -- function pfunc (_object : poV);
8640 -- ...
8642 -- Note that this must come after the record type declaration, since
8643 -- the specs refer to this type.
8645 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8646 Loc : constant Source_Ptr := Sloc (N);
8647 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8649 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8650 -- This flag indicates whether the lock free implementation is active
8652 Pdef : constant Node_Id := Protected_Definition (N);
8653 -- This contains two lists; one for visible and one for private decls
8655 Rec_Decl : Node_Id;
8656 Cdecls : List_Id;
8657 Discr_Map : constant Elist_Id := New_Elmt_List;
8658 Priv : Node_Id;
8659 New_Priv : Node_Id;
8660 Comp : Node_Id;
8661 Comp_Id : Entity_Id;
8662 Sub : Node_Id;
8663 Current_Node : Node_Id := N;
8664 Bdef : Entity_Id := Empty; -- avoid uninit warning
8665 Edef : Entity_Id := Empty; -- avoid uninit warning
8666 Entries_Aggr : Node_Id;
8667 Body_Id : Entity_Id;
8668 Body_Arr : Node_Id;
8669 E_Count : Int;
8670 Object_Comp : Node_Id;
8672 procedure Check_Inlining (Subp : Entity_Id);
8673 -- If the original operation has a pragma Inline, propagate the flag
8674 -- to the internal body, for possible inlining later on. The source
8675 -- operation is invisible to the back-end and is never actually called.
8677 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8678 -- When compiling under the Ravenscar profile, private components must
8679 -- have a static size, or else a protected object will require heap
8680 -- allocation, violating the corresponding restriction. It is preferable
8681 -- to make this check here, because it provides a better error message
8682 -- than the back-end, which refers to the object as a whole.
8684 procedure Register_Handler;
8685 -- For a protected operation that is an interrupt handler, add the
8686 -- freeze action that will register it as such.
8688 --------------------
8689 -- Check_Inlining --
8690 --------------------
8692 procedure Check_Inlining (Subp : Entity_Id) is
8693 begin
8694 if Is_Inlined (Subp) then
8695 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8696 Set_Is_Inlined (Subp, False);
8697 end if;
8698 end Check_Inlining;
8700 ---------------------------------
8701 -- Check_Static_Component_Size --
8702 ---------------------------------
8704 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8705 Typ : constant Entity_Id := Etype (Comp);
8706 C : Entity_Id;
8708 begin
8709 if Is_Scalar_Type (Typ) then
8710 return True;
8712 elsif Is_Array_Type (Typ) then
8713 return Compile_Time_Known_Bounds (Typ);
8715 elsif Is_Record_Type (Typ) then
8716 C := First_Component (Typ);
8717 while Present (C) loop
8718 if not Static_Component_Size (C) then
8719 return False;
8720 end if;
8722 Next_Component (C);
8723 end loop;
8725 return True;
8727 -- Any other type will be checked by the back-end
8729 else
8730 return True;
8731 end if;
8732 end Static_Component_Size;
8734 ----------------------
8735 -- Register_Handler --
8736 ----------------------
8738 procedure Register_Handler is
8740 -- All semantic checks already done in Sem_Prag
8742 Prot_Proc : constant Entity_Id :=
8743 Defining_Unit_Name (Specification (Current_Node));
8745 Proc_Address : constant Node_Id :=
8746 Make_Attribute_Reference (Loc,
8747 Prefix =>
8748 New_Reference_To (Prot_Proc, Loc),
8749 Attribute_Name => Name_Address);
8751 RTS_Call : constant Entity_Id :=
8752 Make_Procedure_Call_Statement (Loc,
8753 Name =>
8754 New_Reference_To
8755 (RTE (RE_Register_Interrupt_Handler), Loc),
8756 Parameter_Associations => New_List (Proc_Address));
8757 begin
8758 Append_Freeze_Action (Prot_Proc, RTS_Call);
8759 end Register_Handler;
8761 -- Start of processing for Expand_N_Protected_Type_Declaration
8763 begin
8764 if Present (Corresponding_Record_Type (Prot_Typ)) then
8765 return;
8766 else
8767 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8768 end if;
8770 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8772 Qualify_Entity_Names (N);
8774 -- If the type has discriminants, their occurrences in the declaration
8775 -- have been replaced by the corresponding discriminals. For components
8776 -- that are constrained by discriminants, their homologues in the
8777 -- corresponding record type must refer to the discriminants of that
8778 -- record, so we must apply a new renaming to subtypes_indications:
8780 -- protected discriminant => discriminal => record discriminant
8782 -- This replacement is not applied to default expressions, for which
8783 -- the discriminal is correct.
8785 if Has_Discriminants (Prot_Typ) then
8786 declare
8787 Disc : Entity_Id;
8788 Decl : Node_Id;
8790 begin
8791 Disc := First_Discriminant (Prot_Typ);
8792 Decl := First (Discriminant_Specifications (Rec_Decl));
8793 while Present (Disc) loop
8794 Append_Elmt (Discriminal (Disc), Discr_Map);
8795 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8796 Next_Discriminant (Disc);
8797 Next (Decl);
8798 end loop;
8799 end;
8800 end if;
8802 -- Fill in the component declarations
8804 -- Add components for entry families. For each entry family, create an
8805 -- anonymous type declaration with the same size, and analyze the type.
8807 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8809 pragma Assert (Present (Pdef));
8811 -- Add private field components
8813 if Present (Private_Declarations (Pdef)) then
8814 Priv := First (Private_Declarations (Pdef));
8815 while Present (Priv) loop
8816 if Nkind (Priv) = N_Component_Declaration then
8817 if not Static_Component_Size (Defining_Identifier (Priv)) then
8819 -- When compiling for a restricted profile, the private
8820 -- components must have a static size. If not, this is an
8821 -- error for a single protected declaration, and rates a
8822 -- warning on a protected type declaration.
8824 if not Comes_From_Source (Prot_Typ) then
8825 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8827 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8828 Error_Msg_N ("component has non-static size??", Priv);
8829 Error_Msg_NE
8830 ("\creation of protected object of type& will violate"
8831 & " restriction No_Implicit_Heap_Allocations??",
8832 Priv, Prot_Typ);
8833 end if;
8834 end if;
8836 -- The component definition consists of a subtype indication,
8837 -- or (in Ada 2005) an access definition. Make a copy of the
8838 -- proper definition.
8840 declare
8841 Old_Comp : constant Node_Id := Component_Definition (Priv);
8842 Oent : constant Entity_Id := Defining_Identifier (Priv);
8843 New_Comp : Node_Id;
8844 Nent : constant Entity_Id :=
8845 Make_Defining_Identifier (Sloc (Oent),
8846 Chars => Chars (Oent));
8848 begin
8849 if Present (Subtype_Indication (Old_Comp)) then
8850 New_Comp :=
8851 Make_Component_Definition (Sloc (Oent),
8852 Aliased_Present => False,
8853 Subtype_Indication =>
8854 New_Copy_Tree (Subtype_Indication (Old_Comp),
8855 Discr_Map));
8856 else
8857 New_Comp :=
8858 Make_Component_Definition (Sloc (Oent),
8859 Aliased_Present => False,
8860 Access_Definition =>
8861 New_Copy_Tree (Access_Definition (Old_Comp),
8862 Discr_Map));
8863 end if;
8865 New_Priv :=
8866 Make_Component_Declaration (Loc,
8867 Defining_Identifier => Nent,
8868 Component_Definition => New_Comp,
8869 Expression => Expression (Priv));
8871 Set_Has_Per_Object_Constraint (Nent,
8872 Has_Per_Object_Constraint (Oent));
8874 Append_To (Cdecls, New_Priv);
8875 end;
8877 elsif Nkind (Priv) = N_Subprogram_Declaration then
8879 -- Make the unprotected version of the subprogram available
8880 -- for expansion of intra object calls. There is need for
8881 -- a protected version only if the subprogram is an interrupt
8882 -- handler, otherwise this operation can only be called from
8883 -- within the body.
8885 Sub :=
8886 Make_Subprogram_Declaration (Loc,
8887 Specification =>
8888 Build_Protected_Sub_Specification
8889 (Priv, Prot_Typ, Unprotected_Mode));
8891 Insert_After (Current_Node, Sub);
8892 Analyze (Sub);
8894 Set_Protected_Body_Subprogram
8895 (Defining_Unit_Name (Specification (Priv)),
8896 Defining_Unit_Name (Specification (Sub)));
8897 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
8898 Current_Node := Sub;
8900 Sub :=
8901 Make_Subprogram_Declaration (Loc,
8902 Specification =>
8903 Build_Protected_Sub_Specification
8904 (Priv, Prot_Typ, Protected_Mode));
8906 Insert_After (Current_Node, Sub);
8907 Analyze (Sub);
8908 Current_Node := Sub;
8910 if Is_Interrupt_Handler
8911 (Defining_Unit_Name (Specification (Priv)))
8912 then
8913 if not Restricted_Profile then
8914 Register_Handler;
8915 end if;
8916 end if;
8917 end if;
8919 Next (Priv);
8920 end loop;
8921 end if;
8923 -- Except for the lock-free implementation, prepend the _Object field
8924 -- with the right type to the component list. We need to compute the
8925 -- number of entries, and in some cases the number of Attach_Handler
8926 -- pragmas.
8928 if not Lock_Free_Active then
8929 declare
8930 Ritem : Node_Id;
8931 Num_Attach_Handler : Int := 0;
8932 Protection_Subtype : Node_Id;
8933 Entry_Count_Expr : constant Node_Id :=
8934 Build_Entry_Count_Expression
8935 (Prot_Typ, Cdecls, Loc);
8937 begin
8938 -- Could this be simplified using Corresponding_Runtime_Package???
8940 if Has_Attach_Handler (Prot_Typ) then
8941 Ritem := First_Rep_Item (Prot_Typ);
8942 while Present (Ritem) loop
8943 if Nkind (Ritem) = N_Pragma
8944 and then Pragma_Name (Ritem) = Name_Attach_Handler
8945 then
8946 Num_Attach_Handler := Num_Attach_Handler + 1;
8947 end if;
8949 Next_Rep_Item (Ritem);
8950 end loop;
8952 if Restricted_Profile then
8953 if Has_Entries (Prot_Typ) then
8954 Protection_Subtype :=
8955 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8956 else
8957 Protection_Subtype :=
8958 New_Reference_To (RTE (RE_Protection), Loc);
8959 end if;
8961 else
8962 Protection_Subtype :=
8963 Make_Subtype_Indication (Loc,
8964 Subtype_Mark =>
8965 New_Reference_To
8966 (RTE (RE_Static_Interrupt_Protection), Loc),
8967 Constraint =>
8968 Make_Index_Or_Discriminant_Constraint (Loc,
8969 Constraints => New_List (
8970 Entry_Count_Expr,
8971 Make_Integer_Literal (Loc, Num_Attach_Handler))));
8972 end if;
8974 elsif Has_Interrupt_Handler (Prot_Typ)
8975 and then not Restriction_Active (No_Dynamic_Attachment)
8976 then
8977 Protection_Subtype :=
8978 Make_Subtype_Indication (Loc,
8979 Subtype_Mark =>
8980 New_Reference_To
8981 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
8982 Constraint =>
8983 Make_Index_Or_Discriminant_Constraint (Loc,
8984 Constraints => New_List (Entry_Count_Expr)));
8986 -- Type has explicit entries or generated primitive entry wrappers
8988 elsif Has_Entries (Prot_Typ)
8989 or else (Ada_Version >= Ada_2005
8990 and then Present (Interface_List (N)))
8991 then
8992 case Corresponding_Runtime_Package (Prot_Typ) is
8993 when System_Tasking_Protected_Objects_Entries =>
8994 Protection_Subtype :=
8995 Make_Subtype_Indication (Loc,
8996 Subtype_Mark =>
8997 New_Reference_To
8998 (RTE (RE_Protection_Entries), Loc),
8999 Constraint =>
9000 Make_Index_Or_Discriminant_Constraint (Loc,
9001 Constraints => New_List (Entry_Count_Expr)));
9003 when System_Tasking_Protected_Objects_Single_Entry =>
9004 Protection_Subtype :=
9005 New_Reference_To (RTE (RE_Protection_Entry), Loc);
9007 when others =>
9008 raise Program_Error;
9009 end case;
9011 else
9012 Protection_Subtype :=
9013 New_Reference_To (RTE (RE_Protection), Loc);
9014 end if;
9016 Object_Comp :=
9017 Make_Component_Declaration (Loc,
9018 Defining_Identifier =>
9019 Make_Defining_Identifier (Loc, Name_uObject),
9020 Component_Definition =>
9021 Make_Component_Definition (Loc,
9022 Aliased_Present => True,
9023 Subtype_Indication => Protection_Subtype));
9024 end;
9026 -- Put the _Object component after the private component so that it
9027 -- be finalized early as required by 9.4 (20)
9029 Append_To (Cdecls, Object_Comp);
9030 end if;
9032 Insert_After (Current_Node, Rec_Decl);
9033 Current_Node := Rec_Decl;
9035 -- Analyze the record declaration immediately after construction,
9036 -- because the initialization procedure is needed for single object
9037 -- declarations before the next entity is analyzed (the freeze call
9038 -- that generates this initialization procedure is found below).
9040 Analyze (Rec_Decl, Suppress => All_Checks);
9042 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9043 -- the corresponding record is frozen. If any wrappers are generated,
9044 -- Current_Node is updated accordingly.
9046 if Ada_Version >= Ada_2005 then
9047 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9048 end if;
9050 -- Collect pointers to entry bodies and their barriers, to be placed
9051 -- in the Entry_Bodies_Array for the type. For each entry/family we
9052 -- add an expression to the aggregate which is the initial value of
9053 -- this array. The array is declared after all protected subprograms.
9055 if Has_Entries (Prot_Typ) then
9056 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9057 else
9058 Entries_Aggr := Empty;
9059 end if;
9061 -- Build two new procedure specifications for each protected subprogram;
9062 -- one to call from outside the object and one to call from inside.
9063 -- Build a barrier function and an entry body action procedure
9064 -- specification for each protected entry. Initialize the entry body
9065 -- array. If subprogram is flagged as eliminated, do not generate any
9066 -- internal operations.
9068 E_Count := 0;
9069 Comp := First (Visible_Declarations (Pdef));
9070 while Present (Comp) loop
9071 if Nkind (Comp) = N_Subprogram_Declaration then
9072 Sub :=
9073 Make_Subprogram_Declaration (Loc,
9074 Specification =>
9075 Build_Protected_Sub_Specification
9076 (Comp, Prot_Typ, Unprotected_Mode));
9078 Insert_After (Current_Node, Sub);
9079 Analyze (Sub);
9081 Set_Protected_Body_Subprogram
9082 (Defining_Unit_Name (Specification (Comp)),
9083 Defining_Unit_Name (Specification (Sub)));
9084 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9086 -- Make the protected version of the subprogram available for
9087 -- expansion of external calls.
9089 Current_Node := Sub;
9091 Sub :=
9092 Make_Subprogram_Declaration (Loc,
9093 Specification =>
9094 Build_Protected_Sub_Specification
9095 (Comp, Prot_Typ, Protected_Mode));
9097 Insert_After (Current_Node, Sub);
9098 Analyze (Sub);
9100 Current_Node := Sub;
9102 -- Generate an overriding primitive operation specification for
9103 -- this subprogram if the protected type implements an interface.
9105 if Ada_Version >= Ada_2005
9106 and then
9107 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9108 then
9109 Sub :=
9110 Make_Subprogram_Declaration (Loc,
9111 Specification =>
9112 Build_Protected_Sub_Specification
9113 (Comp, Prot_Typ, Dispatching_Mode));
9115 Insert_After (Current_Node, Sub);
9116 Analyze (Sub);
9118 Current_Node := Sub;
9119 end if;
9121 -- If a pragma Interrupt_Handler applies, build and add a call to
9122 -- Register_Interrupt_Handler to the freezing actions of the
9123 -- protected version (Current_Node) of the subprogram:
9125 -- system.interrupts.register_interrupt_handler
9126 -- (prot_procP'address);
9128 if not Restricted_Profile
9129 and then Is_Interrupt_Handler
9130 (Defining_Unit_Name (Specification (Comp)))
9131 then
9132 Register_Handler;
9133 end if;
9135 elsif Nkind (Comp) = N_Entry_Declaration then
9136 E_Count := E_Count + 1;
9137 Comp_Id := Defining_Identifier (Comp);
9139 Edef :=
9140 Make_Defining_Identifier (Loc,
9141 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9142 Sub :=
9143 Make_Subprogram_Declaration (Loc,
9144 Specification =>
9145 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9147 Insert_After (Current_Node, Sub);
9148 Analyze (Sub);
9150 -- Build wrapper procedure for pre/postconditions
9152 Build_PPC_Wrapper (Comp_Id, N);
9154 Set_Protected_Body_Subprogram
9155 (Defining_Identifier (Comp),
9156 Defining_Unit_Name (Specification (Sub)));
9158 Current_Node := Sub;
9160 Bdef :=
9161 Make_Defining_Identifier (Loc,
9162 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
9163 Sub :=
9164 Make_Subprogram_Declaration (Loc,
9165 Specification =>
9166 Build_Barrier_Function_Specification (Loc, Bdef));
9168 Insert_After (Current_Node, Sub);
9169 Analyze (Sub);
9170 Set_Protected_Body_Subprogram (Bdef, Bdef);
9171 Set_Barrier_Function (Comp_Id, Bdef);
9172 Set_Scope (Bdef, Scope (Comp_Id));
9173 Current_Node := Sub;
9175 -- Collect pointers to the protected subprogram and the barrier
9176 -- of the current entry, for insertion into Entry_Bodies_Array.
9178 Append_To (Expressions (Entries_Aggr),
9179 Make_Aggregate (Loc,
9180 Expressions => New_List (
9181 Make_Attribute_Reference (Loc,
9182 Prefix => New_Reference_To (Bdef, Loc),
9183 Attribute_Name => Name_Unrestricted_Access),
9184 Make_Attribute_Reference (Loc,
9185 Prefix => New_Reference_To (Edef, Loc),
9186 Attribute_Name => Name_Unrestricted_Access))));
9187 end if;
9189 Next (Comp);
9190 end loop;
9192 -- If there are some private entry declarations, expand it as if they
9193 -- were visible entries.
9195 if Present (Private_Declarations (Pdef)) then
9196 Comp := First (Private_Declarations (Pdef));
9197 while Present (Comp) loop
9198 if Nkind (Comp) = N_Entry_Declaration then
9199 E_Count := E_Count + 1;
9200 Comp_Id := Defining_Identifier (Comp);
9202 Edef :=
9203 Make_Defining_Identifier (Loc,
9204 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9205 Sub :=
9206 Make_Subprogram_Declaration (Loc,
9207 Specification =>
9208 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9210 Insert_After (Current_Node, Sub);
9211 Analyze (Sub);
9213 Set_Protected_Body_Subprogram
9214 (Defining_Identifier (Comp),
9215 Defining_Unit_Name (Specification (Sub)));
9217 Current_Node := Sub;
9219 Bdef :=
9220 Make_Defining_Identifier (Loc,
9221 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9223 Sub :=
9224 Make_Subprogram_Declaration (Loc,
9225 Specification =>
9226 Build_Barrier_Function_Specification (Loc, Bdef));
9228 Insert_After (Current_Node, Sub);
9229 Analyze (Sub);
9230 Set_Protected_Body_Subprogram (Bdef, Bdef);
9231 Set_Barrier_Function (Comp_Id, Bdef);
9232 Set_Scope (Bdef, Scope (Comp_Id));
9233 Current_Node := Sub;
9235 -- Collect pointers to the protected subprogram and the barrier
9236 -- of the current entry, for insertion into Entry_Bodies_Array.
9238 Append_To (Expressions (Entries_Aggr),
9239 Make_Aggregate (Loc,
9240 Expressions => New_List (
9241 Make_Attribute_Reference (Loc,
9242 Prefix => New_Reference_To (Bdef, Loc),
9243 Attribute_Name => Name_Unrestricted_Access),
9244 Make_Attribute_Reference (Loc,
9245 Prefix => New_Reference_To (Edef, Loc),
9246 Attribute_Name => Name_Unrestricted_Access))));
9247 end if;
9249 Next (Comp);
9250 end loop;
9251 end if;
9253 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9254 -- all protected subprograms have been collected.
9256 if Has_Entries (Prot_Typ) then
9257 Body_Id :=
9258 Make_Defining_Identifier (Sloc (Prot_Typ),
9259 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9261 case Corresponding_Runtime_Package (Prot_Typ) is
9262 when System_Tasking_Protected_Objects_Entries =>
9263 Body_Arr := Make_Object_Declaration (Loc,
9264 Defining_Identifier => Body_Id,
9265 Aliased_Present => True,
9266 Object_Definition =>
9267 Make_Subtype_Indication (Loc,
9268 Subtype_Mark => New_Reference_To (
9269 RTE (RE_Protected_Entry_Body_Array), Loc),
9270 Constraint =>
9271 Make_Index_Or_Discriminant_Constraint (Loc,
9272 Constraints => New_List (
9273 Make_Range (Loc,
9274 Make_Integer_Literal (Loc, 1),
9275 Make_Integer_Literal (Loc, E_Count))))),
9276 Expression => Entries_Aggr);
9278 when System_Tasking_Protected_Objects_Single_Entry =>
9279 Body_Arr := Make_Object_Declaration (Loc,
9280 Defining_Identifier => Body_Id,
9281 Aliased_Present => True,
9282 Object_Definition => New_Reference_To
9283 (RTE (RE_Entry_Body), Loc),
9284 Expression =>
9285 Make_Aggregate (Loc,
9286 Expressions => New_List (
9287 Make_Attribute_Reference (Loc,
9288 Prefix => New_Reference_To (Bdef, Loc),
9289 Attribute_Name => Name_Unrestricted_Access),
9290 Make_Attribute_Reference (Loc,
9291 Prefix => New_Reference_To (Edef, Loc),
9292 Attribute_Name => Name_Unrestricted_Access))));
9294 when others =>
9295 raise Program_Error;
9296 end case;
9298 -- A pointer to this array will be placed in the corresponding record
9299 -- by its initialization procedure so this needs to be analyzed here.
9301 Insert_After (Current_Node, Body_Arr);
9302 Current_Node := Body_Arr;
9303 Analyze (Body_Arr);
9305 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9307 -- Finally, build the function that maps an entry index into the
9308 -- corresponding body. A pointer to this function is placed in each
9309 -- object of the type. Except for a ravenscar-like profile (no abort,
9310 -- no entry queue, 1 entry)
9312 if Corresponding_Runtime_Package (Prot_Typ) =
9313 System_Tasking_Protected_Objects_Entries
9314 then
9315 Sub :=
9316 Make_Subprogram_Declaration (Loc,
9317 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9318 Insert_After (Current_Node, Sub);
9319 Analyze (Sub);
9320 end if;
9321 end if;
9322 end Expand_N_Protected_Type_Declaration;
9324 --------------------------------
9325 -- Expand_N_Requeue_Statement --
9326 --------------------------------
9328 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9329 -- operations, depending on the source and destination (task or protected
9330 -- object). A dispatching requeue statement is expanded into a call to the
9331 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9332 -- jump around the remainder of processing for the original entry and, if
9333 -- the destination is (different) protected object, to attempt to service
9334 -- it. The following illustrates the various cases:
9336 -- procedure entE
9337 -- (O : System.Address;
9338 -- P : System.Address;
9339 -- E : Protected_Entry_Index)
9340 -- is
9341 -- <discriminant renamings>
9342 -- <private object renamings>
9343 -- type poVP is access poV;
9344 -- _object : ptVP := ptVP!(O);
9346 -- begin
9347 -- begin
9348 -- <start of statement sequence for entry>
9350 -- -- Requeue from one protected entry body to another protected
9351 -- -- entry.
9353 -- Requeue_Protected_Entry (
9354 -- _object._object'Access,
9355 -- new._object'Access,
9356 -- E,
9357 -- Abort_Present);
9358 -- return;
9360 -- <some more of the statement sequence for entry>
9362 -- -- Requeue from an entry body to a task entry
9364 -- Requeue_Protected_To_Task_Entry (
9365 -- New._task_id,
9366 -- E,
9367 -- Abort_Present);
9368 -- return;
9370 -- <rest of statement sequence for entry>
9371 -- Complete_Entry_Body (_object._object);
9373 -- exception
9374 -- when all others =>
9375 -- Exceptional_Complete_Entry_Body (
9376 -- _object._object, Get_GNAT_Exception);
9377 -- end;
9378 -- end entE;
9380 -- Requeue of a task entry call to a task entry
9382 -- Accept_Call (E, Ann);
9383 -- <start of statement sequence for accept statement>
9384 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9385 -- goto Lnn;
9386 -- <rest of statement sequence for accept statement>
9387 -- <<Lnn>>
9388 -- Complete_Rendezvous;
9390 -- exception
9391 -- when all others =>
9392 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9394 -- Requeue of a task entry call to a protected entry
9396 -- Accept_Call (E, Ann);
9397 -- <start of statement sequence for accept statement>
9398 -- Requeue_Task_To_Protected_Entry (
9399 -- new._object'Access,
9400 -- E,
9401 -- Abort_Present);
9402 -- newS (new, Pnn);
9403 -- goto Lnn;
9404 -- <rest of statement sequence for accept statement>
9405 -- <<Lnn>>
9406 -- Complete_Rendezvous;
9408 -- exception
9409 -- when all others =>
9410 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9412 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9413 -- marked by pragma Implemented (XXX, By_Entry).
9415 -- The requeue is inside a protected entry:
9417 -- procedure entE
9418 -- (O : System.Address;
9419 -- P : System.Address;
9420 -- E : Protected_Entry_Index)
9421 -- is
9422 -- <discriminant renamings>
9423 -- <private object renamings>
9424 -- type poVP is access poV;
9425 -- _object : ptVP := ptVP!(O);
9427 -- begin
9428 -- begin
9429 -- <start of statement sequence for entry>
9431 -- _Disp_Requeue
9432 -- (<interface class-wide object>,
9433 -- True,
9434 -- _object'Address,
9435 -- Ada.Tags.Get_Offset_Index
9436 -- (Tag (_object),
9437 -- <interface dispatch table index of target entry>),
9438 -- Abort_Present);
9439 -- return;
9441 -- <rest of statement sequence for entry>
9442 -- Complete_Entry_Body (_object._object);
9444 -- exception
9445 -- when all others =>
9446 -- Exceptional_Complete_Entry_Body (
9447 -- _object._object, Get_GNAT_Exception);
9448 -- end;
9449 -- end entE;
9451 -- The requeue is inside a task entry:
9453 -- Accept_Call (E, Ann);
9454 -- <start of statement sequence for accept statement>
9455 -- _Disp_Requeue
9456 -- (<interface class-wide object>,
9457 -- False,
9458 -- null,
9459 -- Ada.Tags.Get_Offset_Index
9460 -- (Tag (_object),
9461 -- <interface dispatch table index of target entrt>),
9462 -- Abort_Present);
9463 -- newS (new, Pnn);
9464 -- goto Lnn;
9465 -- <rest of statement sequence for accept statement>
9466 -- <<Lnn>>
9467 -- Complete_Rendezvous;
9469 -- exception
9470 -- when all others =>
9471 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9473 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9474 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9475 -- statement is replaced by a dispatching call with actual parameters taken
9476 -- from the inner-most accept statement or entry body.
9478 -- Target.Primitive (Param1, ..., ParamN);
9480 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9481 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9482 -- at all.
9484 -- declare
9485 -- S : constant Offset_Index :=
9486 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9487 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9489 -- begin
9490 -- if C = POK_Protected_Entry
9491 -- or else C = POK_Task_Entry
9492 -- then
9493 -- <statements for dispatching requeue>
9495 -- elsif C = POK_Protected_Procedure then
9496 -- <dispatching call equivalent>
9498 -- else
9499 -- raise Program_Error;
9500 -- end if;
9501 -- end;
9503 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9504 Loc : constant Source_Ptr := Sloc (N);
9505 Conc_Typ : Entity_Id;
9506 Concval : Node_Id;
9507 Ename : Node_Id;
9508 Index : Node_Id;
9509 Old_Typ : Entity_Id;
9511 function Build_Dispatching_Call_Equivalent return Node_Id;
9512 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9513 -- the form Concval.Ename. It is statically known that Ename is allowed
9514 -- to be implemented by a protected procedure. Create a dispatching call
9515 -- equivalent of Concval.Ename taking the actual parameters from the
9516 -- inner-most accept statement or entry body.
9518 function Build_Dispatching_Requeue return Node_Id;
9519 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9520 -- the form Concval.Ename. It is statically known that Ename is allowed
9521 -- to be implemented by a protected or a task entry. Create a call to
9522 -- primitive _Disp_Requeue which handles the low-level actions.
9524 function Build_Dispatching_Requeue_To_Any return Node_Id;
9525 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9526 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9527 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9528 -- determines at runtime whether Ename denotes an entry or a procedure
9529 -- and perform the appropriate kind of dispatching select.
9531 function Build_Normal_Requeue return Node_Id;
9532 -- N denotes a non-dispatching requeue statement to either a task or a
9533 -- protected entry. Build the appropriate runtime call to perform the
9534 -- action.
9536 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9537 -- For a protected entry, create a return statement to skip the rest of
9538 -- the entry body. Otherwise, create a goto statement to skip the rest
9539 -- of a task accept statement. The lookup for the enclosing entry body
9540 -- or accept statement starts from Search.
9542 ---------------------------------------
9543 -- Build_Dispatching_Call_Equivalent --
9544 ---------------------------------------
9546 function Build_Dispatching_Call_Equivalent return Node_Id is
9547 Call_Ent : constant Entity_Id := Entity (Ename);
9548 Obj : constant Node_Id := Original_Node (Concval);
9549 Acc_Ent : Node_Id;
9550 Actuals : List_Id;
9551 Formal : Node_Id;
9552 Formals : List_Id;
9554 begin
9555 -- Climb the parent chain looking for the inner-most entry body or
9556 -- accept statement.
9558 Acc_Ent := N;
9559 while Present (Acc_Ent)
9560 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9561 N_Entry_Body)
9562 loop
9563 Acc_Ent := Parent (Acc_Ent);
9564 end loop;
9566 -- A requeue statement should be housed inside an entry body or an
9567 -- accept statement at some level. If this is not the case, then the
9568 -- tree is malformed.
9570 pragma Assert (Present (Acc_Ent));
9572 -- Recover the list of formal parameters
9574 if Nkind (Acc_Ent) = N_Entry_Body then
9575 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9576 end if;
9578 Formals := Parameter_Specifications (Acc_Ent);
9580 -- Create the actual parameters for the dispatching call. These are
9581 -- simply copies of the entry body or accept statement formals in the
9582 -- same order as they appear.
9584 Actuals := No_List;
9586 if Present (Formals) then
9587 Actuals := New_List;
9588 Formal := First (Formals);
9589 while Present (Formal) loop
9590 Append_To (Actuals,
9591 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9592 Next (Formal);
9593 end loop;
9594 end if;
9596 -- Generate:
9597 -- Obj.Call_Ent (Actuals);
9599 return
9600 Make_Procedure_Call_Statement (Loc,
9601 Name =>
9602 Make_Selected_Component (Loc,
9603 Prefix => Make_Identifier (Loc, Chars (Obj)),
9604 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9606 Parameter_Associations => Actuals);
9607 end Build_Dispatching_Call_Equivalent;
9609 -------------------------------
9610 -- Build_Dispatching_Requeue --
9611 -------------------------------
9613 function Build_Dispatching_Requeue return Node_Id is
9614 Params : constant List_Id := New_List;
9616 begin
9617 -- Process the "with abort" parameter
9619 Prepend_To (Params,
9620 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9622 -- Process the entry wrapper's position in the primary dispatch
9623 -- table parameter. Generate:
9625 -- Ada.Tags.Get_Entry_Index
9626 -- (T => To_Tag_Ptr (Obj'Address).all,
9627 -- Position =>
9628 -- Ada.Tags.Get_Offset_Index
9629 -- (Ada.Tags.Tag (Concval),
9630 -- <interface dispatch table position of Ename>));
9632 -- Note that Obj'Address is recursively expanded into a call to
9633 -- Base_Address (Obj).
9635 if Tagged_Type_Expansion then
9636 Prepend_To (Params,
9637 Make_Function_Call (Loc,
9638 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9639 Parameter_Associations => New_List (
9641 Make_Explicit_Dereference (Loc,
9642 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9643 Make_Attribute_Reference (Loc,
9644 Prefix => New_Copy_Tree (Concval),
9645 Attribute_Name => Name_Address))),
9647 Make_Function_Call (Loc,
9648 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9649 Parameter_Associations => New_List (
9650 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9651 Make_Integer_Literal (Loc,
9652 DT_Position (Entity (Ename))))))));
9654 -- VM targets
9656 else
9657 Prepend_To (Params,
9658 Make_Function_Call (Loc,
9659 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9660 Parameter_Associations => New_List (
9662 Make_Attribute_Reference (Loc,
9663 Prefix => Concval,
9664 Attribute_Name => Name_Tag),
9666 Make_Function_Call (Loc,
9667 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9669 Parameter_Associations => New_List (
9671 -- Obj_Tag
9673 Make_Attribute_Reference (Loc,
9674 Prefix => Concval,
9675 Attribute_Name => Name_Tag),
9677 -- Tag_Typ
9679 Make_Attribute_Reference (Loc,
9680 Prefix => New_Reference_To (Etype (Concval), Loc),
9681 Attribute_Name => Name_Tag),
9683 -- Position
9685 Make_Integer_Literal (Loc,
9686 DT_Position (Entity (Ename))))))));
9687 end if;
9689 -- Specific actuals for protected to XXX requeue
9691 if Is_Protected_Type (Old_Typ) then
9692 Prepend_To (Params,
9693 Make_Attribute_Reference (Loc, -- _object'Address
9694 Prefix =>
9695 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9696 Attribute_Name => Name_Address));
9698 Prepend_To (Params, -- True
9699 New_Reference_To (Standard_True, Loc));
9701 -- Specific actuals for task to XXX requeue
9703 else
9704 pragma Assert (Is_Task_Type (Old_Typ));
9706 Prepend_To (Params, -- null
9707 New_Reference_To (RTE (RE_Null_Address), Loc));
9709 Prepend_To (Params, -- False
9710 New_Reference_To (Standard_False, Loc));
9711 end if;
9713 -- Add the object parameter
9715 Prepend_To (Params, New_Copy_Tree (Concval));
9717 -- Generate:
9718 -- _Disp_Requeue (<Params>);
9720 -- Find entity for Disp_Requeue operation, which belongs to
9721 -- the type and may not be directly visible.
9723 declare
9724 Elmt : Elmt_Id;
9725 Op : Entity_Id;
9727 begin
9728 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9729 while Present (Elmt) loop
9730 Op := Node (Elmt);
9731 exit when Chars (Op) = Name_uDisp_Requeue;
9732 Next_Elmt (Elmt);
9733 end loop;
9735 return
9736 Make_Procedure_Call_Statement (Loc,
9737 Name => New_Occurrence_Of (Op, Loc),
9738 Parameter_Associations => Params);
9739 end;
9740 end Build_Dispatching_Requeue;
9742 --------------------------------------
9743 -- Build_Dispatching_Requeue_To_Any --
9744 --------------------------------------
9746 function Build_Dispatching_Requeue_To_Any return Node_Id is
9747 Call_Ent : constant Entity_Id := Entity (Ename);
9748 Obj : constant Node_Id := Original_Node (Concval);
9749 Skip : constant Node_Id := Build_Skip_Statement (N);
9750 C : Entity_Id;
9751 Decls : List_Id;
9752 S : Entity_Id;
9753 Stmts : List_Id;
9755 begin
9756 Decls := New_List;
9757 Stmts := New_List;
9759 -- Dispatch table slot processing, generate:
9760 -- S : Integer;
9762 S := Build_S (Loc, Decls);
9764 -- Call kind processing, generate:
9765 -- C : Ada.Tags.Prim_Op_Kind;
9767 C := Build_C (Loc, Decls);
9769 -- Generate:
9770 -- S := Ada.Tags.Get_Offset_Index
9771 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9773 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9775 -- Generate:
9776 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9778 Append_To (Stmts,
9779 Make_Procedure_Call_Statement (Loc,
9780 Name =>
9781 New_Reference_To (
9782 Find_Prim_Op (Etype (Etype (Obj)),
9783 Name_uDisp_Get_Prim_Op_Kind),
9784 Loc),
9785 Parameter_Associations => New_List (
9786 New_Copy_Tree (Obj),
9787 New_Reference_To (S, Loc),
9788 New_Reference_To (C, Loc))));
9790 Append_To (Stmts,
9792 -- if C = POK_Protected_Entry
9793 -- or else C = POK_Task_Entry
9794 -- then
9796 Make_Implicit_If_Statement (N,
9797 Condition =>
9798 Make_Op_Or (Loc,
9799 Left_Opnd =>
9800 Make_Op_Eq (Loc,
9801 Left_Opnd =>
9802 New_Reference_To (C, Loc),
9803 Right_Opnd =>
9804 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
9806 Right_Opnd =>
9807 Make_Op_Eq (Loc,
9808 Left_Opnd =>
9809 New_Reference_To (C, Loc),
9810 Right_Opnd =>
9811 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
9813 -- Dispatching requeue equivalent
9815 Then_Statements => New_List (
9816 Build_Dispatching_Requeue,
9817 Skip),
9819 -- elsif C = POK_Protected_Procedure then
9821 Elsif_Parts => New_List (
9822 Make_Elsif_Part (Loc,
9823 Condition =>
9824 Make_Op_Eq (Loc,
9825 Left_Opnd =>
9826 New_Reference_To (C, Loc),
9827 Right_Opnd =>
9828 New_Reference_To (
9829 RTE (RE_POK_Protected_Procedure), Loc)),
9831 -- Dispatching call equivalent
9833 Then_Statements => New_List (
9834 Build_Dispatching_Call_Equivalent))),
9836 -- else
9837 -- raise Program_Error;
9838 -- end if;
9840 Else_Statements => New_List (
9841 Make_Raise_Program_Error (Loc,
9842 Reason => PE_Explicit_Raise))));
9844 -- Wrap everything into a block
9846 return
9847 Make_Block_Statement (Loc,
9848 Declarations => Decls,
9849 Handled_Statement_Sequence =>
9850 Make_Handled_Sequence_Of_Statements (Loc,
9851 Statements => Stmts));
9852 end Build_Dispatching_Requeue_To_Any;
9854 --------------------------
9855 -- Build_Normal_Requeue --
9856 --------------------------
9858 function Build_Normal_Requeue return Node_Id is
9859 Params : constant List_Id := New_List;
9860 Param : Node_Id;
9861 RT_Call : Node_Id;
9863 begin
9864 -- Process the "with abort" parameter
9866 Prepend_To (Params,
9867 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9869 -- Add the index expression to the parameters. It is common among all
9870 -- four cases.
9872 Prepend_To (Params,
9873 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
9875 if Is_Protected_Type (Old_Typ) then
9876 declare
9877 Self_Param : Node_Id;
9879 begin
9880 Self_Param :=
9881 Make_Attribute_Reference (Loc,
9882 Prefix =>
9883 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9884 Attribute_Name =>
9885 Name_Unchecked_Access);
9887 -- Protected to protected requeue
9889 if Is_Protected_Type (Conc_Typ) then
9890 RT_Call :=
9891 New_Reference_To (
9892 RTE (RE_Requeue_Protected_Entry), Loc);
9894 Param :=
9895 Make_Attribute_Reference (Loc,
9896 Prefix =>
9897 Concurrent_Ref (Concval),
9898 Attribute_Name =>
9899 Name_Unchecked_Access);
9901 -- Protected to task requeue
9903 else pragma Assert (Is_Task_Type (Conc_Typ));
9904 RT_Call :=
9905 New_Reference_To (
9906 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9908 Param := Concurrent_Ref (Concval);
9909 end if;
9911 Prepend_To (Params, Param);
9912 Prepend_To (Params, Self_Param);
9913 end;
9915 else pragma Assert (Is_Task_Type (Old_Typ));
9917 -- Task to protected requeue
9919 if Is_Protected_Type (Conc_Typ) then
9920 RT_Call :=
9921 New_Reference_To (
9922 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
9924 Param :=
9925 Make_Attribute_Reference (Loc,
9926 Prefix =>
9927 Concurrent_Ref (Concval),
9928 Attribute_Name =>
9929 Name_Unchecked_Access);
9931 -- Task to task requeue
9933 else pragma Assert (Is_Task_Type (Conc_Typ));
9934 RT_Call :=
9935 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
9937 Param := Concurrent_Ref (Concval);
9938 end if;
9940 Prepend_To (Params, Param);
9941 end if;
9943 return
9944 Make_Procedure_Call_Statement (Loc,
9945 Name => RT_Call,
9946 Parameter_Associations => Params);
9947 end Build_Normal_Requeue;
9949 --------------------------
9950 -- Build_Skip_Statement --
9951 --------------------------
9953 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
9954 Skip_Stmt : Node_Id;
9956 begin
9957 -- Build a return statement to skip the rest of the entire body
9959 if Is_Protected_Type (Old_Typ) then
9960 Skip_Stmt := Make_Simple_Return_Statement (Loc);
9962 -- If the requeue is within a task, find the end label of the
9963 -- enclosing accept statement and create a goto statement to it.
9965 else
9966 declare
9967 Acc : Node_Id;
9968 Label : Node_Id;
9970 begin
9971 -- Climb the parent chain looking for the enclosing accept
9972 -- statement.
9974 Acc := Parent (Search);
9975 while Present (Acc)
9976 and then Nkind (Acc) /= N_Accept_Statement
9977 loop
9978 Acc := Parent (Acc);
9979 end loop;
9981 -- The last statement is the second label used for completing
9982 -- the rendezvous the usual way. The label we are looking for
9983 -- is right before it.
9985 Label :=
9986 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
9988 pragma Assert (Nkind (Label) = N_Label);
9990 -- Generate a goto statement to skip the rest of the accept
9992 Skip_Stmt :=
9993 Make_Goto_Statement (Loc,
9994 Name =>
9995 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
9996 end;
9997 end if;
9999 Set_Analyzed (Skip_Stmt);
10001 return Skip_Stmt;
10002 end Build_Skip_Statement;
10004 -- Start of processing for Expand_N_Requeue_Statement
10006 begin
10007 -- Extract the components of the entry call
10009 Extract_Entry (N, Concval, Ename, Index);
10010 Conc_Typ := Etype (Concval);
10012 -- If the prefix is an access to class-wide type, dereference to get
10013 -- object and entry type.
10015 if Is_Access_Type (Conc_Typ) then
10016 Conc_Typ := Designated_Type (Conc_Typ);
10017 Rewrite (Concval,
10018 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10019 Analyze_And_Resolve (Concval, Conc_Typ);
10020 end if;
10022 -- Examine the scope stack in order to find nearest enclosing protected
10023 -- or task type. This will constitute our invocation source.
10025 Old_Typ := Current_Scope;
10026 while Present (Old_Typ)
10027 and then not Is_Protected_Type (Old_Typ)
10028 and then not Is_Task_Type (Old_Typ)
10029 loop
10030 Old_Typ := Scope (Old_Typ);
10031 end loop;
10033 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10034 -- Concval.Ename where the type of Concval is class-wide concurrent
10035 -- interface.
10037 if Ada_Version >= Ada_2012
10038 and then Present (Concval)
10039 and then Is_Class_Wide_Type (Conc_Typ)
10040 and then Is_Concurrent_Interface (Conc_Typ)
10041 then
10042 declare
10043 Has_Impl : Boolean := False;
10044 Impl_Kind : Name_Id := No_Name;
10046 begin
10047 -- Check whether the Ename is flagged by pragma Implemented
10049 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10050 Has_Impl := True;
10051 Impl_Kind := Implementation_Kind (Entity (Ename));
10052 end if;
10054 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10055 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10057 if Has_Impl
10058 and then Impl_Kind = Name_By_Entry
10059 then
10060 Rewrite (N, Build_Dispatching_Requeue);
10061 Analyze (N);
10062 Insert_After (N, Build_Skip_Statement (N));
10064 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10065 -- a protected procedure. In this case the requeue is transformed
10066 -- into a dispatching call.
10068 elsif Has_Impl
10069 and then Impl_Kind = Name_By_Protected_Procedure
10070 then
10071 Rewrite (N, Build_Dispatching_Call_Equivalent);
10072 Analyze (N);
10074 -- The procedure_or_entry_NAME's implementation kind is either
10075 -- By_Any, Optional, or pragma Implemented was not applied at all.
10076 -- In this case a runtime test determines whether Ename denotes an
10077 -- entry or a protected procedure and performs the appropriate
10078 -- call.
10080 else
10081 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10082 Analyze (N);
10083 end if;
10084 end;
10086 -- Processing for regular (non-dispatching) requeues
10088 else
10089 Rewrite (N, Build_Normal_Requeue);
10090 Analyze (N);
10091 Insert_After (N, Build_Skip_Statement (N));
10092 end if;
10093 end Expand_N_Requeue_Statement;
10095 -------------------------------
10096 -- Expand_N_Selective_Accept --
10097 -------------------------------
10099 procedure Expand_N_Selective_Accept (N : Node_Id) is
10100 Loc : constant Source_Ptr := Sloc (N);
10101 Alts : constant List_Id := Select_Alternatives (N);
10103 -- Note: in the below declarations a lot of new lists are allocated
10104 -- unconditionally which may well not end up being used. That's not
10105 -- a good idea since it wastes space gratuitously ???
10107 Accept_Case : List_Id;
10108 Accept_List : constant List_Id := New_List;
10110 Alt : Node_Id;
10111 Alt_List : constant List_Id := New_List;
10112 Alt_Stats : List_Id;
10113 Ann : Entity_Id := Empty;
10115 Check_Guard : Boolean := True;
10117 Decls : constant List_Id := New_List;
10118 Stats : constant List_Id := New_List;
10119 Body_List : constant List_Id := New_List;
10120 Trailing_List : constant List_Id := New_List;
10122 Choices : List_Id;
10123 Else_Present : Boolean := False;
10124 Terminate_Alt : Node_Id := Empty;
10125 Select_Mode : Node_Id;
10127 Delay_Case : List_Id;
10128 Delay_Count : Integer := 0;
10129 Delay_Val : Entity_Id;
10130 Delay_Index : Entity_Id;
10131 Delay_Min : Entity_Id;
10132 Delay_Num : Int := 1;
10133 Delay_Alt_List : List_Id := New_List;
10134 Delay_List : constant List_Id := New_List;
10135 D : Entity_Id;
10136 M : Entity_Id;
10138 First_Delay : Boolean := True;
10139 Guard_Open : Entity_Id;
10141 End_Lab : Node_Id;
10142 Index : Int := 1;
10143 Lab : Node_Id;
10144 Num_Alts : Int;
10145 Num_Accept : Nat := 0;
10146 Proc : Node_Id;
10147 Time_Type : Entity_Id;
10148 Select_Call : Node_Id;
10150 Qnam : constant Entity_Id :=
10151 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10153 Xnam : constant Entity_Id :=
10154 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10156 -----------------------
10157 -- Local subprograms --
10158 -----------------------
10160 function Accept_Or_Raise return List_Id;
10161 -- For the rare case where delay alternatives all have guards, and
10162 -- all of them are closed, it is still possible that there were open
10163 -- accept alternatives with no callers. We must reexamine the
10164 -- Accept_List, and execute a selective wait with no else if some
10165 -- accept is open. If none, we raise program_error.
10167 procedure Add_Accept (Alt : Node_Id);
10168 -- Process a single accept statement in a select alternative. Build
10169 -- procedure for body of accept, and add entry to dispatch table with
10170 -- expression for guard, in preparation for call to run time select.
10172 function Make_And_Declare_Label (Num : Int) return Node_Id;
10173 -- Manufacture a label using Num as a serial number and declare it.
10174 -- The declaration is appended to Decls. The label marks the trailing
10175 -- statements of an accept or delay alternative.
10177 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10178 -- Build call to Selective_Wait runtime routine
10180 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10181 -- Add code to compare value of delay with previous values, and
10182 -- generate case entry for trailing statements.
10184 procedure Process_Accept_Alternative
10185 (Alt : Node_Id;
10186 Index : Int;
10187 Proc : Node_Id);
10188 -- Add code to call corresponding procedure, and branch to
10189 -- trailing statements, if any.
10191 ---------------------
10192 -- Accept_Or_Raise --
10193 ---------------------
10195 function Accept_Or_Raise return List_Id is
10196 Cond : Node_Id;
10197 Stats : List_Id;
10198 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10200 begin
10201 -- We generate the following:
10203 -- for J in q'range loop
10204 -- if q(J).S /=null_task_entry then
10205 -- selective_wait (simple_mode,...);
10206 -- done := True;
10207 -- exit;
10208 -- end if;
10209 -- end loop;
10211 -- if no rendez_vous then
10212 -- raise program_error;
10213 -- end if;
10215 -- Note that the code needs to know that the selector name
10216 -- in an Accept_Alternative is named S.
10218 Cond := Make_Op_Ne (Loc,
10219 Left_Opnd =>
10220 Make_Selected_Component (Loc,
10221 Prefix =>
10222 Make_Indexed_Component (Loc,
10223 Prefix => New_Reference_To (Qnam, Loc),
10224 Expressions => New_List (New_Reference_To (J, Loc))),
10225 Selector_Name => Make_Identifier (Loc, Name_S)),
10226 Right_Opnd =>
10227 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
10229 Stats := New_List (
10230 Make_Implicit_Loop_Statement (N,
10231 Iteration_Scheme =>
10232 Make_Iteration_Scheme (Loc,
10233 Loop_Parameter_Specification =>
10234 Make_Loop_Parameter_Specification (Loc,
10235 Defining_Identifier => J,
10236 Discrete_Subtype_Definition =>
10237 Make_Attribute_Reference (Loc,
10238 Prefix => New_Reference_To (Qnam, Loc),
10239 Attribute_Name => Name_Range,
10240 Expressions => New_List (
10241 Make_Integer_Literal (Loc, 1))))),
10243 Statements => New_List (
10244 Make_Implicit_If_Statement (N,
10245 Condition => Cond,
10246 Then_Statements => New_List (
10247 Make_Select_Call (
10248 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
10249 Make_Exit_Statement (Loc))))));
10251 Append_To (Stats,
10252 Make_Raise_Program_Error (Loc,
10253 Condition => Make_Op_Eq (Loc,
10254 Left_Opnd => New_Reference_To (Xnam, Loc),
10255 Right_Opnd =>
10256 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10257 Reason => PE_All_Guards_Closed));
10259 return Stats;
10260 end Accept_Or_Raise;
10262 ----------------
10263 -- Add_Accept --
10264 ----------------
10266 procedure Add_Accept (Alt : Node_Id) is
10267 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10268 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10269 Eloc : constant Source_Ptr := Sloc (Ename);
10270 Eent : constant Entity_Id := Entity (Ename);
10271 Index : constant Node_Id := Entry_Index (Acc_Stm);
10272 Null_Body : Node_Id;
10273 Proc_Body : Node_Id;
10274 PB_Ent : Entity_Id;
10275 Expr : Node_Id;
10276 Call : Node_Id;
10278 begin
10279 if No (Ann) then
10280 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10281 end if;
10283 if Present (Condition (Alt)) then
10284 Expr :=
10285 Make_If_Expression (Eloc, New_List (
10286 Condition (Alt),
10287 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10288 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
10289 else
10290 Expr :=
10291 Entry_Index_Expression
10292 (Eloc, Eent, Index, Scope (Eent));
10293 end if;
10295 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10296 Null_Body := New_Reference_To (Standard_False, Eloc);
10298 if Abort_Allowed then
10299 Call := Make_Procedure_Call_Statement (Eloc,
10300 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
10301 Insert_Before (First (Statements (Handled_Statement_Sequence (
10302 Accept_Statement (Alt)))), Call);
10303 Analyze (Call);
10304 end if;
10306 PB_Ent :=
10307 Make_Defining_Identifier (Eloc,
10308 New_External_Name (Chars (Ename), 'A', Num_Accept));
10310 if Comes_From_Source (Alt) then
10311 Set_Debug_Info_Needed (PB_Ent);
10312 end if;
10314 Proc_Body :=
10315 Make_Subprogram_Body (Eloc,
10316 Specification =>
10317 Make_Procedure_Specification (Eloc,
10318 Defining_Unit_Name => PB_Ent),
10319 Declarations => Declarations (Acc_Stm),
10320 Handled_Statement_Sequence =>
10321 Build_Accept_Body (Accept_Statement (Alt)));
10323 -- During the analysis of the body of the accept statement, any
10324 -- zero cost exception handler records were collected in the
10325 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10326 -- This is where we move them to where they belong, namely the
10327 -- newly created procedure.
10329 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10330 Append (Proc_Body, Body_List);
10332 else
10333 Null_Body := New_Reference_To (Standard_True, Eloc);
10335 -- if accept statement has declarations, insert above, given that
10336 -- we are not creating a body for the accept.
10338 if Present (Declarations (Acc_Stm)) then
10339 Insert_Actions (N, Declarations (Acc_Stm));
10340 end if;
10341 end if;
10343 Append_To (Accept_List,
10344 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10346 Num_Accept := Num_Accept + 1;
10347 end Add_Accept;
10349 ----------------------------
10350 -- Make_And_Declare_Label --
10351 ----------------------------
10353 function Make_And_Declare_Label (Num : Int) return Node_Id is
10354 Lab_Id : Node_Id;
10356 begin
10357 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10358 Lab :=
10359 Make_Label (Loc, Lab_Id);
10361 Append_To (Decls,
10362 Make_Implicit_Label_Declaration (Loc,
10363 Defining_Identifier =>
10364 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10365 Label_Construct => Lab));
10367 return Lab;
10368 end Make_And_Declare_Label;
10370 ----------------------
10371 -- Make_Select_Call --
10372 ----------------------
10374 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10375 Params : constant List_Id := New_List;
10377 begin
10378 Append (
10379 Make_Attribute_Reference (Loc,
10380 Prefix => New_Reference_To (Qnam, Loc),
10381 Attribute_Name => Name_Unchecked_Access),
10382 Params);
10383 Append (Select_Mode, Params);
10384 Append (New_Reference_To (Ann, Loc), Params);
10385 Append (New_Reference_To (Xnam, Loc), Params);
10387 return
10388 Make_Procedure_Call_Statement (Loc,
10389 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
10390 Parameter_Associations => Params);
10391 end Make_Select_Call;
10393 --------------------------------
10394 -- Process_Accept_Alternative --
10395 --------------------------------
10397 procedure Process_Accept_Alternative
10398 (Alt : Node_Id;
10399 Index : Int;
10400 Proc : Node_Id)
10402 Astmt : constant Node_Id := Accept_Statement (Alt);
10403 Alt_Stats : List_Id;
10405 begin
10406 Adjust_Condition (Condition (Alt));
10408 -- Accept with body
10410 if Present (Handled_Statement_Sequence (Astmt)) then
10411 Alt_Stats :=
10412 New_List (
10413 Make_Procedure_Call_Statement (Sloc (Proc),
10414 Name =>
10415 New_Reference_To
10416 (Defining_Unit_Name (Specification (Proc)),
10417 Sloc (Proc))));
10419 -- Accept with no body (followed by trailing statements)
10421 else
10422 Alt_Stats := Empty_List;
10423 end if;
10425 Ensure_Statement_Present (Sloc (Astmt), Alt);
10427 -- After the call, if any, branch to trailing statements, if any.
10428 -- We create a label for each, as well as the corresponding label
10429 -- declaration.
10431 if not Is_Empty_List (Statements (Alt)) then
10432 Lab := Make_And_Declare_Label (Index);
10433 Append (Lab, Trailing_List);
10434 Append_List (Statements (Alt), Trailing_List);
10435 Append_To (Trailing_List,
10436 Make_Goto_Statement (Loc,
10437 Name => New_Copy (Identifier (End_Lab))));
10439 else
10440 Lab := End_Lab;
10441 end if;
10443 Append_To (Alt_Stats,
10444 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10446 Append_To (Alt_List,
10447 Make_Case_Statement_Alternative (Loc,
10448 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10449 Statements => Alt_Stats));
10450 end Process_Accept_Alternative;
10452 -------------------------------
10453 -- Process_Delay_Alternative --
10454 -------------------------------
10456 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10457 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10458 Cond : Node_Id;
10459 Delay_Alt : List_Id;
10461 begin
10462 -- Deal with C/Fortran boolean as delay condition
10464 Adjust_Condition (Condition (Alt));
10466 -- Determine the smallest specified delay
10468 -- for each delay alternative generate:
10470 -- if guard-expression then
10471 -- Delay_Val := delay-expression;
10472 -- Guard_Open := True;
10473 -- if Delay_Val < Delay_Min then
10474 -- Delay_Min := Delay_Val;
10475 -- Delay_Index := Index;
10476 -- end if;
10477 -- end if;
10479 -- The enclosing if-statement is omitted if there is no guard
10481 if Delay_Count = 1 or else First_Delay then
10482 First_Delay := False;
10484 Delay_Alt := New_List (
10485 Make_Assignment_Statement (Loc,
10486 Name => New_Reference_To (Delay_Min, Loc),
10487 Expression => Expression (Delay_Statement (Alt))));
10489 if Delay_Count > 1 then
10490 Append_To (Delay_Alt,
10491 Make_Assignment_Statement (Loc,
10492 Name => New_Reference_To (Delay_Index, Loc),
10493 Expression => Make_Integer_Literal (Loc, Index)));
10494 end if;
10496 else
10497 Delay_Alt := New_List (
10498 Make_Assignment_Statement (Loc,
10499 Name => New_Reference_To (Delay_Val, Loc),
10500 Expression => Expression (Delay_Statement (Alt))));
10502 if Time_Type = Standard_Duration then
10503 Cond :=
10504 Make_Op_Lt (Loc,
10505 Left_Opnd => New_Reference_To (Delay_Val, Loc),
10506 Right_Opnd => New_Reference_To (Delay_Min, Loc));
10508 else
10509 -- The scope of the time type must define a comparison
10510 -- operator. The scope itself may not be visible, so we
10511 -- construct a node with entity information to insure that
10512 -- semantic analysis can find the proper operator.
10514 Cond :=
10515 Make_Function_Call (Loc,
10516 Name => Make_Selected_Component (Loc,
10517 Prefix =>
10518 New_Reference_To (Scope (Time_Type), Loc),
10519 Selector_Name =>
10520 Make_Operator_Symbol (Loc,
10521 Chars => Name_Op_Lt,
10522 Strval => No_String)),
10523 Parameter_Associations =>
10524 New_List (
10525 New_Reference_To (Delay_Val, Loc),
10526 New_Reference_To (Delay_Min, Loc)));
10528 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10529 end if;
10531 Append_To (Delay_Alt,
10532 Make_Implicit_If_Statement (N,
10533 Condition => Cond,
10534 Then_Statements => New_List (
10535 Make_Assignment_Statement (Loc,
10536 Name => New_Reference_To (Delay_Min, Loc),
10537 Expression => New_Reference_To (Delay_Val, Loc)),
10539 Make_Assignment_Statement (Loc,
10540 Name => New_Reference_To (Delay_Index, Loc),
10541 Expression => Make_Integer_Literal (Loc, Index)))));
10542 end if;
10544 if Check_Guard then
10545 Append_To (Delay_Alt,
10546 Make_Assignment_Statement (Loc,
10547 Name => New_Reference_To (Guard_Open, Loc),
10548 Expression => New_Reference_To (Standard_True, Loc)));
10549 end if;
10551 if Present (Condition (Alt)) then
10552 Delay_Alt := New_List (
10553 Make_Implicit_If_Statement (N,
10554 Condition => Condition (Alt),
10555 Then_Statements => Delay_Alt));
10556 end if;
10558 Append_List (Delay_Alt, Delay_List);
10560 Ensure_Statement_Present (Dloc, Alt);
10562 -- If the delay alternative has a statement part, add choice to the
10563 -- case statements for delays.
10565 if not Is_Empty_List (Statements (Alt)) then
10567 if Delay_Count = 1 then
10568 Append_List (Statements (Alt), Delay_Alt_List);
10570 else
10571 Append_To (Delay_Alt_List,
10572 Make_Case_Statement_Alternative (Loc,
10573 Discrete_Choices => New_List (
10574 Make_Integer_Literal (Loc, Index)),
10575 Statements => Statements (Alt)));
10576 end if;
10578 elsif Delay_Count = 1 then
10580 -- If the single delay has no trailing statements, add a branch
10581 -- to the exit label to the selective wait.
10583 Delay_Alt_List := New_List (
10584 Make_Goto_Statement (Loc,
10585 Name => New_Copy (Identifier (End_Lab))));
10587 end if;
10588 end Process_Delay_Alternative;
10590 -- Start of processing for Expand_N_Selective_Accept
10592 begin
10593 Process_Statements_For_Controlled_Objects (N);
10595 -- First insert some declarations before the select. The first is:
10597 -- Ann : Address
10599 -- This variable holds the parameters passed to the accept body. This
10600 -- declaration has already been inserted by the time we get here by
10601 -- a call to Expand_Accept_Declarations made from the semantics when
10602 -- processing the first accept statement contained in the select. We
10603 -- can find this entity as Accept_Address (E), where E is any of the
10604 -- entries references by contained accept statements.
10606 -- The first step is to scan the list of Selective_Accept_Statements
10607 -- to find this entity, and also count the number of accepts, and
10608 -- determine if terminated, delay or else is present:
10610 Num_Alts := 0;
10612 Alt := First (Alts);
10613 while Present (Alt) loop
10614 Process_Statements_For_Controlled_Objects (Alt);
10616 if Nkind (Alt) = N_Accept_Alternative then
10617 Add_Accept (Alt);
10619 elsif Nkind (Alt) = N_Delay_Alternative then
10620 Delay_Count := Delay_Count + 1;
10622 -- If the delays are relative delays, the delay expressions have
10623 -- type Standard_Duration. Otherwise they must have some time type
10624 -- recognized by GNAT.
10626 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10627 Time_Type := Standard_Duration;
10628 else
10629 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10631 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10632 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10633 then
10634 null;
10635 else
10636 Error_Msg_NE (
10637 "& is not a time type (RM 9.6(6))",
10638 Expression (Delay_Statement (Alt)), Time_Type);
10639 Time_Type := Standard_Duration;
10640 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10641 end if;
10642 end if;
10644 if No (Condition (Alt)) then
10646 -- This guard will always be open
10648 Check_Guard := False;
10649 end if;
10651 elsif Nkind (Alt) = N_Terminate_Alternative then
10652 Adjust_Condition (Condition (Alt));
10653 Terminate_Alt := Alt;
10654 end if;
10656 Num_Alts := Num_Alts + 1;
10657 Next (Alt);
10658 end loop;
10660 Else_Present := Present (Else_Statements (N));
10662 -- At the same time (see procedure Add_Accept) we build the accept list:
10664 -- Qnn : Accept_List (1 .. num-select) := (
10665 -- (null-body, entry-index),
10666 -- (null-body, entry-index),
10667 -- ..
10668 -- (null_body, entry-index));
10670 -- In the above declaration, null-body is True if the corresponding
10671 -- accept has no body, and false otherwise. The entry is either the
10672 -- entry index expression if there is no guard, or if a guard is
10673 -- present, then an if expression of the form:
10675 -- (if guard then entry-index else Null_Task_Entry)
10677 -- If a guard is statically known to be false, the entry can simply
10678 -- be omitted from the accept list.
10680 Append_To (Decls,
10681 Make_Object_Declaration (Loc,
10682 Defining_Identifier => Qnam,
10683 Object_Definition => New_Reference_To (RTE (RE_Accept_List), Loc),
10684 Aliased_Present => True,
10685 Expression =>
10686 Make_Qualified_Expression (Loc,
10687 Subtype_Mark =>
10688 New_Reference_To (RTE (RE_Accept_List), Loc),
10689 Expression =>
10690 Make_Aggregate (Loc, Expressions => Accept_List))));
10692 -- Then we declare the variable that holds the index for the accept
10693 -- that will be selected for service:
10695 -- Xnn : Select_Index;
10697 Append_To (Decls,
10698 Make_Object_Declaration (Loc,
10699 Defining_Identifier => Xnam,
10700 Object_Definition =>
10701 New_Reference_To (RTE (RE_Select_Index), Loc),
10702 Expression =>
10703 New_Reference_To (RTE (RE_No_Rendezvous), Loc)));
10705 -- After this follow procedure declarations for each accept body
10707 -- procedure Pnn is
10708 -- begin
10709 -- ...
10710 -- end;
10712 -- where the ... are statements from the corresponding procedure body.
10713 -- No parameters are involved, since the parameters are passed via Ann
10714 -- and the parameter references have already been expanded to be direct
10715 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10716 -- any embedded tasking statements (which would normally be illegal in
10717 -- procedures), have been converted to calls to the tasking runtime so
10718 -- there is no problem in putting them into procedures.
10720 -- The original accept statement has been expanded into a block in
10721 -- the same fashion as for simple accepts (see Build_Accept_Body).
10723 -- Note: we don't really need to build these procedures for the case
10724 -- where no delay statement is present, but it is just as easy to
10725 -- build them unconditionally, and not significantly inefficient,
10726 -- since if they are short they will be inlined anyway.
10728 -- The procedure declarations have been assembled in Body_List
10730 -- If delays are present, we must compute the required delay.
10731 -- We first generate the declarations:
10733 -- Delay_Index : Boolean := 0;
10734 -- Delay_Min : Some_Time_Type.Time;
10735 -- Delay_Val : Some_Time_Type.Time;
10737 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10738 -- active delay that is actually chosen as the basis for the possible
10739 -- delay if an immediate rendez-vous is not possible.
10741 -- In the most common case there is a single delay statement, and this
10742 -- is handled specially.
10744 if Delay_Count > 0 then
10746 -- Generate the required declarations
10748 Delay_Val :=
10749 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10750 Delay_Index :=
10751 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10752 Delay_Min :=
10753 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10755 Append_To (Decls,
10756 Make_Object_Declaration (Loc,
10757 Defining_Identifier => Delay_Val,
10758 Object_Definition => New_Reference_To (Time_Type, Loc)));
10760 Append_To (Decls,
10761 Make_Object_Declaration (Loc,
10762 Defining_Identifier => Delay_Index,
10763 Object_Definition => New_Reference_To (Standard_Integer, Loc),
10764 Expression => Make_Integer_Literal (Loc, 0)));
10766 Append_To (Decls,
10767 Make_Object_Declaration (Loc,
10768 Defining_Identifier => Delay_Min,
10769 Object_Definition => New_Reference_To (Time_Type, Loc),
10770 Expression =>
10771 Unchecked_Convert_To (Time_Type,
10772 Make_Attribute_Reference (Loc,
10773 Prefix =>
10774 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10775 Attribute_Name => Name_Last))));
10777 -- Create Duration and Delay_Mode objects used for passing a delay
10778 -- value to RTS
10780 D := Make_Temporary (Loc, 'D');
10781 M := Make_Temporary (Loc, 'M');
10783 declare
10784 Discr : Entity_Id;
10786 begin
10787 -- Note that these values are defined in s-osprim.ads and must
10788 -- be kept in sync:
10790 -- Relative : constant := 0;
10791 -- Absolute_Calendar : constant := 1;
10792 -- Absolute_RT : constant := 2;
10794 if Time_Type = Standard_Duration then
10795 Discr := Make_Integer_Literal (Loc, 0);
10797 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10798 Discr := Make_Integer_Literal (Loc, 1);
10800 else
10801 pragma Assert
10802 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10803 Discr := Make_Integer_Literal (Loc, 2);
10804 end if;
10806 Append_To (Decls,
10807 Make_Object_Declaration (Loc,
10808 Defining_Identifier => D,
10809 Object_Definition =>
10810 New_Reference_To (Standard_Duration, Loc)));
10812 Append_To (Decls,
10813 Make_Object_Declaration (Loc,
10814 Defining_Identifier => M,
10815 Object_Definition =>
10816 New_Reference_To (Standard_Integer, Loc),
10817 Expression => Discr));
10818 end;
10820 if Check_Guard then
10821 Guard_Open :=
10822 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
10824 Append_To (Decls,
10825 Make_Object_Declaration (Loc,
10826 Defining_Identifier => Guard_Open,
10827 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10828 Expression => New_Reference_To (Standard_False, Loc)));
10829 end if;
10831 -- Delay_Count is zero, don't need M and D set (suppress warning)
10833 else
10834 M := Empty;
10835 D := Empty;
10836 end if;
10838 if Present (Terminate_Alt) then
10840 -- If the terminate alternative guard is False, use
10841 -- Simple_Mode; otherwise use Terminate_Mode.
10843 if Present (Condition (Terminate_Alt)) then
10844 Select_Mode := Make_If_Expression (Loc,
10845 New_List (Condition (Terminate_Alt),
10846 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
10847 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
10848 else
10849 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
10850 end if;
10852 elsif Else_Present or Delay_Count > 0 then
10853 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
10855 else
10856 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
10857 end if;
10859 Select_Call := Make_Select_Call (Select_Mode);
10860 Append (Select_Call, Stats);
10862 -- Now generate code to act on the result. There is an entry
10863 -- in this case for each accept statement with a non-null body,
10864 -- followed by a branch to the statements that follow the Accept.
10865 -- In the absence of delay alternatives, we generate:
10867 -- case X is
10868 -- when No_Rendezvous => -- omitted if simple mode
10869 -- goto Lab0;
10871 -- when 1 =>
10872 -- P1n;
10873 -- goto Lab1;
10875 -- when 2 =>
10876 -- P2n;
10877 -- goto Lab2;
10879 -- when others =>
10880 -- goto Exit;
10881 -- end case;
10883 -- Lab0: Else_Statements;
10884 -- goto exit;
10886 -- Lab1: Trailing_Statements1;
10887 -- goto Exit;
10889 -- Lab2: Trailing_Statements2;
10890 -- goto Exit;
10891 -- ...
10892 -- Exit:
10894 -- Generate label for common exit
10896 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
10898 -- First entry is the default case, when no rendezvous is possible
10900 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10902 if Else_Present then
10904 -- If no rendezvous is possible, the else part is executed
10906 Lab := Make_And_Declare_Label (0);
10907 Alt_Stats := New_List (
10908 Make_Goto_Statement (Loc,
10909 Name => New_Copy (Identifier (Lab))));
10911 Append (Lab, Trailing_List);
10912 Append_List (Else_Statements (N), Trailing_List);
10913 Append_To (Trailing_List,
10914 Make_Goto_Statement (Loc,
10915 Name => New_Copy (Identifier (End_Lab))));
10916 else
10917 Alt_Stats := New_List (
10918 Make_Goto_Statement (Loc,
10919 Name => New_Copy (Identifier (End_Lab))));
10920 end if;
10922 Append_To (Alt_List,
10923 Make_Case_Statement_Alternative (Loc,
10924 Discrete_Choices => Choices,
10925 Statements => Alt_Stats));
10927 -- We make use of the fact that Accept_Index is an integer type, and
10928 -- generate successive literals for entries for each accept. Only those
10929 -- for which there is a body or trailing statements get a case entry.
10931 Alt := First (Select_Alternatives (N));
10932 Proc := First (Body_List);
10933 while Present (Alt) loop
10935 if Nkind (Alt) = N_Accept_Alternative then
10936 Process_Accept_Alternative (Alt, Index, Proc);
10937 Index := Index + 1;
10939 if Present
10940 (Handled_Statement_Sequence (Accept_Statement (Alt)))
10941 then
10942 Next (Proc);
10943 end if;
10945 elsif Nkind (Alt) = N_Delay_Alternative then
10946 Process_Delay_Alternative (Alt, Delay_Num);
10947 Delay_Num := Delay_Num + 1;
10948 end if;
10950 Next (Alt);
10951 end loop;
10953 -- An others choice is always added to the main case, as well
10954 -- as the delay case (to satisfy the compiler).
10956 Append_To (Alt_List,
10957 Make_Case_Statement_Alternative (Loc,
10958 Discrete_Choices =>
10959 New_List (Make_Others_Choice (Loc)),
10960 Statements =>
10961 New_List (Make_Goto_Statement (Loc,
10962 Name => New_Copy (Identifier (End_Lab))))));
10964 Accept_Case := New_List (
10965 Make_Case_Statement (Loc,
10966 Expression => New_Reference_To (Xnam, Loc),
10967 Alternatives => Alt_List));
10969 Append_List (Trailing_List, Accept_Case);
10970 Append_List (Body_List, Decls);
10972 -- Construct case statement for trailing statements of delay
10973 -- alternatives, if there are several of them.
10975 if Delay_Count > 1 then
10976 Append_To (Delay_Alt_List,
10977 Make_Case_Statement_Alternative (Loc,
10978 Discrete_Choices =>
10979 New_List (Make_Others_Choice (Loc)),
10980 Statements =>
10981 New_List (Make_Null_Statement (Loc))));
10983 Delay_Case := New_List (
10984 Make_Case_Statement (Loc,
10985 Expression => New_Reference_To (Delay_Index, Loc),
10986 Alternatives => Delay_Alt_List));
10987 else
10988 Delay_Case := Delay_Alt_List;
10989 end if;
10991 -- If there are no delay alternatives, we append the case statement
10992 -- to the statement list.
10994 if Delay_Count = 0 then
10995 Append_List (Accept_Case, Stats);
10997 -- Delay alternatives present
10999 else
11000 -- If delay alternatives are present we generate:
11002 -- find minimum delay.
11003 -- DX := minimum delay;
11004 -- M := <delay mode>;
11005 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11006 -- DX, MX, X);
11008 -- if X = No_Rendezvous then
11009 -- case statement for delay statements.
11010 -- else
11011 -- case statement for accept alternatives.
11012 -- end if;
11014 declare
11015 Cases : Node_Id;
11016 Stmt : Node_Id;
11017 Parms : List_Id;
11018 Parm : Node_Id;
11019 Conv : Node_Id;
11021 begin
11022 -- The type of the delay expression is known to be legal
11024 if Time_Type = Standard_Duration then
11025 Conv := New_Reference_To (Delay_Min, Loc);
11027 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11028 Conv := Make_Function_Call (Loc,
11029 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
11030 New_List (New_Reference_To (Delay_Min, Loc)));
11032 else
11033 pragma Assert
11034 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11036 Conv := Make_Function_Call (Loc,
11037 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
11038 New_List (New_Reference_To (Delay_Min, Loc)));
11039 end if;
11041 Stmt := Make_Assignment_Statement (Loc,
11042 Name => New_Reference_To (D, Loc),
11043 Expression => Conv);
11045 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11047 Parms := Parameter_Associations (Select_Call);
11048 Parm := First (Parms);
11050 while Present (Parm) and then Parm /= Select_Mode loop
11051 Next (Parm);
11052 end loop;
11054 pragma Assert (Present (Parm));
11055 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
11056 Analyze (Parm);
11058 -- Prepare two new parameters of Duration and Delay_Mode type
11059 -- which represent the value and the mode of the minimum delay.
11061 Next (Parm);
11062 Insert_After (Parm, New_Reference_To (M, Loc));
11063 Insert_After (Parm, New_Reference_To (D, Loc));
11065 -- Create a call to RTS
11067 Rewrite (Select_Call,
11068 Make_Procedure_Call_Statement (Loc,
11069 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
11070 Parameter_Associations => Parms));
11072 -- This new call should follow the calculation of the minimum
11073 -- delay.
11075 Insert_List_Before (Select_Call, Delay_List);
11077 if Check_Guard then
11078 Stmt :=
11079 Make_Implicit_If_Statement (N,
11080 Condition => New_Reference_To (Guard_Open, Loc),
11081 Then_Statements => New_List (
11082 New_Copy_Tree (Stmt),
11083 New_Copy_Tree (Select_Call)),
11084 Else_Statements => Accept_Or_Raise);
11085 Rewrite (Select_Call, Stmt);
11086 else
11087 Insert_Before (Select_Call, Stmt);
11088 end if;
11090 Cases :=
11091 Make_Implicit_If_Statement (N,
11092 Condition => Make_Op_Eq (Loc,
11093 Left_Opnd => New_Reference_To (Xnam, Loc),
11094 Right_Opnd =>
11095 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
11097 Then_Statements => Delay_Case,
11098 Else_Statements => Accept_Case);
11100 Append (Cases, Stats);
11101 end;
11102 end if;
11103 Append (End_Lab, Stats);
11105 -- Replace accept statement with appropriate block
11107 Rewrite (N,
11108 Make_Block_Statement (Loc,
11109 Declarations => Decls,
11110 Handled_Statement_Sequence =>
11111 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11112 Analyze (N);
11114 -- Note: have to worry more about abort deferral in above code ???
11116 -- Final step is to unstack the Accept_Address entries for all accept
11117 -- statements appearing in accept alternatives in the select statement
11119 Alt := First (Alts);
11120 while Present (Alt) loop
11121 if Nkind (Alt) = N_Accept_Alternative then
11122 Remove_Last_Elmt (Accept_Address
11123 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11124 end if;
11126 Next (Alt);
11127 end loop;
11128 end Expand_N_Selective_Accept;
11130 --------------------------------------
11131 -- Expand_N_Single_Task_Declaration --
11132 --------------------------------------
11134 -- Single task declarations should never be present after semantic
11135 -- analysis, since we expect them to be replaced by a declaration of an
11136 -- anonymous task type, followed by a declaration of the task object. We
11137 -- include this routine to make sure that is happening!
11139 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11140 begin
11141 raise Program_Error;
11142 end Expand_N_Single_Task_Declaration;
11144 ------------------------
11145 -- Expand_N_Task_Body --
11146 ------------------------
11148 -- Given a task body
11150 -- task body tname is
11151 -- <declarations>
11152 -- begin
11153 -- <statements>
11154 -- end x;
11156 -- This expansion routine converts it into a procedure and sets the
11157 -- elaboration flag for the procedure to true, to represent the fact
11158 -- that the task body is now elaborated:
11160 -- procedure tnameB (_Task : access tnameV) is
11161 -- discriminal : dtype renames _Task.discriminant;
11163 -- procedure _clean is
11164 -- begin
11165 -- Abort_Defer.all;
11166 -- Complete_Task;
11167 -- Abort_Undefer.all;
11168 -- return;
11169 -- end _clean;
11171 -- begin
11172 -- Abort_Undefer.all;
11173 -- <declarations>
11174 -- System.Task_Stages.Complete_Activation;
11175 -- <statements>
11176 -- at end
11177 -- _clean;
11178 -- end tnameB;
11180 -- tnameE := True;
11182 -- In addition, if the task body is an activator, then a call to activate
11183 -- tasks is added at the start of the statements, before the call to
11184 -- Complete_Activation, and if in addition the task is a master then it
11185 -- must be established as a master. These calls are inserted and analyzed
11186 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11187 -- expanded.
11189 -- There is one discriminal declaration line generated for each
11190 -- discriminant that is present to provide an easy reference point for
11191 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11193 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11194 -- task body procedures have a profile (Arg : System.Address). That is
11195 -- needed because GNARLI has to use the same access-to-subprogram type
11196 -- for all task types. We depend here on knowing that in GNAT, passing
11197 -- an address argument by value is identical to passing a record value
11198 -- by access (in either case a single pointer is passed), so even though
11199 -- this procedure has the wrong profile. In fact it's all OK, since the
11200 -- callings sequence is identical.
11202 procedure Expand_N_Task_Body (N : Node_Id) is
11203 Loc : constant Source_Ptr := Sloc (N);
11204 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11205 Call : Node_Id;
11206 New_N : Node_Id;
11208 Insert_Nod : Node_Id;
11209 -- Used to determine the proper location of wrapper body insertions
11211 begin
11212 -- Add renaming declarations for discriminals and a declaration for the
11213 -- entry family index (if applicable).
11215 Install_Private_Data_Declarations
11216 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11218 -- Add a call to Abort_Undefer at the very beginning of the task
11219 -- body since this body is called with abort still deferred.
11221 if Abort_Allowed then
11222 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11223 Insert_Before
11224 (First (Statements (Handled_Statement_Sequence (N))), Call);
11225 Analyze (Call);
11226 end if;
11228 -- The statement part has already been protected with an at_end and
11229 -- cleanup actions. The call to Complete_Activation must be placed
11230 -- at the head of the sequence of statements of that block. The
11231 -- declarations have been merged in this sequence of statements but
11232 -- the first real statement is accessible from the First_Real_Statement
11233 -- field (which was set for exactly this purpose).
11235 if Restricted_Profile then
11236 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11237 else
11238 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11239 end if;
11241 Insert_Before
11242 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11243 Analyze (Call);
11245 New_N :=
11246 Make_Subprogram_Body (Loc,
11247 Specification => Build_Task_Proc_Specification (Ttyp),
11248 Declarations => Declarations (N),
11249 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11251 -- If the task contains generic instantiations, cleanup actions are
11252 -- delayed until after instantiation. Transfer the activation chain to
11253 -- the subprogram, to insure that the activation call is properly
11254 -- generated. It the task body contains inner tasks, indicate that the
11255 -- subprogram is a task master.
11257 if Delay_Cleanups (Ttyp) then
11258 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11259 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11260 end if;
11262 Rewrite (N, New_N);
11263 Analyze (N);
11265 -- Set elaboration flag immediately after task body. If the body is a
11266 -- subunit, the flag is set in the declarative part containing the stub.
11268 if Nkind (Parent (N)) /= N_Subunit then
11269 Insert_After (N,
11270 Make_Assignment_Statement (Loc,
11271 Name =>
11272 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11273 Expression => New_Reference_To (Standard_True, Loc)));
11274 end if;
11276 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11277 -- the task body. At this point all wrapper specs have been created,
11278 -- frozen and included in the dispatch table for the task type.
11280 if Ada_Version >= Ada_2005 then
11281 if Nkind (Parent (N)) = N_Subunit then
11282 Insert_Nod := Corresponding_Stub (Parent (N));
11283 else
11284 Insert_Nod := N;
11285 end if;
11287 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11288 end if;
11289 end Expand_N_Task_Body;
11291 ------------------------------------
11292 -- Expand_N_Task_Type_Declaration --
11293 ------------------------------------
11295 -- We have several things to do. First we must create a Boolean flag used
11296 -- to mark if the body is elaborated yet. This variable gets set to True
11297 -- when the body of the task is elaborated (we can't rely on the normal
11298 -- ABE mechanism for the task body, since we need to pass an access to
11299 -- this elaboration boolean to the runtime routines).
11301 -- taskE : aliased Boolean := False;
11303 -- Next a variable is declared to hold the task stack size (either the
11304 -- default : Unspecified_Size, or a value that is set by a pragma
11305 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11306 -- the variable is initialized with this value:
11308 -- taskZ : Size_Type := Unspecified_Size;
11309 -- or
11310 -- taskZ : Size_Type := Size_Type (size_expression);
11312 -- Note: No variable is needed to hold the task relative deadline since
11313 -- its value would never be static because the parameter is of a private
11314 -- type (Ada.Real_Time.Time_Span).
11316 -- Next we create a corresponding record type declaration used to represent
11317 -- values of this task. The general form of this type declaration is
11319 -- type taskV (discriminants) is record
11320 -- _Task_Id : Task_Id;
11321 -- entry_family : array (bounds) of Void;
11322 -- _Priority : Integer := priority_expression;
11323 -- _Size : Size_Type := size_expression;
11324 -- _Task_Info : Task_Info_Type := task_info_expression;
11325 -- _CPU : Integer := cpu_range_expression;
11326 -- _Relative_Deadline : Time_Span := time_span_expression;
11327 -- _Domain : Dispatching_Domain := dd_expression;
11328 -- end record;
11330 -- The discriminants are present only if the corresponding task type has
11331 -- discriminants, and they exactly mirror the task type discriminants.
11333 -- The Id field is always present. It contains the Task_Id value, as set by
11334 -- the call to Create_Task. Note that although the task is limited, the
11335 -- task value record type is not limited, so there is no problem in passing
11336 -- this field as an out parameter to Create_Task.
11338 -- One entry_family component is present for each entry family in the task
11339 -- definition. The bounds correspond to the bounds of the entry family
11340 -- (which may depend on discriminants). The element type is void, since we
11341 -- only need the bounds information for determining the entry index. Note
11342 -- that the use of an anonymous array would normally be illegal in this
11343 -- context, but this is a parser check, and the semantics is quite prepared
11344 -- to handle such a case.
11346 -- The _Size field is present only if a Storage_Size pragma appears in the
11347 -- task definition. The expression captures the argument that was present
11348 -- in the pragma, and is used to override the task stack size otherwise
11349 -- associated with the task type.
11351 -- The _Priority field is present only if the task entity has a Priority or
11352 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11353 -- definition clause). It will be filled at the freeze point, when the
11354 -- record init proc is built, to capture the expression of the rep item
11355 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11356 -- here since aspect evaluations are delayed till the freeze point.
11358 -- The _Task_Info field is present only if a Task_Info pragma appears in
11359 -- the task definition. The expression captures the argument that was
11360 -- present in the pragma, and is used to provide the Task_Image parameter
11361 -- to the call to Create_Task.
11363 -- The _CPU field is present only if the task entity has a CPU rep item
11364 -- (pragma, aspect specification or attribute definition clause). It will
11365 -- be filled at the freeze point, when the record init proc is built, to
11366 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11367 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11368 -- are delayed till the freeze point.
11370 -- The _Relative_Deadline field is present only if a Relative_Deadline
11371 -- pragma appears in the task definition. The expression captures the
11372 -- argument that was present in the pragma, and is used to provide the
11373 -- Relative_Deadline parameter to the call to Create_Task.
11375 -- The _Domain field is present only if the task entity has a
11376 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11377 -- definition clause). It will be filled at the freeze point, when the
11378 -- record init proc is built, to capture the expression of the rep item
11379 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11380 -- here since aspect evaluations are delayed till the freeze point.
11382 -- When a task is declared, an instance of the task value record is
11383 -- created. The elaboration of this declaration creates the correct bounds
11384 -- for the entry families, and also evaluates the size, priority, and
11385 -- task_Info expressions if needed. The initialization routine for the task
11386 -- type itself then calls Create_Task with appropriate parameters to
11387 -- initialize the value of the Task_Id field.
11389 -- Note: the address of this record is passed as the "Discriminants"
11390 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11391 -- body procedure, it does not matter that it does not quite match the
11392 -- GNARLI model of what is being passed (the record contains more than just
11393 -- the discriminants, but the discriminants can be found from the record
11394 -- value).
11396 -- The Entity_Id for this created record type is placed in the
11397 -- Corresponding_Record_Type field of the associated task type entity.
11399 -- Next we create a procedure specification for the task body procedure:
11401 -- procedure taskB (_Task : access taskV);
11403 -- Note that this must come after the record type declaration, since
11404 -- the spec refers to this type. It turns out that the initialization
11405 -- procedure for the value type references the task body spec, but that's
11406 -- fine, since it won't be generated till the freeze point for the type,
11407 -- which is certainly after the task body spec declaration.
11409 -- Finally, we set the task index value field of the entry attribute in
11410 -- the case of a simple entry.
11412 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11413 Loc : constant Source_Ptr := Sloc (N);
11414 TaskId : constant Entity_Id := Defining_Identifier (N);
11415 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11416 Tasknm : constant Name_Id := Chars (Tasktyp);
11417 Taskdef : constant Node_Id := Task_Definition (N);
11419 Body_Decl : Node_Id;
11420 Cdecls : List_Id;
11421 Decl_Stack : Node_Id;
11422 Elab_Decl : Node_Id;
11423 Ent_Stack : Entity_Id;
11424 Proc_Spec : Node_Id;
11425 Rec_Decl : Node_Id;
11426 Rec_Ent : Entity_Id;
11427 Size_Decl : Entity_Id;
11428 Task_Size : Node_Id;
11430 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11431 -- Searches the task definition T for the first occurrence of the pragma
11432 -- Relative Deadline. The caller has ensured that the pragma is present
11433 -- in the task definition. Note that this routine cannot be implemented
11434 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11435 -- not chained because their expansion into a procedure call statement
11436 -- would cause a break in the chain.
11438 ----------------------------------
11439 -- Get_Relative_Deadline_Pragma --
11440 ----------------------------------
11442 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11443 N : Node_Id;
11445 begin
11446 N := First (Visible_Declarations (T));
11447 while Present (N) loop
11448 if Nkind (N) = N_Pragma
11449 and then Pragma_Name (N) = Name_Relative_Deadline
11450 then
11451 return N;
11452 end if;
11454 Next (N);
11455 end loop;
11457 N := First (Private_Declarations (T));
11458 while Present (N) loop
11459 if Nkind (N) = N_Pragma
11460 and then Pragma_Name (N) = Name_Relative_Deadline
11461 then
11462 return N;
11463 end if;
11465 Next (N);
11466 end loop;
11468 raise Program_Error;
11469 end Get_Relative_Deadline_Pragma;
11471 -- Start of processing for Expand_N_Task_Type_Declaration
11473 begin
11474 -- If already expanded, nothing to do
11476 if Present (Corresponding_Record_Type (Tasktyp)) then
11477 return;
11478 end if;
11480 -- Here we will do the expansion
11482 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11484 Rec_Ent := Defining_Identifier (Rec_Decl);
11485 Cdecls := Component_Items (Component_List
11486 (Type_Definition (Rec_Decl)));
11488 Qualify_Entity_Names (N);
11490 -- First create the elaboration variable
11492 Elab_Decl :=
11493 Make_Object_Declaration (Loc,
11494 Defining_Identifier =>
11495 Make_Defining_Identifier (Sloc (Tasktyp),
11496 Chars => New_External_Name (Tasknm, 'E')),
11497 Aliased_Present => True,
11498 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
11499 Expression => New_Reference_To (Standard_False, Loc));
11501 Insert_After (N, Elab_Decl);
11503 -- Next create the declaration of the size variable (tasknmZ)
11505 Set_Storage_Size_Variable (Tasktyp,
11506 Make_Defining_Identifier (Sloc (Tasktyp),
11507 Chars => New_External_Name (Tasknm, 'Z')));
11509 if Present (Taskdef)
11510 and then Has_Storage_Size_Pragma (Taskdef)
11511 and then
11512 Is_Static_Expression
11513 (Expression
11514 (First (Pragma_Argument_Associations
11515 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11516 then
11517 Size_Decl :=
11518 Make_Object_Declaration (Loc,
11519 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11520 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
11521 Expression =>
11522 Convert_To (RTE (RE_Size_Type),
11523 Relocate_Node
11524 (Expression (First (Pragma_Argument_Associations
11525 (Get_Rep_Pragma
11526 (TaskId, Name_Storage_Size)))))));
11528 else
11529 Size_Decl :=
11530 Make_Object_Declaration (Loc,
11531 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11532 Object_Definition =>
11533 New_Reference_To (RTE (RE_Size_Type), Loc),
11534 Expression =>
11535 New_Reference_To (RTE (RE_Unspecified_Size), Loc));
11536 end if;
11538 Insert_After (Elab_Decl, Size_Decl);
11540 -- Next build the rest of the corresponding record declaration. This is
11541 -- done last, since the corresponding record initialization procedure
11542 -- will reference the previously created entities.
11544 -- Fill in the component declarations -- first the _Task_Id field
11546 Append_To (Cdecls,
11547 Make_Component_Declaration (Loc,
11548 Defining_Identifier =>
11549 Make_Defining_Identifier (Loc, Name_uTask_Id),
11550 Component_Definition =>
11551 Make_Component_Definition (Loc,
11552 Aliased_Present => False,
11553 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
11554 Loc))));
11556 -- Declare static ATCB (that is, created by the expander) if we are
11557 -- using the Restricted run time.
11559 if Restricted_Profile then
11560 Append_To (Cdecls,
11561 Make_Component_Declaration (Loc,
11562 Defining_Identifier =>
11563 Make_Defining_Identifier (Loc, Name_uATCB),
11565 Component_Definition =>
11566 Make_Component_Definition (Loc,
11567 Aliased_Present => True,
11568 Subtype_Indication => Make_Subtype_Indication (Loc,
11569 Subtype_Mark =>
11570 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11572 Constraint =>
11573 Make_Index_Or_Discriminant_Constraint (Loc,
11574 Constraints =>
11575 New_List (Make_Integer_Literal (Loc, 0)))))));
11577 end if;
11579 -- Declare static stack (that is, created by the expander) if we are
11580 -- using the Restricted run time on a bare board configuration.
11582 if Restricted_Profile
11583 and then Preallocated_Stacks_On_Target
11584 then
11585 -- First we need to extract the appropriate stack size
11587 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11589 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11590 declare
11591 Expr_N : constant Node_Id :=
11592 Expression (First (
11593 Pragma_Argument_Associations (
11594 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11595 Etyp : constant Entity_Id := Etype (Expr_N);
11596 P : constant Node_Id := Parent (Expr_N);
11598 begin
11599 -- The stack is defined inside the corresponding record.
11600 -- Therefore if the size of the stack is set by means of
11601 -- a discriminant, we must reference the discriminant of the
11602 -- corresponding record type.
11604 if Nkind (Expr_N) in N_Has_Entity
11605 and then Present (Discriminal_Link (Entity (Expr_N)))
11606 then
11607 Task_Size :=
11608 New_Reference_To
11609 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11610 Loc);
11611 Set_Parent (Task_Size, P);
11612 Set_Etype (Task_Size, Etyp);
11613 Set_Analyzed (Task_Size);
11615 else
11616 Task_Size := Relocate_Node (Expr_N);
11617 end if;
11618 end;
11620 else
11621 Task_Size :=
11622 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
11623 end if;
11625 Decl_Stack := Make_Component_Declaration (Loc,
11626 Defining_Identifier => Ent_Stack,
11628 Component_Definition =>
11629 Make_Component_Definition (Loc,
11630 Aliased_Present => True,
11631 Subtype_Indication => Make_Subtype_Indication (Loc,
11632 Subtype_Mark =>
11633 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11635 Constraint =>
11636 Make_Index_Or_Discriminant_Constraint (Loc,
11637 Constraints => New_List (Make_Range (Loc,
11638 Low_Bound => Make_Integer_Literal (Loc, 1),
11639 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11640 Task_Size)))))));
11642 Append_To (Cdecls, Decl_Stack);
11644 -- The appropriate alignment for the stack is ensured by the run-time
11645 -- code in charge of task creation.
11647 end if;
11649 -- Add components for entry families
11651 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11653 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11654 -- item is present.
11656 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11657 Append_To (Cdecls,
11658 Make_Component_Declaration (Loc,
11659 Defining_Identifier =>
11660 Make_Defining_Identifier (Loc, Name_uPriority),
11661 Component_Definition =>
11662 Make_Component_Definition (Loc,
11663 Aliased_Present => False,
11664 Subtype_Indication =>
11665 New_Reference_To (Standard_Integer, Loc))));
11666 end if;
11668 -- Add the _Size component if a Storage_Size pragma is present
11670 if Present (Taskdef)
11671 and then Has_Storage_Size_Pragma (Taskdef)
11672 then
11673 Append_To (Cdecls,
11674 Make_Component_Declaration (Loc,
11675 Defining_Identifier =>
11676 Make_Defining_Identifier (Loc, Name_uSize),
11678 Component_Definition =>
11679 Make_Component_Definition (Loc,
11680 Aliased_Present => False,
11681 Subtype_Indication =>
11682 New_Reference_To (RTE (RE_Size_Type), Loc)),
11684 Expression =>
11685 Convert_To (RTE (RE_Size_Type),
11686 Relocate_Node (
11687 Expression (First (
11688 Pragma_Argument_Associations (
11689 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11690 end if;
11692 -- Add the _Task_Info component if a Task_Info pragma is present
11694 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11695 Append_To (Cdecls,
11696 Make_Component_Declaration (Loc,
11697 Defining_Identifier =>
11698 Make_Defining_Identifier (Loc, Name_uTask_Info),
11700 Component_Definition =>
11701 Make_Component_Definition (Loc,
11702 Aliased_Present => False,
11703 Subtype_Indication =>
11704 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
11706 Expression => New_Copy (
11707 Expression (First (
11708 Pragma_Argument_Associations (
11709 Get_Rep_Pragma
11710 (TaskId, Name_Task_Info, Check_Parents => False)))))));
11711 end if;
11713 -- Add the _CPU component if a CPU rep item is present
11715 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
11716 Append_To (Cdecls,
11717 Make_Component_Declaration (Loc,
11718 Defining_Identifier =>
11719 Make_Defining_Identifier (Loc, Name_uCPU),
11721 Component_Definition =>
11722 Make_Component_Definition (Loc,
11723 Aliased_Present => False,
11724 Subtype_Indication =>
11725 New_Reference_To (RTE (RE_CPU_Range), Loc))));
11726 end if;
11728 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11729 -- present. If we are using a restricted run time this component will
11730 -- not be added (deadlines are not allowed by the Ravenscar profile).
11732 if not Restricted_Profile
11733 and then Present (Taskdef)
11734 and then Has_Relative_Deadline_Pragma (Taskdef)
11735 then
11736 Append_To (Cdecls,
11737 Make_Component_Declaration (Loc,
11738 Defining_Identifier =>
11739 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11741 Component_Definition =>
11742 Make_Component_Definition (Loc,
11743 Aliased_Present => False,
11744 Subtype_Indication =>
11745 New_Reference_To (RTE (RE_Time_Span), Loc)),
11747 Expression =>
11748 Convert_To (RTE (RE_Time_Span),
11749 Relocate_Node (
11750 Expression (First (
11751 Pragma_Argument_Associations (
11752 Get_Relative_Deadline_Pragma (Taskdef))))))));
11753 end if;
11755 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
11756 -- item is present. If we are using a restricted run time this component
11757 -- will not be added (dispatching domains are not allowed by the
11758 -- Ravenscar profile).
11760 if not Restricted_Profile
11761 and then
11762 Has_Rep_Item
11763 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
11764 then
11765 Append_To (Cdecls,
11766 Make_Component_Declaration (Loc,
11767 Defining_Identifier =>
11768 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
11770 Component_Definition =>
11771 Make_Component_Definition (Loc,
11772 Aliased_Present => False,
11773 Subtype_Indication =>
11774 New_Reference_To
11775 (RTE (RE_Dispatching_Domain_Access), Loc))));
11776 end if;
11778 Insert_After (Size_Decl, Rec_Decl);
11780 -- Analyze the record declaration immediately after construction,
11781 -- because the initialization procedure is needed for single task
11782 -- declarations before the next entity is analyzed.
11784 Analyze (Rec_Decl);
11786 -- Create the declaration of the task body procedure
11788 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
11789 Body_Decl :=
11790 Make_Subprogram_Declaration (Loc,
11791 Specification => Proc_Spec);
11793 Insert_After (Rec_Decl, Body_Decl);
11795 -- The subprogram does not comes from source, so we have to indicate the
11796 -- need for debugging information explicitly.
11798 if Comes_From_Source (Original_Node (N)) then
11799 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
11800 end if;
11802 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
11803 -- the corresponding record has been frozen.
11805 if Ada_Version >= Ada_2005 then
11806 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
11807 end if;
11809 -- Ada 2005 (AI-345): We must defer freezing to allow further
11810 -- declaration of primitive subprograms covering task interfaces
11812 if Ada_Version <= Ada_95 then
11814 -- Now we can freeze the corresponding record. This needs manually
11815 -- freezing, since it is really part of the task type, and the task
11816 -- type is frozen at this stage. We of course need the initialization
11817 -- procedure for this corresponding record type and we won't get it
11818 -- in time if we don't freeze now.
11820 declare
11821 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
11822 begin
11823 if Is_Non_Empty_List (L) then
11824 Insert_List_After (Body_Decl, L);
11825 end if;
11826 end;
11827 end if;
11829 -- Complete the expansion of access types to the current task type, if
11830 -- any were declared.
11832 Expand_Previous_Access_Type (Tasktyp);
11834 -- Create wrappers for entries that have pre/postconditions
11836 declare
11837 Ent : Entity_Id;
11839 begin
11840 Ent := First_Entity (Tasktyp);
11841 while Present (Ent) loop
11842 if Ekind_In (Ent, E_Entry, E_Entry_Family)
11843 and then Present (Spec_PPC_List (Contract (Ent)))
11844 then
11845 Build_PPC_Wrapper (Ent, N);
11846 end if;
11848 Next_Entity (Ent);
11849 end loop;
11850 end;
11851 end Expand_N_Task_Type_Declaration;
11853 -------------------------------
11854 -- Expand_N_Timed_Entry_Call --
11855 -------------------------------
11857 -- A timed entry call in normal case is not implemented using ATC mechanism
11858 -- anymore for efficiency reason.
11860 -- select
11861 -- T.E;
11862 -- S1;
11863 -- or
11864 -- delay D;
11865 -- S2;
11866 -- end select;
11868 -- is expanded as follows:
11870 -- 1) When T.E is a task entry_call;
11872 -- declare
11873 -- B : Boolean;
11874 -- X : Task_Entry_Index := <entry index>;
11875 -- DX : Duration := To_Duration (D);
11876 -- M : Delay_Mode := <discriminant>;
11877 -- P : parms := (parm, parm, parm);
11879 -- begin
11880 -- Timed_Protected_Entry_Call
11881 -- (<acceptor-task>, X, P'Address, DX, M, B);
11882 -- if B then
11883 -- S1;
11884 -- else
11885 -- S2;
11886 -- end if;
11887 -- end;
11889 -- 2) When T.E is a protected entry_call;
11891 -- declare
11892 -- B : Boolean;
11893 -- X : Protected_Entry_Index := <entry index>;
11894 -- DX : Duration := To_Duration (D);
11895 -- M : Delay_Mode := <discriminant>;
11896 -- P : parms := (parm, parm, parm);
11898 -- begin
11899 -- Timed_Protected_Entry_Call
11900 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
11901 -- if B then
11902 -- S1;
11903 -- else
11904 -- S2;
11905 -- end if;
11906 -- end;
11908 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call;
11910 -- declare
11911 -- B : Boolean := False;
11912 -- C : Ada.Tags.Prim_Op_Kind;
11913 -- DX : Duration := To_Duration (D)
11914 -- K : Ada.Tags.Tagged_Kind :=
11915 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
11916 -- M : Integer :=...;
11917 -- P : Parameters := (Param1 .. ParamN);
11918 -- S : Integer;
11920 -- begin
11921 -- if K = Ada.Tags.TK_Limited_Tagged then
11922 -- <dispatching-call>;
11923 -- <triggering-statements>
11925 -- else
11926 -- S :=
11927 -- Ada.Tags.Get_Offset_Index
11928 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
11930 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
11932 -- if C = POK_Protected_Entry
11933 -- or else C = POK_Task_Entry
11934 -- then
11935 -- Param1 := P.Param1;
11936 -- ...
11937 -- ParamN := P.ParamN;
11938 -- end if;
11940 -- if B then
11941 -- if C = POK_Procedure
11942 -- or else C = POK_Protected_Procedure
11943 -- or else C = POK_Task_Procedure
11944 -- then
11945 -- <dispatching-call>;
11946 -- end if;
11948 -- <triggering-statements>
11949 -- else
11950 -- <timed-statements>
11951 -- end if;
11952 -- end if;
11953 -- end;
11955 -- The triggering statement and the sequence of timed statements have not
11956 -- been analyzed yet (see Analyzed_Timed_Entry_Call). They may contain
11957 -- local declarations, and therefore the copies that are made during
11958 -- expansion must be disjoint, as for any other inlining.
11960 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
11961 Loc : constant Source_Ptr := Sloc (N);
11963 Actuals : List_Id;
11964 Blk_Typ : Entity_Id;
11965 Call : Node_Id;
11966 Call_Ent : Entity_Id;
11967 Conc_Typ_Stmts : List_Id;
11968 Concval : Node_Id;
11969 D_Alt : constant Node_Id := Delay_Alternative (N);
11970 D_Conv : Node_Id;
11971 D_Disc : Node_Id;
11972 D_Stat : Node_Id := Delay_Statement (D_Alt);
11973 D_Stats : List_Id;
11974 D_Type : Entity_Id;
11975 Decls : List_Id;
11976 Dummy : Node_Id;
11977 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
11978 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
11979 E_Stats : List_Id;
11980 Ename : Node_Id;
11981 Formals : List_Id;
11982 Index : Node_Id;
11983 Is_Disp_Select : Boolean;
11984 Lim_Typ_Stmts : List_Id;
11985 N_Stats : List_Id;
11986 Obj : Entity_Id;
11987 Param : Node_Id;
11988 Params : List_Id;
11989 Stmt : Node_Id;
11990 Stmts : List_Id;
11991 Unpack : List_Id;
11993 B : Entity_Id; -- Call status flag
11994 C : Entity_Id; -- Call kind
11995 D : Entity_Id; -- Delay
11996 K : Entity_Id; -- Tagged kind
11997 M : Entity_Id; -- Delay mode
11998 P : Entity_Id; -- Parameter block
11999 S : Entity_Id; -- Primitive operation slot
12001 begin
12002 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12003 -- was already reported on spec, so do not attempt to expand the call.
12005 if Restriction_Active (No_Select_Statements) then
12006 return;
12007 end if;
12009 Process_Statements_For_Controlled_Objects (E_Alt);
12010 Process_Statements_For_Controlled_Objects (D_Alt);
12012 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12014 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12015 -- may wrap them in blocks.
12017 E_Stats := Statements (E_Alt);
12018 D_Stats := Statements (D_Alt);
12020 -- The arguments in the call may require dynamic allocation, and the
12021 -- call statement may have been transformed into a block. The block
12022 -- may contain additional declarations for internal entities, and the
12023 -- original call is found by sequential search.
12025 if Nkind (E_Call) = N_Block_Statement then
12026 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12027 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12028 N_Entry_Call_Statement)
12029 loop
12030 Next (E_Call);
12031 end loop;
12032 end if;
12034 Is_Disp_Select :=
12035 Ada_Version >= Ada_2005
12036 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12038 if Is_Disp_Select then
12039 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12041 Decls := New_List;
12042 Stmts := New_List;
12044 -- Generate:
12045 -- B : Boolean := False;
12047 B := Build_B (Loc, Decls);
12049 -- Generate:
12050 -- C : Ada.Tags.Prim_Op_Kind;
12052 C := Build_C (Loc, Decls);
12054 -- Because the analysis of all statements was disabled, manually
12055 -- analyze the delay statement.
12057 Analyze (D_Stat);
12058 D_Stat := Original_Node (D_Stat);
12060 else
12061 -- Build an entry call using Simple_Entry_Call
12063 Extract_Entry (E_Call, Concval, Ename, Index);
12064 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12066 Decls := Declarations (E_Call);
12067 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12069 if No (Decls) then
12070 Decls := New_List;
12071 end if;
12073 -- Generate:
12074 -- B : Boolean;
12076 B := Make_Defining_Identifier (Loc, Name_uB);
12078 Prepend_To (Decls,
12079 Make_Object_Declaration (Loc,
12080 Defining_Identifier => B,
12081 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
12082 end if;
12084 -- Duration and mode processing
12086 D_Type := Base_Type (Etype (Expression (D_Stat)));
12088 -- Use the type of the delay expression (Calendar or Real_Time) to
12089 -- generate the appropriate conversion.
12091 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12092 D_Disc := Make_Integer_Literal (Loc, 0);
12093 D_Conv := Relocate_Node (Expression (D_Stat));
12095 elsif Is_RTE (D_Type, RO_CA_Time) then
12096 D_Disc := Make_Integer_Literal (Loc, 1);
12097 D_Conv :=
12098 Make_Function_Call (Loc,
12099 Name => New_Reference_To (RTE (RO_CA_To_Duration), Loc),
12100 Parameter_Associations =>
12101 New_List (New_Copy (Expression (D_Stat))));
12103 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12104 D_Disc := Make_Integer_Literal (Loc, 2);
12105 D_Conv :=
12106 Make_Function_Call (Loc,
12107 Name => New_Reference_To (RTE (RO_RT_To_Duration), Loc),
12108 Parameter_Associations =>
12109 New_List (New_Copy (Expression (D_Stat))));
12110 end if;
12112 D := Make_Temporary (Loc, 'D');
12114 -- Generate:
12115 -- D : Duration;
12117 Append_To (Decls,
12118 Make_Object_Declaration (Loc,
12119 Defining_Identifier => D,
12120 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
12122 M := Make_Temporary (Loc, 'M');
12124 -- Generate:
12125 -- M : Integer := (0 | 1 | 2);
12127 Append_To (Decls,
12128 Make_Object_Declaration (Loc,
12129 Defining_Identifier => M,
12130 Object_Definition => New_Reference_To (Standard_Integer, Loc),
12131 Expression => D_Disc));
12133 -- Do the assignment at this stage only because the evaluation of the
12134 -- expression must not occur before (see ACVC C97302A).
12136 Append_To (Stmts,
12137 Make_Assignment_Statement (Loc,
12138 Name => New_Reference_To (D, Loc),
12139 Expression => D_Conv));
12141 -- Parameter block processing
12143 -- Manually create the parameter block for dispatching calls. In the
12144 -- case of entries, the block has already been created during the call
12145 -- to Build_Simple_Entry_Call.
12147 if Is_Disp_Select then
12149 -- Tagged kind processing, generate:
12150 -- K : Ada.Tags.Tagged_Kind :=
12151 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12153 K := Build_K (Loc, Decls, Obj);
12155 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12156 P :=
12157 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12159 -- Dispatch table slot processing, generate:
12160 -- S : Integer;
12162 S := Build_S (Loc, Decls);
12164 -- Generate:
12165 -- S := Ada.Tags.Get_Offset_Index
12166 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12168 Conc_Typ_Stmts :=
12169 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12171 -- Generate:
12172 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12174 -- where Obj is the controlling formal parameter, S is the dispatch
12175 -- table slot number of the dispatching operation, P is the wrapped
12176 -- parameter block, D is the duration, M is the duration mode, C is
12177 -- the call kind and B is the call status.
12179 Params := New_List;
12181 Append_To (Params, New_Copy_Tree (Obj));
12182 Append_To (Params, New_Reference_To (S, Loc));
12183 Append_To (Params,
12184 Make_Attribute_Reference (Loc,
12185 Prefix => New_Reference_To (P, Loc),
12186 Attribute_Name => Name_Address));
12187 Append_To (Params, New_Reference_To (D, Loc));
12188 Append_To (Params, New_Reference_To (M, Loc));
12189 Append_To (Params, New_Reference_To (C, Loc));
12190 Append_To (Params, New_Reference_To (B, Loc));
12192 Append_To (Conc_Typ_Stmts,
12193 Make_Procedure_Call_Statement (Loc,
12194 Name =>
12195 New_Reference_To
12196 (Find_Prim_Op
12197 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12198 Parameter_Associations => Params));
12200 -- Generate:
12201 -- if C = POK_Protected_Entry
12202 -- or else C = POK_Task_Entry
12203 -- then
12204 -- Param1 := P.Param1;
12205 -- ...
12206 -- ParamN := P.ParamN;
12207 -- end if;
12209 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12211 -- Generate the if statement only when the packed parameters need
12212 -- explicit assignments to their corresponding actuals.
12214 if Present (Unpack) then
12215 Append_To (Conc_Typ_Stmts,
12216 Make_Implicit_If_Statement (N,
12218 Condition =>
12219 Make_Or_Else (Loc,
12220 Left_Opnd =>
12221 Make_Op_Eq (Loc,
12222 Left_Opnd => New_Reference_To (C, Loc),
12223 Right_Opnd =>
12224 New_Reference_To
12225 (RTE (RE_POK_Protected_Entry), Loc)),
12227 Right_Opnd =>
12228 Make_Op_Eq (Loc,
12229 Left_Opnd => New_Reference_To (C, Loc),
12230 Right_Opnd =>
12231 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
12233 Then_Statements => Unpack));
12234 end if;
12236 -- Generate:
12238 -- if B then
12239 -- if C = POK_Procedure
12240 -- or else C = POK_Protected_Procedure
12241 -- or else C = POK_Task_Procedure
12242 -- then
12243 -- <dispatching-call>
12244 -- end if;
12245 -- <triggering-statements>
12246 -- else
12247 -- <timed-statements>
12248 -- end if;
12250 N_Stats := Copy_Separate_List (E_Stats);
12252 Prepend_To (N_Stats,
12253 Make_Implicit_If_Statement (N,
12255 Condition =>
12256 Make_Or_Else (Loc,
12257 Left_Opnd =>
12258 Make_Op_Eq (Loc,
12259 Left_Opnd => New_Reference_To (C, Loc),
12260 Right_Opnd =>
12261 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
12263 Right_Opnd =>
12264 Make_Or_Else (Loc,
12265 Left_Opnd =>
12266 Make_Op_Eq (Loc,
12267 Left_Opnd => New_Reference_To (C, Loc),
12268 Right_Opnd =>
12269 New_Reference_To (RTE (
12270 RE_POK_Protected_Procedure), Loc)),
12271 Right_Opnd =>
12272 Make_Op_Eq (Loc,
12273 Left_Opnd => New_Reference_To (C, Loc),
12274 Right_Opnd =>
12275 New_Reference_To
12276 (RTE (RE_POK_Task_Procedure), Loc)))),
12278 Then_Statements => New_List (E_Call)));
12280 Append_To (Conc_Typ_Stmts,
12281 Make_Implicit_If_Statement (N,
12282 Condition => New_Reference_To (B, Loc),
12283 Then_Statements => N_Stats,
12284 Else_Statements => D_Stats));
12286 -- Generate:
12287 -- <dispatching-call>;
12288 -- <triggering-statements>
12290 Lim_Typ_Stmts := Copy_Separate_List (E_Stats);
12291 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (E_Call));
12293 -- Generate:
12294 -- if K = Ada.Tags.TK_Limited_Tagged then
12295 -- Lim_Typ_Stmts
12296 -- else
12297 -- Conc_Typ_Stmts
12298 -- end if;
12300 Append_To (Stmts,
12301 Make_Implicit_If_Statement (N,
12302 Condition =>
12303 Make_Op_Eq (Loc,
12304 Left_Opnd => New_Reference_To (K, Loc),
12305 Right_Opnd =>
12306 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
12307 Then_Statements => Lim_Typ_Stmts,
12308 Else_Statements => Conc_Typ_Stmts));
12310 else
12311 -- Skip assignments to temporaries created for in-out parameters.
12312 -- This makes unwarranted assumptions about the shape of the expanded
12313 -- tree for the call, and should be cleaned up ???
12315 Stmt := First (Stmts);
12316 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12317 Next (Stmt);
12318 end loop;
12320 -- Do the assignment at this stage only because the evaluation
12321 -- of the expression must not occur before (see ACVC C97302A).
12323 Insert_Before (Stmt,
12324 Make_Assignment_Statement (Loc,
12325 Name => New_Reference_To (D, Loc),
12326 Expression => D_Conv));
12328 Call := Stmt;
12329 Params := Parameter_Associations (Call);
12331 -- For a protected type, we build a Timed_Protected_Entry_Call
12333 if Is_Protected_Type (Etype (Concval)) then
12335 -- Create a new call statement
12337 Param := First (Params);
12338 while Present (Param)
12339 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12340 loop
12341 Next (Param);
12342 end loop;
12344 Dummy := Remove_Next (Next (Param));
12346 -- Remove garbage is following the Cancel_Param if present
12348 Dummy := Next (Param);
12350 -- Remove the mode of the Protected_Entry_Call call, then remove
12351 -- the Communication_Block of the Protected_Entry_Call call, and
12352 -- finally add Duration and a Delay_Mode parameter
12354 pragma Assert (Present (Param));
12355 Rewrite (Param, New_Reference_To (D, Loc));
12357 Rewrite (Dummy, New_Reference_To (M, Loc));
12359 -- Add a Boolean flag for successful entry call
12361 Append_To (Params, New_Reference_To (B, Loc));
12363 case Corresponding_Runtime_Package (Etype (Concval)) is
12364 when System_Tasking_Protected_Objects_Entries =>
12365 Rewrite (Call,
12366 Make_Procedure_Call_Statement (Loc,
12367 Name =>
12368 New_Reference_To
12369 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12370 Parameter_Associations => Params));
12372 when System_Tasking_Protected_Objects_Single_Entry =>
12373 Param := First (Params);
12374 while Present (Param)
12375 and then not
12376 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
12377 loop
12378 Next (Param);
12379 end loop;
12381 Remove (Param);
12383 Rewrite (Call,
12384 Make_Procedure_Call_Statement (Loc,
12385 Name =>
12386 New_Reference_To
12387 (RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
12388 Parameter_Associations => Params));
12390 when others =>
12391 raise Program_Error;
12392 end case;
12394 -- For the task case, build a Timed_Task_Entry_Call
12396 else
12397 -- Create a new call statement
12399 Append_To (Params, New_Reference_To (D, Loc));
12400 Append_To (Params, New_Reference_To (M, Loc));
12401 Append_To (Params, New_Reference_To (B, Loc));
12403 Rewrite (Call,
12404 Make_Procedure_Call_Statement (Loc,
12405 Name =>
12406 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
12407 Parameter_Associations => Params));
12408 end if;
12410 Append_To (Stmts,
12411 Make_Implicit_If_Statement (N,
12412 Condition => New_Reference_To (B, Loc),
12413 Then_Statements => E_Stats,
12414 Else_Statements => D_Stats));
12415 end if;
12417 Rewrite (N,
12418 Make_Block_Statement (Loc,
12419 Declarations => Decls,
12420 Handled_Statement_Sequence =>
12421 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12423 Analyze (N);
12424 end Expand_N_Timed_Entry_Call;
12426 ----------------------------------------
12427 -- Expand_Protected_Body_Declarations --
12428 ----------------------------------------
12430 procedure Expand_Protected_Body_Declarations
12431 (N : Node_Id;
12432 Spec_Id : Entity_Id)
12434 begin
12435 if No_Run_Time_Mode then
12436 Error_Msg_CRT ("protected body", N);
12437 return;
12439 elsif Full_Expander_Active then
12441 -- Associate discriminals with the first subprogram or entry body to
12442 -- be expanded.
12444 if Present (First_Protected_Operation (Declarations (N))) then
12445 Set_Discriminals (Parent (Spec_Id));
12446 end if;
12447 end if;
12448 end Expand_Protected_Body_Declarations;
12450 -------------------------
12451 -- External_Subprogram --
12452 -------------------------
12454 function External_Subprogram (E : Entity_Id) return Entity_Id is
12455 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12457 begin
12458 -- The internal and external subprograms follow each other on the entity
12459 -- chain. Note that previously private operations had no separate
12460 -- external subprogram. We now create one in all cases, because a
12461 -- private operation may actually appear in an external call, through
12462 -- a 'Access reference used for a callback.
12464 -- If the operation is a function that returns an anonymous access type,
12465 -- the corresponding itype appears before the operation, and must be
12466 -- skipped.
12468 -- This mechanism is fragile, there should be a real link between the
12469 -- two versions of the operation, but there is no place to put it ???
12471 if Is_Access_Type (Next_Entity (Subp)) then
12472 return Next_Entity (Next_Entity (Subp));
12473 else
12474 return Next_Entity (Subp);
12475 end if;
12476 end External_Subprogram;
12478 ------------------------------
12479 -- Extract_Dispatching_Call --
12480 ------------------------------
12482 procedure Extract_Dispatching_Call
12483 (N : Node_Id;
12484 Call_Ent : out Entity_Id;
12485 Object : out Entity_Id;
12486 Actuals : out List_Id;
12487 Formals : out List_Id)
12489 Call_Nam : Node_Id;
12491 begin
12492 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12494 if Present (Original_Node (N)) then
12495 Call_Nam := Name (Original_Node (N));
12496 else
12497 Call_Nam := Name (N);
12498 end if;
12500 -- Retrieve the name of the dispatching procedure. It contains the
12501 -- dispatch table slot number.
12503 loop
12504 case Nkind (Call_Nam) is
12505 when N_Identifier =>
12506 exit;
12508 when N_Selected_Component =>
12509 Call_Nam := Selector_Name (Call_Nam);
12511 when others =>
12512 raise Program_Error;
12514 end case;
12515 end loop;
12517 Actuals := Parameter_Associations (N);
12518 Call_Ent := Entity (Call_Nam);
12519 Formals := Parameter_Specifications (Parent (Call_Ent));
12520 Object := First (Actuals);
12522 if Present (Original_Node (Object)) then
12523 Object := Original_Node (Object);
12524 end if;
12526 -- If the type of the dispatching object is an access type then return
12527 -- an explicit dereference.
12529 if Is_Access_Type (Etype (Object)) then
12530 Object := Make_Explicit_Dereference (Sloc (N), Object);
12531 Analyze (Object);
12532 end if;
12533 end Extract_Dispatching_Call;
12535 -------------------
12536 -- Extract_Entry --
12537 -------------------
12539 procedure Extract_Entry
12540 (N : Node_Id;
12541 Concval : out Node_Id;
12542 Ename : out Node_Id;
12543 Index : out Node_Id)
12545 Nam : constant Node_Id := Name (N);
12547 begin
12548 -- For a simple entry, the name is a selected component, with the
12549 -- prefix being the task value, and the selector being the entry.
12551 if Nkind (Nam) = N_Selected_Component then
12552 Concval := Prefix (Nam);
12553 Ename := Selector_Name (Nam);
12554 Index := Empty;
12556 -- For a member of an entry family, the name is an indexed component
12557 -- where the prefix is a selected component, whose prefix in turn is
12558 -- the task value, and whose selector is the entry family. The single
12559 -- expression in the expressions list of the indexed component is the
12560 -- subscript for the family.
12562 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12563 Concval := Prefix (Prefix (Nam));
12564 Ename := Selector_Name (Prefix (Nam));
12565 Index := First (Expressions (Nam));
12566 end if;
12567 end Extract_Entry;
12569 -------------------
12570 -- Family_Offset --
12571 -------------------
12573 function Family_Offset
12574 (Loc : Source_Ptr;
12575 Hi : Node_Id;
12576 Lo : Node_Id;
12577 Ttyp : Entity_Id;
12578 Cap : Boolean) return Node_Id
12580 Ityp : Entity_Id;
12581 Real_Hi : Node_Id;
12582 Real_Lo : Node_Id;
12584 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12585 -- If one of the bounds is a reference to a discriminant, replace with
12586 -- corresponding discriminal of type. Within the body of a task retrieve
12587 -- the renamed discriminant by simple visibility, using its generated
12588 -- name. Within a protected object, find the original discriminant and
12589 -- replace it with the discriminal of the current protected operation.
12591 ------------------------------
12592 -- Convert_Discriminant_Ref --
12593 ------------------------------
12595 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12596 Loc : constant Source_Ptr := Sloc (Bound);
12597 B : Node_Id;
12598 D : Entity_Id;
12600 begin
12601 if Is_Entity_Name (Bound)
12602 and then Ekind (Entity (Bound)) = E_Discriminant
12603 then
12604 if Is_Task_Type (Ttyp)
12605 and then Has_Completion (Ttyp)
12606 then
12607 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12608 Find_Direct_Name (B);
12610 elsif Is_Protected_Type (Ttyp) then
12611 D := First_Discriminant (Ttyp);
12612 while Chars (D) /= Chars (Entity (Bound)) loop
12613 Next_Discriminant (D);
12614 end loop;
12616 B := New_Reference_To (Discriminal (D), Loc);
12618 else
12619 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
12620 end if;
12622 elsif Nkind (Bound) = N_Attribute_Reference then
12623 return Bound;
12625 else
12626 B := New_Copy_Tree (Bound);
12627 end if;
12629 return
12630 Make_Attribute_Reference (Loc,
12631 Attribute_Name => Name_Pos,
12632 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12633 Expressions => New_List (B));
12634 end Convert_Discriminant_Ref;
12636 -- Start of processing for Family_Offset
12638 begin
12639 Real_Hi := Convert_Discriminant_Ref (Hi);
12640 Real_Lo := Convert_Discriminant_Ref (Lo);
12642 if Cap then
12643 if Is_Task_Type (Ttyp) then
12644 Ityp := RTE (RE_Task_Entry_Index);
12645 else
12646 Ityp := RTE (RE_Protected_Entry_Index);
12647 end if;
12649 Real_Hi :=
12650 Make_Attribute_Reference (Loc,
12651 Prefix => New_Reference_To (Ityp, Loc),
12652 Attribute_Name => Name_Min,
12653 Expressions => New_List (
12654 Real_Hi,
12655 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12657 Real_Lo :=
12658 Make_Attribute_Reference (Loc,
12659 Prefix => New_Reference_To (Ityp, Loc),
12660 Attribute_Name => Name_Max,
12661 Expressions => New_List (
12662 Real_Lo,
12663 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12664 end if;
12666 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12667 end Family_Offset;
12669 -----------------
12670 -- Family_Size --
12671 -----------------
12673 function Family_Size
12674 (Loc : Source_Ptr;
12675 Hi : Node_Id;
12676 Lo : Node_Id;
12677 Ttyp : Entity_Id;
12678 Cap : Boolean) return Node_Id
12680 Ityp : Entity_Id;
12682 begin
12683 if Is_Task_Type (Ttyp) then
12684 Ityp := RTE (RE_Task_Entry_Index);
12685 else
12686 Ityp := RTE (RE_Protected_Entry_Index);
12687 end if;
12689 return
12690 Make_Attribute_Reference (Loc,
12691 Prefix => New_Reference_To (Ityp, Loc),
12692 Attribute_Name => Name_Max,
12693 Expressions => New_List (
12694 Make_Op_Add (Loc,
12695 Left_Opnd =>
12696 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12697 Right_Opnd =>
12698 Make_Integer_Literal (Loc, 1)),
12699 Make_Integer_Literal (Loc, 0)));
12700 end Family_Size;
12702 ----------------------------
12703 -- Find_Enclosing_Context --
12704 ----------------------------
12706 procedure Find_Enclosing_Context
12707 (N : Node_Id;
12708 Context : out Node_Id;
12709 Context_Id : out Entity_Id;
12710 Context_Decls : out List_Id)
12712 begin
12713 -- Traverse the parent chain looking for an enclosing body, block,
12714 -- package or return statement.
12716 Context := Parent (N);
12717 while not Nkind_In (Context, N_Block_Statement,
12718 N_Entry_Body,
12719 N_Extended_Return_Statement,
12720 N_Package_Body,
12721 N_Package_Declaration,
12722 N_Subprogram_Body,
12723 N_Task_Body)
12724 loop
12725 Context := Parent (Context);
12726 end loop;
12728 -- Extract the constituents of the context
12730 if Nkind (Context) = N_Extended_Return_Statement then
12731 Context_Decls := Return_Object_Declarations (Context);
12732 Context_Id := Return_Statement_Entity (Context);
12734 -- Package declarations and bodies use a common library-level activation
12735 -- chain or task master, therefore return the package declaration as the
12736 -- proper carrier for the appropriate flag.
12738 elsif Nkind (Context) = N_Package_Body then
12739 Context_Decls := Declarations (Context);
12740 Context_Id := Corresponding_Spec (Context);
12741 Context := Parent (Context_Id);
12743 if Nkind (Context) = N_Defining_Program_Unit_Name then
12744 Context := Parent (Parent (Context));
12745 else
12746 Context := Parent (Context);
12747 end if;
12749 elsif Nkind (Context) = N_Package_Declaration then
12750 Context_Decls := Visible_Declarations (Specification (Context));
12751 Context_Id := Defining_Unit_Name (Specification (Context));
12753 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12754 Context_Id := Defining_Identifier (Context_Id);
12755 end if;
12757 else
12758 Context_Decls := Declarations (Context);
12760 if Nkind (Context) = N_Block_Statement then
12761 Context_Id := Entity (Identifier (Context));
12763 elsif Nkind (Context) = N_Entry_Body then
12764 Context_Id := Defining_Identifier (Context);
12766 elsif Nkind (Context) = N_Subprogram_Body then
12767 if Present (Corresponding_Spec (Context)) then
12768 Context_Id := Corresponding_Spec (Context);
12769 else
12770 Context_Id := Defining_Unit_Name (Specification (Context));
12772 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12773 Context_Id := Defining_Identifier (Context_Id);
12774 end if;
12775 end if;
12777 elsif Nkind (Context) = N_Task_Body then
12778 Context_Id := Corresponding_Spec (Context);
12780 else
12781 raise Program_Error;
12782 end if;
12783 end if;
12785 pragma Assert (Present (Context));
12786 pragma Assert (Present (Context_Id));
12787 pragma Assert (Present (Context_Decls));
12788 end Find_Enclosing_Context;
12790 -----------------------
12791 -- Find_Master_Scope --
12792 -----------------------
12794 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
12795 S : Entity_Id;
12797 begin
12798 -- In Ada 2005, the master is the innermost enclosing scope that is not
12799 -- transient. If the enclosing block is the rewriting of a call or the
12800 -- scope is an extended return statement this is valid master. The
12801 -- master in an extended return is only used within the return, and is
12802 -- subsequently overwritten in Move_Activation_Chain, but it must exist
12803 -- now before that overwriting occurs.
12805 S := Scope (E);
12807 if Ada_Version >= Ada_2005 then
12808 while Is_Internal (S) loop
12809 if Nkind (Parent (S)) = N_Block_Statement
12810 and then
12811 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
12812 then
12813 exit;
12815 elsif Ekind (S) = E_Return_Statement then
12816 exit;
12818 else
12819 S := Scope (S);
12820 end if;
12821 end loop;
12822 end if;
12824 return S;
12825 end Find_Master_Scope;
12827 -------------------------------
12828 -- First_Protected_Operation --
12829 -------------------------------
12831 function First_Protected_Operation (D : List_Id) return Node_Id is
12832 First_Op : Node_Id;
12834 begin
12835 First_Op := First (D);
12836 while Present (First_Op)
12837 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
12838 loop
12839 Next (First_Op);
12840 end loop;
12842 return First_Op;
12843 end First_Protected_Operation;
12845 ---------------------------------------
12846 -- Install_Private_Data_Declarations --
12847 ---------------------------------------
12849 procedure Install_Private_Data_Declarations
12850 (Loc : Source_Ptr;
12851 Spec_Id : Entity_Id;
12852 Conc_Typ : Entity_Id;
12853 Body_Nod : Node_Id;
12854 Decls : List_Id;
12855 Barrier : Boolean := False;
12856 Family : Boolean := False)
12858 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
12859 Decl : Node_Id;
12860 Def : Node_Id;
12861 Insert_Node : Node_Id := Empty;
12862 Obj_Ent : Entity_Id;
12864 procedure Add (Decl : Node_Id);
12865 -- Add a single declaration after Insert_Node. If this is the first
12866 -- addition, Decl is added to the front of Decls and it becomes the
12867 -- insertion node.
12869 function Replace_Bound (Bound : Node_Id) return Node_Id;
12870 -- The bounds of an entry index may depend on discriminants, create a
12871 -- reference to the corresponding prival. Otherwise return a duplicate
12872 -- of the original bound.
12874 ---------
12875 -- Add --
12876 ---------
12878 procedure Add (Decl : Node_Id) is
12879 begin
12880 if No (Insert_Node) then
12881 Prepend_To (Decls, Decl);
12882 else
12883 Insert_After (Insert_Node, Decl);
12884 end if;
12886 Insert_Node := Decl;
12887 end Add;
12889 --------------------------
12890 -- Replace_Discriminant --
12891 --------------------------
12893 function Replace_Bound (Bound : Node_Id) return Node_Id is
12894 begin
12895 if Nkind (Bound) = N_Identifier
12896 and then Is_Discriminal (Entity (Bound))
12897 then
12898 return Make_Identifier (Loc, Chars (Entity (Bound)));
12899 else
12900 return Duplicate_Subexpr (Bound);
12901 end if;
12902 end Replace_Bound;
12904 -- Start of processing for Install_Private_Data_Declarations
12906 begin
12907 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
12908 -- formal parameter _O, _object or _task depending on the context.
12910 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
12912 -- Special processing of _O for barrier functions, protected entries
12913 -- and families.
12915 if Barrier
12916 or else
12917 (Is_Protected
12918 and then
12919 (Ekind (Spec_Id) = E_Entry
12920 or else Ekind (Spec_Id) = E_Entry_Family))
12921 then
12922 declare
12923 Conc_Rec : constant Entity_Id :=
12924 Corresponding_Record_Type (Conc_Typ);
12925 Typ_Id : constant Entity_Id :=
12926 Make_Defining_Identifier (Loc,
12927 New_External_Name (Chars (Conc_Rec), 'P'));
12928 begin
12929 -- Generate:
12930 -- type prot_typVP is access prot_typV;
12932 Decl :=
12933 Make_Full_Type_Declaration (Loc,
12934 Defining_Identifier => Typ_Id,
12935 Type_Definition =>
12936 Make_Access_To_Object_Definition (Loc,
12937 Subtype_Indication =>
12938 New_Reference_To (Conc_Rec, Loc)));
12939 Add (Decl);
12941 -- Generate:
12942 -- _object : prot_typVP := prot_typV (_O);
12944 Decl :=
12945 Make_Object_Declaration (Loc,
12946 Defining_Identifier =>
12947 Make_Defining_Identifier (Loc, Name_uObject),
12948 Object_Definition => New_Reference_To (Typ_Id, Loc),
12949 Expression =>
12950 Unchecked_Convert_To (Typ_Id,
12951 New_Reference_To (Obj_Ent, Loc)));
12952 Add (Decl);
12954 -- Set the reference to the concurrent object
12956 Obj_Ent := Defining_Identifier (Decl);
12957 end;
12958 end if;
12960 -- Step 2: Create the Protection object and build its declaration for
12961 -- any protected entry (family) of subprogram. Note for the lock-free
12962 -- implementation, the Protection object is not needed anymore.
12964 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
12965 declare
12966 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
12967 Prot_Typ : RE_Id;
12969 begin
12970 Set_Protection_Object (Spec_Id, Prot_Ent);
12972 -- Determine the proper protection type
12974 if Has_Attach_Handler (Conc_Typ)
12975 and then not Restricted_Profile
12976 and then not Restriction_Active (No_Dynamic_Attachment)
12977 then
12978 Prot_Typ := RE_Static_Interrupt_Protection;
12980 elsif Has_Interrupt_Handler (Conc_Typ)
12981 and then not Restriction_Active (No_Dynamic_Attachment)
12982 then
12983 Prot_Typ := RE_Dynamic_Interrupt_Protection;
12985 -- The type has explicit entries or generated primitive entry
12986 -- wrappers.
12988 elsif Has_Entries (Conc_Typ)
12989 or else
12990 (Ada_Version >= Ada_2005
12991 and then Present (Interface_List (Parent (Conc_Typ))))
12992 then
12993 case Corresponding_Runtime_Package (Conc_Typ) is
12994 when System_Tasking_Protected_Objects_Entries =>
12995 Prot_Typ := RE_Protection_Entries;
12997 when System_Tasking_Protected_Objects_Single_Entry =>
12998 Prot_Typ := RE_Protection_Entry;
13000 when others =>
13001 raise Program_Error;
13002 end case;
13004 else
13005 Prot_Typ := RE_Protection;
13006 end if;
13008 -- Generate:
13009 -- conc_typR : protection_typ renames _object._object;
13011 Decl :=
13012 Make_Object_Renaming_Declaration (Loc,
13013 Defining_Identifier => Prot_Ent,
13014 Subtype_Mark =>
13015 New_Reference_To (RTE (Prot_Typ), Loc),
13016 Name =>
13017 Make_Selected_Component (Loc,
13018 Prefix => New_Reference_To (Obj_Ent, Loc),
13019 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13020 Add (Decl);
13021 end;
13022 end if;
13024 -- Step 3: Add discriminant renamings (if any)
13026 if Has_Discriminants (Conc_Typ) then
13027 declare
13028 D : Entity_Id;
13030 begin
13031 D := First_Discriminant (Conc_Typ);
13032 while Present (D) loop
13034 -- Adjust the source location
13036 Set_Sloc (Discriminal (D), Loc);
13038 -- Generate:
13039 -- discr_name : discr_typ renames _object.discr_name;
13040 -- or
13041 -- discr_name : discr_typ renames _task.discr_name;
13043 Decl :=
13044 Make_Object_Renaming_Declaration (Loc,
13045 Defining_Identifier => Discriminal (D),
13046 Subtype_Mark => New_Reference_To (Etype (D), Loc),
13047 Name =>
13048 Make_Selected_Component (Loc,
13049 Prefix => New_Reference_To (Obj_Ent, Loc),
13050 Selector_Name => Make_Identifier (Loc, Chars (D))));
13051 Add (Decl);
13053 Next_Discriminant (D);
13054 end loop;
13055 end;
13056 end if;
13058 -- Step 4: Add private component renamings (if any)
13060 if Is_Protected then
13061 Def := Protected_Definition (Parent (Conc_Typ));
13063 if Present (Private_Declarations (Def)) then
13064 declare
13065 Comp : Node_Id;
13066 Comp_Id : Entity_Id;
13067 Decl_Id : Entity_Id;
13069 begin
13070 Comp := First (Private_Declarations (Def));
13071 while Present (Comp) loop
13072 if Nkind (Comp) = N_Component_Declaration then
13073 Comp_Id := Defining_Identifier (Comp);
13074 Decl_Id :=
13075 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13077 -- Minimal decoration
13079 if Ekind (Spec_Id) = E_Function then
13080 Set_Ekind (Decl_Id, E_Constant);
13081 else
13082 Set_Ekind (Decl_Id, E_Variable);
13083 end if;
13085 Set_Prival (Comp_Id, Decl_Id);
13086 Set_Prival_Link (Decl_Id, Comp_Id);
13087 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13089 -- Generate:
13090 -- comp_name : comp_typ renames _object.comp_name;
13092 Decl :=
13093 Make_Object_Renaming_Declaration (Loc,
13094 Defining_Identifier => Decl_Id,
13095 Subtype_Mark =>
13096 New_Reference_To (Etype (Comp_Id), Loc),
13097 Name =>
13098 Make_Selected_Component (Loc,
13099 Prefix =>
13100 New_Reference_To (Obj_Ent, Loc),
13101 Selector_Name =>
13102 Make_Identifier (Loc, Chars (Comp_Id))));
13103 Add (Decl);
13104 end if;
13106 Next (Comp);
13107 end loop;
13108 end;
13109 end if;
13110 end if;
13112 -- Step 5: Add the declaration of the entry index and the associated
13113 -- type for barrier functions and entry families.
13115 if (Barrier and then Family)
13116 or else Ekind (Spec_Id) = E_Entry_Family
13117 then
13118 declare
13119 E : constant Entity_Id := Index_Object (Spec_Id);
13120 Index : constant Entity_Id :=
13121 Defining_Identifier (
13122 Entry_Index_Specification (
13123 Entry_Body_Formal_Part (Body_Nod)));
13124 Index_Con : constant Entity_Id :=
13125 Make_Defining_Identifier (Loc, Chars (Index));
13126 High : Node_Id;
13127 Index_Typ : Entity_Id;
13128 Low : Node_Id;
13130 begin
13131 -- Minimal decoration
13133 Set_Ekind (Index_Con, E_Constant);
13134 Set_Entry_Index_Constant (Index, Index_Con);
13135 Set_Discriminal_Link (Index_Con, Index);
13137 -- Retrieve the bounds of the entry family
13139 High := Type_High_Bound (Etype (Index));
13140 Low := Type_Low_Bound (Etype (Index));
13142 -- In the simple case the entry family is given by a subtype
13143 -- mark and the index constant has the same type.
13145 if Is_Entity_Name (Original_Node (
13146 Discrete_Subtype_Definition (Parent (Index))))
13147 then
13148 Index_Typ := Etype (Index);
13150 -- Otherwise a new subtype declaration is required
13152 else
13153 High := Replace_Bound (High);
13154 Low := Replace_Bound (Low);
13156 Index_Typ := Make_Temporary (Loc, 'J');
13158 -- Generate:
13159 -- subtype Jnn is <Etype of Index> range Low .. High;
13161 Decl :=
13162 Make_Subtype_Declaration (Loc,
13163 Defining_Identifier => Index_Typ,
13164 Subtype_Indication =>
13165 Make_Subtype_Indication (Loc,
13166 Subtype_Mark =>
13167 New_Reference_To (Base_Type (Etype (Index)), Loc),
13168 Constraint =>
13169 Make_Range_Constraint (Loc,
13170 Range_Expression =>
13171 Make_Range (Loc, Low, High))));
13172 Add (Decl);
13173 end if;
13175 Set_Etype (Index_Con, Index_Typ);
13177 -- Create the object which designates the index:
13178 -- J : constant Jnn :=
13179 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13181 -- where Jnn is the subtype created above or the original type of
13182 -- the index, _E is a formal of the protected body subprogram and
13183 -- <index expr> is the index of the first family member.
13185 Decl :=
13186 Make_Object_Declaration (Loc,
13187 Defining_Identifier => Index_Con,
13188 Constant_Present => True,
13189 Object_Definition =>
13190 New_Reference_To (Index_Typ, Loc),
13192 Expression =>
13193 Make_Attribute_Reference (Loc,
13194 Prefix =>
13195 New_Reference_To (Index_Typ, Loc),
13196 Attribute_Name => Name_Val,
13198 Expressions => New_List (
13200 Make_Op_Add (Loc,
13201 Left_Opnd =>
13202 Make_Op_Subtract (Loc,
13203 Left_Opnd =>
13204 New_Reference_To (E, Loc),
13205 Right_Opnd =>
13206 Entry_Index_Expression (Loc,
13207 Defining_Identifier (Body_Nod),
13208 Empty, Conc_Typ)),
13210 Right_Opnd =>
13211 Make_Attribute_Reference (Loc,
13212 Prefix =>
13213 New_Reference_To (Index_Typ, Loc),
13214 Attribute_Name => Name_Pos,
13215 Expressions => New_List (
13216 Make_Attribute_Reference (Loc,
13217 Prefix =>
13218 New_Reference_To (Index_Typ, Loc),
13219 Attribute_Name => Name_First)))))));
13220 Add (Decl);
13221 end;
13222 end if;
13223 end Install_Private_Data_Declarations;
13225 -----------------------
13226 -- Is_Exception_Safe --
13227 -----------------------
13229 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13231 function Has_Side_Effect (N : Node_Id) return Boolean;
13232 -- Return True whenever encountering a subprogram call or raise
13233 -- statement of any kind in the sequence of statements
13235 ---------------------
13236 -- Has_Side_Effect --
13237 ---------------------
13239 -- What is this doing buried two levels down in exp_ch9. It seems like a
13240 -- generally useful function, and indeed there may be code duplication
13241 -- going on here ???
13243 function Has_Side_Effect (N : Node_Id) return Boolean is
13244 Stmt : Node_Id;
13245 Expr : Node_Id;
13247 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13248 -- Indicate whether N is a subprogram call or a raise statement
13250 ----------------------
13251 -- Is_Call_Or_Raise --
13252 ----------------------
13254 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13255 begin
13256 return Nkind_In (N, N_Procedure_Call_Statement,
13257 N_Function_Call,
13258 N_Raise_Statement,
13259 N_Raise_Constraint_Error,
13260 N_Raise_Program_Error,
13261 N_Raise_Storage_Error);
13262 end Is_Call_Or_Raise;
13264 -- Start of processing for Has_Side_Effect
13266 begin
13267 Stmt := N;
13268 while Present (Stmt) loop
13269 if Is_Call_Or_Raise (Stmt) then
13270 return True;
13271 end if;
13273 -- An object declaration can also contain a function call or a
13274 -- raise statement.
13276 if Nkind (Stmt) = N_Object_Declaration then
13277 Expr := Expression (Stmt);
13279 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13280 return True;
13281 end if;
13282 end if;
13284 Next (Stmt);
13285 end loop;
13287 return False;
13288 end Has_Side_Effect;
13290 -- Start of processing for Is_Exception_Safe
13292 begin
13293 -- If the checks handled by the back end are not disabled, we cannot
13294 -- ensure that no exception will be raised.
13296 if not Access_Checks_Suppressed (Empty)
13297 or else not Discriminant_Checks_Suppressed (Empty)
13298 or else not Range_Checks_Suppressed (Empty)
13299 or else not Index_Checks_Suppressed (Empty)
13300 or else Opt.Stack_Checking_Enabled
13301 then
13302 return False;
13303 end if;
13305 if Has_Side_Effect (First (Declarations (Subprogram)))
13306 or else
13307 Has_Side_Effect
13308 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13309 then
13310 return False;
13311 else
13312 return True;
13313 end if;
13314 end Is_Exception_Safe;
13316 ---------------------------------
13317 -- Is_Potentially_Large_Family --
13318 ---------------------------------
13320 function Is_Potentially_Large_Family
13321 (Base_Index : Entity_Id;
13322 Conctyp : Entity_Id;
13323 Lo : Node_Id;
13324 Hi : Node_Id) return Boolean
13326 begin
13327 return Scope (Base_Index) = Standard_Standard
13328 and then Base_Index = Base_Type (Standard_Integer)
13329 and then Has_Discriminants (Conctyp)
13330 and then
13331 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13332 and then
13333 (Denotes_Discriminant (Lo, True)
13334 or else
13335 Denotes_Discriminant (Hi, True));
13336 end Is_Potentially_Large_Family;
13338 -------------------------------------
13339 -- Is_Private_Primitive_Subprogram --
13340 -------------------------------------
13342 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13343 begin
13344 return
13345 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13346 and then Is_Private_Primitive (Id);
13347 end Is_Private_Primitive_Subprogram;
13349 ------------------
13350 -- Index_Object --
13351 ------------------
13353 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13354 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13355 Formal : Entity_Id;
13357 begin
13358 Formal := First_Formal (Bod_Subp);
13359 while Present (Formal) loop
13361 -- Look for formal parameter _E
13363 if Chars (Formal) = Name_uE then
13364 return Formal;
13365 end if;
13367 Next_Formal (Formal);
13368 end loop;
13370 -- A protected body subprogram should always have the parameter in
13371 -- question.
13373 raise Program_Error;
13374 end Index_Object;
13376 --------------------------------
13377 -- Make_Initialize_Protection --
13378 --------------------------------
13380 function Make_Initialize_Protection
13381 (Protect_Rec : Entity_Id) return List_Id
13383 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13384 P_Arr : Entity_Id;
13385 Pdec : Node_Id;
13386 Ptyp : constant Node_Id :=
13387 Corresponding_Concurrent_Type (Protect_Rec);
13388 Args : List_Id;
13389 L : constant List_Id := New_List;
13390 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13391 Restricted : constant Boolean := Restricted_Profile;
13393 begin
13394 -- We may need two calls to properly initialize the object, one to
13395 -- Initialize_Protection, and possibly one to Install_Handlers if we
13396 -- have a pragma Attach_Handler.
13398 -- Get protected declaration. In the case of a task type declaration,
13399 -- this is simply the parent of the protected type entity. In the single
13400 -- protected object declaration, this parent will be the implicit type,
13401 -- and we can find the corresponding single protected object declaration
13402 -- by searching forward in the declaration list in the tree.
13404 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13405 -- of this type should have been removed during semantic analysis.
13407 Pdec := Parent (Ptyp);
13408 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13409 N_Single_Protected_Declaration)
13410 loop
13411 Next (Pdec);
13412 end loop;
13414 -- Build the parameter list for the call. Note that _Init is the name
13415 -- of the formal for the object to be initialized, which is the task
13416 -- value record itself.
13418 Args := New_List;
13420 -- For lock-free implementation, skip initializations of the Protection
13421 -- object.
13423 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13424 -- Object parameter. This is a pointer to the object of type
13425 -- Protection used by the GNARL to control the protected object.
13427 Append_To (Args,
13428 Make_Attribute_Reference (Loc,
13429 Prefix =>
13430 Make_Selected_Component (Loc,
13431 Prefix => Make_Identifier (Loc, Name_uInit),
13432 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13433 Attribute_Name => Name_Unchecked_Access));
13435 -- Priority parameter. Set to Unspecified_Priority unless there is a
13436 -- Priority rep item, in which case we take the value from the pragma
13437 -- or attribute definition clause, or there is an Interrupt_Priority
13438 -- rep item and no Priority rep item, and we set the ceiling to
13439 -- Interrupt_Priority'Last, an implementation-defined value, see
13440 -- (RM D.3(10)).
13442 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13443 declare
13444 Prio_Clause : constant Node_Id :=
13445 Get_Rep_Item
13446 (Ptyp, Name_Priority, Check_Parents => False);
13448 Prio : Node_Id;
13449 Temp : Entity_Id;
13451 begin
13452 -- Pragma Priority
13454 if Nkind (Prio_Clause) = N_Pragma then
13455 Prio :=
13456 Expression
13457 (First (Pragma_Argument_Associations (Prio_Clause)));
13459 -- Attribute definition clause Priority
13461 else
13462 Prio := Expression (Prio_Clause);
13463 end if;
13465 -- If priority is a static expression, then we can duplicate it
13466 -- with no problem and simply append it to the argument list.
13468 if Is_Static_Expression (Prio) then
13469 Append_To (Args,
13470 Duplicate_Subexpr_No_Checks (Prio));
13472 -- Otherwise, the priority may be a per-object expression, if
13473 -- it depends on a discriminant of the type. In this case,
13474 -- create local variable to capture the expression. Note that
13475 -- it is really necessary to create this variable explicitly.
13476 -- It might be thought that removing side effects would the
13477 -- appropriate approach, but that could generate declarations
13478 -- improperly placed in the enclosing scope.
13480 -- Note: Use System.Any_Priority as the expected type for the
13481 -- non-static priority expression, in case the expression has
13482 -- not been analyzed yet (as occurs for example with pragma
13483 -- Interrupt_Priority).
13485 else
13486 Temp := Make_Temporary (Loc, 'R', Prio);
13487 Append_To (L,
13488 Make_Object_Declaration (Loc,
13489 Defining_Identifier => Temp,
13490 Object_Definition =>
13491 New_Occurrence_Of (RTE (RE_Any_Priority), Loc),
13492 Expression => Relocate_Node (Prio)));
13494 Append_To (Args, New_Occurrence_Of (Temp, Loc));
13495 end if;
13496 end;
13498 -- When no priority is specified but an xx_Handler pragma is, we
13499 -- default to System.Interrupts.Default_Interrupt_Priority, see
13500 -- D.3(10).
13502 elsif Has_Attach_Handler (Ptyp)
13503 or else Has_Interrupt_Handler (Ptyp)
13504 then
13505 Append_To (Args,
13506 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
13508 -- Normal case, no priority or xx_Handler specified, default priority
13510 else
13511 Append_To (Args,
13512 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13513 end if;
13515 -- Test for Compiler_Info parameter. This parameter allows entry body
13516 -- procedures and barrier functions to be called from the runtime. It
13517 -- is a pointer to the record generated by the compiler to represent
13518 -- the protected object.
13520 -- A protected type without entries that covers an interface and
13521 -- overrides the abstract routines with protected procedures is
13522 -- considered equivalent to a protected type with entries in the
13523 -- context of dispatching select statements.
13525 if Has_Entry
13526 or else Has_Interfaces (Protect_Rec)
13527 or else
13528 ((Has_Attach_Handler (Ptyp) or else Has_Interrupt_Handler (Ptyp))
13529 and then not Restriction_Active (No_Dynamic_Attachment))
13530 then
13531 declare
13532 Pkg_Id : constant RTU_Id :=
13533 Corresponding_Runtime_Package (Ptyp);
13535 Called_Subp : RE_Id;
13537 begin
13538 case Pkg_Id is
13539 when System_Tasking_Protected_Objects_Entries =>
13540 Called_Subp := RE_Initialize_Protection_Entries;
13542 when System_Tasking_Protected_Objects =>
13543 Called_Subp := RE_Initialize_Protection;
13545 when System_Tasking_Protected_Objects_Single_Entry =>
13546 Called_Subp := RE_Initialize_Protection_Entry;
13548 when others =>
13549 raise Program_Error;
13550 end case;
13552 if Has_Entry
13553 or else not Restricted
13554 or else Has_Interfaces (Protect_Rec)
13555 then
13556 Append_To (Args,
13557 Make_Attribute_Reference (Loc,
13558 Prefix => Make_Identifier (Loc, Name_uInit),
13559 Attribute_Name => Name_Address));
13560 end if;
13562 -- Entry_Bodies parameter. This is a pointer to an array of
13563 -- pointers to the entry body procedures and barrier functions
13564 -- of the object. If the protected type has no entries this
13565 -- object will not exist, in this case, pass a null.
13567 if Has_Entry then
13568 P_Arr := Entry_Bodies_Array (Ptyp);
13570 Append_To (Args,
13571 Make_Attribute_Reference (Loc,
13572 Prefix => New_Reference_To (P_Arr, Loc),
13573 Attribute_Name => Name_Unrestricted_Access));
13575 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13577 -- Find index mapping function (clumsy but ok for now)
13579 while Ekind (P_Arr) /= E_Function loop
13580 Next_Entity (P_Arr);
13581 end loop;
13583 Append_To (Args,
13584 Make_Attribute_Reference (Loc,
13585 Prefix => New_Reference_To (P_Arr, Loc),
13586 Attribute_Name => Name_Unrestricted_Access));
13587 end if;
13589 elsif Pkg_Id =
13590 System_Tasking_Protected_Objects_Single_Entry
13591 then
13592 Append_To (Args, Make_Null (Loc));
13594 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13595 Append_To (Args, Make_Null (Loc));
13596 Append_To (Args, Make_Null (Loc));
13597 end if;
13599 Append_To (L,
13600 Make_Procedure_Call_Statement (Loc,
13601 Name => New_Reference_To (RTE (Called_Subp), Loc),
13602 Parameter_Associations => Args));
13603 end;
13604 else
13605 Append_To (L,
13606 Make_Procedure_Call_Statement (Loc,
13607 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
13608 Parameter_Associations => Args));
13609 end if;
13610 end if;
13612 if Has_Attach_Handler (Ptyp) then
13614 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13615 -- make the following call:
13617 -- Install_Handlers (_object,
13618 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13620 -- or, in the case of Ravenscar:
13622 -- Install_Restricted_Handlers
13623 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13625 declare
13626 Args : constant List_Id := New_List;
13627 Table : constant List_Id := New_List;
13628 Ritem : Node_Id := First_Rep_Item (Ptyp);
13630 begin
13631 -- Build the Attach_Handler table argument
13633 while Present (Ritem) loop
13634 if Nkind (Ritem) = N_Pragma
13635 and then Pragma_Name (Ritem) = Name_Attach_Handler
13636 then
13637 declare
13638 Handler : constant Node_Id :=
13639 First (Pragma_Argument_Associations (Ritem));
13641 Interrupt : constant Node_Id := Next (Handler);
13642 Expr : constant Node_Id := Expression (Interrupt);
13644 begin
13645 Append_To (Table,
13646 Make_Aggregate (Loc, Expressions => New_List (
13647 Unchecked_Convert_To
13648 (RTE (RE_System_Interrupt_Id), Expr),
13649 Make_Attribute_Reference (Loc,
13650 Prefix => Make_Selected_Component (Loc,
13651 Make_Identifier (Loc, Name_uInit),
13652 Duplicate_Subexpr_No_Checks
13653 (Expression (Handler))),
13654 Attribute_Name => Name_Access))));
13655 end;
13656 end if;
13658 Next_Rep_Item (Ritem);
13659 end loop;
13661 -- Append the table argument we just built
13663 Append_To (Args, Make_Aggregate (Loc, Table));
13665 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13666 -- call to the statements.
13668 if Restricted then
13669 -- Call a simplified version of Install_Handlers to be used
13670 -- when the Ravenscar restrictions are in effect
13671 -- (Install_Restricted_Handlers).
13673 Append_To (L,
13674 Make_Procedure_Call_Statement (Loc,
13675 Name =>
13676 New_Reference_To
13677 (RTE (RE_Install_Restricted_Handlers), Loc),
13678 Parameter_Associations => Args));
13680 else
13681 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13682 -- First, prepends the _object argument
13684 Prepend_To (Args,
13685 Make_Attribute_Reference (Loc,
13686 Prefix =>
13687 Make_Selected_Component (Loc,
13688 Prefix => Make_Identifier (Loc, Name_uInit),
13689 Selector_Name =>
13690 Make_Identifier (Loc, Name_uObject)),
13691 Attribute_Name => Name_Unchecked_Access));
13692 end if;
13694 -- Then, insert call to Install_Handlers
13696 Append_To (L,
13697 Make_Procedure_Call_Statement (Loc,
13698 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
13699 Parameter_Associations => Args));
13700 end if;
13701 end;
13702 end if;
13704 return L;
13705 end Make_Initialize_Protection;
13707 ---------------------------
13708 -- Make_Task_Create_Call --
13709 ---------------------------
13711 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13712 Loc : constant Source_Ptr := Sloc (Task_Rec);
13713 Args : List_Id;
13714 Ecount : Node_Id;
13715 Name : Node_Id;
13716 Tdec : Node_Id;
13717 Tdef : Node_Id;
13718 Tnam : Name_Id;
13719 Ttyp : Node_Id;
13721 begin
13722 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
13723 Tnam := Chars (Ttyp);
13725 -- Get task declaration. In the case of a task type declaration, this is
13726 -- simply the parent of the task type entity. In the single task
13727 -- declaration, this parent will be the implicit type, and we can find
13728 -- the corresponding single task declaration by searching forward in the
13729 -- declaration list in the tree.
13731 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
13732 -- this type should have been removed during semantic analysis.
13734 Tdec := Parent (Ttyp);
13735 while not Nkind_In (Tdec, N_Task_Type_Declaration,
13736 N_Single_Task_Declaration)
13737 loop
13738 Next (Tdec);
13739 end loop;
13741 -- Now we can find the task definition from this declaration
13743 Tdef := Task_Definition (Tdec);
13745 -- Build the parameter list for the call. Note that _Init is the name
13746 -- of the formal for the object to be initialized, which is the task
13747 -- value record itself.
13749 Args := New_List;
13751 -- Priority parameter. Set to Unspecified_Priority unless there is a
13752 -- Priority rep item, in which case we take the value from the rep item.
13754 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
13755 Append_To (Args,
13756 Make_Selected_Component (Loc,
13757 Prefix => Make_Identifier (Loc, Name_uInit),
13758 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
13759 else
13760 Append_To (Args,
13761 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13762 end if;
13764 -- Optional Stack parameter
13766 if Restricted_Profile then
13768 -- If the stack has been preallocated by the expander then
13769 -- pass its address. Otherwise, pass a null address.
13771 if Preallocated_Stacks_On_Target then
13772 Append_To (Args,
13773 Make_Attribute_Reference (Loc,
13774 Prefix =>
13775 Make_Selected_Component (Loc,
13776 Prefix => Make_Identifier (Loc, Name_uInit),
13777 Selector_Name => Make_Identifier (Loc, Name_uStack)),
13778 Attribute_Name => Name_Address));
13780 else
13781 Append_To (Args,
13782 New_Reference_To (RTE (RE_Null_Address), Loc));
13783 end if;
13784 end if;
13786 -- Size parameter. If no Storage_Size pragma is present, then
13787 -- the size is taken from the taskZ variable for the type, which
13788 -- is either Unspecified_Size, or has been reset by the use of
13789 -- a Storage_Size attribute definition clause. If a pragma is
13790 -- present, then the size is taken from the _Size field of the
13791 -- task value record, which was set from the pragma value.
13793 if Present (Tdef)
13794 and then Has_Storage_Size_Pragma (Tdef)
13795 then
13796 Append_To (Args,
13797 Make_Selected_Component (Loc,
13798 Prefix => Make_Identifier (Loc, Name_uInit),
13799 Selector_Name => Make_Identifier (Loc, Name_uSize)));
13801 else
13802 Append_To (Args,
13803 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
13804 end if;
13806 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
13807 -- Task_Info pragma, in which case we take the value from the pragma.
13809 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
13810 Append_To (Args,
13811 Make_Selected_Component (Loc,
13812 Prefix => Make_Identifier (Loc, Name_uInit),
13813 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
13815 else
13816 Append_To (Args,
13817 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
13818 end if;
13820 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
13821 -- in which case we take the value from the rep item. The parameter is
13822 -- passed as an Integer because in the case of unspecified CPU the
13823 -- value is not in the range of CPU_Range.
13825 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
13826 Append_To (Args,
13827 Convert_To (Standard_Integer,
13828 Make_Selected_Component (Loc,
13829 Prefix => Make_Identifier (Loc, Name_uInit),
13830 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
13831 else
13832 Append_To (Args,
13833 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
13834 end if;
13836 if not Restricted_Profile then
13838 -- Deadline parameter. If no Relative_Deadline pragma is present,
13839 -- then the deadline is Time_Span_Zero. If a pragma is present, then
13840 -- the deadline is taken from the _Relative_Deadline field of the
13841 -- task value record, which was set from the pragma value. Note that
13842 -- this parameter must not be generated for the restricted profiles
13843 -- since Ravenscar does not allow deadlines.
13845 -- Case where pragma Relative_Deadline applies: use given value
13847 if Present (Tdef)
13848 and then Has_Relative_Deadline_Pragma (Tdef)
13849 then
13850 Append_To (Args,
13851 Make_Selected_Component (Loc,
13852 Prefix =>
13853 Make_Identifier (Loc, Name_uInit),
13854 Selector_Name =>
13855 Make_Identifier (Loc, Name_uRelative_Deadline)));
13857 -- No pragma Relative_Deadline apply to the task
13859 else
13860 Append_To (Args,
13861 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
13862 end if;
13864 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
13865 -- present, then the dispatching domain is null. If a rep item is
13866 -- present, then the dispatching domain is taken from the
13867 -- _Dispatching_Domain field of the task value record, which was set
13868 -- from the rep item value. Note that this parameter must not be
13869 -- generated for the restricted profiles since Ravenscar does not
13870 -- allow dispatching domains.
13872 -- Case where Dispatching_Domain rep item applies: use given value
13874 if Has_Rep_Item
13875 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
13876 then
13877 Append_To (Args,
13878 Make_Selected_Component (Loc,
13879 Prefix =>
13880 Make_Identifier (Loc, Name_uInit),
13881 Selector_Name =>
13882 Make_Identifier (Loc, Name_uDispatching_Domain)));
13884 -- No pragma or aspect Dispatching_Domain apply to the task
13886 else
13887 Append_To (Args, Make_Null (Loc));
13888 end if;
13890 -- Number of entries. This is an expression of the form:
13892 -- n + _Init.a'Length + _Init.a'B'Length + ...
13894 -- where a,b... are the entry family names for the task definition
13896 Ecount :=
13897 Build_Entry_Count_Expression
13898 (Ttyp,
13899 Component_Items
13900 (Component_List
13901 (Type_Definition
13902 (Parent (Corresponding_Record_Type (Ttyp))))),
13903 Loc);
13904 Append_To (Args, Ecount);
13906 -- Master parameter. This is a reference to the _Master parameter of
13907 -- the initialization procedure, except in the case of the pragma
13908 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
13909 -- System.Tasking.Library_Task_Level.
13911 if Restriction_Active (No_Task_Hierarchy) = False then
13912 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
13913 else
13914 Append_To (Args,
13915 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
13916 end if;
13917 end if;
13919 -- State parameter. This is a pointer to the task body procedure. The
13920 -- required value is obtained by taking 'Unrestricted_Access of the task
13921 -- body procedure and converting it (with an unchecked conversion) to
13922 -- the type required by the task kernel. For further details, see the
13923 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
13924 -- than 'Address in order to avoid creating trampolines.
13926 declare
13927 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
13928 Subp_Ptr_Typ : constant Node_Id :=
13929 Create_Itype (E_Access_Subprogram_Type, Tdec);
13930 Ref : constant Node_Id := Make_Itype_Reference (Loc);
13932 begin
13933 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
13934 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
13936 -- Be sure to freeze a reference to the access-to-subprogram type,
13937 -- otherwise gigi will complain that it's in the wrong scope, because
13938 -- it's actually inside the init procedure for the record type that
13939 -- corresponds to the task type.
13941 -- This processing is causing a crash in the .NET/JVM back ends that
13942 -- is not yet understood, so skip it in these cases ???
13944 if VM_Target = No_VM then
13945 Set_Itype (Ref, Subp_Ptr_Typ);
13946 Append_Freeze_Action (Task_Rec, Ref);
13948 Append_To (Args,
13949 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13950 Make_Qualified_Expression (Loc,
13951 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
13952 Expression =>
13953 Make_Attribute_Reference (Loc,
13954 Prefix =>
13955 New_Occurrence_Of (Body_Proc, Loc),
13956 Attribute_Name => Name_Unrestricted_Access))));
13958 -- For the .NET/JVM cases revert to the original code below ???
13960 else
13961 Append_To (Args,
13962 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13963 Make_Attribute_Reference (Loc,
13964 Prefix =>
13965 New_Occurrence_Of (Body_Proc, Loc),
13966 Attribute_Name => Name_Address)));
13967 end if;
13968 end;
13970 -- Discriminants parameter. This is just the address of the task
13971 -- value record itself (which contains the discriminant values
13973 Append_To (Args,
13974 Make_Attribute_Reference (Loc,
13975 Prefix => Make_Identifier (Loc, Name_uInit),
13976 Attribute_Name => Name_Address));
13978 -- Elaborated parameter. This is an access to the elaboration Boolean
13980 Append_To (Args,
13981 Make_Attribute_Reference (Loc,
13982 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
13983 Attribute_Name => Name_Unchecked_Access));
13985 -- Add Chain parameter (not done for sequential elaboration policy, see
13986 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
13988 if Partition_Elaboration_Policy /= 'S' then
13989 Append_To (Args, Make_Identifier (Loc, Name_uChain));
13990 end if;
13992 -- Task name parameter. Take this from the _Task_Id parameter to the
13993 -- init call unless there is a Task_Name pragma, in which case we take
13994 -- the value from the pragma.
13996 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
13997 -- Copy expression in full, because it may be dynamic and have
13998 -- side effects.
14000 Append_To (Args,
14001 New_Copy_Tree
14002 (Expression
14003 (First
14004 (Pragma_Argument_Associations
14005 (Get_Rep_Pragma
14006 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14008 else
14009 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14010 end if;
14012 -- Created_Task parameter. This is the _Task_Id field of the task
14013 -- record value
14015 Append_To (Args,
14016 Make_Selected_Component (Loc,
14017 Prefix => Make_Identifier (Loc, Name_uInit),
14018 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14020 declare
14021 Create_RE : RE_Id;
14023 begin
14024 if Restricted_Profile then
14025 if Partition_Elaboration_Policy = 'S' then
14026 Create_RE := RE_Create_Restricted_Task_Sequential;
14027 else
14028 Create_RE := RE_Create_Restricted_Task;
14029 end if;
14030 else
14031 Create_RE := RE_Create_Task;
14032 end if;
14034 Name := New_Reference_To (RTE (Create_RE), Loc);
14035 end;
14037 return
14038 Make_Procedure_Call_Statement (Loc,
14039 Name => Name,
14040 Parameter_Associations => Args);
14041 end Make_Task_Create_Call;
14043 ------------------------------
14044 -- Next_Protected_Operation --
14045 ------------------------------
14047 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14048 Next_Op : Node_Id;
14050 begin
14051 Next_Op := Next (N);
14052 while Present (Next_Op)
14053 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
14054 loop
14055 Next (Next_Op);
14056 end loop;
14058 return Next_Op;
14059 end Next_Protected_Operation;
14061 ---------------------
14062 -- Null_Statements --
14063 ---------------------
14065 function Null_Statements (Stats : List_Id) return Boolean is
14066 Stmt : Node_Id;
14068 begin
14069 Stmt := First (Stats);
14070 while Nkind (Stmt) /= N_Empty
14071 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14072 or else
14073 (Nkind (Stmt) = N_Pragma
14074 and then (Pragma_Name (Stmt) = Name_Unreferenced
14075 or else
14076 Pragma_Name (Stmt) = Name_Unmodified
14077 or else
14078 Pragma_Name (Stmt) = Name_Warnings)))
14079 loop
14080 Next (Stmt);
14081 end loop;
14083 return Nkind (Stmt) = N_Empty;
14084 end Null_Statements;
14086 --------------------------
14087 -- Parameter_Block_Pack --
14088 --------------------------
14090 function Parameter_Block_Pack
14091 (Loc : Source_Ptr;
14092 Blk_Typ : Entity_Id;
14093 Actuals : List_Id;
14094 Formals : List_Id;
14095 Decls : List_Id;
14096 Stmts : List_Id) return Node_Id
14098 Actual : Entity_Id;
14099 Expr : Node_Id := Empty;
14100 Formal : Entity_Id;
14101 Has_Param : Boolean := False;
14102 P : Entity_Id;
14103 Params : List_Id;
14104 Temp_Asn : Node_Id;
14105 Temp_Nam : Node_Id;
14107 begin
14108 Actual := First (Actuals);
14109 Formal := Defining_Identifier (First (Formals));
14110 Params := New_List;
14112 while Present (Actual) loop
14113 if Is_By_Copy_Type (Etype (Actual)) then
14114 -- Generate:
14115 -- Jnn : aliased <formal-type>
14117 Temp_Nam := Make_Temporary (Loc, 'J');
14119 Append_To (Decls,
14120 Make_Object_Declaration (Loc,
14121 Aliased_Present =>
14122 True,
14123 Defining_Identifier =>
14124 Temp_Nam,
14125 Object_Definition =>
14126 New_Reference_To (Etype (Formal), Loc)));
14128 if Ekind (Formal) /= E_Out_Parameter then
14130 -- Generate:
14131 -- Jnn := <actual>
14133 Temp_Asn :=
14134 New_Reference_To (Temp_Nam, Loc);
14136 Set_Assignment_OK (Temp_Asn);
14138 Append_To (Stmts,
14139 Make_Assignment_Statement (Loc,
14140 Name =>
14141 Temp_Asn,
14142 Expression =>
14143 New_Copy_Tree (Actual)));
14144 end if;
14146 -- Generate:
14147 -- Jnn'unchecked_access
14149 Append_To (Params,
14150 Make_Attribute_Reference (Loc,
14151 Attribute_Name =>
14152 Name_Unchecked_Access,
14153 Prefix =>
14154 New_Reference_To (Temp_Nam, Loc)));
14156 Has_Param := True;
14158 -- The controlling parameter is omitted
14160 else
14161 if not Is_Controlling_Actual (Actual) then
14162 Append_To (Params,
14163 Make_Reference (Loc, New_Copy_Tree (Actual)));
14165 Has_Param := True;
14166 end if;
14167 end if;
14169 Next_Actual (Actual);
14170 Next_Formal_With_Extras (Formal);
14171 end loop;
14173 if Has_Param then
14174 Expr := Make_Aggregate (Loc, Params);
14175 end if;
14177 -- Generate:
14178 -- P : Ann := (
14179 -- J1'unchecked_access;
14180 -- <actual2>'reference;
14181 -- ...);
14183 P := Make_Temporary (Loc, 'P');
14185 Append_To (Decls,
14186 Make_Object_Declaration (Loc,
14187 Defining_Identifier =>
14189 Object_Definition =>
14190 New_Reference_To (Blk_Typ, Loc),
14191 Expression =>
14192 Expr));
14194 return P;
14195 end Parameter_Block_Pack;
14197 ----------------------------
14198 -- Parameter_Block_Unpack --
14199 ----------------------------
14201 function Parameter_Block_Unpack
14202 (Loc : Source_Ptr;
14203 P : Entity_Id;
14204 Actuals : List_Id;
14205 Formals : List_Id) return List_Id
14207 Actual : Entity_Id;
14208 Asnmt : Node_Id;
14209 Formal : Entity_Id;
14210 Has_Asnmt : Boolean := False;
14211 Result : constant List_Id := New_List;
14213 begin
14214 Actual := First (Actuals);
14215 Formal := Defining_Identifier (First (Formals));
14216 while Present (Actual) loop
14217 if Is_By_Copy_Type (Etype (Actual))
14218 and then Ekind (Formal) /= E_In_Parameter
14219 then
14220 -- Generate:
14221 -- <actual> := P.<formal>;
14223 Asnmt :=
14224 Make_Assignment_Statement (Loc,
14225 Name =>
14226 New_Copy (Actual),
14227 Expression =>
14228 Make_Explicit_Dereference (Loc,
14229 Make_Selected_Component (Loc,
14230 Prefix =>
14231 New_Reference_To (P, Loc),
14232 Selector_Name =>
14233 Make_Identifier (Loc, Chars (Formal)))));
14235 Set_Assignment_OK (Name (Asnmt));
14236 Append_To (Result, Asnmt);
14238 Has_Asnmt := True;
14239 end if;
14241 Next_Actual (Actual);
14242 Next_Formal_With_Extras (Formal);
14243 end loop;
14245 if Has_Asnmt then
14246 return Result;
14247 else
14248 return New_List (Make_Null_Statement (Loc));
14249 end if;
14250 end Parameter_Block_Unpack;
14252 ----------------------
14253 -- Set_Discriminals --
14254 ----------------------
14256 procedure Set_Discriminals (Dec : Node_Id) is
14257 D : Entity_Id;
14258 Pdef : Entity_Id;
14259 D_Minal : Entity_Id;
14261 begin
14262 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14263 Pdef := Defining_Identifier (Dec);
14265 if Has_Discriminants (Pdef) then
14266 D := First_Discriminant (Pdef);
14267 while Present (D) loop
14268 D_Minal :=
14269 Make_Defining_Identifier (Sloc (D),
14270 Chars => New_External_Name (Chars (D), 'D'));
14272 Set_Ekind (D_Minal, E_Constant);
14273 Set_Etype (D_Minal, Etype (D));
14274 Set_Scope (D_Minal, Pdef);
14275 Set_Discriminal (D, D_Minal);
14276 Set_Discriminal_Link (D_Minal, D);
14278 Next_Discriminant (D);
14279 end loop;
14280 end if;
14281 end Set_Discriminals;
14283 -----------------------
14284 -- Trivial_Accept_OK --
14285 -----------------------
14287 function Trivial_Accept_OK return Boolean is
14288 begin
14289 case Opt.Task_Dispatching_Policy is
14291 -- If we have the default task dispatching policy in effect, we can
14292 -- definitely do the optimization (one way of looking at this is to
14293 -- think of the formal definition of the default policy being allowed
14294 -- to run any task it likes after a rendezvous, so even if notionally
14295 -- a full rescheduling occurs, we can say that our dispatching policy
14296 -- (i.e. the default dispatching policy) reorders the queue to be the
14297 -- same as just before the call.
14299 when ' ' =>
14300 return True;
14302 -- FIFO_Within_Priorities certainly does not permit this
14303 -- optimization since the Rendezvous is a scheduling action that may
14304 -- require some other task to be run.
14306 when 'F' =>
14307 return False;
14309 -- For now, disallow the optimization for all other policies. This
14310 -- may be over-conservative, but it is certainly not incorrect.
14312 when others =>
14313 return False;
14315 end case;
14316 end Trivial_Accept_OK;
14318 end Exp_Ch9;