2014-10-31 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobb3f9ab6fc5e84db5d2bf7c471439361bfb924c61
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Prag; use Exp_Prag;
44 with Exp_Tss; use Exp_Tss;
45 with Exp_Util; use Exp_Util;
46 with Fname; use Fname;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
49 with Lib; use Lib;
50 with Namet; use Namet;
51 with Nlists; use Nlists;
52 with Nmake; use Nmake;
53 with Opt; use Opt;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_SCIL; use Sem_SCIL;
69 with Sem_Util; use Sem_Util;
70 with Sinfo; use Sinfo;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Stringt; use Stringt;
74 with Targparm; use Targparm;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Validsw; use Validsw;
79 package body Exp_Ch6 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Add_Access_Actual_To_Build_In_Place_Call
86 (Function_Call : Node_Id;
87 Function_Id : Entity_Id;
88 Return_Object : Node_Id;
89 Is_Access : Boolean := False);
90 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
91 -- object name given by Return_Object and add the attribute to the end of
92 -- the actual parameter list associated with the build-in-place function
93 -- call denoted by Function_Call. However, if Is_Access is True, then
94 -- Return_Object is already an access expression, in which case it's passed
95 -- along directly to the build-in-place function. Finally, if Return_Object
96 -- is empty, then pass a null literal as the actual.
98 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
99 (Function_Call : Node_Id;
100 Function_Id : Entity_Id;
101 Alloc_Form : BIP_Allocation_Form := Unspecified;
102 Alloc_Form_Exp : Node_Id := Empty;
103 Pool_Actual : Node_Id := Make_Null (No_Location));
104 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
105 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
106 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
107 -- otherwise pass a literal corresponding to the Alloc_Form parameter
108 -- (which must not be Unspecified in that case). Pool_Actual is the
109 -- parameter to pass to BIP_Storage_Pool.
111 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
112 (Func_Call : Node_Id;
113 Func_Id : Entity_Id;
114 Ptr_Typ : Entity_Id := Empty;
115 Master_Exp : Node_Id := Empty);
116 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
117 -- finalization actions, add an actual parameter which is a pointer to the
118 -- finalization master of the caller. If Master_Exp is not Empty, then that
119 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
120 -- will result in an automatic "null" value for the actual.
122 procedure Add_Task_Actuals_To_Build_In_Place_Call
123 (Function_Call : Node_Id;
124 Function_Id : Entity_Id;
125 Master_Actual : Node_Id;
126 Chain : Node_Id := Empty);
127 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
128 -- contains tasks, add two actual parameters: the master, and a pointer to
129 -- the caller's activation chain. Master_Actual is the actual parameter
130 -- expression to pass for the master. In most cases, this is the current
131 -- master (_master). The two exceptions are: If the function call is the
132 -- initialization expression for an allocator, we pass the master of the
133 -- access type. If the function call is the initialization expression for a
134 -- return object, we pass along the master passed in by the caller. In most
135 -- contexts, the activation chain to pass is the local one, which is
136 -- indicated by No (Chain). However, in an allocator, the caller passes in
137 -- the activation Chain. Note: Master_Actual can be Empty, but only if
138 -- there are no tasks.
140 procedure Check_Overriding_Operation (Subp : Entity_Id);
141 -- Subp is a dispatching operation. Check whether it may override an
142 -- inherited private operation, in which case its DT entry is that of
143 -- the hidden operation, not the one it may have received earlier.
144 -- This must be done before emitting the code to set the corresponding
145 -- DT to the address of the subprogram. The actual placement of Subp in
146 -- the proper place in the list of primitive operations is done in
147 -- Declare_Inherited_Private_Subprograms, which also has to deal with
148 -- implicit operations. This duplication is unavoidable for now???
150 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
151 -- This procedure is called only if the subprogram body N, whose spec
152 -- has the given entity Spec, contains a parameterless recursive call.
153 -- It attempts to generate runtime code to detect if this a case of
154 -- infinite recursion.
156 -- The body is scanned to determine dependencies. If the only external
157 -- dependencies are on a small set of scalar variables, then the values
158 -- of these variables are captured on entry to the subprogram, and if
159 -- the values are not changed for the call, we know immediately that
160 -- we have an infinite recursion.
162 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id);
163 -- For each actual of an in-out or out parameter which is a numeric
164 -- (view) conversion of the form T (A), where A denotes a variable,
165 -- we insert the declaration:
167 -- Temp : T[ := T (A)];
169 -- prior to the call. Then we replace the actual with a reference to Temp,
170 -- and append the assignment:
172 -- A := TypeA (Temp);
174 -- after the call. Here TypeA is the actual type of variable A. For out
175 -- parameters, the initial declaration has no expression. If A is not an
176 -- entity name, we generate instead:
178 -- Var : TypeA renames A;
179 -- Temp : T := Var; -- omitting expression for out parameter.
180 -- ...
181 -- Var := TypeA (Temp);
183 -- For other in-out parameters, we emit the required constraint checks
184 -- before and/or after the call.
186 -- For all parameter modes, actuals that denote components and slices of
187 -- packed arrays are expanded into suitable temporaries.
189 -- For non-scalar objects that are possibly unaligned, add call by copy
190 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
192 -- For OUT and IN OUT parameters, add predicate checks after the call
193 -- based on the predicates of the actual type.
195 -- The parameter N is IN OUT because in some cases, the expansion code
196 -- rewrites the call as an expression actions with the call inside. In
197 -- this case N is reset to point to the inside call so that the caller
198 -- can continue processing of this call.
200 procedure Expand_Ctrl_Function_Call (N : Node_Id);
201 -- N is a function call which returns a controlled object. Transform the
202 -- call into a temporary which retrieves the returned object from the
203 -- secondary stack using 'reference.
205 procedure Expand_Non_Function_Return (N : Node_Id);
206 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
207 -- a procedure body, entry body, accept statement, or extended return
208 -- statement. Note that all non-function returns are simple return
209 -- statements.
211 function Expand_Protected_Object_Reference
212 (N : Node_Id;
213 Scop : Entity_Id) return Node_Id;
215 procedure Expand_Protected_Subprogram_Call
216 (N : Node_Id;
217 Subp : Entity_Id;
218 Scop : Entity_Id);
219 -- A call to a protected subprogram within the protected object may appear
220 -- as a regular call. The list of actuals must be expanded to contain a
221 -- reference to the object itself, and the call becomes a call to the
222 -- corresponding protected subprogram.
224 function Has_Unconstrained_Access_Discriminants
225 (Subtyp : Entity_Id) return Boolean;
226 -- Returns True if the given subtype is unconstrained and has one
227 -- or more access discriminants.
229 procedure Expand_Simple_Function_Return (N : Node_Id);
230 -- Expand simple return from function. In the case where we are returning
231 -- from a function body this is called by Expand_N_Simple_Return_Statement.
233 ----------------------------------------------
234 -- Add_Access_Actual_To_Build_In_Place_Call --
235 ----------------------------------------------
237 procedure Add_Access_Actual_To_Build_In_Place_Call
238 (Function_Call : Node_Id;
239 Function_Id : Entity_Id;
240 Return_Object : Node_Id;
241 Is_Access : Boolean := False)
243 Loc : constant Source_Ptr := Sloc (Function_Call);
244 Obj_Address : Node_Id;
245 Obj_Acc_Formal : Entity_Id;
247 begin
248 -- Locate the implicit access parameter in the called function
250 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
252 -- If no return object is provided, then pass null
254 if not Present (Return_Object) then
255 Obj_Address := Make_Null (Loc);
256 Set_Parent (Obj_Address, Function_Call);
258 -- If Return_Object is already an expression of an access type, then use
259 -- it directly, since it must be an access value denoting the return
260 -- object, and couldn't possibly be the return object itself.
262 elsif Is_Access then
263 Obj_Address := Return_Object;
264 Set_Parent (Obj_Address, Function_Call);
266 -- Apply Unrestricted_Access to caller's return object
268 else
269 Obj_Address :=
270 Make_Attribute_Reference (Loc,
271 Prefix => Return_Object,
272 Attribute_Name => Name_Unrestricted_Access);
274 Set_Parent (Return_Object, Obj_Address);
275 Set_Parent (Obj_Address, Function_Call);
276 end if;
278 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
280 -- Build the parameter association for the new actual and add it to the
281 -- end of the function's actuals.
283 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
284 end Add_Access_Actual_To_Build_In_Place_Call;
286 ------------------------------------------------------
287 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
288 ------------------------------------------------------
290 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
291 (Function_Call : Node_Id;
292 Function_Id : Entity_Id;
293 Alloc_Form : BIP_Allocation_Form := Unspecified;
294 Alloc_Form_Exp : Node_Id := Empty;
295 Pool_Actual : Node_Id := Make_Null (No_Location))
297 Loc : constant Source_Ptr := Sloc (Function_Call);
298 Alloc_Form_Actual : Node_Id;
299 Alloc_Form_Formal : Node_Id;
300 Pool_Formal : Node_Id;
302 begin
303 -- The allocation form generally doesn't need to be passed in the case
304 -- of a constrained result subtype, since normally the caller performs
305 -- the allocation in that case. However this formal is still needed in
306 -- the case where the function has a tagged result, because generally
307 -- such functions can be called in a dispatching context and such calls
308 -- must be handled like calls to class-wide functions.
310 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
311 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
312 then
313 return;
314 end if;
316 -- Locate the implicit allocation form parameter in the called function.
317 -- Maybe it would be better for each implicit formal of a build-in-place
318 -- function to have a flag or a Uint attribute to identify it. ???
320 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
322 if Present (Alloc_Form_Exp) then
323 pragma Assert (Alloc_Form = Unspecified);
325 Alloc_Form_Actual := Alloc_Form_Exp;
327 else
328 pragma Assert (Alloc_Form /= Unspecified);
330 Alloc_Form_Actual :=
331 Make_Integer_Literal (Loc,
332 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
333 end if;
335 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
337 -- Build the parameter association for the new actual and add it to the
338 -- end of the function's actuals.
340 Add_Extra_Actual_To_Call
341 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
343 -- Pass the Storage_Pool parameter. This parameter is omitted on
344 -- .NET/JVM/ZFP as those targets do not support pools.
346 if VM_Target = No_VM
347 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
348 then
349 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
350 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
351 Add_Extra_Actual_To_Call
352 (Function_Call, Pool_Formal, Pool_Actual);
353 end if;
354 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
356 -----------------------------------------------------------
357 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
358 -----------------------------------------------------------
360 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
361 (Func_Call : Node_Id;
362 Func_Id : Entity_Id;
363 Ptr_Typ : Entity_Id := Empty;
364 Master_Exp : Node_Id := Empty)
366 begin
367 if not Needs_BIP_Finalization_Master (Func_Id) then
368 return;
369 end if;
371 declare
372 Formal : constant Entity_Id :=
373 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
374 Loc : constant Source_Ptr := Sloc (Func_Call);
376 Actual : Node_Id;
377 Desig_Typ : Entity_Id;
379 begin
380 -- If there is a finalization master actual, such as the implicit
381 -- finalization master of an enclosing build-in-place function,
382 -- then this must be added as an extra actual of the call.
384 if Present (Master_Exp) then
385 Actual := Master_Exp;
387 -- Case where the context does not require an actual master
389 elsif No (Ptr_Typ) then
390 Actual := Make_Null (Loc);
392 else
393 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
395 -- Check for a library-level access type whose designated type has
396 -- supressed finalization. Such an access types lack a master.
397 -- Pass a null actual to the callee in order to signal a missing
398 -- master.
400 if Is_Library_Level_Entity (Ptr_Typ)
401 and then Finalize_Storage_Only (Desig_Typ)
402 then
403 Actual := Make_Null (Loc);
405 -- Types in need of finalization actions
407 elsif Needs_Finalization (Desig_Typ) then
409 -- The general mechanism of creating finalization masters for
410 -- anonymous access types is disabled by default, otherwise
411 -- finalization masters will pop all over the place. Such types
412 -- use context-specific masters.
414 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
415 and then No (Finalization_Master (Ptr_Typ))
416 then
417 Build_Finalization_Master
418 (Typ => Ptr_Typ,
419 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
420 Encl_Scope => Scope (Ptr_Typ));
421 end if;
423 -- Access-to-controlled types should always have a master
425 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
427 Actual :=
428 Make_Attribute_Reference (Loc,
429 Prefix =>
430 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
431 Attribute_Name => Name_Unrestricted_Access);
433 -- Tagged types
435 else
436 Actual := Make_Null (Loc);
437 end if;
438 end if;
440 Analyze_And_Resolve (Actual, Etype (Formal));
442 -- Build the parameter association for the new actual and add it to
443 -- the end of the function's actuals.
445 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
446 end;
447 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
449 ------------------------------
450 -- Add_Extra_Actual_To_Call --
451 ------------------------------
453 procedure Add_Extra_Actual_To_Call
454 (Subprogram_Call : Node_Id;
455 Extra_Formal : Entity_Id;
456 Extra_Actual : Node_Id)
458 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
459 Param_Assoc : Node_Id;
461 begin
462 Param_Assoc :=
463 Make_Parameter_Association (Loc,
464 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
465 Explicit_Actual_Parameter => Extra_Actual);
467 Set_Parent (Param_Assoc, Subprogram_Call);
468 Set_Parent (Extra_Actual, Param_Assoc);
470 if Present (Parameter_Associations (Subprogram_Call)) then
471 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
472 N_Parameter_Association
473 then
475 -- Find last named actual, and append
477 declare
478 L : Node_Id;
479 begin
480 L := First_Actual (Subprogram_Call);
481 while Present (L) loop
482 if No (Next_Actual (L)) then
483 Set_Next_Named_Actual (Parent (L), Extra_Actual);
484 exit;
485 end if;
486 Next_Actual (L);
487 end loop;
488 end;
490 else
491 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
492 end if;
494 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
496 else
497 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
498 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
499 end if;
500 end Add_Extra_Actual_To_Call;
502 ---------------------------------------------
503 -- Add_Task_Actuals_To_Build_In_Place_Call --
504 ---------------------------------------------
506 procedure Add_Task_Actuals_To_Build_In_Place_Call
507 (Function_Call : Node_Id;
508 Function_Id : Entity_Id;
509 Master_Actual : Node_Id;
510 Chain : Node_Id := Empty)
512 Loc : constant Source_Ptr := Sloc (Function_Call);
513 Result_Subt : constant Entity_Id :=
514 Available_View (Etype (Function_Id));
515 Actual : Node_Id;
516 Chain_Actual : Node_Id;
517 Chain_Formal : Node_Id;
518 Master_Formal : Node_Id;
520 begin
521 -- No such extra parameters are needed if there are no tasks
523 if not Has_Task (Result_Subt) then
524 return;
525 end if;
527 Actual := Master_Actual;
529 -- Use a dummy _master actual in case of No_Task_Hierarchy
531 if Restriction_Active (No_Task_Hierarchy) then
532 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
534 -- In the case where we use the master associated with an access type,
535 -- the actual is an entity and requires an explicit reference.
537 elsif Nkind (Actual) = N_Defining_Identifier then
538 Actual := New_Occurrence_Of (Actual, Loc);
539 end if;
541 -- Locate the implicit master parameter in the called function
543 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
544 Analyze_And_Resolve (Actual, Etype (Master_Formal));
546 -- Build the parameter association for the new actual and add it to the
547 -- end of the function's actuals.
549 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
551 -- Locate the implicit activation chain parameter in the called function
553 Chain_Formal :=
554 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
556 -- Create the actual which is a pointer to the current activation chain
558 if No (Chain) then
559 Chain_Actual :=
560 Make_Attribute_Reference (Loc,
561 Prefix => Make_Identifier (Loc, Name_uChain),
562 Attribute_Name => Name_Unrestricted_Access);
564 -- Allocator case; make a reference to the Chain passed in by the caller
566 else
567 Chain_Actual :=
568 Make_Attribute_Reference (Loc,
569 Prefix => New_Occurrence_Of (Chain, Loc),
570 Attribute_Name => Name_Unrestricted_Access);
571 end if;
573 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
575 -- Build the parameter association for the new actual and add it to the
576 -- end of the function's actuals.
578 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
579 end Add_Task_Actuals_To_Build_In_Place_Call;
581 -----------------------
582 -- BIP_Formal_Suffix --
583 -----------------------
585 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
586 begin
587 case Kind is
588 when BIP_Alloc_Form =>
589 return "BIPalloc";
590 when BIP_Storage_Pool =>
591 return "BIPstoragepool";
592 when BIP_Finalization_Master =>
593 return "BIPfinalizationmaster";
594 when BIP_Task_Master =>
595 return "BIPtaskmaster";
596 when BIP_Activation_Chain =>
597 return "BIPactivationchain";
598 when BIP_Object_Access =>
599 return "BIPaccess";
600 end case;
601 end BIP_Formal_Suffix;
603 ---------------------------
604 -- Build_In_Place_Formal --
605 ---------------------------
607 function Build_In_Place_Formal
608 (Func : Entity_Id;
609 Kind : BIP_Formal_Kind) return Entity_Id
611 Formal_Name : constant Name_Id :=
612 New_External_Name
613 (Chars (Func), BIP_Formal_Suffix (Kind));
614 Extra_Formal : Entity_Id := Extra_Formals (Func);
616 begin
617 -- Maybe it would be better for each implicit formal of a build-in-place
618 -- function to have a flag or a Uint attribute to identify it. ???
620 -- The return type in the function declaration may have been a limited
621 -- view, and the extra formals for the function were not generated at
622 -- that point. At the point of call the full view must be available and
623 -- the extra formals can be created.
625 if No (Extra_Formal) then
626 Create_Extra_Formals (Func);
627 Extra_Formal := Extra_Formals (Func);
628 end if;
630 loop
631 pragma Assert (Present (Extra_Formal));
632 exit when Chars (Extra_Formal) = Formal_Name;
634 Next_Formal_With_Extras (Extra_Formal);
635 end loop;
637 return Extra_Formal;
638 end Build_In_Place_Formal;
640 --------------------------------
641 -- Check_Overriding_Operation --
642 --------------------------------
644 procedure Check_Overriding_Operation (Subp : Entity_Id) is
645 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
646 Op_List : constant Elist_Id := Primitive_Operations (Typ);
647 Op_Elmt : Elmt_Id;
648 Prim_Op : Entity_Id;
649 Par_Op : Entity_Id;
651 begin
652 if Is_Derived_Type (Typ)
653 and then not Is_Private_Type (Typ)
654 and then In_Open_Scopes (Scope (Etype (Typ)))
655 and then Is_Base_Type (Typ)
656 then
657 -- Subp overrides an inherited private operation if there is an
658 -- inherited operation with a different name than Subp (see
659 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
660 -- same name as Subp.
662 Op_Elmt := First_Elmt (Op_List);
663 while Present (Op_Elmt) loop
664 Prim_Op := Node (Op_Elmt);
665 Par_Op := Alias (Prim_Op);
667 if Present (Par_Op)
668 and then not Comes_From_Source (Prim_Op)
669 and then Chars (Prim_Op) /= Chars (Par_Op)
670 and then Chars (Par_Op) = Chars (Subp)
671 and then Is_Hidden (Par_Op)
672 and then Type_Conformant (Prim_Op, Subp)
673 then
674 Set_DT_Position (Subp, DT_Position (Prim_Op));
675 end if;
677 Next_Elmt (Op_Elmt);
678 end loop;
679 end if;
680 end Check_Overriding_Operation;
682 -------------------------------
683 -- Detect_Infinite_Recursion --
684 -------------------------------
686 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
687 Loc : constant Source_Ptr := Sloc (N);
689 Var_List : constant Elist_Id := New_Elmt_List;
690 -- List of globals referenced by body of procedure
692 Call_List : constant Elist_Id := New_Elmt_List;
693 -- List of recursive calls in body of procedure
695 Shad_List : constant Elist_Id := New_Elmt_List;
696 -- List of entity id's for entities created to capture the value of
697 -- referenced globals on entry to the procedure.
699 Scop : constant Uint := Scope_Depth (Spec);
700 -- This is used to record the scope depth of the current procedure, so
701 -- that we can identify global references.
703 Max_Vars : constant := 4;
704 -- Do not test more than four global variables
706 Count_Vars : Natural := 0;
707 -- Count variables found so far
709 Var : Entity_Id;
710 Elm : Elmt_Id;
711 Ent : Entity_Id;
712 Call : Elmt_Id;
713 Decl : Node_Id;
714 Test : Node_Id;
715 Elm1 : Elmt_Id;
716 Elm2 : Elmt_Id;
717 Last : Node_Id;
719 function Process (Nod : Node_Id) return Traverse_Result;
720 -- Function to traverse the subprogram body (using Traverse_Func)
722 -------------
723 -- Process --
724 -------------
726 function Process (Nod : Node_Id) return Traverse_Result is
727 begin
728 -- Procedure call
730 if Nkind (Nod) = N_Procedure_Call_Statement then
732 -- Case of one of the detected recursive calls
734 if Is_Entity_Name (Name (Nod))
735 and then Has_Recursive_Call (Entity (Name (Nod)))
736 and then Entity (Name (Nod)) = Spec
737 then
738 Append_Elmt (Nod, Call_List);
739 return Skip;
741 -- Any other procedure call may have side effects
743 else
744 return Abandon;
745 end if;
747 -- A call to a pure function can always be ignored
749 elsif Nkind (Nod) = N_Function_Call
750 and then Is_Entity_Name (Name (Nod))
751 and then Is_Pure (Entity (Name (Nod)))
752 then
753 return Skip;
755 -- Case of an identifier reference
757 elsif Nkind (Nod) = N_Identifier then
758 Ent := Entity (Nod);
760 -- If no entity, then ignore the reference
762 -- Not clear why this can happen. To investigate, remove this
763 -- test and look at the crash that occurs here in 3401-004 ???
765 if No (Ent) then
766 return Skip;
768 -- Ignore entities with no Scope, again not clear how this
769 -- can happen, to investigate, look at 4108-008 ???
771 elsif No (Scope (Ent)) then
772 return Skip;
774 -- Ignore the reference if not to a more global object
776 elsif Scope_Depth (Scope (Ent)) >= Scop then
777 return Skip;
779 -- References to types, exceptions and constants are always OK
781 elsif Is_Type (Ent)
782 or else Ekind (Ent) = E_Exception
783 or else Ekind (Ent) = E_Constant
784 then
785 return Skip;
787 -- If other than a non-volatile scalar variable, we have some
788 -- kind of global reference (e.g. to a function) that we cannot
789 -- deal with so we forget the attempt.
791 elsif Ekind (Ent) /= E_Variable
792 or else not Is_Scalar_Type (Etype (Ent))
793 or else Treat_As_Volatile (Ent)
794 then
795 return Abandon;
797 -- Otherwise we have a reference to a global scalar
799 else
800 -- Loop through global entities already detected
802 Elm := First_Elmt (Var_List);
803 loop
804 -- If not detected before, record this new global reference
806 if No (Elm) then
807 Count_Vars := Count_Vars + 1;
809 if Count_Vars <= Max_Vars then
810 Append_Elmt (Entity (Nod), Var_List);
811 else
812 return Abandon;
813 end if;
815 exit;
817 -- If recorded before, ignore
819 elsif Node (Elm) = Entity (Nod) then
820 return Skip;
822 -- Otherwise keep looking
824 else
825 Next_Elmt (Elm);
826 end if;
827 end loop;
829 return Skip;
830 end if;
832 -- For all other node kinds, recursively visit syntactic children
834 else
835 return OK;
836 end if;
837 end Process;
839 function Traverse_Body is new Traverse_Func (Process);
841 -- Start of processing for Detect_Infinite_Recursion
843 begin
844 -- Do not attempt detection in No_Implicit_Conditional mode, since we
845 -- won't be able to generate the code to handle the recursion in any
846 -- case.
848 if Restriction_Active (No_Implicit_Conditionals) then
849 return;
850 end if;
852 -- Otherwise do traversal and quit if we get abandon signal
854 if Traverse_Body (N) = Abandon then
855 return;
857 -- We must have a call, since Has_Recursive_Call was set. If not just
858 -- ignore (this is only an error check, so if we have a funny situation,
859 -- due to bugs or errors, we do not want to bomb).
861 elsif Is_Empty_Elmt_List (Call_List) then
862 return;
863 end if;
865 -- Here is the case where we detect recursion at compile time
867 -- Push our current scope for analyzing the declarations and code that
868 -- we will insert for the checking.
870 Push_Scope (Spec);
872 -- This loop builds temporary variables for each of the referenced
873 -- globals, so that at the end of the loop the list Shad_List contains
874 -- these temporaries in one-to-one correspondence with the elements in
875 -- Var_List.
877 Last := Empty;
878 Elm := First_Elmt (Var_List);
879 while Present (Elm) loop
880 Var := Node (Elm);
881 Ent := Make_Temporary (Loc, 'S');
882 Append_Elmt (Ent, Shad_List);
884 -- Insert a declaration for this temporary at the start of the
885 -- declarations for the procedure. The temporaries are declared as
886 -- constant objects initialized to the current values of the
887 -- corresponding temporaries.
889 Decl :=
890 Make_Object_Declaration (Loc,
891 Defining_Identifier => Ent,
892 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
893 Constant_Present => True,
894 Expression => New_Occurrence_Of (Var, Loc));
896 if No (Last) then
897 Prepend (Decl, Declarations (N));
898 else
899 Insert_After (Last, Decl);
900 end if;
902 Last := Decl;
903 Analyze (Decl);
904 Next_Elmt (Elm);
905 end loop;
907 -- Loop through calls
909 Call := First_Elmt (Call_List);
910 while Present (Call) loop
912 -- Build a predicate expression of the form
914 -- True
915 -- and then global1 = temp1
916 -- and then global2 = temp2
917 -- ...
919 -- This predicate determines if any of the global values
920 -- referenced by the procedure have changed since the
921 -- current call, if not an infinite recursion is assured.
923 Test := New_Occurrence_Of (Standard_True, Loc);
925 Elm1 := First_Elmt (Var_List);
926 Elm2 := First_Elmt (Shad_List);
927 while Present (Elm1) loop
928 Test :=
929 Make_And_Then (Loc,
930 Left_Opnd => Test,
931 Right_Opnd =>
932 Make_Op_Eq (Loc,
933 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
934 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
936 Next_Elmt (Elm1);
937 Next_Elmt (Elm2);
938 end loop;
940 -- Now we replace the call with the sequence
942 -- if no-changes (see above) then
943 -- raise Storage_Error;
944 -- else
945 -- original-call
946 -- end if;
948 Rewrite (Node (Call),
949 Make_If_Statement (Loc,
950 Condition => Test,
951 Then_Statements => New_List (
952 Make_Raise_Storage_Error (Loc,
953 Reason => SE_Infinite_Recursion)),
955 Else_Statements => New_List (
956 Relocate_Node (Node (Call)))));
958 Analyze (Node (Call));
960 Next_Elmt (Call);
961 end loop;
963 -- Remove temporary scope stack entry used for analysis
965 Pop_Scope;
966 end Detect_Infinite_Recursion;
968 --------------------
969 -- Expand_Actuals --
970 --------------------
972 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
973 Loc : constant Source_Ptr := Sloc (N);
974 Actual : Node_Id;
975 Formal : Entity_Id;
976 N_Node : Node_Id;
977 Post_Call : List_Id;
978 E_Actual : Entity_Id;
979 E_Formal : Entity_Id;
981 procedure Add_Call_By_Copy_Code;
982 -- For cases where the parameter must be passed by copy, this routine
983 -- generates a temporary variable into which the actual is copied and
984 -- then passes this as the parameter. For an OUT or IN OUT parameter,
985 -- an assignment is also generated to copy the result back. The call
986 -- also takes care of any constraint checks required for the type
987 -- conversion case (on both the way in and the way out).
989 procedure Add_Simple_Call_By_Copy_Code;
990 -- This is similar to the above, but is used in cases where we know
991 -- that all that is needed is to simply create a temporary and copy
992 -- the value in and out of the temporary.
994 procedure Check_Fortran_Logical;
995 -- A value of type Logical that is passed through a formal parameter
996 -- must be normalized because .TRUE. usually does not have the same
997 -- representation as True. We assume that .FALSE. = False = 0.
998 -- What about functions that return a logical type ???
1000 function Is_Legal_Copy return Boolean;
1001 -- Check that an actual can be copied before generating the temporary
1002 -- to be used in the call. If the actual is of a by_reference type then
1003 -- the program is illegal (this can only happen in the presence of
1004 -- rep. clauses that force an incorrect alignment). If the formal is
1005 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1006 -- the effect that this might lead to unaligned arguments.
1008 function Make_Var (Actual : Node_Id) return Entity_Id;
1009 -- Returns an entity that refers to the given actual parameter, Actual
1010 -- (not including any type conversion). If Actual is an entity name,
1011 -- then this entity is returned unchanged, otherwise a renaming is
1012 -- created to provide an entity for the actual.
1014 procedure Reset_Packed_Prefix;
1015 -- The expansion of a packed array component reference is delayed in
1016 -- the context of a call. Now we need to complete the expansion, so we
1017 -- unmark the analyzed bits in all prefixes.
1019 ---------------------------
1020 -- Add_Call_By_Copy_Code --
1021 ---------------------------
1023 procedure Add_Call_By_Copy_Code is
1024 Expr : Node_Id;
1025 Init : Node_Id;
1026 Temp : Entity_Id;
1027 Indic : Node_Id;
1028 Var : Entity_Id;
1029 F_Typ : constant Entity_Id := Etype (Formal);
1030 V_Typ : Entity_Id;
1031 Crep : Boolean;
1033 begin
1034 if not Is_Legal_Copy then
1035 return;
1036 end if;
1038 Temp := Make_Temporary (Loc, 'T', Actual);
1040 -- Use formal type for temp, unless formal type is an unconstrained
1041 -- array, in which case we don't have to worry about bounds checks,
1042 -- and we use the actual type, since that has appropriate bounds.
1044 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1045 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1046 else
1047 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1048 end if;
1050 if Nkind (Actual) = N_Type_Conversion then
1051 V_Typ := Etype (Expression (Actual));
1053 -- If the formal is an (in-)out parameter, capture the name
1054 -- of the variable in order to build the post-call assignment.
1056 Var := Make_Var (Expression (Actual));
1058 Crep := not Same_Representation
1059 (F_Typ, Etype (Expression (Actual)));
1061 else
1062 V_Typ := Etype (Actual);
1063 Var := Make_Var (Actual);
1064 Crep := False;
1065 end if;
1067 -- Setup initialization for case of in out parameter, or an out
1068 -- parameter where the formal is an unconstrained array (in the
1069 -- latter case, we have to pass in an object with bounds).
1071 -- If this is an out parameter, the initial copy is wasteful, so as
1072 -- an optimization for the one-dimensional case we extract the
1073 -- bounds of the actual and build an uninitialized temporary of the
1074 -- right size.
1076 if Ekind (Formal) = E_In_Out_Parameter
1077 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1078 then
1079 if Nkind (Actual) = N_Type_Conversion then
1080 if Conversion_OK (Actual) then
1081 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1082 else
1083 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1084 end if;
1086 elsif Ekind (Formal) = E_Out_Parameter
1087 and then Is_Array_Type (F_Typ)
1088 and then Number_Dimensions (F_Typ) = 1
1089 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1090 then
1091 -- Actual is a one-dimensional array or slice, and the type
1092 -- requires no initialization. Create a temporary of the
1093 -- right size, but do not copy actual into it (optimization).
1095 Init := Empty;
1096 Indic :=
1097 Make_Subtype_Indication (Loc,
1098 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1099 Constraint =>
1100 Make_Index_Or_Discriminant_Constraint (Loc,
1101 Constraints => New_List (
1102 Make_Range (Loc,
1103 Low_Bound =>
1104 Make_Attribute_Reference (Loc,
1105 Prefix => New_Occurrence_Of (Var, Loc),
1106 Attribute_Name => Name_First),
1107 High_Bound =>
1108 Make_Attribute_Reference (Loc,
1109 Prefix => New_Occurrence_Of (Var, Loc),
1110 Attribute_Name => Name_Last)))));
1112 else
1113 Init := New_Occurrence_Of (Var, Loc);
1114 end if;
1116 -- An initialization is created for packed conversions as
1117 -- actuals for out parameters to enable Make_Object_Declaration
1118 -- to determine the proper subtype for N_Node. Note that this
1119 -- is wasteful because the extra copying on the call side is
1120 -- not required for such out parameters. ???
1122 elsif Ekind (Formal) = E_Out_Parameter
1123 and then Nkind (Actual) = N_Type_Conversion
1124 and then (Is_Bit_Packed_Array (F_Typ)
1125 or else
1126 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1127 then
1128 if Conversion_OK (Actual) then
1129 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1130 else
1131 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1132 end if;
1134 elsif Ekind (Formal) = E_In_Parameter then
1136 -- Handle the case in which the actual is a type conversion
1138 if Nkind (Actual) = N_Type_Conversion then
1139 if Conversion_OK (Actual) then
1140 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1141 else
1142 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1143 end if;
1144 else
1145 Init := New_Occurrence_Of (Var, Loc);
1146 end if;
1148 else
1149 Init := Empty;
1150 end if;
1152 N_Node :=
1153 Make_Object_Declaration (Loc,
1154 Defining_Identifier => Temp,
1155 Object_Definition => Indic,
1156 Expression => Init);
1157 Set_Assignment_OK (N_Node);
1158 Insert_Action (N, N_Node);
1160 -- Now, normally the deal here is that we use the defining
1161 -- identifier created by that object declaration. There is
1162 -- one exception to this. In the change of representation case
1163 -- the above declaration will end up looking like:
1165 -- temp : type := identifier;
1167 -- And in this case we might as well use the identifier directly
1168 -- and eliminate the temporary. Note that the analysis of the
1169 -- declaration was not a waste of time in that case, since it is
1170 -- what generated the necessary change of representation code. If
1171 -- the change of representation introduced additional code, as in
1172 -- a fixed-integer conversion, the expression is not an identifier
1173 -- and must be kept.
1175 if Crep
1176 and then Present (Expression (N_Node))
1177 and then Is_Entity_Name (Expression (N_Node))
1178 then
1179 Temp := Entity (Expression (N_Node));
1180 Rewrite (N_Node, Make_Null_Statement (Loc));
1181 end if;
1183 -- For IN parameter, all we do is to replace the actual
1185 if Ekind (Formal) = E_In_Parameter then
1186 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1187 Analyze (Actual);
1189 -- Processing for OUT or IN OUT parameter
1191 else
1192 -- Kill current value indications for the temporary variable we
1193 -- created, since we just passed it as an OUT parameter.
1195 Kill_Current_Values (Temp);
1196 Set_Is_Known_Valid (Temp, False);
1198 -- If type conversion, use reverse conversion on exit
1200 if Nkind (Actual) = N_Type_Conversion then
1201 if Conversion_OK (Actual) then
1202 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1203 else
1204 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1205 end if;
1206 else
1207 Expr := New_Occurrence_Of (Temp, Loc);
1208 end if;
1210 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1211 Analyze (Actual);
1213 -- If the actual is a conversion of a packed reference, it may
1214 -- already have been expanded by Remove_Side_Effects, and the
1215 -- resulting variable is a temporary which does not designate
1216 -- the proper out-parameter, which may not be addressable. In
1217 -- that case, generate an assignment to the original expression
1218 -- (before expansion of the packed reference) so that the proper
1219 -- expansion of assignment to a packed component can take place.
1221 declare
1222 Obj : Node_Id;
1223 Lhs : Node_Id;
1225 begin
1226 if Is_Renaming_Of_Object (Var)
1227 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1228 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1229 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1230 = N_Indexed_Component
1231 and then
1232 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1233 then
1234 Obj := Renamed_Object (Var);
1235 Lhs :=
1236 Make_Selected_Component (Loc,
1237 Prefix =>
1238 New_Copy_Tree (Original_Node (Prefix (Obj))),
1239 Selector_Name => New_Copy (Selector_Name (Obj)));
1240 Reset_Analyzed_Flags (Lhs);
1242 else
1243 Lhs := New_Occurrence_Of (Var, Loc);
1244 end if;
1246 Set_Assignment_OK (Lhs);
1248 if Is_Access_Type (E_Formal)
1249 and then Is_Entity_Name (Lhs)
1250 and then
1251 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1252 then
1253 -- Copyback target is an Ada 2012 stand-alone object of an
1254 -- anonymous access type.
1256 pragma Assert (Ada_Version >= Ada_2012);
1258 if Type_Access_Level (E_Formal) >
1259 Object_Access_Level (Lhs)
1260 then
1261 Append_To (Post_Call,
1262 Make_Raise_Program_Error (Loc,
1263 Reason => PE_Accessibility_Check_Failed));
1264 end if;
1266 Append_To (Post_Call,
1267 Make_Assignment_Statement (Loc,
1268 Name => Lhs,
1269 Expression => Expr));
1271 -- We would like to somehow suppress generation of the
1272 -- extra_accessibility assignment generated by the expansion
1273 -- of the above assignment statement. It's not a correctness
1274 -- issue because the following assignment renders it dead,
1275 -- but generating back-to-back assignments to the same
1276 -- target is undesirable. ???
1278 Append_To (Post_Call,
1279 Make_Assignment_Statement (Loc,
1280 Name => New_Occurrence_Of (
1281 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1282 Expression => Make_Integer_Literal (Loc,
1283 Type_Access_Level (E_Formal))));
1285 else
1286 Append_To (Post_Call,
1287 Make_Assignment_Statement (Loc,
1288 Name => Lhs,
1289 Expression => Expr));
1290 end if;
1291 end;
1292 end if;
1293 end Add_Call_By_Copy_Code;
1295 ----------------------------------
1296 -- Add_Simple_Call_By_Copy_Code --
1297 ----------------------------------
1299 procedure Add_Simple_Call_By_Copy_Code is
1300 Temp : Entity_Id;
1301 Decl : Node_Id;
1302 Incod : Node_Id;
1303 Outcod : Node_Id;
1304 Lhs : Node_Id;
1305 Rhs : Node_Id;
1306 Indic : Node_Id;
1307 F_Typ : constant Entity_Id := Etype (Formal);
1309 begin
1310 if not Is_Legal_Copy then
1311 return;
1312 end if;
1314 -- Use formal type for temp, unless formal type is an unconstrained
1315 -- array, in which case we don't have to worry about bounds checks,
1316 -- and we use the actual type, since that has appropriate bounds.
1318 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1319 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1320 else
1321 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1322 end if;
1324 -- Prepare to generate code
1326 Reset_Packed_Prefix;
1328 Temp := Make_Temporary (Loc, 'T', Actual);
1329 Incod := Relocate_Node (Actual);
1330 Outcod := New_Copy_Tree (Incod);
1332 -- Generate declaration of temporary variable, initializing it
1333 -- with the input parameter unless we have an OUT formal or
1334 -- this is an initialization call.
1336 -- If the formal is an out parameter with discriminants, the
1337 -- discriminants must be captured even if the rest of the object
1338 -- is in principle uninitialized, because the discriminants may
1339 -- be read by the called subprogram.
1341 if Ekind (Formal) = E_Out_Parameter then
1342 Incod := Empty;
1344 if Has_Discriminants (Etype (Formal)) then
1345 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1346 end if;
1348 elsif Inside_Init_Proc then
1350 -- Could use a comment here to match comment below ???
1352 if Nkind (Actual) /= N_Selected_Component
1353 or else
1354 not Has_Discriminant_Dependent_Constraint
1355 (Entity (Selector_Name (Actual)))
1356 then
1357 Incod := Empty;
1359 -- Otherwise, keep the component in order to generate the proper
1360 -- actual subtype, that depends on enclosing discriminants.
1362 else
1363 null;
1364 end if;
1365 end if;
1367 Decl :=
1368 Make_Object_Declaration (Loc,
1369 Defining_Identifier => Temp,
1370 Object_Definition => Indic,
1371 Expression => Incod);
1373 if Inside_Init_Proc
1374 and then No (Incod)
1375 then
1376 -- If the call is to initialize a component of a composite type,
1377 -- and the component does not depend on discriminants, use the
1378 -- actual type of the component. This is required in case the
1379 -- component is constrained, because in general the formal of the
1380 -- initialization procedure will be unconstrained. Note that if
1381 -- the component being initialized is constrained by an enclosing
1382 -- discriminant, the presence of the initialization in the
1383 -- declaration will generate an expression for the actual subtype.
1385 Set_No_Initialization (Decl);
1386 Set_Object_Definition (Decl,
1387 New_Occurrence_Of (Etype (Actual), Loc));
1388 end if;
1390 Insert_Action (N, Decl);
1392 -- The actual is simply a reference to the temporary
1394 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1396 -- Generate copy out if OUT or IN OUT parameter
1398 if Ekind (Formal) /= E_In_Parameter then
1399 Lhs := Outcod;
1400 Rhs := New_Occurrence_Of (Temp, Loc);
1402 -- Deal with conversion
1404 if Nkind (Lhs) = N_Type_Conversion then
1405 Lhs := Expression (Lhs);
1406 Rhs := Convert_To (Etype (Actual), Rhs);
1407 end if;
1409 Append_To (Post_Call,
1410 Make_Assignment_Statement (Loc,
1411 Name => Lhs,
1412 Expression => Rhs));
1413 Set_Assignment_OK (Name (Last (Post_Call)));
1414 end if;
1415 end Add_Simple_Call_By_Copy_Code;
1417 ---------------------------
1418 -- Check_Fortran_Logical --
1419 ---------------------------
1421 procedure Check_Fortran_Logical is
1422 Logical : constant Entity_Id := Etype (Formal);
1423 Var : Entity_Id;
1425 -- Note: this is very incomplete, e.g. it does not handle arrays
1426 -- of logical values. This is really not the right approach at all???)
1428 begin
1429 if Convention (Subp) = Convention_Fortran
1430 and then Root_Type (Etype (Formal)) = Standard_Boolean
1431 and then Ekind (Formal) /= E_In_Parameter
1432 then
1433 Var := Make_Var (Actual);
1434 Append_To (Post_Call,
1435 Make_Assignment_Statement (Loc,
1436 Name => New_Occurrence_Of (Var, Loc),
1437 Expression =>
1438 Unchecked_Convert_To (
1439 Logical,
1440 Make_Op_Ne (Loc,
1441 Left_Opnd => New_Occurrence_Of (Var, Loc),
1442 Right_Opnd =>
1443 Unchecked_Convert_To (
1444 Logical,
1445 New_Occurrence_Of (Standard_False, Loc))))));
1446 end if;
1447 end Check_Fortran_Logical;
1449 -------------------
1450 -- Is_Legal_Copy --
1451 -------------------
1453 function Is_Legal_Copy return Boolean is
1454 begin
1455 -- An attempt to copy a value of such a type can only occur if
1456 -- representation clauses give the actual a misaligned address.
1458 if Is_By_Reference_Type (Etype (Formal)) then
1460 -- If the front-end does not perform full type layout, the actual
1461 -- may in fact be properly aligned but there is not enough front-
1462 -- end information to determine this. In that case gigi will emit
1463 -- an error if a copy is not legal, or generate the proper code.
1464 -- For other backends we report the error now.
1466 -- Seems wrong to be issuing an error in the expander, since it
1467 -- will be missed in -gnatc mode ???
1469 if Frontend_Layout_On_Target then
1470 Error_Msg_N
1471 ("misaligned actual cannot be passed by reference", Actual);
1472 end if;
1474 return False;
1476 -- For users of Starlet, we assume that the specification of by-
1477 -- reference mechanism is mandatory. This may lead to unaligned
1478 -- objects but at least for DEC legacy code it is known to work.
1479 -- The warning will alert users of this code that a problem may
1480 -- be lurking.
1482 elsif Mechanism (Formal) = By_Reference
1483 and then Is_Valued_Procedure (Scope (Formal))
1484 then
1485 Error_Msg_N
1486 ("by_reference actual may be misaligned??", Actual);
1487 return False;
1489 else
1490 return True;
1491 end if;
1492 end Is_Legal_Copy;
1494 --------------
1495 -- Make_Var --
1496 --------------
1498 function Make_Var (Actual : Node_Id) return Entity_Id is
1499 Var : Entity_Id;
1501 begin
1502 if Is_Entity_Name (Actual) then
1503 return Entity (Actual);
1505 else
1506 Var := Make_Temporary (Loc, 'T', Actual);
1508 N_Node :=
1509 Make_Object_Renaming_Declaration (Loc,
1510 Defining_Identifier => Var,
1511 Subtype_Mark =>
1512 New_Occurrence_Of (Etype (Actual), Loc),
1513 Name => Relocate_Node (Actual));
1515 Insert_Action (N, N_Node);
1516 return Var;
1517 end if;
1518 end Make_Var;
1520 -------------------------
1521 -- Reset_Packed_Prefix --
1522 -------------------------
1524 procedure Reset_Packed_Prefix is
1525 Pfx : Node_Id := Actual;
1526 begin
1527 loop
1528 Set_Analyzed (Pfx, False);
1529 exit when
1530 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1531 Pfx := Prefix (Pfx);
1532 end loop;
1533 end Reset_Packed_Prefix;
1535 -- Start of processing for Expand_Actuals
1537 begin
1538 Post_Call := New_List;
1540 Formal := First_Formal (Subp);
1541 Actual := First_Actual (N);
1542 while Present (Formal) loop
1543 E_Formal := Etype (Formal);
1544 E_Actual := Etype (Actual);
1546 if Is_Scalar_Type (E_Formal)
1547 or else Nkind (Actual) = N_Slice
1548 then
1549 Check_Fortran_Logical;
1551 -- RM 6.4.1 (11)
1553 elsif Ekind (Formal) /= E_Out_Parameter then
1555 -- The unusual case of the current instance of a protected type
1556 -- requires special handling. This can only occur in the context
1557 -- of a call within the body of a protected operation.
1559 if Is_Entity_Name (Actual)
1560 and then Ekind (Entity (Actual)) = E_Protected_Type
1561 and then In_Open_Scopes (Entity (Actual))
1562 then
1563 if Scope (Subp) /= Entity (Actual) then
1564 Error_Msg_N
1565 ("operation outside protected type may not "
1566 & "call back its protected operations??", Actual);
1567 end if;
1569 Rewrite (Actual,
1570 Expand_Protected_Object_Reference (N, Entity (Actual)));
1571 end if;
1573 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1574 -- build-in-place function, then a temporary return object needs
1575 -- to be created and access to it must be passed to the function.
1576 -- Currently we limit such functions to those with inherently
1577 -- limited result subtypes, but eventually we plan to expand the
1578 -- functions that are treated as build-in-place to include other
1579 -- composite result types.
1581 if Is_Build_In_Place_Function_Call (Actual) then
1582 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1583 end if;
1585 Apply_Constraint_Check (Actual, E_Formal);
1587 -- Out parameter case. No constraint checks on access type
1588 -- RM 6.4.1 (13)
1590 elsif Is_Access_Type (E_Formal) then
1591 null;
1593 -- RM 6.4.1 (14)
1595 elsif Has_Discriminants (Base_Type (E_Formal))
1596 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1597 then
1598 Apply_Constraint_Check (Actual, E_Formal);
1600 -- RM 6.4.1 (15)
1602 else
1603 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1604 end if;
1606 -- Processing for IN-OUT and OUT parameters
1608 if Ekind (Formal) /= E_In_Parameter then
1610 -- For type conversions of arrays, apply length/range checks
1612 if Is_Array_Type (E_Formal)
1613 and then Nkind (Actual) = N_Type_Conversion
1614 then
1615 if Is_Constrained (E_Formal) then
1616 Apply_Length_Check (Expression (Actual), E_Formal);
1617 else
1618 Apply_Range_Check (Expression (Actual), E_Formal);
1619 end if;
1620 end if;
1622 -- If argument is a type conversion for a type that is passed
1623 -- by copy, then we must pass the parameter by copy.
1625 if Nkind (Actual) = N_Type_Conversion
1626 and then
1627 (Is_Numeric_Type (E_Formal)
1628 or else Is_Access_Type (E_Formal)
1629 or else Is_Enumeration_Type (E_Formal)
1630 or else Is_Bit_Packed_Array (Etype (Formal))
1631 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1633 -- Also pass by copy if change of representation
1635 or else not Same_Representation
1636 (Etype (Formal),
1637 Etype (Expression (Actual))))
1638 then
1639 Add_Call_By_Copy_Code;
1641 -- References to components of bit packed arrays are expanded
1642 -- at this point, rather than at the point of analysis of the
1643 -- actuals, to handle the expansion of the assignment to
1644 -- [in] out parameters.
1646 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1647 Add_Simple_Call_By_Copy_Code;
1649 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1650 -- because the back-end cannot cope with such objects. In other
1651 -- cases where alignment forces a copy, the back-end generates
1652 -- it properly. It should not be generated unconditionally in the
1653 -- front-end because it does not know precisely the alignment
1654 -- requirements of the target, and makes too conservative an
1655 -- estimate, leading to superfluous copies or spurious errors
1656 -- on by-reference parameters.
1658 elsif Nkind (Actual) = N_Selected_Component
1659 and then
1660 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1661 and then not Represented_As_Scalar (Etype (Formal))
1662 then
1663 Add_Simple_Call_By_Copy_Code;
1665 -- References to slices of bit packed arrays are expanded
1667 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1668 Add_Call_By_Copy_Code;
1670 -- References to possibly unaligned slices of arrays are expanded
1672 elsif Is_Possibly_Unaligned_Slice (Actual) then
1673 Add_Call_By_Copy_Code;
1675 -- Deal with access types where the actual subtype and the
1676 -- formal subtype are not the same, requiring a check.
1678 -- It is necessary to exclude tagged types because of "downward
1679 -- conversion" errors.
1681 elsif Is_Access_Type (E_Formal)
1682 and then not Same_Type (E_Formal, E_Actual)
1683 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1684 then
1685 Add_Call_By_Copy_Code;
1687 -- If the actual is not a scalar and is marked for volatile
1688 -- treatment, whereas the formal is not volatile, then pass
1689 -- by copy unless it is a by-reference type.
1691 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1692 -- because this is the enforcement of a language rule that applies
1693 -- only to "real" volatile variables, not e.g. to the address
1694 -- clause overlay case.
1696 elsif Is_Entity_Name (Actual)
1697 and then Is_Volatile (Entity (Actual))
1698 and then not Is_By_Reference_Type (E_Actual)
1699 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1700 and then not Is_Volatile (E_Formal)
1701 then
1702 Add_Call_By_Copy_Code;
1704 elsif Nkind (Actual) = N_Indexed_Component
1705 and then Is_Entity_Name (Prefix (Actual))
1706 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1707 then
1708 Add_Call_By_Copy_Code;
1710 -- Add call-by-copy code for the case of scalar out parameters
1711 -- when it is not known at compile time that the subtype of the
1712 -- formal is a subrange of the subtype of the actual (or vice
1713 -- versa for in out parameters), in order to get range checks
1714 -- on such actuals. (Maybe this case should be handled earlier
1715 -- in the if statement???)
1717 elsif Is_Scalar_Type (E_Formal)
1718 and then
1719 (not In_Subrange_Of (E_Formal, E_Actual)
1720 or else
1721 (Ekind (Formal) = E_In_Out_Parameter
1722 and then not In_Subrange_Of (E_Actual, E_Formal)))
1723 then
1724 -- Perhaps the setting back to False should be done within
1725 -- Add_Call_By_Copy_Code, since it could get set on other
1726 -- cases occurring above???
1728 if Do_Range_Check (Actual) then
1729 Set_Do_Range_Check (Actual, False);
1730 end if;
1732 Add_Call_By_Copy_Code;
1733 end if;
1735 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1736 -- by-reference parameters on exit from the call. If the actual
1737 -- is a derived type and the operation is inherited, the body
1738 -- of the operation will not contain a call to the predicate
1739 -- function, so it must be done explicitly after the call. Ditto
1740 -- if the actual is an entity of a predicated subtype.
1742 -- The rule refers to by-reference types, but a check is needed
1743 -- for by-copy types as well. That check is subsumed by the rule
1744 -- for subtype conversion on assignment, but we can generate the
1745 -- required check now.
1747 -- Note also that Subp may be either a subprogram entity for
1748 -- direct calls, or a type entity for indirect calls, which must
1749 -- be handled separately because the name does not denote an
1750 -- overloadable entity.
1752 declare
1753 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1754 Atyp : Entity_Id;
1756 begin
1757 if No (Aund) then
1758 Atyp := E_Actual;
1759 else
1760 Atyp := Aund;
1761 end if;
1763 if Has_Predicates (Atyp)
1764 and then Present (Predicate_Function (Atyp))
1766 -- Skip predicate checks for special cases
1768 and then Predicate_Tests_On_Arguments (Subp)
1769 then
1770 Append_To (Post_Call,
1771 Make_Predicate_Check (Atyp, Actual));
1772 end if;
1773 end;
1775 -- Processing for IN parameters
1777 else
1778 -- For IN parameters is in the packed array case, we expand an
1779 -- indexed component (the circuit in Exp_Ch4 deliberately left
1780 -- indexed components appearing as actuals untouched, so that
1781 -- the special processing above for the OUT and IN OUT cases
1782 -- could be performed. We could make the test in Exp_Ch4 more
1783 -- complex and have it detect the parameter mode, but it is
1784 -- easier simply to handle all cases here.)
1786 if Nkind (Actual) = N_Indexed_Component
1787 and then Is_Packed (Etype (Prefix (Actual)))
1788 then
1789 Reset_Packed_Prefix;
1790 Expand_Packed_Element_Reference (Actual);
1792 -- If we have a reference to a bit packed array, we copy it, since
1793 -- the actual must be byte aligned.
1795 -- Is this really necessary in all cases???
1797 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1798 Add_Simple_Call_By_Copy_Code;
1800 -- If a non-scalar actual is possibly unaligned, we need a copy
1802 elsif Is_Possibly_Unaligned_Object (Actual)
1803 and then not Represented_As_Scalar (Etype (Formal))
1804 then
1805 Add_Simple_Call_By_Copy_Code;
1807 -- Similarly, we have to expand slices of packed arrays here
1808 -- because the result must be byte aligned.
1810 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1811 Add_Call_By_Copy_Code;
1813 -- Only processing remaining is to pass by copy if this is a
1814 -- reference to a possibly unaligned slice, since the caller
1815 -- expects an appropriately aligned argument.
1817 elsif Is_Possibly_Unaligned_Slice (Actual) then
1818 Add_Call_By_Copy_Code;
1820 -- An unusual case: a current instance of an enclosing task can be
1821 -- an actual, and must be replaced by a reference to self.
1823 elsif Is_Entity_Name (Actual)
1824 and then Is_Task_Type (Entity (Actual))
1825 then
1826 if In_Open_Scopes (Entity (Actual)) then
1827 Rewrite (Actual,
1828 (Make_Function_Call (Loc,
1829 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1830 Analyze (Actual);
1832 -- A task type cannot otherwise appear as an actual
1834 else
1835 raise Program_Error;
1836 end if;
1837 end if;
1838 end if;
1840 Next_Formal (Formal);
1841 Next_Actual (Actual);
1842 end loop;
1844 -- Find right place to put post call stuff if it is present
1846 if not Is_Empty_List (Post_Call) then
1848 -- Cases where the call is not a member of a statement list
1850 if not Is_List_Member (N) then
1852 -- In Ada 2012 the call may be a function call in an expression
1853 -- (since OUT and IN OUT parameters are now allowed for such
1854 -- calls). The write-back of (in)-out parameters is handled
1855 -- by the back-end, but the constraint checks generated when
1856 -- subtypes of formal and actual don't match must be inserted
1857 -- in the form of assignments.
1859 if Ada_Version >= Ada_2012
1860 and then Nkind (N) = N_Function_Call
1861 then
1862 -- We used to just do handle this by climbing up parents to
1863 -- a non-statement/declaration and then simply making a call
1864 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1865 -- work. If we are in the middle of an expression, e.g. the
1866 -- condition of an IF, this call would insert after the IF
1867 -- statement, which is much too late to be doing the write
1868 -- back. For example:
1870 -- if Clobber (X) then
1871 -- Put_Line (X'Img);
1872 -- else
1873 -- goto Junk
1874 -- end if;
1876 -- Now assume Clobber changes X, if we put the write back
1877 -- after the IF, the Put_Line gets the wrong value and the
1878 -- goto causes the write back to be skipped completely.
1880 -- To deal with this, we replace the call by
1882 -- do
1883 -- Tnnn : function-result-type renames function-call;
1884 -- Post_Call actions
1885 -- in
1886 -- Tnnn;
1887 -- end;
1889 -- Note: this won't do in Modify_Tree_For_C mode, but we
1890 -- will deal with that later (it will require creating a
1891 -- declaration for Temp, using Insert_Declaration) ???
1893 declare
1894 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
1895 FRTyp : constant Entity_Id := Etype (N);
1896 Name : constant Node_Id := Relocate_Node (N);
1898 begin
1899 Prepend_To (Post_Call,
1900 Make_Object_Renaming_Declaration (Loc,
1901 Defining_Identifier => Tnnn,
1902 Subtype_Mark => New_Occurrence_Of (FRTyp, Loc),
1903 Name => Name));
1905 Rewrite (N,
1906 Make_Expression_With_Actions (Loc,
1907 Actions => Post_Call,
1908 Expression => New_Occurrence_Of (Tnnn, Loc)));
1910 -- We don't want to just blindly call Analyze_And_Resolve
1911 -- because that would cause unwanted recursion on the call.
1912 -- So for a moment set the call as analyzed to prevent that
1913 -- recursion, and get the rest analyzed properly, then reset
1914 -- the analyzed flag, so our caller can continue.
1916 Set_Analyzed (Name, True);
1917 Analyze_And_Resolve (N, FRTyp);
1918 Set_Analyzed (Name, False);
1920 -- Reset calling argument to point to function call inside
1921 -- the expression with actions so the caller can continue
1922 -- to process the call.
1924 N := Name;
1925 end;
1927 -- If not the special Ada 2012 case of a function call, then
1928 -- we must have the triggering statement of a triggering
1929 -- alternative or an entry call alternative, and we can add
1930 -- the post call stuff to the corresponding statement list.
1932 else
1933 declare
1934 P : Node_Id;
1936 begin
1937 P := Parent (N);
1938 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1939 N_Entry_Call_Alternative));
1941 if Is_Non_Empty_List (Statements (P)) then
1942 Insert_List_Before_And_Analyze
1943 (First (Statements (P)), Post_Call);
1944 else
1945 Set_Statements (P, Post_Call);
1946 end if;
1948 return;
1949 end;
1950 end if;
1952 -- Otherwise, normal case where N is in a statement sequence,
1953 -- just put the post-call stuff after the call statement.
1955 else
1956 Insert_Actions_After (N, Post_Call);
1957 return;
1958 end if;
1959 end if;
1961 -- The call node itself is re-analyzed in Expand_Call
1963 end Expand_Actuals;
1965 -----------------
1966 -- Expand_Call --
1967 -----------------
1969 -- This procedure handles expansion of function calls and procedure call
1970 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1971 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1973 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1974 -- Provide values of actuals for all formals in Extra_Formals list
1975 -- Replace "call" to enumeration literal function by literal itself
1976 -- Rewrite call to predefined operator as operator
1977 -- Replace actuals to in-out parameters that are numeric conversions,
1978 -- with explicit assignment to temporaries before and after the call.
1980 -- Note that the list of actuals has been filled with default expressions
1981 -- during semantic analysis of the call. Only the extra actuals required
1982 -- for the 'Constrained attribute and for accessibility checks are added
1983 -- at this point.
1985 procedure Expand_Call (N : Node_Id) is
1986 Loc : constant Source_Ptr := Sloc (N);
1987 Call_Node : Node_Id := N;
1988 Extra_Actuals : List_Id := No_List;
1989 Prev : Node_Id := Empty;
1991 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1992 -- Adds one entry to the end of the actual parameter list. Used for
1993 -- default parameters and for extra actuals (for Extra_Formals). The
1994 -- argument is an N_Parameter_Association node.
1996 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1997 -- Adds an extra actual to the list of extra actuals. Expr is the
1998 -- expression for the value of the actual, EF is the entity for the
1999 -- extra formal.
2001 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2002 -- Within an instance, a type derived from an untagged formal derived
2003 -- type inherits from the original parent, not from the actual. The
2004 -- current derivation mechanism has the derived type inherit from the
2005 -- actual, which is only correct outside of the instance. If the
2006 -- subprogram is inherited, we test for this particular case through a
2007 -- convoluted tree traversal before setting the proper subprogram to be
2008 -- called.
2010 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2011 -- Return true if E comes from an instance that is not yet frozen
2013 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2014 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2016 function New_Value (From : Node_Id) return Node_Id;
2017 -- From is the original Expression. New_Value is equivalent to a call
2018 -- to Duplicate_Subexpr with an explicit dereference when From is an
2019 -- access parameter.
2021 --------------------------
2022 -- Add_Actual_Parameter --
2023 --------------------------
2025 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2026 Actual_Expr : constant Node_Id :=
2027 Explicit_Actual_Parameter (Insert_Param);
2029 begin
2030 -- Case of insertion is first named actual
2032 if No (Prev) or else
2033 Nkind (Parent (Prev)) /= N_Parameter_Association
2034 then
2035 Set_Next_Named_Actual
2036 (Insert_Param, First_Named_Actual (Call_Node));
2037 Set_First_Named_Actual (Call_Node, Actual_Expr);
2039 if No (Prev) then
2040 if No (Parameter_Associations (Call_Node)) then
2041 Set_Parameter_Associations (Call_Node, New_List);
2042 end if;
2044 Append (Insert_Param, Parameter_Associations (Call_Node));
2046 else
2047 Insert_After (Prev, Insert_Param);
2048 end if;
2050 -- Case of insertion is not first named actual
2052 else
2053 Set_Next_Named_Actual
2054 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2055 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2056 Append (Insert_Param, Parameter_Associations (Call_Node));
2057 end if;
2059 Prev := Actual_Expr;
2060 end Add_Actual_Parameter;
2062 ----------------------
2063 -- Add_Extra_Actual --
2064 ----------------------
2066 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2067 Loc : constant Source_Ptr := Sloc (Expr);
2069 begin
2070 if Extra_Actuals = No_List then
2071 Extra_Actuals := New_List;
2072 Set_Parent (Extra_Actuals, Call_Node);
2073 end if;
2075 Append_To (Extra_Actuals,
2076 Make_Parameter_Association (Loc,
2077 Selector_Name => New_Occurrence_Of (EF, Loc),
2078 Explicit_Actual_Parameter => Expr));
2080 Analyze_And_Resolve (Expr, Etype (EF));
2082 if Nkind (Call_Node) = N_Function_Call then
2083 Set_Is_Accessibility_Actual (Parent (Expr));
2084 end if;
2085 end Add_Extra_Actual;
2087 ---------------------------
2088 -- Inherited_From_Formal --
2089 ---------------------------
2091 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2092 Par : Entity_Id;
2093 Gen_Par : Entity_Id;
2094 Gen_Prim : Elist_Id;
2095 Elmt : Elmt_Id;
2096 Indic : Node_Id;
2098 begin
2099 -- If the operation is inherited, it is attached to the corresponding
2100 -- type derivation. If the parent in the derivation is a generic
2101 -- actual, it is a subtype of the actual, and we have to recover the
2102 -- original derived type declaration to find the proper parent.
2104 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2105 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2106 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2107 N_Derived_Type_Definition
2108 or else not In_Instance
2109 then
2110 return Empty;
2112 else
2113 Indic :=
2114 Subtype_Indication
2115 (Type_Definition (Original_Node (Parent (S))));
2117 if Nkind (Indic) = N_Subtype_Indication then
2118 Par := Entity (Subtype_Mark (Indic));
2119 else
2120 Par := Entity (Indic);
2121 end if;
2122 end if;
2124 if not Is_Generic_Actual_Type (Par)
2125 or else Is_Tagged_Type (Par)
2126 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2127 or else not In_Open_Scopes (Scope (Par))
2128 then
2129 return Empty;
2130 else
2131 Gen_Par := Generic_Parent_Type (Parent (Par));
2132 end if;
2134 -- If the actual has no generic parent type, the formal is not
2135 -- a formal derived type, so nothing to inherit.
2137 if No (Gen_Par) then
2138 return Empty;
2139 end if;
2141 -- If the generic parent type is still the generic type, this is a
2142 -- private formal, not a derived formal, and there are no operations
2143 -- inherited from the formal.
2145 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2146 return Empty;
2147 end if;
2149 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2151 Elmt := First_Elmt (Gen_Prim);
2152 while Present (Elmt) loop
2153 if Chars (Node (Elmt)) = Chars (S) then
2154 declare
2155 F1 : Entity_Id;
2156 F2 : Entity_Id;
2158 begin
2159 F1 := First_Formal (S);
2160 F2 := First_Formal (Node (Elmt));
2161 while Present (F1)
2162 and then Present (F2)
2163 loop
2164 if Etype (F1) = Etype (F2)
2165 or else Etype (F2) = Gen_Par
2166 then
2167 Next_Formal (F1);
2168 Next_Formal (F2);
2169 else
2170 Next_Elmt (Elmt);
2171 exit; -- not the right subprogram
2172 end if;
2174 return Node (Elmt);
2175 end loop;
2176 end;
2178 else
2179 Next_Elmt (Elmt);
2180 end if;
2181 end loop;
2183 raise Program_Error;
2184 end Inherited_From_Formal;
2186 --------------------------
2187 -- In_Unfrozen_Instance --
2188 --------------------------
2190 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2191 S : Entity_Id;
2193 begin
2194 S := E;
2195 while Present (S) and then S /= Standard_Standard loop
2196 if Is_Generic_Instance (S)
2197 and then Present (Freeze_Node (S))
2198 and then not Analyzed (Freeze_Node (S))
2199 then
2200 return True;
2201 end if;
2203 S := Scope (S);
2204 end loop;
2206 return False;
2207 end In_Unfrozen_Instance;
2209 -------------------------
2210 -- Is_Direct_Deep_Call --
2211 -------------------------
2213 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2214 begin
2215 if Is_TSS (Subp, TSS_Deep_Adjust)
2216 or else Is_TSS (Subp, TSS_Deep_Finalize)
2217 or else Is_TSS (Subp, TSS_Deep_Initialize)
2218 then
2219 declare
2220 Actual : Node_Id;
2221 Formal : Node_Id;
2223 begin
2224 Actual := First (Parameter_Associations (N));
2225 Formal := First_Formal (Subp);
2226 while Present (Actual)
2227 and then Present (Formal)
2228 loop
2229 if Nkind (Actual) = N_Identifier
2230 and then Is_Controlling_Actual (Actual)
2231 and then Etype (Actual) = Etype (Formal)
2232 then
2233 return True;
2234 end if;
2236 Next (Actual);
2237 Next_Formal (Formal);
2238 end loop;
2239 end;
2240 end if;
2242 return False;
2243 end Is_Direct_Deep_Call;
2245 ---------------
2246 -- New_Value --
2247 ---------------
2249 function New_Value (From : Node_Id) return Node_Id is
2250 Res : constant Node_Id := Duplicate_Subexpr (From);
2251 begin
2252 if Is_Access_Type (Etype (From)) then
2253 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2254 else
2255 return Res;
2256 end if;
2257 end New_Value;
2259 -- Local variables
2261 Curr_S : constant Entity_Id := Current_Scope;
2262 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2263 Actual : Node_Id;
2264 Formal : Entity_Id;
2265 Orig_Subp : Entity_Id := Empty;
2266 Param_Count : Natural := 0;
2267 Parent_Formal : Entity_Id;
2268 Parent_Subp : Entity_Id;
2269 Scop : Entity_Id;
2270 Subp : Entity_Id;
2272 Prev_Orig : Node_Id;
2273 -- Original node for an actual, which may have been rewritten. If the
2274 -- actual is a function call that has been transformed from a selected
2275 -- component, the original node is unanalyzed. Otherwise, it carries
2276 -- semantic information used to generate additional actuals.
2278 CW_Interface_Formals_Present : Boolean := False;
2280 -- Start of processing for Expand_Call
2282 begin
2283 -- Expand the procedure call if the first actual has a dimension and if
2284 -- the procedure is Put (Ada 2012).
2286 if Ada_Version >= Ada_2012
2287 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2288 and then Present (Parameter_Associations (Call_Node))
2289 then
2290 Expand_Put_Call_With_Symbol (Call_Node);
2291 end if;
2293 -- Ignore if previous error
2295 if Nkind (Call_Node) in N_Has_Etype
2296 and then Etype (Call_Node) = Any_Type
2297 then
2298 return;
2299 end if;
2301 -- Call using access to subprogram with explicit dereference
2303 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2304 Subp := Etype (Name (Call_Node));
2305 Parent_Subp := Empty;
2307 -- Case of call to simple entry, where the Name is a selected component
2308 -- whose prefix is the task, and whose selector name is the entry name
2310 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2311 Subp := Entity (Selector_Name (Name (Call_Node)));
2312 Parent_Subp := Empty;
2314 -- Case of call to member of entry family, where Name is an indexed
2315 -- component, with the prefix being a selected component giving the
2316 -- task and entry family name, and the index being the entry index.
2318 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2319 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2320 Parent_Subp := Empty;
2322 -- Normal case
2324 else
2325 Subp := Entity (Name (Call_Node));
2326 Parent_Subp := Alias (Subp);
2328 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2329 -- if we can tell that the first parameter cannot possibly be null.
2330 -- This improves efficiency by avoiding a run-time test.
2332 -- We do not do this if Raise_Exception_Always does not exist, which
2333 -- can happen in configurable run time profiles which provide only a
2334 -- Raise_Exception.
2336 if Is_RTE (Subp, RE_Raise_Exception)
2337 and then RTE_Available (RE_Raise_Exception_Always)
2338 then
2339 declare
2340 FA : constant Node_Id :=
2341 Original_Node (First_Actual (Call_Node));
2343 begin
2344 -- The case we catch is where the first argument is obtained
2345 -- using the Identity attribute (which must always be
2346 -- non-null).
2348 if Nkind (FA) = N_Attribute_Reference
2349 and then Attribute_Name (FA) = Name_Identity
2350 then
2351 Subp := RTE (RE_Raise_Exception_Always);
2352 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2353 end if;
2354 end;
2355 end if;
2357 if Ekind (Subp) = E_Entry then
2358 Parent_Subp := Empty;
2359 end if;
2360 end if;
2362 -- Detect the following code in System.Finalization_Masters only on
2363 -- .NET/JVM targets:
2365 -- procedure Finalize (Master : in out Finalization_Master) is
2366 -- begin
2367 -- . . .
2368 -- begin
2369 -- Finalize (Curr_Ptr.all);
2371 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2372 -- cannot be named in library or user code, the compiler has to deal
2373 -- with this by transforming the call to Finalize into Deep_Finalize.
2375 if VM_Target /= No_VM
2376 and then Chars (Subp) = Name_Finalize
2377 and then Ekind (Curr_S) = E_Block
2378 and then Ekind (Scope (Curr_S)) = E_Procedure
2379 and then Chars (Scope (Curr_S)) = Name_Finalize
2380 and then Etype (First_Formal (Scope (Curr_S))) =
2381 RTE (RE_Finalization_Master)
2382 then
2383 declare
2384 Deep_Fin : constant Entity_Id :=
2385 Find_Prim_Op (RTE (RE_Root_Controlled),
2386 TSS_Deep_Finalize);
2387 begin
2388 -- Since Root_Controlled is a tagged type, the compiler should
2389 -- always generate Deep_Finalize for it.
2391 pragma Assert (Present (Deep_Fin));
2393 -- Generate:
2394 -- Deep_Finalize (Curr_Ptr.all);
2396 Rewrite (N,
2397 Make_Procedure_Call_Statement (Loc,
2398 Name =>
2399 New_Occurrence_Of (Deep_Fin, Loc),
2400 Parameter_Associations =>
2401 New_Copy_List_Tree (Parameter_Associations (N))));
2403 Analyze (N);
2404 return;
2405 end;
2406 end if;
2408 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2409 -- alternative in an asynchronous select or as an entry call in
2410 -- a conditional or timed select. Check whether the procedure call
2411 -- is a renaming of an entry and rewrite it as an entry call.
2413 if Ada_Version >= Ada_2005
2414 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2415 and then
2416 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2417 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2418 or else
2419 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2420 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2421 then
2422 declare
2423 Ren_Decl : Node_Id;
2424 Ren_Root : Entity_Id := Subp;
2426 begin
2427 -- This may be a chain of renamings, find the root
2429 if Present (Alias (Ren_Root)) then
2430 Ren_Root := Alias (Ren_Root);
2431 end if;
2433 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2434 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2436 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2437 Rewrite (Call_Node,
2438 Make_Entry_Call_Statement (Loc,
2439 Name =>
2440 New_Copy_Tree (Name (Ren_Decl)),
2441 Parameter_Associations =>
2442 New_Copy_List_Tree
2443 (Parameter_Associations (Call_Node))));
2445 return;
2446 end if;
2447 end if;
2448 end;
2449 end if;
2451 -- First step, compute extra actuals, corresponding to any Extra_Formals
2452 -- present. Note that we do not access Extra_Formals directly, instead
2453 -- we simply note the presence of the extra formals as we process the
2454 -- regular formals collecting corresponding actuals in Extra_Actuals.
2456 -- We also generate any required range checks for actuals for in formals
2457 -- as we go through the loop, since this is a convenient place to do it.
2458 -- (Though it seems that this would be better done in Expand_Actuals???)
2460 -- Special case: Thunks must not compute the extra actuals; they must
2461 -- just propagate to the target primitive their extra actuals.
2463 if Is_Thunk (Current_Scope)
2464 and then Thunk_Entity (Current_Scope) = Subp
2465 and then Present (Extra_Formals (Subp))
2466 then
2467 pragma Assert (Present (Extra_Formals (Current_Scope)));
2469 declare
2470 Target_Formal : Entity_Id;
2471 Thunk_Formal : Entity_Id;
2473 begin
2474 Target_Formal := Extra_Formals (Subp);
2475 Thunk_Formal := Extra_Formals (Current_Scope);
2476 while Present (Target_Formal) loop
2477 Add_Extra_Actual
2478 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2480 Target_Formal := Extra_Formal (Target_Formal);
2481 Thunk_Formal := Extra_Formal (Thunk_Formal);
2482 end loop;
2484 while Is_Non_Empty_List (Extra_Actuals) loop
2485 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2486 end loop;
2488 Expand_Actuals (Call_Node, Subp);
2489 return;
2490 end;
2491 end if;
2493 Formal := First_Formal (Subp);
2494 Actual := First_Actual (Call_Node);
2495 Param_Count := 1;
2496 while Present (Formal) loop
2498 -- Generate range check if required
2500 if Do_Range_Check (Actual)
2501 and then Ekind (Formal) = E_In_Parameter
2502 then
2503 Generate_Range_Check
2504 (Actual, Etype (Formal), CE_Range_Check_Failed);
2505 end if;
2507 -- Prepare to examine current entry
2509 Prev := Actual;
2510 Prev_Orig := Original_Node (Prev);
2512 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2513 -- to expand it in a further round.
2515 CW_Interface_Formals_Present :=
2516 CW_Interface_Formals_Present
2517 or else
2518 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2519 and then Is_Interface (Etype (Etype (Formal))))
2520 or else
2521 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2522 and then Is_Interface (Directly_Designated_Type
2523 (Etype (Etype (Formal)))));
2525 -- Create possible extra actual for constrained case. Usually, the
2526 -- extra actual is of the form actual'constrained, but since this
2527 -- attribute is only available for unconstrained records, TRUE is
2528 -- expanded if the type of the formal happens to be constrained (for
2529 -- instance when this procedure is inherited from an unconstrained
2530 -- record to a constrained one) or if the actual has no discriminant
2531 -- (its type is constrained). An exception to this is the case of a
2532 -- private type without discriminants. In this case we pass FALSE
2533 -- because the object has underlying discriminants with defaults.
2535 if Present (Extra_Constrained (Formal)) then
2536 if Ekind (Etype (Prev)) in Private_Kind
2537 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2538 then
2539 Add_Extra_Actual
2540 (New_Occurrence_Of (Standard_False, Loc),
2541 Extra_Constrained (Formal));
2543 elsif Is_Constrained (Etype (Formal))
2544 or else not Has_Discriminants (Etype (Prev))
2545 then
2546 Add_Extra_Actual
2547 (New_Occurrence_Of (Standard_True, Loc),
2548 Extra_Constrained (Formal));
2550 -- Do not produce extra actuals for Unchecked_Union parameters.
2551 -- Jump directly to the end of the loop.
2553 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2554 goto Skip_Extra_Actual_Generation;
2556 else
2557 -- If the actual is a type conversion, then the constrained
2558 -- test applies to the actual, not the target type.
2560 declare
2561 Act_Prev : Node_Id;
2563 begin
2564 -- Test for unchecked conversions as well, which can occur
2565 -- as out parameter actuals on calls to stream procedures.
2567 Act_Prev := Prev;
2568 while Nkind_In (Act_Prev, N_Type_Conversion,
2569 N_Unchecked_Type_Conversion)
2570 loop
2571 Act_Prev := Expression (Act_Prev);
2572 end loop;
2574 -- If the expression is a conversion of a dereference, this
2575 -- is internally generated code that manipulates addresses,
2576 -- e.g. when building interface tables. No check should
2577 -- occur in this case, and the discriminated object is not
2578 -- directly a hand.
2580 if not Comes_From_Source (Actual)
2581 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2582 and then Nkind (Act_Prev) = N_Explicit_Dereference
2583 then
2584 Add_Extra_Actual
2585 (New_Occurrence_Of (Standard_False, Loc),
2586 Extra_Constrained (Formal));
2588 else
2589 Add_Extra_Actual
2590 (Make_Attribute_Reference (Sloc (Prev),
2591 Prefix =>
2592 Duplicate_Subexpr_No_Checks
2593 (Act_Prev, Name_Req => True),
2594 Attribute_Name => Name_Constrained),
2595 Extra_Constrained (Formal));
2596 end if;
2597 end;
2598 end if;
2599 end if;
2601 -- Create possible extra actual for accessibility level
2603 if Present (Extra_Accessibility (Formal)) then
2605 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2606 -- attribute, then the original actual may be an aliased object
2607 -- occurring as the prefix in a call using "Object.Operation"
2608 -- notation. In that case we must pass the level of the object,
2609 -- so Prev_Orig is reset to Prev and the attribute will be
2610 -- processed by the code for Access attributes further below.
2612 if Prev_Orig /= Prev
2613 and then Nkind (Prev) = N_Attribute_Reference
2614 and then
2615 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2616 and then Is_Aliased_View (Prev_Orig)
2617 then
2618 Prev_Orig := Prev;
2619 end if;
2621 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2622 -- accessibility levels.
2624 if Is_Thunk (Current_Scope) then
2625 declare
2626 Parm_Ent : Entity_Id;
2628 begin
2629 if Is_Controlling_Actual (Actual) then
2631 -- Find the corresponding actual of the thunk
2633 Parm_Ent := First_Entity (Current_Scope);
2634 for J in 2 .. Param_Count loop
2635 Next_Entity (Parm_Ent);
2636 end loop;
2638 -- Handle unchecked conversion of access types generated
2639 -- in thunks (cf. Expand_Interface_Thunk).
2641 elsif Is_Access_Type (Etype (Actual))
2642 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2643 then
2644 Parm_Ent := Entity (Expression (Actual));
2646 else pragma Assert (Is_Entity_Name (Actual));
2647 Parm_Ent := Entity (Actual);
2648 end if;
2650 Add_Extra_Actual
2651 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2652 Extra_Accessibility (Formal));
2653 end;
2655 elsif Is_Entity_Name (Prev_Orig) then
2657 -- When passing an access parameter, or a renaming of an access
2658 -- parameter, as the actual to another access parameter we need
2659 -- to pass along the actual's own access level parameter. This
2660 -- is done if we are within the scope of the formal access
2661 -- parameter (if this is an inlined body the extra formal is
2662 -- irrelevant).
2664 if (Is_Formal (Entity (Prev_Orig))
2665 or else
2666 (Present (Renamed_Object (Entity (Prev_Orig)))
2667 and then
2668 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2669 and then
2670 Is_Formal
2671 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2672 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2673 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2674 then
2675 declare
2676 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2678 begin
2679 pragma Assert (Present (Parm_Ent));
2681 if Present (Extra_Accessibility (Parm_Ent)) then
2682 Add_Extra_Actual
2683 (New_Occurrence_Of
2684 (Extra_Accessibility (Parm_Ent), Loc),
2685 Extra_Accessibility (Formal));
2687 -- If the actual access parameter does not have an
2688 -- associated extra formal providing its scope level,
2689 -- then treat the actual as having library-level
2690 -- accessibility.
2692 else
2693 Add_Extra_Actual
2694 (Make_Integer_Literal (Loc,
2695 Intval => Scope_Depth (Standard_Standard)),
2696 Extra_Accessibility (Formal));
2697 end if;
2698 end;
2700 -- The actual is a normal access value, so just pass the level
2701 -- of the actual's access type.
2703 else
2704 Add_Extra_Actual
2705 (Dynamic_Accessibility_Level (Prev_Orig),
2706 Extra_Accessibility (Formal));
2707 end if;
2709 -- If the actual is an access discriminant, then pass the level
2710 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2712 elsif Nkind (Prev_Orig) = N_Selected_Component
2713 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2714 E_Discriminant
2715 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2716 E_Anonymous_Access_Type
2717 then
2718 Add_Extra_Actual
2719 (Make_Integer_Literal (Loc,
2720 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2721 Extra_Accessibility (Formal));
2723 -- All other cases
2725 else
2726 case Nkind (Prev_Orig) is
2728 when N_Attribute_Reference =>
2729 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2731 -- For X'Access, pass on the level of the prefix X
2733 when Attribute_Access =>
2735 -- If this is an Access attribute applied to the
2736 -- the current instance object passed to a type
2737 -- initialization procedure, then use the level
2738 -- of the type itself. This is not really correct,
2739 -- as there should be an extra level parameter
2740 -- passed in with _init formals (only in the case
2741 -- where the type is immutably limited), but we
2742 -- don't have an easy way currently to create such
2743 -- an extra formal (init procs aren't ever frozen).
2744 -- For now we just use the level of the type,
2745 -- which may be too shallow, but that works better
2746 -- than passing Object_Access_Level of the type,
2747 -- which can be one level too deep in some cases.
2748 -- ???
2750 if Is_Entity_Name (Prefix (Prev_Orig))
2751 and then Is_Type (Entity (Prefix (Prev_Orig)))
2752 then
2753 Add_Extra_Actual
2754 (Make_Integer_Literal (Loc,
2755 Intval =>
2756 Type_Access_Level
2757 (Entity (Prefix (Prev_Orig)))),
2758 Extra_Accessibility (Formal));
2760 else
2761 Add_Extra_Actual
2762 (Make_Integer_Literal (Loc,
2763 Intval =>
2764 Object_Access_Level
2765 (Prefix (Prev_Orig))),
2766 Extra_Accessibility (Formal));
2767 end if;
2769 -- Treat the unchecked attributes as library-level
2771 when Attribute_Unchecked_Access |
2772 Attribute_Unrestricted_Access =>
2773 Add_Extra_Actual
2774 (Make_Integer_Literal (Loc,
2775 Intval => Scope_Depth (Standard_Standard)),
2776 Extra_Accessibility (Formal));
2778 -- No other cases of attributes returning access
2779 -- values that can be passed to access parameters.
2781 when others =>
2782 raise Program_Error;
2784 end case;
2786 -- For allocators we pass the level of the execution of the
2787 -- called subprogram, which is one greater than the current
2788 -- scope level.
2790 when N_Allocator =>
2791 Add_Extra_Actual
2792 (Make_Integer_Literal (Loc,
2793 Intval => Scope_Depth (Current_Scope) + 1),
2794 Extra_Accessibility (Formal));
2796 -- For most other cases we simply pass the level of the
2797 -- actual's access type. The type is retrieved from
2798 -- Prev rather than Prev_Orig, because in some cases
2799 -- Prev_Orig denotes an original expression that has
2800 -- not been analyzed.
2802 when others =>
2803 Add_Extra_Actual
2804 (Dynamic_Accessibility_Level (Prev),
2805 Extra_Accessibility (Formal));
2806 end case;
2807 end if;
2808 end if;
2810 -- Perform the check of 4.6(49) that prevents a null value from being
2811 -- passed as an actual to an access parameter. Note that the check
2812 -- is elided in the common cases of passing an access attribute or
2813 -- access parameter as an actual. Also, we currently don't enforce
2814 -- this check for expander-generated actuals and when -gnatdj is set.
2816 if Ada_Version >= Ada_2005 then
2818 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2819 -- the intent of 6.4.1(13) is that null-exclusion checks should
2820 -- not be done for 'out' parameters, even though it refers only
2821 -- to constraint checks, and a null_exclusion is not a constraint.
2822 -- Note that AI05-0196-1 corrects this mistake in the RM.
2824 if Is_Access_Type (Etype (Formal))
2825 and then Can_Never_Be_Null (Etype (Formal))
2826 and then Ekind (Formal) /= E_Out_Parameter
2827 and then Nkind (Prev) /= N_Raise_Constraint_Error
2828 and then (Known_Null (Prev)
2829 or else not Can_Never_Be_Null (Etype (Prev)))
2830 then
2831 Install_Null_Excluding_Check (Prev);
2832 end if;
2834 -- Ada_Version < Ada_2005
2836 else
2837 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2838 or else Access_Checks_Suppressed (Subp)
2839 then
2840 null;
2842 elsif Debug_Flag_J then
2843 null;
2845 elsif not Comes_From_Source (Prev) then
2846 null;
2848 elsif Is_Entity_Name (Prev)
2849 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2850 then
2851 null;
2853 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2854 null;
2856 -- Suppress null checks when passing to access parameters of Java
2857 -- and CIL subprograms. (Should this be done for other foreign
2858 -- conventions as well ???)
2860 elsif Convention (Subp) = Convention_Java
2861 or else Convention (Subp) = Convention_CIL
2862 then
2863 null;
2865 else
2866 Install_Null_Excluding_Check (Prev);
2867 end if;
2868 end if;
2870 -- Perform appropriate validity checks on parameters that
2871 -- are entities.
2873 if Validity_Checks_On then
2874 if (Ekind (Formal) = E_In_Parameter
2875 and then Validity_Check_In_Params)
2876 or else
2877 (Ekind (Formal) = E_In_Out_Parameter
2878 and then Validity_Check_In_Out_Params)
2879 then
2880 -- If the actual is an indexed component of a packed type (or
2881 -- is an indexed or selected component whose prefix recursively
2882 -- meets this condition), it has not been expanded yet. It will
2883 -- be copied in the validity code that follows, and has to be
2884 -- expanded appropriately, so reanalyze it.
2886 -- What we do is just to unset analyzed bits on prefixes till
2887 -- we reach something that does not have a prefix.
2889 declare
2890 Nod : Node_Id;
2892 begin
2893 Nod := Actual;
2894 while Nkind_In (Nod, N_Indexed_Component,
2895 N_Selected_Component)
2896 loop
2897 Set_Analyzed (Nod, False);
2898 Nod := Prefix (Nod);
2899 end loop;
2900 end;
2902 Ensure_Valid (Actual);
2903 end if;
2904 end if;
2906 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2907 -- since this is a left side reference. We only do this for calls
2908 -- from the source program since we assume that compiler generated
2909 -- calls explicitly generate any required checks. We also need it
2910 -- only if we are doing standard validity checks, since clearly it is
2911 -- not needed if validity checks are off, and in subscript validity
2912 -- checking mode, all indexed components are checked with a call
2913 -- directly from Expand_N_Indexed_Component.
2915 if Comes_From_Source (Call_Node)
2916 and then Ekind (Formal) /= E_In_Parameter
2917 and then Validity_Checks_On
2918 and then Validity_Check_Default
2919 and then not Validity_Check_Subscripts
2920 then
2921 Check_Valid_Lvalue_Subscripts (Actual);
2922 end if;
2924 -- Mark any scalar OUT parameter that is a simple variable as no
2925 -- longer known to be valid (unless the type is always valid). This
2926 -- reflects the fact that if an OUT parameter is never set in a
2927 -- procedure, then it can become invalid on the procedure return.
2929 if Ekind (Formal) = E_Out_Parameter
2930 and then Is_Entity_Name (Actual)
2931 and then Ekind (Entity (Actual)) = E_Variable
2932 and then not Is_Known_Valid (Etype (Actual))
2933 then
2934 Set_Is_Known_Valid (Entity (Actual), False);
2935 end if;
2937 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2938 -- clear current values, since they can be clobbered. We are probably
2939 -- doing this in more places than we need to, but better safe than
2940 -- sorry when it comes to retaining bad current values.
2942 if Ekind (Formal) /= E_In_Parameter
2943 and then Is_Entity_Name (Actual)
2944 and then Present (Entity (Actual))
2945 then
2946 declare
2947 Ent : constant Entity_Id := Entity (Actual);
2948 Sav : Node_Id;
2950 begin
2951 -- For an OUT or IN OUT parameter that is an assignable entity,
2952 -- we do not want to clobber the Last_Assignment field, since
2953 -- if it is set, it was precisely because it is indeed an OUT
2954 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
2955 -- since the subprogram could have returned in invalid value.
2957 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
2958 and then Is_Assignable (Ent)
2959 then
2960 Sav := Last_Assignment (Ent);
2961 Kill_Current_Values (Ent);
2962 Set_Last_Assignment (Ent, Sav);
2963 Set_Is_Known_Valid (Ent, False);
2965 -- For all other cases, just kill the current values
2967 else
2968 Kill_Current_Values (Ent);
2969 end if;
2970 end;
2971 end if;
2973 -- If the formal is class wide and the actual is an aggregate, force
2974 -- evaluation so that the back end who does not know about class-wide
2975 -- type, does not generate a temporary of the wrong size.
2977 if not Is_Class_Wide_Type (Etype (Formal)) then
2978 null;
2980 elsif Nkind (Actual) = N_Aggregate
2981 or else (Nkind (Actual) = N_Qualified_Expression
2982 and then Nkind (Expression (Actual)) = N_Aggregate)
2983 then
2984 Force_Evaluation (Actual);
2985 end if;
2987 -- In a remote call, if the formal is of a class-wide type, check
2988 -- that the actual meets the requirements described in E.4(18).
2990 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2991 Insert_Action (Actual,
2992 Make_Transportable_Check (Loc,
2993 Duplicate_Subexpr_Move_Checks (Actual)));
2994 end if;
2996 -- This label is required when skipping extra actual generation for
2997 -- Unchecked_Union parameters.
2999 <<Skip_Extra_Actual_Generation>>
3001 Param_Count := Param_Count + 1;
3002 Next_Actual (Actual);
3003 Next_Formal (Formal);
3004 end loop;
3006 -- If we are calling an Ada 2012 function which needs to have the
3007 -- "accessibility level determined by the point of call" (AI05-0234)
3008 -- passed in to it, then pass it in.
3010 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3011 and then
3012 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3013 then
3014 declare
3015 Ancestor : Node_Id := Parent (Call_Node);
3016 Level : Node_Id := Empty;
3017 Defer : Boolean := False;
3019 begin
3020 -- Unimplemented: if Subp returns an anonymous access type, then
3022 -- a) if the call is the operand of an explict conversion, then
3023 -- the target type of the conversion (a named access type)
3024 -- determines the accessibility level pass in;
3026 -- b) if the call defines an access discriminant of an object
3027 -- (e.g., the discriminant of an object being created by an
3028 -- allocator, or the discriminant of a function result),
3029 -- then the accessibility level to pass in is that of the
3030 -- discriminated object being initialized).
3032 -- ???
3034 while Nkind (Ancestor) = N_Qualified_Expression
3035 loop
3036 Ancestor := Parent (Ancestor);
3037 end loop;
3039 case Nkind (Ancestor) is
3040 when N_Allocator =>
3042 -- At this point, we'd like to assign
3044 -- Level := Dynamic_Accessibility_Level (Ancestor);
3046 -- but Etype of Ancestor may not have been set yet,
3047 -- so that doesn't work.
3049 -- Handle this later in Expand_Allocator_Expression.
3051 Defer := True;
3053 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3054 declare
3055 Def_Id : constant Entity_Id :=
3056 Defining_Identifier (Ancestor);
3058 begin
3059 if Is_Return_Object (Def_Id) then
3060 if Present (Extra_Accessibility_Of_Result
3061 (Return_Applies_To (Scope (Def_Id))))
3062 then
3063 -- Pass along value that was passed in if the
3064 -- routine we are returning from also has an
3065 -- Accessibility_Of_Result formal.
3067 Level :=
3068 New_Occurrence_Of
3069 (Extra_Accessibility_Of_Result
3070 (Return_Applies_To (Scope (Def_Id))), Loc);
3071 end if;
3072 else
3073 Level :=
3074 Make_Integer_Literal (Loc,
3075 Intval => Object_Access_Level (Def_Id));
3076 end if;
3077 end;
3079 when N_Simple_Return_Statement =>
3080 if Present (Extra_Accessibility_Of_Result
3081 (Return_Applies_To
3082 (Return_Statement_Entity (Ancestor))))
3083 then
3084 -- Pass along value that was passed in if the returned
3085 -- routine also has an Accessibility_Of_Result formal.
3087 Level :=
3088 New_Occurrence_Of
3089 (Extra_Accessibility_Of_Result
3090 (Return_Applies_To
3091 (Return_Statement_Entity (Ancestor))), Loc);
3092 end if;
3094 when others =>
3095 null;
3096 end case;
3098 if not Defer then
3099 if not Present (Level) then
3101 -- The "innermost master that evaluates the function call".
3103 -- ??? - Should we use Integer'Last here instead in order
3104 -- to deal with (some of) the problems associated with
3105 -- calls to subps whose enclosing scope is unknown (e.g.,
3106 -- Anon_Access_To_Subp_Param.all)?
3108 Level := Make_Integer_Literal (Loc,
3109 Scope_Depth (Current_Scope) + 1);
3110 end if;
3112 Add_Extra_Actual
3113 (Level,
3114 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3115 end if;
3116 end;
3117 end if;
3119 -- If we are expanding the RHS of an assignment we need to check if tag
3120 -- propagation is needed. You might expect this processing to be in
3121 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3122 -- assignment might be transformed to a declaration for an unconstrained
3123 -- value if the expression is classwide.
3125 if Nkind (Call_Node) = N_Function_Call
3126 and then Is_Tag_Indeterminate (Call_Node)
3127 and then Is_Entity_Name (Name (Call_Node))
3128 then
3129 declare
3130 Ass : Node_Id := Empty;
3132 begin
3133 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3134 Ass := Parent (Call_Node);
3136 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3137 and then Nkind (Parent (Parent (Call_Node))) =
3138 N_Assignment_Statement
3139 then
3140 Ass := Parent (Parent (Call_Node));
3142 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3143 and then Nkind (Parent (Parent (Call_Node))) =
3144 N_Assignment_Statement
3145 then
3146 Ass := Parent (Parent (Call_Node));
3147 end if;
3149 if Present (Ass)
3150 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3151 then
3152 if Is_Access_Type (Etype (Call_Node)) then
3153 if Designated_Type (Etype (Call_Node)) /=
3154 Root_Type (Etype (Name (Ass)))
3155 then
3156 Error_Msg_NE
3157 ("tag-indeterminate expression "
3158 & " must have designated type& (RM 5.2 (6))",
3159 Call_Node, Root_Type (Etype (Name (Ass))));
3160 else
3161 Propagate_Tag (Name (Ass), Call_Node);
3162 end if;
3164 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3165 Error_Msg_NE
3166 ("tag-indeterminate expression must have type&"
3167 & "(RM 5.2 (6))",
3168 Call_Node, Root_Type (Etype (Name (Ass))));
3170 else
3171 Propagate_Tag (Name (Ass), Call_Node);
3172 end if;
3174 -- The call will be rewritten as a dispatching call, and
3175 -- expanded as such.
3177 return;
3178 end if;
3179 end;
3180 end if;
3182 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3183 -- it to point to the correct secondary virtual table
3185 if Nkind (Call_Node) in N_Subprogram_Call
3186 and then CW_Interface_Formals_Present
3187 then
3188 Expand_Interface_Actuals (Call_Node);
3189 end if;
3191 -- Deals with Dispatch_Call if we still have a call, before expanding
3192 -- extra actuals since this will be done on the re-analysis of the
3193 -- dispatching call. Note that we do not try to shorten the actual list
3194 -- for a dispatching call, it would not make sense to do so. Expansion
3195 -- of dispatching calls is suppressed when VM_Target, because the VM
3196 -- back-ends directly handle the generation of dispatching calls and
3197 -- would have to undo any expansion to an indirect call.
3199 if Nkind (Call_Node) in N_Subprogram_Call
3200 and then Present (Controlling_Argument (Call_Node))
3201 then
3202 declare
3203 Call_Typ : constant Entity_Id := Etype (Call_Node);
3204 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3205 Eq_Prim_Op : Entity_Id := Empty;
3206 New_Call : Node_Id;
3207 Param : Node_Id;
3208 Prev_Call : Node_Id;
3210 begin
3211 if not Is_Limited_Type (Typ) then
3212 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3213 end if;
3215 if Tagged_Type_Expansion then
3216 Expand_Dispatching_Call (Call_Node);
3218 -- The following return is worrisome. Is it really OK to skip
3219 -- all remaining processing in this procedure ???
3221 return;
3223 -- VM targets
3225 else
3226 Apply_Tag_Checks (Call_Node);
3228 -- If this is a dispatching "=", we must first compare the
3229 -- tags so we generate: x.tag = y.tag and then x = y
3231 if Subp = Eq_Prim_Op then
3233 -- Mark the node as analyzed to avoid reanalizing this
3234 -- dispatching call (which would cause a never-ending loop)
3236 Prev_Call := Relocate_Node (Call_Node);
3237 Set_Analyzed (Prev_Call);
3239 Param := First_Actual (Call_Node);
3240 New_Call :=
3241 Make_And_Then (Loc,
3242 Left_Opnd =>
3243 Make_Op_Eq (Loc,
3244 Left_Opnd =>
3245 Make_Selected_Component (Loc,
3246 Prefix => New_Value (Param),
3247 Selector_Name =>
3248 New_Occurrence_Of
3249 (First_Tag_Component (Typ), Loc)),
3251 Right_Opnd =>
3252 Make_Selected_Component (Loc,
3253 Prefix =>
3254 Unchecked_Convert_To (Typ,
3255 New_Value (Next_Actual (Param))),
3256 Selector_Name =>
3257 New_Occurrence_Of
3258 (First_Tag_Component (Typ), Loc))),
3259 Right_Opnd => Prev_Call);
3261 Rewrite (Call_Node, New_Call);
3263 Analyze_And_Resolve
3264 (Call_Node, Call_Typ, Suppress => All_Checks);
3265 end if;
3267 -- Expansion of a dispatching call results in an indirect call,
3268 -- which in turn causes current values to be killed (see
3269 -- Resolve_Call), so on VM targets we do the call here to
3270 -- ensure consistent warnings between VM and non-VM targets.
3272 Kill_Current_Values;
3273 end if;
3275 -- If this is a dispatching "=" then we must update the reference
3276 -- to the call node because we generated:
3277 -- x.tag = y.tag and then x = y
3279 if Subp = Eq_Prim_Op then
3280 Call_Node := Right_Opnd (Call_Node);
3281 end if;
3282 end;
3283 end if;
3285 -- Similarly, expand calls to RCI subprograms on which pragma
3286 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3287 -- later. Do this only when the call comes from source since we
3288 -- do not want such a rewriting to occur in expanded code.
3290 if Is_All_Remote_Call (Call_Node) then
3291 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3293 -- Similarly, do not add extra actuals for an entry call whose entity
3294 -- is a protected procedure, or for an internal protected subprogram
3295 -- call, because it will be rewritten as a protected subprogram call
3296 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3298 elsif Is_Protected_Type (Scope (Subp))
3299 and then (Ekind (Subp) = E_Procedure
3300 or else Ekind (Subp) = E_Function)
3301 then
3302 null;
3304 -- During that loop we gathered the extra actuals (the ones that
3305 -- correspond to Extra_Formals), so now they can be appended.
3307 else
3308 while Is_Non_Empty_List (Extra_Actuals) loop
3309 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3310 end loop;
3311 end if;
3313 -- At this point we have all the actuals, so this is the point at which
3314 -- the various expansion activities for actuals is carried out.
3316 Expand_Actuals (Call_Node, Subp);
3318 -- Verify that the actuals do not share storage. This check must be done
3319 -- on the caller side rather that inside the subprogram to avoid issues
3320 -- of parameter passing.
3322 if Check_Aliasing_Of_Parameters then
3323 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3324 end if;
3326 -- If the subprogram is a renaming, or if it is inherited, replace it in
3327 -- the call with the name of the actual subprogram being called. If this
3328 -- is a dispatching call, the run-time decides what to call. The Alias
3329 -- attribute does not apply to entries.
3331 if Nkind (Call_Node) /= N_Entry_Call_Statement
3332 and then No (Controlling_Argument (Call_Node))
3333 and then Present (Parent_Subp)
3334 and then not Is_Direct_Deep_Call (Subp)
3335 then
3336 if Present (Inherited_From_Formal (Subp)) then
3337 Parent_Subp := Inherited_From_Formal (Subp);
3338 else
3339 Parent_Subp := Ultimate_Alias (Parent_Subp);
3340 end if;
3342 -- The below setting of Entity is suspect, see F109-018 discussion???
3344 Set_Entity (Name (Call_Node), Parent_Subp);
3346 if Is_Abstract_Subprogram (Parent_Subp)
3347 and then not In_Instance
3348 then
3349 Error_Msg_NE
3350 ("cannot call abstract subprogram &!",
3351 Name (Call_Node), Parent_Subp);
3352 end if;
3354 -- Inspect all formals of derived subprogram Subp. Compare parameter
3355 -- types with the parent subprogram and check whether an actual may
3356 -- need a type conversion to the corresponding formal of the parent
3357 -- subprogram.
3359 -- Not clear whether intrinsic subprograms need such conversions. ???
3361 if not Is_Intrinsic_Subprogram (Parent_Subp)
3362 or else Is_Generic_Instance (Parent_Subp)
3363 then
3364 declare
3365 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3366 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3367 -- and resolve the newly generated construct.
3369 -------------
3370 -- Convert --
3371 -------------
3373 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3374 begin
3375 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3376 Analyze (Act);
3377 Resolve (Act, Typ);
3378 end Convert;
3380 -- Local variables
3382 Actual_Typ : Entity_Id;
3383 Formal_Typ : Entity_Id;
3384 Parent_Typ : Entity_Id;
3386 begin
3387 Actual := First_Actual (Call_Node);
3388 Formal := First_Formal (Subp);
3389 Parent_Formal := First_Formal (Parent_Subp);
3390 while Present (Formal) loop
3391 Actual_Typ := Etype (Actual);
3392 Formal_Typ := Etype (Formal);
3393 Parent_Typ := Etype (Parent_Formal);
3395 -- For an IN parameter of a scalar type, the parent formal
3396 -- type and derived formal type differ or the parent formal
3397 -- type and actual type do not match statically.
3399 if Is_Scalar_Type (Formal_Typ)
3400 and then Ekind (Formal) = E_In_Parameter
3401 and then Formal_Typ /= Parent_Typ
3402 and then
3403 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3404 and then not Raises_Constraint_Error (Actual)
3405 then
3406 Convert (Actual, Parent_Typ);
3407 Enable_Range_Check (Actual);
3409 -- If the actual has been marked as requiring a range
3410 -- check, then generate it here.
3412 if Do_Range_Check (Actual) then
3413 Generate_Range_Check
3414 (Actual, Etype (Formal), CE_Range_Check_Failed);
3415 end if;
3417 -- For access types, the parent formal type and actual type
3418 -- differ.
3420 elsif Is_Access_Type (Formal_Typ)
3421 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3422 then
3423 if Ekind (Formal) /= E_In_Parameter then
3424 Convert (Actual, Parent_Typ);
3426 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3427 and then Designated_Type (Parent_Typ) /=
3428 Designated_Type (Actual_Typ)
3429 and then not Is_Controlling_Formal (Formal)
3430 then
3431 -- This unchecked conversion is not necessary unless
3432 -- inlining is enabled, because in that case the type
3433 -- mismatch may become visible in the body about to be
3434 -- inlined.
3436 Rewrite (Actual,
3437 Unchecked_Convert_To (Parent_Typ,
3438 Relocate_Node (Actual)));
3439 Analyze (Actual);
3440 Resolve (Actual, Parent_Typ);
3441 end if;
3443 -- If there is a change of representation, then generate a
3444 -- warning, and do the change of representation.
3446 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3447 Error_Msg_N
3448 ("??change of representation required", Actual);
3449 Convert (Actual, Parent_Typ);
3451 -- For array and record types, the parent formal type and
3452 -- derived formal type have different sizes or pragma Pack
3453 -- status.
3455 elsif ((Is_Array_Type (Formal_Typ)
3456 and then Is_Array_Type (Parent_Typ))
3457 or else
3458 (Is_Record_Type (Formal_Typ)
3459 and then Is_Record_Type (Parent_Typ)))
3460 and then
3461 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3462 or else Has_Pragma_Pack (Formal_Typ) /=
3463 Has_Pragma_Pack (Parent_Typ))
3464 then
3465 Convert (Actual, Parent_Typ);
3466 end if;
3468 Next_Actual (Actual);
3469 Next_Formal (Formal);
3470 Next_Formal (Parent_Formal);
3471 end loop;
3472 end;
3473 end if;
3475 Orig_Subp := Subp;
3476 Subp := Parent_Subp;
3477 end if;
3479 -- Deal with case where call is an explicit dereference
3481 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3483 -- Handle case of access to protected subprogram type
3485 if Is_Access_Protected_Subprogram_Type
3486 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3487 then
3488 -- If this is a call through an access to protected operation, the
3489 -- prefix has the form (object'address, operation'access). Rewrite
3490 -- as a for other protected calls: the object is the 1st parameter
3491 -- of the list of actuals.
3493 declare
3494 Call : Node_Id;
3495 Parm : List_Id;
3496 Nam : Node_Id;
3497 Obj : Node_Id;
3498 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3500 T : constant Entity_Id :=
3501 Equivalent_Type (Base_Type (Etype (Ptr)));
3503 D_T : constant Entity_Id :=
3504 Designated_Type (Base_Type (Etype (Ptr)));
3506 begin
3507 Obj :=
3508 Make_Selected_Component (Loc,
3509 Prefix => Unchecked_Convert_To (T, Ptr),
3510 Selector_Name =>
3511 New_Occurrence_Of (First_Entity (T), Loc));
3513 Nam :=
3514 Make_Selected_Component (Loc,
3515 Prefix => Unchecked_Convert_To (T, Ptr),
3516 Selector_Name =>
3517 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3519 Nam :=
3520 Make_Explicit_Dereference (Loc,
3521 Prefix => Nam);
3523 if Present (Parameter_Associations (Call_Node)) then
3524 Parm := Parameter_Associations (Call_Node);
3525 else
3526 Parm := New_List;
3527 end if;
3529 Prepend (Obj, Parm);
3531 if Etype (D_T) = Standard_Void_Type then
3532 Call :=
3533 Make_Procedure_Call_Statement (Loc,
3534 Name => Nam,
3535 Parameter_Associations => Parm);
3536 else
3537 Call :=
3538 Make_Function_Call (Loc,
3539 Name => Nam,
3540 Parameter_Associations => Parm);
3541 end if;
3543 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3544 Set_Etype (Call, Etype (D_T));
3546 -- We do not re-analyze the call to avoid infinite recursion.
3547 -- We analyze separately the prefix and the object, and set
3548 -- the checks on the prefix that would otherwise be emitted
3549 -- when resolving a call.
3551 Rewrite (Call_Node, Call);
3552 Analyze (Nam);
3553 Apply_Access_Check (Nam);
3554 Analyze (Obj);
3555 return;
3556 end;
3557 end if;
3558 end if;
3560 -- If this is a call to an intrinsic subprogram, then perform the
3561 -- appropriate expansion to the corresponding tree node and we
3562 -- are all done (since after that the call is gone).
3564 -- In the case where the intrinsic is to be processed by the back end,
3565 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3566 -- since the idea in this case is to pass the call unchanged. If the
3567 -- intrinsic is an inherited unchecked conversion, and the derived type
3568 -- is the target type of the conversion, we must retain it as the return
3569 -- type of the expression. Otherwise the expansion below, which uses the
3570 -- parent operation, will yield the wrong type.
3572 if Is_Intrinsic_Subprogram (Subp) then
3573 Expand_Intrinsic_Call (Call_Node, Subp);
3575 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3576 and then Parent_Subp /= Orig_Subp
3577 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3578 then
3579 Set_Etype (Call_Node, Etype (Orig_Subp));
3580 end if;
3582 return;
3583 end if;
3585 if Ekind_In (Subp, E_Function, E_Procedure) then
3587 -- We perform two simple optimization on calls:
3589 -- a) replace calls to null procedures unconditionally;
3591 -- b) for To_Address, just do an unchecked conversion. Not only is
3592 -- this efficient, but it also avoids order of elaboration problems
3593 -- when address clauses are inlined (address expression elaborated
3594 -- at the wrong point).
3596 -- We perform these optimization regardless of whether we are in the
3597 -- main unit or in a unit in the context of the main unit, to ensure
3598 -- that tree generated is the same in both cases, for CodePeer use.
3600 if Is_RTE (Subp, RE_To_Address) then
3601 Rewrite (Call_Node,
3602 Unchecked_Convert_To
3603 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3604 return;
3606 elsif Is_Null_Procedure (Subp) then
3607 Rewrite (Call_Node, Make_Null_Statement (Loc));
3608 return;
3609 end if;
3611 -- Handle inlining. No action needed if the subprogram is not inlined
3613 if not Is_Inlined (Subp) then
3614 null;
3616 -- Handle frontend inlining
3618 elsif not Back_End_Inlining then
3619 Inlined_Subprogram : declare
3620 Bod : Node_Id;
3621 Must_Inline : Boolean := False;
3622 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3624 begin
3625 -- Verify that the body to inline has already been seen, and
3626 -- that if the body is in the current unit the inlining does
3627 -- not occur earlier. This avoids order-of-elaboration problems
3628 -- in the back end.
3630 -- This should be documented in sinfo/einfo ???
3632 if No (Spec)
3633 or else Nkind (Spec) /= N_Subprogram_Declaration
3634 or else No (Body_To_Inline (Spec))
3635 then
3636 Must_Inline := False;
3638 -- If this an inherited function that returns a private type,
3639 -- do not inline if the full view is an unconstrained array,
3640 -- because such calls cannot be inlined.
3642 elsif Present (Orig_Subp)
3643 and then Is_Array_Type (Etype (Orig_Subp))
3644 and then not Is_Constrained (Etype (Orig_Subp))
3645 then
3646 Must_Inline := False;
3648 elsif In_Unfrozen_Instance (Scope (Subp)) then
3649 Must_Inline := False;
3651 else
3652 Bod := Body_To_Inline (Spec);
3654 if (In_Extended_Main_Code_Unit (Call_Node)
3655 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3656 or else Has_Pragma_Inline_Always (Subp))
3657 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3658 or else
3659 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3660 then
3661 Must_Inline := True;
3663 -- If we are compiling a package body that is not the main
3664 -- unit, it must be for inlining/instantiation purposes,
3665 -- in which case we inline the call to insure that the same
3666 -- temporaries are generated when compiling the body by
3667 -- itself. Otherwise link errors can occur.
3669 -- If the function being called is itself in the main unit,
3670 -- we cannot inline, because there is a risk of double
3671 -- elaboration and/or circularity: the inlining can make
3672 -- visible a private entity in the body of the main unit,
3673 -- that gigi will see before its sees its proper definition.
3675 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3676 and then In_Package_Body
3677 then
3678 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3679 end if;
3680 end if;
3682 if Must_Inline then
3683 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3685 else
3686 -- Let the back end handle it
3688 Add_Inlined_Body (Subp);
3690 if Front_End_Inlining
3691 and then Nkind (Spec) = N_Subprogram_Declaration
3692 and then (In_Extended_Main_Code_Unit (Call_Node))
3693 and then No (Body_To_Inline (Spec))
3694 and then not Has_Completion (Subp)
3695 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3696 then
3697 Cannot_Inline
3698 ("cannot inline& (body not seen yet)?",
3699 Call_Node, Subp);
3700 end if;
3701 end if;
3702 end Inlined_Subprogram;
3704 -- Back end inlining: let the back end handle it
3706 elsif No (Unit_Declaration_Node (Subp))
3707 or else Nkind (Unit_Declaration_Node (Subp)) /=
3708 N_Subprogram_Declaration
3709 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
3710 then
3711 Add_Inlined_Body (Subp);
3712 Register_Backend_Call (Call_Node);
3714 -- If the call is to a function in a run-time unit that is marked
3715 -- Inline_Always, we must suppress debugging information on it,
3716 -- so that the code that is eventually inlined will not affect
3717 -- debugging of the user program.
3719 if Is_Predefined_File_Name
3720 (Unit_File_Name (Get_Source_Unit (Sloc (Subp))))
3721 and then In_Extended_Main_Source_Unit (N)
3722 then
3723 Set_Needs_Debug_Info (Subp, False);
3724 end if;
3726 -- Front end expansion of simple functions returning unconstrained
3727 -- types (see Check_And_Split_Unconstrained_Function) and simple
3728 -- renamings inlined by the front end (see Build_Renamed_Entity).
3730 else
3731 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3732 end if;
3733 end if;
3735 -- Check for protected subprogram. This is either an intra-object call,
3736 -- or a protected function call. Protected procedure calls are rewritten
3737 -- as entry calls and handled accordingly.
3739 -- In Ada 2005, this may be an indirect call to an access parameter that
3740 -- is an access_to_subprogram. In that case the anonymous type has a
3741 -- scope that is a protected operation, but the call is a regular one.
3742 -- In either case do not expand call if subprogram is eliminated.
3744 Scop := Scope (Subp);
3746 if Nkind (Call_Node) /= N_Entry_Call_Statement
3747 and then Is_Protected_Type (Scop)
3748 and then Ekind (Subp) /= E_Subprogram_Type
3749 and then not Is_Eliminated (Subp)
3750 then
3751 -- If the call is an internal one, it is rewritten as a call to the
3752 -- corresponding unprotected subprogram.
3754 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3755 end if;
3757 -- Functions returning controlled objects need special attention. If
3758 -- the return type is limited, then the context is initialization and
3759 -- different processing applies. If the call is to a protected function,
3760 -- the expansion above will call Expand_Call recursively. Otherwise the
3761 -- function call is transformed into a temporary which obtains the
3762 -- result from the secondary stack.
3764 if Needs_Finalization (Etype (Subp)) then
3765 if not Is_Limited_View (Etype (Subp))
3766 and then
3767 (No (First_Formal (Subp))
3768 or else
3769 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3770 then
3771 Expand_Ctrl_Function_Call (Call_Node);
3773 -- Build-in-place function calls which appear in anonymous contexts
3774 -- need a transient scope to ensure the proper finalization of the
3775 -- intermediate result after its use.
3777 elsif Is_Build_In_Place_Function_Call (Call_Node)
3778 and then
3779 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3780 N_Function_Call,
3781 N_Indexed_Component,
3782 N_Object_Renaming_Declaration,
3783 N_Procedure_Call_Statement,
3784 N_Selected_Component,
3785 N_Slice)
3786 then
3787 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3788 end if;
3789 end if;
3790 end Expand_Call;
3792 -------------------------------
3793 -- Expand_Ctrl_Function_Call --
3794 -------------------------------
3796 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3797 function Is_Element_Reference (N : Node_Id) return Boolean;
3798 -- Determine whether node N denotes a reference to an Ada 2012 container
3799 -- element.
3801 --------------------------
3802 -- Is_Element_Reference --
3803 --------------------------
3805 function Is_Element_Reference (N : Node_Id) return Boolean is
3806 Ref : constant Node_Id := Original_Node (N);
3808 begin
3809 -- Analysis marks an element reference by setting the generalized
3810 -- indexing attribute of an indexed component before the component
3811 -- is rewritten into a function call.
3813 return
3814 Nkind (Ref) = N_Indexed_Component
3815 and then Present (Generalized_Indexing (Ref));
3816 end Is_Element_Reference;
3818 -- Local variables
3820 Is_Elem_Ref : constant Boolean := Is_Element_Reference (N);
3822 -- Start of processing for Expand_Ctrl_Function_Call
3824 begin
3825 -- Optimization, if the returned value (which is on the sec-stack) is
3826 -- returned again, no need to copy/readjust/finalize, we can just pass
3827 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3828 -- attachment is needed
3830 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3831 return;
3832 end if;
3834 -- Resolution is now finished, make sure we don't start analysis again
3835 -- because of the duplication.
3837 Set_Analyzed (N);
3839 -- A function which returns a controlled object uses the secondary
3840 -- stack. Rewrite the call into a temporary which obtains the result of
3841 -- the function using 'reference.
3843 Remove_Side_Effects (N);
3845 -- When the temporary function result appears inside a case expression
3846 -- or an if expression, its lifetime must be extended to match that of
3847 -- the context. If not, the function result will be finalized too early
3848 -- and the evaluation of the expression could yield incorrect result. An
3849 -- exception to this rule are references to Ada 2012 container elements.
3850 -- Such references must be finalized at the end of each iteration of the
3851 -- related quantified expression, otherwise the container will remain
3852 -- busy.
3854 if not Is_Elem_Ref
3855 and then Within_Case_Or_If_Expression (N)
3856 and then Nkind (N) = N_Explicit_Dereference
3857 then
3858 Set_Is_Processed_Transient (Entity (Prefix (N)));
3859 end if;
3860 end Expand_Ctrl_Function_Call;
3862 ----------------------------------------
3863 -- Expand_N_Extended_Return_Statement --
3864 ----------------------------------------
3866 -- If there is a Handled_Statement_Sequence, we rewrite this:
3868 -- return Result : T := <expression> do
3869 -- <handled_seq_of_stms>
3870 -- end return;
3872 -- to be:
3874 -- declare
3875 -- Result : T := <expression>;
3876 -- begin
3877 -- <handled_seq_of_stms>
3878 -- return Result;
3879 -- end;
3881 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3883 -- return Result : T := <expression>;
3885 -- to be:
3887 -- return <expression>;
3889 -- unless it's build-in-place or there's no <expression>, in which case
3890 -- we generate:
3892 -- declare
3893 -- Result : T := <expression>;
3894 -- begin
3895 -- return Result;
3896 -- end;
3898 -- Note that this case could have been written by the user as an extended
3899 -- return statement, or could have been transformed to this from a simple
3900 -- return statement.
3902 -- That is, we need to have a reified return object if there are statements
3903 -- (which might refer to it) or if we're doing build-in-place (so we can
3904 -- set its address to the final resting place or if there is no expression
3905 -- (in which case default initial values might need to be set).
3907 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
3908 Loc : constant Source_Ptr := Sloc (N);
3910 Par_Func : constant Entity_Id :=
3911 Return_Applies_To (Return_Statement_Entity (N));
3912 Result_Subt : constant Entity_Id := Etype (Par_Func);
3913 Ret_Obj_Id : constant Entity_Id :=
3914 First_Entity (Return_Statement_Entity (N));
3915 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
3917 Is_Build_In_Place : constant Boolean :=
3918 Is_Build_In_Place_Function (Par_Func);
3920 Exp : Node_Id;
3921 HSS : Node_Id;
3922 Result : Node_Id;
3923 Return_Stmt : Node_Id;
3924 Stmts : List_Id;
3926 function Build_Heap_Allocator
3927 (Temp_Id : Entity_Id;
3928 Temp_Typ : Entity_Id;
3929 Func_Id : Entity_Id;
3930 Ret_Typ : Entity_Id;
3931 Alloc_Expr : Node_Id) return Node_Id;
3932 -- Create the statements necessary to allocate a return object on the
3933 -- caller's master. The master is available through implicit parameter
3934 -- BIPfinalizationmaster.
3936 -- if BIPfinalizationmaster /= null then
3937 -- declare
3938 -- type Ptr_Typ is access Ret_Typ;
3939 -- for Ptr_Typ'Storage_Pool use
3940 -- Base_Pool (BIPfinalizationmaster.all).all;
3941 -- Local : Ptr_Typ;
3943 -- begin
3944 -- procedure Allocate (...) is
3945 -- begin
3946 -- System.Storage_Pools.Subpools.Allocate_Any (...);
3947 -- end Allocate;
3949 -- Local := <Alloc_Expr>;
3950 -- Temp_Id := Temp_Typ (Local);
3951 -- end;
3952 -- end if;
3954 -- Temp_Id is the temporary which is used to reference the internally
3955 -- created object in all allocation forms. Temp_Typ is the type of the
3956 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
3957 -- type of Func_Id. Alloc_Expr is the actual allocator.
3959 function Move_Activation_Chain return Node_Id;
3960 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
3961 -- with parameters:
3962 -- From current activation chain
3963 -- To activation chain passed in by the caller
3964 -- New_Master master passed in by the caller
3966 --------------------------
3967 -- Build_Heap_Allocator --
3968 --------------------------
3970 function Build_Heap_Allocator
3971 (Temp_Id : Entity_Id;
3972 Temp_Typ : Entity_Id;
3973 Func_Id : Entity_Id;
3974 Ret_Typ : Entity_Id;
3975 Alloc_Expr : Node_Id) return Node_Id
3977 begin
3978 pragma Assert (Is_Build_In_Place_Function (Func_Id));
3980 -- Processing for build-in-place object allocation. This is disabled
3981 -- on .NET/JVM because the targets do not support pools.
3983 if VM_Target = No_VM
3984 and then Needs_Finalization (Ret_Typ)
3985 then
3986 declare
3987 Decls : constant List_Id := New_List;
3988 Fin_Mas_Id : constant Entity_Id :=
3989 Build_In_Place_Formal
3990 (Func_Id, BIP_Finalization_Master);
3991 Stmts : constant List_Id := New_List;
3992 Desig_Typ : Entity_Id;
3993 Local_Id : Entity_Id;
3994 Pool_Id : Entity_Id;
3995 Ptr_Typ : Entity_Id;
3997 begin
3998 -- Generate:
3999 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4001 Pool_Id := Make_Temporary (Loc, 'P');
4003 Append_To (Decls,
4004 Make_Object_Renaming_Declaration (Loc,
4005 Defining_Identifier => Pool_Id,
4006 Subtype_Mark =>
4007 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4008 Name =>
4009 Make_Explicit_Dereference (Loc,
4010 Prefix =>
4011 Make_Function_Call (Loc,
4012 Name =>
4013 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4014 Parameter_Associations => New_List (
4015 Make_Explicit_Dereference (Loc,
4016 Prefix =>
4017 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4019 -- Create an access type which uses the storage pool of the
4020 -- caller's master. This additional type is necessary because
4021 -- the finalization master cannot be associated with the type
4022 -- of the temporary. Otherwise the secondary stack allocation
4023 -- will fail.
4025 Desig_Typ := Ret_Typ;
4027 -- Ensure that the build-in-place machinery uses a fat pointer
4028 -- when allocating an unconstrained array on the heap. In this
4029 -- case the result object type is a constrained array type even
4030 -- though the function type is unconstrained.
4032 if Ekind (Desig_Typ) = E_Array_Subtype then
4033 Desig_Typ := Base_Type (Desig_Typ);
4034 end if;
4036 -- Generate:
4037 -- type Ptr_Typ is access Desig_Typ;
4039 Ptr_Typ := Make_Temporary (Loc, 'P');
4041 Append_To (Decls,
4042 Make_Full_Type_Declaration (Loc,
4043 Defining_Identifier => Ptr_Typ,
4044 Type_Definition =>
4045 Make_Access_To_Object_Definition (Loc,
4046 Subtype_Indication =>
4047 New_Occurrence_Of (Desig_Typ, Loc))));
4049 -- Perform minor decoration in order to set the master and the
4050 -- storage pool attributes.
4052 Set_Ekind (Ptr_Typ, E_Access_Type);
4053 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4054 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4056 -- Create the temporary, generate:
4057 -- Local_Id : Ptr_Typ;
4059 Local_Id := Make_Temporary (Loc, 'T');
4061 Append_To (Decls,
4062 Make_Object_Declaration (Loc,
4063 Defining_Identifier => Local_Id,
4064 Object_Definition =>
4065 New_Occurrence_Of (Ptr_Typ, Loc)));
4067 -- Allocate the object, generate:
4068 -- Local_Id := <Alloc_Expr>;
4070 Append_To (Stmts,
4071 Make_Assignment_Statement (Loc,
4072 Name => New_Occurrence_Of (Local_Id, Loc),
4073 Expression => Alloc_Expr));
4075 -- Generate:
4076 -- Temp_Id := Temp_Typ (Local_Id);
4078 Append_To (Stmts,
4079 Make_Assignment_Statement (Loc,
4080 Name => New_Occurrence_Of (Temp_Id, Loc),
4081 Expression =>
4082 Unchecked_Convert_To (Temp_Typ,
4083 New_Occurrence_Of (Local_Id, Loc))));
4085 -- Wrap the allocation in a block. This is further conditioned
4086 -- by checking the caller finalization master at runtime. A
4087 -- null value indicates a non-existent master, most likely due
4088 -- to a Finalize_Storage_Only allocation.
4090 -- Generate:
4091 -- if BIPfinalizationmaster /= null then
4092 -- declare
4093 -- <Decls>
4094 -- begin
4095 -- <Stmts>
4096 -- end;
4097 -- end if;
4099 return
4100 Make_If_Statement (Loc,
4101 Condition =>
4102 Make_Op_Ne (Loc,
4103 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4104 Right_Opnd => Make_Null (Loc)),
4106 Then_Statements => New_List (
4107 Make_Block_Statement (Loc,
4108 Declarations => Decls,
4109 Handled_Statement_Sequence =>
4110 Make_Handled_Sequence_Of_Statements (Loc,
4111 Statements => Stmts))));
4112 end;
4114 -- For all other cases, generate:
4115 -- Temp_Id := <Alloc_Expr>;
4117 else
4118 return
4119 Make_Assignment_Statement (Loc,
4120 Name => New_Occurrence_Of (Temp_Id, Loc),
4121 Expression => Alloc_Expr);
4122 end if;
4123 end Build_Heap_Allocator;
4125 ---------------------------
4126 -- Move_Activation_Chain --
4127 ---------------------------
4129 function Move_Activation_Chain return Node_Id is
4130 begin
4131 return
4132 Make_Procedure_Call_Statement (Loc,
4133 Name =>
4134 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4136 Parameter_Associations => New_List (
4138 -- Source chain
4140 Make_Attribute_Reference (Loc,
4141 Prefix => Make_Identifier (Loc, Name_uChain),
4142 Attribute_Name => Name_Unrestricted_Access),
4144 -- Destination chain
4146 New_Occurrence_Of
4147 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
4149 -- New master
4151 New_Occurrence_Of
4152 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
4153 end Move_Activation_Chain;
4155 -- Start of processing for Expand_N_Extended_Return_Statement
4157 begin
4158 -- Given that functionality of interface thunks is simple (just displace
4159 -- the pointer to the object) they are always handled by means of
4160 -- simple return statements.
4162 pragma Assert (not Is_Thunk (Current_Scope));
4164 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4165 Exp := Expression (Ret_Obj_Decl);
4166 else
4167 Exp := Empty;
4168 end if;
4170 HSS := Handled_Statement_Sequence (N);
4172 -- If the returned object needs finalization actions, the function must
4173 -- perform the appropriate cleanup should it fail to return. The state
4174 -- of the function itself is tracked through a flag which is coupled
4175 -- with the scope finalizer. There is one flag per each return object
4176 -- in case of multiple returns.
4178 if Is_Build_In_Place
4179 and then Needs_Finalization (Etype (Ret_Obj_Id))
4180 then
4181 declare
4182 Flag_Decl : Node_Id;
4183 Flag_Id : Entity_Id;
4184 Func_Bod : Node_Id;
4186 begin
4187 -- Recover the function body
4189 Func_Bod := Unit_Declaration_Node (Par_Func);
4191 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4192 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4193 end if;
4195 -- Create a flag to track the function state
4197 Flag_Id := Make_Temporary (Loc, 'F');
4198 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4200 -- Insert the flag at the beginning of the function declarations,
4201 -- generate:
4202 -- Fnn : Boolean := False;
4204 Flag_Decl :=
4205 Make_Object_Declaration (Loc,
4206 Defining_Identifier => Flag_Id,
4207 Object_Definition =>
4208 New_Occurrence_Of (Standard_Boolean, Loc),
4209 Expression =>
4210 New_Occurrence_Of (Standard_False, Loc));
4212 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4213 Analyze (Flag_Decl);
4214 end;
4215 end if;
4217 -- Build a simple_return_statement that returns the return object when
4218 -- there is a statement sequence, or no expression, or the result will
4219 -- be built in place. Note however that we currently do this for all
4220 -- composite cases, even though nonlimited composite results are not yet
4221 -- built in place (though we plan to do so eventually).
4223 if Present (HSS)
4224 or else Is_Composite_Type (Result_Subt)
4225 or else No (Exp)
4226 then
4227 if No (HSS) then
4228 Stmts := New_List;
4230 -- If the extended return has a handled statement sequence, then wrap
4231 -- it in a block and use the block as the first statement.
4233 else
4234 Stmts := New_List (
4235 Make_Block_Statement (Loc,
4236 Declarations => New_List,
4237 Handled_Statement_Sequence => HSS));
4238 end if;
4240 -- If the result type contains tasks, we call Move_Activation_Chain.
4241 -- Later, the cleanup code will call Complete_Master, which will
4242 -- terminate any unactivated tasks belonging to the return statement
4243 -- master. But Move_Activation_Chain updates their master to be that
4244 -- of the caller, so they will not be terminated unless the return
4245 -- statement completes unsuccessfully due to exception, abort, goto,
4246 -- or exit. As a formality, we test whether the function requires the
4247 -- result to be built in place, though that's necessarily true for
4248 -- the case of result types with task parts.
4250 if Is_Build_In_Place
4251 and then Has_Task (Result_Subt)
4252 then
4253 -- The return expression is an aggregate for a complex type which
4254 -- contains tasks. This particular case is left unexpanded since
4255 -- the regular expansion would insert all temporaries and
4256 -- initialization code in the wrong block.
4258 if Nkind (Exp) = N_Aggregate then
4259 Expand_N_Aggregate (Exp);
4260 end if;
4262 -- Do not move the activation chain if the return object does not
4263 -- contain tasks.
4265 if Has_Task (Etype (Ret_Obj_Id)) then
4266 Append_To (Stmts, Move_Activation_Chain);
4267 end if;
4268 end if;
4270 -- Update the state of the function right before the object is
4271 -- returned.
4273 if Is_Build_In_Place
4274 and then Needs_Finalization (Etype (Ret_Obj_Id))
4275 then
4276 declare
4277 Flag_Id : constant Entity_Id :=
4278 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4280 begin
4281 -- Generate:
4282 -- Fnn := True;
4284 Append_To (Stmts,
4285 Make_Assignment_Statement (Loc,
4286 Name => New_Occurrence_Of (Flag_Id, Loc),
4287 Expression => New_Occurrence_Of (Standard_True, Loc)));
4288 end;
4289 end if;
4291 -- Build a simple_return_statement that returns the return object
4293 Return_Stmt :=
4294 Make_Simple_Return_Statement (Loc,
4295 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4296 Append_To (Stmts, Return_Stmt);
4298 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4299 end if;
4301 -- Case where we build a return statement block
4303 if Present (HSS) then
4304 Result :=
4305 Make_Block_Statement (Loc,
4306 Declarations => Return_Object_Declarations (N),
4307 Handled_Statement_Sequence => HSS);
4309 -- We set the entity of the new block statement to be that of the
4310 -- return statement. This is necessary so that various fields, such
4311 -- as Finalization_Chain_Entity carry over from the return statement
4312 -- to the block. Note that this block is unusual, in that its entity
4313 -- is an E_Return_Statement rather than an E_Block.
4315 Set_Identifier
4316 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4318 -- If the object decl was already rewritten as a renaming, then we
4319 -- don't want to do the object allocation and transformation of of
4320 -- the return object declaration to a renaming. This case occurs
4321 -- when the return object is initialized by a call to another
4322 -- build-in-place function, and that function is responsible for
4323 -- the allocation of the return object.
4325 if Is_Build_In_Place
4326 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4327 then
4328 pragma Assert
4329 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4330 and then Is_Build_In_Place_Function_Call
4331 (Expression (Original_Node (Ret_Obj_Decl))));
4333 -- Return the build-in-place result by reference
4335 Set_By_Ref (Return_Stmt);
4337 elsif Is_Build_In_Place then
4339 -- Locate the implicit access parameter associated with the
4340 -- caller-supplied return object and convert the return
4341 -- statement's return object declaration to a renaming of a
4342 -- dereference of the access parameter. If the return object's
4343 -- declaration includes an expression that has not already been
4344 -- expanded as separate assignments, then add an assignment
4345 -- statement to ensure the return object gets initialized.
4347 -- declare
4348 -- Result : T [:= <expression>];
4349 -- begin
4350 -- ...
4352 -- is converted to
4354 -- declare
4355 -- Result : T renames FuncRA.all;
4356 -- [Result := <expression;]
4357 -- begin
4358 -- ...
4360 declare
4361 Return_Obj_Id : constant Entity_Id :=
4362 Defining_Identifier (Ret_Obj_Decl);
4363 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4364 Return_Obj_Expr : constant Node_Id :=
4365 Expression (Ret_Obj_Decl);
4366 Constr_Result : constant Boolean :=
4367 Is_Constrained (Result_Subt);
4368 Obj_Alloc_Formal : Entity_Id;
4369 Object_Access : Entity_Id;
4370 Obj_Acc_Deref : Node_Id;
4371 Init_Assignment : Node_Id := Empty;
4373 begin
4374 -- Build-in-place results must be returned by reference
4376 Set_By_Ref (Return_Stmt);
4378 -- Retrieve the implicit access parameter passed by the caller
4380 Object_Access :=
4381 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
4383 -- If the return object's declaration includes an expression
4384 -- and the declaration isn't marked as No_Initialization, then
4385 -- we need to generate an assignment to the object and insert
4386 -- it after the declaration before rewriting it as a renaming
4387 -- (otherwise we'll lose the initialization). The case where
4388 -- the result type is an interface (or class-wide interface)
4389 -- is also excluded because the context of the function call
4390 -- must be unconstrained, so the initialization will always
4391 -- be done as part of an allocator evaluation (storage pool
4392 -- or secondary stack), never to a constrained target object
4393 -- passed in by the caller. Besides the assignment being
4394 -- unneeded in this case, it avoids problems with trying to
4395 -- generate a dispatching assignment when the return expression
4396 -- is a nonlimited descendant of a limited interface (the
4397 -- interface has no assignment operation).
4399 if Present (Return_Obj_Expr)
4400 and then not No_Initialization (Ret_Obj_Decl)
4401 and then not Is_Interface (Return_Obj_Typ)
4402 then
4403 Init_Assignment :=
4404 Make_Assignment_Statement (Loc,
4405 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
4406 Expression => Relocate_Node (Return_Obj_Expr));
4408 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4409 Set_Assignment_OK (Name (Init_Assignment));
4410 Set_No_Ctrl_Actions (Init_Assignment);
4412 Set_Parent (Name (Init_Assignment), Init_Assignment);
4413 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4415 Set_Expression (Ret_Obj_Decl, Empty);
4417 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4418 and then not Is_Class_Wide_Type
4419 (Etype (Expression (Init_Assignment)))
4420 then
4421 Rewrite (Expression (Init_Assignment),
4422 Make_Type_Conversion (Loc,
4423 Subtype_Mark =>
4424 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
4425 Expression =>
4426 Relocate_Node (Expression (Init_Assignment))));
4427 end if;
4429 -- In the case of functions where the calling context can
4430 -- determine the form of allocation needed, initialization
4431 -- is done with each part of the if statement that handles
4432 -- the different forms of allocation (this is true for
4433 -- unconstrained and tagged result subtypes).
4435 if Constr_Result
4436 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4437 then
4438 Insert_After (Ret_Obj_Decl, Init_Assignment);
4439 end if;
4440 end if;
4442 -- When the function's subtype is unconstrained, a run-time
4443 -- test is needed to determine the form of allocation to use
4444 -- for the return object. The function has an implicit formal
4445 -- parameter indicating this. If the BIP_Alloc_Form formal has
4446 -- the value one, then the caller has passed access to an
4447 -- existing object for use as the return object. If the value
4448 -- is two, then the return object must be allocated on the
4449 -- secondary stack. Otherwise, the object must be allocated in
4450 -- a storage pool (currently only supported for the global
4451 -- heap, user-defined storage pools TBD ???). We generate an
4452 -- if statement to test the implicit allocation formal and
4453 -- initialize a local access value appropriately, creating
4454 -- allocators in the secondary stack and global heap cases.
4455 -- The special formal also exists and must be tested when the
4456 -- function has a tagged result, even when the result subtype
4457 -- is constrained, because in general such functions can be
4458 -- called in dispatching contexts and must be handled similarly
4459 -- to functions with a class-wide result.
4461 if not Constr_Result
4462 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4463 then
4464 Obj_Alloc_Formal :=
4465 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
4467 declare
4468 Pool_Id : constant Entity_Id :=
4469 Make_Temporary (Loc, 'P');
4470 Alloc_Obj_Id : Entity_Id;
4471 Alloc_Obj_Decl : Node_Id;
4472 Alloc_If_Stmt : Node_Id;
4473 Heap_Allocator : Node_Id;
4474 Pool_Decl : Node_Id;
4475 Pool_Allocator : Node_Id;
4476 Ptr_Type_Decl : Node_Id;
4477 Ref_Type : Entity_Id;
4478 SS_Allocator : Node_Id;
4480 begin
4481 -- Reuse the itype created for the function's implicit
4482 -- access formal. This avoids the need to create a new
4483 -- access type here, plus it allows assigning the access
4484 -- formal directly without applying a conversion.
4486 -- Ref_Type := Etype (Object_Access);
4488 -- Create an access type designating the function's
4489 -- result subtype.
4491 Ref_Type := Make_Temporary (Loc, 'A');
4493 Ptr_Type_Decl :=
4494 Make_Full_Type_Declaration (Loc,
4495 Defining_Identifier => Ref_Type,
4496 Type_Definition =>
4497 Make_Access_To_Object_Definition (Loc,
4498 All_Present => True,
4499 Subtype_Indication =>
4500 New_Occurrence_Of (Return_Obj_Typ, Loc)));
4502 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4504 -- Create an access object that will be initialized to an
4505 -- access value denoting the return object, either coming
4506 -- from an implicit access value passed in by the caller
4507 -- or from the result of an allocator.
4509 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4510 Set_Etype (Alloc_Obj_Id, Ref_Type);
4512 Alloc_Obj_Decl :=
4513 Make_Object_Declaration (Loc,
4514 Defining_Identifier => Alloc_Obj_Id,
4515 Object_Definition =>
4516 New_Occurrence_Of (Ref_Type, Loc));
4518 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4520 -- Create allocators for both the secondary stack and
4521 -- global heap. If there's an initialization expression,
4522 -- then create these as initialized allocators.
4524 if Present (Return_Obj_Expr)
4525 and then not No_Initialization (Ret_Obj_Decl)
4526 then
4527 -- Always use the type of the expression for the
4528 -- qualified expression, rather than the result type.
4529 -- In general we cannot always use the result type
4530 -- for the allocator, because the expression might be
4531 -- of a specific type, such as in the case of an
4532 -- aggregate or even a nonlimited object when the
4533 -- result type is a limited class-wide interface type.
4535 Heap_Allocator :=
4536 Make_Allocator (Loc,
4537 Expression =>
4538 Make_Qualified_Expression (Loc,
4539 Subtype_Mark =>
4540 New_Occurrence_Of
4541 (Etype (Return_Obj_Expr), Loc),
4542 Expression =>
4543 New_Copy_Tree (Return_Obj_Expr)));
4545 else
4546 -- If the function returns a class-wide type we cannot
4547 -- use the return type for the allocator. Instead we
4548 -- use the type of the expression, which must be an
4549 -- aggregate of a definite type.
4551 if Is_Class_Wide_Type (Return_Obj_Typ) then
4552 Heap_Allocator :=
4553 Make_Allocator (Loc,
4554 Expression =>
4555 New_Occurrence_Of
4556 (Etype (Return_Obj_Expr), Loc));
4557 else
4558 Heap_Allocator :=
4559 Make_Allocator (Loc,
4560 Expression =>
4561 New_Occurrence_Of (Return_Obj_Typ, Loc));
4562 end if;
4564 -- If the object requires default initialization then
4565 -- that will happen later following the elaboration of
4566 -- the object renaming. If we don't turn it off here
4567 -- then the object will be default initialized twice.
4569 Set_No_Initialization (Heap_Allocator);
4570 end if;
4572 -- The Pool_Allocator is just like the Heap_Allocator,
4573 -- except we set Storage_Pool and Procedure_To_Call so
4574 -- it will use the user-defined storage pool.
4576 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4578 -- Do not generate the renaming of the build-in-place
4579 -- pool parameter on .NET/JVM/ZFP because the parameter
4580 -- is not created in the first place.
4582 if VM_Target = No_VM
4583 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
4584 then
4585 Pool_Decl :=
4586 Make_Object_Renaming_Declaration (Loc,
4587 Defining_Identifier => Pool_Id,
4588 Subtype_Mark =>
4589 New_Occurrence_Of
4590 (RTE (RE_Root_Storage_Pool), Loc),
4591 Name =>
4592 Make_Explicit_Dereference (Loc,
4593 New_Occurrence_Of
4594 (Build_In_Place_Formal
4595 (Par_Func, BIP_Storage_Pool), Loc)));
4596 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4597 Set_Procedure_To_Call
4598 (Pool_Allocator, RTE (RE_Allocate_Any));
4599 else
4600 Pool_Decl := Make_Null_Statement (Loc);
4601 end if;
4603 -- If the No_Allocators restriction is active, then only
4604 -- an allocator for secondary stack allocation is needed.
4605 -- It's OK for such allocators to have Comes_From_Source
4606 -- set to False, because gigi knows not to flag them as
4607 -- being a violation of No_Implicit_Heap_Allocations.
4609 if Restriction_Active (No_Allocators) then
4610 SS_Allocator := Heap_Allocator;
4611 Heap_Allocator := Make_Null (Loc);
4612 Pool_Allocator := Make_Null (Loc);
4614 -- Otherwise the heap and pool allocators may be needed,
4615 -- so we make another allocator for secondary stack
4616 -- allocation.
4618 else
4619 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4621 -- The heap and pool allocators are marked as
4622 -- Comes_From_Source since they correspond to an
4623 -- explicit user-written allocator (that is, it will
4624 -- only be executed on behalf of callers that call the
4625 -- function as initialization for such an allocator).
4626 -- Prevents errors when No_Implicit_Heap_Allocations
4627 -- is in force.
4629 Set_Comes_From_Source (Heap_Allocator, True);
4630 Set_Comes_From_Source (Pool_Allocator, True);
4631 end if;
4633 -- The allocator is returned on the secondary stack. We
4634 -- don't do this on VM targets, since the SS is not used.
4636 if VM_Target = No_VM then
4637 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4638 Set_Procedure_To_Call
4639 (SS_Allocator, RTE (RE_SS_Allocate));
4641 -- The allocator is returned on the secondary stack,
4642 -- so indicate that the function return, as well as
4643 -- the block that encloses the allocator, must not
4644 -- release it. The flags must be set now because
4645 -- the decision to use the secondary stack is done
4646 -- very late in the course of expanding the return
4647 -- statement, past the point where these flags are
4648 -- normally set.
4650 Set_Sec_Stack_Needed_For_Return (Par_Func);
4651 Set_Sec_Stack_Needed_For_Return
4652 (Return_Statement_Entity (N));
4653 Set_Uses_Sec_Stack (Par_Func);
4654 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4655 end if;
4657 -- Create an if statement to test the BIP_Alloc_Form
4658 -- formal and initialize the access object to either the
4659 -- BIP_Object_Access formal (BIP_Alloc_Form =
4660 -- Caller_Allocation), the result of allocating the
4661 -- object in the secondary stack (BIP_Alloc_Form =
4662 -- Secondary_Stack), or else an allocator to create the
4663 -- return object in the heap or user-defined pool
4664 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4666 -- ??? An unchecked type conversion must be made in the
4667 -- case of assigning the access object formal to the
4668 -- local access object, because a normal conversion would
4669 -- be illegal in some cases (such as converting access-
4670 -- to-unconstrained to access-to-constrained), but the
4671 -- the unchecked conversion will presumably fail to work
4672 -- right in just such cases. It's not clear at all how to
4673 -- handle this. ???
4675 Alloc_If_Stmt :=
4676 Make_If_Statement (Loc,
4677 Condition =>
4678 Make_Op_Eq (Loc,
4679 Left_Opnd =>
4680 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4681 Right_Opnd =>
4682 Make_Integer_Literal (Loc,
4683 UI_From_Int (BIP_Allocation_Form'Pos
4684 (Caller_Allocation)))),
4686 Then_Statements => New_List (
4687 Make_Assignment_Statement (Loc,
4688 Name =>
4689 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4690 Expression =>
4691 Make_Unchecked_Type_Conversion (Loc,
4692 Subtype_Mark =>
4693 New_Occurrence_Of (Ref_Type, Loc),
4694 Expression =>
4695 New_Occurrence_Of (Object_Access, Loc)))),
4697 Elsif_Parts => New_List (
4698 Make_Elsif_Part (Loc,
4699 Condition =>
4700 Make_Op_Eq (Loc,
4701 Left_Opnd =>
4702 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4703 Right_Opnd =>
4704 Make_Integer_Literal (Loc,
4705 UI_From_Int (BIP_Allocation_Form'Pos
4706 (Secondary_Stack)))),
4708 Then_Statements => New_List (
4709 Make_Assignment_Statement (Loc,
4710 Name =>
4711 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4712 Expression => SS_Allocator))),
4714 Make_Elsif_Part (Loc,
4715 Condition =>
4716 Make_Op_Eq (Loc,
4717 Left_Opnd =>
4718 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4719 Right_Opnd =>
4720 Make_Integer_Literal (Loc,
4721 UI_From_Int (BIP_Allocation_Form'Pos
4722 (Global_Heap)))),
4724 Then_Statements => New_List (
4725 Build_Heap_Allocator
4726 (Temp_Id => Alloc_Obj_Id,
4727 Temp_Typ => Ref_Type,
4728 Func_Id => Par_Func,
4729 Ret_Typ => Return_Obj_Typ,
4730 Alloc_Expr => Heap_Allocator)))),
4732 Else_Statements => New_List (
4733 Pool_Decl,
4734 Build_Heap_Allocator
4735 (Temp_Id => Alloc_Obj_Id,
4736 Temp_Typ => Ref_Type,
4737 Func_Id => Par_Func,
4738 Ret_Typ => Return_Obj_Typ,
4739 Alloc_Expr => Pool_Allocator)));
4741 -- If a separate initialization assignment was created
4742 -- earlier, append that following the assignment of the
4743 -- implicit access formal to the access object, to ensure
4744 -- that the return object is initialized in that case. In
4745 -- this situation, the target of the assignment must be
4746 -- rewritten to denote a dereference of the access to the
4747 -- return object passed in by the caller.
4749 if Present (Init_Assignment) then
4750 Rewrite (Name (Init_Assignment),
4751 Make_Explicit_Dereference (Loc,
4752 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
4754 Set_Etype
4755 (Name (Init_Assignment), Etype (Return_Obj_Id));
4757 Append_To
4758 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
4759 end if;
4761 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
4763 -- Remember the local access object for use in the
4764 -- dereference of the renaming created below.
4766 Object_Access := Alloc_Obj_Id;
4767 end;
4768 end if;
4770 -- Replace the return object declaration with a renaming of a
4771 -- dereference of the access value designating the return
4772 -- object.
4774 Obj_Acc_Deref :=
4775 Make_Explicit_Dereference (Loc,
4776 Prefix => New_Occurrence_Of (Object_Access, Loc));
4778 Rewrite (Ret_Obj_Decl,
4779 Make_Object_Renaming_Declaration (Loc,
4780 Defining_Identifier => Return_Obj_Id,
4781 Access_Definition => Empty,
4782 Subtype_Mark =>
4783 New_Occurrence_Of (Return_Obj_Typ, Loc),
4784 Name => Obj_Acc_Deref));
4786 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4787 end;
4788 end if;
4790 -- Case where we do not build a block
4792 else
4793 -- We're about to drop Return_Object_Declarations on the floor, so
4794 -- we need to insert it, in case it got expanded into useful code.
4795 -- Remove side effects from expression, which may be duplicated in
4796 -- subsequent checks (see Expand_Simple_Function_Return).
4798 Insert_List_Before (N, Return_Object_Declarations (N));
4799 Remove_Side_Effects (Exp);
4801 -- Build simple_return_statement that returns the expression directly
4803 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
4804 Result := Return_Stmt;
4805 end if;
4807 -- Set the flag to prevent infinite recursion
4809 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
4811 Rewrite (N, Result);
4812 Analyze (N);
4813 end Expand_N_Extended_Return_Statement;
4815 ----------------------------
4816 -- Expand_N_Function_Call --
4817 ----------------------------
4819 procedure Expand_N_Function_Call (N : Node_Id) is
4820 begin
4821 Expand_Call (N);
4822 end Expand_N_Function_Call;
4824 ---------------------------------------
4825 -- Expand_N_Procedure_Call_Statement --
4826 ---------------------------------------
4828 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4829 begin
4830 Expand_Call (N);
4831 end Expand_N_Procedure_Call_Statement;
4833 --------------------------------------
4834 -- Expand_N_Simple_Return_Statement --
4835 --------------------------------------
4837 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4838 begin
4839 -- Defend against previous errors (i.e. the return statement calls a
4840 -- function that is not available in configurable runtime).
4842 if Present (Expression (N))
4843 and then Nkind (Expression (N)) = N_Empty
4844 then
4845 Check_Error_Detected;
4846 return;
4847 end if;
4849 -- Distinguish the function and non-function cases:
4851 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4853 when E_Function |
4854 E_Generic_Function =>
4855 Expand_Simple_Function_Return (N);
4857 when E_Procedure |
4858 E_Generic_Procedure |
4859 E_Entry |
4860 E_Entry_Family |
4861 E_Return_Statement =>
4862 Expand_Non_Function_Return (N);
4864 when others =>
4865 raise Program_Error;
4866 end case;
4868 exception
4869 when RE_Not_Available =>
4870 return;
4871 end Expand_N_Simple_Return_Statement;
4873 ------------------------------
4874 -- Expand_N_Subprogram_Body --
4875 ------------------------------
4877 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4878 -- by the back-end.
4880 -- Add dummy push/pop label nodes at start and end to clear any local
4881 -- exception indications if local-exception-to-goto optimization is active.
4883 -- Add return statement if last statement in body is not a return statement
4884 -- (this makes things easier on Gigi which does not want to have to handle
4885 -- a missing return).
4887 -- Add call to Activate_Tasks if body is a task activator
4889 -- Deal with possible detection of infinite recursion
4891 -- Eliminate body completely if convention stubbed
4893 -- Encode entity names within body, since we will not need to reference
4894 -- these entities any longer in the front end.
4896 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4898 -- Reset Pure indication if any parameter has root type System.Address
4899 -- or has any parameters of limited types, where limited means that the
4900 -- run-time view is limited (i.e. the full type is limited).
4902 -- Wrap thread body
4904 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4905 Loc : constant Source_Ptr := Sloc (N);
4906 H : constant Node_Id := Handled_Statement_Sequence (N);
4907 Body_Id : Entity_Id;
4908 Except_H : Node_Id;
4909 L : List_Id;
4910 Spec_Id : Entity_Id;
4912 procedure Add_Return (S : List_Id);
4913 -- Append a return statement to the statement sequence S if the last
4914 -- statement is not already a return or a goto statement. Note that
4915 -- the latter test is not critical, it does not matter if we add a few
4916 -- extra returns, since they get eliminated anyway later on.
4918 ----------------
4919 -- Add_Return --
4920 ----------------
4922 procedure Add_Return (S : List_Id) is
4923 Last_Stm : Node_Id;
4924 Loc : Source_Ptr;
4926 begin
4927 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4928 -- not relevant in this context since they are not executable.
4930 Last_Stm := Last (S);
4931 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4932 Prev (Last_Stm);
4933 end loop;
4935 -- Now insert return unless last statement is a transfer
4937 if not Is_Transfer (Last_Stm) then
4939 -- The source location for the return is the end label of the
4940 -- procedure if present. Otherwise use the sloc of the last
4941 -- statement in the list. If the list comes from a generated
4942 -- exception handler and we are not debugging generated code,
4943 -- all the statements within the handler are made invisible
4944 -- to the debugger.
4946 if Nkind (Parent (S)) = N_Exception_Handler
4947 and then not Comes_From_Source (Parent (S))
4948 then
4949 Loc := Sloc (Last_Stm);
4950 elsif Present (End_Label (H)) then
4951 Loc := Sloc (End_Label (H));
4952 else
4953 Loc := Sloc (Last_Stm);
4954 end if;
4956 declare
4957 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
4959 begin
4960 -- Append return statement, and set analyzed manually. We can't
4961 -- call Analyze on this return since the scope is wrong.
4963 -- Note: it almost works to push the scope and then do the
4964 -- Analyze call, but something goes wrong in some weird cases
4965 -- and it is not worth worrying about ???
4967 -- The return statement is handled properly, and the call
4968 -- to the postcondition, inserted below, does not require
4969 -- information from the body either. However, that call is
4970 -- analyzed in the enclosing scope, and an elaboration check
4971 -- might improperly be added to it. A guard in Sem_Elab is
4972 -- needed to prevent that spurious check, see Check_Elab_Call.
4974 Append_To (S, Rtn);
4975 Set_Analyzed (Rtn);
4977 -- Call _Postconditions procedure if appropriate. We need to
4978 -- do this explicitly because we did not analyze the generated
4979 -- return statement above, so the call did not get inserted.
4981 if Ekind (Spec_Id) = E_Procedure
4982 and then Has_Postconditions (Spec_Id)
4983 then
4984 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
4985 Insert_Action (Rtn,
4986 Make_Procedure_Call_Statement (Loc,
4987 Name =>
4988 New_Occurrence_Of
4989 (Postcondition_Proc (Spec_Id), Loc)));
4990 end if;
4991 end;
4992 end if;
4993 end Add_Return;
4995 -- Start of processing for Expand_N_Subprogram_Body
4997 begin
4998 -- Set L to either the list of declarations if present, or to the list
4999 -- of statements if no declarations are present. This is used to insert
5000 -- new stuff at the start.
5002 if Is_Non_Empty_List (Declarations (N)) then
5003 L := Declarations (N);
5004 else
5005 L := Statements (H);
5006 end if;
5008 -- If local-exception-to-goto optimization active, insert dummy push
5009 -- statements at start, and dummy pop statements at end, but inhibit
5010 -- this if we have No_Exception_Handlers, since they are useless and
5011 -- intefere with analysis, e.g. by codepeer.
5013 if (Debug_Flag_Dot_G
5014 or else Restriction_Active (No_Exception_Propagation))
5015 and then not Restriction_Active (No_Exception_Handlers)
5016 and then not CodePeer_Mode
5017 and then Is_Non_Empty_List (L)
5018 then
5019 declare
5020 FS : constant Node_Id := First (L);
5021 FL : constant Source_Ptr := Sloc (FS);
5022 LS : Node_Id;
5023 LL : Source_Ptr;
5025 begin
5026 -- LS points to either last statement, if statements are present
5027 -- or to the last declaration if there are no statements present.
5028 -- It is the node after which the pop's are generated.
5030 if Is_Non_Empty_List (Statements (H)) then
5031 LS := Last (Statements (H));
5032 else
5033 LS := Last (L);
5034 end if;
5036 LL := Sloc (LS);
5038 Insert_List_Before_And_Analyze (FS, New_List (
5039 Make_Push_Constraint_Error_Label (FL),
5040 Make_Push_Program_Error_Label (FL),
5041 Make_Push_Storage_Error_Label (FL)));
5043 Insert_List_After_And_Analyze (LS, New_List (
5044 Make_Pop_Constraint_Error_Label (LL),
5045 Make_Pop_Program_Error_Label (LL),
5046 Make_Pop_Storage_Error_Label (LL)));
5047 end;
5048 end if;
5050 -- Find entity for subprogram
5052 Body_Id := Defining_Entity (N);
5054 if Present (Corresponding_Spec (N)) then
5055 Spec_Id := Corresponding_Spec (N);
5056 else
5057 Spec_Id := Body_Id;
5058 end if;
5060 -- Need poll on entry to subprogram if polling enabled. We only do this
5061 -- for non-empty subprograms, since it does not seem necessary to poll
5062 -- for a dummy null subprogram.
5064 if Is_Non_Empty_List (L) then
5066 -- Do not add a polling call if the subprogram is to be inlined by
5067 -- the back-end, to avoid repeated calls with multiple inlinings.
5069 if Is_Inlined (Spec_Id)
5070 and then Front_End_Inlining
5071 and then Optimization_Level > 1
5072 then
5073 null;
5074 else
5075 Generate_Poll_Call (First (L));
5076 end if;
5077 end if;
5079 -- If this is a Pure function which has any parameters whose root type
5080 -- is System.Address, reset the Pure indication, since it will likely
5081 -- cause incorrect code to be generated as the parameter is probably
5082 -- a pointer, and the fact that the same pointer is passed does not mean
5083 -- that the same value is being referenced.
5085 -- Note that if the programmer gave an explicit Pure_Function pragma,
5086 -- then we believe the programmer, and leave the subprogram Pure.
5088 -- This code should probably be at the freeze point, so that it happens
5089 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5090 -- semantic tree has Is_Pure set properly ???
5092 if Is_Pure (Spec_Id)
5093 and then Is_Subprogram (Spec_Id)
5094 and then not Has_Pragma_Pure_Function (Spec_Id)
5095 then
5096 declare
5097 F : Entity_Id;
5099 begin
5100 F := First_Formal (Spec_Id);
5101 while Present (F) loop
5102 if Is_Descendent_Of_Address (Etype (F))
5104 -- Note that this test is being made in the body of the
5105 -- subprogram, not the spec, so we are testing the full
5106 -- type for being limited here, as required.
5108 or else Is_Limited_Type (Etype (F))
5109 then
5110 Set_Is_Pure (Spec_Id, False);
5112 if Spec_Id /= Body_Id then
5113 Set_Is_Pure (Body_Id, False);
5114 end if;
5116 exit;
5117 end if;
5119 Next_Formal (F);
5120 end loop;
5121 end;
5122 end if;
5124 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5126 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5127 declare
5128 F : Entity_Id;
5129 A : Node_Id;
5131 begin
5132 -- Loop through formals
5134 F := First_Formal (Spec_Id);
5135 while Present (F) loop
5136 if Is_Scalar_Type (Etype (F))
5137 and then Ekind (F) = E_Out_Parameter
5138 then
5139 Check_Restriction (No_Default_Initialization, F);
5141 -- Insert the initialization. We turn off validity checks
5142 -- for this assignment, since we do not want any check on
5143 -- the initial value itself (which may well be invalid).
5144 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5146 A := Make_Assignment_Statement (Loc,
5147 Name => New_Occurrence_Of (F, Loc),
5148 Expression => Get_Simple_Init_Val (Etype (F), N));
5149 Set_Suppress_Assignment_Checks (A);
5151 Insert_Before_And_Analyze (First (L),
5152 A, Suppress => Validity_Check);
5153 end if;
5155 Next_Formal (F);
5156 end loop;
5157 end;
5158 end if;
5160 -- Clear out statement list for stubbed procedure
5162 if Present (Corresponding_Spec (N)) then
5163 Set_Elaboration_Flag (N, Spec_Id);
5165 if Convention (Spec_Id) = Convention_Stubbed
5166 or else Is_Eliminated (Spec_Id)
5167 then
5168 Set_Declarations (N, Empty_List);
5169 Set_Handled_Statement_Sequence (N,
5170 Make_Handled_Sequence_Of_Statements (Loc,
5171 Statements => New_List (Make_Null_Statement (Loc))));
5172 return;
5173 end if;
5174 end if;
5176 -- Create a set of discriminals for the next protected subprogram body
5178 if Is_List_Member (N)
5179 and then Present (Parent (List_Containing (N)))
5180 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5181 and then Present (Next_Protected_Operation (N))
5182 then
5183 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5184 end if;
5186 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5187 -- subprograms with no specs are not frozen.
5189 declare
5190 Typ : constant Entity_Id := Etype (Spec_Id);
5191 Utyp : constant Entity_Id := Underlying_Type (Typ);
5193 begin
5194 if not Acts_As_Spec (N)
5195 and then Nkind (Parent (Parent (Spec_Id))) /=
5196 N_Subprogram_Body_Stub
5197 then
5198 null;
5200 elsif Is_Limited_View (Typ) then
5201 Set_Returns_By_Ref (Spec_Id);
5203 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5204 Set_Returns_By_Ref (Spec_Id);
5205 end if;
5206 end;
5208 -- For a procedure, we add a return for all possible syntactic ends of
5209 -- the subprogram.
5211 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5212 Add_Return (Statements (H));
5214 if Present (Exception_Handlers (H)) then
5215 Except_H := First_Non_Pragma (Exception_Handlers (H));
5216 while Present (Except_H) loop
5217 Add_Return (Statements (Except_H));
5218 Next_Non_Pragma (Except_H);
5219 end loop;
5220 end if;
5222 -- For a function, we must deal with the case where there is at least
5223 -- one missing return. What we do is to wrap the entire body of the
5224 -- function in a block:
5226 -- begin
5227 -- ...
5228 -- end;
5230 -- becomes
5232 -- begin
5233 -- begin
5234 -- ...
5235 -- end;
5237 -- raise Program_Error;
5238 -- end;
5240 -- This approach is necessary because the raise must be signalled to the
5241 -- caller, not handled by any local handler (RM 6.4(11)).
5243 -- Note: we do not need to analyze the constructed sequence here, since
5244 -- it has no handler, and an attempt to analyze the handled statement
5245 -- sequence twice is risky in various ways (e.g. the issue of expanding
5246 -- cleanup actions twice).
5248 elsif Has_Missing_Return (Spec_Id) then
5249 declare
5250 Hloc : constant Source_Ptr := Sloc (H);
5251 Blok : constant Node_Id :=
5252 Make_Block_Statement (Hloc,
5253 Handled_Statement_Sequence => H);
5254 Rais : constant Node_Id :=
5255 Make_Raise_Program_Error (Hloc,
5256 Reason => PE_Missing_Return);
5258 begin
5259 Set_Handled_Statement_Sequence (N,
5260 Make_Handled_Sequence_Of_Statements (Hloc,
5261 Statements => New_List (Blok, Rais)));
5263 Push_Scope (Spec_Id);
5264 Analyze (Blok);
5265 Analyze (Rais);
5266 Pop_Scope;
5267 end;
5268 end if;
5270 -- If subprogram contains a parameterless recursive call, then we may
5271 -- have an infinite recursion, so see if we can generate code to check
5272 -- for this possibility if storage checks are not suppressed.
5274 if Ekind (Spec_Id) = E_Procedure
5275 and then Has_Recursive_Call (Spec_Id)
5276 and then not Storage_Checks_Suppressed (Spec_Id)
5277 then
5278 Detect_Infinite_Recursion (N, Spec_Id);
5279 end if;
5281 -- Set to encode entity names in package body before gigi is called
5283 Qualify_Entity_Names (N);
5284 end Expand_N_Subprogram_Body;
5286 -----------------------------------
5287 -- Expand_N_Subprogram_Body_Stub --
5288 -----------------------------------
5290 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5291 begin
5292 if Present (Corresponding_Body (N)) then
5293 Expand_N_Subprogram_Body (
5294 Unit_Declaration_Node (Corresponding_Body (N)));
5295 end if;
5296 end Expand_N_Subprogram_Body_Stub;
5298 -------------------------------------
5299 -- Expand_N_Subprogram_Declaration --
5300 -------------------------------------
5302 -- If the declaration appears within a protected body, it is a private
5303 -- operation of the protected type. We must create the corresponding
5304 -- protected subprogram an associated formals. For a normal protected
5305 -- operation, this is done when expanding the protected type declaration.
5307 -- If the declaration is for a null procedure, emit null body
5309 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5310 Loc : constant Source_Ptr := Sloc (N);
5311 Subp : constant Entity_Id := Defining_Entity (N);
5312 Scop : constant Entity_Id := Scope (Subp);
5313 Prot_Decl : Node_Id;
5314 Prot_Bod : Node_Id;
5315 Prot_Id : Entity_Id;
5317 begin
5318 -- In SPARK, subprogram declarations are only allowed in package
5319 -- specifications.
5321 if Nkind (Parent (N)) /= N_Package_Specification then
5322 if Nkind (Parent (N)) = N_Compilation_Unit then
5323 Check_SPARK_05_Restriction
5324 ("subprogram declaration is not a library item", N);
5326 elsif Present (Next (N))
5327 and then Nkind (Next (N)) = N_Pragma
5328 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
5329 then
5330 -- In SPARK, subprogram declarations are also permitted in
5331 -- declarative parts when immediately followed by a corresponding
5332 -- pragma Import. We only check here that there is some pragma
5333 -- Import.
5335 null;
5336 else
5337 Check_SPARK_05_Restriction
5338 ("subprogram declaration is not allowed here", N);
5339 end if;
5340 end if;
5342 -- Deal with case of protected subprogram. Do not generate protected
5343 -- operation if operation is flagged as eliminated.
5345 if Is_List_Member (N)
5346 and then Present (Parent (List_Containing (N)))
5347 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5348 and then Is_Protected_Type (Scop)
5349 then
5350 if No (Protected_Body_Subprogram (Subp))
5351 and then not Is_Eliminated (Subp)
5352 then
5353 Prot_Decl :=
5354 Make_Subprogram_Declaration (Loc,
5355 Specification =>
5356 Build_Protected_Sub_Specification
5357 (N, Scop, Unprotected_Mode));
5359 -- The protected subprogram is declared outside of the protected
5360 -- body. Given that the body has frozen all entities so far, we
5361 -- analyze the subprogram and perform freezing actions explicitly.
5362 -- including the generation of an explicit freeze node, to ensure
5363 -- that gigi has the proper order of elaboration.
5364 -- If the body is a subunit, the insertion point is before the
5365 -- stub in the parent.
5367 Prot_Bod := Parent (List_Containing (N));
5369 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5370 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5371 end if;
5373 Insert_Before (Prot_Bod, Prot_Decl);
5374 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5375 Set_Has_Delayed_Freeze (Prot_Id);
5377 Push_Scope (Scope (Scop));
5378 Analyze (Prot_Decl);
5379 Freeze_Before (N, Prot_Id);
5380 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5382 -- Create protected operation as well. Even though the operation
5383 -- is only accessible within the body, it is possible to make it
5384 -- available outside of the protected object by using 'Access to
5385 -- provide a callback, so build protected version in all cases.
5387 Prot_Decl :=
5388 Make_Subprogram_Declaration (Loc,
5389 Specification =>
5390 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5391 Insert_Before (Prot_Bod, Prot_Decl);
5392 Analyze (Prot_Decl);
5394 Pop_Scope;
5395 end if;
5397 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5398 -- cases this is superfluous because calls to it will be automatically
5399 -- inlined, but we definitely need the body if preconditions for the
5400 -- procedure are present.
5402 elsif Nkind (Specification (N)) = N_Procedure_Specification
5403 and then Null_Present (Specification (N))
5404 then
5405 declare
5406 Bod : constant Node_Id := Body_To_Inline (N);
5408 begin
5409 Set_Has_Completion (Subp, False);
5410 Append_Freeze_Action (Subp, Bod);
5412 -- The body now contains raise statements, so calls to it will
5413 -- not be inlined.
5415 Set_Is_Inlined (Subp, False);
5416 end;
5417 end if;
5418 end Expand_N_Subprogram_Declaration;
5420 --------------------------------
5421 -- Expand_Non_Function_Return --
5422 --------------------------------
5424 procedure Expand_Non_Function_Return (N : Node_Id) is
5425 pragma Assert (No (Expression (N)));
5427 Loc : constant Source_Ptr := Sloc (N);
5428 Scope_Id : Entity_Id :=
5429 Return_Applies_To (Return_Statement_Entity (N));
5430 Kind : constant Entity_Kind := Ekind (Scope_Id);
5431 Call : Node_Id;
5432 Acc_Stat : Node_Id;
5433 Goto_Stat : Node_Id;
5434 Lab_Node : Node_Id;
5436 begin
5437 -- Call _Postconditions procedure if procedure with active
5438 -- postconditions. Here, we use the Postcondition_Proc attribute,
5439 -- which is needed for implicitly-generated returns. Functions
5440 -- never have implicitly-generated returns, and there's no
5441 -- room for Postcondition_Proc in E_Function, so we look up the
5442 -- identifier Name_uPostconditions for function returns (see
5443 -- Expand_Simple_Function_Return).
5445 if Ekind (Scope_Id) = E_Procedure
5446 and then Has_Postconditions (Scope_Id)
5447 then
5448 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
5449 Insert_Action (N,
5450 Make_Procedure_Call_Statement (Loc,
5451 Name => New_Occurrence_Of (Postcondition_Proc (Scope_Id), Loc)));
5452 end if;
5454 -- If it is a return from a procedure do no extra steps
5456 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5457 return;
5459 -- If it is a nested return within an extended one, replace it with a
5460 -- return of the previously declared return object.
5462 elsif Kind = E_Return_Statement then
5463 Rewrite (N,
5464 Make_Simple_Return_Statement (Loc,
5465 Expression =>
5466 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5467 Set_Comes_From_Extended_Return_Statement (N);
5468 Set_Return_Statement_Entity (N, Scope_Id);
5469 Expand_Simple_Function_Return (N);
5470 return;
5471 end if;
5473 pragma Assert (Is_Entry (Scope_Id));
5475 -- Look at the enclosing block to see whether the return is from an
5476 -- accept statement or an entry body.
5478 for J in reverse 0 .. Scope_Stack.Last loop
5479 Scope_Id := Scope_Stack.Table (J).Entity;
5480 exit when Is_Concurrent_Type (Scope_Id);
5481 end loop;
5483 -- If it is a return from accept statement it is expanded as call to
5484 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5486 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5487 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5489 if Is_Task_Type (Scope_Id) then
5491 Call :=
5492 Make_Procedure_Call_Statement (Loc,
5493 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5494 Insert_Before (N, Call);
5495 -- why not insert actions here???
5496 Analyze (Call);
5498 Acc_Stat := Parent (N);
5499 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5500 Acc_Stat := Parent (Acc_Stat);
5501 end loop;
5503 Lab_Node := Last (Statements
5504 (Handled_Statement_Sequence (Acc_Stat)));
5506 Goto_Stat := Make_Goto_Statement (Loc,
5507 Name => New_Occurrence_Of
5508 (Entity (Identifier (Lab_Node)), Loc));
5510 Set_Analyzed (Goto_Stat);
5512 Rewrite (N, Goto_Stat);
5513 Analyze (N);
5515 -- If it is a return from an entry body, put a Complete_Entry_Body call
5516 -- in front of the return.
5518 elsif Is_Protected_Type (Scope_Id) then
5519 Call :=
5520 Make_Procedure_Call_Statement (Loc,
5521 Name =>
5522 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5523 Parameter_Associations => New_List (
5524 Make_Attribute_Reference (Loc,
5525 Prefix =>
5526 New_Occurrence_Of
5527 (Find_Protection_Object (Current_Scope), Loc),
5528 Attribute_Name => Name_Unchecked_Access)));
5530 Insert_Before (N, Call);
5531 Analyze (Call);
5532 end if;
5533 end Expand_Non_Function_Return;
5535 ---------------------------------------
5536 -- Expand_Protected_Object_Reference --
5537 ---------------------------------------
5539 function Expand_Protected_Object_Reference
5540 (N : Node_Id;
5541 Scop : Entity_Id) return Node_Id
5543 Loc : constant Source_Ptr := Sloc (N);
5544 Corr : Entity_Id;
5545 Rec : Node_Id;
5546 Param : Entity_Id;
5547 Proc : Entity_Id;
5549 begin
5550 Rec := Make_Identifier (Loc, Name_uObject);
5551 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5553 -- Find enclosing protected operation, and retrieve its first parameter,
5554 -- which denotes the enclosing protected object. If the enclosing
5555 -- operation is an entry, we are immediately within the protected body,
5556 -- and we can retrieve the object from the service entries procedure. A
5557 -- barrier function has the same signature as an entry. A barrier
5558 -- function is compiled within the protected object, but unlike
5559 -- protected operations its never needs locks, so that its protected
5560 -- body subprogram points to itself.
5562 Proc := Current_Scope;
5563 while Present (Proc)
5564 and then Scope (Proc) /= Scop
5565 loop
5566 Proc := Scope (Proc);
5567 end loop;
5569 Corr := Protected_Body_Subprogram (Proc);
5571 if No (Corr) then
5573 -- Previous error left expansion incomplete.
5574 -- Nothing to do on this call.
5576 return Empty;
5577 end if;
5579 Param :=
5580 Defining_Identifier
5581 (First (Parameter_Specifications (Parent (Corr))));
5583 if Is_Subprogram (Proc) and then Proc /= Corr then
5585 -- Protected function or procedure
5587 Set_Entity (Rec, Param);
5589 -- Rec is a reference to an entity which will not be in scope when
5590 -- the call is reanalyzed, and needs no further analysis.
5592 Set_Analyzed (Rec);
5594 else
5595 -- Entry or barrier function for entry body. The first parameter of
5596 -- the entry body procedure is pointer to the object. We create a
5597 -- local variable of the proper type, duplicating what is done to
5598 -- define _object later on.
5600 declare
5601 Decls : List_Id;
5602 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5604 begin
5605 Decls := New_List (
5606 Make_Full_Type_Declaration (Loc,
5607 Defining_Identifier => Obj_Ptr,
5608 Type_Definition =>
5609 Make_Access_To_Object_Definition (Loc,
5610 Subtype_Indication =>
5611 New_Occurrence_Of
5612 (Corresponding_Record_Type (Scop), Loc))));
5614 Insert_Actions (N, Decls);
5615 Freeze_Before (N, Obj_Ptr);
5617 Rec :=
5618 Make_Explicit_Dereference (Loc,
5619 Prefix =>
5620 Unchecked_Convert_To (Obj_Ptr,
5621 New_Occurrence_Of (Param, Loc)));
5623 -- Analyze new actual. Other actuals in calls are already analyzed
5624 -- and the list of actuals is not reanalyzed after rewriting.
5626 Set_Parent (Rec, N);
5627 Analyze (Rec);
5628 end;
5629 end if;
5631 return Rec;
5632 end Expand_Protected_Object_Reference;
5634 --------------------------------------
5635 -- Expand_Protected_Subprogram_Call --
5636 --------------------------------------
5638 procedure Expand_Protected_Subprogram_Call
5639 (N : Node_Id;
5640 Subp : Entity_Id;
5641 Scop : Entity_Id)
5643 Rec : Node_Id;
5645 procedure Freeze_Called_Function;
5646 -- If it is a function call it can appear in elaboration code and
5647 -- the called entity must be frozen before the call. This must be
5648 -- done before the call is expanded, as the expansion may rewrite it
5649 -- to something other than a call (e.g. a temporary initialized in a
5650 -- transient block).
5652 ----------------------------
5653 -- Freeze_Called_Function --
5654 ----------------------------
5656 procedure Freeze_Called_Function is
5657 begin
5658 if Ekind (Subp) = E_Function then
5659 Freeze_Expression (Name (N));
5660 end if;
5661 end Freeze_Called_Function;
5663 -- Start of processing for Expand_Protected_Subprogram_Call
5665 begin
5666 -- If the protected object is not an enclosing scope, this is an inter-
5667 -- object function call. Inter-object procedure calls are expanded by
5668 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5669 -- subprogram being called is in the protected body being compiled, and
5670 -- if the protected object in the call is statically the enclosing type.
5671 -- The object may be an component of some other data structure, in which
5672 -- case this must be handled as an inter-object call.
5674 if not In_Open_Scopes (Scop)
5675 or else not Is_Entity_Name (Name (N))
5676 then
5677 if Nkind (Name (N)) = N_Selected_Component then
5678 Rec := Prefix (Name (N));
5680 else
5681 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5682 Rec := Prefix (Prefix (Name (N)));
5683 end if;
5685 Freeze_Called_Function;
5686 Build_Protected_Subprogram_Call (N,
5687 Name => New_Occurrence_Of (Subp, Sloc (N)),
5688 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5689 External => True);
5691 else
5692 Rec := Expand_Protected_Object_Reference (N, Scop);
5694 if No (Rec) then
5695 return;
5696 end if;
5698 Freeze_Called_Function;
5699 Build_Protected_Subprogram_Call (N,
5700 Name => Name (N),
5701 Rec => Rec,
5702 External => False);
5704 end if;
5706 -- Analyze and resolve the new call. The actuals have already been
5707 -- resolved, but expansion of a function call will add extra actuals
5708 -- if needed. Analysis of a procedure call already includes resolution.
5710 Analyze (N);
5712 if Ekind (Subp) = E_Function then
5713 Resolve (N, Etype (Subp));
5714 end if;
5715 end Expand_Protected_Subprogram_Call;
5717 --------------------------------------------
5718 -- Has_Unconstrained_Access_Discriminants --
5719 --------------------------------------------
5721 function Has_Unconstrained_Access_Discriminants
5722 (Subtyp : Entity_Id) return Boolean
5724 Discr : Entity_Id;
5726 begin
5727 if Has_Discriminants (Subtyp)
5728 and then not Is_Constrained (Subtyp)
5729 then
5730 Discr := First_Discriminant (Subtyp);
5731 while Present (Discr) loop
5732 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
5733 return True;
5734 end if;
5736 Next_Discriminant (Discr);
5737 end loop;
5738 end if;
5740 return False;
5741 end Has_Unconstrained_Access_Discriminants;
5743 -----------------------------------
5744 -- Expand_Simple_Function_Return --
5745 -----------------------------------
5747 -- The "simple" comes from the syntax rule simple_return_statement. The
5748 -- semantics are not at all simple.
5750 procedure Expand_Simple_Function_Return (N : Node_Id) is
5751 Loc : constant Source_Ptr := Sloc (N);
5753 Scope_Id : constant Entity_Id :=
5754 Return_Applies_To (Return_Statement_Entity (N));
5755 -- The function we are returning from
5757 R_Type : constant Entity_Id := Etype (Scope_Id);
5758 -- The result type of the function
5760 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5762 Exp : constant Node_Id := Expression (N);
5763 pragma Assert (Present (Exp));
5765 Exptyp : constant Entity_Id := Etype (Exp);
5766 -- The type of the expression (not necessarily the same as R_Type)
5768 Subtype_Ind : Node_Id;
5769 -- If the result type of the function is class-wide and the expression
5770 -- has a specific type, then we use the expression's type as the type of
5771 -- the return object. In cases where the expression is an aggregate that
5772 -- is built in place, this avoids the need for an expensive conversion
5773 -- of the return object to the specific type on assignments to the
5774 -- individual components.
5776 begin
5777 if Is_Class_Wide_Type (R_Type)
5778 and then not Is_Class_Wide_Type (Etype (Exp))
5779 then
5780 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5781 else
5782 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5783 end if;
5785 -- For the case of a simple return that does not come from an extended
5786 -- return, in the case of Ada 2005 where we are returning a limited
5787 -- type, we rewrite "return <expression>;" to be:
5789 -- return _anon_ : <return_subtype> := <expression>
5791 -- The expansion produced by Expand_N_Extended_Return_Statement will
5792 -- contain simple return statements (for example, a block containing
5793 -- simple return of the return object), which brings us back here with
5794 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5795 -- checking for a simple return that does not come from an extended
5796 -- return is to avoid this infinite recursion.
5798 -- The reason for this design is that for Ada 2005 limited returns, we
5799 -- need to reify the return object, so we can build it "in place", and
5800 -- we need a block statement to hang finalization and tasking stuff.
5802 -- ??? In order to avoid disruption, we avoid translating to extended
5803 -- return except in the cases where we really need to (Ada 2005 for
5804 -- inherently limited). We might prefer to do this translation in all
5805 -- cases (except perhaps for the case of Ada 95 inherently limited),
5806 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5807 -- code. This would also allow us to do the build-in-place optimization
5808 -- for efficiency even in cases where it is semantically not required.
5810 -- As before, we check the type of the return expression rather than the
5811 -- return type of the function, because the latter may be a limited
5812 -- class-wide interface type, which is not a limited type, even though
5813 -- the type of the expression may be.
5815 if not Comes_From_Extended_Return_Statement (N)
5816 and then Is_Limited_View (Etype (Expression (N)))
5817 and then Ada_Version >= Ada_2005
5818 and then not Debug_Flag_Dot_L
5820 -- The functionality of interface thunks is simple and it is always
5821 -- handled by means of simple return statements. This leaves their
5822 -- expansion simple and clean.
5824 and then not Is_Thunk (Current_Scope)
5825 then
5826 declare
5827 Return_Object_Entity : constant Entity_Id :=
5828 Make_Temporary (Loc, 'R', Exp);
5830 Obj_Decl : constant Node_Id :=
5831 Make_Object_Declaration (Loc,
5832 Defining_Identifier => Return_Object_Entity,
5833 Object_Definition => Subtype_Ind,
5834 Expression => Exp);
5836 Ext : constant Node_Id :=
5837 Make_Extended_Return_Statement (Loc,
5838 Return_Object_Declarations => New_List (Obj_Decl));
5839 -- Do not perform this high-level optimization if the result type
5840 -- is an interface because the "this" pointer must be displaced.
5842 begin
5843 Rewrite (N, Ext);
5844 Analyze (N);
5845 return;
5846 end;
5847 end if;
5849 -- Here we have a simple return statement that is part of the expansion
5850 -- of an extended return statement (either written by the user, or
5851 -- generated by the above code).
5853 -- Always normalize C/Fortran boolean result. This is not always needed,
5854 -- but it seems a good idea to minimize the passing around of non-
5855 -- normalized values, and in any case this handles the processing of
5856 -- barrier functions for protected types, which turn the condition into
5857 -- a return statement.
5859 if Is_Boolean_Type (Exptyp)
5860 and then Nonzero_Is_True (Exptyp)
5861 then
5862 Adjust_Condition (Exp);
5863 Adjust_Result_Type (Exp, Exptyp);
5864 end if;
5866 -- Do validity check if enabled for returns
5868 if Validity_Checks_On
5869 and then Validity_Check_Returns
5870 then
5871 Ensure_Valid (Exp);
5872 end if;
5874 -- Check the result expression of a scalar function against the subtype
5875 -- of the function by inserting a conversion. This conversion must
5876 -- eventually be performed for other classes of types, but for now it's
5877 -- only done for scalars.
5878 -- ???
5880 if Is_Scalar_Type (Exptyp) then
5881 Rewrite (Exp, Convert_To (R_Type, Exp));
5883 -- The expression is resolved to ensure that the conversion gets
5884 -- expanded to generate a possible constraint check.
5886 Analyze_And_Resolve (Exp, R_Type);
5887 end if;
5889 -- Deal with returning variable length objects and controlled types
5891 -- Nothing to do if we are returning by reference, or this is not a
5892 -- type that requires special processing (indicated by the fact that
5893 -- it requires a cleanup scope for the secondary stack case).
5895 if Is_Limited_View (Exptyp)
5896 or else Is_Limited_Interface (Exptyp)
5897 then
5898 null;
5900 -- No copy needed for thunks returning interface type objects since
5901 -- the object is returned by reference and the maximum functionality
5902 -- required is just to displace the pointer.
5904 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
5905 null;
5907 elsif not Requires_Transient_Scope (R_Type) then
5909 -- Mutable records with no variable length components are not
5910 -- returned on the sec-stack, so we need to make sure that the
5911 -- backend will only copy back the size of the actual value, and not
5912 -- the maximum size. We create an actual subtype for this purpose.
5914 declare
5915 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
5916 Decl : Node_Id;
5917 Ent : Entity_Id;
5918 begin
5919 if Has_Discriminants (Ubt)
5920 and then not Is_Constrained (Ubt)
5921 and then not Has_Unchecked_Union (Ubt)
5922 then
5923 Decl := Build_Actual_Subtype (Ubt, Exp);
5924 Ent := Defining_Identifier (Decl);
5925 Insert_Action (Exp, Decl);
5926 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
5927 Analyze_And_Resolve (Exp);
5928 end if;
5929 end;
5931 -- Here if secondary stack is used
5933 else
5934 -- Prevent the reclamation of the secondary stack by all enclosing
5935 -- blocks and loops as well as the related function, otherwise the
5936 -- result will be reclaimed too early or even clobbered. Due to a
5937 -- possible mix of internally generated blocks, source blocks and
5938 -- loops, the scope stack may not be contiguous as all labels are
5939 -- inserted at the top level within the related function. Instead,
5940 -- perform a parent-based traversal and mark all appropriate
5941 -- constructs.
5943 declare
5944 P : Node_Id;
5946 begin
5947 P := N;
5948 while Present (P) loop
5950 -- Mark the label of a source or internally generated block or
5951 -- loop.
5953 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
5954 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
5956 -- Mark the enclosing function
5958 elsif Nkind (P) = N_Subprogram_Body then
5959 if Present (Corresponding_Spec (P)) then
5960 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
5961 else
5962 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
5963 end if;
5965 -- Do not go beyond the enclosing function
5967 exit;
5968 end if;
5970 P := Parent (P);
5971 end loop;
5972 end;
5974 -- Optimize the case where the result is a function call. In this
5975 -- case either the result is already on the secondary stack, or is
5976 -- already being returned with the stack pointer depressed and no
5977 -- further processing is required except to set the By_Ref flag
5978 -- to ensure that gigi does not attempt an extra unnecessary copy.
5979 -- (actually not just unnecessary but harmfully wrong in the case
5980 -- of a controlled type, where gigi does not know how to do a copy).
5981 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
5982 -- for array types if the constrained status of the target type is
5983 -- different from that of the expression.
5985 if Requires_Transient_Scope (Exptyp)
5986 and then
5987 (not Is_Array_Type (Exptyp)
5988 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
5989 or else CW_Or_Has_Controlled_Part (Utyp))
5990 and then Nkind (Exp) = N_Function_Call
5991 then
5992 Set_By_Ref (N);
5994 -- Remove side effects from the expression now so that other parts
5995 -- of the expander do not have to reanalyze this node without this
5996 -- optimization
5998 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6000 -- For controlled types, do the allocation on the secondary stack
6001 -- manually in order to call adjust at the right time:
6003 -- type Anon1 is access R_Type;
6004 -- for Anon1'Storage_pool use ss_pool;
6005 -- Anon2 : anon1 := new R_Type'(expr);
6006 -- return Anon2.all;
6008 -- We do the same for classwide types that are not potentially
6009 -- controlled (by the virtue of restriction No_Finalization) because
6010 -- gigi is not able to properly allocate class-wide types.
6012 elsif CW_Or_Has_Controlled_Part (Utyp) then
6013 declare
6014 Loc : constant Source_Ptr := Sloc (N);
6015 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6016 Alloc_Node : Node_Id;
6017 Temp : Entity_Id;
6019 begin
6020 Set_Ekind (Acc_Typ, E_Access_Type);
6022 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6024 -- This is an allocator for the secondary stack, and it's fine
6025 -- to have Comes_From_Source set False on it, as gigi knows not
6026 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6028 Alloc_Node :=
6029 Make_Allocator (Loc,
6030 Expression =>
6031 Make_Qualified_Expression (Loc,
6032 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6033 Expression => Relocate_Node (Exp)));
6035 -- We do not want discriminant checks on the declaration,
6036 -- given that it gets its value from the allocator.
6038 Set_No_Initialization (Alloc_Node);
6040 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6042 Insert_List_Before_And_Analyze (N, New_List (
6043 Make_Full_Type_Declaration (Loc,
6044 Defining_Identifier => Acc_Typ,
6045 Type_Definition =>
6046 Make_Access_To_Object_Definition (Loc,
6047 Subtype_Indication => Subtype_Ind)),
6049 Make_Object_Declaration (Loc,
6050 Defining_Identifier => Temp,
6051 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6052 Expression => Alloc_Node)));
6054 Rewrite (Exp,
6055 Make_Explicit_Dereference (Loc,
6056 Prefix => New_Occurrence_Of (Temp, Loc)));
6058 -- Ada 2005 (AI-251): If the type of the returned object is
6059 -- an interface then add an implicit type conversion to force
6060 -- displacement of the "this" pointer.
6062 if Is_Interface (R_Type) then
6063 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6064 end if;
6066 Analyze_And_Resolve (Exp, R_Type);
6067 end;
6069 -- Otherwise use the gigi mechanism to allocate result on the
6070 -- secondary stack.
6072 else
6073 Check_Restriction (No_Secondary_Stack, N);
6074 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6076 -- If we are generating code for the VM do not use
6077 -- SS_Allocate since everything is heap-allocated anyway.
6079 if VM_Target = No_VM then
6080 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6081 end if;
6082 end if;
6083 end if;
6085 -- Implement the rules of 6.5(8-10), which require a tag check in
6086 -- the case of a limited tagged return type, and tag reassignment for
6087 -- nonlimited tagged results. These actions are needed when the return
6088 -- type is a specific tagged type and the result expression is a
6089 -- conversion or a formal parameter, because in that case the tag of
6090 -- the expression might differ from the tag of the specific result type.
6092 if Is_Tagged_Type (Utyp)
6093 and then not Is_Class_Wide_Type (Utyp)
6094 and then (Nkind_In (Exp, N_Type_Conversion,
6095 N_Unchecked_Type_Conversion)
6096 or else (Is_Entity_Name (Exp)
6097 and then Ekind (Entity (Exp)) in Formal_Kind))
6098 then
6099 -- When the return type is limited, perform a check that the tag of
6100 -- the result is the same as the tag of the return type.
6102 if Is_Limited_Type (R_Type) then
6103 Insert_Action (Exp,
6104 Make_Raise_Constraint_Error (Loc,
6105 Condition =>
6106 Make_Op_Ne (Loc,
6107 Left_Opnd =>
6108 Make_Selected_Component (Loc,
6109 Prefix => Duplicate_Subexpr (Exp),
6110 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6111 Right_Opnd =>
6112 Make_Attribute_Reference (Loc,
6113 Prefix =>
6114 New_Occurrence_Of (Base_Type (Utyp), Loc),
6115 Attribute_Name => Name_Tag)),
6116 Reason => CE_Tag_Check_Failed));
6118 -- If the result type is a specific nonlimited tagged type, then we
6119 -- have to ensure that the tag of the result is that of the result
6120 -- type. This is handled by making a copy of the expression in
6121 -- the case where it might have a different tag, namely when the
6122 -- expression is a conversion or a formal parameter. We create a new
6123 -- object of the result type and initialize it from the expression,
6124 -- which will implicitly force the tag to be set appropriately.
6126 else
6127 declare
6128 ExpR : constant Node_Id := Relocate_Node (Exp);
6129 Result_Id : constant Entity_Id :=
6130 Make_Temporary (Loc, 'R', ExpR);
6131 Result_Exp : constant Node_Id :=
6132 New_Occurrence_Of (Result_Id, Loc);
6133 Result_Obj : constant Node_Id :=
6134 Make_Object_Declaration (Loc,
6135 Defining_Identifier => Result_Id,
6136 Object_Definition =>
6137 New_Occurrence_Of (R_Type, Loc),
6138 Constant_Present => True,
6139 Expression => ExpR);
6141 begin
6142 Set_Assignment_OK (Result_Obj);
6143 Insert_Action (Exp, Result_Obj);
6145 Rewrite (Exp, Result_Exp);
6146 Analyze_And_Resolve (Exp, R_Type);
6147 end;
6148 end if;
6150 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6151 -- a check that the level of the return expression's underlying type
6152 -- is not deeper than the level of the master enclosing the function.
6153 -- Always generate the check when the type of the return expression
6154 -- is class-wide, when it's a type conversion, or when it's a formal
6155 -- parameter. Otherwise, suppress the check in the case where the
6156 -- return expression has a specific type whose level is known not to
6157 -- be statically deeper than the function's result type.
6159 -- No runtime check needed in interface thunks since it is performed
6160 -- by the target primitive associated with the thunk.
6162 -- Note: accessibility check is skipped in the VM case, since there
6163 -- does not seem to be any practical way to implement this check.
6165 elsif Ada_Version >= Ada_2005
6166 and then Tagged_Type_Expansion
6167 and then Is_Class_Wide_Type (R_Type)
6168 and then not Is_Thunk (Current_Scope)
6169 and then not Scope_Suppress.Suppress (Accessibility_Check)
6170 and then
6171 (Is_Class_Wide_Type (Etype (Exp))
6172 or else Nkind_In (Exp, N_Type_Conversion,
6173 N_Unchecked_Type_Conversion)
6174 or else (Is_Entity_Name (Exp)
6175 and then Ekind (Entity (Exp)) in Formal_Kind)
6176 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6177 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6178 then
6179 declare
6180 Tag_Node : Node_Id;
6182 begin
6183 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6184 -- "this" to reference the base of the object. This is required to
6185 -- get access to the TSD of the object.
6187 if Is_Class_Wide_Type (Etype (Exp))
6188 and then Is_Interface (Etype (Exp))
6189 and then Nkind (Exp) = N_Explicit_Dereference
6190 then
6191 Tag_Node :=
6192 Make_Explicit_Dereference (Loc,
6193 Prefix =>
6194 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6195 Make_Function_Call (Loc,
6196 Name =>
6197 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6198 Parameter_Associations => New_List (
6199 Unchecked_Convert_To (RTE (RE_Address),
6200 Duplicate_Subexpr (Prefix (Exp)))))));
6201 else
6202 Tag_Node :=
6203 Make_Attribute_Reference (Loc,
6204 Prefix => Duplicate_Subexpr (Exp),
6205 Attribute_Name => Name_Tag);
6206 end if;
6208 Insert_Action (Exp,
6209 Make_Raise_Program_Error (Loc,
6210 Condition =>
6211 Make_Op_Gt (Loc,
6212 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6213 Right_Opnd =>
6214 Make_Integer_Literal (Loc,
6215 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6216 Reason => PE_Accessibility_Check_Failed));
6217 end;
6219 -- AI05-0073: If function has a controlling access result, check that
6220 -- the tag of the return value, if it is not null, matches designated
6221 -- type of return type.
6223 -- The return expression is referenced twice in the code below, so it
6224 -- must be made free of side effects. Given that different compilers
6225 -- may evaluate these parameters in different order, both occurrences
6226 -- perform a copy.
6228 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6229 and then Has_Controlling_Result (Scope_Id)
6230 then
6231 Insert_Action (N,
6232 Make_Raise_Constraint_Error (Loc,
6233 Condition =>
6234 Make_And_Then (Loc,
6235 Left_Opnd =>
6236 Make_Op_Ne (Loc,
6237 Left_Opnd => Duplicate_Subexpr (Exp),
6238 Right_Opnd => Make_Null (Loc)),
6240 Right_Opnd => Make_Op_Ne (Loc,
6241 Left_Opnd =>
6242 Make_Selected_Component (Loc,
6243 Prefix => Duplicate_Subexpr (Exp),
6244 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6246 Right_Opnd =>
6247 Make_Attribute_Reference (Loc,
6248 Prefix =>
6249 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6250 Attribute_Name => Name_Tag))),
6252 Reason => CE_Tag_Check_Failed),
6253 Suppress => All_Checks);
6254 end if;
6256 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6257 -- ensure that the function result does not outlive an
6258 -- object designated by one of it discriminants.
6260 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6261 and then Has_Unconstrained_Access_Discriminants (R_Type)
6262 then
6263 declare
6264 Discrim_Source : Node_Id;
6266 procedure Check_Against_Result_Level (Level : Node_Id);
6267 -- Check the given accessibility level against the level
6268 -- determined by the point of call. (AI05-0234).
6270 --------------------------------
6271 -- Check_Against_Result_Level --
6272 --------------------------------
6274 procedure Check_Against_Result_Level (Level : Node_Id) is
6275 begin
6276 Insert_Action (N,
6277 Make_Raise_Program_Error (Loc,
6278 Condition =>
6279 Make_Op_Gt (Loc,
6280 Left_Opnd => Level,
6281 Right_Opnd =>
6282 New_Occurrence_Of
6283 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6284 Reason => PE_Accessibility_Check_Failed));
6285 end Check_Against_Result_Level;
6287 begin
6288 Discrim_Source := Exp;
6289 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6290 Discrim_Source := Expression (Discrim_Source);
6291 end loop;
6293 if Nkind (Discrim_Source) = N_Identifier
6294 and then Is_Return_Object (Entity (Discrim_Source))
6295 then
6296 Discrim_Source := Entity (Discrim_Source);
6298 if Is_Constrained (Etype (Discrim_Source)) then
6299 Discrim_Source := Etype (Discrim_Source);
6300 else
6301 Discrim_Source := Expression (Parent (Discrim_Source));
6302 end if;
6304 elsif Nkind (Discrim_Source) = N_Identifier
6305 and then Nkind_In (Original_Node (Discrim_Source),
6306 N_Aggregate, N_Extension_Aggregate)
6307 then
6308 Discrim_Source := Original_Node (Discrim_Source);
6310 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6311 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6312 then
6313 Discrim_Source := Original_Node (Discrim_Source);
6314 end if;
6316 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6317 N_Type_Conversion,
6318 N_Unchecked_Type_Conversion)
6319 loop
6320 Discrim_Source := Expression (Discrim_Source);
6321 end loop;
6323 case Nkind (Discrim_Source) is
6324 when N_Defining_Identifier =>
6326 pragma Assert (Is_Composite_Type (Discrim_Source)
6327 and then Has_Discriminants (Discrim_Source)
6328 and then Is_Constrained (Discrim_Source));
6330 declare
6331 Discrim : Entity_Id :=
6332 First_Discriminant (Base_Type (R_Type));
6333 Disc_Elmt : Elmt_Id :=
6334 First_Elmt (Discriminant_Constraint
6335 (Discrim_Source));
6336 begin
6337 loop
6338 if Ekind (Etype (Discrim)) =
6339 E_Anonymous_Access_Type
6340 then
6341 Check_Against_Result_Level
6342 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6343 end if;
6345 Next_Elmt (Disc_Elmt);
6346 Next_Discriminant (Discrim);
6347 exit when not Present (Discrim);
6348 end loop;
6349 end;
6351 when N_Aggregate | N_Extension_Aggregate =>
6353 -- Unimplemented: extension aggregate case where discrims
6354 -- come from ancestor part, not extension part.
6356 declare
6357 Discrim : Entity_Id :=
6358 First_Discriminant (Base_Type (R_Type));
6360 Disc_Exp : Node_Id := Empty;
6362 Positionals_Exhausted
6363 : Boolean := not Present (Expressions
6364 (Discrim_Source));
6366 function Associated_Expr
6367 (Comp_Id : Entity_Id;
6368 Associations : List_Id) return Node_Id;
6370 -- Given a component and a component associations list,
6371 -- locate the expression for that component; returns
6372 -- Empty if no such expression is found.
6374 ---------------------
6375 -- Associated_Expr --
6376 ---------------------
6378 function Associated_Expr
6379 (Comp_Id : Entity_Id;
6380 Associations : List_Id) return Node_Id
6382 Assoc : Node_Id;
6383 Choice : Node_Id;
6385 begin
6386 -- Simple linear search seems ok here
6388 Assoc := First (Associations);
6389 while Present (Assoc) loop
6390 Choice := First (Choices (Assoc));
6391 while Present (Choice) loop
6392 if (Nkind (Choice) = N_Identifier
6393 and then Chars (Choice) = Chars (Comp_Id))
6394 or else (Nkind (Choice) = N_Others_Choice)
6395 then
6396 return Expression (Assoc);
6397 end if;
6399 Next (Choice);
6400 end loop;
6402 Next (Assoc);
6403 end loop;
6405 return Empty;
6406 end Associated_Expr;
6408 -- Start of processing for Expand_Simple_Function_Return
6410 begin
6411 if not Positionals_Exhausted then
6412 Disc_Exp := First (Expressions (Discrim_Source));
6413 end if;
6415 loop
6416 if Positionals_Exhausted then
6417 Disc_Exp :=
6418 Associated_Expr
6419 (Discrim,
6420 Component_Associations (Discrim_Source));
6421 end if;
6423 if Ekind (Etype (Discrim)) =
6424 E_Anonymous_Access_Type
6425 then
6426 Check_Against_Result_Level
6427 (Dynamic_Accessibility_Level (Disc_Exp));
6428 end if;
6430 Next_Discriminant (Discrim);
6431 exit when not Present (Discrim);
6433 if not Positionals_Exhausted then
6434 Next (Disc_Exp);
6435 Positionals_Exhausted := not Present (Disc_Exp);
6436 end if;
6437 end loop;
6438 end;
6440 when N_Function_Call =>
6442 -- No check needed (check performed by callee)
6444 null;
6446 when others =>
6448 declare
6449 Level : constant Node_Id :=
6450 Make_Integer_Literal (Loc,
6451 Object_Access_Level (Discrim_Source));
6453 begin
6454 -- Unimplemented: check for name prefix that includes
6455 -- a dereference of an access value with a dynamic
6456 -- accessibility level (e.g., an access param or a
6457 -- saooaaat) and use dynamic level in that case. For
6458 -- example:
6459 -- return Access_Param.all(Some_Index).Some_Component;
6460 -- ???
6462 Set_Etype (Level, Standard_Natural);
6463 Check_Against_Result_Level (Level);
6464 end;
6466 end case;
6467 end;
6468 end if;
6470 -- If we are returning an object that may not be bit-aligned, then copy
6471 -- the value into a temporary first. This copy may need to expand to a
6472 -- loop of component operations.
6474 if Is_Possibly_Unaligned_Slice (Exp)
6475 or else Is_Possibly_Unaligned_Object (Exp)
6476 then
6477 declare
6478 ExpR : constant Node_Id := Relocate_Node (Exp);
6479 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6480 begin
6481 Insert_Action (Exp,
6482 Make_Object_Declaration (Loc,
6483 Defining_Identifier => Tnn,
6484 Constant_Present => True,
6485 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6486 Expression => ExpR),
6487 Suppress => All_Checks);
6488 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6489 end;
6490 end if;
6492 -- Generate call to postcondition checks if they are present
6494 if Ekind (Scope_Id) = E_Function
6495 and then Has_Postconditions (Scope_Id)
6496 then
6497 -- We are going to reference the returned value twice in this case,
6498 -- once in the call to _Postconditions, and once in the actual return
6499 -- statement, but we can't have side effects happening twice, and in
6500 -- any case for efficiency we don't want to do the computation twice.
6502 -- If the returned expression is an entity name, we don't need to
6503 -- worry since it is efficient and safe to reference it twice, that's
6504 -- also true for literals other than string literals, and for the
6505 -- case of X.all where X is an entity name.
6507 if Is_Entity_Name (Exp)
6508 or else Nkind_In (Exp, N_Character_Literal,
6509 N_Integer_Literal,
6510 N_Real_Literal)
6511 or else (Nkind (Exp) = N_Explicit_Dereference
6512 and then Is_Entity_Name (Prefix (Exp)))
6513 then
6514 null;
6516 -- Otherwise we are going to need a temporary to capture the value
6518 else
6519 declare
6520 ExpR : Node_Id := Relocate_Node (Exp);
6521 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6523 begin
6524 -- In the case of discriminated objects, we have created a
6525 -- constrained subtype above, and used the underlying type.
6526 -- This transformation is post-analysis and harmless, except
6527 -- that now the call to the post-condition will be analyzed and
6528 -- type kinds have to match.
6530 if Nkind (ExpR) = N_Unchecked_Type_Conversion
6531 and then
6532 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
6533 then
6534 ExpR := Expression (ExpR);
6535 end if;
6537 -- For a complex expression of an elementary type, capture
6538 -- value in the temporary and use it as the reference.
6540 if Is_Elementary_Type (R_Type) then
6541 Insert_Action (Exp,
6542 Make_Object_Declaration (Loc,
6543 Defining_Identifier => Tnn,
6544 Constant_Present => True,
6545 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6546 Expression => ExpR),
6547 Suppress => All_Checks);
6549 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6551 -- If we have something we can rename, generate a renaming of
6552 -- the object and replace the expression with a reference
6554 elsif Is_Object_Reference (Exp) then
6555 Insert_Action (Exp,
6556 Make_Object_Renaming_Declaration (Loc,
6557 Defining_Identifier => Tnn,
6558 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6559 Name => ExpR),
6560 Suppress => All_Checks);
6562 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6564 -- Otherwise we have something like a string literal or an
6565 -- aggregate. We could copy the value, but that would be
6566 -- inefficient. Instead we make a reference to the value and
6567 -- capture this reference with a renaming, the expression is
6568 -- then replaced by a dereference of this renaming.
6570 else
6571 -- For now, copy the value, since the code below does not
6572 -- seem to work correctly ???
6574 Insert_Action (Exp,
6575 Make_Object_Declaration (Loc,
6576 Defining_Identifier => Tnn,
6577 Constant_Present => True,
6578 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6579 Expression => Relocate_Node (Exp)),
6580 Suppress => All_Checks);
6582 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6584 -- Insert_Action (Exp,
6585 -- Make_Object_Renaming_Declaration (Loc,
6586 -- Defining_Identifier => Tnn,
6587 -- Access_Definition =>
6588 -- Make_Access_Definition (Loc,
6589 -- All_Present => True,
6590 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6591 -- Name =>
6592 -- Make_Reference (Loc,
6593 -- Prefix => Relocate_Node (Exp))),
6594 -- Suppress => All_Checks);
6596 -- Rewrite (Exp,
6597 -- Make_Explicit_Dereference (Loc,
6598 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6599 end if;
6600 end;
6601 end if;
6603 -- Generate call to _postconditions
6605 Insert_Action (Exp,
6606 Make_Procedure_Call_Statement (Loc,
6607 Name => Make_Identifier (Loc, Name_uPostconditions),
6608 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6609 end if;
6611 -- Ada 2005 (AI-251): If this return statement corresponds with an
6612 -- simple return statement associated with an extended return statement
6613 -- and the type of the returned object is an interface then generate an
6614 -- implicit conversion to force displacement of the "this" pointer.
6616 if Ada_Version >= Ada_2005
6617 and then Comes_From_Extended_Return_Statement (N)
6618 and then Nkind (Expression (N)) = N_Identifier
6619 and then Is_Interface (Utyp)
6620 and then Utyp /= Underlying_Type (Exptyp)
6621 then
6622 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6623 Analyze_And_Resolve (Exp);
6624 end if;
6625 end Expand_Simple_Function_Return;
6627 --------------------------------
6628 -- Expand_Subprogram_Contract --
6629 --------------------------------
6631 procedure Expand_Subprogram_Contract
6632 (N : Node_Id;
6633 Spec_Id : Entity_Id;
6634 Body_Id : Entity_Id)
6636 procedure Add_Invariant_And_Predicate_Checks
6637 (Subp_Id : Entity_Id;
6638 Stmts : in out List_Id;
6639 Result : out Node_Id);
6640 -- Process the result of function Subp_Id (if applicable) and all its
6641 -- formals. Add invariant and predicate checks where applicable. The
6642 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6643 -- function, Result contains the entity of parameter _Result, to be
6644 -- used in the creation of procedure _Postconditions.
6646 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
6647 -- Append a node to a list. If there is no list, create a new one. When
6648 -- the item denotes a pragma, it is added to the list only when it is
6649 -- enabled.
6651 procedure Build_Postconditions_Procedure
6652 (Subp_Id : Entity_Id;
6653 Stmts : List_Id;
6654 Result : Entity_Id);
6655 -- Create the body of procedure _Postconditions which handles various
6656 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6657 -- of statements to be checked on exit. Parameter Result is the entity
6658 -- of parameter _Result when Subp_Id denotes a function.
6660 function Build_Pragma_Check_Equivalent
6661 (Prag : Node_Id;
6662 Subp_Id : Entity_Id := Empty;
6663 Inher_Id : Entity_Id := Empty) return Node_Id;
6664 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6665 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6666 -- the routine corrects the references of all formals of Inher_Id to
6667 -- point to the formals of Subp_Id.
6669 procedure Collect_Body_Postconditions (Stmts : in out List_Id);
6670 -- Process all postconditions found in the declarations of the body. The
6671 -- routine appends the pragma Check equivalents to list Stmts.
6673 procedure Collect_Spec_Postconditions
6674 (Subp_Id : Entity_Id;
6675 Stmts : in out List_Id);
6676 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
6677 -- The routine appends the pragma Check equivalents to list Stmts.
6679 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id);
6680 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
6681 -- routine prepends the pragma Check equivalents to the declarations of
6682 -- the body.
6684 procedure Prepend_To_Declarations (Item : Node_Id);
6685 -- Prepend a single item to the declarations of the subprogram body
6687 procedure Process_Contract_Cases
6688 (Subp_Id : Entity_Id;
6689 Stmts : in out List_Id);
6690 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
6691 -- appends the expanded code to list Stmts.
6693 ----------------------------------------
6694 -- Add_Invariant_And_Predicate_Checks --
6695 ----------------------------------------
6697 procedure Add_Invariant_And_Predicate_Checks
6698 (Subp_Id : Entity_Id;
6699 Stmts : in out List_Id;
6700 Result : out Node_Id)
6702 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
6703 -- Id denotes the return value of a function or a formal parameter.
6704 -- Add an invariant check if the type of Id is access to a type with
6705 -- invariants. The routine appends the generated code to Stmts.
6707 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
6708 -- Determine whether type Typ can benefit from invariant checks. To
6709 -- qualify, the type must have a non-null invariant procedure and
6710 -- subprogram Subp_Id must appear visible from the point of view of
6711 -- the type.
6713 ---------------------------------
6714 -- Add_Invariant_Access_Checks --
6715 ---------------------------------
6717 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
6718 Loc : constant Source_Ptr := Sloc (N);
6719 Ref : Node_Id;
6720 Typ : Entity_Id;
6722 begin
6723 Typ := Etype (Id);
6725 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
6726 Typ := Designated_Type (Typ);
6728 if Invariant_Checks_OK (Typ) then
6729 Ref :=
6730 Make_Explicit_Dereference (Loc,
6731 Prefix => New_Occurrence_Of (Id, Loc));
6732 Set_Etype (Ref, Typ);
6734 -- Generate:
6735 -- if <Id> /= null then
6736 -- <invariant_call (<Ref>)>
6737 -- end if;
6739 Append_Enabled_Item
6740 (Item =>
6741 Make_If_Statement (Loc,
6742 Condition =>
6743 Make_Op_Ne (Loc,
6744 Left_Opnd => New_Occurrence_Of (Id, Loc),
6745 Right_Opnd => Make_Null (Loc)),
6746 Then_Statements => New_List (
6747 Make_Invariant_Call (Ref))),
6748 List => Stmts);
6749 end if;
6750 end if;
6751 end Add_Invariant_Access_Checks;
6753 -------------------------
6754 -- Invariant_Checks_OK --
6755 -------------------------
6757 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
6758 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
6759 -- Determine whether the body of procedure Proc_Id contains a sole
6760 -- null statement, possibly followed by an optional return.
6762 function Has_Public_Visibility_Of_Subprogram return Boolean;
6763 -- Determine whether type Typ has public visibility of subprogram
6764 -- Subp_Id.
6766 -------------------
6767 -- Has_Null_Body --
6768 -------------------
6770 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
6771 Body_Id : Entity_Id;
6772 Decl : Node_Id;
6773 Spec : Node_Id;
6774 Stmt1 : Node_Id;
6775 Stmt2 : Node_Id;
6777 begin
6778 Spec := Parent (Proc_Id);
6779 Decl := Parent (Spec);
6781 -- Retrieve the entity of the invariant procedure body
6783 if Nkind (Spec) = N_Procedure_Specification
6784 and then Nkind (Decl) = N_Subprogram_Declaration
6785 then
6786 Body_Id := Corresponding_Body (Decl);
6788 -- The body acts as a spec
6790 else
6791 Body_Id := Proc_Id;
6792 end if;
6794 -- The body will be generated later
6796 if No (Body_Id) then
6797 return False;
6798 end if;
6800 Spec := Parent (Body_Id);
6801 Decl := Parent (Spec);
6803 pragma Assert
6804 (Nkind (Spec) = N_Procedure_Specification
6805 and then Nkind (Decl) = N_Subprogram_Body);
6807 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
6809 -- Look for a null statement followed by an optional return
6810 -- statement.
6812 if Nkind (Stmt1) = N_Null_Statement then
6813 Stmt2 := Next (Stmt1);
6815 if Present (Stmt2) then
6816 return Nkind (Stmt2) = N_Simple_Return_Statement;
6817 else
6818 return True;
6819 end if;
6820 end if;
6822 return False;
6823 end Has_Null_Body;
6825 -----------------------------------------
6826 -- Has_Public_Visibility_Of_Subprogram --
6827 -----------------------------------------
6829 function Has_Public_Visibility_Of_Subprogram return Boolean is
6830 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
6832 begin
6833 -- An Initialization procedure must be considered visible even
6834 -- though it is internally generated.
6836 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
6837 return True;
6839 elsif Ekind (Scope (Typ)) /= E_Package then
6840 return False;
6842 -- Internally generated code is never publicly visible except
6843 -- for a subprogram that is the implementation of an expression
6844 -- function. In that case the visibility is determined by the
6845 -- last check.
6847 elsif not Comes_From_Source (Subp_Decl)
6848 and then
6849 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
6850 or else not
6851 Comes_From_Source (Defining_Entity (Subp_Decl)))
6852 then
6853 return False;
6855 -- Determine whether the subprogram is declared in the visible
6856 -- declarations of the package containing the type.
6858 else
6859 return List_Containing (Subp_Decl) =
6860 Visible_Declarations
6861 (Specification (Unit_Declaration_Node (Scope (Typ))));
6862 end if;
6863 end Has_Public_Visibility_Of_Subprogram;
6865 -- Start of processing for Invariant_Checks_OK
6867 begin
6868 return
6869 Has_Invariants (Typ)
6870 and then Present (Invariant_Procedure (Typ))
6871 and then not Has_Null_Body (Invariant_Procedure (Typ))
6872 and then Has_Public_Visibility_Of_Subprogram;
6873 end Invariant_Checks_OK;
6875 -- Local variables
6877 Loc : constant Source_Ptr := Sloc (N);
6878 -- Source location of subprogram contract
6880 Formal : Entity_Id;
6881 Typ : Entity_Id;
6883 -- Start of processing for Add_Invariant_And_Predicate_Checks
6885 begin
6886 Result := Empty;
6888 -- Do not generate any checks if no code is being generated
6890 if not Expander_Active then
6891 return;
6892 end if;
6894 -- Process the result of a function
6896 if Ekind_In (Subp_Id, E_Function, E_Generic_Function) then
6897 Typ := Etype (Subp_Id);
6899 -- Generate _Result which is used in procedure _Postconditions to
6900 -- verify the return value.
6902 Result := Make_Defining_Identifier (Loc, Name_uResult);
6903 Set_Etype (Result, Typ);
6905 -- Add an invariant check when the return type has invariants and
6906 -- the related function is visible to the outside.
6908 if Invariant_Checks_OK (Typ) then
6909 Append_Enabled_Item
6910 (Item =>
6911 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
6912 List => Stmts);
6913 end if;
6915 -- Add an invariant check when the return type is an access to a
6916 -- type with invariants.
6918 Add_Invariant_Access_Checks (Result);
6919 end if;
6921 -- Add invariant and predicates for all formals that qualify
6923 Formal := First_Formal (Subp_Id);
6924 while Present (Formal) loop
6925 Typ := Etype (Formal);
6927 if Ekind (Formal) /= E_In_Parameter
6928 or else Is_Access_Type (Typ)
6929 then
6930 if Invariant_Checks_OK (Typ) then
6931 Append_Enabled_Item
6932 (Item =>
6933 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
6934 List => Stmts);
6935 end if;
6937 Add_Invariant_Access_Checks (Formal);
6939 -- Note: we used to add predicate checks for OUT and IN OUT
6940 -- formals here, but that was misguided, since such checks are
6941 -- performed on the caller side, based on the predicate of the
6942 -- actual, rather than the predicate of the formal.
6944 end if;
6946 Next_Formal (Formal);
6947 end loop;
6948 end Add_Invariant_And_Predicate_Checks;
6950 -------------------------
6951 -- Append_Enabled_Item --
6952 -------------------------
6954 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
6955 begin
6956 -- Do not chain ignored or disabled pragmas
6958 if Nkind (Item) = N_Pragma
6959 and then (Is_Ignored (Item) or else Is_Disabled (Item))
6960 then
6961 null;
6963 -- Otherwise, add the item
6965 else
6966 if No (List) then
6967 List := New_List;
6968 end if;
6970 -- If the pragma is a conjunct in a composite postcondition, it
6971 -- has been processed in reverse order. In the postcondition body
6972 -- if must appear before the others.
6974 if Nkind (Item) = N_Pragma
6975 and then From_Aspect_Specification (Item)
6976 and then Split_PPC (Item)
6977 then
6978 Prepend (Item, List);
6979 else
6980 Append (Item, List);
6981 end if;
6982 end if;
6983 end Append_Enabled_Item;
6985 ------------------------------------
6986 -- Build_Postconditions_Procedure --
6987 ------------------------------------
6989 procedure Build_Postconditions_Procedure
6990 (Subp_Id : Entity_Id;
6991 Stmts : List_Id;
6992 Result : Entity_Id)
6994 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
6995 -- Insert node Stmt before the first source declaration of the
6996 -- related subprogram's body. If no such declaration exists, Stmt
6997 -- becomes the last declaration.
6999 --------------------------------------------
7000 -- Insert_Before_First_Source_Declaration --
7001 --------------------------------------------
7003 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
7004 Decls : constant List_Id := Declarations (N);
7005 Decl : Node_Id;
7007 begin
7008 -- Inspect the declarations of the related subprogram body looking
7009 -- for the first source declaration.
7011 if Present (Decls) then
7012 Decl := First (Decls);
7013 while Present (Decl) loop
7014 if Comes_From_Source (Decl) then
7015 Insert_Before (Decl, Stmt);
7016 return;
7017 end if;
7019 Next (Decl);
7020 end loop;
7022 -- If we get there, then the subprogram body lacks any source
7023 -- declarations. The body of _Postconditions now acts as the
7024 -- last declaration.
7026 Append (Stmt, Decls);
7028 -- Ensure that the body has a declaration list
7030 else
7031 Set_Declarations (N, New_List (Stmt));
7032 end if;
7033 end Insert_Before_First_Source_Declaration;
7035 -- Local variables
7037 Loc : constant Source_Ptr := Sloc (N);
7038 Params : List_Id := No_List;
7039 Proc_Id : Entity_Id;
7041 -- Start of processing for Build_Postconditions_Procedure
7043 begin
7044 -- Do not create the routine if no code is being generated
7046 if not Expander_Active then
7047 return;
7049 -- Nothing to do if there are no actions to check on exit
7051 elsif No (Stmts) then
7052 return;
7053 end if;
7055 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
7057 -- The related subprogram is a function, create the specification of
7058 -- parameter _Result.
7060 if Present (Result) then
7061 Params := New_List (
7062 Make_Parameter_Specification (Loc,
7063 Defining_Identifier => Result,
7064 Parameter_Type =>
7065 New_Occurrence_Of (Etype (Result), Loc)));
7066 end if;
7068 -- Insert _Postconditions before the first source declaration of the
7069 -- body. This ensures that the body will not cause any premature
7070 -- freezing as it may mention types:
7072 -- procedure Proc (Obj : Array_Typ) is
7073 -- procedure _postconditions is
7074 -- begin
7075 -- ... Obj ...
7076 -- end _postconditions;
7078 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7079 -- begin
7081 -- In the example above, Obj is of type T but the incorrect placement
7082 -- of _Postconditions will cause a crash in gigi due to an out of
7083 -- order reference. The body of _Postconditions must be placed after
7084 -- the declaration of Temp to preserve correct visibility.
7086 -- Note that we set an explicit End_Label in order to override the
7087 -- sloc of the implicit RETURN statement, and prevent it from
7088 -- inheriting the sloc of one of the postconditions: this would cause
7089 -- confusing debug info to be produced, interfering with coverage
7090 -- analysis tools.
7092 Insert_Before_First_Source_Declaration (
7093 Make_Subprogram_Body (Loc,
7094 Specification =>
7095 Make_Procedure_Specification (Loc,
7096 Defining_Unit_Name => Proc_Id,
7097 Parameter_Specifications => Params),
7099 Declarations => Empty_List,
7100 Handled_Statement_Sequence =>
7101 Make_Handled_Sequence_Of_Statements (Loc,
7102 Statements => Stmts,
7103 End_Label => Make_Identifier (Loc, Chars (Proc_Id)))));
7105 -- Set the attributes of the related subprogram to capture the
7106 -- generated procedure.
7108 if Ekind_In (Subp_Id, E_Generic_Procedure, E_Procedure) then
7109 Set_Postcondition_Proc (Subp_Id, Proc_Id);
7110 end if;
7112 Set_Has_Postconditions (Subp_Id);
7113 end Build_Postconditions_Procedure;
7115 -----------------------------------
7116 -- Build_Pragma_Check_Equivalent --
7117 -----------------------------------
7119 function Build_Pragma_Check_Equivalent
7120 (Prag : Node_Id;
7121 Subp_Id : Entity_Id := Empty;
7122 Inher_Id : Entity_Id := Empty) return Node_Id
7124 Loc : constant Source_Ptr := Sloc (Prag);
7125 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
7126 Check_Prag : Node_Id;
7127 Formals_Map : Elist_Id;
7128 Inher_Formal : Entity_Id;
7129 Msg_Arg : Node_Id;
7130 Nam : Name_Id;
7131 Subp_Formal : Entity_Id;
7133 begin
7134 Formals_Map := No_Elist;
7136 -- When the pre- or postcondition is inherited, map the formals of
7137 -- the inherited subprogram to those of the current subprogram.
7139 if Present (Inher_Id) then
7140 pragma Assert (Present (Subp_Id));
7142 Formals_Map := New_Elmt_List;
7144 -- Create a relation <inherited formal> => <subprogram formal>
7146 Inher_Formal := First_Formal (Inher_Id);
7147 Subp_Formal := First_Formal (Subp_Id);
7148 while Present (Inher_Formal) and then Present (Subp_Formal) loop
7149 Append_Elmt (Inher_Formal, Formals_Map);
7150 Append_Elmt (Subp_Formal, Formals_Map);
7152 Next_Formal (Inher_Formal);
7153 Next_Formal (Subp_Formal);
7154 end loop;
7155 end if;
7157 -- Copy the original pragma while performing substitutions (if
7158 -- applicable).
7160 Check_Prag :=
7161 New_Copy_Tree
7162 (Source => Prag,
7163 Map => Formals_Map,
7164 New_Scope => Current_Scope);
7166 -- Mark the pragma as being internally generated and reset the
7167 -- Analyzed flag.
7169 Set_Comes_From_Source (Check_Prag, False);
7170 Set_Analyzed (Check_Prag, False);
7172 -- For a postcondition pragma within a generic, preserve the pragma
7173 -- for later expansion. This is also used when an error was detected,
7174 -- thus setting Expander_Active to False.
7176 if Prag_Nam = Name_Postcondition and then not Expander_Active then
7177 return Check_Prag;
7178 end if;
7180 if Present (Corresponding_Aspect (Prag)) then
7181 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
7182 else
7183 Nam := Prag_Nam;
7184 end if;
7186 -- Convert the copy into pragma Check by correcting the name and
7187 -- adding a check_kind argument.
7189 Set_Pragma_Identifier
7190 (Check_Prag, Make_Identifier (Loc, Name_Check));
7192 Prepend_To (Pragma_Argument_Associations (Check_Prag),
7193 Make_Pragma_Argument_Association (Loc,
7194 Expression => Make_Identifier (Loc, Nam)));
7196 -- Update the error message when the pragma is inherited
7198 if Present (Inher_Id) then
7199 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
7201 if Chars (Msg_Arg) = Name_Message then
7202 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
7204 -- Insert "inherited" to improve the error message
7206 if Name_Buffer (1 .. 8) = "failed p" then
7207 Insert_Str_In_Name_Buffer ("inherited ", 8);
7208 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
7209 end if;
7210 end if;
7211 end if;
7213 return Check_Prag;
7214 end Build_Pragma_Check_Equivalent;
7216 ---------------------------------
7217 -- Collect_Body_Postconditions --
7218 ---------------------------------
7220 procedure Collect_Body_Postconditions (Stmts : in out List_Id) is
7221 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id);
7222 -- Process all postconditions of the kind denoted by Post_Nam
7224 -----------------------------------------
7225 -- Collect_Body_Postconditions_Of_Kind --
7226 -----------------------------------------
7228 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id) is
7229 procedure Collect_Body_Postconditions_In_Decls
7230 (First_Decl : Node_Id);
7231 -- Process all postconditions found in a declarative list starting
7232 -- with declaration First_Decl.
7234 ------------------------------------------
7235 -- Collect_Body_Postconditions_In_Decls --
7236 ------------------------------------------
7238 procedure Collect_Body_Postconditions_In_Decls
7239 (First_Decl : Node_Id)
7241 Check_Prag : Node_Id;
7242 Decl : Node_Id;
7244 begin
7245 -- Inspect the declarative list looking for a pragma that
7246 -- matches the desired name.
7248 Decl := First_Decl;
7249 while Present (Decl) loop
7251 -- Note that non-matching pragmas are skipped
7253 if Nkind (Decl) = N_Pragma then
7254 if Pragma_Name (Decl) = Post_Nam then
7255 if not Analyzed (Decl) then
7256 Analyze (Decl);
7257 end if;
7259 Check_Prag := Build_Pragma_Check_Equivalent (Decl);
7261 if Expander_Active then
7262 Append_Enabled_Item
7263 (Item => Check_Prag,
7264 List => Stmts);
7266 -- If analyzing a generic unit, save pragma for later
7268 else
7269 Prepend_To_Declarations (Check_Prag);
7270 end if;
7271 end if;
7273 -- Skip internally generated code
7275 elsif not Comes_From_Source (Decl) then
7276 null;
7278 -- Postcondition pragmas are usually grouped together. There
7279 -- is no need to inspect the whole declarative list.
7281 else
7282 exit;
7283 end if;
7285 Next (Decl);
7286 end loop;
7287 end Collect_Body_Postconditions_In_Decls;
7289 -- Local variables
7291 Unit_Decl : constant Node_Id := Parent (N);
7293 -- Start of processing for Collect_Body_Postconditions_Of_Kind
7295 begin
7296 pragma Assert (Nam_In (Post_Nam, Name_Postcondition,
7297 Name_Refined_Post));
7299 -- Inspect the declarations of the subprogram body looking for a
7300 -- pragma that matches the desired name.
7302 Collect_Body_Postconditions_In_Decls
7303 (First_Decl => First (Declarations (N)));
7305 -- The subprogram body being processed is actually the proper body
7306 -- of a stub with a corresponding spec. The subprogram stub may
7307 -- carry a postcondition pragma in which case it must be taken
7308 -- into account. The pragma appears after the stub.
7310 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
7311 Collect_Body_Postconditions_In_Decls
7312 (First_Decl => Next (Corresponding_Stub (Unit_Decl)));
7313 end if;
7314 end Collect_Body_Postconditions_Of_Kind;
7316 -- Start of processing for Collect_Body_Postconditions
7318 begin
7319 Collect_Body_Postconditions_Of_Kind (Name_Refined_Post);
7320 Collect_Body_Postconditions_Of_Kind (Name_Postcondition);
7321 end Collect_Body_Postconditions;
7323 ---------------------------------
7324 -- Collect_Spec_Postconditions --
7325 ---------------------------------
7327 procedure Collect_Spec_Postconditions
7328 (Subp_Id : Entity_Id;
7329 Stmts : in out List_Id)
7331 Inher_Subps : constant Subprogram_List :=
7332 Inherited_Subprograms (Subp_Id);
7333 Check_Prag : Node_Id;
7334 Prag : Node_Id;
7335 Inher_Subp_Id : Entity_Id;
7337 begin
7338 -- Process the contract of the spec
7340 Prag := Pre_Post_Conditions (Contract (Subp_Id));
7341 while Present (Prag) loop
7342 if Pragma_Name (Prag) = Name_Postcondition then
7343 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
7345 if Expander_Active then
7346 Append_Enabled_Item
7347 (Item => Check_Prag,
7348 List => Stmts);
7350 -- When analyzing a generic unit, save the pragma for later
7352 else
7353 Prepend_To_Declarations (Check_Prag);
7354 end if;
7355 end if;
7357 Prag := Next_Pragma (Prag);
7358 end loop;
7360 -- Process the contracts of all inherited subprograms, looking for
7361 -- class-wide postconditions.
7363 for Index in Inher_Subps'Range loop
7364 Inher_Subp_Id := Inher_Subps (Index);
7366 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
7367 while Present (Prag) loop
7368 if Pragma_Name (Prag) = Name_Postcondition
7369 and then Class_Present (Prag)
7370 then
7371 Check_Prag :=
7372 Build_Pragma_Check_Equivalent
7373 (Prag => Prag,
7374 Subp_Id => Subp_Id,
7375 Inher_Id => Inher_Subp_Id);
7377 if Expander_Active then
7378 Append_Enabled_Item
7379 (Item => Check_Prag,
7380 List => Stmts);
7382 -- When analyzing a generic unit, save the pragma for later
7384 else
7385 Prepend_To_Declarations (Check_Prag);
7386 end if;
7387 end if;
7389 Prag := Next_Pragma (Prag);
7390 end loop;
7391 end loop;
7392 end Collect_Spec_Postconditions;
7394 --------------------------------
7395 -- Collect_Spec_Preconditions --
7396 --------------------------------
7398 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id) is
7399 Class_Pre : Node_Id := Empty;
7400 -- The sole class-wide precondition pragma that applies to the
7401 -- subprogram.
7403 procedure Add_Or_Save_Precondition (Prag : Node_Id);
7404 -- Save a class-wide precondition or add a regulat precondition to
7405 -- the declarative list of the body.
7407 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
7408 -- Merge two class-wide preconditions by "or else"-ing them. The
7409 -- changes are accumulated in parameter Into. Update the error
7410 -- message of Into.
7412 ------------------------------
7413 -- Add_Or_Save_Precondition --
7414 ------------------------------
7416 procedure Add_Or_Save_Precondition (Prag : Node_Id) is
7417 Check_Prag : Node_Id;
7419 begin
7420 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
7422 -- Save the sole class-wide precondition (if any) for the next
7423 -- step where it will be merged with inherited preconditions.
7425 if Class_Present (Prag) then
7426 pragma Assert (No (Class_Pre));
7427 Class_Pre := Check_Prag;
7429 -- Accumulate the corresponding Check pragmas to the top of the
7430 -- declarations. Prepending the items ensures that they will be
7431 -- evaluated in their original order.
7433 else
7434 Prepend_To_Declarations (Check_Prag);
7435 end if;
7436 end Add_Or_Save_Precondition;
7438 -------------------------
7439 -- Merge_Preconditions --
7440 -------------------------
7442 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
7443 function Expression_Arg (Prag : Node_Id) return Node_Id;
7444 -- Return the boolean expression argument of a precondition while
7445 -- updating its parenteses count for the subsequent merge.
7447 function Message_Arg (Prag : Node_Id) return Node_Id;
7448 -- Return the message argument of a precondition
7450 --------------------
7451 -- Expression_Arg --
7452 --------------------
7454 function Expression_Arg (Prag : Node_Id) return Node_Id is
7455 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7456 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
7458 begin
7459 if Paren_Count (Arg) = 0 then
7460 Set_Paren_Count (Arg, 1);
7461 end if;
7463 return Arg;
7464 end Expression_Arg;
7466 -----------------
7467 -- Message_Arg --
7468 -----------------
7470 function Message_Arg (Prag : Node_Id) return Node_Id is
7471 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7472 begin
7473 return Get_Pragma_Arg (Last (Args));
7474 end Message_Arg;
7476 -- Local variables
7478 From_Expr : constant Node_Id := Expression_Arg (From);
7479 From_Msg : constant Node_Id := Message_Arg (From);
7480 Into_Expr : constant Node_Id := Expression_Arg (Into);
7481 Into_Msg : constant Node_Id := Message_Arg (Into);
7482 Loc : constant Source_Ptr := Sloc (Into);
7484 -- Start of processing for Merge_Preconditions
7486 begin
7487 -- Merge the two preconditions by "or else"-ing them
7489 Rewrite (Into_Expr,
7490 Make_Or_Else (Loc,
7491 Right_Opnd => Relocate_Node (Into_Expr),
7492 Left_Opnd => From_Expr));
7494 -- Merge the two error messages to produce a single message of the
7495 -- form:
7497 -- failed precondition from ...
7498 -- also failed inherited precondition from ...
7500 if not Exception_Locations_Suppressed then
7501 Start_String (Strval (Into_Msg));
7502 Store_String_Char (ASCII.LF);
7503 Store_String_Chars (" also ");
7504 Store_String_Chars (Strval (From_Msg));
7506 Set_Strval (Into_Msg, End_String);
7507 end if;
7508 end Merge_Preconditions;
7510 -- Local variables
7512 Inher_Subps : constant Subprogram_List :=
7513 Inherited_Subprograms (Subp_Id);
7514 Subp_Decl : constant Node_Id := Parent (Parent (Subp_Id));
7515 Check_Prag : Node_Id;
7516 Decl : Node_Id;
7517 Inher_Subp_Id : Entity_Id;
7518 Prag : Node_Id;
7520 -- Start of processing for Collect_Spec_Preconditions
7522 begin
7523 -- Process the contract of the spec
7525 Prag := Pre_Post_Conditions (Contract (Subp_Id));
7526 while Present (Prag) loop
7527 if Pragma_Name (Prag) = Name_Precondition then
7528 Add_Or_Save_Precondition (Prag);
7529 end if;
7531 Prag := Next_Pragma (Prag);
7532 end loop;
7534 -- The subprogram declaration being processed is actually a body
7535 -- stub. The stub may carry a precondition pragma in which case it
7536 -- must be taken into account. The pragma appears after the stub.
7538 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
7540 -- Inspect the declarations following the body stub
7542 Decl := Next (Subp_Decl);
7543 while Present (Decl) loop
7545 -- Note that non-matching pragmas are skipped
7547 if Nkind (Decl) = N_Pragma then
7548 if Pragma_Name (Decl) = Name_Precondition then
7549 if not Analyzed (Decl) then
7550 Analyze (Decl);
7551 end if;
7553 Add_Or_Save_Precondition (Decl);
7554 end if;
7556 -- Skip internally generated code
7558 elsif not Comes_From_Source (Decl) then
7559 null;
7561 -- Preconditions are usually grouped together. There is no need
7562 -- to inspect the whole declarative list.
7564 else
7565 exit;
7566 end if;
7568 Next (Decl);
7569 end loop;
7570 end if;
7572 -- Process the contracts of all inherited subprograms, looking for
7573 -- class-wide preconditions.
7575 for Index in Inher_Subps'Range loop
7576 Inher_Subp_Id := Inher_Subps (Index);
7578 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
7579 while Present (Prag) loop
7580 if Pragma_Name (Prag) = Name_Precondition
7581 and then Class_Present (Prag)
7582 then
7583 Check_Prag :=
7584 Build_Pragma_Check_Equivalent
7585 (Prag => Prag,
7586 Subp_Id => Subp_Id,
7587 Inher_Id => Inher_Subp_Id);
7589 -- The spec or an inherited subprogram already yielded a
7590 -- class-wide precondition. Merge the existing precondition
7591 -- with the current one using "or else".
7593 if Present (Class_Pre) then
7594 Merge_Preconditions (Check_Prag, Class_Pre);
7595 else
7596 Class_Pre := Check_Prag;
7597 end if;
7598 end if;
7600 Prag := Next_Pragma (Prag);
7601 end loop;
7602 end loop;
7604 -- Add the merged class-wide preconditions (if any)
7606 if Present (Class_Pre) then
7607 Prepend_To_Declarations (Class_Pre);
7608 end if;
7609 end Collect_Spec_Preconditions;
7611 -----------------------------
7612 -- Prepend_To_Declarations --
7613 -----------------------------
7615 procedure Prepend_To_Declarations (Item : Node_Id) is
7616 Decls : List_Id := Declarations (N);
7618 begin
7619 -- Ensure that the body has a declarative list
7621 if No (Decls) then
7622 Decls := New_List;
7623 Set_Declarations (N, Decls);
7624 end if;
7626 Prepend_To (Decls, Item);
7627 end Prepend_To_Declarations;
7629 ----------------------------
7630 -- Process_Contract_Cases --
7631 ----------------------------
7633 procedure Process_Contract_Cases
7634 (Subp_Id : Entity_Id;
7635 Stmts : in out List_Id)
7637 Prag : Node_Id;
7639 begin
7640 -- Do not build the Contract_Cases circuitry if no code is being
7641 -- generated.
7643 if not Expander_Active then
7644 return;
7645 end if;
7647 Prag := Contract_Test_Cases (Contract (Subp_Id));
7648 while Present (Prag) loop
7649 if Pragma_Name (Prag) = Name_Contract_Cases then
7650 Expand_Contract_Cases
7651 (CCs => Prag,
7652 Subp_Id => Subp_Id,
7653 Decls => Declarations (N),
7654 Stmts => Stmts);
7655 end if;
7657 Prag := Next_Pragma (Prag);
7658 end loop;
7659 end Process_Contract_Cases;
7661 -- Local variables
7663 Post_Stmts : List_Id := No_List;
7664 Result : Entity_Id;
7665 Subp_Id : Entity_Id;
7667 -- Start of processing for Expand_Subprogram_Contract
7669 begin
7670 if Present (Spec_Id) then
7671 Subp_Id := Spec_Id;
7672 else
7673 Subp_Id := Body_Id;
7674 end if;
7676 -- Do not process a predicate function as its body will end up with a
7677 -- recursive call to itself and blow up the stack.
7679 if Ekind (Subp_Id) = E_Function
7680 and then Is_Predicate_Function (Subp_Id)
7681 then
7682 return;
7684 -- Do not process TSS subprograms
7686 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
7687 return;
7688 end if;
7690 -- The expansion of a subprogram contract involves the relocation of
7691 -- various contract assertions to the declarations of the body in a
7692 -- particular order. The order is as follows:
7694 -- function Example (...) return ... is
7695 -- procedure _Postconditions (...) is
7696 -- begin
7697 -- <refined postconditions from body>
7698 -- <postconditions from body>
7699 -- <postconditions from spec>
7700 -- <inherited postconditions>
7701 -- <contract case consequences>
7702 -- <invariant check of function result (if applicable)>
7703 -- <invariant and predicate checks of parameters>
7704 -- end _Postconditions;
7706 -- <inherited preconditions>
7707 -- <preconditions from spec>
7708 -- <preconditions from body>
7709 -- <refined preconditions from body>
7710 -- <contract case conditions>
7712 -- <source declarations>
7713 -- begin
7714 -- <source statements>
7716 -- _Preconditions (Result);
7717 -- return Result;
7718 -- end Example;
7720 -- Routine _Postconditions holds all contract assertions that must be
7721 -- verified on exit from the related routine.
7723 -- Collect all [inherited] preconditions from the spec, transform them
7724 -- into Check pragmas and add them to the declarations of the body in
7725 -- the order outlined above.
7727 if Present (Spec_Id) then
7728 Collect_Spec_Preconditions (Spec_Id);
7729 end if;
7731 -- Transform all [refined] postconditions of the body into Check
7732 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
7734 Collect_Body_Postconditions (Post_Stmts);
7736 -- Transform all [inherited] postconditions from the spec into Check
7737 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
7739 if Present (Spec_Id) then
7740 Collect_Spec_Postconditions (Spec_Id, Post_Stmts);
7742 -- Transform pragma Contract_Cases from the spec into its circuitry
7744 Process_Contract_Cases (Spec_Id, Post_Stmts);
7745 end if;
7747 -- Apply invariant and predicate checks on the result of a function (if
7748 -- applicable) and all formals. The resulting checks are accumulated in
7749 -- list Post_Stmts.
7751 Add_Invariant_And_Predicate_Checks (Subp_Id, Post_Stmts, Result);
7753 -- Construct procedure _Postconditions
7755 Build_Postconditions_Procedure (Subp_Id, Post_Stmts, Result);
7756 end Expand_Subprogram_Contract;
7758 --------------------------------
7759 -- Is_Build_In_Place_Function --
7760 --------------------------------
7762 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7763 begin
7764 -- This function is called from Expand_Subtype_From_Expr during
7765 -- semantic analysis, even when expansion is off. In those cases
7766 -- the build_in_place expansion will not take place.
7768 if not Expander_Active then
7769 return False;
7770 end if;
7772 -- For now we test whether E denotes a function or access-to-function
7773 -- type whose result subtype is inherently limited. Later this test
7774 -- may be revised to allow composite nonlimited types. Functions with
7775 -- a foreign convention or whose result type has a foreign convention
7776 -- never qualify.
7778 if Ekind_In (E, E_Function, E_Generic_Function)
7779 or else (Ekind (E) = E_Subprogram_Type
7780 and then Etype (E) /= Standard_Void_Type)
7781 then
7782 -- Note: If the function has a foreign convention, it cannot build
7783 -- its result in place, so you're on your own. On the other hand,
7784 -- if only the return type has a foreign convention, its layout is
7785 -- intended to be compatible with the other language, but the build-
7786 -- in place machinery can ensure that the object is not copied.
7788 if Has_Foreign_Convention (E) then
7789 return False;
7791 -- In Ada 2005 all functions with an inherently limited return type
7792 -- must be handled using a build-in-place profile, including the case
7793 -- of a function with a limited interface result, where the function
7794 -- may return objects of nonlimited descendants.
7796 else
7797 return Is_Limited_View (Etype (E))
7798 and then Ada_Version >= Ada_2005
7799 and then not Debug_Flag_Dot_L;
7800 end if;
7802 else
7803 return False;
7804 end if;
7805 end Is_Build_In_Place_Function;
7807 -------------------------------------
7808 -- Is_Build_In_Place_Function_Call --
7809 -------------------------------------
7811 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7812 Exp_Node : Node_Id := N;
7813 Function_Id : Entity_Id;
7815 begin
7816 -- Return False if the expander is currently inactive, since awareness
7817 -- of build-in-place treatment is only relevant during expansion. Note
7818 -- that Is_Build_In_Place_Function, which is called as part of this
7819 -- function, is also conditioned this way, but we need to check here as
7820 -- well to avoid blowing up on processing protected calls when expansion
7821 -- is disabled (such as with -gnatc) since those would trip over the
7822 -- raise of Program_Error below.
7824 -- In SPARK mode, build-in-place calls are not expanded, so that we
7825 -- may end up with a call that is neither resolved to an entity, nor
7826 -- an indirect call.
7828 if not Expander_Active then
7829 return False;
7830 end if;
7832 -- Step past qualification or unchecked conversion (the latter can occur
7833 -- in cases of calls to 'Input).
7835 if Nkind_In (Exp_Node, N_Qualified_Expression,
7836 N_Unchecked_Type_Conversion)
7837 then
7838 Exp_Node := Expression (N);
7839 end if;
7841 if Nkind (Exp_Node) /= N_Function_Call then
7842 return False;
7844 else
7845 if Is_Entity_Name (Name (Exp_Node)) then
7846 Function_Id := Entity (Name (Exp_Node));
7848 -- In the case of an explicitly dereferenced call, use the subprogram
7849 -- type generated for the dereference.
7851 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7852 Function_Id := Etype (Name (Exp_Node));
7854 -- This may be a call to a protected function.
7856 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7857 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7859 else
7860 raise Program_Error;
7861 end if;
7863 return Is_Build_In_Place_Function (Function_Id);
7864 end if;
7865 end Is_Build_In_Place_Function_Call;
7867 -----------------------
7868 -- Freeze_Subprogram --
7869 -----------------------
7871 procedure Freeze_Subprogram (N : Node_Id) is
7872 Loc : constant Source_Ptr := Sloc (N);
7874 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7875 -- (Ada 2005): Register a predefined primitive in all the secondary
7876 -- dispatch tables of its primitive type.
7878 ----------------------------------
7879 -- Register_Predefined_DT_Entry --
7880 ----------------------------------
7882 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7883 Iface_DT_Ptr : Elmt_Id;
7884 Tagged_Typ : Entity_Id;
7885 Thunk_Id : Entity_Id;
7886 Thunk_Code : Node_Id;
7888 begin
7889 Tagged_Typ := Find_Dispatching_Type (Prim);
7891 if No (Access_Disp_Table (Tagged_Typ))
7892 or else not Has_Interfaces (Tagged_Typ)
7893 or else not RTE_Available (RE_Interface_Tag)
7894 or else Restriction_Active (No_Dispatching_Calls)
7895 then
7896 return;
7897 end if;
7899 -- Skip the first two access-to-dispatch-table pointers since they
7900 -- leads to the primary dispatch table (predefined DT and user
7901 -- defined DT). We are only concerned with the secondary dispatch
7902 -- table pointers. Note that the access-to- dispatch-table pointer
7903 -- corresponds to the first implemented interface retrieved below.
7905 Iface_DT_Ptr :=
7906 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7908 while Present (Iface_DT_Ptr)
7909 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7910 loop
7911 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7912 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7914 if Present (Thunk_Code) then
7915 Insert_Actions_After (N, New_List (
7916 Thunk_Code,
7918 Build_Set_Predefined_Prim_Op_Address (Loc,
7919 Tag_Node =>
7920 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7921 Position => DT_Position (Prim),
7922 Address_Node =>
7923 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7924 Make_Attribute_Reference (Loc,
7925 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7926 Attribute_Name => Name_Unrestricted_Access))),
7928 Build_Set_Predefined_Prim_Op_Address (Loc,
7929 Tag_Node =>
7930 New_Occurrence_Of
7931 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7932 Loc),
7933 Position => DT_Position (Prim),
7934 Address_Node =>
7935 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7936 Make_Attribute_Reference (Loc,
7937 Prefix => New_Occurrence_Of (Prim, Loc),
7938 Attribute_Name => Name_Unrestricted_Access)))));
7939 end if;
7941 -- Skip the tag of the predefined primitives dispatch table
7943 Next_Elmt (Iface_DT_Ptr);
7944 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7946 -- Skip tag of the no-thunks dispatch table
7948 Next_Elmt (Iface_DT_Ptr);
7949 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7951 -- Skip tag of predefined primitives no-thunks dispatch table
7953 Next_Elmt (Iface_DT_Ptr);
7954 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7956 Next_Elmt (Iface_DT_Ptr);
7957 end loop;
7958 end Register_Predefined_DT_Entry;
7960 -- Local variables
7962 Subp : constant Entity_Id := Entity (N);
7964 -- Start of processing for Freeze_Subprogram
7966 begin
7967 -- We suppress the initialization of the dispatch table entry when
7968 -- VM_Target because the dispatching mechanism is handled internally
7969 -- by the VM.
7971 if Is_Dispatching_Operation (Subp)
7972 and then not Is_Abstract_Subprogram (Subp)
7973 and then Present (DTC_Entity (Subp))
7974 and then Present (Scope (DTC_Entity (Subp)))
7975 and then Tagged_Type_Expansion
7976 and then not Restriction_Active (No_Dispatching_Calls)
7977 and then RTE_Available (RE_Tag)
7978 then
7979 declare
7980 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7982 begin
7983 -- Handle private overridden primitives
7985 if not Is_CPP_Class (Typ) then
7986 Check_Overriding_Operation (Subp);
7987 end if;
7989 -- We assume that imported CPP primitives correspond with objects
7990 -- whose constructor is in the CPP side; therefore we don't need
7991 -- to generate code to register them in the dispatch table.
7993 if Is_CPP_Class (Typ) then
7994 null;
7996 -- Handle CPP primitives found in derivations of CPP_Class types.
7997 -- These primitives must have been inherited from some parent, and
7998 -- there is no need to register them in the dispatch table because
7999 -- Build_Inherit_Prims takes care of initializing these slots.
8001 elsif Is_Imported (Subp)
8002 and then (Convention (Subp) = Convention_CPP
8003 or else Convention (Subp) = Convention_C)
8004 then
8005 null;
8007 -- Generate code to register the primitive in non statically
8008 -- allocated dispatch tables
8010 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8012 -- When a primitive is frozen, enter its name in its dispatch
8013 -- table slot.
8015 if not Is_Interface (Typ)
8016 or else Present (Interface_Alias (Subp))
8017 then
8018 if Is_Predefined_Dispatching_Operation (Subp) then
8019 Register_Predefined_DT_Entry (Subp);
8020 end if;
8022 Insert_Actions_After (N,
8023 Register_Primitive (Loc, Prim => Subp));
8024 end if;
8025 end if;
8026 end;
8027 end if;
8029 -- Mark functions that return by reference. Note that it cannot be part
8030 -- of the normal semantic analysis of the spec since the underlying
8031 -- returned type may not be known yet (for private types).
8033 declare
8034 Typ : constant Entity_Id := Etype (Subp);
8035 Utyp : constant Entity_Id := Underlying_Type (Typ);
8036 begin
8037 if Is_Limited_View (Typ) then
8038 Set_Returns_By_Ref (Subp);
8039 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8040 Set_Returns_By_Ref (Subp);
8041 end if;
8042 end;
8044 -- Wnen freezing a null procedure, analyze its delayed aspects now
8045 -- because we may not have reached the end of the declarative list when
8046 -- delayed aspects are normally analyzed. This ensures that dispatching
8047 -- calls are properly rewritten when the generated _Postcondition
8048 -- procedure is analyzed in the null procedure body.
8050 if Nkind (Parent (Subp)) = N_Procedure_Specification
8051 and then Null_Present (Parent (Subp))
8052 then
8053 Analyze_Subprogram_Contract (Subp);
8054 end if;
8055 end Freeze_Subprogram;
8057 -----------------------
8058 -- Is_Null_Procedure --
8059 -----------------------
8061 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8062 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8064 begin
8065 if Ekind (Subp) /= E_Procedure then
8066 return False;
8068 -- Check if this is a declared null procedure
8070 elsif Nkind (Decl) = N_Subprogram_Declaration then
8071 if not Null_Present (Specification (Decl)) then
8072 return False;
8074 elsif No (Body_To_Inline (Decl)) then
8075 return False;
8077 -- Check if the body contains only a null statement, followed by
8078 -- the return statement added during expansion.
8080 else
8081 declare
8082 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8084 Stat : Node_Id;
8085 Stat2 : Node_Id;
8087 begin
8088 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8089 return False;
8090 else
8091 -- We must skip SCIL nodes because they are currently
8092 -- implemented as special N_Null_Statement nodes.
8094 Stat :=
8095 First_Non_SCIL_Node
8096 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8097 Stat2 := Next_Non_SCIL_Node (Stat);
8099 return
8100 Is_Empty_List (Declarations (Orig_Bod))
8101 and then Nkind (Stat) = N_Null_Statement
8102 and then
8103 (No (Stat2)
8104 or else
8105 (Nkind (Stat2) = N_Simple_Return_Statement
8106 and then No (Next (Stat2))));
8107 end if;
8108 end;
8109 end if;
8111 else
8112 return False;
8113 end if;
8114 end Is_Null_Procedure;
8116 -------------------------------------------
8117 -- Make_Build_In_Place_Call_In_Allocator --
8118 -------------------------------------------
8120 procedure Make_Build_In_Place_Call_In_Allocator
8121 (Allocator : Node_Id;
8122 Function_Call : Node_Id)
8124 Acc_Type : constant Entity_Id := Etype (Allocator);
8125 Loc : Source_Ptr;
8126 Func_Call : Node_Id := Function_Call;
8127 Ref_Func_Call : Node_Id;
8128 Function_Id : Entity_Id;
8129 Result_Subt : Entity_Id;
8130 New_Allocator : Node_Id;
8131 Return_Obj_Access : Entity_Id; -- temp for function result
8132 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8133 Alloc_Form : BIP_Allocation_Form;
8134 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8135 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8136 Chain : Entity_Id; -- activation chain, in case of tasks
8138 begin
8139 -- Step past qualification or unchecked conversion (the latter can occur
8140 -- in cases of calls to 'Input).
8142 if Nkind_In (Func_Call,
8143 N_Qualified_Expression,
8144 N_Unchecked_Type_Conversion)
8145 then
8146 Func_Call := Expression (Func_Call);
8147 end if;
8149 -- If the call has already been processed to add build-in-place actuals
8150 -- then return. This should not normally occur in an allocator context,
8151 -- but we add the protection as a defensive measure.
8153 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8154 return;
8155 end if;
8157 -- Mark the call as processed as a build-in-place call
8159 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8161 Loc := Sloc (Function_Call);
8163 if Is_Entity_Name (Name (Func_Call)) then
8164 Function_Id := Entity (Name (Func_Call));
8166 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8167 Function_Id := Etype (Name (Func_Call));
8169 else
8170 raise Program_Error;
8171 end if;
8173 Result_Subt := Available_View (Etype (Function_Id));
8175 -- Create a temp for the function result. In the caller-allocates case,
8176 -- this will be initialized to the result of a new uninitialized
8177 -- allocator. Note: we do not use Allocator as the Related_Node of
8178 -- Return_Obj_Access in call to Make_Temporary below as this would
8179 -- create a sort of infinite "recursion".
8181 Return_Obj_Access := Make_Temporary (Loc, 'R');
8182 Set_Etype (Return_Obj_Access, Acc_Type);
8184 -- When the result subtype is constrained, the return object is
8185 -- allocated on the caller side, and access to it is passed to the
8186 -- function.
8188 -- Here and in related routines, we must examine the full view of the
8189 -- type, because the view at the point of call may differ from that
8190 -- that in the function body, and the expansion mechanism depends on
8191 -- the characteristics of the full view.
8193 if Is_Constrained (Underlying_Type (Result_Subt)) then
8195 -- Replace the initialized allocator of form "new T'(Func (...))"
8196 -- with an uninitialized allocator of form "new T", where T is the
8197 -- result subtype of the called function. The call to the function
8198 -- is handled separately further below.
8200 New_Allocator :=
8201 Make_Allocator (Loc,
8202 Expression => New_Occurrence_Of (Result_Subt, Loc));
8203 Set_No_Initialization (New_Allocator);
8205 -- Copy attributes to new allocator. Note that the new allocator
8206 -- logically comes from source if the original one did, so copy the
8207 -- relevant flag. This ensures proper treatment of the restriction
8208 -- No_Implicit_Heap_Allocations in this case.
8210 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8211 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8212 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8214 Rewrite (Allocator, New_Allocator);
8216 -- Initial value of the temp is the result of the uninitialized
8217 -- allocator
8219 Temp_Init := Relocate_Node (Allocator);
8221 -- Indicate that caller allocates, and pass in the return object
8223 Alloc_Form := Caller_Allocation;
8224 Pool := Make_Null (No_Location);
8225 Return_Obj_Actual :=
8226 Make_Unchecked_Type_Conversion (Loc,
8227 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8228 Expression =>
8229 Make_Explicit_Dereference (Loc,
8230 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8232 -- When the result subtype is unconstrained, the function itself must
8233 -- perform the allocation of the return object, so we pass parameters
8234 -- indicating that.
8236 else
8237 Temp_Init := Empty;
8239 -- Case of a user-defined storage pool. Pass an allocation parameter
8240 -- indicating that the function should allocate its result in the
8241 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8242 -- pool may not be aliased.
8244 if VM_Target = No_VM
8245 and then Present (Associated_Storage_Pool (Acc_Type))
8246 then
8247 Alloc_Form := User_Storage_Pool;
8248 Pool :=
8249 Make_Attribute_Reference (Loc,
8250 Prefix =>
8251 New_Occurrence_Of
8252 (Associated_Storage_Pool (Acc_Type), Loc),
8253 Attribute_Name => Name_Unrestricted_Access);
8255 -- No user-defined pool; pass an allocation parameter indicating that
8256 -- the function should allocate its result on the heap.
8258 else
8259 Alloc_Form := Global_Heap;
8260 Pool := Make_Null (No_Location);
8261 end if;
8263 -- The caller does not provide the return object in this case, so we
8264 -- have to pass null for the object access actual.
8266 Return_Obj_Actual := Empty;
8267 end if;
8269 -- Declare the temp object
8271 Insert_Action (Allocator,
8272 Make_Object_Declaration (Loc,
8273 Defining_Identifier => Return_Obj_Access,
8274 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8275 Expression => Temp_Init));
8277 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8279 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8280 -- then generate an implicit conversion to force displacement of the
8281 -- "this" pointer.
8283 if Is_Interface (Designated_Type (Acc_Type)) then
8284 Rewrite
8285 (Ref_Func_Call,
8286 OK_Convert_To (Acc_Type, Ref_Func_Call));
8287 end if;
8289 declare
8290 Assign : constant Node_Id :=
8291 Make_Assignment_Statement (Loc,
8292 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8293 Expression => Ref_Func_Call);
8294 -- Assign the result of the function call into the temp. In the
8295 -- caller-allocates case, this is overwriting the temp with its
8296 -- initial value, which has no effect. In the callee-allocates case,
8297 -- this is setting the temp to point to the object allocated by the
8298 -- callee.
8300 Actions : List_Id;
8301 -- Actions to be inserted. If there are no tasks, this is just the
8302 -- assignment statement. If the allocated object has tasks, we need
8303 -- to wrap the assignment in a block that activates them. The
8304 -- activation chain of that block must be passed to the function,
8305 -- rather than some outer chain.
8306 begin
8307 if Has_Task (Result_Subt) then
8308 Actions := New_List;
8309 Build_Task_Allocate_Block_With_Init_Stmts
8310 (Actions, Allocator, Init_Stmts => New_List (Assign));
8311 Chain := Activation_Chain_Entity (Last (Actions));
8312 else
8313 Actions := New_List (Assign);
8314 Chain := Empty;
8315 end if;
8317 Insert_Actions (Allocator, Actions);
8318 end;
8320 -- When the function has a controlling result, an allocation-form
8321 -- parameter must be passed indicating that the caller is allocating
8322 -- the result object. This is needed because such a function can be
8323 -- called as a dispatching operation and must be treated similarly
8324 -- to functions with unconstrained result subtypes.
8326 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8327 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8329 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8330 (Func_Call, Function_Id, Acc_Type);
8332 Add_Task_Actuals_To_Build_In_Place_Call
8333 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8334 Chain => Chain);
8336 -- Add an implicit actual to the function call that provides access
8337 -- to the allocated object. An unchecked conversion to the (specific)
8338 -- result subtype of the function is inserted to handle cases where
8339 -- the access type of the allocator has a class-wide designated type.
8341 Add_Access_Actual_To_Build_In_Place_Call
8342 (Func_Call, Function_Id, Return_Obj_Actual);
8344 -- If the build-in-place function call returns a controlled object,
8345 -- the finalization master will require a reference to routine
8346 -- Finalize_Address of the designated type. Setting this attribute
8347 -- is done in the same manner to expansion of allocators.
8349 if Needs_Finalization (Result_Subt) then
8351 -- Controlled types with supressed finalization do not need to
8352 -- associate the address of their Finalize_Address primitives with
8353 -- a master since they do not need a master to begin with.
8355 if Is_Library_Level_Entity (Acc_Type)
8356 and then Finalize_Storage_Only (Result_Subt)
8357 then
8358 null;
8360 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
8361 -- because Finalize_Address is never built.
8363 elsif not CodePeer_Mode then
8364 Insert_Action (Allocator,
8365 Make_Set_Finalize_Address_Call (Loc,
8366 Typ => Etype (Function_Id),
8367 Ptr_Typ => Acc_Type));
8368 end if;
8369 end if;
8371 -- Finally, replace the allocator node with a reference to the temp
8373 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8375 Analyze_And_Resolve (Allocator, Acc_Type);
8376 end Make_Build_In_Place_Call_In_Allocator;
8378 ---------------------------------------------------
8379 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8380 ---------------------------------------------------
8382 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8383 (Function_Call : Node_Id)
8385 Loc : Source_Ptr;
8386 Func_Call : Node_Id := Function_Call;
8387 Function_Id : Entity_Id;
8388 Result_Subt : Entity_Id;
8389 Return_Obj_Id : Entity_Id;
8390 Return_Obj_Decl : Entity_Id;
8392 begin
8393 -- Step past qualification or unchecked conversion (the latter can occur
8394 -- in cases of calls to 'Input).
8396 if Nkind_In (Func_Call, N_Qualified_Expression,
8397 N_Unchecked_Type_Conversion)
8398 then
8399 Func_Call := Expression (Func_Call);
8400 end if;
8402 -- If the call has already been processed to add build-in-place actuals
8403 -- then return. One place this can occur is for calls to build-in-place
8404 -- functions that occur within a call to a protected operation, where
8405 -- due to rewriting and expansion of the protected call there can be
8406 -- more than one call to Expand_Actuals for the same set of actuals.
8408 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8409 return;
8410 end if;
8412 -- Mark the call as processed as a build-in-place call
8414 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8416 Loc := Sloc (Function_Call);
8418 if Is_Entity_Name (Name (Func_Call)) then
8419 Function_Id := Entity (Name (Func_Call));
8421 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8422 Function_Id := Etype (Name (Func_Call));
8424 else
8425 raise Program_Error;
8426 end if;
8428 Result_Subt := Etype (Function_Id);
8430 -- If the build-in-place function returns a controlled object, then the
8431 -- object needs to be finalized immediately after the context. Since
8432 -- this case produces a transient scope, the servicing finalizer needs
8433 -- to name the returned object. Create a temporary which is initialized
8434 -- with the function call:
8436 -- Temp_Id : Func_Type := BIP_Func_Call;
8438 -- The initialization expression of the temporary will be rewritten by
8439 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8440 -- Call_In_Object_Declaration.
8442 if Needs_Finalization (Result_Subt) then
8443 declare
8444 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8445 Temp_Decl : Node_Id;
8447 begin
8448 -- Reset the guard on the function call since the following does
8449 -- not perform actual call expansion.
8451 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8453 Temp_Decl :=
8454 Make_Object_Declaration (Loc,
8455 Defining_Identifier => Temp_Id,
8456 Object_Definition =>
8457 New_Occurrence_Of (Result_Subt, Loc),
8458 Expression =>
8459 New_Copy_Tree (Function_Call));
8461 Insert_Action (Function_Call, Temp_Decl);
8463 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8464 Analyze (Function_Call);
8465 end;
8467 -- When the result subtype is constrained, an object of the subtype is
8468 -- declared and an access value designating it is passed as an actual.
8470 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8472 -- Create a temporary object to hold the function result
8474 Return_Obj_Id := Make_Temporary (Loc, 'R');
8475 Set_Etype (Return_Obj_Id, Result_Subt);
8477 Return_Obj_Decl :=
8478 Make_Object_Declaration (Loc,
8479 Defining_Identifier => Return_Obj_Id,
8480 Aliased_Present => True,
8481 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8483 Set_No_Initialization (Return_Obj_Decl);
8485 Insert_Action (Func_Call, Return_Obj_Decl);
8487 -- When the function has a controlling result, an allocation-form
8488 -- parameter must be passed indicating that the caller is allocating
8489 -- the result object. This is needed because such a function can be
8490 -- called as a dispatching operation and must be treated similarly
8491 -- to functions with unconstrained result subtypes.
8493 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8494 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8496 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8497 (Func_Call, Function_Id);
8499 Add_Task_Actuals_To_Build_In_Place_Call
8500 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8502 -- Add an implicit actual to the function call that provides access
8503 -- to the caller's return object.
8505 Add_Access_Actual_To_Build_In_Place_Call
8506 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8508 -- When the result subtype is unconstrained, the function must allocate
8509 -- the return object in the secondary stack, so appropriate implicit
8510 -- parameters are added to the call to indicate that. A transient
8511 -- scope is established to ensure eventual cleanup of the result.
8513 else
8514 -- Pass an allocation parameter indicating that the function should
8515 -- allocate its result on the secondary stack.
8517 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8518 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8520 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8521 (Func_Call, Function_Id);
8523 Add_Task_Actuals_To_Build_In_Place_Call
8524 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8526 -- Pass a null value to the function since no return object is
8527 -- available on the caller side.
8529 Add_Access_Actual_To_Build_In_Place_Call
8530 (Func_Call, Function_Id, Empty);
8531 end if;
8532 end Make_Build_In_Place_Call_In_Anonymous_Context;
8534 --------------------------------------------
8535 -- Make_Build_In_Place_Call_In_Assignment --
8536 --------------------------------------------
8538 procedure Make_Build_In_Place_Call_In_Assignment
8539 (Assign : Node_Id;
8540 Function_Call : Node_Id)
8542 Lhs : constant Node_Id := Name (Assign);
8543 Func_Call : Node_Id := Function_Call;
8544 Func_Id : Entity_Id;
8545 Loc : Source_Ptr;
8546 Obj_Decl : Node_Id;
8547 Obj_Id : Entity_Id;
8548 Ptr_Typ : Entity_Id;
8549 Ptr_Typ_Decl : Node_Id;
8550 New_Expr : Node_Id;
8551 Result_Subt : Entity_Id;
8552 Target : Node_Id;
8554 begin
8555 -- Step past qualification or unchecked conversion (the latter can occur
8556 -- in cases of calls to 'Input).
8558 if Nkind_In (Func_Call, N_Qualified_Expression,
8559 N_Unchecked_Type_Conversion)
8560 then
8561 Func_Call := Expression (Func_Call);
8562 end if;
8564 -- If the call has already been processed to add build-in-place actuals
8565 -- then return. This should not normally occur in an assignment context,
8566 -- but we add the protection as a defensive measure.
8568 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8569 return;
8570 end if;
8572 -- Mark the call as processed as a build-in-place call
8574 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8576 Loc := Sloc (Function_Call);
8578 if Is_Entity_Name (Name (Func_Call)) then
8579 Func_Id := Entity (Name (Func_Call));
8581 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8582 Func_Id := Etype (Name (Func_Call));
8584 else
8585 raise Program_Error;
8586 end if;
8588 Result_Subt := Etype (Func_Id);
8590 -- When the result subtype is unconstrained, an additional actual must
8591 -- be passed to indicate that the caller is providing the return object.
8592 -- This parameter must also be passed when the called function has a
8593 -- controlling result, because dispatching calls to the function needs
8594 -- to be treated effectively the same as calls to class-wide functions.
8596 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8597 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8599 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8600 (Func_Call, Func_Id);
8602 Add_Task_Actuals_To_Build_In_Place_Call
8603 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8605 -- Add an implicit actual to the function call that provides access to
8606 -- the caller's return object.
8608 Add_Access_Actual_To_Build_In_Place_Call
8609 (Func_Call,
8610 Func_Id,
8611 Make_Unchecked_Type_Conversion (Loc,
8612 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8613 Expression => Relocate_Node (Lhs)));
8615 -- Create an access type designating the function's result subtype
8617 Ptr_Typ := Make_Temporary (Loc, 'A');
8619 Ptr_Typ_Decl :=
8620 Make_Full_Type_Declaration (Loc,
8621 Defining_Identifier => Ptr_Typ,
8622 Type_Definition =>
8623 Make_Access_To_Object_Definition (Loc,
8624 All_Present => True,
8625 Subtype_Indication =>
8626 New_Occurrence_Of (Result_Subt, Loc)));
8627 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8629 -- Finally, create an access object initialized to a reference to the
8630 -- function call. We know this access value is non-null, so mark the
8631 -- entity accordingly to suppress junk access checks.
8633 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8635 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8636 Set_Etype (Obj_Id, Ptr_Typ);
8637 Set_Is_Known_Non_Null (Obj_Id);
8639 Obj_Decl :=
8640 Make_Object_Declaration (Loc,
8641 Defining_Identifier => Obj_Id,
8642 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8643 Expression => New_Expr);
8644 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8646 Rewrite (Assign, Make_Null_Statement (Loc));
8648 -- Retrieve the target of the assignment
8650 if Nkind (Lhs) = N_Selected_Component then
8651 Target := Selector_Name (Lhs);
8652 elsif Nkind (Lhs) = N_Type_Conversion then
8653 Target := Expression (Lhs);
8654 else
8655 Target := Lhs;
8656 end if;
8658 -- If we are assigning to a return object or this is an expression of
8659 -- an extension aggregate, the target should either be an identifier
8660 -- or a simple expression. All other cases imply a different scenario.
8662 if Nkind (Target) in N_Has_Entity then
8663 Target := Entity (Target);
8664 else
8665 return;
8666 end if;
8667 end Make_Build_In_Place_Call_In_Assignment;
8669 ----------------------------------------------------
8670 -- Make_Build_In_Place_Call_In_Object_Declaration --
8671 ----------------------------------------------------
8673 procedure Make_Build_In_Place_Call_In_Object_Declaration
8674 (Object_Decl : Node_Id;
8675 Function_Call : Node_Id)
8677 Loc : Source_Ptr;
8678 Obj_Def_Id : constant Entity_Id :=
8679 Defining_Identifier (Object_Decl);
8680 Enclosing_Func : constant Entity_Id :=
8681 Enclosing_Subprogram (Obj_Def_Id);
8682 Call_Deref : Node_Id;
8683 Caller_Object : Node_Id;
8684 Def_Id : Entity_Id;
8685 Fmaster_Actual : Node_Id := Empty;
8686 Func_Call : Node_Id := Function_Call;
8687 Function_Id : Entity_Id;
8688 Pool_Actual : Node_Id;
8689 Ptr_Typ : Entity_Id;
8690 Ptr_Typ_Decl : Node_Id;
8691 Pass_Caller_Acc : Boolean := False;
8692 Res_Decl : Node_Id;
8693 Result_Subt : Entity_Id;
8695 begin
8696 -- Step past qualification or unchecked conversion (the latter can occur
8697 -- in cases of calls to 'Input).
8699 if Nkind_In (Func_Call, N_Qualified_Expression,
8700 N_Unchecked_Type_Conversion)
8701 then
8702 Func_Call := Expression (Func_Call);
8703 end if;
8705 -- If the call has already been processed to add build-in-place actuals
8706 -- then return. This should not normally occur in an object declaration,
8707 -- but we add the protection as a defensive measure.
8709 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8710 return;
8711 end if;
8713 -- Mark the call as processed as a build-in-place call
8715 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8717 Loc := Sloc (Function_Call);
8719 if Is_Entity_Name (Name (Func_Call)) then
8720 Function_Id := Entity (Name (Func_Call));
8722 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8723 Function_Id := Etype (Name (Func_Call));
8725 else
8726 raise Program_Error;
8727 end if;
8729 Result_Subt := Etype (Function_Id);
8731 -- Create an access type designating the function's result subtype. We
8732 -- use the type of the original call because it may be a call to an
8733 -- inherited operation, which the expansion has replaced with the parent
8734 -- operation that yields the parent type. Note that this access type
8735 -- must be declared before we establish a transient scope, so that it
8736 -- receives the proper accessibility level.
8738 Ptr_Typ := Make_Temporary (Loc, 'A');
8739 Ptr_Typ_Decl :=
8740 Make_Full_Type_Declaration (Loc,
8741 Defining_Identifier => Ptr_Typ,
8742 Type_Definition =>
8743 Make_Access_To_Object_Definition (Loc,
8744 All_Present => True,
8745 Subtype_Indication =>
8746 New_Occurrence_Of (Etype (Function_Call), Loc)));
8748 -- The access type and its accompanying object must be inserted after
8749 -- the object declaration in the constrained case, so that the function
8750 -- call can be passed access to the object. In the unconstrained case,
8751 -- or if the object declaration is for a return object, the access type
8752 -- and object must be inserted before the object, since the object
8753 -- declaration is rewritten to be a renaming of a dereference of the
8754 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8755 -- the result object is in a different (transient) scope, so won't
8756 -- cause freezing.
8758 if Is_Constrained (Underlying_Type (Result_Subt))
8759 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8760 then
8761 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
8762 else
8763 Insert_Action (Object_Decl, Ptr_Typ_Decl);
8764 end if;
8766 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8767 -- elaborated in an inner (transient) scope and thus won't cause
8768 -- freezing by itself.
8770 declare
8771 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8772 New_Occurrence_Of (Ptr_Typ, Loc);
8773 begin
8774 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8775 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8776 end;
8778 -- If the the object is a return object of an enclosing build-in-place
8779 -- function, then the implicit build-in-place parameters of the
8780 -- enclosing function are simply passed along to the called function.
8781 -- (Unfortunately, this won't cover the case of extension aggregates
8782 -- where the ancestor part is a build-in-place unconstrained function
8783 -- call that should be passed along the caller's parameters. Currently
8784 -- those get mishandled by reassigning the result of the call to the
8785 -- aggregate return object, when the call result should really be
8786 -- directly built in place in the aggregate and not in a temporary. ???)
8788 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
8789 Pass_Caller_Acc := True;
8791 -- When the enclosing function has a BIP_Alloc_Form formal then we
8792 -- pass it along to the callee (such as when the enclosing function
8793 -- has an unconstrained or tagged result type).
8795 if Needs_BIP_Alloc_Form (Enclosing_Func) then
8796 if VM_Target = No_VM and then
8797 RTE_Available (RE_Root_Storage_Pool_Ptr)
8798 then
8799 Pool_Actual :=
8800 New_Occurrence_Of (Build_In_Place_Formal
8801 (Enclosing_Func, BIP_Storage_Pool), Loc);
8803 -- The build-in-place pool formal is not built on .NET/JVM
8805 else
8806 Pool_Actual := Empty;
8807 end if;
8809 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8810 (Func_Call,
8811 Function_Id,
8812 Alloc_Form_Exp =>
8813 New_Occurrence_Of
8814 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
8815 Loc),
8816 Pool_Actual => Pool_Actual);
8818 -- Otherwise, if enclosing function has a constrained result subtype,
8819 -- then caller allocation will be used.
8821 else
8822 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8823 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8824 end if;
8826 if Needs_BIP_Finalization_Master (Enclosing_Func) then
8827 Fmaster_Actual :=
8828 New_Occurrence_Of
8829 (Build_In_Place_Formal
8830 (Enclosing_Func, BIP_Finalization_Master), Loc);
8831 end if;
8833 -- Retrieve the BIPacc formal from the enclosing function and convert
8834 -- it to the access type of the callee's BIP_Object_Access formal.
8836 Caller_Object :=
8837 Make_Unchecked_Type_Conversion (Loc,
8838 Subtype_Mark =>
8839 New_Occurrence_Of
8840 (Etype
8841 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8842 Loc),
8843 Expression =>
8844 New_Occurrence_Of
8845 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
8846 Loc));
8848 -- In the constrained case, add an implicit actual to the function call
8849 -- that provides access to the declared object. An unchecked conversion
8850 -- to the (specific) result type of the function is inserted to handle
8851 -- the case where the object is declared with a class-wide type.
8853 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8854 Caller_Object :=
8855 Make_Unchecked_Type_Conversion (Loc,
8856 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8857 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8859 -- When the function has a controlling result, an allocation-form
8860 -- parameter must be passed indicating that the caller is allocating
8861 -- the result object. This is needed because such a function can be
8862 -- called as a dispatching operation and must be treated similarly
8863 -- to functions with unconstrained result subtypes.
8865 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8866 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8868 -- In other unconstrained cases, pass an indication to do the allocation
8869 -- on the secondary stack and set Caller_Object to Empty so that a null
8870 -- value will be passed for the caller's object address. A transient
8871 -- scope is established to ensure eventual cleanup of the result.
8873 else
8874 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8875 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8876 Caller_Object := Empty;
8878 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
8879 end if;
8881 -- Pass along any finalization master actual, which is needed in the
8882 -- case where the called function initializes a return object of an
8883 -- enclosing build-in-place function.
8885 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8886 (Func_Call => Func_Call,
8887 Func_Id => Function_Id,
8888 Master_Exp => Fmaster_Actual);
8890 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
8891 and then Has_Task (Result_Subt)
8892 then
8893 -- Here we're passing along the master that was passed in to this
8894 -- function.
8896 Add_Task_Actuals_To_Build_In_Place_Call
8897 (Func_Call, Function_Id,
8898 Master_Actual =>
8899 New_Occurrence_Of (Build_In_Place_Formal
8900 (Enclosing_Func, BIP_Task_Master), Loc));
8902 else
8903 Add_Task_Actuals_To_Build_In_Place_Call
8904 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8905 end if;
8907 Add_Access_Actual_To_Build_In_Place_Call
8908 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
8910 -- Finally, create an access object initialized to a reference to the
8911 -- function call. We know this access value cannot be null, so mark the
8912 -- entity accordingly to suppress the access check.
8914 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8915 Set_Etype (Def_Id, Ptr_Typ);
8916 Set_Is_Known_Non_Null (Def_Id);
8918 Res_Decl :=
8919 Make_Object_Declaration (Loc,
8920 Defining_Identifier => Def_Id,
8921 Constant_Present => True,
8922 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8923 Expression =>
8924 Make_Reference (Loc, Relocate_Node (Func_Call)));
8926 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8928 -- If the result subtype of the called function is constrained and
8929 -- is not itself the return expression of an enclosing BIP function,
8930 -- then mark the object as having no initialization.
8932 if Is_Constrained (Underlying_Type (Result_Subt))
8933 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8934 then
8935 -- The related object declaration is encased in a transient block
8936 -- because the build-in-place function call contains at least one
8937 -- nested function call that produces a controlled transient
8938 -- temporary:
8940 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8942 -- Since the build-in-place expansion decouples the call from the
8943 -- object declaration, the finalization machinery lacks the context
8944 -- which prompted the generation of the transient block. To resolve
8945 -- this scenario, store the build-in-place call.
8947 if Scope_Is_Transient
8948 and then Node_To_Be_Wrapped = Object_Decl
8949 then
8950 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8951 end if;
8953 Set_Expression (Object_Decl, Empty);
8954 Set_No_Initialization (Object_Decl);
8956 -- In case of an unconstrained result subtype, or if the call is the
8957 -- return expression of an enclosing BIP function, rewrite the object
8958 -- declaration as an object renaming where the renamed object is a
8959 -- dereference of <function_Call>'reference:
8961 -- Obj : Subt renames <function_call>'Ref.all;
8963 else
8964 Call_Deref :=
8965 Make_Explicit_Dereference (Loc,
8966 Prefix => New_Occurrence_Of (Def_Id, Loc));
8968 Loc := Sloc (Object_Decl);
8969 Rewrite (Object_Decl,
8970 Make_Object_Renaming_Declaration (Loc,
8971 Defining_Identifier => Make_Temporary (Loc, 'D'),
8972 Access_Definition => Empty,
8973 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8974 Name => Call_Deref));
8976 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
8978 Analyze (Object_Decl);
8980 -- Replace the internal identifier of the renaming declaration's
8981 -- entity with identifier of the original object entity. We also have
8982 -- to exchange the entities containing their defining identifiers to
8983 -- ensure the correct replacement of the object declaration by the
8984 -- object renaming declaration to avoid homograph conflicts (since
8985 -- the object declaration's defining identifier was already entered
8986 -- in current scope). The Next_Entity links of the two entities also
8987 -- have to be swapped since the entities are part of the return
8988 -- scope's entity list and the list structure would otherwise be
8989 -- corrupted. Finally, the homonym chain must be preserved as well.
8991 declare
8992 Renaming_Def_Id : constant Entity_Id :=
8993 Defining_Identifier (Object_Decl);
8994 Next_Entity_Temp : constant Entity_Id :=
8995 Next_Entity (Renaming_Def_Id);
8996 begin
8997 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
8999 -- Swap next entity links in preparation for exchanging entities
9001 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9002 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9003 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9005 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9007 -- Preserve source indication of original declaration, so that
9008 -- xref information is properly generated for the right entity.
9010 Preserve_Comes_From_Source
9011 (Object_Decl, Original_Node (Object_Decl));
9013 Preserve_Comes_From_Source
9014 (Obj_Def_Id, Original_Node (Object_Decl));
9016 Set_Comes_From_Source (Renaming_Def_Id, False);
9017 end;
9018 end if;
9020 -- If the object entity has a class-wide Etype, then we need to change
9021 -- it to the result subtype of the function call, because otherwise the
9022 -- object will be class-wide without an explicit initialization and
9023 -- won't be allocated properly by the back end. It seems unclean to make
9024 -- such a revision to the type at this point, and we should try to
9025 -- improve this treatment when build-in-place functions with class-wide
9026 -- results are implemented. ???
9028 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9029 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9030 end if;
9031 end Make_Build_In_Place_Call_In_Object_Declaration;
9033 --------------------------------------------
9034 -- Make_CPP_Constructor_Call_In_Allocator --
9035 --------------------------------------------
9037 procedure Make_CPP_Constructor_Call_In_Allocator
9038 (Allocator : Node_Id;
9039 Function_Call : Node_Id)
9041 Loc : constant Source_Ptr := Sloc (Function_Call);
9042 Acc_Type : constant Entity_Id := Etype (Allocator);
9043 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9044 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9046 New_Allocator : Node_Id;
9047 Return_Obj_Access : Entity_Id;
9048 Tmp_Obj : Node_Id;
9050 begin
9051 pragma Assert (Nkind (Allocator) = N_Allocator
9052 and then Nkind (Function_Call) = N_Function_Call);
9053 pragma Assert (Convention (Function_Id) = Convention_CPP
9054 and then Is_Constructor (Function_Id));
9055 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9057 -- Replace the initialized allocator of form "new T'(Func (...))" with
9058 -- an uninitialized allocator of form "new T", where T is the result
9059 -- subtype of the called function. The call to the function is handled
9060 -- separately further below.
9062 New_Allocator :=
9063 Make_Allocator (Loc,
9064 Expression => New_Occurrence_Of (Result_Subt, Loc));
9065 Set_No_Initialization (New_Allocator);
9067 -- Copy attributes to new allocator. Note that the new allocator
9068 -- logically comes from source if the original one did, so copy the
9069 -- relevant flag. This ensures proper treatment of the restriction
9070 -- No_Implicit_Heap_Allocations in this case.
9072 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9073 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9074 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9076 Rewrite (Allocator, New_Allocator);
9078 -- Create a new access object and initialize it to the result of the
9079 -- new uninitialized allocator. Note: we do not use Allocator as the
9080 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9081 -- as this would create a sort of infinite "recursion".
9083 Return_Obj_Access := Make_Temporary (Loc, 'R');
9084 Set_Etype (Return_Obj_Access, Acc_Type);
9086 -- Generate:
9087 -- Rnnn : constant ptr_T := new (T);
9088 -- Init (Rnn.all,...);
9090 Tmp_Obj :=
9091 Make_Object_Declaration (Loc,
9092 Defining_Identifier => Return_Obj_Access,
9093 Constant_Present => True,
9094 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9095 Expression => Relocate_Node (Allocator));
9096 Insert_Action (Allocator, Tmp_Obj);
9098 Insert_List_After_And_Analyze (Tmp_Obj,
9099 Build_Initialization_Call (Loc,
9100 Id_Ref =>
9101 Make_Explicit_Dereference (Loc,
9102 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9103 Typ => Etype (Function_Id),
9104 Constructor_Ref => Function_Call));
9106 -- Finally, replace the allocator node with a reference to the result of
9107 -- the function call itself (which will effectively be an access to the
9108 -- object created by the allocator).
9110 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9112 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9113 -- generate an implicit conversion to force displacement of the "this"
9114 -- pointer.
9116 if Is_Interface (Designated_Type (Acc_Type)) then
9117 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9118 end if;
9120 Analyze_And_Resolve (Allocator, Acc_Type);
9121 end Make_CPP_Constructor_Call_In_Allocator;
9123 -----------------------------------
9124 -- Needs_BIP_Finalization_Master --
9125 -----------------------------------
9127 function Needs_BIP_Finalization_Master
9128 (Func_Id : Entity_Id) return Boolean
9130 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9131 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9132 begin
9133 return
9134 not Restriction_Active (No_Finalization)
9135 and then Needs_Finalization (Func_Typ);
9136 end Needs_BIP_Finalization_Master;
9138 --------------------------
9139 -- Needs_BIP_Alloc_Form --
9140 --------------------------
9142 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9143 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9144 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9145 begin
9146 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9147 end Needs_BIP_Alloc_Form;
9149 --------------------------------------
9150 -- Needs_Result_Accessibility_Level --
9151 --------------------------------------
9153 function Needs_Result_Accessibility_Level
9154 (Func_Id : Entity_Id) return Boolean
9156 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9158 function Has_Unconstrained_Access_Discriminant_Component
9159 (Comp_Typ : Entity_Id) return Boolean;
9160 -- Returns True if any component of the type has an unconstrained access
9161 -- discriminant.
9163 -----------------------------------------------------
9164 -- Has_Unconstrained_Access_Discriminant_Component --
9165 -----------------------------------------------------
9167 function Has_Unconstrained_Access_Discriminant_Component
9168 (Comp_Typ : Entity_Id) return Boolean
9170 begin
9171 if not Is_Limited_Type (Comp_Typ) then
9172 return False;
9174 -- Only limited types can have access discriminants with
9175 -- defaults.
9177 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9178 return True;
9180 elsif Is_Array_Type (Comp_Typ) then
9181 return Has_Unconstrained_Access_Discriminant_Component
9182 (Underlying_Type (Component_Type (Comp_Typ)));
9184 elsif Is_Record_Type (Comp_Typ) then
9185 declare
9186 Comp : Entity_Id;
9188 begin
9189 Comp := First_Component (Comp_Typ);
9190 while Present (Comp) loop
9191 if Has_Unconstrained_Access_Discriminant_Component
9192 (Underlying_Type (Etype (Comp)))
9193 then
9194 return True;
9195 end if;
9197 Next_Component (Comp);
9198 end loop;
9199 end;
9200 end if;
9202 return False;
9203 end Has_Unconstrained_Access_Discriminant_Component;
9205 Feature_Disabled : constant Boolean := True;
9206 -- Temporary
9208 -- Start of processing for Needs_Result_Accessibility_Level
9210 begin
9211 -- False if completion unavailable (how does this happen???)
9213 if not Present (Func_Typ) then
9214 return False;
9216 elsif Feature_Disabled then
9217 return False;
9219 -- False if not a function, also handle enum-lit renames case
9221 elsif Func_Typ = Standard_Void_Type
9222 or else Is_Scalar_Type (Func_Typ)
9223 then
9224 return False;
9226 -- Handle a corner case, a cross-dialect subp renaming. For example,
9227 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9228 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9230 elsif Present (Alias (Func_Id)) then
9232 -- Unimplemented: a cross-dialect subp renaming which does not set
9233 -- the Alias attribute (e.g., a rename of a dereference of an access
9234 -- to subprogram value). ???
9236 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9238 -- Remaining cases require Ada 2012 mode
9240 elsif Ada_Version < Ada_2012 then
9241 return False;
9243 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9244 or else Is_Tagged_Type (Func_Typ)
9245 then
9246 -- In the case of, say, a null tagged record result type, the need
9247 -- for this extra parameter might not be obvious. This function
9248 -- returns True for all tagged types for compatibility reasons.
9249 -- A function with, say, a tagged null controlling result type might
9250 -- be overridden by a primitive of an extension having an access
9251 -- discriminant and the overrider and overridden must have compatible
9252 -- calling conventions (including implicitly declared parameters).
9253 -- Similarly, values of one access-to-subprogram type might designate
9254 -- both a primitive subprogram of a given type and a function
9255 -- which is, for example, not a primitive subprogram of any type.
9256 -- Again, this requires calling convention compatibility.
9257 -- It might be possible to solve these issues by introducing
9258 -- wrappers, but that is not the approach that was chosen.
9260 return True;
9262 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9263 return True;
9265 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9266 return True;
9268 -- False for all other cases
9270 else
9271 return False;
9272 end if;
9273 end Needs_Result_Accessibility_Level;
9275 end Exp_Ch6;