2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob572dae04ea082093cb8e41a79659bc80720bd9d2
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-2008, 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_Ch11; use Exp_Ch11;
33 with Exp_Ch6; use Exp_Ch6;
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_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch11; use Sem_Ch11;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Res; use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Stringt; use Stringt;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
65 package body Exp_Ch9 is
67 -- The following constant establishes the upper bound for the index of
68 -- an entry family. It is used to limit the allocated size of protected
69 -- types with defaulted discriminant of an integer type, when the bound
70 -- of some entry family depends on a discriminant. The limitation to
71 -- entry families of 128K should be reasonable in all cases, and is a
72 -- documented implementation restriction. It will be lifted when protected
73 -- entry families are re-implemented as a single ordered queue.
75 Entry_Family_Bound : constant Int := 2**16;
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Actual_Index_Expression
82 (Sloc : Source_Ptr;
83 Ent : Entity_Id;
84 Index : Node_Id;
85 Tsk : Entity_Id) return Node_Id;
86 -- Compute the index position for an entry call. Tsk is the target task. If
87 -- the bounds of some entry family depend on discriminants, the expression
88 -- computed by this function uses the discriminants of the target task.
90 procedure Add_Object_Pointer
91 (Loc : Source_Ptr;
92 Conc_Typ : Entity_Id;
93 Decls : List_Id);
94 -- Prepend an object pointer declaration to the declaration list Decls.
95 -- This object pointer is initialized to a type conversion of the System.
96 -- Address pointer passed to entry barrier functions and entry body
97 -- procedures.
99 procedure Add_Formal_Renamings
100 (Spec : Node_Id;
101 Decls : List_Id;
102 Ent : Entity_Id;
103 Loc : Source_Ptr);
104 -- Create renaming declarations for the formals, inside the procedure that
105 -- implements an entry body. The renamings make the original names of the
106 -- formals accessible to gdb, and serve no other purpose.
107 -- Spec is the specification of the procedure being built.
108 -- Decls is the list of declarations to be enhanced.
109 -- Ent is the entity for the original entry body.
111 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
112 -- Transform accept statement into a block with added exception handler.
113 -- Used both for simple accept statements and for accept alternatives in
114 -- select statements. Astat is the accept statement.
116 function Build_Barrier_Function
117 (N : Node_Id;
118 Ent : Entity_Id;
119 Pid : Node_Id) return Node_Id;
120 -- Build the function body returning the value of the barrier expression
121 -- for the specified entry body.
123 function Build_Barrier_Function_Specification
124 (Loc : Source_Ptr;
125 Def_Id : Entity_Id) return Node_Id;
126 -- Build a specification for a function implementing the protected entry
127 -- barrier of the specified entry body.
129 function Build_Entry_Count_Expression
130 (Concurrent_Type : Node_Id;
131 Component_List : List_Id;
132 Loc : Source_Ptr) return Node_Id;
133 -- Compute number of entries for concurrent object. This is a count of
134 -- simple entries, followed by an expression that computes the length
135 -- of the range of each entry family. A single array with that size is
136 -- allocated for each concurrent object of the type.
138 function Build_Parameter_Block
139 (Loc : Source_Ptr;
140 Actuals : List_Id;
141 Formals : List_Id;
142 Decls : List_Id) return Entity_Id;
143 -- Generate an access type for each actual parameter in the list Actuals.
144 -- Create an encapsulating record that contains all the actuals and return
145 -- its type. Generate:
146 -- type Ann1 is access all <actual1-type>
147 -- ...
148 -- type AnnN is access all <actualN-type>
149 -- type Pnn is record
150 -- <formal1> : Ann1;
151 -- ...
152 -- <formalN> : AnnN;
153 -- end record;
155 function Build_Wrapper_Body
156 (Loc : Source_Ptr;
157 Proc_Nam : Entity_Id;
158 Obj_Typ : Entity_Id;
159 Formals : List_Id) return Node_Id;
160 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
161 -- associated with a protected or task type. This is required to implement
162 -- dispatching calls through interfaces. Proc_Nam is the entry name to be
163 -- wrapped, Obj_Typ is the type of the newly added formal parameter to
164 -- handle object notation, Formals are the original entry formals that will
165 -- be explicitly replicated.
167 function Build_Wrapper_Spec
168 (Loc : Source_Ptr;
169 Proc_Nam : Entity_Id;
170 Obj_Typ : Entity_Id;
171 Formals : List_Id) return Node_Id;
172 -- Ada 2005 (AI-345): Build the specification of a primitive operation
173 -- associated with a protected or task type. This is required implement
174 -- dispatching calls through interfaces. Proc_Nam is the entry name to be
175 -- wrapped, Obj_Typ is the type of the newly added formal parameter to
176 -- handle object notation, Formals are the original entry formals that will
177 -- be explicitly replicated.
179 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
180 -- Build the function that translates the entry index in the call
181 -- (which depends on the size of entry families) into an index into the
182 -- Entry_Bodies_Array, to determine the body and barrier function used
183 -- in a protected entry call. A pointer to this function appears in every
184 -- protected object.
186 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
187 -- Build subprogram declaration for previous one
189 function Build_Protected_Entry
190 (N : Node_Id;
191 Ent : Entity_Id;
192 Pid : Node_Id) return Node_Id;
193 -- Build the procedure implementing the statement sequence of the specified
194 -- entry body.
196 function Build_Protected_Entry_Specification
197 (Loc : Source_Ptr;
198 Def_Id : Entity_Id;
199 Ent_Id : Entity_Id) return Node_Id;
200 -- Build a specification for the procedure implementing the statements of
201 -- the specified entry body. Add attributes associating it with the entry
202 -- defining identifier Ent_Id.
204 function Build_Protected_Spec
205 (N : Node_Id;
206 Obj_Type : Entity_Id;
207 Ident : Entity_Id;
208 Unprotected : Boolean := False) return List_Id;
209 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
210 -- Subprogram_Type. Builds signature of protected subprogram, adding the
211 -- formal that corresponds to the object itself. For an access to protected
212 -- subprogram, there is no object type to specify, so the parameter has
213 -- type Address and mode In. An indirect call through such a pointer will
214 -- convert the address to a reference to the actual object. The object is
215 -- a limited record and therefore a by_reference type.
217 function Build_Protected_Subprogram_Body
218 (N : Node_Id;
219 Pid : Node_Id;
220 N_Op_Spec : Node_Id) return Node_Id;
221 -- This function is used to construct the protected version of a protected
222 -- subprogram. Its statement sequence first defers abort, then locks
223 -- the associated protected object, and then enters a block that contains
224 -- a call to the unprotected version of the subprogram (for details, see
225 -- Build_Unprotected_Subprogram_Body). This block statement requires
226 -- a cleanup handler that unlocks the object in all cases.
227 -- (see Exp_Ch7.Expand_Cleanup_Actions).
229 function Build_Selected_Name
230 (Prefix : Entity_Id;
231 Selector : Entity_Id;
232 Append_Char : Character := ' ') return Name_Id;
233 -- Build a name in the form of Prefix__Selector, with an optional
234 -- character appended. This is used for internal subprograms generated
235 -- for operations of protected types, including barrier functions.
236 -- For the subprograms generated for entry bodies and entry barriers,
237 -- the generated name includes a sequence number that makes names
238 -- unique in the presence of entry overloading. This is necessary
239 -- because entry body procedures and barrier functions all have the
240 -- same signature.
242 procedure Build_Simple_Entry_Call
243 (N : Node_Id;
244 Concval : Node_Id;
245 Ename : Node_Id;
246 Index : Node_Id);
247 -- Some comments here would be useful ???
249 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
250 -- This routine constructs a specification for the procedure that we will
251 -- build for the task body for task type T. The spec has the form:
253 -- procedure tnameB (_Task : access tnameV);
255 -- where name is the character name taken from the task type entity that
256 -- is passed as the argument to the procedure, and tnameV is the task
257 -- value type that is associated with the task type.
259 function Build_Unprotected_Subprogram_Body
260 (N : Node_Id;
261 Pid : Node_Id) return Node_Id;
262 -- This routine constructs the unprotected version of a protected
263 -- subprogram body, which is contains all of the code in the
264 -- original, unexpanded body. This is the version of the protected
265 -- subprogram that is called from all protected operations on the same
266 -- object, including the protected version of the same subprogram.
268 procedure Collect_Entry_Families
269 (Loc : Source_Ptr;
270 Cdecls : List_Id;
271 Current_Node : in out Node_Id;
272 Conctyp : Entity_Id);
273 -- For each entry family in a concurrent type, create an anonymous array
274 -- type of the right size, and add a component to the corresponding_record.
276 function Concurrent_Object
277 (Spec_Id : Entity_Id;
278 Conc_Typ : Entity_Id) return Entity_Id;
279 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
280 -- the entity associated with the concurrent object in the Protected_Body_
281 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
282 -- denotes formal parameter _O, _object or _task.
284 function Copy_Result_Type (Res : Node_Id) return Node_Id;
285 -- Copy the result type of a function specification, when building the
286 -- internal operation corresponding to a protected function, or when
287 -- expanding an access to protected function. If the result is an anonymous
288 -- access to subprogram itself, we need to create a new signature with the
289 -- same parameter names and the same resolved types, but with new entities
290 -- for the formals.
292 procedure Debug_Private_Data_Declarations (Decls : List_Id);
293 -- Decls is a list which may contain the declarations created by Install_
294 -- Private_Data_Declarations. All generated entities are marked as needing
295 -- debug info and debug nodes are manually generation where necessary. This
296 -- step of the expansion must to be done after private data has been moved
297 -- to its final resting scope to ensure proper visibility of debug objects.
299 function Family_Offset
300 (Loc : Source_Ptr;
301 Hi : Node_Id;
302 Lo : Node_Id;
303 Ttyp : Entity_Id;
304 Cap : Boolean) return Node_Id;
305 -- Compute (Hi - Lo) for two entry family indices. Hi is the index in
306 -- an accept statement, or the upper bound in the discrete subtype of
307 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
308 -- the concurrent type of the entry. If Cap is true, the result is
309 -- capped according to Entry_Family_Bound.
311 function Family_Size
312 (Loc : Source_Ptr;
313 Hi : Node_Id;
314 Lo : Node_Id;
315 Ttyp : Entity_Id;
316 Cap : Boolean) return Node_Id;
317 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
318 -- a family, and handle properly the superflat case. This is equivalent
319 -- to the use of 'Length on the index type, but must use Family_Offset
320 -- to handle properly the case of bounds that depend on discriminants.
321 -- If Cap is true, the result is capped according to Entry_Family_Bound.
323 procedure Extract_Dispatching_Call
324 (N : Node_Id;
325 Call_Ent : out Entity_Id;
326 Object : out Entity_Id;
327 Actuals : out List_Id;
328 Formals : out List_Id);
329 -- Given a dispatching call, extract the entity of the name of the call,
330 -- its object parameter, its actual parameters and the formal parameters
331 -- of the overridden interface-level version.
333 procedure Extract_Entry
334 (N : Node_Id;
335 Concval : out Node_Id;
336 Ename : out Node_Id;
337 Index : out Node_Id);
338 -- Given an entry call, returns the associated concurrent object,
339 -- the entry name, and the entry family index.
341 function Find_Task_Or_Protected_Pragma
342 (T : Node_Id;
343 P : Name_Id) return Node_Id;
344 -- Searches the task or protected definition T for the first occurrence
345 -- of the pragma whose name is given by P. The caller has ensured that
346 -- the pragma is present in the task definition. A special case is that
347 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
348 -- ??? Should be implemented with the rep item chain mechanism.
350 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
351 -- Given a subprogram identifier, return the entity which is associated
352 -- with the protection entry index in the Protected_Body_Subprogram or the
353 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
354 -- parameter _E.
356 function Is_Potentially_Large_Family
357 (Base_Index : Entity_Id;
358 Conctyp : Entity_Id;
359 Lo : Node_Id;
360 Hi : Node_Id) return Boolean;
362 function Null_Statements (Stats : List_Id) return Boolean;
363 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
364 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
365 -- well to still count as null. Returns True for a null sequence. The
366 -- argument is the list of statements from the DO-END sequence.
368 function Parameter_Block_Pack
369 (Loc : Source_Ptr;
370 Blk_Typ : Entity_Id;
371 Actuals : List_Id;
372 Formals : List_Id;
373 Decls : List_Id;
374 Stmts : List_Id) return Entity_Id;
375 -- Set the components of the generated parameter block with the values of
376 -- the actual parameters. Generate aliased temporaries to capture the
377 -- values for types that are passed by copy. Otherwise generate a reference
378 -- to the actual's value. Return the address of the aggregate block.
379 -- Generate:
380 -- Jnn1 : alias <formal-type1>;
381 -- Jnn1 := <actual1>;
382 -- ...
383 -- P : Blk_Typ := (
384 -- Jnn1'unchecked_access;
385 -- <actual2>'reference;
386 -- ...);
388 function Parameter_Block_Unpack
389 (Loc : Source_Ptr;
390 P : Entity_Id;
391 Actuals : List_Id;
392 Formals : List_Id) return List_Id;
393 -- Retrieve the values of the components from the parameter block and
394 -- assign then to the original actual parameters. Generate:
395 -- <actual1> := P.<formal1>;
396 -- ...
397 -- <actualN> := P.<formalN>;
399 function Trivial_Accept_OK return Boolean;
400 -- If there is no DO-END block for an accept, or if the DO-END block has
401 -- only null statements, then it is possible to do the Rendezvous with much
402 -- less overhead using the Accept_Trivial routine in the run-time library.
403 -- However, this is not always a valid optimization. Whether it is valid or
404 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
405 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
406 -- a rescheduling is required, so this optimization is not allowed. This
407 -- function returns True if the optimization is permitted.
409 -----------------------------
410 -- Actual_Index_Expression --
411 -----------------------------
413 function Actual_Index_Expression
414 (Sloc : Source_Ptr;
415 Ent : Entity_Id;
416 Index : Node_Id;
417 Tsk : Entity_Id) return Node_Id
419 Ttyp : constant Entity_Id := Etype (Tsk);
420 Expr : Node_Id;
421 Num : Node_Id;
422 Lo : Node_Id;
423 Hi : Node_Id;
424 Prev : Entity_Id;
425 S : Node_Id;
427 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
428 -- Compute difference between bounds of entry family
430 --------------------------
431 -- Actual_Family_Offset --
432 --------------------------
434 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
436 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
437 -- Replace a reference to a discriminant with a selected component
438 -- denoting the discriminant of the target task.
440 -----------------------------
441 -- Actual_Discriminant_Ref --
442 -----------------------------
444 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
445 Typ : constant Entity_Id := Etype (Bound);
446 B : Node_Id;
448 begin
449 if not Is_Entity_Name (Bound)
450 or else Ekind (Entity (Bound)) /= E_Discriminant
451 then
452 if Nkind (Bound) = N_Attribute_Reference then
453 return Bound;
454 else
455 B := New_Copy_Tree (Bound);
456 end if;
458 else
459 B :=
460 Make_Selected_Component (Sloc,
461 Prefix => New_Copy_Tree (Tsk),
462 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
464 Analyze_And_Resolve (B, Typ);
465 end if;
467 return
468 Make_Attribute_Reference (Sloc,
469 Attribute_Name => Name_Pos,
470 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
471 Expressions => New_List (B));
472 end Actual_Discriminant_Ref;
474 -- Start of processing for Actual_Family_Offset
476 begin
477 return
478 Make_Op_Subtract (Sloc,
479 Left_Opnd => Actual_Discriminant_Ref (Hi),
480 Right_Opnd => Actual_Discriminant_Ref (Lo));
481 end Actual_Family_Offset;
483 -- Start of processing for Actual_Index_Expression
485 begin
486 -- The queues of entries and entry families appear in textual order in
487 -- the associated record. The entry index is computed as the sum of the
488 -- number of queues for all entries that precede the designated one, to
489 -- which is added the index expression, if this expression denotes a
490 -- member of a family.
492 -- The following is a place holder for the count of simple entries
494 Num := Make_Integer_Literal (Sloc, 1);
496 -- We construct an expression which is a series of addition operations.
497 -- See comments in Entry_Index_Expression, which is identical in
498 -- structure.
500 if Present (Index) then
501 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
503 Expr :=
504 Make_Op_Add (Sloc,
505 Left_Opnd => Num,
507 Right_Opnd =>
508 Actual_Family_Offset (
509 Make_Attribute_Reference (Sloc,
510 Attribute_Name => Name_Pos,
511 Prefix => New_Reference_To (Base_Type (S), Sloc),
512 Expressions => New_List (Relocate_Node (Index))),
513 Type_Low_Bound (S)));
514 else
515 Expr := Num;
516 end if;
518 -- Now add lengths of preceding entries and entry families
520 Prev := First_Entity (Ttyp);
522 while Chars (Prev) /= Chars (Ent)
523 or else (Ekind (Prev) /= Ekind (Ent))
524 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
525 loop
526 if Ekind (Prev) = E_Entry then
527 Set_Intval (Num, Intval (Num) + 1);
529 elsif Ekind (Prev) = E_Entry_Family then
530 S :=
531 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
533 -- The need for the following full view retrieval stems from
534 -- this complex case of nested generics and tasking:
536 -- generic
537 -- type Formal_Index is range <>;
538 -- ...
539 -- package Outer is
540 -- type Index is private;
541 -- generic
542 -- ...
543 -- package Inner is
544 -- procedure P;
545 -- end Inner;
546 -- private
547 -- type Index is new Formal_Index range 1 .. 10;
548 -- end Outer;
550 -- package body Outer is
551 -- task type T is
552 -- entry Fam (Index); -- (2)
553 -- entry E;
554 -- end T;
555 -- package body Inner is -- (3)
556 -- procedure P is
557 -- begin
558 -- T.E; -- (1)
559 -- end P;
560 -- end Inner;
561 -- ...
563 -- We are currently building the index expression for the entry
564 -- call "T.E" (1). Part of the expansion must mention the range
565 -- of the discrete type "Index" (2) of entry family "Fam".
566 -- However only the private view of type "Index" is available to
567 -- the inner generic (3) because there was no prior mention of
568 -- the type inside "Inner". This visibility requirement is
569 -- implicit and cannot be detected during the construction of
570 -- the generic trees and needs special handling.
572 if In_Instance_Body
573 and then Is_Private_Type (S)
574 and then Present (Full_View (S))
575 then
576 S := Full_View (S);
577 end if;
579 Lo := Type_Low_Bound (S);
580 Hi := Type_High_Bound (S);
582 Expr :=
583 Make_Op_Add (Sloc,
584 Left_Opnd => Expr,
585 Right_Opnd =>
586 Make_Op_Add (Sloc,
587 Left_Opnd =>
588 Actual_Family_Offset (Hi, Lo),
589 Right_Opnd =>
590 Make_Integer_Literal (Sloc, 1)));
592 -- Other components are anonymous types to be ignored
594 else
595 null;
596 end if;
598 Next_Entity (Prev);
599 end loop;
601 return Expr;
602 end Actual_Index_Expression;
604 --------------------------
605 -- Add_Formal_Renamings --
606 --------------------------
608 procedure Add_Formal_Renamings
609 (Spec : Node_Id;
610 Decls : List_Id;
611 Ent : Entity_Id;
612 Loc : Source_Ptr)
614 Ptr : constant Entity_Id :=
615 Defining_Identifier
616 (Next (First (Parameter_Specifications (Spec))));
617 -- The name of the formal that holds the address of the parameter block
618 -- for the call.
620 Comp : Entity_Id;
621 Decl : Node_Id;
622 Formal : Entity_Id;
623 New_F : Entity_Id;
625 begin
626 Formal := First_Formal (Ent);
627 while Present (Formal) loop
628 Comp := Entry_Component (Formal);
629 New_F :=
630 Make_Defining_Identifier (Sloc (Formal),
631 Chars => Chars (Formal));
632 Set_Etype (New_F, Etype (Formal));
633 Set_Scope (New_F, Ent);
635 -- Now we set debug info needed on New_F even though it does not
636 -- come from source, so that the debugger will get the right
637 -- information for these generated names.
639 Set_Debug_Info_Needed (New_F);
641 if Ekind (Formal) = E_In_Parameter then
642 Set_Ekind (New_F, E_Constant);
643 else
644 Set_Ekind (New_F, E_Variable);
645 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
646 end if;
648 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
650 Decl :=
651 Make_Object_Renaming_Declaration (Loc,
652 Defining_Identifier => New_F,
653 Subtype_Mark =>
654 New_Reference_To (Etype (Formal), Loc),
655 Name =>
656 Make_Explicit_Dereference (Loc,
657 Make_Selected_Component (Loc,
658 Prefix =>
659 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
660 Make_Identifier (Loc, Chars (Ptr))),
661 Selector_Name =>
662 New_Reference_To (Comp, Loc))));
664 Append (Decl, Decls);
665 Set_Renamed_Object (Formal, New_F);
666 Next_Formal (Formal);
667 end loop;
668 end Add_Formal_Renamings;
670 ------------------------
671 -- Add_Object_Pointer --
672 ------------------------
674 procedure Add_Object_Pointer
675 (Loc : Source_Ptr;
676 Conc_Typ : Entity_Id;
677 Decls : List_Id)
679 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
680 Decl : Node_Id;
681 Obj_Ptr : Node_Id;
683 begin
684 -- Create the renaming declaration for the Protection object of a
685 -- protected type. _Object is used by Complete_Entry_Body.
686 -- ??? An attempt to make this a renaming was unsuccessful.
688 -- Build the entity for the access type
690 Obj_Ptr :=
691 Make_Defining_Identifier (Loc,
692 New_External_Name (Chars (Rec_Typ), 'P'));
694 -- Generate:
695 -- _object : poVP := poVP!O;
697 Decl :=
698 Make_Object_Declaration (Loc,
699 Defining_Identifier =>
700 Make_Defining_Identifier (Loc, Name_uObject),
701 Object_Definition =>
702 New_Reference_To (Obj_Ptr, Loc),
703 Expression =>
704 Unchecked_Convert_To (Obj_Ptr,
705 Make_Identifier (Loc, Name_uO)));
706 Set_Debug_Info_Needed (Defining_Identifier (Decl));
707 Prepend_To (Decls, Decl);
709 -- Generate:
710 -- type poVP is access poV;
712 Decl :=
713 Make_Full_Type_Declaration (Loc,
714 Defining_Identifier =>
715 Obj_Ptr,
716 Type_Definition =>
717 Make_Access_To_Object_Definition (Loc,
718 Subtype_Indication =>
719 New_Reference_To (Rec_Typ, Loc)));
720 Set_Debug_Info_Needed (Defining_Identifier (Decl));
721 Prepend_To (Decls, Decl);
722 end Add_Object_Pointer;
724 -----------------------
725 -- Build_Accept_Body --
726 -----------------------
728 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
729 Loc : constant Source_Ptr := Sloc (Astat);
730 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
731 New_S : Node_Id;
732 Hand : Node_Id;
733 Call : Node_Id;
734 Ohandle : Node_Id;
736 begin
737 -- At the end of the statement sequence, Complete_Rendezvous is called.
738 -- A label skipping the Complete_Rendezvous, and all other accept
739 -- processing, has already been added for the expansion of requeue
740 -- statements.
742 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
743 Insert_Before (Last (Statements (Stats)), Call);
744 Analyze (Call);
746 -- If exception handlers are present, then append Complete_Rendezvous
747 -- calls to the handlers, and construct the required outer block.
749 if Present (Exception_Handlers (Stats)) then
750 Hand := First (Exception_Handlers (Stats));
752 while Present (Hand) loop
753 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
754 Append (Call, Statements (Hand));
755 Analyze (Call);
756 Next (Hand);
757 end loop;
759 New_S :=
760 Make_Handled_Sequence_Of_Statements (Loc,
761 Statements => New_List (
762 Make_Block_Statement (Loc,
763 Handled_Statement_Sequence => Stats)));
765 else
766 New_S := Stats;
767 end if;
769 -- At this stage we know that the new statement sequence does not
770 -- have an exception handler part, so we supply one to call
771 -- Exceptional_Complete_Rendezvous. This handler is
773 -- when all others =>
774 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
776 -- We handle Abort_Signal to make sure that we properly catch the abort
777 -- case and wake up the caller.
779 Ohandle := Make_Others_Choice (Loc);
780 Set_All_Others (Ohandle);
782 Set_Exception_Handlers (New_S,
783 New_List (
784 Make_Implicit_Exception_Handler (Loc,
785 Exception_Choices => New_List (Ohandle),
787 Statements => New_List (
788 Make_Procedure_Call_Statement (Loc,
789 Name => New_Reference_To (
790 RTE (RE_Exceptional_Complete_Rendezvous), Loc),
791 Parameter_Associations => New_List (
792 Make_Function_Call (Loc,
793 Name => New_Reference_To (
794 RTE (RE_Get_GNAT_Exception), Loc))))))));
796 Set_Parent (New_S, Astat); -- temp parent for Analyze call
797 Analyze_Exception_Handlers (Exception_Handlers (New_S));
798 Expand_Exception_Handlers (New_S);
800 -- Exceptional_Complete_Rendezvous must be called with abort
801 -- still deferred, which is the case for a "when all others" handler.
803 return New_S;
804 end Build_Accept_Body;
806 -----------------------------------
807 -- Build_Activation_Chain_Entity --
808 -----------------------------------
810 procedure Build_Activation_Chain_Entity (N : Node_Id) is
811 P : Node_Id;
812 Decls : List_Id;
813 Chain : Entity_Id;
815 begin
816 -- Loop to find enclosing construct containing activation chain variable
818 P := Parent (N);
820 while not Nkind_In (P, N_Subprogram_Body,
821 N_Package_Declaration,
822 N_Package_Body,
823 N_Block_Statement,
824 N_Task_Body,
825 N_Extended_Return_Statement)
826 loop
827 P := Parent (P);
828 end loop;
830 -- If we are in a package body, the activation chain variable is
831 -- declared in the body, but the Activation_Chain_Entity is attached to
832 -- the spec.
834 if Nkind (P) = N_Package_Body then
835 Decls := Declarations (P);
836 P := Unit_Declaration_Node (Corresponding_Spec (P));
838 elsif Nkind (P) = N_Package_Declaration then
839 Decls := Visible_Declarations (Specification (P));
841 elsif Nkind (P) = N_Extended_Return_Statement then
842 Decls := Return_Object_Declarations (P);
844 else
845 Decls := Declarations (P);
846 end if;
848 -- If activation chain entity not already declared, declare it
850 if Nkind (P) = N_Extended_Return_Statement
851 or else No (Activation_Chain_Entity (P))
852 then
853 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
855 -- Note: An extended return statement is not really a task activator,
856 -- but it does have an activation chain on which to store the tasks
857 -- temporarily. On successful return, the tasks on this chain are
858 -- moved to the chain passed in by the caller. We do not build an
859 -- Activation_Chain_Entity for an N_Extended_Return_Statement,
860 -- because we do not want to build a call to Activate_Tasks. Task
861 -- activation is the responsibility of the caller.
863 if Nkind (P) /= N_Extended_Return_Statement then
864 Set_Activation_Chain_Entity (P, Chain);
865 end if;
867 Prepend_To (Decls,
868 Make_Object_Declaration (Sloc (P),
869 Defining_Identifier => Chain,
870 Aliased_Present => True,
871 Object_Definition =>
872 New_Reference_To (RTE (RE_Activation_Chain), Sloc (P))));
874 Analyze (First (Decls));
875 end if;
876 end Build_Activation_Chain_Entity;
878 ----------------------------
879 -- Build_Barrier_Function --
880 ----------------------------
882 function Build_Barrier_Function
883 (N : Node_Id;
884 Ent : Entity_Id;
885 Pid : Node_Id) return Node_Id
887 Loc : constant Source_Ptr := Sloc (N);
888 Func_Id : constant Entity_Id := Barrier_Function (Ent);
889 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
890 Op_Decls : constant List_Id := New_List;
891 Func_Body : Node_Id;
893 begin
894 -- Add a declaration for the Protection object, renaming declarations
895 -- for the discriminals and privals and finally a declaration for the
896 -- entry family index (if applicable).
898 Install_Private_Data_Declarations
899 (Loc, Func_Id, Pid, N, Op_Decls, True, Ekind (Ent) = E_Entry_Family);
901 -- Note: the condition in the barrier function needs to be properly
902 -- processed for the C/Fortran boolean possibility, but this happens
903 -- automatically since the return statement does this normalization.
905 Func_Body :=
906 Make_Subprogram_Body (Loc,
907 Specification =>
908 Build_Barrier_Function_Specification (Loc,
909 Make_Defining_Identifier (Loc, Chars (Func_Id))),
910 Declarations => Op_Decls,
911 Handled_Statement_Sequence =>
912 Make_Handled_Sequence_Of_Statements (Loc,
913 Statements => New_List (
914 Make_Simple_Return_Statement (Loc,
915 Expression => Condition (Ent_Formals)))));
916 Set_Is_Entry_Barrier_Function (Func_Body);
918 return Func_Body;
919 end Build_Barrier_Function;
921 ------------------------------------------
922 -- Build_Barrier_Function_Specification --
923 ------------------------------------------
925 function Build_Barrier_Function_Specification
926 (Loc : Source_Ptr;
927 Def_Id : Entity_Id) return Node_Id
929 begin
930 Set_Debug_Info_Needed (Def_Id);
932 return Make_Function_Specification (Loc,
933 Defining_Unit_Name => Def_Id,
934 Parameter_Specifications => New_List (
935 Make_Parameter_Specification (Loc,
936 Defining_Identifier =>
937 Make_Defining_Identifier (Loc, Name_uO),
938 Parameter_Type =>
939 New_Reference_To (RTE (RE_Address), Loc)),
941 Make_Parameter_Specification (Loc,
942 Defining_Identifier =>
943 Make_Defining_Identifier (Loc, Name_uE),
944 Parameter_Type =>
945 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
947 Result_Definition =>
948 New_Reference_To (Standard_Boolean, Loc));
949 end Build_Barrier_Function_Specification;
951 --------------------------
952 -- Build_Call_With_Task --
953 --------------------------
955 function Build_Call_With_Task
956 (N : Node_Id;
957 E : Entity_Id) return Node_Id
959 Loc : constant Source_Ptr := Sloc (N);
960 begin
961 return
962 Make_Function_Call (Loc,
963 Name => New_Reference_To (E, Loc),
964 Parameter_Associations => New_List (Concurrent_Ref (N)));
965 end Build_Call_With_Task;
967 --------------------------------
968 -- Build_Corresponding_Record --
969 --------------------------------
971 function Build_Corresponding_Record
972 (N : Node_Id;
973 Ctyp : Entity_Id;
974 Loc : Source_Ptr) return Node_Id
976 Rec_Ent : constant Entity_Id :=
977 Make_Defining_Identifier
978 (Loc, New_External_Name (Chars (Ctyp), 'V'));
979 Disc : Entity_Id;
980 Dlist : List_Id;
981 New_Disc : Entity_Id;
982 Cdecls : List_Id;
984 begin
985 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
986 Set_Ekind (Rec_Ent, E_Record_Type);
987 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
988 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
989 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
990 Set_Stored_Constraint (Rec_Ent, No_Elist);
991 Cdecls := New_List;
993 -- Use discriminals to create list of discriminants for record, and
994 -- create new discriminals for use in default expressions, etc. It is
995 -- worth noting that a task discriminant gives rise to 5 entities;
997 -- a) The original discriminant.
998 -- b) The discriminal for use in the task.
999 -- c) The discriminant of the corresponding record.
1000 -- d) The discriminal for the init proc of the corresponding record.
1001 -- e) The local variable that renames the discriminant in the procedure
1002 -- for the task body.
1004 -- In fact the discriminals b) are used in the renaming declarations
1005 -- for e). See details in einfo (Handling of Discriminants).
1007 if Present (Discriminant_Specifications (N)) then
1008 Dlist := New_List;
1009 Disc := First_Discriminant (Ctyp);
1011 while Present (Disc) loop
1012 New_Disc := CR_Discriminant (Disc);
1014 Append_To (Dlist,
1015 Make_Discriminant_Specification (Loc,
1016 Defining_Identifier => New_Disc,
1017 Discriminant_Type =>
1018 New_Occurrence_Of (Etype (Disc), Loc),
1019 Expression =>
1020 New_Copy (Discriminant_Default_Value (Disc))));
1022 Next_Discriminant (Disc);
1023 end loop;
1025 else
1026 Dlist := No_List;
1027 end if;
1029 -- Now we can construct the record type declaration. Note that this
1030 -- record is "limited tagged". It is "limited" to reflect the underlying
1031 -- limitedness of the task or protected object that it represents, and
1032 -- ensuring for example that it is properly passed by reference. It is
1033 -- "tagged" to give support to dispatching calls through interfaces (Ada
1034 -- 2005: AI-345)
1036 return
1037 Make_Full_Type_Declaration (Loc,
1038 Defining_Identifier => Rec_Ent,
1039 Discriminant_Specifications => Dlist,
1040 Type_Definition =>
1041 Make_Record_Definition (Loc,
1042 Component_List =>
1043 Make_Component_List (Loc,
1044 Component_Items => Cdecls),
1045 Tagged_Present =>
1046 Ada_Version >= Ada_05 and then Is_Tagged_Type (Ctyp),
1047 Limited_Present => True));
1048 end Build_Corresponding_Record;
1050 ----------------------------------
1051 -- Build_Entry_Count_Expression --
1052 ----------------------------------
1054 function Build_Entry_Count_Expression
1055 (Concurrent_Type : Node_Id;
1056 Component_List : List_Id;
1057 Loc : Source_Ptr) return Node_Id
1059 Eindx : Nat;
1060 Ent : Entity_Id;
1061 Ecount : Node_Id;
1062 Comp : Node_Id;
1063 Lo : Node_Id;
1064 Hi : Node_Id;
1065 Typ : Entity_Id;
1066 Large : Boolean;
1068 begin
1069 -- Count number of non-family entries
1071 Eindx := 0;
1072 Ent := First_Entity (Concurrent_Type);
1073 while Present (Ent) loop
1074 if Ekind (Ent) = E_Entry then
1075 Eindx := Eindx + 1;
1076 end if;
1078 Next_Entity (Ent);
1079 end loop;
1081 Ecount := Make_Integer_Literal (Loc, Eindx);
1083 -- Loop through entry families building the addition nodes
1085 Ent := First_Entity (Concurrent_Type);
1086 Comp := First (Component_List);
1087 while Present (Ent) loop
1088 if Ekind (Ent) = E_Entry_Family then
1089 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1090 Next (Comp);
1091 end loop;
1093 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1094 Hi := Type_High_Bound (Typ);
1095 Lo := Type_Low_Bound (Typ);
1096 Large := Is_Potentially_Large_Family
1097 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1098 Ecount :=
1099 Make_Op_Add (Loc,
1100 Left_Opnd => Ecount,
1101 Right_Opnd => Family_Size
1102 (Loc, Hi, Lo, Concurrent_Type, Large));
1103 end if;
1105 Next_Entity (Ent);
1106 end loop;
1108 return Ecount;
1109 end Build_Entry_Count_Expression;
1111 -----------------------
1112 -- Build_Entry_Names --
1113 -----------------------
1115 function Build_Entry_Names (Conc_Typ : Entity_Id) return Node_Id is
1116 Loc : constant Source_Ptr := Sloc (Conc_Typ);
1117 B_Decls : List_Id;
1118 B_Stmts : List_Id;
1119 Comp : Node_Id;
1120 Index : Entity_Id;
1121 Index_Typ : RE_Id;
1122 Typ : Entity_Id := Conc_Typ;
1124 procedure Build_Entry_Family_Name (Id : Entity_Id);
1125 -- Generate:
1126 -- for Lnn in Family_Low .. Family_High loop
1127 -- Inn := Inn + 1;
1128 -- Set_Entry_Name
1129 -- (_init._object, Inn, new String ("<Entry name> " & Lnn'Img));
1130 -- _init._task_id
1131 -- end loop;
1132 -- Note that the bounds of the range may reference discriminants. The
1133 -- above construct is added directly to the statements of the block.
1135 procedure Build_Entry_Name (Id : Entity_Id);
1136 -- Generate:
1137 -- Inn := Inn + 1;
1138 -- Set_Entry_Name (_init._task_id, Inn, new String ("<Entry name>");
1139 -- _init._object
1140 -- The above construct is added directly to the statements of the block.
1142 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id;
1143 -- Generate the call to the runtime routine Set_Entry_Name with actuals
1144 -- _init._task_id or _init._object, Inn and Arg3.
1146 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id;
1147 -- Given a protected type or its corresponding record, find the type of
1148 -- field _object.
1150 procedure Increment_Index (Stmts : List_Id);
1151 -- Generate the following and add it to Stmts
1152 -- Inn := Inn + 1;
1154 -----------------------------
1155 -- Build_Entry_Family_Name --
1156 -----------------------------
1158 procedure Build_Entry_Family_Name (Id : Entity_Id) is
1159 Def : constant Node_Id :=
1160 Discrete_Subtype_Definition (Parent (Id));
1161 L_Id : constant Entity_Id :=
1162 Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
1163 L_Stmts : constant List_Id := New_List;
1164 Val : Node_Id;
1166 function Build_Range (Def : Node_Id) return Node_Id;
1167 -- Given a discrete subtype definition of an entry family, generate a
1168 -- range node which covers the range of Def's type.
1170 -----------------
1171 -- Build_Range --
1172 -----------------
1174 function Build_Range (Def : Node_Id) return Node_Id is
1175 High : Node_Id := Type_High_Bound (Etype (Def));
1176 Low : Node_Id := Type_Low_Bound (Etype (Def));
1178 begin
1179 -- If a bound references a discriminant, generate an identifier
1180 -- with the same name. Resolution will map it to the formals of
1181 -- the init proc.
1183 if Is_Entity_Name (Low)
1184 and then Ekind (Entity (Low)) = E_Discriminant
1185 then
1186 Low := Make_Identifier (Loc, Chars (Low));
1187 else
1188 Low := New_Copy_Tree (Low);
1189 end if;
1191 if Is_Entity_Name (High)
1192 and then Ekind (Entity (High)) = E_Discriminant
1193 then
1194 High := Make_Identifier (Loc, Chars (High));
1195 else
1196 High := New_Copy_Tree (High);
1197 end if;
1199 return
1200 Make_Range (Loc,
1201 Low_Bound => Low,
1202 High_Bound => High);
1203 end Build_Range;
1205 -- Start of processing for Build_Entry_Family_Name
1207 begin
1208 Get_Name_String (Chars (Id));
1210 if Is_Enumeration_Type (Etype (Def)) then
1211 Name_Len := Name_Len + 1;
1212 Name_Buffer (Name_Len) := ' ';
1213 end if;
1215 -- Generate:
1216 -- new String'("<Entry name>" & Lnn'Img);
1218 Val :=
1219 Make_Allocator (Loc,
1220 Make_Qualified_Expression (Loc,
1221 Subtype_Mark =>
1222 New_Reference_To (Standard_String, Loc),
1223 Expression =>
1224 Make_Op_Concat (Loc,
1225 Left_Opnd =>
1226 Make_String_Literal (Loc,
1227 String_From_Name_Buffer),
1228 Right_Opnd =>
1229 Make_Attribute_Reference (Loc,
1230 Prefix =>
1231 New_Reference_To (L_Id, Loc),
1232 Attribute_Name => Name_Img))));
1234 Increment_Index (L_Stmts);
1235 Append_To (L_Stmts, Build_Set_Entry_Name_Call (Val));
1237 -- Generate:
1238 -- for Lnn in Family_Low .. Family_High loop
1239 -- Inn := Inn + 1;
1240 -- Set_Entry_Name (_init._task_id, Inn, <Val>);
1241 -- end loop;
1243 Append_To (B_Stmts,
1244 Make_Loop_Statement (Loc,
1245 Iteration_Scheme =>
1246 Make_Iteration_Scheme (Loc,
1247 Loop_Parameter_Specification =>
1248 Make_Loop_Parameter_Specification (Loc,
1249 Defining_Identifier => L_Id,
1250 Discrete_Subtype_Definition =>
1251 Build_Range (Def))),
1252 Statements => L_Stmts,
1253 End_Label => Empty));
1254 end Build_Entry_Family_Name;
1256 ----------------------
1257 -- Build_Entry_Name --
1258 ----------------------
1260 procedure Build_Entry_Name (Id : Entity_Id) is
1261 Val : Node_Id;
1263 begin
1264 Get_Name_String (Chars (Id));
1265 Val :=
1266 Make_Allocator (Loc,
1267 Make_Qualified_Expression (Loc,
1268 Subtype_Mark =>
1269 New_Reference_To (Standard_String, Loc),
1270 Expression =>
1271 Make_String_Literal (Loc,
1272 String_From_Name_Buffer)));
1274 Increment_Index (B_Stmts);
1275 Append_To (B_Stmts, Build_Set_Entry_Name_Call (Val));
1276 end Build_Entry_Name;
1278 -------------------------------
1279 -- Build_Set_Entry_Name_Call --
1280 -------------------------------
1282 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id is
1283 Arg1 : Name_Id;
1284 Proc : RE_Id;
1286 begin
1287 -- Determine the proper name for the first argument and the RTS
1288 -- routine to call.
1290 if Is_Protected_Type (Typ) then
1291 Arg1 := Name_uObject;
1292 Proc := RO_PE_Set_Entry_Name;
1294 else pragma Assert (Is_Task_Type (Typ));
1295 Arg1 := Name_uTask_Id;
1296 Proc := RO_TS_Set_Entry_Name;
1297 end if;
1299 -- Generate:
1300 -- Set_Entry_Name (_init.Arg1, Inn, Arg3);
1302 return
1303 Make_Procedure_Call_Statement (Loc,
1304 Name =>
1305 New_Reference_To (RTE (Proc), Loc),
1306 Parameter_Associations => New_List (
1307 Make_Selected_Component (Loc, -- _init._object
1308 Prefix => -- _init._task_id
1309 Make_Identifier (Loc, Name_uInit),
1310 Selector_Name =>
1311 Make_Identifier (Loc, Arg1)),
1312 New_Reference_To (Index, Loc), -- Inn
1313 Arg3)); -- Val
1314 end Build_Set_Entry_Name_Call;
1316 --------------------------
1317 -- Find_Protection_Type --
1318 --------------------------
1320 function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id is
1321 Comp : Entity_Id;
1322 Typ : Entity_Id := Conc_Typ;
1324 begin
1325 if Is_Concurrent_Type (Typ) then
1326 Typ := Corresponding_Record_Type (Typ);
1327 end if;
1329 Comp := First_Component (Typ);
1330 while Present (Comp) loop
1331 if Chars (Comp) = Name_uObject then
1332 return Base_Type (Etype (Comp));
1333 end if;
1335 Next_Component (Comp);
1336 end loop;
1338 -- The corresponding record of a protected type should always have an
1339 -- _object field.
1341 raise Program_Error;
1342 end Find_Protection_Type;
1344 ---------------------
1345 -- Increment_Index --
1346 ---------------------
1348 procedure Increment_Index (Stmts : List_Id) is
1349 begin
1350 -- Generate:
1351 -- Inn := Inn + 1;
1353 Append_To (Stmts,
1354 Make_Assignment_Statement (Loc,
1355 Name =>
1356 New_Reference_To (Index, Loc),
1357 Expression =>
1358 Make_Op_Add (Loc,
1359 Left_Opnd =>
1360 New_Reference_To (Index, Loc),
1361 Right_Opnd =>
1362 Make_Integer_Literal (Loc, 1))));
1363 end Increment_Index;
1365 -- Start of processing for Build_Entry_Names
1367 begin
1368 -- Retrieve the original concurrent type
1370 if Is_Concurrent_Record_Type (Typ) then
1371 Typ := Corresponding_Concurrent_Type (Typ);
1372 end if;
1374 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
1376 -- Nothing to do if the type has no entries
1378 if not Has_Entries (Typ) then
1379 return Empty;
1380 end if;
1382 -- Avoid generating entry names for a protected type with only one entry
1384 if Is_Protected_Type (Typ)
1385 and then Find_Protection_Type (Typ) /= RTE (RE_Protection_Entries)
1386 then
1387 return Empty;
1388 end if;
1390 Index := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
1392 -- Step 1: Generate the declaration of the index variable:
1393 -- Inn : Protected_Entry_Index := 0;
1394 -- or
1395 -- Inn : Task_Entry_Index := 0;
1397 if Is_Protected_Type (Typ) then
1398 Index_Typ := RE_Protected_Entry_Index;
1399 else
1400 Index_Typ := RE_Task_Entry_Index;
1401 end if;
1403 B_Decls := New_List;
1404 Append_To (B_Decls,
1405 Make_Object_Declaration (Loc,
1406 Defining_Identifier => Index,
1407 Object_Definition =>
1408 New_Reference_To (RTE (Index_Typ), Loc),
1409 Expression =>
1410 Make_Integer_Literal (Loc, 0)));
1412 B_Stmts := New_List;
1414 -- Step 2: Generate a call to Set_Entry_Name for each entry and entry
1415 -- family member.
1417 Comp := First_Entity (Typ);
1418 while Present (Comp) loop
1419 if Ekind (Comp) = E_Entry then
1420 Build_Entry_Name (Comp);
1422 elsif Ekind (Comp) = E_Entry_Family then
1423 Build_Entry_Family_Name (Comp);
1424 end if;
1426 Next_Entity (Comp);
1427 end loop;
1429 -- Step 3: Wrap the statements in a block
1431 return
1432 Make_Block_Statement (Loc,
1433 Declarations => B_Decls,
1434 Handled_Statement_Sequence =>
1435 Make_Handled_Sequence_Of_Statements (Loc,
1436 Statements => B_Stmts));
1437 end Build_Entry_Names;
1439 ---------------------------
1440 -- Build_Parameter_Block --
1441 ---------------------------
1443 function Build_Parameter_Block
1444 (Loc : Source_Ptr;
1445 Actuals : List_Id;
1446 Formals : List_Id;
1447 Decls : List_Id) return Entity_Id
1449 Actual : Entity_Id;
1450 Comp_Nam : Node_Id;
1451 Comps : List_Id;
1452 Formal : Entity_Id;
1453 Has_Comp : Boolean := False;
1454 Rec_Nam : Node_Id;
1456 begin
1457 Actual := First (Actuals);
1458 Comps := New_List;
1459 Formal := Defining_Identifier (First (Formals));
1461 while Present (Actual) loop
1462 if not Is_Controlling_Actual (Actual) then
1464 -- Generate:
1465 -- type Ann is access all <actual-type>
1467 Comp_Nam :=
1468 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
1470 Append_To (Decls,
1471 Make_Full_Type_Declaration (Loc,
1472 Defining_Identifier =>
1473 Comp_Nam,
1474 Type_Definition =>
1475 Make_Access_To_Object_Definition (Loc,
1476 All_Present =>
1477 True,
1478 Constant_Present =>
1479 Ekind (Formal) = E_In_Parameter,
1480 Subtype_Indication =>
1481 New_Reference_To (Etype (Actual), Loc))));
1483 -- Generate:
1484 -- Param : Ann;
1486 Append_To (Comps,
1487 Make_Component_Declaration (Loc,
1488 Defining_Identifier =>
1489 Make_Defining_Identifier (Loc, Chars (Formal)),
1490 Component_Definition =>
1491 Make_Component_Definition (Loc,
1492 Aliased_Present =>
1493 False,
1494 Subtype_Indication =>
1495 New_Reference_To (Comp_Nam, Loc))));
1497 Has_Comp := True;
1498 end if;
1500 Next_Actual (Actual);
1501 Next_Formal_With_Extras (Formal);
1502 end loop;
1504 Rec_Nam :=
1505 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
1507 if Has_Comp then
1509 -- Generate:
1510 -- type Pnn is record
1511 -- Param1 : Ann1;
1512 -- ...
1513 -- ParamN : AnnN;
1515 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1516 -- the original parameter names and Ann1 .. AnnN are the access to
1517 -- actual types.
1519 Append_To (Decls,
1520 Make_Full_Type_Declaration (Loc,
1521 Defining_Identifier =>
1522 Rec_Nam,
1523 Type_Definition =>
1524 Make_Record_Definition (Loc,
1525 Component_List =>
1526 Make_Component_List (Loc, Comps))));
1527 else
1528 -- Generate:
1529 -- type Pnn is null record;
1531 Append_To (Decls,
1532 Make_Full_Type_Declaration (Loc,
1533 Defining_Identifier =>
1534 Rec_Nam,
1535 Type_Definition =>
1536 Make_Record_Definition (Loc,
1537 Null_Present => True,
1538 Component_List => Empty)));
1539 end if;
1541 return Rec_Nam;
1542 end Build_Parameter_Block;
1544 ------------------------
1545 -- Build_Wrapper_Body --
1546 ------------------------
1548 function Build_Wrapper_Body
1549 (Loc : Source_Ptr;
1550 Proc_Nam : Entity_Id;
1551 Obj_Typ : Entity_Id;
1552 Formals : List_Id) return Node_Id
1554 Actuals : List_Id := No_List;
1555 Body_Spec : Node_Id;
1556 Conv_Id : Node_Id;
1557 First_Formal : Node_Id;
1558 Formal : Node_Id;
1560 begin
1561 Body_Spec := Build_Wrapper_Spec (Loc, Proc_Nam, Obj_Typ, Formals);
1563 -- If we did not generate the specification do have nothing else to do
1565 if Body_Spec = Empty then
1566 return Empty;
1567 end if;
1569 -- Map formals to actuals. Use the list built for the wrapper spec,
1570 -- skipping the object notation parameter.
1572 First_Formal := First (Parameter_Specifications (Body_Spec));
1574 Formal := First_Formal;
1575 Next (Formal);
1577 if Present (Formal) then
1578 Actuals := New_List;
1580 while Present (Formal) loop
1581 Append_To (Actuals,
1582 Make_Identifier (Loc, Chars =>
1583 Chars (Defining_Identifier (Formal))));
1585 Next (Formal);
1586 end loop;
1587 end if;
1589 -- An access-to-variable first parameter will require an explicit
1590 -- dereference in the unchecked conversion. This case occurs when
1591 -- a protected entry wrapper must override an interface-level
1592 -- procedure with interface access as first parameter.
1594 -- SubprgName (O.all).Proc_Nam (Formal_1 .. Formal_N)
1596 if Nkind (Parameter_Type (First_Formal)) = N_Access_Definition then
1597 Conv_Id :=
1598 Make_Explicit_Dereference (Loc,
1599 Prefix =>
1600 Make_Identifier (Loc, Chars => Name_uO));
1601 else
1602 Conv_Id :=
1603 Make_Identifier (Loc, Chars => Name_uO);
1604 end if;
1606 if Ekind (Proc_Nam) = E_Function then
1607 return
1608 Make_Subprogram_Body (Loc,
1609 Specification => Body_Spec,
1610 Declarations => Empty_List,
1611 Handled_Statement_Sequence =>
1612 Make_Handled_Sequence_Of_Statements (Loc,
1613 Statements =>
1614 New_List (
1615 Make_Simple_Return_Statement (Loc,
1616 Make_Function_Call (Loc,
1617 Name =>
1618 Make_Selected_Component (Loc,
1619 Prefix =>
1620 Unchecked_Convert_To (
1621 Corresponding_Concurrent_Type (Obj_Typ),
1622 Conv_Id),
1623 Selector_Name =>
1624 New_Reference_To (Proc_Nam, Loc)),
1625 Parameter_Associations => Actuals)))));
1626 else
1627 return
1628 Make_Subprogram_Body (Loc,
1629 Specification => Body_Spec,
1630 Declarations => Empty_List,
1631 Handled_Statement_Sequence =>
1632 Make_Handled_Sequence_Of_Statements (Loc,
1633 Statements =>
1634 New_List (
1635 Make_Procedure_Call_Statement (Loc,
1636 Name =>
1637 Make_Selected_Component (Loc,
1638 Prefix =>
1639 Unchecked_Convert_To (
1640 Corresponding_Concurrent_Type (Obj_Typ),
1641 Conv_Id),
1642 Selector_Name =>
1643 New_Reference_To (Proc_Nam, Loc)),
1644 Parameter_Associations => Actuals))));
1645 end if;
1646 end Build_Wrapper_Body;
1648 ------------------------
1649 -- Build_Wrapper_Spec --
1650 ------------------------
1652 function Build_Wrapper_Spec
1653 (Loc : Source_Ptr;
1654 Proc_Nam : Entity_Id;
1655 Obj_Typ : Entity_Id;
1656 Formals : List_Id) return Node_Id
1658 New_Name_Id : constant Entity_Id :=
1659 Make_Defining_Identifier (Loc, Chars (Proc_Nam));
1661 First_Param : Node_Id := Empty;
1662 Iface : Entity_Id;
1663 Iface_Elmt : Elmt_Id := No_Elmt;
1664 New_Formals : List_Id;
1665 Obj_Param : Node_Id;
1666 Obj_Param_Typ : Node_Id;
1667 Iface_Prim_Op : Entity_Id;
1668 Iface_Prim_Op_Elmt : Elmt_Id;
1670 function Overriding_Possible
1671 (Iface_Prim_Op : Entity_Id;
1672 Proc_Nam : Entity_Id) return Boolean;
1673 -- Determine whether a primitive operation can be overridden by the
1674 -- wrapper. Iface_Prim_Op is the candidate primitive operation of an
1675 -- abstract interface type, Proc_Nam is the generated entry wrapper.
1677 function Replicate_Entry_Formals
1678 (Loc : Source_Ptr;
1679 Formals : List_Id) return List_Id;
1680 -- An explicit parameter replication is required due to the
1681 -- Is_Entry_Formal flag being set for all the formals. The explicit
1682 -- replication removes the flag that would otherwise cause a different
1683 -- path of analysis.
1685 -------------------------
1686 -- Overriding_Possible --
1687 -------------------------
1689 function Overriding_Possible
1690 (Iface_Prim_Op : Entity_Id;
1691 Proc_Nam : Entity_Id) return Boolean
1693 Prim_Op_Spec : constant Node_Id := Parent (Iface_Prim_Op);
1694 Proc_Spec : constant Node_Id := Parent (Proc_Nam);
1696 Is_Access_To_Variable : Boolean;
1697 Is_Out_Present : Boolean;
1699 function Type_Conformant_Parameters
1700 (Prim_Op_Param_Specs : List_Id;
1701 Proc_Param_Specs : List_Id) return Boolean;
1702 -- Determine whether the parameters of the generated entry wrapper
1703 -- and those of a primitive operation are type conformant. During
1704 -- this check, the first parameter of the primitive operation is
1705 -- always skipped.
1707 --------------------------------
1708 -- Type_Conformant_Parameters --
1709 --------------------------------
1711 function Type_Conformant_Parameters
1712 (Prim_Op_Param_Specs : List_Id;
1713 Proc_Param_Specs : List_Id) return Boolean
1715 Prim_Op_Param : Node_Id;
1716 Prim_Op_Typ : Entity_Id;
1717 Proc_Param : Node_Id;
1718 Proc_Typ : Entity_Id;
1720 begin
1721 -- Skip the first parameter of the primitive operation
1723 Prim_Op_Param := Next (First (Prim_Op_Param_Specs));
1724 Proc_Param := First (Proc_Param_Specs);
1725 while Present (Prim_Op_Param)
1726 and then Present (Proc_Param)
1727 loop
1728 Prim_Op_Typ := Find_Parameter_Type (Prim_Op_Param);
1729 Proc_Typ := Find_Parameter_Type (Proc_Param);
1731 -- The two parameters must be mode conformant
1733 if not Conforming_Types
1734 (Prim_Op_Typ, Proc_Typ, Mode_Conformant)
1735 then
1736 return False;
1737 end if;
1739 Next (Prim_Op_Param);
1740 Next (Proc_Param);
1741 end loop;
1743 -- One of the lists is longer than the other
1745 if Present (Prim_Op_Param) or else Present (Proc_Param) then
1746 return False;
1747 end if;
1749 return True;
1750 end Type_Conformant_Parameters;
1752 -- Start of processing for Overriding_Possible
1754 begin
1755 if Chars (Iface_Prim_Op) /= Chars (Proc_Nam) then
1756 return False;
1757 end if;
1759 -- Special check for protected procedures: If an inherited subprogram
1760 -- is implemented by a protected procedure or an entry, then the
1761 -- first parameter of the inherited subprogram shall be of mode OUT
1762 -- or IN OUT, or an access-to-variable parameter.
1764 if Ekind (Iface_Prim_Op) = E_Procedure then
1766 Is_Out_Present :=
1767 Present (Parameter_Specifications (Prim_Op_Spec))
1768 and then
1769 Out_Present (First (Parameter_Specifications (Prim_Op_Spec)));
1771 Is_Access_To_Variable :=
1772 Present (Parameter_Specifications (Prim_Op_Spec))
1773 and then
1774 Nkind (Parameter_Type
1775 (First
1776 (Parameter_Specifications (Prim_Op_Spec)))) =
1777 N_Access_Definition;
1779 if not Is_Out_Present
1780 and then not Is_Access_To_Variable
1781 then
1782 return False;
1783 end if;
1784 end if;
1786 return Type_Conformant_Parameters (
1787 Parameter_Specifications (Prim_Op_Spec),
1788 Parameter_Specifications (Proc_Spec));
1789 end Overriding_Possible;
1791 -----------------------------
1792 -- Replicate_Entry_Formals --
1793 -----------------------------
1795 function Replicate_Entry_Formals
1796 (Loc : Source_Ptr;
1797 Formals : List_Id) return List_Id
1799 New_Formals : constant List_Id := New_List;
1800 Formal : Node_Id;
1801 Param_Type : Node_Id;
1803 begin
1804 Formal := First (Formals);
1805 while Present (Formal) loop
1807 -- Create an explicit copy of the entry parameter
1809 -- When creating the wrapper subprogram for a primitive operation
1810 -- of a protected interface we must construct an equivalent
1811 -- signature to that of the overriding operation. For regular
1812 -- parameters we can just use the type of the formal, but for
1813 -- access to subprogram parameters we need to reanalyze the
1814 -- parameter type to create local entities for the signature of
1815 -- the subprogram type. Using the entities of the overriding
1816 -- subprogram will result in out-of-scope errors in the back-end.
1818 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
1819 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
1820 else
1821 Param_Type :=
1822 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
1823 end if;
1825 Append_To (New_Formals,
1826 Make_Parameter_Specification (Loc,
1827 Defining_Identifier =>
1828 Make_Defining_Identifier (Loc,
1829 Chars => Chars (Defining_Identifier (Formal))),
1830 In_Present => In_Present (Formal),
1831 Out_Present => Out_Present (Formal),
1832 Parameter_Type => Param_Type));
1834 Next (Formal);
1835 end loop;
1837 return New_Formals;
1838 end Replicate_Entry_Formals;
1840 -- Start of processing for Build_Wrapper_Spec
1842 begin
1843 -- The mode is determined by the first parameter of the interface-level
1844 -- procedure that the current entry is trying to override.
1846 pragma Assert (Is_Non_Empty_List (Abstract_Interface_List (Obj_Typ)));
1848 -- We must examine all the protected operations of the implemented
1849 -- interfaces in order to discover a possible overriding candidate.
1851 Iface := Etype (First (Abstract_Interface_List (Obj_Typ)));
1853 Examine_Parents : loop
1854 if Present (Primitive_Operations (Iface)) then
1855 Iface_Prim_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
1856 while Present (Iface_Prim_Op_Elmt) loop
1857 Iface_Prim_Op := Node (Iface_Prim_Op_Elmt);
1859 if not Is_Predefined_Dispatching_Operation (Iface_Prim_Op) then
1860 while Present (Alias (Iface_Prim_Op)) loop
1861 Iface_Prim_Op := Alias (Iface_Prim_Op);
1862 end loop;
1864 -- The current primitive operation can be overridden by the
1865 -- generated entry wrapper.
1867 if Overriding_Possible (Iface_Prim_Op, Proc_Nam) then
1868 First_Param := First (Parameter_Specifications
1869 (Parent (Iface_Prim_Op)));
1871 goto Found;
1872 end if;
1873 end if;
1875 Next_Elmt (Iface_Prim_Op_Elmt);
1876 end loop;
1877 end if;
1879 exit Examine_Parents when Etype (Iface) = Iface;
1881 Iface := Etype (Iface);
1882 end loop Examine_Parents;
1884 if Present (Interfaces
1885 (Corresponding_Record_Type (Scope (Proc_Nam))))
1886 then
1887 Iface_Elmt := First_Elmt
1888 (Interfaces
1889 (Corresponding_Record_Type (Scope (Proc_Nam))));
1890 Examine_Interfaces : while Present (Iface_Elmt) loop
1891 Iface := Node (Iface_Elmt);
1893 if Present (Primitive_Operations (Iface)) then
1894 Iface_Prim_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
1895 while Present (Iface_Prim_Op_Elmt) loop
1896 Iface_Prim_Op := Node (Iface_Prim_Op_Elmt);
1898 if not Is_Predefined_Dispatching_Operation
1899 (Iface_Prim_Op)
1900 then
1901 while Present (Alias (Iface_Prim_Op)) loop
1902 Iface_Prim_Op := Alias (Iface_Prim_Op);
1903 end loop;
1905 -- The current primitive operation can be overridden by
1906 -- the generated entry wrapper.
1908 if Overriding_Possible (Iface_Prim_Op, Proc_Nam) then
1909 First_Param := First (Parameter_Specifications
1910 (Parent (Iface_Prim_Op)));
1912 goto Found;
1913 end if;
1914 end if;
1916 Next_Elmt (Iface_Prim_Op_Elmt);
1917 end loop;
1918 end if;
1920 Next_Elmt (Iface_Elmt);
1921 end loop Examine_Interfaces;
1922 end if;
1924 -- Return if no interface primitive can be overridden
1926 return Empty;
1928 <<Found>>
1930 New_Formals := Replicate_Entry_Formals (Loc, Formals);
1932 -- ??? Certain source packages contain protected or task types that do
1933 -- not implement any interfaces and are compiled with the -gnat05
1934 -- switch. In this case, a default first parameter is created.
1936 -- If the interface operation has an access parameter, create a copy
1937 -- of it, with the same null exclusion indicator if present.
1939 if Present (First_Param) then
1940 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
1941 Obj_Param_Typ :=
1942 Make_Access_Definition (Loc,
1943 Subtype_Mark =>
1944 New_Reference_To (Obj_Typ, Loc));
1945 Set_Null_Exclusion_Present (Obj_Param_Typ,
1946 Null_Exclusion_Present (Parameter_Type (First_Param)));
1948 else
1949 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
1950 end if;
1952 Obj_Param :=
1953 Make_Parameter_Specification (Loc,
1954 Defining_Identifier =>
1955 Make_Defining_Identifier (Loc, Name_uO),
1956 In_Present => In_Present (First_Param),
1957 Out_Present => Out_Present (First_Param),
1958 Parameter_Type => Obj_Param_Typ);
1960 else
1961 Obj_Param :=
1962 Make_Parameter_Specification (Loc,
1963 Defining_Identifier =>
1964 Make_Defining_Identifier (Loc, Name_uO),
1965 In_Present => True,
1966 Out_Present => True,
1967 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
1968 end if;
1970 Prepend_To (New_Formals, Obj_Param);
1972 -- Minimum decoration needed to catch the entity in
1973 -- Sem_Ch6.Override_Dispatching_Operation
1975 if Ekind (Proc_Nam) = E_Procedure
1976 or else Ekind (Proc_Nam) = E_Entry
1977 then
1978 Set_Ekind (New_Name_Id, E_Procedure);
1979 Set_Is_Primitive_Wrapper (New_Name_Id);
1980 Set_Wrapped_Entity (New_Name_Id, Proc_Nam);
1982 return
1983 Make_Procedure_Specification (Loc,
1984 Defining_Unit_Name => New_Name_Id,
1985 Parameter_Specifications => New_Formals);
1987 else pragma Assert (Ekind (Proc_Nam) = E_Function);
1988 Set_Ekind (New_Name_Id, E_Function);
1990 return
1991 Make_Function_Specification (Loc,
1992 Defining_Unit_Name => New_Name_Id,
1993 Parameter_Specifications => New_Formals,
1994 Result_Definition =>
1995 New_Copy (Result_Definition (Parent (Proc_Nam))));
1996 end if;
1997 end Build_Wrapper_Spec;
1999 ---------------------------
2000 -- Build_Find_Body_Index --
2001 ---------------------------
2003 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2004 Loc : constant Source_Ptr := Sloc (Typ);
2005 Ent : Entity_Id;
2006 E_Typ : Entity_Id;
2007 Has_F : Boolean := False;
2008 Index : Nat;
2009 If_St : Node_Id := Empty;
2010 Lo : Node_Id;
2011 Hi : Node_Id;
2012 Decls : List_Id := New_List;
2013 Ret : Node_Id;
2014 Spec : Node_Id;
2015 Siz : Node_Id := Empty;
2017 procedure Add_If_Clause (Expr : Node_Id);
2018 -- Add test for range of current entry
2020 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2021 -- If a bound of an entry is given by a discriminant, retrieve the
2022 -- actual value of the discriminant from the enclosing object.
2024 -------------------
2025 -- Add_If_Clause --
2026 -------------------
2028 procedure Add_If_Clause (Expr : Node_Id) is
2029 Cond : Node_Id;
2030 Stats : constant List_Id :=
2031 New_List (
2032 Make_Simple_Return_Statement (Loc,
2033 Expression => Make_Integer_Literal (Loc, Index + 1)));
2035 begin
2036 -- Index for current entry body
2038 Index := Index + 1;
2040 -- Compute total length of entry queues so far
2042 if No (Siz) then
2043 Siz := Expr;
2044 else
2045 Siz :=
2046 Make_Op_Add (Loc,
2047 Left_Opnd => Siz,
2048 Right_Opnd => Expr);
2049 end if;
2051 Cond :=
2052 Make_Op_Le (Loc,
2053 Left_Opnd => Make_Identifier (Loc, Name_uE),
2054 Right_Opnd => Siz);
2056 -- Map entry queue indices in the range of the current family
2057 -- into the current index, that designates the entry body.
2059 if No (If_St) then
2060 If_St :=
2061 Make_Implicit_If_Statement (Typ,
2062 Condition => Cond,
2063 Then_Statements => Stats,
2064 Elsif_Parts => New_List);
2066 Ret := If_St;
2068 else
2069 Append (
2070 Make_Elsif_Part (Loc,
2071 Condition => Cond,
2072 Then_Statements => Stats),
2073 Elsif_Parts (If_St));
2074 end if;
2075 end Add_If_Clause;
2077 ------------------------------
2078 -- Convert_Discriminant_Ref --
2079 ------------------------------
2081 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2082 B : Node_Id;
2084 begin
2085 if Is_Entity_Name (Bound)
2086 and then Ekind (Entity (Bound)) = E_Discriminant
2087 then
2088 B :=
2089 Make_Selected_Component (Loc,
2090 Prefix =>
2091 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2092 Make_Explicit_Dereference (Loc,
2093 Make_Identifier (Loc, Name_uObject))),
2094 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2095 Set_Etype (B, Etype (Entity (Bound)));
2096 else
2097 B := New_Copy_Tree (Bound);
2098 end if;
2100 return B;
2101 end Convert_Discriminant_Ref;
2103 -- Start of processing for Build_Find_Body_Index
2105 begin
2106 Spec := Build_Find_Body_Index_Spec (Typ);
2108 Ent := First_Entity (Typ);
2109 while Present (Ent) loop
2110 if Ekind (Ent) = E_Entry_Family then
2111 Has_F := True;
2112 exit;
2113 end if;
2115 Next_Entity (Ent);
2116 end loop;
2118 if not Has_F then
2120 -- If the protected type has no entry families, there is a one-one
2121 -- correspondence between entry queue and entry body.
2123 Ret :=
2124 Make_Simple_Return_Statement (Loc,
2125 Expression => Make_Identifier (Loc, Name_uE));
2127 else
2128 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2129 -- the following:
2131 -- if E <= l1 then return 1;
2132 -- elsif E <= l1 + l2 then return 2;
2133 -- ...
2135 Index := 0;
2136 Siz := Empty;
2137 Ent := First_Entity (Typ);
2139 Add_Object_Pointer (Loc, Typ, Decls);
2141 while Present (Ent) loop
2143 if Ekind (Ent) = E_Entry then
2144 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2146 elsif Ekind (Ent) = E_Entry_Family then
2148 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2149 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2150 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2151 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2152 end if;
2154 Next_Entity (Ent);
2155 end loop;
2157 if Index = 1 then
2158 Decls := New_List;
2159 Ret :=
2160 Make_Simple_Return_Statement (Loc,
2161 Expression => Make_Integer_Literal (Loc, 1));
2163 elsif Nkind (Ret) = N_If_Statement then
2165 -- Ranges are in increasing order, so last one doesn't need guard
2167 declare
2168 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2169 begin
2170 Remove (Nod);
2171 Set_Else_Statements (Ret, Then_Statements (Nod));
2172 end;
2173 end if;
2174 end if;
2176 return
2177 Make_Subprogram_Body (Loc,
2178 Specification => Spec,
2179 Declarations => Decls,
2180 Handled_Statement_Sequence =>
2181 Make_Handled_Sequence_Of_Statements (Loc,
2182 Statements => New_List (Ret)));
2183 end Build_Find_Body_Index;
2185 --------------------------------
2186 -- Build_Find_Body_Index_Spec --
2187 --------------------------------
2189 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2190 Loc : constant Source_Ptr := Sloc (Typ);
2191 Id : constant Entity_Id :=
2192 Make_Defining_Identifier (Loc,
2193 Chars => New_External_Name (Chars (Typ), 'F'));
2194 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2195 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2197 begin
2198 return
2199 Make_Function_Specification (Loc,
2200 Defining_Unit_Name => Id,
2201 Parameter_Specifications => New_List (
2202 Make_Parameter_Specification (Loc,
2203 Defining_Identifier => Parm1,
2204 Parameter_Type =>
2205 New_Reference_To (RTE (RE_Address), Loc)),
2207 Make_Parameter_Specification (Loc,
2208 Defining_Identifier => Parm2,
2209 Parameter_Type =>
2210 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
2211 Result_Definition => New_Occurrence_Of (
2212 RTE (RE_Protected_Entry_Index), Loc));
2213 end Build_Find_Body_Index_Spec;
2215 -------------------------
2216 -- Build_Master_Entity --
2217 -------------------------
2219 procedure Build_Master_Entity (E : Entity_Id) is
2220 Loc : constant Source_Ptr := Sloc (E);
2221 P : Node_Id;
2222 Decl : Node_Id;
2223 S : Entity_Id;
2225 begin
2226 S := Scope (E);
2228 -- Ada 2005 (AI-287): Do not set/get the has_master_entity reminder
2229 -- in internal scopes, unless present already.. Required for nested
2230 -- limited aggregates, where the expansion of task components may
2231 -- generate inner blocks. If the block is the rewriting of a call
2232 -- this is valid master.
2234 if Ada_Version >= Ada_05 then
2235 while Is_Internal (S) loop
2236 if Nkind (Parent (S)) = N_Block_Statement
2237 and then
2238 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
2239 then
2240 exit;
2241 else
2242 S := Scope (S);
2243 end if;
2244 end loop;
2245 end if;
2247 -- Nothing to do if we already built a master entity for this scope
2248 -- or if there is no task hierarchy.
2250 if Has_Master_Entity (S)
2251 or else Restriction_Active (No_Task_Hierarchy)
2252 then
2253 return;
2254 end if;
2256 -- Otherwise first build the master entity
2257 -- _Master : constant Master_Id := Current_Master.all;
2258 -- and insert it just before the current declaration
2260 Decl :=
2261 Make_Object_Declaration (Loc,
2262 Defining_Identifier =>
2263 Make_Defining_Identifier (Loc, Name_uMaster),
2264 Constant_Present => True,
2265 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
2266 Expression =>
2267 Make_Explicit_Dereference (Loc,
2268 New_Reference_To (RTE (RE_Current_Master), Loc)));
2270 P := Parent (E);
2271 Insert_Before (P, Decl);
2272 Analyze (Decl);
2274 -- Ada 2005 (AI-287): Set the has_master_entity reminder in the
2275 -- non-internal scope selected above.
2277 if Ada_Version >= Ada_05 then
2278 Set_Has_Master_Entity (S);
2279 else
2280 Set_Has_Master_Entity (Scope (E));
2281 end if;
2283 -- Now mark the containing scope as a task master
2285 while Nkind (P) /= N_Compilation_Unit loop
2286 P := Parent (P);
2288 -- If we fall off the top, we are at the outer level, and the
2289 -- environment task is our effective master, so nothing to mark.
2291 if Nkind_In
2292 (P, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2293 then
2294 Set_Is_Task_Master (P, True);
2295 return;
2297 elsif Nkind (Parent (P)) = N_Subunit then
2298 P := Corresponding_Stub (Parent (P));
2299 end if;
2300 end loop;
2301 end Build_Master_Entity;
2303 ---------------------------
2304 -- Build_Protected_Entry --
2305 ---------------------------
2307 function Build_Protected_Entry
2308 (N : Node_Id;
2309 Ent : Entity_Id;
2310 Pid : Node_Id) return Node_Id
2312 Loc : constant Source_Ptr := Sloc (N);
2314 Decls : constant List_Id := Declarations (N);
2315 End_Lab : constant Node_Id :=
2316 End_Label (Handled_Statement_Sequence (N));
2317 End_Loc : constant Source_Ptr :=
2318 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
2319 -- Used for the generated call to Complete_Entry_Body
2321 Han_Loc : Source_Ptr;
2322 -- Used for the exception handler, inserted at end of the body
2324 Op_Decls : constant List_Id := New_List;
2325 Complete : Node_Id;
2326 Edef : Entity_Id;
2327 Espec : Node_Id;
2328 Ohandle : Node_Id;
2329 Op_Stats : List_Id;
2331 begin
2332 -- Set the source location on the exception handler only when debugging
2333 -- the expanded code (see Make_Implicit_Exception_Handler).
2335 if Debug_Generated_Code then
2336 Han_Loc := End_Loc;
2338 -- Otherwise the inserted code should not be visible to the debugger
2340 else
2341 Han_Loc := No_Location;
2342 end if;
2344 Edef :=
2345 Make_Defining_Identifier (Loc,
2346 Chars => Chars (Protected_Body_Subprogram (Ent)));
2347 Espec :=
2348 Build_Protected_Entry_Specification (Loc, Edef, Empty);
2350 -- Add the following declarations:
2351 -- type poVP is access poV;
2352 -- _object : poVP := poVP (_O);
2354 -- where _O is the formal parameter associated with the concurrent
2355 -- object. These declarations are needed for Complete_Entry_Body.
2357 Add_Object_Pointer (Loc, Pid, Op_Decls);
2359 -- Add renamings for all formals, the Protection object, discriminals,
2360 -- privals and the entry index constant for use by debugger.
2362 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
2363 Debug_Private_Data_Declarations (Decls);
2365 case Corresponding_Runtime_Package (Pid) is
2366 when System_Tasking_Protected_Objects_Entries =>
2367 Complete :=
2368 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
2370 when System_Tasking_Protected_Objects_Single_Entry =>
2371 Complete :=
2372 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
2374 when others =>
2375 raise Program_Error;
2376 end case;
2378 Op_Stats := New_List (
2379 Make_Block_Statement (Loc,
2380 Declarations => Decls,
2381 Handled_Statement_Sequence =>
2382 Handled_Statement_Sequence (N)),
2384 Make_Procedure_Call_Statement (End_Loc,
2385 Name => Complete,
2386 Parameter_Associations => New_List (
2387 Make_Attribute_Reference (End_Loc,
2388 Prefix =>
2389 Make_Selected_Component (End_Loc,
2390 Prefix =>
2391 Make_Identifier (End_Loc, Name_uObject),
2392 Selector_Name =>
2393 Make_Identifier (End_Loc, Name_uObject)),
2394 Attribute_Name => Name_Unchecked_Access))));
2396 -- When exceptions can not be propagated, we never need to call
2397 -- Exception_Complete_Entry_Body
2399 if No_Exception_Handlers_Set then
2400 return
2401 Make_Subprogram_Body (Loc,
2402 Specification => Espec,
2403 Declarations => Op_Decls,
2404 Handled_Statement_Sequence =>
2405 Make_Handled_Sequence_Of_Statements (Loc,
2406 Statements => Op_Stats,
2407 End_Label => End_Lab));
2409 else
2410 Ohandle := Make_Others_Choice (Loc);
2411 Set_All_Others (Ohandle);
2413 case Corresponding_Runtime_Package (Pid) is
2414 when System_Tasking_Protected_Objects_Entries =>
2415 Complete :=
2416 New_Reference_To
2417 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
2419 when System_Tasking_Protected_Objects_Single_Entry =>
2420 Complete :=
2421 New_Reference_To
2422 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
2424 when others =>
2425 raise Program_Error;
2426 end case;
2428 -- Create body of entry procedure. The renaming declarations are
2429 -- placed ahead of the block that contains the actual entry body.
2431 return
2432 Make_Subprogram_Body (Loc,
2433 Specification => Espec,
2434 Declarations => Op_Decls,
2435 Handled_Statement_Sequence =>
2436 Make_Handled_Sequence_Of_Statements (Loc,
2437 Statements => Op_Stats,
2438 End_Label => End_Lab,
2439 Exception_Handlers => New_List (
2440 Make_Implicit_Exception_Handler (Han_Loc,
2441 Exception_Choices => New_List (Ohandle),
2443 Statements => New_List (
2444 Make_Procedure_Call_Statement (Han_Loc,
2445 Name => Complete,
2446 Parameter_Associations => New_List (
2447 Make_Attribute_Reference (Han_Loc,
2448 Prefix =>
2449 Make_Selected_Component (Han_Loc,
2450 Prefix =>
2451 Make_Identifier (Han_Loc, Name_uObject),
2452 Selector_Name =>
2453 Make_Identifier (Han_Loc, Name_uObject)),
2454 Attribute_Name => Name_Unchecked_Access),
2456 Make_Function_Call (Han_Loc,
2457 Name => New_Reference_To (
2458 RTE (RE_Get_GNAT_Exception), Loc)))))))));
2459 end if;
2460 end Build_Protected_Entry;
2462 -----------------------------------------
2463 -- Build_Protected_Entry_Specification --
2464 -----------------------------------------
2466 function Build_Protected_Entry_Specification
2467 (Loc : Source_Ptr;
2468 Def_Id : Entity_Id;
2469 Ent_Id : Entity_Id) return Node_Id
2471 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
2473 begin
2474 Set_Debug_Info_Needed (Def_Id);
2476 if Present (Ent_Id) then
2477 Append_Elmt (P, Accept_Address (Ent_Id));
2478 end if;
2480 return
2481 Make_Procedure_Specification (Loc,
2482 Defining_Unit_Name => Def_Id,
2483 Parameter_Specifications => New_List (
2484 Make_Parameter_Specification (Loc,
2485 Defining_Identifier =>
2486 Make_Defining_Identifier (Loc, Name_uO),
2487 Parameter_Type =>
2488 New_Reference_To (RTE (RE_Address), Loc)),
2490 Make_Parameter_Specification (Loc,
2491 Defining_Identifier => P,
2492 Parameter_Type =>
2493 New_Reference_To (RTE (RE_Address), Loc)),
2495 Make_Parameter_Specification (Loc,
2496 Defining_Identifier =>
2497 Make_Defining_Identifier (Loc, Name_uE),
2498 Parameter_Type =>
2499 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
2500 end Build_Protected_Entry_Specification;
2502 --------------------------
2503 -- Build_Protected_Spec --
2504 --------------------------
2506 function Build_Protected_Spec
2507 (N : Node_Id;
2508 Obj_Type : Entity_Id;
2509 Ident : Entity_Id;
2510 Unprotected : Boolean := False) return List_Id
2512 Loc : constant Source_Ptr := Sloc (N);
2513 Decl : Node_Id;
2514 Formal : Entity_Id;
2515 New_Plist : List_Id;
2516 New_Param : Node_Id;
2518 begin
2519 New_Plist := New_List;
2521 Formal := First_Formal (Ident);
2522 while Present (Formal) loop
2523 New_Param :=
2524 Make_Parameter_Specification (Loc,
2525 Defining_Identifier =>
2526 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
2527 In_Present => In_Present (Parent (Formal)),
2528 Out_Present => Out_Present (Parent (Formal)),
2529 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
2531 if Unprotected then
2532 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
2533 end if;
2535 Append (New_Param, New_Plist);
2536 Next_Formal (Formal);
2537 end loop;
2539 -- If the subprogram is a procedure and the context is not an access
2540 -- to protected subprogram, the parameter is in-out. Otherwise it is
2541 -- an in parameter.
2543 Decl :=
2544 Make_Parameter_Specification (Loc,
2545 Defining_Identifier =>
2546 Make_Defining_Identifier (Loc, Name_uObject),
2547 In_Present => True,
2548 Out_Present =>
2549 (Etype (Ident) = Standard_Void_Type
2550 and then not Is_RTE (Obj_Type, RE_Address)),
2551 Parameter_Type =>
2552 New_Reference_To (Obj_Type, Loc));
2553 Set_Debug_Info_Needed (Defining_Identifier (Decl));
2554 Prepend_To (New_Plist, Decl);
2556 return New_Plist;
2557 end Build_Protected_Spec;
2559 ---------------------------------------
2560 -- Build_Protected_Sub_Specification --
2561 ---------------------------------------
2563 function Build_Protected_Sub_Specification
2564 (N : Node_Id;
2565 Prot_Typ : Entity_Id;
2566 Mode : Subprogram_Protection_Mode) return Node_Id
2568 Loc : constant Source_Ptr := Sloc (N);
2569 Decl : Node_Id;
2570 Def_Id : Entity_Id;
2571 New_Id : Entity_Id;
2572 New_Plist : List_Id;
2573 New_Spec : Node_Id;
2575 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
2576 (Dispatching_Mode => ' ',
2577 Protected_Mode => 'P',
2578 Unprotected_Mode => 'N');
2580 begin
2581 if Ekind (Defining_Unit_Name (Specification (N))) =
2582 E_Subprogram_Body
2583 then
2584 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
2585 else
2586 Decl := N;
2587 end if;
2589 Def_Id := Defining_Unit_Name (Specification (Decl));
2591 New_Plist :=
2592 Build_Protected_Spec
2593 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
2594 Mode = Unprotected_Mode);
2595 New_Id :=
2596 Make_Defining_Identifier (Loc,
2597 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
2599 -- The unprotected operation carries the user code, and debugging
2600 -- information must be generated for it, even though this spec does
2601 -- not come from source. It is also convenient to allow gdb to step
2602 -- into the protected operation, even though it only contains lock/
2603 -- unlock calls.
2605 Set_Debug_Info_Needed (New_Id);
2607 if Nkind (Specification (Decl)) = N_Procedure_Specification then
2608 New_Spec :=
2609 Make_Procedure_Specification (Loc,
2610 Defining_Unit_Name => New_Id,
2611 Parameter_Specifications => New_Plist);
2613 -- Create a new specification for the anonymous subprogram type
2615 else
2616 New_Spec :=
2617 Make_Function_Specification (Loc,
2618 Defining_Unit_Name => New_Id,
2619 Parameter_Specifications => New_Plist,
2620 Result_Definition =>
2621 Copy_Result_Type (Result_Definition (Specification (Decl))));
2623 Set_Return_Present (Defining_Unit_Name (New_Spec));
2624 end if;
2626 return New_Spec;
2627 end Build_Protected_Sub_Specification;
2629 -------------------------------------
2630 -- Build_Protected_Subprogram_Body --
2631 -------------------------------------
2633 function Build_Protected_Subprogram_Body
2634 (N : Node_Id;
2635 Pid : Node_Id;
2636 N_Op_Spec : Node_Id) return Node_Id
2638 Loc : constant Source_Ptr := Sloc (N);
2639 Op_Spec : Node_Id;
2640 P_Op_Spec : Node_Id;
2641 Uactuals : List_Id;
2642 Pformal : Node_Id;
2643 Unprot_Call : Node_Id;
2644 Sub_Body : Node_Id;
2645 Lock_Name : Node_Id;
2646 Lock_Stmt : Node_Id;
2647 Service_Name : Node_Id;
2648 R : Node_Id;
2649 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
2650 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
2651 Stmts : List_Id;
2652 Object_Parm : Node_Id;
2653 Exc_Safe : Boolean;
2655 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
2656 -- Tell whether a given subprogram cannot raise an exception
2658 -----------------------
2659 -- Is_Exception_Safe --
2660 -----------------------
2662 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
2664 function Has_Side_Effect (N : Node_Id) return Boolean;
2665 -- Return True whenever encountering a subprogram call or raise
2666 -- statement of any kind in the sequence of statements
2668 ---------------------
2669 -- Has_Side_Effect --
2670 ---------------------
2672 -- What is this doing buried two levels down in exp_ch9. It seems
2673 -- like a generally useful function, and indeed there may be code
2674 -- duplication going on here ???
2676 function Has_Side_Effect (N : Node_Id) return Boolean is
2677 Stmt : Node_Id;
2678 Expr : Node_Id;
2680 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
2681 -- Indicate whether N is a subprogram call or a raise statement
2683 ----------------------
2684 -- Is_Call_Or_Raise --
2685 ----------------------
2687 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
2688 begin
2689 return Nkind_In (N, N_Procedure_Call_Statement,
2690 N_Function_Call,
2691 N_Raise_Statement,
2692 N_Raise_Constraint_Error,
2693 N_Raise_Program_Error,
2694 N_Raise_Storage_Error);
2695 end Is_Call_Or_Raise;
2697 -- Start of processing for Has_Side_Effect
2699 begin
2700 Stmt := N;
2701 while Present (Stmt) loop
2702 if Is_Call_Or_Raise (Stmt) then
2703 return True;
2704 end if;
2706 -- An object declaration can also contain a function call
2707 -- or a raise statement
2709 if Nkind (Stmt) = N_Object_Declaration then
2710 Expr := Expression (Stmt);
2712 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
2713 return True;
2714 end if;
2715 end if;
2717 Next (Stmt);
2718 end loop;
2720 return False;
2721 end Has_Side_Effect;
2723 -- Start of processing for Is_Exception_Safe
2725 begin
2726 -- If the checks handled by the back end are not disabled, we cannot
2727 -- ensure that no exception will be raised.
2729 if not Access_Checks_Suppressed (Empty)
2730 or else not Discriminant_Checks_Suppressed (Empty)
2731 or else not Range_Checks_Suppressed (Empty)
2732 or else not Index_Checks_Suppressed (Empty)
2733 or else Opt.Stack_Checking_Enabled
2734 then
2735 return False;
2736 end if;
2738 if Has_Side_Effect (First (Declarations (Subprogram)))
2739 or else
2740 Has_Side_Effect (
2741 First (Statements (Handled_Statement_Sequence (Subprogram))))
2742 then
2743 return False;
2744 else
2745 return True;
2746 end if;
2747 end Is_Exception_Safe;
2749 -- Start of processing for Build_Protected_Subprogram_Body
2751 begin
2752 Op_Spec := Specification (N);
2753 Exc_Safe := Is_Exception_Safe (N);
2755 P_Op_Spec :=
2756 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
2758 -- Build a list of the formal parameters of the protected version of
2759 -- the subprogram to use as the actual parameters of the unprotected
2760 -- version.
2762 Uactuals := New_List;
2763 Pformal := First (Parameter_Specifications (P_Op_Spec));
2764 while Present (Pformal) loop
2765 Append (
2766 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))),
2767 Uactuals);
2768 Next (Pformal);
2769 end loop;
2771 -- Make a call to the unprotected version of the subprogram built above
2772 -- for use by the protected version built below.
2774 if Nkind (Op_Spec) = N_Function_Specification then
2775 if Exc_Safe then
2776 R := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2777 Unprot_Call :=
2778 Make_Object_Declaration (Loc,
2779 Defining_Identifier => R,
2780 Constant_Present => True,
2781 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
2782 Expression =>
2783 Make_Function_Call (Loc,
2784 Name => Make_Identifier (Loc,
2785 Chars (Defining_Unit_Name (N_Op_Spec))),
2786 Parameter_Associations => Uactuals));
2787 Return_Stmt := Make_Simple_Return_Statement (Loc,
2788 Expression => New_Reference_To (R, Loc));
2790 else
2791 Unprot_Call := Make_Simple_Return_Statement (Loc,
2792 Expression => Make_Function_Call (Loc,
2793 Name =>
2794 Make_Identifier (Loc,
2795 Chars (Defining_Unit_Name (N_Op_Spec))),
2796 Parameter_Associations => Uactuals));
2797 end if;
2799 else
2800 Unprot_Call :=
2801 Make_Procedure_Call_Statement (Loc,
2802 Name =>
2803 Make_Identifier (Loc,
2804 Chars (Defining_Unit_Name (N_Op_Spec))),
2805 Parameter_Associations => Uactuals);
2806 end if;
2808 -- Wrap call in block that will be covered by an at_end handler
2810 if not Exc_Safe then
2811 Unprot_Call := Make_Block_Statement (Loc,
2812 Handled_Statement_Sequence =>
2813 Make_Handled_Sequence_Of_Statements (Loc,
2814 Statements => New_List (Unprot_Call)));
2815 end if;
2817 -- Make the protected subprogram body. This locks the protected
2818 -- object and calls the unprotected version of the subprogram.
2820 case Corresponding_Runtime_Package (Pid) is
2821 when System_Tasking_Protected_Objects_Entries =>
2822 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
2823 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
2825 when System_Tasking_Protected_Objects_Single_Entry =>
2826 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
2827 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
2829 when System_Tasking_Protected_Objects =>
2830 Lock_Name := New_Reference_To (RTE (RE_Lock), Loc);
2831 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
2833 when others =>
2834 raise Program_Error;
2835 end case;
2837 Object_Parm :=
2838 Make_Attribute_Reference (Loc,
2839 Prefix =>
2840 Make_Selected_Component (Loc,
2841 Prefix =>
2842 Make_Identifier (Loc, Name_uObject),
2843 Selector_Name =>
2844 Make_Identifier (Loc, Name_uObject)),
2845 Attribute_Name => Name_Unchecked_Access);
2847 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
2848 Name => Lock_Name,
2849 Parameter_Associations => New_List (Object_Parm));
2851 if Abort_Allowed then
2852 Stmts := New_List (
2853 Make_Procedure_Call_Statement (Loc,
2854 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
2855 Parameter_Associations => Empty_List),
2856 Lock_Stmt);
2858 else
2859 Stmts := New_List (Lock_Stmt);
2860 end if;
2862 if not Exc_Safe then
2863 Append (Unprot_Call, Stmts);
2864 else
2865 if Nkind (Op_Spec) = N_Function_Specification then
2866 Pre_Stmts := Stmts;
2867 Stmts := Empty_List;
2868 else
2869 Append (Unprot_Call, Stmts);
2870 end if;
2872 Append (
2873 Make_Procedure_Call_Statement (Loc,
2874 Name => Service_Name,
2875 Parameter_Associations =>
2876 New_List (New_Copy_Tree (Object_Parm))),
2877 Stmts);
2879 if Abort_Allowed then
2880 Append (
2881 Make_Procedure_Call_Statement (Loc,
2882 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
2883 Parameter_Associations => Empty_List),
2884 Stmts);
2885 end if;
2887 if Nkind (Op_Spec) = N_Function_Specification then
2888 Append (Return_Stmt, Stmts);
2889 Append (Make_Block_Statement (Loc,
2890 Declarations => New_List (Unprot_Call),
2891 Handled_Statement_Sequence =>
2892 Make_Handled_Sequence_Of_Statements (Loc,
2893 Statements => Stmts)), Pre_Stmts);
2894 Stmts := Pre_Stmts;
2895 end if;
2896 end if;
2898 Sub_Body :=
2899 Make_Subprogram_Body (Loc,
2900 Declarations => Empty_List,
2901 Specification => P_Op_Spec,
2902 Handled_Statement_Sequence =>
2903 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
2905 if not Exc_Safe then
2906 Set_Is_Protected_Subprogram_Body (Sub_Body);
2907 end if;
2909 return Sub_Body;
2910 end Build_Protected_Subprogram_Body;
2912 -------------------------------------
2913 -- Build_Protected_Subprogram_Call --
2914 -------------------------------------
2916 procedure Build_Protected_Subprogram_Call
2917 (N : Node_Id;
2918 Name : Node_Id;
2919 Rec : Node_Id;
2920 External : Boolean := True)
2922 Loc : constant Source_Ptr := Sloc (N);
2923 Sub : constant Entity_Id := Entity (Name);
2924 New_Sub : Node_Id;
2925 Params : List_Id;
2927 begin
2928 if External then
2929 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
2930 else
2931 New_Sub :=
2932 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
2933 end if;
2935 if Present (Parameter_Associations (N)) then
2936 Params := New_Copy_List_Tree (Parameter_Associations (N));
2937 else
2938 Params := New_List;
2939 end if;
2941 Prepend (Rec, Params);
2943 if Ekind (Sub) = E_Procedure then
2944 Rewrite (N,
2945 Make_Procedure_Call_Statement (Loc,
2946 Name => New_Sub,
2947 Parameter_Associations => Params));
2949 else
2950 pragma Assert (Ekind (Sub) = E_Function);
2951 Rewrite (N,
2952 Make_Function_Call (Loc,
2953 Name => New_Sub,
2954 Parameter_Associations => Params));
2955 end if;
2957 if External
2958 and then Nkind (Rec) = N_Unchecked_Type_Conversion
2959 and then Is_Entity_Name (Expression (Rec))
2960 and then Is_Shared_Passive (Entity (Expression (Rec)))
2961 then
2962 Add_Shared_Var_Lock_Procs (N);
2963 end if;
2964 end Build_Protected_Subprogram_Call;
2966 -------------------------
2967 -- Build_Selected_Name --
2968 -------------------------
2970 function Build_Selected_Name
2971 (Prefix : Entity_Id;
2972 Selector : Entity_Id;
2973 Append_Char : Character := ' ') return Name_Id
2975 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
2976 Select_Len : Natural;
2978 begin
2979 Get_Name_String (Chars (Selector));
2980 Select_Len := Name_Len;
2981 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
2982 Get_Name_String (Chars (Prefix));
2984 -- If scope is anonymous type, discard suffix to recover name of
2985 -- single protected object. Otherwise use protected type name.
2987 if Name_Buffer (Name_Len) = 'T' then
2988 Name_Len := Name_Len - 1;
2989 end if;
2991 Name_Buffer (Name_Len + 1) := '_';
2992 Name_Buffer (Name_Len + 2) := '_';
2994 Name_Len := Name_Len + 2;
2995 for J in 1 .. Select_Len loop
2996 Name_Len := Name_Len + 1;
2997 Name_Buffer (Name_Len) := Select_Buffer (J);
2998 end loop;
3000 -- Now add the Append_Char if specified. The encoding to follow
3001 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
3002 -- then the entity is associated to a protected type subprogram.
3003 -- Otherwise, it is a protected type entry. For each case, the
3004 -- encoding to follow for the suffix is documented in exp_dbug.ads.
3006 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
3008 if Append_Char /= ' ' then
3009 if Append_Char = 'P' or Append_Char = 'N' then
3010 Name_Len := Name_Len + 1;
3011 Name_Buffer (Name_Len) := Append_Char;
3012 return Name_Find;
3013 else
3014 Name_Buffer (Name_Len + 1) := '_';
3015 Name_Buffer (Name_Len + 2) := Append_Char;
3016 Name_Len := Name_Len + 2;
3017 return New_External_Name (Name_Find, ' ', -1);
3018 end if;
3019 else
3020 return Name_Find;
3021 end if;
3022 end Build_Selected_Name;
3024 -----------------------------
3025 -- Build_Simple_Entry_Call --
3026 -----------------------------
3028 -- A task entry call is converted to a call to Call_Simple
3030 -- declare
3031 -- P : parms := (parm, parm, parm);
3032 -- begin
3033 -- Call_Simple (acceptor-task, entry-index, P'Address);
3034 -- parm := P.param;
3035 -- parm := P.param;
3036 -- ...
3037 -- end;
3039 -- Here Pnn is an aggregate of the type constructed for the entry to hold
3040 -- the parameters, and the constructed aggregate value contains either the
3041 -- parameters or, in the case of non-elementary types, references to these
3042 -- parameters. Then the address of this aggregate is passed to the runtime
3043 -- routine, along with the task id value and the task entry index value.
3044 -- Pnn is only required if parameters are present.
3046 -- The assignments after the call are present only in the case of in-out
3047 -- or out parameters for elementary types, and are used to assign back the
3048 -- resulting values of such parameters.
3050 -- Note: the reason that we insert a block here is that in the context
3051 -- of selects, conditional entry calls etc. the entry call statement
3052 -- appears on its own, not as an element of a list.
3054 -- A protected entry call is converted to a Protected_Entry_Call:
3056 -- declare
3057 -- P : E1_Params := (param, param, param);
3058 -- Pnn : Boolean;
3059 -- Bnn : Communications_Block;
3061 -- declare
3062 -- P : E1_Params := (param, param, param);
3063 -- Bnn : Communications_Block;
3065 -- begin
3066 -- Protected_Entry_Call (
3067 -- Object => po._object'Access,
3068 -- E => <entry index>;
3069 -- Uninterpreted_Data => P'Address;
3070 -- Mode => Simple_Call;
3071 -- Block => Bnn);
3072 -- parm := P.param;
3073 -- parm := P.param;
3074 -- ...
3075 -- end;
3077 procedure Build_Simple_Entry_Call
3078 (N : Node_Id;
3079 Concval : Node_Id;
3080 Ename : Node_Id;
3081 Index : Node_Id)
3083 begin
3084 Expand_Call (N);
3086 -- If call has been inlined, nothing left to do
3088 if Nkind (N) = N_Block_Statement then
3089 return;
3090 end if;
3092 -- Convert entry call to Call_Simple call
3094 declare
3095 Loc : constant Source_Ptr := Sloc (N);
3096 Parms : constant List_Id := Parameter_Associations (N);
3097 Stats : constant List_Id := New_List;
3098 Actual : Node_Id;
3099 Call : Node_Id;
3100 Comm_Name : Entity_Id;
3101 Conctyp : Node_Id;
3102 Decls : List_Id;
3103 Ent : Entity_Id;
3104 Ent_Acc : Entity_Id;
3105 Formal : Node_Id;
3106 Iface_Tag : Entity_Id;
3107 Iface_Typ : Entity_Id;
3108 N_Node : Node_Id;
3109 N_Var : Node_Id;
3110 P : Entity_Id;
3111 Parm1 : Node_Id;
3112 Parm2 : Node_Id;
3113 Parm3 : Node_Id;
3114 Pdecl : Node_Id;
3115 Plist : List_Id;
3116 X : Entity_Id;
3117 Xdecl : Node_Id;
3119 begin
3120 -- Simple entry and entry family cases merge here
3122 Ent := Entity (Ename);
3123 Ent_Acc := Entry_Parameters_Type (Ent);
3124 Conctyp := Etype (Concval);
3126 -- If prefix is an access type, dereference to obtain the task type
3128 if Is_Access_Type (Conctyp) then
3129 Conctyp := Designated_Type (Conctyp);
3130 end if;
3132 -- Special case for protected subprogram calls
3134 if Is_Protected_Type (Conctyp)
3135 and then Is_Subprogram (Entity (Ename))
3136 then
3137 if not Is_Eliminated (Entity (Ename)) then
3138 Build_Protected_Subprogram_Call
3139 (N, Ename, Convert_Concurrent (Concval, Conctyp));
3140 Analyze (N);
3141 end if;
3143 return;
3144 end if;
3146 -- First parameter is the Task_Id value from the task value or the
3147 -- Object from the protected object value, obtained by selecting
3148 -- the _Task_Id or _Object from the result of doing an unchecked
3149 -- conversion to convert the value to the corresponding record type.
3151 if Nkind (Concval) = N_Function_Call
3152 and then Is_Task_Type (Conctyp)
3153 and then Ada_Version >= Ada_05
3154 then
3155 declare
3156 Obj : constant Entity_Id :=
3157 Make_Defining_Identifier (Loc, New_Internal_Name ('F'));
3158 Decl : Node_Id;
3160 begin
3161 Decl :=
3162 Make_Object_Declaration (Loc,
3163 Defining_Identifier => Obj,
3164 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
3165 Expression => Relocate_Node (Concval));
3166 Set_Etype (Obj, Conctyp);
3167 Decls := New_List (Decl);
3168 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
3169 end;
3171 else
3172 Decls := New_List;
3173 end if;
3175 Parm1 := Concurrent_Ref (Concval);
3177 -- Second parameter is the entry index, computed by the routine
3178 -- provided for this purpose. The value of this expression is
3179 -- assigned to an intermediate variable to assure that any entry
3180 -- family index expressions are evaluated before the entry
3181 -- parameters.
3183 if Abort_Allowed
3184 or else Restriction_Active (No_Entry_Queue) = False
3185 or else not Is_Protected_Type (Conctyp)
3186 or else Number_Entries (Conctyp) > 1
3187 or else (Has_Attach_Handler (Conctyp)
3188 and then not Restricted_Profile)
3189 then
3190 X := Make_Defining_Identifier (Loc, Name_uX);
3192 Xdecl :=
3193 Make_Object_Declaration (Loc,
3194 Defining_Identifier => X,
3195 Object_Definition =>
3196 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
3197 Expression => Actual_Index_Expression (
3198 Loc, Entity (Ename), Index, Concval));
3200 Append_To (Decls, Xdecl);
3201 Parm2 := New_Reference_To (X, Loc);
3203 else
3204 Xdecl := Empty;
3205 Parm2 := Empty;
3206 end if;
3208 -- The third parameter is the packaged parameters. If there are
3209 -- none, then it is just the null address, since nothing is passed.
3211 if No (Parms) then
3212 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
3213 P := Empty;
3215 -- Case of parameters present, where third argument is the address
3216 -- of a packaged record containing the required parameter values.
3218 else
3219 -- First build a list of parameter values, which are references to
3220 -- objects of the parameter types.
3222 Plist := New_List;
3224 Actual := First_Actual (N);
3225 Formal := First_Formal (Ent);
3227 while Present (Actual) loop
3229 -- If it is a by_copy_type, copy it to a new variable. The
3230 -- packaged record has a field that points to this variable.
3232 if Is_By_Copy_Type (Etype (Actual)) then
3233 N_Node :=
3234 Make_Object_Declaration (Loc,
3235 Defining_Identifier =>
3236 Make_Defining_Identifier (Loc,
3237 Chars => New_Internal_Name ('J')),
3238 Aliased_Present => True,
3239 Object_Definition =>
3240 New_Reference_To (Etype (Formal), Loc));
3242 -- Mark the object as not needing initialization since the
3243 -- initialization is performed separately, avoiding errors
3244 -- on cases such as formals of null-excluding access types.
3246 Set_No_Initialization (N_Node);
3248 -- We must make an assignment statement separate for the
3249 -- case of limited type. We cannot assign it unless the
3250 -- Assignment_OK flag is set first. An out formal of an
3251 -- access type must also be initialized from the actual,
3252 -- as stated in RM 6.4.1 (13).
3254 if Ekind (Formal) /= E_Out_Parameter
3255 or else Is_Access_Type (Etype (Formal))
3256 then
3257 N_Var :=
3258 New_Reference_To (Defining_Identifier (N_Node), Loc);
3259 Set_Assignment_OK (N_Var);
3260 Append_To (Stats,
3261 Make_Assignment_Statement (Loc,
3262 Name => N_Var,
3263 Expression => Relocate_Node (Actual)));
3264 end if;
3266 Append (N_Node, Decls);
3268 Append_To (Plist,
3269 Make_Attribute_Reference (Loc,
3270 Attribute_Name => Name_Unchecked_Access,
3271 Prefix =>
3272 New_Reference_To (Defining_Identifier (N_Node), Loc)));
3273 else
3274 -- Interface class-wide formal
3276 if Ada_Version >= Ada_05
3277 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
3278 and then Is_Interface (Etype (Formal))
3279 then
3280 Iface_Typ := Etype (Etype (Formal));
3282 -- Generate:
3283 -- formal_iface_type! (actual.iface_tag)'reference
3285 Iface_Tag :=
3286 Find_Interface_Tag (Etype (Actual), Iface_Typ);
3287 pragma Assert (Present (Iface_Tag));
3289 Append_To (Plist,
3290 Make_Reference (Loc,
3291 Unchecked_Convert_To (Iface_Typ,
3292 Make_Selected_Component (Loc,
3293 Prefix =>
3294 Relocate_Node (Actual),
3295 Selector_Name =>
3296 New_Reference_To (Iface_Tag, Loc)))));
3297 else
3298 -- Generate:
3299 -- actual'reference
3301 Append_To (Plist,
3302 Make_Reference (Loc, Relocate_Node (Actual)));
3303 end if;
3304 end if;
3306 Next_Actual (Actual);
3307 Next_Formal_With_Extras (Formal);
3308 end loop;
3310 -- Now build the declaration of parameters initialized with the
3311 -- aggregate containing this constructed parameter list.
3313 P := Make_Defining_Identifier (Loc, Name_uP);
3315 Pdecl :=
3316 Make_Object_Declaration (Loc,
3317 Defining_Identifier => P,
3318 Object_Definition =>
3319 New_Reference_To (Designated_Type (Ent_Acc), Loc),
3320 Expression =>
3321 Make_Aggregate (Loc, Expressions => Plist));
3323 Parm3 :=
3324 Make_Attribute_Reference (Loc,
3325 Prefix => New_Reference_To (P, Loc),
3326 Attribute_Name => Name_Address);
3328 Append (Pdecl, Decls);
3329 end if;
3331 -- Now we can create the call, case of protected type
3333 if Is_Protected_Type (Conctyp) then
3334 case Corresponding_Runtime_Package (Conctyp) is
3335 when System_Tasking_Protected_Objects_Entries =>
3337 -- Change the type of the index declaration
3339 Set_Object_Definition (Xdecl,
3340 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
3342 -- Some additional declarations for protected entry calls
3344 if No (Decls) then
3345 Decls := New_List;
3346 end if;
3348 -- Bnn : Communications_Block;
3350 Comm_Name :=
3351 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
3353 Append_To (Decls,
3354 Make_Object_Declaration (Loc,
3355 Defining_Identifier => Comm_Name,
3356 Object_Definition =>
3357 New_Reference_To (RTE (RE_Communication_Block), Loc)));
3359 -- Some additional statements for protected entry calls
3361 -- Protected_Entry_Call (
3362 -- Object => po._object'Access,
3363 -- E => <entry index>;
3364 -- Uninterpreted_Data => P'Address;
3365 -- Mode => Simple_Call;
3366 -- Block => Bnn);
3368 Call :=
3369 Make_Procedure_Call_Statement (Loc,
3370 Name =>
3371 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
3373 Parameter_Associations => New_List (
3374 Make_Attribute_Reference (Loc,
3375 Attribute_Name => Name_Unchecked_Access,
3376 Prefix => Parm1),
3377 Parm2,
3378 Parm3,
3379 New_Reference_To (RTE (RE_Simple_Call), Loc),
3380 New_Occurrence_Of (Comm_Name, Loc)));
3382 when System_Tasking_Protected_Objects_Single_Entry =>
3383 -- Protected_Single_Entry_Call (
3384 -- Object => po._object'Access,
3385 -- Uninterpreted_Data => P'Address;
3386 -- Mode => Simple_Call);
3388 Call :=
3389 Make_Procedure_Call_Statement (Loc,
3390 Name => New_Reference_To (
3391 RTE (RE_Protected_Single_Entry_Call), Loc),
3393 Parameter_Associations => New_List (
3394 Make_Attribute_Reference (Loc,
3395 Attribute_Name => Name_Unchecked_Access,
3396 Prefix => Parm1),
3397 Parm3,
3398 New_Reference_To (RTE (RE_Simple_Call), Loc)));
3400 when others =>
3401 raise Program_Error;
3402 end case;
3404 -- Case of task type
3406 else
3407 Call :=
3408 Make_Procedure_Call_Statement (Loc,
3409 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
3410 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
3412 end if;
3414 Append_To (Stats, Call);
3416 -- If there are out or in/out parameters by copy add assignment
3417 -- statements for the result values.
3419 if Present (Parms) then
3420 Actual := First_Actual (N);
3421 Formal := First_Formal (Ent);
3423 Set_Assignment_OK (Actual);
3424 while Present (Actual) loop
3425 if Is_By_Copy_Type (Etype (Actual))
3426 and then Ekind (Formal) /= E_In_Parameter
3427 then
3428 N_Node :=
3429 Make_Assignment_Statement (Loc,
3430 Name => New_Copy (Actual),
3431 Expression =>
3432 Make_Explicit_Dereference (Loc,
3433 Make_Selected_Component (Loc,
3434 Prefix => New_Reference_To (P, Loc),
3435 Selector_Name =>
3436 Make_Identifier (Loc, Chars (Formal)))));
3438 -- In all cases (including limited private types) we want
3439 -- the assignment to be valid.
3441 Set_Assignment_OK (Name (N_Node));
3443 -- If the call is the triggering alternative in an
3444 -- asynchronous select, or the entry_call alternative of a
3445 -- conditional entry call, the assignments for in-out
3446 -- parameters are incorporated into the statement list that
3447 -- follows, so that there are executed only if the entry
3448 -- call succeeds.
3450 if (Nkind (Parent (N)) = N_Triggering_Alternative
3451 and then N = Triggering_Statement (Parent (N)))
3452 or else
3453 (Nkind (Parent (N)) = N_Entry_Call_Alternative
3454 and then N = Entry_Call_Statement (Parent (N)))
3455 then
3456 if No (Statements (Parent (N))) then
3457 Set_Statements (Parent (N), New_List);
3458 end if;
3460 Prepend (N_Node, Statements (Parent (N)));
3462 else
3463 Insert_After (Call, N_Node);
3464 end if;
3465 end if;
3467 Next_Actual (Actual);
3468 Next_Formal_With_Extras (Formal);
3469 end loop;
3470 end if;
3472 -- Finally, create block and analyze it
3474 Rewrite (N,
3475 Make_Block_Statement (Loc,
3476 Declarations => Decls,
3477 Handled_Statement_Sequence =>
3478 Make_Handled_Sequence_Of_Statements (Loc,
3479 Statements => Stats)));
3481 Analyze (N);
3482 end;
3483 end Build_Simple_Entry_Call;
3485 --------------------------------
3486 -- Build_Task_Activation_Call --
3487 --------------------------------
3489 procedure Build_Task_Activation_Call (N : Node_Id) is
3490 Loc : constant Source_Ptr := Sloc (N);
3491 Chain : Entity_Id;
3492 Call : Node_Id;
3493 Name : Node_Id;
3494 P : Node_Id;
3496 begin
3497 -- Get the activation chain entity. Except in the case of a package
3498 -- body, this is in the node that was passed. For a package body, we
3499 -- have to find the corresponding package declaration node.
3501 if Nkind (N) = N_Package_Body then
3502 P := Corresponding_Spec (N);
3503 loop
3504 P := Parent (P);
3505 exit when Nkind (P) = N_Package_Declaration;
3506 end loop;
3508 Chain := Activation_Chain_Entity (P);
3510 else
3511 Chain := Activation_Chain_Entity (N);
3512 end if;
3514 if Present (Chain) then
3515 if Restricted_Profile then
3516 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
3517 else
3518 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
3519 end if;
3521 Call :=
3522 Make_Procedure_Call_Statement (Loc,
3523 Name => Name,
3524 Parameter_Associations =>
3525 New_List (Make_Attribute_Reference (Loc,
3526 Prefix => New_Occurrence_Of (Chain, Loc),
3527 Attribute_Name => Name_Unchecked_Access)));
3529 if Nkind (N) = N_Package_Declaration then
3530 if Present (Corresponding_Body (N)) then
3531 null;
3533 elsif Present (Private_Declarations (Specification (N))) then
3534 Append (Call, Private_Declarations (Specification (N)));
3536 else
3537 Append (Call, Visible_Declarations (Specification (N)));
3538 end if;
3540 else
3541 if Present (Handled_Statement_Sequence (N)) then
3543 -- The call goes at the start of the statement sequence
3544 -- after the start of exception range label if one is present.
3546 declare
3547 Stm : Node_Id;
3549 begin
3550 Stm := First (Statements (Handled_Statement_Sequence (N)));
3552 -- A special case, skip exception range label if one is
3553 -- present (from front end zcx processing).
3555 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
3556 Next (Stm);
3557 end if;
3559 -- Another special case, if the first statement is a block
3560 -- from optimization of a local raise to a goto, then the
3561 -- call goes inside this block.
3563 if Nkind (Stm) = N_Block_Statement
3564 and then Exception_Junk (Stm)
3565 then
3566 Stm :=
3567 First (Statements (Handled_Statement_Sequence (Stm)));
3568 end if;
3570 -- Insertion point is after any exception label pushes,
3571 -- since we want it covered by any local handlers.
3573 while Nkind (Stm) in N_Push_xxx_Label loop
3574 Next (Stm);
3575 end loop;
3577 -- Now we have the proper insertion point
3579 Insert_Before (Stm, Call);
3580 end;
3582 else
3583 Set_Handled_Statement_Sequence (N,
3584 Make_Handled_Sequence_Of_Statements (Loc,
3585 Statements => New_List (Call)));
3586 end if;
3587 end if;
3589 Analyze (Call);
3590 Check_Task_Activation (N);
3591 end if;
3592 end Build_Task_Activation_Call;
3594 -------------------------------
3595 -- Build_Task_Allocate_Block --
3596 -------------------------------
3598 procedure Build_Task_Allocate_Block
3599 (Actions : List_Id;
3600 N : Node_Id;
3601 Args : List_Id)
3603 T : constant Entity_Id := Entity (Expression (N));
3604 Init : constant Entity_Id := Base_Init_Proc (T);
3605 Loc : constant Source_Ptr := Sloc (N);
3606 Chain : constant Entity_Id :=
3607 Make_Defining_Identifier (Loc, Name_uChain);
3609 Blkent : Entity_Id;
3610 Block : Node_Id;
3612 begin
3613 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3615 Block :=
3616 Make_Block_Statement (Loc,
3617 Identifier => New_Reference_To (Blkent, Loc),
3618 Declarations => New_List (
3620 -- _Chain : Activation_Chain;
3622 Make_Object_Declaration (Loc,
3623 Defining_Identifier => Chain,
3624 Aliased_Present => True,
3625 Object_Definition =>
3626 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
3628 Handled_Statement_Sequence =>
3629 Make_Handled_Sequence_Of_Statements (Loc,
3631 Statements => New_List (
3633 -- Init (Args);
3635 Make_Procedure_Call_Statement (Loc,
3636 Name => New_Reference_To (Init, Loc),
3637 Parameter_Associations => Args),
3639 -- Activate_Tasks (_Chain);
3641 Make_Procedure_Call_Statement (Loc,
3642 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
3643 Parameter_Associations => New_List (
3644 Make_Attribute_Reference (Loc,
3645 Prefix => New_Reference_To (Chain, Loc),
3646 Attribute_Name => Name_Unchecked_Access))))),
3648 Has_Created_Identifier => True,
3649 Is_Task_Allocation_Block => True);
3651 Append_To (Actions,
3652 Make_Implicit_Label_Declaration (Loc,
3653 Defining_Identifier => Blkent,
3654 Label_Construct => Block));
3656 Append_To (Actions, Block);
3658 Set_Activation_Chain_Entity (Block, Chain);
3659 end Build_Task_Allocate_Block;
3661 -----------------------------------------------
3662 -- Build_Task_Allocate_Block_With_Init_Stmts --
3663 -----------------------------------------------
3665 procedure Build_Task_Allocate_Block_With_Init_Stmts
3666 (Actions : List_Id;
3667 N : Node_Id;
3668 Init_Stmts : List_Id)
3670 Loc : constant Source_Ptr := Sloc (N);
3671 Chain : constant Entity_Id :=
3672 Make_Defining_Identifier (Loc, Name_uChain);
3673 Blkent : Entity_Id;
3674 Block : Node_Id;
3676 begin
3677 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3679 Append_To (Init_Stmts,
3680 Make_Procedure_Call_Statement (Loc,
3681 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
3682 Parameter_Associations => New_List (
3683 Make_Attribute_Reference (Loc,
3684 Prefix => New_Reference_To (Chain, Loc),
3685 Attribute_Name => Name_Unchecked_Access))));
3687 Block :=
3688 Make_Block_Statement (Loc,
3689 Identifier => New_Reference_To (Blkent, Loc),
3690 Declarations => New_List (
3692 -- _Chain : Activation_Chain;
3694 Make_Object_Declaration (Loc,
3695 Defining_Identifier => Chain,
3696 Aliased_Present => True,
3697 Object_Definition =>
3698 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
3700 Handled_Statement_Sequence =>
3701 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
3703 Has_Created_Identifier => True,
3704 Is_Task_Allocation_Block => True);
3706 Append_To (Actions,
3707 Make_Implicit_Label_Declaration (Loc,
3708 Defining_Identifier => Blkent,
3709 Label_Construct => Block));
3711 Append_To (Actions, Block);
3713 Set_Activation_Chain_Entity (Block, Chain);
3714 end Build_Task_Allocate_Block_With_Init_Stmts;
3716 -----------------------------------
3717 -- Build_Task_Proc_Specification --
3718 -----------------------------------
3720 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
3721 Loc : constant Source_Ptr := Sloc (T);
3722 Spec_Id : Entity_Id;
3724 begin
3725 Spec_Id :=
3726 Make_Defining_Identifier (Loc,
3727 Chars => New_External_Name (Chars (T), 'B'));
3728 Set_Is_Internal (Spec_Id);
3730 -- Associate the procedure with the task, if this is the declaration
3731 -- (and not the body) of the procedure.
3733 if No (Task_Body_Procedure (T)) then
3734 Set_Task_Body_Procedure (T, Spec_Id);
3735 end if;
3737 return
3738 Make_Procedure_Specification (Loc,
3739 Defining_Unit_Name => Spec_Id,
3740 Parameter_Specifications => New_List (
3741 Make_Parameter_Specification (Loc,
3742 Defining_Identifier =>
3743 Make_Defining_Identifier (Loc, Name_uTask),
3744 Parameter_Type =>
3745 Make_Access_Definition (Loc,
3746 Subtype_Mark =>
3747 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
3748 end Build_Task_Proc_Specification;
3750 ---------------------------------------
3751 -- Build_Unprotected_Subprogram_Body --
3752 ---------------------------------------
3754 function Build_Unprotected_Subprogram_Body
3755 (N : Node_Id;
3756 Pid : Node_Id) return Node_Id
3758 Decls : constant List_Id := Declarations (N);
3760 begin
3761 -- Add renamings for the Protection object, discriminals, privals and
3762 -- the entry index constant for use by debugger.
3764 Debug_Private_Data_Declarations (Decls);
3766 -- Make an unprotected version of the subprogram for use within the same
3767 -- object, with a new name and an additional parameter representing the
3768 -- object.
3770 return
3771 Make_Subprogram_Body (Sloc (N),
3772 Specification =>
3773 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
3774 Declarations => Decls,
3775 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
3776 end Build_Unprotected_Subprogram_Body;
3778 ----------------------------
3779 -- Collect_Entry_Families --
3780 ----------------------------
3782 procedure Collect_Entry_Families
3783 (Loc : Source_Ptr;
3784 Cdecls : List_Id;
3785 Current_Node : in out Node_Id;
3786 Conctyp : Entity_Id)
3788 Efam : Entity_Id;
3789 Efam_Decl : Node_Id;
3790 Efam_Type : Entity_Id;
3792 begin
3793 Efam := First_Entity (Conctyp);
3794 while Present (Efam) loop
3795 if Ekind (Efam) = E_Entry_Family then
3796 Efam_Type :=
3797 Make_Defining_Identifier (Loc,
3798 Chars => New_Internal_Name ('F'));
3800 declare
3801 Bas : Entity_Id :=
3802 Base_Type
3803 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
3805 Bas_Decl : Node_Id := Empty;
3806 Lo, Hi : Node_Id;
3808 begin
3809 Get_Index_Bounds
3810 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
3812 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
3813 Bas :=
3814 Make_Defining_Identifier (Loc,
3815 Chars => New_Internal_Name ('B'));
3817 Bas_Decl :=
3818 Make_Subtype_Declaration (Loc,
3819 Defining_Identifier => Bas,
3820 Subtype_Indication =>
3821 Make_Subtype_Indication (Loc,
3822 Subtype_Mark =>
3823 New_Occurrence_Of (Standard_Integer, Loc),
3824 Constraint =>
3825 Make_Range_Constraint (Loc,
3826 Range_Expression => Make_Range (Loc,
3827 Make_Integer_Literal
3828 (Loc, -Entry_Family_Bound),
3829 Make_Integer_Literal
3830 (Loc, Entry_Family_Bound - 1)))));
3832 Insert_After (Current_Node, Bas_Decl);
3833 Current_Node := Bas_Decl;
3834 Analyze (Bas_Decl);
3835 end if;
3837 Efam_Decl :=
3838 Make_Full_Type_Declaration (Loc,
3839 Defining_Identifier => Efam_Type,
3840 Type_Definition =>
3841 Make_Unconstrained_Array_Definition (Loc,
3842 Subtype_Marks =>
3843 (New_List (New_Occurrence_Of (Bas, Loc))),
3845 Component_Definition =>
3846 Make_Component_Definition (Loc,
3847 Aliased_Present => False,
3848 Subtype_Indication =>
3849 New_Reference_To (Standard_Character, Loc))));
3850 end;
3852 Insert_After (Current_Node, Efam_Decl);
3853 Current_Node := Efam_Decl;
3854 Analyze (Efam_Decl);
3856 Append_To (Cdecls,
3857 Make_Component_Declaration (Loc,
3858 Defining_Identifier =>
3859 Make_Defining_Identifier (Loc, Chars (Efam)),
3861 Component_Definition =>
3862 Make_Component_Definition (Loc,
3863 Aliased_Present => False,
3864 Subtype_Indication =>
3865 Make_Subtype_Indication (Loc,
3866 Subtype_Mark =>
3867 New_Occurrence_Of (Efam_Type, Loc),
3869 Constraint =>
3870 Make_Index_Or_Discriminant_Constraint (Loc,
3871 Constraints => New_List (
3872 New_Occurrence_Of
3873 (Etype (Discrete_Subtype_Definition
3874 (Parent (Efam))), Loc)))))));
3876 end if;
3878 Next_Entity (Efam);
3879 end loop;
3880 end Collect_Entry_Families;
3882 -----------------------
3883 -- Concurrent_Object --
3884 -----------------------
3886 function Concurrent_Object
3887 (Spec_Id : Entity_Id;
3888 Conc_Typ : Entity_Id) return Entity_Id
3890 begin
3891 -- Parameter _O or _object
3893 if Is_Protected_Type (Conc_Typ) then
3894 return First_Formal (Protected_Body_Subprogram (Spec_Id));
3896 -- Parameter _task
3898 else
3899 pragma Assert (Is_Task_Type (Conc_Typ));
3900 return First_Formal (Task_Body_Procedure (Conc_Typ));
3901 end if;
3902 end Concurrent_Object;
3904 ----------------------
3905 -- Copy_Result_Type --
3906 ----------------------
3908 function Copy_Result_Type (Res : Node_Id) return Node_Id is
3909 New_Res : constant Node_Id := New_Copy_Tree (Res);
3910 Par_Spec : Node_Id;
3911 Formal : Entity_Id;
3913 begin
3914 -- If the result type is an access_to_subprogram, we must create
3915 -- new entities for its spec.
3917 if Nkind (New_Res) = N_Access_Definition
3918 and then Present (Access_To_Subprogram_Definition (New_Res))
3919 then
3920 -- Provide new entities for the formals
3922 Par_Spec := First (Parameter_Specifications
3923 (Access_To_Subprogram_Definition (New_Res)));
3924 while Present (Par_Spec) loop
3925 Formal := Defining_Identifier (Par_Spec);
3926 Set_Defining_Identifier (Par_Spec,
3927 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
3928 Next (Par_Spec);
3929 end loop;
3930 end if;
3932 return New_Res;
3933 end Copy_Result_Type;
3935 --------------------
3936 -- Concurrent_Ref --
3937 --------------------
3939 -- The expression returned for a reference to a concurrent object has the
3940 -- form:
3942 -- taskV!(name)._Task_Id
3944 -- for a task, and
3946 -- objectV!(name)._Object
3948 -- for a protected object. For the case of an access to a concurrent
3949 -- object, there is an extra explicit dereference:
3951 -- taskV!(name.all)._Task_Id
3952 -- objectV!(name.all)._Object
3954 -- here taskV and objectV are the types for the associated records, which
3955 -- contain the required _Task_Id and _Object fields for tasks and protected
3956 -- objects, respectively.
3958 -- For the case of a task type name, the expression is
3960 -- Self;
3962 -- i.e. a call to the Self function which returns precisely this Task_Id
3964 -- For the case of a protected type name, the expression is
3966 -- objectR
3968 -- which is a renaming of the _object field of the current object object
3969 -- record, passed into protected operations as a parameter.
3971 function Concurrent_Ref (N : Node_Id) return Node_Id is
3972 Loc : constant Source_Ptr := Sloc (N);
3973 Ntyp : constant Entity_Id := Etype (N);
3974 Dtyp : Entity_Id;
3975 Sel : Name_Id;
3977 function Is_Current_Task (T : Entity_Id) return Boolean;
3978 -- Check whether the reference is to the immediately enclosing task
3979 -- type, or to an outer one (rare but legal).
3981 ---------------------
3982 -- Is_Current_Task --
3983 ---------------------
3985 function Is_Current_Task (T : Entity_Id) return Boolean is
3986 Scop : Entity_Id;
3988 begin
3989 Scop := Current_Scope;
3990 while Present (Scop)
3991 and then Scop /= Standard_Standard
3992 loop
3994 if Scop = T then
3995 return True;
3997 elsif Is_Task_Type (Scop) then
3998 return False;
4000 -- If this is a procedure nested within the task type, we must
4001 -- assume that it can be called from an inner task, and therefore
4002 -- cannot treat it as a local reference.
4004 elsif Is_Overloadable (Scop)
4005 and then In_Open_Scopes (T)
4006 then
4007 return False;
4009 else
4010 Scop := Scope (Scop);
4011 end if;
4012 end loop;
4014 -- We know that we are within the task body, so should have found it
4015 -- in scope.
4017 raise Program_Error;
4018 end Is_Current_Task;
4020 -- Start of processing for Concurrent_Ref
4022 begin
4023 if Is_Access_Type (Ntyp) then
4024 Dtyp := Designated_Type (Ntyp);
4026 if Is_Protected_Type (Dtyp) then
4027 Sel := Name_uObject;
4028 else
4029 Sel := Name_uTask_Id;
4030 end if;
4032 return
4033 Make_Selected_Component (Loc,
4034 Prefix =>
4035 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
4036 Make_Explicit_Dereference (Loc, N)),
4037 Selector_Name => Make_Identifier (Loc, Sel));
4039 elsif Is_Entity_Name (N)
4040 and then Is_Concurrent_Type (Entity (N))
4041 then
4042 if Is_Task_Type (Entity (N)) then
4044 if Is_Current_Task (Entity (N)) then
4045 return
4046 Make_Function_Call (Loc,
4047 Name => New_Reference_To (RTE (RE_Self), Loc));
4049 else
4050 declare
4051 Decl : Node_Id;
4052 T_Self : constant Entity_Id :=
4053 Make_Defining_Identifier (Loc,
4054 Chars => New_Internal_Name ('T'));
4055 T_Body : constant Node_Id :=
4056 Parent (Corresponding_Body (Parent (Entity (N))));
4058 begin
4059 Decl := Make_Object_Declaration (Loc,
4060 Defining_Identifier => T_Self,
4061 Object_Definition =>
4062 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
4063 Expression =>
4064 Make_Function_Call (Loc,
4065 Name => New_Reference_To (RTE (RE_Self), Loc)));
4066 Prepend (Decl, Declarations (T_Body));
4067 Analyze (Decl);
4068 Set_Scope (T_Self, Entity (N));
4069 return New_Occurrence_Of (T_Self, Loc);
4070 end;
4071 end if;
4073 else
4074 pragma Assert (Is_Protected_Type (Entity (N)));
4076 return
4077 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
4078 end if;
4080 else
4081 if Is_Protected_Type (Ntyp) then
4082 Sel := Name_uObject;
4084 elsif Is_Task_Type (Ntyp) then
4085 Sel := Name_uTask_Id;
4087 else
4088 raise Program_Error;
4089 end if;
4091 return
4092 Make_Selected_Component (Loc,
4093 Prefix =>
4094 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
4095 New_Copy_Tree (N)),
4096 Selector_Name => Make_Identifier (Loc, Sel));
4097 end if;
4098 end Concurrent_Ref;
4100 ------------------------
4101 -- Convert_Concurrent --
4102 ------------------------
4104 function Convert_Concurrent
4105 (N : Node_Id;
4106 Typ : Entity_Id) return Node_Id
4108 begin
4109 if not Is_Concurrent_Type (Typ) then
4110 return N;
4111 else
4112 return
4113 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
4114 New_Copy_Tree (N));
4115 end if;
4116 end Convert_Concurrent;
4118 -------------------------------------
4119 -- Debug_Private_Data_Declarations --
4120 -------------------------------------
4122 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
4123 Debug_Nod : Node_Id;
4124 Decl : Node_Id;
4126 begin
4127 Decl := First (Decls);
4128 while Present (Decl)
4129 and then not Comes_From_Source (Decl)
4130 loop
4131 -- Declaration for concurrent entity _object and its access type,
4132 -- along with the entry index subtype:
4133 -- type prot_typVP is access prot_typV;
4134 -- _object : prot_typVP := prot_typV (_O);
4135 -- subtype Jnn is <Type of Index> range Low .. High;
4137 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
4138 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4140 -- Declaration for the Protection object, discriminals, privals and
4141 -- entry index constant:
4142 -- conc_typR : protection_typ renames _object._object;
4143 -- discr_nameD : discr_typ renames _object.discr_name;
4144 -- discr_nameD : discr_typ renames _task.discr_name;
4145 -- prival_name : comp_typ renames _object.comp_name;
4146 -- J : constant Jnn :=
4147 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
4149 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
4150 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4151 Debug_Nod := Debug_Renaming_Declaration (Decl);
4153 if Present (Debug_Nod) then
4154 Insert_After (Decl, Debug_Nod);
4155 end if;
4156 end if;
4158 Next (Decl);
4159 end loop;
4160 end Debug_Private_Data_Declarations;
4162 ----------------------------
4163 -- Entry_Index_Expression --
4164 ----------------------------
4166 function Entry_Index_Expression
4167 (Sloc : Source_Ptr;
4168 Ent : Entity_Id;
4169 Index : Node_Id;
4170 Ttyp : Entity_Id) return Node_Id
4172 Expr : Node_Id;
4173 Num : Node_Id;
4174 Lo : Node_Id;
4175 Hi : Node_Id;
4176 Prev : Entity_Id;
4177 S : Node_Id;
4179 begin
4180 -- The queues of entries and entry families appear in textual order in
4181 -- the associated record. The entry index is computed as the sum of the
4182 -- number of queues for all entries that precede the designated one, to
4183 -- which is added the index expression, if this expression denotes a
4184 -- member of a family.
4186 -- The following is a place holder for the count of simple entries
4188 Num := Make_Integer_Literal (Sloc, 1);
4190 -- We construct an expression which is a series of addition operations.
4191 -- The first operand is the number of single entries that precede this
4192 -- one, the second operand is the index value relative to the start of
4193 -- the referenced family, and the remaining operands are the lengths of
4194 -- the entry families that precede this entry, i.e. the constructed
4195 -- expression is:
4197 -- number_simple_entries +
4198 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
4199 -- family'length + ...
4201 -- where index-value is the given index value, and s is the index
4202 -- subtype (we have to use pos because the subtype might be an
4203 -- enumeration type preventing direct subtraction). Note that the task
4204 -- entry array is one-indexed.
4206 -- The upper bound of the entry family may be a discriminant, so we
4207 -- retrieve the lower bound explicitly to compute offset, rather than
4208 -- using the index subtype which may mention a discriminant.
4210 if Present (Index) then
4211 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
4213 Expr :=
4214 Make_Op_Add (Sloc,
4215 Left_Opnd => Num,
4217 Right_Opnd =>
4218 Family_Offset (
4219 Sloc,
4220 Make_Attribute_Reference (Sloc,
4221 Attribute_Name => Name_Pos,
4222 Prefix => New_Reference_To (Base_Type (S), Sloc),
4223 Expressions => New_List (Relocate_Node (Index))),
4224 Type_Low_Bound (S),
4225 Ttyp,
4226 False));
4227 else
4228 Expr := Num;
4229 end if;
4231 -- Now add lengths of preceding entries and entry families
4233 Prev := First_Entity (Ttyp);
4235 while Chars (Prev) /= Chars (Ent)
4236 or else (Ekind (Prev) /= Ekind (Ent))
4237 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
4238 loop
4239 if Ekind (Prev) = E_Entry then
4240 Set_Intval (Num, Intval (Num) + 1);
4242 elsif Ekind (Prev) = E_Entry_Family then
4243 S :=
4244 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
4245 Lo := Type_Low_Bound (S);
4246 Hi := Type_High_Bound (S);
4248 Expr :=
4249 Make_Op_Add (Sloc,
4250 Left_Opnd => Expr,
4251 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
4253 -- Other components are anonymous types to be ignored
4255 else
4256 null;
4257 end if;
4259 Next_Entity (Prev);
4260 end loop;
4262 return Expr;
4263 end Entry_Index_Expression;
4265 ---------------------------
4266 -- Establish_Task_Master --
4267 ---------------------------
4269 procedure Establish_Task_Master (N : Node_Id) is
4270 Call : Node_Id;
4271 begin
4272 if Restriction_Active (No_Task_Hierarchy) = False then
4273 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
4274 Prepend_To (Declarations (N), Call);
4275 Analyze (Call);
4276 end if;
4277 end Establish_Task_Master;
4279 --------------------------------
4280 -- Expand_Accept_Declarations --
4281 --------------------------------
4283 -- Part of the expansion of an accept statement involves the creation of
4284 -- a declaration that can be referenced from the statement sequence of
4285 -- the accept:
4287 -- Ann : Address;
4289 -- This declaration is inserted immediately before the accept statement
4290 -- and it is important that it be inserted before the statements of the
4291 -- statement sequence are analyzed. Thus it would be too late to create
4292 -- this declaration in the Expand_N_Accept_Statement routine, which is
4293 -- why there is a separate procedure to be called directly from Sem_Ch9.
4295 -- Ann is used to hold the address of the record containing the parameters
4296 -- (see Expand_N_Entry_Call for more details on how this record is built).
4297 -- References to the parameters do an unchecked conversion of this address
4298 -- to a pointer to the required record type, and then access the field that
4299 -- holds the value of the required parameter. The entity for the address
4300 -- variable is held as the top stack element (i.e. the last element) of the
4301 -- Accept_Address stack in the corresponding entry entity, and this element
4302 -- must be set in place before the statements are processed.
4304 -- The above description applies to the case of a stand alone accept
4305 -- statement, i.e. one not appearing as part of a select alternative.
4307 -- For the case of an accept that appears as part of a select alternative
4308 -- of a selective accept, we must still create the declaration right away,
4309 -- since Ann is needed immediately, but there is an important difference:
4311 -- The declaration is inserted before the selective accept, not before
4312 -- the accept statement (which is not part of a list anyway, and so would
4313 -- not accommodate inserted declarations)
4315 -- We only need one address variable for the entire selective accept. So
4316 -- the Ann declaration is created only for the first accept alternative,
4317 -- and subsequent accept alternatives reference the same Ann variable.
4319 -- We can distinguish the two cases by seeing whether the accept statement
4320 -- is part of a list. If not, then it must be in an accept alternative.
4322 -- To expand the requeue statement, a label is provided at the end of the
4323 -- accept statement or alternative of which it is a part, so that the
4324 -- statement can be skipped after the requeue is complete. This label is
4325 -- created here rather than during the expansion of the accept statement,
4326 -- because it will be needed by any requeue statements within the accept,
4327 -- which are expanded before the accept.
4329 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
4330 Loc : constant Source_Ptr := Sloc (N);
4331 Stats : constant Node_Id := Handled_Statement_Sequence (N);
4332 Ann : Entity_Id := Empty;
4333 Adecl : Node_Id;
4334 Lab_Id : Node_Id;
4335 Lab : Node_Id;
4336 Ldecl : Node_Id;
4337 Ldecl2 : Node_Id;
4339 begin
4340 if Expander_Active then
4342 -- If we have no handled statement sequence, we may need to build
4343 -- a dummy sequence consisting of a null statement. This can be
4344 -- skipped if the trivial accept optimization is permitted.
4346 if not Trivial_Accept_OK
4347 and then
4348 (No (Stats) or else Null_Statements (Statements (Stats)))
4349 then
4350 Set_Handled_Statement_Sequence (N,
4351 Make_Handled_Sequence_Of_Statements (Loc,
4352 New_List (Make_Null_Statement (Loc))));
4353 end if;
4355 -- Create and declare two labels to be placed at the end of the
4356 -- accept statement. The first label is used to allow requeues to
4357 -- skip the remainder of entry processing. The second label is used
4358 -- to skip the remainder of entry processing if the rendezvous
4359 -- completes in the middle of the accept body.
4361 if Present (Handled_Statement_Sequence (N)) then
4362 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
4363 Set_Entity (Lab_Id,
4364 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
4365 Lab := Make_Label (Loc, Lab_Id);
4366 Ldecl :=
4367 Make_Implicit_Label_Declaration (Loc,
4368 Defining_Identifier => Entity (Lab_Id),
4369 Label_Construct => Lab);
4370 Append (Lab, Statements (Handled_Statement_Sequence (N)));
4372 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
4373 Set_Entity (Lab_Id,
4374 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
4375 Lab := Make_Label (Loc, Lab_Id);
4376 Ldecl2 :=
4377 Make_Implicit_Label_Declaration (Loc,
4378 Defining_Identifier => Entity (Lab_Id),
4379 Label_Construct => Lab);
4380 Append (Lab, Statements (Handled_Statement_Sequence (N)));
4382 else
4383 Ldecl := Empty;
4384 Ldecl2 := Empty;
4385 end if;
4387 -- Case of stand alone accept statement
4389 if Is_List_Member (N) then
4391 if Present (Handled_Statement_Sequence (N)) then
4392 Ann :=
4393 Make_Defining_Identifier (Loc,
4394 Chars => New_Internal_Name ('A'));
4396 Adecl :=
4397 Make_Object_Declaration (Loc,
4398 Defining_Identifier => Ann,
4399 Object_Definition =>
4400 New_Reference_To (RTE (RE_Address), Loc));
4402 Insert_Before (N, Adecl);
4403 Analyze (Adecl);
4405 Insert_Before (N, Ldecl);
4406 Analyze (Ldecl);
4408 Insert_Before (N, Ldecl2);
4409 Analyze (Ldecl2);
4410 end if;
4412 -- Case of accept statement which is in an accept alternative
4414 else
4415 declare
4416 Acc_Alt : constant Node_Id := Parent (N);
4417 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
4418 Alt : Node_Id;
4420 begin
4421 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
4422 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
4424 -- ??? Consider a single label for select statements
4426 if Present (Handled_Statement_Sequence (N)) then
4427 Prepend (Ldecl2,
4428 Statements (Handled_Statement_Sequence (N)));
4429 Analyze (Ldecl2);
4431 Prepend (Ldecl,
4432 Statements (Handled_Statement_Sequence (N)));
4433 Analyze (Ldecl);
4434 end if;
4436 -- Find first accept alternative of the selective accept. A
4437 -- valid selective accept must have at least one accept in it.
4439 Alt := First (Select_Alternatives (Sel_Acc));
4441 while Nkind (Alt) /= N_Accept_Alternative loop
4442 Next (Alt);
4443 end loop;
4445 -- If we are the first accept statement, then we have to create
4446 -- the Ann variable, as for the stand alone case, except that
4447 -- it is inserted before the selective accept. Similarly, a
4448 -- label for requeue expansion must be declared.
4450 if N = Accept_Statement (Alt) then
4451 Ann :=
4452 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4454 Adecl :=
4455 Make_Object_Declaration (Loc,
4456 Defining_Identifier => Ann,
4457 Object_Definition =>
4458 New_Reference_To (RTE (RE_Address), Loc));
4460 Insert_Before (Sel_Acc, Adecl);
4461 Analyze (Adecl);
4463 -- If we are not the first accept statement, then find the Ann
4464 -- variable allocated by the first accept and use it.
4466 else
4467 Ann :=
4468 Node (Last_Elmt (Accept_Address
4469 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
4470 end if;
4471 end;
4472 end if;
4474 -- Merge here with Ann either created or referenced, and Adecl
4475 -- pointing to the corresponding declaration. Remaining processing
4476 -- is the same for the two cases.
4478 if Present (Ann) then
4479 Append_Elmt (Ann, Accept_Address (Ent));
4480 Set_Debug_Info_Needed (Ann);
4481 end if;
4483 -- Create renaming declarations for the entry formals. Each reference
4484 -- to a formal becomes a dereference of a component of the parameter
4485 -- block, whose address is held in Ann. These declarations are
4486 -- eventually inserted into the accept block, and analyzed there so
4487 -- that they have the proper scope for gdb and do not conflict with
4488 -- other declarations.
4490 if Present (Parameter_Specifications (N))
4491 and then Present (Handled_Statement_Sequence (N))
4492 then
4493 declare
4494 Comp : Entity_Id;
4495 Decl : Node_Id;
4496 Formal : Entity_Id;
4497 New_F : Entity_Id;
4499 begin
4500 Push_Scope (Ent);
4501 Formal := First_Formal (Ent);
4503 while Present (Formal) loop
4504 Comp := Entry_Component (Formal);
4505 New_F :=
4506 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
4508 Set_Etype (New_F, Etype (Formal));
4509 Set_Scope (New_F, Ent);
4511 -- Now we set debug info needed on New_F even though it does
4512 -- not come from source, so that the debugger will get the
4513 -- right information for these generated names.
4515 Set_Debug_Info_Needed (New_F);
4517 if Ekind (Formal) = E_In_Parameter then
4518 Set_Ekind (New_F, E_Constant);
4519 else
4520 Set_Ekind (New_F, E_Variable);
4521 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
4522 end if;
4524 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
4526 Decl :=
4527 Make_Object_Renaming_Declaration (Loc,
4528 Defining_Identifier =>
4529 New_F,
4530 Subtype_Mark =>
4531 New_Reference_To (Etype (Formal), Loc),
4532 Name =>
4533 Make_Explicit_Dereference (Loc,
4534 Make_Selected_Component (Loc,
4535 Prefix =>
4536 Unchecked_Convert_To (
4537 Entry_Parameters_Type (Ent),
4538 New_Reference_To (Ann, Loc)),
4539 Selector_Name =>
4540 New_Reference_To (Comp, Loc))));
4542 if No (Declarations (N)) then
4543 Set_Declarations (N, New_List);
4544 end if;
4546 Append (Decl, Declarations (N));
4547 Set_Renamed_Object (Formal, New_F);
4548 Next_Formal (Formal);
4549 end loop;
4551 End_Scope;
4552 end;
4553 end if;
4554 end if;
4555 end Expand_Accept_Declarations;
4557 ---------------------------------------------
4558 -- Expand_Access_Protected_Subprogram_Type --
4559 ---------------------------------------------
4561 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
4562 Loc : constant Source_Ptr := Sloc (N);
4563 Comps : List_Id;
4564 T : constant Entity_Id := Defining_Identifier (N);
4565 D_T : constant Entity_Id := Designated_Type (T);
4566 D_T2 : constant Entity_Id := Make_Defining_Identifier (Loc,
4567 Chars => New_Internal_Name ('D'));
4568 E_T : constant Entity_Id := Make_Defining_Identifier (Loc,
4569 Chars => New_Internal_Name ('E'));
4570 P_List : constant List_Id := Build_Protected_Spec
4571 (N, RTE (RE_Address), D_T, False);
4572 Decl1 : Node_Id;
4573 Decl2 : Node_Id;
4574 Def1 : Node_Id;
4576 begin
4577 -- Create access to protected subprogram with full signature
4579 if Nkind (Type_Definition (N)) = N_Access_Function_Definition then
4580 Def1 :=
4581 Make_Access_Function_Definition (Loc,
4582 Parameter_Specifications => P_List,
4583 Result_Definition =>
4584 Copy_Result_Type (Result_Definition (Type_Definition (N))));
4586 else
4587 Def1 :=
4588 Make_Access_Procedure_Definition (Loc,
4589 Parameter_Specifications => P_List);
4590 end if;
4592 Decl1 :=
4593 Make_Full_Type_Declaration (Loc,
4594 Defining_Identifier => D_T2,
4595 Type_Definition => Def1);
4597 Analyze (Decl1);
4598 Insert_After (N, Decl1);
4600 -- Create Equivalent_Type, a record with two components for an access to
4601 -- object and an access to subprogram.
4603 Comps := New_List (
4604 Make_Component_Declaration (Loc,
4605 Defining_Identifier =>
4606 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
4607 Component_Definition =>
4608 Make_Component_Definition (Loc,
4609 Aliased_Present => False,
4610 Subtype_Indication =>
4611 New_Occurrence_Of (RTE (RE_Address), Loc))),
4613 Make_Component_Declaration (Loc,
4614 Defining_Identifier =>
4615 Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
4616 Component_Definition =>
4617 Make_Component_Definition (Loc,
4618 Aliased_Present => False,
4619 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
4621 Decl2 :=
4622 Make_Full_Type_Declaration (Loc,
4623 Defining_Identifier => E_T,
4624 Type_Definition =>
4625 Make_Record_Definition (Loc,
4626 Component_List =>
4627 Make_Component_List (Loc,
4628 Component_Items => Comps)));
4630 Analyze (Decl2);
4631 Insert_After (Decl1, Decl2);
4632 Set_Equivalent_Type (T, E_T);
4633 end Expand_Access_Protected_Subprogram_Type;
4635 --------------------------
4636 -- Expand_Entry_Barrier --
4637 --------------------------
4639 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
4640 Cond : constant Node_Id :=
4641 Condition (Entry_Body_Formal_Part (N));
4642 Prot : constant Entity_Id := Scope (Ent);
4643 Spec_Decl : constant Node_Id := Parent (Prot);
4644 Func : Node_Id;
4645 B_F : Node_Id;
4646 Body_Decl : Node_Id;
4648 begin
4649 if No_Run_Time_Mode then
4650 Error_Msg_CRT ("entry barrier", N);
4651 return;
4652 end if;
4654 -- The body of the entry barrier must be analyzed in the context of the
4655 -- protected object, but its scope is external to it, just as any other
4656 -- unprotected version of a protected operation. The specification has
4657 -- been produced when the protected type declaration was elaborated. We
4658 -- build the body, insert it in the enclosing scope, but analyze it in
4659 -- the current context. A more uniform approach would be to treat the
4660 -- barrier just as a protected function, and discard the protected
4661 -- version of it because it is never called.
4663 if Expander_Active then
4664 B_F := Build_Barrier_Function (N, Ent, Prot);
4665 Func := Barrier_Function (Ent);
4666 Set_Corresponding_Spec (B_F, Func);
4668 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
4670 if Nkind (Parent (Body_Decl)) = N_Subunit then
4671 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
4672 end if;
4674 Insert_Before_And_Analyze (Body_Decl, B_F);
4676 Set_Discriminals (Spec_Decl);
4677 Set_Scope (Func, Scope (Prot));
4679 else
4680 Analyze_And_Resolve (Cond, Any_Boolean);
4681 end if;
4683 -- The Ravenscar profile restricts barriers to simple variables declared
4684 -- within the protected object. We also allow Boolean constants, since
4685 -- these appear in several published examples and are also allowed by
4686 -- the Aonix compiler.
4688 -- Note that after analysis variables in this context will be replaced
4689 -- by the corresponding prival, that is to say a renaming of a selected
4690 -- component of the form _Object.Var. If expansion is disabled, as
4691 -- within a generic, we check that the entity appears in the current
4692 -- scope.
4694 if Is_Entity_Name (Cond) then
4696 -- A small optimization of useless renamings. If the scope of the
4697 -- entity of the condition is not the barrier function, then the
4698 -- condition does not reference any of the generated renamings
4699 -- within the function.
4701 if Expander_Active
4702 and then Scope (Entity (Cond)) /= Func
4703 then
4704 Set_Declarations (B_F, Empty_List);
4705 end if;
4707 if Entity (Cond) = Standard_False
4708 or else
4709 Entity (Cond) = Standard_True
4710 then
4711 return;
4713 elsif not Expander_Active
4714 and then Scope (Entity (Cond)) = Current_Scope
4715 then
4716 return;
4718 -- Check for case of _object.all.field (note that the explicit
4719 -- dereference gets inserted by analyze/expand of _object.field)
4721 elsif Present (Renamed_Object (Entity (Cond)))
4722 and then
4723 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
4724 and then
4725 Chars
4726 (Prefix
4727 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
4728 then
4729 return;
4730 end if;
4731 end if;
4733 -- It is not a boolean variable or literal, so check the restriction
4735 Check_Restriction (Simple_Barriers, Cond);
4736 end Expand_Entry_Barrier;
4738 ------------------------------
4739 -- Expand_N_Abort_Statement --
4740 ------------------------------
4742 -- Expand abort T1, T2, .. Tn; into:
4743 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
4745 procedure Expand_N_Abort_Statement (N : Node_Id) is
4746 Loc : constant Source_Ptr := Sloc (N);
4747 Tlist : constant List_Id := Names (N);
4748 Count : Nat;
4749 Aggr : Node_Id;
4750 Tasknm : Node_Id;
4752 begin
4753 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
4754 Count := 0;
4756 Tasknm := First (Tlist);
4758 while Present (Tasknm) loop
4759 Count := Count + 1;
4761 -- A task interface class-wide type object is being aborted.
4762 -- Retrieve its _task_id by calling a dispatching routine.
4764 if Ada_Version >= Ada_05
4765 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
4766 and then Is_Interface (Etype (Tasknm))
4767 and then Is_Task_Interface (Etype (Tasknm))
4768 then
4769 Append_To (Component_Associations (Aggr),
4770 Make_Component_Association (Loc,
4771 Choices => New_List (
4772 Make_Integer_Literal (Loc, Count)),
4773 Expression =>
4775 -- Task_Id (Tasknm._disp_get_task_id)
4777 Make_Unchecked_Type_Conversion (Loc,
4778 Subtype_Mark =>
4779 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
4780 Expression =>
4781 Make_Selected_Component (Loc,
4782 Prefix =>
4783 New_Copy_Tree (Tasknm),
4784 Selector_Name =>
4785 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
4787 else
4788 Append_To (Component_Associations (Aggr),
4789 Make_Component_Association (Loc,
4790 Choices => New_List (
4791 Make_Integer_Literal (Loc, Count)),
4792 Expression => Concurrent_Ref (Tasknm)));
4793 end if;
4795 Next (Tasknm);
4796 end loop;
4798 Rewrite (N,
4799 Make_Procedure_Call_Statement (Loc,
4800 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
4801 Parameter_Associations => New_List (
4802 Make_Qualified_Expression (Loc,
4803 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
4804 Expression => Aggr))));
4806 Analyze (N);
4807 end Expand_N_Abort_Statement;
4809 -------------------------------
4810 -- Expand_N_Accept_Statement --
4811 -------------------------------
4813 -- This procedure handles expansion of accept statements that stand
4814 -- alone, i.e. they are not part of an accept alternative. The expansion
4815 -- of accept statement in accept alternatives is handled by the routines
4816 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
4817 -- following description applies only to stand alone accept statements.
4819 -- If there is no handled statement sequence, or only null statements,
4820 -- then this is called a trivial accept, and the expansion is:
4822 -- Accept_Trivial (entry-index)
4824 -- If there is a handled statement sequence, then the expansion is:
4826 -- Ann : Address;
4827 -- {Lnn : Label}
4829 -- begin
4830 -- begin
4831 -- Accept_Call (entry-index, Ann);
4832 -- Renaming_Declarations for formals
4833 -- <statement sequence from N_Accept_Statement node>
4834 -- Complete_Rendezvous;
4835 -- <<Lnn>>
4837 -- exception
4838 -- when ... =>
4839 -- <exception handler from N_Accept_Statement node>
4840 -- Complete_Rendezvous;
4841 -- when ... =>
4842 -- <exception handler from N_Accept_Statement node>
4843 -- Complete_Rendezvous;
4844 -- ...
4845 -- end;
4847 -- exception
4848 -- when all others =>
4849 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
4850 -- end;
4852 -- The first three declarations were already inserted ahead of the accept
4853 -- statement by the Expand_Accept_Declarations procedure, which was called
4854 -- directly from the semantics during analysis of the accept statement,
4855 -- before analyzing its contained statements.
4857 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
4858 -- from possible expansion activity (the original source of course does
4859 -- not have any declarations associated with the accept statement, since
4860 -- an accept statement has no declarative part). In particular, if the
4861 -- expander is active, the first such declaration is the declaration of
4862 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
4864 -- The two blocks are merged into a single block if the inner block has
4865 -- no exception handlers, but otherwise two blocks are required, since
4866 -- exceptions might be raised in the exception handlers of the inner
4867 -- block, and Exceptional_Complete_Rendezvous must be called.
4869 procedure Expand_N_Accept_Statement (N : Node_Id) is
4870 Loc : constant Source_Ptr := Sloc (N);
4871 Stats : constant Node_Id := Handled_Statement_Sequence (N);
4872 Ename : constant Node_Id := Entry_Direct_Name (N);
4873 Eindx : constant Node_Id := Entry_Index (N);
4874 Eent : constant Entity_Id := Entity (Ename);
4875 Acstack : constant Elist_Id := Accept_Address (Eent);
4876 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
4877 Ttyp : constant Entity_Id := Etype (Scope (Eent));
4878 Blkent : Entity_Id;
4879 Call : Node_Id;
4880 Block : Node_Id;
4882 -- Start of processing for Expand_N_Accept_Statement
4884 begin
4885 -- If accept statement is not part of a list, then its parent must be
4886 -- an accept alternative, and, as described above, we do not do any
4887 -- expansion for such accept statements at this level.
4889 if not Is_List_Member (N) then
4890 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
4891 return;
4893 -- Trivial accept case (no statement sequence, or null statements).
4894 -- If the accept statement has declarations, then just insert them
4895 -- before the procedure call.
4897 elsif Trivial_Accept_OK
4898 and then (No (Stats) or else Null_Statements (Statements (Stats)))
4899 then
4900 -- Remove declarations for renamings, because the parameter block
4901 -- will not be assigned.
4903 declare
4904 D : Node_Id;
4905 Next_D : Node_Id;
4907 begin
4908 D := First (Declarations (N));
4910 while Present (D) loop
4911 Next_D := Next (D);
4912 if Nkind (D) = N_Object_Renaming_Declaration then
4913 Remove (D);
4914 end if;
4916 D := Next_D;
4917 end loop;
4918 end;
4920 if Present (Declarations (N)) then
4921 Insert_Actions (N, Declarations (N));
4922 end if;
4924 Rewrite (N,
4925 Make_Procedure_Call_Statement (Loc,
4926 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
4927 Parameter_Associations => New_List (
4928 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
4930 Analyze (N);
4932 -- Discard Entry_Address that was created for it, so it will not be
4933 -- emitted if this accept statement is in the statement part of a
4934 -- delay alternative.
4936 if Present (Stats) then
4937 Remove_Last_Elmt (Acstack);
4938 end if;
4940 -- Case of statement sequence present
4942 else
4943 -- Construct the block, using the declarations from the accept
4944 -- statement if any to initialize the declarations of the block.
4946 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4947 Set_Ekind (Blkent, E_Block);
4948 Set_Etype (Blkent, Standard_Void_Type);
4949 Set_Scope (Blkent, Current_Scope);
4951 Block :=
4952 Make_Block_Statement (Loc,
4953 Identifier => New_Reference_To (Blkent, Loc),
4954 Declarations => Declarations (N),
4955 Handled_Statement_Sequence => Build_Accept_Body (N));
4957 -- Prepend call to Accept_Call to main statement sequence If the
4958 -- accept has exception handlers, the statement sequence is wrapped
4959 -- in a block. Insert call and renaming declarations in the
4960 -- declarations of the block, so they are elaborated before the
4961 -- handlers.
4963 Call :=
4964 Make_Procedure_Call_Statement (Loc,
4965 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
4966 Parameter_Associations => New_List (
4967 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
4968 New_Reference_To (Ann, Loc)));
4970 if Parent (Stats) = N then
4971 Prepend (Call, Statements (Stats));
4972 else
4973 Set_Declarations
4974 (Parent (Stats),
4975 New_List (Call));
4976 end if;
4978 Analyze (Call);
4980 Push_Scope (Blkent);
4982 declare
4983 D : Node_Id;
4984 Next_D : Node_Id;
4985 Typ : Entity_Id;
4987 begin
4988 D := First (Declarations (N));
4989 while Present (D) loop
4990 Next_D := Next (D);
4992 if Nkind (D) = N_Object_Renaming_Declaration then
4994 -- The renaming declarations for the formals were created
4995 -- during analysis of the accept statement, and attached to
4996 -- the list of declarations. Place them now in the context
4997 -- of the accept block or subprogram.
4999 Remove (D);
5000 Typ := Entity (Subtype_Mark (D));
5001 Insert_After (Call, D);
5002 Analyze (D);
5004 -- If the formal is class_wide, it does not have an actual
5005 -- subtype. The analysis of the renaming declaration creates
5006 -- one, but we need to retain the class-wide nature of the
5007 -- entity.
5009 if Is_Class_Wide_Type (Typ) then
5010 Set_Etype (Defining_Identifier (D), Typ);
5011 end if;
5013 end if;
5015 D := Next_D;
5016 end loop;
5017 end;
5019 End_Scope;
5021 -- Replace the accept statement by the new block
5023 Rewrite (N, Block);
5024 Analyze (N);
5026 -- Last step is to unstack the Accept_Address value
5028 Remove_Last_Elmt (Acstack);
5029 end if;
5030 end Expand_N_Accept_Statement;
5032 ----------------------------------
5033 -- Expand_N_Asynchronous_Select --
5034 ----------------------------------
5036 -- This procedure assumes that the trigger statement is an entry call or
5037 -- a dispatching procedure call. A delay alternative should already have
5038 -- been expanded into an entry call to the appropriate delay object Wait
5039 -- entry.
5041 -- If the trigger is a task entry call, the select is implemented with
5042 -- a Task_Entry_Call:
5044 -- declare
5045 -- B : Boolean;
5046 -- C : Boolean;
5047 -- P : parms := (parm, parm, parm);
5049 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5051 -- procedure _clean is
5052 -- begin
5053 -- ...
5054 -- Cancel_Task_Entry_Call (C);
5055 -- ...
5056 -- end _clean;
5058 -- begin
5059 -- Abort_Defer;
5060 -- Task_Entry_Call
5061 -- (<acceptor-task>, -- Acceptor
5062 -- <entry-index>, -- E
5063 -- P'Address, -- Uninterpreted_Data
5064 -- Asynchronous_Call, -- Mode
5065 -- B); -- Rendezvous_Successful
5067 -- begin
5068 -- begin
5069 -- Abort_Undefer;
5070 -- <abortable-part>
5071 -- at end
5072 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5073 -- end;
5074 -- exception
5075 -- when Abort_Signal => Abort_Undefer;
5076 -- end;
5078 -- parm := P.param;
5079 -- parm := P.param;
5080 -- ...
5081 -- if not C then
5082 -- <triggered-statements>
5083 -- end if;
5084 -- end;
5086 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
5087 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
5088 -- as follows:
5090 -- declare
5091 -- P : parms := (parm, parm, parm);
5092 -- begin
5093 -- Call_Simple (acceptor-task, entry-index, P'Address);
5094 -- parm := P.param;
5095 -- parm := P.param;
5096 -- ...
5097 -- end;
5099 -- so the task at hand is to convert the latter expansion into the former
5101 -- If the trigger is a protected entry call, the select is implemented
5102 -- with Protected_Entry_Call:
5104 -- declare
5105 -- P : E1_Params := (param, param, param);
5106 -- Bnn : Communications_Block;
5108 -- begin
5109 -- declare
5111 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5113 -- procedure _clean is
5114 -- begin
5115 -- ...
5116 -- if Enqueued (Bnn) then
5117 -- Cancel_Protected_Entry_Call (Bnn);
5118 -- end if;
5119 -- ...
5120 -- end _clean;
5122 -- begin
5123 -- begin
5124 -- Protected_Entry_Call
5125 -- (po._object'Access, -- Object
5126 -- <entry index>, -- E
5127 -- P'Address, -- Uninterpreted_Data
5128 -- Asynchronous_Call, -- Mode
5129 -- Bnn); -- Block
5131 -- if Enqueued (Bnn) then
5132 -- <abortable-part>
5133 -- end if;
5134 -- at end
5135 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5136 -- end;
5137 -- exception
5138 -- when Abort_Signal => Abort_Undefer;
5139 -- end;
5141 -- if not Cancelled (Bnn) then
5142 -- <triggered-statements>
5143 -- end if;
5144 -- end;
5146 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
5147 -- entry call:
5149 -- declare
5150 -- P : E1_Params := (param, param, param);
5151 -- Bnn : Communications_Block;
5153 -- begin
5154 -- Protected_Entry_Call
5155 -- (po._object'Access, -- Object
5156 -- <entry index>, -- E
5157 -- P'Address, -- Uninterpreted_Data
5158 -- Simple_Call, -- Mode
5159 -- Bnn); -- Block
5160 -- parm := P.param;
5161 -- parm := P.param;
5162 -- ...
5163 -- end;
5165 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
5166 -- expanded into:
5168 -- declare
5169 -- B : Boolean := False;
5170 -- Bnn : Communication_Block;
5171 -- C : Ada.Tags.Prim_Op_Kind;
5172 -- D : System.Storage_Elements.Dummy_Communication_Block;
5173 -- K : Ada.Tags.Tagged_Kind :=
5174 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
5175 -- P : Parameters := (Param1 .. ParamN);
5176 -- S : Integer;
5177 -- U : Boolean;
5179 -- begin
5180 -- if K = Ada.Tags.TK_Limited_Tagged then
5181 -- <dispatching-call>;
5182 -- <triggering-statements>;
5184 -- else
5185 -- S :=
5186 -- Ada.Tags.Get_Offset_Index
5187 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
5189 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
5191 -- if C = POK_Protected_Entry then
5192 -- declare
5193 -- procedure _clean is
5194 -- begin
5195 -- if Enqueued (Bnn) then
5196 -- Cancel_Protected_Entry_Call (Bnn);
5197 -- end if;
5198 -- end _clean;
5200 -- begin
5201 -- begin
5202 -- _Disp_Asynchronous_Select
5203 -- (<object>, S, P'Address, D, B);
5204 -- Bnn := Communication_Block (D);
5206 -- Param1 := P.Param1;
5207 -- ...
5208 -- ParamN := P.ParamN;
5210 -- if Enqueued (Bnn) then
5211 -- <abortable-statements>
5212 -- end if;
5213 -- at end
5214 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5215 -- end;
5216 -- exception
5217 -- when Abort_Signal => Abort_Undefer;
5218 -- end;
5220 -- if not Cancelled (Bnn) then
5221 -- <triggering-statements>
5222 -- end if;
5224 -- elsif C = POK_Task_Entry then
5225 -- declare
5226 -- procedure _clean is
5227 -- begin
5228 -- Cancel_Task_Entry_Call (U);
5229 -- end _clean;
5231 -- begin
5232 -- Abort_Defer;
5234 -- _Disp_Asynchronous_Select
5235 -- (<object>, S, P'Address, D, B);
5236 -- Bnn := Communication_Bloc (D);
5238 -- Param1 := P.Param1;
5239 -- ...
5240 -- ParamN := P.ParamN;
5242 -- begin
5243 -- begin
5244 -- Abort_Undefer;
5245 -- <abortable-statements>
5246 -- at end
5247 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5248 -- end;
5249 -- exception
5250 -- when Abort_Signal => Abort_Undefer;
5251 -- end;
5253 -- if not U then
5254 -- <triggering-statements>
5255 -- end if;
5256 -- end;
5258 -- else
5259 -- <dispatching-call>;
5260 -- <triggering-statements>
5261 -- end if;
5262 -- end if;
5263 -- end;
5265 -- The job is to convert this to the asynchronous form
5267 -- If the trigger is a delay statement, it will have been expanded into a
5268 -- call to one of the GNARL delay procedures. This routine will convert
5269 -- this into a protected entry call on a delay object and then continue
5270 -- processing as for a protected entry call trigger. This requires
5271 -- declaring a Delay_Block object and adding a pointer to this object to
5272 -- the parameter list of the delay procedure to form the parameter list of
5273 -- the entry call. This object is used by the runtime to queue the delay
5274 -- request.
5276 -- For a description of the use of P and the assignments after the call,
5277 -- see Expand_N_Entry_Call_Statement.
5279 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
5280 Loc : constant Source_Ptr := Sloc (N);
5281 Abrt : constant Node_Id := Abortable_Part (N);
5282 Astats : constant List_Id := Statements (Abrt);
5283 Trig : constant Node_Id := Triggering_Alternative (N);
5284 Tstats : constant List_Id := Statements (Trig);
5286 Abort_Block_Ent : Entity_Id;
5287 Abortable_Block : Node_Id;
5288 Actuals : List_Id;
5289 Blk_Ent : Entity_Id;
5290 Blk_Typ : Entity_Id;
5291 Call : Node_Id;
5292 Call_Ent : Entity_Id;
5293 Cancel_Param : Entity_Id;
5294 Cleanup_Block : Node_Id;
5295 Cleanup_Block_Ent : Entity_Id;
5296 Cleanup_Stmts : List_Id;
5297 Conc_Typ_Stmts : List_Id;
5298 Concval : Node_Id;
5299 Dblock_Ent : Entity_Id;
5300 Decl : Node_Id;
5301 Decls : List_Id;
5302 Ecall : Node_Id;
5303 Ename : Node_Id;
5304 Enqueue_Call : Node_Id;
5305 Formals : List_Id;
5306 Hdle : List_Id;
5307 Index : Node_Id;
5308 Lim_Typ_Stmts : List_Id;
5309 N_Orig : Node_Id;
5310 Obj : Entity_Id;
5311 Param : Node_Id;
5312 Params : List_Id;
5313 Pdef : Entity_Id;
5314 ProtE_Stmts : List_Id;
5315 ProtP_Stmts : List_Id;
5316 Stmt : Node_Id;
5317 Stmts : List_Id;
5318 Target_Undefer : RE_Id;
5319 TaskE_Stmts : List_Id;
5320 Undefer_Args : List_Id := No_List;
5322 B : Entity_Id; -- Call status flag
5323 Bnn : Entity_Id; -- Communication block
5324 C : Entity_Id; -- Call kind
5325 K : Entity_Id; -- Tagged kind
5326 P : Entity_Id; -- Parameter block
5327 S : Entity_Id; -- Primitive operation slot
5328 T : Entity_Id; -- Additional status flag
5330 begin
5331 Blk_Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5332 Ecall := Triggering_Statement (Trig);
5334 -- The arguments in the call may require dynamic allocation, and the
5335 -- call statement may have been transformed into a block. The block
5336 -- may contain additional declarations for internal entities, and the
5337 -- original call is found by sequential search.
5339 if Nkind (Ecall) = N_Block_Statement then
5340 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
5341 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
5342 N_Entry_Call_Statement)
5343 loop
5344 Next (Ecall);
5345 end loop;
5346 end if;
5348 -- This is either a dispatching call or a delay statement used as a
5349 -- trigger which was expanded into a procedure call.
5351 if Nkind (Ecall) = N_Procedure_Call_Statement then
5352 if Ada_Version >= Ada_05
5353 and then
5354 (No (Original_Node (Ecall))
5355 or else not Nkind_In (Original_Node (Ecall),
5356 N_Delay_Relative_Statement,
5357 N_Delay_Until_Statement))
5358 then
5359 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
5361 Decls := New_List;
5362 Stmts := New_List;
5364 -- Call status flag processing, generate:
5365 -- B : Boolean := False;
5367 B := Build_B (Loc, Decls);
5369 -- Communication block processing, generate:
5370 -- Bnn : Communication_Block;
5372 Bnn := Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
5374 Append_To (Decls,
5375 Make_Object_Declaration (Loc,
5376 Defining_Identifier =>
5377 Bnn,
5378 Object_Definition =>
5379 New_Reference_To (RTE (RE_Communication_Block), Loc)));
5381 -- Call kind processing, generate:
5382 -- C : Ada.Tags.Prim_Op_Kind;
5384 C := Build_C (Loc, Decls);
5386 -- Tagged kind processing, generate:
5387 -- K : Ada.Tags.Tagged_Kind :=
5388 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
5390 -- Dummy communication block, generate:
5391 -- D : Dummy_Communication_Block;
5393 Append_To (Decls,
5394 Make_Object_Declaration (Loc,
5395 Defining_Identifier =>
5396 Make_Defining_Identifier (Loc, Name_uD),
5397 Object_Definition =>
5398 New_Reference_To (
5399 RTE (RE_Dummy_Communication_Block), Loc)));
5401 K := Build_K (Loc, Decls, Obj);
5403 -- Parameter block processing
5405 Blk_Typ := Build_Parameter_Block
5406 (Loc, Actuals, Formals, Decls);
5407 P := Parameter_Block_Pack
5408 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
5410 -- Dispatch table slot processing, generate:
5411 -- S : Integer;
5413 S := Build_S (Loc, Decls);
5415 -- Additional status flag processing, generate:
5417 T := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
5419 Append_To (Decls,
5420 Make_Object_Declaration (Loc,
5421 Defining_Identifier =>
5423 Object_Definition =>
5424 New_Reference_To (Standard_Boolean, Loc)));
5426 ------------------------------
5427 -- Protected entry handling --
5428 ------------------------------
5430 -- Generate:
5431 -- Param1 := P.Param1;
5432 -- ...
5433 -- ParamN := P.ParamN;
5435 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
5437 -- Generate:
5438 -- Bnn := Communication_Block (D);
5440 Prepend_To (Cleanup_Stmts,
5441 Make_Assignment_Statement (Loc,
5442 Name =>
5443 New_Reference_To (Bnn, Loc),
5444 Expression =>
5445 Make_Unchecked_Type_Conversion (Loc,
5446 Subtype_Mark =>
5447 New_Reference_To (RTE (RE_Communication_Block), Loc),
5448 Expression =>
5449 Make_Identifier (Loc, Name_uD))));
5451 -- Generate:
5452 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
5454 Prepend_To (Cleanup_Stmts,
5455 Make_Procedure_Call_Statement (Loc,
5456 Name =>
5457 New_Reference_To (
5458 Find_Prim_Op (Etype (Etype (Obj)),
5459 Name_uDisp_Asynchronous_Select),
5460 Loc),
5461 Parameter_Associations =>
5462 New_List (
5463 New_Copy_Tree (Obj), -- <object>
5464 New_Reference_To (S, Loc), -- S
5465 Make_Attribute_Reference (Loc, -- P'Address
5466 Prefix =>
5467 New_Reference_To (P, Loc),
5468 Attribute_Name =>
5469 Name_Address),
5470 Make_Identifier (Loc, Name_uD), -- D
5471 New_Reference_To (B, Loc)))); -- B
5473 -- Generate:
5474 -- if Enqueued (Bnn) then
5475 -- <abortable-statements>
5476 -- end if;
5478 Append_To (Cleanup_Stmts,
5479 Make_If_Statement (Loc,
5480 Condition =>
5481 Make_Function_Call (Loc,
5482 Name =>
5483 New_Reference_To (RTE (RE_Enqueued), Loc),
5484 Parameter_Associations =>
5485 New_List (
5486 New_Reference_To (Bnn, Loc))),
5488 Then_Statements =>
5489 New_Copy_List_Tree (Astats)));
5491 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
5492 -- will then generate a _clean for the communication block Bnn.
5494 -- Generate:
5495 -- declare
5496 -- procedure _clean is
5497 -- begin
5498 -- if Enqueued (Bnn) then
5499 -- Cancel_Protected_Entry_Call (Bnn);
5500 -- end if;
5501 -- end _clean;
5502 -- begin
5503 -- Cleanup_Stmts
5504 -- at end
5505 -- _clean;
5506 -- end;
5508 Cleanup_Block_Ent :=
5509 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
5511 Cleanup_Block :=
5512 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
5514 -- Wrap the cleanup block in an exception handling block
5516 -- Generate:
5517 -- begin
5518 -- Cleanup_Block
5519 -- exception
5520 -- when Abort_Signal => Abort_Undefer;
5521 -- end;
5523 Abort_Block_Ent :=
5524 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5526 ProtE_Stmts :=
5527 New_List (
5528 Make_Implicit_Label_Declaration (Loc,
5529 Defining_Identifier =>
5530 Abort_Block_Ent),
5532 Build_Abort_Block
5533 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
5535 -- Generate:
5536 -- if not Cancelled (Bnn) then
5537 -- <triggering-statements>
5538 -- end if;
5540 Append_To (ProtE_Stmts,
5541 Make_If_Statement (Loc,
5542 Condition =>
5543 Make_Op_Not (Loc,
5544 Right_Opnd =>
5545 Make_Function_Call (Loc,
5546 Name =>
5547 New_Reference_To (RTE (RE_Cancelled), Loc),
5548 Parameter_Associations =>
5549 New_List (
5550 New_Reference_To (Bnn, Loc)))),
5552 Then_Statements =>
5553 New_Copy_List_Tree (Tstats)));
5555 -------------------------
5556 -- Task entry handling --
5557 -------------------------
5559 -- Generate:
5560 -- Param1 := P.Param1;
5561 -- ...
5562 -- ParamN := P.ParamN;
5564 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
5566 -- Generate:
5567 -- Bnn := Communication_Block (D);
5569 Append_To (TaskE_Stmts,
5570 Make_Assignment_Statement (Loc,
5571 Name =>
5572 New_Reference_To (Bnn, Loc),
5573 Expression =>
5574 Make_Unchecked_Type_Conversion (Loc,
5575 Subtype_Mark =>
5576 New_Reference_To (RTE (RE_Communication_Block), Loc),
5577 Expression =>
5578 Make_Identifier (Loc, Name_uD))));
5580 -- Generate:
5581 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
5583 Prepend_To (TaskE_Stmts,
5584 Make_Procedure_Call_Statement (Loc,
5585 Name =>
5586 New_Reference_To (
5587 Find_Prim_Op (Etype (Etype (Obj)),
5588 Name_uDisp_Asynchronous_Select),
5589 Loc),
5590 Parameter_Associations =>
5591 New_List (
5592 New_Copy_Tree (Obj), -- <object>
5593 New_Reference_To (S, Loc), -- S
5594 Make_Attribute_Reference (Loc, -- P'Address
5595 Prefix =>
5596 New_Reference_To (P, Loc),
5597 Attribute_Name =>
5598 Name_Address),
5599 Make_Identifier (Loc, Name_uD), -- D
5600 New_Reference_To (B, Loc)))); -- B
5602 -- Generate:
5603 -- Abort_Defer;
5605 Prepend_To (TaskE_Stmts,
5606 Make_Procedure_Call_Statement (Loc,
5607 Name =>
5608 New_Reference_To (RTE (RE_Abort_Defer), Loc),
5609 Parameter_Associations =>
5610 No_List));
5612 -- Generate:
5613 -- Abort_Undefer;
5614 -- <abortable-statements>
5616 Cleanup_Stmts := New_Copy_List_Tree (Astats);
5618 Prepend_To (Cleanup_Stmts,
5619 Make_Procedure_Call_Statement (Loc,
5620 Name =>
5621 New_Reference_To (RTE (RE_Abort_Undefer), Loc),
5622 Parameter_Associations =>
5623 No_List));
5625 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
5626 -- will generate a _clean for the additional status flag.
5628 -- Generate:
5629 -- declare
5630 -- procedure _clean is
5631 -- begin
5632 -- Cancel_Task_Entry_Call (U);
5633 -- end _clean;
5634 -- begin
5635 -- Cleanup_Stmts
5636 -- at end
5637 -- _clean;
5638 -- end;
5640 Cleanup_Block_Ent :=
5641 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
5643 Cleanup_Block :=
5644 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
5646 -- Wrap the cleanup block in an exception handling block
5648 -- Generate:
5649 -- begin
5650 -- Cleanup_Block
5651 -- exception
5652 -- when Abort_Signal => Abort_Undefer;
5653 -- end;
5655 Abort_Block_Ent :=
5656 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5658 Append_To (TaskE_Stmts,
5659 Make_Implicit_Label_Declaration (Loc,
5660 Defining_Identifier =>
5661 Abort_Block_Ent));
5663 Append_To (TaskE_Stmts,
5664 Build_Abort_Block
5665 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
5667 -- Generate:
5668 -- if not T then
5669 -- <triggering-statements>
5670 -- end if;
5672 Append_To (TaskE_Stmts,
5673 Make_If_Statement (Loc,
5674 Condition =>
5675 Make_Op_Not (Loc,
5676 Right_Opnd =>
5677 New_Reference_To (T, Loc)),
5679 Then_Statements =>
5680 New_Copy_List_Tree (Tstats)));
5682 ----------------------------------
5683 -- Protected procedure handling --
5684 ----------------------------------
5686 -- Generate:
5687 -- <dispatching-call>;
5688 -- <triggering-statements>
5690 ProtP_Stmts := New_Copy_List_Tree (Tstats);
5691 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
5693 -- Generate:
5694 -- S := Ada.Tags.Get_Offset_Index
5695 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
5697 Conc_Typ_Stmts :=
5698 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
5700 -- Generate:
5701 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
5703 Append_To (Conc_Typ_Stmts,
5704 Make_Procedure_Call_Statement (Loc,
5705 Name =>
5706 New_Reference_To (
5707 Find_Prim_Op (Etype (Etype (Obj)),
5708 Name_uDisp_Get_Prim_Op_Kind),
5709 Loc),
5710 Parameter_Associations =>
5711 New_List (
5712 New_Copy_Tree (Obj),
5713 New_Reference_To (S, Loc),
5714 New_Reference_To (C, Loc))));
5716 -- Generate:
5717 -- if C = POK_Procedure_Entry then
5718 -- ProtE_Stmts
5719 -- elsif C = POK_Task_Entry then
5720 -- TaskE_Stmts
5721 -- else
5722 -- ProtP_Stmts
5723 -- end if;
5725 Append_To (Conc_Typ_Stmts,
5726 Make_If_Statement (Loc,
5727 Condition =>
5728 Make_Op_Eq (Loc,
5729 Left_Opnd =>
5730 New_Reference_To (C, Loc),
5731 Right_Opnd =>
5732 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
5734 Then_Statements =>
5735 ProtE_Stmts,
5737 Elsif_Parts =>
5738 New_List (
5739 Make_Elsif_Part (Loc,
5740 Condition =>
5741 Make_Op_Eq (Loc,
5742 Left_Opnd =>
5743 New_Reference_To (C, Loc),
5744 Right_Opnd =>
5745 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
5747 Then_Statements =>
5748 TaskE_Stmts)),
5750 Else_Statements =>
5751 ProtP_Stmts));
5753 -- Generate:
5754 -- <dispatching-call>;
5755 -- <triggering-statements>
5757 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
5758 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
5760 -- Generate:
5761 -- if K = Ada.Tags.TK_Limited_Tagged then
5762 -- Lim_Typ_Stmts
5763 -- else
5764 -- Conc_Typ_Stmts
5765 -- end if;
5767 Append_To (Stmts,
5768 Make_If_Statement (Loc,
5769 Condition =>
5770 Make_Op_Eq (Loc,
5771 Left_Opnd =>
5772 New_Reference_To (K, Loc),
5773 Right_Opnd =>
5774 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
5776 Then_Statements =>
5777 Lim_Typ_Stmts,
5779 Else_Statements =>
5780 Conc_Typ_Stmts));
5782 Rewrite (N,
5783 Make_Block_Statement (Loc,
5784 Declarations =>
5785 Decls,
5786 Handled_Statement_Sequence =>
5787 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
5789 Analyze (N);
5790 return;
5792 -- Delay triggering statement processing
5794 else
5795 -- Add a Delay_Block object to the parameter list of the delay
5796 -- procedure to form the parameter list of the Wait entry call.
5798 Dblock_Ent :=
5799 Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
5801 Pdef := Entity (Name (Ecall));
5803 if Is_RTE (Pdef, RO_CA_Delay_For) then
5804 Enqueue_Call :=
5805 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
5807 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
5808 Enqueue_Call :=
5809 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
5811 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
5812 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
5813 end if;
5815 Append_To (Parameter_Associations (Ecall),
5816 Make_Attribute_Reference (Loc,
5817 Prefix => New_Reference_To (Dblock_Ent, Loc),
5818 Attribute_Name => Name_Unchecked_Access));
5820 -- Create the inner block to protect the abortable part
5822 Hdle := New_List (
5823 Make_Implicit_Exception_Handler (Loc,
5824 Exception_Choices =>
5825 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
5826 Statements => New_List (
5827 Make_Procedure_Call_Statement (Loc,
5828 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
5830 Prepend_To (Astats,
5831 Make_Procedure_Call_Statement (Loc,
5832 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
5834 Abortable_Block :=
5835 Make_Block_Statement (Loc,
5836 Identifier => New_Reference_To (Blk_Ent, Loc),
5837 Handled_Statement_Sequence =>
5838 Make_Handled_Sequence_Of_Statements (Loc,
5839 Statements => Astats),
5840 Has_Created_Identifier => True,
5841 Is_Asynchronous_Call_Block => True);
5843 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
5845 Rewrite (Ecall,
5846 Make_Implicit_If_Statement (N,
5847 Condition => Make_Function_Call (Loc,
5848 Name => Enqueue_Call,
5849 Parameter_Associations => Parameter_Associations (Ecall)),
5850 Then_Statements =>
5851 New_List (Make_Block_Statement (Loc,
5852 Handled_Statement_Sequence =>
5853 Make_Handled_Sequence_Of_Statements (Loc,
5854 Statements => New_List (
5855 Make_Implicit_Label_Declaration (Loc,
5856 Defining_Identifier => Blk_Ent,
5857 Label_Construct => Abortable_Block),
5858 Abortable_Block),
5859 Exception_Handlers => Hdle)))));
5861 Stmts := New_List (Ecall);
5863 -- Construct statement sequence for new block
5865 Append_To (Stmts,
5866 Make_Implicit_If_Statement (N,
5867 Condition => Make_Function_Call (Loc,
5868 Name => New_Reference_To (
5869 RTE (RE_Timed_Out), Loc),
5870 Parameter_Associations => New_List (
5871 Make_Attribute_Reference (Loc,
5872 Prefix => New_Reference_To (Dblock_Ent, Loc),
5873 Attribute_Name => Name_Unchecked_Access))),
5874 Then_Statements => Tstats));
5876 -- The result is the new block
5878 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
5880 Rewrite (N,
5881 Make_Block_Statement (Loc,
5882 Declarations => New_List (
5883 Make_Object_Declaration (Loc,
5884 Defining_Identifier => Dblock_Ent,
5885 Aliased_Present => True,
5886 Object_Definition => New_Reference_To (
5887 RTE (RE_Delay_Block), Loc))),
5889 Handled_Statement_Sequence =>
5890 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
5892 Analyze (N);
5893 return;
5894 end if;
5896 else
5897 N_Orig := N;
5898 end if;
5900 Extract_Entry (Ecall, Concval, Ename, Index);
5901 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
5903 Stmts := Statements (Handled_Statement_Sequence (Ecall));
5904 Decls := Declarations (Ecall);
5906 if Is_Protected_Type (Etype (Concval)) then
5908 -- Get the declarations of the block expanded from the entry call
5910 Decl := First (Decls);
5911 while Present (Decl)
5912 and then
5913 (Nkind (Decl) /= N_Object_Declaration
5914 or else not Is_RTE (Etype (Object_Definition (Decl)),
5915 RE_Communication_Block))
5916 loop
5917 Next (Decl);
5918 end loop;
5920 pragma Assert (Present (Decl));
5921 Cancel_Param := Defining_Identifier (Decl);
5923 -- Change the mode of the Protected_Entry_Call call
5925 -- Protected_Entry_Call (
5926 -- Object => po._object'Access,
5927 -- E => <entry index>;
5928 -- Uninterpreted_Data => P'Address;
5929 -- Mode => Asynchronous_Call;
5930 -- Block => Bnn);
5932 Stmt := First (Stmts);
5934 -- Skip assignments to temporaries created for in-out parameters
5936 -- This makes unwarranted assumptions about the shape of the expanded
5937 -- tree for the call, and should be cleaned up ???
5939 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
5940 Next (Stmt);
5941 end loop;
5943 Call := Stmt;
5945 Param := First (Parameter_Associations (Call));
5946 while Present (Param)
5947 and then not Is_RTE (Etype (Param), RE_Call_Modes)
5948 loop
5949 Next (Param);
5950 end loop;
5952 pragma Assert (Present (Param));
5953 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
5954 Analyze (Param);
5956 -- Append an if statement to execute the abortable part
5958 -- Generate:
5959 -- if Enqueued (Bnn) then
5961 Append_To (Stmts,
5962 Make_Implicit_If_Statement (N,
5963 Condition => Make_Function_Call (Loc,
5964 Name => New_Reference_To (
5965 RTE (RE_Enqueued), Loc),
5966 Parameter_Associations => New_List (
5967 New_Reference_To (Cancel_Param, Loc))),
5968 Then_Statements => Astats));
5970 Abortable_Block :=
5971 Make_Block_Statement (Loc,
5972 Identifier => New_Reference_To (Blk_Ent, Loc),
5973 Handled_Statement_Sequence =>
5974 Make_Handled_Sequence_Of_Statements (Loc,
5975 Statements => Stmts),
5976 Has_Created_Identifier => True,
5977 Is_Asynchronous_Call_Block => True);
5979 -- For the VM call Update_Exception instead of Abort_Undefer.
5980 -- See 4jexcept.ads for an explanation.
5982 if VM_Target = No_VM then
5983 Target_Undefer := RE_Abort_Undefer;
5984 else
5985 Target_Undefer := RE_Update_Exception;
5986 Undefer_Args :=
5987 New_List (Make_Function_Call (Loc,
5988 Name => New_Occurrence_Of
5989 (RTE (RE_Current_Target_Exception), Loc)));
5990 end if;
5992 Stmts := New_List (
5993 Make_Block_Statement (Loc,
5994 Handled_Statement_Sequence =>
5995 Make_Handled_Sequence_Of_Statements (Loc,
5996 Statements => New_List (
5997 Make_Implicit_Label_Declaration (Loc,
5998 Defining_Identifier => Blk_Ent,
5999 Label_Construct => Abortable_Block),
6000 Abortable_Block),
6002 -- exception
6004 Exception_Handlers => New_List (
6005 Make_Implicit_Exception_Handler (Loc,
6007 -- when Abort_Signal =>
6008 -- Abort_Undefer.all;
6010 Exception_Choices =>
6011 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
6012 Statements => New_List (
6013 Make_Procedure_Call_Statement (Loc,
6014 Name => New_Reference_To (
6015 RTE (Target_Undefer), Loc),
6016 Parameter_Associations => Undefer_Args)))))),
6018 -- if not Cancelled (Bnn) then
6019 -- triggered statements
6020 -- end if;
6022 Make_Implicit_If_Statement (N,
6023 Condition => Make_Op_Not (Loc,
6024 Right_Opnd =>
6025 Make_Function_Call (Loc,
6026 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
6027 Parameter_Associations => New_List (
6028 New_Occurrence_Of (Cancel_Param, Loc)))),
6029 Then_Statements => Tstats));
6031 -- Asynchronous task entry call
6033 else
6034 if No (Decls) then
6035 Decls := New_List;
6036 end if;
6038 B := Make_Defining_Identifier (Loc, Name_uB);
6040 -- Insert declaration of B in declarations of existing block
6042 Prepend_To (Decls,
6043 Make_Object_Declaration (Loc,
6044 Defining_Identifier => B,
6045 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6047 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
6049 -- Insert declaration of C in declarations of existing block
6051 Prepend_To (Decls,
6052 Make_Object_Declaration (Loc,
6053 Defining_Identifier => Cancel_Param,
6054 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6056 -- Remove and save the call to Call_Simple
6058 Stmt := First (Stmts);
6060 -- Skip assignments to temporaries created for in-out parameters.
6061 -- This makes unwarranted assumptions about the shape of the expanded
6062 -- tree for the call, and should be cleaned up ???
6064 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6065 Next (Stmt);
6066 end loop;
6068 Call := Stmt;
6070 -- Create the inner block to protect the abortable part
6072 Hdle := New_List (
6073 Make_Implicit_Exception_Handler (Loc,
6074 Exception_Choices =>
6075 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
6076 Statements =>
6077 New_List (
6078 Make_Procedure_Call_Statement (Loc,
6079 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
6081 Prepend_To (Astats,
6082 Make_Procedure_Call_Statement (Loc,
6083 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
6085 Abortable_Block :=
6086 Make_Block_Statement (Loc,
6087 Identifier => New_Reference_To (Blk_Ent, Loc),
6088 Handled_Statement_Sequence =>
6089 Make_Handled_Sequence_Of_Statements (Loc,
6090 Statements => Astats),
6091 Has_Created_Identifier => True,
6092 Is_Asynchronous_Call_Block => True);
6094 Insert_After (Call,
6095 Make_Block_Statement (Loc,
6096 Handled_Statement_Sequence =>
6097 Make_Handled_Sequence_Of_Statements (Loc,
6098 Statements => New_List (
6099 Make_Implicit_Label_Declaration (Loc,
6100 Defining_Identifier =>
6101 Blk_Ent,
6102 Label_Construct =>
6103 Abortable_Block),
6104 Abortable_Block),
6105 Exception_Handlers => Hdle)));
6107 -- Create new call statement
6109 Params := Parameter_Associations (Call);
6111 Append_To (Params,
6112 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
6113 Append_To (Params,
6114 New_Reference_To (B, Loc));
6116 Rewrite (Call,
6117 Make_Procedure_Call_Statement (Loc,
6118 Name =>
6119 New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
6120 Parameter_Associations => Params));
6122 -- Construct statement sequence for new block
6124 Append_To (Stmts,
6125 Make_Implicit_If_Statement (N,
6126 Condition =>
6127 Make_Op_Not (Loc,
6128 New_Reference_To (Cancel_Param, Loc)),
6129 Then_Statements => Tstats));
6131 -- Protected the call against abort
6133 Prepend_To (Stmts,
6134 Make_Procedure_Call_Statement (Loc,
6135 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
6136 Parameter_Associations => Empty_List));
6137 end if;
6139 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
6141 -- The result is the new block
6143 Rewrite (N_Orig,
6144 Make_Block_Statement (Loc,
6145 Declarations => Decls,
6146 Handled_Statement_Sequence =>
6147 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6149 Analyze (N_Orig);
6150 end Expand_N_Asynchronous_Select;
6152 -------------------------------------
6153 -- Expand_N_Conditional_Entry_Call --
6154 -------------------------------------
6156 -- The conditional task entry call is converted to a call to
6157 -- Task_Entry_Call:
6159 -- declare
6160 -- B : Boolean;
6161 -- P : parms := (parm, parm, parm);
6163 -- begin
6164 -- Task_Entry_Call
6165 -- (<acceptor-task>, -- Acceptor
6166 -- <entry-index>, -- E
6167 -- P'Address, -- Uninterpreted_Data
6168 -- Conditional_Call, -- Mode
6169 -- B); -- Rendezvous_Successful
6170 -- parm := P.param;
6171 -- parm := P.param;
6172 -- ...
6173 -- if B then
6174 -- normal-statements
6175 -- else
6176 -- else-statements
6177 -- end if;
6178 -- end;
6180 -- For a description of the use of P and the assignments after the call,
6181 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
6182 -- conditional entry call has already been expanded (by the Expand_N_Entry
6183 -- _Call_Statement procedure) as follows:
6185 -- declare
6186 -- P : parms := (parm, parm, parm);
6187 -- begin
6188 -- ... info for in-out parameters
6189 -- Call_Simple (acceptor-task, entry-index, P'Address);
6190 -- parm := P.param;
6191 -- parm := P.param;
6192 -- ...
6193 -- end;
6195 -- so the task at hand is to convert the latter expansion into the former
6197 -- The conditional protected entry call is converted to a call to
6198 -- Protected_Entry_Call:
6200 -- declare
6201 -- P : parms := (parm, parm, parm);
6202 -- Bnn : Communications_Block;
6204 -- begin
6205 -- Protected_Entry_Call
6206 -- (po._object'Access, -- Object
6207 -- <entry index>, -- E
6208 -- P'Address, -- Uninterpreted_Data
6209 -- Conditional_Call, -- Mode
6210 -- Bnn); -- Block
6211 -- parm := P.param;
6212 -- parm := P.param;
6213 -- ...
6214 -- if Cancelled (Bnn) then
6215 -- else-statements
6216 -- else
6217 -- normal-statements
6218 -- end if;
6219 -- end;
6221 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
6222 -- into:
6224 -- declare
6225 -- B : Boolean := False;
6226 -- C : Ada.Tags.Prim_Op_Kind;
6227 -- K : Ada.Tags.Tagged_Kind :=
6228 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6229 -- P : Parameters := (Param1 .. ParamN);
6230 -- S : Integer;
6232 -- begin
6233 -- if K = Ada.Tags.TK_Limited_Tagged then
6234 -- <dispatching-call>;
6235 -- <triggering-statements>
6237 -- else
6238 -- S :=
6239 -- Ada.Tags.Get_Offset_Index
6240 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6242 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
6244 -- if C = POK_Protected_Entry
6245 -- or else C = POK_Task_Entry
6246 -- then
6247 -- Param1 := P.Param1;
6248 -- ...
6249 -- ParamN := P.ParamN;
6250 -- end if;
6252 -- if B then
6253 -- if C = POK_Procedure
6254 -- or else C = POK_Protected_Procedure
6255 -- or else C = POK_Task_Procedure
6256 -- then
6257 -- <dispatching-call>;
6258 -- end if;
6260 -- <triggering-statements>
6261 -- else
6262 -- <else-statements>
6263 -- end if;
6264 -- end if;
6265 -- end;
6267 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
6268 Loc : constant Source_Ptr := Sloc (N);
6269 Alt : constant Node_Id := Entry_Call_Alternative (N);
6270 Blk : Node_Id := Entry_Call_Statement (Alt);
6272 Actuals : List_Id;
6273 Blk_Typ : Entity_Id;
6274 Call : Node_Id;
6275 Call_Ent : Entity_Id;
6276 Conc_Typ_Stmts : List_Id;
6277 Decl : Node_Id;
6278 Decls : List_Id;
6279 Formals : List_Id;
6280 Lim_Typ_Stmts : List_Id;
6281 N_Stats : List_Id;
6282 Obj : Entity_Id;
6283 Param : Node_Id;
6284 Params : List_Id;
6285 Stmt : Node_Id;
6286 Stmts : List_Id;
6287 Transient_Blk : Node_Id;
6288 Unpack : List_Id;
6290 B : Entity_Id; -- Call status flag
6291 C : Entity_Id; -- Call kind
6292 K : Entity_Id; -- Tagged kind
6293 P : Entity_Id; -- Parameter block
6294 S : Entity_Id; -- Primitive operation slot
6296 begin
6297 if Ada_Version >= Ada_05
6298 and then Nkind (Blk) = N_Procedure_Call_Statement
6299 then
6300 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
6302 Decls := New_List;
6303 Stmts := New_List;
6305 -- Call status flag processing, generate:
6306 -- B : Boolean := False;
6308 B := Build_B (Loc, Decls);
6310 -- Call kind processing, generate:
6311 -- C : Ada.Tags.Prim_Op_Kind;
6313 C := Build_C (Loc, Decls);
6315 -- Tagged kind processing, generate:
6316 -- K : Ada.Tags.Tagged_Kind :=
6317 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6319 K := Build_K (Loc, Decls, Obj);
6321 -- Parameter block processing
6323 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
6324 P := Parameter_Block_Pack
6325 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6327 -- Dispatch table slot processing, generate:
6328 -- S : Integer;
6330 S := Build_S (Loc, Decls);
6332 -- Generate:
6333 -- S := Ada.Tags.Get_Offset_Index
6334 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
6336 Conc_Typ_Stmts :=
6337 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
6339 -- Generate:
6340 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
6342 Append_To (Conc_Typ_Stmts,
6343 Make_Procedure_Call_Statement (Loc,
6344 Name =>
6345 New_Reference_To (
6346 Find_Prim_Op (Etype (Etype (Obj)),
6347 Name_uDisp_Conditional_Select),
6348 Loc),
6349 Parameter_Associations =>
6350 New_List (
6351 New_Copy_Tree (Obj), -- <object>
6352 New_Reference_To (S, Loc), -- S
6353 Make_Attribute_Reference (Loc, -- P'Address
6354 Prefix =>
6355 New_Reference_To (P, Loc),
6356 Attribute_Name =>
6357 Name_Address),
6358 New_Reference_To (C, Loc), -- C
6359 New_Reference_To (B, Loc)))); -- B
6361 -- Generate:
6362 -- if C = POK_Protected_Entry
6363 -- or else C = POK_Task_Entry
6364 -- then
6365 -- Param1 := P.Param1;
6366 -- ...
6367 -- ParamN := P.ParamN;
6368 -- end if;
6370 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6372 -- Generate the if statement only when the packed parameters need
6373 -- explicit assignments to their corresponding actuals.
6375 if Present (Unpack) then
6376 Append_To (Conc_Typ_Stmts,
6377 Make_If_Statement (Loc,
6379 Condition =>
6380 Make_Or_Else (Loc,
6381 Left_Opnd =>
6382 Make_Op_Eq (Loc,
6383 Left_Opnd =>
6384 New_Reference_To (C, Loc),
6385 Right_Opnd =>
6386 New_Reference_To (RTE (
6387 RE_POK_Protected_Entry), Loc)),
6388 Right_Opnd =>
6389 Make_Op_Eq (Loc,
6390 Left_Opnd =>
6391 New_Reference_To (C, Loc),
6392 Right_Opnd =>
6393 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
6395 Then_Statements =>
6396 Unpack));
6397 end if;
6399 -- Generate:
6400 -- if B then
6401 -- if C = POK_Procedure
6402 -- or else C = POK_Protected_Procedure
6403 -- or else C = POK_Task_Procedure
6404 -- then
6405 -- <dispatching-call>
6406 -- end if;
6407 -- <normal-statements>
6408 -- else
6409 -- <else-statements>
6410 -- end if;
6412 N_Stats := New_Copy_List_Tree (Statements (Alt));
6414 Prepend_To (N_Stats,
6415 Make_If_Statement (Loc,
6416 Condition =>
6417 Make_Or_Else (Loc,
6418 Left_Opnd =>
6419 Make_Op_Eq (Loc,
6420 Left_Opnd =>
6421 New_Reference_To (C, Loc),
6422 Right_Opnd =>
6423 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
6425 Right_Opnd =>
6426 Make_Or_Else (Loc,
6427 Left_Opnd =>
6428 Make_Op_Eq (Loc,
6429 Left_Opnd =>
6430 New_Reference_To (C, Loc),
6431 Right_Opnd =>
6432 New_Reference_To (RTE (
6433 RE_POK_Protected_Procedure), Loc)),
6435 Right_Opnd =>
6436 Make_Op_Eq (Loc,
6437 Left_Opnd =>
6438 New_Reference_To (C, Loc),
6439 Right_Opnd =>
6440 New_Reference_To (RTE (
6441 RE_POK_Task_Procedure), Loc)))),
6443 Then_Statements =>
6444 New_List (Blk)));
6446 Append_To (Conc_Typ_Stmts,
6447 Make_If_Statement (Loc,
6448 Condition => New_Reference_To (B, Loc),
6449 Then_Statements => N_Stats,
6450 Else_Statements => Else_Statements (N)));
6452 -- Generate:
6453 -- <dispatching-call>;
6454 -- <triggering-statements>
6456 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
6457 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
6459 -- Generate:
6460 -- if K = Ada.Tags.TK_Limited_Tagged then
6461 -- Lim_Typ_Stmts
6462 -- else
6463 -- Conc_Typ_Stmts
6464 -- end if;
6466 Append_To (Stmts,
6467 Make_If_Statement (Loc,
6468 Condition =>
6469 Make_Op_Eq (Loc,
6470 Left_Opnd =>
6471 New_Reference_To (K, Loc),
6472 Right_Opnd =>
6473 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
6475 Then_Statements =>
6476 Lim_Typ_Stmts,
6478 Else_Statements =>
6479 Conc_Typ_Stmts));
6481 Rewrite (N,
6482 Make_Block_Statement (Loc,
6483 Declarations =>
6484 Decls,
6485 Handled_Statement_Sequence =>
6486 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6488 -- As described above, The entry alternative is transformed into a
6489 -- block that contains the gnulli call, and possibly assignment
6490 -- statements for in-out parameters. The gnulli call may itself be
6491 -- rewritten into a transient block if some unconstrained parameters
6492 -- require it. We need to retrieve the call to complete its parameter
6493 -- list.
6495 else
6496 Transient_Blk :=
6497 First_Real_Statement (Handled_Statement_Sequence (Blk));
6499 if Present (Transient_Blk)
6500 and then Nkind (Transient_Blk) = N_Block_Statement
6501 then
6502 Blk := Transient_Blk;
6503 end if;
6505 Stmts := Statements (Handled_Statement_Sequence (Blk));
6506 Stmt := First (Stmts);
6507 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6508 Next (Stmt);
6509 end loop;
6511 Call := Stmt;
6512 Params := Parameter_Associations (Call);
6514 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
6516 -- Substitute Conditional_Entry_Call for Simple_Call parameter
6518 Param := First (Params);
6519 while Present (Param)
6520 and then not Is_RTE (Etype (Param), RE_Call_Modes)
6521 loop
6522 Next (Param);
6523 end loop;
6525 pragma Assert (Present (Param));
6526 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
6528 Analyze (Param);
6530 -- Find the Communication_Block parameter for the call to the
6531 -- Cancelled function.
6533 Decl := First (Declarations (Blk));
6534 while Present (Decl)
6535 and then not Is_RTE (Etype (Object_Definition (Decl)),
6536 RE_Communication_Block)
6537 loop
6538 Next (Decl);
6539 end loop;
6541 -- Add an if statement to execute the else part if the call
6542 -- does not succeed (as indicated by the Cancelled predicate).
6544 Append_To (Stmts,
6545 Make_Implicit_If_Statement (N,
6546 Condition => Make_Function_Call (Loc,
6547 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
6548 Parameter_Associations => New_List (
6549 New_Reference_To (Defining_Identifier (Decl), Loc))),
6550 Then_Statements => Else_Statements (N),
6551 Else_Statements => Statements (Alt)));
6553 else
6554 B := Make_Defining_Identifier (Loc, Name_uB);
6556 -- Insert declaration of B in declarations of existing block
6558 if No (Declarations (Blk)) then
6559 Set_Declarations (Blk, New_List);
6560 end if;
6562 Prepend_To (Declarations (Blk),
6563 Make_Object_Declaration (Loc,
6564 Defining_Identifier => B,
6565 Object_Definition =>
6566 New_Reference_To (Standard_Boolean, Loc)));
6568 -- Create new call statement
6570 Append_To (Params,
6571 New_Reference_To (RTE (RE_Conditional_Call), Loc));
6572 Append_To (Params, New_Reference_To (B, Loc));
6574 Rewrite (Call,
6575 Make_Procedure_Call_Statement (Loc,
6576 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
6577 Parameter_Associations => Params));
6579 -- Construct statement sequence for new block
6581 Append_To (Stmts,
6582 Make_Implicit_If_Statement (N,
6583 Condition => New_Reference_To (B, Loc),
6584 Then_Statements => Statements (Alt),
6585 Else_Statements => Else_Statements (N)));
6586 end if;
6588 -- The result is the new block
6590 Rewrite (N,
6591 Make_Block_Statement (Loc,
6592 Declarations => Declarations (Blk),
6593 Handled_Statement_Sequence =>
6594 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6595 end if;
6597 Analyze (N);
6598 end Expand_N_Conditional_Entry_Call;
6600 ---------------------------------------
6601 -- Expand_N_Delay_Relative_Statement --
6602 ---------------------------------------
6604 -- Delay statement is implemented as a procedure call to Delay_For
6605 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
6606 -- simple delays imposed by the use of Protected Objects.
6608 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
6609 Loc : constant Source_Ptr := Sloc (N);
6610 begin
6611 Rewrite (N,
6612 Make_Procedure_Call_Statement (Loc,
6613 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
6614 Parameter_Associations => New_List (Expression (N))));
6615 Analyze (N);
6616 end Expand_N_Delay_Relative_Statement;
6618 ------------------------------------
6619 -- Expand_N_Delay_Until_Statement --
6620 ------------------------------------
6622 -- Delay Until statement is implemented as a procedure call to
6623 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
6625 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
6626 Loc : constant Source_Ptr := Sloc (N);
6627 Typ : Entity_Id;
6629 begin
6630 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
6631 Typ := RTE (RO_CA_Delay_Until);
6632 else
6633 Typ := RTE (RO_RT_Delay_Until);
6634 end if;
6636 Rewrite (N,
6637 Make_Procedure_Call_Statement (Loc,
6638 Name => New_Reference_To (Typ, Loc),
6639 Parameter_Associations => New_List (Expression (N))));
6641 Analyze (N);
6642 end Expand_N_Delay_Until_Statement;
6644 -------------------------
6645 -- Expand_N_Entry_Body --
6646 -------------------------
6648 procedure Expand_N_Entry_Body (N : Node_Id) is
6649 begin
6650 -- Associate discriminals with the next protected operation body to be
6651 -- expanded.
6653 if Present (Next_Protected_Operation (N)) then
6654 Set_Discriminals (Parent (Current_Scope));
6655 end if;
6656 end Expand_N_Entry_Body;
6658 -----------------------------------
6659 -- Expand_N_Entry_Call_Statement --
6660 -----------------------------------
6662 -- An entry call is expanded into GNARLI calls to implement a simple entry
6663 -- call (see Build_Simple_Entry_Call).
6665 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
6666 Concval : Node_Id;
6667 Ename : Node_Id;
6668 Index : Node_Id;
6670 begin
6671 if No_Run_Time_Mode then
6672 Error_Msg_CRT ("entry call", N);
6673 return;
6674 end if;
6676 -- If this entry call is part of an asynchronous select, don't expand it
6677 -- here; it will be expanded with the select statement. Don't expand
6678 -- timed entry calls either, as they are translated into asynchronous
6679 -- entry calls.
6681 -- ??? This whole approach is questionable; it may be better to go back
6682 -- to allowing the expansion to take place and then attempting to fix it
6683 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
6684 -- whether the expanded call is on a task or protected entry.
6686 if (Nkind (Parent (N)) /= N_Triggering_Alternative
6687 or else N /= Triggering_Statement (Parent (N)))
6688 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
6689 or else N /= Entry_Call_Statement (Parent (N))
6690 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
6691 then
6692 Extract_Entry (N, Concval, Ename, Index);
6693 Build_Simple_Entry_Call (N, Concval, Ename, Index);
6694 end if;
6695 end Expand_N_Entry_Call_Statement;
6697 --------------------------------
6698 -- Expand_N_Entry_Declaration --
6699 --------------------------------
6701 -- If there are parameters, then first, each of the formals is marked by
6702 -- setting Is_Entry_Formal. Next a record type is built which is used to
6703 -- hold the parameter values. The name of this record type is entryP where
6704 -- entry is the name of the entry, with an additional corresponding access
6705 -- type called entryPA. The record type has matching components for each
6706 -- formal (the component names are the same as the formal names). For
6707 -- elementary types, the component type matches the formal type. For
6708 -- composite types, an access type is declared (with the name formalA)
6709 -- which designates the formal type, and the type of the component is this
6710 -- access type. Finally the Entry_Component of each formal is set to
6711 -- reference the corresponding record component.
6713 procedure Expand_N_Entry_Declaration (N : Node_Id) is
6714 Loc : constant Source_Ptr := Sloc (N);
6715 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
6716 Components : List_Id;
6717 Formal : Node_Id;
6718 Ftype : Entity_Id;
6719 Last_Decl : Node_Id;
6720 Component : Entity_Id;
6721 Ctype : Entity_Id;
6722 Decl : Node_Id;
6723 Rec_Ent : Entity_Id;
6724 Acc_Ent : Entity_Id;
6726 begin
6727 Formal := First_Formal (Entry_Ent);
6728 Last_Decl := N;
6730 -- Most processing is done only if parameters are present
6732 if Present (Formal) then
6733 Components := New_List;
6735 -- Loop through formals
6737 while Present (Formal) loop
6738 Set_Is_Entry_Formal (Formal);
6739 Component :=
6740 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
6741 Set_Entry_Component (Formal, Component);
6742 Set_Entry_Formal (Component, Formal);
6743 Ftype := Etype (Formal);
6745 -- Declare new access type and then append
6747 Ctype :=
6748 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
6750 Decl :=
6751 Make_Full_Type_Declaration (Loc,
6752 Defining_Identifier => Ctype,
6753 Type_Definition =>
6754 Make_Access_To_Object_Definition (Loc,
6755 All_Present => True,
6756 Constant_Present => Ekind (Formal) = E_In_Parameter,
6757 Subtype_Indication => New_Reference_To (Ftype, Loc)));
6759 Insert_After (Last_Decl, Decl);
6760 Last_Decl := Decl;
6762 Append_To (Components,
6763 Make_Component_Declaration (Loc,
6764 Defining_Identifier => Component,
6765 Component_Definition =>
6766 Make_Component_Definition (Loc,
6767 Aliased_Present => False,
6768 Subtype_Indication => New_Reference_To (Ctype, Loc))));
6770 Next_Formal_With_Extras (Formal);
6771 end loop;
6773 -- Create the Entry_Parameter_Record declaration
6775 Rec_Ent :=
6776 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
6778 Decl :=
6779 Make_Full_Type_Declaration (Loc,
6780 Defining_Identifier => Rec_Ent,
6781 Type_Definition =>
6782 Make_Record_Definition (Loc,
6783 Component_List =>
6784 Make_Component_List (Loc,
6785 Component_Items => Components)));
6787 Insert_After (Last_Decl, Decl);
6788 Last_Decl := Decl;
6790 -- Construct and link in the corresponding access type
6792 Acc_Ent :=
6793 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
6795 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
6797 Decl :=
6798 Make_Full_Type_Declaration (Loc,
6799 Defining_Identifier => Acc_Ent,
6800 Type_Definition =>
6801 Make_Access_To_Object_Definition (Loc,
6802 All_Present => True,
6803 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
6805 Insert_After (Last_Decl, Decl);
6806 Last_Decl := Decl;
6807 end if;
6808 end Expand_N_Entry_Declaration;
6810 -----------------------------
6811 -- Expand_N_Protected_Body --
6812 -----------------------------
6814 -- Protected bodies are expanded to the completion of the subprograms
6815 -- created for the corresponding protected type. These are a protected and
6816 -- unprotected version of each protected subprogram in the object, a
6817 -- function to calculate each entry barrier, and a procedure to execute the
6818 -- sequence of statements of each protected entry body. For example, for
6819 -- protected type ptype:
6821 -- function entB
6822 -- (O : System.Address;
6823 -- E : Protected_Entry_Index)
6824 -- return Boolean
6825 -- is
6826 -- <discriminant renamings>
6827 -- <private object renamings>
6828 -- begin
6829 -- return <barrier expression>;
6830 -- end entB;
6832 -- procedure pprocN (_object : in out poV;...) is
6833 -- <discriminant renamings>
6834 -- <private object renamings>
6835 -- begin
6836 -- <sequence of statements>
6837 -- end pprocN;
6839 -- procedure pprocP (_object : in out poV;...) is
6840 -- procedure _clean is
6841 -- Pn : Boolean;
6842 -- begin
6843 -- ptypeS (_object, Pn);
6844 -- Unlock (_object._object'Access);
6845 -- Abort_Undefer.all;
6846 -- end _clean;
6848 -- begin
6849 -- Abort_Defer.all;
6850 -- Lock (_object._object'Access);
6851 -- pprocN (_object;...);
6852 -- at end
6853 -- _clean;
6854 -- end pproc;
6856 -- function pfuncN (_object : poV;...) return Return_Type is
6857 -- <discriminant renamings>
6858 -- <private object renamings>
6859 -- begin
6860 -- <sequence of statements>
6861 -- end pfuncN;
6863 -- function pfuncP (_object : poV) return Return_Type is
6864 -- procedure _clean is
6865 -- begin
6866 -- Unlock (_object._object'Access);
6867 -- Abort_Undefer.all;
6868 -- end _clean;
6870 -- begin
6871 -- Abort_Defer.all;
6872 -- Lock (_object._object'Access);
6873 -- return pfuncN (_object);
6875 -- at end
6876 -- _clean;
6877 -- end pfunc;
6879 -- procedure entE
6880 -- (O : System.Address;
6881 -- P : System.Address;
6882 -- E : Protected_Entry_Index)
6883 -- is
6884 -- <discriminant renamings>
6885 -- <private object renamings>
6886 -- type poVP is access poV;
6887 -- _Object : ptVP := ptVP!(O);
6889 -- begin
6890 -- begin
6891 -- <statement sequence>
6892 -- Complete_Entry_Body (_Object._Object);
6893 -- exception
6894 -- when all others =>
6895 -- Exceptional_Complete_Entry_Body (
6896 -- _Object._Object, Get_GNAT_Exception);
6897 -- end;
6898 -- end entE;
6900 -- The type poV is the record created for the protected type to hold
6901 -- the state of the protected object.
6903 procedure Expand_N_Protected_Body (N : Node_Id) is
6904 Loc : constant Source_Ptr := Sloc (N);
6905 Pid : constant Entity_Id := Corresponding_Spec (N);
6906 Op_Body : Node_Id;
6907 Op_Decl : Node_Id;
6908 Op_Id : Entity_Id;
6909 Disp_Op_Body : Node_Id;
6910 New_Op_Body : Node_Id;
6911 Current_Node : Node_Id;
6912 Num_Entries : Natural := 0;
6914 function Build_Dispatching_Subprogram_Body
6915 (N : Node_Id;
6916 Pid : Node_Id;
6917 Prot_Bod : Node_Id) return Node_Id;
6918 -- Build a dispatching version of the protected subprogram body. The
6919 -- newly generated subprogram contains a call to the original protected
6920 -- body. The following code is generated:
6922 -- function <protected-function-name> (Param1 .. ParamN) return
6923 -- <return-type> is
6924 -- begin
6925 -- return <protected-function-name>P (Param1 .. ParamN);
6926 -- end <protected-function-name>;
6928 -- or
6930 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
6931 -- begin
6932 -- <protected-procedure-name>P (Param1 .. ParamN);
6933 -- end <protected-procedure-name>
6935 ---------------------------------------
6936 -- Build_Dispatching_Subprogram_Body --
6937 ---------------------------------------
6939 function Build_Dispatching_Subprogram_Body
6940 (N : Node_Id;
6941 Pid : Node_Id;
6942 Prot_Bod : Node_Id) return Node_Id
6944 Loc : constant Source_Ptr := Sloc (N);
6945 Actuals : List_Id;
6946 Formal : Node_Id;
6947 Spec : Node_Id;
6948 Stmts : List_Id;
6950 begin
6951 -- Generate a specification without a letter suffix in order to
6952 -- override an interface function or procedure.
6954 Spec :=
6955 Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
6957 -- The formal parameters become the actuals of the protected
6958 -- function or procedure call.
6960 Actuals := New_List;
6961 Formal := First (Parameter_Specifications (Spec));
6962 while Present (Formal) loop
6963 Append_To (Actuals,
6964 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
6966 Next (Formal);
6967 end loop;
6969 if Nkind (Spec) = N_Procedure_Specification then
6970 Stmts :=
6971 New_List (
6972 Make_Procedure_Call_Statement (Loc,
6973 Name =>
6974 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
6975 Parameter_Associations => Actuals));
6976 else
6977 pragma Assert (Nkind (Spec) = N_Function_Specification);
6979 Stmts :=
6980 New_List (
6981 Make_Simple_Return_Statement (Loc,
6982 Expression =>
6983 Make_Function_Call (Loc,
6984 Name =>
6985 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
6986 Parameter_Associations => Actuals)));
6987 end if;
6989 return
6990 Make_Subprogram_Body (Loc,
6991 Declarations => Empty_List,
6992 Specification => Spec,
6993 Handled_Statement_Sequence =>
6994 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
6995 end Build_Dispatching_Subprogram_Body;
6997 -- Start of processing for Expand_N_Protected_Body
6999 begin
7000 if No_Run_Time_Mode then
7001 Error_Msg_CRT ("protected body", N);
7002 return;
7003 end if;
7005 if Nkind (Parent (N)) = N_Subunit then
7007 -- This is the proper body corresponding to a stub. The declarations
7008 -- must be inserted at the point of the stub, which is in the decla-
7009 -- rative part of the parent unit.
7011 Current_Node := Corresponding_Stub (Parent (N));
7013 else
7014 Current_Node := N;
7015 end if;
7017 Op_Body := First (Declarations (N));
7019 -- The protected body is replaced with the bodies of its
7020 -- protected operations, and the declarations for internal objects
7021 -- that may have been created for entry family bounds.
7023 Rewrite (N, Make_Null_Statement (Sloc (N)));
7024 Analyze (N);
7026 while Present (Op_Body) loop
7027 case Nkind (Op_Body) is
7028 when N_Subprogram_Declaration =>
7029 null;
7031 when N_Subprogram_Body =>
7033 -- Exclude functions created to analyze defaults
7035 if not Is_Eliminated (Defining_Entity (Op_Body))
7036 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
7037 then
7038 New_Op_Body :=
7039 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
7041 -- Propagate the finalization chain to the new body.
7042 -- In the unlikely event that the subprogram contains a
7043 -- declaration or allocator for an object that requires
7044 -- finalization, the corresponding chain is created when
7045 -- analyzing the body, and attached to its entity. This
7046 -- entity is not further elaborated, and so the chain
7047 -- properly belongs to the newly created subprogram body.
7049 if Present
7050 (Finalization_Chain_Entity (Defining_Entity (Op_Body)))
7051 then
7052 Set_Finalization_Chain_Entity
7053 (Protected_Body_Subprogram
7054 (Corresponding_Spec (Op_Body)),
7055 Finalization_Chain_Entity (Defining_Entity (Op_Body)));
7056 Set_Analyzed
7057 (Handled_Statement_Sequence (New_Op_Body), False);
7058 end if;
7060 Insert_After (Current_Node, New_Op_Body);
7061 Current_Node := New_Op_Body;
7062 Analyze (New_Op_Body);
7064 -- Build the corresponding protected operation. It may
7065 -- appear that this is needed only if this is a visible
7066 -- operation of the type, or if it is an interrupt handler,
7067 -- and this was the strategy used previously in GNAT.
7068 -- However, the operation may be exported through a
7069 -- 'Access to an external caller. This is the common idiom
7070 -- in code that uses the Ada 2005 Timing_Events package
7071 -- As a result we need to produce the protected body for
7072 -- both visible and private operations.
7074 if Present (Corresponding_Spec (Op_Body)) then
7075 Op_Decl :=
7076 Unit_Declaration_Node (Corresponding_Spec (Op_Body));
7078 if Nkind (Parent (Op_Decl)) =
7079 N_Protected_Definition
7080 then
7081 New_Op_Body :=
7082 Build_Protected_Subprogram_Body (
7083 Op_Body, Pid, Specification (New_Op_Body));
7085 Insert_After (Current_Node, New_Op_Body);
7086 Analyze (New_Op_Body);
7088 Current_Node := New_Op_Body;
7090 -- Generate an overriding primitive operation body for
7091 -- this subprogram if the protected type implements
7092 -- an interface.
7094 if Ada_Version >= Ada_05
7095 and then Present (Interfaces (
7096 Corresponding_Record_Type (Pid)))
7097 then
7098 Disp_Op_Body :=
7099 Build_Dispatching_Subprogram_Body (
7100 Op_Body, Pid, New_Op_Body);
7102 Insert_After (Current_Node, Disp_Op_Body);
7103 Analyze (Disp_Op_Body);
7105 Current_Node := Disp_Op_Body;
7106 end if;
7107 end if;
7108 end if;
7109 end if;
7111 when N_Entry_Body =>
7112 Op_Id := Defining_Identifier (Op_Body);
7113 Num_Entries := Num_Entries + 1;
7115 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
7117 Insert_After (Current_Node, New_Op_Body);
7118 Current_Node := New_Op_Body;
7119 Analyze (New_Op_Body);
7121 when N_Implicit_Label_Declaration =>
7122 null;
7124 when N_Itype_Reference =>
7125 Insert_After (Current_Node, New_Copy (Op_Body));
7127 when N_Freeze_Entity =>
7128 New_Op_Body := New_Copy (Op_Body);
7130 if Present (Entity (Op_Body))
7131 and then Freeze_Node (Entity (Op_Body)) = Op_Body
7132 then
7133 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
7134 end if;
7136 Insert_After (Current_Node, New_Op_Body);
7137 Current_Node := New_Op_Body;
7138 Analyze (New_Op_Body);
7140 when N_Pragma =>
7141 New_Op_Body := New_Copy (Op_Body);
7142 Insert_After (Current_Node, New_Op_Body);
7143 Current_Node := New_Op_Body;
7144 Analyze (New_Op_Body);
7146 when N_Object_Declaration =>
7147 pragma Assert (not Comes_From_Source (Op_Body));
7148 New_Op_Body := New_Copy (Op_Body);
7149 Insert_After (Current_Node, New_Op_Body);
7150 Current_Node := New_Op_Body;
7151 Analyze (New_Op_Body);
7153 when others =>
7154 raise Program_Error;
7156 end case;
7158 Next (Op_Body);
7159 end loop;
7161 -- Finally, create the body of the function that maps an entry index
7162 -- into the corresponding body index, except when there is no entry,
7163 -- or in a ravenscar-like profile.
7165 if Corresponding_Runtime_Package (Pid) =
7166 System_Tasking_Protected_Objects_Entries
7167 then
7168 New_Op_Body := Build_Find_Body_Index (Pid);
7169 Insert_After (Current_Node, New_Op_Body);
7170 Current_Node := New_Op_Body;
7171 Analyze (New_Op_Body);
7172 end if;
7174 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
7175 -- the protected body. At this point the entry specs have been created,
7176 -- frozen and included in the dispatch table for the protected type.
7178 pragma Assert (Present (Corresponding_Record_Type (Pid)));
7180 if Ada_Version >= Ada_05
7181 and then Present (Protected_Definition (Parent (Pid)))
7182 and then Present (Interfaces (Corresponding_Record_Type (Pid)))
7183 then
7184 declare
7185 Vis_Decl : Node_Id :=
7186 First (Visible_Declarations
7187 (Protected_Definition (Parent (Pid))));
7188 Wrap_Body : Node_Id;
7190 begin
7191 -- Examine the visible declarations of the protected type, looking
7192 -- for an entry declaration. We do not consider entry families
7193 -- since they cannot have dispatching operations, thus they do not
7194 -- need entry wrappers.
7196 while Present (Vis_Decl) loop
7197 if Nkind (Vis_Decl) = N_Entry_Declaration then
7198 Wrap_Body :=
7199 Build_Wrapper_Body (Loc,
7200 Proc_Nam => Defining_Identifier (Vis_Decl),
7201 Obj_Typ => Corresponding_Record_Type (Pid),
7202 Formals => Parameter_Specifications (Vis_Decl));
7204 if Wrap_Body /= Empty then
7205 Insert_After (Current_Node, Wrap_Body);
7206 Current_Node := Wrap_Body;
7208 Analyze (Wrap_Body);
7209 end if;
7211 elsif Nkind (Vis_Decl) = N_Subprogram_Declaration then
7212 Wrap_Body :=
7213 Build_Wrapper_Body (Loc,
7214 Proc_Nam => Defining_Unit_Name
7215 (Specification (Vis_Decl)),
7216 Obj_Typ => Corresponding_Record_Type (Pid),
7217 Formals => Parameter_Specifications
7218 (Specification (Vis_Decl)));
7220 if Wrap_Body /= Empty then
7221 Insert_After (Current_Node, Wrap_Body);
7222 Current_Node := Wrap_Body;
7224 Analyze (Wrap_Body);
7225 end if;
7226 end if;
7228 Next (Vis_Decl);
7229 end loop;
7230 end;
7231 end if;
7232 end Expand_N_Protected_Body;
7234 -----------------------------------------
7235 -- Expand_N_Protected_Type_Declaration --
7236 -----------------------------------------
7238 -- First we create a corresponding record type declaration used to
7239 -- represent values of this protected type.
7240 -- The general form of this type declaration is
7242 -- type poV (discriminants) is record
7243 -- _Object : aliased <kind>Protection
7244 -- [(<entry count> [, <handler count>])];
7245 -- [entry_family : array (bounds) of Void;]
7246 -- <private data fields>
7247 -- end record;
7249 -- The discriminants are present only if the corresponding protected type
7250 -- has discriminants, and they exactly mirror the protected type
7251 -- discriminants. The private data fields similarly mirror the private
7252 -- declarations of the protected type.
7254 -- The Object field is always present. It contains RTS specific data used
7255 -- to control the protected object. It is declared as Aliased so that it
7256 -- can be passed as a pointer to the RTS. This allows the protected record
7257 -- to be referenced within RTS data structures. An appropriate Protection
7258 -- type and discriminant are generated.
7260 -- The Service field is present for protected objects with entries. It
7261 -- contains sufficient information to allow the entry service procedure for
7262 -- this object to be called when the object is not known till runtime.
7264 -- One entry_family component is present for each entry family in the
7265 -- task definition (see Expand_N_Task_Type_Declaration).
7267 -- When a protected object is declared, an instance of the protected type
7268 -- value record is created. The elaboration of this declaration creates the
7269 -- correct bounds for the entry families, and also evaluates the priority
7270 -- expression if needed. The initialization routine for the protected type
7271 -- itself then calls Initialize_Protection with appropriate parameters to
7272 -- initialize the value of the Task_Id field. Install_Handlers may be also
7273 -- called if a pragma Attach_Handler applies.
7275 -- Note: this record is passed to the subprograms created by the expansion
7276 -- of protected subprograms and entries. It is an in parameter to protected
7277 -- functions and an in out parameter to procedures and entry bodies. The
7278 -- Entity_Id for this created record type is placed in the
7279 -- Corresponding_Record_Type field of the associated protected type entity.
7281 -- Next we create a procedure specifications for protected subprograms and
7282 -- entry bodies. For each protected subprograms two subprograms are
7283 -- created, an unprotected and a protected version. The unprotected version
7284 -- is called from within other operations of the same protected object.
7286 -- We also build the call to register the procedure if a pragma
7287 -- Interrupt_Handler applies.
7289 -- A single subprogram is created to service all entry bodies; it has an
7290 -- additional boolean out parameter indicating that the previous entry call
7291 -- made by the current task was serviced immediately, i.e. not by proxy.
7292 -- The O parameter contains a pointer to a record object of the type
7293 -- described above. An untyped interface is used here to allow this
7294 -- procedure to be called in places where the type of the object to be
7295 -- serviced is not known. This must be done, for example, when a call that
7296 -- may have been requeued is cancelled; the corresponding object must be
7297 -- serviced, but which object that is not known till runtime.
7299 -- procedure ptypeS
7300 -- (O : System.Address; P : out Boolean);
7301 -- procedure pprocN (_object : in out poV);
7302 -- procedure pproc (_object : in out poV);
7303 -- function pfuncN (_object : poV);
7304 -- function pfunc (_object : poV);
7305 -- ...
7307 -- Note that this must come after the record type declaration, since
7308 -- the specs refer to this type.
7310 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
7311 Loc : constant Source_Ptr := Sloc (N);
7312 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
7314 Pdef : constant Node_Id := Protected_Definition (N);
7315 -- This contains two lists; one for visible and one for private decls
7317 Rec_Decl : Node_Id;
7318 Cdecls : List_Id;
7319 Discr_Map : constant Elist_Id := New_Elmt_List;
7320 Priv : Node_Id;
7321 New_Priv : Node_Id;
7322 Comp : Node_Id;
7323 Comp_Id : Entity_Id;
7324 Sub : Node_Id;
7325 Current_Node : Node_Id := N;
7326 Bdef : Entity_Id := Empty; -- avoid uninit warning
7327 Edef : Entity_Id := Empty; -- avoid uninit warning
7328 Entries_Aggr : Node_Id;
7329 Body_Id : Entity_Id;
7330 Body_Arr : Node_Id;
7331 E_Count : Int;
7332 Object_Comp : Node_Id;
7334 procedure Register_Handler;
7335 -- For a protected operation that is an interrupt handler, add the
7336 -- freeze action that will register it as such.
7338 ----------------------
7339 -- Register_Handler --
7340 ----------------------
7342 procedure Register_Handler is
7344 -- All semantic checks already done in Sem_Prag
7346 Prot_Proc : constant Entity_Id :=
7347 Defining_Unit_Name
7348 (Specification (Current_Node));
7350 Proc_Address : constant Node_Id :=
7351 Make_Attribute_Reference (Loc,
7352 Prefix => New_Reference_To (Prot_Proc, Loc),
7353 Attribute_Name => Name_Address);
7355 RTS_Call : constant Entity_Id :=
7356 Make_Procedure_Call_Statement (Loc,
7357 Name =>
7358 New_Reference_To (
7359 RTE (RE_Register_Interrupt_Handler), Loc),
7360 Parameter_Associations =>
7361 New_List (Proc_Address));
7362 begin
7363 Append_Freeze_Action (Prot_Proc, RTS_Call);
7364 end Register_Handler;
7366 -- Start of processing for Expand_N_Protected_Type_Declaration
7368 begin
7369 if Present (Corresponding_Record_Type (Prot_Typ)) then
7370 return;
7371 else
7372 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
7373 end if;
7375 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
7377 -- Ada 2005 (AI-345): Propagate the attribute that contains the list
7378 -- of implemented interfaces.
7380 Set_Interface_List (Type_Definition (Rec_Decl), Interface_List (N));
7382 Qualify_Entity_Names (N);
7384 -- If the type has discriminants, their occurrences in the declaration
7385 -- have been replaced by the corresponding discriminals. For components
7386 -- that are constrained by discriminants, their homologues in the
7387 -- corresponding record type must refer to the discriminants of that
7388 -- record, so we must apply a new renaming to subtypes_indications:
7390 -- protected discriminant => discriminal => record discriminant
7392 -- This replacement is not applied to default expressions, for which
7393 -- the discriminal is correct.
7395 if Has_Discriminants (Prot_Typ) then
7396 declare
7397 Disc : Entity_Id;
7398 Decl : Node_Id;
7400 begin
7401 Disc := First_Discriminant (Prot_Typ);
7402 Decl := First (Discriminant_Specifications (Rec_Decl));
7403 while Present (Disc) loop
7404 Append_Elmt (Discriminal (Disc), Discr_Map);
7405 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
7406 Next_Discriminant (Disc);
7407 Next (Decl);
7408 end loop;
7409 end;
7410 end if;
7412 -- Fill in the component declarations
7414 -- Add components for entry families. For each entry family, create an
7415 -- anonymous type declaration with the same size, and analyze the type.
7417 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
7419 -- Prepend the _Object field with the right type to the component list.
7420 -- We need to compute the number of entries, and in some cases the
7421 -- number of Attach_Handler pragmas.
7423 declare
7424 Ritem : Node_Id;
7425 Num_Attach_Handler : Int := 0;
7426 Protection_Subtype : Node_Id;
7427 Entry_Count_Expr : constant Node_Id :=
7428 Build_Entry_Count_Expression
7429 (Prot_Typ, Cdecls, Loc);
7431 begin
7432 -- Could this be simplified using Corresponding_Runtime_Package???
7434 if Has_Attach_Handler (Prot_Typ) then
7435 Ritem := First_Rep_Item (Prot_Typ);
7436 while Present (Ritem) loop
7437 if Nkind (Ritem) = N_Pragma
7438 and then Pragma_Name (Ritem) = Name_Attach_Handler
7439 then
7440 Num_Attach_Handler := Num_Attach_Handler + 1;
7441 end if;
7443 Next_Rep_Item (Ritem);
7444 end loop;
7446 if Restricted_Profile then
7447 if Has_Entries (Prot_Typ) then
7448 Protection_Subtype :=
7449 New_Reference_To (RTE (RE_Protection_Entry), Loc);
7450 else
7451 Protection_Subtype :=
7452 New_Reference_To (RTE (RE_Protection), Loc);
7453 end if;
7454 else
7455 Protection_Subtype :=
7456 Make_Subtype_Indication
7457 (Sloc => Loc,
7458 Subtype_Mark =>
7459 New_Reference_To
7460 (RTE (RE_Static_Interrupt_Protection), Loc),
7461 Constraint =>
7462 Make_Index_Or_Discriminant_Constraint (
7463 Sloc => Loc,
7464 Constraints => New_List (
7465 Entry_Count_Expr,
7466 Make_Integer_Literal (Loc, Num_Attach_Handler))));
7467 end if;
7469 elsif Has_Interrupt_Handler (Prot_Typ) then
7470 Protection_Subtype :=
7471 Make_Subtype_Indication (
7472 Sloc => Loc,
7473 Subtype_Mark => New_Reference_To
7474 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
7475 Constraint =>
7476 Make_Index_Or_Discriminant_Constraint (
7477 Sloc => Loc,
7478 Constraints => New_List (Entry_Count_Expr)));
7480 -- Type has explicit entries or generated primitive entry wrappers
7482 elsif Has_Entries (Prot_Typ)
7483 or else (Ada_Version >= Ada_05
7484 and then Present (Interface_List (N)))
7485 then
7486 case Corresponding_Runtime_Package (Prot_Typ) is
7487 when System_Tasking_Protected_Objects_Entries =>
7488 Protection_Subtype :=
7489 Make_Subtype_Indication (Loc,
7490 Subtype_Mark =>
7491 New_Reference_To (RTE (RE_Protection_Entries), Loc),
7492 Constraint =>
7493 Make_Index_Or_Discriminant_Constraint (
7494 Sloc => Loc,
7495 Constraints => New_List (Entry_Count_Expr)));
7497 when System_Tasking_Protected_Objects_Single_Entry =>
7498 Protection_Subtype :=
7499 New_Reference_To (RTE (RE_Protection_Entry), Loc);
7501 when others =>
7502 raise Program_Error;
7503 end case;
7505 else
7506 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
7507 end if;
7509 Object_Comp :=
7510 Make_Component_Declaration (Loc,
7511 Defining_Identifier =>
7512 Make_Defining_Identifier (Loc, Name_uObject),
7513 Component_Definition =>
7514 Make_Component_Definition (Loc,
7515 Aliased_Present => True,
7516 Subtype_Indication => Protection_Subtype));
7517 end;
7519 pragma Assert (Present (Pdef));
7521 -- Add private field components
7523 if Present (Private_Declarations (Pdef)) then
7524 Priv := First (Private_Declarations (Pdef));
7526 while Present (Priv) loop
7528 if Nkind (Priv) = N_Component_Declaration then
7530 -- The component definition consists of a subtype indication,
7531 -- or (in Ada 2005) an access definition. Make a copy of the
7532 -- proper definition.
7534 declare
7535 Old_Comp : constant Node_Id := Component_Definition (Priv);
7536 Pent : constant Entity_Id := Defining_Identifier (Priv);
7537 New_Comp : Node_Id;
7539 begin
7540 if Present (Subtype_Indication (Old_Comp)) then
7541 New_Comp :=
7542 Make_Component_Definition (Sloc (Pent),
7543 Aliased_Present => False,
7544 Subtype_Indication =>
7545 New_Copy_Tree (Subtype_Indication (Old_Comp),
7546 Discr_Map));
7547 else
7548 New_Comp :=
7549 Make_Component_Definition (Sloc (Pent),
7550 Aliased_Present => False,
7551 Access_Definition =>
7552 New_Copy_Tree (Access_Definition (Old_Comp),
7553 Discr_Map));
7554 end if;
7556 New_Priv :=
7557 Make_Component_Declaration (Loc,
7558 Defining_Identifier =>
7559 Make_Defining_Identifier (Sloc (Pent), Chars (Pent)),
7560 Component_Definition => New_Comp,
7561 Expression => Expression (Priv));
7563 Append_To (Cdecls, New_Priv);
7564 end;
7566 elsif Nkind (Priv) = N_Subprogram_Declaration then
7568 -- Make the unprotected version of the subprogram available
7569 -- for expansion of intra object calls. There is need for
7570 -- a protected version only if the subprogram is an interrupt
7571 -- handler, otherwise this operation can only be called from
7572 -- within the body.
7574 Sub :=
7575 Make_Subprogram_Declaration (Loc,
7576 Specification =>
7577 Build_Protected_Sub_Specification
7578 (Priv, Prot_Typ, Unprotected_Mode));
7580 Insert_After (Current_Node, Sub);
7581 Analyze (Sub);
7583 Set_Protected_Body_Subprogram
7584 (Defining_Unit_Name (Specification (Priv)),
7585 Defining_Unit_Name (Specification (Sub)));
7587 Current_Node := Sub;
7589 Sub :=
7590 Make_Subprogram_Declaration (Loc,
7591 Specification =>
7592 Build_Protected_Sub_Specification
7593 (Priv, Prot_Typ, Protected_Mode));
7595 Insert_After (Current_Node, Sub);
7596 Analyze (Sub);
7597 Current_Node := Sub;
7599 if Is_Interrupt_Handler
7600 (Defining_Unit_Name (Specification (Priv)))
7601 then
7602 if not Restricted_Profile then
7603 Register_Handler;
7604 end if;
7605 end if;
7606 end if;
7608 Next (Priv);
7609 end loop;
7610 end if;
7612 -- Put the _Object component after the private component so that it
7613 -- be finalized early as required by 9.4 (20)
7615 Append_To (Cdecls, Object_Comp);
7617 Insert_After (Current_Node, Rec_Decl);
7618 Current_Node := Rec_Decl;
7620 -- Analyze the record declaration immediately after construction,
7621 -- because the initialization procedure is needed for single object
7622 -- declarations before the next entity is analyzed (the freeze call
7623 -- that generates this initialization procedure is found below).
7625 Analyze (Rec_Decl, Suppress => All_Checks);
7627 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
7628 -- the corresponding record is frozen
7630 if Ada_Version >= Ada_05
7631 and then Present (Visible_Declarations (Pdef))
7632 and then Present (Corresponding_Record_Type
7633 (Defining_Identifier (Parent (Pdef))))
7634 and then Present (Interfaces
7635 (Corresponding_Record_Type
7636 (Defining_Identifier (Parent (Pdef)))))
7637 then
7638 declare
7639 Current_Node : Node_Id := Rec_Decl;
7640 Vis_Decl : Node_Id;
7641 Wrap_Spec : Node_Id;
7642 New_N : Node_Id;
7644 begin
7645 -- Examine the visible declarations of the protected type, looking
7646 -- for declarations of entries, and subprograms. We do not
7647 -- consider entry families since they cannot have dispatching
7648 -- operations, thus they do not need entry wrappers.
7650 Vis_Decl := First (Visible_Declarations (Pdef));
7652 while Present (Vis_Decl) loop
7654 Wrap_Spec := Empty;
7656 if Nkind (Vis_Decl) = N_Entry_Declaration
7657 and then No (Discrete_Subtype_Definition (Vis_Decl))
7658 then
7659 Wrap_Spec :=
7660 Build_Wrapper_Spec (Loc,
7661 Proc_Nam => Defining_Identifier (Vis_Decl),
7662 Obj_Typ => Defining_Identifier (Rec_Decl),
7663 Formals => Parameter_Specifications (Vis_Decl));
7665 elsif Nkind (Vis_Decl) = N_Subprogram_Declaration then
7666 Wrap_Spec :=
7667 Build_Wrapper_Spec (Loc,
7668 Proc_Nam => Defining_Unit_Name
7669 (Specification (Vis_Decl)),
7670 Obj_Typ => Defining_Identifier (Rec_Decl),
7671 Formals => Parameter_Specifications
7672 (Specification (Vis_Decl)));
7674 end if;
7676 if Wrap_Spec /= Empty then
7677 New_N := Make_Subprogram_Declaration (Loc,
7678 Specification => Wrap_Spec);
7680 Insert_After (Current_Node, New_N);
7681 Current_Node := New_N;
7683 Analyze (New_N);
7684 end if;
7686 Next (Vis_Decl);
7687 end loop;
7688 end;
7689 end if;
7691 -- Collect pointers to entry bodies and their barriers, to be placed
7692 -- in the Entry_Bodies_Array for the type. For each entry/family we
7693 -- add an expression to the aggregate which is the initial value of
7694 -- this array. The array is declared after all protected subprograms.
7696 if Has_Entries (Prot_Typ) then
7697 Entries_Aggr :=
7698 Make_Aggregate (Loc, Expressions => New_List);
7700 else
7701 Entries_Aggr := Empty;
7702 end if;
7704 -- Build two new procedure specifications for each protected subprogram;
7705 -- one to call from outside the object and one to call from inside.
7706 -- Build a barrier function and an entry body action procedure
7707 -- specification for each protected entry. Initialize the entry body
7708 -- array. If subprogram is flagged as eliminated, do not generate any
7709 -- internal operations.
7711 E_Count := 0;
7713 Comp := First (Visible_Declarations (Pdef));
7715 while Present (Comp) loop
7716 if Nkind (Comp) = N_Subprogram_Declaration
7717 and then not Is_Eliminated (Defining_Entity (Comp))
7718 then
7719 Sub :=
7720 Make_Subprogram_Declaration (Loc,
7721 Specification =>
7722 Build_Protected_Sub_Specification
7723 (Comp, Prot_Typ, Unprotected_Mode));
7725 Insert_After (Current_Node, Sub);
7726 Analyze (Sub);
7728 Set_Protected_Body_Subprogram
7729 (Defining_Unit_Name (Specification (Comp)),
7730 Defining_Unit_Name (Specification (Sub)));
7732 -- Make the protected version of the subprogram available for
7733 -- expansion of external calls.
7735 Current_Node := Sub;
7737 Sub :=
7738 Make_Subprogram_Declaration (Loc,
7739 Specification =>
7740 Build_Protected_Sub_Specification
7741 (Comp, Prot_Typ, Protected_Mode));
7743 Insert_After (Current_Node, Sub);
7744 Analyze (Sub);
7746 Current_Node := Sub;
7748 -- Generate an overriding primitive operation specification for
7749 -- this subprogram if the protected type implements an interface.
7751 if Ada_Version >= Ada_05
7752 and then
7753 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
7754 then
7755 Sub :=
7756 Make_Subprogram_Declaration (Loc,
7757 Specification =>
7758 Build_Protected_Sub_Specification
7759 (Comp, Prot_Typ, Dispatching_Mode));
7761 Insert_After (Current_Node, Sub);
7762 Analyze (Sub);
7764 Current_Node := Sub;
7765 end if;
7767 -- If a pragma Interrupt_Handler applies, build and add a call to
7768 -- Register_Interrupt_Handler to the freezing actions of the
7769 -- protected version (Current_Node) of the subprogram:
7771 -- system.interrupts.register_interrupt_handler
7772 -- (prot_procP'address);
7774 if not Restricted_Profile
7775 and then Is_Interrupt_Handler
7776 (Defining_Unit_Name (Specification (Comp)))
7777 then
7778 Register_Handler;
7779 end if;
7781 elsif Nkind (Comp) = N_Entry_Declaration then
7782 E_Count := E_Count + 1;
7783 Comp_Id := Defining_Identifier (Comp);
7785 Edef :=
7786 Make_Defining_Identifier (Loc,
7787 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
7788 Sub :=
7789 Make_Subprogram_Declaration (Loc,
7790 Specification =>
7791 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
7793 Insert_After (Current_Node, Sub);
7794 Analyze (Sub);
7796 Set_Protected_Body_Subprogram
7797 (Defining_Identifier (Comp),
7798 Defining_Unit_Name (Specification (Sub)));
7800 Current_Node := Sub;
7802 Bdef :=
7803 Make_Defining_Identifier (Loc,
7804 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
7805 Sub :=
7806 Make_Subprogram_Declaration (Loc,
7807 Specification =>
7808 Build_Barrier_Function_Specification (Loc, Bdef));
7810 Insert_After (Current_Node, Sub);
7811 Analyze (Sub);
7812 Set_Protected_Body_Subprogram (Bdef, Bdef);
7813 Set_Barrier_Function (Comp_Id, Bdef);
7814 Set_Scope (Bdef, Scope (Comp_Id));
7815 Current_Node := Sub;
7817 -- Collect pointers to the protected subprogram and the barrier
7818 -- of the current entry, for insertion into Entry_Bodies_Array.
7820 Append (
7821 Make_Aggregate (Loc,
7822 Expressions => New_List (
7823 Make_Attribute_Reference (Loc,
7824 Prefix => New_Reference_To (Bdef, Loc),
7825 Attribute_Name => Name_Unrestricted_Access),
7826 Make_Attribute_Reference (Loc,
7827 Prefix => New_Reference_To (Edef, Loc),
7828 Attribute_Name => Name_Unrestricted_Access))),
7829 Expressions (Entries_Aggr));
7831 end if;
7833 Next (Comp);
7834 end loop;
7836 -- If there are some private entry declarations, expand it as if they
7837 -- were visible entries.
7839 if Present (Private_Declarations (Pdef)) then
7840 Comp := First (Private_Declarations (Pdef));
7841 while Present (Comp) loop
7842 if Nkind (Comp) = N_Entry_Declaration then
7843 E_Count := E_Count + 1;
7844 Comp_Id := Defining_Identifier (Comp);
7846 Edef :=
7847 Make_Defining_Identifier (Loc,
7848 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
7849 Sub :=
7850 Make_Subprogram_Declaration (Loc,
7851 Specification =>
7852 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
7854 Insert_After (Current_Node, Sub);
7855 Analyze (Sub);
7857 Set_Protected_Body_Subprogram
7858 (Defining_Identifier (Comp),
7859 Defining_Unit_Name (Specification (Sub)));
7861 Current_Node := Sub;
7863 Bdef :=
7864 Make_Defining_Identifier (Loc,
7865 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
7867 Sub :=
7868 Make_Subprogram_Declaration (Loc,
7869 Specification =>
7870 Build_Barrier_Function_Specification (Loc, Bdef));
7872 Insert_After (Current_Node, Sub);
7873 Analyze (Sub);
7874 Set_Protected_Body_Subprogram (Bdef, Bdef);
7875 Set_Barrier_Function (Comp_Id, Bdef);
7876 Set_Scope (Bdef, Scope (Comp_Id));
7877 Current_Node := Sub;
7879 -- Collect pointers to the protected subprogram and the barrier
7880 -- of the current entry, for insertion into Entry_Bodies_Array.
7882 Append_To (Expressions (Entries_Aggr),
7883 Make_Aggregate (Loc,
7884 Expressions => New_List (
7885 Make_Attribute_Reference (Loc,
7886 Prefix => New_Reference_To (Bdef, Loc),
7887 Attribute_Name => Name_Unrestricted_Access),
7888 Make_Attribute_Reference (Loc,
7889 Prefix => New_Reference_To (Edef, Loc),
7890 Attribute_Name => Name_Unrestricted_Access))));
7891 end if;
7893 Next (Comp);
7894 end loop;
7895 end if;
7897 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
7898 -- all protected subprograms have been collected.
7900 if Has_Entries (Prot_Typ) then
7901 Body_Id :=
7902 Make_Defining_Identifier (Sloc (Prot_Typ),
7903 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
7905 case Corresponding_Runtime_Package (Prot_Typ) is
7906 when System_Tasking_Protected_Objects_Entries =>
7907 Body_Arr := Make_Object_Declaration (Loc,
7908 Defining_Identifier => Body_Id,
7909 Aliased_Present => True,
7910 Object_Definition =>
7911 Make_Subtype_Indication (Loc,
7912 Subtype_Mark => New_Reference_To (
7913 RTE (RE_Protected_Entry_Body_Array), Loc),
7914 Constraint =>
7915 Make_Index_Or_Discriminant_Constraint (Loc,
7916 Constraints => New_List (
7917 Make_Range (Loc,
7918 Make_Integer_Literal (Loc, 1),
7919 Make_Integer_Literal (Loc, E_Count))))),
7920 Expression => Entries_Aggr);
7922 when System_Tasking_Protected_Objects_Single_Entry =>
7923 Body_Arr := Make_Object_Declaration (Loc,
7924 Defining_Identifier => Body_Id,
7925 Aliased_Present => True,
7926 Object_Definition => New_Reference_To
7927 (RTE (RE_Entry_Body), Loc),
7928 Expression =>
7929 Make_Aggregate (Loc,
7930 Expressions => New_List (
7931 Make_Attribute_Reference (Loc,
7932 Prefix => New_Reference_To (Bdef, Loc),
7933 Attribute_Name => Name_Unrestricted_Access),
7934 Make_Attribute_Reference (Loc,
7935 Prefix => New_Reference_To (Edef, Loc),
7936 Attribute_Name => Name_Unrestricted_Access))));
7938 when others =>
7939 raise Program_Error;
7940 end case;
7942 -- A pointer to this array will be placed in the corresponding record
7943 -- by its initialization procedure so this needs to be analyzed here.
7945 Insert_After (Current_Node, Body_Arr);
7946 Current_Node := Body_Arr;
7947 Analyze (Body_Arr);
7949 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
7951 -- Finally, build the function that maps an entry index into the
7952 -- corresponding body. A pointer to this function is placed in each
7953 -- object of the type. Except for a ravenscar-like profile (no abort,
7954 -- no entry queue, 1 entry)
7956 if Corresponding_Runtime_Package (Prot_Typ) =
7957 System_Tasking_Protected_Objects_Entries
7958 then
7959 Sub :=
7960 Make_Subprogram_Declaration (Loc,
7961 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
7962 Insert_After (Current_Node, Sub);
7963 Analyze (Sub);
7964 end if;
7965 end if;
7966 end Expand_N_Protected_Type_Declaration;
7968 --------------------------------
7969 -- Expand_N_Requeue_Statement --
7970 --------------------------------
7972 -- A non-dispatching requeue statement is expanded into one of four GNARLI
7973 -- operations, depending on the source and destination (task or protected
7974 -- object). A dispatching requeue statement is expanded into a call to the
7975 -- predefined primitive _Disp_Requeue. In addition, code is generated to
7976 -- jump around the remainder of processing for the original entry and, if
7977 -- the destination is (different) protected object, to attempt to service
7978 -- it. The following illustrates the various cases:
7980 -- procedure entE
7981 -- (O : System.Address;
7982 -- P : System.Address;
7983 -- E : Protected_Entry_Index)
7984 -- is
7985 -- <discriminant renamings>
7986 -- <private object renamings>
7987 -- type poVP is access poV;
7988 -- _object : ptVP := ptVP!(O);
7990 -- begin
7991 -- begin
7992 -- <start of statement sequence for entry>
7994 -- -- Requeue from one protected entry body to another protected
7995 -- -- entry.
7997 -- Requeue_Protected_Entry (
7998 -- _object._object'Access,
7999 -- new._object'Access,
8000 -- E,
8001 -- Abort_Present);
8002 -- return;
8004 -- <some more of the statement sequence for entry>
8006 -- -- Requeue from an entry body to a task entry
8008 -- Requeue_Protected_To_Task_Entry (
8009 -- New._task_id,
8010 -- E,
8011 -- Abort_Present);
8012 -- return;
8014 -- <rest of statement sequence for entry>
8015 -- Complete_Entry_Body (_object._object);
8017 -- exception
8018 -- when all others =>
8019 -- Exceptional_Complete_Entry_Body (
8020 -- _object._object, Get_GNAT_Exception);
8021 -- end;
8022 -- end entE;
8024 -- Requeue of a task entry call to a task entry
8026 -- Accept_Call (E, Ann);
8027 -- <start of statement sequence for accept statement>
8028 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
8029 -- goto Lnn;
8030 -- <rest of statement sequence for accept statement>
8031 -- <<Lnn>>
8032 -- Complete_Rendezvous;
8034 -- exception
8035 -- when all others =>
8036 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8038 -- Requeue of a task entry call to a protected entry
8040 -- Accept_Call (E, Ann);
8041 -- <start of statement sequence for accept statement>
8042 -- Requeue_Task_To_Protected_Entry (
8043 -- new._object'Access,
8044 -- E,
8045 -- Abort_Present);
8046 -- newS (new, Pnn);
8047 -- goto Lnn;
8048 -- <rest of statement sequence for accept statement>
8049 -- <<Lnn>>
8050 -- Complete_Rendezvous;
8052 -- exception
8053 -- when all others =>
8054 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8056 -- Ada 2005 (AI05-0030): Dispatching requeue from protected to interface
8057 -- class-wide type:
8059 -- procedure entE
8060 -- (O : System.Address;
8061 -- P : System.Address;
8062 -- E : Protected_Entry_Index)
8063 -- is
8064 -- <discriminant renamings>
8065 -- <private object renamings>
8066 -- type poVP is access poV;
8067 -- _object : ptVP := ptVP!(O);
8069 -- begin
8070 -- begin
8071 -- <start of statement sequence for entry>
8073 -- _Disp_Requeue
8074 -- (<interface class-wide object>,
8075 -- True,
8076 -- _object'Address,
8077 -- Ada.Tags.Get_Offset_Index
8078 -- (Tag (_object),
8079 -- <interface dispatch table index of target entry>),
8080 -- Abort_Present);
8081 -- return;
8083 -- <rest of statement sequence for entry>
8084 -- Complete_Entry_Body (_object._object);
8086 -- exception
8087 -- when all others =>
8088 -- Exceptional_Complete_Entry_Body (
8089 -- _object._object, Get_GNAT_Exception);
8090 -- end;
8091 -- end entE;
8093 -- Ada 2005 (AI05-0030): Dispatching requeue from task to interface
8094 -- class-wide type:
8096 -- Accept_Call (E, Ann);
8097 -- <start of statement sequence for accept statement>
8098 -- _Disp_Requeue
8099 -- (<interface class-wide object>,
8100 -- False,
8101 -- null,
8102 -- Ada.Tags.Get_Offset_Index
8103 -- (Tag (_object),
8104 -- <interface dispatch table index of target entrt>),
8105 -- Abort_Present);
8106 -- newS (new, Pnn);
8107 -- goto Lnn;
8108 -- <rest of statement sequence for accept statement>
8109 -- <<Lnn>>
8110 -- Complete_Rendezvous;
8112 -- exception
8113 -- when all others =>
8114 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8116 -- Further details on these expansions can be found in Expand_N_Protected_
8117 -- Body and Expand_N_Accept_Statement.
8119 procedure Expand_N_Requeue_Statement (N : Node_Id) is
8120 Loc : constant Source_Ptr := Sloc (N);
8121 Abortable : Node_Id;
8122 Acc_Stat : Node_Id;
8123 Conc_Typ : Entity_Id;
8124 Concval : Node_Id;
8125 Ename : Node_Id;
8126 Index : Node_Id;
8127 Lab_Node : Node_Id;
8128 New_Param : Node_Id;
8129 Old_Typ : Entity_Id;
8130 Params : List_Id;
8131 Rcall : Node_Id;
8132 RTS_Call : Entity_Id;
8133 Self_Param : Node_Id;
8134 Skip_Stat : Node_Id;
8136 begin
8137 Abortable :=
8138 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc);
8140 -- Extract the components of the entry call
8142 Extract_Entry (N, Concval, Ename, Index);
8143 Conc_Typ := Etype (Concval);
8145 -- Examine the scope stack in order to find nearest enclosing protected
8146 -- or task type. This will constitute our invocation source.
8148 Old_Typ := Current_Scope;
8149 while Present (Old_Typ)
8150 and then not Is_Protected_Type (Old_Typ)
8151 and then not Is_Task_Type (Old_Typ)
8152 loop
8153 Old_Typ := Scope (Old_Typ);
8154 end loop;
8156 -- Generate the parameter list for all cases. The abortable flag is
8157 -- common among dispatching and regular requeue.
8159 Params := New_List (Abortable);
8161 -- Ada 2005 (AI05-0030): We have a dispatching requeue of the form
8162 -- Concval.Ename where the type of Concval is class-wide concurrent
8163 -- interface.
8165 if Ada_Version >= Ada_05
8166 and then Present (Concval)
8167 and then Is_Class_Wide_Type (Conc_Typ)
8168 and then Is_Concurrent_Interface (Conc_Typ)
8169 then
8170 RTS_Call := Make_Identifier (Loc, Name_uDisp_Requeue);
8172 -- Generate:
8173 -- Ada.Tags.Get_Offset_Index
8174 -- (Ada.Tags.Tag (Concval),
8175 -- <interface dispatch table position of Ename>)
8177 Prepend_To (Params,
8178 Make_Function_Call (Loc,
8179 Name =>
8180 New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
8181 Parameter_Associations =>
8182 New_List (
8183 Unchecked_Convert_To (RTE (RE_Tag), Concval),
8184 Make_Integer_Literal (Loc, DT_Position (Entity (Ename))))));
8186 -- Specific actuals for protected to interface class-wide type
8187 -- requeue.
8189 if Is_Protected_Type (Old_Typ) then
8190 Prepend_To (Params,
8191 Make_Attribute_Reference (Loc, -- _object'Address
8192 Prefix =>
8193 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
8194 Attribute_Name =>
8195 Name_Address));
8196 Prepend_To (Params, -- True
8197 New_Reference_To (Standard_True, Loc));
8199 -- Specific actuals for task to interface class-wide type requeue
8201 else
8202 pragma Assert (Is_Task_Type (Old_Typ));
8204 Prepend_To (Params, -- null
8205 New_Reference_To (RTE (RE_Null_Address), Loc));
8206 Prepend_To (Params, -- False
8207 New_Reference_To (Standard_False, Loc));
8208 end if;
8210 -- Finally, add the common object parameter
8212 Prepend_To (Params, New_Copy_Tree (Concval));
8214 -- Regular requeue processing
8216 else
8217 New_Param := Concurrent_Ref (Concval);
8219 -- The index expression is common among all four cases
8221 Prepend_To (Params,
8222 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
8224 if Is_Protected_Type (Old_Typ) then
8225 Self_Param :=
8226 Make_Attribute_Reference (Loc,
8227 Prefix =>
8228 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
8229 Attribute_Name =>
8230 Name_Unchecked_Access);
8232 -- Protected to protected requeue
8234 if Is_Protected_Type (Conc_Typ) then
8235 RTS_Call :=
8236 New_Reference_To (RTE (RE_Requeue_Protected_Entry), Loc);
8238 New_Param :=
8239 Make_Attribute_Reference (Loc,
8240 Prefix =>
8241 New_Param,
8242 Attribute_Name =>
8243 Name_Unchecked_Access);
8245 -- Protected to task requeue
8247 else
8248 pragma Assert (Is_Task_Type (Conc_Typ));
8249 RTS_Call :=
8250 New_Reference_To (
8251 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
8252 end if;
8254 Prepend (New_Param, Params);
8255 Prepend (Self_Param, Params);
8257 else
8258 pragma Assert (Is_Task_Type (Old_Typ));
8260 -- Task to protected requeue
8262 if Is_Protected_Type (Conc_Typ) then
8263 RTS_Call :=
8264 New_Reference_To (
8265 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
8267 New_Param :=
8268 Make_Attribute_Reference (Loc,
8269 Prefix =>
8270 New_Param,
8271 Attribute_Name =>
8272 Name_Unchecked_Access);
8274 -- Task to task requeue
8276 else
8277 pragma Assert (Is_Task_Type (Conc_Typ));
8278 RTS_Call :=
8279 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
8280 end if;
8282 Prepend (New_Param, Params);
8283 end if;
8284 end if;
8286 -- Create the GNARLI or predefined primitive call
8288 Rcall :=
8289 Make_Procedure_Call_Statement (Loc,
8290 Name => RTS_Call,
8291 Parameter_Associations => Params);
8293 Rewrite (N, Rcall);
8294 Analyze (N);
8296 if Is_Protected_Type (Old_Typ) then
8298 -- Build the return statement to skip the rest of the entry body
8300 Skip_Stat := Make_Simple_Return_Statement (Loc);
8302 else
8303 -- If the requeue is within a task, find the end label of the
8304 -- enclosing accept statement.
8306 Acc_Stat := Parent (N);
8307 while Nkind (Acc_Stat) /= N_Accept_Statement loop
8308 Acc_Stat := Parent (Acc_Stat);
8309 end loop;
8311 -- The last statement is the second label, used for completing the
8312 -- rendezvous the usual way. The label we are looking for is right
8313 -- before it.
8315 Lab_Node :=
8316 Prev (Last (Statements (Handled_Statement_Sequence (Acc_Stat))));
8318 pragma Assert (Nkind (Lab_Node) = N_Label);
8320 -- Build the goto statement to skip the rest of the accept
8321 -- statement.
8323 Skip_Stat :=
8324 Make_Goto_Statement (Loc,
8325 Name => New_Occurrence_Of (Entity (Identifier (Lab_Node)), Loc));
8326 end if;
8328 Set_Analyzed (Skip_Stat);
8330 Insert_After (N, Skip_Stat);
8331 end Expand_N_Requeue_Statement;
8333 -------------------------------
8334 -- Expand_N_Selective_Accept --
8335 -------------------------------
8337 procedure Expand_N_Selective_Accept (N : Node_Id) is
8338 Loc : constant Source_Ptr := Sloc (N);
8339 Alts : constant List_Id := Select_Alternatives (N);
8341 -- Note: in the below declarations a lot of new lists are allocated
8342 -- unconditionally which may well not end up being used. That's
8343 -- not a good idea since it wastes space gratuitously ???
8345 Accept_Case : List_Id;
8346 Accept_List : constant List_Id := New_List;
8348 Alt : Node_Id;
8349 Alt_List : constant List_Id := New_List;
8350 Alt_Stats : List_Id;
8351 Ann : Entity_Id := Empty;
8353 Block : Node_Id;
8354 Check_Guard : Boolean := True;
8356 Decls : constant List_Id := New_List;
8357 Stats : constant List_Id := New_List;
8358 Body_List : constant List_Id := New_List;
8359 Trailing_List : constant List_Id := New_List;
8361 Choices : List_Id;
8362 Else_Present : Boolean := False;
8363 Terminate_Alt : Node_Id := Empty;
8364 Select_Mode : Node_Id;
8366 Delay_Case : List_Id;
8367 Delay_Count : Integer := 0;
8368 Delay_Val : Entity_Id;
8369 Delay_Index : Entity_Id;
8370 Delay_Min : Entity_Id;
8371 Delay_Num : Int := 1;
8372 Delay_Alt_List : List_Id := New_List;
8373 Delay_List : constant List_Id := New_List;
8374 D : Entity_Id;
8375 M : Entity_Id;
8377 First_Delay : Boolean := True;
8378 Guard_Open : Entity_Id;
8380 End_Lab : Node_Id;
8381 Index : Int := 1;
8382 Lab : Node_Id;
8383 Num_Alts : Int;
8384 Num_Accept : Nat := 0;
8385 Proc : Node_Id;
8386 Q : Node_Id;
8387 Time_Type : Entity_Id;
8388 X : Node_Id;
8389 Select_Call : Node_Id;
8391 Qnam : constant Entity_Id :=
8392 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
8394 Xnam : constant Entity_Id :=
8395 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
8397 -----------------------
8398 -- Local subprograms --
8399 -----------------------
8401 function Accept_Or_Raise return List_Id;
8402 -- For the rare case where delay alternatives all have guards, and
8403 -- all of them are closed, it is still possible that there were open
8404 -- accept alternatives with no callers. We must reexamine the
8405 -- Accept_List, and execute a selective wait with no else if some
8406 -- accept is open. If none, we raise program_error.
8408 procedure Add_Accept (Alt : Node_Id);
8409 -- Process a single accept statement in a select alternative. Build
8410 -- procedure for body of accept, and add entry to dispatch table with
8411 -- expression for guard, in preparation for call to run time select.
8413 function Make_And_Declare_Label (Num : Int) return Node_Id;
8414 -- Manufacture a label using Num as a serial number and declare it.
8415 -- The declaration is appended to Decls. The label marks the trailing
8416 -- statements of an accept or delay alternative.
8418 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
8419 -- Build call to Selective_Wait runtime routine
8421 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
8422 -- Add code to compare value of delay with previous values, and
8423 -- generate case entry for trailing statements.
8425 procedure Process_Accept_Alternative
8426 (Alt : Node_Id;
8427 Index : Int;
8428 Proc : Node_Id);
8429 -- Add code to call corresponding procedure, and branch to
8430 -- trailing statements, if any.
8432 ---------------------
8433 -- Accept_Or_Raise --
8434 ---------------------
8436 function Accept_Or_Raise return List_Id is
8437 Cond : Node_Id;
8438 Stats : List_Id;
8439 J : constant Entity_Id := Make_Defining_Identifier (Loc,
8440 New_Internal_Name ('J'));
8442 begin
8443 -- We generate the following:
8445 -- for J in q'range loop
8446 -- if q(J).S /=null_task_entry then
8447 -- selective_wait (simple_mode,...);
8448 -- done := True;
8449 -- exit;
8450 -- end if;
8451 -- end loop;
8453 -- if no rendez_vous then
8454 -- raise program_error;
8455 -- end if;
8457 -- Note that the code needs to know that the selector name
8458 -- in an Accept_Alternative is named S.
8460 Cond := Make_Op_Ne (Loc,
8461 Left_Opnd =>
8462 Make_Selected_Component (Loc,
8463 Prefix => Make_Indexed_Component (Loc,
8464 Prefix => New_Reference_To (Qnam, Loc),
8465 Expressions => New_List (New_Reference_To (J, Loc))),
8466 Selector_Name => Make_Identifier (Loc, Name_S)),
8467 Right_Opnd =>
8468 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
8470 Stats := New_List (
8471 Make_Implicit_Loop_Statement (N,
8472 Identifier => Empty,
8473 Iteration_Scheme =>
8474 Make_Iteration_Scheme (Loc,
8475 Loop_Parameter_Specification =>
8476 Make_Loop_Parameter_Specification (Loc,
8477 Defining_Identifier => J,
8478 Discrete_Subtype_Definition =>
8479 Make_Attribute_Reference (Loc,
8480 Prefix => New_Reference_To (Qnam, Loc),
8481 Attribute_Name => Name_Range,
8482 Expressions => New_List (
8483 Make_Integer_Literal (Loc, 1))))),
8485 Statements => New_List (
8486 Make_Implicit_If_Statement (N,
8487 Condition => Cond,
8488 Then_Statements => New_List (
8489 Make_Select_Call (
8490 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
8491 Make_Exit_Statement (Loc))))));
8493 Append_To (Stats,
8494 Make_Raise_Program_Error (Loc,
8495 Condition => Make_Op_Eq (Loc,
8496 Left_Opnd => New_Reference_To (Xnam, Loc),
8497 Right_Opnd =>
8498 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
8499 Reason => PE_All_Guards_Closed));
8501 return Stats;
8502 end Accept_Or_Raise;
8504 ----------------
8505 -- Add_Accept --
8506 ----------------
8508 procedure Add_Accept (Alt : Node_Id) is
8509 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
8510 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
8511 Eent : constant Entity_Id := Entity (Ename);
8512 Index : constant Node_Id := Entry_Index (Acc_Stm);
8513 Null_Body : Node_Id;
8514 Proc_Body : Node_Id;
8515 PB_Ent : Entity_Id;
8516 Expr : Node_Id;
8517 Call : Node_Id;
8519 begin
8520 if No (Ann) then
8521 Ann := Node (Last_Elmt (Accept_Address (Eent)));
8522 end if;
8524 if Present (Condition (Alt)) then
8525 Expr :=
8526 Make_Conditional_Expression (Loc, New_List (
8527 Condition (Alt),
8528 Entry_Index_Expression (Loc, Eent, Index, Scope (Eent)),
8529 New_Reference_To (RTE (RE_Null_Task_Entry), Loc)));
8530 else
8531 Expr :=
8532 Entry_Index_Expression
8533 (Loc, Eent, Index, Scope (Eent));
8534 end if;
8536 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
8537 Null_Body := New_Reference_To (Standard_False, Loc);
8539 if Abort_Allowed then
8540 Call := Make_Procedure_Call_Statement (Loc,
8541 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc));
8542 Insert_Before (First (Statements (Handled_Statement_Sequence (
8543 Accept_Statement (Alt)))), Call);
8544 Analyze (Call);
8545 end if;
8547 PB_Ent :=
8548 Make_Defining_Identifier (Sloc (Ename),
8549 New_External_Name (Chars (Ename), 'A', Num_Accept));
8551 if Comes_From_Source (Alt) then
8552 Set_Debug_Info_Needed (PB_Ent);
8553 end if;
8555 Proc_Body :=
8556 Make_Subprogram_Body (Loc,
8557 Specification =>
8558 Make_Procedure_Specification (Loc,
8559 Defining_Unit_Name => PB_Ent),
8560 Declarations => Declarations (Acc_Stm),
8561 Handled_Statement_Sequence =>
8562 Build_Accept_Body (Accept_Statement (Alt)));
8564 -- During the analysis of the body of the accept statement, any
8565 -- zero cost exception handler records were collected in the
8566 -- Accept_Handler_Records field of the N_Accept_Alternative node.
8567 -- This is where we move them to where they belong, namely the
8568 -- newly created procedure.
8570 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
8571 Append (Proc_Body, Body_List);
8573 else
8574 Null_Body := New_Reference_To (Standard_True, Loc);
8576 -- if accept statement has declarations, insert above, given that
8577 -- we are not creating a body for the accept.
8579 if Present (Declarations (Acc_Stm)) then
8580 Insert_Actions (N, Declarations (Acc_Stm));
8581 end if;
8582 end if;
8584 Append_To (Accept_List,
8585 Make_Aggregate (Loc, Expressions => New_List (Null_Body, Expr)));
8587 Num_Accept := Num_Accept + 1;
8588 end Add_Accept;
8590 ----------------------------
8591 -- Make_And_Declare_Label --
8592 ----------------------------
8594 function Make_And_Declare_Label (Num : Int) return Node_Id is
8595 Lab_Id : Node_Id;
8597 begin
8598 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
8599 Lab :=
8600 Make_Label (Loc, Lab_Id);
8602 Append_To (Decls,
8603 Make_Implicit_Label_Declaration (Loc,
8604 Defining_Identifier =>
8605 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
8606 Label_Construct => Lab));
8608 return Lab;
8609 end Make_And_Declare_Label;
8611 ----------------------
8612 -- Make_Select_Call --
8613 ----------------------
8615 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
8616 Params : constant List_Id := New_List;
8618 begin
8619 Append (
8620 Make_Attribute_Reference (Loc,
8621 Prefix => New_Reference_To (Qnam, Loc),
8622 Attribute_Name => Name_Unchecked_Access),
8623 Params);
8624 Append (Select_Mode, Params);
8625 Append (New_Reference_To (Ann, Loc), Params);
8626 Append (New_Reference_To (Xnam, Loc), Params);
8628 return
8629 Make_Procedure_Call_Statement (Loc,
8630 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
8631 Parameter_Associations => Params);
8632 end Make_Select_Call;
8634 --------------------------------
8635 -- Process_Accept_Alternative --
8636 --------------------------------
8638 procedure Process_Accept_Alternative
8639 (Alt : Node_Id;
8640 Index : Int;
8641 Proc : Node_Id)
8643 Choices : List_Id := No_List;
8644 Alt_Stats : List_Id;
8646 begin
8647 Adjust_Condition (Condition (Alt));
8648 Alt_Stats := No_List;
8650 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
8651 Choices := New_List (
8652 Make_Integer_Literal (Loc, Index));
8654 Alt_Stats := New_List (
8655 Make_Procedure_Call_Statement (Loc,
8656 Name => New_Reference_To (
8657 Defining_Unit_Name (Specification (Proc)), Loc)));
8658 end if;
8660 if Statements (Alt) /= Empty_List then
8662 if No (Alt_Stats) then
8664 -- Accept with no body, followed by trailing statements
8666 Choices := New_List (
8667 Make_Integer_Literal (Loc, Index));
8669 Alt_Stats := New_List;
8670 end if;
8672 -- After the call, if any, branch to to trailing statements. We
8673 -- create a label for each, as well as the corresponding label
8674 -- declaration.
8676 Lab := Make_And_Declare_Label (Index);
8677 Append_To (Alt_Stats,
8678 Make_Goto_Statement (Loc,
8679 Name => New_Copy (Identifier (Lab))));
8681 Append (Lab, Trailing_List);
8682 Append_List (Statements (Alt), Trailing_List);
8683 Append_To (Trailing_List,
8684 Make_Goto_Statement (Loc,
8685 Name => New_Copy (Identifier (End_Lab))));
8686 end if;
8688 if Present (Alt_Stats) then
8690 -- Procedure call. and/or trailing statements
8692 Append_To (Alt_List,
8693 Make_Case_Statement_Alternative (Loc,
8694 Discrete_Choices => Choices,
8695 Statements => Alt_Stats));
8696 end if;
8697 end Process_Accept_Alternative;
8699 -------------------------------
8700 -- Process_Delay_Alternative --
8701 -------------------------------
8703 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
8704 Choices : List_Id;
8705 Cond : Node_Id;
8706 Delay_Alt : List_Id;
8708 begin
8709 -- Deal with C/Fortran boolean as delay condition
8711 Adjust_Condition (Condition (Alt));
8713 -- Determine the smallest specified delay
8715 -- for each delay alternative generate:
8717 -- if guard-expression then
8718 -- Delay_Val := delay-expression;
8719 -- Guard_Open := True;
8720 -- if Delay_Val < Delay_Min then
8721 -- Delay_Min := Delay_Val;
8722 -- Delay_Index := Index;
8723 -- end if;
8724 -- end if;
8726 -- The enclosing if-statement is omitted if there is no guard
8728 if Delay_Count = 1
8729 or else First_Delay
8730 then
8731 First_Delay := False;
8733 Delay_Alt := New_List (
8734 Make_Assignment_Statement (Loc,
8735 Name => New_Reference_To (Delay_Min, Loc),
8736 Expression => Expression (Delay_Statement (Alt))));
8738 if Delay_Count > 1 then
8739 Append_To (Delay_Alt,
8740 Make_Assignment_Statement (Loc,
8741 Name => New_Reference_To (Delay_Index, Loc),
8742 Expression => Make_Integer_Literal (Loc, Index)));
8743 end if;
8745 else
8746 Delay_Alt := New_List (
8747 Make_Assignment_Statement (Loc,
8748 Name => New_Reference_To (Delay_Val, Loc),
8749 Expression => Expression (Delay_Statement (Alt))));
8751 if Time_Type = Standard_Duration then
8752 Cond :=
8753 Make_Op_Lt (Loc,
8754 Left_Opnd => New_Reference_To (Delay_Val, Loc),
8755 Right_Opnd => New_Reference_To (Delay_Min, Loc));
8757 else
8758 -- The scope of the time type must define a comparison
8759 -- operator. The scope itself may not be visible, so we
8760 -- construct a node with entity information to insure that
8761 -- semantic analysis can find the proper operator.
8763 Cond :=
8764 Make_Function_Call (Loc,
8765 Name => Make_Selected_Component (Loc,
8766 Prefix => New_Reference_To (Scope (Time_Type), Loc),
8767 Selector_Name =>
8768 Make_Operator_Symbol (Loc,
8769 Chars => Name_Op_Lt,
8770 Strval => No_String)),
8771 Parameter_Associations =>
8772 New_List (
8773 New_Reference_To (Delay_Val, Loc),
8774 New_Reference_To (Delay_Min, Loc)));
8776 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
8777 end if;
8779 Append_To (Delay_Alt,
8780 Make_Implicit_If_Statement (N,
8781 Condition => Cond,
8782 Then_Statements => New_List (
8783 Make_Assignment_Statement (Loc,
8784 Name => New_Reference_To (Delay_Min, Loc),
8785 Expression => New_Reference_To (Delay_Val, Loc)),
8787 Make_Assignment_Statement (Loc,
8788 Name => New_Reference_To (Delay_Index, Loc),
8789 Expression => Make_Integer_Literal (Loc, Index)))));
8790 end if;
8792 if Check_Guard then
8793 Append_To (Delay_Alt,
8794 Make_Assignment_Statement (Loc,
8795 Name => New_Reference_To (Guard_Open, Loc),
8796 Expression => New_Reference_To (Standard_True, Loc)));
8797 end if;
8799 if Present (Condition (Alt)) then
8800 Delay_Alt := New_List (
8801 Make_Implicit_If_Statement (N,
8802 Condition => Condition (Alt),
8803 Then_Statements => Delay_Alt));
8804 end if;
8806 Append_List (Delay_Alt, Delay_List);
8808 -- If the delay alternative has a statement part, add choice to the
8809 -- case statements for delays.
8811 if Present (Statements (Alt)) then
8813 if Delay_Count = 1 then
8814 Append_List (Statements (Alt), Delay_Alt_List);
8816 else
8817 Choices := New_List (
8818 Make_Integer_Literal (Loc, Index));
8820 Append_To (Delay_Alt_List,
8821 Make_Case_Statement_Alternative (Loc,
8822 Discrete_Choices => Choices,
8823 Statements => Statements (Alt)));
8824 end if;
8826 elsif Delay_Count = 1 then
8828 -- If the single delay has no trailing statements, add a branch
8829 -- to the exit label to the selective wait.
8831 Delay_Alt_List := New_List (
8832 Make_Goto_Statement (Loc,
8833 Name => New_Copy (Identifier (End_Lab))));
8835 end if;
8836 end Process_Delay_Alternative;
8838 -- Start of processing for Expand_N_Selective_Accept
8840 begin
8841 -- First insert some declarations before the select. The first is:
8843 -- Ann : Address
8845 -- This variable holds the parameters passed to the accept body. This
8846 -- declaration has already been inserted by the time we get here by
8847 -- a call to Expand_Accept_Declarations made from the semantics when
8848 -- processing the first accept statement contained in the select. We
8849 -- can find this entity as Accept_Address (E), where E is any of the
8850 -- entries references by contained accept statements.
8852 -- The first step is to scan the list of Selective_Accept_Statements
8853 -- to find this entity, and also count the number of accepts, and
8854 -- determine if terminated, delay or else is present:
8856 Num_Alts := 0;
8858 Alt := First (Alts);
8859 while Present (Alt) loop
8861 if Nkind (Alt) = N_Accept_Alternative then
8862 Add_Accept (Alt);
8864 elsif Nkind (Alt) = N_Delay_Alternative then
8865 Delay_Count := Delay_Count + 1;
8867 -- If the delays are relative delays, the delay expressions have
8868 -- type Standard_Duration. Otherwise they must have some time type
8869 -- recognized by GNAT.
8871 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
8872 Time_Type := Standard_Duration;
8873 else
8874 Time_Type := Etype (Expression (Delay_Statement (Alt)));
8876 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
8877 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
8878 then
8879 null;
8880 else
8881 Error_Msg_NE (
8882 "& is not a time type (RM 9.6(6))",
8883 Expression (Delay_Statement (Alt)), Time_Type);
8884 Time_Type := Standard_Duration;
8885 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
8886 end if;
8887 end if;
8889 if No (Condition (Alt)) then
8891 -- This guard will always be open
8893 Check_Guard := False;
8894 end if;
8896 elsif Nkind (Alt) = N_Terminate_Alternative then
8897 Adjust_Condition (Condition (Alt));
8898 Terminate_Alt := Alt;
8899 end if;
8901 Num_Alts := Num_Alts + 1;
8902 Next (Alt);
8903 end loop;
8905 Else_Present := Present (Else_Statements (N));
8907 -- At the same time (see procedure Add_Accept) we build the accept list:
8909 -- Qnn : Accept_List (1 .. num-select) := (
8910 -- (null-body, entry-index),
8911 -- (null-body, entry-index),
8912 -- ..
8913 -- (null_body, entry-index));
8915 -- In the above declaration, null-body is True if the corresponding
8916 -- accept has no body, and false otherwise. The entry is either the
8917 -- entry index expression if there is no guard, or if a guard is
8918 -- present, then a conditional expression of the form:
8920 -- (if guard then entry-index else Null_Task_Entry)
8922 -- If a guard is statically known to be false, the entry can simply
8923 -- be omitted from the accept list.
8925 Q :=
8926 Make_Object_Declaration (Loc,
8927 Defining_Identifier => Qnam,
8928 Object_Definition =>
8929 New_Reference_To (RTE (RE_Accept_List), Loc),
8930 Aliased_Present => True,
8932 Expression =>
8933 Make_Qualified_Expression (Loc,
8934 Subtype_Mark =>
8935 New_Reference_To (RTE (RE_Accept_List), Loc),
8936 Expression =>
8937 Make_Aggregate (Loc, Expressions => Accept_List)));
8939 Append (Q, Decls);
8941 -- Then we declare the variable that holds the index for the accept
8942 -- that will be selected for service:
8944 -- Xnn : Select_Index;
8946 X :=
8947 Make_Object_Declaration (Loc,
8948 Defining_Identifier => Xnam,
8949 Object_Definition =>
8950 New_Reference_To (RTE (RE_Select_Index), Loc),
8951 Expression =>
8952 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
8954 Append (X, Decls);
8956 -- After this follow procedure declarations for each accept body
8958 -- procedure Pnn is
8959 -- begin
8960 -- ...
8961 -- end;
8963 -- where the ... are statements from the corresponding procedure body.
8964 -- No parameters are involved, since the parameters are passed via Ann
8965 -- and the parameter references have already been expanded to be direct
8966 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
8967 -- any embedded tasking statements (which would normally be illegal in
8968 -- procedures), have been converted to calls to the tasking runtime so
8969 -- there is no problem in putting them into procedures.
8971 -- The original accept statement has been expanded into a block in
8972 -- the same fashion as for simple accepts (see Build_Accept_Body).
8974 -- Note: we don't really need to build these procedures for the case
8975 -- where no delay statement is present, but it is just as easy to
8976 -- build them unconditionally, and not significantly inefficient,
8977 -- since if they are short they will be inlined anyway.
8979 -- The procedure declarations have been assembled in Body_List
8981 -- If delays are present, we must compute the required delay.
8982 -- We first generate the declarations:
8984 -- Delay_Index : Boolean := 0;
8985 -- Delay_Min : Some_Time_Type.Time;
8986 -- Delay_Val : Some_Time_Type.Time;
8988 -- Delay_Index will be set to the index of the minimum delay, i.e. the
8989 -- active delay that is actually chosen as the basis for the possible
8990 -- delay if an immediate rendez-vous is not possible.
8992 -- In the most common case there is a single delay statement, and this
8993 -- is handled specially.
8995 if Delay_Count > 0 then
8997 -- Generate the required declarations
8999 Delay_Val :=
9000 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
9001 Delay_Index :=
9002 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
9003 Delay_Min :=
9004 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
9006 Append_To (Decls,
9007 Make_Object_Declaration (Loc,
9008 Defining_Identifier => Delay_Val,
9009 Object_Definition => New_Reference_To (Time_Type, Loc)));
9011 Append_To (Decls,
9012 Make_Object_Declaration (Loc,
9013 Defining_Identifier => Delay_Index,
9014 Object_Definition => New_Reference_To (Standard_Integer, Loc),
9015 Expression => Make_Integer_Literal (Loc, 0)));
9017 Append_To (Decls,
9018 Make_Object_Declaration (Loc,
9019 Defining_Identifier => Delay_Min,
9020 Object_Definition => New_Reference_To (Time_Type, Loc),
9021 Expression =>
9022 Unchecked_Convert_To (Time_Type,
9023 Make_Attribute_Reference (Loc,
9024 Prefix =>
9025 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
9026 Attribute_Name => Name_Last))));
9028 -- Create Duration and Delay_Mode objects used for passing a delay
9029 -- value to RTS
9031 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
9032 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
9034 declare
9035 Discr : Entity_Id;
9037 begin
9038 -- Note that these values are defined in s-osprim.ads and must
9039 -- be kept in sync:
9041 -- Relative : constant := 0;
9042 -- Absolute_Calendar : constant := 1;
9043 -- Absolute_RT : constant := 2;
9045 if Time_Type = Standard_Duration then
9046 Discr := Make_Integer_Literal (Loc, 0);
9048 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
9049 Discr := Make_Integer_Literal (Loc, 1);
9051 else
9052 pragma Assert
9053 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
9054 Discr := Make_Integer_Literal (Loc, 2);
9055 end if;
9057 Append_To (Decls,
9058 Make_Object_Declaration (Loc,
9059 Defining_Identifier => D,
9060 Object_Definition =>
9061 New_Reference_To (Standard_Duration, Loc)));
9063 Append_To (Decls,
9064 Make_Object_Declaration (Loc,
9065 Defining_Identifier => M,
9066 Object_Definition =>
9067 New_Reference_To (Standard_Integer, Loc),
9068 Expression => Discr));
9069 end;
9071 if Check_Guard then
9072 Guard_Open :=
9073 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
9075 Append_To (Decls,
9076 Make_Object_Declaration (Loc,
9077 Defining_Identifier => Guard_Open,
9078 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
9079 Expression => New_Reference_To (Standard_False, Loc)));
9080 end if;
9082 -- Delay_Count is zero, don't need M and D set (suppress warning)
9084 else
9085 M := Empty;
9086 D := Empty;
9087 end if;
9089 if Present (Terminate_Alt) then
9091 -- If the terminate alternative guard is False, use
9092 -- Simple_Mode; otherwise use Terminate_Mode.
9094 if Present (Condition (Terminate_Alt)) then
9095 Select_Mode := Make_Conditional_Expression (Loc,
9096 New_List (Condition (Terminate_Alt),
9097 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
9098 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
9099 else
9100 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
9101 end if;
9103 elsif Else_Present or Delay_Count > 0 then
9104 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
9106 else
9107 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
9108 end if;
9110 Select_Call := Make_Select_Call (Select_Mode);
9111 Append (Select_Call, Stats);
9113 -- Now generate code to act on the result. There is an entry
9114 -- in this case for each accept statement with a non-null body,
9115 -- followed by a branch to the statements that follow the Accept.
9116 -- In the absence of delay alternatives, we generate:
9118 -- case X is
9119 -- when No_Rendezvous => -- omitted if simple mode
9120 -- goto Lab0;
9122 -- when 1 =>
9123 -- P1n;
9124 -- goto Lab1;
9126 -- when 2 =>
9127 -- P2n;
9128 -- goto Lab2;
9130 -- when others =>
9131 -- goto Exit;
9132 -- end case;
9134 -- Lab0: Else_Statements;
9135 -- goto exit;
9137 -- Lab1: Trailing_Statements1;
9138 -- goto Exit;
9140 -- Lab2: Trailing_Statements2;
9141 -- goto Exit;
9142 -- ...
9143 -- Exit:
9145 -- Generate label for common exit
9147 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
9149 -- First entry is the default case, when no rendezvous is possible
9151 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
9153 if Else_Present then
9155 -- If no rendezvous is possible, the else part is executed
9157 Lab := Make_And_Declare_Label (0);
9158 Alt_Stats := New_List (
9159 Make_Goto_Statement (Loc,
9160 Name => New_Copy (Identifier (Lab))));
9162 Append (Lab, Trailing_List);
9163 Append_List (Else_Statements (N), Trailing_List);
9164 Append_To (Trailing_List,
9165 Make_Goto_Statement (Loc,
9166 Name => New_Copy (Identifier (End_Lab))));
9167 else
9168 Alt_Stats := New_List (
9169 Make_Goto_Statement (Loc,
9170 Name => New_Copy (Identifier (End_Lab))));
9171 end if;
9173 Append_To (Alt_List,
9174 Make_Case_Statement_Alternative (Loc,
9175 Discrete_Choices => Choices,
9176 Statements => Alt_Stats));
9178 -- We make use of the fact that Accept_Index is an integer type, and
9179 -- generate successive literals for entries for each accept. Only those
9180 -- for which there is a body or trailing statements get a case entry.
9182 Alt := First (Select_Alternatives (N));
9183 Proc := First (Body_List);
9184 while Present (Alt) loop
9186 if Nkind (Alt) = N_Accept_Alternative then
9187 Process_Accept_Alternative (Alt, Index, Proc);
9188 Index := Index + 1;
9190 if Present
9191 (Handled_Statement_Sequence (Accept_Statement (Alt)))
9192 then
9193 Next (Proc);
9194 end if;
9196 elsif Nkind (Alt) = N_Delay_Alternative then
9197 Process_Delay_Alternative (Alt, Delay_Num);
9198 Delay_Num := Delay_Num + 1;
9199 end if;
9201 Next (Alt);
9202 end loop;
9204 -- An others choice is always added to the main case, as well
9205 -- as the delay case (to satisfy the compiler).
9207 Append_To (Alt_List,
9208 Make_Case_Statement_Alternative (Loc,
9209 Discrete_Choices =>
9210 New_List (Make_Others_Choice (Loc)),
9211 Statements =>
9212 New_List (Make_Goto_Statement (Loc,
9213 Name => New_Copy (Identifier (End_Lab))))));
9215 Accept_Case := New_List (
9216 Make_Case_Statement (Loc,
9217 Expression => New_Reference_To (Xnam, Loc),
9218 Alternatives => Alt_List));
9220 Append_List (Trailing_List, Accept_Case);
9221 Append (End_Lab, Accept_Case);
9222 Append_List (Body_List, Decls);
9224 -- Construct case statement for trailing statements of delay
9225 -- alternatives, if there are several of them.
9227 if Delay_Count > 1 then
9228 Append_To (Delay_Alt_List,
9229 Make_Case_Statement_Alternative (Loc,
9230 Discrete_Choices =>
9231 New_List (Make_Others_Choice (Loc)),
9232 Statements =>
9233 New_List (Make_Null_Statement (Loc))));
9235 Delay_Case := New_List (
9236 Make_Case_Statement (Loc,
9237 Expression => New_Reference_To (Delay_Index, Loc),
9238 Alternatives => Delay_Alt_List));
9239 else
9240 Delay_Case := Delay_Alt_List;
9241 end if;
9243 -- If there are no delay alternatives, we append the case statement
9244 -- to the statement list.
9246 if Delay_Count = 0 then
9247 Append_List (Accept_Case, Stats);
9249 -- Delay alternatives present
9251 else
9252 -- If delay alternatives are present we generate:
9254 -- find minimum delay.
9255 -- DX := minimum delay;
9256 -- M := <delay mode>;
9257 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
9258 -- DX, MX, X);
9260 -- if X = No_Rendezvous then
9261 -- case statement for delay statements.
9262 -- else
9263 -- case statement for accept alternatives.
9264 -- end if;
9266 declare
9267 Cases : Node_Id;
9268 Stmt : Node_Id;
9269 Parms : List_Id;
9270 Parm : Node_Id;
9271 Conv : Node_Id;
9273 begin
9274 -- The type of the delay expression is known to be legal
9276 if Time_Type = Standard_Duration then
9277 Conv := New_Reference_To (Delay_Min, Loc);
9279 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
9280 Conv := Make_Function_Call (Loc,
9281 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
9282 New_List (New_Reference_To (Delay_Min, Loc)));
9284 else
9285 pragma Assert
9286 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
9288 Conv := Make_Function_Call (Loc,
9289 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
9290 New_List (New_Reference_To (Delay_Min, Loc)));
9291 end if;
9293 Stmt := Make_Assignment_Statement (Loc,
9294 Name => New_Reference_To (D, Loc),
9295 Expression => Conv);
9297 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
9299 Parms := Parameter_Associations (Select_Call);
9300 Parm := First (Parms);
9302 while Present (Parm)
9303 and then Parm /= Select_Mode
9304 loop
9305 Next (Parm);
9306 end loop;
9308 pragma Assert (Present (Parm));
9309 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
9310 Analyze (Parm);
9312 -- Prepare two new parameters of Duration and Delay_Mode type
9313 -- which represent the value and the mode of the minimum delay.
9315 Next (Parm);
9316 Insert_After (Parm, New_Reference_To (M, Loc));
9317 Insert_After (Parm, New_Reference_To (D, Loc));
9319 -- Create a call to RTS
9321 Rewrite (Select_Call,
9322 Make_Procedure_Call_Statement (Loc,
9323 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
9324 Parameter_Associations => Parms));
9326 -- This new call should follow the calculation of the minimum
9327 -- delay.
9329 Insert_List_Before (Select_Call, Delay_List);
9331 if Check_Guard then
9332 Stmt :=
9333 Make_Implicit_If_Statement (N,
9334 Condition => New_Reference_To (Guard_Open, Loc),
9335 Then_Statements =>
9336 New_List (New_Copy_Tree (Stmt),
9337 New_Copy_Tree (Select_Call)),
9338 Else_Statements => Accept_Or_Raise);
9339 Rewrite (Select_Call, Stmt);
9340 else
9341 Insert_Before (Select_Call, Stmt);
9342 end if;
9344 Cases :=
9345 Make_Implicit_If_Statement (N,
9346 Condition => Make_Op_Eq (Loc,
9347 Left_Opnd => New_Reference_To (Xnam, Loc),
9348 Right_Opnd =>
9349 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
9351 Then_Statements => Delay_Case,
9352 Else_Statements => Accept_Case);
9354 Append (Cases, Stats);
9355 end;
9356 end if;
9358 -- Replace accept statement with appropriate block
9360 Block :=
9361 Make_Block_Statement (Loc,
9362 Declarations => Decls,
9363 Handled_Statement_Sequence =>
9364 Make_Handled_Sequence_Of_Statements (Loc,
9365 Statements => Stats));
9367 Rewrite (N, Block);
9368 Analyze (N);
9370 -- Note: have to worry more about abort deferral in above code ???
9372 -- Final step is to unstack the Accept_Address entries for all accept
9373 -- statements appearing in accept alternatives in the select statement
9375 Alt := First (Alts);
9376 while Present (Alt) loop
9377 if Nkind (Alt) = N_Accept_Alternative then
9378 Remove_Last_Elmt (Accept_Address
9379 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
9380 end if;
9382 Next (Alt);
9383 end loop;
9384 end Expand_N_Selective_Accept;
9386 --------------------------------------
9387 -- Expand_N_Single_Task_Declaration --
9388 --------------------------------------
9390 -- Single task declarations should never be present after semantic
9391 -- analysis, since we expect them to be replaced by a declaration of an
9392 -- anonymous task type, followed by a declaration of the task object. We
9393 -- include this routine to make sure that is happening!
9395 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
9396 begin
9397 raise Program_Error;
9398 end Expand_N_Single_Task_Declaration;
9400 ------------------------
9401 -- Expand_N_Task_Body --
9402 ------------------------
9404 -- Given a task body
9406 -- task body tname is
9407 -- <declarations>
9408 -- begin
9409 -- <statements>
9410 -- end x;
9412 -- This expansion routine converts it into a procedure and sets the
9413 -- elaboration flag for the procedure to true, to represent the fact
9414 -- that the task body is now elaborated:
9416 -- procedure tnameB (_Task : access tnameV) is
9417 -- discriminal : dtype renames _Task.discriminant;
9419 -- procedure _clean is
9420 -- begin
9421 -- Abort_Defer.all;
9422 -- Complete_Task;
9423 -- Abort_Undefer.all;
9424 -- return;
9425 -- end _clean;
9427 -- begin
9428 -- Abort_Undefer.all;
9429 -- <declarations>
9430 -- System.Task_Stages.Complete_Activation;
9431 -- <statements>
9432 -- at end
9433 -- _clean;
9434 -- end tnameB;
9436 -- tnameE := True;
9438 -- In addition, if the task body is an activator, then a call to activate
9439 -- tasks is added at the start of the statements, before the call to
9440 -- Complete_Activation, and if in addition the task is a master then it
9441 -- must be established as a master. These calls are inserted and analyzed
9442 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
9443 -- expanded.
9445 -- There is one discriminal declaration line generated for each
9446 -- discriminant that is present to provide an easy reference point for
9447 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
9449 -- Note on relationship to GNARLI definition. In the GNARLI definition,
9450 -- task body procedures have a profile (Arg : System.Address). That is
9451 -- needed because GNARLI has to use the same access-to-subprogram type
9452 -- for all task types. We depend here on knowing that in GNAT, passing
9453 -- an address argument by value is identical to passing a record value
9454 -- by access (in either case a single pointer is passed), so even though
9455 -- this procedure has the wrong profile. In fact it's all OK, since the
9456 -- callings sequence is identical.
9458 procedure Expand_N_Task_Body (N : Node_Id) is
9459 Loc : constant Source_Ptr := Sloc (N);
9460 Ttyp : constant Entity_Id := Corresponding_Spec (N);
9461 Call : Node_Id;
9462 New_N : Node_Id;
9464 begin
9465 -- Add renaming declarations for discriminals and a declaration for the
9466 -- entry family index (if applicable).
9468 Install_Private_Data_Declarations
9469 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
9471 -- Add a call to Abort_Undefer at the very beginning of the task
9472 -- body since this body is called with abort still deferred.
9474 if Abort_Allowed then
9475 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
9476 Insert_Before
9477 (First (Statements (Handled_Statement_Sequence (N))), Call);
9478 Analyze (Call);
9479 end if;
9481 -- The statement part has already been protected with an at_end and
9482 -- cleanup actions. The call to Complete_Activation must be placed
9483 -- at the head of the sequence of statements of that block. The
9484 -- declarations have been merged in this sequence of statements but
9485 -- the first real statement is accessible from the First_Real_Statement
9486 -- field (which was set for exactly this purpose).
9488 if Restricted_Profile then
9489 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
9490 else
9491 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
9492 end if;
9494 Insert_Before
9495 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
9496 Analyze (Call);
9498 New_N :=
9499 Make_Subprogram_Body (Loc,
9500 Specification => Build_Task_Proc_Specification (Ttyp),
9501 Declarations => Declarations (N),
9502 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
9504 -- If the task contains generic instantiations, cleanup actions are
9505 -- delayed until after instantiation. Transfer the activation chain to
9506 -- the subprogram, to insure that the activation call is properly
9507 -- generated. It the task body contains inner tasks, indicate that the
9508 -- subprogram is a task master.
9510 if Delay_Cleanups (Ttyp) then
9511 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
9512 Set_Is_Task_Master (New_N, Is_Task_Master (N));
9513 end if;
9515 Rewrite (N, New_N);
9516 Analyze (N);
9518 -- Set elaboration flag immediately after task body. If the body is a
9519 -- subunit, the flag is set in the declarative part containing the stub.
9521 if Nkind (Parent (N)) /= N_Subunit then
9522 Insert_After (N,
9523 Make_Assignment_Statement (Loc,
9524 Name =>
9525 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
9526 Expression => New_Reference_To (Standard_True, Loc)));
9527 end if;
9529 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
9530 -- the task body. At this point the entry specs have been created,
9531 -- frozen and included in the dispatch table for the task type.
9533 pragma Assert (Present (Corresponding_Record_Type (Ttyp)));
9535 if Ada_Version >= Ada_05
9536 and then Present (Task_Definition (Parent (Ttyp)))
9537 and then Present (Interfaces (Corresponding_Record_Type (Ttyp)))
9538 then
9539 declare
9540 Current_Node : Node_Id;
9541 Vis_Decl : Node_Id :=
9542 First (Visible_Declarations (Task_Definition (Parent (Ttyp))));
9543 Wrap_Body : Node_Id;
9545 begin
9546 if Nkind (Parent (N)) = N_Subunit then
9547 Current_Node := Corresponding_Stub (Parent (N));
9548 else
9549 Current_Node := N;
9550 end if;
9552 -- Examine the visible declarations of the task type, looking for
9553 -- an entry declaration. We do not consider entry families since
9554 -- they cannot have dispatching operations, thus they do not need
9555 -- entry wrappers.
9557 while Present (Vis_Decl) loop
9558 if Nkind (Vis_Decl) = N_Entry_Declaration
9559 and then Ekind (Defining_Identifier (Vis_Decl)) = E_Entry
9560 then
9561 -- Create the specification of the wrapper
9563 Wrap_Body :=
9564 Build_Wrapper_Body (Loc,
9565 Proc_Nam => Defining_Identifier (Vis_Decl),
9566 Obj_Typ => Corresponding_Record_Type (Ttyp),
9567 Formals => Parameter_Specifications (Vis_Decl));
9569 if Wrap_Body /= Empty then
9570 Insert_After (Current_Node, Wrap_Body);
9571 Current_Node := Wrap_Body;
9573 Analyze (Wrap_Body);
9574 end if;
9575 end if;
9577 Next (Vis_Decl);
9578 end loop;
9579 end;
9580 end if;
9581 end Expand_N_Task_Body;
9583 ------------------------------------
9584 -- Expand_N_Task_Type_Declaration --
9585 ------------------------------------
9587 -- We have several things to do. First we must create a Boolean flag used
9588 -- to mark if the body is elaborated yet. This variable gets set to True
9589 -- when the body of the task is elaborated (we can't rely on the normal
9590 -- ABE mechanism for the task body, since we need to pass an access to
9591 -- this elaboration boolean to the runtime routines).
9593 -- taskE : aliased Boolean := False;
9595 -- Next a variable is declared to hold the task stack size (either the
9596 -- default : Unspecified_Size, or a value that is set by a pragma
9597 -- Storage_Size). If the value of the pragma Storage_Size is static, then
9598 -- the variable is initialized with this value:
9600 -- taskZ : Size_Type := Unspecified_Size;
9601 -- or
9602 -- taskZ : Size_Type := Size_Type (size_expression);
9604 -- Note: No variable is needed to hold the task relative deadline since
9605 -- its value would never be static because the parameter is of a private
9606 -- type (Ada.Real_Time.Time_Span).
9608 -- Next we create a corresponding record type declaration used to represent
9609 -- values of this task. The general form of this type declaration is
9611 -- type taskV (discriminants) is record
9612 -- _Task_Id : Task_Id;
9613 -- entry_family : array (bounds) of Void;
9614 -- _Priority : Integer := priority_expression;
9615 -- _Size : Size_Type := Size_Type (size_expression);
9616 -- _Task_Info : Task_Info_Type := task_info_expression;
9617 -- end record;
9619 -- The discriminants are present only if the corresponding task type has
9620 -- discriminants, and they exactly mirror the task type discriminants.
9622 -- The Id field is always present. It contains the Task_Id value, as set by
9623 -- the call to Create_Task. Note that although the task is limited, the
9624 -- task value record type is not limited, so there is no problem in passing
9625 -- this field as an out parameter to Create_Task.
9627 -- One entry_family component is present for each entry family in the task
9628 -- definition. The bounds correspond to the bounds of the entry family
9629 -- (which may depend on discriminants). The element type is void, since we
9630 -- only need the bounds information for determining the entry index. Note
9631 -- that the use of an anonymous array would normally be illegal in this
9632 -- context, but this is a parser check, and the semantics is quite prepared
9633 -- to handle such a case.
9635 -- The _Size field is present only if a Storage_Size pragma appears in the
9636 -- task definition. The expression captures the argument that was present
9637 -- in the pragma, and is used to override the task stack size otherwise
9638 -- associated with the task type.
9640 -- The _Priority field is present only if a Priority or Interrupt_Priority
9641 -- pragma appears in the task definition. The expression captures the
9642 -- argument that was present in the pragma, and is used to provide the Size
9643 -- parameter to the call to Create_Task.
9645 -- The _Task_Info field is present only if a Task_Info pragma appears in
9646 -- the task definition. The expression captures the argument that was
9647 -- present in the pragma, and is used to provide the Task_Image parameter
9648 -- to the call to Create_Task.
9650 -- The _Relative_Deadline field is present only if a Relative_Deadline
9651 -- pragma appears in the task definition. The expression captures the
9652 -- argument that was present in the pragma, and is used to provide the
9653 -- Relative_Deadline parameter to the call to Create_Task.
9655 -- When a task is declared, an instance of the task value record is
9656 -- created. The elaboration of this declaration creates the correct bounds
9657 -- for the entry families, and also evaluates the size, priority, and
9658 -- task_Info expressions if needed. The initialization routine for the task
9659 -- type itself then calls Create_Task with appropriate parameters to
9660 -- initialize the value of the Task_Id field.
9662 -- Note: the address of this record is passed as the "Discriminants"
9663 -- parameter for Create_Task. Since Create_Task merely passes this onto the
9664 -- body procedure, it does not matter that it does not quite match the
9665 -- GNARLI model of what is being passed (the record contains more than just
9666 -- the discriminants, but the discriminants can be found from the record
9667 -- value).
9669 -- The Entity_Id for this created record type is placed in the
9670 -- Corresponding_Record_Type field of the associated task type entity.
9672 -- Next we create a procedure specification for the task body procedure:
9674 -- procedure taskB (_Task : access taskV);
9676 -- Note that this must come after the record type declaration, since
9677 -- the spec refers to this type. It turns out that the initialization
9678 -- procedure for the value type references the task body spec, but that's
9679 -- fine, since it won't be generated till the freeze point for the type,
9680 -- which is certainly after the task body spec declaration.
9682 -- Finally, we set the task index value field of the entry attribute in
9683 -- the case of a simple entry.
9685 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
9686 Loc : constant Source_Ptr := Sloc (N);
9687 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
9688 Tasknm : constant Name_Id := Chars (Tasktyp);
9689 Taskdef : constant Node_Id := Task_Definition (N);
9691 Proc_Spec : Node_Id;
9692 Rec_Decl : Node_Id;
9693 Rec_Ent : Entity_Id;
9694 Cdecls : List_Id;
9695 Elab_Decl : Node_Id;
9696 Size_Decl : Node_Id;
9697 Body_Decl : Node_Id;
9698 Task_Size : Node_Id;
9699 Ent_Stack : Entity_Id;
9700 Decl_Stack : Node_Id;
9702 begin
9703 -- If already expanded, nothing to do
9705 if Present (Corresponding_Record_Type (Tasktyp)) then
9706 return;
9707 end if;
9709 -- Here we will do the expansion
9711 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
9713 -- Ada 2005 (AI-345): Propagate the attribute that contains the list
9714 -- of implemented interfaces.
9716 Set_Interface_List (Type_Definition (Rec_Decl), Interface_List (N));
9718 Rec_Ent := Defining_Identifier (Rec_Decl);
9719 Cdecls := Component_Items (Component_List
9720 (Type_Definition (Rec_Decl)));
9722 Qualify_Entity_Names (N);
9724 -- First create the elaboration variable
9726 Elab_Decl :=
9727 Make_Object_Declaration (Loc,
9728 Defining_Identifier =>
9729 Make_Defining_Identifier (Sloc (Tasktyp),
9730 Chars => New_External_Name (Tasknm, 'E')),
9731 Aliased_Present => True,
9732 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
9733 Expression => New_Reference_To (Standard_False, Loc));
9734 Insert_After (N, Elab_Decl);
9736 -- Next create the declaration of the size variable (tasknmZ)
9738 Set_Storage_Size_Variable (Tasktyp,
9739 Make_Defining_Identifier (Sloc (Tasktyp),
9740 Chars => New_External_Name (Tasknm, 'Z')));
9742 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) and then
9743 Is_Static_Expression (Expression (First (
9744 Pragma_Argument_Associations (Find_Task_Or_Protected_Pragma (
9745 Taskdef, Name_Storage_Size)))))
9746 then
9747 Size_Decl :=
9748 Make_Object_Declaration (Loc,
9749 Defining_Identifier => Storage_Size_Variable (Tasktyp),
9750 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
9751 Expression =>
9752 Convert_To (RTE (RE_Size_Type),
9753 Relocate_Node (
9754 Expression (First (
9755 Pragma_Argument_Associations (
9756 Find_Task_Or_Protected_Pragma
9757 (Taskdef, Name_Storage_Size)))))));
9759 else
9760 Size_Decl :=
9761 Make_Object_Declaration (Loc,
9762 Defining_Identifier => Storage_Size_Variable (Tasktyp),
9763 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
9764 Expression => New_Reference_To (RTE (RE_Unspecified_Size), Loc));
9765 end if;
9767 Insert_After (Elab_Decl, Size_Decl);
9769 -- Next build the rest of the corresponding record declaration. This is
9770 -- done last, since the corresponding record initialization procedure
9771 -- will reference the previously created entities.
9773 -- Fill in the component declarations -- first the _Task_Id field
9775 Append_To (Cdecls,
9776 Make_Component_Declaration (Loc,
9777 Defining_Identifier =>
9778 Make_Defining_Identifier (Loc, Name_uTask_Id),
9779 Component_Definition =>
9780 Make_Component_Definition (Loc,
9781 Aliased_Present => False,
9782 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
9783 Loc))));
9785 -- Declare static ATCB (that is, created by the expander) if we are
9786 -- using the Restricted run time.
9788 if Restricted_Profile then
9789 Append_To (Cdecls,
9790 Make_Component_Declaration (Loc,
9791 Defining_Identifier =>
9792 Make_Defining_Identifier (Loc, Name_uATCB),
9794 Component_Definition =>
9795 Make_Component_Definition (Loc,
9796 Aliased_Present => True,
9797 Subtype_Indication => Make_Subtype_Indication (Loc,
9798 Subtype_Mark => New_Occurrence_Of
9799 (RTE (RE_Ada_Task_Control_Block), Loc),
9801 Constraint =>
9802 Make_Index_Or_Discriminant_Constraint (Loc,
9803 Constraints =>
9804 New_List (Make_Integer_Literal (Loc, 0)))))));
9806 end if;
9808 -- Declare static stack (that is, created by the expander) if we are
9809 -- using the Restricted run time on a bare board configuration.
9811 if Restricted_Profile
9812 and then Preallocated_Stacks_On_Target
9813 then
9814 -- First we need to extract the appropriate stack size
9816 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
9818 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
9819 declare
9820 Expr_N : constant Node_Id :=
9821 Expression (First (
9822 Pragma_Argument_Associations (
9823 Find_Task_Or_Protected_Pragma
9824 (Taskdef, Name_Storage_Size))));
9825 Etyp : constant Entity_Id := Etype (Expr_N);
9826 P : constant Node_Id := Parent (Expr_N);
9828 begin
9829 -- The stack is defined inside the corresponding record.
9830 -- Therefore if the size of the stack is set by means of
9831 -- a discriminant, we must reference the discriminant of the
9832 -- corresponding record type.
9834 if Nkind (Expr_N) in N_Has_Entity
9835 and then Present (Discriminal_Link (Entity (Expr_N)))
9836 then
9837 Task_Size :=
9838 New_Reference_To
9839 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
9840 Loc);
9841 Set_Parent (Task_Size, P);
9842 Set_Etype (Task_Size, Etyp);
9843 Set_Analyzed (Task_Size);
9845 else
9846 Task_Size := Relocate_Node (Expr_N);
9847 end if;
9848 end;
9850 else
9851 Task_Size :=
9852 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
9853 end if;
9855 Decl_Stack := Make_Component_Declaration (Loc,
9856 Defining_Identifier => Ent_Stack,
9858 Component_Definition =>
9859 Make_Component_Definition (Loc,
9860 Aliased_Present => True,
9861 Subtype_Indication => Make_Subtype_Indication (Loc,
9862 Subtype_Mark =>
9863 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
9865 Constraint =>
9866 Make_Index_Or_Discriminant_Constraint (Loc,
9867 Constraints => New_List (Make_Range (Loc,
9868 Low_Bound => Make_Integer_Literal (Loc, 1),
9869 High_Bound => Convert_To (RTE (RE_Storage_Offset),
9870 Task_Size)))))));
9872 Append_To (Cdecls, Decl_Stack);
9874 -- The appropriate alignment for the stack is ensured by the run-time
9875 -- code in charge of task creation.
9877 end if;
9879 -- Add components for entry families
9881 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
9883 -- Add the _Priority component if a Priority pragma is present
9885 if Present (Taskdef) and then Has_Priority_Pragma (Taskdef) then
9886 declare
9887 Prag : constant Node_Id :=
9888 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
9889 Expr : Node_Id;
9891 begin
9892 Expr := First (Pragma_Argument_Associations (Prag));
9894 if Nkind (Expr) = N_Pragma_Argument_Association then
9895 Expr := Expression (Expr);
9896 end if;
9898 Expr := New_Copy_Tree (Expr);
9900 -- Add conversion to proper type to do range check if required
9901 -- Note that for runtime units, we allow out of range interrupt
9902 -- priority values to be used in a priority pragma. This is for
9903 -- the benefit of some versions of System.Interrupts which use
9904 -- a special server task with maximum interrupt priority.
9906 if Pragma_Name (Prag) = Name_Priority
9907 and then not GNAT_Mode
9908 then
9909 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
9910 else
9911 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
9912 end if;
9914 Append_To (Cdecls,
9915 Make_Component_Declaration (Loc,
9916 Defining_Identifier =>
9917 Make_Defining_Identifier (Loc, Name_uPriority),
9918 Component_Definition =>
9919 Make_Component_Definition (Loc,
9920 Aliased_Present => False,
9921 Subtype_Indication => New_Reference_To (Standard_Integer,
9922 Loc)),
9923 Expression => Expr));
9924 end;
9925 end if;
9927 -- Add the _Task_Size component if a Storage_Size pragma is present
9929 if Present (Taskdef)
9930 and then Has_Storage_Size_Pragma (Taskdef)
9931 then
9932 Append_To (Cdecls,
9933 Make_Component_Declaration (Loc,
9934 Defining_Identifier =>
9935 Make_Defining_Identifier (Loc, Name_uSize),
9937 Component_Definition =>
9938 Make_Component_Definition (Loc,
9939 Aliased_Present => False,
9940 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
9941 Loc)),
9943 Expression =>
9944 Convert_To (RTE (RE_Size_Type),
9945 Relocate_Node (
9946 Expression (First (
9947 Pragma_Argument_Associations (
9948 Find_Task_Or_Protected_Pragma
9949 (Taskdef, Name_Storage_Size))))))));
9950 end if;
9952 -- Add the _Task_Info component if a Task_Info pragma is present
9954 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
9955 Append_To (Cdecls,
9956 Make_Component_Declaration (Loc,
9957 Defining_Identifier =>
9958 Make_Defining_Identifier (Loc, Name_uTask_Info),
9960 Component_Definition =>
9961 Make_Component_Definition (Loc,
9962 Aliased_Present => False,
9963 Subtype_Indication =>
9964 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
9966 Expression => New_Copy (
9967 Expression (First (
9968 Pragma_Argument_Associations (
9969 Find_Task_Or_Protected_Pragma
9970 (Taskdef, Name_Task_Info)))))));
9971 end if;
9973 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
9974 -- present. If we are using a restricted run time this component will
9975 -- not be added (deadlines are not allowed by the Ravenscar profile).
9977 if not Restricted_Profile
9978 and then Present (Taskdef)
9979 and then Has_Relative_Deadline_Pragma (Taskdef)
9980 then
9981 Append_To (Cdecls,
9982 Make_Component_Declaration (Loc,
9983 Defining_Identifier =>
9984 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
9986 Component_Definition =>
9987 Make_Component_Definition (Loc,
9988 Aliased_Present => False,
9989 Subtype_Indication =>
9990 New_Reference_To (RTE (RE_Time_Span), Loc)),
9992 Expression =>
9993 Convert_To (RTE (RE_Time_Span),
9994 Relocate_Node (
9995 Expression (First (
9996 Pragma_Argument_Associations (
9997 Find_Task_Or_Protected_Pragma
9998 (Taskdef, Name_Relative_Deadline))))))));
9999 end if;
10001 Insert_After (Size_Decl, Rec_Decl);
10003 -- Analyze the record declaration immediately after construction,
10004 -- because the initialization procedure is needed for single task
10005 -- declarations before the next entity is analyzed.
10007 Analyze (Rec_Decl);
10009 -- Create the declaration of the task body procedure
10011 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
10012 Body_Decl :=
10013 Make_Subprogram_Declaration (Loc,
10014 Specification => Proc_Spec);
10016 Insert_After (Rec_Decl, Body_Decl);
10018 -- The subprogram does not comes from source, so we have to indicate the
10019 -- need for debugging information explicitly.
10021 if Comes_From_Source (Original_Node (N)) then
10022 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
10023 end if;
10025 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
10026 -- the corresponding record has been frozen.
10028 if Ada_Version >= Ada_05
10029 and then Present (Taskdef)
10030 and then Present (Corresponding_Record_Type
10031 (Defining_Identifier (Parent (Taskdef))))
10032 and then Present (Interfaces
10033 (Corresponding_Record_Type
10034 (Defining_Identifier (Parent (Taskdef)))))
10035 then
10036 declare
10037 Current_Node : Node_Id := Rec_Decl;
10038 Vis_Decl : Node_Id := First (Visible_Declarations (Taskdef));
10039 Wrap_Spec : Node_Id;
10040 New_N : Node_Id;
10042 begin
10043 -- Examine the visible declarations of the task type, looking for
10044 -- an entry declaration. We do not consider entry families since
10045 -- they cannot have dispatching operations, thus they do not need
10046 -- entry wrappers.
10048 while Present (Vis_Decl) loop
10049 if Nkind (Vis_Decl) = N_Entry_Declaration
10050 and then Ekind (Defining_Identifier (Vis_Decl)) = E_Entry
10051 then
10052 Wrap_Spec :=
10053 Build_Wrapper_Spec (Loc,
10054 Proc_Nam => Defining_Identifier (Vis_Decl),
10055 Obj_Typ => Etype (Rec_Ent),
10056 Formals => Parameter_Specifications (Vis_Decl));
10058 if Wrap_Spec /= Empty then
10059 New_N :=
10060 Make_Subprogram_Declaration (Loc,
10061 Specification => Wrap_Spec);
10063 Insert_After (Current_Node, New_N);
10064 Current_Node := New_N;
10066 Analyze (New_N);
10067 end if;
10068 end if;
10070 Next (Vis_Decl);
10071 end loop;
10072 end;
10073 end if;
10075 -- Ada 2005 (AI-345): We must defer freezing to allow further
10076 -- declaration of primitive subprograms covering task interfaces
10078 if Ada_Version <= Ada_95 then
10080 -- Now we can freeze the corresponding record. This needs manually
10081 -- freezing, since it is really part of the task type, and the task
10082 -- type is frozen at this stage. We of course need the initialization
10083 -- procedure for this corresponding record type and we won't get it
10084 -- in time if we don't freeze now.
10086 declare
10087 L : constant List_Id := Freeze_Entity (Rec_Ent, Loc);
10088 begin
10089 if Is_Non_Empty_List (L) then
10090 Insert_List_After (Body_Decl, L);
10091 end if;
10092 end;
10093 end if;
10095 -- Complete the expansion of access types to the current task type, if
10096 -- any were declared.
10098 Expand_Previous_Access_Type (Tasktyp);
10099 end Expand_N_Task_Type_Declaration;
10101 -------------------------------
10102 -- Expand_N_Timed_Entry_Call --
10103 -------------------------------
10105 -- A timed entry call in normal case is not implemented using ATC mechanism
10106 -- anymore for efficiency reason.
10108 -- select
10109 -- T.E;
10110 -- S1;
10111 -- or
10112 -- Delay D;
10113 -- S2;
10114 -- end select;
10116 -- is expanded as follow:
10118 -- 1) When T.E is a task entry_call;
10120 -- declare
10121 -- B : Boolean;
10122 -- X : Task_Entry_Index := <entry index>;
10123 -- DX : Duration := To_Duration (D);
10124 -- M : Delay_Mode := <discriminant>;
10125 -- P : parms := (parm, parm, parm);
10127 -- begin
10128 -- Timed_Protected_Entry_Call
10129 -- (<acceptor-task>, X, P'Address, DX, M, B);
10130 -- if B then
10131 -- S1;
10132 -- else
10133 -- S2;
10134 -- end if;
10135 -- end;
10137 -- 2) When T.E is a protected entry_call;
10139 -- declare
10140 -- B : Boolean;
10141 -- X : Protected_Entry_Index := <entry index>;
10142 -- DX : Duration := To_Duration (D);
10143 -- M : Delay_Mode := <discriminant>;
10144 -- P : parms := (parm, parm, parm);
10146 -- begin
10147 -- Timed_Protected_Entry_Call
10148 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
10149 -- if B then
10150 -- S1;
10151 -- else
10152 -- S2;
10153 -- end if;
10154 -- end;
10156 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call;
10158 -- declare
10159 -- B : Boolean := False;
10160 -- C : Ada.Tags.Prim_Op_Kind;
10161 -- DX : Duration := To_Duration (D)
10162 -- K : Ada.Tags.Tagged_Kind :=
10163 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
10164 -- M : Integer :=...;
10165 -- P : Parameters := (Param1 .. ParamN);
10166 -- S : Iteger;
10168 -- begin
10169 -- if K = Ada.Tags.TK_Limited_Tagged then
10170 -- <dispatching-call>;
10171 -- <triggering-statements>
10173 -- else
10174 -- S :=
10175 -- Ada.Tags.Get_Offset_Index
10176 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
10178 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
10180 -- if C = POK_Protected_Entry
10181 -- or else C = POK_Task_Entry
10182 -- then
10183 -- Param1 := P.Param1;
10184 -- ...
10185 -- ParamN := P.ParamN;
10186 -- end if;
10188 -- if B then
10189 -- if C = POK_Procedure
10190 -- or else C = POK_Protected_Procedure
10191 -- or else C = POK_Task_Procedure
10192 -- then
10193 -- <dispatching-call>;
10194 -- end if;
10196 -- <triggering-statements>
10197 -- else
10198 -- <timed-statements>
10199 -- end if;
10200 -- end if;
10201 -- end;
10203 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
10204 Loc : constant Source_Ptr := Sloc (N);
10206 E_Call : Node_Id :=
10207 Entry_Call_Statement (Entry_Call_Alternative (N));
10208 E_Stats : constant List_Id :=
10209 Statements (Entry_Call_Alternative (N));
10210 D_Stat : Node_Id :=
10211 Delay_Statement (Delay_Alternative (N));
10212 D_Stats : constant List_Id :=
10213 Statements (Delay_Alternative (N));
10215 Actuals : List_Id;
10216 Blk_Typ : Entity_Id;
10217 Call : Node_Id;
10218 Call_Ent : Entity_Id;
10219 Conc_Typ_Stmts : List_Id;
10220 Concval : Node_Id;
10221 D_Conv : Node_Id;
10222 D_Disc : Node_Id;
10223 D_Type : Entity_Id;
10224 Decls : List_Id;
10225 Dummy : Node_Id;
10226 Ename : Node_Id;
10227 Formals : List_Id;
10228 Index : Node_Id;
10229 Is_Disp_Select : Boolean;
10230 Lim_Typ_Stmts : List_Id;
10231 N_Stats : List_Id;
10232 Obj : Entity_Id;
10233 Param : Node_Id;
10234 Params : List_Id;
10235 Stmt : Node_Id;
10236 Stmts : List_Id;
10237 Unpack : List_Id;
10239 B : Entity_Id; -- Call status flag
10240 C : Entity_Id; -- Call kind
10241 D : Entity_Id; -- Delay
10242 K : Entity_Id; -- Tagged kind
10243 M : Entity_Id; -- Delay mode
10244 P : Entity_Id; -- Parameter block
10245 S : Entity_Id; -- Primitive operation slot
10247 begin
10248 -- The arguments in the call may require dynamic allocation, and the
10249 -- call statement may have been transformed into a block. The block
10250 -- may contain additional declarations for internal entities, and the
10251 -- original call is found by sequential search.
10253 if Nkind (E_Call) = N_Block_Statement then
10254 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
10255 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
10256 N_Entry_Call_Statement)
10257 loop
10258 Next (E_Call);
10259 end loop;
10260 end if;
10262 Is_Disp_Select :=
10263 Ada_Version >= Ada_05
10264 and then Nkind (E_Call) = N_Procedure_Call_Statement;
10266 if Is_Disp_Select then
10267 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
10269 Decls := New_List;
10270 Stmts := New_List;
10272 -- Generate:
10273 -- B : Boolean := False;
10275 B := Build_B (Loc, Decls);
10277 -- Generate:
10278 -- C : Ada.Tags.Prim_Op_Kind;
10280 C := Build_C (Loc, Decls);
10282 -- Because the analysis of all statements was disabled, manually
10283 -- analyze the delay statement.
10285 Analyze (D_Stat);
10286 D_Stat := Original_Node (D_Stat);
10288 else
10289 -- Build an entry call using Simple_Entry_Call
10291 Extract_Entry (E_Call, Concval, Ename, Index);
10292 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
10294 Decls := Declarations (E_Call);
10295 Stmts := Statements (Handled_Statement_Sequence (E_Call));
10297 if No (Decls) then
10298 Decls := New_List;
10299 end if;
10301 -- Generate:
10302 -- B : Boolean;
10304 B := Make_Defining_Identifier (Loc, Name_uB);
10306 Prepend_To (Decls,
10307 Make_Object_Declaration (Loc,
10308 Defining_Identifier =>
10310 Object_Definition =>
10311 New_Reference_To (Standard_Boolean, Loc)));
10312 end if;
10314 -- Duration and mode processing
10316 D_Type := Base_Type (Etype (Expression (D_Stat)));
10318 -- Use the type of the delay expression (Calendar or Real_Time) to
10319 -- generate the appropriate conversion.
10321 if Nkind (D_Stat) = N_Delay_Relative_Statement then
10322 D_Disc := Make_Integer_Literal (Loc, 0);
10323 D_Conv := Relocate_Node (Expression (D_Stat));
10325 elsif Is_RTE (D_Type, RO_CA_Time) then
10326 D_Disc := Make_Integer_Literal (Loc, 1);
10327 D_Conv := Make_Function_Call (Loc,
10328 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
10329 New_List (New_Copy (Expression (D_Stat))));
10331 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
10332 D_Disc := Make_Integer_Literal (Loc, 2);
10333 D_Conv := Make_Function_Call (Loc,
10334 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
10335 New_List (New_Copy (Expression (D_Stat))));
10336 end if;
10338 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
10340 -- Generate:
10341 -- D : Duration;
10343 Append_To (Decls,
10344 Make_Object_Declaration (Loc,
10345 Defining_Identifier =>
10347 Object_Definition =>
10348 New_Reference_To (Standard_Duration, Loc)));
10350 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
10352 -- Generate:
10353 -- M : Integer := (0 | 1 | 2);
10355 Append_To (Decls,
10356 Make_Object_Declaration (Loc,
10357 Defining_Identifier =>
10359 Object_Definition =>
10360 New_Reference_To (Standard_Integer, Loc),
10361 Expression =>
10362 D_Disc));
10364 -- Do the assignment at this stage only because the evaluation of the
10365 -- expression must not occur before (see ACVC C97302A).
10367 Append_To (Stmts,
10368 Make_Assignment_Statement (Loc,
10369 Name =>
10370 New_Reference_To (D, Loc),
10371 Expression =>
10372 D_Conv));
10374 -- Parameter block processing
10376 -- Manually create the parameter block for dispatching calls. In the
10377 -- case of entries, the block has already been created during the call
10378 -- to Build_Simple_Entry_Call.
10380 if Is_Disp_Select then
10382 -- Tagged kind processing, generate:
10383 -- K : Ada.Tags.Tagged_Kind :=
10384 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
10386 K := Build_K (Loc, Decls, Obj);
10388 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
10389 P := Parameter_Block_Pack
10390 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
10392 -- Dispatch table slot processing, generate:
10393 -- S : Integer;
10395 S := Build_S (Loc, Decls);
10397 -- Generate:
10398 -- S := Ada.Tags.Get_Offset_Index
10399 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
10401 Conc_Typ_Stmts :=
10402 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
10404 -- Generate:
10405 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
10407 -- where Obj is the controlling formal parameter, S is the dispatch
10408 -- table slot number of the dispatching operation, P is the wrapped
10409 -- parameter block, D is the duration, M is the duration mode, C is
10410 -- the call kind and B is the call status.
10412 Params := New_List;
10414 Append_To (Params, New_Copy_Tree (Obj));
10415 Append_To (Params, New_Reference_To (S, Loc));
10416 Append_To (Params, Make_Attribute_Reference (Loc,
10417 Prefix => New_Reference_To (P, Loc),
10418 Attribute_Name => Name_Address));
10419 Append_To (Params, New_Reference_To (D, Loc));
10420 Append_To (Params, New_Reference_To (M, Loc));
10421 Append_To (Params, New_Reference_To (C, Loc));
10422 Append_To (Params, New_Reference_To (B, Loc));
10424 Append_To (Conc_Typ_Stmts,
10425 Make_Procedure_Call_Statement (Loc,
10426 Name =>
10427 New_Reference_To (
10428 Find_Prim_Op (Etype (Etype (Obj)),
10429 Name_uDisp_Timed_Select),
10430 Loc),
10431 Parameter_Associations =>
10432 Params));
10434 -- Generate:
10435 -- if C = POK_Protected_Entry
10436 -- or else C = POK_Task_Entry
10437 -- then
10438 -- Param1 := P.Param1;
10439 -- ...
10440 -- ParamN := P.ParamN;
10441 -- end if;
10443 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
10445 -- Generate the if statement only when the packed parameters need
10446 -- explicit assignments to their corresponding actuals.
10448 if Present (Unpack) then
10449 Append_To (Conc_Typ_Stmts,
10450 Make_If_Statement (Loc,
10452 Condition =>
10453 Make_Or_Else (Loc,
10454 Left_Opnd =>
10455 Make_Op_Eq (Loc,
10456 Left_Opnd =>
10457 New_Reference_To (C, Loc),
10458 Right_Opnd =>
10459 New_Reference_To (RTE (
10460 RE_POK_Protected_Entry), Loc)),
10461 Right_Opnd =>
10462 Make_Op_Eq (Loc,
10463 Left_Opnd =>
10464 New_Reference_To (C, Loc),
10465 Right_Opnd =>
10466 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
10468 Then_Statements =>
10469 Unpack));
10470 end if;
10472 -- Generate:
10474 -- if B then
10475 -- if C = POK_Procedure
10476 -- or else C = POK_Protected_Procedure
10477 -- or else C = POK_Task_Procedure
10478 -- then
10479 -- <dispatching-call>
10480 -- end if;
10481 -- <triggering-statements>
10482 -- else
10483 -- <timed-statements>
10484 -- end if;
10486 N_Stats := New_Copy_List_Tree (E_Stats);
10488 Prepend_To (N_Stats,
10489 Make_If_Statement (Loc,
10491 Condition =>
10492 Make_Or_Else (Loc,
10493 Left_Opnd =>
10494 Make_Op_Eq (Loc,
10495 Left_Opnd =>
10496 New_Reference_To (C, Loc),
10497 Right_Opnd =>
10498 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
10499 Right_Opnd =>
10500 Make_Or_Else (Loc,
10501 Left_Opnd =>
10502 Make_Op_Eq (Loc,
10503 Left_Opnd =>
10504 New_Reference_To (C, Loc),
10505 Right_Opnd =>
10506 New_Reference_To (RTE (
10507 RE_POK_Protected_Procedure), Loc)),
10508 Right_Opnd =>
10509 Make_Op_Eq (Loc,
10510 Left_Opnd =>
10511 New_Reference_To (C, Loc),
10512 Right_Opnd =>
10513 New_Reference_To (RTE (
10514 RE_POK_Task_Procedure), Loc)))),
10516 Then_Statements =>
10517 New_List (E_Call)));
10519 Append_To (Conc_Typ_Stmts,
10520 Make_If_Statement (Loc,
10521 Condition => New_Reference_To (B, Loc),
10522 Then_Statements => N_Stats,
10523 Else_Statements => D_Stats));
10525 -- Generate:
10526 -- <dispatching-call>;
10527 -- <triggering-statements>
10529 Lim_Typ_Stmts := New_Copy_List_Tree (E_Stats);
10530 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (E_Call));
10532 -- Generate:
10533 -- if K = Ada.Tags.TK_Limited_Tagged then
10534 -- Lim_Typ_Stmts
10535 -- else
10536 -- Conc_Typ_Stmts
10537 -- end if;
10539 Append_To (Stmts,
10540 Make_If_Statement (Loc,
10541 Condition =>
10542 Make_Op_Eq (Loc,
10543 Left_Opnd =>
10544 New_Reference_To (K, Loc),
10545 Right_Opnd =>
10546 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
10548 Then_Statements =>
10549 Lim_Typ_Stmts,
10551 Else_Statements =>
10552 Conc_Typ_Stmts));
10554 else
10555 -- Skip assignments to temporaries created for in-out parameters.
10556 -- This makes unwarranted assumptions about the shape of the expanded
10557 -- tree for the call, and should be cleaned up ???
10559 Stmt := First (Stmts);
10560 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
10561 Next (Stmt);
10562 end loop;
10564 -- Do the assignment at this stage only because the evaluation
10565 -- of the expression must not occur before (see ACVC C97302A).
10567 Insert_Before (Stmt,
10568 Make_Assignment_Statement (Loc,
10569 Name => New_Reference_To (D, Loc),
10570 Expression => D_Conv));
10572 Call := Stmt;
10573 Params := Parameter_Associations (Call);
10575 -- For a protected type, we build a Timed_Protected_Entry_Call
10577 if Is_Protected_Type (Etype (Concval)) then
10579 -- Create a new call statement
10581 Param := First (Params);
10582 while Present (Param)
10583 and then not Is_RTE (Etype (Param), RE_Call_Modes)
10584 loop
10585 Next (Param);
10586 end loop;
10588 Dummy := Remove_Next (Next (Param));
10590 -- Remove garbage is following the Cancel_Param if present
10592 Dummy := Next (Param);
10594 -- Remove the mode of the Protected_Entry_Call call, then remove
10595 -- the Communication_Block of the Protected_Entry_Call call, and
10596 -- finally add Duration and a Delay_Mode parameter
10598 pragma Assert (Present (Param));
10599 Rewrite (Param, New_Reference_To (D, Loc));
10601 Rewrite (Dummy, New_Reference_To (M, Loc));
10603 -- Add a Boolean flag for successful entry call
10605 Append_To (Params, New_Reference_To (B, Loc));
10607 case Corresponding_Runtime_Package (Etype (Concval)) is
10608 when System_Tasking_Protected_Objects_Entries =>
10609 Rewrite (Call,
10610 Make_Procedure_Call_Statement (Loc,
10611 Name =>
10612 New_Reference_To
10613 (RTE (RE_Timed_Protected_Entry_Call), Loc),
10614 Parameter_Associations => Params));
10616 when System_Tasking_Protected_Objects_Single_Entry =>
10617 Param := First (Params);
10618 while Present (Param)
10619 and then not
10620 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
10621 loop
10622 Next (Param);
10623 end loop;
10625 Remove (Param);
10627 Rewrite (Call,
10628 Make_Procedure_Call_Statement (Loc,
10629 Name => New_Reference_To (
10630 RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
10631 Parameter_Associations => Params));
10633 when others =>
10634 raise Program_Error;
10635 end case;
10637 -- For the task case, build a Timed_Task_Entry_Call
10639 else
10640 -- Create a new call statement
10642 Append_To (Params, New_Reference_To (D, Loc));
10643 Append_To (Params, New_Reference_To (M, Loc));
10644 Append_To (Params, New_Reference_To (B, Loc));
10646 Rewrite (Call,
10647 Make_Procedure_Call_Statement (Loc,
10648 Name =>
10649 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
10650 Parameter_Associations => Params));
10651 end if;
10653 Append_To (Stmts,
10654 Make_Implicit_If_Statement (N,
10655 Condition => New_Reference_To (B, Loc),
10656 Then_Statements => E_Stats,
10657 Else_Statements => D_Stats));
10658 end if;
10660 Rewrite (N,
10661 Make_Block_Statement (Loc,
10662 Declarations => Decls,
10663 Handled_Statement_Sequence =>
10664 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
10666 Analyze (N);
10667 end Expand_N_Timed_Entry_Call;
10669 ----------------------------------------
10670 -- Expand_Protected_Body_Declarations --
10671 ----------------------------------------
10673 procedure Expand_Protected_Body_Declarations
10674 (N : Node_Id;
10675 Spec_Id : Entity_Id)
10677 begin
10678 if No_Run_Time_Mode then
10679 Error_Msg_CRT ("protected body", N);
10680 return;
10682 elsif Expander_Active then
10684 -- Associate discriminals with the first subprogram or entry body to
10685 -- be expanded.
10687 if Present (First_Protected_Operation (Declarations (N))) then
10688 Set_Discriminals (Parent (Spec_Id));
10689 end if;
10690 end if;
10691 end Expand_Protected_Body_Declarations;
10693 -------------------------
10694 -- External_Subprogram --
10695 -------------------------
10697 function External_Subprogram (E : Entity_Id) return Entity_Id is
10698 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
10700 begin
10701 -- The internal and external subprograms follow each other on the entity
10702 -- chain. Note that previously private operations had no separate
10703 -- external subprogram. We now create one in all cases, because a
10704 -- private operation may actually appear in an external call, through
10705 -- a 'Access reference used for a callback.
10707 -- If the operation is a function that returns an anonymous access type,
10708 -- the corresponding itype appears before the operation, and must be
10709 -- skipped.
10711 -- This mechanism is fragile, there should be a real link between the
10712 -- two versions of the operation, but there is no place to put it ???
10714 if Is_Access_Type (Next_Entity (Subp)) then
10715 return Next_Entity (Next_Entity (Subp));
10716 else
10717 return Next_Entity (Subp);
10718 end if;
10719 end External_Subprogram;
10721 ------------------------------
10722 -- Extract_Dispatching_Call --
10723 ------------------------------
10725 procedure Extract_Dispatching_Call
10726 (N : Node_Id;
10727 Call_Ent : out Entity_Id;
10728 Object : out Entity_Id;
10729 Actuals : out List_Id;
10730 Formals : out List_Id)
10732 Call_Nam : Node_Id;
10734 begin
10735 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
10737 if Present (Original_Node (N)) then
10738 Call_Nam := Name (Original_Node (N));
10739 else
10740 Call_Nam := Name (N);
10741 end if;
10743 -- Retrieve the name of the dispatching procedure. It contains the
10744 -- dispatch table slot number.
10746 loop
10747 case Nkind (Call_Nam) is
10748 when N_Identifier =>
10749 exit;
10751 when N_Selected_Component =>
10752 Call_Nam := Selector_Name (Call_Nam);
10754 when others =>
10755 raise Program_Error;
10757 end case;
10758 end loop;
10760 Actuals := Parameter_Associations (N);
10761 Call_Ent := Entity (Call_Nam);
10762 Formals := Parameter_Specifications (Parent (Call_Ent));
10763 Object := First (Actuals);
10765 if Present (Original_Node (Object)) then
10766 Object := Original_Node (Object);
10767 end if;
10768 end Extract_Dispatching_Call;
10770 -------------------
10771 -- Extract_Entry --
10772 -------------------
10774 procedure Extract_Entry
10775 (N : Node_Id;
10776 Concval : out Node_Id;
10777 Ename : out Node_Id;
10778 Index : out Node_Id)
10780 Nam : constant Node_Id := Name (N);
10782 begin
10783 -- For a simple entry, the name is a selected component, with the
10784 -- prefix being the task value, and the selector being the entry.
10786 if Nkind (Nam) = N_Selected_Component then
10787 Concval := Prefix (Nam);
10788 Ename := Selector_Name (Nam);
10789 Index := Empty;
10791 -- For a member of an entry family, the name is an indexed component
10792 -- where the prefix is a selected component, whose prefix in turn is
10793 -- the task value, and whose selector is the entry family. The single
10794 -- expression in the expressions list of the indexed component is the
10795 -- subscript for the family.
10797 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
10798 Concval := Prefix (Prefix (Nam));
10799 Ename := Selector_Name (Prefix (Nam));
10800 Index := First (Expressions (Nam));
10801 end if;
10802 end Extract_Entry;
10804 -------------------
10805 -- Family_Offset --
10806 -------------------
10808 function Family_Offset
10809 (Loc : Source_Ptr;
10810 Hi : Node_Id;
10811 Lo : Node_Id;
10812 Ttyp : Entity_Id;
10813 Cap : Boolean) return Node_Id
10815 Ityp : Entity_Id;
10816 Real_Hi : Node_Id;
10817 Real_Lo : Node_Id;
10819 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
10820 -- If one of the bounds is a reference to a discriminant, replace with
10821 -- corresponding discriminal of type. Within the body of a task retrieve
10822 -- the renamed discriminant by simple visibility, using its generated
10823 -- name. Within a protected object, find the original discriminant and
10824 -- replace it with the discriminal of the current protected operation.
10826 ------------------------------
10827 -- Convert_Discriminant_Ref --
10828 ------------------------------
10830 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
10831 Loc : constant Source_Ptr := Sloc (Bound);
10832 B : Node_Id;
10833 D : Entity_Id;
10835 begin
10836 if Is_Entity_Name (Bound)
10837 and then Ekind (Entity (Bound)) = E_Discriminant
10838 then
10839 if Is_Task_Type (Ttyp)
10840 and then Has_Completion (Ttyp)
10841 then
10842 B := Make_Identifier (Loc, Chars (Entity (Bound)));
10843 Find_Direct_Name (B);
10845 elsif Is_Protected_Type (Ttyp) then
10846 D := First_Discriminant (Ttyp);
10847 while Chars (D) /= Chars (Entity (Bound)) loop
10848 Next_Discriminant (D);
10849 end loop;
10851 B := New_Reference_To (Discriminal (D), Loc);
10853 else
10854 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
10855 end if;
10857 elsif Nkind (Bound) = N_Attribute_Reference then
10858 return Bound;
10860 else
10861 B := New_Copy_Tree (Bound);
10862 end if;
10864 return
10865 Make_Attribute_Reference (Loc,
10866 Attribute_Name => Name_Pos,
10867 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
10868 Expressions => New_List (B));
10869 end Convert_Discriminant_Ref;
10871 -- Start of processing for Family_Offset
10873 begin
10874 Real_Hi := Convert_Discriminant_Ref (Hi);
10875 Real_Lo := Convert_Discriminant_Ref (Lo);
10877 if Cap then
10878 if Is_Task_Type (Ttyp) then
10879 Ityp := RTE (RE_Task_Entry_Index);
10880 else
10881 Ityp := RTE (RE_Protected_Entry_Index);
10882 end if;
10884 Real_Hi :=
10885 Make_Attribute_Reference (Loc,
10886 Prefix => New_Reference_To (Ityp, Loc),
10887 Attribute_Name => Name_Min,
10888 Expressions => New_List (
10889 Real_Hi,
10890 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
10892 Real_Lo :=
10893 Make_Attribute_Reference (Loc,
10894 Prefix => New_Reference_To (Ityp, Loc),
10895 Attribute_Name => Name_Max,
10896 Expressions => New_List (
10897 Real_Lo,
10898 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
10899 end if;
10901 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
10902 end Family_Offset;
10904 -----------------
10905 -- Family_Size --
10906 -----------------
10908 function Family_Size
10909 (Loc : Source_Ptr;
10910 Hi : Node_Id;
10911 Lo : Node_Id;
10912 Ttyp : Entity_Id;
10913 Cap : Boolean) return Node_Id
10915 Ityp : Entity_Id;
10917 begin
10918 if Is_Task_Type (Ttyp) then
10919 Ityp := RTE (RE_Task_Entry_Index);
10920 else
10921 Ityp := RTE (RE_Protected_Entry_Index);
10922 end if;
10924 return
10925 Make_Attribute_Reference (Loc,
10926 Prefix => New_Reference_To (Ityp, Loc),
10927 Attribute_Name => Name_Max,
10928 Expressions => New_List (
10929 Make_Op_Add (Loc,
10930 Left_Opnd =>
10931 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
10932 Right_Opnd =>
10933 Make_Integer_Literal (Loc, 1)),
10934 Make_Integer_Literal (Loc, 0)));
10935 end Family_Size;
10937 -----------------------------------
10938 -- Find_Task_Or_Protected_Pragma --
10939 -----------------------------------
10941 function Find_Task_Or_Protected_Pragma
10942 (T : Node_Id;
10943 P : Name_Id) return Node_Id
10945 N : Node_Id;
10947 begin
10948 N := First (Visible_Declarations (T));
10949 while Present (N) loop
10950 if Nkind (N) = N_Pragma then
10951 if Pragma_Name (N) = P then
10952 return N;
10954 elsif P = Name_Priority
10955 and then Pragma_Name (N) = Name_Interrupt_Priority
10956 then
10957 return N;
10959 else
10960 Next (N);
10961 end if;
10963 else
10964 Next (N);
10965 end if;
10966 end loop;
10968 N := First (Private_Declarations (T));
10969 while Present (N) loop
10970 if Nkind (N) = N_Pragma then
10971 if Pragma_Name (N) = P then
10972 return N;
10974 elsif P = Name_Priority
10975 and then Pragma_Name (N) = Name_Interrupt_Priority
10976 then
10977 return N;
10979 else
10980 Next (N);
10981 end if;
10983 else
10984 Next (N);
10985 end if;
10986 end loop;
10988 raise Program_Error;
10989 end Find_Task_Or_Protected_Pragma;
10991 -------------------------------
10992 -- First_Protected_Operation --
10993 -------------------------------
10995 function First_Protected_Operation (D : List_Id) return Node_Id is
10996 First_Op : Node_Id;
10998 begin
10999 First_Op := First (D);
11000 while Present (First_Op)
11001 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
11002 loop
11003 Next (First_Op);
11004 end loop;
11006 return First_Op;
11007 end First_Protected_Operation;
11009 ---------------------------------------
11010 -- Install_Private_Data_Declarations --
11011 ---------------------------------------
11013 procedure Install_Private_Data_Declarations
11014 (Loc : Source_Ptr;
11015 Spec_Id : Entity_Id;
11016 Conc_Typ : Entity_Id;
11017 Body_Nod : Node_Id;
11018 Decls : List_Id;
11019 Barrier : Boolean := False;
11020 Family : Boolean := False)
11022 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
11023 Decl : Node_Id;
11024 Def : Node_Id;
11025 Insert_Node : Node_Id := Empty;
11026 Obj_Ent : Entity_Id;
11028 procedure Add (Decl : Node_Id);
11029 -- Add a single declaration after Insert_Node. If this is the first
11030 -- addition, Decl is added to the front of Decls and it becomes the
11031 -- insertion node.
11033 function Replace_Bound (Bound : Node_Id) return Node_Id;
11034 -- The bounds of an entry index may depend on discriminants, create a
11035 -- reference to the corresponding prival. Otherwise return a duplicate
11036 -- of the original bound.
11038 ---------
11039 -- Add --
11040 ---------
11042 procedure Add (Decl : Node_Id) is
11043 begin
11044 if No (Insert_Node) then
11045 Prepend_To (Decls, Decl);
11046 else
11047 Insert_After (Insert_Node, Decl);
11048 end if;
11050 Insert_Node := Decl;
11051 end Add;
11053 --------------------------
11054 -- Replace_Discriminant --
11055 --------------------------
11057 function Replace_Bound (Bound : Node_Id) return Node_Id is
11058 begin
11059 if Nkind (Bound) = N_Identifier
11060 and then Is_Discriminal (Entity (Bound))
11061 then
11062 return Make_Identifier (Loc, Chars (Entity (Bound)));
11063 else
11064 return Duplicate_Subexpr (Bound);
11065 end if;
11066 end Replace_Bound;
11068 -- Start of processing for Install_Private_Data_Declarations
11070 begin
11071 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
11072 -- formal parameter _O, _object or _task depending on the context.
11074 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
11076 -- Special processing of _O for barrier functions, protected entries
11077 -- and families.
11079 if Barrier
11080 or else
11081 (Is_Protected
11082 and then
11083 (Ekind (Spec_Id) = E_Entry
11084 or else Ekind (Spec_Id) = E_Entry_Family))
11085 then
11086 declare
11087 Conc_Rec : constant Entity_Id :=
11088 Corresponding_Record_Type (Conc_Typ);
11089 Typ_Id : constant Entity_Id :=
11090 Make_Defining_Identifier (Loc,
11091 New_External_Name (Chars (Conc_Rec), 'P'));
11092 begin
11093 -- Generate:
11094 -- type prot_typVP is access prot_typV;
11096 Decl :=
11097 Make_Full_Type_Declaration (Loc,
11098 Defining_Identifier => Typ_Id,
11099 Type_Definition =>
11100 Make_Access_To_Object_Definition (Loc,
11101 Subtype_Indication =>
11102 New_Reference_To (Conc_Rec, Loc)));
11103 Add (Decl);
11105 -- Generate:
11106 -- _object : prot_typVP := prot_typV (_O);
11108 Decl :=
11109 Make_Object_Declaration (Loc,
11110 Defining_Identifier =>
11111 Make_Defining_Identifier (Loc, Name_uObject),
11112 Object_Definition => New_Reference_To (Typ_Id, Loc),
11113 Expression =>
11114 Unchecked_Convert_To (Typ_Id,
11115 New_Reference_To (Obj_Ent, Loc)));
11116 Add (Decl);
11118 -- Set the reference to the concurrent object
11120 Obj_Ent := Defining_Identifier (Decl);
11121 end;
11122 end if;
11124 -- Step 2: Create the Protection object and build its declaration for
11125 -- any protected entry (family) of subprogram.
11127 if Is_Protected then
11128 declare
11129 Prot_Ent : constant Entity_Id :=
11130 Make_Defining_Identifier (Loc,
11131 New_Internal_Name ('R'));
11132 Prot_Typ : RE_Id;
11134 begin
11135 Set_Protection_Object (Spec_Id, Prot_Ent);
11137 -- Determine the proper protection type
11139 if Has_Attach_Handler (Conc_Typ)
11140 and then not Restricted_Profile
11141 then
11142 Prot_Typ := RE_Static_Interrupt_Protection;
11144 elsif Has_Interrupt_Handler (Conc_Typ) then
11145 Prot_Typ := RE_Dynamic_Interrupt_Protection;
11147 -- The type has explicit entries or generated primitive entry
11148 -- wrappers.
11150 elsif Has_Entries (Conc_Typ)
11151 or else
11152 (Ada_Version >= Ada_05
11153 and then Present (Interface_List (Parent (Conc_Typ))))
11154 then
11155 case Corresponding_Runtime_Package (Conc_Typ) is
11156 when System_Tasking_Protected_Objects_Entries =>
11157 Prot_Typ := RE_Protection_Entries;
11159 when System_Tasking_Protected_Objects_Single_Entry =>
11160 Prot_Typ := RE_Protection_Entry;
11162 when others =>
11163 raise Program_Error;
11164 end case;
11166 else
11167 Prot_Typ := RE_Protection;
11168 end if;
11170 -- Generate:
11171 -- conc_typR : protection_typ renames _object._object;
11173 Decl :=
11174 Make_Object_Renaming_Declaration (Loc,
11175 Defining_Identifier => Prot_Ent,
11176 Subtype_Mark =>
11177 New_Reference_To (RTE (Prot_Typ), Loc),
11178 Name =>
11179 Make_Selected_Component (Loc,
11180 Prefix =>
11181 New_Reference_To (Obj_Ent, Loc),
11182 Selector_Name =>
11183 Make_Identifier (Loc, Name_uObject)));
11184 Add (Decl);
11185 end;
11186 end if;
11188 -- Step 3: Add discriminant renamings (if any)
11190 if Has_Discriminants (Conc_Typ) then
11191 declare
11192 D : Entity_Id;
11194 begin
11195 D := First_Discriminant (Conc_Typ);
11196 while Present (D) loop
11198 -- Adjust the source location
11200 Set_Sloc (Discriminal (D), Loc);
11202 -- Generate:
11203 -- discr_name : discr_typ renames _object.discr_name;
11204 -- or
11205 -- discr_name : discr_typ renames _task.discr_name;
11207 Decl :=
11208 Make_Object_Renaming_Declaration (Loc,
11209 Defining_Identifier => Discriminal (D),
11210 Subtype_Mark => New_Reference_To (Etype (D), Loc),
11211 Name =>
11212 Make_Selected_Component (Loc,
11213 Prefix => New_Reference_To (Obj_Ent, Loc),
11214 Selector_Name => Make_Identifier (Loc, Chars (D))));
11215 Add (Decl);
11217 Next_Discriminant (D);
11218 end loop;
11219 end;
11220 end if;
11222 -- Step 4: Add private component renamings (if any)
11224 if Is_Protected then
11225 Def := Protected_Definition (Parent (Conc_Typ));
11227 if Present (Private_Declarations (Def)) then
11228 declare
11229 Comp : Node_Id;
11230 Comp_Id : Entity_Id;
11231 Decl_Id : Entity_Id;
11233 begin
11234 Comp := First (Private_Declarations (Def));
11235 while Present (Comp) loop
11236 if Nkind (Comp) = N_Component_Declaration then
11237 Comp_Id := Defining_Identifier (Comp);
11238 Decl_Id :=
11239 Make_Defining_Identifier (Loc, Chars (Comp_Id));
11241 -- Minimal decoration
11243 if Ekind (Spec_Id) = E_Function then
11244 Set_Ekind (Decl_Id, E_Constant);
11245 else
11246 Set_Ekind (Decl_Id, E_Variable);
11247 end if;
11249 Set_Prival (Comp_Id, Decl_Id);
11250 Set_Prival_Link (Decl_Id, Comp_Id);
11251 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
11253 -- Generate:
11254 -- comp_name : comp_typ renames _object.comp_name;
11256 Decl :=
11257 Make_Object_Renaming_Declaration (Loc,
11258 Defining_Identifier => Decl_Id,
11259 Subtype_Mark =>
11260 New_Reference_To (Etype (Comp_Id), Loc),
11261 Name =>
11262 Make_Selected_Component (Loc,
11263 Prefix =>
11264 New_Reference_To (Obj_Ent, Loc),
11265 Selector_Name =>
11266 Make_Identifier (Loc, Chars (Comp_Id))));
11267 Add (Decl);
11268 end if;
11270 Next (Comp);
11271 end loop;
11272 end;
11273 end if;
11274 end if;
11276 -- Step 5: Add the declaration of the entry index and the associated
11277 -- type for barrier functions and entry families.
11279 if (Barrier and then Family)
11280 or else Ekind (Spec_Id) = E_Entry_Family
11281 then
11282 declare
11283 E : constant Entity_Id := Index_Object (Spec_Id);
11284 Index : constant Entity_Id :=
11285 Defining_Identifier (
11286 Entry_Index_Specification (
11287 Entry_Body_Formal_Part (Body_Nod)));
11288 Index_Con : constant Entity_Id :=
11289 Make_Defining_Identifier (Loc, Chars (Index));
11290 High : Node_Id;
11291 Index_Typ : Entity_Id;
11292 Low : Node_Id;
11294 begin
11295 -- Minimal decoration
11297 Set_Ekind (Index_Con, E_Constant);
11298 Set_Entry_Index_Constant (Index, Index_Con);
11299 Set_Discriminal_Link (Index_Con, Index);
11301 -- Retrieve the bounds of the entry family
11303 High := Type_High_Bound (Etype (Index));
11304 Low := Type_Low_Bound (Etype (Index));
11306 -- In the simple case the entry family is given by a subtype
11307 -- mark and the index constant has the same type.
11309 if Is_Entity_Name (Original_Node (
11310 Discrete_Subtype_Definition (Parent (Index))))
11311 then
11312 Index_Typ := Etype (Index);
11314 -- Otherwise a new subtype declaration is required
11316 else
11317 High := Replace_Bound (High);
11318 Low := Replace_Bound (Low);
11320 Index_Typ :=
11321 Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
11323 -- Generate:
11324 -- subtype Jnn is <Etype of Index> range Low .. High;
11326 Decl :=
11327 Make_Subtype_Declaration (Loc,
11328 Defining_Identifier => Index_Typ,
11329 Subtype_Indication =>
11330 Make_Subtype_Indication (Loc,
11331 Subtype_Mark =>
11332 New_Reference_To (Base_Type (Etype (Index)), Loc),
11333 Constraint =>
11334 Make_Range_Constraint (Loc,
11335 Range_Expression =>
11336 Make_Range (Loc, Low, High))));
11337 Add (Decl);
11338 end if;
11340 Set_Etype (Index_Con, Index_Typ);
11342 -- Create the object which designates the index:
11343 -- J : constant Jnn :=
11344 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
11346 -- where Jnn is the subtype created above or the original type of
11347 -- the index, _E is a formal of the protected body subprogram and
11348 -- <index expr> is the index of the first family member.
11350 Decl :=
11351 Make_Object_Declaration (Loc,
11352 Defining_Identifier => Index_Con,
11353 Constant_Present => True,
11354 Object_Definition =>
11355 New_Reference_To (Index_Typ, Loc),
11357 Expression =>
11358 Make_Attribute_Reference (Loc,
11359 Prefix =>
11360 New_Reference_To (Index_Typ, Loc),
11361 Attribute_Name => Name_Val,
11363 Expressions => New_List (
11365 Make_Op_Add (Loc,
11366 Left_Opnd =>
11367 Make_Op_Subtract (Loc,
11368 Left_Opnd =>
11369 New_Reference_To (E, Loc),
11370 Right_Opnd =>
11371 Entry_Index_Expression (Loc,
11372 Defining_Identifier (Body_Nod),
11373 Empty, Conc_Typ)),
11375 Right_Opnd =>
11376 Make_Attribute_Reference (Loc,
11377 Prefix =>
11378 New_Reference_To (Index_Typ, Loc),
11379 Attribute_Name => Name_Pos,
11380 Expressions => New_List (
11381 Make_Attribute_Reference (Loc,
11382 Prefix =>
11383 New_Reference_To (Index_Typ, Loc),
11384 Attribute_Name => Name_First)))))));
11385 Add (Decl);
11386 end;
11387 end if;
11388 end Install_Private_Data_Declarations;
11390 ---------------------------------
11391 -- Is_Potentially_Large_Family --
11392 ---------------------------------
11394 function Is_Potentially_Large_Family
11395 (Base_Index : Entity_Id;
11396 Conctyp : Entity_Id;
11397 Lo : Node_Id;
11398 Hi : Node_Id) return Boolean
11400 begin
11401 return Scope (Base_Index) = Standard_Standard
11402 and then Base_Index = Base_Type (Standard_Integer)
11403 and then Has_Discriminants (Conctyp)
11404 and then Present
11405 (Discriminant_Default_Value (First_Discriminant (Conctyp)))
11406 and then
11407 (Denotes_Discriminant (Lo, True)
11408 or else Denotes_Discriminant (Hi, True));
11409 end Is_Potentially_Large_Family;
11411 ------------------
11412 -- Index_Object --
11413 ------------------
11415 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
11416 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
11417 Formal : Entity_Id;
11419 begin
11420 Formal := First_Formal (Bod_Subp);
11421 while Present (Formal) loop
11423 -- Look for formal parameter _E
11425 if Chars (Formal) = Name_uE then
11426 return Formal;
11427 end if;
11429 Next_Formal (Formal);
11430 end loop;
11432 -- A protected body subprogram should always have the parameter in
11433 -- question.
11435 raise Program_Error;
11436 end Index_Object;
11438 --------------------------------
11439 -- Make_Initialize_Protection --
11440 --------------------------------
11442 function Make_Initialize_Protection
11443 (Protect_Rec : Entity_Id) return List_Id
11445 Loc : constant Source_Ptr := Sloc (Protect_Rec);
11446 P_Arr : Entity_Id;
11447 Pdef : Node_Id;
11448 Pdec : Node_Id;
11449 Ptyp : constant Node_Id :=
11450 Corresponding_Concurrent_Type (Protect_Rec);
11451 Args : List_Id;
11452 L : constant List_Id := New_List;
11453 Has_Entry : constant Boolean := Has_Entries (Ptyp);
11454 Restricted : constant Boolean := Restricted_Profile;
11456 begin
11457 -- We may need two calls to properly initialize the object, one to
11458 -- Initialize_Protection, and possibly one to Install_Handlers if we
11459 -- have a pragma Attach_Handler.
11461 -- Get protected declaration. In the case of a task type declaration,
11462 -- this is simply the parent of the protected type entity. In the single
11463 -- protected object declaration, this parent will be the implicit type,
11464 -- and we can find the corresponding single protected object declaration
11465 -- by searching forward in the declaration list in the tree.
11467 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
11468 -- of this type should have been removed during semantic analysis.
11470 Pdec := Parent (Ptyp);
11471 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
11472 N_Single_Protected_Declaration)
11473 loop
11474 Next (Pdec);
11475 end loop;
11477 -- Now we can find the object definition from this declaration
11479 Pdef := Protected_Definition (Pdec);
11481 -- Build the parameter list for the call. Note that _Init is the name
11482 -- of the formal for the object to be initialized, which is the task
11483 -- value record itself.
11485 Args := New_List;
11487 -- Object parameter. This is a pointer to the object of type
11488 -- Protection used by the GNARL to control the protected object.
11490 Append_To (Args,
11491 Make_Attribute_Reference (Loc,
11492 Prefix =>
11493 Make_Selected_Component (Loc,
11494 Prefix => Make_Identifier (Loc, Name_uInit),
11495 Selector_Name => Make_Identifier (Loc, Name_uObject)),
11496 Attribute_Name => Name_Unchecked_Access));
11498 -- Priority parameter. Set to Unspecified_Priority unless there is a
11499 -- priority pragma, in which case we take the value from the pragma,
11500 -- or there is an interrupt pragma and no priority pragma, and we
11501 -- set the ceiling to Interrupt_Priority'Last, an implementation-
11502 -- defined value, see D.3(10).
11504 if Present (Pdef)
11505 and then Has_Priority_Pragma (Pdef)
11506 then
11507 declare
11508 Prio : constant Node_Id :=
11509 Expression
11510 (First
11511 (Pragma_Argument_Associations
11512 (Find_Task_Or_Protected_Pragma
11513 (Pdef, Name_Priority))));
11514 Temp : Entity_Id;
11516 begin
11517 -- If priority is a static expression, then we can duplicate it
11518 -- with no problem and simply append it to the argument list.
11520 if Is_Static_Expression (Prio) then
11521 Append_To (Args,
11522 Duplicate_Subexpr_No_Checks (Prio));
11524 -- Otherwise, the priority may be a per-object expression, if it
11525 -- depends on a discriminant of the type. In this case, create
11526 -- local variable to capture the expression. Note that it is
11527 -- really necessary to create this variable explicitly. It might
11528 -- be thought that removing side effects would the appropriate
11529 -- approach, but that could generate declarations improperly
11530 -- placed in the enclosing scope.
11532 -- Note: Use System.Any_Priority as the expected type for the
11533 -- non-static priority expression, in case the expression has not
11534 -- been analyzed yet (as occurs for example with pragma
11535 -- Interrupt_Priority).
11537 else
11538 Temp :=
11539 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
11541 Append_To (L,
11542 Make_Object_Declaration (Loc,
11543 Defining_Identifier => Temp,
11544 Object_Definition =>
11545 New_Occurrence_Of (RTE (RE_Any_Priority), Loc),
11546 Expression => Relocate_Node (Prio)));
11548 Append_To (Args, New_Occurrence_Of (Temp, Loc));
11549 end if;
11550 end;
11552 -- When no priority is specified but an xx_Handler pragma is, we default
11553 -- to System.Interrupts.Default_Interrupt_Priority, see D.3(10).
11555 elsif Has_Interrupt_Handler (Ptyp)
11556 or else Has_Attach_Handler (Ptyp)
11557 then
11558 Append_To (Args,
11559 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
11561 -- Normal case, no priority or xx_Handler specified, default priority
11563 else
11564 Append_To (Args,
11565 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
11566 end if;
11568 -- Test for Compiler_Info parameter. This parameter allows entry body
11569 -- procedures and barrier functions to be called from the runtime. It
11570 -- is a pointer to the record generated by the compiler to represent
11571 -- the protected object.
11573 if Has_Entry
11574 or else Has_Interrupt_Handler (Ptyp)
11575 or else Has_Attach_Handler (Ptyp)
11576 or else Has_Interfaces (Protect_Rec)
11577 then
11578 declare
11579 Pkg_Id : constant RTU_Id :=
11580 Corresponding_Runtime_Package (Ptyp);
11581 Called_Subp : RE_Id;
11583 begin
11584 case Pkg_Id is
11585 when System_Tasking_Protected_Objects_Entries =>
11586 Called_Subp := RE_Initialize_Protection_Entries;
11588 when System_Tasking_Protected_Objects =>
11589 Called_Subp := RE_Initialize_Protection;
11591 when System_Tasking_Protected_Objects_Single_Entry =>
11592 Called_Subp := RE_Initialize_Protection_Entry;
11594 when others =>
11595 raise Program_Error;
11596 end case;
11598 if Has_Entry or else not Restricted then
11599 Append_To (Args,
11600 Make_Attribute_Reference (Loc,
11601 Prefix => Make_Identifier (Loc, Name_uInit),
11602 Attribute_Name => Name_Address));
11603 end if;
11605 -- Entry_Bodies parameter. This is a pointer to an array of
11606 -- pointers to the entry body procedures and barrier functions of
11607 -- the object. If the protected type has no entries this object
11608 -- will not exist, in this case, pass a null.
11610 if Has_Entry then
11611 P_Arr := Entry_Bodies_Array (Ptyp);
11613 Append_To (Args,
11614 Make_Attribute_Reference (Loc,
11615 Prefix => New_Reference_To (P_Arr, Loc),
11616 Attribute_Name => Name_Unrestricted_Access));
11618 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
11620 -- Find index mapping function (clumsy but ok for now)
11622 while Ekind (P_Arr) /= E_Function loop
11623 Next_Entity (P_Arr);
11624 end loop;
11626 Append_To (Args,
11627 Make_Attribute_Reference (Loc,
11628 Prefix =>
11629 New_Reference_To (P_Arr, Loc),
11630 Attribute_Name => Name_Unrestricted_Access));
11632 -- Build_Entry_Names generation flag. When set to true, the
11633 -- runtime will allocate an array to hold the string names
11634 -- of protected entries.
11636 if not Restricted_Profile then
11637 if Entry_Names_OK then
11638 Append_To (Args,
11639 New_Reference_To (Standard_True, Loc));
11640 else
11641 Append_To (Args,
11642 New_Reference_To (Standard_False, Loc));
11643 end if;
11644 end if;
11645 end if;
11647 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
11648 Append_To (Args, Make_Null (Loc));
11650 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
11651 Append_To (Args, Make_Null (Loc));
11652 Append_To (Args, Make_Null (Loc));
11653 Append_To (Args, New_Reference_To (Standard_False, Loc));
11654 end if;
11656 Append_To (L,
11657 Make_Procedure_Call_Statement (Loc,
11658 Name => New_Reference_To (RTE (Called_Subp), Loc),
11659 Parameter_Associations => Args));
11660 end;
11661 else
11662 Append_To (L,
11663 Make_Procedure_Call_Statement (Loc,
11664 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
11665 Parameter_Associations => Args));
11666 end if;
11668 if Has_Attach_Handler (Ptyp) then
11670 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
11671 -- make the following call:
11673 -- Install_Handlers (_object,
11674 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
11676 -- or, in the case of Ravenscar:
11678 -- Install_Restricted_Handlers
11679 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
11681 declare
11682 Args : constant List_Id := New_List;
11683 Table : constant List_Id := New_List;
11684 Ritem : Node_Id := First_Rep_Item (Ptyp);
11686 begin
11687 -- Build the Attach_Handler table argument
11689 while Present (Ritem) loop
11690 if Nkind (Ritem) = N_Pragma
11691 and then Pragma_Name (Ritem) = Name_Attach_Handler
11692 then
11693 declare
11694 Handler : constant Node_Id :=
11695 First (Pragma_Argument_Associations (Ritem));
11697 Interrupt : constant Node_Id := Next (Handler);
11698 Expr : constant Node_Id := Expression (Interrupt);
11700 begin
11701 Append_To (Table,
11702 Make_Aggregate (Loc, Expressions => New_List (
11703 Unchecked_Convert_To
11704 (RTE (RE_System_Interrupt_Id), Expr),
11705 Make_Attribute_Reference (Loc,
11706 Prefix => Make_Selected_Component (Loc,
11707 Make_Identifier (Loc, Name_uInit),
11708 Duplicate_Subexpr_No_Checks
11709 (Expression (Handler))),
11710 Attribute_Name => Name_Access))));
11711 end;
11712 end if;
11714 Next_Rep_Item (Ritem);
11715 end loop;
11717 -- Append the table argument we just built
11719 Append_To (Args, Make_Aggregate (Loc, Table));
11721 -- Append the Install_Handlers (or Install_Restricted_Handlers)
11722 -- call to the statements.
11724 if Restricted then
11725 -- Call a simplified version of Install_Handlers to be used
11726 -- when the Ravenscar restrictions are in effect
11727 -- (Install_Restricted_Handlers).
11729 Append_To (L,
11730 Make_Procedure_Call_Statement (Loc,
11731 Name =>
11732 New_Reference_To
11733 (RTE (RE_Install_Restricted_Handlers), Loc),
11734 Parameter_Associations => Args));
11736 else
11737 -- First, prepends the _object argument
11739 Prepend_To (Args,
11740 Make_Attribute_Reference (Loc,
11741 Prefix =>
11742 Make_Selected_Component (Loc,
11743 Prefix => Make_Identifier (Loc, Name_uInit),
11744 Selector_Name => Make_Identifier (Loc, Name_uObject)),
11745 Attribute_Name => Name_Unchecked_Access));
11747 -- Then, insert call to Install_Handlers
11749 Append_To (L,
11750 Make_Procedure_Call_Statement (Loc,
11751 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
11752 Parameter_Associations => Args));
11753 end if;
11754 end;
11755 end if;
11757 return L;
11758 end Make_Initialize_Protection;
11760 ---------------------------
11761 -- Make_Task_Create_Call --
11762 ---------------------------
11764 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
11765 Loc : constant Source_Ptr := Sloc (Task_Rec);
11766 Args : List_Id;
11767 Ecount : Node_Id;
11768 Name : Node_Id;
11769 Tdec : Node_Id;
11770 Tdef : Node_Id;
11771 Tnam : Name_Id;
11772 Ttyp : Node_Id;
11774 begin
11775 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
11776 Tnam := Chars (Ttyp);
11778 -- Get task declaration. In the case of a task type declaration, this is
11779 -- simply the parent of the task type entity. In the single task
11780 -- declaration, this parent will be the implicit type, and we can find
11781 -- the corresponding single task declaration by searching forward in the
11782 -- declaration list in the tree.
11784 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
11785 -- this type should have been removed during semantic analysis.
11787 Tdec := Parent (Ttyp);
11788 while not Nkind_In (Tdec, N_Task_Type_Declaration,
11789 N_Single_Task_Declaration)
11790 loop
11791 Next (Tdec);
11792 end loop;
11794 -- Now we can find the task definition from this declaration
11796 Tdef := Task_Definition (Tdec);
11798 -- Build the parameter list for the call. Note that _Init is the name
11799 -- of the formal for the object to be initialized, which is the task
11800 -- value record itself.
11802 Args := New_List;
11804 -- Priority parameter. Set to Unspecified_Priority unless there is a
11805 -- priority pragma, in which case we take the value from the pragma.
11807 if Present (Tdef) and then Has_Priority_Pragma (Tdef) then
11808 Append_To (Args,
11809 Make_Selected_Component (Loc,
11810 Prefix => Make_Identifier (Loc, Name_uInit),
11811 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
11812 else
11813 Append_To (Args,
11814 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
11815 end if;
11817 -- Optional Stack parameter
11819 if Restricted_Profile then
11821 -- If the stack has been preallocated by the expander then
11822 -- pass its address. Otherwise, pass a null address.
11824 if Preallocated_Stacks_On_Target then
11825 Append_To (Args,
11826 Make_Attribute_Reference (Loc,
11827 Prefix => Make_Selected_Component (Loc,
11828 Prefix => Make_Identifier (Loc, Name_uInit),
11829 Selector_Name =>
11830 Make_Identifier (Loc, Name_uStack)),
11831 Attribute_Name => Name_Address));
11833 else
11834 Append_To (Args,
11835 New_Reference_To (RTE (RE_Null_Address), Loc));
11836 end if;
11837 end if;
11839 -- Size parameter. If no Storage_Size pragma is present, then
11840 -- the size is taken from the taskZ variable for the type, which
11841 -- is either Unspecified_Size, or has been reset by the use of
11842 -- a Storage_Size attribute definition clause. If a pragma is
11843 -- present, then the size is taken from the _Size field of the
11844 -- task value record, which was set from the pragma value.
11846 if Present (Tdef)
11847 and then Has_Storage_Size_Pragma (Tdef)
11848 then
11849 Append_To (Args,
11850 Make_Selected_Component (Loc,
11851 Prefix => Make_Identifier (Loc, Name_uInit),
11852 Selector_Name => Make_Identifier (Loc, Name_uSize)));
11854 else
11855 Append_To (Args,
11856 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
11857 end if;
11859 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
11860 -- Task_Info pragma, in which case we take the value from the pragma.
11862 if Present (Tdef)
11863 and then Has_Task_Info_Pragma (Tdef)
11864 then
11865 Append_To (Args,
11866 Make_Selected_Component (Loc,
11867 Prefix => Make_Identifier (Loc, Name_uInit),
11868 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
11870 else
11871 Append_To (Args,
11872 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
11873 end if;
11875 if not Restricted_Profile then
11877 -- Deadline parameter. If no Relative_Deadline pragma is present,
11878 -- then the deadline is Time_Span_Zero. If a pragma is present, then
11879 -- the deadline is taken from the _Relative_Deadline field of the
11880 -- task value record, which was set from the pragma value. Note that
11881 -- this parameter must not be generated for the restricted profiles
11882 -- since Ravenscar does not allow deadlines.
11884 -- Case where pragma Relative_Deadline applies: use given value
11886 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
11887 Append_To (Args,
11888 Make_Selected_Component (Loc,
11889 Prefix => Make_Identifier (Loc, Name_uInit),
11890 Selector_Name =>
11891 Make_Identifier (Loc, Name_uRelative_Deadline)));
11893 -- No pragma Relative_Deadline apply to the task
11895 else
11896 Append_To (Args,
11897 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
11898 end if;
11900 -- Number of entries. This is an expression of the form:
11902 -- n + _Init.a'Length + _Init.a'B'Length + ...
11904 -- where a,b... are the entry family names for the task definition
11906 Ecount :=
11907 Build_Entry_Count_Expression
11908 (Ttyp,
11909 Component_Items
11910 (Component_List
11911 (Type_Definition
11912 (Parent (Corresponding_Record_Type (Ttyp))))),
11913 Loc);
11914 Append_To (Args, Ecount);
11916 -- Master parameter. This is a reference to the _Master parameter of
11917 -- the initialization procedure, except in the case of the pragma
11918 -- Restrictions (No_Task_Hierarchy) where the value is fixed to 3.
11919 -- See comments in System.Tasking.Initialization.Init_RTS for the
11920 -- value 3.
11922 if Restriction_Active (No_Task_Hierarchy) = False then
11923 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
11924 else
11925 Append_To (Args, Make_Integer_Literal (Loc, 3));
11926 end if;
11927 end if;
11929 -- State parameter. This is a pointer to the task body procedure. The
11930 -- required value is obtained by taking 'Unrestricted_Access of the task
11931 -- body procedure and converting it (with an unchecked conversion) to
11932 -- the type required by the task kernel. For further details, see the
11933 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
11934 -- than 'Address in order to avoid creating trampolines.
11936 declare
11937 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
11938 Subp_Ptr_Typ : constant Node_Id :=
11939 Create_Itype (E_Access_Subprogram_Type, Tdec);
11940 Ref : constant Node_Id := Make_Itype_Reference (Loc);
11942 begin
11943 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
11944 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
11946 -- Be sure to freeze a reference to the access-to-subprogram type,
11947 -- otherwise gigi will complain that it's in the wrong scope, because
11948 -- it's actually inside the init procedure for the record type that
11949 -- corresponds to the task type.
11951 -- This processing is causing a crash in the .NET/JVM back ends that
11952 -- is not yet understood, so skip it in these cases ???
11954 if VM_Target = No_VM then
11955 Set_Itype (Ref, Subp_Ptr_Typ);
11956 Append_Freeze_Action (Task_Rec, Ref);
11958 Append_To (Args,
11959 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
11960 Make_Qualified_Expression (Loc,
11961 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
11962 Expression =>
11963 Make_Attribute_Reference (Loc,
11964 Prefix =>
11965 New_Occurrence_Of (Body_Proc, Loc),
11966 Attribute_Name => Name_Unrestricted_Access))));
11968 -- For the .NET/JVM cases revert to the original code below ???
11970 else
11971 Append_To (Args,
11972 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
11973 Make_Attribute_Reference (Loc,
11974 Prefix =>
11975 New_Occurrence_Of (Body_Proc, Loc),
11976 Attribute_Name => Name_Address)));
11977 end if;
11978 end;
11980 -- Discriminants parameter. This is just the address of the task
11981 -- value record itself (which contains the discriminant values
11983 Append_To (Args,
11984 Make_Attribute_Reference (Loc,
11985 Prefix => Make_Identifier (Loc, Name_uInit),
11986 Attribute_Name => Name_Address));
11988 -- Elaborated parameter. This is an access to the elaboration Boolean
11990 Append_To (Args,
11991 Make_Attribute_Reference (Loc,
11992 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
11993 Attribute_Name => Name_Unchecked_Access));
11995 -- Chain parameter. This is a reference to the _Chain parameter of
11996 -- the initialization procedure.
11998 Append_To (Args, Make_Identifier (Loc, Name_uChain));
12000 -- Task name parameter. Take this from the _Task_Id parameter to the
12001 -- init call unless there is a Task_Name pragma, in which case we take
12002 -- the value from the pragma.
12004 if Present (Tdef)
12005 and then Has_Task_Name_Pragma (Tdef)
12006 then
12007 Append_To (Args,
12008 New_Copy (
12009 Expression (First (
12010 Pragma_Argument_Associations (
12011 Find_Task_Or_Protected_Pragma
12012 (Tdef, Name_Task_Name))))));
12014 else
12015 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
12016 end if;
12018 -- Created_Task parameter. This is the _Task_Id field of the task
12019 -- record value
12021 Append_To (Args,
12022 Make_Selected_Component (Loc,
12023 Prefix => Make_Identifier (Loc, Name_uInit),
12024 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
12026 -- Build_Entry_Names generation flag. When set to true, the runtime
12027 -- will allocate an array to hold the string names of task entries.
12029 if not Restricted_Profile then
12030 if Has_Entries (Ttyp)
12031 and then Entry_Names_OK
12032 then
12033 Append_To (Args, New_Reference_To (Standard_True, Loc));
12034 else
12035 Append_To (Args, New_Reference_To (Standard_False, Loc));
12036 end if;
12037 end if;
12039 if Restricted_Profile then
12040 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
12041 else
12042 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
12043 end if;
12045 return
12046 Make_Procedure_Call_Statement (Loc,
12047 Name => Name,
12048 Parameter_Associations => Args);
12049 end Make_Task_Create_Call;
12051 ------------------------------
12052 -- Next_Protected_Operation --
12053 ------------------------------
12055 function Next_Protected_Operation (N : Node_Id) return Node_Id is
12056 Next_Op : Node_Id;
12058 begin
12059 Next_Op := Next (N);
12060 while Present (Next_Op)
12061 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
12062 loop
12063 Next (Next_Op);
12064 end loop;
12066 return Next_Op;
12067 end Next_Protected_Operation;
12069 ---------------------
12070 -- Null_Statements --
12071 ---------------------
12073 function Null_Statements (Stats : List_Id) return Boolean is
12074 Stmt : Node_Id;
12076 begin
12077 Stmt := First (Stats);
12078 while Nkind (Stmt) /= N_Empty
12079 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
12080 or else
12081 (Nkind (Stmt) = N_Pragma
12082 and then (Pragma_Name (Stmt) = Name_Unreferenced
12083 or else
12084 Pragma_Name (Stmt) = Name_Unmodified
12085 or else
12086 Pragma_Name (Stmt) = Name_Warnings)))
12087 loop
12088 Next (Stmt);
12089 end loop;
12091 return Nkind (Stmt) = N_Empty;
12092 end Null_Statements;
12094 --------------------------
12095 -- Parameter_Block_Pack --
12096 --------------------------
12098 function Parameter_Block_Pack
12099 (Loc : Source_Ptr;
12100 Blk_Typ : Entity_Id;
12101 Actuals : List_Id;
12102 Formals : List_Id;
12103 Decls : List_Id;
12104 Stmts : List_Id) return Node_Id
12106 Actual : Entity_Id;
12107 Expr : Node_Id := Empty;
12108 Formal : Entity_Id;
12109 Has_Param : Boolean := False;
12110 P : Entity_Id;
12111 Params : List_Id;
12112 Temp_Asn : Node_Id;
12113 Temp_Nam : Node_Id;
12115 begin
12116 Actual := First (Actuals);
12117 Formal := Defining_Identifier (First (Formals));
12118 Params := New_List;
12120 while Present (Actual) loop
12121 if Is_By_Copy_Type (Etype (Actual)) then
12122 -- Generate:
12123 -- Jnn : aliased <formal-type>
12125 Temp_Nam :=
12126 Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
12128 Append_To (Decls,
12129 Make_Object_Declaration (Loc,
12130 Aliased_Present =>
12131 True,
12132 Defining_Identifier =>
12133 Temp_Nam,
12134 Object_Definition =>
12135 New_Reference_To (Etype (Formal), Loc)));
12137 if Ekind (Formal) /= E_Out_Parameter then
12139 -- Generate:
12140 -- Jnn := <actual>
12142 Temp_Asn :=
12143 New_Reference_To (Temp_Nam, Loc);
12145 Set_Assignment_OK (Temp_Asn);
12147 Append_To (Stmts,
12148 Make_Assignment_Statement (Loc,
12149 Name =>
12150 Temp_Asn,
12151 Expression =>
12152 New_Copy_Tree (Actual)));
12153 end if;
12155 -- Generate:
12156 -- Jnn'unchecked_access
12158 Append_To (Params,
12159 Make_Attribute_Reference (Loc,
12160 Attribute_Name =>
12161 Name_Unchecked_Access,
12162 Prefix =>
12163 New_Reference_To (Temp_Nam, Loc)));
12165 Has_Param := True;
12167 -- The controlling parameter is omitted
12169 else
12170 if not Is_Controlling_Actual (Actual) then
12171 Append_To (Params,
12172 Make_Reference (Loc, New_Copy_Tree (Actual)));
12174 Has_Param := True;
12175 end if;
12176 end if;
12178 Next_Actual (Actual);
12179 Next_Formal_With_Extras (Formal);
12180 end loop;
12182 if Has_Param then
12183 Expr := Make_Aggregate (Loc, Params);
12184 end if;
12186 -- Generate:
12187 -- P : Ann := (
12188 -- J1'unchecked_access;
12189 -- <actual2>'reference;
12190 -- ...);
12192 P := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
12194 Append_To (Decls,
12195 Make_Object_Declaration (Loc,
12196 Defining_Identifier =>
12198 Object_Definition =>
12199 New_Reference_To (Blk_Typ, Loc),
12200 Expression =>
12201 Expr));
12203 return P;
12204 end Parameter_Block_Pack;
12206 ----------------------------
12207 -- Parameter_Block_Unpack --
12208 ----------------------------
12210 function Parameter_Block_Unpack
12211 (Loc : Source_Ptr;
12212 P : Entity_Id;
12213 Actuals : List_Id;
12214 Formals : List_Id) return List_Id
12216 Actual : Entity_Id;
12217 Asnmt : Node_Id;
12218 Formal : Entity_Id;
12219 Has_Asnmt : Boolean := False;
12220 Result : constant List_Id := New_List;
12222 begin
12223 Actual := First (Actuals);
12224 Formal := Defining_Identifier (First (Formals));
12225 while Present (Actual) loop
12226 if Is_By_Copy_Type (Etype (Actual))
12227 and then Ekind (Formal) /= E_In_Parameter
12228 then
12229 -- Generate:
12230 -- <actual> := P.<formal>;
12232 Asnmt :=
12233 Make_Assignment_Statement (Loc,
12234 Name =>
12235 New_Copy (Actual),
12236 Expression =>
12237 Make_Explicit_Dereference (Loc,
12238 Make_Selected_Component (Loc,
12239 Prefix =>
12240 New_Reference_To (P, Loc),
12241 Selector_Name =>
12242 Make_Identifier (Loc, Chars (Formal)))));
12244 Set_Assignment_OK (Name (Asnmt));
12245 Append_To (Result, Asnmt);
12247 Has_Asnmt := True;
12248 end if;
12250 Next_Actual (Actual);
12251 Next_Formal_With_Extras (Formal);
12252 end loop;
12254 if Has_Asnmt then
12255 return Result;
12256 else
12257 return New_List (Make_Null_Statement (Loc));
12258 end if;
12259 end Parameter_Block_Unpack;
12261 ----------------------
12262 -- Set_Discriminals --
12263 ----------------------
12265 procedure Set_Discriminals (Dec : Node_Id) is
12266 D : Entity_Id;
12267 Pdef : Entity_Id;
12268 D_Minal : Entity_Id;
12270 begin
12271 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
12272 Pdef := Defining_Identifier (Dec);
12274 if Has_Discriminants (Pdef) then
12275 D := First_Discriminant (Pdef);
12276 while Present (D) loop
12277 D_Minal :=
12278 Make_Defining_Identifier (Sloc (D),
12279 Chars => New_External_Name (Chars (D), 'D'));
12281 Set_Ekind (D_Minal, E_Constant);
12282 Set_Etype (D_Minal, Etype (D));
12283 Set_Scope (D_Minal, Pdef);
12284 Set_Discriminal (D, D_Minal);
12285 Set_Discriminal_Link (D_Minal, D);
12287 Next_Discriminant (D);
12288 end loop;
12289 end if;
12290 end Set_Discriminals;
12292 -----------------------
12293 -- Trivial_Accept_OK --
12294 -----------------------
12296 function Trivial_Accept_OK return Boolean is
12297 begin
12298 case Opt.Task_Dispatching_Policy is
12300 -- If we have the default task dispatching policy in effect, we can
12301 -- definitely do the optimization (one way of looking at this is to
12302 -- think of the formal definition of the default policy being allowed
12303 -- to run any task it likes after a rendezvous, so even if notionally
12304 -- a full rescheduling occurs, we can say that our dispatching policy
12305 -- (i.e. the default dispatching policy) reorders the queue to be the
12306 -- same as just before the call.
12308 when ' ' =>
12309 return True;
12311 -- FIFO_Within_Priorities certainly certainly does not permit this
12312 -- optimization since the Rendezvous is a scheduling action that may
12313 -- require some other task to be run.
12315 when 'F' =>
12316 return False;
12318 -- For now, disallow the optimization for all other policies. This
12319 -- may be over-conservative, but it is certainly not incorrect.
12321 when others =>
12322 return False;
12324 end case;
12325 end Trivial_Accept_OK;
12327 end Exp_Ch9;