* config/linux/x86/futex_bits.h (sys_futex0) [__x86_64__]: Change
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob67caf2f2787556e421e148894ae42b55f14c5d70
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-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with 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_Unst; use Exp_Unst;
46 with Exp_Util; use Exp_Util;
47 with Freeze; use Freeze;
48 with Ghost; use Ghost;
49 with Inline; use Inline;
50 with Lib; use Lib;
51 with Namet; use Namet;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Restrict; use Restrict;
56 with Rident; use Rident;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_SCIL; use Sem_SCIL;
70 with Sem_Util; use Sem_Util;
71 with Sinfo; use Sinfo;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with Stringt; use Stringt;
75 with Table;
76 with Targparm; use Targparm;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Validsw; use Validsw;
81 package body Exp_Ch6 is
83 -------------------------------------
84 -- Table for Unnesting Subprograms --
85 -------------------------------------
87 -- When we expand a subprogram body, if it has nested subprograms and if
88 -- we are in Unnest_Subprogram_Mode, then we record the subprogram entity
89 -- and the body in this table, to later be passed to Unnest_Subprogram.
91 -- We need this delaying mechanism, because we have to wait until all
92 -- instantiated bodies have been inserted before doing the unnesting.
94 type Unest_Entry is record
95 Ent : Entity_Id;
96 -- Entity for subprogram to be unnested
98 Bod : Node_Id;
99 -- Subprogram body to be unnested
100 end record;
102 package Unest_Bodies is new Table.Table (
103 Table_Component_Type => Unest_Entry,
104 Table_Index_Type => Nat,
105 Table_Low_Bound => 1,
106 Table_Initial => 100,
107 Table_Increment => 200,
108 Table_Name => "Unest_Bodies");
110 -----------------------
111 -- Local Subprograms --
112 -----------------------
114 procedure Add_Access_Actual_To_Build_In_Place_Call
115 (Function_Call : Node_Id;
116 Function_Id : Entity_Id;
117 Return_Object : Node_Id;
118 Is_Access : Boolean := False);
119 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
120 -- object name given by Return_Object and add the attribute to the end of
121 -- the actual parameter list associated with the build-in-place function
122 -- call denoted by Function_Call. However, if Is_Access is True, then
123 -- Return_Object is already an access expression, in which case it's passed
124 -- along directly to the build-in-place function. Finally, if Return_Object
125 -- is empty, then pass a null literal as the actual.
127 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
128 (Function_Call : Node_Id;
129 Function_Id : Entity_Id;
130 Alloc_Form : BIP_Allocation_Form := Unspecified;
131 Alloc_Form_Exp : Node_Id := Empty;
132 Pool_Actual : Node_Id := Make_Null (No_Location));
133 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
134 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
135 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
136 -- otherwise pass a literal corresponding to the Alloc_Form parameter
137 -- (which must not be Unspecified in that case). Pool_Actual is the
138 -- parameter to pass to BIP_Storage_Pool.
140 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
141 (Func_Call : Node_Id;
142 Func_Id : Entity_Id;
143 Ptr_Typ : Entity_Id := Empty;
144 Master_Exp : Node_Id := Empty);
145 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
146 -- finalization actions, add an actual parameter which is a pointer to the
147 -- finalization master of the caller. If Master_Exp is not Empty, then that
148 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
149 -- will result in an automatic "null" value for the actual.
151 procedure Add_Task_Actuals_To_Build_In_Place_Call
152 (Function_Call : Node_Id;
153 Function_Id : Entity_Id;
154 Master_Actual : Node_Id;
155 Chain : Node_Id := Empty);
156 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
157 -- contains tasks, add two actual parameters: the master, and a pointer to
158 -- the caller's activation chain. Master_Actual is the actual parameter
159 -- expression to pass for the master. In most cases, this is the current
160 -- master (_master). The two exceptions are: If the function call is the
161 -- initialization expression for an allocator, we pass the master of the
162 -- access type. If the function call is the initialization expression for a
163 -- return object, we pass along the master passed in by the caller. In most
164 -- contexts, the activation chain to pass is the local one, which is
165 -- indicated by No (Chain). However, in an allocator, the caller passes in
166 -- the activation Chain. Note: Master_Actual can be Empty, but only if
167 -- there are no tasks.
169 procedure Check_Overriding_Operation (Subp : Entity_Id);
170 -- Subp is a dispatching operation. Check whether it may override an
171 -- inherited private operation, in which case its DT entry is that of
172 -- the hidden operation, not the one it may have received earlier.
173 -- This must be done before emitting the code to set the corresponding
174 -- DT to the address of the subprogram. The actual placement of Subp in
175 -- the proper place in the list of primitive operations is done in
176 -- Declare_Inherited_Private_Subprograms, which also has to deal with
177 -- implicit operations. This duplication is unavoidable for now???
179 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
180 -- This procedure is called only if the subprogram body N, whose spec
181 -- has the given entity Spec, contains a parameterless recursive call.
182 -- It attempts to generate runtime code to detect if this a case of
183 -- infinite recursion.
185 -- The body is scanned to determine dependencies. If the only external
186 -- dependencies are on a small set of scalar variables, then the values
187 -- of these variables are captured on entry to the subprogram, and if
188 -- the values are not changed for the call, we know immediately that
189 -- we have an infinite recursion.
191 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id);
192 -- For each actual of an in-out or out parameter which is a numeric
193 -- (view) conversion of the form T (A), where A denotes a variable,
194 -- we insert the declaration:
196 -- Temp : T[ := T (A)];
198 -- prior to the call. Then we replace the actual with a reference to Temp,
199 -- and append the assignment:
201 -- A := TypeA (Temp);
203 -- after the call. Here TypeA is the actual type of variable A. For out
204 -- parameters, the initial declaration has no expression. If A is not an
205 -- entity name, we generate instead:
207 -- Var : TypeA renames A;
208 -- Temp : T := Var; -- omitting expression for out parameter.
209 -- ...
210 -- Var := TypeA (Temp);
212 -- For other in-out parameters, we emit the required constraint checks
213 -- before and/or after the call.
215 -- For all parameter modes, actuals that denote components and slices of
216 -- packed arrays are expanded into suitable temporaries.
218 -- For non-scalar objects that are possibly unaligned, add call by copy
219 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
221 -- For OUT and IN OUT parameters, add predicate checks after the call
222 -- based on the predicates of the actual type.
224 -- The parameter N is IN OUT because in some cases, the expansion code
225 -- rewrites the call as an expression actions with the call inside. In
226 -- this case N is reset to point to the inside call so that the caller
227 -- can continue processing of this call.
229 procedure Expand_Ctrl_Function_Call (N : Node_Id);
230 -- N is a function call which returns a controlled object. Transform the
231 -- call into a temporary which retrieves the returned object from the
232 -- secondary stack using 'reference.
234 procedure Expand_Non_Function_Return (N : Node_Id);
235 -- Expand a simple return statement found in a procedure body, entry body,
236 -- accept statement, or an extended return statement. Note that all non-
237 -- function returns are simple return statements.
239 function Expand_Protected_Object_Reference
240 (N : Node_Id;
241 Scop : Entity_Id) return Node_Id;
243 procedure Expand_Protected_Subprogram_Call
244 (N : Node_Id;
245 Subp : Entity_Id;
246 Scop : Entity_Id);
247 -- A call to a protected subprogram within the protected object may appear
248 -- as a regular call. The list of actuals must be expanded to contain a
249 -- reference to the object itself, and the call becomes a call to the
250 -- corresponding protected subprogram.
252 function Has_Unconstrained_Access_Discriminants
253 (Subtyp : Entity_Id) return Boolean;
254 -- Returns True if the given subtype is unconstrained and has one
255 -- or more access discriminants.
257 procedure Expand_Simple_Function_Return (N : Node_Id);
258 -- Expand simple return from function. In the case where we are returning
259 -- from a function body this is called by Expand_N_Simple_Return_Statement.
261 ----------------------------------------------
262 -- Add_Access_Actual_To_Build_In_Place_Call --
263 ----------------------------------------------
265 procedure Add_Access_Actual_To_Build_In_Place_Call
266 (Function_Call : Node_Id;
267 Function_Id : Entity_Id;
268 Return_Object : Node_Id;
269 Is_Access : Boolean := False)
271 Loc : constant Source_Ptr := Sloc (Function_Call);
272 Obj_Address : Node_Id;
273 Obj_Acc_Formal : Entity_Id;
275 begin
276 -- Locate the implicit access parameter in the called function
278 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
280 -- If no return object is provided, then pass null
282 if not Present (Return_Object) then
283 Obj_Address := Make_Null (Loc);
284 Set_Parent (Obj_Address, Function_Call);
286 -- If Return_Object is already an expression of an access type, then use
287 -- it directly, since it must be an access value denoting the return
288 -- object, and couldn't possibly be the return object itself.
290 elsif Is_Access then
291 Obj_Address := Return_Object;
292 Set_Parent (Obj_Address, Function_Call);
294 -- Apply Unrestricted_Access to caller's return object
296 else
297 Obj_Address :=
298 Make_Attribute_Reference (Loc,
299 Prefix => Return_Object,
300 Attribute_Name => Name_Unrestricted_Access);
302 Set_Parent (Return_Object, Obj_Address);
303 Set_Parent (Obj_Address, Function_Call);
304 end if;
306 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
308 -- Build the parameter association for the new actual and add it to the
309 -- end of the function's actuals.
311 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
312 end Add_Access_Actual_To_Build_In_Place_Call;
314 ------------------------------------------------------
315 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
316 ------------------------------------------------------
318 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
319 (Function_Call : Node_Id;
320 Function_Id : Entity_Id;
321 Alloc_Form : BIP_Allocation_Form := Unspecified;
322 Alloc_Form_Exp : Node_Id := Empty;
323 Pool_Actual : Node_Id := Make_Null (No_Location))
325 Loc : constant Source_Ptr := Sloc (Function_Call);
326 Alloc_Form_Actual : Node_Id;
327 Alloc_Form_Formal : Node_Id;
328 Pool_Formal : Node_Id;
330 begin
331 -- The allocation form generally doesn't need to be passed in the case
332 -- of a constrained result subtype, since normally the caller performs
333 -- the allocation in that case. However this formal is still needed in
334 -- the case where the function has a tagged result, because generally
335 -- such functions can be called in a dispatching context and such calls
336 -- must be handled like calls to class-wide functions.
338 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
339 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
340 then
341 return;
342 end if;
344 -- Locate the implicit allocation form parameter in the called function.
345 -- Maybe it would be better for each implicit formal of a build-in-place
346 -- function to have a flag or a Uint attribute to identify it. ???
348 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
350 if Present (Alloc_Form_Exp) then
351 pragma Assert (Alloc_Form = Unspecified);
353 Alloc_Form_Actual := Alloc_Form_Exp;
355 else
356 pragma Assert (Alloc_Form /= Unspecified);
358 Alloc_Form_Actual :=
359 Make_Integer_Literal (Loc,
360 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
361 end if;
363 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
365 -- Build the parameter association for the new actual and add it to the
366 -- end of the function's actuals.
368 Add_Extra_Actual_To_Call
369 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
371 -- Pass the Storage_Pool parameter. This parameter is omitted on
372 -- .NET/JVM/ZFP as those targets do not support pools.
374 if VM_Target = No_VM
375 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
376 then
377 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
378 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
379 Add_Extra_Actual_To_Call
380 (Function_Call, Pool_Formal, Pool_Actual);
381 end if;
382 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
384 -----------------------------------------------------------
385 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
386 -----------------------------------------------------------
388 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
389 (Func_Call : Node_Id;
390 Func_Id : Entity_Id;
391 Ptr_Typ : Entity_Id := Empty;
392 Master_Exp : Node_Id := Empty)
394 begin
395 if not Needs_BIP_Finalization_Master (Func_Id) then
396 return;
397 end if;
399 declare
400 Formal : constant Entity_Id :=
401 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
402 Loc : constant Source_Ptr := Sloc (Func_Call);
404 Actual : Node_Id;
405 Desig_Typ : Entity_Id;
407 begin
408 -- If there is a finalization master actual, such as the implicit
409 -- finalization master of an enclosing build-in-place function,
410 -- then this must be added as an extra actual of the call.
412 if Present (Master_Exp) then
413 Actual := Master_Exp;
415 -- Case where the context does not require an actual master
417 elsif No (Ptr_Typ) then
418 Actual := Make_Null (Loc);
420 else
421 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
423 -- Check for a library-level access type whose designated type has
424 -- supressed finalization. Such an access types lack a master.
425 -- Pass a null actual to the callee in order to signal a missing
426 -- master.
428 if Is_Library_Level_Entity (Ptr_Typ)
429 and then Finalize_Storage_Only (Desig_Typ)
430 then
431 Actual := Make_Null (Loc);
433 -- Types in need of finalization actions
435 elsif Needs_Finalization (Desig_Typ) then
437 -- The general mechanism of creating finalization masters for
438 -- anonymous access types is disabled by default, otherwise
439 -- finalization masters will pop all over the place. Such types
440 -- use context-specific masters.
442 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
443 and then No (Finalization_Master (Ptr_Typ))
444 then
445 Build_Finalization_Master
446 (Typ => Ptr_Typ,
447 For_Anonymous => True,
448 Context_Scope => Scope (Ptr_Typ),
449 Insertion_Node => Associated_Node_For_Itype (Ptr_Typ));
450 end if;
452 -- Access-to-controlled types should always have a master
454 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
456 Actual :=
457 Make_Attribute_Reference (Loc,
458 Prefix =>
459 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
460 Attribute_Name => Name_Unrestricted_Access);
462 -- Tagged types
464 else
465 Actual := Make_Null (Loc);
466 end if;
467 end if;
469 Analyze_And_Resolve (Actual, Etype (Formal));
471 -- Build the parameter association for the new actual and add it to
472 -- the end of the function's actuals.
474 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
475 end;
476 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
478 ------------------------------
479 -- Add_Extra_Actual_To_Call --
480 ------------------------------
482 procedure Add_Extra_Actual_To_Call
483 (Subprogram_Call : Node_Id;
484 Extra_Formal : Entity_Id;
485 Extra_Actual : Node_Id)
487 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
488 Param_Assoc : Node_Id;
490 begin
491 Param_Assoc :=
492 Make_Parameter_Association (Loc,
493 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
494 Explicit_Actual_Parameter => Extra_Actual);
496 Set_Parent (Param_Assoc, Subprogram_Call);
497 Set_Parent (Extra_Actual, Param_Assoc);
499 if Present (Parameter_Associations (Subprogram_Call)) then
500 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
501 N_Parameter_Association
502 then
504 -- Find last named actual, and append
506 declare
507 L : Node_Id;
508 begin
509 L := First_Actual (Subprogram_Call);
510 while Present (L) loop
511 if No (Next_Actual (L)) then
512 Set_Next_Named_Actual (Parent (L), Extra_Actual);
513 exit;
514 end if;
515 Next_Actual (L);
516 end loop;
517 end;
519 else
520 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
521 end if;
523 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
525 else
526 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
527 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
528 end if;
529 end Add_Extra_Actual_To_Call;
531 ---------------------------------------------
532 -- Add_Task_Actuals_To_Build_In_Place_Call --
533 ---------------------------------------------
535 procedure Add_Task_Actuals_To_Build_In_Place_Call
536 (Function_Call : Node_Id;
537 Function_Id : Entity_Id;
538 Master_Actual : Node_Id;
539 Chain : Node_Id := Empty)
541 Loc : constant Source_Ptr := Sloc (Function_Call);
542 Result_Subt : constant Entity_Id :=
543 Available_View (Etype (Function_Id));
544 Actual : Node_Id;
545 Chain_Actual : Node_Id;
546 Chain_Formal : Node_Id;
547 Master_Formal : Node_Id;
549 begin
550 -- No such extra parameters are needed if there are no tasks
552 if not Has_Task (Result_Subt) then
553 return;
554 end if;
556 Actual := Master_Actual;
558 -- Use a dummy _master actual in case of No_Task_Hierarchy
560 if Restriction_Active (No_Task_Hierarchy) then
561 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
563 -- In the case where we use the master associated with an access type,
564 -- the actual is an entity and requires an explicit reference.
566 elsif Nkind (Actual) = N_Defining_Identifier then
567 Actual := New_Occurrence_Of (Actual, Loc);
568 end if;
570 -- Locate the implicit master parameter in the called function
572 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
573 Analyze_And_Resolve (Actual, Etype (Master_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, Master_Formal, Actual);
580 -- Locate the implicit activation chain parameter in the called function
582 Chain_Formal :=
583 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
585 -- Create the actual which is a pointer to the current activation chain
587 if No (Chain) then
588 Chain_Actual :=
589 Make_Attribute_Reference (Loc,
590 Prefix => Make_Identifier (Loc, Name_uChain),
591 Attribute_Name => Name_Unrestricted_Access);
593 -- Allocator case; make a reference to the Chain passed in by the caller
595 else
596 Chain_Actual :=
597 Make_Attribute_Reference (Loc,
598 Prefix => New_Occurrence_Of (Chain, Loc),
599 Attribute_Name => Name_Unrestricted_Access);
600 end if;
602 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
604 -- Build the parameter association for the new actual and add it to the
605 -- end of the function's actuals.
607 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
608 end Add_Task_Actuals_To_Build_In_Place_Call;
610 -----------------------
611 -- BIP_Formal_Suffix --
612 -----------------------
614 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
615 begin
616 case Kind is
617 when BIP_Alloc_Form =>
618 return "BIPalloc";
619 when BIP_Storage_Pool =>
620 return "BIPstoragepool";
621 when BIP_Finalization_Master =>
622 return "BIPfinalizationmaster";
623 when BIP_Task_Master =>
624 return "BIPtaskmaster";
625 when BIP_Activation_Chain =>
626 return "BIPactivationchain";
627 when BIP_Object_Access =>
628 return "BIPaccess";
629 end case;
630 end BIP_Formal_Suffix;
632 ---------------------------
633 -- Build_In_Place_Formal --
634 ---------------------------
636 function Build_In_Place_Formal
637 (Func : Entity_Id;
638 Kind : BIP_Formal_Kind) return Entity_Id
640 Formal_Name : constant Name_Id :=
641 New_External_Name
642 (Chars (Func), BIP_Formal_Suffix (Kind));
643 Extra_Formal : Entity_Id := Extra_Formals (Func);
645 begin
646 -- Maybe it would be better for each implicit formal of a build-in-place
647 -- function to have a flag or a Uint attribute to identify it. ???
649 -- The return type in the function declaration may have been a limited
650 -- view, and the extra formals for the function were not generated at
651 -- that point. At the point of call the full view must be available and
652 -- the extra formals can be created.
654 if No (Extra_Formal) then
655 Create_Extra_Formals (Func);
656 Extra_Formal := Extra_Formals (Func);
657 end if;
659 loop
660 pragma Assert (Present (Extra_Formal));
661 exit when Chars (Extra_Formal) = Formal_Name;
663 Next_Formal_With_Extras (Extra_Formal);
664 end loop;
666 return Extra_Formal;
667 end Build_In_Place_Formal;
669 --------------------------------
670 -- Check_Overriding_Operation --
671 --------------------------------
673 procedure Check_Overriding_Operation (Subp : Entity_Id) is
674 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
675 Op_List : constant Elist_Id := Primitive_Operations (Typ);
676 Op_Elmt : Elmt_Id;
677 Prim_Op : Entity_Id;
678 Par_Op : Entity_Id;
680 begin
681 if Is_Derived_Type (Typ)
682 and then not Is_Private_Type (Typ)
683 and then In_Open_Scopes (Scope (Etype (Typ)))
684 and then Is_Base_Type (Typ)
685 then
686 -- Subp overrides an inherited private operation if there is an
687 -- inherited operation with a different name than Subp (see
688 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
689 -- same name as Subp.
691 Op_Elmt := First_Elmt (Op_List);
692 while Present (Op_Elmt) loop
693 Prim_Op := Node (Op_Elmt);
694 Par_Op := Alias (Prim_Op);
696 if Present (Par_Op)
697 and then not Comes_From_Source (Prim_Op)
698 and then Chars (Prim_Op) /= Chars (Par_Op)
699 and then Chars (Par_Op) = Chars (Subp)
700 and then Is_Hidden (Par_Op)
701 and then Type_Conformant (Prim_Op, Subp)
702 then
703 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
704 end if;
706 Next_Elmt (Op_Elmt);
707 end loop;
708 end if;
709 end Check_Overriding_Operation;
711 -------------------------------
712 -- Detect_Infinite_Recursion --
713 -------------------------------
715 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
716 Loc : constant Source_Ptr := Sloc (N);
718 Var_List : constant Elist_Id := New_Elmt_List;
719 -- List of globals referenced by body of procedure
721 Call_List : constant Elist_Id := New_Elmt_List;
722 -- List of recursive calls in body of procedure
724 Shad_List : constant Elist_Id := New_Elmt_List;
725 -- List of entity id's for entities created to capture the value of
726 -- referenced globals on entry to the procedure.
728 Scop : constant Uint := Scope_Depth (Spec);
729 -- This is used to record the scope depth of the current procedure, so
730 -- that we can identify global references.
732 Max_Vars : constant := 4;
733 -- Do not test more than four global variables
735 Count_Vars : Natural := 0;
736 -- Count variables found so far
738 Var : Entity_Id;
739 Elm : Elmt_Id;
740 Ent : Entity_Id;
741 Call : Elmt_Id;
742 Decl : Node_Id;
743 Test : Node_Id;
744 Elm1 : Elmt_Id;
745 Elm2 : Elmt_Id;
746 Last : Node_Id;
748 function Process (Nod : Node_Id) return Traverse_Result;
749 -- Function to traverse the subprogram body (using Traverse_Func)
751 -------------
752 -- Process --
753 -------------
755 function Process (Nod : Node_Id) return Traverse_Result is
756 begin
757 -- Procedure call
759 if Nkind (Nod) = N_Procedure_Call_Statement then
761 -- Case of one of the detected recursive calls
763 if Is_Entity_Name (Name (Nod))
764 and then Has_Recursive_Call (Entity (Name (Nod)))
765 and then Entity (Name (Nod)) = Spec
766 then
767 Append_Elmt (Nod, Call_List);
768 return Skip;
770 -- Any other procedure call may have side effects
772 else
773 return Abandon;
774 end if;
776 -- A call to a pure function can always be ignored
778 elsif Nkind (Nod) = N_Function_Call
779 and then Is_Entity_Name (Name (Nod))
780 and then Is_Pure (Entity (Name (Nod)))
781 then
782 return Skip;
784 -- Case of an identifier reference
786 elsif Nkind (Nod) = N_Identifier then
787 Ent := Entity (Nod);
789 -- If no entity, then ignore the reference
791 -- Not clear why this can happen. To investigate, remove this
792 -- test and look at the crash that occurs here in 3401-004 ???
794 if No (Ent) then
795 return Skip;
797 -- Ignore entities with no Scope, again not clear how this
798 -- can happen, to investigate, look at 4108-008 ???
800 elsif No (Scope (Ent)) then
801 return Skip;
803 -- Ignore the reference if not to a more global object
805 elsif Scope_Depth (Scope (Ent)) >= Scop then
806 return Skip;
808 -- References to types, exceptions and constants are always OK
810 elsif Is_Type (Ent)
811 or else Ekind (Ent) = E_Exception
812 or else Ekind (Ent) = E_Constant
813 then
814 return Skip;
816 -- If other than a non-volatile scalar variable, we have some
817 -- kind of global reference (e.g. to a function) that we cannot
818 -- deal with so we forget the attempt.
820 elsif Ekind (Ent) /= E_Variable
821 or else not Is_Scalar_Type (Etype (Ent))
822 or else Treat_As_Volatile (Ent)
823 then
824 return Abandon;
826 -- Otherwise we have a reference to a global scalar
828 else
829 -- Loop through global entities already detected
831 Elm := First_Elmt (Var_List);
832 loop
833 -- If not detected before, record this new global reference
835 if No (Elm) then
836 Count_Vars := Count_Vars + 1;
838 if Count_Vars <= Max_Vars then
839 Append_Elmt (Entity (Nod), Var_List);
840 else
841 return Abandon;
842 end if;
844 exit;
846 -- If recorded before, ignore
848 elsif Node (Elm) = Entity (Nod) then
849 return Skip;
851 -- Otherwise keep looking
853 else
854 Next_Elmt (Elm);
855 end if;
856 end loop;
858 return Skip;
859 end if;
861 -- For all other node kinds, recursively visit syntactic children
863 else
864 return OK;
865 end if;
866 end Process;
868 function Traverse_Body is new Traverse_Func (Process);
870 -- Start of processing for Detect_Infinite_Recursion
872 begin
873 -- Do not attempt detection in No_Implicit_Conditional mode, since we
874 -- won't be able to generate the code to handle the recursion in any
875 -- case.
877 if Restriction_Active (No_Implicit_Conditionals) then
878 return;
879 end if;
881 -- Otherwise do traversal and quit if we get abandon signal
883 if Traverse_Body (N) = Abandon then
884 return;
886 -- We must have a call, since Has_Recursive_Call was set. If not just
887 -- ignore (this is only an error check, so if we have a funny situation,
888 -- due to bugs or errors, we do not want to bomb).
890 elsif Is_Empty_Elmt_List (Call_List) then
891 return;
892 end if;
894 -- Here is the case where we detect recursion at compile time
896 -- Push our current scope for analyzing the declarations and code that
897 -- we will insert for the checking.
899 Push_Scope (Spec);
901 -- This loop builds temporary variables for each of the referenced
902 -- globals, so that at the end of the loop the list Shad_List contains
903 -- these temporaries in one-to-one correspondence with the elements in
904 -- Var_List.
906 Last := Empty;
907 Elm := First_Elmt (Var_List);
908 while Present (Elm) loop
909 Var := Node (Elm);
910 Ent := Make_Temporary (Loc, 'S');
911 Append_Elmt (Ent, Shad_List);
913 -- Insert a declaration for this temporary at the start of the
914 -- declarations for the procedure. The temporaries are declared as
915 -- constant objects initialized to the current values of the
916 -- corresponding temporaries.
918 Decl :=
919 Make_Object_Declaration (Loc,
920 Defining_Identifier => Ent,
921 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
922 Constant_Present => True,
923 Expression => New_Occurrence_Of (Var, Loc));
925 if No (Last) then
926 Prepend (Decl, Declarations (N));
927 else
928 Insert_After (Last, Decl);
929 end if;
931 Last := Decl;
932 Analyze (Decl);
933 Next_Elmt (Elm);
934 end loop;
936 -- Loop through calls
938 Call := First_Elmt (Call_List);
939 while Present (Call) loop
941 -- Build a predicate expression of the form
943 -- True
944 -- and then global1 = temp1
945 -- and then global2 = temp2
946 -- ...
948 -- This predicate determines if any of the global values
949 -- referenced by the procedure have changed since the
950 -- current call, if not an infinite recursion is assured.
952 Test := New_Occurrence_Of (Standard_True, Loc);
954 Elm1 := First_Elmt (Var_List);
955 Elm2 := First_Elmt (Shad_List);
956 while Present (Elm1) loop
957 Test :=
958 Make_And_Then (Loc,
959 Left_Opnd => Test,
960 Right_Opnd =>
961 Make_Op_Eq (Loc,
962 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
963 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
965 Next_Elmt (Elm1);
966 Next_Elmt (Elm2);
967 end loop;
969 -- Now we replace the call with the sequence
971 -- if no-changes (see above) then
972 -- raise Storage_Error;
973 -- else
974 -- original-call
975 -- end if;
977 Rewrite (Node (Call),
978 Make_If_Statement (Loc,
979 Condition => Test,
980 Then_Statements => New_List (
981 Make_Raise_Storage_Error (Loc,
982 Reason => SE_Infinite_Recursion)),
984 Else_Statements => New_List (
985 Relocate_Node (Node (Call)))));
987 Analyze (Node (Call));
989 Next_Elmt (Call);
990 end loop;
992 -- Remove temporary scope stack entry used for analysis
994 Pop_Scope;
995 end Detect_Infinite_Recursion;
997 --------------------
998 -- Expand_Actuals --
999 --------------------
1001 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
1002 Loc : constant Source_Ptr := Sloc (N);
1003 Actual : Node_Id;
1004 Formal : Entity_Id;
1005 N_Node : Node_Id;
1006 Post_Call : List_Id;
1007 E_Actual : Entity_Id;
1008 E_Formal : Entity_Id;
1010 procedure Add_Call_By_Copy_Code;
1011 -- For cases where the parameter must be passed by copy, this routine
1012 -- generates a temporary variable into which the actual is copied and
1013 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1014 -- an assignment is also generated to copy the result back. The call
1015 -- also takes care of any constraint checks required for the type
1016 -- conversion case (on both the way in and the way out).
1018 procedure Add_Simple_Call_By_Copy_Code;
1019 -- This is similar to the above, but is used in cases where we know
1020 -- that all that is needed is to simply create a temporary and copy
1021 -- the value in and out of the temporary.
1023 procedure Check_Fortran_Logical;
1024 -- A value of type Logical that is passed through a formal parameter
1025 -- must be normalized because .TRUE. usually does not have the same
1026 -- representation as True. We assume that .FALSE. = False = 0.
1027 -- What about functions that return a logical type ???
1029 function Is_Legal_Copy return Boolean;
1030 -- Check that an actual can be copied before generating the temporary
1031 -- to be used in the call. If the actual is of a by_reference type then
1032 -- the program is illegal (this can only happen in the presence of
1033 -- rep. clauses that force an incorrect alignment). If the formal is
1034 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1035 -- the effect that this might lead to unaligned arguments.
1037 function Make_Var (Actual : Node_Id) return Entity_Id;
1038 -- Returns an entity that refers to the given actual parameter, Actual
1039 -- (not including any type conversion). If Actual is an entity name,
1040 -- then this entity is returned unchanged, otherwise a renaming is
1041 -- created to provide an entity for the actual.
1043 procedure Reset_Packed_Prefix;
1044 -- The expansion of a packed array component reference is delayed in
1045 -- the context of a call. Now we need to complete the expansion, so we
1046 -- unmark the analyzed bits in all prefixes.
1048 ---------------------------
1049 -- Add_Call_By_Copy_Code --
1050 ---------------------------
1052 procedure Add_Call_By_Copy_Code is
1053 Expr : Node_Id;
1054 Init : Node_Id;
1055 Temp : Entity_Id;
1056 Indic : Node_Id;
1057 Var : Entity_Id;
1058 F_Typ : constant Entity_Id := Etype (Formal);
1059 V_Typ : Entity_Id;
1060 Crep : Boolean;
1062 begin
1063 if not Is_Legal_Copy then
1064 return;
1065 end if;
1067 Temp := Make_Temporary (Loc, 'T', Actual);
1069 -- Use formal type for temp, unless formal type is an unconstrained
1070 -- array, in which case we don't have to worry about bounds checks,
1071 -- and we use the actual type, since that has appropriate bounds.
1073 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1074 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1075 else
1076 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1077 end if;
1079 if Nkind (Actual) = N_Type_Conversion then
1080 V_Typ := Etype (Expression (Actual));
1082 -- If the formal is an (in-)out parameter, capture the name
1083 -- of the variable in order to build the post-call assignment.
1085 Var := Make_Var (Expression (Actual));
1087 Crep := not Same_Representation
1088 (F_Typ, Etype (Expression (Actual)));
1090 else
1091 V_Typ := Etype (Actual);
1092 Var := Make_Var (Actual);
1093 Crep := False;
1094 end if;
1096 -- Setup initialization for case of in out parameter, or an out
1097 -- parameter where the formal is an unconstrained array (in the
1098 -- latter case, we have to pass in an object with bounds).
1100 -- If this is an out parameter, the initial copy is wasteful, so as
1101 -- an optimization for the one-dimensional case we extract the
1102 -- bounds of the actual and build an uninitialized temporary of the
1103 -- right size.
1105 if Ekind (Formal) = E_In_Out_Parameter
1106 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1107 then
1108 if Nkind (Actual) = N_Type_Conversion then
1109 if Conversion_OK (Actual) then
1110 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1111 else
1112 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1113 end if;
1115 elsif Ekind (Formal) = E_Out_Parameter
1116 and then Is_Array_Type (F_Typ)
1117 and then Number_Dimensions (F_Typ) = 1
1118 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1119 then
1120 -- Actual is a one-dimensional array or slice, and the type
1121 -- requires no initialization. Create a temporary of the
1122 -- right size, but do not copy actual into it (optimization).
1124 Init := Empty;
1125 Indic :=
1126 Make_Subtype_Indication (Loc,
1127 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1128 Constraint =>
1129 Make_Index_Or_Discriminant_Constraint (Loc,
1130 Constraints => New_List (
1131 Make_Range (Loc,
1132 Low_Bound =>
1133 Make_Attribute_Reference (Loc,
1134 Prefix => New_Occurrence_Of (Var, Loc),
1135 Attribute_Name => Name_First),
1136 High_Bound =>
1137 Make_Attribute_Reference (Loc,
1138 Prefix => New_Occurrence_Of (Var, Loc),
1139 Attribute_Name => Name_Last)))));
1141 else
1142 Init := New_Occurrence_Of (Var, Loc);
1143 end if;
1145 -- An initialization is created for packed conversions as
1146 -- actuals for out parameters to enable Make_Object_Declaration
1147 -- to determine the proper subtype for N_Node. Note that this
1148 -- is wasteful because the extra copying on the call side is
1149 -- not required for such out parameters. ???
1151 elsif Ekind (Formal) = E_Out_Parameter
1152 and then Nkind (Actual) = N_Type_Conversion
1153 and then (Is_Bit_Packed_Array (F_Typ)
1154 or else
1155 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1156 then
1157 if Conversion_OK (Actual) then
1158 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1159 else
1160 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1161 end if;
1163 elsif Ekind (Formal) = E_In_Parameter then
1165 -- Handle the case in which the actual is a type conversion
1167 if Nkind (Actual) = N_Type_Conversion then
1168 if Conversion_OK (Actual) then
1169 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1170 else
1171 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1172 end if;
1173 else
1174 Init := New_Occurrence_Of (Var, Loc);
1175 end if;
1177 else
1178 Init := Empty;
1179 end if;
1181 N_Node :=
1182 Make_Object_Declaration (Loc,
1183 Defining_Identifier => Temp,
1184 Object_Definition => Indic,
1185 Expression => Init);
1186 Set_Assignment_OK (N_Node);
1187 Insert_Action (N, N_Node);
1189 -- Now, normally the deal here is that we use the defining
1190 -- identifier created by that object declaration. There is
1191 -- one exception to this. In the change of representation case
1192 -- the above declaration will end up looking like:
1194 -- temp : type := identifier;
1196 -- And in this case we might as well use the identifier directly
1197 -- and eliminate the temporary. Note that the analysis of the
1198 -- declaration was not a waste of time in that case, since it is
1199 -- what generated the necessary change of representation code. If
1200 -- the change of representation introduced additional code, as in
1201 -- a fixed-integer conversion, the expression is not an identifier
1202 -- and must be kept.
1204 if Crep
1205 and then Present (Expression (N_Node))
1206 and then Is_Entity_Name (Expression (N_Node))
1207 then
1208 Temp := Entity (Expression (N_Node));
1209 Rewrite (N_Node, Make_Null_Statement (Loc));
1210 end if;
1212 -- For IN parameter, all we do is to replace the actual
1214 if Ekind (Formal) = E_In_Parameter then
1215 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1216 Analyze (Actual);
1218 -- Processing for OUT or IN OUT parameter
1220 else
1221 -- Kill current value indications for the temporary variable we
1222 -- created, since we just passed it as an OUT parameter.
1224 Kill_Current_Values (Temp);
1225 Set_Is_Known_Valid (Temp, False);
1227 -- If type conversion, use reverse conversion on exit
1229 if Nkind (Actual) = N_Type_Conversion then
1230 if Conversion_OK (Actual) then
1231 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1232 else
1233 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1234 end if;
1235 else
1236 Expr := New_Occurrence_Of (Temp, Loc);
1237 end if;
1239 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1240 Analyze (Actual);
1242 -- If the actual is a conversion of a packed reference, it may
1243 -- already have been expanded by Remove_Side_Effects, and the
1244 -- resulting variable is a temporary which does not designate
1245 -- the proper out-parameter, which may not be addressable. In
1246 -- that case, generate an assignment to the original expression
1247 -- (before expansion of the packed reference) so that the proper
1248 -- expansion of assignment to a packed component can take place.
1250 declare
1251 Obj : Node_Id;
1252 Lhs : Node_Id;
1254 begin
1255 if Is_Renaming_Of_Object (Var)
1256 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1257 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1258 = N_Indexed_Component
1259 and then
1260 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1261 then
1262 Obj := Renamed_Object (Var);
1263 Lhs :=
1264 Make_Selected_Component (Loc,
1265 Prefix =>
1266 New_Copy_Tree (Original_Node (Prefix (Obj))),
1267 Selector_Name => New_Copy (Selector_Name (Obj)));
1268 Reset_Analyzed_Flags (Lhs);
1270 else
1271 Lhs := New_Occurrence_Of (Var, Loc);
1272 end if;
1274 Set_Assignment_OK (Lhs);
1276 if Is_Access_Type (E_Formal)
1277 and then Is_Entity_Name (Lhs)
1278 and then
1279 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1280 then
1281 -- Copyback target is an Ada 2012 stand-alone object of an
1282 -- anonymous access type.
1284 pragma Assert (Ada_Version >= Ada_2012);
1286 if Type_Access_Level (E_Formal) >
1287 Object_Access_Level (Lhs)
1288 then
1289 Append_To (Post_Call,
1290 Make_Raise_Program_Error (Loc,
1291 Reason => PE_Accessibility_Check_Failed));
1292 end if;
1294 Append_To (Post_Call,
1295 Make_Assignment_Statement (Loc,
1296 Name => Lhs,
1297 Expression => Expr));
1299 -- We would like to somehow suppress generation of the
1300 -- extra_accessibility assignment generated by the expansion
1301 -- of the above assignment statement. It's not a correctness
1302 -- issue because the following assignment renders it dead,
1303 -- but generating back-to-back assignments to the same
1304 -- target is undesirable. ???
1306 Append_To (Post_Call,
1307 Make_Assignment_Statement (Loc,
1308 Name => New_Occurrence_Of (
1309 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1310 Expression => Make_Integer_Literal (Loc,
1311 Type_Access_Level (E_Formal))));
1313 else
1314 Append_To (Post_Call,
1315 Make_Assignment_Statement (Loc,
1316 Name => Lhs,
1317 Expression => Expr));
1318 end if;
1319 end;
1320 end if;
1321 end Add_Call_By_Copy_Code;
1323 ----------------------------------
1324 -- Add_Simple_Call_By_Copy_Code --
1325 ----------------------------------
1327 procedure Add_Simple_Call_By_Copy_Code is
1328 Temp : Entity_Id;
1329 Decl : Node_Id;
1330 Incod : Node_Id;
1331 Outcod : Node_Id;
1332 Lhs : Node_Id;
1333 Rhs : Node_Id;
1334 Indic : Node_Id;
1335 F_Typ : constant Entity_Id := Etype (Formal);
1337 begin
1338 if not Is_Legal_Copy then
1339 return;
1340 end if;
1342 -- Use formal type for temp, unless formal type is an unconstrained
1343 -- array, in which case we don't have to worry about bounds checks,
1344 -- and we use the actual type, since that has appropriate bounds.
1346 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1347 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1348 else
1349 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1350 end if;
1352 -- Prepare to generate code
1354 Reset_Packed_Prefix;
1356 Temp := Make_Temporary (Loc, 'T', Actual);
1357 Incod := Relocate_Node (Actual);
1358 Outcod := New_Copy_Tree (Incod);
1360 -- Generate declaration of temporary variable, initializing it
1361 -- with the input parameter unless we have an OUT formal or
1362 -- this is an initialization call.
1364 -- If the formal is an out parameter with discriminants, the
1365 -- discriminants must be captured even if the rest of the object
1366 -- is in principle uninitialized, because the discriminants may
1367 -- be read by the called subprogram.
1369 if Ekind (Formal) = E_Out_Parameter then
1370 Incod := Empty;
1372 if Has_Discriminants (Etype (Formal)) then
1373 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1374 end if;
1376 elsif Inside_Init_Proc then
1378 -- Could use a comment here to match comment below ???
1380 if Nkind (Actual) /= N_Selected_Component
1381 or else
1382 not Has_Discriminant_Dependent_Constraint
1383 (Entity (Selector_Name (Actual)))
1384 then
1385 Incod := Empty;
1387 -- Otherwise, keep the component in order to generate the proper
1388 -- actual subtype, that depends on enclosing discriminants.
1390 else
1391 null;
1392 end if;
1393 end if;
1395 Decl :=
1396 Make_Object_Declaration (Loc,
1397 Defining_Identifier => Temp,
1398 Object_Definition => Indic,
1399 Expression => Incod);
1401 if Inside_Init_Proc
1402 and then No (Incod)
1403 then
1404 -- If the call is to initialize a component of a composite type,
1405 -- and the component does not depend on discriminants, use the
1406 -- actual type of the component. This is required in case the
1407 -- component is constrained, because in general the formal of the
1408 -- initialization procedure will be unconstrained. Note that if
1409 -- the component being initialized is constrained by an enclosing
1410 -- discriminant, the presence of the initialization in the
1411 -- declaration will generate an expression for the actual subtype.
1413 Set_No_Initialization (Decl);
1414 Set_Object_Definition (Decl,
1415 New_Occurrence_Of (Etype (Actual), Loc));
1416 end if;
1418 Insert_Action (N, Decl);
1420 -- The actual is simply a reference to the temporary
1422 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1424 -- Generate copy out if OUT or IN OUT parameter
1426 if Ekind (Formal) /= E_In_Parameter then
1427 Lhs := Outcod;
1428 Rhs := New_Occurrence_Of (Temp, Loc);
1430 -- Deal with conversion
1432 if Nkind (Lhs) = N_Type_Conversion then
1433 Lhs := Expression (Lhs);
1434 Rhs := Convert_To (Etype (Actual), Rhs);
1435 end if;
1437 Append_To (Post_Call,
1438 Make_Assignment_Statement (Loc,
1439 Name => Lhs,
1440 Expression => Rhs));
1441 Set_Assignment_OK (Name (Last (Post_Call)));
1442 end if;
1443 end Add_Simple_Call_By_Copy_Code;
1445 ---------------------------
1446 -- Check_Fortran_Logical --
1447 ---------------------------
1449 procedure Check_Fortran_Logical is
1450 Logical : constant Entity_Id := Etype (Formal);
1451 Var : Entity_Id;
1453 -- Note: this is very incomplete, e.g. it does not handle arrays
1454 -- of logical values. This is really not the right approach at all???)
1456 begin
1457 if Convention (Subp) = Convention_Fortran
1458 and then Root_Type (Etype (Formal)) = Standard_Boolean
1459 and then Ekind (Formal) /= E_In_Parameter
1460 then
1461 Var := Make_Var (Actual);
1462 Append_To (Post_Call,
1463 Make_Assignment_Statement (Loc,
1464 Name => New_Occurrence_Of (Var, Loc),
1465 Expression =>
1466 Unchecked_Convert_To (
1467 Logical,
1468 Make_Op_Ne (Loc,
1469 Left_Opnd => New_Occurrence_Of (Var, Loc),
1470 Right_Opnd =>
1471 Unchecked_Convert_To (
1472 Logical,
1473 New_Occurrence_Of (Standard_False, Loc))))));
1474 end if;
1475 end Check_Fortran_Logical;
1477 -------------------
1478 -- Is_Legal_Copy --
1479 -------------------
1481 function Is_Legal_Copy return Boolean is
1482 begin
1483 -- An attempt to copy a value of such a type can only occur if
1484 -- representation clauses give the actual a misaligned address.
1486 if Is_By_Reference_Type (Etype (Formal)) then
1488 -- If the front-end does not perform full type layout, the actual
1489 -- may in fact be properly aligned but there is not enough front-
1490 -- end information to determine this. In that case gigi will emit
1491 -- an error if a copy is not legal, or generate the proper code.
1492 -- For other backends we report the error now.
1494 -- Seems wrong to be issuing an error in the expander, since it
1495 -- will be missed in -gnatc mode ???
1497 if Frontend_Layout_On_Target then
1498 Error_Msg_N
1499 ("misaligned actual cannot be passed by reference", Actual);
1500 end if;
1502 return False;
1504 -- For users of Starlet, we assume that the specification of by-
1505 -- reference mechanism is mandatory. This may lead to unaligned
1506 -- objects but at least for DEC legacy code it is known to work.
1507 -- The warning will alert users of this code that a problem may
1508 -- be lurking.
1510 elsif Mechanism (Formal) = By_Reference
1511 and then Is_Valued_Procedure (Scope (Formal))
1512 then
1513 Error_Msg_N
1514 ("by_reference actual may be misaligned??", Actual);
1515 return False;
1517 else
1518 return True;
1519 end if;
1520 end Is_Legal_Copy;
1522 --------------
1523 -- Make_Var --
1524 --------------
1526 function Make_Var (Actual : Node_Id) return Entity_Id is
1527 Var : Entity_Id;
1529 begin
1530 if Is_Entity_Name (Actual) then
1531 return Entity (Actual);
1533 else
1534 Var := Make_Temporary (Loc, 'T', Actual);
1536 N_Node :=
1537 Make_Object_Renaming_Declaration (Loc,
1538 Defining_Identifier => Var,
1539 Subtype_Mark =>
1540 New_Occurrence_Of (Etype (Actual), Loc),
1541 Name => Relocate_Node (Actual));
1543 Insert_Action (N, N_Node);
1544 return Var;
1545 end if;
1546 end Make_Var;
1548 -------------------------
1549 -- Reset_Packed_Prefix --
1550 -------------------------
1552 procedure Reset_Packed_Prefix is
1553 Pfx : Node_Id := Actual;
1554 begin
1555 loop
1556 Set_Analyzed (Pfx, False);
1557 exit when
1558 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1559 Pfx := Prefix (Pfx);
1560 end loop;
1561 end Reset_Packed_Prefix;
1563 -- Start of processing for Expand_Actuals
1565 begin
1566 Post_Call := New_List;
1568 Formal := First_Formal (Subp);
1569 Actual := First_Actual (N);
1570 while Present (Formal) loop
1571 E_Formal := Etype (Formal);
1572 E_Actual := Etype (Actual);
1574 if Is_Scalar_Type (E_Formal)
1575 or else Nkind (Actual) = N_Slice
1576 then
1577 Check_Fortran_Logical;
1579 -- RM 6.4.1 (11)
1581 elsif Ekind (Formal) /= E_Out_Parameter then
1583 -- The unusual case of the current instance of a protected type
1584 -- requires special handling. This can only occur in the context
1585 -- of a call within the body of a protected operation.
1587 if Is_Entity_Name (Actual)
1588 and then Ekind (Entity (Actual)) = E_Protected_Type
1589 and then In_Open_Scopes (Entity (Actual))
1590 then
1591 if Scope (Subp) /= Entity (Actual) then
1592 Error_Msg_N
1593 ("operation outside protected type may not "
1594 & "call back its protected operations??", Actual);
1595 end if;
1597 Rewrite (Actual,
1598 Expand_Protected_Object_Reference (N, Entity (Actual)));
1599 end if;
1601 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1602 -- build-in-place function, then a temporary return object needs
1603 -- to be created and access to it must be passed to the function.
1604 -- Currently we limit such functions to those with inherently
1605 -- limited result subtypes, but eventually we plan to expand the
1606 -- functions that are treated as build-in-place to include other
1607 -- composite result types.
1609 if Is_Build_In_Place_Function_Call (Actual) then
1610 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1611 end if;
1613 Apply_Constraint_Check (Actual, E_Formal);
1615 -- Out parameter case. No constraint checks on access type
1616 -- RM 6.4.1 (13)
1618 elsif Is_Access_Type (E_Formal) then
1619 null;
1621 -- RM 6.4.1 (14)
1623 elsif Has_Discriminants (Base_Type (E_Formal))
1624 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1625 then
1626 Apply_Constraint_Check (Actual, E_Formal);
1628 -- RM 6.4.1 (15)
1630 else
1631 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1632 end if;
1634 -- Processing for IN-OUT and OUT parameters
1636 if Ekind (Formal) /= E_In_Parameter then
1638 -- For type conversions of arrays, apply length/range checks
1640 if Is_Array_Type (E_Formal)
1641 and then Nkind (Actual) = N_Type_Conversion
1642 then
1643 if Is_Constrained (E_Formal) then
1644 Apply_Length_Check (Expression (Actual), E_Formal);
1645 else
1646 Apply_Range_Check (Expression (Actual), E_Formal);
1647 end if;
1648 end if;
1650 -- If argument is a type conversion for a type that is passed
1651 -- by copy, then we must pass the parameter by copy.
1653 if Nkind (Actual) = N_Type_Conversion
1654 and then
1655 (Is_Numeric_Type (E_Formal)
1656 or else Is_Access_Type (E_Formal)
1657 or else Is_Enumeration_Type (E_Formal)
1658 or else Is_Bit_Packed_Array (Etype (Formal))
1659 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1661 -- Also pass by copy if change of representation
1663 or else not Same_Representation
1664 (Etype (Formal),
1665 Etype (Expression (Actual))))
1666 then
1667 Add_Call_By_Copy_Code;
1669 -- References to components of bit packed arrays are expanded
1670 -- at this point, rather than at the point of analysis of the
1671 -- actuals, to handle the expansion of the assignment to
1672 -- [in] out parameters.
1674 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1675 Add_Simple_Call_By_Copy_Code;
1677 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1678 -- because the back-end cannot cope with such objects. In other
1679 -- cases where alignment forces a copy, the back-end generates
1680 -- it properly. It should not be generated unconditionally in the
1681 -- front-end because it does not know precisely the alignment
1682 -- requirements of the target, and makes too conservative an
1683 -- estimate, leading to superfluous copies or spurious errors
1684 -- on by-reference parameters.
1686 elsif Nkind (Actual) = N_Selected_Component
1687 and then
1688 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1689 and then not Represented_As_Scalar (Etype (Formal))
1690 then
1691 Add_Simple_Call_By_Copy_Code;
1693 -- References to slices of bit packed arrays are expanded
1695 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1696 Add_Call_By_Copy_Code;
1698 -- References to possibly unaligned slices of arrays are expanded
1700 elsif Is_Possibly_Unaligned_Slice (Actual) then
1701 Add_Call_By_Copy_Code;
1703 -- Deal with access types where the actual subtype and the
1704 -- formal subtype are not the same, requiring a check.
1706 -- It is necessary to exclude tagged types because of "downward
1707 -- conversion" errors.
1709 elsif Is_Access_Type (E_Formal)
1710 and then not Same_Type (E_Formal, E_Actual)
1711 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1712 then
1713 Add_Call_By_Copy_Code;
1715 -- If the actual is not a scalar and is marked for volatile
1716 -- treatment, whereas the formal is not volatile, then pass
1717 -- by copy unless it is a by-reference type.
1719 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1720 -- because this is the enforcement of a language rule that applies
1721 -- only to "real" volatile variables, not e.g. to the address
1722 -- clause overlay case.
1724 elsif Is_Entity_Name (Actual)
1725 and then Is_Volatile (Entity (Actual))
1726 and then not Is_By_Reference_Type (E_Actual)
1727 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1728 and then not Is_Volatile (E_Formal)
1729 then
1730 Add_Call_By_Copy_Code;
1732 elsif Nkind (Actual) = N_Indexed_Component
1733 and then Is_Entity_Name (Prefix (Actual))
1734 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1735 then
1736 Add_Call_By_Copy_Code;
1738 -- Add call-by-copy code for the case of scalar out parameters
1739 -- when it is not known at compile time that the subtype of the
1740 -- formal is a subrange of the subtype of the actual (or vice
1741 -- versa for in out parameters), in order to get range checks
1742 -- on such actuals. (Maybe this case should be handled earlier
1743 -- in the if statement???)
1745 elsif Is_Scalar_Type (E_Formal)
1746 and then
1747 (not In_Subrange_Of (E_Formal, E_Actual)
1748 or else
1749 (Ekind (Formal) = E_In_Out_Parameter
1750 and then not In_Subrange_Of (E_Actual, E_Formal)))
1751 then
1752 -- Perhaps the setting back to False should be done within
1753 -- Add_Call_By_Copy_Code, since it could get set on other
1754 -- cases occurring above???
1756 if Do_Range_Check (Actual) then
1757 Set_Do_Range_Check (Actual, False);
1758 end if;
1760 Add_Call_By_Copy_Code;
1761 end if;
1763 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1764 -- by-reference parameters on exit from the call. If the actual
1765 -- is a derived type and the operation is inherited, the body
1766 -- of the operation will not contain a call to the predicate
1767 -- function, so it must be done explicitly after the call. Ditto
1768 -- if the actual is an entity of a predicated subtype.
1770 -- The rule refers to by-reference types, but a check is needed
1771 -- for by-copy types as well. That check is subsumed by the rule
1772 -- for subtype conversion on assignment, but we can generate the
1773 -- required check now.
1775 -- Note also that Subp may be either a subprogram entity for
1776 -- direct calls, or a type entity for indirect calls, which must
1777 -- be handled separately because the name does not denote an
1778 -- overloadable entity.
1780 By_Ref_Predicate_Check : declare
1781 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1782 Atyp : Entity_Id;
1784 function Is_Public_Subp return Boolean;
1785 -- Check whether the subprogram being called is a visible
1786 -- operation of the type of the actual. Used to determine
1787 -- whether an invariant check must be generated on the
1788 -- caller side.
1790 ---------------------
1791 -- Is_Public_Subp --
1792 ---------------------
1794 function Is_Public_Subp return Boolean is
1795 Pack : constant Entity_Id := Scope (Subp);
1796 Subp_Decl : Node_Id;
1798 begin
1799 if not Is_Subprogram (Subp) then
1800 return False;
1802 -- The operation may be inherited, or a primitive of the
1803 -- root type.
1805 elsif
1806 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
1807 N_Full_Type_Declaration)
1808 then
1809 Subp_Decl := Parent (Subp);
1811 else
1812 Subp_Decl := Unit_Declaration_Node (Subp);
1813 end if;
1815 return Ekind (Pack) = E_Package
1816 and then
1817 List_Containing (Subp_Decl) =
1818 Visible_Declarations
1819 (Specification (Unit_Declaration_Node (Pack)));
1820 end Is_Public_Subp;
1822 -- Start of processing for By_Ref_Predicate_Check
1824 begin
1825 if No (Aund) then
1826 Atyp := E_Actual;
1827 else
1828 Atyp := Aund;
1829 end if;
1831 if Has_Predicates (Atyp)
1832 and then Present (Predicate_Function (Atyp))
1834 -- Skip predicate checks for special cases
1836 and then Predicate_Tests_On_Arguments (Subp)
1837 then
1838 Append_To (Post_Call,
1839 Make_Predicate_Check (Atyp, Actual));
1840 end if;
1842 -- We generated caller-side invariant checks in two cases:
1844 -- a) when calling an inherited operation, where there is an
1845 -- implicit view conversion of the actual to the parent type.
1847 -- b) When the conversion is explicit
1849 -- We treat these cases separately because the required
1850 -- conversion for a) is added later when expanding the call.
1852 if Has_Invariants (Etype (Actual))
1853 and then
1854 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
1855 then
1856 if Comes_From_Source (N) and then Is_Public_Subp then
1857 Append_To (Post_Call, Make_Invariant_Call (Actual));
1858 end if;
1860 elsif Nkind (Actual) = N_Type_Conversion
1861 and then Has_Invariants (Etype (Expression (Actual)))
1862 then
1863 if Comes_From_Source (N) and then Is_Public_Subp then
1864 Append_To (Post_Call,
1865 Make_Invariant_Call (Expression (Actual)));
1866 end if;
1867 end if;
1868 end By_Ref_Predicate_Check;
1870 -- Processing for IN parameters
1872 else
1873 -- For IN parameters is in the packed array case, we expand an
1874 -- indexed component (the circuit in Exp_Ch4 deliberately left
1875 -- indexed components appearing as actuals untouched, so that
1876 -- the special processing above for the OUT and IN OUT cases
1877 -- could be performed. We could make the test in Exp_Ch4 more
1878 -- complex and have it detect the parameter mode, but it is
1879 -- easier simply to handle all cases here.)
1881 if Nkind (Actual) = N_Indexed_Component
1882 and then Is_Packed (Etype (Prefix (Actual)))
1883 then
1884 Reset_Packed_Prefix;
1885 Expand_Packed_Element_Reference (Actual);
1887 -- If we have a reference to a bit packed array, we copy it, since
1888 -- the actual must be byte aligned.
1890 -- Is this really necessary in all cases???
1892 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1893 Add_Simple_Call_By_Copy_Code;
1895 -- If a non-scalar actual is possibly unaligned, we need a copy
1897 elsif Is_Possibly_Unaligned_Object (Actual)
1898 and then not Represented_As_Scalar (Etype (Formal))
1899 then
1900 Add_Simple_Call_By_Copy_Code;
1902 -- Similarly, we have to expand slices of packed arrays here
1903 -- because the result must be byte aligned.
1905 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1906 Add_Call_By_Copy_Code;
1908 -- Only processing remaining is to pass by copy if this is a
1909 -- reference to a possibly unaligned slice, since the caller
1910 -- expects an appropriately aligned argument.
1912 elsif Is_Possibly_Unaligned_Slice (Actual) then
1913 Add_Call_By_Copy_Code;
1915 -- An unusual case: a current instance of an enclosing task can be
1916 -- an actual, and must be replaced by a reference to self.
1918 elsif Is_Entity_Name (Actual)
1919 and then Is_Task_Type (Entity (Actual))
1920 then
1921 if In_Open_Scopes (Entity (Actual)) then
1922 Rewrite (Actual,
1923 (Make_Function_Call (Loc,
1924 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1925 Analyze (Actual);
1927 -- A task type cannot otherwise appear as an actual
1929 else
1930 raise Program_Error;
1931 end if;
1932 end if;
1933 end if;
1935 Next_Formal (Formal);
1936 Next_Actual (Actual);
1937 end loop;
1939 -- Find right place to put post call stuff if it is present
1941 if not Is_Empty_List (Post_Call) then
1943 -- Cases where the call is not a member of a statement list
1945 if not Is_List_Member (N) then
1947 -- In Ada 2012 the call may be a function call in an expression
1948 -- (since OUT and IN OUT parameters are now allowed for such
1949 -- calls). The write-back of (in)-out parameters is handled
1950 -- by the back-end, but the constraint checks generated when
1951 -- subtypes of formal and actual don't match must be inserted
1952 -- in the form of assignments.
1954 if Ada_Version >= Ada_2012
1955 and then Nkind (N) = N_Function_Call
1956 then
1957 -- We used to just do handle this by climbing up parents to
1958 -- a non-statement/declaration and then simply making a call
1959 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1960 -- work. If we are in the middle of an expression, e.g. the
1961 -- condition of an IF, this call would insert after the IF
1962 -- statement, which is much too late to be doing the write
1963 -- back. For example:
1965 -- if Clobber (X) then
1966 -- Put_Line (X'Img);
1967 -- else
1968 -- goto Junk
1969 -- end if;
1971 -- Now assume Clobber changes X, if we put the write back
1972 -- after the IF, the Put_Line gets the wrong value and the
1973 -- goto causes the write back to be skipped completely.
1975 -- To deal with this, we replace the call by
1977 -- do
1978 -- Tnnn : constant function-result-type := function-call;
1979 -- Post_Call actions
1980 -- in
1981 -- Tnnn;
1982 -- end;
1984 -- Note: this won't do in Modify_Tree_For_C mode, but we
1985 -- will deal with that later (it will require creating a
1986 -- declaration for Temp, using Insert_Declaration) ???
1988 declare
1989 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
1990 FRTyp : constant Entity_Id := Etype (N);
1991 Name : constant Node_Id := Relocate_Node (N);
1993 begin
1994 Prepend_To (Post_Call,
1995 Make_Object_Declaration (Loc,
1996 Defining_Identifier => Tnnn,
1997 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
1998 Constant_Present => True,
1999 Expression => Name));
2001 Rewrite (N,
2002 Make_Expression_With_Actions (Loc,
2003 Actions => Post_Call,
2004 Expression => New_Occurrence_Of (Tnnn, Loc)));
2006 -- We don't want to just blindly call Analyze_And_Resolve
2007 -- because that would cause unwanted recursion on the call.
2008 -- So for a moment set the call as analyzed to prevent that
2009 -- recursion, and get the rest analyzed properly, then reset
2010 -- the analyzed flag, so our caller can continue.
2012 Set_Analyzed (Name, True);
2013 Analyze_And_Resolve (N, FRTyp);
2014 Set_Analyzed (Name, False);
2016 -- Reset calling argument to point to function call inside
2017 -- the expression with actions so the caller can continue
2018 -- to process the call. In spite of the fact that it is
2019 -- marked Analyzed above, it may be rewritten by Remove_
2020 -- Side_Effects if validity checks are present, so go back
2021 -- to original call.
2023 N := Original_Node (Name);
2024 end;
2026 -- If not the special Ada 2012 case of a function call, then
2027 -- we must have the triggering statement of a triggering
2028 -- alternative or an entry call alternative, and we can add
2029 -- the post call stuff to the corresponding statement list.
2031 else
2032 declare
2033 P : Node_Id;
2035 begin
2036 P := Parent (N);
2037 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
2038 N_Entry_Call_Alternative));
2040 if Is_Non_Empty_List (Statements (P)) then
2041 Insert_List_Before_And_Analyze
2042 (First (Statements (P)), Post_Call);
2043 else
2044 Set_Statements (P, Post_Call);
2045 end if;
2047 return;
2048 end;
2049 end if;
2051 -- Otherwise, normal case where N is in a statement sequence,
2052 -- just put the post-call stuff after the call statement.
2054 else
2055 Insert_Actions_After (N, Post_Call);
2056 return;
2057 end if;
2058 end if;
2060 -- The call node itself is re-analyzed in Expand_Call
2062 end Expand_Actuals;
2064 -----------------
2065 -- Expand_Call --
2066 -----------------
2068 -- This procedure handles expansion of function calls and procedure call
2069 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2070 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2072 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2073 -- Provide values of actuals for all formals in Extra_Formals list
2074 -- Replace "call" to enumeration literal function by literal itself
2075 -- Rewrite call to predefined operator as operator
2076 -- Replace actuals to in-out parameters that are numeric conversions,
2077 -- with explicit assignment to temporaries before and after the call.
2079 -- Note that the list of actuals has been filled with default expressions
2080 -- during semantic analysis of the call. Only the extra actuals required
2081 -- for the 'Constrained attribute and for accessibility checks are added
2082 -- at this point.
2084 procedure Expand_Call (N : Node_Id) is
2085 Loc : constant Source_Ptr := Sloc (N);
2086 Call_Node : Node_Id := N;
2087 Extra_Actuals : List_Id := No_List;
2088 Prev : Node_Id := Empty;
2090 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2091 -- Adds one entry to the end of the actual parameter list. Used for
2092 -- default parameters and for extra actuals (for Extra_Formals). The
2093 -- argument is an N_Parameter_Association node.
2095 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2096 -- Adds an extra actual to the list of extra actuals. Expr is the
2097 -- expression for the value of the actual, EF is the entity for the
2098 -- extra formal.
2100 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2101 -- Within an instance, a type derived from an untagged formal derived
2102 -- type inherits from the original parent, not from the actual. The
2103 -- current derivation mechanism has the derived type inherit from the
2104 -- actual, which is only correct outside of the instance. If the
2105 -- subprogram is inherited, we test for this particular case through a
2106 -- convoluted tree traversal before setting the proper subprogram to be
2107 -- called.
2109 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2110 -- Return true if E comes from an instance that is not yet frozen
2112 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2113 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2115 function New_Value (From : Node_Id) return Node_Id;
2116 -- From is the original Expression. New_Value is equivalent to a call
2117 -- to Duplicate_Subexpr with an explicit dereference when From is an
2118 -- access parameter.
2120 --------------------------
2121 -- Add_Actual_Parameter --
2122 --------------------------
2124 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2125 Actual_Expr : constant Node_Id :=
2126 Explicit_Actual_Parameter (Insert_Param);
2128 begin
2129 -- Case of insertion is first named actual
2131 if No (Prev) or else
2132 Nkind (Parent (Prev)) /= N_Parameter_Association
2133 then
2134 Set_Next_Named_Actual
2135 (Insert_Param, First_Named_Actual (Call_Node));
2136 Set_First_Named_Actual (Call_Node, Actual_Expr);
2138 if No (Prev) then
2139 if No (Parameter_Associations (Call_Node)) then
2140 Set_Parameter_Associations (Call_Node, New_List);
2141 end if;
2143 Append (Insert_Param, Parameter_Associations (Call_Node));
2145 else
2146 Insert_After (Prev, Insert_Param);
2147 end if;
2149 -- Case of insertion is not first named actual
2151 else
2152 Set_Next_Named_Actual
2153 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2154 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2155 Append (Insert_Param, Parameter_Associations (Call_Node));
2156 end if;
2158 Prev := Actual_Expr;
2159 end Add_Actual_Parameter;
2161 ----------------------
2162 -- Add_Extra_Actual --
2163 ----------------------
2165 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2166 Loc : constant Source_Ptr := Sloc (Expr);
2168 begin
2169 if Extra_Actuals = No_List then
2170 Extra_Actuals := New_List;
2171 Set_Parent (Extra_Actuals, Call_Node);
2172 end if;
2174 Append_To (Extra_Actuals,
2175 Make_Parameter_Association (Loc,
2176 Selector_Name => New_Occurrence_Of (EF, Loc),
2177 Explicit_Actual_Parameter => Expr));
2179 Analyze_And_Resolve (Expr, Etype (EF));
2181 if Nkind (Call_Node) = N_Function_Call then
2182 Set_Is_Accessibility_Actual (Parent (Expr));
2183 end if;
2184 end Add_Extra_Actual;
2186 ---------------------------
2187 -- Inherited_From_Formal --
2188 ---------------------------
2190 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2191 Par : Entity_Id;
2192 Gen_Par : Entity_Id;
2193 Gen_Prim : Elist_Id;
2194 Elmt : Elmt_Id;
2195 Indic : Node_Id;
2197 begin
2198 -- If the operation is inherited, it is attached to the corresponding
2199 -- type derivation. If the parent in the derivation is a generic
2200 -- actual, it is a subtype of the actual, and we have to recover the
2201 -- original derived type declaration to find the proper parent.
2203 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2204 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2205 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2206 N_Derived_Type_Definition
2207 or else not In_Instance
2208 then
2209 return Empty;
2211 else
2212 Indic :=
2213 Subtype_Indication
2214 (Type_Definition (Original_Node (Parent (S))));
2216 if Nkind (Indic) = N_Subtype_Indication then
2217 Par := Entity (Subtype_Mark (Indic));
2218 else
2219 Par := Entity (Indic);
2220 end if;
2221 end if;
2223 if not Is_Generic_Actual_Type (Par)
2224 or else Is_Tagged_Type (Par)
2225 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2226 or else not In_Open_Scopes (Scope (Par))
2227 then
2228 return Empty;
2229 else
2230 Gen_Par := Generic_Parent_Type (Parent (Par));
2231 end if;
2233 -- If the actual has no generic parent type, the formal is not
2234 -- a formal derived type, so nothing to inherit.
2236 if No (Gen_Par) then
2237 return Empty;
2238 end if;
2240 -- If the generic parent type is still the generic type, this is a
2241 -- private formal, not a derived formal, and there are no operations
2242 -- inherited from the formal.
2244 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2245 return Empty;
2246 end if;
2248 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2250 Elmt := First_Elmt (Gen_Prim);
2251 while Present (Elmt) loop
2252 if Chars (Node (Elmt)) = Chars (S) then
2253 declare
2254 F1 : Entity_Id;
2255 F2 : Entity_Id;
2257 begin
2258 F1 := First_Formal (S);
2259 F2 := First_Formal (Node (Elmt));
2260 while Present (F1)
2261 and then Present (F2)
2262 loop
2263 if Etype (F1) = Etype (F2)
2264 or else Etype (F2) = Gen_Par
2265 then
2266 Next_Formal (F1);
2267 Next_Formal (F2);
2268 else
2269 Next_Elmt (Elmt);
2270 exit; -- not the right subprogram
2271 end if;
2273 return Node (Elmt);
2274 end loop;
2275 end;
2277 else
2278 Next_Elmt (Elmt);
2279 end if;
2280 end loop;
2282 raise Program_Error;
2283 end Inherited_From_Formal;
2285 --------------------------
2286 -- In_Unfrozen_Instance --
2287 --------------------------
2289 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2290 S : Entity_Id;
2292 begin
2293 S := E;
2294 while Present (S) and then S /= Standard_Standard loop
2295 if Is_Generic_Instance (S)
2296 and then Present (Freeze_Node (S))
2297 and then not Analyzed (Freeze_Node (S))
2298 then
2299 return True;
2300 end if;
2302 S := Scope (S);
2303 end loop;
2305 return False;
2306 end In_Unfrozen_Instance;
2308 -------------------------
2309 -- Is_Direct_Deep_Call --
2310 -------------------------
2312 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2313 begin
2314 if Is_TSS (Subp, TSS_Deep_Adjust)
2315 or else Is_TSS (Subp, TSS_Deep_Finalize)
2316 or else Is_TSS (Subp, TSS_Deep_Initialize)
2317 then
2318 declare
2319 Actual : Node_Id;
2320 Formal : Node_Id;
2322 begin
2323 Actual := First (Parameter_Associations (N));
2324 Formal := First_Formal (Subp);
2325 while Present (Actual)
2326 and then Present (Formal)
2327 loop
2328 if Nkind (Actual) = N_Identifier
2329 and then Is_Controlling_Actual (Actual)
2330 and then Etype (Actual) = Etype (Formal)
2331 then
2332 return True;
2333 end if;
2335 Next (Actual);
2336 Next_Formal (Formal);
2337 end loop;
2338 end;
2339 end if;
2341 return False;
2342 end Is_Direct_Deep_Call;
2344 ---------------
2345 -- New_Value --
2346 ---------------
2348 function New_Value (From : Node_Id) return Node_Id is
2349 Res : constant Node_Id := Duplicate_Subexpr (From);
2350 begin
2351 if Is_Access_Type (Etype (From)) then
2352 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2353 else
2354 return Res;
2355 end if;
2356 end New_Value;
2358 -- Local variables
2360 Curr_S : constant Entity_Id := Current_Scope;
2361 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2362 Actual : Node_Id;
2363 Formal : Entity_Id;
2364 Orig_Subp : Entity_Id := Empty;
2365 Param_Count : Natural := 0;
2366 Parent_Formal : Entity_Id;
2367 Parent_Subp : Entity_Id;
2368 Scop : Entity_Id;
2369 Subp : Entity_Id;
2371 Prev_Orig : Node_Id;
2372 -- Original node for an actual, which may have been rewritten. If the
2373 -- actual is a function call that has been transformed from a selected
2374 -- component, the original node is unanalyzed. Otherwise, it carries
2375 -- semantic information used to generate additional actuals.
2377 CW_Interface_Formals_Present : Boolean := False;
2379 -- Start of processing for Expand_Call
2381 begin
2382 -- Expand the procedure call if the first actual has a dimension and if
2383 -- the procedure is Put (Ada 2012).
2385 if Ada_Version >= Ada_2012
2386 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2387 and then Present (Parameter_Associations (Call_Node))
2388 then
2389 Expand_Put_Call_With_Symbol (Call_Node);
2390 end if;
2392 -- Ignore if previous error
2394 if Nkind (Call_Node) in N_Has_Etype
2395 and then Etype (Call_Node) = Any_Type
2396 then
2397 return;
2398 end if;
2400 -- Call using access to subprogram with explicit dereference
2402 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2403 Subp := Etype (Name (Call_Node));
2404 Parent_Subp := Empty;
2406 -- Case of call to simple entry, where the Name is a selected component
2407 -- whose prefix is the task, and whose selector name is the entry name
2409 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2410 Subp := Entity (Selector_Name (Name (Call_Node)));
2411 Parent_Subp := Empty;
2413 -- Case of call to member of entry family, where Name is an indexed
2414 -- component, with the prefix being a selected component giving the
2415 -- task and entry family name, and the index being the entry index.
2417 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2418 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2419 Parent_Subp := Empty;
2421 -- Normal case
2423 else
2424 Subp := Entity (Name (Call_Node));
2425 Parent_Subp := Alias (Subp);
2427 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2428 -- if we can tell that the first parameter cannot possibly be null.
2429 -- This improves efficiency by avoiding a run-time test.
2431 -- We do not do this if Raise_Exception_Always does not exist, which
2432 -- can happen in configurable run time profiles which provide only a
2433 -- Raise_Exception.
2435 if Is_RTE (Subp, RE_Raise_Exception)
2436 and then RTE_Available (RE_Raise_Exception_Always)
2437 then
2438 declare
2439 FA : constant Node_Id :=
2440 Original_Node (First_Actual (Call_Node));
2442 begin
2443 -- The case we catch is where the first argument is obtained
2444 -- using the Identity attribute (which must always be
2445 -- non-null).
2447 if Nkind (FA) = N_Attribute_Reference
2448 and then Attribute_Name (FA) = Name_Identity
2449 then
2450 Subp := RTE (RE_Raise_Exception_Always);
2451 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2452 end if;
2453 end;
2454 end if;
2456 if Ekind (Subp) = E_Entry then
2457 Parent_Subp := Empty;
2458 end if;
2459 end if;
2461 -- Detect the following code in System.Finalization_Masters only on
2462 -- .NET/JVM targets:
2464 -- procedure Finalize (Master : in out Finalization_Master) is
2465 -- begin
2466 -- . . .
2467 -- begin
2468 -- Finalize (Curr_Ptr.all);
2470 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2471 -- cannot be named in library or user code, the compiler has to deal
2472 -- with this by transforming the call to Finalize into Deep_Finalize.
2474 if VM_Target /= No_VM
2475 and then Chars (Subp) = Name_Finalize
2476 and then Ekind (Curr_S) = E_Block
2477 and then Ekind (Scope (Curr_S)) = E_Procedure
2478 and then Chars (Scope (Curr_S)) = Name_Finalize
2479 and then Etype (First_Formal (Scope (Curr_S))) =
2480 RTE (RE_Finalization_Master)
2481 then
2482 declare
2483 Deep_Fin : constant Entity_Id :=
2484 Find_Prim_Op (RTE (RE_Root_Controlled),
2485 TSS_Deep_Finalize);
2486 begin
2487 -- Since Root_Controlled is a tagged type, the compiler should
2488 -- always generate Deep_Finalize for it.
2490 pragma Assert (Present (Deep_Fin));
2492 -- Generate:
2493 -- Deep_Finalize (Curr_Ptr.all);
2495 Rewrite (N,
2496 Make_Procedure_Call_Statement (Loc,
2497 Name =>
2498 New_Occurrence_Of (Deep_Fin, Loc),
2499 Parameter_Associations =>
2500 New_Copy_List_Tree (Parameter_Associations (N))));
2502 Analyze (N);
2503 return;
2504 end;
2505 end if;
2507 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2508 -- alternative in an asynchronous select or as an entry call in
2509 -- a conditional or timed select. Check whether the procedure call
2510 -- is a renaming of an entry and rewrite it as an entry call.
2512 if Ada_Version >= Ada_2005
2513 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2514 and then
2515 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2516 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2517 or else
2518 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2519 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2520 then
2521 declare
2522 Ren_Decl : Node_Id;
2523 Ren_Root : Entity_Id := Subp;
2525 begin
2526 -- This may be a chain of renamings, find the root
2528 if Present (Alias (Ren_Root)) then
2529 Ren_Root := Alias (Ren_Root);
2530 end if;
2532 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2533 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2535 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2536 Rewrite (Call_Node,
2537 Make_Entry_Call_Statement (Loc,
2538 Name =>
2539 New_Copy_Tree (Name (Ren_Decl)),
2540 Parameter_Associations =>
2541 New_Copy_List_Tree
2542 (Parameter_Associations (Call_Node))));
2544 return;
2545 end if;
2546 end if;
2547 end;
2548 end if;
2550 -- First step, compute extra actuals, corresponding to any Extra_Formals
2551 -- present. Note that we do not access Extra_Formals directly, instead
2552 -- we simply note the presence of the extra formals as we process the
2553 -- regular formals collecting corresponding actuals in Extra_Actuals.
2555 -- We also generate any required range checks for actuals for in formals
2556 -- as we go through the loop, since this is a convenient place to do it.
2557 -- (Though it seems that this would be better done in Expand_Actuals???)
2559 -- Special case: Thunks must not compute the extra actuals; they must
2560 -- just propagate to the target primitive their extra actuals.
2562 if Is_Thunk (Current_Scope)
2563 and then Thunk_Entity (Current_Scope) = Subp
2564 and then Present (Extra_Formals (Subp))
2565 then
2566 pragma Assert (Present (Extra_Formals (Current_Scope)));
2568 declare
2569 Target_Formal : Entity_Id;
2570 Thunk_Formal : Entity_Id;
2572 begin
2573 Target_Formal := Extra_Formals (Subp);
2574 Thunk_Formal := Extra_Formals (Current_Scope);
2575 while Present (Target_Formal) loop
2576 Add_Extra_Actual
2577 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2579 Target_Formal := Extra_Formal (Target_Formal);
2580 Thunk_Formal := Extra_Formal (Thunk_Formal);
2581 end loop;
2583 while Is_Non_Empty_List (Extra_Actuals) loop
2584 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2585 end loop;
2587 Expand_Actuals (Call_Node, Subp);
2588 return;
2589 end;
2590 end if;
2592 Formal := First_Formal (Subp);
2593 Actual := First_Actual (Call_Node);
2594 Param_Count := 1;
2595 while Present (Formal) loop
2597 -- Generate range check if required
2599 if Do_Range_Check (Actual)
2600 and then Ekind (Formal) = E_In_Parameter
2601 then
2602 Generate_Range_Check
2603 (Actual, Etype (Formal), CE_Range_Check_Failed);
2604 end if;
2606 -- Prepare to examine current entry
2608 Prev := Actual;
2609 Prev_Orig := Original_Node (Prev);
2611 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2612 -- to expand it in a further round.
2614 CW_Interface_Formals_Present :=
2615 CW_Interface_Formals_Present
2616 or else
2617 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2618 and then Is_Interface (Etype (Etype (Formal))))
2619 or else
2620 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2621 and then Is_Interface (Directly_Designated_Type
2622 (Etype (Etype (Formal)))));
2624 -- Create possible extra actual for constrained case. Usually, the
2625 -- extra actual is of the form actual'constrained, but since this
2626 -- attribute is only available for unconstrained records, TRUE is
2627 -- expanded if the type of the formal happens to be constrained (for
2628 -- instance when this procedure is inherited from an unconstrained
2629 -- record to a constrained one) or if the actual has no discriminant
2630 -- (its type is constrained). An exception to this is the case of a
2631 -- private type without discriminants. In this case we pass FALSE
2632 -- because the object has underlying discriminants with defaults.
2634 if Present (Extra_Constrained (Formal)) then
2635 if Ekind (Etype (Prev)) in Private_Kind
2636 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2637 then
2638 Add_Extra_Actual
2639 (New_Occurrence_Of (Standard_False, Loc),
2640 Extra_Constrained (Formal));
2642 elsif Is_Constrained (Etype (Formal))
2643 or else not Has_Discriminants (Etype (Prev))
2644 then
2645 Add_Extra_Actual
2646 (New_Occurrence_Of (Standard_True, Loc),
2647 Extra_Constrained (Formal));
2649 -- Do not produce extra actuals for Unchecked_Union parameters.
2650 -- Jump directly to the end of the loop.
2652 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2653 goto Skip_Extra_Actual_Generation;
2655 else
2656 -- If the actual is a type conversion, then the constrained
2657 -- test applies to the actual, not the target type.
2659 declare
2660 Act_Prev : Node_Id;
2662 begin
2663 -- Test for unchecked conversions as well, which can occur
2664 -- as out parameter actuals on calls to stream procedures.
2666 Act_Prev := Prev;
2667 while Nkind_In (Act_Prev, N_Type_Conversion,
2668 N_Unchecked_Type_Conversion)
2669 loop
2670 Act_Prev := Expression (Act_Prev);
2671 end loop;
2673 -- If the expression is a conversion of a dereference, this
2674 -- is internally generated code that manipulates addresses,
2675 -- e.g. when building interface tables. No check should
2676 -- occur in this case, and the discriminated object is not
2677 -- directly a hand.
2679 if not Comes_From_Source (Actual)
2680 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2681 and then Nkind (Act_Prev) = N_Explicit_Dereference
2682 then
2683 Add_Extra_Actual
2684 (New_Occurrence_Of (Standard_False, Loc),
2685 Extra_Constrained (Formal));
2687 else
2688 Add_Extra_Actual
2689 (Make_Attribute_Reference (Sloc (Prev),
2690 Prefix =>
2691 Duplicate_Subexpr_No_Checks
2692 (Act_Prev, Name_Req => True),
2693 Attribute_Name => Name_Constrained),
2694 Extra_Constrained (Formal));
2695 end if;
2696 end;
2697 end if;
2698 end if;
2700 -- Create possible extra actual for accessibility level
2702 if Present (Extra_Accessibility (Formal)) then
2704 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2705 -- attribute, then the original actual may be an aliased object
2706 -- occurring as the prefix in a call using "Object.Operation"
2707 -- notation. In that case we must pass the level of the object,
2708 -- so Prev_Orig is reset to Prev and the attribute will be
2709 -- processed by the code for Access attributes further below.
2711 if Prev_Orig /= Prev
2712 and then Nkind (Prev) = N_Attribute_Reference
2713 and then
2714 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2715 and then Is_Aliased_View (Prev_Orig)
2716 then
2717 Prev_Orig := Prev;
2718 end if;
2720 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2721 -- accessibility levels.
2723 if Is_Thunk (Current_Scope) then
2724 declare
2725 Parm_Ent : Entity_Id;
2727 begin
2728 if Is_Controlling_Actual (Actual) then
2730 -- Find the corresponding actual of the thunk
2732 Parm_Ent := First_Entity (Current_Scope);
2733 for J in 2 .. Param_Count loop
2734 Next_Entity (Parm_Ent);
2735 end loop;
2737 -- Handle unchecked conversion of access types generated
2738 -- in thunks (cf. Expand_Interface_Thunk).
2740 elsif Is_Access_Type (Etype (Actual))
2741 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2742 then
2743 Parm_Ent := Entity (Expression (Actual));
2745 else pragma Assert (Is_Entity_Name (Actual));
2746 Parm_Ent := Entity (Actual);
2747 end if;
2749 Add_Extra_Actual
2750 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2751 Extra_Accessibility (Formal));
2752 end;
2754 elsif Is_Entity_Name (Prev_Orig) then
2756 -- When passing an access parameter, or a renaming of an access
2757 -- parameter, as the actual to another access parameter we need
2758 -- to pass along the actual's own access level parameter. This
2759 -- is done if we are within the scope of the formal access
2760 -- parameter (if this is an inlined body the extra formal is
2761 -- irrelevant).
2763 if (Is_Formal (Entity (Prev_Orig))
2764 or else
2765 (Present (Renamed_Object (Entity (Prev_Orig)))
2766 and then
2767 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2768 and then
2769 Is_Formal
2770 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2771 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2772 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2773 then
2774 declare
2775 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2777 begin
2778 pragma Assert (Present (Parm_Ent));
2780 if Present (Extra_Accessibility (Parm_Ent)) then
2781 Add_Extra_Actual
2782 (New_Occurrence_Of
2783 (Extra_Accessibility (Parm_Ent), Loc),
2784 Extra_Accessibility (Formal));
2786 -- If the actual access parameter does not have an
2787 -- associated extra formal providing its scope level,
2788 -- then treat the actual as having library-level
2789 -- accessibility.
2791 else
2792 Add_Extra_Actual
2793 (Make_Integer_Literal (Loc,
2794 Intval => Scope_Depth (Standard_Standard)),
2795 Extra_Accessibility (Formal));
2796 end if;
2797 end;
2799 -- The actual is a normal access value, so just pass the level
2800 -- of the actual's access type.
2802 else
2803 Add_Extra_Actual
2804 (Dynamic_Accessibility_Level (Prev_Orig),
2805 Extra_Accessibility (Formal));
2806 end if;
2808 -- If the actual is an access discriminant, then pass the level
2809 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2811 elsif Nkind (Prev_Orig) = N_Selected_Component
2812 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2813 E_Discriminant
2814 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2815 E_Anonymous_Access_Type
2816 then
2817 Add_Extra_Actual
2818 (Make_Integer_Literal (Loc,
2819 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2820 Extra_Accessibility (Formal));
2822 -- All other cases
2824 else
2825 case Nkind (Prev_Orig) is
2827 when N_Attribute_Reference =>
2828 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2830 -- For X'Access, pass on the level of the prefix X
2832 when Attribute_Access =>
2834 -- If this is an Access attribute applied to the
2835 -- the current instance object passed to a type
2836 -- initialization procedure, then use the level
2837 -- of the type itself. This is not really correct,
2838 -- as there should be an extra level parameter
2839 -- passed in with _init formals (only in the case
2840 -- where the type is immutably limited), but we
2841 -- don't have an easy way currently to create such
2842 -- an extra formal (init procs aren't ever frozen).
2843 -- For now we just use the level of the type,
2844 -- which may be too shallow, but that works better
2845 -- than passing Object_Access_Level of the type,
2846 -- which can be one level too deep in some cases.
2847 -- ???
2849 if Is_Entity_Name (Prefix (Prev_Orig))
2850 and then Is_Type (Entity (Prefix (Prev_Orig)))
2851 then
2852 Add_Extra_Actual
2853 (Make_Integer_Literal (Loc,
2854 Intval =>
2855 Type_Access_Level
2856 (Entity (Prefix (Prev_Orig)))),
2857 Extra_Accessibility (Formal));
2859 else
2860 Add_Extra_Actual
2861 (Make_Integer_Literal (Loc,
2862 Intval =>
2863 Object_Access_Level
2864 (Prefix (Prev_Orig))),
2865 Extra_Accessibility (Formal));
2866 end if;
2868 -- Treat the unchecked attributes as library-level
2870 when Attribute_Unchecked_Access |
2871 Attribute_Unrestricted_Access =>
2872 Add_Extra_Actual
2873 (Make_Integer_Literal (Loc,
2874 Intval => Scope_Depth (Standard_Standard)),
2875 Extra_Accessibility (Formal));
2877 -- No other cases of attributes returning access
2878 -- values that can be passed to access parameters.
2880 when others =>
2881 raise Program_Error;
2883 end case;
2885 -- For allocators we pass the level of the execution of the
2886 -- called subprogram, which is one greater than the current
2887 -- scope level.
2889 when N_Allocator =>
2890 Add_Extra_Actual
2891 (Make_Integer_Literal (Loc,
2892 Intval => Scope_Depth (Current_Scope) + 1),
2893 Extra_Accessibility (Formal));
2895 -- For most other cases we simply pass the level of the
2896 -- actual's access type. The type is retrieved from
2897 -- Prev rather than Prev_Orig, because in some cases
2898 -- Prev_Orig denotes an original expression that has
2899 -- not been analyzed.
2901 when others =>
2902 Add_Extra_Actual
2903 (Dynamic_Accessibility_Level (Prev),
2904 Extra_Accessibility (Formal));
2905 end case;
2906 end if;
2907 end if;
2909 -- Perform the check of 4.6(49) that prevents a null value from being
2910 -- passed as an actual to an access parameter. Note that the check
2911 -- is elided in the common cases of passing an access attribute or
2912 -- access parameter as an actual. Also, we currently don't enforce
2913 -- this check for expander-generated actuals and when -gnatdj is set.
2915 if Ada_Version >= Ada_2005 then
2917 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2918 -- the intent of 6.4.1(13) is that null-exclusion checks should
2919 -- not be done for 'out' parameters, even though it refers only
2920 -- to constraint checks, and a null_exclusion is not a constraint.
2921 -- Note that AI05-0196-1 corrects this mistake in the RM.
2923 if Is_Access_Type (Etype (Formal))
2924 and then Can_Never_Be_Null (Etype (Formal))
2925 and then Ekind (Formal) /= E_Out_Parameter
2926 and then Nkind (Prev) /= N_Raise_Constraint_Error
2927 and then (Known_Null (Prev)
2928 or else not Can_Never_Be_Null (Etype (Prev)))
2929 then
2930 Install_Null_Excluding_Check (Prev);
2931 end if;
2933 -- Ada_Version < Ada_2005
2935 else
2936 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2937 or else Access_Checks_Suppressed (Subp)
2938 then
2939 null;
2941 elsif Debug_Flag_J then
2942 null;
2944 elsif not Comes_From_Source (Prev) then
2945 null;
2947 elsif Is_Entity_Name (Prev)
2948 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2949 then
2950 null;
2952 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2953 null;
2955 -- Suppress null checks when passing to access parameters of Java
2956 -- and CIL subprograms. (Should this be done for other foreign
2957 -- conventions as well ???)
2959 elsif Convention (Subp) = Convention_Java
2960 or else Convention (Subp) = Convention_CIL
2961 then
2962 null;
2964 else
2965 Install_Null_Excluding_Check (Prev);
2966 end if;
2967 end if;
2969 -- Perform appropriate validity checks on parameters that
2970 -- are entities.
2972 if Validity_Checks_On then
2973 if (Ekind (Formal) = E_In_Parameter
2974 and then Validity_Check_In_Params)
2975 or else
2976 (Ekind (Formal) = E_In_Out_Parameter
2977 and then Validity_Check_In_Out_Params)
2978 then
2979 -- If the actual is an indexed component of a packed type (or
2980 -- is an indexed or selected component whose prefix recursively
2981 -- meets this condition), it has not been expanded yet. It will
2982 -- be copied in the validity code that follows, and has to be
2983 -- expanded appropriately, so reanalyze it.
2985 -- What we do is just to unset analyzed bits on prefixes till
2986 -- we reach something that does not have a prefix.
2988 declare
2989 Nod : Node_Id;
2991 begin
2992 Nod := Actual;
2993 while Nkind_In (Nod, N_Indexed_Component,
2994 N_Selected_Component)
2995 loop
2996 Set_Analyzed (Nod, False);
2997 Nod := Prefix (Nod);
2998 end loop;
2999 end;
3001 Ensure_Valid (Actual);
3002 end if;
3003 end if;
3005 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3006 -- since this is a left side reference. We only do this for calls
3007 -- from the source program since we assume that compiler generated
3008 -- calls explicitly generate any required checks. We also need it
3009 -- only if we are doing standard validity checks, since clearly it is
3010 -- not needed if validity checks are off, and in subscript validity
3011 -- checking mode, all indexed components are checked with a call
3012 -- directly from Expand_N_Indexed_Component.
3014 if Comes_From_Source (Call_Node)
3015 and then Ekind (Formal) /= E_In_Parameter
3016 and then Validity_Checks_On
3017 and then Validity_Check_Default
3018 and then not Validity_Check_Subscripts
3019 then
3020 Check_Valid_Lvalue_Subscripts (Actual);
3021 end if;
3023 -- Mark any scalar OUT parameter that is a simple variable as no
3024 -- longer known to be valid (unless the type is always valid). This
3025 -- reflects the fact that if an OUT parameter is never set in a
3026 -- procedure, then it can become invalid on the procedure return.
3028 if Ekind (Formal) = E_Out_Parameter
3029 and then Is_Entity_Name (Actual)
3030 and then Ekind (Entity (Actual)) = E_Variable
3031 and then not Is_Known_Valid (Etype (Actual))
3032 then
3033 Set_Is_Known_Valid (Entity (Actual), False);
3034 end if;
3036 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3037 -- clear current values, since they can be clobbered. We are probably
3038 -- doing this in more places than we need to, but better safe than
3039 -- sorry when it comes to retaining bad current values.
3041 if Ekind (Formal) /= E_In_Parameter
3042 and then Is_Entity_Name (Actual)
3043 and then Present (Entity (Actual))
3044 then
3045 declare
3046 Ent : constant Entity_Id := Entity (Actual);
3047 Sav : Node_Id;
3049 begin
3050 -- For an OUT or IN OUT parameter that is an assignable entity,
3051 -- we do not want to clobber the Last_Assignment field, since
3052 -- if it is set, it was precisely because it is indeed an OUT
3053 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3054 -- since the subprogram could have returned in invalid value.
3056 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3057 and then Is_Assignable (Ent)
3058 then
3059 Sav := Last_Assignment (Ent);
3060 Kill_Current_Values (Ent);
3061 Set_Last_Assignment (Ent, Sav);
3062 Set_Is_Known_Valid (Ent, False);
3064 -- For all other cases, just kill the current values
3066 else
3067 Kill_Current_Values (Ent);
3068 end if;
3069 end;
3070 end if;
3072 -- If the formal is class wide and the actual is an aggregate, force
3073 -- evaluation so that the back end who does not know about class-wide
3074 -- type, does not generate a temporary of the wrong size.
3076 if not Is_Class_Wide_Type (Etype (Formal)) then
3077 null;
3079 elsif Nkind (Actual) = N_Aggregate
3080 or else (Nkind (Actual) = N_Qualified_Expression
3081 and then Nkind (Expression (Actual)) = N_Aggregate)
3082 then
3083 Force_Evaluation (Actual);
3084 end if;
3086 -- In a remote call, if the formal is of a class-wide type, check
3087 -- that the actual meets the requirements described in E.4(18).
3089 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3090 Insert_Action (Actual,
3091 Make_Transportable_Check (Loc,
3092 Duplicate_Subexpr_Move_Checks (Actual)));
3093 end if;
3095 -- This label is required when skipping extra actual generation for
3096 -- Unchecked_Union parameters.
3098 <<Skip_Extra_Actual_Generation>>
3100 Param_Count := Param_Count + 1;
3101 Next_Actual (Actual);
3102 Next_Formal (Formal);
3103 end loop;
3105 -- If we are calling an Ada 2012 function which needs to have the
3106 -- "accessibility level determined by the point of call" (AI05-0234)
3107 -- passed in to it, then pass it in.
3109 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3110 and then
3111 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3112 then
3113 declare
3114 Ancestor : Node_Id := Parent (Call_Node);
3115 Level : Node_Id := Empty;
3116 Defer : Boolean := False;
3118 begin
3119 -- Unimplemented: if Subp returns an anonymous access type, then
3121 -- a) if the call is the operand of an explict conversion, then
3122 -- the target type of the conversion (a named access type)
3123 -- determines the accessibility level pass in;
3125 -- b) if the call defines an access discriminant of an object
3126 -- (e.g., the discriminant of an object being created by an
3127 -- allocator, or the discriminant of a function result),
3128 -- then the accessibility level to pass in is that of the
3129 -- discriminated object being initialized).
3131 -- ???
3133 while Nkind (Ancestor) = N_Qualified_Expression
3134 loop
3135 Ancestor := Parent (Ancestor);
3136 end loop;
3138 case Nkind (Ancestor) is
3139 when N_Allocator =>
3141 -- At this point, we'd like to assign
3143 -- Level := Dynamic_Accessibility_Level (Ancestor);
3145 -- but Etype of Ancestor may not have been set yet,
3146 -- so that doesn't work.
3148 -- Handle this later in Expand_Allocator_Expression.
3150 Defer := True;
3152 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3153 declare
3154 Def_Id : constant Entity_Id :=
3155 Defining_Identifier (Ancestor);
3157 begin
3158 if Is_Return_Object (Def_Id) then
3159 if Present (Extra_Accessibility_Of_Result
3160 (Return_Applies_To (Scope (Def_Id))))
3161 then
3162 -- Pass along value that was passed in if the
3163 -- routine we are returning from also has an
3164 -- Accessibility_Of_Result formal.
3166 Level :=
3167 New_Occurrence_Of
3168 (Extra_Accessibility_Of_Result
3169 (Return_Applies_To (Scope (Def_Id))), Loc);
3170 end if;
3171 else
3172 Level :=
3173 Make_Integer_Literal (Loc,
3174 Intval => Object_Access_Level (Def_Id));
3175 end if;
3176 end;
3178 when N_Simple_Return_Statement =>
3179 if Present (Extra_Accessibility_Of_Result
3180 (Return_Applies_To
3181 (Return_Statement_Entity (Ancestor))))
3182 then
3183 -- Pass along value that was passed in if the returned
3184 -- routine also has an Accessibility_Of_Result formal.
3186 Level :=
3187 New_Occurrence_Of
3188 (Extra_Accessibility_Of_Result
3189 (Return_Applies_To
3190 (Return_Statement_Entity (Ancestor))), Loc);
3191 end if;
3193 when others =>
3194 null;
3195 end case;
3197 if not Defer then
3198 if not Present (Level) then
3200 -- The "innermost master that evaluates the function call".
3202 -- ??? - Should we use Integer'Last here instead in order
3203 -- to deal with (some of) the problems associated with
3204 -- calls to subps whose enclosing scope is unknown (e.g.,
3205 -- Anon_Access_To_Subp_Param.all)?
3207 Level := Make_Integer_Literal (Loc,
3208 Scope_Depth (Current_Scope) + 1);
3209 end if;
3211 Add_Extra_Actual
3212 (Level,
3213 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3214 end if;
3215 end;
3216 end if;
3218 -- If we are expanding the RHS of an assignment we need to check if tag
3219 -- propagation is needed. You might expect this processing to be in
3220 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3221 -- assignment might be transformed to a declaration for an unconstrained
3222 -- value if the expression is classwide.
3224 if Nkind (Call_Node) = N_Function_Call
3225 and then Is_Tag_Indeterminate (Call_Node)
3226 and then Is_Entity_Name (Name (Call_Node))
3227 then
3228 declare
3229 Ass : Node_Id := Empty;
3231 begin
3232 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3233 Ass := Parent (Call_Node);
3235 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3236 and then Nkind (Parent (Parent (Call_Node))) =
3237 N_Assignment_Statement
3238 then
3239 Ass := Parent (Parent (Call_Node));
3241 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3242 and then Nkind (Parent (Parent (Call_Node))) =
3243 N_Assignment_Statement
3244 then
3245 Ass := Parent (Parent (Call_Node));
3246 end if;
3248 if Present (Ass)
3249 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3250 then
3251 if Is_Access_Type (Etype (Call_Node)) then
3252 if Designated_Type (Etype (Call_Node)) /=
3253 Root_Type (Etype (Name (Ass)))
3254 then
3255 Error_Msg_NE
3256 ("tag-indeterminate expression "
3257 & " must have designated type& (RM 5.2 (6))",
3258 Call_Node, Root_Type (Etype (Name (Ass))));
3259 else
3260 Propagate_Tag (Name (Ass), Call_Node);
3261 end if;
3263 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3264 Error_Msg_NE
3265 ("tag-indeterminate expression must have type&"
3266 & "(RM 5.2 (6))",
3267 Call_Node, Root_Type (Etype (Name (Ass))));
3269 else
3270 Propagate_Tag (Name (Ass), Call_Node);
3271 end if;
3273 -- The call will be rewritten as a dispatching call, and
3274 -- expanded as such.
3276 return;
3277 end if;
3278 end;
3279 end if;
3281 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3282 -- it to point to the correct secondary virtual table
3284 if Nkind (Call_Node) in N_Subprogram_Call
3285 and then CW_Interface_Formals_Present
3286 then
3287 Expand_Interface_Actuals (Call_Node);
3288 end if;
3290 -- Deals with Dispatch_Call if we still have a call, before expanding
3291 -- extra actuals since this will be done on the re-analysis of the
3292 -- dispatching call. Note that we do not try to shorten the actual list
3293 -- for a dispatching call, it would not make sense to do so. Expansion
3294 -- of dispatching calls is suppressed when VM_Target, because the VM
3295 -- back-ends directly handle the generation of dispatching calls and
3296 -- would have to undo any expansion to an indirect call.
3298 if Nkind (Call_Node) in N_Subprogram_Call
3299 and then Present (Controlling_Argument (Call_Node))
3300 then
3301 declare
3302 Call_Typ : constant Entity_Id := Etype (Call_Node);
3303 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3304 Eq_Prim_Op : Entity_Id := Empty;
3305 New_Call : Node_Id;
3306 Param : Node_Id;
3307 Prev_Call : Node_Id;
3309 begin
3310 if not Is_Limited_Type (Typ) then
3311 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3312 end if;
3314 if Tagged_Type_Expansion then
3315 Expand_Dispatching_Call (Call_Node);
3317 -- The following return is worrisome. Is it really OK to skip
3318 -- all remaining processing in this procedure ???
3320 return;
3322 -- VM targets
3324 else
3325 Apply_Tag_Checks (Call_Node);
3327 -- If this is a dispatching "=", we must first compare the
3328 -- tags so we generate: x.tag = y.tag and then x = y
3330 if Subp = Eq_Prim_Op then
3332 -- Mark the node as analyzed to avoid reanalizing this
3333 -- dispatching call (which would cause a never-ending loop)
3335 Prev_Call := Relocate_Node (Call_Node);
3336 Set_Analyzed (Prev_Call);
3338 Param := First_Actual (Call_Node);
3339 New_Call :=
3340 Make_And_Then (Loc,
3341 Left_Opnd =>
3342 Make_Op_Eq (Loc,
3343 Left_Opnd =>
3344 Make_Selected_Component (Loc,
3345 Prefix => New_Value (Param),
3346 Selector_Name =>
3347 New_Occurrence_Of
3348 (First_Tag_Component (Typ), Loc)),
3350 Right_Opnd =>
3351 Make_Selected_Component (Loc,
3352 Prefix =>
3353 Unchecked_Convert_To (Typ,
3354 New_Value (Next_Actual (Param))),
3355 Selector_Name =>
3356 New_Occurrence_Of
3357 (First_Tag_Component (Typ), Loc))),
3358 Right_Opnd => Prev_Call);
3360 Rewrite (Call_Node, New_Call);
3362 Analyze_And_Resolve
3363 (Call_Node, Call_Typ, Suppress => All_Checks);
3364 end if;
3366 -- Expansion of a dispatching call results in an indirect call,
3367 -- which in turn causes current values to be killed (see
3368 -- Resolve_Call), so on VM targets we do the call here to
3369 -- ensure consistent warnings between VM and non-VM targets.
3371 Kill_Current_Values;
3372 end if;
3374 -- If this is a dispatching "=" then we must update the reference
3375 -- to the call node because we generated:
3376 -- x.tag = y.tag and then x = y
3378 if Subp = Eq_Prim_Op then
3379 Call_Node := Right_Opnd (Call_Node);
3380 end if;
3381 end;
3382 end if;
3384 -- Similarly, expand calls to RCI subprograms on which pragma
3385 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3386 -- later. Do this only when the call comes from source since we
3387 -- do not want such a rewriting to occur in expanded code.
3389 if Is_All_Remote_Call (Call_Node) then
3390 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3392 -- Similarly, do not add extra actuals for an entry call whose entity
3393 -- is a protected procedure, or for an internal protected subprogram
3394 -- call, because it will be rewritten as a protected subprogram call
3395 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3397 elsif Is_Protected_Type (Scope (Subp))
3398 and then (Ekind (Subp) = E_Procedure
3399 or else Ekind (Subp) = E_Function)
3400 then
3401 null;
3403 -- During that loop we gathered the extra actuals (the ones that
3404 -- correspond to Extra_Formals), so now they can be appended.
3406 else
3407 while Is_Non_Empty_List (Extra_Actuals) loop
3408 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3409 end loop;
3410 end if;
3412 -- At this point we have all the actuals, so this is the point at which
3413 -- the various expansion activities for actuals is carried out.
3415 Expand_Actuals (Call_Node, Subp);
3417 -- Verify that the actuals do not share storage. This check must be done
3418 -- on the caller side rather that inside the subprogram to avoid issues
3419 -- of parameter passing.
3421 if Check_Aliasing_Of_Parameters then
3422 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3423 end if;
3425 -- If the subprogram is a renaming, or if it is inherited, replace it in
3426 -- the call with the name of the actual subprogram being called. If this
3427 -- is a dispatching call, the run-time decides what to call. The Alias
3428 -- attribute does not apply to entries.
3430 if Nkind (Call_Node) /= N_Entry_Call_Statement
3431 and then No (Controlling_Argument (Call_Node))
3432 and then Present (Parent_Subp)
3433 and then not Is_Direct_Deep_Call (Subp)
3434 then
3435 if Present (Inherited_From_Formal (Subp)) then
3436 Parent_Subp := Inherited_From_Formal (Subp);
3437 else
3438 Parent_Subp := Ultimate_Alias (Parent_Subp);
3439 end if;
3441 -- The below setting of Entity is suspect, see F109-018 discussion???
3443 Set_Entity (Name (Call_Node), Parent_Subp);
3445 if Is_Abstract_Subprogram (Parent_Subp)
3446 and then not In_Instance
3447 then
3448 Error_Msg_NE
3449 ("cannot call abstract subprogram &!",
3450 Name (Call_Node), Parent_Subp);
3451 end if;
3453 -- Inspect all formals of derived subprogram Subp. Compare parameter
3454 -- types with the parent subprogram and check whether an actual may
3455 -- need a type conversion to the corresponding formal of the parent
3456 -- subprogram.
3458 -- Not clear whether intrinsic subprograms need such conversions. ???
3460 if not Is_Intrinsic_Subprogram (Parent_Subp)
3461 or else Is_Generic_Instance (Parent_Subp)
3462 then
3463 declare
3464 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3465 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3466 -- and resolve the newly generated construct.
3468 -------------
3469 -- Convert --
3470 -------------
3472 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3473 begin
3474 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3475 Analyze (Act);
3476 Resolve (Act, Typ);
3477 end Convert;
3479 -- Local variables
3481 Actual_Typ : Entity_Id;
3482 Formal_Typ : Entity_Id;
3483 Parent_Typ : Entity_Id;
3485 begin
3486 Actual := First_Actual (Call_Node);
3487 Formal := First_Formal (Subp);
3488 Parent_Formal := First_Formal (Parent_Subp);
3489 while Present (Formal) loop
3490 Actual_Typ := Etype (Actual);
3491 Formal_Typ := Etype (Formal);
3492 Parent_Typ := Etype (Parent_Formal);
3494 -- For an IN parameter of a scalar type, the parent formal
3495 -- type and derived formal type differ or the parent formal
3496 -- type and actual type do not match statically.
3498 if Is_Scalar_Type (Formal_Typ)
3499 and then Ekind (Formal) = E_In_Parameter
3500 and then Formal_Typ /= Parent_Typ
3501 and then
3502 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3503 and then not Raises_Constraint_Error (Actual)
3504 then
3505 Convert (Actual, Parent_Typ);
3506 Enable_Range_Check (Actual);
3508 -- If the actual has been marked as requiring a range
3509 -- check, then generate it here.
3511 if Do_Range_Check (Actual) then
3512 Generate_Range_Check
3513 (Actual, Etype (Formal), CE_Range_Check_Failed);
3514 end if;
3516 -- For access types, the parent formal type and actual type
3517 -- differ.
3519 elsif Is_Access_Type (Formal_Typ)
3520 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3521 then
3522 if Ekind (Formal) /= E_In_Parameter then
3523 Convert (Actual, Parent_Typ);
3525 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3526 and then Designated_Type (Parent_Typ) /=
3527 Designated_Type (Actual_Typ)
3528 and then not Is_Controlling_Formal (Formal)
3529 then
3530 -- This unchecked conversion is not necessary unless
3531 -- inlining is enabled, because in that case the type
3532 -- mismatch may become visible in the body about to be
3533 -- inlined.
3535 Rewrite (Actual,
3536 Unchecked_Convert_To (Parent_Typ,
3537 Relocate_Node (Actual)));
3538 Analyze (Actual);
3539 Resolve (Actual, Parent_Typ);
3540 end if;
3542 -- If there is a change of representation, then generate a
3543 -- warning, and do the change of representation.
3545 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3546 Error_Msg_N
3547 ("??change of representation required", Actual);
3548 Convert (Actual, Parent_Typ);
3550 -- For array and record types, the parent formal type and
3551 -- derived formal type have different sizes or pragma Pack
3552 -- status.
3554 elsif ((Is_Array_Type (Formal_Typ)
3555 and then Is_Array_Type (Parent_Typ))
3556 or else
3557 (Is_Record_Type (Formal_Typ)
3558 and then Is_Record_Type (Parent_Typ)))
3559 and then
3560 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3561 or else Has_Pragma_Pack (Formal_Typ) /=
3562 Has_Pragma_Pack (Parent_Typ))
3563 then
3564 Convert (Actual, Parent_Typ);
3565 end if;
3567 Next_Actual (Actual);
3568 Next_Formal (Formal);
3569 Next_Formal (Parent_Formal);
3570 end loop;
3571 end;
3572 end if;
3574 Orig_Subp := Subp;
3575 Subp := Parent_Subp;
3576 end if;
3578 -- Deal with case where call is an explicit dereference
3580 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3582 -- Handle case of access to protected subprogram type
3584 if Is_Access_Protected_Subprogram_Type
3585 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3586 then
3587 -- If this is a call through an access to protected operation, the
3588 -- prefix has the form (object'address, operation'access). Rewrite
3589 -- as a for other protected calls: the object is the 1st parameter
3590 -- of the list of actuals.
3592 declare
3593 Call : Node_Id;
3594 Parm : List_Id;
3595 Nam : Node_Id;
3596 Obj : Node_Id;
3597 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3599 T : constant Entity_Id :=
3600 Equivalent_Type (Base_Type (Etype (Ptr)));
3602 D_T : constant Entity_Id :=
3603 Designated_Type (Base_Type (Etype (Ptr)));
3605 begin
3606 Obj :=
3607 Make_Selected_Component (Loc,
3608 Prefix => Unchecked_Convert_To (T, Ptr),
3609 Selector_Name =>
3610 New_Occurrence_Of (First_Entity (T), Loc));
3612 Nam :=
3613 Make_Selected_Component (Loc,
3614 Prefix => Unchecked_Convert_To (T, Ptr),
3615 Selector_Name =>
3616 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3618 Nam :=
3619 Make_Explicit_Dereference (Loc,
3620 Prefix => Nam);
3622 if Present (Parameter_Associations (Call_Node)) then
3623 Parm := Parameter_Associations (Call_Node);
3624 else
3625 Parm := New_List;
3626 end if;
3628 Prepend (Obj, Parm);
3630 if Etype (D_T) = Standard_Void_Type then
3631 Call :=
3632 Make_Procedure_Call_Statement (Loc,
3633 Name => Nam,
3634 Parameter_Associations => Parm);
3635 else
3636 Call :=
3637 Make_Function_Call (Loc,
3638 Name => Nam,
3639 Parameter_Associations => Parm);
3640 end if;
3642 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3643 Set_Etype (Call, Etype (D_T));
3645 -- We do not re-analyze the call to avoid infinite recursion.
3646 -- We analyze separately the prefix and the object, and set
3647 -- the checks on the prefix that would otherwise be emitted
3648 -- when resolving a call.
3650 Rewrite (Call_Node, Call);
3651 Analyze (Nam);
3652 Apply_Access_Check (Nam);
3653 Analyze (Obj);
3654 return;
3655 end;
3656 end if;
3657 end if;
3659 -- If this is a call to an intrinsic subprogram, then perform the
3660 -- appropriate expansion to the corresponding tree node and we
3661 -- are all done (since after that the call is gone).
3663 -- In the case where the intrinsic is to be processed by the back end,
3664 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3665 -- since the idea in this case is to pass the call unchanged. If the
3666 -- intrinsic is an inherited unchecked conversion, and the derived type
3667 -- is the target type of the conversion, we must retain it as the return
3668 -- type of the expression. Otherwise the expansion below, which uses the
3669 -- parent operation, will yield the wrong type.
3671 if Is_Intrinsic_Subprogram (Subp) then
3672 Expand_Intrinsic_Call (Call_Node, Subp);
3674 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3675 and then Parent_Subp /= Orig_Subp
3676 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3677 then
3678 Set_Etype (Call_Node, Etype (Orig_Subp));
3679 end if;
3681 return;
3682 end if;
3684 if Ekind_In (Subp, E_Function, E_Procedure) then
3686 -- We perform two simple optimization on calls:
3688 -- a) replace calls to null procedures unconditionally;
3690 -- b) for To_Address, just do an unchecked conversion. Not only is
3691 -- this efficient, but it also avoids order of elaboration problems
3692 -- when address clauses are inlined (address expression elaborated
3693 -- at the wrong point).
3695 -- We perform these optimization regardless of whether we are in the
3696 -- main unit or in a unit in the context of the main unit, to ensure
3697 -- that tree generated is the same in both cases, for CodePeer use.
3699 if Is_RTE (Subp, RE_To_Address) then
3700 Rewrite (Call_Node,
3701 Unchecked_Convert_To
3702 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3703 return;
3705 elsif Is_Null_Procedure (Subp) then
3706 Rewrite (Call_Node, Make_Null_Statement (Loc));
3707 return;
3708 end if;
3710 -- Handle inlining. No action needed if the subprogram is not inlined
3712 if not Is_Inlined (Subp) then
3713 null;
3715 -- Handle frontend inlining
3717 elsif not Back_End_Inlining then
3718 Inlined_Subprogram : declare
3719 Bod : Node_Id;
3720 Must_Inline : Boolean := False;
3721 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3723 begin
3724 -- Verify that the body to inline has already been seen, and
3725 -- that if the body is in the current unit the inlining does
3726 -- not occur earlier. This avoids order-of-elaboration problems
3727 -- in the back end.
3729 -- This should be documented in sinfo/einfo ???
3731 if No (Spec)
3732 or else Nkind (Spec) /= N_Subprogram_Declaration
3733 or else No (Body_To_Inline (Spec))
3734 then
3735 Must_Inline := False;
3737 -- If this an inherited function that returns a private type,
3738 -- do not inline if the full view is an unconstrained array,
3739 -- because such calls cannot be inlined.
3741 elsif Present (Orig_Subp)
3742 and then Is_Array_Type (Etype (Orig_Subp))
3743 and then not Is_Constrained (Etype (Orig_Subp))
3744 then
3745 Must_Inline := False;
3747 elsif In_Unfrozen_Instance (Scope (Subp)) then
3748 Must_Inline := False;
3750 else
3751 Bod := Body_To_Inline (Spec);
3753 if (In_Extended_Main_Code_Unit (Call_Node)
3754 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3755 or else Has_Pragma_Inline_Always (Subp))
3756 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3757 or else
3758 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3759 then
3760 Must_Inline := True;
3762 -- If we are compiling a package body that is not the main
3763 -- unit, it must be for inlining/instantiation purposes,
3764 -- in which case we inline the call to insure that the same
3765 -- temporaries are generated when compiling the body by
3766 -- itself. Otherwise link errors can occur.
3768 -- If the function being called is itself in the main unit,
3769 -- we cannot inline, because there is a risk of double
3770 -- elaboration and/or circularity: the inlining can make
3771 -- visible a private entity in the body of the main unit,
3772 -- that gigi will see before its sees its proper definition.
3774 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3775 and then In_Package_Body
3776 then
3777 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3778 end if;
3779 end if;
3781 if Must_Inline then
3782 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3784 else
3785 -- Let the back end handle it
3787 Add_Inlined_Body (Subp, Call_Node);
3789 if Front_End_Inlining
3790 and then Nkind (Spec) = N_Subprogram_Declaration
3791 and then (In_Extended_Main_Code_Unit (Call_Node))
3792 and then No (Body_To_Inline (Spec))
3793 and then not Has_Completion (Subp)
3794 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3795 then
3796 Cannot_Inline
3797 ("cannot inline& (body not seen yet)?",
3798 Call_Node, Subp);
3799 end if;
3800 end if;
3801 end Inlined_Subprogram;
3803 -- Back end inlining: let the back end handle it
3805 elsif No (Unit_Declaration_Node (Subp))
3806 or else Nkind (Unit_Declaration_Node (Subp)) /=
3807 N_Subprogram_Declaration
3808 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
3809 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
3810 N_Entity
3811 then
3812 Add_Inlined_Body (Subp, Call_Node);
3814 -- Front end expansion of simple functions returning unconstrained
3815 -- types (see Check_And_Split_Unconstrained_Function). Note that the
3816 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
3817 -- also Build_Renamed_Body) cannot be expanded here because this may
3818 -- give rise to order-of-elaboration issues for the types of the
3819 -- parameters of the subprogram, if any.
3821 else
3822 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3823 end if;
3824 end if;
3826 -- Check for protected subprogram. This is either an intra-object call,
3827 -- or a protected function call. Protected procedure calls are rewritten
3828 -- as entry calls and handled accordingly.
3830 -- In Ada 2005, this may be an indirect call to an access parameter that
3831 -- is an access_to_subprogram. In that case the anonymous type has a
3832 -- scope that is a protected operation, but the call is a regular one.
3833 -- In either case do not expand call if subprogram is eliminated.
3835 Scop := Scope (Subp);
3837 if Nkind (Call_Node) /= N_Entry_Call_Statement
3838 and then Is_Protected_Type (Scop)
3839 and then Ekind (Subp) /= E_Subprogram_Type
3840 and then not Is_Eliminated (Subp)
3841 then
3842 -- If the call is an internal one, it is rewritten as a call to the
3843 -- corresponding unprotected subprogram.
3845 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3846 end if;
3848 -- Functions returning controlled objects need special attention. If
3849 -- the return type is limited, then the context is initialization and
3850 -- different processing applies. If the call is to a protected function,
3851 -- the expansion above will call Expand_Call recursively. Otherwise the
3852 -- function call is transformed into a temporary which obtains the
3853 -- result from the secondary stack.
3855 if Needs_Finalization (Etype (Subp)) then
3856 if not Is_Limited_View (Etype (Subp))
3857 and then
3858 (No (First_Formal (Subp))
3859 or else
3860 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3861 then
3862 Expand_Ctrl_Function_Call (Call_Node);
3864 -- Build-in-place function calls which appear in anonymous contexts
3865 -- need a transient scope to ensure the proper finalization of the
3866 -- intermediate result after its use.
3868 elsif Is_Build_In_Place_Function_Call (Call_Node)
3869 and then
3870 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3871 N_Function_Call,
3872 N_Indexed_Component,
3873 N_Object_Renaming_Declaration,
3874 N_Procedure_Call_Statement,
3875 N_Selected_Component,
3876 N_Slice)
3877 then
3878 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3879 end if;
3880 end if;
3881 end Expand_Call;
3883 -------------------------------
3884 -- Expand_Ctrl_Function_Call --
3885 -------------------------------
3887 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3888 function Is_Element_Reference (N : Node_Id) return Boolean;
3889 -- Determine whether node N denotes a reference to an Ada 2012 container
3890 -- element.
3892 --------------------------
3893 -- Is_Element_Reference --
3894 --------------------------
3896 function Is_Element_Reference (N : Node_Id) return Boolean is
3897 Ref : constant Node_Id := Original_Node (N);
3899 begin
3900 -- Analysis marks an element reference by setting the generalized
3901 -- indexing attribute of an indexed component before the component
3902 -- is rewritten into a function call.
3904 return
3905 Nkind (Ref) = N_Indexed_Component
3906 and then Present (Generalized_Indexing (Ref));
3907 end Is_Element_Reference;
3909 -- Local variables
3911 Is_Elem_Ref : constant Boolean := Is_Element_Reference (N);
3913 -- Start of processing for Expand_Ctrl_Function_Call
3915 begin
3916 -- Optimization, if the returned value (which is on the sec-stack) is
3917 -- returned again, no need to copy/readjust/finalize, we can just pass
3918 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3919 -- attachment is needed
3921 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3922 return;
3923 end if;
3925 -- Resolution is now finished, make sure we don't start analysis again
3926 -- because of the duplication.
3928 Set_Analyzed (N);
3930 -- A function which returns a controlled object uses the secondary
3931 -- stack. Rewrite the call into a temporary which obtains the result of
3932 -- the function using 'reference.
3934 Remove_Side_Effects (N);
3936 -- When the temporary function result appears inside a case expression
3937 -- or an if expression, its lifetime must be extended to match that of
3938 -- the context. If not, the function result will be finalized too early
3939 -- and the evaluation of the expression could yield incorrect result. An
3940 -- exception to this rule are references to Ada 2012 container elements.
3941 -- Such references must be finalized at the end of each iteration of the
3942 -- related quantified expression, otherwise the container will remain
3943 -- busy.
3945 if not Is_Elem_Ref
3946 and then Within_Case_Or_If_Expression (N)
3947 and then Nkind (N) = N_Explicit_Dereference
3948 then
3949 Set_Is_Processed_Transient (Entity (Prefix (N)));
3950 end if;
3951 end Expand_Ctrl_Function_Call;
3953 ----------------------------------------
3954 -- Expand_N_Extended_Return_Statement --
3955 ----------------------------------------
3957 -- If there is a Handled_Statement_Sequence, we rewrite this:
3959 -- return Result : T := <expression> do
3960 -- <handled_seq_of_stms>
3961 -- end return;
3963 -- to be:
3965 -- declare
3966 -- Result : T := <expression>;
3967 -- begin
3968 -- <handled_seq_of_stms>
3969 -- return Result;
3970 -- end;
3972 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3974 -- return Result : T := <expression>;
3976 -- to be:
3978 -- return <expression>;
3980 -- unless it's build-in-place or there's no <expression>, in which case
3981 -- we generate:
3983 -- declare
3984 -- Result : T := <expression>;
3985 -- begin
3986 -- return Result;
3987 -- end;
3989 -- Note that this case could have been written by the user as an extended
3990 -- return statement, or could have been transformed to this from a simple
3991 -- return statement.
3993 -- That is, we need to have a reified return object if there are statements
3994 -- (which might refer to it) or if we're doing build-in-place (so we can
3995 -- set its address to the final resting place or if there is no expression
3996 -- (in which case default initial values might need to be set).
3998 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
3999 Loc : constant Source_Ptr := Sloc (N);
4001 Par_Func : constant Entity_Id :=
4002 Return_Applies_To (Return_Statement_Entity (N));
4003 Result_Subt : constant Entity_Id := Etype (Par_Func);
4004 Ret_Obj_Id : constant Entity_Id :=
4005 First_Entity (Return_Statement_Entity (N));
4006 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4008 Is_Build_In_Place : constant Boolean :=
4009 Is_Build_In_Place_Function (Par_Func);
4011 Exp : Node_Id;
4012 HSS : Node_Id;
4013 Result : Node_Id;
4014 Return_Stmt : Node_Id;
4015 Stmts : List_Id;
4017 function Build_Heap_Allocator
4018 (Temp_Id : Entity_Id;
4019 Temp_Typ : Entity_Id;
4020 Func_Id : Entity_Id;
4021 Ret_Typ : Entity_Id;
4022 Alloc_Expr : Node_Id) return Node_Id;
4023 -- Create the statements necessary to allocate a return object on the
4024 -- caller's master. The master is available through implicit parameter
4025 -- BIPfinalizationmaster.
4027 -- if BIPfinalizationmaster /= null then
4028 -- declare
4029 -- type Ptr_Typ is access Ret_Typ;
4030 -- for Ptr_Typ'Storage_Pool use
4031 -- Base_Pool (BIPfinalizationmaster.all).all;
4032 -- Local : Ptr_Typ;
4034 -- begin
4035 -- procedure Allocate (...) is
4036 -- begin
4037 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4038 -- end Allocate;
4040 -- Local := <Alloc_Expr>;
4041 -- Temp_Id := Temp_Typ (Local);
4042 -- end;
4043 -- end if;
4045 -- Temp_Id is the temporary which is used to reference the internally
4046 -- created object in all allocation forms. Temp_Typ is the type of the
4047 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4048 -- type of Func_Id. Alloc_Expr is the actual allocator.
4050 function Move_Activation_Chain return Node_Id;
4051 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4052 -- with parameters:
4053 -- From current activation chain
4054 -- To activation chain passed in by the caller
4055 -- New_Master master passed in by the caller
4057 --------------------------
4058 -- Build_Heap_Allocator --
4059 --------------------------
4061 function Build_Heap_Allocator
4062 (Temp_Id : Entity_Id;
4063 Temp_Typ : Entity_Id;
4064 Func_Id : Entity_Id;
4065 Ret_Typ : Entity_Id;
4066 Alloc_Expr : Node_Id) return Node_Id
4068 begin
4069 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4071 -- Processing for build-in-place object allocation. This is disabled
4072 -- on .NET/JVM because the targets do not support pools.
4074 if VM_Target = No_VM
4075 and then Needs_Finalization (Ret_Typ)
4076 then
4077 declare
4078 Decls : constant List_Id := New_List;
4079 Fin_Mas_Id : constant Entity_Id :=
4080 Build_In_Place_Formal
4081 (Func_Id, BIP_Finalization_Master);
4082 Stmts : constant List_Id := New_List;
4083 Desig_Typ : Entity_Id;
4084 Local_Id : Entity_Id;
4085 Pool_Id : Entity_Id;
4086 Ptr_Typ : Entity_Id;
4088 begin
4089 -- Generate:
4090 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4092 Pool_Id := Make_Temporary (Loc, 'P');
4094 Append_To (Decls,
4095 Make_Object_Renaming_Declaration (Loc,
4096 Defining_Identifier => Pool_Id,
4097 Subtype_Mark =>
4098 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4099 Name =>
4100 Make_Explicit_Dereference (Loc,
4101 Prefix =>
4102 Make_Function_Call (Loc,
4103 Name =>
4104 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4105 Parameter_Associations => New_List (
4106 Make_Explicit_Dereference (Loc,
4107 Prefix =>
4108 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4110 -- Create an access type which uses the storage pool of the
4111 -- caller's master. This additional type is necessary because
4112 -- the finalization master cannot be associated with the type
4113 -- of the temporary. Otherwise the secondary stack allocation
4114 -- will fail.
4116 Desig_Typ := Ret_Typ;
4118 -- Ensure that the build-in-place machinery uses a fat pointer
4119 -- when allocating an unconstrained array on the heap. In this
4120 -- case the result object type is a constrained array type even
4121 -- though the function type is unconstrained.
4123 if Ekind (Desig_Typ) = E_Array_Subtype then
4124 Desig_Typ := Base_Type (Desig_Typ);
4125 end if;
4127 -- Generate:
4128 -- type Ptr_Typ is access Desig_Typ;
4130 Ptr_Typ := Make_Temporary (Loc, 'P');
4132 Append_To (Decls,
4133 Make_Full_Type_Declaration (Loc,
4134 Defining_Identifier => Ptr_Typ,
4135 Type_Definition =>
4136 Make_Access_To_Object_Definition (Loc,
4137 Subtype_Indication =>
4138 New_Occurrence_Of (Desig_Typ, Loc))));
4140 -- Perform minor decoration in order to set the master and the
4141 -- storage pool attributes.
4143 Set_Ekind (Ptr_Typ, E_Access_Type);
4144 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4145 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4147 -- Create the temporary, generate:
4148 -- Local_Id : Ptr_Typ;
4150 Local_Id := Make_Temporary (Loc, 'T');
4152 Append_To (Decls,
4153 Make_Object_Declaration (Loc,
4154 Defining_Identifier => Local_Id,
4155 Object_Definition =>
4156 New_Occurrence_Of (Ptr_Typ, Loc)));
4158 -- Allocate the object, generate:
4159 -- Local_Id := <Alloc_Expr>;
4161 Append_To (Stmts,
4162 Make_Assignment_Statement (Loc,
4163 Name => New_Occurrence_Of (Local_Id, Loc),
4164 Expression => Alloc_Expr));
4166 -- Generate:
4167 -- Temp_Id := Temp_Typ (Local_Id);
4169 Append_To (Stmts,
4170 Make_Assignment_Statement (Loc,
4171 Name => New_Occurrence_Of (Temp_Id, Loc),
4172 Expression =>
4173 Unchecked_Convert_To (Temp_Typ,
4174 New_Occurrence_Of (Local_Id, Loc))));
4176 -- Wrap the allocation in a block. This is further conditioned
4177 -- by checking the caller finalization master at runtime. A
4178 -- null value indicates a non-existent master, most likely due
4179 -- to a Finalize_Storage_Only allocation.
4181 -- Generate:
4182 -- if BIPfinalizationmaster /= null then
4183 -- declare
4184 -- <Decls>
4185 -- begin
4186 -- <Stmts>
4187 -- end;
4188 -- end if;
4190 return
4191 Make_If_Statement (Loc,
4192 Condition =>
4193 Make_Op_Ne (Loc,
4194 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4195 Right_Opnd => Make_Null (Loc)),
4197 Then_Statements => New_List (
4198 Make_Block_Statement (Loc,
4199 Declarations => Decls,
4200 Handled_Statement_Sequence =>
4201 Make_Handled_Sequence_Of_Statements (Loc,
4202 Statements => Stmts))));
4203 end;
4205 -- For all other cases, generate:
4206 -- Temp_Id := <Alloc_Expr>;
4208 else
4209 return
4210 Make_Assignment_Statement (Loc,
4211 Name => New_Occurrence_Of (Temp_Id, Loc),
4212 Expression => Alloc_Expr);
4213 end if;
4214 end Build_Heap_Allocator;
4216 ---------------------------
4217 -- Move_Activation_Chain --
4218 ---------------------------
4220 function Move_Activation_Chain return Node_Id is
4221 begin
4222 return
4223 Make_Procedure_Call_Statement (Loc,
4224 Name =>
4225 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4227 Parameter_Associations => New_List (
4229 -- Source chain
4231 Make_Attribute_Reference (Loc,
4232 Prefix => Make_Identifier (Loc, Name_uChain),
4233 Attribute_Name => Name_Unrestricted_Access),
4235 -- Destination chain
4237 New_Occurrence_Of
4238 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
4240 -- New master
4242 New_Occurrence_Of
4243 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
4244 end Move_Activation_Chain;
4246 -- Start of processing for Expand_N_Extended_Return_Statement
4248 begin
4249 -- Given that functionality of interface thunks is simple (just displace
4250 -- the pointer to the object) they are always handled by means of
4251 -- simple return statements.
4253 pragma Assert (not Is_Thunk (Current_Scope));
4255 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4256 Exp := Expression (Ret_Obj_Decl);
4257 else
4258 Exp := Empty;
4259 end if;
4261 HSS := Handled_Statement_Sequence (N);
4263 -- If the returned object needs finalization actions, the function must
4264 -- perform the appropriate cleanup should it fail to return. The state
4265 -- of the function itself is tracked through a flag which is coupled
4266 -- with the scope finalizer. There is one flag per each return object
4267 -- in case of multiple returns.
4269 if Is_Build_In_Place
4270 and then Needs_Finalization (Etype (Ret_Obj_Id))
4271 then
4272 declare
4273 Flag_Decl : Node_Id;
4274 Flag_Id : Entity_Id;
4275 Func_Bod : Node_Id;
4277 begin
4278 -- Recover the function body
4280 Func_Bod := Unit_Declaration_Node (Par_Func);
4282 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4283 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4284 end if;
4286 -- Create a flag to track the function state
4288 Flag_Id := Make_Temporary (Loc, 'F');
4289 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4291 -- Insert the flag at the beginning of the function declarations,
4292 -- generate:
4293 -- Fnn : Boolean := False;
4295 Flag_Decl :=
4296 Make_Object_Declaration (Loc,
4297 Defining_Identifier => Flag_Id,
4298 Object_Definition =>
4299 New_Occurrence_Of (Standard_Boolean, Loc),
4300 Expression =>
4301 New_Occurrence_Of (Standard_False, Loc));
4303 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4304 Analyze (Flag_Decl);
4305 end;
4306 end if;
4308 -- Build a simple_return_statement that returns the return object when
4309 -- there is a statement sequence, or no expression, or the result will
4310 -- be built in place. Note however that we currently do this for all
4311 -- composite cases, even though nonlimited composite results are not yet
4312 -- built in place (though we plan to do so eventually).
4314 if Present (HSS)
4315 or else Is_Composite_Type (Result_Subt)
4316 or else No (Exp)
4317 then
4318 if No (HSS) then
4319 Stmts := New_List;
4321 -- If the extended return has a handled statement sequence, then wrap
4322 -- it in a block and use the block as the first statement.
4324 else
4325 Stmts := New_List (
4326 Make_Block_Statement (Loc,
4327 Declarations => New_List,
4328 Handled_Statement_Sequence => HSS));
4329 end if;
4331 -- If the result type contains tasks, we call Move_Activation_Chain.
4332 -- Later, the cleanup code will call Complete_Master, which will
4333 -- terminate any unactivated tasks belonging to the return statement
4334 -- master. But Move_Activation_Chain updates their master to be that
4335 -- of the caller, so they will not be terminated unless the return
4336 -- statement completes unsuccessfully due to exception, abort, goto,
4337 -- or exit. As a formality, we test whether the function requires the
4338 -- result to be built in place, though that's necessarily true for
4339 -- the case of result types with task parts.
4341 if Is_Build_In_Place
4342 and then Has_Task (Result_Subt)
4343 then
4344 -- The return expression is an aggregate for a complex type which
4345 -- contains tasks. This particular case is left unexpanded since
4346 -- the regular expansion would insert all temporaries and
4347 -- initialization code in the wrong block.
4349 if Nkind (Exp) = N_Aggregate then
4350 Expand_N_Aggregate (Exp);
4351 end if;
4353 -- Do not move the activation chain if the return object does not
4354 -- contain tasks.
4356 if Has_Task (Etype (Ret_Obj_Id)) then
4357 Append_To (Stmts, Move_Activation_Chain);
4358 end if;
4359 end if;
4361 -- Update the state of the function right before the object is
4362 -- returned.
4364 if Is_Build_In_Place
4365 and then Needs_Finalization (Etype (Ret_Obj_Id))
4366 then
4367 declare
4368 Flag_Id : constant Entity_Id :=
4369 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4371 begin
4372 -- Generate:
4373 -- Fnn := True;
4375 Append_To (Stmts,
4376 Make_Assignment_Statement (Loc,
4377 Name => New_Occurrence_Of (Flag_Id, Loc),
4378 Expression => New_Occurrence_Of (Standard_True, Loc)));
4379 end;
4380 end if;
4382 -- Build a simple_return_statement that returns the return object
4384 Return_Stmt :=
4385 Make_Simple_Return_Statement (Loc,
4386 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4387 Append_To (Stmts, Return_Stmt);
4389 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4390 end if;
4392 -- Case where we build a return statement block
4394 if Present (HSS) then
4395 Result :=
4396 Make_Block_Statement (Loc,
4397 Declarations => Return_Object_Declarations (N),
4398 Handled_Statement_Sequence => HSS);
4400 -- We set the entity of the new block statement to be that of the
4401 -- return statement. This is necessary so that various fields, such
4402 -- as Finalization_Chain_Entity carry over from the return statement
4403 -- to the block. Note that this block is unusual, in that its entity
4404 -- is an E_Return_Statement rather than an E_Block.
4406 Set_Identifier
4407 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4409 -- If the object decl was already rewritten as a renaming, then we
4410 -- don't want to do the object allocation and transformation of
4411 -- the return object declaration to a renaming. This case occurs
4412 -- when the return object is initialized by a call to another
4413 -- build-in-place function, and that function is responsible for
4414 -- the allocation of the return object.
4416 if Is_Build_In_Place
4417 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4418 then
4419 pragma Assert
4420 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4421 and then Is_Build_In_Place_Function_Call
4422 (Expression (Original_Node (Ret_Obj_Decl))));
4424 -- Return the build-in-place result by reference
4426 Set_By_Ref (Return_Stmt);
4428 elsif Is_Build_In_Place then
4430 -- Locate the implicit access parameter associated with the
4431 -- caller-supplied return object and convert the return
4432 -- statement's return object declaration to a renaming of a
4433 -- dereference of the access parameter. If the return object's
4434 -- declaration includes an expression that has not already been
4435 -- expanded as separate assignments, then add an assignment
4436 -- statement to ensure the return object gets initialized.
4438 -- declare
4439 -- Result : T [:= <expression>];
4440 -- begin
4441 -- ...
4443 -- is converted to
4445 -- declare
4446 -- Result : T renames FuncRA.all;
4447 -- [Result := <expression;]
4448 -- begin
4449 -- ...
4451 declare
4452 Return_Obj_Id : constant Entity_Id :=
4453 Defining_Identifier (Ret_Obj_Decl);
4454 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4455 Return_Obj_Expr : constant Node_Id :=
4456 Expression (Ret_Obj_Decl);
4457 Constr_Result : constant Boolean :=
4458 Is_Constrained (Result_Subt);
4459 Obj_Alloc_Formal : Entity_Id;
4460 Object_Access : Entity_Id;
4461 Obj_Acc_Deref : Node_Id;
4462 Init_Assignment : Node_Id := Empty;
4464 begin
4465 -- Build-in-place results must be returned by reference
4467 Set_By_Ref (Return_Stmt);
4469 -- Retrieve the implicit access parameter passed by the caller
4471 Object_Access :=
4472 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
4474 -- If the return object's declaration includes an expression
4475 -- and the declaration isn't marked as No_Initialization, then
4476 -- we need to generate an assignment to the object and insert
4477 -- it after the declaration before rewriting it as a renaming
4478 -- (otherwise we'll lose the initialization). The case where
4479 -- the result type is an interface (or class-wide interface)
4480 -- is also excluded because the context of the function call
4481 -- must be unconstrained, so the initialization will always
4482 -- be done as part of an allocator evaluation (storage pool
4483 -- or secondary stack), never to a constrained target object
4484 -- passed in by the caller. Besides the assignment being
4485 -- unneeded in this case, it avoids problems with trying to
4486 -- generate a dispatching assignment when the return expression
4487 -- is a nonlimited descendant of a limited interface (the
4488 -- interface has no assignment operation).
4490 if Present (Return_Obj_Expr)
4491 and then not No_Initialization (Ret_Obj_Decl)
4492 and then not Is_Interface (Return_Obj_Typ)
4493 then
4494 Init_Assignment :=
4495 Make_Assignment_Statement (Loc,
4496 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
4497 Expression => Relocate_Node (Return_Obj_Expr));
4499 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4500 Set_Assignment_OK (Name (Init_Assignment));
4501 Set_No_Ctrl_Actions (Init_Assignment);
4503 Set_Parent (Name (Init_Assignment), Init_Assignment);
4504 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4506 Set_Expression (Ret_Obj_Decl, Empty);
4508 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4509 and then not Is_Class_Wide_Type
4510 (Etype (Expression (Init_Assignment)))
4511 then
4512 Rewrite (Expression (Init_Assignment),
4513 Make_Type_Conversion (Loc,
4514 Subtype_Mark =>
4515 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
4516 Expression =>
4517 Relocate_Node (Expression (Init_Assignment))));
4518 end if;
4520 -- In the case of functions where the calling context can
4521 -- determine the form of allocation needed, initialization
4522 -- is done with each part of the if statement that handles
4523 -- the different forms of allocation (this is true for
4524 -- unconstrained and tagged result subtypes).
4526 if Constr_Result
4527 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4528 then
4529 Insert_After (Ret_Obj_Decl, Init_Assignment);
4530 end if;
4531 end if;
4533 -- When the function's subtype is unconstrained, a run-time
4534 -- test is needed to determine the form of allocation to use
4535 -- for the return object. The function has an implicit formal
4536 -- parameter indicating this. If the BIP_Alloc_Form formal has
4537 -- the value one, then the caller has passed access to an
4538 -- existing object for use as the return object. If the value
4539 -- is two, then the return object must be allocated on the
4540 -- secondary stack. Otherwise, the object must be allocated in
4541 -- a storage pool (currently only supported for the global
4542 -- heap, user-defined storage pools TBD ???). We generate an
4543 -- if statement to test the implicit allocation formal and
4544 -- initialize a local access value appropriately, creating
4545 -- allocators in the secondary stack and global heap cases.
4546 -- The special formal also exists and must be tested when the
4547 -- function has a tagged result, even when the result subtype
4548 -- is constrained, because in general such functions can be
4549 -- called in dispatching contexts and must be handled similarly
4550 -- to functions with a class-wide result.
4552 if not Constr_Result
4553 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4554 then
4555 Obj_Alloc_Formal :=
4556 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
4558 declare
4559 Pool_Id : constant Entity_Id :=
4560 Make_Temporary (Loc, 'P');
4561 Alloc_Obj_Id : Entity_Id;
4562 Alloc_Obj_Decl : Node_Id;
4563 Alloc_If_Stmt : Node_Id;
4564 Heap_Allocator : Node_Id;
4565 Pool_Decl : Node_Id;
4566 Pool_Allocator : Node_Id;
4567 Ptr_Type_Decl : Node_Id;
4568 Ref_Type : Entity_Id;
4569 SS_Allocator : Node_Id;
4571 begin
4572 -- Reuse the itype created for the function's implicit
4573 -- access formal. This avoids the need to create a new
4574 -- access type here, plus it allows assigning the access
4575 -- formal directly without applying a conversion.
4577 -- Ref_Type := Etype (Object_Access);
4579 -- Create an access type designating the function's
4580 -- result subtype.
4582 Ref_Type := Make_Temporary (Loc, 'A');
4584 Ptr_Type_Decl :=
4585 Make_Full_Type_Declaration (Loc,
4586 Defining_Identifier => Ref_Type,
4587 Type_Definition =>
4588 Make_Access_To_Object_Definition (Loc,
4589 All_Present => True,
4590 Subtype_Indication =>
4591 New_Occurrence_Of (Return_Obj_Typ, Loc)));
4593 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4595 -- Create an access object that will be initialized to an
4596 -- access value denoting the return object, either coming
4597 -- from an implicit access value passed in by the caller
4598 -- or from the result of an allocator.
4600 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4601 Set_Etype (Alloc_Obj_Id, Ref_Type);
4603 Alloc_Obj_Decl :=
4604 Make_Object_Declaration (Loc,
4605 Defining_Identifier => Alloc_Obj_Id,
4606 Object_Definition =>
4607 New_Occurrence_Of (Ref_Type, Loc));
4609 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4611 -- Create allocators for both the secondary stack and
4612 -- global heap. If there's an initialization expression,
4613 -- then create these as initialized allocators.
4615 if Present (Return_Obj_Expr)
4616 and then not No_Initialization (Ret_Obj_Decl)
4617 then
4618 -- Always use the type of the expression for the
4619 -- qualified expression, rather than the result type.
4620 -- In general we cannot always use the result type
4621 -- for the allocator, because the expression might be
4622 -- of a specific type, such as in the case of an
4623 -- aggregate or even a nonlimited object when the
4624 -- result type is a limited class-wide interface type.
4626 Heap_Allocator :=
4627 Make_Allocator (Loc,
4628 Expression =>
4629 Make_Qualified_Expression (Loc,
4630 Subtype_Mark =>
4631 New_Occurrence_Of
4632 (Etype (Return_Obj_Expr), Loc),
4633 Expression =>
4634 New_Copy_Tree (Return_Obj_Expr)));
4636 else
4637 -- If the function returns a class-wide type we cannot
4638 -- use the return type for the allocator. Instead we
4639 -- use the type of the expression, which must be an
4640 -- aggregate of a definite type.
4642 if Is_Class_Wide_Type (Return_Obj_Typ) then
4643 Heap_Allocator :=
4644 Make_Allocator (Loc,
4645 Expression =>
4646 New_Occurrence_Of
4647 (Etype (Return_Obj_Expr), Loc));
4648 else
4649 Heap_Allocator :=
4650 Make_Allocator (Loc,
4651 Expression =>
4652 New_Occurrence_Of (Return_Obj_Typ, Loc));
4653 end if;
4655 -- If the object requires default initialization then
4656 -- that will happen later following the elaboration of
4657 -- the object renaming. If we don't turn it off here
4658 -- then the object will be default initialized twice.
4660 Set_No_Initialization (Heap_Allocator);
4661 end if;
4663 -- The Pool_Allocator is just like the Heap_Allocator,
4664 -- except we set Storage_Pool and Procedure_To_Call so
4665 -- it will use the user-defined storage pool.
4667 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4669 -- Do not generate the renaming of the build-in-place
4670 -- pool parameter on .NET/JVM/ZFP because the parameter
4671 -- is not created in the first place.
4673 if VM_Target = No_VM
4674 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
4675 then
4676 Pool_Decl :=
4677 Make_Object_Renaming_Declaration (Loc,
4678 Defining_Identifier => Pool_Id,
4679 Subtype_Mark =>
4680 New_Occurrence_Of
4681 (RTE (RE_Root_Storage_Pool), Loc),
4682 Name =>
4683 Make_Explicit_Dereference (Loc,
4684 New_Occurrence_Of
4685 (Build_In_Place_Formal
4686 (Par_Func, BIP_Storage_Pool), Loc)));
4687 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4688 Set_Procedure_To_Call
4689 (Pool_Allocator, RTE (RE_Allocate_Any));
4690 else
4691 Pool_Decl := Make_Null_Statement (Loc);
4692 end if;
4694 -- If the No_Allocators restriction is active, then only
4695 -- an allocator for secondary stack allocation is needed.
4696 -- It's OK for such allocators to have Comes_From_Source
4697 -- set to False, because gigi knows not to flag them as
4698 -- being a violation of No_Implicit_Heap_Allocations.
4700 if Restriction_Active (No_Allocators) then
4701 SS_Allocator := Heap_Allocator;
4702 Heap_Allocator := Make_Null (Loc);
4703 Pool_Allocator := Make_Null (Loc);
4705 -- Otherwise the heap and pool allocators may be needed,
4706 -- so we make another allocator for secondary stack
4707 -- allocation.
4709 else
4710 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4712 -- The heap and pool allocators are marked as
4713 -- Comes_From_Source since they correspond to an
4714 -- explicit user-written allocator (that is, it will
4715 -- only be executed on behalf of callers that call the
4716 -- function as initialization for such an allocator).
4717 -- Prevents errors when No_Implicit_Heap_Allocations
4718 -- is in force.
4720 Set_Comes_From_Source (Heap_Allocator, True);
4721 Set_Comes_From_Source (Pool_Allocator, True);
4722 end if;
4724 -- The allocator is returned on the secondary stack. We
4725 -- don't do this on VM targets, since the SS is not used.
4727 if VM_Target = No_VM then
4728 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4729 Set_Procedure_To_Call
4730 (SS_Allocator, RTE (RE_SS_Allocate));
4732 -- The allocator is returned on the secondary stack,
4733 -- so indicate that the function return, as well as
4734 -- the block that encloses the allocator, must not
4735 -- release it. The flags must be set now because
4736 -- the decision to use the secondary stack is done
4737 -- very late in the course of expanding the return
4738 -- statement, past the point where these flags are
4739 -- normally set.
4741 Set_Sec_Stack_Needed_For_Return (Par_Func);
4742 Set_Sec_Stack_Needed_For_Return
4743 (Return_Statement_Entity (N));
4744 Set_Uses_Sec_Stack (Par_Func);
4745 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4746 end if;
4748 -- Create an if statement to test the BIP_Alloc_Form
4749 -- formal and initialize the access object to either the
4750 -- BIP_Object_Access formal (BIP_Alloc_Form =
4751 -- Caller_Allocation), the result of allocating the
4752 -- object in the secondary stack (BIP_Alloc_Form =
4753 -- Secondary_Stack), or else an allocator to create the
4754 -- return object in the heap or user-defined pool
4755 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4757 -- ??? An unchecked type conversion must be made in the
4758 -- case of assigning the access object formal to the
4759 -- local access object, because a normal conversion would
4760 -- be illegal in some cases (such as converting access-
4761 -- to-unconstrained to access-to-constrained), but the
4762 -- the unchecked conversion will presumably fail to work
4763 -- right in just such cases. It's not clear at all how to
4764 -- handle this. ???
4766 Alloc_If_Stmt :=
4767 Make_If_Statement (Loc,
4768 Condition =>
4769 Make_Op_Eq (Loc,
4770 Left_Opnd =>
4771 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4772 Right_Opnd =>
4773 Make_Integer_Literal (Loc,
4774 UI_From_Int (BIP_Allocation_Form'Pos
4775 (Caller_Allocation)))),
4777 Then_Statements => New_List (
4778 Make_Assignment_Statement (Loc,
4779 Name =>
4780 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4781 Expression =>
4782 Make_Unchecked_Type_Conversion (Loc,
4783 Subtype_Mark =>
4784 New_Occurrence_Of (Ref_Type, Loc),
4785 Expression =>
4786 New_Occurrence_Of (Object_Access, Loc)))),
4788 Elsif_Parts => New_List (
4789 Make_Elsif_Part (Loc,
4790 Condition =>
4791 Make_Op_Eq (Loc,
4792 Left_Opnd =>
4793 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4794 Right_Opnd =>
4795 Make_Integer_Literal (Loc,
4796 UI_From_Int (BIP_Allocation_Form'Pos
4797 (Secondary_Stack)))),
4799 Then_Statements => New_List (
4800 Make_Assignment_Statement (Loc,
4801 Name =>
4802 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4803 Expression => SS_Allocator))),
4805 Make_Elsif_Part (Loc,
4806 Condition =>
4807 Make_Op_Eq (Loc,
4808 Left_Opnd =>
4809 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4810 Right_Opnd =>
4811 Make_Integer_Literal (Loc,
4812 UI_From_Int (BIP_Allocation_Form'Pos
4813 (Global_Heap)))),
4815 Then_Statements => New_List (
4816 Build_Heap_Allocator
4817 (Temp_Id => Alloc_Obj_Id,
4818 Temp_Typ => Ref_Type,
4819 Func_Id => Par_Func,
4820 Ret_Typ => Return_Obj_Typ,
4821 Alloc_Expr => Heap_Allocator)))),
4823 Else_Statements => New_List (
4824 Pool_Decl,
4825 Build_Heap_Allocator
4826 (Temp_Id => Alloc_Obj_Id,
4827 Temp_Typ => Ref_Type,
4828 Func_Id => Par_Func,
4829 Ret_Typ => Return_Obj_Typ,
4830 Alloc_Expr => Pool_Allocator)));
4832 -- If a separate initialization assignment was created
4833 -- earlier, append that following the assignment of the
4834 -- implicit access formal to the access object, to ensure
4835 -- that the return object is initialized in that case. In
4836 -- this situation, the target of the assignment must be
4837 -- rewritten to denote a dereference of the access to the
4838 -- return object passed in by the caller.
4840 if Present (Init_Assignment) then
4841 Rewrite (Name (Init_Assignment),
4842 Make_Explicit_Dereference (Loc,
4843 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
4845 Set_Etype
4846 (Name (Init_Assignment), Etype (Return_Obj_Id));
4848 Append_To
4849 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
4850 end if;
4852 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
4854 -- Remember the local access object for use in the
4855 -- dereference of the renaming created below.
4857 Object_Access := Alloc_Obj_Id;
4858 end;
4859 end if;
4861 -- Replace the return object declaration with a renaming of a
4862 -- dereference of the access value designating the return
4863 -- object.
4865 Obj_Acc_Deref :=
4866 Make_Explicit_Dereference (Loc,
4867 Prefix => New_Occurrence_Of (Object_Access, Loc));
4869 Rewrite (Ret_Obj_Decl,
4870 Make_Object_Renaming_Declaration (Loc,
4871 Defining_Identifier => Return_Obj_Id,
4872 Access_Definition => Empty,
4873 Subtype_Mark =>
4874 New_Occurrence_Of (Return_Obj_Typ, Loc),
4875 Name => Obj_Acc_Deref));
4877 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4878 end;
4879 end if;
4881 -- Case where we do not build a block
4883 else
4884 -- We're about to drop Return_Object_Declarations on the floor, so
4885 -- we need to insert it, in case it got expanded into useful code.
4886 -- Remove side effects from expression, which may be duplicated in
4887 -- subsequent checks (see Expand_Simple_Function_Return).
4889 Insert_List_Before (N, Return_Object_Declarations (N));
4890 Remove_Side_Effects (Exp);
4892 -- Build simple_return_statement that returns the expression directly
4894 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
4895 Result := Return_Stmt;
4896 end if;
4898 -- Set the flag to prevent infinite recursion
4900 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
4902 Rewrite (N, Result);
4903 Analyze (N);
4904 end Expand_N_Extended_Return_Statement;
4906 ----------------------------
4907 -- Expand_N_Function_Call --
4908 ----------------------------
4910 procedure Expand_N_Function_Call (N : Node_Id) is
4911 begin
4912 Expand_Call (N);
4913 end Expand_N_Function_Call;
4915 ---------------------------------------
4916 -- Expand_N_Procedure_Call_Statement --
4917 ---------------------------------------
4919 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4920 GM : constant Ghost_Mode_Type := Ghost_Mode;
4922 begin
4923 -- The procedure call may be Ghost if the name is Ghost. Set the mode
4924 -- now to ensure that any nodes generated during expansion are properly
4925 -- flagged as ignored Ghost.
4927 Set_Ghost_Mode (N);
4928 Expand_Call (N);
4930 -- Restore the original Ghost mode once analysis and expansion have
4931 -- taken place.
4933 Ghost_Mode := GM;
4934 end Expand_N_Procedure_Call_Statement;
4936 --------------------------------------
4937 -- Expand_N_Simple_Return_Statement --
4938 --------------------------------------
4940 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4941 begin
4942 -- Defend against previous errors (i.e. the return statement calls a
4943 -- function that is not available in configurable runtime).
4945 if Present (Expression (N))
4946 and then Nkind (Expression (N)) = N_Empty
4947 then
4948 Check_Error_Detected;
4949 return;
4950 end if;
4952 -- Distinguish the function and non-function cases:
4954 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4956 when E_Function |
4957 E_Generic_Function =>
4958 Expand_Simple_Function_Return (N);
4960 when E_Procedure |
4961 E_Generic_Procedure |
4962 E_Entry |
4963 E_Entry_Family |
4964 E_Return_Statement =>
4965 Expand_Non_Function_Return (N);
4967 when others =>
4968 raise Program_Error;
4969 end case;
4971 exception
4972 when RE_Not_Available =>
4973 return;
4974 end Expand_N_Simple_Return_Statement;
4976 ------------------------------
4977 -- Expand_N_Subprogram_Body --
4978 ------------------------------
4980 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4981 -- by the back-end.
4983 -- Add dummy push/pop label nodes at start and end to clear any local
4984 -- exception indications if local-exception-to-goto optimization is active.
4986 -- Add return statement if last statement in body is not a return statement
4987 -- (this makes things easier on Gigi which does not want to have to handle
4988 -- a missing return).
4990 -- Add call to Activate_Tasks if body is a task activator
4992 -- Deal with possible detection of infinite recursion
4994 -- Eliminate body completely if convention stubbed
4996 -- Encode entity names within body, since we will not need to reference
4997 -- these entities any longer in the front end.
4999 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5001 -- Reset Pure indication if any parameter has root type System.Address
5002 -- or has any parameters of limited types, where limited means that the
5003 -- run-time view is limited (i.e. the full type is limited).
5005 -- Wrap thread body
5007 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5008 GM : constant Ghost_Mode_Type := Ghost_Mode;
5009 Loc : constant Source_Ptr := Sloc (N);
5010 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5011 Body_Id : Entity_Id;
5012 Except_H : Node_Id;
5013 L : List_Id;
5014 Spec_Id : Entity_Id;
5016 procedure Add_Return (S : List_Id);
5017 -- Append a return statement to the statement sequence S if the last
5018 -- statement is not already a return or a goto statement. Note that
5019 -- the latter test is not critical, it does not matter if we add a few
5020 -- extra returns, since they get eliminated anyway later on.
5022 procedure Restore_Globals;
5023 -- Restore the values of all saved global variables
5025 ----------------
5026 -- Add_Return --
5027 ----------------
5029 procedure Add_Return (S : List_Id) is
5030 Last_Stmt : Node_Id;
5031 Loc : Source_Ptr;
5032 Stmt : Node_Id;
5034 begin
5035 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5036 -- not relevant in this context since they are not executable.
5038 Last_Stmt := Last (S);
5039 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5040 Prev (Last_Stmt);
5041 end loop;
5043 -- Now insert return unless last statement is a transfer
5045 if not Is_Transfer (Last_Stmt) then
5047 -- The source location for the return is the end label of the
5048 -- procedure if present. Otherwise use the sloc of the last
5049 -- statement in the list. If the list comes from a generated
5050 -- exception handler and we are not debugging generated code,
5051 -- all the statements within the handler are made invisible
5052 -- to the debugger.
5054 if Nkind (Parent (S)) = N_Exception_Handler
5055 and then not Comes_From_Source (Parent (S))
5056 then
5057 Loc := Sloc (Last_Stmt);
5058 elsif Present (End_Label (HSS)) then
5059 Loc := Sloc (End_Label (HSS));
5060 else
5061 Loc := Sloc (Last_Stmt);
5062 end if;
5064 -- Append return statement, and set analyzed manually. We can't
5065 -- call Analyze on this return since the scope is wrong.
5067 -- Note: it almost works to push the scope and then do the Analyze
5068 -- call, but something goes wrong in some weird cases and it is
5069 -- not worth worrying about ???
5071 Stmt := Make_Simple_Return_Statement (Loc);
5073 -- The return statement is handled properly, and the call to the
5074 -- postcondition, inserted below, does not require information
5075 -- from the body either. However, that call is analyzed in the
5076 -- enclosing scope, and an elaboration check might improperly be
5077 -- added to it. A guard in Sem_Elab is needed to prevent that
5078 -- spurious check, see Check_Elab_Call.
5080 Append_To (S, Stmt);
5081 Set_Analyzed (Stmt);
5083 -- Call the _Postconditions procedure if the related subprogram
5084 -- has contract assertions that need to be verified on exit.
5086 if Ekind (Spec_Id) = E_Procedure
5087 and then Present (Postconditions_Proc (Spec_Id))
5088 then
5089 Insert_Action (Stmt,
5090 Make_Procedure_Call_Statement (Loc,
5091 Name =>
5092 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5093 end if;
5094 end if;
5095 end Add_Return;
5097 ---------------------
5098 -- Restore_Globals --
5099 ---------------------
5101 procedure Restore_Globals is
5102 begin
5103 Ghost_Mode := GM;
5104 end Restore_Globals;
5106 -- Start of processing for Expand_N_Subprogram_Body
5108 begin
5109 -- The subprogram body may be subject to pragma Ghost with policy
5110 -- Ignore. Set the mode now to ensure that any nodes generated during
5111 -- expansion are flagged as ignored Ghost.
5113 Set_Ghost_Mode (N);
5115 -- Set L to either the list of declarations if present, or to the list
5116 -- of statements if no declarations are present. This is used to insert
5117 -- new stuff at the start.
5119 if Is_Non_Empty_List (Declarations (N)) then
5120 L := Declarations (N);
5121 else
5122 L := Statements (HSS);
5123 end if;
5125 -- If local-exception-to-goto optimization active, insert dummy push
5126 -- statements at start, and dummy pop statements at end, but inhibit
5127 -- this if we have No_Exception_Handlers, since they are useless and
5128 -- intefere with analysis, e.g. by codepeer.
5130 if (Debug_Flag_Dot_G
5131 or else Restriction_Active (No_Exception_Propagation))
5132 and then not Restriction_Active (No_Exception_Handlers)
5133 and then not CodePeer_Mode
5134 and then Is_Non_Empty_List (L)
5135 then
5136 declare
5137 FS : constant Node_Id := First (L);
5138 FL : constant Source_Ptr := Sloc (FS);
5139 LS : Node_Id;
5140 LL : Source_Ptr;
5142 begin
5143 -- LS points to either last statement, if statements are present
5144 -- or to the last declaration if there are no statements present.
5145 -- It is the node after which the pop's are generated.
5147 if Is_Non_Empty_List (Statements (HSS)) then
5148 LS := Last (Statements (HSS));
5149 else
5150 LS := Last (L);
5151 end if;
5153 LL := Sloc (LS);
5155 Insert_List_Before_And_Analyze (FS, New_List (
5156 Make_Push_Constraint_Error_Label (FL),
5157 Make_Push_Program_Error_Label (FL),
5158 Make_Push_Storage_Error_Label (FL)));
5160 Insert_List_After_And_Analyze (LS, New_List (
5161 Make_Pop_Constraint_Error_Label (LL),
5162 Make_Pop_Program_Error_Label (LL),
5163 Make_Pop_Storage_Error_Label (LL)));
5164 end;
5165 end if;
5167 -- Find entity for subprogram
5169 Body_Id := Defining_Entity (N);
5171 if Present (Corresponding_Spec (N)) then
5172 Spec_Id := Corresponding_Spec (N);
5173 else
5174 Spec_Id := Body_Id;
5175 end if;
5177 -- Need poll on entry to subprogram if polling enabled. We only do this
5178 -- for non-empty subprograms, since it does not seem necessary to poll
5179 -- for a dummy null subprogram.
5181 if Is_Non_Empty_List (L) then
5183 -- Do not add a polling call if the subprogram is to be inlined by
5184 -- the back-end, to avoid repeated calls with multiple inlinings.
5186 if Is_Inlined (Spec_Id)
5187 and then Front_End_Inlining
5188 and then Optimization_Level > 1
5189 then
5190 null;
5191 else
5192 Generate_Poll_Call (First (L));
5193 end if;
5194 end if;
5196 -- If this is a Pure function which has any parameters whose root type
5197 -- is System.Address, reset the Pure indication, since it will likely
5198 -- cause incorrect code to be generated as the parameter is probably
5199 -- a pointer, and the fact that the same pointer is passed does not mean
5200 -- that the same value is being referenced.
5202 -- Note that if the programmer gave an explicit Pure_Function pragma,
5203 -- then we believe the programmer, and leave the subprogram Pure.
5205 -- This code should probably be at the freeze point, so that it happens
5206 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5207 -- semantic tree has Is_Pure set properly ???
5209 if Is_Pure (Spec_Id)
5210 and then Is_Subprogram (Spec_Id)
5211 and then not Has_Pragma_Pure_Function (Spec_Id)
5212 then
5213 declare
5214 F : Entity_Id;
5216 begin
5217 F := First_Formal (Spec_Id);
5218 while Present (F) loop
5219 if Is_Descendent_Of_Address (Etype (F))
5221 -- Note that this test is being made in the body of the
5222 -- subprogram, not the spec, so we are testing the full
5223 -- type for being limited here, as required.
5225 or else Is_Limited_Type (Etype (F))
5226 then
5227 Set_Is_Pure (Spec_Id, False);
5229 if Spec_Id /= Body_Id then
5230 Set_Is_Pure (Body_Id, False);
5231 end if;
5233 exit;
5234 end if;
5236 Next_Formal (F);
5237 end loop;
5238 end;
5239 end if;
5241 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5243 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5244 declare
5245 F : Entity_Id;
5246 A : Node_Id;
5248 begin
5249 -- Loop through formals
5251 F := First_Formal (Spec_Id);
5252 while Present (F) loop
5253 if Is_Scalar_Type (Etype (F))
5254 and then Ekind (F) = E_Out_Parameter
5255 then
5256 Check_Restriction (No_Default_Initialization, F);
5258 -- Insert the initialization. We turn off validity checks
5259 -- for this assignment, since we do not want any check on
5260 -- the initial value itself (which may well be invalid).
5261 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5263 A :=
5264 Make_Assignment_Statement (Loc,
5265 Name => New_Occurrence_Of (F, Loc),
5266 Expression => Get_Simple_Init_Val (Etype (F), N));
5267 Set_Suppress_Assignment_Checks (A);
5269 Insert_Before_And_Analyze (First (L),
5270 A, Suppress => Validity_Check);
5271 end if;
5273 Next_Formal (F);
5274 end loop;
5275 end;
5276 end if;
5278 -- Clear out statement list for stubbed procedure
5280 if Present (Corresponding_Spec (N)) then
5281 Set_Elaboration_Flag (N, Spec_Id);
5283 if Convention (Spec_Id) = Convention_Stubbed
5284 or else Is_Eliminated (Spec_Id)
5285 then
5286 Set_Declarations (N, Empty_List);
5287 Set_Handled_Statement_Sequence (N,
5288 Make_Handled_Sequence_Of_Statements (Loc,
5289 Statements => New_List (Make_Null_Statement (Loc))));
5291 Restore_Globals;
5292 return;
5293 end if;
5294 end if;
5296 -- Create a set of discriminals for the next protected subprogram body
5298 if Is_List_Member (N)
5299 and then Present (Parent (List_Containing (N)))
5300 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5301 and then Present (Next_Protected_Operation (N))
5302 then
5303 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5304 end if;
5306 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5307 -- subprograms with no specs are not frozen.
5309 declare
5310 Typ : constant Entity_Id := Etype (Spec_Id);
5311 Utyp : constant Entity_Id := Underlying_Type (Typ);
5313 begin
5314 if not Acts_As_Spec (N)
5315 and then Nkind (Parent (Parent (Spec_Id))) /=
5316 N_Subprogram_Body_Stub
5317 then
5318 null;
5320 elsif Is_Limited_View (Typ) then
5321 Set_Returns_By_Ref (Spec_Id);
5323 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5324 Set_Returns_By_Ref (Spec_Id);
5325 end if;
5326 end;
5328 -- For a procedure, we add a return for all possible syntactic ends of
5329 -- the subprogram.
5331 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5332 Add_Return (Statements (HSS));
5334 if Present (Exception_Handlers (HSS)) then
5335 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5336 while Present (Except_H) loop
5337 Add_Return (Statements (Except_H));
5338 Next_Non_Pragma (Except_H);
5339 end loop;
5340 end if;
5342 -- For a function, we must deal with the case where there is at least
5343 -- one missing return. What we do is to wrap the entire body of the
5344 -- function in a block:
5346 -- begin
5347 -- ...
5348 -- end;
5350 -- becomes
5352 -- begin
5353 -- begin
5354 -- ...
5355 -- end;
5357 -- raise Program_Error;
5358 -- end;
5360 -- This approach is necessary because the raise must be signalled to the
5361 -- caller, not handled by any local handler (RM 6.4(11)).
5363 -- Note: we do not need to analyze the constructed sequence here, since
5364 -- it has no handler, and an attempt to analyze the handled statement
5365 -- sequence twice is risky in various ways (e.g. the issue of expanding
5366 -- cleanup actions twice).
5368 elsif Has_Missing_Return (Spec_Id) then
5369 declare
5370 Hloc : constant Source_Ptr := Sloc (HSS);
5371 Blok : constant Node_Id :=
5372 Make_Block_Statement (Hloc,
5373 Handled_Statement_Sequence => HSS);
5374 Rais : constant Node_Id :=
5375 Make_Raise_Program_Error (Hloc,
5376 Reason => PE_Missing_Return);
5378 begin
5379 Set_Handled_Statement_Sequence (N,
5380 Make_Handled_Sequence_Of_Statements (Hloc,
5381 Statements => New_List (Blok, Rais)));
5383 Push_Scope (Spec_Id);
5384 Analyze (Blok);
5385 Analyze (Rais);
5386 Pop_Scope;
5387 end;
5388 end if;
5390 -- If subprogram contains a parameterless recursive call, then we may
5391 -- have an infinite recursion, so see if we can generate code to check
5392 -- for this possibility if storage checks are not suppressed.
5394 if Ekind (Spec_Id) = E_Procedure
5395 and then Has_Recursive_Call (Spec_Id)
5396 and then not Storage_Checks_Suppressed (Spec_Id)
5397 then
5398 Detect_Infinite_Recursion (N, Spec_Id);
5399 end if;
5401 -- Set to encode entity names in package body before gigi is called
5403 Qualify_Entity_Names (N);
5405 -- If we are unnesting procedures, and this is an outer level procedure
5406 -- with nested subprograms, do the unnesting operation now.
5408 if Opt.Unnest_Subprogram_Mode
5410 -- We are only interested in subprograms (not generic subprograms)
5412 and then Is_Subprogram (Spec_Id)
5414 -- Only deal with outer level subprograms. Nested subprograms are
5415 -- handled as part of dealing with the outer level subprogram in
5416 -- which they are nested.
5418 and then Enclosing_Subprogram (Spec_Id) = Empty
5420 -- We are only interested in subprograms that have nested subprograms
5422 and then Has_Nested_Subprogram (Spec_Id)
5423 then
5424 Unest_Bodies.Append ((Spec_Id, N));
5425 end if;
5427 Restore_Globals;
5428 end Expand_N_Subprogram_Body;
5430 -----------------------------------
5431 -- Expand_N_Subprogram_Body_Stub --
5432 -----------------------------------
5434 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5435 begin
5436 if Present (Corresponding_Body (N)) then
5437 Expand_N_Subprogram_Body (
5438 Unit_Declaration_Node (Corresponding_Body (N)));
5439 end if;
5440 end Expand_N_Subprogram_Body_Stub;
5442 -------------------------------------
5443 -- Expand_N_Subprogram_Declaration --
5444 -------------------------------------
5446 -- If the declaration appears within a protected body, it is a private
5447 -- operation of the protected type. We must create the corresponding
5448 -- protected subprogram an associated formals. For a normal protected
5449 -- operation, this is done when expanding the protected type declaration.
5451 -- If the declaration is for a null procedure, emit null body
5453 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5454 Loc : constant Source_Ptr := Sloc (N);
5455 GM : constant Ghost_Mode_Type := Ghost_Mode;
5456 Subp : constant Entity_Id := Defining_Entity (N);
5457 Scop : constant Entity_Id := Scope (Subp);
5458 Prot_Bod : Node_Id;
5459 Prot_Decl : Node_Id;
5460 Prot_Id : Entity_Id;
5462 begin
5463 -- The subprogram declaration may be subject to pragma Ghost with policy
5464 -- Ignore. Set the mode now to ensure that any nodes generated during
5465 -- expansion are flagged as ignored Ghost.
5467 Set_Ghost_Mode (N);
5469 -- In SPARK, subprogram declarations are only allowed in package
5470 -- specifications.
5472 if Nkind (Parent (N)) /= N_Package_Specification then
5473 if Nkind (Parent (N)) = N_Compilation_Unit then
5474 Check_SPARK_05_Restriction
5475 ("subprogram declaration is not a library item", N);
5477 elsif Present (Next (N))
5478 and then Nkind (Next (N)) = N_Pragma
5479 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
5480 then
5481 -- In SPARK, subprogram declarations are also permitted in
5482 -- declarative parts when immediately followed by a corresponding
5483 -- pragma Import. We only check here that there is some pragma
5484 -- Import.
5486 null;
5487 else
5488 Check_SPARK_05_Restriction
5489 ("subprogram declaration is not allowed here", N);
5490 end if;
5491 end if;
5493 -- Deal with case of protected subprogram. Do not generate protected
5494 -- operation if operation is flagged as eliminated.
5496 if Is_List_Member (N)
5497 and then Present (Parent (List_Containing (N)))
5498 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5499 and then Is_Protected_Type (Scop)
5500 then
5501 if No (Protected_Body_Subprogram (Subp))
5502 and then not Is_Eliminated (Subp)
5503 then
5504 Prot_Decl :=
5505 Make_Subprogram_Declaration (Loc,
5506 Specification =>
5507 Build_Protected_Sub_Specification
5508 (N, Scop, Unprotected_Mode));
5510 -- The protected subprogram is declared outside of the protected
5511 -- body. Given that the body has frozen all entities so far, we
5512 -- analyze the subprogram and perform freezing actions explicitly.
5513 -- including the generation of an explicit freeze node, to ensure
5514 -- that gigi has the proper order of elaboration.
5515 -- If the body is a subunit, the insertion point is before the
5516 -- stub in the parent.
5518 Prot_Bod := Parent (List_Containing (N));
5520 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5521 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5522 end if;
5524 Insert_Before (Prot_Bod, Prot_Decl);
5525 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5526 Set_Has_Delayed_Freeze (Prot_Id);
5528 Push_Scope (Scope (Scop));
5529 Analyze (Prot_Decl);
5530 Freeze_Before (N, Prot_Id);
5531 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5533 -- Create protected operation as well. Even though the operation
5534 -- is only accessible within the body, it is possible to make it
5535 -- available outside of the protected object by using 'Access to
5536 -- provide a callback, so build protected version in all cases.
5538 Prot_Decl :=
5539 Make_Subprogram_Declaration (Loc,
5540 Specification =>
5541 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5542 Insert_Before (Prot_Bod, Prot_Decl);
5543 Analyze (Prot_Decl);
5545 Pop_Scope;
5546 end if;
5548 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5549 -- cases this is superfluous because calls to it will be automatically
5550 -- inlined, but we definitely need the body if preconditions for the
5551 -- procedure are present.
5553 elsif Nkind (Specification (N)) = N_Procedure_Specification
5554 and then Null_Present (Specification (N))
5555 then
5556 declare
5557 Bod : constant Node_Id := Body_To_Inline (N);
5559 begin
5560 Set_Has_Completion (Subp, False);
5561 Append_Freeze_Action (Subp, Bod);
5563 -- The body now contains raise statements, so calls to it will
5564 -- not be inlined.
5566 Set_Is_Inlined (Subp, False);
5567 end;
5568 end if;
5570 -- Restore the original Ghost mode once analysis and expansion have
5571 -- taken place.
5573 Ghost_Mode := GM;
5574 end Expand_N_Subprogram_Declaration;
5576 --------------------------------
5577 -- Expand_Non_Function_Return --
5578 --------------------------------
5580 procedure Expand_Non_Function_Return (N : Node_Id) is
5581 pragma Assert (No (Expression (N)));
5583 Loc : constant Source_Ptr := Sloc (N);
5584 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5585 Kind : constant Entity_Kind := Ekind (Scope_Id);
5586 Call : Node_Id;
5587 Acc_Stat : Node_Id;
5588 Goto_Stat : Node_Id;
5589 Lab_Node : Node_Id;
5591 begin
5592 -- Call the _Postconditions procedure if the related subprogram has
5593 -- contract assertions that need to be verified on exit.
5595 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5596 and then Present (Postconditions_Proc (Scope_Id))
5597 then
5598 Insert_Action (N,
5599 Make_Procedure_Call_Statement (Loc,
5600 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5601 end if;
5603 -- If it is a return from a procedure do no extra steps
5605 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5606 return;
5608 -- If it is a nested return within an extended one, replace it with a
5609 -- return of the previously declared return object.
5611 elsif Kind = E_Return_Statement then
5612 Rewrite (N,
5613 Make_Simple_Return_Statement (Loc,
5614 Expression =>
5615 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5616 Set_Comes_From_Extended_Return_Statement (N);
5617 Set_Return_Statement_Entity (N, Scope_Id);
5618 Expand_Simple_Function_Return (N);
5619 return;
5620 end if;
5622 pragma Assert (Is_Entry (Scope_Id));
5624 -- Look at the enclosing block to see whether the return is from an
5625 -- accept statement or an entry body.
5627 for J in reverse 0 .. Scope_Stack.Last loop
5628 Scope_Id := Scope_Stack.Table (J).Entity;
5629 exit when Is_Concurrent_Type (Scope_Id);
5630 end loop;
5632 -- If it is a return from accept statement it is expanded as call to
5633 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5635 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5636 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5638 if Is_Task_Type (Scope_Id) then
5640 Call :=
5641 Make_Procedure_Call_Statement (Loc,
5642 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5643 Insert_Before (N, Call);
5644 -- why not insert actions here???
5645 Analyze (Call);
5647 Acc_Stat := Parent (N);
5648 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5649 Acc_Stat := Parent (Acc_Stat);
5650 end loop;
5652 Lab_Node := Last (Statements
5653 (Handled_Statement_Sequence (Acc_Stat)));
5655 Goto_Stat := Make_Goto_Statement (Loc,
5656 Name => New_Occurrence_Of
5657 (Entity (Identifier (Lab_Node)), Loc));
5659 Set_Analyzed (Goto_Stat);
5661 Rewrite (N, Goto_Stat);
5662 Analyze (N);
5664 -- If it is a return from an entry body, put a Complete_Entry_Body call
5665 -- in front of the return.
5667 elsif Is_Protected_Type (Scope_Id) then
5668 Call :=
5669 Make_Procedure_Call_Statement (Loc,
5670 Name =>
5671 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5672 Parameter_Associations => New_List (
5673 Make_Attribute_Reference (Loc,
5674 Prefix =>
5675 New_Occurrence_Of
5676 (Find_Protection_Object (Current_Scope), Loc),
5677 Attribute_Name => Name_Unchecked_Access)));
5679 Insert_Before (N, Call);
5680 Analyze (Call);
5681 end if;
5682 end Expand_Non_Function_Return;
5684 ---------------------------------------
5685 -- Expand_Protected_Object_Reference --
5686 ---------------------------------------
5688 function Expand_Protected_Object_Reference
5689 (N : Node_Id;
5690 Scop : Entity_Id) return Node_Id
5692 Loc : constant Source_Ptr := Sloc (N);
5693 Corr : Entity_Id;
5694 Rec : Node_Id;
5695 Param : Entity_Id;
5696 Proc : Entity_Id;
5698 begin
5699 Rec := Make_Identifier (Loc, Name_uObject);
5700 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5702 -- Find enclosing protected operation, and retrieve its first parameter,
5703 -- which denotes the enclosing protected object. If the enclosing
5704 -- operation is an entry, we are immediately within the protected body,
5705 -- and we can retrieve the object from the service entries procedure. A
5706 -- barrier function has the same signature as an entry. A barrier
5707 -- function is compiled within the protected object, but unlike
5708 -- protected operations its never needs locks, so that its protected
5709 -- body subprogram points to itself.
5711 Proc := Current_Scope;
5712 while Present (Proc)
5713 and then Scope (Proc) /= Scop
5714 loop
5715 Proc := Scope (Proc);
5716 end loop;
5718 Corr := Protected_Body_Subprogram (Proc);
5720 if No (Corr) then
5722 -- Previous error left expansion incomplete.
5723 -- Nothing to do on this call.
5725 return Empty;
5726 end if;
5728 Param :=
5729 Defining_Identifier
5730 (First (Parameter_Specifications (Parent (Corr))));
5732 if Is_Subprogram (Proc) and then Proc /= Corr then
5734 -- Protected function or procedure
5736 Set_Entity (Rec, Param);
5738 -- Rec is a reference to an entity which will not be in scope when
5739 -- the call is reanalyzed, and needs no further analysis.
5741 Set_Analyzed (Rec);
5743 else
5744 -- Entry or barrier function for entry body. The first parameter of
5745 -- the entry body procedure is pointer to the object. We create a
5746 -- local variable of the proper type, duplicating what is done to
5747 -- define _object later on.
5749 declare
5750 Decls : List_Id;
5751 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5753 begin
5754 Decls := New_List (
5755 Make_Full_Type_Declaration (Loc,
5756 Defining_Identifier => Obj_Ptr,
5757 Type_Definition =>
5758 Make_Access_To_Object_Definition (Loc,
5759 Subtype_Indication =>
5760 New_Occurrence_Of
5761 (Corresponding_Record_Type (Scop), Loc))));
5763 Insert_Actions (N, Decls);
5764 Freeze_Before (N, Obj_Ptr);
5766 Rec :=
5767 Make_Explicit_Dereference (Loc,
5768 Prefix =>
5769 Unchecked_Convert_To (Obj_Ptr,
5770 New_Occurrence_Of (Param, Loc)));
5772 -- Analyze new actual. Other actuals in calls are already analyzed
5773 -- and the list of actuals is not reanalyzed after rewriting.
5775 Set_Parent (Rec, N);
5776 Analyze (Rec);
5777 end;
5778 end if;
5780 return Rec;
5781 end Expand_Protected_Object_Reference;
5783 --------------------------------------
5784 -- Expand_Protected_Subprogram_Call --
5785 --------------------------------------
5787 procedure Expand_Protected_Subprogram_Call
5788 (N : Node_Id;
5789 Subp : Entity_Id;
5790 Scop : Entity_Id)
5792 Rec : Node_Id;
5794 procedure Freeze_Called_Function;
5795 -- If it is a function call it can appear in elaboration code and
5796 -- the called entity must be frozen before the call. This must be
5797 -- done before the call is expanded, as the expansion may rewrite it
5798 -- to something other than a call (e.g. a temporary initialized in a
5799 -- transient block).
5801 ----------------------------
5802 -- Freeze_Called_Function --
5803 ----------------------------
5805 procedure Freeze_Called_Function is
5806 begin
5807 if Ekind (Subp) = E_Function then
5808 Freeze_Expression (Name (N));
5809 end if;
5810 end Freeze_Called_Function;
5812 -- Start of processing for Expand_Protected_Subprogram_Call
5814 begin
5815 -- If the protected object is not an enclosing scope, this is an inter-
5816 -- object function call. Inter-object procedure calls are expanded by
5817 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5818 -- subprogram being called is in the protected body being compiled, and
5819 -- if the protected object in the call is statically the enclosing type.
5820 -- The object may be an component of some other data structure, in which
5821 -- case this must be handled as an inter-object call.
5823 if not In_Open_Scopes (Scop)
5824 or else not Is_Entity_Name (Name (N))
5825 then
5826 if Nkind (Name (N)) = N_Selected_Component then
5827 Rec := Prefix (Name (N));
5829 else
5830 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5831 Rec := Prefix (Prefix (Name (N)));
5832 end if;
5834 Freeze_Called_Function;
5835 Build_Protected_Subprogram_Call (N,
5836 Name => New_Occurrence_Of (Subp, Sloc (N)),
5837 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5838 External => True);
5840 else
5841 Rec := Expand_Protected_Object_Reference (N, Scop);
5843 if No (Rec) then
5844 return;
5845 end if;
5847 Freeze_Called_Function;
5848 Build_Protected_Subprogram_Call (N,
5849 Name => Name (N),
5850 Rec => Rec,
5851 External => False);
5853 end if;
5855 -- Analyze and resolve the new call. The actuals have already been
5856 -- resolved, but expansion of a function call will add extra actuals
5857 -- if needed. Analysis of a procedure call already includes resolution.
5859 Analyze (N);
5861 if Ekind (Subp) = E_Function then
5862 Resolve (N, Etype (Subp));
5863 end if;
5864 end Expand_Protected_Subprogram_Call;
5866 -----------------------------------
5867 -- Expand_Simple_Function_Return --
5868 -----------------------------------
5870 -- The "simple" comes from the syntax rule simple_return_statement. The
5871 -- semantics are not at all simple.
5873 procedure Expand_Simple_Function_Return (N : Node_Id) is
5874 Loc : constant Source_Ptr := Sloc (N);
5876 Scope_Id : constant Entity_Id :=
5877 Return_Applies_To (Return_Statement_Entity (N));
5878 -- The function we are returning from
5880 R_Type : constant Entity_Id := Etype (Scope_Id);
5881 -- The result type of the function
5883 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5885 Exp : constant Node_Id := Expression (N);
5886 pragma Assert (Present (Exp));
5888 Exptyp : constant Entity_Id := Etype (Exp);
5889 -- The type of the expression (not necessarily the same as R_Type)
5891 Subtype_Ind : Node_Id;
5892 -- If the result type of the function is class-wide and the expression
5893 -- has a specific type, then we use the expression's type as the type of
5894 -- the return object. In cases where the expression is an aggregate that
5895 -- is built in place, this avoids the need for an expensive conversion
5896 -- of the return object to the specific type on assignments to the
5897 -- individual components.
5899 begin
5900 if Is_Class_Wide_Type (R_Type)
5901 and then not Is_Class_Wide_Type (Etype (Exp))
5902 then
5903 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5904 else
5905 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5906 end if;
5908 -- For the case of a simple return that does not come from an extended
5909 -- return, in the case of Ada 2005 where we are returning a limited
5910 -- type, we rewrite "return <expression>;" to be:
5912 -- return _anon_ : <return_subtype> := <expression>
5914 -- The expansion produced by Expand_N_Extended_Return_Statement will
5915 -- contain simple return statements (for example, a block containing
5916 -- simple return of the return object), which brings us back here with
5917 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5918 -- checking for a simple return that does not come from an extended
5919 -- return is to avoid this infinite recursion.
5921 -- The reason for this design is that for Ada 2005 limited returns, we
5922 -- need to reify the return object, so we can build it "in place", and
5923 -- we need a block statement to hang finalization and tasking stuff.
5925 -- ??? In order to avoid disruption, we avoid translating to extended
5926 -- return except in the cases where we really need to (Ada 2005 for
5927 -- inherently limited). We might prefer to do this translation in all
5928 -- cases (except perhaps for the case of Ada 95 inherently limited),
5929 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5930 -- code. This would also allow us to do the build-in-place optimization
5931 -- for efficiency even in cases where it is semantically not required.
5933 -- As before, we check the type of the return expression rather than the
5934 -- return type of the function, because the latter may be a limited
5935 -- class-wide interface type, which is not a limited type, even though
5936 -- the type of the expression may be.
5938 if not Comes_From_Extended_Return_Statement (N)
5939 and then Is_Limited_View (Etype (Expression (N)))
5940 and then Ada_Version >= Ada_2005
5941 and then not Debug_Flag_Dot_L
5943 -- The functionality of interface thunks is simple and it is always
5944 -- handled by means of simple return statements. This leaves their
5945 -- expansion simple and clean.
5947 and then not Is_Thunk (Current_Scope)
5948 then
5949 declare
5950 Return_Object_Entity : constant Entity_Id :=
5951 Make_Temporary (Loc, 'R', Exp);
5953 Obj_Decl : constant Node_Id :=
5954 Make_Object_Declaration (Loc,
5955 Defining_Identifier => Return_Object_Entity,
5956 Object_Definition => Subtype_Ind,
5957 Expression => Exp);
5959 Ext : constant Node_Id :=
5960 Make_Extended_Return_Statement (Loc,
5961 Return_Object_Declarations => New_List (Obj_Decl));
5962 -- Do not perform this high-level optimization if the result type
5963 -- is an interface because the "this" pointer must be displaced.
5965 begin
5966 Rewrite (N, Ext);
5967 Analyze (N);
5968 return;
5969 end;
5970 end if;
5972 -- Here we have a simple return statement that is part of the expansion
5973 -- of an extended return statement (either written by the user, or
5974 -- generated by the above code).
5976 -- Always normalize C/Fortran boolean result. This is not always needed,
5977 -- but it seems a good idea to minimize the passing around of non-
5978 -- normalized values, and in any case this handles the processing of
5979 -- barrier functions for protected types, which turn the condition into
5980 -- a return statement.
5982 if Is_Boolean_Type (Exptyp)
5983 and then Nonzero_Is_True (Exptyp)
5984 then
5985 Adjust_Condition (Exp);
5986 Adjust_Result_Type (Exp, Exptyp);
5987 end if;
5989 -- Do validity check if enabled for returns
5991 if Validity_Checks_On
5992 and then Validity_Check_Returns
5993 then
5994 Ensure_Valid (Exp);
5995 end if;
5997 -- Check the result expression of a scalar function against the subtype
5998 -- of the function by inserting a conversion. This conversion must
5999 -- eventually be performed for other classes of types, but for now it's
6000 -- only done for scalars.
6001 -- ???
6003 if Is_Scalar_Type (Exptyp) then
6004 Rewrite (Exp, Convert_To (R_Type, Exp));
6006 -- The expression is resolved to ensure that the conversion gets
6007 -- expanded to generate a possible constraint check.
6009 Analyze_And_Resolve (Exp, R_Type);
6010 end if;
6012 -- Deal with returning variable length objects and controlled types
6014 -- Nothing to do if we are returning by reference, or this is not a
6015 -- type that requires special processing (indicated by the fact that
6016 -- it requires a cleanup scope for the secondary stack case).
6018 if Is_Limited_View (Exptyp)
6019 or else Is_Limited_Interface (Exptyp)
6020 then
6021 null;
6023 -- No copy needed for thunks returning interface type objects since
6024 -- the object is returned by reference and the maximum functionality
6025 -- required is just to displace the pointer.
6027 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6028 null;
6030 -- If the call is within a thunk and the type is a limited view, the
6031 -- backend will eventually see the non-limited view of the type.
6033 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6034 return;
6036 elsif not Requires_Transient_Scope (R_Type) then
6038 -- Mutable records with no variable length components are not
6039 -- returned on the sec-stack, so we need to make sure that the
6040 -- backend will only copy back the size of the actual value, and not
6041 -- the maximum size. We create an actual subtype for this purpose.
6043 declare
6044 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6045 Decl : Node_Id;
6046 Ent : Entity_Id;
6047 begin
6048 if Has_Discriminants (Ubt)
6049 and then not Is_Constrained (Ubt)
6050 and then not Has_Unchecked_Union (Ubt)
6051 then
6052 Decl := Build_Actual_Subtype (Ubt, Exp);
6053 Ent := Defining_Identifier (Decl);
6054 Insert_Action (Exp, Decl);
6055 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6056 Analyze_And_Resolve (Exp);
6057 end if;
6058 end;
6060 -- Here if secondary stack is used
6062 else
6063 -- Prevent the reclamation of the secondary stack by all enclosing
6064 -- blocks and loops as well as the related function, otherwise the
6065 -- result will be reclaimed too early or even clobbered. Due to a
6066 -- possible mix of internally generated blocks, source blocks and
6067 -- loops, the scope stack may not be contiguous as all labels are
6068 -- inserted at the top level within the related function. Instead,
6069 -- perform a parent-based traversal and mark all appropriate
6070 -- constructs.
6072 declare
6073 P : Node_Id;
6075 begin
6076 P := N;
6077 while Present (P) loop
6079 -- Mark the label of a source or internally generated block or
6080 -- loop.
6082 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
6083 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
6085 -- Mark the enclosing function
6087 elsif Nkind (P) = N_Subprogram_Body then
6088 if Present (Corresponding_Spec (P)) then
6089 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
6090 else
6091 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
6092 end if;
6094 -- Do not go beyond the enclosing function
6096 exit;
6097 end if;
6099 P := Parent (P);
6100 end loop;
6101 end;
6103 -- Optimize the case where the result is a function call. In this
6104 -- case either the result is already on the secondary stack, or is
6105 -- already being returned with the stack pointer depressed and no
6106 -- further processing is required except to set the By_Ref flag
6107 -- to ensure that gigi does not attempt an extra unnecessary copy.
6108 -- (actually not just unnecessary but harmfully wrong in the case
6109 -- of a controlled type, where gigi does not know how to do a copy).
6110 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6111 -- for array types if the constrained status of the target type is
6112 -- different from that of the expression.
6114 if Requires_Transient_Scope (Exptyp)
6115 and then
6116 (not Is_Array_Type (Exptyp)
6117 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6118 or else CW_Or_Has_Controlled_Part (Utyp))
6119 and then Nkind (Exp) = N_Function_Call
6120 then
6121 Set_By_Ref (N);
6123 -- Remove side effects from the expression now so that other parts
6124 -- of the expander do not have to reanalyze this node without this
6125 -- optimization
6127 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6129 -- For controlled types, do the allocation on the secondary stack
6130 -- manually in order to call adjust at the right time:
6132 -- type Anon1 is access R_Type;
6133 -- for Anon1'Storage_pool use ss_pool;
6134 -- Anon2 : anon1 := new R_Type'(expr);
6135 -- return Anon2.all;
6137 -- We do the same for classwide types that are not potentially
6138 -- controlled (by the virtue of restriction No_Finalization) because
6139 -- gigi is not able to properly allocate class-wide types.
6141 elsif CW_Or_Has_Controlled_Part (Utyp) then
6142 declare
6143 Loc : constant Source_Ptr := Sloc (N);
6144 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6145 Alloc_Node : Node_Id;
6146 Temp : Entity_Id;
6148 begin
6149 Set_Ekind (Acc_Typ, E_Access_Type);
6151 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6153 -- This is an allocator for the secondary stack, and it's fine
6154 -- to have Comes_From_Source set False on it, as gigi knows not
6155 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6157 Alloc_Node :=
6158 Make_Allocator (Loc,
6159 Expression =>
6160 Make_Qualified_Expression (Loc,
6161 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6162 Expression => Relocate_Node (Exp)));
6164 -- We do not want discriminant checks on the declaration,
6165 -- given that it gets its value from the allocator.
6167 Set_No_Initialization (Alloc_Node);
6169 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6171 Insert_List_Before_And_Analyze (N, New_List (
6172 Make_Full_Type_Declaration (Loc,
6173 Defining_Identifier => Acc_Typ,
6174 Type_Definition =>
6175 Make_Access_To_Object_Definition (Loc,
6176 Subtype_Indication => Subtype_Ind)),
6178 Make_Object_Declaration (Loc,
6179 Defining_Identifier => Temp,
6180 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6181 Expression => Alloc_Node)));
6183 Rewrite (Exp,
6184 Make_Explicit_Dereference (Loc,
6185 Prefix => New_Occurrence_Of (Temp, Loc)));
6187 -- Ada 2005 (AI-251): If the type of the returned object is
6188 -- an interface then add an implicit type conversion to force
6189 -- displacement of the "this" pointer.
6191 if Is_Interface (R_Type) then
6192 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6193 end if;
6195 Analyze_And_Resolve (Exp, R_Type);
6196 end;
6198 -- Otherwise use the gigi mechanism to allocate result on the
6199 -- secondary stack.
6201 else
6202 Check_Restriction (No_Secondary_Stack, N);
6203 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6205 -- If we are generating code for the VM do not use
6206 -- SS_Allocate since everything is heap-allocated anyway.
6208 if VM_Target = No_VM then
6209 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6210 end if;
6211 end if;
6212 end if;
6214 -- Implement the rules of 6.5(8-10), which require a tag check in
6215 -- the case of a limited tagged return type, and tag reassignment for
6216 -- nonlimited tagged results. These actions are needed when the return
6217 -- type is a specific tagged type and the result expression is a
6218 -- conversion or a formal parameter, because in that case the tag of
6219 -- the expression might differ from the tag of the specific result type.
6221 if Is_Tagged_Type (Utyp)
6222 and then not Is_Class_Wide_Type (Utyp)
6223 and then (Nkind_In (Exp, N_Type_Conversion,
6224 N_Unchecked_Type_Conversion)
6225 or else (Is_Entity_Name (Exp)
6226 and then Ekind (Entity (Exp)) in Formal_Kind))
6227 then
6228 -- When the return type is limited, perform a check that the tag of
6229 -- the result is the same as the tag of the return type.
6231 if Is_Limited_Type (R_Type) then
6232 Insert_Action (Exp,
6233 Make_Raise_Constraint_Error (Loc,
6234 Condition =>
6235 Make_Op_Ne (Loc,
6236 Left_Opnd =>
6237 Make_Selected_Component (Loc,
6238 Prefix => Duplicate_Subexpr (Exp),
6239 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6240 Right_Opnd =>
6241 Make_Attribute_Reference (Loc,
6242 Prefix =>
6243 New_Occurrence_Of (Base_Type (Utyp), Loc),
6244 Attribute_Name => Name_Tag)),
6245 Reason => CE_Tag_Check_Failed));
6247 -- If the result type is a specific nonlimited tagged type, then we
6248 -- have to ensure that the tag of the result is that of the result
6249 -- type. This is handled by making a copy of the expression in
6250 -- the case where it might have a different tag, namely when the
6251 -- expression is a conversion or a formal parameter. We create a new
6252 -- object of the result type and initialize it from the expression,
6253 -- which will implicitly force the tag to be set appropriately.
6255 else
6256 declare
6257 ExpR : constant Node_Id := Relocate_Node (Exp);
6258 Result_Id : constant Entity_Id :=
6259 Make_Temporary (Loc, 'R', ExpR);
6260 Result_Exp : constant Node_Id :=
6261 New_Occurrence_Of (Result_Id, Loc);
6262 Result_Obj : constant Node_Id :=
6263 Make_Object_Declaration (Loc,
6264 Defining_Identifier => Result_Id,
6265 Object_Definition =>
6266 New_Occurrence_Of (R_Type, Loc),
6267 Constant_Present => True,
6268 Expression => ExpR);
6270 begin
6271 Set_Assignment_OK (Result_Obj);
6272 Insert_Action (Exp, Result_Obj);
6274 Rewrite (Exp, Result_Exp);
6275 Analyze_And_Resolve (Exp, R_Type);
6276 end;
6277 end if;
6279 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6280 -- a check that the level of the return expression's underlying type
6281 -- is not deeper than the level of the master enclosing the function.
6282 -- Always generate the check when the type of the return expression
6283 -- is class-wide, when it's a type conversion, or when it's a formal
6284 -- parameter. Otherwise, suppress the check in the case where the
6285 -- return expression has a specific type whose level is known not to
6286 -- be statically deeper than the function's result type.
6288 -- No runtime check needed in interface thunks since it is performed
6289 -- by the target primitive associated with the thunk.
6291 -- Note: accessibility check is skipped in the VM case, since there
6292 -- does not seem to be any practical way to implement this check.
6294 elsif Ada_Version >= Ada_2005
6295 and then Tagged_Type_Expansion
6296 and then Is_Class_Wide_Type (R_Type)
6297 and then not Is_Thunk (Current_Scope)
6298 and then not Scope_Suppress.Suppress (Accessibility_Check)
6299 and then
6300 (Is_Class_Wide_Type (Etype (Exp))
6301 or else Nkind_In (Exp, N_Type_Conversion,
6302 N_Unchecked_Type_Conversion)
6303 or else (Is_Entity_Name (Exp)
6304 and then Ekind (Entity (Exp)) in Formal_Kind)
6305 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6306 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6307 then
6308 declare
6309 Tag_Node : Node_Id;
6311 begin
6312 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6313 -- "this" to reference the base of the object. This is required to
6314 -- get access to the TSD of the object.
6316 if Is_Class_Wide_Type (Etype (Exp))
6317 and then Is_Interface (Etype (Exp))
6318 then
6319 -- If the expression is an explicit dereference then we can
6320 -- directly displace the pointer to reference the base of
6321 -- the object.
6323 if Nkind (Exp) = N_Explicit_Dereference then
6324 Tag_Node :=
6325 Make_Explicit_Dereference (Loc,
6326 Prefix =>
6327 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6328 Make_Function_Call (Loc,
6329 Name =>
6330 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6331 Parameter_Associations => New_List (
6332 Unchecked_Convert_To (RTE (RE_Address),
6333 Duplicate_Subexpr (Prefix (Exp)))))));
6335 -- Similar case to the previous one but the expression is a
6336 -- renaming of an explicit dereference.
6338 elsif Nkind (Exp) = N_Identifier
6339 and then Present (Renamed_Object (Entity (Exp)))
6340 and then Nkind (Renamed_Object (Entity (Exp)))
6341 = N_Explicit_Dereference
6342 then
6343 Tag_Node :=
6344 Make_Explicit_Dereference (Loc,
6345 Prefix =>
6346 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6347 Make_Function_Call (Loc,
6348 Name =>
6349 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6350 Parameter_Associations => New_List (
6351 Unchecked_Convert_To (RTE (RE_Address),
6352 Duplicate_Subexpr
6353 (Prefix
6354 (Renamed_Object (Entity (Exp)))))))));
6356 -- Common case: obtain the address of the actual object and
6357 -- displace the pointer to reference the base of the object.
6359 else
6360 Tag_Node :=
6361 Make_Explicit_Dereference (Loc,
6362 Prefix =>
6363 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6364 Make_Function_Call (Loc,
6365 Name =>
6366 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6367 Parameter_Associations => New_List (
6368 Make_Attribute_Reference (Loc,
6369 Prefix => Duplicate_Subexpr (Exp),
6370 Attribute_Name => Name_Address)))));
6371 end if;
6372 else
6373 Tag_Node :=
6374 Make_Attribute_Reference (Loc,
6375 Prefix => Duplicate_Subexpr (Exp),
6376 Attribute_Name => Name_Tag);
6377 end if;
6379 Insert_Action (Exp,
6380 Make_Raise_Program_Error (Loc,
6381 Condition =>
6382 Make_Op_Gt (Loc,
6383 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6384 Right_Opnd =>
6385 Make_Integer_Literal (Loc,
6386 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6387 Reason => PE_Accessibility_Check_Failed));
6388 end;
6390 -- AI05-0073: If function has a controlling access result, check that
6391 -- the tag of the return value, if it is not null, matches designated
6392 -- type of return type.
6394 -- The return expression is referenced twice in the code below, so it
6395 -- must be made free of side effects. Given that different compilers
6396 -- may evaluate these parameters in different order, both occurrences
6397 -- perform a copy.
6399 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6400 and then Has_Controlling_Result (Scope_Id)
6401 then
6402 Insert_Action (N,
6403 Make_Raise_Constraint_Error (Loc,
6404 Condition =>
6405 Make_And_Then (Loc,
6406 Left_Opnd =>
6407 Make_Op_Ne (Loc,
6408 Left_Opnd => Duplicate_Subexpr (Exp),
6409 Right_Opnd => Make_Null (Loc)),
6411 Right_Opnd => Make_Op_Ne (Loc,
6412 Left_Opnd =>
6413 Make_Selected_Component (Loc,
6414 Prefix => Duplicate_Subexpr (Exp),
6415 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6417 Right_Opnd =>
6418 Make_Attribute_Reference (Loc,
6419 Prefix =>
6420 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6421 Attribute_Name => Name_Tag))),
6423 Reason => CE_Tag_Check_Failed),
6424 Suppress => All_Checks);
6425 end if;
6427 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6428 -- ensure that the function result does not outlive an
6429 -- object designated by one of it discriminants.
6431 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6432 and then Has_Unconstrained_Access_Discriminants (R_Type)
6433 then
6434 declare
6435 Discrim_Source : Node_Id;
6437 procedure Check_Against_Result_Level (Level : Node_Id);
6438 -- Check the given accessibility level against the level
6439 -- determined by the point of call. (AI05-0234).
6441 --------------------------------
6442 -- Check_Against_Result_Level --
6443 --------------------------------
6445 procedure Check_Against_Result_Level (Level : Node_Id) is
6446 begin
6447 Insert_Action (N,
6448 Make_Raise_Program_Error (Loc,
6449 Condition =>
6450 Make_Op_Gt (Loc,
6451 Left_Opnd => Level,
6452 Right_Opnd =>
6453 New_Occurrence_Of
6454 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6455 Reason => PE_Accessibility_Check_Failed));
6456 end Check_Against_Result_Level;
6458 begin
6459 Discrim_Source := Exp;
6460 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6461 Discrim_Source := Expression (Discrim_Source);
6462 end loop;
6464 if Nkind (Discrim_Source) = N_Identifier
6465 and then Is_Return_Object (Entity (Discrim_Source))
6466 then
6467 Discrim_Source := Entity (Discrim_Source);
6469 if Is_Constrained (Etype (Discrim_Source)) then
6470 Discrim_Source := Etype (Discrim_Source);
6471 else
6472 Discrim_Source := Expression (Parent (Discrim_Source));
6473 end if;
6475 elsif Nkind (Discrim_Source) = N_Identifier
6476 and then Nkind_In (Original_Node (Discrim_Source),
6477 N_Aggregate, N_Extension_Aggregate)
6478 then
6479 Discrim_Source := Original_Node (Discrim_Source);
6481 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6482 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6483 then
6484 Discrim_Source := Original_Node (Discrim_Source);
6485 end if;
6487 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6488 N_Type_Conversion,
6489 N_Unchecked_Type_Conversion)
6490 loop
6491 Discrim_Source := Expression (Discrim_Source);
6492 end loop;
6494 case Nkind (Discrim_Source) is
6495 when N_Defining_Identifier =>
6497 pragma Assert (Is_Composite_Type (Discrim_Source)
6498 and then Has_Discriminants (Discrim_Source)
6499 and then Is_Constrained (Discrim_Source));
6501 declare
6502 Discrim : Entity_Id :=
6503 First_Discriminant (Base_Type (R_Type));
6504 Disc_Elmt : Elmt_Id :=
6505 First_Elmt (Discriminant_Constraint
6506 (Discrim_Source));
6507 begin
6508 loop
6509 if Ekind (Etype (Discrim)) =
6510 E_Anonymous_Access_Type
6511 then
6512 Check_Against_Result_Level
6513 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6514 end if;
6516 Next_Elmt (Disc_Elmt);
6517 Next_Discriminant (Discrim);
6518 exit when not Present (Discrim);
6519 end loop;
6520 end;
6522 when N_Aggregate | N_Extension_Aggregate =>
6524 -- Unimplemented: extension aggregate case where discrims
6525 -- come from ancestor part, not extension part.
6527 declare
6528 Discrim : Entity_Id :=
6529 First_Discriminant (Base_Type (R_Type));
6531 Disc_Exp : Node_Id := Empty;
6533 Positionals_Exhausted
6534 : Boolean := not Present (Expressions
6535 (Discrim_Source));
6537 function Associated_Expr
6538 (Comp_Id : Entity_Id;
6539 Associations : List_Id) return Node_Id;
6541 -- Given a component and a component associations list,
6542 -- locate the expression for that component; returns
6543 -- Empty if no such expression is found.
6545 ---------------------
6546 -- Associated_Expr --
6547 ---------------------
6549 function Associated_Expr
6550 (Comp_Id : Entity_Id;
6551 Associations : List_Id) return Node_Id
6553 Assoc : Node_Id;
6554 Choice : Node_Id;
6556 begin
6557 -- Simple linear search seems ok here
6559 Assoc := First (Associations);
6560 while Present (Assoc) loop
6561 Choice := First (Choices (Assoc));
6562 while Present (Choice) loop
6563 if (Nkind (Choice) = N_Identifier
6564 and then Chars (Choice) = Chars (Comp_Id))
6565 or else (Nkind (Choice) = N_Others_Choice)
6566 then
6567 return Expression (Assoc);
6568 end if;
6570 Next (Choice);
6571 end loop;
6573 Next (Assoc);
6574 end loop;
6576 return Empty;
6577 end Associated_Expr;
6579 -- Start of processing for Expand_Simple_Function_Return
6581 begin
6582 if not Positionals_Exhausted then
6583 Disc_Exp := First (Expressions (Discrim_Source));
6584 end if;
6586 loop
6587 if Positionals_Exhausted then
6588 Disc_Exp :=
6589 Associated_Expr
6590 (Discrim,
6591 Component_Associations (Discrim_Source));
6592 end if;
6594 if Ekind (Etype (Discrim)) =
6595 E_Anonymous_Access_Type
6596 then
6597 Check_Against_Result_Level
6598 (Dynamic_Accessibility_Level (Disc_Exp));
6599 end if;
6601 Next_Discriminant (Discrim);
6602 exit when not Present (Discrim);
6604 if not Positionals_Exhausted then
6605 Next (Disc_Exp);
6606 Positionals_Exhausted := not Present (Disc_Exp);
6607 end if;
6608 end loop;
6609 end;
6611 when N_Function_Call =>
6613 -- No check needed (check performed by callee)
6615 null;
6617 when others =>
6619 declare
6620 Level : constant Node_Id :=
6621 Make_Integer_Literal (Loc,
6622 Object_Access_Level (Discrim_Source));
6624 begin
6625 -- Unimplemented: check for name prefix that includes
6626 -- a dereference of an access value with a dynamic
6627 -- accessibility level (e.g., an access param or a
6628 -- saooaaat) and use dynamic level in that case. For
6629 -- example:
6630 -- return Access_Param.all(Some_Index).Some_Component;
6631 -- ???
6633 Set_Etype (Level, Standard_Natural);
6634 Check_Against_Result_Level (Level);
6635 end;
6637 end case;
6638 end;
6639 end if;
6641 -- If we are returning an object that may not be bit-aligned, then copy
6642 -- the value into a temporary first. This copy may need to expand to a
6643 -- loop of component operations.
6645 if Is_Possibly_Unaligned_Slice (Exp)
6646 or else Is_Possibly_Unaligned_Object (Exp)
6647 then
6648 declare
6649 ExpR : constant Node_Id := Relocate_Node (Exp);
6650 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6651 begin
6652 Insert_Action (Exp,
6653 Make_Object_Declaration (Loc,
6654 Defining_Identifier => Tnn,
6655 Constant_Present => True,
6656 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6657 Expression => ExpR),
6658 Suppress => All_Checks);
6659 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6660 end;
6661 end if;
6663 -- Call the _Postconditions procedure if the related function has
6664 -- contract assertions that need to be verified on exit.
6666 if Ekind (Scope_Id) = E_Function
6667 and then Present (Postconditions_Proc (Scope_Id))
6668 then
6669 -- In the case of discriminated objects, we have created a
6670 -- constrained subtype above, and used the underlying type. This
6671 -- transformation is post-analysis and harmless, except that now the
6672 -- call to the post-condition will be analyzed and the type kinds
6673 -- have to match.
6675 if Nkind (Exp) = N_Unchecked_Type_Conversion
6676 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
6677 then
6678 Rewrite (Exp, Expression (Relocate_Node (Exp)));
6679 end if;
6681 -- We are going to reference the returned value twice in this case,
6682 -- once in the call to _Postconditions, and once in the actual return
6683 -- statement, but we can't have side effects happening twice.
6685 Remove_Side_Effects (Exp);
6687 -- Generate call to _Postconditions
6689 Insert_Action (Exp,
6690 Make_Procedure_Call_Statement (Loc,
6691 Name =>
6692 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6693 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
6694 end if;
6696 -- Ada 2005 (AI-251): If this return statement corresponds with an
6697 -- simple return statement associated with an extended return statement
6698 -- and the type of the returned object is an interface then generate an
6699 -- implicit conversion to force displacement of the "this" pointer.
6701 if Ada_Version >= Ada_2005
6702 and then Comes_From_Extended_Return_Statement (N)
6703 and then Nkind (Expression (N)) = N_Identifier
6704 and then Is_Interface (Utyp)
6705 and then Utyp /= Underlying_Type (Exptyp)
6706 then
6707 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6708 Analyze_And_Resolve (Exp);
6709 end if;
6710 end Expand_Simple_Function_Return;
6712 --------------------------------
6713 -- Expand_Subprogram_Contract --
6714 --------------------------------
6716 procedure Expand_Subprogram_Contract (N : Node_Id) is
6717 Body_Id : constant Entity_Id := Defining_Entity (N);
6718 Spec_Id : constant Entity_Id := Corresponding_Spec (N);
6720 procedure Add_Invariant_And_Predicate_Checks
6721 (Subp_Id : Entity_Id;
6722 Stmts : in out List_Id;
6723 Result : out Node_Id);
6724 -- Process the result of function Subp_Id (if applicable) and all its
6725 -- formals. Add invariant and predicate checks where applicable. The
6726 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6727 -- function, Result contains the entity of parameter _Result, to be
6728 -- used in the creation of procedure _Postconditions.
6730 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
6731 -- Append a node to a list. If there is no list, create a new one. When
6732 -- the item denotes a pragma, it is added to the list only when it is
6733 -- enabled.
6735 procedure Build_Postconditions_Procedure
6736 (Subp_Id : Entity_Id;
6737 Stmts : List_Id;
6738 Result : Entity_Id);
6739 -- Create the body of procedure _Postconditions which handles various
6740 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6741 -- of statements to be checked on exit. Parameter Result is the entity
6742 -- of parameter _Result when Subp_Id denotes a function.
6744 function Build_Pragma_Check_Equivalent
6745 (Prag : Node_Id;
6746 Subp_Id : Entity_Id := Empty;
6747 Inher_Id : Entity_Id := Empty) return Node_Id;
6748 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6749 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6750 -- the routine corrects the references of all formals of Inher_Id to
6751 -- point to the formals of Subp_Id.
6753 procedure Process_Contract_Cases (Stmts : in out List_Id);
6754 -- Process pragma Contract_Cases. This routine prepends items to the
6755 -- body declarations and appends items to list Stmts.
6757 procedure Process_Postconditions (Stmts : in out List_Id);
6758 -- Collect all [inherited] spec and body postconditions and accumulate
6759 -- their pragma Check equivalents in list Stmts.
6761 procedure Process_Preconditions;
6762 -- Collect all [inherited] spec and body preconditions and prepend their
6763 -- pragma Check equivalents to the declarations of the body.
6765 ----------------------------------------
6766 -- Add_Invariant_And_Predicate_Checks --
6767 ----------------------------------------
6769 procedure Add_Invariant_And_Predicate_Checks
6770 (Subp_Id : Entity_Id;
6771 Stmts : in out List_Id;
6772 Result : out Node_Id)
6774 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
6775 -- Id denotes the return value of a function or a formal parameter.
6776 -- Add an invariant check if the type of Id is access to a type with
6777 -- invariants. The routine appends the generated code to Stmts.
6779 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
6780 -- Determine whether type Typ can benefit from invariant checks. To
6781 -- qualify, the type must have a non-null invariant procedure and
6782 -- subprogram Subp_Id must appear visible from the point of view of
6783 -- the type.
6785 ---------------------------------
6786 -- Add_Invariant_Access_Checks --
6787 ---------------------------------
6789 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
6790 Loc : constant Source_Ptr := Sloc (N);
6791 Ref : Node_Id;
6792 Typ : Entity_Id;
6794 begin
6795 Typ := Etype (Id);
6797 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
6798 Typ := Designated_Type (Typ);
6800 if Invariant_Checks_OK (Typ) then
6801 Ref :=
6802 Make_Explicit_Dereference (Loc,
6803 Prefix => New_Occurrence_Of (Id, Loc));
6804 Set_Etype (Ref, Typ);
6806 -- Generate:
6807 -- if <Id> /= null then
6808 -- <invariant_call (<Ref>)>
6809 -- end if;
6811 Append_Enabled_Item
6812 (Item =>
6813 Make_If_Statement (Loc,
6814 Condition =>
6815 Make_Op_Ne (Loc,
6816 Left_Opnd => New_Occurrence_Of (Id, Loc),
6817 Right_Opnd => Make_Null (Loc)),
6818 Then_Statements => New_List (
6819 Make_Invariant_Call (Ref))),
6820 List => Stmts);
6821 end if;
6822 end if;
6823 end Add_Invariant_Access_Checks;
6825 -------------------------
6826 -- Invariant_Checks_OK --
6827 -------------------------
6829 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
6830 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
6831 -- Determine whether the body of procedure Proc_Id contains a sole
6832 -- null statement, possibly followed by an optional return.
6834 function Has_Public_Visibility_Of_Subprogram return Boolean;
6835 -- Determine whether type Typ has public visibility of subprogram
6836 -- Subp_Id.
6838 -------------------
6839 -- Has_Null_Body --
6840 -------------------
6842 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
6843 Body_Id : Entity_Id;
6844 Decl : Node_Id;
6845 Spec : Node_Id;
6846 Stmt1 : Node_Id;
6847 Stmt2 : Node_Id;
6849 begin
6850 Spec := Parent (Proc_Id);
6851 Decl := Parent (Spec);
6853 -- Retrieve the entity of the invariant procedure body
6855 if Nkind (Spec) = N_Procedure_Specification
6856 and then Nkind (Decl) = N_Subprogram_Declaration
6857 then
6858 Body_Id := Corresponding_Body (Decl);
6860 -- The body acts as a spec
6862 else
6863 Body_Id := Proc_Id;
6864 end if;
6866 -- The body will be generated later
6868 if No (Body_Id) then
6869 return False;
6870 end if;
6872 Spec := Parent (Body_Id);
6873 Decl := Parent (Spec);
6875 pragma Assert
6876 (Nkind (Spec) = N_Procedure_Specification
6877 and then Nkind (Decl) = N_Subprogram_Body);
6879 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
6881 -- Look for a null statement followed by an optional return
6882 -- statement.
6884 if Nkind (Stmt1) = N_Null_Statement then
6885 Stmt2 := Next (Stmt1);
6887 if Present (Stmt2) then
6888 return Nkind (Stmt2) = N_Simple_Return_Statement;
6889 else
6890 return True;
6891 end if;
6892 end if;
6894 return False;
6895 end Has_Null_Body;
6897 -----------------------------------------
6898 -- Has_Public_Visibility_Of_Subprogram --
6899 -----------------------------------------
6901 function Has_Public_Visibility_Of_Subprogram return Boolean is
6902 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
6904 begin
6905 -- An Initialization procedure must be considered visible even
6906 -- though it is internally generated.
6908 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
6909 return True;
6911 elsif Ekind (Scope (Typ)) /= E_Package then
6912 return False;
6914 -- Internally generated code is never publicly visible except
6915 -- for a subprogram that is the implementation of an expression
6916 -- function. In that case the visibility is determined by the
6917 -- last check.
6919 elsif not Comes_From_Source (Subp_Decl)
6920 and then
6921 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
6922 or else not
6923 Comes_From_Source (Defining_Entity (Subp_Decl)))
6924 then
6925 return False;
6927 -- Determine whether the subprogram is declared in the visible
6928 -- declarations of the package containing the type.
6930 else
6931 return List_Containing (Subp_Decl) =
6932 Visible_Declarations
6933 (Specification (Unit_Declaration_Node (Scope (Typ))));
6934 end if;
6935 end Has_Public_Visibility_Of_Subprogram;
6937 -- Start of processing for Invariant_Checks_OK
6939 begin
6940 return
6941 Has_Invariants (Typ)
6942 and then Present (Invariant_Procedure (Typ))
6943 and then not Has_Null_Body (Invariant_Procedure (Typ))
6944 and then Has_Public_Visibility_Of_Subprogram;
6945 end Invariant_Checks_OK;
6947 -- Local variables
6949 Loc : constant Source_Ptr := Sloc (N);
6950 -- Source location of subprogram contract
6952 Formal : Entity_Id;
6953 Typ : Entity_Id;
6955 -- Start of processing for Add_Invariant_And_Predicate_Checks
6957 begin
6958 Result := Empty;
6960 -- Process the result of a function
6962 if Ekind (Subp_Id) = E_Function then
6963 Typ := Etype (Subp_Id);
6965 -- Generate _Result which is used in procedure _Postconditions to
6966 -- verify the return value.
6968 Result := Make_Defining_Identifier (Loc, Name_uResult);
6969 Set_Etype (Result, Typ);
6971 -- Add an invariant check when the return type has invariants and
6972 -- the related function is visible to the outside.
6974 if Invariant_Checks_OK (Typ) then
6975 Append_Enabled_Item
6976 (Item =>
6977 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
6978 List => Stmts);
6979 end if;
6981 -- Add an invariant check when the return type is an access to a
6982 -- type with invariants.
6984 Add_Invariant_Access_Checks (Result);
6985 end if;
6987 -- Add invariant and predicates for all formals that qualify
6989 Formal := First_Formal (Subp_Id);
6990 while Present (Formal) loop
6991 Typ := Etype (Formal);
6993 if Ekind (Formal) /= E_In_Parameter
6994 or else Is_Access_Type (Typ)
6995 then
6996 if Invariant_Checks_OK (Typ) then
6997 Append_Enabled_Item
6998 (Item =>
6999 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
7000 List => Stmts);
7001 end if;
7003 Add_Invariant_Access_Checks (Formal);
7005 -- Note: we used to add predicate checks for OUT and IN OUT
7006 -- formals here, but that was misguided, since such checks are
7007 -- performed on the caller side, based on the predicate of the
7008 -- actual, rather than the predicate of the formal.
7010 end if;
7012 Next_Formal (Formal);
7013 end loop;
7014 end Add_Invariant_And_Predicate_Checks;
7016 -------------------------
7017 -- Append_Enabled_Item --
7018 -------------------------
7020 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
7021 begin
7022 -- Do not chain ignored or disabled pragmas
7024 if Nkind (Item) = N_Pragma
7025 and then (Is_Ignored (Item) or else Is_Disabled (Item))
7026 then
7027 null;
7029 -- Otherwise, add the item
7031 else
7032 if No (List) then
7033 List := New_List;
7034 end if;
7036 -- If the pragma is a conjunct in a composite postcondition, it
7037 -- has been processed in reverse order. In the postcondition body
7038 -- if must appear before the others.
7040 if Nkind (Item) = N_Pragma
7041 and then From_Aspect_Specification (Item)
7042 and then Split_PPC (Item)
7043 then
7044 Prepend (Item, List);
7045 else
7046 Append (Item, List);
7047 end if;
7048 end if;
7049 end Append_Enabled_Item;
7051 ------------------------------------
7052 -- Build_Postconditions_Procedure --
7053 ------------------------------------
7055 procedure Build_Postconditions_Procedure
7056 (Subp_Id : Entity_Id;
7057 Stmts : List_Id;
7058 Result : Entity_Id)
7060 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
7061 -- Insert node Stmt before the first source declaration of the
7062 -- related subprogram's body. If no such declaration exists, Stmt
7063 -- becomes the last declaration.
7065 --------------------------------------------
7066 -- Insert_Before_First_Source_Declaration --
7067 --------------------------------------------
7069 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
7070 Decls : constant List_Id := Declarations (N);
7071 Decl : Node_Id;
7073 begin
7074 -- Inspect the declarations of the related subprogram body looking
7075 -- for the first source declaration.
7077 if Present (Decls) then
7078 Decl := First (Decls);
7079 while Present (Decl) loop
7080 if Comes_From_Source (Decl) then
7081 Insert_Before (Decl, Stmt);
7082 return;
7083 end if;
7085 Next (Decl);
7086 end loop;
7088 -- If we get there, then the subprogram body lacks any source
7089 -- declarations. The body of _Postconditions now acts as the
7090 -- last declaration.
7092 Append (Stmt, Decls);
7094 -- Ensure that the body has a declaration list
7096 else
7097 Set_Declarations (N, New_List (Stmt));
7098 end if;
7099 end Insert_Before_First_Source_Declaration;
7101 -- Local variables
7103 Loc : constant Source_Ptr := Sloc (N);
7104 Params : List_Id := No_List;
7105 Proc_Bod : Node_Id;
7106 Proc_Id : Entity_Id;
7108 -- Start of processing for Build_Postconditions_Procedure
7110 begin
7111 -- Nothing to do if there are no actions to check on exit
7113 if No (Stmts) then
7114 return;
7115 end if;
7117 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
7118 Set_Debug_Info_Needed (Proc_Id);
7119 Set_Postconditions_Proc (Subp_Id, Proc_Id);
7121 -- The related subprogram is a function, create the specification of
7122 -- parameter _Result.
7124 if Present (Result) then
7125 Params := New_List (
7126 Make_Parameter_Specification (Loc,
7127 Defining_Identifier => Result,
7128 Parameter_Type =>
7129 New_Occurrence_Of (Etype (Result), Loc)));
7130 end if;
7132 -- Insert _Postconditions before the first source declaration of the
7133 -- body. This ensures that the body will not cause any premature
7134 -- freezing as it may mention types:
7136 -- procedure Proc (Obj : Array_Typ) is
7137 -- procedure _postconditions is
7138 -- begin
7139 -- ... Obj ...
7140 -- end _postconditions;
7142 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7143 -- begin
7145 -- In the example above, Obj is of type T but the incorrect placement
7146 -- of _Postconditions will cause a crash in gigi due to an out of
7147 -- order reference. The body of _Postconditions must be placed after
7148 -- the declaration of Temp to preserve correct visibility.
7150 -- Set an explicit End_Lavel to override the sloc of the implicit
7151 -- RETURN statement, and prevent it from inheriting the sloc of one
7152 -- the postconditions: this would cause confusing debug into to be
7153 -- produced, interfering with coverage analysis tools.
7155 Proc_Bod :=
7156 Make_Subprogram_Body (Loc,
7157 Specification =>
7158 Make_Procedure_Specification (Loc,
7159 Defining_Unit_Name => Proc_Id,
7160 Parameter_Specifications => Params),
7162 Declarations => Empty_List,
7163 Handled_Statement_Sequence =>
7164 Make_Handled_Sequence_Of_Statements (Loc,
7165 Statements => Stmts,
7166 End_Label => Make_Identifier (Loc, Chars (Proc_Id))));
7168 Insert_Before_First_Source_Declaration (Proc_Bod);
7169 Analyze (Proc_Bod);
7170 end Build_Postconditions_Procedure;
7172 -----------------------------------
7173 -- Build_Pragma_Check_Equivalent --
7174 -----------------------------------
7176 function Build_Pragma_Check_Equivalent
7177 (Prag : Node_Id;
7178 Subp_Id : Entity_Id := Empty;
7179 Inher_Id : Entity_Id := Empty) return Node_Id
7181 function Suppress_Reference (N : Node_Id) return Traverse_Result;
7182 -- Detect whether node N references a formal parameter subject to
7183 -- pragma Unreferenced. If this is the case, set Comes_From_Source
7184 -- to False to suppress the generation of a reference when analyzing
7185 -- N later on.
7187 ------------------------
7188 -- Suppress_Reference --
7189 ------------------------
7191 function Suppress_Reference (N : Node_Id) return Traverse_Result is
7192 Formal : Entity_Id;
7194 begin
7195 if Is_Entity_Name (N) and then Present (Entity (N)) then
7196 Formal := Entity (N);
7198 -- The formal parameter is subject to pragma Unreferenced.
7199 -- Prevent the generation of a reference by resetting the
7200 -- Comes_From_Source flag.
7202 if Is_Formal (Formal)
7203 and then Has_Pragma_Unreferenced (Formal)
7204 then
7205 Set_Comes_From_Source (N, False);
7206 end if;
7207 end if;
7209 return OK;
7210 end Suppress_Reference;
7212 procedure Suppress_References is
7213 new Traverse_Proc (Suppress_Reference);
7215 -- Local variables
7217 Loc : constant Source_Ptr := Sloc (Prag);
7218 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
7219 Check_Prag : Node_Id;
7220 Formals_Map : Elist_Id;
7221 Inher_Formal : Entity_Id;
7222 Msg_Arg : Node_Id;
7223 Nam : Name_Id;
7224 Subp_Formal : Entity_Id;
7226 -- Start of processing for Build_Pragma_Check_Equivalent
7228 begin
7229 Formals_Map := No_Elist;
7231 -- When the pre- or postcondition is inherited, map the formals of
7232 -- the inherited subprogram to those of the current subprogram.
7234 if Present (Inher_Id) then
7235 pragma Assert (Present (Subp_Id));
7237 Formals_Map := New_Elmt_List;
7239 -- Create a relation <inherited formal> => <subprogram formal>
7241 Inher_Formal := First_Formal (Inher_Id);
7242 Subp_Formal := First_Formal (Subp_Id);
7243 while Present (Inher_Formal) and then Present (Subp_Formal) loop
7244 Append_Elmt (Inher_Formal, Formals_Map);
7245 Append_Elmt (Subp_Formal, Formals_Map);
7247 Next_Formal (Inher_Formal);
7248 Next_Formal (Subp_Formal);
7249 end loop;
7250 end if;
7252 -- Copy the original pragma while performing substitutions (if
7253 -- applicable).
7255 Check_Prag :=
7256 New_Copy_Tree
7257 (Source => Prag,
7258 Map => Formals_Map,
7259 New_Scope => Current_Scope);
7261 -- Mark the pragma as being internally generated and reset the
7262 -- Analyzed flag.
7264 Set_Analyzed (Check_Prag, False);
7265 Set_Comes_From_Source (Check_Prag, False);
7267 -- The tree of the original pragma may contain references to the
7268 -- formal parameters of the related subprogram. At the same time
7269 -- the corresponding body may mark the formals as unreferenced:
7271 -- procedure Proc (Formal : ...)
7272 -- with Pre => Formal ...;
7274 -- procedure Proc (Formal : ...) is
7275 -- pragma Unreferenced (Formal);
7276 -- ...
7278 -- This creates problems because all pragma Check equivalents are
7279 -- analyzed at the end of the body declarations. Since all source
7280 -- references have already been accounted for, reset any references
7281 -- to such formals in the generated pragma Check equivalent.
7283 Suppress_References (Check_Prag);
7285 if Present (Corresponding_Aspect (Prag)) then
7286 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
7287 else
7288 Nam := Prag_Nam;
7289 end if;
7291 -- Convert the copy into pragma Check by correcting the name and
7292 -- adding a check_kind argument.
7294 Set_Pragma_Identifier
7295 (Check_Prag, Make_Identifier (Loc, Name_Check));
7297 Prepend_To (Pragma_Argument_Associations (Check_Prag),
7298 Make_Pragma_Argument_Association (Loc,
7299 Expression => Make_Identifier (Loc, Nam)));
7301 -- Update the error message when the pragma is inherited
7303 if Present (Inher_Id) then
7304 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
7306 if Chars (Msg_Arg) = Name_Message then
7307 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
7309 -- Insert "inherited" to improve the error message
7311 if Name_Buffer (1 .. 8) = "failed p" then
7312 Insert_Str_In_Name_Buffer ("inherited ", 8);
7313 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
7314 end if;
7315 end if;
7316 end if;
7318 return Check_Prag;
7319 end Build_Pragma_Check_Equivalent;
7321 ----------------------------
7322 -- Process_Contract_Cases --
7323 ----------------------------
7325 procedure Process_Contract_Cases (Stmts : in out List_Id) is
7326 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id);
7327 -- Process pragma Contract_Cases for subprogram Subp_Id
7329 --------------------------------
7330 -- Process_Contract_Cases_For --
7331 --------------------------------
7333 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id) is
7334 Items : constant Node_Id := Contract (Subp_Id);
7335 Prag : Node_Id;
7337 begin
7338 if Present (Items) then
7339 Prag := Contract_Test_Cases (Items);
7340 while Present (Prag) loop
7341 if Pragma_Name (Prag) = Name_Contract_Cases then
7342 Expand_Pragma_Contract_Cases
7343 (CCs => Prag,
7344 Subp_Id => Subp_Id,
7345 Decls => Declarations (N),
7346 Stmts => Stmts);
7347 end if;
7349 Prag := Next_Pragma (Prag);
7350 end loop;
7351 end if;
7352 end Process_Contract_Cases_For;
7354 -- Start of processing for Process_Contract_Cases
7356 begin
7357 Process_Contract_Cases_For (Body_Id);
7359 if Present (Spec_Id) then
7360 Process_Contract_Cases_For (Spec_Id);
7361 end if;
7362 end Process_Contract_Cases;
7364 ----------------------------
7365 -- Process_Postconditions --
7366 ----------------------------
7368 procedure Process_Postconditions (Stmts : in out List_Id) is
7369 procedure Process_Body_Postconditions (Post_Nam : Name_Id);
7370 -- Collect all [refined] postconditions of a specific kind denoted
7371 -- by Post_Nam that belong to the body and generate pragma Check
7372 -- equivalents in list Stmts.
7374 procedure Process_Spec_Postconditions;
7375 -- Collect all [inherited] postconditions of the spec and generate
7376 -- pragma Check equivalents in list Stmts.
7378 ---------------------------------
7379 -- Process_Body_Postconditions --
7380 ---------------------------------
7382 procedure Process_Body_Postconditions (Post_Nam : Name_Id) is
7383 Items : constant Node_Id := Contract (Body_Id);
7384 Unit_Decl : constant Node_Id := Parent (N);
7385 Decl : Node_Id;
7386 Prag : Node_Id;
7388 begin
7389 -- Process the contract
7391 if Present (Items) then
7392 Prag := Pre_Post_Conditions (Items);
7393 while Present (Prag) loop
7394 if Pragma_Name (Prag) = Post_Nam then
7395 Append_Enabled_Item
7396 (Item => Build_Pragma_Check_Equivalent (Prag),
7397 List => Stmts);
7398 end if;
7400 Prag := Next_Pragma (Prag);
7401 end loop;
7402 end if;
7404 -- The subprogram body being processed is actually the proper body
7405 -- of a stub with a corresponding spec. The subprogram stub may
7406 -- carry a postcondition pragma in which case it must be taken
7407 -- into account. The pragma appears after the stub.
7409 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
7410 Decl := Next (Corresponding_Stub (Unit_Decl));
7411 while Present (Decl) loop
7413 -- Note that non-matching pragmas are skipped
7415 if Nkind (Decl) = N_Pragma then
7416 if Pragma_Name (Decl) = Post_Nam then
7417 Append_Enabled_Item
7418 (Item => Build_Pragma_Check_Equivalent (Decl),
7419 List => Stmts);
7420 end if;
7422 -- Skip internally generated code
7424 elsif not Comes_From_Source (Decl) then
7425 null;
7427 -- Postcondition pragmas are usually grouped together. There
7428 -- is no need to inspect the whole declarative list.
7430 else
7431 exit;
7432 end if;
7434 Next (Decl);
7435 end loop;
7436 end if;
7437 end Process_Body_Postconditions;
7439 ---------------------------------
7440 -- Process_Spec_Postconditions --
7441 ---------------------------------
7443 procedure Process_Spec_Postconditions is
7444 Subps : constant Subprogram_List :=
7445 Inherited_Subprograms (Spec_Id);
7446 Items : Node_Id;
7447 Prag : Node_Id;
7448 Subp_Id : Entity_Id;
7450 begin
7451 -- Process the contract
7453 Items := Contract (Spec_Id);
7455 if Present (Items) then
7456 Prag := Pre_Post_Conditions (Items);
7457 while Present (Prag) loop
7458 if Pragma_Name (Prag) = Name_Postcondition then
7459 Append_Enabled_Item
7460 (Item => Build_Pragma_Check_Equivalent (Prag),
7461 List => Stmts);
7462 end if;
7464 Prag := Next_Pragma (Prag);
7465 end loop;
7466 end if;
7468 -- Process the contracts of all inherited subprograms, looking for
7469 -- class-wide postconditions.
7471 for Index in Subps'Range loop
7472 Subp_Id := Subps (Index);
7473 Items := Contract (Subp_Id);
7475 if Present (Items) then
7476 Prag := Pre_Post_Conditions (Items);
7477 while Present (Prag) loop
7478 if Pragma_Name (Prag) = Name_Postcondition
7479 and then Class_Present (Prag)
7480 then
7481 Append_Enabled_Item
7482 (Item =>
7483 Build_Pragma_Check_Equivalent
7484 (Prag => Prag,
7485 Subp_Id => Spec_Id,
7486 Inher_Id => Subp_Id),
7487 List => Stmts);
7488 end if;
7490 Prag := Next_Pragma (Prag);
7491 end loop;
7492 end if;
7493 end loop;
7494 end Process_Spec_Postconditions;
7496 -- Start of processing for Process_Postconditions
7498 begin
7499 -- The processing of postconditions is done in reverse order (body
7500 -- first) to ensure the following arrangement:
7502 -- <refined postconditions from body>
7503 -- <postconditions from body>
7504 -- <postconditions from spec>
7505 -- <inherited postconditions>
7507 Process_Body_Postconditions (Name_Refined_Post);
7508 Process_Body_Postconditions (Name_Postcondition);
7510 if Present (Spec_Id) then
7511 Process_Spec_Postconditions;
7512 end if;
7513 end Process_Postconditions;
7515 ---------------------------
7516 -- Process_Preconditions --
7517 ---------------------------
7519 procedure Process_Preconditions is
7520 Class_Pre : Node_Id := Empty;
7521 -- The sole [inherited] class-wide precondition pragma that applies
7522 -- to the subprogram.
7524 Insert_Node : Node_Id := Empty;
7525 -- The insertion node after which all pragma Check equivalents are
7526 -- inserted.
7528 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
7529 -- Merge two class-wide preconditions by "or else"-ing them. The
7530 -- changes are accumulated in parameter Into. Update the error
7531 -- message of Into.
7533 procedure Prepend_To_Decls (Item : Node_Id);
7534 -- Prepend a single item to the declarations of the subprogram body
7536 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id);
7537 -- Save a class-wide precondition into Class_Pre or prepend a normal
7538 -- precondition ot the declarations of the body and analyze it.
7540 procedure Process_Inherited_Preconditions;
7541 -- Collect all inherited class-wide preconditions and merge them into
7542 -- one big precondition to be evaluated as pragma Check.
7544 procedure Process_Preconditions_For (Subp_Id : Entity_Id);
7545 -- Collect all preconditions of subprogram Subp_Id and prepend their
7546 -- pragma Check equivalents to the declarations of the body.
7548 -------------------------
7549 -- Merge_Preconditions --
7550 -------------------------
7552 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
7553 function Expression_Arg (Prag : Node_Id) return Node_Id;
7554 -- Return the boolean expression argument of a precondition while
7555 -- updating its parenteses count for the subsequent merge.
7557 function Message_Arg (Prag : Node_Id) return Node_Id;
7558 -- Return the message argument of a precondition
7560 --------------------
7561 -- Expression_Arg --
7562 --------------------
7564 function Expression_Arg (Prag : Node_Id) return Node_Id is
7565 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7566 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
7568 begin
7569 if Paren_Count (Arg) = 0 then
7570 Set_Paren_Count (Arg, 1);
7571 end if;
7573 return Arg;
7574 end Expression_Arg;
7576 -----------------
7577 -- Message_Arg --
7578 -----------------
7580 function Message_Arg (Prag : Node_Id) return Node_Id is
7581 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7582 begin
7583 return Get_Pragma_Arg (Last (Args));
7584 end Message_Arg;
7586 -- Local variables
7588 From_Expr : constant Node_Id := Expression_Arg (From);
7589 From_Msg : constant Node_Id := Message_Arg (From);
7590 Into_Expr : constant Node_Id := Expression_Arg (Into);
7591 Into_Msg : constant Node_Id := Message_Arg (Into);
7592 Loc : constant Source_Ptr := Sloc (Into);
7594 -- Start of processing for Merge_Preconditions
7596 begin
7597 -- Merge the two preconditions by "or else"-ing them
7599 Rewrite (Into_Expr,
7600 Make_Or_Else (Loc,
7601 Right_Opnd => Relocate_Node (Into_Expr),
7602 Left_Opnd => From_Expr));
7604 -- Merge the two error messages to produce a single message of the
7605 -- form:
7607 -- failed precondition from ...
7608 -- also failed inherited precondition from ...
7610 if not Exception_Locations_Suppressed then
7611 Start_String (Strval (Into_Msg));
7612 Store_String_Char (ASCII.LF);
7613 Store_String_Chars (" also ");
7614 Store_String_Chars (Strval (From_Msg));
7616 Set_Strval (Into_Msg, End_String);
7617 end if;
7618 end Merge_Preconditions;
7620 ----------------------
7621 -- Prepend_To_Decls --
7622 ----------------------
7624 procedure Prepend_To_Decls (Item : Node_Id) is
7625 Decls : List_Id := Declarations (N);
7627 begin
7628 -- Ensure that the body has a declarative list
7630 if No (Decls) then
7631 Decls := New_List;
7632 Set_Declarations (N, Decls);
7633 end if;
7635 Prepend_To (Decls, Item);
7636 end Prepend_To_Decls;
7638 ------------------------------
7639 -- Prepend_To_Decls_Or_Save --
7640 ------------------------------
7642 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id) is
7643 Check_Prag : Node_Id;
7645 begin
7646 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
7648 -- Save the sole class-wide precondition (if any) for the next
7649 -- step where it will be merged with inherited preconditions.
7651 if Class_Present (Prag) then
7652 pragma Assert (No (Class_Pre));
7653 Class_Pre := Check_Prag;
7655 -- Accumulate the corresponding Check pragmas at the top of the
7656 -- declarations. Prepending the items ensures that they will be
7657 -- evaluated in their original order.
7659 else
7660 if Present (Insert_Node) then
7661 Insert_After (Insert_Node, Check_Prag);
7662 else
7663 Prepend_To_Decls (Check_Prag);
7664 end if;
7666 Analyze (Check_Prag);
7667 end if;
7668 end Prepend_To_Decls_Or_Save;
7670 -------------------------------------
7671 -- Process_Inherited_Preconditions --
7672 -------------------------------------
7674 procedure Process_Inherited_Preconditions is
7675 Subps : constant Subprogram_List :=
7676 Inherited_Subprograms (Spec_Id);
7677 Check_Prag : Node_Id;
7678 Items : Node_Id;
7679 Prag : Node_Id;
7680 Subp_Id : Entity_Id;
7682 begin
7683 -- Process the contracts of all inherited subprograms, looking for
7684 -- class-wide preconditions.
7686 for Index in Subps'Range loop
7687 Subp_Id := Subps (Index);
7688 Items := Contract (Subp_Id);
7690 if Present (Items) then
7691 Prag := Pre_Post_Conditions (Items);
7692 while Present (Prag) loop
7693 if Pragma_Name (Prag) = Name_Precondition
7694 and then Class_Present (Prag)
7695 then
7696 Check_Prag :=
7697 Build_Pragma_Check_Equivalent
7698 (Prag => Prag,
7699 Subp_Id => Spec_Id,
7700 Inher_Id => Subp_Id);
7702 -- The spec or an inherited subprogram already yielded
7703 -- a class-wide precondition. Merge the existing
7704 -- precondition with the current one using "or else".
7706 if Present (Class_Pre) then
7707 Merge_Preconditions (Check_Prag, Class_Pre);
7708 else
7709 Class_Pre := Check_Prag;
7710 end if;
7711 end if;
7713 Prag := Next_Pragma (Prag);
7714 end loop;
7715 end if;
7716 end loop;
7718 -- Add the merged class-wide preconditions
7720 if Present (Class_Pre) then
7721 Prepend_To_Decls (Class_Pre);
7722 Analyze (Class_Pre);
7723 end if;
7724 end Process_Inherited_Preconditions;
7726 -------------------------------
7727 -- Process_Preconditions_For --
7728 -------------------------------
7730 procedure Process_Preconditions_For (Subp_Id : Entity_Id) is
7731 Items : constant Node_Id := Contract (Subp_Id);
7732 Decl : Node_Id;
7733 Prag : Node_Id;
7734 Subp_Decl : Node_Id;
7736 begin
7737 -- Process the contract
7739 if Present (Items) then
7740 Prag := Pre_Post_Conditions (Items);
7741 while Present (Prag) loop
7742 if Pragma_Name (Prag) = Name_Precondition then
7743 Prepend_To_Decls_Or_Save (Prag);
7744 end if;
7746 Prag := Next_Pragma (Prag);
7747 end loop;
7748 end if;
7750 -- The subprogram declaration being processed is actually a body
7751 -- stub. The stub may carry a precondition pragma in which case it
7752 -- must be taken into account. The pragma appears after the stub.
7754 Subp_Decl := Unit_Declaration_Node (Subp_Id);
7756 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
7758 -- Inspect the declarations following the body stub
7760 Decl := Next (Subp_Decl);
7761 while Present (Decl) loop
7763 -- Note that non-matching pragmas are skipped
7765 if Nkind (Decl) = N_Pragma then
7766 if Pragma_Name (Decl) = Name_Precondition then
7767 Prepend_To_Decls_Or_Save (Decl);
7768 end if;
7770 -- Skip internally generated code
7772 elsif not Comes_From_Source (Decl) then
7773 null;
7775 -- Preconditions are usually grouped together. There is no
7776 -- need to inspect the whole declarative list.
7778 else
7779 exit;
7780 end if;
7782 Next (Decl);
7783 end loop;
7784 end if;
7785 end Process_Preconditions_For;
7787 -- Local variables
7789 Decls : constant List_Id := Declarations (N);
7790 Decl : Node_Id;
7792 -- Start of processing for Process_Preconditions
7794 begin
7795 -- Find the last internally generate declaration starting from the
7796 -- top of the body declarations. This ensures that discriminals and
7797 -- subtypes are properly visible to the pragma Check equivalents.
7799 if Present (Decls) then
7800 Decl := First (Decls);
7801 while Present (Decl) loop
7802 exit when Comes_From_Source (Decl);
7803 Insert_Node := Decl;
7804 Next (Decl);
7805 end loop;
7806 end if;
7808 -- The processing of preconditions is done in reverse order (body
7809 -- first) because each pragma Check equivalent is inserted at the
7810 -- top of the declarations. This ensures that the final order is
7811 -- consistent with following diagram:
7813 -- <inherited preconditions>
7814 -- <preconditions from spec>
7815 -- <preconditions from body>
7817 Process_Preconditions_For (Body_Id);
7819 if Present (Spec_Id) then
7820 Process_Preconditions_For (Spec_Id);
7821 Process_Inherited_Preconditions;
7822 end if;
7823 end Process_Preconditions;
7825 -- Local variables
7827 Restore_Scope : Boolean := False;
7828 Result : Entity_Id;
7829 Stmts : List_Id := No_List;
7830 Subp_Id : Entity_Id;
7832 -- Start of processing for Expand_Subprogram_Contract
7834 begin
7835 -- Obtain the entity of the initial declaration
7837 if Present (Spec_Id) then
7838 Subp_Id := Spec_Id;
7839 else
7840 Subp_Id := Body_Id;
7841 end if;
7843 -- Do not perform expansion activity when it is not needed
7845 if not Expander_Active then
7846 return;
7848 -- ASIS requires an unaltered tree
7850 elsif ASIS_Mode then
7851 return;
7853 -- GNATprove does not need the executable semantics of a contract
7855 elsif GNATprove_Mode then
7856 return;
7858 -- The contract of a generic subprogram or one declared in a generic
7859 -- context is not expanded as the corresponding instance will provide
7860 -- the executable semantics of the contract.
7862 elsif Is_Generic_Subprogram (Subp_Id) or else Inside_A_Generic then
7863 return;
7865 -- All subprograms carry a contract, but for some it is not significant
7866 -- and should not be processed. This is a small optimization.
7868 elsif not Has_Significant_Contract (Subp_Id) then
7869 return;
7870 end if;
7872 -- Do not re-expand the same contract. This scenario occurs when a
7873 -- construct is rewritten into something else during its analysis
7874 -- (expression functions for instance).
7876 if Has_Expanded_Contract (Subp_Id) then
7877 return;
7879 -- Otherwise mark the subprogram
7881 else
7882 Set_Has_Expanded_Contract (Subp_Id);
7883 end if;
7885 -- Ensure that the formal parameters are visible when expanding all
7886 -- contract items.
7888 if not In_Open_Scopes (Subp_Id) then
7889 Restore_Scope := True;
7890 Push_Scope (Subp_Id);
7892 if Is_Generic_Subprogram (Subp_Id) then
7893 Install_Generic_Formals (Subp_Id);
7894 else
7895 Install_Formals (Subp_Id);
7896 end if;
7897 end if;
7899 -- The expansion of a subprogram contract involves the creation of Check
7900 -- pragmas to verify the contract assertions of the spec and body in a
7901 -- particular order. The order is as follows:
7903 -- function Example (...) return ... is
7904 -- procedure _Postconditions (...) is
7905 -- begin
7906 -- <refined postconditions from body>
7907 -- <postconditions from body>
7908 -- <postconditions from spec>
7909 -- <inherited postconditions>
7910 -- <contract case consequences>
7911 -- <invariant check of function result>
7912 -- <invariant and predicate checks of parameters>
7913 -- end _Postconditions;
7915 -- <inherited preconditions>
7916 -- <preconditions from spec>
7917 -- <preconditions from body>
7918 -- <contract case conditions>
7920 -- <source declarations>
7921 -- begin
7922 -- <source statements>
7924 -- _Preconditions (Result);
7925 -- return Result;
7926 -- end Example;
7928 -- Routine _Postconditions holds all contract assertions that must be
7929 -- verified on exit from the related subprogram.
7931 -- Step 1: Handle all preconditions. This action must come before the
7932 -- processing of pragma Contract_Cases because the pragma prepends items
7933 -- to the body declarations.
7935 Process_Preconditions;
7937 -- Step 2: Handle all postconditions. This action must come before the
7938 -- processing of pragma Contract_Cases because the pragma appends items
7939 -- to list Stmts.
7941 Process_Postconditions (Stmts);
7943 -- Step 3: Handle pragma Contract_Cases. This action must come before
7944 -- the processing of invariants and predicates because those append
7945 -- items to list Smts.
7947 Process_Contract_Cases (Stmts);
7949 -- Step 4: Apply invariant and predicate checks on a function result and
7950 -- all formals. The resulting checks are accumulated in list Stmts.
7952 Add_Invariant_And_Predicate_Checks (Subp_Id, Stmts, Result);
7954 -- Step 5: Construct procedure _Postconditions
7956 Build_Postconditions_Procedure (Subp_Id, Stmts, Result);
7958 if Restore_Scope then
7959 End_Scope;
7960 end if;
7961 end Expand_Subprogram_Contract;
7963 --------------------------------------------
7964 -- Has_Unconstrained_Access_Discriminants --
7965 --------------------------------------------
7967 function Has_Unconstrained_Access_Discriminants
7968 (Subtyp : Entity_Id) return Boolean
7970 Discr : Entity_Id;
7972 begin
7973 if Has_Discriminants (Subtyp)
7974 and then not Is_Constrained (Subtyp)
7975 then
7976 Discr := First_Discriminant (Subtyp);
7977 while Present (Discr) loop
7978 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7979 return True;
7980 end if;
7982 Next_Discriminant (Discr);
7983 end loop;
7984 end if;
7986 return False;
7987 end Has_Unconstrained_Access_Discriminants;
7989 ----------------
7990 -- Initialize --
7991 ----------------
7993 procedure Initialize is
7994 begin
7995 Unest_Bodies.Init;
7996 end Initialize;
7998 --------------------------------
7999 -- Is_Build_In_Place_Function --
8000 --------------------------------
8002 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
8003 begin
8004 -- This function is called from Expand_Subtype_From_Expr during
8005 -- semantic analysis, even when expansion is off. In those cases
8006 -- the build_in_place expansion will not take place.
8008 if not Expander_Active then
8009 return False;
8010 end if;
8012 -- For now we test whether E denotes a function or access-to-function
8013 -- type whose result subtype is inherently limited. Later this test
8014 -- may be revised to allow composite nonlimited types. Functions with
8015 -- a foreign convention or whose result type has a foreign convention
8016 -- never qualify.
8018 if Ekind_In (E, E_Function, E_Generic_Function)
8019 or else (Ekind (E) = E_Subprogram_Type
8020 and then Etype (E) /= Standard_Void_Type)
8021 then
8022 -- Note: If the function has a foreign convention, it cannot build
8023 -- its result in place, so you're on your own. On the other hand,
8024 -- if only the return type has a foreign convention, its layout is
8025 -- intended to be compatible with the other language, but the build-
8026 -- in place machinery can ensure that the object is not copied.
8028 if Has_Foreign_Convention (E) then
8029 return False;
8031 -- In Ada 2005 all functions with an inherently limited return type
8032 -- must be handled using a build-in-place profile, including the case
8033 -- of a function with a limited interface result, where the function
8034 -- may return objects of nonlimited descendants.
8036 else
8037 return Is_Limited_View (Etype (E))
8038 and then Ada_Version >= Ada_2005
8039 and then not Debug_Flag_Dot_L;
8040 end if;
8042 else
8043 return False;
8044 end if;
8045 end Is_Build_In_Place_Function;
8047 -------------------------------------
8048 -- Is_Build_In_Place_Function_Call --
8049 -------------------------------------
8051 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
8052 Exp_Node : Node_Id := N;
8053 Function_Id : Entity_Id;
8055 begin
8056 -- Return False if the expander is currently inactive, since awareness
8057 -- of build-in-place treatment is only relevant during expansion. Note
8058 -- that Is_Build_In_Place_Function, which is called as part of this
8059 -- function, is also conditioned this way, but we need to check here as
8060 -- well to avoid blowing up on processing protected calls when expansion
8061 -- is disabled (such as with -gnatc) since those would trip over the
8062 -- raise of Program_Error below.
8064 -- In SPARK mode, build-in-place calls are not expanded, so that we
8065 -- may end up with a call that is neither resolved to an entity, nor
8066 -- an indirect call.
8068 if not Expander_Active then
8069 return False;
8070 end if;
8072 -- Step past qualification or unchecked conversion (the latter can occur
8073 -- in cases of calls to 'Input).
8075 if Nkind_In (Exp_Node, N_Qualified_Expression,
8076 N_Unchecked_Type_Conversion)
8077 then
8078 Exp_Node := Expression (N);
8079 end if;
8081 if Nkind (Exp_Node) /= N_Function_Call then
8082 return False;
8084 else
8085 if Is_Entity_Name (Name (Exp_Node)) then
8086 Function_Id := Entity (Name (Exp_Node));
8088 -- In the case of an explicitly dereferenced call, use the subprogram
8089 -- type generated for the dereference.
8091 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8092 Function_Id := Etype (Name (Exp_Node));
8094 -- This may be a call to a protected function.
8096 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
8097 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
8099 else
8100 raise Program_Error;
8101 end if;
8103 return Is_Build_In_Place_Function (Function_Id);
8104 end if;
8105 end Is_Build_In_Place_Function_Call;
8107 -----------------------
8108 -- Freeze_Subprogram --
8109 -----------------------
8111 procedure Freeze_Subprogram (N : Node_Id) is
8112 Loc : constant Source_Ptr := Sloc (N);
8114 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
8115 -- (Ada 2005): Register a predefined primitive in all the secondary
8116 -- dispatch tables of its primitive type.
8118 ----------------------------------
8119 -- Register_Predefined_DT_Entry --
8120 ----------------------------------
8122 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
8123 Iface_DT_Ptr : Elmt_Id;
8124 Tagged_Typ : Entity_Id;
8125 Thunk_Id : Entity_Id;
8126 Thunk_Code : Node_Id;
8128 begin
8129 Tagged_Typ := Find_Dispatching_Type (Prim);
8131 if No (Access_Disp_Table (Tagged_Typ))
8132 or else not Has_Interfaces (Tagged_Typ)
8133 or else not RTE_Available (RE_Interface_Tag)
8134 or else Restriction_Active (No_Dispatching_Calls)
8135 then
8136 return;
8137 end if;
8139 -- Skip the first two access-to-dispatch-table pointers since they
8140 -- leads to the primary dispatch table (predefined DT and user
8141 -- defined DT). We are only concerned with the secondary dispatch
8142 -- table pointers. Note that the access-to- dispatch-table pointer
8143 -- corresponds to the first implemented interface retrieved below.
8145 Iface_DT_Ptr :=
8146 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
8148 while Present (Iface_DT_Ptr)
8149 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
8150 loop
8151 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8152 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
8154 if Present (Thunk_Code) then
8155 Insert_Actions_After (N, New_List (
8156 Thunk_Code,
8158 Build_Set_Predefined_Prim_Op_Address (Loc,
8159 Tag_Node =>
8160 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
8161 Position => DT_Position (Prim),
8162 Address_Node =>
8163 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8164 Make_Attribute_Reference (Loc,
8165 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
8166 Attribute_Name => Name_Unrestricted_Access))),
8168 Build_Set_Predefined_Prim_Op_Address (Loc,
8169 Tag_Node =>
8170 New_Occurrence_Of
8171 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
8172 Loc),
8173 Position => DT_Position (Prim),
8174 Address_Node =>
8175 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8176 Make_Attribute_Reference (Loc,
8177 Prefix => New_Occurrence_Of (Prim, Loc),
8178 Attribute_Name => Name_Unrestricted_Access)))));
8179 end if;
8181 -- Skip the tag of the predefined primitives dispatch table
8183 Next_Elmt (Iface_DT_Ptr);
8184 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8186 -- Skip tag of the no-thunks dispatch table
8188 Next_Elmt (Iface_DT_Ptr);
8189 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8191 -- Skip tag of predefined primitives no-thunks dispatch table
8193 Next_Elmt (Iface_DT_Ptr);
8194 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8196 Next_Elmt (Iface_DT_Ptr);
8197 end loop;
8198 end Register_Predefined_DT_Entry;
8200 -- Local variables
8202 Subp : constant Entity_Id := Entity (N);
8204 -- Start of processing for Freeze_Subprogram
8206 begin
8207 -- We suppress the initialization of the dispatch table entry when
8208 -- VM_Target because the dispatching mechanism is handled internally
8209 -- by the VM.
8211 if Is_Dispatching_Operation (Subp)
8212 and then not Is_Abstract_Subprogram (Subp)
8213 and then Present (DTC_Entity (Subp))
8214 and then Present (Scope (DTC_Entity (Subp)))
8215 and then Tagged_Type_Expansion
8216 and then not Restriction_Active (No_Dispatching_Calls)
8217 and then RTE_Available (RE_Tag)
8218 then
8219 declare
8220 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
8222 begin
8223 -- Handle private overridden primitives
8225 if not Is_CPP_Class (Typ) then
8226 Check_Overriding_Operation (Subp);
8227 end if;
8229 -- We assume that imported CPP primitives correspond with objects
8230 -- whose constructor is in the CPP side; therefore we don't need
8231 -- to generate code to register them in the dispatch table.
8233 if Is_CPP_Class (Typ) then
8234 null;
8236 -- Handle CPP primitives found in derivations of CPP_Class types.
8237 -- These primitives must have been inherited from some parent, and
8238 -- there is no need to register them in the dispatch table because
8239 -- Build_Inherit_Prims takes care of initializing these slots.
8241 elsif Is_Imported (Subp)
8242 and then (Convention (Subp) = Convention_CPP
8243 or else Convention (Subp) = Convention_C)
8244 then
8245 null;
8247 -- Generate code to register the primitive in non statically
8248 -- allocated dispatch tables
8250 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8252 -- When a primitive is frozen, enter its name in its dispatch
8253 -- table slot.
8255 if not Is_Interface (Typ)
8256 or else Present (Interface_Alias (Subp))
8257 then
8258 if Is_Predefined_Dispatching_Operation (Subp) then
8259 Register_Predefined_DT_Entry (Subp);
8260 end if;
8262 Insert_Actions_After (N,
8263 Register_Primitive (Loc, Prim => Subp));
8264 end if;
8265 end if;
8266 end;
8267 end if;
8269 -- Mark functions that return by reference. Note that it cannot be part
8270 -- of the normal semantic analysis of the spec since the underlying
8271 -- returned type may not be known yet (for private types).
8273 declare
8274 Typ : constant Entity_Id := Etype (Subp);
8275 Utyp : constant Entity_Id := Underlying_Type (Typ);
8276 begin
8277 if Is_Limited_View (Typ) then
8278 Set_Returns_By_Ref (Subp);
8279 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8280 Set_Returns_By_Ref (Subp);
8281 end if;
8282 end;
8284 -- Wnen freezing a null procedure, analyze its delayed aspects now
8285 -- because we may not have reached the end of the declarative list when
8286 -- delayed aspects are normally analyzed. This ensures that dispatching
8287 -- calls are properly rewritten when the generated _Postcondition
8288 -- procedure is analyzed in the null procedure body.
8290 if Nkind (Parent (Subp)) = N_Procedure_Specification
8291 and then Null_Present (Parent (Subp))
8292 then
8293 Analyze_Subprogram_Contract (Subp);
8294 end if;
8295 end Freeze_Subprogram;
8297 -----------------------
8298 -- Is_Null_Procedure --
8299 -----------------------
8301 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8302 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8304 begin
8305 if Ekind (Subp) /= E_Procedure then
8306 return False;
8308 -- Check if this is a declared null procedure
8310 elsif Nkind (Decl) = N_Subprogram_Declaration then
8311 if not Null_Present (Specification (Decl)) then
8312 return False;
8314 elsif No (Body_To_Inline (Decl)) then
8315 return False;
8317 -- Check if the body contains only a null statement, followed by
8318 -- the return statement added during expansion.
8320 else
8321 declare
8322 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8324 Stat : Node_Id;
8325 Stat2 : Node_Id;
8327 begin
8328 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8329 return False;
8330 else
8331 -- We must skip SCIL nodes because they are currently
8332 -- implemented as special N_Null_Statement nodes.
8334 Stat :=
8335 First_Non_SCIL_Node
8336 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8337 Stat2 := Next_Non_SCIL_Node (Stat);
8339 return
8340 Is_Empty_List (Declarations (Orig_Bod))
8341 and then Nkind (Stat) = N_Null_Statement
8342 and then
8343 (No (Stat2)
8344 or else
8345 (Nkind (Stat2) = N_Simple_Return_Statement
8346 and then No (Next (Stat2))));
8347 end if;
8348 end;
8349 end if;
8351 else
8352 return False;
8353 end if;
8354 end Is_Null_Procedure;
8356 -------------------------------------------
8357 -- Make_Build_In_Place_Call_In_Allocator --
8358 -------------------------------------------
8360 procedure Make_Build_In_Place_Call_In_Allocator
8361 (Allocator : Node_Id;
8362 Function_Call : Node_Id)
8364 Acc_Type : constant Entity_Id := Etype (Allocator);
8365 Loc : Source_Ptr;
8366 Func_Call : Node_Id := Function_Call;
8367 Ref_Func_Call : Node_Id;
8368 Function_Id : Entity_Id;
8369 Result_Subt : Entity_Id;
8370 New_Allocator : Node_Id;
8371 Return_Obj_Access : Entity_Id; -- temp for function result
8372 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8373 Alloc_Form : BIP_Allocation_Form;
8374 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8375 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8376 Chain : Entity_Id; -- activation chain, in case of tasks
8378 begin
8379 -- Step past qualification or unchecked conversion (the latter can occur
8380 -- in cases of calls to 'Input).
8382 if Nkind_In (Func_Call,
8383 N_Qualified_Expression,
8384 N_Unchecked_Type_Conversion)
8385 then
8386 Func_Call := Expression (Func_Call);
8387 end if;
8389 -- If the call has already been processed to add build-in-place actuals
8390 -- then return. This should not normally occur in an allocator context,
8391 -- but we add the protection as a defensive measure.
8393 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8394 return;
8395 end if;
8397 -- Mark the call as processed as a build-in-place call
8399 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8401 Loc := Sloc (Function_Call);
8403 if Is_Entity_Name (Name (Func_Call)) then
8404 Function_Id := Entity (Name (Func_Call));
8406 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8407 Function_Id := Etype (Name (Func_Call));
8409 else
8410 raise Program_Error;
8411 end if;
8413 Result_Subt := Available_View (Etype (Function_Id));
8415 -- Create a temp for the function result. In the caller-allocates case,
8416 -- this will be initialized to the result of a new uninitialized
8417 -- allocator. Note: we do not use Allocator as the Related_Node of
8418 -- Return_Obj_Access in call to Make_Temporary below as this would
8419 -- create a sort of infinite "recursion".
8421 Return_Obj_Access := Make_Temporary (Loc, 'R');
8422 Set_Etype (Return_Obj_Access, Acc_Type);
8424 -- When the result subtype is constrained, the return object is
8425 -- allocated on the caller side, and access to it is passed to the
8426 -- function.
8428 -- Here and in related routines, we must examine the full view of the
8429 -- type, because the view at the point of call may differ from that
8430 -- that in the function body, and the expansion mechanism depends on
8431 -- the characteristics of the full view.
8433 if Is_Constrained (Underlying_Type (Result_Subt)) then
8435 -- Replace the initialized allocator of form "new T'(Func (...))"
8436 -- with an uninitialized allocator of form "new T", where T is the
8437 -- result subtype of the called function. The call to the function
8438 -- is handled separately further below.
8440 New_Allocator :=
8441 Make_Allocator (Loc,
8442 Expression => New_Occurrence_Of (Result_Subt, Loc));
8443 Set_No_Initialization (New_Allocator);
8445 -- Copy attributes to new allocator. Note that the new allocator
8446 -- logically comes from source if the original one did, so copy the
8447 -- relevant flag. This ensures proper treatment of the restriction
8448 -- No_Implicit_Heap_Allocations in this case.
8450 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8451 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8452 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8454 Rewrite (Allocator, New_Allocator);
8456 -- Initial value of the temp is the result of the uninitialized
8457 -- allocator
8459 Temp_Init := Relocate_Node (Allocator);
8461 -- Indicate that caller allocates, and pass in the return object
8463 Alloc_Form := Caller_Allocation;
8464 Pool := Make_Null (No_Location);
8465 Return_Obj_Actual :=
8466 Make_Unchecked_Type_Conversion (Loc,
8467 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8468 Expression =>
8469 Make_Explicit_Dereference (Loc,
8470 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8472 -- When the result subtype is unconstrained, the function itself must
8473 -- perform the allocation of the return object, so we pass parameters
8474 -- indicating that.
8476 else
8477 Temp_Init := Empty;
8479 -- Case of a user-defined storage pool. Pass an allocation parameter
8480 -- indicating that the function should allocate its result in the
8481 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8482 -- pool may not be aliased.
8484 if VM_Target = No_VM
8485 and then Present (Associated_Storage_Pool (Acc_Type))
8486 then
8487 Alloc_Form := User_Storage_Pool;
8488 Pool :=
8489 Make_Attribute_Reference (Loc,
8490 Prefix =>
8491 New_Occurrence_Of
8492 (Associated_Storage_Pool (Acc_Type), Loc),
8493 Attribute_Name => Name_Unrestricted_Access);
8495 -- No user-defined pool; pass an allocation parameter indicating that
8496 -- the function should allocate its result on the heap.
8498 else
8499 Alloc_Form := Global_Heap;
8500 Pool := Make_Null (No_Location);
8501 end if;
8503 -- The caller does not provide the return object in this case, so we
8504 -- have to pass null for the object access actual.
8506 Return_Obj_Actual := Empty;
8507 end if;
8509 -- Declare the temp object
8511 Insert_Action (Allocator,
8512 Make_Object_Declaration (Loc,
8513 Defining_Identifier => Return_Obj_Access,
8514 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8515 Expression => Temp_Init));
8517 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8519 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8520 -- then generate an implicit conversion to force displacement of the
8521 -- "this" pointer.
8523 if Is_Interface (Designated_Type (Acc_Type)) then
8524 Rewrite
8525 (Ref_Func_Call,
8526 OK_Convert_To (Acc_Type, Ref_Func_Call));
8527 end if;
8529 declare
8530 Assign : constant Node_Id :=
8531 Make_Assignment_Statement (Loc,
8532 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8533 Expression => Ref_Func_Call);
8534 -- Assign the result of the function call into the temp. In the
8535 -- caller-allocates case, this is overwriting the temp with its
8536 -- initial value, which has no effect. In the callee-allocates case,
8537 -- this is setting the temp to point to the object allocated by the
8538 -- callee.
8540 Actions : List_Id;
8541 -- Actions to be inserted. If there are no tasks, this is just the
8542 -- assignment statement. If the allocated object has tasks, we need
8543 -- to wrap the assignment in a block that activates them. The
8544 -- activation chain of that block must be passed to the function,
8545 -- rather than some outer chain.
8546 begin
8547 if Has_Task (Result_Subt) then
8548 Actions := New_List;
8549 Build_Task_Allocate_Block_With_Init_Stmts
8550 (Actions, Allocator, Init_Stmts => New_List (Assign));
8551 Chain := Activation_Chain_Entity (Last (Actions));
8552 else
8553 Actions := New_List (Assign);
8554 Chain := Empty;
8555 end if;
8557 Insert_Actions (Allocator, Actions);
8558 end;
8560 -- When the function has a controlling result, an allocation-form
8561 -- parameter must be passed indicating that the caller is allocating
8562 -- the result object. This is needed because such a function can be
8563 -- called as a dispatching operation and must be treated similarly
8564 -- to functions with unconstrained result subtypes.
8566 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8567 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8569 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8570 (Func_Call, Function_Id, Acc_Type);
8572 Add_Task_Actuals_To_Build_In_Place_Call
8573 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8574 Chain => Chain);
8576 -- Add an implicit actual to the function call that provides access
8577 -- to the allocated object. An unchecked conversion to the (specific)
8578 -- result subtype of the function is inserted to handle cases where
8579 -- the access type of the allocator has a class-wide designated type.
8581 Add_Access_Actual_To_Build_In_Place_Call
8582 (Func_Call, Function_Id, Return_Obj_Actual);
8584 -- Finally, replace the allocator node with a reference to the temp
8586 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8588 Analyze_And_Resolve (Allocator, Acc_Type);
8589 end Make_Build_In_Place_Call_In_Allocator;
8591 ---------------------------------------------------
8592 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8593 ---------------------------------------------------
8595 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8596 (Function_Call : Node_Id)
8598 Loc : Source_Ptr;
8599 Func_Call : Node_Id := Function_Call;
8600 Function_Id : Entity_Id;
8601 Result_Subt : Entity_Id;
8602 Return_Obj_Id : Entity_Id;
8603 Return_Obj_Decl : Entity_Id;
8605 begin
8606 -- Step past qualification or unchecked conversion (the latter can occur
8607 -- in cases of calls to 'Input).
8609 if Nkind_In (Func_Call, N_Qualified_Expression,
8610 N_Unchecked_Type_Conversion)
8611 then
8612 Func_Call := Expression (Func_Call);
8613 end if;
8615 -- If the call has already been processed to add build-in-place actuals
8616 -- then return. One place this can occur is for calls to build-in-place
8617 -- functions that occur within a call to a protected operation, where
8618 -- due to rewriting and expansion of the protected call there can be
8619 -- more than one call to Expand_Actuals for the same set of actuals.
8621 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8622 return;
8623 end if;
8625 -- Mark the call as processed as a build-in-place call
8627 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8629 Loc := Sloc (Function_Call);
8631 if Is_Entity_Name (Name (Func_Call)) then
8632 Function_Id := Entity (Name (Func_Call));
8634 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8635 Function_Id := Etype (Name (Func_Call));
8637 else
8638 raise Program_Error;
8639 end if;
8641 Result_Subt := Etype (Function_Id);
8643 -- If the build-in-place function returns a controlled object, then the
8644 -- object needs to be finalized immediately after the context. Since
8645 -- this case produces a transient scope, the servicing finalizer needs
8646 -- to name the returned object. Create a temporary which is initialized
8647 -- with the function call:
8649 -- Temp_Id : Func_Type := BIP_Func_Call;
8651 -- The initialization expression of the temporary will be rewritten by
8652 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8653 -- Call_In_Object_Declaration.
8655 if Needs_Finalization (Result_Subt) then
8656 declare
8657 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8658 Temp_Decl : Node_Id;
8660 begin
8661 -- Reset the guard on the function call since the following does
8662 -- not perform actual call expansion.
8664 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8666 Temp_Decl :=
8667 Make_Object_Declaration (Loc,
8668 Defining_Identifier => Temp_Id,
8669 Object_Definition =>
8670 New_Occurrence_Of (Result_Subt, Loc),
8671 Expression =>
8672 New_Copy_Tree (Function_Call));
8674 Insert_Action (Function_Call, Temp_Decl);
8676 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8677 Analyze (Function_Call);
8678 end;
8680 -- When the result subtype is constrained, an object of the subtype is
8681 -- declared and an access value designating it is passed as an actual.
8683 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8685 -- Create a temporary object to hold the function result
8687 Return_Obj_Id := Make_Temporary (Loc, 'R');
8688 Set_Etype (Return_Obj_Id, Result_Subt);
8690 Return_Obj_Decl :=
8691 Make_Object_Declaration (Loc,
8692 Defining_Identifier => Return_Obj_Id,
8693 Aliased_Present => True,
8694 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8696 Set_No_Initialization (Return_Obj_Decl);
8698 Insert_Action (Func_Call, Return_Obj_Decl);
8700 -- When the function has a controlling result, an allocation-form
8701 -- parameter must be passed indicating that the caller is allocating
8702 -- the result object. This is needed because such a function can be
8703 -- called as a dispatching operation and must be treated similarly
8704 -- to functions with unconstrained result subtypes.
8706 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8707 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8709 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8710 (Func_Call, Function_Id);
8712 Add_Task_Actuals_To_Build_In_Place_Call
8713 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8715 -- Add an implicit actual to the function call that provides access
8716 -- to the caller's return object.
8718 Add_Access_Actual_To_Build_In_Place_Call
8719 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8721 -- When the result subtype is unconstrained, the function must allocate
8722 -- the return object in the secondary stack, so appropriate implicit
8723 -- parameters are added to the call to indicate that. A transient
8724 -- scope is established to ensure eventual cleanup of the result.
8726 else
8727 -- Pass an allocation parameter indicating that the function should
8728 -- allocate its result on the secondary stack.
8730 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8731 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8733 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8734 (Func_Call, Function_Id);
8736 Add_Task_Actuals_To_Build_In_Place_Call
8737 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8739 -- Pass a null value to the function since no return object is
8740 -- available on the caller side.
8742 Add_Access_Actual_To_Build_In_Place_Call
8743 (Func_Call, Function_Id, Empty);
8744 end if;
8745 end Make_Build_In_Place_Call_In_Anonymous_Context;
8747 --------------------------------------------
8748 -- Make_Build_In_Place_Call_In_Assignment --
8749 --------------------------------------------
8751 procedure Make_Build_In_Place_Call_In_Assignment
8752 (Assign : Node_Id;
8753 Function_Call : Node_Id)
8755 Lhs : constant Node_Id := Name (Assign);
8756 Func_Call : Node_Id := Function_Call;
8757 Func_Id : Entity_Id;
8758 Loc : Source_Ptr;
8759 Obj_Decl : Node_Id;
8760 Obj_Id : Entity_Id;
8761 Ptr_Typ : Entity_Id;
8762 Ptr_Typ_Decl : Node_Id;
8763 New_Expr : Node_Id;
8764 Result_Subt : Entity_Id;
8765 Target : Node_Id;
8767 begin
8768 -- Step past qualification or unchecked conversion (the latter can occur
8769 -- in cases of calls to 'Input).
8771 if Nkind_In (Func_Call, N_Qualified_Expression,
8772 N_Unchecked_Type_Conversion)
8773 then
8774 Func_Call := Expression (Func_Call);
8775 end if;
8777 -- If the call has already been processed to add build-in-place actuals
8778 -- then return. This should not normally occur in an assignment context,
8779 -- but we add the protection as a defensive measure.
8781 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8782 return;
8783 end if;
8785 -- Mark the call as processed as a build-in-place call
8787 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8789 Loc := Sloc (Function_Call);
8791 if Is_Entity_Name (Name (Func_Call)) then
8792 Func_Id := Entity (Name (Func_Call));
8794 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8795 Func_Id := Etype (Name (Func_Call));
8797 else
8798 raise Program_Error;
8799 end if;
8801 Result_Subt := Etype (Func_Id);
8803 -- When the result subtype is unconstrained, an additional actual must
8804 -- be passed to indicate that the caller is providing the return object.
8805 -- This parameter must also be passed when the called function has a
8806 -- controlling result, because dispatching calls to the function needs
8807 -- to be treated effectively the same as calls to class-wide functions.
8809 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8810 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8812 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8813 (Func_Call, Func_Id);
8815 Add_Task_Actuals_To_Build_In_Place_Call
8816 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8818 -- Add an implicit actual to the function call that provides access to
8819 -- the caller's return object.
8821 Add_Access_Actual_To_Build_In_Place_Call
8822 (Func_Call,
8823 Func_Id,
8824 Make_Unchecked_Type_Conversion (Loc,
8825 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8826 Expression => Relocate_Node (Lhs)));
8828 -- Create an access type designating the function's result subtype
8830 Ptr_Typ := Make_Temporary (Loc, 'A');
8832 Ptr_Typ_Decl :=
8833 Make_Full_Type_Declaration (Loc,
8834 Defining_Identifier => Ptr_Typ,
8835 Type_Definition =>
8836 Make_Access_To_Object_Definition (Loc,
8837 All_Present => True,
8838 Subtype_Indication =>
8839 New_Occurrence_Of (Result_Subt, Loc)));
8840 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8842 -- Finally, create an access object initialized to a reference to the
8843 -- function call. We know this access value is non-null, so mark the
8844 -- entity accordingly to suppress junk access checks.
8846 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8848 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8849 Set_Etype (Obj_Id, Ptr_Typ);
8850 Set_Is_Known_Non_Null (Obj_Id);
8852 Obj_Decl :=
8853 Make_Object_Declaration (Loc,
8854 Defining_Identifier => Obj_Id,
8855 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8856 Expression => New_Expr);
8857 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8859 Rewrite (Assign, Make_Null_Statement (Loc));
8861 -- Retrieve the target of the assignment
8863 if Nkind (Lhs) = N_Selected_Component then
8864 Target := Selector_Name (Lhs);
8865 elsif Nkind (Lhs) = N_Type_Conversion then
8866 Target := Expression (Lhs);
8867 else
8868 Target := Lhs;
8869 end if;
8871 -- If we are assigning to a return object or this is an expression of
8872 -- an extension aggregate, the target should either be an identifier
8873 -- or a simple expression. All other cases imply a different scenario.
8875 if Nkind (Target) in N_Has_Entity then
8876 Target := Entity (Target);
8877 else
8878 return;
8879 end if;
8880 end Make_Build_In_Place_Call_In_Assignment;
8882 ----------------------------------------------------
8883 -- Make_Build_In_Place_Call_In_Object_Declaration --
8884 ----------------------------------------------------
8886 procedure Make_Build_In_Place_Call_In_Object_Declaration
8887 (Object_Decl : Node_Id;
8888 Function_Call : Node_Id)
8890 Loc : Source_Ptr;
8891 Obj_Def_Id : constant Entity_Id :=
8892 Defining_Identifier (Object_Decl);
8893 Enclosing_Func : constant Entity_Id :=
8894 Enclosing_Subprogram (Obj_Def_Id);
8895 Call_Deref : Node_Id;
8896 Caller_Object : Node_Id;
8897 Def_Id : Entity_Id;
8898 Fmaster_Actual : Node_Id := Empty;
8899 Func_Call : Node_Id := Function_Call;
8900 Function_Id : Entity_Id;
8901 Pool_Actual : Node_Id;
8902 Ptr_Typ : Entity_Id;
8903 Ptr_Typ_Decl : Node_Id;
8904 Pass_Caller_Acc : Boolean := False;
8905 Res_Decl : Node_Id;
8906 Result_Subt : Entity_Id;
8908 Definite : Boolean;
8909 -- True for definite function result subtype
8911 begin
8912 -- Step past qualification or unchecked conversion (the latter can occur
8913 -- in cases of calls to 'Input).
8915 if Nkind_In (Func_Call, N_Qualified_Expression,
8916 N_Unchecked_Type_Conversion)
8917 then
8918 Func_Call := Expression (Func_Call);
8919 end if;
8921 -- If the call has already been processed to add build-in-place actuals
8922 -- then return. This should not normally occur in an object declaration,
8923 -- but we add the protection as a defensive measure.
8925 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8926 return;
8927 end if;
8929 -- Mark the call as processed as a build-in-place call
8931 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8933 Loc := Sloc (Function_Call);
8935 if Is_Entity_Name (Name (Func_Call)) then
8936 Function_Id := Entity (Name (Func_Call));
8938 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8939 Function_Id := Etype (Name (Func_Call));
8941 else
8942 raise Program_Error;
8943 end if;
8945 Result_Subt := Etype (Function_Id);
8946 Definite := Is_Definite_Subtype (Underlying_Type (Result_Subt));
8948 -- Create an access type designating the function's result subtype. We
8949 -- use the type of the original call because it may be a call to an
8950 -- inherited operation, which the expansion has replaced with the parent
8951 -- operation that yields the parent type. Note that this access type
8952 -- must be declared before we establish a transient scope, so that it
8953 -- receives the proper accessibility level.
8955 Ptr_Typ := Make_Temporary (Loc, 'A');
8956 Ptr_Typ_Decl :=
8957 Make_Full_Type_Declaration (Loc,
8958 Defining_Identifier => Ptr_Typ,
8959 Type_Definition =>
8960 Make_Access_To_Object_Definition (Loc,
8961 All_Present => True,
8962 Subtype_Indication =>
8963 New_Occurrence_Of (Etype (Function_Call), Loc)));
8965 -- The access type and its accompanying object must be inserted after
8966 -- the object declaration in the constrained case, so that the function
8967 -- call can be passed access to the object. In the indefinite case,
8968 -- or if the object declaration is for a return object, the access type
8969 -- and object must be inserted before the object, since the object
8970 -- declaration is rewritten to be a renaming of a dereference of the
8971 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8972 -- the result object is in a different (transient) scope, so won't
8973 -- cause freezing.
8975 if Definite
8976 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8977 then
8978 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
8979 else
8980 Insert_Action (Object_Decl, Ptr_Typ_Decl);
8981 end if;
8983 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8984 -- elaborated in an inner (transient) scope and thus won't cause
8985 -- freezing by itself.
8987 declare
8988 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8989 New_Occurrence_Of (Ptr_Typ, Loc);
8990 begin
8991 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8992 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8993 end;
8995 -- If the object is a return object of an enclosing build-in-place
8996 -- function, then the implicit build-in-place parameters of the
8997 -- enclosing function are simply passed along to the called function.
8998 -- (Unfortunately, this won't cover the case of extension aggregates
8999 -- where the ancestor part is a build-in-place indefinite function
9000 -- call that should be passed along the caller's parameters. Currently
9001 -- those get mishandled by reassigning the result of the call to the
9002 -- aggregate return object, when the call result should really be
9003 -- directly built in place in the aggregate and not in a temporary. ???)
9005 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
9006 Pass_Caller_Acc := True;
9008 -- When the enclosing function has a BIP_Alloc_Form formal then we
9009 -- pass it along to the callee (such as when the enclosing function
9010 -- has an unconstrained or tagged result type).
9012 if Needs_BIP_Alloc_Form (Enclosing_Func) then
9013 if VM_Target = No_VM and then
9014 RTE_Available (RE_Root_Storage_Pool_Ptr)
9015 then
9016 Pool_Actual :=
9017 New_Occurrence_Of (Build_In_Place_Formal
9018 (Enclosing_Func, BIP_Storage_Pool), Loc);
9020 -- The build-in-place pool formal is not built on .NET/JVM
9022 else
9023 Pool_Actual := Empty;
9024 end if;
9026 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9027 (Func_Call,
9028 Function_Id,
9029 Alloc_Form_Exp =>
9030 New_Occurrence_Of
9031 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
9032 Loc),
9033 Pool_Actual => Pool_Actual);
9035 -- Otherwise, if enclosing function has a definite result subtype,
9036 -- then caller allocation will be used.
9038 else
9039 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9040 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9041 end if;
9043 if Needs_BIP_Finalization_Master (Enclosing_Func) then
9044 Fmaster_Actual :=
9045 New_Occurrence_Of
9046 (Build_In_Place_Formal
9047 (Enclosing_Func, BIP_Finalization_Master), Loc);
9048 end if;
9050 -- Retrieve the BIPacc formal from the enclosing function and convert
9051 -- it to the access type of the callee's BIP_Object_Access formal.
9053 Caller_Object :=
9054 Make_Unchecked_Type_Conversion (Loc,
9055 Subtype_Mark =>
9056 New_Occurrence_Of
9057 (Etype
9058 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
9059 Loc),
9060 Expression =>
9061 New_Occurrence_Of
9062 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
9063 Loc));
9065 -- In the definite case, add an implicit actual to the function call
9066 -- that provides access to the declared object. An unchecked conversion
9067 -- to the (specific) result type of the function is inserted to handle
9068 -- the case where the object is declared with a class-wide type.
9070 elsif Definite then
9071 Caller_Object :=
9072 Make_Unchecked_Type_Conversion (Loc,
9073 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9074 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
9076 -- When the function has a controlling result, an allocation-form
9077 -- parameter must be passed indicating that the caller is allocating
9078 -- the result object. This is needed because such a function can be
9079 -- called as a dispatching operation and must be treated similarly
9080 -- to functions with indefinite result subtypes.
9082 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9083 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9085 -- In other indefinite cases, pass an indication to do the allocation
9086 -- on the secondary stack and set Caller_Object to Empty so that a null
9087 -- value will be passed for the caller's object address. A transient
9088 -- scope is established to ensure eventual cleanup of the result.
9090 else
9091 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9092 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9093 Caller_Object := Empty;
9095 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
9096 end if;
9098 -- Pass along any finalization master actual, which is needed in the
9099 -- case where the called function initializes a return object of an
9100 -- enclosing build-in-place function.
9102 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9103 (Func_Call => Func_Call,
9104 Func_Id => Function_Id,
9105 Master_Exp => Fmaster_Actual);
9107 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
9108 and then Has_Task (Result_Subt)
9109 then
9110 -- Here we're passing along the master that was passed in to this
9111 -- function.
9113 Add_Task_Actuals_To_Build_In_Place_Call
9114 (Func_Call, Function_Id,
9115 Master_Actual =>
9116 New_Occurrence_Of (Build_In_Place_Formal
9117 (Enclosing_Func, BIP_Task_Master), Loc));
9119 else
9120 Add_Task_Actuals_To_Build_In_Place_Call
9121 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9122 end if;
9124 Add_Access_Actual_To_Build_In_Place_Call
9125 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
9127 -- Finally, create an access object initialized to a reference to the
9128 -- function call. We know this access value cannot be null, so mark the
9129 -- entity accordingly to suppress the access check.
9131 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9132 Set_Etype (Def_Id, Ptr_Typ);
9133 Set_Is_Known_Non_Null (Def_Id);
9135 Res_Decl :=
9136 Make_Object_Declaration (Loc,
9137 Defining_Identifier => Def_Id,
9138 Constant_Present => True,
9139 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9140 Expression =>
9141 Make_Reference (Loc, Relocate_Node (Func_Call)));
9143 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9145 -- If the result subtype of the called function is definite and is not
9146 -- itself the return expression of an enclosing BIP function, then mark
9147 -- the object as having no initialization.
9149 if Definite
9150 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9151 then
9152 -- The related object declaration is encased in a transient block
9153 -- because the build-in-place function call contains at least one
9154 -- nested function call that produces a controlled transient
9155 -- temporary:
9157 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9159 -- Since the build-in-place expansion decouples the call from the
9160 -- object declaration, the finalization machinery lacks the context
9161 -- which prompted the generation of the transient block. To resolve
9162 -- this scenario, store the build-in-place call.
9164 if Scope_Is_Transient
9165 and then Node_To_Be_Wrapped = Object_Decl
9166 then
9167 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9168 end if;
9170 Set_Expression (Object_Decl, Empty);
9171 Set_No_Initialization (Object_Decl);
9173 -- In case of an indefinite result subtype, or if the call is the
9174 -- return expression of an enclosing BIP function, rewrite the object
9175 -- declaration as an object renaming where the renamed object is a
9176 -- dereference of <function_Call>'reference:
9178 -- Obj : Subt renames <function_call>'Ref.all;
9180 else
9181 Call_Deref :=
9182 Make_Explicit_Dereference (Loc,
9183 Prefix => New_Occurrence_Of (Def_Id, Loc));
9185 Loc := Sloc (Object_Decl);
9186 Rewrite (Object_Decl,
9187 Make_Object_Renaming_Declaration (Loc,
9188 Defining_Identifier => Make_Temporary (Loc, 'D'),
9189 Access_Definition => Empty,
9190 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9191 Name => Call_Deref));
9193 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
9195 Analyze (Object_Decl);
9197 -- Replace the internal identifier of the renaming declaration's
9198 -- entity with identifier of the original object entity. We also have
9199 -- to exchange the entities containing their defining identifiers to
9200 -- ensure the correct replacement of the object declaration by the
9201 -- object renaming declaration to avoid homograph conflicts (since
9202 -- the object declaration's defining identifier was already entered
9203 -- in current scope). The Next_Entity links of the two entities also
9204 -- have to be swapped since the entities are part of the return
9205 -- scope's entity list and the list structure would otherwise be
9206 -- corrupted. Finally, the homonym chain must be preserved as well.
9208 declare
9209 Renaming_Def_Id : constant Entity_Id :=
9210 Defining_Identifier (Object_Decl);
9211 Next_Entity_Temp : constant Entity_Id :=
9212 Next_Entity (Renaming_Def_Id);
9213 begin
9214 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
9216 -- Swap next entity links in preparation for exchanging entities
9218 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9219 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9220 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9222 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9224 -- Preserve source indication of original declaration, so that
9225 -- xref information is properly generated for the right entity.
9227 Preserve_Comes_From_Source
9228 (Object_Decl, Original_Node (Object_Decl));
9230 Preserve_Comes_From_Source
9231 (Obj_Def_Id, Original_Node (Object_Decl));
9233 Set_Comes_From_Source (Renaming_Def_Id, False);
9234 end;
9235 end if;
9237 -- If the object entity has a class-wide Etype, then we need to change
9238 -- it to the result subtype of the function call, because otherwise the
9239 -- object will be class-wide without an explicit initialization and
9240 -- won't be allocated properly by the back end. It seems unclean to make
9241 -- such a revision to the type at this point, and we should try to
9242 -- improve this treatment when build-in-place functions with class-wide
9243 -- results are implemented. ???
9245 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9246 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9247 end if;
9248 end Make_Build_In_Place_Call_In_Object_Declaration;
9250 --------------------------------------------
9251 -- Make_CPP_Constructor_Call_In_Allocator --
9252 --------------------------------------------
9254 procedure Make_CPP_Constructor_Call_In_Allocator
9255 (Allocator : Node_Id;
9256 Function_Call : Node_Id)
9258 Loc : constant Source_Ptr := Sloc (Function_Call);
9259 Acc_Type : constant Entity_Id := Etype (Allocator);
9260 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9261 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9263 New_Allocator : Node_Id;
9264 Return_Obj_Access : Entity_Id;
9265 Tmp_Obj : Node_Id;
9267 begin
9268 pragma Assert (Nkind (Allocator) = N_Allocator
9269 and then Nkind (Function_Call) = N_Function_Call);
9270 pragma Assert (Convention (Function_Id) = Convention_CPP
9271 and then Is_Constructor (Function_Id));
9272 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9274 -- Replace the initialized allocator of form "new T'(Func (...))" with
9275 -- an uninitialized allocator of form "new T", where T is the result
9276 -- subtype of the called function. The call to the function is handled
9277 -- separately further below.
9279 New_Allocator :=
9280 Make_Allocator (Loc,
9281 Expression => New_Occurrence_Of (Result_Subt, Loc));
9282 Set_No_Initialization (New_Allocator);
9284 -- Copy attributes to new allocator. Note that the new allocator
9285 -- logically comes from source if the original one did, so copy the
9286 -- relevant flag. This ensures proper treatment of the restriction
9287 -- No_Implicit_Heap_Allocations in this case.
9289 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9290 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9291 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9293 Rewrite (Allocator, New_Allocator);
9295 -- Create a new access object and initialize it to the result of the
9296 -- new uninitialized allocator. Note: we do not use Allocator as the
9297 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9298 -- as this would create a sort of infinite "recursion".
9300 Return_Obj_Access := Make_Temporary (Loc, 'R');
9301 Set_Etype (Return_Obj_Access, Acc_Type);
9303 -- Generate:
9304 -- Rnnn : constant ptr_T := new (T);
9305 -- Init (Rnn.all,...);
9307 Tmp_Obj :=
9308 Make_Object_Declaration (Loc,
9309 Defining_Identifier => Return_Obj_Access,
9310 Constant_Present => True,
9311 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9312 Expression => Relocate_Node (Allocator));
9313 Insert_Action (Allocator, Tmp_Obj);
9315 Insert_List_After_And_Analyze (Tmp_Obj,
9316 Build_Initialization_Call (Loc,
9317 Id_Ref =>
9318 Make_Explicit_Dereference (Loc,
9319 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9320 Typ => Etype (Function_Id),
9321 Constructor_Ref => Function_Call));
9323 -- Finally, replace the allocator node with a reference to the result of
9324 -- the function call itself (which will effectively be an access to the
9325 -- object created by the allocator).
9327 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9329 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9330 -- generate an implicit conversion to force displacement of the "this"
9331 -- pointer.
9333 if Is_Interface (Designated_Type (Acc_Type)) then
9334 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9335 end if;
9337 Analyze_And_Resolve (Allocator, Acc_Type);
9338 end Make_CPP_Constructor_Call_In_Allocator;
9340 -----------------------------------
9341 -- Needs_BIP_Finalization_Master --
9342 -----------------------------------
9344 function Needs_BIP_Finalization_Master
9345 (Func_Id : Entity_Id) return Boolean
9347 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9348 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9349 begin
9350 return
9351 not Restriction_Active (No_Finalization)
9352 and then Needs_Finalization (Func_Typ);
9353 end Needs_BIP_Finalization_Master;
9355 --------------------------
9356 -- Needs_BIP_Alloc_Form --
9357 --------------------------
9359 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9360 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9361 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9362 begin
9363 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9364 end Needs_BIP_Alloc_Form;
9366 --------------------------------------
9367 -- Needs_Result_Accessibility_Level --
9368 --------------------------------------
9370 function Needs_Result_Accessibility_Level
9371 (Func_Id : Entity_Id) return Boolean
9373 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9375 function Has_Unconstrained_Access_Discriminant_Component
9376 (Comp_Typ : Entity_Id) return Boolean;
9377 -- Returns True if any component of the type has an unconstrained access
9378 -- discriminant.
9380 -----------------------------------------------------
9381 -- Has_Unconstrained_Access_Discriminant_Component --
9382 -----------------------------------------------------
9384 function Has_Unconstrained_Access_Discriminant_Component
9385 (Comp_Typ : Entity_Id) return Boolean
9387 begin
9388 if not Is_Limited_Type (Comp_Typ) then
9389 return False;
9391 -- Only limited types can have access discriminants with
9392 -- defaults.
9394 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9395 return True;
9397 elsif Is_Array_Type (Comp_Typ) then
9398 return Has_Unconstrained_Access_Discriminant_Component
9399 (Underlying_Type (Component_Type (Comp_Typ)));
9401 elsif Is_Record_Type (Comp_Typ) then
9402 declare
9403 Comp : Entity_Id;
9405 begin
9406 Comp := First_Component (Comp_Typ);
9407 while Present (Comp) loop
9408 if Has_Unconstrained_Access_Discriminant_Component
9409 (Underlying_Type (Etype (Comp)))
9410 then
9411 return True;
9412 end if;
9414 Next_Component (Comp);
9415 end loop;
9416 end;
9417 end if;
9419 return False;
9420 end Has_Unconstrained_Access_Discriminant_Component;
9422 Feature_Disabled : constant Boolean := True;
9423 -- Temporary
9425 -- Start of processing for Needs_Result_Accessibility_Level
9427 begin
9428 -- False if completion unavailable (how does this happen???)
9430 if not Present (Func_Typ) then
9431 return False;
9433 elsif Feature_Disabled then
9434 return False;
9436 -- False if not a function, also handle enum-lit renames case
9438 elsif Func_Typ = Standard_Void_Type
9439 or else Is_Scalar_Type (Func_Typ)
9440 then
9441 return False;
9443 -- Handle a corner case, a cross-dialect subp renaming. For example,
9444 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9445 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9447 elsif Present (Alias (Func_Id)) then
9449 -- Unimplemented: a cross-dialect subp renaming which does not set
9450 -- the Alias attribute (e.g., a rename of a dereference of an access
9451 -- to subprogram value). ???
9453 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9455 -- Remaining cases require Ada 2012 mode
9457 elsif Ada_Version < Ada_2012 then
9458 return False;
9460 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9461 or else Is_Tagged_Type (Func_Typ)
9462 then
9463 -- In the case of, say, a null tagged record result type, the need
9464 -- for this extra parameter might not be obvious. This function
9465 -- returns True for all tagged types for compatibility reasons.
9466 -- A function with, say, a tagged null controlling result type might
9467 -- be overridden by a primitive of an extension having an access
9468 -- discriminant and the overrider and overridden must have compatible
9469 -- calling conventions (including implicitly declared parameters).
9470 -- Similarly, values of one access-to-subprogram type might designate
9471 -- both a primitive subprogram of a given type and a function
9472 -- which is, for example, not a primitive subprogram of any type.
9473 -- Again, this requires calling convention compatibility.
9474 -- It might be possible to solve these issues by introducing
9475 -- wrappers, but that is not the approach that was chosen.
9477 return True;
9479 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9480 return True;
9482 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9483 return True;
9485 -- False for all other cases
9487 else
9488 return False;
9489 end if;
9490 end Needs_Result_Accessibility_Level;
9492 ------------------------
9493 -- Unnest_Subprograms --
9494 ------------------------
9496 procedure Unnest_Subprograms is
9497 begin
9498 for J in Unest_Bodies.First .. Unest_Bodies.Last loop
9499 declare
9500 UBJ : Unest_Entry renames Unest_Bodies.Table (J);
9501 begin
9502 Unnest_Subprogram (UBJ.Ent, UBJ.Bod);
9503 end;
9504 end loop;
9505 end Unnest_Subprograms;
9507 end Exp_Ch6;