FSF GCC merge 02/23/03
[official-gcc.git] / gcc / ada / exp_ch9.adb
bloba456107c57e685c9aa4bbe339b19f2b068f30beb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 -- --
26 ------------------------------------------------------------------------------
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch11; use Exp_Ch11;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Dbug; use Exp_Dbug;
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 Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch11; use Sem_Ch11;
52 with Sem_Elab; use Sem_Elab;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Tbuild; use Tbuild;
59 with Types; use Types;
60 with Uintp; use Uintp;
61 with Opt;
63 package body Exp_Ch9 is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 function Actual_Index_Expression
70 (Sloc : Source_Ptr;
71 Ent : Entity_Id;
72 Index : Node_Id;
73 Tsk : Entity_Id)
74 return Node_Id;
75 -- Compute the index position for an entry call. Tsk is the target
76 -- task. If the bounds of some entry family depend on discriminants,
77 -- the expression computed by this function uses the discriminants
78 -- of the target task.
80 function Index_Constant_Declaration
81 (N : Node_Id;
82 Index_Id : Entity_Id;
83 Prot : Entity_Id)
84 return List_Id;
85 -- For an entry family and its barrier function, we define a local entity
86 -- that maps the index in the call into the entry index into the object:
88 -- I : constant Index_Type := Index_Type'Val (
89 -- E - <<index of first family member>> +
90 -- Protected_Entry_Index (Index_Type'Pos (Index_Type'First)));
92 procedure Add_Object_Pointer
93 (Decls : List_Id;
94 Pid : Entity_Id;
95 Loc : Source_Ptr);
96 -- Prepend an object pointer declaration to the declaration list
97 -- Decls. This object pointer is initialized to a type conversion
98 -- of the System.Address pointer passed to entry barrier functions
99 -- and entry body procedures.
101 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
102 -- Transform accept statement into a block with added exception handler.
103 -- Used both for simple accept statements and for accept alternatives in
104 -- select statements. Astat is the accept statement.
106 function Build_Barrier_Function
107 (N : Node_Id;
108 Ent : Entity_Id;
109 Pid : Node_Id)
110 return Node_Id;
111 -- Build the function body returning the value of the barrier expression
112 -- for the specified entry body.
114 function Build_Barrier_Function_Specification
115 (Def_Id : Entity_Id;
116 Loc : Source_Ptr)
117 return Node_Id;
118 -- Build a specification for a function implementing
119 -- the protected entry barrier of the specified entry body.
121 function Build_Corresponding_Record
122 (N : Node_Id;
123 Ctyp : Node_Id;
124 Loc : Source_Ptr)
125 return Node_Id;
126 -- Common to tasks and protected types. Copy discriminant specifications,
127 -- build record declaration. N is the type declaration, Ctyp is the
128 -- concurrent entity (task type or protected type).
130 function Build_Entry_Count_Expression
131 (Concurrent_Type : Node_Id;
132 Component_List : List_Id;
133 Loc : Source_Ptr)
134 return Node_Id;
135 -- Compute number of entries for concurrent object. This is a count of
136 -- simple entries, followed by an expression that computes the length
137 -- of the range of each entry family. A single array with that size is
138 -- allocated for each concurrent object of the type.
140 function Build_Find_Body_Index
141 (Typ : Entity_Id)
142 return Node_Id;
143 -- Build the function that translates the entry index in the call
144 -- (which depends on the size of entry families) into an index into the
145 -- Entry_Bodies_Array, to determine the body and barrier function used
146 -- in a protected entry call. A pointer to this function appears in every
147 -- protected object.
149 function Build_Find_Body_Index_Spec
150 (Typ : Entity_Id)
151 return Node_Id;
152 -- Build subprogram declaration for previous one.
154 function Build_Protected_Entry
155 (N : Node_Id;
156 Ent : Entity_Id;
157 Pid : Node_Id)
158 return Node_Id;
159 -- Build the procedure implementing the statement sequence of
160 -- the specified entry body.
162 function Build_Protected_Entry_Specification
163 (Def_Id : Entity_Id;
164 Ent_Id : Entity_Id;
165 Loc : Source_Ptr)
166 return Node_Id;
167 -- Build a specification for a procedure implementing
168 -- the statement sequence of the specified entry body.
169 -- Add attributes associating it with the entry defining identifier
170 -- Ent_Id.
172 function Build_Protected_Subprogram_Body
173 (N : Node_Id;
174 Pid : Node_Id;
175 N_Op_Spec : Node_Id)
176 return Node_Id;
177 -- This function is used to construct the protected version of a protected
178 -- subprogram. Its statement sequence first defers abortion, then locks
179 -- the associated protected object, and then enters a block that contains
180 -- a call to the unprotected version of the subprogram (for details, see
181 -- Build_Unprotected_Subprogram_Body). This block statement requires
182 -- a cleanup handler that unlocks the object in all cases.
183 -- (see Exp_Ch7.Expand_Cleanup_Actions).
185 function Build_Protected_Spec
186 (N : Node_Id;
187 Obj_Type : Entity_Id;
188 Unprotected : Boolean := False;
189 Ident : Entity_Id)
190 return List_Id;
191 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
192 -- Subprogram_Type. Builds signature of protected subprogram, adding the
193 -- formal that corresponds to the object itself. For an access to protected
194 -- subprogram, there is no object type to specify, so the additional
195 -- parameter has type Address and mode In. An indirect call through such
196 -- a pointer converts the address to a reference to the actual object.
197 -- The object is a limited record and therefore a by_reference type.
199 function Build_Selected_Name
200 (Prefix, Selector : Name_Id;
201 Append_Char : Character := ' ')
202 return Name_Id;
203 -- Build a name in the form of Prefix__Selector, with an optional
204 -- character appended. This is used for internal subprograms generated
205 -- for operations of protected types, including barrier functions. In
206 -- order to simplify the work of the debugger, the prefix includes the
207 -- characters PT.
209 procedure Build_Simple_Entry_Call
210 (N : Node_Id;
211 Concval : Node_Id;
212 Ename : Node_Id;
213 Index : Node_Id);
214 -- Some comments here would be useful ???
216 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
217 -- This routine constructs a specification for the procedure that we will
218 -- build for the task body for task type T. The spec has the form:
220 -- procedure tnameB (_Task : access tnameV);
222 -- where name is the character name taken from the task type entity that
223 -- is passed as the argument to the procedure, and tnameV is the task
224 -- value type that is associated with the task type.
226 function Build_Unprotected_Subprogram_Body
227 (N : Node_Id;
228 Pid : Node_Id)
229 return Node_Id;
230 -- This routine constructs the unprotected version of a protected
231 -- subprogram body, which is contains all of the code in the
232 -- original, unexpanded body. This is the version of the protected
233 -- subprogram that is called from all protected operations on the same
234 -- object, including the protected version of the same subprogram.
236 procedure Collect_Entry_Families
237 (Loc : Source_Ptr;
238 Cdecls : List_Id;
239 Current_Node : in out Node_Id;
240 Conctyp : Entity_Id);
241 -- For each entry family in a concurrent type, create an anonymous array
242 -- type of the right size, and add a component to the corresponding_record.
244 function Family_Offset
245 (Loc : Source_Ptr;
246 Hi : Node_Id;
247 Lo : Node_Id;
248 Ttyp : Entity_Id)
249 return Node_Id;
250 -- Compute (Hi - Lo) for two entry family indices. Hi is the index in
251 -- an accept statement, or the upper bound in the discrete subtype of
252 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
253 -- the concurrent type of the entry.
255 function Family_Size
256 (Loc : Source_Ptr;
257 Hi : Node_Id;
258 Lo : Node_Id;
259 Ttyp : Entity_Id)
260 return Node_Id;
261 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
262 -- a family, and handle properly the superflat case. This is equivalent
263 -- to the use of 'Length on the index type, but must use Family_Offset
264 -- to handle properly the case of bounds that depend on discriminants.
266 procedure Extract_Entry
267 (N : Node_Id;
268 Concval : out Node_Id;
269 Ename : out Node_Id;
270 Index : out Node_Id);
271 -- Given an entry call, returns the associated concurrent object,
272 -- the entry name, and the entry family index.
274 function Find_Task_Or_Protected_Pragma
275 (T : Node_Id;
276 P : Name_Id)
277 return Node_Id;
278 -- Searches the task or protected definition T for the first occurrence
279 -- of the pragma whose name is given by P. The caller has ensured that
280 -- the pragma is present in the task definition. A special case is that
281 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
282 -- ??? Should be implemented with the rep item chain mechanism.
284 procedure Update_Prival_Subtypes (N : Node_Id);
285 -- The actual subtypes of the privals will differ from the type of the
286 -- private declaration in the original protected type, if the protected
287 -- type has discriminants or if the prival has constrained components.
288 -- This is because the privals are generated out of sequence w.r.t. the
289 -- analysis of a protected body. After generating the bodies for protected
290 -- operations, we set correctly the type of all references to privals, by
291 -- means of a recursive tree traversal, which is heavy-handed but
292 -- correct.
294 -----------------------------
295 -- Actual_Index_Expression --
296 -----------------------------
298 function Actual_Index_Expression
299 (Sloc : Source_Ptr;
300 Ent : Entity_Id;
301 Index : Node_Id;
302 Tsk : Entity_Id)
303 return Node_Id
305 Expr : Node_Id;
306 Num : Node_Id;
307 Lo : Node_Id;
308 Hi : Node_Id;
309 Prev : Entity_Id;
310 S : Node_Id;
311 Ttyp : Entity_Id := Etype (Tsk);
313 --------------------------
314 -- Actual_Family_Offset --
315 --------------------------
317 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
318 -- Compute difference between bounds of entry family.
320 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
322 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
323 -- Replace a reference to a discriminant with a selected component
324 -- denoting the discriminant of the target task.
326 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
327 Typ : Entity_Id := Etype (Bound);
328 B : Node_Id;
330 begin
331 if not Is_Entity_Name (Bound)
332 or else Ekind (Entity (Bound)) /= E_Discriminant
333 then
334 if Nkind (Bound) = N_Attribute_Reference then
335 return Bound;
336 else
337 B := New_Copy_Tree (Bound);
338 end if;
340 else
341 B :=
342 Make_Selected_Component (Sloc,
343 Prefix => New_Copy_Tree (Tsk),
344 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
346 Analyze_And_Resolve (B, Typ);
347 end if;
349 return
350 Make_Attribute_Reference (Sloc,
351 Attribute_Name => Name_Pos,
352 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
353 Expressions => New_List (B));
354 end Actual_Discriminant_Ref;
356 begin
357 return
358 Make_Op_Subtract (Sloc,
359 Left_Opnd => Actual_Discriminant_Ref (Hi),
360 Right_Opnd => Actual_Discriminant_Ref (Lo));
361 end Actual_Family_Offset;
363 begin
364 -- The queues of entries and entry families appear in textual
365 -- order in the associated record. The entry index is computed as
366 -- the sum of the number of queues for all entries that precede the
367 -- designated one, to which is added the index expression, if this
368 -- expression denotes a member of a family.
370 -- The following is a place holder for the count of simple entries.
372 Num := Make_Integer_Literal (Sloc, 1);
374 -- We construct an expression which is a series of addition
375 -- operations. See comments in Entry_Index_Expression, which is
376 -- identical in structure.
378 if Present (Index) then
379 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
381 Expr :=
382 Make_Op_Add (Sloc,
383 Left_Opnd => Num,
385 Right_Opnd =>
386 Actual_Family_Offset (
387 Make_Attribute_Reference (Sloc,
388 Attribute_Name => Name_Pos,
389 Prefix => New_Reference_To (Base_Type (S), Sloc),
390 Expressions => New_List (Relocate_Node (Index))),
391 Type_Low_Bound (S)));
392 else
393 Expr := Num;
394 end if;
396 -- Now add lengths of preceding entries and entry families.
398 Prev := First_Entity (Ttyp);
400 while Chars (Prev) /= Chars (Ent)
401 or else (Ekind (Prev) /= Ekind (Ent))
402 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
403 loop
404 if Ekind (Prev) = E_Entry then
405 Set_Intval (Num, Intval (Num) + 1);
407 elsif Ekind (Prev) = E_Entry_Family then
408 S :=
409 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
410 Lo := Type_Low_Bound (S);
411 Hi := Type_High_Bound (S);
413 Expr :=
414 Make_Op_Add (Sloc,
415 Left_Opnd => Expr,
416 Right_Opnd =>
417 Make_Op_Add (Sloc,
418 Left_Opnd =>
419 Actual_Family_Offset (Hi, Lo),
420 Right_Opnd =>
421 Make_Integer_Literal (Sloc, 1)));
423 -- Other components are anonymous types to be ignored.
425 else
426 null;
427 end if;
429 Next_Entity (Prev);
430 end loop;
432 return Expr;
433 end Actual_Index_Expression;
435 ----------------------------------
436 -- Add_Discriminal_Declarations --
437 ----------------------------------
439 procedure Add_Discriminal_Declarations
440 (Decls : List_Id;
441 Typ : Entity_Id;
442 Name : Name_Id;
443 Loc : Source_Ptr)
445 D : Entity_Id;
447 begin
448 if Has_Discriminants (Typ) then
449 D := First_Discriminant (Typ);
451 while Present (D) loop
453 Prepend_To (Decls,
454 Make_Object_Renaming_Declaration (Loc,
455 Defining_Identifier => Discriminal (D),
456 Subtype_Mark => New_Reference_To (Etype (D), Loc),
457 Name =>
458 Make_Selected_Component (Loc,
459 Prefix => Make_Identifier (Loc, Name),
460 Selector_Name => Make_Identifier (Loc, Chars (D)))));
462 Next_Discriminant (D);
463 end loop;
464 end if;
465 end Add_Discriminal_Declarations;
467 ------------------------
468 -- Add_Object_Pointer --
469 ------------------------
471 procedure Add_Object_Pointer
472 (Decls : List_Id;
473 Pid : Entity_Id;
474 Loc : Source_Ptr)
476 Obj_Ptr : Node_Id;
478 begin
479 -- Prepend the declaration of _object. This must be first in the
480 -- declaration list, since it is used by the discriminal and
481 -- prival declarations.
482 -- ??? An attempt to make this a renaming was unsuccessful.
484 -- type poVP is access poV;
485 -- _object : poVP := poVP!O;
487 Obj_Ptr :=
488 Make_Defining_Identifier (Loc,
489 Chars =>
490 New_External_Name
491 (Chars (Corresponding_Record_Type (Pid)), 'P'));
493 Prepend_To (Decls,
494 Make_Object_Declaration (Loc,
495 Defining_Identifier =>
496 Make_Defining_Identifier (Loc, Name_uObject),
497 Object_Definition => New_Reference_To (Obj_Ptr, Loc),
498 Expression =>
499 Unchecked_Convert_To (Obj_Ptr,
500 Make_Identifier (Loc, Name_uO))));
502 Prepend_To (Decls,
503 Make_Full_Type_Declaration (Loc,
504 Defining_Identifier => Obj_Ptr,
505 Type_Definition => Make_Access_To_Object_Definition (Loc,
506 Subtype_Indication =>
507 New_Reference_To (Corresponding_Record_Type (Pid), Loc))));
509 end Add_Object_Pointer;
511 ------------------------------
512 -- Add_Private_Declarations --
513 ------------------------------
515 procedure Add_Private_Declarations
516 (Decls : List_Id;
517 Typ : Entity_Id;
518 Name : Name_Id;
519 Loc : Source_Ptr)
521 P : Node_Id;
522 Pdef : Entity_Id;
523 Def : Node_Id := Protected_Definition (Parent (Typ));
524 Body_Ent : constant Entity_Id := Corresponding_Body (Parent (Typ));
526 begin
527 pragma Assert (Nkind (Def) = N_Protected_Definition);
529 if Present (Private_Declarations (Def)) then
530 P := First (Private_Declarations (Def));
532 while Present (P) loop
533 if Nkind (P) = N_Component_Declaration then
534 Pdef := Defining_Identifier (P);
535 Prepend_To (Decls,
536 Make_Object_Renaming_Declaration (Loc,
537 Defining_Identifier => Prival (Pdef),
538 Subtype_Mark => New_Reference_To (Etype (Pdef), Loc),
539 Name =>
540 Make_Selected_Component (Loc,
541 Prefix => Make_Identifier (Loc, Name),
542 Selector_Name => Make_Identifier (Loc, Chars (Pdef)))));
543 end if;
544 Next (P);
545 end loop;
546 end if;
548 -- One more "prival" for the object itself, with the right protection
549 -- type.
551 declare
552 Protection_Type : RE_Id;
553 begin
554 if Has_Attach_Handler (Typ) then
555 if Restricted_Profile then
556 Protection_Type := RE_Protection_Entry;
557 else
558 Protection_Type := RE_Static_Interrupt_Protection;
559 end if;
561 elsif Has_Interrupt_Handler (Typ) then
562 Protection_Type := RE_Dynamic_Interrupt_Protection;
564 elsif Has_Entries (Typ) then
565 if Abort_Allowed
566 or else Restrictions (No_Entry_Queue) = False
567 or else Number_Entries (Typ) > 1
568 then
569 Protection_Type := RE_Protection_Entries;
570 else
571 Protection_Type := RE_Protection_Entry;
572 end if;
574 else
575 Protection_Type := RE_Protection;
576 end if;
578 Prepend_To (Decls,
579 Make_Object_Renaming_Declaration (Loc,
580 Defining_Identifier => Object_Ref (Body_Ent),
581 Subtype_Mark => New_Reference_To (RTE (Protection_Type), Loc),
582 Name =>
583 Make_Selected_Component (Loc,
584 Prefix => Make_Identifier (Loc, Name),
585 Selector_Name => Make_Identifier (Loc, Name_uObject))));
586 end;
588 end Add_Private_Declarations;
590 -----------------------
591 -- Build_Accept_Body --
592 -----------------------
594 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
595 Loc : constant Source_Ptr := Sloc (Astat);
596 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
597 New_S : Node_Id;
598 Hand : Node_Id;
599 Call : Node_Id;
600 Ohandle : Node_Id;
602 begin
603 -- At the end of the statement sequence, Complete_Rendezvous is called.
604 -- A label skipping the Complete_Rendezvous, and all other
605 -- accept processing, has already been added for the expansion
606 -- of requeue statements.
608 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
609 Insert_Before (Last (Statements (Stats)), Call);
610 Analyze (Call);
612 -- If exception handlers are present, then append Complete_Rendezvous
613 -- calls to the handlers, and construct the required outer block.
615 if Present (Exception_Handlers (Stats)) then
616 Hand := First (Exception_Handlers (Stats));
618 while Present (Hand) loop
619 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
620 Append (Call, Statements (Hand));
621 Analyze (Call);
622 Next (Hand);
623 end loop;
625 New_S :=
626 Make_Handled_Sequence_Of_Statements (Loc,
627 Statements => New_List (
628 Make_Block_Statement (Loc,
629 Handled_Statement_Sequence => Stats)));
631 else
632 New_S := Stats;
633 end if;
635 -- At this stage we know that the new statement sequence does not
636 -- have an exception handler part, so we supply one to call
637 -- Exceptional_Complete_Rendezvous. This handler is
639 -- when all others =>
640 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
642 -- We handle Abort_Signal to make sure that we properly catch the abort
643 -- case and wake up the caller.
645 Ohandle := Make_Others_Choice (Loc);
646 Set_All_Others (Ohandle);
648 Set_Exception_Handlers (New_S,
649 New_List (
650 Make_Exception_Handler (Loc,
651 Exception_Choices => New_List (Ohandle),
653 Statements => New_List (
654 Make_Procedure_Call_Statement (Loc,
655 Name => New_Reference_To (
656 RTE (RE_Exceptional_Complete_Rendezvous), Loc),
657 Parameter_Associations => New_List (
658 Make_Function_Call (Loc,
659 Name => New_Reference_To (
660 RTE (RE_Get_GNAT_Exception), Loc))))))));
662 Set_Parent (New_S, Astat); -- temp parent for Analyze call
663 Analyze_Exception_Handlers (Exception_Handlers (New_S));
664 Expand_Exception_Handlers (New_S);
666 -- Exceptional_Complete_Rendezvous must be called with abort
667 -- still deferred, which is the case for a "when all others" handler.
669 return New_S;
671 end Build_Accept_Body;
673 -----------------------------------
674 -- Build_Activation_Chain_Entity --
675 -----------------------------------
677 procedure Build_Activation_Chain_Entity (N : Node_Id) is
678 P : Node_Id;
679 B : Node_Id;
680 Decls : List_Id;
682 begin
683 -- Loop to find enclosing construct containing activation chain variable
685 P := Parent (N);
687 while Nkind (P) /= N_Subprogram_Body
688 and then Nkind (P) /= N_Package_Declaration
689 and then Nkind (P) /= N_Package_Body
690 and then Nkind (P) /= N_Block_Statement
691 and then Nkind (P) /= N_Task_Body
692 loop
693 P := Parent (P);
694 end loop;
696 -- If we are in a package body, the activation chain variable is
697 -- allocated in the corresponding spec. First, we save the package
698 -- body node because we enter the new entity in its Declarations list.
700 B := P;
702 if Nkind (P) = N_Package_Body then
703 P := Unit_Declaration_Node (Corresponding_Spec (P));
704 Decls := Declarations (B);
706 elsif Nkind (P) = N_Package_Declaration then
707 Decls := Visible_Declarations (Specification (B));
709 else
710 Decls := Declarations (B);
711 end if;
713 -- If activation chain entity not already declared, declare it
715 if No (Activation_Chain_Entity (P)) then
716 Set_Activation_Chain_Entity
717 (P, Make_Defining_Identifier (Sloc (N), Name_uChain));
719 Prepend_To (Decls,
720 Make_Object_Declaration (Sloc (P),
721 Defining_Identifier => Activation_Chain_Entity (P),
722 Aliased_Present => True,
723 Object_Definition =>
724 New_Reference_To (RTE (RE_Activation_Chain), Sloc (P))));
726 Analyze (First (Decls));
727 end if;
729 end Build_Activation_Chain_Entity;
731 ----------------------------
732 -- Build_Barrier_Function --
733 ----------------------------
735 function Build_Barrier_Function
736 (N : Node_Id;
737 Ent : Entity_Id;
738 Pid : Node_Id)
739 return Node_Id
741 Loc : constant Source_Ptr := Sloc (N);
742 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
743 Index_Spec : constant Node_Id := Entry_Index_Specification
744 (Ent_Formals);
745 Bdef : Entity_Id;
746 Bspec : Node_Id;
747 Op_Decls : List_Id := New_List;
749 begin
750 Bdef :=
751 Make_Defining_Identifier (Loc, Chars (Barrier_Function (Ent)));
752 Bspec := Build_Barrier_Function_Specification (Bdef, Loc);
754 -- <object pointer declaration>
755 -- <discriminant renamings>
756 -- <private object renamings>
757 -- Add discriminal and private renamings. These names have
758 -- already been used to expand references to discriminants
759 -- and private data.
761 Add_Discriminal_Declarations (Op_Decls, Pid, Name_uObject, Loc);
762 Add_Private_Declarations (Op_Decls, Pid, Name_uObject, Loc);
763 Add_Object_Pointer (Op_Decls, Pid, Loc);
765 -- If this is the barrier for an entry family, the entry index is
766 -- visible in the body of the barrier. Create a local variable that
767 -- converts the entry index (which is the last formal of the barrier
768 -- function) into the appropriate offset into the entry array. The
769 -- entry index constant must be set, as for the entry body, so that
770 -- local references to the entry index are correctly replaced with
771 -- the local variable. This parallels what is done for entry bodies.
773 if Present (Index_Spec) then
774 declare
775 Index_Id : constant Entity_Id := Defining_Identifier (Index_Spec);
776 Index_Con : constant Entity_Id :=
777 Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
779 begin
780 Set_Entry_Index_Constant (Index_Id, Index_Con);
781 Append_List_To (Op_Decls,
782 Index_Constant_Declaration (N, Index_Id, Pid));
783 end;
784 end if;
786 -- Note: the condition in the barrier function needs to be properly
787 -- processed for the C/Fortran boolean possibility, but this happens
788 -- automatically since the return statement does this normalization.
790 return
791 Make_Subprogram_Body (Loc,
792 Specification => Bspec,
793 Declarations => Op_Decls,
794 Handled_Statement_Sequence =>
795 Make_Handled_Sequence_Of_Statements (Loc,
796 Statements => New_List (
797 Make_Return_Statement (Loc,
798 Expression => Condition (Ent_Formals)))));
799 end Build_Barrier_Function;
801 ------------------------------------------
802 -- Build_Barrier_Function_Specification --
803 ------------------------------------------
805 function Build_Barrier_Function_Specification
806 (Def_Id : Entity_Id;
807 Loc : Source_Ptr)
808 return Node_Id
810 begin
811 return Make_Function_Specification (Loc,
812 Defining_Unit_Name => Def_Id,
813 Parameter_Specifications => New_List (
814 Make_Parameter_Specification (Loc,
815 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
816 Parameter_Type =>
817 New_Reference_To (RTE (RE_Address), Loc)),
819 Make_Parameter_Specification (Loc,
820 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
821 Parameter_Type =>
822 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
824 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc));
825 end Build_Barrier_Function_Specification;
827 --------------------------
828 -- Build_Call_With_Task --
829 --------------------------
831 function Build_Call_With_Task
832 (N : Node_Id;
833 E : Entity_Id)
834 return Node_Id
836 Loc : constant Source_Ptr := Sloc (N);
838 begin
839 return
840 Make_Function_Call (Loc,
841 Name => New_Reference_To (E, Loc),
842 Parameter_Associations => New_List (Concurrent_Ref (N)));
843 end Build_Call_With_Task;
845 --------------------------------
846 -- Build_Corresponding_Record --
847 --------------------------------
849 function Build_Corresponding_Record
850 (N : Node_Id;
851 Ctyp : Entity_Id;
852 Loc : Source_Ptr)
853 return Node_Id
855 Rec_Ent : constant Entity_Id :=
856 Make_Defining_Identifier
857 (Loc, New_External_Name (Chars (Ctyp), 'V'));
858 Disc : Entity_Id;
859 Dlist : List_Id;
860 New_Disc : Entity_Id;
861 Cdecls : List_Id;
863 begin
864 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
865 Set_Ekind (Rec_Ent, E_Record_Type);
866 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
867 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
868 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
869 Set_Girder_Constraint (Rec_Ent, No_Elist);
870 Cdecls := New_List;
872 -- Use discriminals to create list of discriminants for record, and
873 -- create new discriminals for use in default expressions, etc. It is
874 -- worth noting that a task discriminant gives rise to 5 entities;
876 -- a) The original discriminant.
877 -- b) The discriminal for use in the task.
878 -- c) The discriminant of the corresponding record.
879 -- d) The discriminal for the init_proc of the corresponding record.
880 -- e) The local variable that renames the discriminant in the procedure
881 -- for the task body.
883 -- In fact the discriminals b) are used in the renaming declarations
884 -- for e). See details in einfo (Handling of Discriminants).
886 if Present (Discriminant_Specifications (N)) then
887 Dlist := New_List;
888 Disc := First_Discriminant (Ctyp);
890 while Present (Disc) loop
891 New_Disc := CR_Discriminant (Disc);
893 Append_To (Dlist,
894 Make_Discriminant_Specification (Loc,
895 Defining_Identifier => New_Disc,
896 Discriminant_Type =>
897 New_Occurrence_Of (Etype (Disc), Loc),
898 Expression =>
899 New_Copy (Discriminant_Default_Value (Disc))));
901 Next_Discriminant (Disc);
902 end loop;
904 else
905 Dlist := No_List;
906 end if;
908 -- Now we can construct the record type declaration. Note that this
909 -- record is limited, reflecting the underlying limitedness of the
910 -- task or protected object that it represents, and ensuring for
911 -- example that it is properly passed by reference.
913 return
914 Make_Full_Type_Declaration (Loc,
915 Defining_Identifier => Rec_Ent,
916 Discriminant_Specifications => Dlist,
917 Type_Definition =>
918 Make_Record_Definition (Loc,
919 Component_List =>
920 Make_Component_List (Loc,
921 Component_Items => Cdecls),
922 Limited_Present => True));
923 end Build_Corresponding_Record;
925 ----------------------------------
926 -- Build_Entry_Count_Expression --
927 ----------------------------------
929 function Build_Entry_Count_Expression
930 (Concurrent_Type : Node_Id;
931 Component_List : List_Id;
932 Loc : Source_Ptr)
933 return Node_Id
935 Eindx : Nat;
936 Ent : Entity_Id;
937 Ecount : Node_Id;
938 Comp : Node_Id;
939 Lo : Node_Id;
940 Hi : Node_Id;
941 Typ : Entity_Id;
943 begin
944 Ent := First_Entity (Concurrent_Type);
945 Eindx := 0;
947 -- Count number of non-family entries
949 while Present (Ent) loop
950 if Ekind (Ent) = E_Entry then
951 Eindx := Eindx + 1;
952 end if;
954 Next_Entity (Ent);
955 end loop;
957 Ecount := Make_Integer_Literal (Loc, Eindx);
959 -- Loop through entry families building the addition nodes
961 Ent := First_Entity (Concurrent_Type);
962 Comp := First (Component_List);
964 while Present (Ent) loop
965 if Ekind (Ent) = E_Entry_Family then
966 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
967 Next (Comp);
968 end loop;
970 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
971 Hi := Type_High_Bound (Typ);
972 Lo := Type_Low_Bound (Typ);
974 Ecount :=
975 Make_Op_Add (Loc,
976 Left_Opnd => Ecount,
977 Right_Opnd => Family_Size (Loc, Hi, Lo, Concurrent_Type));
978 end if;
980 Next_Entity (Ent);
981 end loop;
983 return Ecount;
984 end Build_Entry_Count_Expression;
986 ---------------------------
987 -- Build_Find_Body_Index --
988 ---------------------------
990 function Build_Find_Body_Index
991 (Typ : Entity_Id)
992 return Node_Id
994 Loc : constant Source_Ptr := Sloc (Typ);
995 Ent : Entity_Id;
996 E_Typ : Entity_Id;
997 Has_F : Boolean := False;
998 Index : Nat;
999 If_St : Node_Id := Empty;
1000 Lo : Node_Id;
1001 Hi : Node_Id;
1002 Decls : List_Id := New_List;
1003 Ret : Node_Id;
1004 Spec : Node_Id;
1005 Siz : Node_Id := Empty;
1007 procedure Add_If_Clause (Expr : Node_Id);
1008 -- Add test for range of current entry.
1010 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
1011 -- If a bound of an entry is given by a discriminant, retrieve the
1012 -- actual value of the discriminant from the enclosing object.
1014 -------------------
1015 -- Add_If_Clause --
1016 -------------------
1018 procedure Add_If_Clause (Expr : Node_Id) is
1019 Cond : Node_Id;
1020 Stats : constant List_Id :=
1021 New_List (
1022 Make_Return_Statement (Loc,
1023 Expression => Make_Integer_Literal (Loc, Index + 1)));
1025 begin
1026 -- Index for current entry body.
1028 Index := Index + 1;
1030 -- Compute total length of entry queues so far.
1032 if No (Siz) then
1033 Siz := Expr;
1034 else
1035 Siz :=
1036 Make_Op_Add (Loc,
1037 Left_Opnd => Siz,
1038 Right_Opnd => Expr);
1039 end if;
1041 Cond :=
1042 Make_Op_Le (Loc,
1043 Left_Opnd => Make_Identifier (Loc, Name_uE),
1044 Right_Opnd => Siz);
1046 -- Map entry queue indices in the range of the current family
1047 -- into the current index, that designates the entry body.
1049 if No (If_St) then
1050 If_St :=
1051 Make_Implicit_If_Statement (Typ,
1052 Condition => Cond,
1053 Then_Statements => Stats,
1054 Elsif_Parts => New_List);
1056 Ret := If_St;
1058 else
1059 Append (
1060 Make_Elsif_Part (Loc,
1061 Condition => Cond,
1062 Then_Statements => Stats),
1063 Elsif_Parts (If_St));
1064 end if;
1066 end Add_If_Clause;
1068 ------------------------------
1069 -- Convert_Discriminant_Ref --
1070 ------------------------------
1072 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
1073 B : Node_Id;
1075 begin
1076 if Is_Entity_Name (Bound)
1077 and then Ekind (Entity (Bound)) = E_Discriminant
1078 then
1079 B :=
1080 Make_Selected_Component (Loc,
1081 Prefix =>
1082 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
1083 Make_Explicit_Dereference (Loc,
1084 Make_Identifier (Loc, Name_uObject))),
1085 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
1086 Set_Etype (B, Etype (Entity (Bound)));
1087 else
1088 B := New_Copy_Tree (Bound);
1089 end if;
1091 return B;
1092 end Convert_Discriminant_Ref;
1094 -- Start of processing for Build_Find_Body_Index
1096 begin
1097 Spec := Build_Find_Body_Index_Spec (Typ);
1099 Ent := First_Entity (Typ);
1101 while Present (Ent) loop
1103 if Ekind (Ent) = E_Entry_Family then
1104 Has_F := True;
1105 exit;
1106 end if;
1108 Next_Entity (Ent);
1109 end loop;
1111 if not Has_F then
1113 -- If the protected type has no entry families, there is a one-one
1114 -- correspondence between entry queue and entry body.
1116 Ret :=
1117 Make_Return_Statement (Loc,
1118 Expression => Make_Identifier (Loc, Name_uE));
1120 else
1121 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
1122 -- the following:
1124 -- if E <= l1 then return 1;
1125 -- elsif E <= l1 + l2 then return 2;
1126 -- ...
1128 Index := 0;
1129 Siz := Empty;
1130 Ent := First_Entity (Typ);
1132 Add_Object_Pointer (Decls, Typ, Loc);
1134 while Present (Ent) loop
1136 if Ekind (Ent) = E_Entry then
1137 Add_If_Clause (Make_Integer_Literal (Loc, 1));
1139 elsif Ekind (Ent) = E_Entry_Family then
1141 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1142 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
1143 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
1144 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ));
1145 end if;
1147 Next_Entity (Ent);
1148 end loop;
1150 if Index = 1 then
1151 Decls := New_List;
1152 Ret :=
1153 Make_Return_Statement (Loc,
1154 Expression => Make_Integer_Literal (Loc, 1));
1156 elsif Nkind (Ret) = N_If_Statement then
1158 -- Ranges are in increasing order, so last one doesn't need a
1159 -- guard.
1161 declare
1162 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
1164 begin
1165 Remove (Nod);
1166 Set_Else_Statements (Ret, Then_Statements (Nod));
1167 end;
1168 end if;
1169 end if;
1171 return
1172 Make_Subprogram_Body (Loc,
1173 Specification => Spec,
1174 Declarations => Decls,
1175 Handled_Statement_Sequence =>
1176 Make_Handled_Sequence_Of_Statements (Loc,
1177 Statements => New_List (Ret)));
1179 end Build_Find_Body_Index;
1181 --------------------------------
1182 -- Build_Find_Body_Index_Spec --
1183 --------------------------------
1185 function Build_Find_Body_Index_Spec
1186 (Typ : Entity_Id)
1187 return Node_Id
1189 Loc : constant Source_Ptr := Sloc (Typ);
1190 Id : constant Entity_Id :=
1191 Make_Defining_Identifier (Loc,
1192 Chars => New_External_Name (Chars (Typ), 'F'));
1193 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
1194 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
1196 begin
1197 return
1198 Make_Function_Specification (Loc,
1199 Defining_Unit_Name => Id,
1200 Parameter_Specifications => New_List (
1201 Make_Parameter_Specification (Loc,
1202 Defining_Identifier => Parm1,
1203 Parameter_Type =>
1204 New_Reference_To (RTE (RE_Address), Loc)),
1206 Make_Parameter_Specification (Loc,
1207 Defining_Identifier => Parm2,
1208 Parameter_Type =>
1209 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1210 Subtype_Mark => New_Occurrence_Of (
1211 RTE (RE_Protected_Entry_Index), Loc));
1213 end Build_Find_Body_Index_Spec;
1215 -------------------------
1216 -- Build_Master_Entity --
1217 -------------------------
1219 procedure Build_Master_Entity (E : Entity_Id) is
1220 Loc : constant Source_Ptr := Sloc (E);
1221 P : Node_Id;
1222 Decl : Node_Id;
1224 begin
1225 -- Nothing to do if we already built a master entity for this scope
1226 -- or if there is no task hierarchy.
1228 if Has_Master_Entity (Scope (E))
1229 or else Restrictions (No_Task_Hierarchy)
1230 then
1231 return;
1232 end if;
1234 -- Otherwise first build the master entity
1235 -- _Master : constant Master_Id := Current_Master.all;
1236 -- and insert it just before the current declaration
1238 Decl :=
1239 Make_Object_Declaration (Loc,
1240 Defining_Identifier =>
1241 Make_Defining_Identifier (Loc, Name_uMaster),
1242 Constant_Present => True,
1243 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
1244 Expression =>
1245 Make_Explicit_Dereference (Loc,
1246 New_Reference_To (RTE (RE_Current_Master), Loc)));
1248 P := Parent (E);
1249 Insert_Before (P, Decl);
1250 Analyze (Decl);
1251 Set_Has_Master_Entity (Scope (E));
1253 -- Now mark the containing scope as a task master
1255 while Nkind (P) /= N_Compilation_Unit loop
1256 P := Parent (P);
1258 -- If we fall off the top, we are at the outer level, and the
1259 -- environment task is our effective master, so nothing to mark.
1261 if Nkind (P) = N_Task_Body
1262 or else Nkind (P) = N_Block_Statement
1263 or else Nkind (P) = N_Subprogram_Body
1264 then
1265 Set_Is_Task_Master (P, True);
1266 return;
1268 elsif Nkind (Parent (P)) = N_Subunit then
1269 P := Corresponding_Stub (Parent (P));
1270 end if;
1271 end loop;
1272 end Build_Master_Entity;
1274 ---------------------------
1275 -- Build_Protected_Entry --
1276 ---------------------------
1278 function Build_Protected_Entry
1279 (N : Node_Id;
1280 Ent : Entity_Id;
1281 Pid : Node_Id)
1282 return Node_Id
1284 Loc : constant Source_Ptr := Sloc (N);
1285 Edef : Entity_Id;
1286 Espec : Node_Id;
1287 Op_Decls : List_Id := New_List;
1288 Op_Stats : List_Id;
1289 Ohandle : Node_Id;
1290 Complete : Node_Id;
1292 begin
1293 Edef :=
1294 Make_Defining_Identifier (Loc,
1295 Chars => Chars (Protected_Body_Subprogram (Ent)));
1296 Espec := Build_Protected_Entry_Specification (Edef, Empty, Loc);
1298 -- <object pointer declaration>
1299 -- Add object pointer declaration. This is needed by the
1300 -- discriminal and prival renamings, which should already
1301 -- have been inserted into the declaration list.
1303 Add_Object_Pointer (Op_Decls, Pid, Loc);
1305 if Abort_Allowed
1306 or else Restrictions (No_Entry_Queue) = False
1307 or else Number_Entries (Pid) > 1
1308 then
1309 Complete := New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
1310 else
1311 Complete :=
1312 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
1313 end if;
1315 Op_Stats := New_List (
1316 Make_Block_Statement (Loc,
1317 Declarations => Declarations (N),
1318 Handled_Statement_Sequence =>
1319 Handled_Statement_Sequence (N)),
1321 Make_Procedure_Call_Statement (Loc,
1322 Name => Complete,
1323 Parameter_Associations => New_List (
1324 Make_Attribute_Reference (Loc,
1325 Prefix =>
1326 Make_Selected_Component (Loc,
1327 Prefix =>
1328 Make_Identifier (Loc, Name_uObject),
1330 Selector_Name =>
1331 Make_Identifier (Loc, Name_uObject)),
1332 Attribute_Name => Name_Unchecked_Access))));
1334 if Restrictions (No_Exception_Handlers) then
1335 return
1336 Make_Subprogram_Body (Loc,
1337 Specification => Espec,
1338 Declarations => Op_Decls,
1339 Handled_Statement_Sequence =>
1340 Make_Handled_Sequence_Of_Statements (Loc, Op_Stats));
1342 else
1343 Ohandle := Make_Others_Choice (Loc);
1344 Set_All_Others (Ohandle);
1346 if Abort_Allowed
1347 or else Restrictions (No_Entry_Queue) = False
1348 or else Number_Entries (Pid) > 1
1349 then
1350 Complete :=
1351 New_Reference_To (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
1353 else
1354 Complete := New_Reference_To (
1355 RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
1356 end if;
1358 return
1359 Make_Subprogram_Body (Loc,
1360 Specification => Espec,
1361 Declarations => Op_Decls,
1362 Handled_Statement_Sequence =>
1363 Make_Handled_Sequence_Of_Statements (Loc,
1364 Statements => Op_Stats,
1365 Exception_Handlers => New_List (
1366 Make_Exception_Handler (Loc,
1367 Exception_Choices => New_List (Ohandle),
1369 Statements => New_List (
1370 Make_Procedure_Call_Statement (Loc,
1371 Name => Complete,
1372 Parameter_Associations => New_List (
1373 Make_Attribute_Reference (Loc,
1374 Prefix =>
1375 Make_Selected_Component (Loc,
1376 Prefix =>
1377 Make_Identifier (Loc, Name_uObject),
1378 Selector_Name =>
1379 Make_Identifier (Loc, Name_uObject)),
1380 Attribute_Name => Name_Unchecked_Access),
1382 Make_Function_Call (Loc,
1383 Name => New_Reference_To (
1384 RTE (RE_Get_GNAT_Exception), Loc)))))))));
1385 end if;
1386 end Build_Protected_Entry;
1388 -----------------------------------------
1389 -- Build_Protected_Entry_Specification --
1390 -----------------------------------------
1392 function Build_Protected_Entry_Specification
1393 (Def_Id : Entity_Id;
1394 Ent_Id : Entity_Id;
1395 Loc : Source_Ptr)
1396 return Node_Id
1398 P : Entity_Id;
1400 begin
1401 P := Make_Defining_Identifier (Loc, Name_uP);
1403 if Present (Ent_Id) then
1404 Append_Elmt (P, Accept_Address (Ent_Id));
1405 end if;
1407 return Make_Procedure_Specification (Loc,
1408 Defining_Unit_Name => Def_Id,
1409 Parameter_Specifications => New_List (
1410 Make_Parameter_Specification (Loc,
1411 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
1412 Parameter_Type =>
1413 New_Reference_To (RTE (RE_Address), Loc)),
1415 Make_Parameter_Specification (Loc,
1416 Defining_Identifier => P,
1417 Parameter_Type =>
1418 New_Reference_To (RTE (RE_Address), Loc)),
1420 Make_Parameter_Specification (Loc,
1421 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
1422 Parameter_Type =>
1423 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
1424 end Build_Protected_Entry_Specification;
1426 --------------------------
1427 -- Build_Protected_Spec --
1428 --------------------------
1430 function Build_Protected_Spec
1431 (N : Node_Id;
1432 Obj_Type : Entity_Id;
1433 Unprotected : Boolean := False;
1434 Ident : Entity_Id)
1435 return List_Id
1437 Loc : constant Source_Ptr := Sloc (N);
1438 Formal : Entity_Id;
1439 New_Plist : List_Id;
1440 New_Param : Node_Id;
1442 begin
1443 New_Plist := New_List;
1444 Formal := First_Formal (Ident);
1446 while Present (Formal) loop
1447 New_Param :=
1448 Make_Parameter_Specification (Loc,
1449 Defining_Identifier =>
1450 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
1451 In_Present => In_Present (Parent (Formal)),
1452 Out_Present => Out_Present (Parent (Formal)),
1453 Parameter_Type =>
1454 New_Reference_To (Etype (Formal), Loc));
1456 if Unprotected then
1457 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
1458 end if;
1460 Append (New_Param, New_Plist);
1461 Next_Formal (Formal);
1462 end loop;
1464 -- If the subprogram is a procedure and the context is not an access
1465 -- to protected subprogram, the parameter is in-out. Otherwise it is
1466 -- an in parameter.
1468 Prepend_To (New_Plist,
1469 Make_Parameter_Specification (Loc,
1470 Defining_Identifier =>
1471 Make_Defining_Identifier (Loc, Name_uObject),
1472 In_Present => True,
1473 Out_Present =>
1474 (Etype (Ident) = Standard_Void_Type
1475 and then not Is_RTE (Obj_Type, RE_Address)),
1476 Parameter_Type => New_Reference_To (Obj_Type, Loc)));
1478 return New_Plist;
1479 end Build_Protected_Spec;
1481 ---------------------------------------
1482 -- Build_Protected_Sub_Specification --
1483 ---------------------------------------
1485 function Build_Protected_Sub_Specification
1486 (N : Node_Id;
1487 Prottyp : Entity_Id;
1488 Unprotected : Boolean := False)
1489 return Node_Id
1491 Loc : constant Source_Ptr := Sloc (N);
1492 Decl : Node_Id;
1493 Protnm : constant Name_Id := Chars (Prottyp);
1494 Ident : Entity_Id;
1495 Nam : Name_Id;
1496 New_Plist : List_Id;
1497 Append_Char : Character;
1498 New_Spec : Node_Id;
1500 begin
1501 if Ekind
1502 (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
1503 then
1504 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
1505 else
1506 Decl := N;
1507 end if;
1509 Ident := Defining_Unit_Name (Specification (Decl));
1510 Nam := Chars (Ident);
1512 New_Plist := Build_Protected_Spec
1513 (Decl, Corresponding_Record_Type (Prottyp),
1514 Unprotected, Ident);
1516 if Unprotected then
1517 Append_Char := 'N';
1518 else
1519 Append_Char := 'P';
1520 end if;
1522 if Nkind (Specification (Decl)) = N_Procedure_Specification then
1523 return
1524 Make_Procedure_Specification (Loc,
1525 Defining_Unit_Name =>
1526 Make_Defining_Identifier (Loc,
1527 Chars => Build_Selected_Name (Protnm, Nam, Append_Char)),
1528 Parameter_Specifications => New_Plist);
1530 else
1531 New_Spec :=
1532 Make_Function_Specification (Loc,
1533 Defining_Unit_Name =>
1534 Make_Defining_Identifier (Loc,
1535 Chars => Build_Selected_Name (Protnm, Nam, Append_Char)),
1536 Parameter_Specifications => New_Plist,
1537 Subtype_Mark => New_Copy (Subtype_Mark (Specification (Decl))));
1538 Set_Return_Present (Defining_Unit_Name (New_Spec));
1539 return New_Spec;
1540 end if;
1541 end Build_Protected_Sub_Specification;
1543 -------------------------------------
1544 -- Build_Protected_Subprogram_Body --
1545 -------------------------------------
1547 function Build_Protected_Subprogram_Body
1548 (N : Node_Id;
1549 Pid : Node_Id;
1550 N_Op_Spec : Node_Id)
1551 return Node_Id
1553 Loc : constant Source_Ptr := Sloc (N);
1554 Op_Spec : Node_Id;
1555 Op_Def : Entity_Id;
1556 Sub_Name : Name_Id;
1557 P_Op_Spec : Node_Id;
1558 Uactuals : List_Id;
1559 Pformal : Node_Id;
1560 Unprot_Call : Node_Id;
1561 Sub_Body : Node_Id;
1562 Lock_Name : Node_Id;
1563 Lock_Stmt : Node_Id;
1564 Unlock_Name : Node_Id;
1565 Unlock_Stmt : Node_Id;
1566 Service_Name : Node_Id;
1567 Service_Stmt : Node_Id;
1568 R : Node_Id;
1569 Return_Stmt : Node_Id := Empty;
1570 Pre_Stmts : List_Id := No_List;
1571 -- Initializations to avoid spurious warnings from GCC3.
1572 Stmts : List_Id;
1573 Object_Parm : Node_Id;
1574 Exc_Safe : Boolean;
1576 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
1577 -- Tell whether a given subprogram cannot raise an exception
1579 -----------------------
1580 -- Is_Exception_Safe --
1581 -----------------------
1583 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
1585 function Has_Side_Effect (N : Node_Id) return Boolean;
1586 -- Return True whenever encountering a subprogram call or a
1587 -- raise statement of any kind in the sequence of statements N
1589 ---------------------
1590 -- Has_Side_Effect --
1591 ---------------------
1593 -- What is this doing buried two levels down in exp_ch9. It
1594 -- seems like a generally useful function, and indeed there
1595 -- may be code duplication going on here ???
1597 function Has_Side_Effect (N : Node_Id) return Boolean is
1598 Stmt : Node_Id := N;
1599 Expr : Node_Id;
1601 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
1602 -- Indicate whether N is a subprogram call or a raise statement
1604 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
1605 begin
1606 return Nkind (N) = N_Procedure_Call_Statement
1607 or else Nkind (N) = N_Function_Call
1608 or else Nkind (N) = N_Raise_Statement
1609 or else Nkind (N) = N_Raise_Constraint_Error
1610 or else Nkind (N) = N_Raise_Program_Error
1611 or else Nkind (N) = N_Raise_Storage_Error;
1612 end Is_Call_Or_Raise;
1614 -- Start of processing for Has_Side_Effect
1616 begin
1617 while Present (Stmt) loop
1618 if Is_Call_Or_Raise (Stmt) then
1619 return True;
1620 end if;
1622 -- An object declaration can also contain a function call
1623 -- or a raise statement
1625 if Nkind (Stmt) = N_Object_Declaration then
1626 Expr := Expression (Stmt);
1628 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
1629 return True;
1630 end if;
1631 end if;
1633 Next (Stmt);
1634 end loop;
1636 return False;
1637 end Has_Side_Effect;
1639 -- Start of processing for Is_Exception_Safe
1641 begin
1642 -- If the checks handled by the back end are not disabled, we cannot
1643 -- ensure that no exception will be raised.
1645 if not Access_Checks_Suppressed (Empty)
1646 or else not Discriminant_Checks_Suppressed (Empty)
1647 or else not Range_Checks_Suppressed (Empty)
1648 or else not Index_Checks_Suppressed (Empty)
1649 or else Opt.Stack_Checking_Enabled
1650 then
1651 return False;
1652 end if;
1654 if Has_Side_Effect (First (Declarations (Subprogram)))
1655 or else
1656 Has_Side_Effect (
1657 First (Statements (Handled_Statement_Sequence (Subprogram))))
1658 then
1659 return False;
1660 else
1661 return True;
1662 end if;
1663 end Is_Exception_Safe;
1665 -- Start of processing for Build_Protected_Subprogram_Body
1667 begin
1668 Op_Spec := Specification (N);
1669 Op_Def := Defining_Unit_Name (Op_Spec);
1670 Exc_Safe := Is_Exception_Safe (N);
1672 Sub_Name := Chars (Defining_Unit_Name (Specification (N)));
1674 P_Op_Spec :=
1675 Build_Protected_Sub_Specification (N,
1676 Pid, Unprotected => False);
1678 -- Build a list of the formal parameters of the protected
1679 -- version of the subprogram to use as the actual parameters
1680 -- of the unprotected version.
1682 Uactuals := New_List;
1683 Pformal := First (Parameter_Specifications (P_Op_Spec));
1685 while Present (Pformal) loop
1686 Append (
1687 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))),
1688 Uactuals);
1689 Next (Pformal);
1690 end loop;
1692 -- Make a call to the unprotected version of the subprogram
1693 -- built above for use by the protected version built below.
1695 if Nkind (Op_Spec) = N_Function_Specification then
1696 if Exc_Safe then
1697 R := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
1698 Unprot_Call :=
1699 Make_Object_Declaration (Loc,
1700 Defining_Identifier => R,
1701 Constant_Present => True,
1702 Object_Definition => New_Copy (Subtype_Mark (N_Op_Spec)),
1703 Expression =>
1704 Make_Function_Call (Loc,
1705 Name => Make_Identifier (Loc,
1706 Chars (Defining_Unit_Name (N_Op_Spec))),
1707 Parameter_Associations => Uactuals));
1708 Return_Stmt := Make_Return_Statement (Loc,
1709 Expression => New_Reference_To (R, Loc));
1711 else
1712 Unprot_Call := Make_Return_Statement (Loc,
1713 Expression => Make_Function_Call (Loc,
1714 Name =>
1715 Make_Identifier (Loc,
1716 Chars (Defining_Unit_Name (N_Op_Spec))),
1717 Parameter_Associations => Uactuals));
1718 end if;
1720 else
1721 Unprot_Call := Make_Procedure_Call_Statement (Loc,
1722 Name =>
1723 Make_Identifier (Loc,
1724 Chars (Defining_Unit_Name (N_Op_Spec))),
1725 Parameter_Associations => Uactuals);
1726 end if;
1728 -- Wrap call in block that will be covered by an at_end handler.
1730 if not Exc_Safe then
1731 Unprot_Call := Make_Block_Statement (Loc,
1732 Handled_Statement_Sequence =>
1733 Make_Handled_Sequence_Of_Statements (Loc,
1734 Statements => New_List (Unprot_Call)));
1735 end if;
1737 -- Make the protected subprogram body. This locks the protected
1738 -- object and calls the unprotected version of the subprogram.
1740 -- If the protected object is controlled (i.e it has entries or
1741 -- needs finalization for interrupt handling), call Lock_Entries,
1742 -- except if the protected object follows the Ravenscar profile, in
1743 -- which case call Lock_Entry, otherwise call the simplified version,
1744 -- Lock.
1746 if Has_Entries (Pid)
1747 or else Has_Interrupt_Handler (Pid)
1748 or else Has_Attach_Handler (Pid)
1749 then
1750 if Abort_Allowed
1751 or else Restrictions (No_Entry_Queue) = False
1752 or else Number_Entries (Pid) > 1
1753 then
1754 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
1755 Unlock_Name := New_Reference_To (RTE (RE_Unlock_Entries), Loc);
1756 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
1758 else
1759 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
1760 Unlock_Name := New_Reference_To (RTE (RE_Unlock_Entry), Loc);
1761 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
1762 end if;
1764 else
1765 Lock_Name := New_Reference_To (RTE (RE_Lock), Loc);
1766 Unlock_Name := New_Reference_To (RTE (RE_Unlock), Loc);
1767 Service_Name := Empty;
1768 end if;
1770 Object_Parm :=
1771 Make_Attribute_Reference (Loc,
1772 Prefix =>
1773 Make_Selected_Component (Loc,
1774 Prefix =>
1775 Make_Identifier (Loc, Name_uObject),
1776 Selector_Name =>
1777 Make_Identifier (Loc, Name_uObject)),
1778 Attribute_Name => Name_Unchecked_Access);
1780 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
1781 Name => Lock_Name,
1782 Parameter_Associations => New_List (Object_Parm));
1784 if Abort_Allowed then
1785 Stmts := New_List (
1786 Make_Procedure_Call_Statement (Loc,
1787 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
1788 Parameter_Associations => Empty_List),
1789 Lock_Stmt);
1791 else
1792 Stmts := New_List (Lock_Stmt);
1793 end if;
1795 if not Exc_Safe then
1796 Append (Unprot_Call, Stmts);
1797 else
1798 if Nkind (Op_Spec) = N_Function_Specification then
1799 Pre_Stmts := Stmts;
1800 Stmts := Empty_List;
1801 else
1802 Append (Unprot_Call, Stmts);
1803 end if;
1805 if Service_Name /= Empty then
1806 Service_Stmt := Make_Procedure_Call_Statement (Loc,
1807 Name => Service_Name,
1808 Parameter_Associations =>
1809 New_List (New_Copy_Tree (Object_Parm)));
1810 Append (Service_Stmt, Stmts);
1811 end if;
1813 Unlock_Stmt :=
1814 Make_Procedure_Call_Statement (Loc,
1815 Name => Unlock_Name,
1816 Parameter_Associations => New_List (
1817 New_Copy_Tree (Object_Parm)));
1818 Append (Unlock_Stmt, Stmts);
1820 if Abort_Allowed then
1821 Append (
1822 Make_Procedure_Call_Statement (Loc,
1823 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
1824 Parameter_Associations => Empty_List),
1825 Stmts);
1826 end if;
1828 if Nkind (Op_Spec) = N_Function_Specification then
1829 Append (Return_Stmt, Stmts);
1830 Append (Make_Block_Statement (Loc,
1831 Declarations => New_List (Unprot_Call),
1832 Handled_Statement_Sequence =>
1833 Make_Handled_Sequence_Of_Statements (Loc,
1834 Statements => Stmts)), Pre_Stmts);
1835 Stmts := Pre_Stmts;
1836 end if;
1837 end if;
1839 Sub_Body :=
1840 Make_Subprogram_Body (Loc,
1841 Declarations => Empty_List,
1842 Specification => P_Op_Spec,
1843 Handled_Statement_Sequence =>
1844 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
1846 if not Exc_Safe then
1847 Set_Is_Protected_Subprogram_Body (Sub_Body);
1848 end if;
1850 return Sub_Body;
1851 end Build_Protected_Subprogram_Body;
1853 -------------------------------------
1854 -- Build_Protected_Subprogram_Call --
1855 -------------------------------------
1857 procedure Build_Protected_Subprogram_Call
1858 (N : Node_Id;
1859 Name : Node_Id;
1860 Rec : Node_Id;
1861 External : Boolean := True)
1863 Loc : constant Source_Ptr := Sloc (N);
1864 Sub : Entity_Id := Entity (Name);
1865 New_Sub : Node_Id;
1866 Params : List_Id;
1868 begin
1869 if External then
1870 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
1871 else
1872 New_Sub :=
1873 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
1874 end if;
1876 if Present (Parameter_Associations (N)) then
1877 Params := New_Copy_List_Tree (Parameter_Associations (N));
1878 else
1879 Params := New_List;
1880 end if;
1882 Prepend (Rec, Params);
1884 if Ekind (Sub) = E_Procedure then
1885 Rewrite (N,
1886 Make_Procedure_Call_Statement (Loc,
1887 Name => New_Sub,
1888 Parameter_Associations => Params));
1890 else
1891 pragma Assert (Ekind (Sub) = E_Function);
1892 Rewrite (N,
1893 Make_Function_Call (Loc,
1894 Name => New_Sub,
1895 Parameter_Associations => Params));
1896 end if;
1898 if External
1899 and then Nkind (Rec) = N_Unchecked_Type_Conversion
1900 and then Is_Entity_Name (Expression (Rec))
1901 and then Is_Shared_Passive (Entity (Expression (Rec)))
1902 then
1903 Add_Shared_Var_Lock_Procs (N);
1904 end if;
1906 end Build_Protected_Subprogram_Call;
1908 -------------------------
1909 -- Build_Selected_Name --
1910 -------------------------
1912 function Build_Selected_Name
1913 (Prefix, Selector : Name_Id;
1914 Append_Char : Character := ' ')
1915 return Name_Id
1917 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
1918 Select_Len : Natural;
1920 begin
1921 Get_Name_String (Selector);
1922 Select_Len := Name_Len;
1923 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
1924 Get_Name_String (Prefix);
1926 -- If scope is anonymous type, discard suffix to recover name of
1927 -- single protected object. Otherwise use protected type name.
1929 if Name_Buffer (Name_Len) = 'T' then
1930 Name_Len := Name_Len - 1;
1931 end if;
1933 Name_Buffer (Name_Len + 1) := 'P';
1934 Name_Buffer (Name_Len + 2) := 'T';
1935 Name_Buffer (Name_Len + 3) := '_';
1936 Name_Buffer (Name_Len + 4) := '_';
1938 Name_Len := Name_Len + 4;
1939 for J in 1 .. Select_Len loop
1940 Name_Len := Name_Len + 1;
1941 Name_Buffer (Name_Len) := Select_Buffer (J);
1942 end loop;
1944 if Append_Char /= ' ' then
1945 Name_Len := Name_Len + 1;
1946 Name_Buffer (Name_Len) := Append_Char;
1947 end if;
1949 return Name_Find;
1950 end Build_Selected_Name;
1952 -----------------------------
1953 -- Build_Simple_Entry_Call --
1954 -----------------------------
1956 -- A task entry call is converted to a call to Call_Simple
1958 -- declare
1959 -- P : parms := (parm, parm, parm);
1960 -- begin
1961 -- Call_Simple (acceptor-task, entry-index, P'Address);
1962 -- parm := P.param;
1963 -- parm := P.param;
1964 -- ...
1965 -- end;
1967 -- Here Pnn is an aggregate of the type constructed for the entry to hold
1968 -- the parameters, and the constructed aggregate value contains either the
1969 -- parameters or, in the case of non-elementary types, references to these
1970 -- parameters. Then the address of this aggregate is passed to the runtime
1971 -- routine, along with the task id value and the task entry index value.
1972 -- Pnn is only required if parameters are present.
1974 -- The assignments after the call are present only in the case of in-out
1975 -- or out parameters for elementary types, and are used to assign back the
1976 -- resulting values of such parameters.
1978 -- Note: the reason that we insert a block here is that in the context
1979 -- of selects, conditional entry calls etc. the entry call statement
1980 -- appears on its own, not as an element of a list.
1982 -- A protected entry call is converted to a Protected_Entry_Call:
1984 -- declare
1985 -- P : E1_Params := (param, param, param);
1986 -- Pnn : Boolean;
1987 -- Bnn : Communications_Block;
1989 -- declare
1990 -- P : E1_Params := (param, param, param);
1991 -- Bnn : Communications_Block;
1993 -- begin
1994 -- Protected_Entry_Call (
1995 -- Object => po._object'Access,
1996 -- E => <entry index>;
1997 -- Uninterpreted_Data => P'Address;
1998 -- Mode => Simple_Call;
1999 -- Block => Bnn);
2000 -- parm := P.param;
2001 -- parm := P.param;
2002 -- ...
2003 -- end;
2005 procedure Build_Simple_Entry_Call
2006 (N : Node_Id;
2007 Concval : Node_Id;
2008 Ename : Node_Id;
2009 Index : Node_Id)
2011 begin
2012 Expand_Call (N);
2014 -- Convert entry call to Call_Simple call
2016 declare
2017 Loc : constant Source_Ptr := Sloc (N);
2018 Parms : constant List_Id := Parameter_Associations (N);
2019 Pdecl : Node_Id;
2020 Xdecl : Node_Id;
2021 Decls : List_Id;
2022 Conctyp : Node_Id;
2023 Ent : Entity_Id;
2024 Ent_Acc : Entity_Id;
2025 P : Entity_Id;
2026 X : Entity_Id;
2027 Plist : List_Id;
2028 Parm1 : Node_Id;
2029 Parm2 : Node_Id;
2030 Parm3 : Node_Id;
2031 Call : Node_Id;
2032 Actual : Node_Id;
2033 Formal : Node_Id;
2034 N_Node : Node_Id;
2035 N_Var : Node_Id;
2036 Stats : List_Id := New_List;
2037 Comm_Name : Entity_Id;
2039 begin
2040 -- Simple entry and entry family cases merge here
2042 Ent := Entity (Ename);
2043 Ent_Acc := Entry_Parameters_Type (Ent);
2044 Conctyp := Etype (Concval);
2046 -- If prefix is an access type, dereference to obtain the task type
2048 if Is_Access_Type (Conctyp) then
2049 Conctyp := Designated_Type (Conctyp);
2050 end if;
2052 -- Special case for protected subprogram calls.
2054 if Is_Protected_Type (Conctyp)
2055 and then Is_Subprogram (Entity (Ename))
2056 then
2057 Build_Protected_Subprogram_Call
2058 (N, Ename, Convert_Concurrent (Concval, Conctyp));
2059 Analyze (N);
2060 return;
2061 end if;
2063 -- First parameter is the Task_Id value from the task value or the
2064 -- Object from the protected object value, obtained by selecting
2065 -- the _Task_Id or _Object from the result of doing an unchecked
2066 -- conversion to convert the value to the corresponding record type.
2068 Parm1 := Concurrent_Ref (Concval);
2070 -- Second parameter is the entry index, computed by the routine
2071 -- provided for this purpose. The value of this expression is
2072 -- assigned to an intermediate variable to assure that any entry
2073 -- family index expressions are evaluated before the entry
2074 -- parameters.
2076 if Abort_Allowed
2077 or else Restrictions (No_Entry_Queue) = False
2078 or else not Is_Protected_Type (Conctyp)
2079 or else Number_Entries (Conctyp) > 1
2080 then
2081 X := Make_Defining_Identifier (Loc, Name_uX);
2083 Xdecl :=
2084 Make_Object_Declaration (Loc,
2085 Defining_Identifier => X,
2086 Object_Definition =>
2087 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
2088 Expression => Actual_Index_Expression (
2089 Loc, Entity (Ename), Index, Concval));
2091 Decls := New_List (Xdecl);
2092 Parm2 := New_Reference_To (X, Loc);
2094 else
2095 Xdecl := Empty;
2096 Decls := New_List;
2097 Parm2 := Empty;
2098 end if;
2100 -- The third parameter is the packaged parameters. If there are
2101 -- none, then it is just the null address, since nothing is passed
2103 if No (Parms) then
2104 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
2105 P := Empty;
2107 -- Case of parameters present, where third argument is the address
2108 -- of a packaged record containing the required parameter values.
2110 else
2111 -- First build a list of parameter values, which are
2112 -- references to objects of the parameter types.
2114 Plist := New_List;
2116 Actual := First_Actual (N);
2117 Formal := First_Formal (Ent);
2119 while Present (Actual) loop
2121 -- If it is a by_copy_type, copy it to a new variable. The
2122 -- packaged record has a field that points to this variable.
2124 if Is_By_Copy_Type (Etype (Actual)) then
2125 N_Node :=
2126 Make_Object_Declaration (Loc,
2127 Defining_Identifier =>
2128 Make_Defining_Identifier (Loc,
2129 Chars => New_Internal_Name ('I')),
2130 Aliased_Present => True,
2131 Object_Definition =>
2132 New_Reference_To (Etype (Formal), Loc));
2134 -- We have to make an assignment statement separate for
2135 -- the case of limited type. We can not assign it unless
2136 -- the Assignment_OK flag is set first.
2138 if Ekind (Formal) /= E_Out_Parameter then
2139 N_Var :=
2140 New_Reference_To (Defining_Identifier (N_Node), Loc);
2141 Set_Assignment_OK (N_Var);
2142 Append_To (Stats,
2143 Make_Assignment_Statement (Loc,
2144 Name => N_Var,
2145 Expression => Relocate_Node (Actual)));
2146 end if;
2148 Append (N_Node, Decls);
2150 Append_To (Plist,
2151 Make_Attribute_Reference (Loc,
2152 Attribute_Name => Name_Unchecked_Access,
2153 Prefix =>
2154 New_Reference_To (Defining_Identifier (N_Node), Loc)));
2155 else
2156 Append_To (Plist,
2157 Make_Reference (Loc, Prefix => Relocate_Node (Actual)));
2158 end if;
2160 Next_Actual (Actual);
2161 Next_Formal_With_Extras (Formal);
2162 end loop;
2164 -- Now build the declaration of parameters initialized with the
2165 -- aggregate containing this constructed parameter list.
2167 P := Make_Defining_Identifier (Loc, Name_uP);
2169 Pdecl :=
2170 Make_Object_Declaration (Loc,
2171 Defining_Identifier => P,
2172 Object_Definition =>
2173 New_Reference_To (Designated_Type (Ent_Acc), Loc),
2174 Expression =>
2175 Make_Aggregate (Loc, Expressions => Plist));
2177 Parm3 :=
2178 Make_Attribute_Reference (Loc,
2179 Attribute_Name => Name_Address,
2180 Prefix => New_Reference_To (P, Loc));
2182 Append (Pdecl, Decls);
2183 end if;
2185 -- Now we can create the call, case of protected type
2187 if Is_Protected_Type (Conctyp) then
2188 if Abort_Allowed
2189 or else Restrictions (No_Entry_Queue) = False
2190 or else Number_Entries (Conctyp) > 1
2191 then
2192 -- Change the type of the index declaration
2194 Set_Object_Definition (Xdecl,
2195 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
2197 -- Some additional declarations for protected entry calls
2199 if No (Decls) then
2200 Decls := New_List;
2201 end if;
2203 -- Bnn : Communications_Block;
2205 Comm_Name :=
2206 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
2208 Append_To (Decls,
2209 Make_Object_Declaration (Loc,
2210 Defining_Identifier => Comm_Name,
2211 Object_Definition =>
2212 New_Reference_To (RTE (RE_Communication_Block), Loc)));
2214 -- Some additional statements for protected entry calls
2216 -- Protected_Entry_Call (
2217 -- Object => po._object'Access,
2218 -- E => <entry index>;
2219 -- Uninterpreted_Data => P'Address;
2220 -- Mode => Simple_Call;
2221 -- Block => Bnn);
2223 Call :=
2224 Make_Procedure_Call_Statement (Loc,
2225 Name =>
2226 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
2228 Parameter_Associations => New_List (
2229 Make_Attribute_Reference (Loc,
2230 Attribute_Name => Name_Unchecked_Access,
2231 Prefix => Parm1),
2232 Parm2,
2233 Parm3,
2234 New_Reference_To (RTE (RE_Simple_Call), Loc),
2235 New_Occurrence_Of (Comm_Name, Loc)));
2237 else
2238 -- Protected_Single_Entry_Call (
2239 -- Object => po._object'Access,
2240 -- Uninterpreted_Data => P'Address;
2241 -- Mode => Simple_Call);
2243 Call :=
2244 Make_Procedure_Call_Statement (Loc,
2245 Name => New_Reference_To (
2246 RTE (RE_Protected_Single_Entry_Call), Loc),
2248 Parameter_Associations => New_List (
2249 Make_Attribute_Reference (Loc,
2250 Attribute_Name => Name_Unchecked_Access,
2251 Prefix => Parm1),
2252 Parm3,
2253 New_Reference_To (RTE (RE_Simple_Call), Loc)));
2254 end if;
2256 -- Case of task type
2258 else
2259 Call :=
2260 Make_Procedure_Call_Statement (Loc,
2261 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
2262 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
2264 end if;
2266 Append_To (Stats, Call);
2268 -- If there are out or in/out parameters by copy
2269 -- add assignment statements for the result values.
2271 if Present (Parms) then
2272 Actual := First_Actual (N);
2273 Formal := First_Formal (Ent);
2275 Set_Assignment_OK (Actual);
2276 while Present (Actual) loop
2277 if Is_By_Copy_Type (Etype (Actual))
2278 and then Ekind (Formal) /= E_In_Parameter
2279 then
2280 N_Node :=
2281 Make_Assignment_Statement (Loc,
2282 Name => New_Copy (Actual),
2283 Expression =>
2284 Make_Explicit_Dereference (Loc,
2285 Make_Selected_Component (Loc,
2286 Prefix => New_Reference_To (P, Loc),
2287 Selector_Name =>
2288 Make_Identifier (Loc, Chars (Formal)))));
2290 -- In all cases (including limited private types) we
2291 -- want the assignment to be valid.
2293 Set_Assignment_OK (Name (N_Node));
2295 -- If the call is the triggering alternative in an
2296 -- asynchronous select, or the entry_call alternative
2297 -- of a conditional entry call, the assignments for in-out
2298 -- parameters are incorporated into the statement list
2299 -- that follows, so that there are executed only if the
2300 -- entry call succeeds.
2302 if (Nkind (Parent (N)) = N_Triggering_Alternative
2303 and then N = Triggering_Statement (Parent (N)))
2304 or else
2305 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2306 and then N = Entry_Call_Statement (Parent (N)))
2307 then
2308 if No (Statements (Parent (N))) then
2309 Set_Statements (Parent (N), New_List);
2310 end if;
2312 Prepend (N_Node, Statements (Parent (N)));
2314 else
2315 Insert_After (Call, N_Node);
2316 end if;
2317 end if;
2319 Next_Actual (Actual);
2320 Next_Formal_With_Extras (Formal);
2321 end loop;
2322 end if;
2324 -- Finally, create block and analyze it
2326 Rewrite (N,
2327 Make_Block_Statement (Loc,
2328 Declarations => Decls,
2329 Handled_Statement_Sequence =>
2330 Make_Handled_Sequence_Of_Statements (Loc,
2331 Statements => Stats)));
2333 Analyze (N);
2334 end;
2336 end Build_Simple_Entry_Call;
2338 --------------------------------
2339 -- Build_Task_Activation_Call --
2340 --------------------------------
2342 procedure Build_Task_Activation_Call (N : Node_Id) is
2343 Loc : constant Source_Ptr := Sloc (N);
2344 Chain : Entity_Id;
2345 Call : Node_Id;
2346 Name : Node_Id;
2347 P : Node_Id;
2349 begin
2350 -- Get the activation chain entity. Except in the case of a package
2351 -- body, this is in the node that was passed. For a package body, we
2352 -- have to find the corresponding package declaration node.
2354 if Nkind (N) = N_Package_Body then
2355 P := Corresponding_Spec (N);
2357 loop
2358 P := Parent (P);
2359 exit when Nkind (P) = N_Package_Declaration;
2360 end loop;
2362 Chain := Activation_Chain_Entity (P);
2364 else
2365 Chain := Activation_Chain_Entity (N);
2366 end if;
2368 if Present (Chain) then
2369 if Restricted_Profile then
2370 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
2371 else
2372 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
2373 end if;
2375 Call :=
2376 Make_Procedure_Call_Statement (Loc,
2377 Name => Name,
2378 Parameter_Associations =>
2379 New_List (Make_Attribute_Reference (Loc,
2380 Prefix => New_Occurrence_Of (Chain, Loc),
2381 Attribute_Name => Name_Unchecked_Access)));
2383 if Nkind (N) = N_Package_Declaration then
2384 if Present (Corresponding_Body (N)) then
2385 null;
2387 elsif Present (Private_Declarations (Specification (N))) then
2388 Append (Call, Private_Declarations (Specification (N)));
2390 else
2391 Append (Call, Visible_Declarations (Specification (N)));
2392 end if;
2394 else
2395 if Present (Handled_Statement_Sequence (N)) then
2397 -- The call goes at the start of the statement sequence, but
2398 -- after the start of exception range label if one is present.
2400 declare
2401 Stm : Node_Id;
2403 begin
2404 Stm := First (Statements (Handled_Statement_Sequence (N)));
2406 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
2407 Next (Stm);
2408 end if;
2410 Insert_Before (Stm, Call);
2411 end;
2413 else
2414 Set_Handled_Statement_Sequence (N,
2415 Make_Handled_Sequence_Of_Statements (Loc,
2416 Statements => New_List (Call)));
2417 end if;
2418 end if;
2420 Analyze (Call);
2421 Check_Task_Activation (N);
2422 end if;
2424 end Build_Task_Activation_Call;
2426 -------------------------------
2427 -- Build_Task_Allocate_Block --
2428 -------------------------------
2430 procedure Build_Task_Allocate_Block
2431 (Actions : List_Id;
2432 N : Node_Id;
2433 Args : List_Id)
2435 T : constant Entity_Id := Entity (Expression (N));
2436 Init : constant Entity_Id := Base_Init_Proc (T);
2437 Loc : constant Source_Ptr := Sloc (N);
2439 Chain : Entity_Id := Make_Defining_Identifier (Loc, Name_uChain);
2440 Blkent : Entity_Id;
2441 Block : Node_Id;
2443 begin
2444 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2446 Block :=
2447 Make_Block_Statement (Loc,
2448 Identifier => New_Reference_To (Blkent, Loc),
2449 Declarations => New_List (
2451 -- _Chain : Activation_Chain;
2453 Make_Object_Declaration (Loc,
2454 Defining_Identifier => Chain,
2455 Aliased_Present => True,
2456 Object_Definition =>
2457 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
2459 Handled_Statement_Sequence =>
2460 Make_Handled_Sequence_Of_Statements (Loc,
2462 Statements => New_List (
2464 -- Init (Args);
2466 Make_Procedure_Call_Statement (Loc,
2467 Name => New_Reference_To (Init, Loc),
2468 Parameter_Associations => Args),
2470 -- Activate_Tasks (_Chain);
2472 Make_Procedure_Call_Statement (Loc,
2473 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
2474 Parameter_Associations => New_List (
2475 Make_Attribute_Reference (Loc,
2476 Prefix => New_Reference_To (Chain, Loc),
2477 Attribute_Name => Name_Unchecked_Access))))),
2479 Has_Created_Identifier => True,
2480 Is_Task_Allocation_Block => True);
2482 Append_To (Actions,
2483 Make_Implicit_Label_Declaration (Loc,
2484 Defining_Identifier => Blkent,
2485 Label_Construct => Block));
2487 Append_To (Actions, Block);
2489 Set_Activation_Chain_Entity (Block, Chain);
2491 end Build_Task_Allocate_Block;
2493 -----------------------------------
2494 -- Build_Task_Proc_Specification --
2495 -----------------------------------
2497 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
2498 Loc : constant Source_Ptr := Sloc (T);
2499 Nam : constant Name_Id := Chars (T);
2500 Tdec : constant Node_Id := Declaration_Node (T);
2501 Ent : Entity_Id;
2503 begin
2504 Ent :=
2505 Make_Defining_Identifier (Loc,
2506 Chars => New_External_Name (Nam, 'B'));
2507 Set_Is_Internal (Ent);
2509 -- Associate the procedure with the task, if this is the declaration
2510 -- (and not the body) of the procedure.
2512 if No (Task_Body_Procedure (Tdec)) then
2513 Set_Task_Body_Procedure (Tdec, Ent);
2514 end if;
2516 return
2517 Make_Procedure_Specification (Loc,
2518 Defining_Unit_Name => Ent,
2519 Parameter_Specifications =>
2520 New_List (
2521 Make_Parameter_Specification (Loc,
2522 Defining_Identifier =>
2523 Make_Defining_Identifier (Loc, Name_uTask),
2524 Parameter_Type =>
2525 Make_Access_Definition (Loc,
2526 Subtype_Mark =>
2527 New_Reference_To
2528 (Corresponding_Record_Type (T), Loc)))));
2530 end Build_Task_Proc_Specification;
2532 ---------------------------------------
2533 -- Build_Unprotected_Subprogram_Body --
2534 ---------------------------------------
2536 function Build_Unprotected_Subprogram_Body
2537 (N : Node_Id;
2538 Pid : Node_Id)
2539 return Node_Id
2541 Loc : constant Source_Ptr := Sloc (N);
2542 Sub_Name : Name_Id;
2543 N_Op_Spec : Node_Id;
2544 Op_Decls : List_Id;
2546 begin
2547 -- Make an unprotected version of the subprogram for use
2548 -- within the same object, with a new name and an additional
2549 -- parameter representing the object.
2551 Op_Decls := Declarations (N);
2552 Sub_Name := Chars (Defining_Unit_Name (Specification (N)));
2554 N_Op_Spec :=
2555 Build_Protected_Sub_Specification
2556 (N, Pid, Unprotected => True);
2558 return
2559 Make_Subprogram_Body (Loc,
2560 Specification => N_Op_Spec,
2561 Declarations => Op_Decls,
2562 Handled_Statement_Sequence =>
2563 Handled_Statement_Sequence (N));
2565 end Build_Unprotected_Subprogram_Body;
2567 ----------------------------
2568 -- Collect_Entry_Families --
2569 ----------------------------
2571 procedure Collect_Entry_Families
2572 (Loc : Source_Ptr;
2573 Cdecls : List_Id;
2574 Current_Node : in out Node_Id;
2575 Conctyp : Entity_Id)
2577 Efam : Entity_Id;
2578 Efam_Decl : Node_Id;
2579 Efam_Type : Entity_Id;
2581 begin
2582 Efam := First_Entity (Conctyp);
2584 while Present (Efam) loop
2586 if Ekind (Efam) = E_Entry_Family then
2587 Efam_Type :=
2588 Make_Defining_Identifier (Loc,
2589 Chars => New_Internal_Name ('F'));
2591 Efam_Decl :=
2592 Make_Full_Type_Declaration (Loc,
2593 Defining_Identifier => Efam_Type,
2594 Type_Definition =>
2595 Make_Unconstrained_Array_Definition (Loc,
2596 Subtype_Marks => (New_List (
2597 New_Occurrence_Of (
2598 Base_Type
2599 (Etype (Discrete_Subtype_Definition
2600 (Parent (Efam)))), Loc))),
2602 Subtype_Indication =>
2603 New_Reference_To (Standard_Character, Loc)));
2605 Insert_After (Current_Node, Efam_Decl);
2606 Current_Node := Efam_Decl;
2607 Analyze (Efam_Decl);
2609 Append_To (Cdecls,
2610 Make_Component_Declaration (Loc,
2611 Defining_Identifier =>
2612 Make_Defining_Identifier (Loc, Chars (Efam)),
2614 Subtype_Indication =>
2615 Make_Subtype_Indication (Loc,
2616 Subtype_Mark =>
2617 New_Occurrence_Of (Efam_Type, Loc),
2619 Constraint =>
2620 Make_Index_Or_Discriminant_Constraint (Loc,
2621 Constraints => New_List (
2622 New_Occurrence_Of
2623 (Etype (Discrete_Subtype_Definition
2624 (Parent (Efam))), Loc))))));
2625 end if;
2627 Next_Entity (Efam);
2628 end loop;
2629 end Collect_Entry_Families;
2631 --------------------
2632 -- Concurrent_Ref --
2633 --------------------
2635 -- The expression returned for a reference to a concurrent
2636 -- object has the form:
2638 -- taskV!(name)._Task_Id
2640 -- for a task, and
2642 -- objectV!(name)._Object
2644 -- for a protected object.
2646 -- For the case of an access to a concurrent object,
2647 -- there is an extra explicit dereference:
2649 -- taskV!(name.all)._Task_Id
2650 -- objectV!(name.all)._Object
2652 -- here taskV and objectV are the types for the associated records, which
2653 -- contain the required _Task_Id and _Object fields for tasks and
2654 -- protected objects, respectively.
2656 -- For the case of a task type name, the expression is
2658 -- Self;
2660 -- i.e. a call to the Self function which returns precisely this Task_Id
2662 -- For the case of a protected type name, the expression is
2664 -- objectR
2666 -- which is a renaming of the _object field of the current object
2667 -- object record, passed into protected operations as a parameter.
2669 function Concurrent_Ref (N : Node_Id) return Node_Id is
2670 Loc : constant Source_Ptr := Sloc (N);
2671 Ntyp : constant Entity_Id := Etype (N);
2672 Dtyp : Entity_Id;
2673 Sel : Name_Id;
2675 function Is_Current_Task (T : Entity_Id) return Boolean;
2676 -- Check whether the reference is to the immediately enclosing task
2677 -- type, or to an outer one (rare but legal).
2679 ---------------------
2680 -- Is_Current_Task --
2681 ---------------------
2683 function Is_Current_Task (T : Entity_Id) return Boolean is
2684 Scop : Entity_Id;
2686 begin
2687 Scop := Current_Scope;
2688 while Present (Scop)
2689 and then Scop /= Standard_Standard
2690 loop
2692 if Scop = T then
2693 return True;
2695 elsif Is_Task_Type (Scop) then
2696 return False;
2698 -- If this is a procedure nested within the task type, we must
2699 -- assume that it can be called from an inner task, and therefore
2700 -- cannot treat it as a local reference.
2702 elsif Is_Overloadable (Scop)
2703 and then In_Open_Scopes (T)
2704 then
2705 return False;
2707 else
2708 Scop := Scope (Scop);
2709 end if;
2710 end loop;
2712 -- We know that we are within the task body, so should have
2713 -- found it in scope.
2715 raise Program_Error;
2716 end Is_Current_Task;
2718 -- Start of processing for Concurrent_Ref
2720 begin
2721 if Is_Access_Type (Ntyp) then
2722 Dtyp := Designated_Type (Ntyp);
2724 if Is_Protected_Type (Dtyp) then
2725 Sel := Name_uObject;
2726 else
2727 Sel := Name_uTask_Id;
2728 end if;
2730 return
2731 Make_Selected_Component (Loc,
2732 Prefix =>
2733 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
2734 Make_Explicit_Dereference (Loc, N)),
2735 Selector_Name => Make_Identifier (Loc, Sel));
2737 elsif Is_Entity_Name (N)
2738 and then Is_Concurrent_Type (Entity (N))
2739 then
2740 if Is_Task_Type (Entity (N)) then
2742 if Is_Current_Task (Entity (N)) then
2743 return
2744 Make_Function_Call (Loc,
2745 Name => New_Reference_To (RTE (RE_Self), Loc));
2747 else
2748 declare
2749 Decl : Node_Id;
2750 T_Self : constant Entity_Id
2751 := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2752 T_Body : constant Node_Id
2753 := Parent (Corresponding_Body (Parent (Entity (N))));
2755 begin
2756 Decl := Make_Object_Declaration (Loc,
2757 Defining_Identifier => T_Self,
2758 Object_Definition =>
2759 New_Occurrence_Of (RTE (RO_ST_Task_ID), Loc),
2760 Expression =>
2761 Make_Function_Call (Loc,
2762 Name => New_Reference_To (RTE (RE_Self), Loc)));
2763 Prepend (Decl, Declarations (T_Body));
2764 Analyze (Decl);
2765 Set_Scope (T_Self, Entity (N));
2766 return New_Occurrence_Of (T_Self, Loc);
2767 end;
2768 end if;
2770 else
2771 pragma Assert (Is_Protected_Type (Entity (N)));
2772 return
2773 New_Reference_To (
2774 Object_Ref (Corresponding_Body (Parent (Base_Type (Ntyp)))),
2775 Loc);
2776 end if;
2778 else
2779 pragma Assert (Is_Concurrent_Type (Ntyp));
2781 if Is_Protected_Type (Ntyp) then
2782 Sel := Name_uObject;
2783 else
2784 Sel := Name_uTask_Id;
2785 end if;
2787 return
2788 Make_Selected_Component (Loc,
2789 Prefix =>
2790 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
2791 New_Copy_Tree (N)),
2792 Selector_Name => Make_Identifier (Loc, Sel));
2793 end if;
2794 end Concurrent_Ref;
2796 ------------------------
2797 -- Convert_Concurrent --
2798 ------------------------
2800 function Convert_Concurrent
2801 (N : Node_Id;
2802 Typ : Entity_Id)
2803 return Node_Id
2805 begin
2806 if not Is_Concurrent_Type (Typ) then
2807 return N;
2808 else
2809 return
2810 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2811 New_Copy_Tree (N));
2812 end if;
2813 end Convert_Concurrent;
2815 ----------------------------
2816 -- Entry_Index_Expression --
2817 ----------------------------
2819 function Entry_Index_Expression
2820 (Sloc : Source_Ptr;
2821 Ent : Entity_Id;
2822 Index : Node_Id;
2823 Ttyp : Entity_Id)
2824 return Node_Id
2826 Expr : Node_Id;
2827 Num : Node_Id;
2828 Lo : Node_Id;
2829 Hi : Node_Id;
2830 Prev : Entity_Id;
2831 S : Node_Id;
2833 begin
2834 -- The queues of entries and entry families appear in textual
2835 -- order in the associated record. The entry index is computed as
2836 -- the sum of the number of queues for all entries that precede the
2837 -- designated one, to which is added the index expression, if this
2838 -- expression denotes a member of a family.
2840 -- The following is a place holder for the count of simple entries.
2842 Num := Make_Integer_Literal (Sloc, 1);
2844 -- We construct an expression which is a series of addition
2845 -- operations. The first operand is the number of single entries that
2846 -- precede this one, the second operand is the index value relative
2847 -- to the start of the referenced family, and the remaining operands
2848 -- are the lengths of the entry families that precede this entry, i.e.
2849 -- the constructed expression is:
2851 -- number_simple_entries +
2852 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
2853 -- family'length + ...
2855 -- where index-value is the given index value, and s is the index
2856 -- subtype (we have to use pos because the subtype might be an
2857 -- enumeration type preventing direct subtraction).
2858 -- Note that the task entry array is one-indexed.
2860 -- The upper bound of the entry family may be a discriminant, so we
2861 -- retrieve the lower bound explicitly to compute offset, rather than
2862 -- using the index subtype which may mention a discriminant.
2864 if Present (Index) then
2865 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
2867 Expr :=
2868 Make_Op_Add (Sloc,
2869 Left_Opnd => Num,
2871 Right_Opnd =>
2872 Family_Offset (
2873 Sloc,
2874 Make_Attribute_Reference (Sloc,
2875 Attribute_Name => Name_Pos,
2876 Prefix => New_Reference_To (Base_Type (S), Sloc),
2877 Expressions => New_List (Relocate_Node (Index))),
2878 Type_Low_Bound (S),
2879 Ttyp));
2880 else
2881 Expr := Num;
2882 end if;
2884 -- Now add lengths of preceding entries and entry families.
2886 Prev := First_Entity (Ttyp);
2888 while Chars (Prev) /= Chars (Ent)
2889 or else (Ekind (Prev) /= Ekind (Ent))
2890 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
2891 loop
2892 if Ekind (Prev) = E_Entry then
2893 Set_Intval (Num, Intval (Num) + 1);
2895 elsif Ekind (Prev) = E_Entry_Family then
2896 S :=
2897 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
2898 Lo := Type_Low_Bound (S);
2899 Hi := Type_High_Bound (S);
2901 Expr :=
2902 Make_Op_Add (Sloc,
2903 Left_Opnd => Expr,
2904 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp));
2906 -- Other components are anonymous types to be ignored.
2908 else
2909 null;
2910 end if;
2912 Next_Entity (Prev);
2913 end loop;
2915 return Expr;
2916 end Entry_Index_Expression;
2918 ---------------------------
2919 -- Establish_Task_Master --
2920 ---------------------------
2922 procedure Establish_Task_Master (N : Node_Id) is
2923 Call : Node_Id;
2925 begin
2926 if Restrictions (No_Task_Hierarchy) = False then
2927 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
2928 Prepend_To (Declarations (N), Call);
2929 Analyze (Call);
2930 end if;
2931 end Establish_Task_Master;
2933 --------------------------------
2934 -- Expand_Accept_Declarations --
2935 --------------------------------
2937 -- Part of the expansion of an accept statement involves the creation of
2938 -- a declaration that can be referenced from the statement sequence of
2939 -- the accept:
2941 -- Ann : Address;
2943 -- This declaration is inserted immediately before the accept statement
2944 -- and it is important that it be inserted before the statements of the
2945 -- statement sequence are analyzed. Thus it would be too late to create
2946 -- this declaration in the Expand_N_Accept_Statement routine, which is
2947 -- why there is a separate procedure to be called directly from Sem_Ch9.
2949 -- Ann is used to hold the address of the record containing the parameters
2950 -- (see Expand_N_Entry_Call for more details on how this record is built).
2951 -- References to the parameters do an unchecked conversion of this address
2952 -- to a pointer to the required record type, and then access the field that
2953 -- holds the value of the required parameter. The entity for the address
2954 -- variable is held as the top stack element (i.e. the last element) of the
2955 -- Accept_Address stack in the corresponding entry entity, and this element
2956 -- must be set in place before the statements are processed.
2958 -- The above description applies to the case of a stand alone accept
2959 -- statement, i.e. one not appearing as part of a select alternative.
2961 -- For the case of an accept that appears as part of a select alternative
2962 -- of a selective accept, we must still create the declaration right away,
2963 -- since Ann is needed immediately, but there is an important difference:
2965 -- The declaration is inserted before the selective accept, not before
2966 -- the accept statement (which is not part of a list anyway, and so would
2967 -- not accommodate inserted declarations)
2969 -- We only need one address variable for the entire selective accept. So
2970 -- the Ann declaration is created only for the first accept alternative,
2971 -- and subsequent accept alternatives reference the same Ann variable.
2973 -- We can distinguish the two cases by seeing whether the accept statement
2974 -- is part of a list. If not, then it must be in an accept alternative.
2976 -- To expand the requeue statement, a label is provided at the end of
2977 -- the accept statement or alternative of which it is a part, so that
2978 -- the statement can be skipped after the requeue is complete.
2979 -- This label is created here rather than during the expansion of the
2980 -- accept statement, because it will be needed by any requeue
2981 -- statements within the accept, which are expanded before the
2982 -- accept.
2984 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
2985 Loc : constant Source_Ptr := Sloc (N);
2986 Ann : Entity_Id := Empty;
2987 Adecl : Node_Id;
2988 Lab_Id : Node_Id;
2989 Lab : Node_Id;
2990 Ldecl : Node_Id;
2991 Ldecl2 : Node_Id;
2993 begin
2994 if Expander_Active then
2996 -- If we have no handled statement sequence, then build a dummy
2997 -- sequence consisting of a null statement. This is only done if
2998 -- pragma FIFO_Within_Priorities is specified. The issue here is
2999 -- that even a null accept body has an effect on the called task
3000 -- in terms of its position in the queue, so we cannot optimize
3001 -- the context switch away. However, if FIFO_Within_Priorities
3002 -- is not active, the optimization is legitimate, since we can
3003 -- say that our dispatching policy (i.e. the default dispatching
3004 -- policy) reorders the queue to be the same as just before the
3005 -- call. In the absence of a specified dispatching policy, we are
3006 -- allowed to modify queue orders for a given priority at will!
3008 if Opt.Task_Dispatching_Policy = 'F' and then
3009 not Present (Handled_Statement_Sequence (N))
3010 then
3011 Set_Handled_Statement_Sequence (N,
3012 Make_Handled_Sequence_Of_Statements (Loc,
3013 New_List (Make_Null_Statement (Loc))));
3014 end if;
3016 -- Create and declare two labels to be placed at the end of the
3017 -- accept statement. The first label is used to allow requeues to
3018 -- skip the remainder of entry processing. The second label is
3019 -- used to skip the remainder of entry processing if the rendezvous
3020 -- completes in the middle of the accept body.
3022 if Present (Handled_Statement_Sequence (N)) then
3023 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3024 Set_Entity (Lab_Id,
3025 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3026 Lab := Make_Label (Loc, Lab_Id);
3027 Ldecl :=
3028 Make_Implicit_Label_Declaration (Loc,
3029 Defining_Identifier => Entity (Lab_Id),
3030 Label_Construct => Lab);
3031 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3033 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3034 Set_Entity (Lab_Id,
3035 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3036 Lab := Make_Label (Loc, Lab_Id);
3037 Ldecl2 :=
3038 Make_Implicit_Label_Declaration (Loc,
3039 Defining_Identifier => Entity (Lab_Id),
3040 Label_Construct => Lab);
3041 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3043 else
3044 Ldecl := Empty;
3045 Ldecl2 := Empty;
3046 end if;
3048 -- Case of stand alone accept statement
3050 if Is_List_Member (N) then
3052 if Present (Handled_Statement_Sequence (N)) then
3053 Ann :=
3054 Make_Defining_Identifier (Loc,
3055 Chars => New_Internal_Name ('A'));
3057 Adecl :=
3058 Make_Object_Declaration (Loc,
3059 Defining_Identifier => Ann,
3060 Object_Definition =>
3061 New_Reference_To (RTE (RE_Address), Loc));
3063 Insert_Before (N, Adecl);
3064 Analyze (Adecl);
3066 Insert_Before (N, Ldecl);
3067 Analyze (Ldecl);
3069 Insert_Before (N, Ldecl2);
3070 Analyze (Ldecl2);
3071 end if;
3073 -- Case of accept statement which is in an accept alternative
3075 else
3076 declare
3077 Acc_Alt : constant Node_Id := Parent (N);
3078 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
3079 Alt : Node_Id;
3081 begin
3082 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
3083 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
3085 -- ??? Consider a single label for select statements.
3087 if Present (Handled_Statement_Sequence (N)) then
3088 Prepend (Ldecl2,
3089 Statements (Handled_Statement_Sequence (N)));
3090 Analyze (Ldecl2);
3092 Prepend (Ldecl,
3093 Statements (Handled_Statement_Sequence (N)));
3094 Analyze (Ldecl);
3095 end if;
3097 -- Find first accept alternative of the selective accept. A
3098 -- valid selective accept must have at least one accept in it.
3100 Alt := First (Select_Alternatives (Sel_Acc));
3102 while Nkind (Alt) /= N_Accept_Alternative loop
3103 Next (Alt);
3104 end loop;
3106 -- If we are the first accept statement, then we have to
3107 -- create the Ann variable, as for the stand alone case,
3108 -- except that it is inserted before the selective accept.
3109 -- Similarly, a label for requeue expansion must be
3110 -- declared.
3112 if N = Accept_Statement (Alt) then
3113 Ann :=
3114 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3116 Adecl :=
3117 Make_Object_Declaration (Loc,
3118 Defining_Identifier => Ann,
3119 Object_Definition =>
3120 New_Reference_To (RTE (RE_Address), Loc));
3122 Insert_Before (Sel_Acc, Adecl);
3123 Analyze (Adecl);
3125 -- If we are not the first accept statement, then find the
3126 -- Ann variable allocated by the first accept and use it.
3128 else
3129 Ann :=
3130 Node (Last_Elmt (Accept_Address
3131 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
3132 end if;
3133 end;
3134 end if;
3136 -- Merge here with Ann either created or referenced, and Adecl
3137 -- pointing to the corresponding declaration. Remaining processing
3138 -- is the same for the two cases.
3140 if Present (Ann) then
3141 Append_Elmt (Ann, Accept_Address (Ent));
3142 end if;
3143 end if;
3144 end Expand_Accept_Declarations;
3146 ---------------------------------------------
3147 -- Expand_Access_Protected_Subprogram_Type --
3148 ---------------------------------------------
3150 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
3151 Loc : constant Source_Ptr := Sloc (N);
3152 Comps : List_Id;
3153 T : constant Entity_Id := Defining_Identifier (N);
3154 D_T : constant Entity_Id := Designated_Type (T);
3155 D_T2 : constant Entity_Id := Make_Defining_Identifier
3156 (Loc, New_Internal_Name ('D'));
3157 E_T : constant Entity_Id := Make_Defining_Identifier
3158 (Loc, New_Internal_Name ('E'));
3159 P_List : constant List_Id := Build_Protected_Spec
3160 (N, RTE (RE_Address), False, D_T);
3161 Decl1 : Node_Id;
3162 Decl2 : Node_Id;
3163 Def1 : Node_Id;
3165 begin
3166 -- Create access to protected subprogram with full signature.
3168 if Nkind (Type_Definition (N)) = N_Access_Function_Definition then
3169 Def1 :=
3170 Make_Access_Function_Definition (Loc,
3171 Parameter_Specifications => P_List,
3172 Subtype_Mark => New_Copy (Subtype_Mark (Type_Definition (N))));
3174 else
3175 Def1 :=
3176 Make_Access_Procedure_Definition (Loc,
3177 Parameter_Specifications => P_List);
3178 end if;
3180 Decl1 :=
3181 Make_Full_Type_Declaration (Loc,
3182 Defining_Identifier => D_T2,
3183 Type_Definition => Def1);
3185 Insert_After (N, Decl1);
3187 -- Create Equivalent_Type, a record with two components for an
3188 -- an access to object an an access to subprogram.
3190 Comps := New_List (
3191 Make_Component_Declaration (Loc,
3192 Defining_Identifier =>
3193 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
3194 Subtype_Indication =>
3195 New_Occurrence_Of (RTE (RE_Address), Loc)),
3197 Make_Component_Declaration (Loc,
3198 Defining_Identifier =>
3199 Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
3200 Subtype_Indication =>
3201 New_Occurrence_Of (D_T2, Loc)));
3203 Decl2 :=
3204 Make_Full_Type_Declaration (Loc,
3205 Defining_Identifier => E_T,
3206 Type_Definition =>
3207 Make_Record_Definition (Loc,
3208 Component_List =>
3209 Make_Component_List (Loc,
3210 Component_Items => Comps)));
3212 Insert_After (Decl1, Decl2);
3213 Set_Equivalent_Type (T, E_T);
3215 end Expand_Access_Protected_Subprogram_Type;
3217 --------------------------
3218 -- Expand_Entry_Barrier --
3219 --------------------------
3221 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
3222 Loc : constant Source_Ptr := Sloc (N);
3223 Func : Node_Id;
3224 B_F : Node_Id;
3225 Prot : constant Entity_Id := Scope (Ent);
3226 Spec_Decl : Node_Id := Parent (Prot);
3227 Body_Decl : Node_Id;
3228 Cond : Node_Id := Condition (Entry_Body_Formal_Part (N));
3230 begin
3231 -- The body of the entry barrier must be analyzed in the context of
3232 -- the protected object, but its scope is external to it, just as any
3233 -- other unprotected version of a protected operation. The specification
3234 -- has been produced when the protected type declaration was elaborated.
3235 -- We build the body, insert it in the enclosing scope, but analyze it
3236 -- in the current context. A more uniform approach would be to treat a
3237 -- barrier just as a protected function, and discard the protected
3238 -- version of it because it is never called.
3240 if Expander_Active then
3241 B_F := Build_Barrier_Function (N, Ent, Prot);
3242 Func := Barrier_Function (Ent);
3243 Set_Corresponding_Spec (B_F, Func);
3245 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
3247 if Nkind (Parent (Body_Decl)) = N_Subunit then
3248 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
3249 end if;
3251 Insert_Before_And_Analyze (Body_Decl, B_F);
3253 Update_Prival_Subtypes (B_F);
3255 Set_Privals (Spec_Decl, N, Loc);
3256 Set_Discriminals (Spec_Decl);
3257 Set_Scope (Func, Scope (Prot));
3258 else
3259 Analyze (Cond);
3260 end if;
3262 -- The Ravenscar profile restricts barriers to simple variables
3263 -- declared within the protected object. We also allow Boolean
3264 -- constants, since these appear in several published examples
3265 -- and are also allowed by the Aonix compiler.
3267 -- Note that after analysis variables in this context will be
3268 -- replaced by the corresponding prival, that is to say a renaming
3269 -- of a selected component of the form _Object.Var. If expansion is
3270 -- disabled, as within a generic, we check that the entity appears in
3271 -- the current scope.
3273 if Is_Entity_Name (Cond) then
3275 if Entity (Cond) = Standard_False
3276 or else
3277 Entity (Cond) = Standard_True
3278 then
3279 return;
3281 elsif not Expander_Active
3282 and then Scope (Entity (Cond)) = Current_Scope
3283 then
3284 return;
3286 elsif Present (Renamed_Object (Entity (Cond)))
3287 and then
3288 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
3289 and then
3290 Chars (Prefix (Renamed_Object (Entity (Cond)))) = Name_uObject
3291 then
3292 return;
3293 end if;
3294 end if;
3296 -- It is not a boolean variable or literal, so check the restriction
3298 Check_Restriction (Boolean_Entry_Barriers, Cond);
3299 end Expand_Entry_Barrier;
3301 ------------------------------------
3302 -- Expand_Entry_Body_Declarations --
3303 ------------------------------------
3305 procedure Expand_Entry_Body_Declarations (N : Node_Id) is
3306 Loc : constant Source_Ptr := Sloc (N);
3307 Index_Spec : Node_Id;
3309 begin
3310 if Expander_Active then
3312 -- Expand entry bodies corresponding to entry families
3313 -- by assigning a placeholder for the constant that will
3314 -- be used to expand references to the entry index parameter.
3316 Index_Spec :=
3317 Entry_Index_Specification (Entry_Body_Formal_Part (N));
3319 if Present (Index_Spec) then
3320 Set_Entry_Index_Constant (
3321 Defining_Identifier (Index_Spec),
3322 Make_Defining_Identifier (Loc, New_Internal_Name ('I')));
3323 end if;
3325 end if;
3326 end Expand_Entry_Body_Declarations;
3328 ------------------------------
3329 -- Expand_N_Abort_Statement --
3330 ------------------------------
3332 -- Expand abort T1, T2, .. Tn; into:
3333 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
3335 procedure Expand_N_Abort_Statement (N : Node_Id) is
3336 Loc : constant Source_Ptr := Sloc (N);
3337 Tlist : constant List_Id := Names (N);
3338 Count : Nat;
3339 Aggr : Node_Id;
3340 Tasknm : Node_Id;
3342 begin
3343 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
3344 Count := 0;
3346 Tasknm := First (Tlist);
3348 while Present (Tasknm) loop
3349 Count := Count + 1;
3350 Append_To (Component_Associations (Aggr),
3351 Make_Component_Association (Loc,
3352 Choices => New_List (
3353 Make_Integer_Literal (Loc, Count)),
3354 Expression => Concurrent_Ref (Tasknm)));
3355 Next (Tasknm);
3356 end loop;
3358 Rewrite (N,
3359 Make_Procedure_Call_Statement (Loc,
3360 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
3361 Parameter_Associations => New_List (
3362 Make_Qualified_Expression (Loc,
3363 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
3364 Expression => Aggr))));
3366 Analyze (N);
3368 end Expand_N_Abort_Statement;
3370 -------------------------------
3371 -- Expand_N_Accept_Statement --
3372 -------------------------------
3374 -- This procedure handles expansion of accept statements that stand
3375 -- alone, i.e. they are not part of an accept alternative. The expansion
3376 -- of accept statement in accept alternatives is handled by the routines
3377 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
3378 -- following description applies only to stand alone accept statements.
3380 -- If there is no handled statement sequence, or only null statements,
3381 -- then this is called a trivial accept, and the expansion is:
3383 -- Accept_Trivial (entry-index)
3385 -- If there is a handled statement sequence, then the expansion is:
3387 -- Ann : Address;
3388 -- {Lnn : Label}
3390 -- begin
3391 -- begin
3392 -- Accept_Call (entry-index, Ann);
3393 -- <statement sequence from N_Accept_Statement node>
3394 -- Complete_Rendezvous;
3395 -- <<Lnn>>
3397 -- exception
3398 -- when ... =>
3399 -- <exception handler from N_Accept_Statement node>
3400 -- Complete_Rendezvous;
3401 -- when ... =>
3402 -- <exception handler from N_Accept_Statement node>
3403 -- Complete_Rendezvous;
3404 -- ...
3405 -- end;
3407 -- exception
3408 -- when all others =>
3409 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
3410 -- end;
3412 -- The first three declarations were already inserted ahead of the
3413 -- accept statement by the Expand_Accept_Declarations procedure, which
3414 -- was called directly from the semantics during analysis of the accept.
3415 -- statement, before analyzing its contained statements.
3417 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
3418 -- from possible expansion activity (the original source of course does
3419 -- not have any declarations associated with the accept statement, since
3420 -- an accept statement has no declarative part). In particular, if the
3421 -- expander is active, the first such declaration is the declaration of
3422 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
3424 -- The two blocks are merged into a single block if the inner block has
3425 -- no exception handlers, but otherwise two blocks are required, since
3426 -- exceptions might be raised in the exception handlers of the inner
3427 -- block, and Exceptional_Complete_Rendezvous must be called.
3429 procedure Expand_N_Accept_Statement (N : Node_Id) is
3430 Loc : constant Source_Ptr := Sloc (N);
3431 Stats : constant Node_Id := Handled_Statement_Sequence (N);
3432 Ename : constant Node_Id := Entry_Direct_Name (N);
3433 Eindx : constant Node_Id := Entry_Index (N);
3434 Eent : constant Entity_Id := Entity (Ename);
3435 Acstack : constant Elist_Id := Accept_Address (Eent);
3436 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
3437 Ttyp : constant Entity_Id := Etype (Scope (Eent));
3438 Call : Node_Id;
3439 Block : Node_Id;
3441 function Null_Statements (Stats : List_Id) return Boolean;
3442 -- Check for null statement sequence (i.e a list of labels and
3443 -- null statements)
3445 function Null_Statements (Stats : List_Id) return Boolean is
3446 Stmt : Node_Id;
3448 begin
3449 Stmt := First (Stats);
3450 while Nkind (Stmt) /= N_Empty
3451 and then (Nkind (Stmt) = N_Null_Statement
3452 or else
3453 Nkind (Stmt) = N_Label)
3454 loop
3455 Next (Stmt);
3456 end loop;
3458 return Nkind (Stmt) = N_Empty;
3459 end Null_Statements;
3461 -- Start of processing for Expand_N_Accept_Statement
3463 begin
3464 -- If accept statement is not part of a list, then its parent must be
3465 -- an accept alternative, and, as described above, we do not do any
3466 -- expansion for such accept statements at this level.
3468 if not Is_List_Member (N) then
3469 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
3470 return;
3472 -- Trivial accept case (no statement sequence, or null statements).
3473 -- If the accept statement has declarations, then just insert them
3474 -- before the procedure call.
3476 -- We avoid this optimization when FIFO_Within_Priorities is active,
3477 -- since it is not correct according to annex D semantics. The problem
3478 -- is that the call is required to reorder the acceptors position on
3479 -- its ready queue, even though there is nothing to be done. However,
3480 -- if no policy is specified, then we decide that our dispatching
3481 -- policy always reorders the queue right after the RV to look the
3482 -- way they were just before the RV. Since we are allowed to freely
3483 -- reorder same-priority queues (this is part of what dispatching
3484 -- policies are all about), the optimization is legitimate.
3486 elsif Opt.Task_Dispatching_Policy /= 'F'
3487 and then (No (Stats) or else Null_Statements (Statements (Stats)))
3488 then
3489 if Present (Declarations (N)) then
3490 Insert_Actions (N, Declarations (N));
3491 end if;
3493 Rewrite (N,
3494 Make_Procedure_Call_Statement (Loc,
3495 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
3496 Parameter_Associations => New_List (
3497 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
3499 Analyze (N);
3501 -- Discard Entry_Address that was created for it, so it will not be
3502 -- emitted if this accept statement is in the statement part of a
3503 -- delay alternative.
3505 if Present (Stats) then
3506 Remove_Last_Elmt (Acstack);
3507 end if;
3509 -- Case of statement sequence present
3511 else
3512 -- Construct the block, using the declarations from the accept
3513 -- statement if any to initialize the declarations of the block.
3515 Block :=
3516 Make_Block_Statement (Loc,
3517 Declarations => Declarations (N),
3518 Handled_Statement_Sequence => Build_Accept_Body (N));
3520 -- Prepend call to Accept_Call to main statement sequence
3522 Call :=
3523 Make_Procedure_Call_Statement (Loc,
3524 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
3525 Parameter_Associations => New_List (
3526 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
3527 New_Reference_To (Ann, Loc)));
3529 Prepend (Call, Statements (Stats));
3530 Analyze (Call);
3532 -- Replace the accept statement by the new block
3534 Rewrite (N, Block);
3535 Analyze (N);
3537 -- Last step is to unstack the Accept_Address value
3539 Remove_Last_Elmt (Acstack);
3540 end if;
3542 end Expand_N_Accept_Statement;
3544 ----------------------------------
3545 -- Expand_N_Asynchronous_Select --
3546 ----------------------------------
3548 -- This procedure assumes that the trigger statement is an entry
3549 -- call. A delay alternative should already have been expanded
3550 -- into an entry call to the appropriate delay object Wait entry.
3552 -- If the trigger is a task entry call, the select is implemented
3553 -- with Task_Entry_Call:
3555 -- declare
3556 -- B : Boolean;
3557 -- C : Boolean;
3558 -- P : parms := (parm, parm, parm);
3560 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions.
3562 -- procedure _clean is
3563 -- begin
3564 -- ...
3565 -- Cancel_Task_Entry_Call (C);
3566 -- ...
3567 -- end _clean;
3568 -- begin
3569 -- Abort_Defer;
3570 -- Task_Entry_Call
3571 -- (acceptor-task,
3572 -- entry-index,
3573 -- P'Address,
3574 -- Asynchronous_Call,
3575 -- B);
3576 -- begin
3577 -- begin
3578 -- Abort_Undefer;
3579 -- abortable-part
3580 -- at end
3581 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
3582 -- end;
3583 -- exception
3584 -- when Abort_Signal => Abort_Undefer;
3585 -- end;
3586 -- parm := P.param;
3587 -- parm := P.param;
3588 -- ...
3589 -- if not C then
3590 -- triggered-statements
3591 -- end if;
3592 -- end;
3594 -- Note that Build_Simple_Entry_Call is used to expand the entry
3595 -- of the asynchronous entry call (by the
3596 -- Expand_N_Entry_Call_Statement procedure) as follows:
3598 -- declare
3599 -- P : parms := (parm, parm, parm);
3600 -- begin
3601 -- Call_Simple (acceptor-task, entry-index, P'Address);
3602 -- parm := P.param;
3603 -- parm := P.param;
3604 -- ...
3605 -- end;
3607 -- so the task at hand is to convert the latter expansion into the former
3609 -- If the trigger is a protected entry call, the select is
3610 -- implemented with Protected_Entry_Call:
3612 -- declare
3613 -- P : E1_Params := (param, param, param);
3614 -- Bnn : Communications_Block;
3615 -- begin
3616 -- declare
3618 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions.
3620 -- procedure _clean is
3621 -- begin
3622 -- ...
3623 -- if Enqueued (Bnn) then
3624 -- Cancel_Protected_Entry_Call (Bnn);
3625 -- end if;
3626 -- ...
3627 -- end _clean;
3628 -- begin
3629 -- begin
3630 -- Protected_Entry_Call (
3631 -- Object => po._object'Access,
3632 -- E => <entry index>;
3633 -- Uninterpreted_Data => P'Address;
3634 -- Mode => Asynchronous_Call;
3635 -- Block => Bnn);
3636 -- if Enqueued (Bnn) then
3637 -- <abortable part>
3638 -- end if;
3639 -- at end
3640 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
3641 -- end;
3642 -- exception
3643 -- when Abort_Signal =>
3644 -- Abort_Undefer;
3645 -- null;
3646 -- end;
3647 -- if not Cancelled (Bnn) then
3648 -- triggered statements
3649 -- end if;
3650 -- end;
3652 -- Build_Simple_Entry_Call is used to expand the all to a simple
3653 -- protected entry call:
3655 -- declare
3656 -- P : E1_Params := (param, param, param);
3657 -- Bnn : Communications_Block;
3659 -- begin
3660 -- Protected_Entry_Call (
3661 -- Object => po._object'Access,
3662 -- E => <entry index>;
3663 -- Uninterpreted_Data => P'Address;
3664 -- Mode => Simple_Call;
3665 -- Block => Bnn);
3666 -- parm := P.param;
3667 -- parm := P.param;
3668 -- ...
3669 -- end;
3671 -- The job is to convert this to the asynchronous form.
3673 -- If the trigger is a delay statement, it will have been expanded
3674 -- into a call to one of the GNARL delay procedures. This routine
3675 -- will convert this into a protected entry call on a delay object
3676 -- and then continue processing as for a protected entry call trigger.
3677 -- This requires declaring a Delay_Block object and adding a pointer
3678 -- to this object to the parameter list of the delay procedure to form
3679 -- the parameter list of the entry call. This object is used by
3680 -- the runtime to queue the delay request.
3682 -- For a description of the use of P and the assignments after the
3683 -- call, see Expand_N_Entry_Call_Statement.
3685 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
3686 Loc : constant Source_Ptr := Sloc (N);
3687 Trig : constant Node_Id := Triggering_Alternative (N);
3688 Abrt : constant Node_Id := Abortable_Part (N);
3689 Tstats : constant List_Id := Statements (Trig);
3691 Ecall : Node_Id;
3692 Astats : List_Id := Statements (Abrt);
3693 Concval : Node_Id;
3694 Ename : Node_Id;
3695 Index : Node_Id;
3696 Hdle : List_Id;
3697 Decls : List_Id;
3698 Decl : Node_Id;
3699 Parms : List_Id;
3700 Parm : Node_Id;
3701 Call : Node_Id;
3702 Stmts : List_Id;
3703 Enqueue_Call : Node_Id;
3704 Stmt : Node_Id;
3705 B : Entity_Id;
3706 Pdef : Entity_Id;
3707 Dblock_Ent : Entity_Id;
3708 N_Orig : Node_Id;
3709 Abortable_Block : Node_Id;
3710 Cancel_Param : Entity_Id;
3711 Blkent : Entity_Id;
3712 Target_Undefer : RE_Id;
3713 Undefer_Args : List_Id := No_List;
3715 begin
3716 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3717 Ecall := Triggering_Statement (Trig);
3719 -- The arguments in the call may require dynamic allocation, and the
3720 -- call statement may have been transformed into a block. The block
3721 -- may contain additional declarations for internal entities, and the
3722 -- original call is found by sequential search.
3724 if Nkind (Ecall) = N_Block_Statement then
3725 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
3727 while Nkind (Ecall) /= N_Procedure_Call_Statement
3728 and then Nkind (Ecall) /= N_Entry_Call_Statement
3729 loop
3730 Next (Ecall);
3731 end loop;
3732 end if;
3734 -- If a delay was used as a trigger, it will have been expanded
3735 -- into a procedure call. Convert it to the appropriate sequence of
3736 -- statements, similar to what is done for a task entry call.
3737 -- Note that this currently supports only Duration, Real_Time.Time,
3738 -- and Calendar.Time.
3740 if Nkind (Ecall) = N_Procedure_Call_Statement then
3742 -- Add a Delay_Block object to the parameter list of the
3743 -- delay procedure to form the parameter list of the Wait
3744 -- entry call.
3746 Dblock_Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
3748 Pdef := Entity (Name (Ecall));
3750 if Is_RTE (Pdef, RO_CA_Delay_For) then
3751 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
3753 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
3754 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
3756 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
3757 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
3758 end if;
3760 Append_To (Parameter_Associations (Ecall),
3761 Make_Attribute_Reference (Loc,
3762 Prefix => New_Reference_To (Dblock_Ent, Loc),
3763 Attribute_Name => Name_Unchecked_Access));
3765 -- Create the inner block to protect the abortable part.
3767 Hdle := New_List (
3768 Make_Exception_Handler (Loc,
3769 Exception_Choices =>
3770 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
3771 Statements => New_List (
3772 Make_Procedure_Call_Statement (Loc,
3773 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
3775 Prepend_To (Astats,
3776 Make_Procedure_Call_Statement (Loc,
3777 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
3779 Abortable_Block :=
3780 Make_Block_Statement (Loc,
3781 Identifier => New_Reference_To (Blkent, Loc),
3782 Handled_Statement_Sequence =>
3783 Make_Handled_Sequence_Of_Statements (Loc,
3784 Statements => Astats),
3785 Has_Created_Identifier => True,
3786 Is_Asynchronous_Call_Block => True);
3788 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
3790 Rewrite (Ecall,
3791 Make_Implicit_If_Statement (N,
3792 Condition => Make_Function_Call (Loc,
3793 Name => Enqueue_Call,
3794 Parameter_Associations => Parameter_Associations (Ecall)),
3795 Then_Statements =>
3796 New_List (Make_Block_Statement (Loc,
3797 Handled_Statement_Sequence =>
3798 Make_Handled_Sequence_Of_Statements (Loc,
3799 Statements => New_List (
3800 Make_Implicit_Label_Declaration (Loc,
3801 Defining_Identifier => Blkent,
3802 Label_Construct => Abortable_Block),
3803 Abortable_Block),
3804 Exception_Handlers => Hdle)))));
3806 Stmts := New_List (Ecall);
3808 -- Construct statement sequence for new block
3810 Append_To (Stmts,
3811 Make_Implicit_If_Statement (N,
3812 Condition => Make_Function_Call (Loc,
3813 Name => New_Reference_To (
3814 RTE (RE_Timed_Out), Loc),
3815 Parameter_Associations => New_List (
3816 Make_Attribute_Reference (Loc,
3817 Prefix => New_Reference_To (Dblock_Ent, Loc),
3818 Attribute_Name => Name_Unchecked_Access))),
3819 Then_Statements => Tstats));
3821 -- The result is the new block
3823 Set_Entry_Cancel_Parameter (Blkent, Dblock_Ent);
3825 Rewrite (N,
3826 Make_Block_Statement (Loc,
3827 Declarations => New_List (
3828 Make_Object_Declaration (Loc,
3829 Defining_Identifier => Dblock_Ent,
3830 Aliased_Present => True,
3831 Object_Definition => New_Reference_To (
3832 RTE (RE_Delay_Block), Loc))),
3834 Handled_Statement_Sequence =>
3835 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
3837 Analyze (N);
3838 return;
3840 else
3841 N_Orig := N;
3842 end if;
3844 Extract_Entry (Ecall, Concval, Ename, Index);
3845 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
3847 Stmts := Statements (Handled_Statement_Sequence (Ecall));
3848 Decls := Declarations (Ecall);
3850 if Is_Protected_Type (Etype (Concval)) then
3852 -- Get the declarations of the block expanded from the entry call
3854 Decl := First (Decls);
3855 while Present (Decl)
3856 and then (Nkind (Decl) /= N_Object_Declaration
3857 or else not Is_RTE
3858 (Etype (Object_Definition (Decl)), RE_Communication_Block))
3859 loop
3860 Next (Decl);
3861 end loop;
3863 pragma Assert (Present (Decl));
3864 Cancel_Param := Defining_Identifier (Decl);
3866 -- Change the mode of the Protected_Entry_Call call.
3867 -- Protected_Entry_Call (
3868 -- Object => po._object'Access,
3869 -- E => <entry index>;
3870 -- Uninterpreted_Data => P'Address;
3871 -- Mode => Asynchronous_Call;
3872 -- Block => Bnn);
3874 Stmt := First (Stmts);
3876 -- Skip assignments to temporaries created for in-out parameters.
3877 -- This makes unwarranted assumptions about the shape of the expanded
3878 -- tree for the call, and should be cleaned up ???
3880 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
3881 Next (Stmt);
3882 end loop;
3884 Call := Stmt;
3886 Parm := First (Parameter_Associations (Call));
3887 while Present (Parm)
3888 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
3889 loop
3890 Next (Parm);
3891 end loop;
3893 pragma Assert (Present (Parm));
3894 Rewrite (Parm, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
3895 Analyze (Parm);
3897 -- Append an if statement to execute the abortable part.
3898 -- if Enqueued (Bnn) then
3900 Append_To (Stmts,
3901 Make_Implicit_If_Statement (N,
3902 Condition => Make_Function_Call (Loc,
3903 Name => New_Reference_To (
3904 RTE (RE_Enqueued), Loc),
3905 Parameter_Associations => New_List (
3906 New_Reference_To (Cancel_Param, Loc))),
3907 Then_Statements => Astats));
3909 Abortable_Block :=
3910 Make_Block_Statement (Loc,
3911 Identifier => New_Reference_To (Blkent, Loc),
3912 Handled_Statement_Sequence =>
3913 Make_Handled_Sequence_Of_Statements (Loc,
3914 Statements => Stmts),
3915 Has_Created_Identifier => True,
3916 Is_Asynchronous_Call_Block => True);
3918 -- For the JVM call Update_Exception instead of Abort_Undefer.
3919 -- See 4jexcept.ads for an explanation.
3921 if Hostparm.Java_VM then
3922 Target_Undefer := RE_Update_Exception;
3923 Undefer_Args :=
3924 New_List (Make_Function_Call (Loc,
3925 Name => New_Occurrence_Of
3926 (RTE (RE_Current_Target_Exception), Loc)));
3927 else
3928 Target_Undefer := RE_Abort_Undefer;
3929 end if;
3931 Stmts := New_List (
3932 Make_Block_Statement (Loc,
3933 Handled_Statement_Sequence =>
3934 Make_Handled_Sequence_Of_Statements (Loc,
3935 Statements => New_List (
3936 Make_Implicit_Label_Declaration (Loc,
3937 Defining_Identifier => Blkent,
3938 Label_Construct => Abortable_Block),
3939 Abortable_Block),
3941 -- exception
3943 Exception_Handlers => New_List (
3944 Make_Exception_Handler (Loc,
3946 -- when Abort_Signal =>
3947 -- Abort_Undefer.all;
3949 Exception_Choices =>
3950 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
3951 Statements => New_List (
3952 Make_Procedure_Call_Statement (Loc,
3953 Name => New_Reference_To (
3954 RTE (Target_Undefer), Loc),
3955 Parameter_Associations => Undefer_Args)))))),
3957 -- if not Cancelled (Bnn) then
3958 -- triggered statements
3959 -- end if;
3961 Make_Implicit_If_Statement (N,
3962 Condition => Make_Op_Not (Loc,
3963 Right_Opnd =>
3964 Make_Function_Call (Loc,
3965 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
3966 Parameter_Associations => New_List (
3967 New_Occurrence_Of (Cancel_Param, Loc)))),
3968 Then_Statements => Tstats));
3970 -- Asynchronous task entry call
3972 else
3973 if No (Decls) then
3974 Decls := New_List;
3975 end if;
3977 B := Make_Defining_Identifier (Loc, Name_uB);
3979 -- Insert declaration of B in declarations of existing block
3981 Prepend_To (Decls,
3982 Make_Object_Declaration (Loc,
3983 Defining_Identifier => B,
3984 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
3986 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
3988 -- Insert declaration of C in declarations of existing block
3990 Prepend_To (Decls,
3991 Make_Object_Declaration (Loc,
3992 Defining_Identifier => Cancel_Param,
3993 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
3995 -- Remove and save the call to Call_Simple.
3997 Stmt := First (Stmts);
3999 -- Skip assignments to temporaries created for in-out parameters.
4000 -- This makes unwarranted assumptions about the shape of the expanded
4001 -- tree for the call, and should be cleaned up ???
4003 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4004 Next (Stmt);
4005 end loop;
4007 Call := Stmt;
4009 -- Create the inner block to protect the abortable part.
4011 Hdle := New_List (
4012 Make_Exception_Handler (Loc,
4013 Exception_Choices =>
4014 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4015 Statements => New_List (
4016 Make_Procedure_Call_Statement (Loc,
4017 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
4019 Prepend_To (Astats,
4020 Make_Procedure_Call_Statement (Loc,
4021 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
4023 Abortable_Block :=
4024 Make_Block_Statement (Loc,
4025 Identifier => New_Reference_To (Blkent, Loc),
4026 Handled_Statement_Sequence =>
4027 Make_Handled_Sequence_Of_Statements (Loc,
4028 Statements => Astats),
4029 Has_Created_Identifier => True,
4030 Is_Asynchronous_Call_Block => True);
4032 Insert_After (Call,
4033 Make_Block_Statement (Loc,
4034 Handled_Statement_Sequence =>
4035 Make_Handled_Sequence_Of_Statements (Loc,
4036 Statements => New_List (
4037 Make_Implicit_Label_Declaration (Loc,
4038 Defining_Identifier => Blkent,
4039 Label_Construct => Abortable_Block),
4040 Abortable_Block),
4041 Exception_Handlers => Hdle)));
4043 -- Create new call statement
4045 Parms := Parameter_Associations (Call);
4046 Append_To (Parms, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
4047 Append_To (Parms, New_Reference_To (B, Loc));
4048 Rewrite (Call,
4049 Make_Procedure_Call_Statement (Loc,
4050 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4051 Parameter_Associations => Parms));
4053 -- Construct statement sequence for new block
4055 Append_To (Stmts,
4056 Make_Implicit_If_Statement (N,
4057 Condition => Make_Op_Not (Loc,
4058 New_Reference_To (Cancel_Param, Loc)),
4059 Then_Statements => Tstats));
4061 -- Protected the call against abortion
4063 Prepend_To (Stmts,
4064 Make_Procedure_Call_Statement (Loc,
4065 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4066 Parameter_Associations => Empty_List));
4067 end if;
4069 Set_Entry_Cancel_Parameter (Blkent, Cancel_Param);
4071 -- The result is the new block
4073 Rewrite (N_Orig,
4074 Make_Block_Statement (Loc,
4075 Declarations => Decls,
4076 Handled_Statement_Sequence =>
4077 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4079 Analyze (N_Orig);
4081 end Expand_N_Asynchronous_Select;
4083 -------------------------------------
4084 -- Expand_N_Conditional_Entry_Call --
4085 -------------------------------------
4087 -- The conditional task entry call is converted to a call to
4088 -- Task_Entry_Call:
4090 -- declare
4091 -- B : Boolean;
4092 -- P : parms := (parm, parm, parm);
4094 -- begin
4095 -- Task_Entry_Call
4096 -- (acceptor-task,
4097 -- entry-index,
4098 -- P'Address,
4099 -- Conditional_Call,
4100 -- B);
4101 -- parm := P.param;
4102 -- parm := P.param;
4103 -- ...
4104 -- if B then
4105 -- normal-statements
4106 -- else
4107 -- else-statements
4108 -- end if;
4109 -- end;
4111 -- For a description of the use of P and the assignments after the
4112 -- call, see Expand_N_Entry_Call_Statement. Note that the entry call
4113 -- of the conditional entry call has already been expanded (by the
4114 -- Expand_N_Entry_Call_Statement procedure) as follows:
4116 -- declare
4117 -- P : parms := (parm, parm, parm);
4118 -- begin
4119 -- ... info for in-out parameters
4120 -- Call_Simple (acceptor-task, entry-index, P'Address);
4121 -- parm := P.param;
4122 -- parm := P.param;
4123 -- ...
4124 -- end;
4126 -- so the task at hand is to convert the latter expansion into the former
4128 -- The conditional protected entry call is converted to a call to
4129 -- Protected_Entry_Call:
4131 -- declare
4132 -- P : parms := (parm, parm, parm);
4133 -- Bnn : Communications_Block;
4135 -- begin
4136 -- Protected_Entry_Call (
4137 -- Object => po._object'Access,
4138 -- E => <entry index>;
4139 -- Uninterpreted_Data => P'Address;
4140 -- Mode => Conditional_Call;
4141 -- Block => Bnn);
4142 -- parm := P.param;
4143 -- parm := P.param;
4144 -- ...
4145 -- if Cancelled (Bnn) then
4146 -- else-statements
4147 -- else
4148 -- normal-statements
4149 -- end if;
4150 -- end;
4152 -- As for tasks, the entry call of the conditional entry call has
4153 -- already been expanded (by the Expand_N_Entry_Call_Statement procedure)
4154 -- as follows:
4156 -- declare
4157 -- P : E1_Params := (param, param, param);
4158 -- Bnn : Communications_Block;
4160 -- begin
4161 -- Protected_Entry_Call (
4162 -- Object => po._object'Access,
4163 -- E => <entry index>;
4164 -- Uninterpreted_Data => P'Address;
4165 -- Mode => Simple_Call;
4166 -- Block => Bnn);
4167 -- parm := P.param;
4168 -- parm := P.param;
4169 -- ...
4170 -- end;
4172 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
4173 Loc : constant Source_Ptr := Sloc (N);
4174 Alt : constant Node_Id := Entry_Call_Alternative (N);
4175 Blk : Node_Id := Entry_Call_Statement (Alt);
4176 Transient_Blk : Node_Id;
4178 Parms : List_Id;
4179 Parm : Node_Id;
4180 Call : Node_Id;
4181 Stmts : List_Id;
4182 B : Entity_Id;
4183 Decl : Node_Id;
4184 Stmt : Node_Id;
4186 begin
4187 -- As described above, The entry alternative is transformed into a
4188 -- block that contains the gnulli call, and possibly assignment
4189 -- statements for in-out parameters. The gnulli call may itself be
4190 -- rewritten into a transient block if some unconstrained parameters
4191 -- require it. We need to retrieve the call to complete its parameter
4192 -- list.
4194 Transient_Blk :=
4195 First_Real_Statement (Handled_Statement_Sequence (Blk));
4197 if Present (Transient_Blk)
4198 and then
4199 Nkind (Transient_Blk) = N_Block_Statement
4200 then
4201 Blk := Transient_Blk;
4202 end if;
4204 Stmts := Statements (Handled_Statement_Sequence (Blk));
4206 Stmt := First (Stmts);
4208 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4209 Next (Stmt);
4210 end loop;
4212 Call := Stmt;
4214 Parms := Parameter_Associations (Call);
4216 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
4218 -- Substitute Conditional_Entry_Call for Simple_Call
4219 -- parameter.
4221 Parm := First (Parms);
4222 while Present (Parm)
4223 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
4224 loop
4225 Next (Parm);
4226 end loop;
4228 pragma Assert (Present (Parm));
4229 Rewrite (Parm, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4231 Analyze (Parm);
4233 -- Find the Communication_Block parameter for the call
4234 -- to the Cancelled function.
4236 Decl := First (Declarations (Blk));
4237 while Present (Decl)
4238 and then not
4239 Is_RTE (Etype (Object_Definition (Decl)), RE_Communication_Block)
4240 loop
4241 Next (Decl);
4242 end loop;
4244 -- Add an if statement to execute the else part if the call
4245 -- does not succeed (as indicated by the Cancelled predicate).
4247 Append_To (Stmts,
4248 Make_Implicit_If_Statement (N,
4249 Condition => Make_Function_Call (Loc,
4250 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
4251 Parameter_Associations => New_List (
4252 New_Reference_To (Defining_Identifier (Decl), Loc))),
4253 Then_Statements => Else_Statements (N),
4254 Else_Statements => Statements (Alt)));
4256 else
4257 B := Make_Defining_Identifier (Loc, Name_uB);
4259 -- Insert declaration of B in declarations of existing block
4261 if No (Declarations (Blk)) then
4262 Set_Declarations (Blk, New_List);
4263 end if;
4265 Prepend_To (Declarations (Blk),
4266 Make_Object_Declaration (Loc,
4267 Defining_Identifier => B,
4268 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4270 -- Create new call statement
4272 Append_To (Parms, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4273 Append_To (Parms, New_Reference_To (B, Loc));
4275 Rewrite (Call,
4276 Make_Procedure_Call_Statement (Loc,
4277 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4278 Parameter_Associations => Parms));
4280 -- Construct statement sequence for new block
4282 Append_To (Stmts,
4283 Make_Implicit_If_Statement (N,
4284 Condition => New_Reference_To (B, Loc),
4285 Then_Statements => Statements (Alt),
4286 Else_Statements => Else_Statements (N)));
4288 end if;
4290 -- The result is the new block
4292 Rewrite (N,
4293 Make_Block_Statement (Loc,
4294 Declarations => Declarations (Blk),
4295 Handled_Statement_Sequence =>
4296 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4298 Analyze (N);
4300 end Expand_N_Conditional_Entry_Call;
4302 ---------------------------------------
4303 -- Expand_N_Delay_Relative_Statement --
4304 ---------------------------------------
4306 -- Delay statement is implemented as a procedure call to Delay_For
4307 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
4308 -- simple delays imposed by the use of Protected Objects.
4310 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
4311 Loc : constant Source_Ptr := Sloc (N);
4313 begin
4314 Rewrite (N,
4315 Make_Procedure_Call_Statement (Loc,
4316 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
4317 Parameter_Associations => New_List (Expression (N))));
4318 Analyze (N);
4319 end Expand_N_Delay_Relative_Statement;
4321 ------------------------------------
4322 -- Expand_N_Delay_Until_Statement --
4323 ------------------------------------
4325 -- Delay Until statement is implemented as a procedure call to
4326 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
4328 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
4329 Loc : constant Source_Ptr := Sloc (N);
4330 Typ : Entity_Id;
4332 begin
4333 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
4334 Typ := RTE (RO_CA_Delay_Until);
4335 else
4336 Typ := RTE (RO_RT_Delay_Until);
4337 end if;
4339 Rewrite (N,
4340 Make_Procedure_Call_Statement (Loc,
4341 Name => New_Reference_To (Typ, Loc),
4342 Parameter_Associations => New_List (Expression (N))));
4344 Analyze (N);
4345 end Expand_N_Delay_Until_Statement;
4347 -------------------------
4348 -- Expand_N_Entry_Body --
4349 -------------------------
4351 procedure Expand_N_Entry_Body (N : Node_Id) is
4352 Loc : constant Source_Ptr := Sloc (N);
4353 Next_Op : Node_Id;
4354 Dec : Node_Id := Parent (Current_Scope);
4355 Ent_Formals : Node_Id := Entry_Body_Formal_Part (N);
4356 Index_Spec : Node_Id := Entry_Index_Specification (Ent_Formals);
4358 begin
4359 -- Add the renamings for private declarations and discriminants.
4361 Add_Discriminal_Declarations
4362 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4363 Add_Private_Declarations
4364 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4366 if Present (Index_Spec) then
4367 Append_List_To (Declarations (N),
4368 Index_Constant_Declaration
4369 (N, Defining_Identifier (Index_Spec), Defining_Identifier (Dec)));
4370 end if;
4372 -- Associate privals and discriminals with the next protected
4373 -- operation body to be expanded. These are used to expand
4374 -- references to private data objects and discriminants,
4375 -- respectively.
4377 Next_Op := Next_Protected_Operation (N);
4379 if Present (Next_Op) then
4380 Set_Privals (Dec, Next_Op, Loc);
4381 Set_Discriminals (Dec);
4382 end if;
4384 end Expand_N_Entry_Body;
4386 -----------------------------------
4387 -- Expand_N_Entry_Call_Statement --
4388 -----------------------------------
4390 -- An entry call is expanded into GNARLI calls to implement
4391 -- a simple entry call (see Build_Simple_Entry_Call).
4393 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
4394 Concval : Node_Id;
4395 Ename : Node_Id;
4396 Index : Node_Id;
4398 begin
4399 -- If this entry call is part of an asynchronous select, don't
4400 -- expand it here; it will be expanded with the select statement.
4401 -- Don't expand timed entry calls either, as they are translated
4402 -- into asynchronous entry calls.
4404 -- ??? This whole approach is questionable; it may be better
4405 -- to go back to allowing the expansion to take place and then
4406 -- attempting to fix it up in Expand_N_Asynchronous_Select.
4407 -- The tricky part is figuring out whether the expanded
4408 -- call is on a task or protected entry.
4410 if (Nkind (Parent (N)) /= N_Triggering_Alternative
4411 or else N /= Triggering_Statement (Parent (N)))
4412 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
4413 or else N /= Entry_Call_Statement (Parent (N))
4414 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
4415 then
4416 Extract_Entry (N, Concval, Ename, Index);
4417 Build_Simple_Entry_Call (N, Concval, Ename, Index);
4418 end if;
4420 end Expand_N_Entry_Call_Statement;
4422 --------------------------------
4423 -- Expand_N_Entry_Declaration --
4424 --------------------------------
4426 -- If there are parameters, then first, each of the formals is marked
4427 -- by setting Is_Entry_Formal. Next a record type is built which is
4428 -- used to hold the parameter values. The name of this record type is
4429 -- entryP where entry is the name of the entry, with an additional
4430 -- corresponding access type called entryPA. The record type has matching
4431 -- components for each formal (the component names are the same as the
4432 -- formal names). For elementary types, the component type matches the
4433 -- formal type. For composite types, an access type is declared (with
4434 -- the name formalA) which designates the formal type, and the type of
4435 -- the component is this access type. Finally the Entry_Component of
4436 -- each formal is set to reference the corresponding record component.
4438 procedure Expand_N_Entry_Declaration (N : Node_Id) is
4439 Loc : constant Source_Ptr := Sloc (N);
4440 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
4441 Components : List_Id;
4442 Formal : Node_Id;
4443 Ftype : Entity_Id;
4444 Last_Decl : Node_Id;
4445 Component : Entity_Id;
4446 Ctype : Entity_Id;
4447 Decl : Node_Id;
4448 Rec_Ent : Entity_Id;
4449 Acc_Ent : Entity_Id;
4451 begin
4452 Formal := First_Formal (Entry_Ent);
4453 Last_Decl := N;
4455 -- Most processing is done only if parameters are present
4457 if Present (Formal) then
4458 Components := New_List;
4460 -- Loop through formals
4462 while Present (Formal) loop
4463 Set_Is_Entry_Formal (Formal);
4464 Component :=
4465 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
4466 Set_Entry_Component (Formal, Component);
4467 Set_Entry_Formal (Component, Formal);
4468 Ftype := Etype (Formal);
4470 -- Declare new access type and then append
4472 Ctype :=
4473 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4475 Decl :=
4476 Make_Full_Type_Declaration (Loc,
4477 Defining_Identifier => Ctype,
4478 Type_Definition =>
4479 Make_Access_To_Object_Definition (Loc,
4480 All_Present => True,
4481 Constant_Present => Ekind (Formal) = E_In_Parameter,
4482 Subtype_Indication => New_Reference_To (Ftype, Loc)));
4484 Insert_After (Last_Decl, Decl);
4485 Last_Decl := Decl;
4487 Append_To (Components,
4488 Make_Component_Declaration (Loc,
4489 Defining_Identifier => Component,
4490 Subtype_Indication => New_Reference_To (Ctype, Loc)));
4492 Next_Formal_With_Extras (Formal);
4493 end loop;
4495 -- Create the Entry_Parameter_Record declaration
4497 Rec_Ent :=
4498 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
4500 Decl :=
4501 Make_Full_Type_Declaration (Loc,
4502 Defining_Identifier => Rec_Ent,
4503 Type_Definition =>
4504 Make_Record_Definition (Loc,
4505 Component_List =>
4506 Make_Component_List (Loc,
4507 Component_Items => Components)));
4509 Insert_After (Last_Decl, Decl);
4510 Last_Decl := Decl;
4512 -- Construct and link in the corresponding access type
4514 Acc_Ent :=
4515 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4517 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
4519 Decl :=
4520 Make_Full_Type_Declaration (Loc,
4521 Defining_Identifier => Acc_Ent,
4522 Type_Definition =>
4523 Make_Access_To_Object_Definition (Loc,
4524 All_Present => True,
4525 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
4527 Insert_After (Last_Decl, Decl);
4528 Last_Decl := Decl;
4530 end if;
4532 end Expand_N_Entry_Declaration;
4534 -----------------------------
4535 -- Expand_N_Protected_Body --
4536 -----------------------------
4538 -- Protected bodies are expanded to the completion of the subprograms
4539 -- created for the corresponding protected type. These are a protected
4540 -- and unprotected version of each protected subprogram in the object,
4541 -- a function to calculate each entry barrier, and a procedure to
4542 -- execute the sequence of statements of each protected entry body.
4543 -- For example, for protected type ptype:
4545 -- function entB
4546 -- (O : System.Address;
4547 -- E : Protected_Entry_Index)
4548 -- return Boolean
4549 -- is
4550 -- <discriminant renamings>
4551 -- <private object renamings>
4552 -- begin
4553 -- return <barrier expression>;
4554 -- end entB;
4556 -- procedure pprocN (_object : in out poV;...) is
4557 -- <discriminant renamings>
4558 -- <private object renamings>
4559 -- begin
4560 -- <sequence of statements>
4561 -- end pprocN;
4563 -- procedure pproc (_object : in out poV;...) is
4564 -- procedure _clean is
4565 -- Pn : Boolean;
4566 -- begin
4567 -- ptypeS (_object, Pn);
4568 -- Unlock (_object._object'Access);
4569 -- Abort_Undefer.all;
4570 -- end _clean;
4571 -- begin
4572 -- Abort_Defer.all;
4573 -- Lock (_object._object'Access);
4574 -- pprocN (_object;...);
4575 -- at end
4576 -- _clean;
4577 -- end pproc;
4579 -- function pfuncN (_object : poV;...) return Return_Type is
4580 -- <discriminant renamings>
4581 -- <private object renamings>
4582 -- begin
4583 -- <sequence of statements>
4584 -- end pfuncN;
4586 -- function pfunc (_object : poV) return Return_Type is
4587 -- procedure _clean is
4588 -- begin
4589 -- Unlock (_object._object'Access);
4590 -- Abort_Undefer.all;
4591 -- end _clean;
4592 -- begin
4593 -- Abort_Defer.all;
4594 -- Lock (_object._object'Access);
4595 -- return pfuncN (_object);
4596 -- at end
4597 -- _clean;
4598 -- end pfunc;
4600 -- procedure entE
4601 -- (O : System.Address;
4602 -- P : System.Address;
4603 -- E : Protected_Entry_Index)
4604 -- is
4605 -- <discriminant renamings>
4606 -- <private object renamings>
4607 -- type poVP is access poV;
4608 -- _Object : ptVP := ptVP!(O);
4609 -- begin
4610 -- begin
4611 -- <statement sequence>
4612 -- Complete_Entry_Body (_Object._Object);
4613 -- exception
4614 -- when all others =>
4615 -- Exceptional_Complete_Entry_Body (
4616 -- _Object._Object, Get_GNAT_Exception);
4617 -- end;
4618 -- end entE;
4620 -- The type poV is the record created for the protected type to hold
4621 -- the state of the protected object.
4623 procedure Expand_N_Protected_Body (N : Node_Id) is
4624 Pid : constant Entity_Id := Corresponding_Spec (N);
4625 Has_Entries : Boolean := False;
4626 Op_Decl : Node_Id;
4627 Op_Body : Node_Id;
4628 Op_Id : Entity_Id;
4629 New_Op_Body : Node_Id;
4630 Current_Node : Node_Id;
4631 Num_Entries : Natural := 0;
4633 begin
4634 if Nkind (Parent (N)) = N_Subunit then
4636 -- This is the proper body corresponding to a stub. The declarations
4637 -- must be inserted at the point of the stub, which is in the decla-
4638 -- rative part of the parent unit.
4640 Current_Node := Corresponding_Stub (Parent (N));
4642 else
4643 Current_Node := N;
4644 end if;
4646 Op_Body := First (Declarations (N));
4648 -- The protected body is replaced with the bodies of its
4649 -- protected operations, and the declarations for internal objects
4650 -- that may have been created for entry family bounds.
4652 Rewrite (N, Make_Null_Statement (Sloc (N)));
4653 Analyze (N);
4655 while Present (Op_Body) loop
4657 case Nkind (Op_Body) is
4658 when N_Subprogram_Declaration =>
4659 null;
4661 when N_Subprogram_Body =>
4663 -- Exclude functions created to analyze defaults.
4665 if not Is_Eliminated (Defining_Entity (Op_Body)) then
4666 New_Op_Body :=
4667 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
4669 Insert_After (Current_Node, New_Op_Body);
4670 Current_Node := New_Op_Body;
4671 Analyze (New_Op_Body);
4673 Update_Prival_Subtypes (New_Op_Body);
4675 -- Build the corresponding protected operation only if
4676 -- this is a visible operation of the type, or if it is
4677 -- an interrupt handler. Otherwise it is only callable
4678 -- from within the object, and the unprotected version
4679 -- is sufficient.
4681 if Present (Corresponding_Spec (Op_Body)) then
4682 Op_Decl :=
4683 Unit_Declaration_Node (Corresponding_Spec (Op_Body));
4685 if Nkind (Parent (Op_Decl)) = N_Protected_Definition
4686 and then
4687 (List_Containing (Op_Decl) =
4688 Visible_Declarations (Parent (Op_Decl))
4689 or else
4690 Is_Interrupt_Handler
4691 (Corresponding_Spec (Op_Body)))
4692 then
4693 New_Op_Body :=
4694 Build_Protected_Subprogram_Body (
4695 Op_Body, Pid, Specification (New_Op_Body));
4697 Insert_After (Current_Node, New_Op_Body);
4698 Analyze (New_Op_Body);
4699 end if;
4700 end if;
4701 end if;
4703 when N_Entry_Body =>
4704 Op_Id := Defining_Identifier (Op_Body);
4705 Has_Entries := True;
4706 Num_Entries := Num_Entries + 1;
4708 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
4710 Insert_After (Current_Node, New_Op_Body);
4711 Current_Node := New_Op_Body;
4712 Analyze (New_Op_Body);
4714 Update_Prival_Subtypes (New_Op_Body);
4716 when N_Implicit_Label_Declaration =>
4717 null;
4719 when N_Itype_Reference =>
4720 Insert_After (Current_Node, New_Copy (Op_Body));
4722 when N_Freeze_Entity =>
4723 New_Op_Body := New_Copy (Op_Body);
4725 if Present (Entity (Op_Body))
4726 and then Freeze_Node (Entity (Op_Body)) = Op_Body
4727 then
4728 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
4729 end if;
4731 Insert_After (Current_Node, New_Op_Body);
4732 Current_Node := New_Op_Body;
4733 Analyze (New_Op_Body);
4735 when N_Pragma =>
4736 New_Op_Body := New_Copy (Op_Body);
4737 Insert_After (Current_Node, New_Op_Body);
4738 Current_Node := New_Op_Body;
4739 Analyze (New_Op_Body);
4741 when N_Object_Declaration =>
4742 pragma Assert (not Comes_From_Source (Op_Body));
4743 New_Op_Body := New_Copy (Op_Body);
4744 Insert_After (Current_Node, New_Op_Body);
4745 Current_Node := New_Op_Body;
4746 Analyze (New_Op_Body);
4748 when others =>
4749 raise Program_Error;
4751 end case;
4753 Next (Op_Body);
4754 end loop;
4756 -- Finally, create the body of the function that maps an entry index
4757 -- into the corresponding body index, except when there is no entry,
4758 -- or in a ravenscar-like profile (no abort, no entry queue, 1 entry)
4760 if Has_Entries
4761 and then (Abort_Allowed
4762 or else Restrictions (No_Entry_Queue) = False
4763 or else Num_Entries > 1)
4764 then
4765 New_Op_Body := Build_Find_Body_Index (Pid);
4766 Insert_After (Current_Node, New_Op_Body);
4767 Analyze (New_Op_Body);
4768 end if;
4769 end Expand_N_Protected_Body;
4771 -----------------------------------------
4772 -- Expand_N_Protected_Type_Declaration --
4773 -----------------------------------------
4775 -- First we create a corresponding record type declaration used to
4776 -- represent values of this protected type.
4777 -- The general form of this type declaration is
4779 -- type poV (discriminants) is record
4780 -- _Object : aliased <kind>Protection
4781 -- [(<entry count> [, <handler count>])];
4782 -- [entry_family : array (bounds) of Void;]
4783 -- <private data fields>
4784 -- end record;
4786 -- The discriminants are present only if the corresponding protected
4787 -- type has discriminants, and they exactly mirror the protected type
4788 -- discriminants. The private data fields similarly mirror the
4789 -- private declarations of the protected type.
4791 -- The Object field is always present. It contains RTS specific data
4792 -- used to control the protected object. It is declared as Aliased
4793 -- so that it can be passed as a pointer to the RTS. This allows the
4794 -- protected record to be referenced within RTS data structures.
4795 -- An appropriate Protection type and discriminant are generated.
4797 -- The Service field is present for protected objects with entries. It
4798 -- contains sufficient information to allow the entry service procedure
4799 -- for this object to be called when the object is not known till runtime.
4801 -- One entry_family component is present for each entry family in the
4802 -- task definition (see Expand_N_Task_Type_Declaration).
4804 -- When a protected object is declared, an instance of the protected type
4805 -- value record is created. The elaboration of this declaration creates
4806 -- the correct bounds for the entry families, and also evaluates the
4807 -- priority expression if needed. The initialization routine for
4808 -- the protected type itself then calls Initialize_Protection with
4809 -- appropriate parameters to initialize the value of the Task_Id field.
4810 -- Install_Handlers may be also called if a pragma Attach_Handler applies.
4812 -- Note: this record is passed to the subprograms created by the
4813 -- expansion of protected subprograms and entries. It is an in parameter
4814 -- to protected functions and an in out parameter to procedures and
4815 -- entry bodies. The Entity_Id for this created record type is placed
4816 -- in the Corresponding_Record_Type field of the associated protected
4817 -- type entity.
4819 -- Next we create a procedure specifications for protected subprograms
4820 -- and entry bodies. For each protected subprograms two subprograms are
4821 -- created, an unprotected and a protected version. The unprotected
4822 -- version is called from within other operations of the same protected
4823 -- object.
4825 -- We also build the call to register the procedure if a pragma
4826 -- Interrupt_Handler applies.
4828 -- A single subprogram is created to service all entry bodies; it has an
4829 -- additional boolean out parameter indicating that the previous entry
4830 -- call made by the current task was serviced immediately, i.e. not by
4831 -- proxy. The O parameter contains a pointer to a record object of the
4832 -- type described above. An untyped interface is used here to allow this
4833 -- procedure to be called in places where the type of the object to be
4834 -- serviced is not known. This must be done, for example, when a call
4835 -- that may have been requeued is cancelled; the corresponding object
4836 -- must be serviced, but which object that is not known till runtime.
4838 -- procedure ptypeS
4839 -- (O : System.Address; P : out Boolean);
4840 -- procedure pprocN (_object : in out poV);
4841 -- procedure pproc (_object : in out poV);
4842 -- function pfuncN (_object : poV);
4843 -- function pfunc (_object : poV);
4844 -- ...
4846 -- Note that this must come after the record type declaration, since
4847 -- the specs refer to this type.
4849 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
4850 Loc : constant Source_Ptr := Sloc (N);
4851 Prottyp : constant Entity_Id := Defining_Identifier (N);
4852 Protnm : constant Name_Id := Chars (Prottyp);
4854 Pdef : constant Node_Id := Protected_Definition (N);
4855 -- This contains two lists; one for visible and one for private decls
4857 Rec_Decl : Node_Id;
4858 Cdecls : List_Id;
4859 Discr_Map : Elist_Id := New_Elmt_List;
4860 Priv : Node_Id;
4861 Pent : Entity_Id;
4862 New_Priv : Node_Id;
4863 Comp : Node_Id;
4864 Comp_Id : Entity_Id;
4865 Sub : Node_Id;
4866 Current_Node : Node_Id := N;
4867 Nam : Name_Id;
4868 Bdef : Entity_Id := Empty; -- avoid uninit warning
4869 Edef : Entity_Id := Empty; -- avoid uninit warning
4870 Entries_Aggr : Node_Id;
4871 Body_Id : Entity_Id;
4872 Body_Arr : Node_Id;
4873 E_Count : Int;
4874 Object_Comp : Node_Id;
4876 procedure Register_Handler;
4877 -- for a protected operation that is an interrupt handler, add the
4878 -- freeze action that will register it as such.
4880 ----------------------
4881 -- Register_Handler --
4882 ----------------------
4884 procedure Register_Handler is
4886 -- All semantic checks already done in Sem_Prag
4888 Prot_Proc : constant Entity_Id :=
4889 Defining_Unit_Name
4890 (Specification (Current_Node));
4892 Proc_Address : constant Node_Id :=
4893 Make_Attribute_Reference (Loc,
4894 Prefix => New_Reference_To (Prot_Proc, Loc),
4895 Attribute_Name => Name_Address);
4897 RTS_Call : constant Entity_Id :=
4898 Make_Procedure_Call_Statement (Loc,
4899 Name =>
4900 New_Reference_To (
4901 RTE (RE_Register_Interrupt_Handler), Loc),
4902 Parameter_Associations =>
4903 New_List (Proc_Address));
4904 begin
4905 Append_Freeze_Action (Prot_Proc, RTS_Call);
4906 end Register_Handler;
4908 -- Start of processing for Expand_N_Protected_Type_Declaration
4910 begin
4911 if Present (Corresponding_Record_Type (Prottyp)) then
4912 return;
4913 else
4914 Rec_Decl := Build_Corresponding_Record (N, Prottyp, Loc);
4915 Cdecls := Component_Items
4916 (Component_List (Type_Definition (Rec_Decl)));
4917 end if;
4919 Qualify_Entity_Names (N);
4921 -- If the type has discriminants, their occurrences in the declaration
4922 -- have been replaced by the corresponding discriminals. For components
4923 -- that are constrained by discriminants, their homologues in the
4924 -- corresponding record type must refer to the discriminants of that
4925 -- record, so we must apply a new renaming to subtypes_indications:
4927 -- protected discriminant => discriminal => record discriminant.
4928 -- This replacement is not applied to default expressions, for which
4929 -- the discriminal is correct.
4931 if Has_Discriminants (Prottyp) then
4932 declare
4933 Disc : Entity_Id;
4934 Decl : Node_Id;
4936 begin
4937 Disc := First_Discriminant (Prottyp);
4938 Decl := First (Discriminant_Specifications (Rec_Decl));
4940 while Present (Disc) loop
4941 Append_Elmt (Discriminal (Disc), Discr_Map);
4942 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
4943 Next_Discriminant (Disc);
4944 Next (Decl);
4945 end loop;
4946 end;
4947 end if;
4949 -- Fill in the component declarations.
4951 -- Add components for entry families. For each entry family,
4952 -- create an anonymous type declaration with the same size, and
4953 -- analyze the type.
4955 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prottyp);
4957 -- Prepend the _Object field with the right type to the component
4958 -- list. We need to compute the number of entries, and in some cases
4959 -- the number of Attach_Handler pragmas.
4961 declare
4962 Ritem : Node_Id;
4963 Num_Attach_Handler : Int := 0;
4964 Protection_Subtype : Node_Id;
4965 Entry_Count_Expr : constant Node_Id :=
4966 Build_Entry_Count_Expression
4967 (Prottyp, Cdecls, Loc);
4969 begin
4970 if Has_Attach_Handler (Prottyp) then
4971 Ritem := First_Rep_Item (Prottyp);
4972 while Present (Ritem) loop
4973 if Nkind (Ritem) = N_Pragma
4974 and then Chars (Ritem) = Name_Attach_Handler
4975 then
4976 Num_Attach_Handler := Num_Attach_Handler + 1;
4977 end if;
4979 Next_Rep_Item (Ritem);
4980 end loop;
4982 if Restricted_Profile then
4983 Protection_Subtype :=
4984 New_Reference_To (RTE (RE_Protection_Entry), Loc);
4986 else
4987 Protection_Subtype :=
4988 Make_Subtype_Indication
4989 (Sloc => Loc,
4990 Subtype_Mark =>
4991 New_Reference_To
4992 (RTE (RE_Static_Interrupt_Protection), Loc),
4993 Constraint =>
4994 Make_Index_Or_Discriminant_Constraint (
4995 Sloc => Loc,
4996 Constraints => New_List (
4997 Entry_Count_Expr,
4998 Make_Integer_Literal (Loc, Num_Attach_Handler))));
4999 end if;
5001 elsif Has_Interrupt_Handler (Prottyp) then
5002 Protection_Subtype :=
5003 Make_Subtype_Indication (
5004 Sloc => Loc,
5005 Subtype_Mark => New_Reference_To
5006 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
5007 Constraint =>
5008 Make_Index_Or_Discriminant_Constraint (
5009 Sloc => Loc,
5010 Constraints => New_List (Entry_Count_Expr)));
5012 elsif Has_Entries (Prottyp) then
5013 if Abort_Allowed
5014 or else Restrictions (No_Entry_Queue) = False
5015 or else Number_Entries (Prottyp) > 1
5016 then
5017 Protection_Subtype :=
5018 Make_Subtype_Indication (
5019 Sloc => Loc,
5020 Subtype_Mark =>
5021 New_Reference_To (RTE (RE_Protection_Entries), Loc),
5022 Constraint =>
5023 Make_Index_Or_Discriminant_Constraint (
5024 Sloc => Loc,
5025 Constraints => New_List (Entry_Count_Expr)));
5027 else
5028 Protection_Subtype :=
5029 New_Reference_To (RTE (RE_Protection_Entry), Loc);
5030 end if;
5032 else
5033 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
5034 end if;
5036 Object_Comp :=
5037 Make_Component_Declaration (Loc,
5038 Defining_Identifier =>
5039 Make_Defining_Identifier (Loc, Name_uObject),
5040 Aliased_Present => True,
5041 Subtype_Indication => Protection_Subtype);
5042 end;
5044 pragma Assert (Present (Pdef));
5046 -- Add private field components.
5048 if Present (Private_Declarations (Pdef)) then
5049 Priv := First (Private_Declarations (Pdef));
5051 while Present (Priv) loop
5053 if Nkind (Priv) = N_Component_Declaration then
5054 Pent := Defining_Identifier (Priv);
5055 New_Priv :=
5056 Make_Component_Declaration (Loc,
5057 Defining_Identifier =>
5058 Make_Defining_Identifier (Sloc (Pent), Chars (Pent)),
5059 Subtype_Indication =>
5060 New_Copy_Tree (Subtype_Indication (Priv), Discr_Map),
5061 Expression => Expression (Priv));
5063 Append_To (Cdecls, New_Priv);
5065 elsif Nkind (Priv) = N_Subprogram_Declaration then
5067 -- Make the unprotected version of the subprogram available
5068 -- for expansion of intra object calls. There is need for
5069 -- a protected version only if the subprogram is an interrupt
5070 -- handler, otherwise this operation can only be called from
5071 -- within the body.
5073 Sub :=
5074 Make_Subprogram_Declaration (Loc,
5075 Specification =>
5076 Build_Protected_Sub_Specification
5077 (Priv, Prottyp, Unprotected => True));
5079 Insert_After (Current_Node, Sub);
5080 Analyze (Sub);
5082 Set_Protected_Body_Subprogram
5083 (Defining_Unit_Name (Specification (Priv)),
5084 Defining_Unit_Name (Specification (Sub)));
5086 Current_Node := Sub;
5087 if Is_Interrupt_Handler
5088 (Defining_Unit_Name (Specification (Priv)))
5089 then
5090 Sub :=
5091 Make_Subprogram_Declaration (Loc,
5092 Specification =>
5093 Build_Protected_Sub_Specification
5094 (Priv, Prottyp, Unprotected => False));
5096 Insert_After (Current_Node, Sub);
5097 Analyze (Sub);
5098 Current_Node := Sub;
5100 if not Restricted_Profile then
5101 Register_Handler;
5102 end if;
5103 end if;
5104 end if;
5106 Next (Priv);
5107 end loop;
5108 end if;
5110 -- Put the _Object component after the private component so that it
5111 -- be finalized early as required by 9.4 (20)
5113 Append_To (Cdecls, Object_Comp);
5115 Insert_After (Current_Node, Rec_Decl);
5116 Current_Node := Rec_Decl;
5118 -- Analyze the record declaration immediately after construction,
5119 -- because the initialization procedure is needed for single object
5120 -- declarations before the next entity is analyzed (the freeze call
5121 -- that generates this initialization procedure is found below).
5123 Analyze (Rec_Decl, Suppress => All_Checks);
5125 -- Collect pointers to entry bodies and their barriers, to be placed
5126 -- in the Entry_Bodies_Array for the type. For each entry/family we
5127 -- add an expression to the aggregate which is the initial value of
5128 -- this array. The array is declared after all protected subprograms.
5130 if Has_Entries (Prottyp) then
5131 Entries_Aggr :=
5132 Make_Aggregate (Loc, Expressions => New_List);
5134 else
5135 Entries_Aggr := Empty;
5136 end if;
5138 -- Build two new procedure specifications for each protected
5139 -- subprogram; one to call from outside the object and one to
5140 -- call from inside. Build a barrier function and an entry
5141 -- body action procedure specification for each protected entry.
5142 -- Initialize the entry body array.
5144 E_Count := 0;
5146 Comp := First (Visible_Declarations (Pdef));
5148 while Present (Comp) loop
5149 if Nkind (Comp) = N_Subprogram_Declaration then
5150 Sub :=
5151 Make_Subprogram_Declaration (Loc,
5152 Specification =>
5153 Build_Protected_Sub_Specification
5154 (Comp, Prottyp, Unprotected => True));
5156 Insert_After (Current_Node, Sub);
5157 Analyze (Sub);
5159 Set_Protected_Body_Subprogram
5160 (Defining_Unit_Name (Specification (Comp)),
5161 Defining_Unit_Name (Specification (Sub)));
5163 -- Make the protected version of the subprogram available
5164 -- for expansion of external calls.
5166 Current_Node := Sub;
5168 Sub :=
5169 Make_Subprogram_Declaration (Loc,
5170 Specification =>
5171 Build_Protected_Sub_Specification
5172 (Comp, Prottyp, Unprotected => False));
5174 Insert_After (Current_Node, Sub);
5175 Analyze (Sub);
5176 Current_Node := Sub;
5178 -- If a pragma Interrupt_Handler applies, build and add
5179 -- a call to Register_Interrupt_Handler to the freezing actions
5180 -- of the protected version (Current_Node) of the subprogram:
5181 -- system.interrupts.register_interrupt_handler
5182 -- (prot_procP'address);
5184 if not Restricted_Profile
5185 and then Is_Interrupt_Handler
5186 (Defining_Unit_Name (Specification (Comp)))
5187 then
5188 Register_Handler;
5189 end if;
5191 elsif Nkind (Comp) = N_Entry_Declaration then
5192 E_Count := E_Count + 1;
5193 Comp_Id := Defining_Identifier (Comp);
5194 Set_Privals_Chain (Comp_Id, New_Elmt_List);
5195 Nam := Chars (Comp_Id);
5196 Edef :=
5197 Make_Defining_Identifier (Loc,
5198 Build_Selected_Name (Protnm, New_Internal_Name ('E')));
5199 Sub :=
5200 Make_Subprogram_Declaration (Loc,
5201 Specification =>
5202 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
5204 Insert_After (Current_Node, Sub);
5205 Analyze (Sub);
5207 Set_Protected_Body_Subprogram (
5208 Defining_Identifier (Comp),
5209 Defining_Unit_Name (Specification (Sub)));
5211 Current_Node := Sub;
5213 Bdef :=
5214 Make_Defining_Identifier (Loc,
5215 Build_Selected_Name (Protnm, New_Internal_Name ('B')));
5216 Sub :=
5217 Make_Subprogram_Declaration (Loc,
5218 Specification =>
5219 Build_Barrier_Function_Specification (Bdef, Loc));
5221 Insert_After (Current_Node, Sub);
5222 Analyze (Sub);
5223 Set_Protected_Body_Subprogram (Bdef, Bdef);
5224 Set_Barrier_Function (Comp_Id, Bdef);
5225 Set_Scope (Bdef, Scope (Comp_Id));
5226 Current_Node := Sub;
5228 -- Collect pointers to the protected subprogram and the barrier
5229 -- of the current entry, for insertion into Entry_Bodies_Array.
5231 Append (
5232 Make_Aggregate (Loc,
5233 Expressions => New_List (
5234 Make_Attribute_Reference (Loc,
5235 Prefix => New_Reference_To (Bdef, Loc),
5236 Attribute_Name => Name_Unrestricted_Access),
5237 Make_Attribute_Reference (Loc,
5238 Prefix => New_Reference_To (Edef, Loc),
5239 Attribute_Name => Name_Unrestricted_Access))),
5240 Expressions (Entries_Aggr));
5242 end if;
5244 Next (Comp);
5245 end loop;
5247 -- If there are some private entry declarations, expand it as if they
5248 -- were visible entries.
5250 if Present (Private_Declarations (Pdef)) then
5251 Comp := First (Private_Declarations (Pdef));
5253 while Present (Comp) loop
5254 if Nkind (Comp) = N_Entry_Declaration then
5255 E_Count := E_Count + 1;
5256 Comp_Id := Defining_Identifier (Comp);
5257 Set_Privals_Chain (Comp_Id, New_Elmt_List);
5258 Nam := Chars (Comp_Id);
5259 Edef :=
5260 Make_Defining_Identifier (Loc,
5261 Build_Selected_Name (Protnm, New_Internal_Name ('E')));
5263 Sub :=
5264 Make_Subprogram_Declaration (Loc,
5265 Specification =>
5266 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
5268 Insert_After (Current_Node, Sub);
5269 Analyze (Sub);
5271 Set_Protected_Body_Subprogram (
5272 Defining_Identifier (Comp),
5273 Defining_Unit_Name (Specification (Sub)));
5275 Current_Node := Sub;
5277 Bdef :=
5278 Make_Defining_Identifier (Loc,
5279 Build_Selected_Name (Protnm, New_Internal_Name ('B')));
5280 Sub :=
5281 Make_Subprogram_Declaration (Loc,
5282 Specification =>
5283 Build_Barrier_Function_Specification (Bdef, Loc));
5285 Insert_After (Current_Node, Sub);
5286 Analyze (Sub);
5287 Set_Protected_Body_Subprogram (Bdef, Bdef);
5288 Set_Barrier_Function (Comp_Id, Bdef);
5289 Set_Scope (Bdef, Scope (Comp_Id));
5290 Current_Node := Sub;
5292 -- Collect pointers to the protected subprogram and the
5293 -- barrier of the current entry, for insertion into
5294 -- Entry_Bodies_Array.
5296 Append (
5297 Make_Aggregate (Loc,
5298 Expressions => New_List (
5299 Make_Attribute_Reference (Loc,
5300 Prefix => New_Reference_To (Bdef, Loc),
5301 Attribute_Name => Name_Unrestricted_Access),
5302 Make_Attribute_Reference (Loc,
5303 Prefix => New_Reference_To (Edef, Loc),
5304 Attribute_Name => Name_Unrestricted_Access))),
5305 Expressions (Entries_Aggr));
5306 end if;
5308 Next (Comp);
5309 end loop;
5310 end if;
5312 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
5313 -- all protected subprograms have been collected.
5315 if Has_Entries (Prottyp) then
5316 Body_Id := Make_Defining_Identifier (Sloc (Prottyp),
5317 New_External_Name (Chars (Prottyp), 'A'));
5319 if Abort_Allowed
5320 or else Restrictions (No_Entry_Queue) = False
5321 or else E_Count > 1
5322 then
5323 Body_Arr := Make_Object_Declaration (Loc,
5324 Defining_Identifier => Body_Id,
5325 Aliased_Present => True,
5326 Object_Definition =>
5327 Make_Subtype_Indication (Loc,
5328 Subtype_Mark => New_Reference_To (
5329 RTE (RE_Protected_Entry_Body_Array), Loc),
5330 Constraint =>
5331 Make_Index_Or_Discriminant_Constraint (Loc,
5332 Constraints => New_List (
5333 Make_Range (Loc,
5334 Make_Integer_Literal (Loc, 1),
5335 Make_Integer_Literal (Loc, E_Count))))),
5336 Expression => Entries_Aggr);
5338 else
5339 Body_Arr := Make_Object_Declaration (Loc,
5340 Defining_Identifier => Body_Id,
5341 Aliased_Present => True,
5342 Object_Definition => New_Reference_To (RTE (RE_Entry_Body), Loc),
5343 Expression =>
5344 Make_Aggregate (Loc,
5345 Expressions => New_List (
5346 Make_Attribute_Reference (Loc,
5347 Prefix => New_Reference_To (Bdef, Loc),
5348 Attribute_Name => Name_Unrestricted_Access),
5349 Make_Attribute_Reference (Loc,
5350 Prefix => New_Reference_To (Edef, Loc),
5351 Attribute_Name => Name_Unrestricted_Access))));
5352 end if;
5354 -- A pointer to this array will be placed in the corresponding
5355 -- record by its initialization procedure, so this needs to be
5356 -- analyzed here.
5358 Insert_After (Current_Node, Body_Arr);
5359 Current_Node := Body_Arr;
5360 Analyze (Body_Arr);
5362 Set_Entry_Bodies_Array (Prottyp, Body_Id);
5364 -- Finally, build the function that maps an entry index into the
5365 -- corresponding body. A pointer to this function is placed in each
5366 -- object of the type. Except for a ravenscar-like profile (no abort,
5367 -- no entry queue, 1 entry)
5369 if Abort_Allowed
5370 or else Restrictions (No_Entry_Queue) = False
5371 or else E_Count > 1
5372 then
5373 Sub :=
5374 Make_Subprogram_Declaration (Loc,
5375 Specification => Build_Find_Body_Index_Spec (Prottyp));
5376 Insert_After (Current_Node, Sub);
5377 Analyze (Sub);
5378 end if;
5379 end if;
5380 end Expand_N_Protected_Type_Declaration;
5382 --------------------------------
5383 -- Expand_N_Requeue_Statement --
5384 --------------------------------
5386 -- A requeue statement is expanded into one of four GNARLI operations,
5387 -- depending on the source and destination (task or protected object).
5388 -- In addition, code must be generated to jump around the remainder of
5389 -- processing for the original entry and, if the destination is a
5390 -- (different) protected object, to attempt to service it.
5391 -- The following illustrates the various cases:
5393 -- procedure entE
5394 -- (O : System.Address;
5395 -- P : System.Address;
5396 -- E : Protected_Entry_Index)
5397 -- is
5398 -- <discriminant renamings>
5399 -- <private object renamings>
5400 -- type poVP is access poV;
5401 -- _Object : ptVP := ptVP!(O);
5403 -- begin
5404 -- begin
5405 -- <start of statement sequence for entry>
5407 -- -- Requeue from one protected entry body to another protected
5408 -- -- entry.
5410 -- Requeue_Protected_Entry (
5411 -- _object._object'Access,
5412 -- new._object'Access,
5413 -- E,
5414 -- Abort_Present);
5415 -- return;
5417 -- <some more of the statement sequence for entry>
5419 -- -- Requeue from an entry body to a task entry.
5421 -- Requeue_Protected_To_Task_Entry (
5422 -- New._task_id,
5423 -- E,
5424 -- Abort_Present);
5425 -- return;
5427 -- <rest of statement sequence for entry>
5428 -- Complete_Entry_Body (_Object._Object);
5430 -- exception
5431 -- when all others =>
5432 -- Exceptional_Complete_Entry_Body (
5433 -- _Object._Object, Get_GNAT_Exception);
5434 -- end;
5435 -- end entE;
5437 -- Requeue of a task entry call to a task entry.
5439 -- Accept_Call (E, Ann);
5440 -- <start of statement sequence for accept statement>
5441 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
5442 -- goto Lnn;
5443 -- <rest of statement sequence for accept statement>
5444 -- <<Lnn>>
5445 -- Complete_Rendezvous;
5446 -- exception
5447 -- when all others =>
5448 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5450 -- Requeue of a task entry call to a protected entry.
5452 -- Accept_Call (E, Ann);
5453 -- <start of statement sequence for accept statement>
5454 -- Requeue_Task_To_Protected_Entry (
5455 -- new._object'Access,
5456 -- E,
5457 -- Abort_Present);
5458 -- newS (new, Pnn);
5459 -- goto Lnn;
5460 -- <rest of statement sequence for accept statement>
5461 -- <<Lnn>>
5462 -- Complete_Rendezvous;
5463 -- exception
5464 -- when all others =>
5465 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5467 -- Further details on these expansions can be found in
5468 -- Expand_N_Protected_Body and Expand_N_Accept_Statement.
5470 procedure Expand_N_Requeue_Statement (N : Node_Id) is
5471 Loc : constant Source_Ptr := Sloc (N);
5472 Acc_Stat : Node_Id;
5473 Concval : Node_Id;
5474 Ename : Node_Id;
5475 Index : Node_Id;
5476 Conctyp : Entity_Id;
5477 Oldtyp : Entity_Id;
5478 Lab_Node : Node_Id;
5479 Rcall : Node_Id;
5480 Abortable : Node_Id;
5481 Skip_Stat : Node_Id;
5482 Self_Param : Node_Id;
5483 New_Param : Node_Id;
5484 Params : List_Id;
5485 RTS_Call : Entity_Id;
5487 begin
5488 if Abort_Present (N) then
5489 Abortable := New_Occurrence_Of (Standard_True, Loc);
5490 else
5491 Abortable := New_Occurrence_Of (Standard_False, Loc);
5492 end if;
5494 -- Set up the target object.
5496 Extract_Entry (N, Concval, Ename, Index);
5497 Conctyp := Etype (Concval);
5498 New_Param := Concurrent_Ref (Concval);
5500 -- The target entry index and abortable flag are the same for all cases.
5502 Params := New_List (
5503 Entry_Index_Expression (Loc, Entity (Ename), Index, Conctyp),
5504 Abortable);
5506 -- Determine proper GNARLI call and required additional parameters
5507 -- Loop to find nearest enclosing task type or protected type
5509 Oldtyp := Current_Scope;
5510 loop
5511 if Is_Task_Type (Oldtyp) then
5512 if Is_Task_Type (Conctyp) then
5513 RTS_Call := RTE (RE_Requeue_Task_Entry);
5515 else
5516 pragma Assert (Is_Protected_Type (Conctyp));
5517 RTS_Call := RTE (RE_Requeue_Task_To_Protected_Entry);
5518 New_Param :=
5519 Make_Attribute_Reference (Loc,
5520 Prefix => New_Param,
5521 Attribute_Name => Name_Unchecked_Access);
5522 end if;
5524 Prepend (New_Param, Params);
5525 exit;
5527 elsif Is_Protected_Type (Oldtyp) then
5528 Self_Param :=
5529 Make_Attribute_Reference (Loc,
5530 Prefix => Concurrent_Ref (New_Occurrence_Of (Oldtyp, Loc)),
5531 Attribute_Name => Name_Unchecked_Access);
5533 if Is_Task_Type (Conctyp) then
5534 RTS_Call := RTE (RE_Requeue_Protected_To_Task_Entry);
5536 else
5537 pragma Assert (Is_Protected_Type (Conctyp));
5538 RTS_Call := RTE (RE_Requeue_Protected_Entry);
5539 New_Param :=
5540 Make_Attribute_Reference (Loc,
5541 Prefix => New_Param,
5542 Attribute_Name => Name_Unchecked_Access);
5543 end if;
5545 Prepend (New_Param, Params);
5546 Prepend (Self_Param, Params);
5547 exit;
5549 -- If neither task type or protected type, must be in some
5550 -- inner enclosing block, so move on out
5552 else
5553 Oldtyp := Scope (Oldtyp);
5554 end if;
5555 end loop;
5557 -- Create the GNARLI call.
5559 Rcall := Make_Procedure_Call_Statement (Loc,
5560 Name =>
5561 New_Occurrence_Of (RTS_Call, Loc),
5562 Parameter_Associations => Params);
5564 Rewrite (N, Rcall);
5565 Analyze (N);
5567 if Is_Protected_Type (Oldtyp) then
5569 -- Build the return statement to skip the rest of the entry body
5571 Skip_Stat := Make_Return_Statement (Loc);
5573 else
5574 -- If the requeue is within a task, find the end label of the
5575 -- enclosing accept statement.
5577 Acc_Stat := Parent (N);
5578 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5579 Acc_Stat := Parent (Acc_Stat);
5580 end loop;
5582 -- The last statement is the second label, used for completing the
5583 -- rendezvous the usual way.
5584 -- The label we are looking for is right before it.
5586 Lab_Node :=
5587 Prev (Last (Statements (Handled_Statement_Sequence (Acc_Stat))));
5589 pragma Assert (Nkind (Lab_Node) = N_Label);
5591 -- Build the goto statement to skip the rest of the accept
5592 -- statement.
5594 Skip_Stat :=
5595 Make_Goto_Statement (Loc,
5596 Name => New_Occurrence_Of (Entity (Identifier (Lab_Node)), Loc));
5597 end if;
5599 Set_Analyzed (Skip_Stat);
5601 Insert_After (N, Skip_Stat);
5603 end Expand_N_Requeue_Statement;
5605 -------------------------------
5606 -- Expand_N_Selective_Accept --
5607 -------------------------------
5609 procedure Expand_N_Selective_Accept (N : Node_Id) is
5610 Loc : constant Source_Ptr := Sloc (N);
5611 Alts : constant List_Id := Select_Alternatives (N);
5613 Accept_Case : List_Id;
5614 Accept_List : List_Id := New_List;
5616 Alt : Node_Id;
5617 Alt_List : List_Id := New_List;
5618 Alt_Stats : List_Id;
5619 Ann : Entity_Id := Empty;
5621 Block : Node_Id;
5622 Check_Guard : Boolean := True;
5623 Decls : List_Id := New_List;
5624 Stats : List_Id := New_List;
5626 Body_List : List_Id := New_List;
5627 Trailing_List : List_Id := New_List;
5629 Choices : List_Id;
5630 Else_Present : Boolean := False;
5631 Terminate_Alt : Node_Id := Empty;
5632 Select_Mode : Node_Id;
5634 Delay_Case : List_Id;
5635 Delay_Count : Integer := 0;
5636 Delay_Val : Entity_Id;
5637 Delay_Index : Entity_Id;
5638 Delay_Min : Entity_Id;
5639 Delay_Num : Int := 1;
5640 Delay_Alt_List : List_Id := New_List;
5641 Delay_List : List_Id := New_List;
5642 D : Entity_Id;
5643 M : Entity_Id;
5645 First_Delay : Boolean := True;
5646 Guard_Open : Entity_Id;
5648 End_Lab : Node_Id;
5649 Index : Int := 1;
5650 Lab : Node_Id;
5651 Num_Alts : Int;
5652 Num_Accept : Nat := 0;
5653 Proc : Node_Id;
5654 Q : Node_Id;
5655 Time_Type : Entity_Id;
5656 X : Node_Id;
5657 Select_Call : Node_Id;
5659 Qnam : constant Entity_Id :=
5660 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
5662 Xnam : constant Entity_Id :=
5663 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
5665 -----------------------
5666 -- Local subprograms --
5667 -----------------------
5669 function Accept_Or_Raise return List_Id;
5670 -- For the rare case where delay alternatives all have guards, and
5671 -- all of them are closed, it is still possible that there were open
5672 -- accept alternatives with no callers. We must reexamine the
5673 -- Accept_List, and execute a selective wait with no else if some
5674 -- accept is open. If none, we raise program_error.
5676 procedure Add_Accept (Alt : Node_Id);
5677 -- Process a single accept statement in a select alternative. Build
5678 -- procedure for body of accept, and add entry to dispatch table with
5679 -- expression for guard, in preparation for call to run time select.
5681 function Make_And_Declare_Label (Num : Int) return Node_Id;
5682 -- Manufacture a label using Num as a serial number and declare it.
5683 -- The declaration is appended to Decls. The label marks the trailing
5684 -- statements of an accept or delay alternative.
5686 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
5687 -- Build call to Selective_Wait runtime routine.
5689 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
5690 -- Add code to compare value of delay with previous values, and
5691 -- generate case entry for trailing statements.
5693 procedure Process_Accept_Alternative
5694 (Alt : Node_Id;
5695 Index : Int;
5696 Proc : Node_Id);
5697 -- Add code to call corresponding procedure, and branch to
5698 -- trailing statements, if any.
5700 ---------------------
5701 -- Accept_Or_Raise --
5702 ---------------------
5704 function Accept_Or_Raise return List_Id is
5705 Cond : Node_Id;
5706 Stats : List_Id;
5707 J : constant Entity_Id := Make_Defining_Identifier (Loc,
5708 New_Internal_Name ('J'));
5710 begin
5711 -- We generate the following:
5713 -- for J in q'range loop
5714 -- if q(J).S /=null_task_entry then
5715 -- selective_wait (simple_mode,...);
5716 -- done := True;
5717 -- exit;
5718 -- end if;
5719 -- end loop;
5721 -- if no rendez_vous then
5722 -- raise program_error;
5723 -- end if;
5725 -- Note that the code needs to know that the selector name
5726 -- in an Accept_Alternative is named S.
5728 Cond := Make_Op_Ne (Loc,
5729 Left_Opnd =>
5730 Make_Selected_Component (Loc,
5731 Prefix => Make_Indexed_Component (Loc,
5732 Prefix => New_Reference_To (Qnam, Loc),
5733 Expressions => New_List (New_Reference_To (J, Loc))),
5734 Selector_Name => Make_Identifier (Loc, Name_S)),
5735 Right_Opnd =>
5736 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
5738 Stats := New_List (
5739 Make_Implicit_Loop_Statement (N,
5740 Identifier => Empty,
5741 Iteration_Scheme =>
5742 Make_Iteration_Scheme (Loc,
5743 Loop_Parameter_Specification =>
5744 Make_Loop_Parameter_Specification (Loc,
5745 Defining_Identifier => J,
5746 Discrete_Subtype_Definition =>
5747 Make_Attribute_Reference (Loc,
5748 Prefix => New_Reference_To (Qnam, Loc),
5749 Attribute_Name => Name_Range,
5750 Expressions => New_List (
5751 Make_Integer_Literal (Loc, 1))))),
5753 Statements => New_List (
5754 Make_Implicit_If_Statement (N,
5755 Condition => Cond,
5756 Then_Statements => New_List (
5757 Make_Select_Call (
5758 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
5759 Make_Exit_Statement (Loc))))));
5761 Append_To (Stats,
5762 Make_Raise_Program_Error (Loc,
5763 Condition => Make_Op_Eq (Loc,
5764 Left_Opnd => New_Reference_To (Xnam, Loc),
5765 Right_Opnd =>
5766 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
5767 Reason => PE_All_Guards_Closed));
5769 return Stats;
5770 end Accept_Or_Raise;
5772 ----------------
5773 -- Add_Accept --
5774 ----------------
5776 procedure Add_Accept (Alt : Node_Id) is
5777 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
5778 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
5779 Eent : constant Entity_Id := Entity (Ename);
5780 Index : constant Node_Id := Entry_Index (Acc_Stm);
5781 Null_Body : Node_Id;
5782 Proc_Body : Node_Id;
5783 PB_Ent : Entity_Id;
5784 Expr : Node_Id;
5785 Call : Node_Id;
5787 begin
5788 if No (Ann) then
5789 Ann := Node (Last_Elmt (Accept_Address (Eent)));
5790 end if;
5792 if Present (Condition (Alt)) then
5793 Expr :=
5794 Make_Conditional_Expression (Loc, New_List (
5795 Condition (Alt),
5796 Entry_Index_Expression (Loc, Eent, Index, Scope (Eent)),
5797 New_Reference_To (RTE (RE_Null_Task_Entry), Loc)));
5798 else
5799 Expr :=
5800 Entry_Index_Expression
5801 (Loc, Eent, Index, Scope (Eent));
5802 end if;
5804 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
5805 Null_Body := New_Reference_To (Standard_False, Loc);
5807 if Abort_Allowed then
5808 Call := Make_Procedure_Call_Statement (Loc,
5809 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc));
5810 Insert_Before (First (Statements (Handled_Statement_Sequence (
5811 Accept_Statement (Alt)))), Call);
5812 Analyze (Call);
5813 end if;
5815 PB_Ent :=
5816 Make_Defining_Identifier (Sloc (Ename),
5817 New_External_Name (Chars (Ename), 'A', Num_Accept));
5819 Proc_Body :=
5820 Make_Subprogram_Body (Loc,
5821 Specification =>
5822 Make_Procedure_Specification (Loc,
5823 Defining_Unit_Name => PB_Ent),
5824 Declarations => Declarations (Acc_Stm),
5825 Handled_Statement_Sequence =>
5826 Build_Accept_Body (Accept_Statement (Alt)));
5828 -- During the analysis of the body of the accept statement, any
5829 -- zero cost exception handler records were collected in the
5830 -- Accept_Handler_Records field of the N_Accept_Alternative
5831 -- node. This is where we move them to where they belong,
5832 -- namely the newly created procedure.
5834 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
5835 Append (Proc_Body, Body_List);
5837 else
5838 Null_Body := New_Reference_To (Standard_True, Loc);
5840 -- if accept statement has declarations, insert above, given
5841 -- that we are not creating a body for the accept.
5843 if Present (Declarations (Acc_Stm)) then
5844 Insert_Actions (N, Declarations (Acc_Stm));
5845 end if;
5846 end if;
5848 Append_To (Accept_List,
5849 Make_Aggregate (Loc, Expressions => New_List (Null_Body, Expr)));
5851 Num_Accept := Num_Accept + 1;
5853 end Add_Accept;
5855 ----------------------------
5856 -- Make_And_Declare_Label --
5857 ----------------------------
5859 function Make_And_Declare_Label (Num : Int) return Node_Id is
5860 Lab_Id : Node_Id;
5862 begin
5863 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
5864 Lab :=
5865 Make_Label (Loc, Lab_Id);
5867 Append_To (Decls,
5868 Make_Implicit_Label_Declaration (Loc,
5869 Defining_Identifier =>
5870 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
5871 Label_Construct => Lab));
5873 return Lab;
5874 end Make_And_Declare_Label;
5876 ----------------------
5877 -- Make_Select_Call --
5878 ----------------------
5880 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
5881 Params : List_Id := New_List;
5883 begin
5884 Append (
5885 Make_Attribute_Reference (Loc,
5886 Prefix => New_Reference_To (Qnam, Loc),
5887 Attribute_Name => Name_Unchecked_Access),
5888 Params);
5889 Append (Select_Mode, Params);
5890 Append (New_Reference_To (Ann, Loc), Params);
5891 Append (New_Reference_To (Xnam, Loc), Params);
5893 return
5894 Make_Procedure_Call_Statement (Loc,
5895 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
5896 Parameter_Associations => Params);
5897 end Make_Select_Call;
5899 --------------------------------
5900 -- Process_Accept_Alternative --
5901 --------------------------------
5903 procedure Process_Accept_Alternative
5904 (Alt : Node_Id;
5905 Index : Int;
5906 Proc : Node_Id)
5908 Choices : List_Id := No_List;
5909 Alt_Stats : List_Id;
5911 begin
5912 Adjust_Condition (Condition (Alt));
5913 Alt_Stats := No_List;
5915 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
5916 Choices := New_List (
5917 Make_Integer_Literal (Loc, Index));
5919 Alt_Stats := New_List (
5920 Make_Procedure_Call_Statement (Loc,
5921 Name => New_Reference_To (
5922 Defining_Unit_Name (Specification (Proc)), Loc)));
5923 end if;
5925 if Statements (Alt) /= Empty_List then
5927 if No (Alt_Stats) then
5929 -- Accept with no body, followed by trailing statements.
5931 Choices := New_List (
5932 Make_Integer_Literal (Loc, Index));
5934 Alt_Stats := New_List;
5935 end if;
5937 -- After the call, if any, branch to to trailing statements.
5938 -- We create a label for each, as well as the corresponding
5939 -- label declaration.
5941 Lab := Make_And_Declare_Label (Index);
5942 Append_To (Alt_Stats,
5943 Make_Goto_Statement (Loc,
5944 Name => New_Copy (Identifier (Lab))));
5946 Append (Lab, Trailing_List);
5947 Append_List (Statements (Alt), Trailing_List);
5948 Append_To (Trailing_List,
5949 Make_Goto_Statement (Loc,
5950 Name => New_Copy (Identifier (End_Lab))));
5951 end if;
5953 if Present (Alt_Stats) then
5955 -- Procedure call. and/or trailing statements
5957 Append_To (Alt_List,
5958 Make_Case_Statement_Alternative (Loc,
5959 Discrete_Choices => Choices,
5960 Statements => Alt_Stats));
5961 end if;
5962 end Process_Accept_Alternative;
5964 -------------------------------
5965 -- Process_Delay_Alternative --
5966 -------------------------------
5968 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
5969 Choices : List_Id;
5970 Cond : Node_Id;
5971 Delay_Alt : List_Id;
5973 begin
5974 -- Deal with C/Fortran boolean as delay condition
5976 Adjust_Condition (Condition (Alt));
5978 -- Determine the smallest specified delay.
5979 -- for each delay alternative generate:
5981 -- if guard-expression then
5982 -- Delay_Val := delay-expression;
5983 -- Guard_Open := True;
5984 -- if Delay_Val < Delay_Min then
5985 -- Delay_Min := Delay_Val;
5986 -- Delay_Index := Index;
5987 -- end if;
5988 -- end if;
5990 -- The enclosing if-statement is omitted if there is no guard.
5992 if Delay_Count = 1
5993 or else First_Delay
5994 then
5995 First_Delay := False;
5997 Delay_Alt := New_List (
5998 Make_Assignment_Statement (Loc,
5999 Name => New_Reference_To (Delay_Min, Loc),
6000 Expression => Expression (Delay_Statement (Alt))));
6002 if Delay_Count > 1 then
6003 Append_To (Delay_Alt,
6004 Make_Assignment_Statement (Loc,
6005 Name => New_Reference_To (Delay_Index, Loc),
6006 Expression => Make_Integer_Literal (Loc, Index)));
6007 end if;
6009 else
6010 Delay_Alt := New_List (
6011 Make_Assignment_Statement (Loc,
6012 Name => New_Reference_To (Delay_Val, Loc),
6013 Expression => Expression (Delay_Statement (Alt))));
6015 if Time_Type = Standard_Duration then
6016 Cond :=
6017 Make_Op_Lt (Loc,
6018 Left_Opnd => New_Reference_To (Delay_Val, Loc),
6019 Right_Opnd => New_Reference_To (Delay_Min, Loc));
6021 else
6022 -- The scope of the time type must define a comparison
6023 -- operator. The scope itself may not be visible, so we
6024 -- construct a node with entity information to insure that
6025 -- semantic analysis can find the proper operator.
6027 Cond :=
6028 Make_Function_Call (Loc,
6029 Name => Make_Selected_Component (Loc,
6030 Prefix => New_Reference_To (Scope (Time_Type), Loc),
6031 Selector_Name =>
6032 Make_Operator_Symbol (Loc,
6033 Chars => Name_Op_Lt,
6034 Strval => No_String)),
6035 Parameter_Associations =>
6036 New_List (
6037 New_Reference_To (Delay_Val, Loc),
6038 New_Reference_To (Delay_Min, Loc)));
6040 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
6041 end if;
6043 Append_To (Delay_Alt,
6044 Make_Implicit_If_Statement (N,
6045 Condition => Cond,
6046 Then_Statements => New_List (
6047 Make_Assignment_Statement (Loc,
6048 Name => New_Reference_To (Delay_Min, Loc),
6049 Expression => New_Reference_To (Delay_Val, Loc)),
6051 Make_Assignment_Statement (Loc,
6052 Name => New_Reference_To (Delay_Index, Loc),
6053 Expression => Make_Integer_Literal (Loc, Index)))));
6054 end if;
6056 if Check_Guard then
6057 Append_To (Delay_Alt,
6058 Make_Assignment_Statement (Loc,
6059 Name => New_Reference_To (Guard_Open, Loc),
6060 Expression => New_Reference_To (Standard_True, Loc)));
6061 end if;
6063 if Present (Condition (Alt)) then
6064 Delay_Alt := New_List (
6065 Make_Implicit_If_Statement (N,
6066 Condition => Condition (Alt),
6067 Then_Statements => Delay_Alt));
6068 end if;
6070 Append_List (Delay_Alt, Delay_List);
6072 -- If the delay alternative has a statement part, add a
6073 -- choice to the case statements for delays.
6075 if Present (Statements (Alt)) then
6077 if Delay_Count = 1 then
6078 Append_List (Statements (Alt), Delay_Alt_List);
6080 else
6081 Choices := New_List (
6082 Make_Integer_Literal (Loc, Index));
6084 Append_To (Delay_Alt_List,
6085 Make_Case_Statement_Alternative (Loc,
6086 Discrete_Choices => Choices,
6087 Statements => Statements (Alt)));
6088 end if;
6090 elsif Delay_Count = 1 then
6092 -- If the single delay has no trailing statements, add a branch
6093 -- to the exit label to the selective wait.
6095 Delay_Alt_List := New_List (
6096 Make_Goto_Statement (Loc,
6097 Name => New_Copy (Identifier (End_Lab))));
6099 end if;
6100 end Process_Delay_Alternative;
6102 -- Start of processing for Expand_N_Selective_Accept
6104 begin
6105 -- First insert some declarations before the select. The first is:
6107 -- Ann : Address
6109 -- This variable holds the parameters passed to the accept body. This
6110 -- declaration has already been inserted by the time we get here by
6111 -- a call to Expand_Accept_Declarations made from the semantics when
6112 -- processing the first accept statement contained in the select. We
6113 -- can find this entity as Accept_Address (E), where E is any of the
6114 -- entries references by contained accept statements.
6116 -- The first step is to scan the list of Selective_Accept_Statements
6117 -- to find this entity, and also count the number of accepts, and
6118 -- determine if terminated, delay or else is present:
6120 Num_Alts := 0;
6122 Alt := First (Alts);
6123 while Present (Alt) loop
6125 if Nkind (Alt) = N_Accept_Alternative then
6126 Add_Accept (Alt);
6128 elsif Nkind (Alt) = N_Delay_Alternative then
6129 Delay_Count := Delay_Count + 1;
6131 -- If the delays are relative delays, the delay expressions have
6132 -- type Standard_Duration. Otherwise they must have some time type
6133 -- recognized by GNAT.
6135 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
6136 Time_Type := Standard_Duration;
6137 else
6138 Time_Type := Etype (Expression (Delay_Statement (Alt)));
6140 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
6141 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
6142 then
6143 null;
6144 else
6145 Error_Msg_NE (
6146 "& is not a time type ('R'M 9.6(6))",
6147 Expression (Delay_Statement (Alt)), Time_Type);
6148 Time_Type := Standard_Duration;
6149 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
6150 end if;
6151 end if;
6153 if No (Condition (Alt)) then
6155 -- This guard will always be open.
6157 Check_Guard := False;
6158 end if;
6160 elsif Nkind (Alt) = N_Terminate_Alternative then
6161 Adjust_Condition (Condition (Alt));
6162 Terminate_Alt := Alt;
6163 end if;
6165 Num_Alts := Num_Alts + 1;
6166 Next (Alt);
6167 end loop;
6169 Else_Present := Present (Else_Statements (N));
6171 -- At the same time (see procedure Add_Accept) we build the accept list:
6173 -- Qnn : Accept_List (1 .. num-select) := (
6174 -- (null-body, entry-index),
6175 -- (null-body, entry-index),
6176 -- ..
6177 -- (null_body, entry-index));
6179 -- In the above declaration, null-body is True if the corresponding
6180 -- accept has no body, and false otherwise. The entry is either the
6181 -- entry index expression if there is no guard, or if a guard is
6182 -- present, then a conditional expression of the form:
6184 -- (if guard then entry-index else Null_Task_Entry)
6186 -- If a guard is statically known to be false, the entry can simply
6187 -- be omitted from the accept list.
6189 Q :=
6190 Make_Object_Declaration (Loc,
6191 Defining_Identifier => Qnam,
6192 Object_Definition =>
6193 New_Reference_To (RTE (RE_Accept_List), Loc),
6194 Aliased_Present => True,
6196 Expression =>
6197 Make_Qualified_Expression (Loc,
6198 Subtype_Mark =>
6199 New_Reference_To (RTE (RE_Accept_List), Loc),
6200 Expression =>
6201 Make_Aggregate (Loc, Expressions => Accept_List)));
6203 Append (Q, Decls);
6205 -- Then we declare the variable that holds the index for the accept
6206 -- that will be selected for service:
6208 -- Xnn : Select_Index;
6210 X :=
6211 Make_Object_Declaration (Loc,
6212 Defining_Identifier => Xnam,
6213 Object_Definition =>
6214 New_Reference_To (RTE (RE_Select_Index), Loc),
6215 Expression =>
6216 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
6218 Append (X, Decls);
6220 -- After this follow procedure declarations for each accept body.
6222 -- procedure Pnn is
6223 -- begin
6224 -- ...
6225 -- end;
6227 -- where the ... are statements from the corresponding procedure body.
6228 -- No parameters are involved, since the parameters are passed via Ann
6229 -- and the parameter references have already been expanded to be direct
6230 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
6231 -- any embedded tasking statements (which would normally be illegal in
6232 -- procedures, have been converted to calls to the tasking runtime so
6233 -- there is no problem in putting them into procedures.
6235 -- The original accept statement has been expanded into a block in
6236 -- the same fashion as for simple accepts (see Build_Accept_Body).
6238 -- Note: we don't really need to build these procedures for the case
6239 -- where no delay statement is present, but it is just as easy to
6240 -- build them unconditionally, and not significantly inefficient,
6241 -- since if they are short they will be inlined anyway.
6243 -- The procedure declarations have been assembled in Body_List.
6245 -- If delays are present, we must compute the required delay.
6246 -- We first generate the declarations:
6248 -- Delay_Index : Boolean := 0;
6249 -- Delay_Min : Some_Time_Type.Time;
6250 -- Delay_Val : Some_Time_Type.Time;
6252 -- Delay_Index will be set to the index of the minimum delay, i.e. the
6253 -- active delay that is actually chosen as the basis for the possible
6254 -- delay if an immediate rendez-vous is not possible.
6255 -- In the most common case there is a single delay statement, and this
6256 -- is handled specially.
6258 if Delay_Count > 0 then
6260 -- Generate the required declarations
6262 Delay_Val :=
6263 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
6264 Delay_Index :=
6265 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
6266 Delay_Min :=
6267 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
6269 Append_To (Decls,
6270 Make_Object_Declaration (Loc,
6271 Defining_Identifier => Delay_Val,
6272 Object_Definition => New_Reference_To (Time_Type, Loc)));
6274 Append_To (Decls,
6275 Make_Object_Declaration (Loc,
6276 Defining_Identifier => Delay_Index,
6277 Object_Definition => New_Reference_To (Standard_Integer, Loc),
6278 Expression => Make_Integer_Literal (Loc, 0)));
6280 Append_To (Decls,
6281 Make_Object_Declaration (Loc,
6282 Defining_Identifier => Delay_Min,
6283 Object_Definition => New_Reference_To (Time_Type, Loc),
6284 Expression =>
6285 Unchecked_Convert_To (Time_Type,
6286 Make_Attribute_Reference (Loc,
6287 Prefix =>
6288 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
6289 Attribute_Name => Name_Last))));
6291 -- Create Duration and Delay_Mode objects used for passing a delay
6292 -- value to RTS
6294 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
6295 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
6297 declare
6298 Discr : Entity_Id;
6300 begin
6301 -- Note that these values are defined in s-osprim.ads and must
6302 -- be kept in sync:
6304 -- Relative : constant := 0;
6305 -- Absolute_Calendar : constant := 1;
6306 -- Absolute_RT : constant := 2;
6308 if Time_Type = Standard_Duration then
6309 Discr := Make_Integer_Literal (Loc, 0);
6311 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
6312 Discr := Make_Integer_Literal (Loc, 1);
6314 else
6315 pragma Assert
6316 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
6317 Discr := Make_Integer_Literal (Loc, 2);
6318 end if;
6320 Append_To (Decls,
6321 Make_Object_Declaration (Loc,
6322 Defining_Identifier => D,
6323 Object_Definition =>
6324 New_Reference_To (Standard_Duration, Loc)));
6326 Append_To (Decls,
6327 Make_Object_Declaration (Loc,
6328 Defining_Identifier => M,
6329 Object_Definition =>
6330 New_Reference_To (Standard_Integer, Loc),
6331 Expression => Discr));
6332 end;
6334 if Check_Guard then
6335 Guard_Open :=
6336 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
6338 Append_To (Decls,
6339 Make_Object_Declaration (Loc,
6340 Defining_Identifier => Guard_Open,
6341 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
6342 Expression => New_Reference_To (Standard_False, Loc)));
6343 end if;
6345 -- Delay_Count is zero, don't need M and D set (suppress warning)
6347 else
6348 M := Empty;
6349 D := Empty;
6350 end if;
6352 if Present (Terminate_Alt) then
6354 -- If the terminate alternative guard is False, use
6355 -- Simple_Mode; otherwise use Terminate_Mode.
6357 if Present (Condition (Terminate_Alt)) then
6358 Select_Mode := Make_Conditional_Expression (Loc,
6359 New_List (Condition (Terminate_Alt),
6360 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
6361 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
6362 else
6363 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
6364 end if;
6366 elsif Else_Present or Delay_Count > 0 then
6367 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
6369 else
6370 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
6371 end if;
6373 Select_Call := Make_Select_Call (Select_Mode);
6374 Append (Select_Call, Stats);
6376 -- Now generate code to act on the result. There is an entry
6377 -- in this case for each accept statement with a non-null body,
6378 -- followed by a branch to the statements that follow the Accept.
6379 -- In the absence of delay alternatives, we generate:
6381 -- case X is
6382 -- when No_Rendezvous => -- omitted if simple mode
6383 -- goto Lab0;
6385 -- when 1 =>
6386 -- P1n;
6387 -- goto Lab1;
6389 -- when 2 =>
6390 -- P2n;
6391 -- goto Lab2;
6393 -- when others =>
6394 -- goto Exit;
6395 -- end case;
6397 -- Lab0: Else_Statements;
6398 -- goto exit;
6400 -- Lab1: Trailing_Statements1;
6401 -- goto Exit;
6403 -- Lab2: Trailing_Statements2;
6404 -- goto Exit;
6405 -- ...
6406 -- Exit:
6408 -- Generate label for common exit.
6410 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
6412 -- First entry is the default case, when no rendezvous is possible.
6414 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
6416 if Else_Present then
6418 -- If no rendezvous is possible, the else part is executed.
6420 Lab := Make_And_Declare_Label (0);
6421 Alt_Stats := New_List (
6422 Make_Goto_Statement (Loc,
6423 Name => New_Copy (Identifier (Lab))));
6425 Append (Lab, Trailing_List);
6426 Append_List (Else_Statements (N), Trailing_List);
6427 Append_To (Trailing_List,
6428 Make_Goto_Statement (Loc,
6429 Name => New_Copy (Identifier (End_Lab))));
6430 else
6431 Alt_Stats := New_List (
6432 Make_Goto_Statement (Loc,
6433 Name => New_Copy (Identifier (End_Lab))));
6434 end if;
6436 Append_To (Alt_List,
6437 Make_Case_Statement_Alternative (Loc,
6438 Discrete_Choices => Choices,
6439 Statements => Alt_Stats));
6441 -- We make use of the fact that Accept_Index is an integer type,
6442 -- and generate successive literals for entries for each accept.
6443 -- Only those for which there is a body or trailing statements are
6444 -- given a case entry.
6446 Alt := First (Select_Alternatives (N));
6447 Proc := First (Body_List);
6449 while Present (Alt) loop
6451 if Nkind (Alt) = N_Accept_Alternative then
6452 Process_Accept_Alternative (Alt, Index, Proc);
6453 Index := Index + 1;
6455 if Present
6456 (Handled_Statement_Sequence (Accept_Statement (Alt)))
6457 then
6458 Next (Proc);
6459 end if;
6461 elsif Nkind (Alt) = N_Delay_Alternative then
6462 Process_Delay_Alternative (Alt, Delay_Num);
6463 Delay_Num := Delay_Num + 1;
6464 end if;
6466 Next (Alt);
6467 end loop;
6469 -- An others choice is always added to the main case, as well
6470 -- as the delay case (to satisfy the compiler).
6472 Append_To (Alt_List,
6473 Make_Case_Statement_Alternative (Loc,
6474 Discrete_Choices =>
6475 New_List (Make_Others_Choice (Loc)),
6476 Statements =>
6477 New_List (Make_Goto_Statement (Loc,
6478 Name => New_Copy (Identifier (End_Lab))))));
6480 Accept_Case := New_List (
6481 Make_Case_Statement (Loc,
6482 Expression => New_Reference_To (Xnam, Loc),
6483 Alternatives => Alt_List));
6485 Append_List (Trailing_List, Accept_Case);
6486 Append (End_Lab, Accept_Case);
6487 Append_List (Body_List, Decls);
6489 -- Construct case statement for trailing statements of delay
6490 -- alternatives, if there are several of them.
6492 if Delay_Count > 1 then
6493 Append_To (Delay_Alt_List,
6494 Make_Case_Statement_Alternative (Loc,
6495 Discrete_Choices =>
6496 New_List (Make_Others_Choice (Loc)),
6497 Statements =>
6498 New_List (Make_Null_Statement (Loc))));
6500 Delay_Case := New_List (
6501 Make_Case_Statement (Loc,
6502 Expression => New_Reference_To (Delay_Index, Loc),
6503 Alternatives => Delay_Alt_List));
6504 else
6505 Delay_Case := Delay_Alt_List;
6506 end if;
6508 -- If there are no delay alternatives, we append the case statement
6509 -- to the statement list.
6511 if Delay_Count = 0 then
6512 Append_List (Accept_Case, Stats);
6514 -- Delay alternatives present
6516 else
6517 -- If delay alternatives are present we generate:
6519 -- find minimum delay.
6520 -- DX := minimum delay;
6521 -- M := <delay mode>;
6522 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
6523 -- DX, MX, X);
6525 -- if X = No_Rendezvous then
6526 -- case statement for delay statements.
6527 -- else
6528 -- case statement for accept alternatives.
6529 -- end if;
6531 declare
6532 Cases : Node_Id;
6533 Stmt : Node_Id;
6534 Parms : List_Id;
6535 Parm : Node_Id;
6536 Conv : Node_Id;
6538 begin
6539 -- The type of the delay expression is known to be legal
6541 if Time_Type = Standard_Duration then
6542 Conv := New_Reference_To (Delay_Min, Loc);
6544 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
6545 Conv := Make_Function_Call (Loc,
6546 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
6547 New_List (New_Reference_To (Delay_Min, Loc)));
6549 else
6550 pragma Assert
6551 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
6553 Conv := Make_Function_Call (Loc,
6554 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
6555 New_List (New_Reference_To (Delay_Min, Loc)));
6556 end if;
6558 Stmt := Make_Assignment_Statement (Loc,
6559 Name => New_Reference_To (D, Loc),
6560 Expression => Conv);
6562 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
6564 Parms := Parameter_Associations (Select_Call);
6565 Parm := First (Parms);
6567 while Present (Parm)
6568 and then Parm /= Select_Mode
6569 loop
6570 Next (Parm);
6571 end loop;
6573 pragma Assert (Present (Parm));
6574 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
6575 Analyze (Parm);
6577 -- Prepare two new parameters of Duration and Delay_Mode type
6578 -- which represent the value and the mode of the minimum delay.
6580 Next (Parm);
6581 Insert_After (Parm, New_Reference_To (M, Loc));
6582 Insert_After (Parm, New_Reference_To (D, Loc));
6584 -- Create a call to RTS.
6586 Rewrite (Select_Call,
6587 Make_Procedure_Call_Statement (Loc,
6588 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
6589 Parameter_Associations => Parms));
6591 -- This new call should follow the calculation of the
6592 -- minimum delay.
6594 Insert_List_Before (Select_Call, Delay_List);
6596 if Check_Guard then
6597 Stmt :=
6598 Make_Implicit_If_Statement (N,
6599 Condition => New_Reference_To (Guard_Open, Loc),
6600 Then_Statements =>
6601 New_List (New_Copy_Tree (Stmt),
6602 New_Copy_Tree (Select_Call)),
6603 Else_Statements => Accept_Or_Raise);
6604 Rewrite (Select_Call, Stmt);
6605 else
6606 Insert_Before (Select_Call, Stmt);
6607 end if;
6609 Cases :=
6610 Make_Implicit_If_Statement (N,
6611 Condition => Make_Op_Eq (Loc,
6612 Left_Opnd => New_Reference_To (Xnam, Loc),
6613 Right_Opnd =>
6614 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
6616 Then_Statements => Delay_Case,
6617 Else_Statements => Accept_Case);
6619 Append (Cases, Stats);
6620 end;
6621 end if;
6623 -- Replace accept statement with appropriate block
6625 Block :=
6626 Make_Block_Statement (Loc,
6627 Declarations => Decls,
6628 Handled_Statement_Sequence =>
6629 Make_Handled_Sequence_Of_Statements (Loc,
6630 Statements => Stats));
6632 Rewrite (N, Block);
6633 Analyze (N);
6635 -- Note: have to worry more about abort deferral in above code ???
6637 -- Final step is to unstack the Accept_Address entries for all accept
6638 -- statements appearing in accept alternatives in the select statement
6640 Alt := First (Alts);
6641 while Present (Alt) loop
6642 if Nkind (Alt) = N_Accept_Alternative then
6643 Remove_Last_Elmt (Accept_Address
6644 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
6645 end if;
6647 Next (Alt);
6648 end loop;
6650 end Expand_N_Selective_Accept;
6652 --------------------------------------
6653 -- Expand_N_Single_Task_Declaration --
6654 --------------------------------------
6656 -- Single task declarations should never be present after semantic
6657 -- analysis, since we expect them to be replaced by a declaration of
6658 -- an anonymous task type, followed by a declaration of the task
6659 -- object. We include this routine to make sure that is happening!
6661 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
6662 begin
6663 raise Program_Error;
6664 end Expand_N_Single_Task_Declaration;
6666 ------------------------
6667 -- Expand_N_Task_Body --
6668 ------------------------
6670 -- Given a task body
6672 -- task body tname is
6673 -- <declarations>
6674 -- begin
6675 -- <statements>
6676 -- end x;
6678 -- This expansion routine converts it into a procedure and sets the
6679 -- elaboration flag for the procedure to true, to represent the fact
6680 -- that the task body is now elaborated:
6682 -- procedure tnameB (_Task : access tnameV) is
6683 -- discriminal : dtype renames _Task.discriminant;
6685 -- procedure _clean is
6686 -- begin
6687 -- Abort_Defer.all;
6688 -- Complete_Task;
6689 -- Abort_Undefer.all;
6690 -- return;
6691 -- end _clean;
6692 -- begin
6693 -- Abort_Undefer.all;
6694 -- <declarations>
6695 -- System.Task_Stages.Complete_Activation;
6696 -- <statements>
6697 -- at end
6698 -- _clean;
6699 -- end tnameB;
6701 -- tnameE := True;
6703 -- In addition, if the task body is an activator, then a call to
6704 -- activate tasks is added at the start of the statements, before
6705 -- the call to Complete_Activation, and if in addition the task is
6706 -- a master then it must be established as a master. These calls are
6707 -- inserted and analyzed in Expand_Cleanup_Actions, when the
6708 -- Handled_Sequence_Of_Statements is expanded.
6710 -- There is one discriminal declaration line generated for each
6711 -- discriminant that is present to provide an easy reference point
6712 -- for discriminant references inside the body (see Exp_Ch2.Expand_Name).
6714 -- Note on relationship to GNARLI definition. In the GNARLI definition,
6715 -- task body procedures have a profile (Arg : System.Address). That is
6716 -- needed because GNARLI has to use the same access-to-subprogram type
6717 -- for all task types. We depend here on knowing that in GNAT, passing
6718 -- an address argument by value is identical to passing a record value
6719 -- by access (in either case a single pointer is passed), so even though
6720 -- this procedure has the wrong profile. In fact it's all OK, since the
6721 -- callings sequence is identical.
6723 procedure Expand_N_Task_Body (N : Node_Id) is
6724 Loc : constant Source_Ptr := Sloc (N);
6725 Ttyp : constant Entity_Id := Corresponding_Spec (N);
6726 Call : Node_Id;
6727 New_N : Node_Id;
6729 begin
6730 -- Do not attempt expansion if in no run time mode
6732 if No_Run_Time
6733 and then not Restricted_Profile
6734 then
6735 Disallow_In_No_Run_Time_Mode (N);
6736 return;
6737 end if;
6739 -- Here we start the expansion by generating discriminal declarations
6741 Add_Discriminal_Declarations (Declarations (N), Ttyp, Name_uTask, Loc);
6743 -- Add a call to Abort_Undefer at the very beginning of the task
6744 -- body since this body is called with abort still deferred.
6746 if Abort_Allowed then
6747 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
6748 Insert_Before
6749 (First (Statements (Handled_Statement_Sequence (N))), Call);
6750 Analyze (Call);
6751 end if;
6753 -- The statement part has already been protected with an at_end and
6754 -- cleanup actions. The call to Complete_Activation must be placed
6755 -- at the head of the sequence of statements of that block. The
6756 -- declarations have been merged in this sequence of statements but
6757 -- the first real statement is accessible from the First_Real_Statement
6758 -- field (which was set for exactly this purpose).
6760 if Restricted_Profile then
6761 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
6762 else
6763 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
6764 end if;
6766 Insert_Before
6767 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
6768 Analyze (Call);
6770 New_N :=
6771 Make_Subprogram_Body (Loc,
6772 Specification => Build_Task_Proc_Specification (Ttyp),
6773 Declarations => Declarations (N),
6774 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
6776 -- If the task contains generic instantiations, cleanup actions
6777 -- are delayed until after instantiation. Transfer the activation
6778 -- chain to the subprogram, to insure that the activation call is
6779 -- properly generated. It the task body contains inner tasks, indicate
6780 -- that the subprogram is a task master.
6782 if Delay_Cleanups (Ttyp) then
6783 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
6784 Set_Is_Task_Master (New_N, Is_Task_Master (N));
6785 end if;
6787 Rewrite (N, New_N);
6788 Analyze (N);
6790 -- Set elaboration flag immediately after task body. If the body
6791 -- is a subunit, the flag is set in the declarative part that
6792 -- contains the stub.
6794 if Nkind (Parent (N)) /= N_Subunit then
6795 Insert_After (N,
6796 Make_Assignment_Statement (Loc,
6797 Name =>
6798 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
6799 Expression => New_Reference_To (Standard_True, Loc)));
6800 end if;
6801 end Expand_N_Task_Body;
6803 ------------------------------------
6804 -- Expand_N_Task_Type_Declaration --
6805 ------------------------------------
6807 -- We have several things to do. First we must create a Boolean flag used
6808 -- to mark if the body is elaborated yet. This variable gets set to True
6809 -- when the body of the task is elaborated (we can't rely on the normal
6810 -- ABE mechanism for the task body, since we need to pass an access to
6811 -- this elaboration boolean to the runtime routines).
6813 -- taskE : aliased Boolean := False;
6815 -- Next a variable is declared to hold the task stack size (either
6816 -- the default : Unspecified_Size, or a value that is set by a pragma
6817 -- Storage_Size). If the value of the pragma Storage_Size is static, then
6818 -- the variable is initialized with this value:
6820 -- taskZ : Size_Type := Unspecified_Size;
6821 -- or
6822 -- taskZ : Size_Type := Size_Type (size_expression);
6824 -- Next we create a corresponding record type declaration used to represent
6825 -- values of this task. The general form of this type declaration is
6827 -- type taskV (discriminants) is record
6828 -- _Task_Id : Task_Id;
6829 -- entry_family : array (bounds) of Void;
6830 -- _Priority : Integer := priority_expression;
6831 -- _Size : Size_Type := Size_Type (size_expression);
6832 -- _Task_Info : Task_Info_Type := task_info_expression;
6833 -- _Task_Name : Task_Image_Type := new String'(task_name_expression);
6834 -- end record;
6836 -- The discriminants are present only if the corresponding task type has
6837 -- discriminants, and they exactly mirror the task type discriminants.
6839 -- The Id field is always present. It contains the Task_Id value, as
6840 -- set by the call to Create_Task. Note that although the task is
6841 -- limited, the task value record type is not limited, so there is no
6842 -- problem in passing this field as an out parameter to Create_Task.
6844 -- One entry_family component is present for each entry family in the
6845 -- task definition. The bounds correspond to the bounds of the entry
6846 -- family (which may depend on discriminants). The element type is
6847 -- void, since we only need the bounds information for determining
6848 -- the entry index. Note that the use of an anonymous array would
6849 -- normally be illegal in this context, but this is a parser check,
6850 -- and the semantics is quite prepared to handle such a case.
6852 -- The _Size field is present only if a Storage_Size pragma appears in
6853 -- the task definition. The expression captures the argument that was
6854 -- present in the pragma, and is used to override the task stack size
6855 -- otherwise associated with the task type.
6857 -- The _Priority field is present only if a Priority or Interrupt_Priority
6858 -- pragma appears in the task definition. The expression captures the
6859 -- argument that was present in the pragma, and is used to provide
6860 -- the Size parameter to the call to Create_Task.
6862 -- The _Task_Info field is present only if a Task_Info pragma appears in
6863 -- the task definition. The expression captures the argument that was
6864 -- present in the pragma, and is used to provide the Task_Image parameter
6865 -- to the call to Create_Task.
6867 -- The _Task_Name field is present only if a Task_Name pragma appears in
6868 -- the task definition. The expression captures the argument that was
6869 -- present in the pragma, and is used to provide the Task_Id parameter
6870 -- to the call to Create_Task.
6872 -- When a task is declared, an instance of the task value record is
6873 -- created. The elaboration of this declaration creates the correct
6874 -- bounds for the entry families, and also evaluates the size, priority,
6875 -- and task_Info expressions if needed. The initialization routine for
6876 -- the task type itself then calls Create_Task with appropriate
6877 -- parameters to initialize the value of the Task_Id field.
6879 -- Note: the address of this record is passed as the "Discriminants"
6880 -- parameter for Create_Task. Since Create_Task merely passes this onto
6881 -- the body procedure, it does not matter that it does not quite match
6882 -- the GNARLI model of what is being passed (the record contains more
6883 -- than just the discriminants, but the discriminants can be found from
6884 -- the record value).
6886 -- The Entity_Id for this created record type is placed in the
6887 -- Corresponding_Record_Type field of the associated task type entity.
6889 -- Next we create a procedure specification for the task body procedure:
6891 -- procedure taskB (_Task : access taskV);
6893 -- Note that this must come after the record type declaration, since
6894 -- the spec refers to this type. It turns out that the initialization
6895 -- procedure for the value type references the task body spec, but that's
6896 -- fine, since it won't be generated till the freeze point for the type,
6897 -- which is certainly after the task body spec declaration.
6899 -- Finally, we set the task index value field of the entry attribute in
6900 -- the case of a simple entry.
6902 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
6903 Loc : constant Source_Ptr := Sloc (N);
6904 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
6905 Tasknm : constant Name_Id := Chars (Tasktyp);
6906 Taskdef : constant Node_Id := Task_Definition (N);
6908 Proc_Spec : Node_Id;
6909 Rec_Decl : Node_Id;
6910 Rec_Ent : Entity_Id;
6911 Cdecls : List_Id;
6912 Elab_Decl : Node_Id;
6913 Size_Decl : Node_Id;
6914 Body_Decl : Node_Id;
6916 begin
6917 -- Do not attempt expansion if in no run time mode
6919 if No_Run_Time
6920 and then not Restricted_Profile
6921 then
6922 Disallow_In_No_Run_Time_Mode (N);
6923 return;
6925 -- If already expanded, nothing to do
6927 elsif Present (Corresponding_Record_Type (Tasktyp)) then
6928 return;
6929 end if;
6931 -- Here we will do the expansion
6933 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
6934 Rec_Ent := Defining_Identifier (Rec_Decl);
6935 Cdecls := Component_Items (Component_List
6936 (Type_Definition (Rec_Decl)));
6938 Qualify_Entity_Names (N);
6940 -- First create the elaboration variable
6942 Elab_Decl :=
6943 Make_Object_Declaration (Loc,
6944 Defining_Identifier =>
6945 Make_Defining_Identifier (Sloc (Tasktyp),
6946 Chars => New_External_Name (Tasknm, 'E')),
6947 Aliased_Present => True,
6948 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
6949 Expression => New_Reference_To (Standard_False, Loc));
6950 Insert_After (N, Elab_Decl);
6952 -- Next create the declaration of the size variable (tasknmZ)
6954 Set_Storage_Size_Variable (Tasktyp,
6955 Make_Defining_Identifier (Sloc (Tasktyp),
6956 Chars => New_External_Name (Tasknm, 'Z')));
6958 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) and then
6959 Is_Static_Expression (Expression (First (
6960 Pragma_Argument_Associations (Find_Task_Or_Protected_Pragma (
6961 Taskdef, Name_Storage_Size)))))
6962 then
6963 Size_Decl :=
6964 Make_Object_Declaration (Loc,
6965 Defining_Identifier => Storage_Size_Variable (Tasktyp),
6966 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
6967 Expression =>
6968 Convert_To (RTE (RE_Size_Type),
6969 Relocate_Node (
6970 Expression (First (
6971 Pragma_Argument_Associations (
6972 Find_Task_Or_Protected_Pragma
6973 (Taskdef, Name_Storage_Size)))))));
6975 else
6976 Size_Decl :=
6977 Make_Object_Declaration (Loc,
6978 Defining_Identifier => Storage_Size_Variable (Tasktyp),
6979 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
6980 Expression => New_Reference_To (RTE (RE_Unspecified_Size), Loc));
6981 end if;
6983 Insert_After (Elab_Decl, Size_Decl);
6985 -- Next build the rest of the corresponding record declaration.
6986 -- This is done last, since the corresponding record initialization
6987 -- procedure will reference the previously created entities.
6989 -- Fill in the component declarations. First the _Task_Id field.
6991 Append_To (Cdecls,
6992 Make_Component_Declaration (Loc,
6993 Defining_Identifier =>
6994 Make_Defining_Identifier (Loc, Name_uTask_Id),
6995 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_ID), Loc)));
6997 -- Add components for entry families
6999 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
7001 -- Add the _Priority component if a Priority pragma is present
7003 if Present (Taskdef) and then Has_Priority_Pragma (Taskdef) then
7004 Append_To (Cdecls,
7005 Make_Component_Declaration (Loc,
7006 Defining_Identifier =>
7007 Make_Defining_Identifier (Loc, Name_uPriority),
7008 Subtype_Indication => New_Reference_To (Standard_Integer, Loc),
7009 Expression => New_Copy (
7010 Expression (First (
7011 Pragma_Argument_Associations (
7012 Find_Task_Or_Protected_Pragma
7013 (Taskdef, Name_Priority)))))));
7014 end if;
7016 -- Add the _Task_Size component if a Storage_Size pragma is present
7018 if Present (Taskdef)
7019 and then Has_Storage_Size_Pragma (Taskdef)
7020 then
7021 Append_To (Cdecls,
7022 Make_Component_Declaration (Loc,
7023 Defining_Identifier =>
7024 Make_Defining_Identifier (Loc, Name_uSize),
7026 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type), Loc),
7028 Expression =>
7029 Convert_To (RTE (RE_Size_Type),
7030 Relocate_Node (
7031 Expression (First (
7032 Pragma_Argument_Associations (
7033 Find_Task_Or_Protected_Pragma
7034 (Taskdef, Name_Storage_Size))))))));
7035 end if;
7037 -- Add the _Task_Info component if a Task_Info pragma is present
7039 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
7040 Append_To (Cdecls,
7041 Make_Component_Declaration (Loc,
7042 Defining_Identifier =>
7043 Make_Defining_Identifier (Loc, Name_uTask_Info),
7044 Subtype_Indication =>
7045 New_Reference_To (RTE (RE_Task_Info_Type), Loc),
7046 Expression => New_Copy (
7047 Expression (First (
7048 Pragma_Argument_Associations (
7049 Find_Task_Or_Protected_Pragma
7050 (Taskdef, Name_Task_Info)))))));
7051 end if;
7053 -- Add the _Task_Name component if a Task_Name pragma is present
7055 if Present (Taskdef) and then Has_Task_Name_Pragma (Taskdef) then
7056 Append_To (Cdecls,
7057 Make_Component_Declaration (Loc,
7058 Defining_Identifier =>
7059 Make_Defining_Identifier (Loc, Name_uTask_Info),
7060 Subtype_Indication =>
7061 New_Reference_To (RTE (RE_Task_Image_Type), Loc),
7062 Expression =>
7063 Make_Allocator (Loc,
7064 Expression =>
7065 Make_Qualified_Expression (Loc,
7066 Subtype_Mark =>
7067 New_Occurrence_Of (Standard_String, Loc),
7068 Expression =>
7069 New_Copy (
7070 Expression (First (
7071 Pragma_Argument_Associations (
7072 Find_Task_Or_Protected_Pragma
7073 (Taskdef, Name_Task_Name)))))))));
7074 end if;
7076 Insert_After (Size_Decl, Rec_Decl);
7078 -- Analyze the record declaration immediately after construction,
7079 -- because the initialization procedure is needed for single task
7080 -- declarations before the next entity is analyzed.
7082 Analyze (Rec_Decl);
7084 -- Create the declaration of the task body procedure
7086 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
7087 Body_Decl :=
7088 Make_Subprogram_Declaration (Loc,
7089 Specification => Proc_Spec);
7091 Insert_After (Rec_Decl, Body_Decl);
7093 -- Now we can freeze the corresponding record. This needs manually
7094 -- freezing, since it is really part of the task type, and the task
7095 -- type is frozen at this stage. We of course need the initialization
7096 -- procedure for this corresponding record type and we won't get it
7097 -- in time if we don't freeze now.
7099 declare
7100 L : constant List_Id := Freeze_Entity (Rec_Ent, Loc);
7102 begin
7103 if Is_Non_Empty_List (L) then
7104 Insert_List_After (Body_Decl, L);
7105 end if;
7106 end;
7108 -- Complete the expansion of access types to the current task
7109 -- type, if any were declared.
7111 Expand_Previous_Access_Type (Tasktyp);
7112 end Expand_N_Task_Type_Declaration;
7114 -------------------------------
7115 -- Expand_N_Timed_Entry_Call --
7116 -------------------------------
7118 -- A timed entry call in normal case is not implemented using ATC
7119 -- mechanism anymore for efficiency reason.
7121 -- select
7122 -- T.E;
7123 -- S1;
7124 -- or
7125 -- Delay D;
7126 -- S2;
7127 -- end select;
7129 -- is expanded as follow:
7131 -- 1) When T.E is a task entry_call;
7133 -- declare
7134 -- B : Boolean;
7135 -- X : Task_Entry_Index := <entry index>;
7136 -- DX : Duration := To_Duration (D);
7137 -- M : Delay_Mode := <discriminant>;
7138 -- P : parms := (parm, parm, parm);
7140 -- begin
7141 -- Timed_Protected_Entry_Call (<acceptor-task>, X, P'Address,
7142 -- DX, M, B);
7143 -- if B then
7144 -- S1;
7145 -- else
7146 -- S2;
7147 -- end if;
7148 -- end;
7150 -- 2) When T.E is a protected entry_call;
7152 -- declare
7153 -- B : Boolean;
7154 -- X : Protected_Entry_Index := <entry index>;
7155 -- DX : Duration := To_Duration (D);
7156 -- M : Delay_Mode := <discriminant>;
7157 -- P : parms := (parm, parm, parm);
7159 -- begin
7160 -- Timed_Protected_Entry_Call (<object>'unchecked_access, X,
7161 -- P'Address, DX, M, B);
7162 -- if B then
7163 -- S1;
7164 -- else
7165 -- S2;
7166 -- end if;
7167 -- end;
7169 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
7170 Loc : constant Source_Ptr := Sloc (N);
7172 E_Call : Node_Id :=
7173 Entry_Call_Statement (Entry_Call_Alternative (N));
7174 E_Stats : constant List_Id :=
7175 Statements (Entry_Call_Alternative (N));
7176 D_Stat : constant Node_Id :=
7177 Delay_Statement (Delay_Alternative (N));
7178 D_Stats : constant List_Id :=
7179 Statements (Delay_Alternative (N));
7181 Stmts : List_Id;
7182 Stmt : Node_Id;
7183 Parms : List_Id;
7184 Parm : Node_Id;
7186 Concval : Node_Id;
7187 Ename : Node_Id;
7188 Index : Node_Id;
7190 Decls : List_Id;
7191 Disc : Node_Id;
7192 Conv : Node_Id;
7193 B : Entity_Id;
7194 D : Entity_Id;
7195 Dtyp : Entity_Id;
7196 M : Entity_Id;
7198 Call : Node_Id;
7199 Dummy : Node_Id;
7201 begin
7202 -- The arguments in the call may require dynamic allocation, and the
7203 -- call statement may have been transformed into a block. The block
7204 -- may contain additional declarations for internal entities, and the
7205 -- original call is found by sequential search.
7207 if Nkind (E_Call) = N_Block_Statement then
7208 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
7210 while Nkind (E_Call) /= N_Procedure_Call_Statement
7211 and then Nkind (E_Call) /= N_Entry_Call_Statement
7212 loop
7213 Next (E_Call);
7214 end loop;
7215 end if;
7217 -- Build an entry call using Simple_Entry_Call. We will use this as the
7218 -- base for creating appropriate calls.
7220 Extract_Entry (E_Call, Concval, Ename, Index);
7221 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
7223 Stmts := Statements (Handled_Statement_Sequence (E_Call));
7224 Decls := Declarations (E_Call);
7226 if No (Decls) then
7227 Decls := New_List;
7228 end if;
7230 Dtyp := Base_Type (Etype (Expression (D_Stat)));
7232 -- Use the type of the delay expression (Calendar or Real_Time)
7233 -- to generate the appropriate conversion.
7235 if Nkind (D_Stat) = N_Delay_Relative_Statement then
7236 Disc := Make_Integer_Literal (Loc, 0);
7237 Conv := Relocate_Node (Expression (D_Stat));
7239 elsif Is_RTE (Dtyp, RO_CA_Time) then
7240 Disc := Make_Integer_Literal (Loc, 1);
7241 Conv := Make_Function_Call (Loc,
7242 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
7243 New_List (New_Copy (Expression (D_Stat))));
7245 else pragma Assert (Is_RTE (Dtyp, RO_RT_Time));
7246 Disc := Make_Integer_Literal (Loc, 2);
7247 Conv := Make_Function_Call (Loc,
7248 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
7249 New_List (New_Copy (Expression (D_Stat))));
7250 end if;
7252 -- Create a Duration and a Delay_Mode object used for passing a delay
7253 -- value
7255 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
7256 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
7258 Append_To (Decls,
7259 Make_Object_Declaration (Loc,
7260 Defining_Identifier => D,
7261 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
7263 Append_To (Decls,
7264 Make_Object_Declaration (Loc,
7265 Defining_Identifier => M,
7266 Object_Definition => New_Reference_To (Standard_Integer, Loc),
7267 Expression => Disc));
7269 B := Make_Defining_Identifier (Loc, Name_uB);
7271 -- Create a boolean object used for a return parameter.
7273 Prepend_To (Decls,
7274 Make_Object_Declaration (Loc,
7275 Defining_Identifier => B,
7276 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7278 Stmt := First (Stmts);
7280 -- Skip assignments to temporaries created for in-out parameters.
7281 -- This makes unwarranted assumptions about the shape of the expanded
7282 -- tree for the call, and should be cleaned up ???
7284 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7285 Next (Stmt);
7286 end loop;
7288 -- Do the assignement at this stage only because the evaluation of the
7289 -- expression must not occur before (see ACVC C97302A).
7291 Insert_Before (Stmt,
7292 Make_Assignment_Statement (Loc,
7293 Name => New_Reference_To (D, Loc),
7294 Expression => Conv));
7296 Call := Stmt;
7298 Parms := Parameter_Associations (Call);
7300 -- For a protected type, we build a Timed_Protected_Entry_Call
7302 if Is_Protected_Type (Etype (Concval)) then
7304 -- Create a new call statement
7306 Parm := First (Parms);
7308 while Present (Parm)
7309 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
7310 loop
7311 Next (Parm);
7312 end loop;
7314 Dummy := Remove_Next (Next (Parm));
7316 -- In case some garbage is following the Cancel_Param, remove.
7318 Dummy := Next (Parm);
7320 -- Remove the mode of the Protected_Entry_Call call, the
7321 -- Communication_Block of the Protected_Entry_Call call, and add a
7322 -- Duration and a Delay_Mode parameter
7324 pragma Assert (Present (Parm));
7325 Rewrite (Parm, New_Reference_To (D, Loc));
7327 Rewrite (Dummy, New_Reference_To (M, Loc));
7329 -- Add a Boolean flag for successful entry call.
7331 Append_To (Parms, New_Reference_To (B, Loc));
7333 if Abort_Allowed
7334 or else Restrictions (No_Entry_Queue) = False
7335 or else Number_Entries (Etype (Concval)) > 1
7336 then
7337 Rewrite (Call,
7338 Make_Procedure_Call_Statement (Loc,
7339 Name =>
7340 New_Reference_To (RTE (RE_Timed_Protected_Entry_Call), Loc),
7341 Parameter_Associations => Parms));
7343 else
7344 Parm := First (Parms);
7346 while Present (Parm)
7347 and then not Is_RTE (Etype (Parm), RE_Protected_Entry_Index)
7348 loop
7349 Next (Parm);
7350 end loop;
7352 Remove (Parm);
7354 Rewrite (Call,
7355 Make_Procedure_Call_Statement (Loc,
7356 Name => New_Reference_To (
7357 RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
7358 Parameter_Associations => Parms));
7359 end if;
7361 -- For the task case, build a Timed_Task_Entry_Call
7363 else
7364 -- Create a new call statement
7366 Append_To (Parms, New_Reference_To (D, Loc));
7367 Append_To (Parms, New_Reference_To (M, Loc));
7368 Append_To (Parms, New_Reference_To (B, Loc));
7370 Rewrite (Call,
7371 Make_Procedure_Call_Statement (Loc,
7372 Name => New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
7373 Parameter_Associations => Parms));
7375 end if;
7377 Append_To (Stmts,
7378 Make_Implicit_If_Statement (N,
7379 Condition => New_Reference_To (B, Loc),
7380 Then_Statements => E_Stats,
7381 Else_Statements => D_Stats));
7383 Rewrite (N,
7384 Make_Block_Statement (Loc,
7385 Declarations => Decls,
7386 Handled_Statement_Sequence =>
7387 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7389 Analyze (N);
7391 end Expand_N_Timed_Entry_Call;
7393 ----------------------------------------
7394 -- Expand_Protected_Body_Declarations --
7395 ----------------------------------------
7397 -- Part of the expansion of a protected body involves the creation of
7398 -- a declaration that can be referenced from the statement sequences of
7399 -- the entry bodies:
7401 -- A : Address;
7403 -- This declaration is inserted in the declarations of the service
7404 -- entries procedure for the protected body, and it is important that
7405 -- it be inserted before the statements of the entry body statement
7406 -- sequences are analyzed. Thus it would be too late to create this
7407 -- declaration in the Expand_N_Protected_Body routine, which is why
7408 -- there is a separate procedure to be called directly from Sem_Ch9.
7410 -- Ann is used to hold the address of the record containing the parameters
7411 -- (see Expand_N_Entry_Call for more details on how this record is built).
7412 -- References to the parameters do an unchecked conversion of this address
7413 -- to a pointer to the required record type, and then access the field that
7414 -- holds the value of the required parameter. The entity for the address
7415 -- variable is held as the top stack element (i.e. the last element) of the
7416 -- Accept_Address stack in the corresponding entry entity, and this element
7417 -- must be set in place before the statements are processed.
7419 -- No stack is needed for entry bodies, since they cannot be nested, but
7420 -- it is kept for consistency between protected and task entries. The
7421 -- stack will never contain more than one element. There is also only one
7422 -- such variable for a given protected body, but this is placed on the
7423 -- Accept_Address stack of all of the entries, again for consistency.
7425 -- To expand the requeue statement, a label is provided at the end of
7426 -- the loop in the entry service routine created by the expander (see
7427 -- Expand_N_Protected_Body for details), so that the statement can be
7428 -- skipped after the requeue is complete. This label is created during the
7429 -- expansion of the entry body, which will take place after the expansion
7430 -- of the requeue statements that it contains, so a placeholder defining
7431 -- identifier is associated with the task type here.
7433 -- Another label is provided following case statement created by the
7434 -- expander. This label is need for implementing return statement from
7435 -- entry body so that a return can be expanded as a goto to this label.
7436 -- This label is created during the expansion of the entry body, which
7437 -- will take place after the expansion of the return statements that it
7438 -- contains. Therefore, just like the label for expanding requeues, we
7439 -- need another placeholder for the label.
7441 procedure Expand_Protected_Body_Declarations
7442 (N : Node_Id;
7443 Spec_Id : Entity_Id)
7445 Op : Node_Id;
7447 begin
7448 if Expander_Active then
7450 -- Associate privals with the first subprogram or entry
7451 -- body to be expanded. These are used to expand references
7452 -- to private data objects.
7454 Op := First_Protected_Operation (Declarations (N));
7456 if Present (Op) then
7457 Set_Discriminals (Parent (Spec_Id));
7458 Set_Privals (Parent (Spec_Id), Op, Sloc (N));
7459 end if;
7460 end if;
7461 end Expand_Protected_Body_Declarations;
7463 -------------------------
7464 -- External_Subprogram --
7465 -------------------------
7467 function External_Subprogram (E : Entity_Id) return Entity_Id is
7468 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
7469 Decl : constant Node_Id := Unit_Declaration_Node (E);
7471 begin
7472 -- If the protected operation is defined in the visible part of the
7473 -- protected type, or if it is an interrupt handler, the internal and
7474 -- external subprograms follow each other on the entity chain. If the
7475 -- operation is defined in the private part of the type, there is no
7476 -- need for a separate locking version of the operation, and internal
7477 -- calls use the protected_body_subprogram directly.
7479 if List_Containing (Decl) = Visible_Declarations (Parent (Decl))
7480 or else Is_Interrupt_Handler (E)
7481 then
7482 return Next_Entity (Subp);
7483 else
7484 return (Subp);
7485 end if;
7486 end External_Subprogram;
7488 -------------------
7489 -- Extract_Entry --
7490 -------------------
7492 procedure Extract_Entry
7493 (N : Node_Id;
7494 Concval : out Node_Id;
7495 Ename : out Node_Id;
7496 Index : out Node_Id)
7498 Nam : constant Node_Id := Name (N);
7500 begin
7501 -- For a simple entry, the name is a selected component, with the
7502 -- prefix being the task value, and the selector being the entry.
7504 if Nkind (Nam) = N_Selected_Component then
7505 Concval := Prefix (Nam);
7506 Ename := Selector_Name (Nam);
7507 Index := Empty;
7509 -- For a member of an entry family, the name is an indexed
7510 -- component where the prefix is a selected component,
7511 -- whose prefix in turn is the task value, and whose
7512 -- selector is the entry family. The single expression in
7513 -- the expressions list of the indexed component is the
7514 -- subscript for the family.
7516 else
7517 pragma Assert (Nkind (Nam) = N_Indexed_Component);
7518 Concval := Prefix (Prefix (Nam));
7519 Ename := Selector_Name (Prefix (Nam));
7520 Index := First (Expressions (Nam));
7521 end if;
7523 end Extract_Entry;
7525 -------------------
7526 -- Family_Offset --
7527 -------------------
7529 function Family_Offset
7530 (Loc : Source_Ptr;
7531 Hi : Node_Id;
7532 Lo : Node_Id;
7533 Ttyp : Entity_Id)
7534 return Node_Id
7536 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7537 -- If one of the bounds is a reference to a discriminant, replace
7538 -- with corresponding discriminal of type. Within the body of a task
7539 -- retrieve the renamed discriminant by simple visibility, using its
7540 -- generated name. Within a protected object, find the original dis-
7541 -- criminant and replace it with the discriminal of the current prot-
7542 -- ected operation.
7544 ------------------------------
7545 -- Convert_Discriminant_Ref --
7546 ------------------------------
7548 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7549 Loc : constant Source_Ptr := Sloc (Bound);
7550 B : Node_Id;
7551 D : Entity_Id;
7553 begin
7554 if Is_Entity_Name (Bound)
7555 and then Ekind (Entity (Bound)) = E_Discriminant
7556 then
7557 if Is_Task_Type (Ttyp)
7558 and then Has_Completion (Ttyp)
7559 then
7560 B := Make_Identifier (Loc, Chars (Entity (Bound)));
7561 Find_Direct_Name (B);
7563 elsif Is_Protected_Type (Ttyp) then
7564 D := First_Discriminant (Ttyp);
7566 while Chars (D) /= Chars (Entity (Bound)) loop
7567 Next_Discriminant (D);
7568 end loop;
7570 B := New_Reference_To (Discriminal (D), Loc);
7572 else
7573 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
7574 end if;
7576 elsif Nkind (Bound) = N_Attribute_Reference then
7577 return Bound;
7579 else
7580 B := New_Copy_Tree (Bound);
7581 end if;
7583 return
7584 Make_Attribute_Reference (Loc,
7585 Attribute_Name => Name_Pos,
7586 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
7587 Expressions => New_List (B));
7588 end Convert_Discriminant_Ref;
7590 -- Start of processing for Family_Offset
7592 begin
7593 return
7594 Make_Op_Subtract (Loc,
7595 Left_Opnd => Convert_Discriminant_Ref (Hi),
7596 Right_Opnd => Convert_Discriminant_Ref (Lo));
7598 end Family_Offset;
7600 -----------------
7601 -- Family_Size --
7602 -----------------
7604 function Family_Size
7605 (Loc : Source_Ptr;
7606 Hi : Node_Id;
7607 Lo : Node_Id;
7608 Ttyp : Entity_Id)
7609 return Node_Id
7611 Ityp : Entity_Id;
7613 begin
7614 if Is_Task_Type (Ttyp) then
7615 Ityp := RTE (RE_Task_Entry_Index);
7616 else
7617 Ityp := RTE (RE_Protected_Entry_Index);
7618 end if;
7620 return
7621 Make_Attribute_Reference (Loc,
7622 Prefix => New_Reference_To (Ityp, Loc),
7623 Attribute_Name => Name_Max,
7624 Expressions => New_List (
7625 Make_Op_Add (Loc,
7626 Left_Opnd =>
7627 Family_Offset (Loc, Hi, Lo, Ttyp),
7628 Right_Opnd =>
7629 Make_Integer_Literal (Loc, 1)),
7630 Make_Integer_Literal (Loc, 0)));
7631 end Family_Size;
7633 -----------------------------------
7634 -- Find_Task_Or_Protected_Pragma --
7635 -----------------------------------
7637 function Find_Task_Or_Protected_Pragma
7638 (T : Node_Id;
7639 P : Name_Id)
7640 return Node_Id
7642 N : Node_Id;
7644 begin
7645 N := First (Visible_Declarations (T));
7647 while Present (N) loop
7648 if Nkind (N) = N_Pragma then
7649 if Chars (N) = P then
7650 return N;
7652 elsif P = Name_Priority
7653 and then Chars (N) = Name_Interrupt_Priority
7654 then
7655 return N;
7657 else
7658 Next (N);
7659 end if;
7661 else
7662 Next (N);
7663 end if;
7664 end loop;
7666 N := First (Private_Declarations (T));
7668 while Present (N) loop
7669 if Nkind (N) = N_Pragma then
7670 if Chars (N) = P then
7671 return N;
7673 elsif P = Name_Priority
7674 and then Chars (N) = Name_Interrupt_Priority
7675 then
7676 return N;
7678 else
7679 Next (N);
7680 end if;
7682 else
7683 Next (N);
7684 end if;
7685 end loop;
7687 raise Program_Error;
7688 end Find_Task_Or_Protected_Pragma;
7690 -------------------------------
7691 -- First_Protected_Operation --
7692 -------------------------------
7694 function First_Protected_Operation (D : List_Id) return Node_Id is
7695 First_Op : Node_Id;
7697 begin
7698 First_Op := First (D);
7699 while Present (First_Op)
7700 and then Nkind (First_Op) /= N_Subprogram_Body
7701 and then Nkind (First_Op) /= N_Entry_Body
7702 loop
7703 Next (First_Op);
7704 end loop;
7706 return First_Op;
7707 end First_Protected_Operation;
7709 --------------------------------
7710 -- Index_Constant_Declaration --
7711 --------------------------------
7713 function Index_Constant_Declaration
7714 (N : Node_Id;
7715 Index_Id : Entity_Id;
7716 Prot : Entity_Id)
7717 return List_Id
7719 Loc : constant Source_Ptr := Sloc (N);
7720 Decls : List_Id := New_List;
7721 Index_Con : constant Entity_Id := Entry_Index_Constant (Index_Id);
7722 Index_Typ : Entity_Id;
7724 Hi : Node_Id := Type_High_Bound (Etype (Index_Id));
7725 Lo : Node_Id := Type_Low_Bound (Etype (Index_Id));
7727 function Replace_Discriminant (Bound : Node_Id) return Node_Id;
7728 -- The bounds of the entry index may depend on discriminants, so
7729 -- each declaration of an entry_index_constant must have its own
7730 -- subtype declaration, using the local renaming of the object discri-
7731 -- minant.
7733 --------------------------
7734 -- Replace_Discriminant --
7735 --------------------------
7737 function Replace_Discriminant (Bound : Node_Id) return Node_Id is
7738 begin
7739 if Nkind (Bound) = N_Identifier
7740 and then Ekind (Entity (Bound)) = E_Constant
7741 and then Present (Discriminal_Link (Entity (Bound)))
7742 then
7743 return Make_Identifier (Loc, Chars (Entity (Bound)));
7744 else
7745 return Duplicate_Subexpr (Bound);
7746 end if;
7747 end Replace_Discriminant;
7749 -- Start of processing for Index_Constant_Declaration
7751 begin
7752 Set_Discriminal_Link (Index_Con, Index_Id);
7754 if Is_Entity_Name (
7755 Original_Node (Discrete_Subtype_Definition (Parent (Index_Id))))
7756 then
7757 -- Simple case: entry family is given by a subtype mark, and index
7758 -- constant has the same type, no replacement needed.
7760 Index_Typ := Etype (Index_Id);
7762 else
7763 Hi := Replace_Discriminant (Hi);
7764 Lo := Replace_Discriminant (Lo);
7766 Index_Typ := Make_Defining_Identifier (Loc, New_Internal_Name ('I'));
7768 Append (
7769 Make_Subtype_Declaration (Loc,
7770 Defining_Identifier => Index_Typ,
7771 Subtype_Indication =>
7772 Make_Subtype_Indication (Loc,
7773 Subtype_Mark =>
7774 New_Occurrence_Of (Base_Type (Etype (Index_Id)), Loc),
7775 Constraint =>
7776 Make_Range_Constraint (Loc,
7777 Range_Expression => Make_Range (Loc, Lo, Hi)))),
7778 Decls);
7780 end if;
7782 Append (
7783 Make_Object_Declaration (Loc,
7784 Defining_Identifier => Index_Con,
7785 Constant_Present => True,
7786 Object_Definition => New_Occurrence_Of (Index_Typ, Loc),
7788 Expression =>
7789 Make_Attribute_Reference (Loc,
7790 Prefix => New_Reference_To (Index_Typ, Loc),
7791 Attribute_Name => Name_Val,
7793 Expressions => New_List (
7795 Make_Op_Add (Loc,
7796 Left_Opnd =>
7797 Make_Op_Subtract (Loc,
7798 Left_Opnd => Make_Identifier (Loc, Name_uE),
7799 Right_Opnd =>
7800 Entry_Index_Expression (Loc,
7801 Defining_Identifier (N), Empty, Prot)),
7803 Right_Opnd =>
7804 Make_Attribute_Reference (Loc,
7805 Prefix => New_Reference_To (Index_Typ, Loc),
7806 Attribute_Name => Name_Pos,
7807 Expressions => New_List (
7808 Make_Attribute_Reference (Loc,
7809 Prefix => New_Reference_To (Index_Typ, Loc),
7810 Attribute_Name => Name_First))))))),
7811 Decls);
7813 return Decls;
7814 end Index_Constant_Declaration;
7816 --------------------------------
7817 -- Make_Initialize_Protection --
7818 --------------------------------
7820 function Make_Initialize_Protection
7821 (Protect_Rec : Entity_Id)
7822 return List_Id
7824 Loc : constant Source_Ptr := Sloc (Protect_Rec);
7825 P_Arr : Entity_Id;
7826 Pdef : Node_Id;
7827 Pdec : Node_Id;
7828 Ptyp : Node_Id;
7829 Pnam : Name_Id;
7830 Args : List_Id;
7831 L : List_Id := New_List;
7833 begin
7834 -- We may need two calls to properly initialize the object, one
7835 -- to Initialize_Protection, and possibly one to Install_Handlers
7836 -- if we have a pragma Attach_Handler.
7838 Ptyp := Corresponding_Concurrent_Type (Protect_Rec);
7839 Pnam := Chars (Ptyp);
7841 -- Get protected declaration. In the case of a task type declaration,
7842 -- this is simply the parent of the protected type entity.
7843 -- In the single protected object
7844 -- declaration, this parent will be the implicit type, and we can find
7845 -- the corresponding single protected object declaration by
7846 -- searching forward in the declaration list in the tree.
7847 -- ??? I am not sure that the test for N_Single_Protected_Declaration
7848 -- is needed here. Nodes of this type should have been removed
7849 -- during semantic analysis.
7851 Pdec := Parent (Ptyp);
7853 while Nkind (Pdec) /= N_Protected_Type_Declaration
7854 and then Nkind (Pdec) /= N_Single_Protected_Declaration
7855 loop
7856 Next (Pdec);
7857 end loop;
7859 -- Now we can find the object definition from this declaration
7861 Pdef := Protected_Definition (Pdec);
7863 -- Build the parameter list for the call. Note that _Init is the name
7864 -- of the formal for the object to be initialized, which is the task
7865 -- value record itself.
7867 Args := New_List;
7869 -- Object parameter. This is a pointer to the object of type
7870 -- Protection used by the GNARL to control the protected object.
7872 Append_To (Args,
7873 Make_Attribute_Reference (Loc,
7874 Prefix =>
7875 Make_Selected_Component (Loc,
7876 Prefix => Make_Identifier (Loc, Name_uInit),
7877 Selector_Name => Make_Identifier (Loc, Name_uObject)),
7878 Attribute_Name => Name_Unchecked_Access));
7880 -- Priority parameter. Set to Unspecified_Priority unless there is a
7881 -- priority pragma, in which case we take the value from the pragma,
7882 -- or there is an interrupt pragma and no priority pragma, and we
7883 -- set the ceiling to Interrupt_Priority'Last, an implementation-
7884 -- defined value, see D.3(10).
7886 if Present (Pdef)
7887 and then Has_Priority_Pragma (Pdef)
7888 then
7889 Append_To (Args,
7890 Duplicate_Subexpr (Expression (First (Pragma_Argument_Associations
7891 (Find_Task_Or_Protected_Pragma (Pdef, Name_Priority))))));
7893 elsif Has_Interrupt_Handler (Ptyp)
7894 or else Has_Attach_Handler (Ptyp)
7895 then
7896 -- When no priority is specified but an xx_Handler pragma is,
7897 -- we default to System.Interrupts.Default_Interrupt_Priority,
7898 -- see D.3(10).
7900 Append_To (Args,
7901 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
7903 else
7904 Append_To (Args,
7905 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
7906 end if;
7908 if Has_Entries (Ptyp)
7909 or else Has_Interrupt_Handler (Ptyp)
7910 or else Has_Attach_Handler (Ptyp)
7911 then
7912 -- Compiler_Info parameter. This parameter allows entry body
7913 -- procedures and barrier functions to be called from the runtime.
7914 -- It is a pointer to the record generated by the compiler to
7915 -- represent the protected object.
7917 Append_To (Args,
7918 Make_Attribute_Reference (Loc,
7919 Prefix => Make_Identifier (Loc, Name_uInit),
7920 Attribute_Name => Name_Address));
7922 if Has_Entries (Ptyp) then
7923 -- Entry_Bodies parameter. This is a pointer to an array of
7924 -- pointers to the entry body procedures and barrier functions
7925 -- of the object. If the protected type has no entries this
7926 -- object will not exist; in this case, pass a null.
7928 P_Arr := Entry_Bodies_Array (Ptyp);
7930 Append_To (Args,
7931 Make_Attribute_Reference (Loc,
7932 Prefix => New_Reference_To (P_Arr, Loc),
7933 Attribute_Name => Name_Unrestricted_Access));
7935 if Abort_Allowed
7936 or else Restrictions (No_Entry_Queue) = False
7937 or else Number_Entries (Ptyp) > 1
7938 then
7939 -- Find index mapping function (clumsy but ok for now).
7941 while Ekind (P_Arr) /= E_Function loop
7942 Next_Entity (P_Arr);
7943 end loop;
7945 Append_To (Args,
7946 Make_Attribute_Reference (Loc,
7947 Prefix =>
7948 New_Reference_To (P_Arr, Loc),
7949 Attribute_Name => Name_Unrestricted_Access));
7950 end if;
7952 else
7953 Append_To (Args, Make_Null (Loc));
7954 Append_To (Args, Make_Null (Loc));
7955 end if;
7957 if Abort_Allowed
7958 or else Restrictions (No_Entry_Queue) = False
7959 or else Number_Entries (Ptyp) > 1
7960 then
7961 Append_To (L,
7962 Make_Procedure_Call_Statement (Loc,
7963 Name => New_Reference_To (
7964 RTE (RE_Initialize_Protection_Entries), Loc),
7965 Parameter_Associations => Args));
7967 else
7968 Append_To (L,
7969 Make_Procedure_Call_Statement (Loc,
7970 Name => New_Reference_To (
7971 RTE (RE_Initialize_Protection_Entry), Loc),
7972 Parameter_Associations => Args));
7973 end if;
7975 else
7976 Append_To (L,
7977 Make_Procedure_Call_Statement (Loc,
7978 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
7979 Parameter_Associations => Args));
7980 end if;
7982 if Has_Attach_Handler (Ptyp) then
7984 -- We have a list of N Attach_Handler (ProcI, ExprI),
7985 -- and we have to make the following call:
7986 -- Install_Handlers (_object,
7987 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
7989 declare
7990 Args : List_Id := New_List;
7991 Table : List_Id := New_List;
7992 Ritem : Node_Id := First_Rep_Item (Ptyp);
7994 begin
7995 -- Appends the _object argument
7997 Append_To (Args,
7998 Make_Attribute_Reference (Loc,
7999 Prefix =>
8000 Make_Selected_Component (Loc,
8001 Prefix => Make_Identifier (Loc, Name_uInit),
8002 Selector_Name => Make_Identifier (Loc, Name_uObject)),
8003 Attribute_Name => Name_Unchecked_Access));
8005 -- Build the Attach_Handler table argument
8007 while Present (Ritem) loop
8008 if Nkind (Ritem) = N_Pragma
8009 and then Chars (Ritem) = Name_Attach_Handler
8010 then
8011 declare
8012 Handler : Node_Id :=
8013 First (Pragma_Argument_Associations (Ritem));
8014 Interrupt : Node_Id :=
8015 Next (Handler);
8017 begin
8018 Append_To (Table,
8019 Make_Aggregate (Loc, Expressions => New_List (
8020 Duplicate_Subexpr (Expression (Interrupt)),
8021 Make_Attribute_Reference (Loc,
8022 Prefix => Make_Selected_Component (Loc,
8023 Make_Identifier (Loc, Name_uInit),
8024 Duplicate_Subexpr (Expression (Handler))),
8025 Attribute_Name => Name_Access))));
8026 end;
8027 end if;
8029 Next_Rep_Item (Ritem);
8030 end loop;
8032 -- Appends the table argument we just built.
8033 Append_To (Args, Make_Aggregate (Loc, Table));
8035 -- Appends the Install_Handler call to the statements.
8036 Append_To (L,
8037 Make_Procedure_Call_Statement (Loc,
8038 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
8039 Parameter_Associations => Args));
8040 end;
8041 end if;
8043 return L;
8044 end Make_Initialize_Protection;
8046 ---------------------------
8047 -- Make_Task_Create_Call --
8048 ---------------------------
8050 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
8051 Loc : constant Source_Ptr := Sloc (Task_Rec);
8052 Name : Node_Id;
8053 Tdef : Node_Id;
8054 Tdec : Node_Id;
8055 Ttyp : Node_Id;
8056 Tnam : Name_Id;
8057 Args : List_Id;
8058 Ecount : Node_Id;
8060 begin
8061 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
8062 Tnam := Chars (Ttyp);
8064 -- Get task declaration. In the case of a task type declaration, this
8065 -- is simply the parent of the task type entity. In the single task
8066 -- declaration, this parent will be the implicit type, and we can find
8067 -- the corresponding single task declaration by searching forward in
8068 -- the declaration list in the tree.
8069 -- ??? I am not sure that the test for N_Single_Task_Declaration
8070 -- is needed here. Nodes of this type should have been removed
8071 -- during semantic analysis.
8073 Tdec := Parent (Ttyp);
8075 while Nkind (Tdec) /= N_Task_Type_Declaration
8076 and then Nkind (Tdec) /= N_Single_Task_Declaration
8077 loop
8078 Next (Tdec);
8079 end loop;
8081 -- Now we can find the task definition from this declaration
8083 Tdef := Task_Definition (Tdec);
8085 -- Build the parameter list for the call. Note that _Init is the name
8086 -- of the formal for the object to be initialized, which is the task
8087 -- value record itself.
8089 Args := New_List;
8091 -- Priority parameter. Set to Unspecified_Priority unless there is a
8092 -- priority pragma, in which case we take the value from the pragma.
8094 if Present (Tdef)
8095 and then Has_Priority_Pragma (Tdef)
8096 then
8097 Append_To (Args,
8098 Make_Selected_Component (Loc,
8099 Prefix => Make_Identifier (Loc, Name_uInit),
8100 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
8102 else
8103 Append_To (Args,
8104 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
8105 end if;
8107 -- Size parameter. If no Storage_Size pragma is present, then
8108 -- the size is taken from the taskZ variable for the type, which
8109 -- is either Unspecified_Size, or has been reset by the use of
8110 -- a Storage_Size attribute definition clause. If a pragma is
8111 -- present, then the size is taken from the _Size field of the
8112 -- task value record, which was set from the pragma value.
8114 if Present (Tdef)
8115 and then Has_Storage_Size_Pragma (Tdef)
8116 then
8117 Append_To (Args,
8118 Make_Selected_Component (Loc,
8119 Prefix => Make_Identifier (Loc, Name_uInit),
8120 Selector_Name => Make_Identifier (Loc, Name_uSize)));
8122 else
8123 Append_To (Args,
8124 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
8125 end if;
8127 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
8128 -- Task_Info pragma, in which case we take the value from the pragma.
8130 if Present (Tdef)
8131 and then Has_Task_Info_Pragma (Tdef)
8132 then
8133 Append_To (Args,
8134 Make_Selected_Component (Loc,
8135 Prefix => Make_Identifier (Loc, Name_uInit),
8136 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
8138 else
8139 Append_To (Args,
8140 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
8141 end if;
8143 if not Restricted_Profile then
8145 -- Number of entries. This is an expression of the form:
8147 -- n + _Init.a'Length + _Init.a'B'Length + ...
8149 -- where a,b... are the entry family names for the task definition
8151 Ecount := Build_Entry_Count_Expression (
8152 Ttyp,
8153 Component_Items (Component_List (
8154 Type_Definition (Parent (
8155 Corresponding_Record_Type (Ttyp))))),
8156 Loc);
8157 Append_To (Args, Ecount);
8159 -- Master parameter. This is a reference to the _Master parameter of
8160 -- the initialization procedure, except in the case of the pragma
8161 -- Restrictions (No_Task_Hierarchy) where the value is fixed to 3.
8162 -- See comments in System.Tasking.Initialization.Init_RTS for the
8163 -- value 3.
8165 if Restrictions (No_Task_Hierarchy) = False then
8166 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
8167 else
8168 Append_To (Args, Make_Integer_Literal (Loc, 3));
8169 end if;
8170 end if;
8172 -- State parameter. This is a pointer to the task body procedure. The
8173 -- required value is obtained by taking the address of the task body
8174 -- procedure and converting it (with an unchecked conversion) to the
8175 -- type required by the task kernel. For further details, see the
8176 -- description of Expand_Task_Body
8178 Append_To (Args,
8179 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
8180 Make_Attribute_Reference (Loc,
8181 Prefix =>
8182 New_Occurrence_Of (Get_Task_Body_Procedure (Ttyp), Loc),
8183 Attribute_Name => Name_Address)));
8185 -- Discriminants parameter. This is just the address of the task
8186 -- value record itself (which contains the discriminant values
8188 Append_To (Args,
8189 Make_Attribute_Reference (Loc,
8190 Prefix => Make_Identifier (Loc, Name_uInit),
8191 Attribute_Name => Name_Address));
8193 -- Elaborated parameter. This is an access to the elaboration Boolean
8195 Append_To (Args,
8196 Make_Attribute_Reference (Loc,
8197 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
8198 Attribute_Name => Name_Unchecked_Access));
8200 -- Chain parameter. This is a reference to the _Chain parameter of
8201 -- the initialization procedure.
8203 Append_To (Args, Make_Identifier (Loc, Name_uChain));
8205 -- Task name parameter. Take this from the _Task_Info parameter to the
8206 -- init call unless there is a Task_Name pragma, in which case we take
8207 -- the value from the pragma.
8209 if Present (Tdef)
8210 and then Has_Task_Name_Pragma (Tdef)
8211 then
8212 Append_To (Args,
8213 Make_Selected_Component (Loc,
8214 Prefix => Make_Identifier (Loc, Name_uInit),
8215 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
8217 else
8218 Append_To (Args, Make_Identifier (Loc, Name_uTask_Id));
8219 end if;
8221 -- Created_Task parameter. This is the _Task_Id field of the task
8222 -- record value
8224 Append_To (Args,
8225 Make_Selected_Component (Loc,
8226 Prefix => Make_Identifier (Loc, Name_uInit),
8227 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
8229 if Restricted_Profile then
8230 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
8231 else
8232 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
8233 end if;
8235 return Make_Procedure_Call_Statement (Loc,
8236 Name => Name, Parameter_Associations => Args);
8237 end Make_Task_Create_Call;
8239 ------------------------------
8240 -- Next_Protected_Operation --
8241 ------------------------------
8243 function Next_Protected_Operation (N : Node_Id) return Node_Id is
8244 Next_Op : Node_Id;
8246 begin
8247 Next_Op := Next (N);
8249 while Present (Next_Op)
8250 and then Nkind (Next_Op) /= N_Subprogram_Body
8251 and then Nkind (Next_Op) /= N_Entry_Body
8252 loop
8253 Next (Next_Op);
8254 end loop;
8256 return Next_Op;
8257 end Next_Protected_Operation;
8259 ----------------------
8260 -- Set_Discriminals --
8261 ----------------------
8263 procedure Set_Discriminals (Dec : Node_Id) is
8264 D : Entity_Id;
8265 Pdef : Entity_Id;
8266 D_Minal : Entity_Id;
8268 begin
8269 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
8270 Pdef := Defining_Identifier (Dec);
8272 if Has_Discriminants (Pdef) then
8273 D := First_Discriminant (Pdef);
8275 while Present (D) loop
8276 D_Minal :=
8277 Make_Defining_Identifier (Sloc (D),
8278 Chars => New_External_Name (Chars (D), 'D'));
8280 Set_Ekind (D_Minal, E_Constant);
8281 Set_Etype (D_Minal, Etype (D));
8282 Set_Discriminal (D, D_Minal);
8283 Set_Discriminal_Link (D_Minal, D);
8285 Next_Discriminant (D);
8286 end loop;
8287 end if;
8288 end Set_Discriminals;
8290 -----------------
8291 -- Set_Privals --
8292 -----------------
8294 procedure Set_Privals
8295 (Dec : Node_Id;
8296 Op : Node_Id;
8297 Loc : Source_Ptr)
8299 P_Decl : Node_Id;
8300 P_Id : Entity_Id;
8301 Priv : Entity_Id;
8302 Def : Node_Id;
8303 Body_Ent : Entity_Id;
8304 Prec_Decl : constant Node_Id :=
8305 Parent (Corresponding_Record_Type
8306 (Defining_Identifier (Dec)));
8307 Prec_Def : constant Entity_Id := Type_Definition (Prec_Decl);
8308 Obj_Decl : Node_Id;
8309 P_Subtype : Entity_Id;
8310 New_Decl : Entity_Id;
8311 Assoc_L : Elist_Id := New_Elmt_List;
8312 Op_Id : Entity_Id;
8314 begin
8315 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
8316 pragma Assert
8317 (Nkind (Op) = N_Subprogram_Body or else Nkind (Op) = N_Entry_Body);
8319 Def := Protected_Definition (Dec);
8321 if Present (Private_Declarations (Def)) then
8323 P_Decl := First (Private_Declarations (Def));
8325 while Present (P_Decl) loop
8326 if Nkind (P_Decl) = N_Component_Declaration then
8327 P_Id := Defining_Identifier (P_Decl);
8328 Priv :=
8329 Make_Defining_Identifier (Loc,
8330 New_External_Name (Chars (P_Id), 'P'));
8332 Set_Ekind (Priv, E_Variable);
8333 Set_Etype (Priv, Etype (P_Id));
8334 Set_Scope (Priv, Scope (P_Id));
8335 Set_Esize (Priv, Esize (Etype (P_Id)));
8336 Set_Alignment (Priv, Alignment (Etype (P_Id)));
8338 -- If the type of the component is an itype, we must
8339 -- create a new itype for the corresponding prival in
8340 -- each protected operation, to avoid scoping problems.
8341 -- We create new itypes by copying the tree for the
8342 -- component definition.
8344 if Is_Itype (Etype (P_Id)) then
8345 Append_Elmt (P_Id, Assoc_L);
8346 Append_Elmt (Priv, Assoc_L);
8348 if Nkind (Op) = N_Entry_Body then
8349 Op_Id := Defining_Identifier (Op);
8350 else
8351 Op_Id := Defining_Unit_Name (Specification (Op));
8352 end if;
8354 New_Decl := New_Copy_Tree (P_Decl, Assoc_L,
8355 New_Scope => Op_Id);
8356 end if;
8358 Set_Protected_Operation (P_Id, Op);
8359 Set_Prival (P_Id, Priv);
8360 end if;
8362 Next (P_Decl);
8363 end loop;
8364 end if;
8366 -- There is one more implicit private declaration: the object
8367 -- itself. A "prival" for this is attached to the protected
8368 -- body defining identifier.
8370 Body_Ent := Corresponding_Body (Dec);
8372 Priv :=
8373 Make_Defining_Identifier (Sloc (Body_Ent),
8374 Chars => New_External_Name (Chars (Body_Ent), 'R'));
8376 -- Set the Etype to the implicit subtype of Protection created when
8377 -- the protected type declaration was expanded. This node will not
8378 -- be analyzed until it is used as the defining identifier for the
8379 -- renaming declaration in the protected operation body, and it will
8380 -- be needed in the references expanded before that body is expanded.
8381 -- Since the Protection field is aliased, set Is_Aliased as well.
8383 Obj_Decl := First (Component_Items (Component_List (Prec_Def)));
8384 while Chars (Defining_Identifier (Obj_Decl)) /= Name_uObject loop
8385 Next (Obj_Decl);
8386 end loop;
8388 P_Subtype := Etype (Defining_Identifier (Obj_Decl));
8389 Set_Etype (Priv, P_Subtype);
8390 Set_Is_Aliased (Priv);
8391 Set_Object_Ref (Body_Ent, Priv);
8393 end Set_Privals;
8395 ----------------------------
8396 -- Update_Prival_Subtypes --
8397 ----------------------------
8399 procedure Update_Prival_Subtypes (N : Node_Id) is
8401 function Process (N : Node_Id) return Traverse_Result;
8402 -- Update the etype of occurrences of privals whose etype does not
8403 -- match the current Etype of the prival entity itself.
8405 procedure Update_Array_Bounds (E : Entity_Id);
8406 -- Itypes generated for array expressions may depend on the
8407 -- determinants of the protected object, and need to be processed
8408 -- separately because they are not attached to the tree.
8410 -------------
8411 -- Process --
8412 -------------
8414 function Process (N : Node_Id) return Traverse_Result is
8415 begin
8416 if Is_Entity_Name (N) then
8417 declare
8418 E : Entity_Id := Entity (N);
8420 begin
8421 if Present (E)
8422 and then (Ekind (E) = E_Constant
8423 or else Ekind (E) = E_Variable)
8424 and then Nkind (Parent (E)) = N_Object_Renaming_Declaration
8425 and then not Is_Scalar_Type (Etype (E))
8426 and then Etype (N) /= Etype (E)
8427 then
8428 Set_Etype (N, Etype (Entity (Original_Node (N))));
8430 -- If the prefix has an actual subtype that is different
8431 -- from the nominal one, update the types of the indices,
8432 -- so that the proper constraints are applied. Do not
8433 -- apply this transformation to a packed array, where the
8434 -- index type is computed for a byte array and is different
8435 -- from the source index.
8437 if Nkind (Parent (N)) = N_Indexed_Component
8438 and then
8439 not Is_Bit_Packed_Array (Etype (Prefix (Parent (N))))
8440 then
8441 declare
8442 Indx1 : Node_Id;
8443 I_Typ : Node_Id;
8445 begin
8446 Indx1 := First (Expressions (Parent (N)));
8447 I_Typ := First_Index (Etype (N));
8449 while Present (Indx1) and then Present (I_Typ) loop
8451 if not Is_Entity_Name (Indx1) then
8452 Set_Etype (Indx1, Base_Type (Etype (I_Typ)));
8453 end if;
8455 Next (Indx1);
8456 Next_Index (I_Typ);
8457 end loop;
8458 end;
8459 end if;
8461 elsif Present (E)
8462 and then Ekind (E) = E_Constant
8463 and then Present (Discriminal_Link (E))
8464 then
8465 Set_Etype (N, Etype (E));
8466 end if;
8467 end;
8469 return OK;
8471 elsif Nkind (N) = N_Defining_Identifier
8472 or else Nkind (N) = N_Defining_Operator_Symbol
8473 or else Nkind (N) = N_Defining_Character_Literal
8474 then
8475 return Skip;
8477 elsif Nkind (N) = N_String_Literal then
8478 -- array type, but bounds are constant.
8479 return OK;
8481 elsif Nkind (N) = N_Object_Declaration
8482 and then Is_Itype (Etype (Defining_Identifier (N)))
8483 and then Is_Array_Type (Etype (Defining_Identifier (N)))
8484 then
8485 Update_Array_Bounds (Etype (Defining_Identifier (N)));
8486 return OK;
8488 -- For array components of discriminated records, use the
8489 -- base type directly, because it may depend indirectly
8490 -- on the discriminants of the protected type. Cleaner would
8491 -- be a systematic mechanism to compute actual subtypes of
8492 -- private components ???
8494 elsif Nkind (N) in N_Has_Etype
8495 and then Present (Etype (N))
8496 and then Is_Array_Type (Etype (N))
8497 and then Nkind (N) = N_Selected_Component
8498 and then Has_Discriminants (Etype (Prefix (N)))
8499 then
8500 Set_Etype (N, Base_Type (Etype (N)));
8501 return OK;
8503 else
8504 if Nkind (N) in N_Has_Etype
8505 and then Present (Etype (N))
8506 and then Is_Itype (Etype (N)) then
8508 if Is_Array_Type (Etype (N)) then
8509 Update_Array_Bounds (Etype (N));
8511 elsif Is_Scalar_Type (Etype (N)) then
8512 Update_Prival_Subtypes (Type_Low_Bound (Etype (N)));
8513 Update_Prival_Subtypes (Type_High_Bound (Etype (N)));
8514 end if;
8515 end if;
8517 return OK;
8518 end if;
8519 end Process;
8521 -------------------------
8522 -- Update_Array_Bounds --
8523 -------------------------
8525 procedure Update_Array_Bounds (E : Entity_Id) is
8526 Ind : Node_Id;
8528 begin
8529 Ind := First_Index (E);
8531 while Present (Ind) loop
8532 Update_Prival_Subtypes (Type_Low_Bound (Etype (Ind)));
8533 Update_Prival_Subtypes (Type_High_Bound (Etype (Ind)));
8534 Next_Index (Ind);
8535 end loop;
8536 end Update_Array_Bounds;
8538 procedure Traverse is new Traverse_Proc;
8540 -- Start of processing for Update_Prival_Subtypes
8542 begin
8543 Traverse (N);
8544 end Update_Prival_Subtypes;
8546 end Exp_Ch9;